Skip to content

Commit 5a4d67c

Browse files
committed
change icon api fixed
1 parent 9549062 commit 5a4d67c

File tree

2 files changed

+61
-31
lines changed

2 files changed

+61
-31
lines changed

android/src/main/java/com/divyanshushekhar/flutter_shortcuts/MethodCallImplementation.java

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.divyanshushekhar.flutter_shortcuts;
22

3+
import android.annotation.SuppressLint;
34
import android.app.Activity;
45
import android.content.Context;
56
import android.content.Intent;
@@ -20,8 +21,6 @@
2021

2122
import java.io.IOException;
2223
import java.util.ArrayList;
23-
import java.util.Arrays;
24-
import java.util.Collections;
2524
import java.util.HashMap;
2625
import java.util.List;
2726
import java.util.Map;
@@ -56,6 +55,7 @@ void setActivity(Activity activity) {
5655
this.activity = activity;
5756
}
5857

58+
@RequiresApi(api = Build.VERSION_CODES.O)
5959
@Override
6060
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
6161
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
@@ -96,6 +96,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
9696
break;
9797
case "clearShortcutItems":
9898
ShortcutManagerCompat.removeAllDynamicShortcuts(context);
99+
debugPrint("Removed all shortcuts.");
99100
break;
100101
default:
101102
result.notImplemented();
@@ -228,20 +229,24 @@ private void updateShortcutItem(MethodCall call) {
228229
}
229230
}
230231

231-
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
232+
@SuppressLint("RestrictedApi")
233+
@RequiresApi(api = Build.VERSION_CODES.O)
232234
private void changeShortcutItemIcon(MethodCall call) {
233235
try {
234236
final List<String> args = call.arguments();
235237
final String refId = args.get(0);
236238
final String changeIcon = args.get(1);
237-
Map<String,String> items = deserializeShortcutInfoAtId(refId,changeIcon);
238-
ShortcutInfoCompat shortcutInfo = buildShortcutUsingCompat(items);
239+
Map<String,String> items = shortcutWithoutIcon(refId);
240+
ShortcutInfoCompat.Builder shortcutInfo = createShortcutInfoUsingIconResID(items);
241+
Icon icon = getIconFromFlutterAsset(context,changeIcon);
239242
List<ShortcutInfoCompat> dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(context);
243+
240244
final List<ShortcutInfoCompat> shortcutList = new ArrayList<>();
241245
int flag = 1;
242246
for(ShortcutInfoCompat si : dynamicShortcuts) {
243247
if(si.getId().equalsIgnoreCase(refId)) {
244-
shortcutList.add(shortcutInfo);
248+
IconCompat iconCompat = IconCompat.createFromIcon(context,icon);
249+
shortcutList.add(shortcutInfo.setIcon(iconCompat).build());
245250
flag = 0;
246251
continue;
247252
}
@@ -262,21 +267,47 @@ private void changeShortcutItemIcon(MethodCall call) {
262267
}
263268
}
264269

270+
271+
/* ******************** Utility Functions ********************* */
272+
273+
@SuppressLint("RestrictedApi")
265274
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
266-
private Map<String,String> deserializeShortcutInfoAtId(String id, String icon) {
275+
private Map<String,String> shortcutWithoutIcon(String id) {
267276
HashMap<String, String> map = new HashMap<String, String>();
268277
List<ShortcutInfoCompat> dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(context);
269278
for(ShortcutInfoCompat si : dynamicShortcuts) {
270279
if(si.getId().equalsIgnoreCase(id)) {
271280
map.put("id", si.getId());
272-
map.put("shortLabel", String.valueOf(si.getShortLabel()));
273-
map.put("icon", icon);
281+
map.put("shortLabel", (String) si.getShortLabel());
282+
map.put("longLabel", (String) si.getLongLabel());
274283
map.put("action",si.getIntent().getStringExtra(EXTRA_ACTION));
275284
}
276285
}
277286
return map;
278287
}
279288

289+
@SuppressLint("RestrictedApi")
290+
private ShortcutInfoCompat.Builder createShortcutInfoUsingIconResID(Map<String, String> shortcut) {
291+
final String id = shortcut.get("id");
292+
final String action = shortcut.get("action");
293+
final String shortLabel = shortcut.get("shortLabel");
294+
final String longLabel = shortcut.get("LongLabel");
295+
296+
assert id != null;
297+
ShortcutInfoCompat.Builder shortcutInfoCompat = new ShortcutInfoCompat.Builder(context, id);
298+
299+
final Intent intent = getIntentToOpenMainActivity(action);
300+
301+
if(longLabel != null) {
302+
shortcutInfoCompat.setLongLabel(longLabel);
303+
}
304+
305+
return shortcutInfoCompat
306+
.setShortLabel(shortLabel)
307+
.setIntent(intent);
308+
309+
}
310+
280311
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
281312
private List<ShortcutInfoCompat> shortcutInfoCompatList(List<Map<String, String>> shortcuts) {
282313
final List<ShortcutInfoCompat> shortcutList = new ArrayList<>();
@@ -297,7 +328,6 @@ private ShortcutInfoCompat buildShortcutUsingCompat(Map<String, String> shortcut
297328
final String longLabel = shortcut.get("LongLabel");
298329
final int iconType = Integer.parseInt(Objects.requireNonNull(shortcut.get("shortcutIconType")));
299330

300-
301331
assert id != null;
302332
ShortcutInfoCompat.Builder shortcutInfoCompat = new ShortcutInfoCompat.Builder(context, id);
303333

@@ -312,7 +342,6 @@ private ShortcutInfoCompat buildShortcutUsingCompat(Map<String, String> shortcut
312342
return shortcutInfoCompat
313343
.setShortLabel(shortLabel)
314344
.setIntent(intent)
315-
.addCapabilityBinding("actions.intent.OPEN_APP_FEATURE")
316345
.build();
317346
}
318347

example/lib/main.dart

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -157,26 +157,27 @@ class _MyAppState extends State<MyApp> {
157157
child: Text("Add Shortcut"),
158158
onPressed: () async {
159159
await flutterShortcuts.pushShortcutItems(
160-
shortcutList: <FlutterShortcutItem>[
161-
const FlutterShortcutItem(
162-
id: "1",
163-
action: 'Home page new action',
164-
shortLabel: 'Home Page',
165-
icon: 'assets/icons/home.png',
166-
),
167-
const FlutterShortcutItem(
168-
id: "2",
169-
action: 'Bookmark page new action',
170-
shortLabel: 'Bookmark Page',
171-
icon: 'assets/icons/bookmark.png',
172-
),
173-
const FlutterShortcutItem(
174-
id: "3",
175-
action: 'Settings Action',
176-
shortLabel: 'Setting',
177-
icon: 'assets/icons/settings.png',
178-
),
179-
]);
160+
shortcutList: <FlutterShortcutItem>[
161+
const FlutterShortcutItem(
162+
id: "1",
163+
action: 'Home page new action',
164+
shortLabel: 'Home Page',
165+
icon: 'assets/icons/home.png',
166+
),
167+
const FlutterShortcutItem(
168+
id: "2",
169+
action: 'Bookmark page new action',
170+
shortLabel: 'Bookmark Page',
171+
icon: 'assets/icons/bookmark.png',
172+
),
173+
const FlutterShortcutItem(
174+
id: "3",
175+
action: 'Settings Action',
176+
shortLabel: 'Setting',
177+
icon: 'assets/icons/settings.png',
178+
),
179+
],
180+
);
180181
},
181182
),
182183
ElevatedButton(

0 commit comments

Comments
 (0)