Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 06d41e8

Browse files
diesersamatmithun-mondal
authored andcommitted
[quick_actions] Implementing sharedpreferences approach for killed apps (flutter#1800)
* Implementing "intent extras" approach instead of shared preferences as suggested * Added some tests Method renaming Added ios placeholder for getLaunchAction method
1 parent 1532688 commit 06d41e8

7 files changed

Lines changed: 63 additions & 6 deletions

File tree

packages/quick_actions/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.3.2
2+
3+
* Fixed the quick actions launch on Android when the app is killed.
4+
15
## 0.3.1
26

37
* Added unit tests.
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="io.flutter.plugins.quickactions">
2+
xmlns:tools="http://schemas.android.com/tools"
3+
package="io.flutter.plugins.quickactions">
34

4-
<application>
5-
<activity android:name=".QuickActionsPlugin$ShortcutHandlerActivity" android:exported="false"/>
6-
</application>
5+
<application>
6+
<activity
7+
android:name=".QuickActionsPlugin$ShortcutHandlerActivity"
8+
android:exported="false"
9+
android:relinquishTaskIdentity="true"
10+
tools:targetApi="lollipop" />
11+
</application>
712
</manifest>

packages/quick_actions/android/src/main/java/io/flutter/plugins/quickactions/QuickActionsPlugin.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
@SuppressWarnings("unchecked")
2727
public class QuickActionsPlugin implements MethodCallHandler {
2828
private final Registrar registrar;
29+
private static final String intentExtraAction = "action";
30+
private static String launchAction = null;
2931

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

5053
@Override
@@ -68,6 +71,10 @@ public void onMethodCall(MethodCall call, Result result) {
6871
case "clearShortcutItems":
6972
shortcutManager.removeAllDynamicShortcuts();
7073
break;
74+
case "getLaunchAction":
75+
result.success(launchAction);
76+
launchAction = null;
77+
return;
7178
default:
7279
result.notImplemented();
7380
return;
@@ -117,8 +124,27 @@ protected void onCreate(Bundle savedInstanceState) {
117124
String type = intent.getStringExtra("type");
118125
if (channel != null) {
119126
channel.invokeMethod("launch", type);
127+
} else {
128+
startActivity(getIntentToOpenMainActivity(this, type));
120129
}
121130
finish();
122131
}
132+
133+
/**
134+
* Returns Intent to launch the MainActivity. Used to start the app, if one of quick actions was
135+
* called from the background.
136+
*/
137+
private Intent getIntentToOpenMainActivity(Context context, String type) {
138+
Intent launchIntentForPackage =
139+
context
140+
.getPackageManager()
141+
.getLaunchIntentForPackage(context.getApplicationContext().getPackageName());
142+
if (launchIntentForPackage == null) {
143+
return null;
144+
} else {
145+
launchIntentForPackage.putExtra(intentExtraAction, type);
146+
return launchIntentForPackage;
147+
}
148+
}
123149
}
124150
}

packages/quick_actions/ios/Classes/QuickActionsPlugin.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
2828
} else if ([call.method isEqualToString:@"clearShortcutItems"]) {
2929
[UIApplication sharedApplication].shortcutItems = @[];
3030
result(nil);
31+
} else if ([call.method isEqualToString:@"getLaunchAction"]) {
32+
result(nil);
3133
} else {
3234
result(FlutterMethodNotImplemented);
3335
}

packages/quick_actions/lib/quick_actions.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,19 @@ class QuickActions {
4949
/// Initializes this plugin.
5050
///
5151
/// Call this once before any further interaction with the the plugin.
52-
void initialize(QuickActionHandler handler) {
52+
void initialize(QuickActionHandler handler) async {
5353
channel.setMethodCallHandler((MethodCall call) async {
5454
assert(call.method == 'launch');
5555
handler(call.arguments);
5656
});
57+
runLaunchAction(handler);
58+
}
59+
60+
void runLaunchAction(QuickActionHandler handler) async {
61+
final String action = await channel.invokeMethod<String>('getLaunchAction');
62+
if (action != null) {
63+
handler(action);
64+
}
5765
}
5866

5967
/// Sets the [ShortcutItem]s to become the app's quick actions.

packages/quick_actions/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for creating shortcuts on home screen, also known as
33
Quick Actions on iOS and App Shortcuts on Android.
44
author: Flutter Team <flutter-dev@googlegroups.com>
55
homepage: https://github.com/flutter/plugins/tree/master/packages/quick_actions
6-
version: 0.3.1
6+
version: 0.3.2
77

88
flutter:
99
plugin:

packages/quick_actions/test/quick_actions_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,17 @@ void main() {
5454
isMethodCall('clearShortcutItems', arguments: null),
5555
],
5656
);
57+
log.clear();
58+
});
59+
60+
test('runLaunchAction', () {
61+
quickActions.runLaunchAction(null);
62+
expect(
63+
log,
64+
<Matcher>[
65+
isMethodCall('getLaunchAction', arguments: null),
66+
],
67+
);
68+
log.clear();
5769
});
5870
}

0 commit comments

Comments
 (0)