Skip to content

Commit f8ee0fe

Browse files
committed
flutter/android asset #4
1 parent 76e50c0 commit f8ee0fe

File tree

7 files changed

+39
-41
lines changed

7 files changed

+39
-41
lines changed

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

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -281,20 +281,23 @@ private ShortcutInfo createShortcutInfo(Map<String, String> shortcut) {
281281
final String action = shortcut.get("action");
282282
final String shortLabel = shortcut.get("shortLabel");
283283
final String longLabel = shortcut.get("LongLabel");
284+
final int iconType = Integer.parseInt(shortcut.get("shortcutIconType"));
284285
final ShortcutInfo.Builder shortcutBuilder;
285286
shortcutBuilder = new ShortcutInfo.Builder(context, id);
286287

287-
// final int resourceId = loadResourceId(context, icon);
288288
final Intent intent = getIntentToOpenMainActivity(action);
289289

290-
// if (resourceId > 0) {
291-
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
292-
// shortcutBuilder.setIcon(Icon.createWithResource(context, resourceId));
293-
// }
294-
// }
295-
296-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
297-
shortcutBuilder.setIcon(getIconFromFlutterAsset(context,icon));
290+
// 0 - ShortcutIconType.androidAsset
291+
// 1 - ShortcutIconType.flutterAsset
292+
switch (iconType) {
293+
case 0:
294+
setIconFromNative(shortcutBuilder, icon);
295+
break;
296+
case 1:
297+
setIconFromFlutter(shortcutBuilder, icon);
298+
break;
299+
default:
300+
break;
298301
}
299302

300303
if(longLabel != null) {
@@ -308,6 +311,22 @@ private ShortcutInfo createShortcutInfo(Map<String, String> shortcut) {
308311
.build();
309312
}
310313

314+
315+
private void setIconFromNative(ShortcutInfo.Builder shortcutBuilder, String icon) {
316+
final int resourceId = loadResourceId(context, icon);
317+
if (resourceId > 0) {
318+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
319+
shortcutBuilder.setIcon(Icon.createWithResource(context, resourceId));
320+
}
321+
}
322+
}
323+
324+
private void setIconFromFlutter(ShortcutInfo.Builder shortcutBuilder, String icon) {
325+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
326+
shortcutBuilder.setIcon(getIconFromFlutterAsset(context,icon));
327+
}
328+
}
329+
311330
@RequiresApi(api = Build.VERSION_CODES.O)
312331
private Icon getIconFromFlutterAsset(Context context, String path) {
313332
AssetManager assetManager = context.getAssets();

example/lib/main.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ class _MyAppState extends State<MyApp> {
8686
id: "2",
8787
action: 'Bookmark page action',
8888
shortLabel: 'Bookmark Page',
89-
icon: 'assets/icons/bookmark.png',
89+
// icon: 'assets/icons/bookmark.png',
90+
icon: "ic_launcher",
91+
shortcutIconAsset: ShortcutIconAsset.androidAsset,
9092
),
9193
],
9294
).then((value) {

lib/flutter_shortcuts.dart

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,6 @@ class FlutterShortcuts {
7272
return FlutterShortcutsPlatform.instance.updateShortcutItem(shortcut);
7373
}
7474

75-
// /// [updateShortLabel] updates short label of the shortcut item based on id.
76-
// /// If the ID of the shortcut is not same, no changes will be reflected.
77-
// Future<void> updateShortLabel({String id, String shortLabel}) async {
78-
// return FlutterShortcutsPlatform.instance.updateShortLabel(id, shortLabel);
79-
// }
80-
81-
// /// [updateLongLabel] updates long label of the shortcut item based on id.
82-
// /// If the ID of the shortcut is not same, no changes will be reflected.
83-
// Future<void> updateLongLabel({String id, String longLabel}) async {
84-
// return FlutterShortcutsPlatform.instance.updateLongLabel(id, longLabel);
85-
// }
86-
8775
/// [changeShortcutItemIcon] will change the icon of the shortcut based on id.
8876
/// If the ID of the shortcut is not same, no changes will be reflected.
8977
Future<void> changeShortcutItemIcon(

lib/src/helper/helper.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ for more details.
1010

1111
export 'action/flutter_shortcut_action.dart';
1212
export 'model/flutter_shortcut_item.dart';
13+
export 'enums/shortcut_icon_asset.dart';

lib/src/helper/model/flutter_shortcut_item.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ for more details.
88
99
*/
1010

11+
import '../enums/shortcut_icon_asset.dart';
12+
1113
class FlutterShortcutItem {
1214
/// Create a flutter shortcut item.
1315
/// Eg.
@@ -34,6 +36,7 @@ class FlutterShortcutItem {
3436
required this.id,
3537
required this.action,
3638
required this.shortLabel,
39+
this.shortcutIconAsset = ShortcutIconAsset.flutterAsset,
3740
this.longLabel,
3841
this.icon,
3942
});
@@ -52,4 +55,7 @@ class FlutterShortcutItem {
5255

5356
/// Flutter asset path. Only Supports image files. Eg. .png/.jpg
5457
final String? icon;
58+
59+
/// `ShortcutIconType.native` or `ShortcutIconType.flutterAsset`
60+
final ShortcutIconAsset shortcutIconAsset;
5561
}

lib/src/method_call/flutter_shortcuts_method_call_handler.dart

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,6 @@ class FlutterShortcutsMethodCallHandler extends FlutterShortcutsPlatform {
8080
await channel.invokeMethod<void>('updateShortcutItem', [item]);
8181
}
8282

83-
// @override
84-
// Future<void> updateShortLabel(String id, String shortLabel) async {
85-
// await channel
86-
// .invokeMethod<void>('changeShortcutItemIcon', [id, shortLabel]);
87-
// }
88-
89-
// @override
90-
// Future<void> updateLongLabel(String id, String longLabel) async {
91-
// await channel.invokeMethod<void>('changeShortcutItemIcon', [id, longLabel]);
92-
// }
93-
9483
@override
9584
Future<void> changeShortcutItemIcon(String id, String icon) async {
9685
await channel.invokeMethod<void>('changeShortcutItemIcon', [id, icon]);
@@ -103,6 +92,7 @@ class FlutterShortcutsMethodCallHandler extends FlutterShortcutsPlatform {
10392
'shortLabel': item.shortLabel,
10493
'longLabel': item.longLabel,
10594
'icon': item.icon,
95+
'shortcutIconType': item.shortcutIconAsset.index.toString(),
10696
};
10797
}
10898
}

lib/src/platform/flutter_shortcuts_platform.dart

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,6 @@ abstract class FlutterShortcutsPlatform extends PlatformInterface {
6464
throw UnimplementedError("updateShortcutItem() has not been implemented.");
6565
}
6666

67-
// Future<void> updateShortLabel(String id, String shortLabel) async {
68-
// throw UnimplementedError("updateShortcutItem() has not been implemented.");
69-
// }
70-
71-
// Future<void> updateLongLabel(String id, String longLabel) async {
72-
// throw UnimplementedError("updateShortcutItem() has not been implemented.");
73-
// }
74-
7567
Future<void> changeShortcutItemIcon(String id, String icon) async {
7668
throw UnimplementedError(
7769
"changeShortcutItemIcon() has not been implemented.");

0 commit comments

Comments
 (0)