Skip to content

Commit c840e38

Browse files
authored
[quick_actions_android] Return null from getLaunchAction when no activity is attached (flutter#12331)
`getLaunchAction` threw `quick_action_getlaunchaction_no_activity` when the plugin had no attached activity. `QuickActionsAndroid.initialize` calls `getLaunchAction` unconditionally, so `initialize` threw a `PlatformException` that callers could not prevent. An engine can legitimately run without an attached activity. A cached engine warmed up by a background service is the common case: `audio_service` supplies a cached engine to its activity and also warms up that same engine from `AudioService.onCreate`, so `main()` runs with no activity when a media button or Android Auto starts the service. This returns `null` in that case instead, and logs at debug level. The throw provided no recovery value: * `initialize` registers the Dart handler before it calls `getLaunchAction`, so the handler is already installed when the exception is thrown. * When an activity does attach, `QuickActionsPlugin.onAttachedToActivity` calls `onNewIntent(activity.getIntent())`, which reports the shortcut through `AndroidQuickActionsFlutterApi.launchAction`. `null` is also already the return value for the other "cannot answer" case in this method, on the line directly above (`if (!isVersionAllowed())`). Verified on an Android 12 emulator by pointing an app at this branch and starting its audio service with no UI: no exception, and a shortcut tap afterwards still routed correctly. A cold start from a shortcut still returns the action, so the activity-present path is unchanged. The new unit test fails with `io.flutter.plugins.quickactions.FlutterError` against the unmodified file. Fixes flutter#190348 ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 7660d46 commit c840e38

4 files changed

Lines changed: 29 additions & 6 deletions

File tree

‎packages/quick_actions/quick_actions_android/CHANGELOG.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
## NEXT
1+
## 1.0.34
22

33
* Updates minimum supported SDK version to Flutter 3.41/Dart 3.11.
4+
* Fixes `getLaunchAction` throwing when there is no attached activity.
45

56
## 1.0.33
67

‎packages/quick_actions/quick_actions_android/android/src/main/java/io/flutter/plugins/quickactions/QuickActions.java‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import android.content.Intent;
1212
import android.content.res.Resources;
1313
import android.os.Build;
14+
import android.util.Log;
1415
import androidx.annotation.ChecksSdkIntAtLeast;
1516
import androidx.annotation.NonNull;
1617
import androidx.annotation.Nullable;
@@ -28,6 +29,7 @@
2829
import org.jetbrains.annotations.NotNull;
2930

3031
final class QuickActions implements AndroidQuickActionsApi {
32+
private static final String TAG = "QuickActionsAndroid";
3133
static final String EXTRA_ACTION = "some unique action key";
3234

3335
private final Context context;
@@ -102,10 +104,12 @@ public void clearShortcutItems() {
102104
return null;
103105
}
104106
if (activity == null) {
105-
throw new FlutterError(
106-
"quick_action_getlaunchaction_no_activity",
107-
"There is no activity available when launching action",
108-
null);
107+
// The engine can run without an attached activity, for instance when it is cached and warmed
108+
// up by a background service. Nothing launched the app in that case, so there is no launch
109+
// action to report. Any action that arrives later is reported through the plugin's
110+
// onNewIntent listener once an activity attaches.
111+
Log.d(TAG, "There is no activity available when getting the launch action.");
112+
return null;
109113
}
110114
final Intent intent = activity.getIntent();
111115
final String launchAction = intent.getStringExtra(EXTRA_ACTION);

‎packages/quick_actions/quick_actions_android/android/src/test/java/io/flutter/plugins/quickactions/QuickActionsTest.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
import static io.flutter.plugins.quickactions.QuickActions.EXTRA_ACTION;
88
import static org.junit.Assert.assertFalse;
9+
import static org.junit.Assert.assertNull;
910
import static org.junit.Assert.assertTrue;
11+
import static org.mockito.Mockito.doReturn;
1012
import static org.mockito.Mockito.mock;
13+
import static org.mockito.Mockito.spy;
1114
import static org.mockito.Mockito.when;
1215

1316
import android.app.Activity;
@@ -50,6 +53,21 @@ public void setMessageHandler(@NonNull String channel, @Nullable BinaryMessageHa
5053
static final int UNSUPPORTED_BUILD = 24;
5154
static final String SHORTCUT_TYPE = "action_one";
5255

56+
@Test
57+
public void getLaunchAction_noActivity_returnsNull() {
58+
// Arrange
59+
// Build.VERSION.SDK_INT is 0 in unit tests, so the version check is stubbed to make sure the
60+
// null activity, and not the unsupported version, is what is being exercised here.
61+
final QuickActions quickActions = spy(new QuickActions(mock(Context.class)));
62+
doReturn(true).when(quickActions).isVersionAllowed();
63+
64+
// Act
65+
final String launchAction = quickActions.getLaunchAction();
66+
67+
// Assert
68+
assertNull(launchAction);
69+
}
70+
5371
@Test
5472
public void canAttachToEngine() {
5573
final TestBinaryMessenger testBinaryMessenger = new TestBinaryMessenger();

‎packages/quick_actions/quick_actions_android/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: quick_actions_android
22
description: An implementation for the Android platform of the Flutter `quick_actions` plugin.
33
repository: https://github.com/flutter/packages/tree/main/packages/quick_actions/quick_actions_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+in_app_purchase%22
5-
version: 1.0.33
5+
version: 1.0.34
66

77
environment:
88
sdk: ^3.12.0

0 commit comments

Comments
 (0)