diff --git a/packages/quick_actions/CHANGELOG.md b/packages/quick_actions/CHANGELOG.md index 328199d6c5b8..4251ad7fd347 100644 --- a/packages/quick_actions/CHANGELOG.md +++ b/packages/quick_actions/CHANGELOG.md @@ -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. diff --git a/packages/quick_actions/android/src/main/AndroidManifest.xml b/packages/quick_actions/android/src/main/AndroidManifest.xml index ed26d787b3e6..26daac952d28 100644 --- a/packages/quick_actions/android/src/main/AndroidManifest.xml +++ b/packages/quick_actions/android/src/main/AndroidManifest.xml @@ -1,7 +1,12 @@ + xmlns:tools="http://schemas.android.com/tools" + package="io.flutter.plugins.quickactions"> - - - + + + diff --git a/packages/quick_actions/android/src/main/java/io/flutter/plugins/quickactions/QuickActionsPlugin.java b/packages/quick_actions/android/src/main/java/io/flutter/plugins/quickactions/QuickActionsPlugin.java index f4ef7267a520..6fb276af0934 100644 --- a/packages/quick_actions/android/src/main/java/io/flutter/plugins/quickactions/QuickActionsPlugin.java +++ b/packages/quick_actions/android/src/main/java/io/flutter/plugins/quickactions/QuickActionsPlugin.java @@ -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 @@ -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 @@ -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; @@ -117,8 +124,27 @@ protected void onCreate(Bundle savedInstanceState) { String type = intent.getStringExtra("type"); if (channel != null) { 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; + } + } } } diff --git a/packages/quick_actions/ios/Classes/QuickActionsPlugin.m b/packages/quick_actions/ios/Classes/QuickActionsPlugin.m index f1ff0e41d20d..8f83cc4d9cd2 100644 --- a/packages/quick_actions/ios/Classes/QuickActionsPlugin.m +++ b/packages/quick_actions/ios/Classes/QuickActionsPlugin.m @@ -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); } diff --git a/packages/quick_actions/lib/quick_actions.dart b/packages/quick_actions/lib/quick_actions.dart index 7b60df586319..f240968eb8f5 100644 --- a/packages/quick_actions/lib/quick_actions.dart +++ b/packages/quick_actions/lib/quick_actions.dart @@ -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('getLaunchAction'); + if (action != null) { + handler(action); + } } /// Sets the [ShortcutItem]s to become the app's quick actions. diff --git a/packages/quick_actions/pubspec.yaml b/packages/quick_actions/pubspec.yaml index 3887c597c837..a6d8b791f39a 100644 --- a/packages/quick_actions/pubspec.yaml +++ b/packages/quick_actions/pubspec.yaml @@ -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 homepage: https://github.com/flutter/plugins/tree/master/packages/quick_actions -version: 0.3.1 +version: 0.3.2 flutter: plugin: diff --git a/packages/quick_actions/test/quick_actions_test.dart b/packages/quick_actions/test/quick_actions_test.dart index 91f500af7fe8..115efc5786aa 100644 --- a/packages/quick_actions/test/quick_actions_test.dart +++ b/packages/quick_actions/test/quick_actions_test.dart @@ -54,5 +54,17 @@ void main() { isMethodCall('clearShortcutItems', arguments: null), ], ); + log.clear(); + }); + + test('runLaunchAction', () { + quickActions.runLaunchAction(null); + expect( + log, + [ + isMethodCall('getLaunchAction', arguments: null), + ], + ); + log.clear(); }); }