Skip to content

Commit 5d7a7e3

Browse files
committed
fix the things!
1 parent 2272ef6 commit 5d7a7e3

File tree

8 files changed

+139
-180
lines changed

8 files changed

+139
-180
lines changed

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

Lines changed: 35 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,7 @@ public void setLocalizedTitle(@NonNull String setterArg) {
9090
this.localizedTitle = setterArg;
9191
}
9292

93-
/**
94-
* Name of native resource (xcassets etc; NOT a Flutter asset) to be displayed as the icon for
95-
* this item.
96-
*/
93+
/** Name of native resource to be displayed as the icon for this item. */
9794
private @Nullable String icon;
9895

9996
public @Nullable String getIcon() {
@@ -160,13 +157,6 @@ ArrayList<Object> toList() {
160157
}
161158
}
162159

163-
public interface Result<T> {
164-
@SuppressWarnings("UnknownNullness")
165-
void success(T result);
166-
167-
void error(@NonNull Throwable error);
168-
}
169-
170160
private static class AndroidQuickActionsApiCodec extends StandardMessageCodec {
171161
public static final AndroidQuickActionsApiCodec INSTANCE = new AndroidQuickActionsApiCodec();
172162

@@ -195,13 +185,13 @@ protected void writeValue(@NonNull ByteArrayOutputStream stream, Object value) {
195185

196186
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
197187
public interface AndroidQuickActionsApi {
198-
199-
void getLaunchAction(@NonNull Result<String> result);
200-
201-
void setShortcutItems(
202-
@NonNull List<ShortcutItemMessage> itemsList, @NonNull Result<Void> result);
203-
204-
void clearShortcutItems(@NonNull Result<Void> result);
188+
/** Checks for, and returns the action that launched the app. */
189+
@Nullable
190+
String getLaunchAction();
191+
/** Sets the dynamic shortcuts for the app. */
192+
void setShortcutItems(@NonNull List<ShortcutItemMessage> itemsList);
193+
/** Removes all dynamic shortcuts. */
194+
void clearShortcutItems();
205195

206196
/** The codec used by AndroidQuickActionsApi. */
207197
static @NonNull MessageCodec<Object> getCodec() {
@@ -223,20 +213,14 @@ static void setup(
223213
channel.setMessageHandler(
224214
(message, reply) -> {
225215
ArrayList<Object> wrapped = new ArrayList<Object>();
226-
Result<String> resultCallback =
227-
new Result<String>() {
228-
public void success(String result) {
229-
wrapped.add(0, result);
230-
reply.reply(wrapped);
231-
}
232-
233-
public void error(Throwable error) {
234-
ArrayList<Object> wrappedError = wrapError(error);
235-
reply.reply(wrappedError);
236-
}
237-
};
238-
239-
api.getLaunchAction(resultCallback);
216+
try {
217+
String output = api.getLaunchAction();
218+
wrapped.add(0, output);
219+
} catch (Throwable exception) {
220+
ArrayList<Object> wrappedError = wrapError(exception);
221+
wrapped = wrappedError;
222+
}
223+
reply.reply(wrapped);
240224
});
241225
} else {
242226
channel.setMessageHandler(null);
@@ -254,20 +238,14 @@ public void error(Throwable error) {
254238
ArrayList<Object> wrapped = new ArrayList<Object>();
255239
ArrayList<Object> args = (ArrayList<Object>) message;
256240
List<ShortcutItemMessage> itemsListArg = (List<ShortcutItemMessage>) args.get(0);
257-
Result<Void> resultCallback =
258-
new Result<Void>() {
259-
public void success(Void result) {
260-
wrapped.add(0, null);
261-
reply.reply(wrapped);
262-
}
263-
264-
public void error(Throwable error) {
265-
ArrayList<Object> wrappedError = wrapError(error);
266-
reply.reply(wrappedError);
267-
}
268-
};
269-
270-
api.setShortcutItems(itemsListArg, resultCallback);
241+
try {
242+
api.setShortcutItems(itemsListArg);
243+
wrapped.add(0, null);
244+
} catch (Throwable exception) {
245+
ArrayList<Object> wrappedError = wrapError(exception);
246+
wrapped = wrappedError;
247+
}
248+
reply.reply(wrapped);
271249
});
272250
} else {
273251
channel.setMessageHandler(null);
@@ -283,20 +261,14 @@ public void error(Throwable error) {
283261
channel.setMessageHandler(
284262
(message, reply) -> {
285263
ArrayList<Object> wrapped = new ArrayList<Object>();
286-
Result<Void> resultCallback =
287-
new Result<Void>() {
288-
public void success(Void result) {
289-
wrapped.add(0, null);
290-
reply.reply(wrapped);
291-
}
292-
293-
public void error(Throwable error) {
294-
ArrayList<Object> wrappedError = wrapError(error);
295-
reply.reply(wrappedError);
296-
}
297-
};
298-
299-
api.clearShortcutItems(resultCallback);
264+
try {
265+
api.clearShortcutItems();
266+
wrapped.add(0, null);
267+
} catch (Throwable exception) {
268+
ArrayList<Object> wrappedError = wrapError(exception);
269+
wrapped = wrappedError;
270+
}
271+
reply.reply(wrapped);
300272
});
301273
} else {
302274
channel.setMessageHandler(null);
@@ -321,12 +293,12 @@ public interface Reply<T> {
321293
static @NonNull MessageCodec<Object> getCodec() {
322294
return new StandardMessageCodec();
323295
}
324-
325-
public void handleCall(@NonNull String actionArg, @NonNull Reply<Void> callback) {
296+
/** Sends a string representing a shortcut from the native platform to the app. */
297+
public void launchAction(@NonNull String actionArg, @NonNull Reply<Void> callback) {
326298
BasicMessageChannel<Object> channel =
327299
new BasicMessageChannel<>(
328300
binaryMessenger,
329-
"dev.flutter.pigeon.quick_actions_android.AndroidQuickActionsFlutterApi.handleCall",
301+
"dev.flutter.pigeon.quick_actions_android.AndroidQuickActionsFlutterApi.launchAction",
330302
getCodec());
331303
channel.send(
332304
new ArrayList<Object>(Collections.singletonList(actionArg)),

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

Lines changed: 66 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
import android.os.Looper;
1818
import androidx.annotation.ChecksSdkIntAtLeast;
1919
import androidx.annotation.NonNull;
20+
import androidx.annotation.Nullable;
2021
import io.flutter.plugins.quickactions.Messages.AndroidQuickActionsApi;
2122
import io.flutter.plugins.quickactions.Messages.FlutterError;
22-
import io.flutter.plugins.quickactions.Messages.Result;
2323
import io.flutter.plugins.quickactions.Messages.ShortcutItemMessage;
2424
import java.util.ArrayList;
2525
import java.util.List;
@@ -48,89 +48,87 @@ public Activity getActivity() {
4848

4949
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.N_MR1)
5050
boolean isVersionAllowed() {
51-
// We already know that this functionality does not work for anything
52-
// lower than API 25 so we chose not to return error. Instead we do nothing.
5351
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1;
5452
}
5553

5654
@Override
57-
public void setShortcutItems(
58-
@NonNull List<ShortcutItemMessage> itemsList, @NonNull Result<Void> result) {
59-
if (isVersionAllowed()) {
60-
ShortcutManager shortcutManager =
61-
(ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
62-
List<ShortcutInfo> shortcuts = ShortcutItemMessageToShortcutInfo(itemsList);
63-
Executor uiThreadExecutor = new UiThreadExecutor();
64-
ThreadPoolExecutor executor =
65-
new ThreadPoolExecutor(0, 1, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
66-
67-
executor.execute(
68-
() -> {
69-
boolean dynamicShortcutsSet = false;
70-
try {
71-
shortcutManager.setDynamicShortcuts(shortcuts);
72-
dynamicShortcutsSet = true;
73-
} catch (Exception e) {
74-
// Leave dynamicShortcutsSet as false
75-
}
76-
77-
final boolean didSucceed = dynamicShortcutsSet;
78-
79-
// TODO(camsim99): Move re-dispatch below to background thread when Flutter 2.8+ is
80-
// stable.
81-
uiThreadExecutor.execute(
82-
() -> {
83-
if (didSucceed) {
84-
result.success(null);
85-
} else {
86-
result.error(
87-
new FlutterError(
88-
"quick_action_setshortcutitems_failure",
89-
"Exception thrown when setting dynamic shortcuts",
90-
null));
91-
}
92-
});
93-
});
55+
public void setShortcutItems(@NonNull List<ShortcutItemMessage> itemsList) {
56+
// We already know that this functionality does not work for anything
57+
// lower than API 25 so we chose not to return error. Instead we do nothing.
58+
if (!isVersionAllowed()) {
59+
return;
9460
}
95-
return;
61+
ShortcutManager shortcutManager =
62+
(ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
63+
List<ShortcutInfo> shortcuts = shortcutItemMessageToShortcutInfo(itemsList);
64+
Executor uiThreadExecutor = new UiThreadExecutor();
65+
ThreadPoolExecutor executor =
66+
new ThreadPoolExecutor(0, 1, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
67+
68+
executor.execute(
69+
() -> {
70+
boolean dynamicShortcutsSet = false;
71+
try {
72+
shortcutManager.setDynamicShortcuts(shortcuts);
73+
dynamicShortcutsSet = true;
74+
} catch (Exception e) {
75+
// Leave dynamicShortcutsSet as false
76+
}
77+
78+
final boolean didSucceed = dynamicShortcutsSet;
79+
80+
// TODO(camsim99): Move re-dispatch below to background thread when Flutter 2.8+ is
81+
// stable.
82+
uiThreadExecutor.execute(
83+
() -> {
84+
if (!didSucceed) {
85+
throw new FlutterError(
86+
"quick_action_setshortcutitems_failure",
87+
"Exception thrown when setting dynamic shortcuts",
88+
null);
89+
}
90+
});
91+
});
9692
}
9793

9894
@Override
99-
public void clearShortcutItems(@NonNull Result<Void> result) {
100-
if (isVersionAllowed()) {
101-
ShortcutManager shortcutManager =
102-
(ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
103-
shortcutManager.removeAllDynamicShortcuts();
104-
result.success(null);
95+
public void clearShortcutItems() {
96+
// We already know that this functionality does not work for anything
97+
// lower than API 25 so we chose not to return error. Instead we do nothing.
98+
if (!isVersionAllowed()) {
99+
return;
105100
}
101+
ShortcutManager shortcutManager =
102+
(ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
103+
shortcutManager.removeAllDynamicShortcuts();
106104
}
107105

108106
@Override
109-
public void getLaunchAction(@NonNull Result<String> result) {
110-
if (isVersionAllowed()) {
111-
ShortcutManager shortcutManager =
112-
(ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
113-
if (activity == null) {
114-
result.error(
115-
new FlutterError(
116-
"quick_action_getlaunchaction_no_activity",
117-
"There is no activity available when launching action",
118-
null));
119-
return;
120-
}
121-
final Intent intent = activity.getIntent();
122-
final String launchAction = intent.getStringExtra(EXTRA_ACTION);
123-
if (launchAction != null && !launchAction.isEmpty()) {
124-
shortcutManager.reportShortcutUsed(launchAction);
125-
intent.removeExtra(EXTRA_ACTION);
126-
}
127-
result.success(launchAction);
107+
public @Nullable String getLaunchAction() {
108+
// We already know that this functionality does not work for anything
109+
// lower than API 25 so we chose not to return error. Instead we do nothing.
110+
if (!isVersionAllowed()) {
111+
return null;
112+
}
113+
ShortcutManager shortcutManager =
114+
(ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
115+
if (activity == null) {
116+
throw new FlutterError(
117+
"quick_action_getlaunchaction_no_activity",
118+
"There is no activity available when launching action",
119+
null);
120+
}
121+
final Intent intent = activity.getIntent();
122+
final String launchAction = intent.getStringExtra(EXTRA_ACTION);
123+
if (launchAction != null && !launchAction.isEmpty()) {
124+
shortcutManager.reportShortcutUsed(launchAction);
125+
intent.removeExtra(EXTRA_ACTION);
128126
}
129-
return;
127+
return launchAction;
130128
}
131129

132130
@TargetApi(Build.VERSION_CODES.N_MR1)
133-
private List<ShortcutInfo> ShortcutItemMessageToShortcutInfo(
131+
private List<ShortcutInfo> shortcutItemMessageToShortcutInfo(
134132
@NonNull List<ShortcutItemMessage> shortcuts) {
135133
final List<ShortcutInfo> shortcutInfos = new ArrayList<>();
136134

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

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
6565

6666
@Override
6767
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
68-
if (quickActions == null) {
69-
Log.wtf(TAG, "Already detached from the engine.");
70-
return;
71-
}
72-
7368
Messages.AndroidQuickActionsApi.setup(binding.getBinaryMessenger(), null);
7469
this.quickActions = null;
7570
}
@@ -89,11 +84,6 @@ public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
8984

9085
@Override
9186
public void onDetachedFromActivity() {
92-
if (this.quickActions == null) {
93-
Log.wtf(TAG, "quickActions was never set.");
94-
return;
95-
}
96-
9787
quickActions.setActivity(null);
9888
}
9989

@@ -114,18 +104,17 @@ public boolean onNewIntent(@NonNull Intent intent) {
114104
if (!sdkChecker.sdkIsAtLeast(Build.VERSION_CODES.N_MR1)) {
115105
return false;
116106
}
107+
Activity activity = this.quickActions.getActivity();
117108
// Notify the Dart side if the launch intent has the intent extra relevant to quick actions.
118-
if (intent.hasExtra(QuickActions.EXTRA_ACTION)) {
119-
Context context = this.quickActions.getActivity().getApplicationContext();
109+
if (intent.hasExtra(QuickActions.EXTRA_ACTION) && activity != null) {
110+
Context context = activity.getApplicationContext();
120111
ShortcutManager shortcutManager =
121112
(ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
122113
String shortcutId = intent.getStringExtra(QuickActions.EXTRA_ACTION);
123-
quickActionsFlutterApi.handleCall(
114+
quickActionsFlutterApi.launchAction(
124115
shortcutId,
125-
new AndroidQuickActionsFlutterApi.Reply<Void>() {
126-
public void reply(Void value) {
127-
// noop
128-
}
116+
value -> {
117+
// noop
129118
});
130119
shortcutManager.reportShortcutUsed(shortcutId);
131120
}

0 commit comments

Comments
 (0)