Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/quick_actions/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.2

* Fixed the quick actions launch on Android when the app is killed.

## 0.3.1

* Added unit tests.
Expand Down
13 changes: 9 additions & 4 deletions packages/quick_actions/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.flutter.plugins.quickactions">
xmlns:tools="http://schemas.android.com/tools"
package="io.flutter.plugins.quickactions">

<application>
<activity android:name=".QuickActionsPlugin$ShortcutHandlerActivity" android:exported="false"/>
</application>
<application>
<activity
android:name=".QuickActionsPlugin$ShortcutHandlerActivity"
android:exported="false"
android:relinquishTaskIdentity="true"
tools:targetApi="lollipop" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious, what is this for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need tools:targetApi to hide a warning in android studio regarding android:relinquishTaskIdentity — the previous one. We can use targetApi — lollipop as the plugin anyways works on devices from API at least 25. Also, in case, if the app is killed and we open it using the shortcut, it will work fine but only the first time. If you click the Home button after that and use the same shortcut again it will not work the second and following times without android:relinquishTaskIdentity.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried android:noHistory="true" ? As android:relinquishTaskIdentity="true" works only on API +25, all users in APIs below that will not have the same behavior.

Copy link
Contributor Author

@diesersamat diesersamat Jul 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly the same API version Android shortcuts and its classes were added ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh shortcuts are API 25+. So there is no problem.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will wait the merge to make another PR. I found that some more changes are needed. Sorry for the trouble.

</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
@SuppressWarnings("unchecked")
public class QuickActionsPlugin implements MethodCallHandler {
private final Registrar registrar;
private static final String intentExtraAction = "action";
private static String launchAction = null;

// Channel is a static field because it needs to be accessible to the
// {@link ShortcutHandlerActivity} which has to be a static class with
Expand All @@ -45,6 +47,7 @@ private QuickActionsPlugin(Registrar registrar) {
public static void registerWith(Registrar registrar) {
channel = new MethodChannel(registrar.messenger(), "plugins.flutter.io/quick_actions");
channel.setMethodCallHandler(new QuickActionsPlugin(registrar));
launchAction = registrar.activity().getIntent().getStringExtra(intentExtraAction);
}

@Override
Expand All @@ -68,6 +71,10 @@ public void onMethodCall(MethodCall call, Result result) {
case "clearShortcutItems":
shortcutManager.removeAllDynamicShortcuts();
break;
case "getLaunchAction":
result.success(launchAction);
launchAction = null;
return;
default:
result.notImplemented();
return;
Expand Down Expand Up @@ -117,8 +124,27 @@ protected void onCreate(Bundle savedInstanceState) {
String type = intent.getStringExtra("type");
if (channel != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran your code and this channel check will make the opening fail when channel != null.
Steps to reproduce:

  1. Install and open the app normally.
  2. Minimize the app by tapping in the home button.
  3. Use the app shortcut.
  4. Exit the app using the back button. ## Not the home button.
  5. Use the app shortcut. ## "nothing happens".

Possible fix that is not a breaking change:
In ShortcutHandlerActivity class :

      // Get the Intent that started this activity and extract the string
      Intent intent = getIntent();
      String type = intent.getStringExtra("type");
      startActivity(getIntentToOpenMainActivity(this, type));

      finish();
        launchIntentForPackage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        launchIntentForPackage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

Copy link
Contributor Author

@diesersamat diesersamat Jul 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I know about this issue and it isn't something I wanted to fix in this PR. If you can, please, check the current version of the plugin and make sure that the bug was there before my fix. I don't know if I should fix it in this PR, maybe you can open an issue or even submit a PR? Or do you think that it's better to fix the bug in this PR? Anyway, I will check your solution, because I also faced this issue and was thinking about the ways to fix it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@collinjackson ☝️ ? Another PR? It seems small enough to land together with this version.

Copy link
Contributor Author

@diesersamat diesersamat Jul 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BugsBunnyBR I know why does this happen. When you click the "Back" button, the activity is being killed. So, the activity is killed now, but channel reference and QuickActionsPlugin class instance is still alive. So, later, when you click on the shortcut, it tries to send a message to the channel, because it is not null — here is the difference with the bug fixed in this PR. But as you know, the view is already detached, and you'll get only this message in logcat FlutterView.send called on a detached view, channel=. Unfortunately, you cannot put a callback there to react on this error case and, maybe, start the activity again, because of API design. I've opened an issue a week ago flutter/flutter#35628 but maybe it is possible to fix without changes in FlutterNativeView API design...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I know how to solve the problem. Just remove the static for the channel. In general, having static fields that are associated with context are a bad ideia. But removing the static makes that we need some other few changes, like listening to onNewIntent, that is why I think another pr will be needed to have a proper handling.

channel.invokeMethod("launch", type);
} else {
startActivity(getIntentToOpenMainActivity(this, type));
}
finish();
}

/**
* Returns Intent to launch the MainActivity. Used to start the app, if one of quick actions was
* called from the background.
*/
private Intent getIntentToOpenMainActivity(Context context, String type) {
Intent launchIntentForPackage =
context
.getPackageManager()
.getLaunchIntentForPackage(context.getApplicationContext().getPackageName());
if (launchIntentForPackage == null) {
return null;
} else {
launchIntentForPackage.putExtra(intentExtraAction, type);
return launchIntentForPackage;
}
}
}
}
2 changes: 2 additions & 0 deletions packages/quick_actions/ios/Classes/QuickActionsPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
} else if ([call.method isEqualToString:@"clearShortcutItems"]) {
[UIApplication sharedApplication].shortcutItems = @[];
result(nil);
} else if ([call.method isEqualToString:@"getLaunchAction"]) {
result(nil);
} else {
result(FlutterMethodNotImplemented);
}
Expand Down
10 changes: 9 additions & 1 deletion packages/quick_actions/lib/quick_actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,19 @@ class QuickActions {
/// Initializes this plugin.
///
/// Call this once before any further interaction with the the plugin.
void initialize(QuickActionHandler handler) {
void initialize(QuickActionHandler handler) async {
channel.setMethodCallHandler((MethodCall call) async {
assert(call.method == 'launch');
handler(call.arguments);
});
runLaunchAction(handler);
}

void runLaunchAction(QuickActionHandler handler) async {
final String action = await channel.invokeMethod<String>('getLaunchAction');
if (action != null) {
handler(action);
}
}

/// Sets the [ShortcutItem]s to become the app's quick actions.
Expand Down
2 changes: 1 addition & 1 deletion packages/quick_actions/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for creating shortcuts on home screen, also known as
Quick Actions on iOS and App Shortcuts on Android.
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/quick_actions
version: 0.3.1
version: 0.3.2

flutter:
plugin:
Expand Down
12 changes: 12 additions & 0 deletions packages/quick_actions/test/quick_actions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,17 @@ void main() {
isMethodCall('clearShortcutItems', arguments: null),
],
);
log.clear();
});

test('runLaunchAction', () {
quickActions.runLaunchAction(null);
expect(
log,
<Matcher>[
isMethodCall('getLaunchAction', arguments: null),
],
);
log.clear();
});
}