Skip to content

Commit

Permalink
flutter/android asset #4
Browse files Browse the repository at this point in the history
  • Loading branch information
divshekhar committed May 25, 2021
1 parent 76e50c0 commit f8ee0fe
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,20 +281,23 @@ private ShortcutInfo createShortcutInfo(Map<String, String> shortcut) {
final String action = shortcut.get("action");
final String shortLabel = shortcut.get("shortLabel");
final String longLabel = shortcut.get("LongLabel");
final int iconType = Integer.parseInt(shortcut.get("shortcutIconType"));
final ShortcutInfo.Builder shortcutBuilder;
shortcutBuilder = new ShortcutInfo.Builder(context, id);

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

// if (resourceId > 0) {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
// shortcutBuilder.setIcon(Icon.createWithResource(context, resourceId));
// }
// }

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
shortcutBuilder.setIcon(getIconFromFlutterAsset(context,icon));
// 0 - ShortcutIconType.androidAsset
// 1 - ShortcutIconType.flutterAsset
switch (iconType) {
case 0:
setIconFromNative(shortcutBuilder, icon);
break;
case 1:
setIconFromFlutter(shortcutBuilder, icon);
break;
default:
break;
}

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


private void setIconFromNative(ShortcutInfo.Builder shortcutBuilder, String icon) {
final int resourceId = loadResourceId(context, icon);
if (resourceId > 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
shortcutBuilder.setIcon(Icon.createWithResource(context, resourceId));
}
}
}

private void setIconFromFlutter(ShortcutInfo.Builder shortcutBuilder, String icon) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
shortcutBuilder.setIcon(getIconFromFlutterAsset(context,icon));
}
}

@RequiresApi(api = Build.VERSION_CODES.O)
private Icon getIconFromFlutterAsset(Context context, String path) {
AssetManager assetManager = context.getAssets();
Expand Down
4 changes: 3 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ class _MyAppState extends State<MyApp> {
id: "2",
action: 'Bookmark page action',
shortLabel: 'Bookmark Page',
icon: 'assets/icons/bookmark.png',
// icon: 'assets/icons/bookmark.png',
icon: "ic_launcher",
shortcutIconAsset: ShortcutIconAsset.androidAsset,
),
],
).then((value) {
Expand Down
12 changes: 0 additions & 12 deletions lib/flutter_shortcuts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,6 @@ class FlutterShortcuts {
return FlutterShortcutsPlatform.instance.updateShortcutItem(shortcut);
}

// /// [updateShortLabel] updates short label of the shortcut item based on id.
// /// If the ID of the shortcut is not same, no changes will be reflected.
// Future<void> updateShortLabel({String id, String shortLabel}) async {
// return FlutterShortcutsPlatform.instance.updateShortLabel(id, shortLabel);
// }

// /// [updateLongLabel] updates long label of the shortcut item based on id.
// /// If the ID of the shortcut is not same, no changes will be reflected.
// Future<void> updateLongLabel({String id, String longLabel}) async {
// return FlutterShortcutsPlatform.instance.updateLongLabel(id, longLabel);
// }

/// [changeShortcutItemIcon] will change the icon of the shortcut based on id.
/// If the ID of the shortcut is not same, no changes will be reflected.
Future<void> changeShortcutItemIcon(
Expand Down
1 change: 1 addition & 0 deletions lib/src/helper/helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ for more details.

export 'action/flutter_shortcut_action.dart';
export 'model/flutter_shortcut_item.dart';
export 'enums/shortcut_icon_asset.dart';
6 changes: 6 additions & 0 deletions lib/src/helper/model/flutter_shortcut_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ for more details.
*/

import '../enums/shortcut_icon_asset.dart';

class FlutterShortcutItem {
/// Create a flutter shortcut item.
/// Eg.
Expand All @@ -34,6 +36,7 @@ class FlutterShortcutItem {
required this.id,
required this.action,
required this.shortLabel,
this.shortcutIconAsset = ShortcutIconAsset.flutterAsset,
this.longLabel,
this.icon,
});
Expand All @@ -52,4 +55,7 @@ class FlutterShortcutItem {

/// Flutter asset path. Only Supports image files. Eg. .png/.jpg
final String? icon;

/// `ShortcutIconType.native` or `ShortcutIconType.flutterAsset`
final ShortcutIconAsset shortcutIconAsset;
}
12 changes: 1 addition & 11 deletions lib/src/method_call/flutter_shortcuts_method_call_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,6 @@ class FlutterShortcutsMethodCallHandler extends FlutterShortcutsPlatform {
await channel.invokeMethod<void>('updateShortcutItem', [item]);
}

// @override
// Future<void> updateShortLabel(String id, String shortLabel) async {
// await channel
// .invokeMethod<void>('changeShortcutItemIcon', [id, shortLabel]);
// }

// @override
// Future<void> updateLongLabel(String id, String longLabel) async {
// await channel.invokeMethod<void>('changeShortcutItemIcon', [id, longLabel]);
// }

@override
Future<void> changeShortcutItemIcon(String id, String icon) async {
await channel.invokeMethod<void>('changeShortcutItemIcon', [id, icon]);
Expand All @@ -103,6 +92,7 @@ class FlutterShortcutsMethodCallHandler extends FlutterShortcutsPlatform {
'shortLabel': item.shortLabel,
'longLabel': item.longLabel,
'icon': item.icon,
'shortcutIconType': item.shortcutIconAsset.index.toString(),
};
}
}
8 changes: 0 additions & 8 deletions lib/src/platform/flutter_shortcuts_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@ abstract class FlutterShortcutsPlatform extends PlatformInterface {
throw UnimplementedError("updateShortcutItem() has not been implemented.");
}

// Future<void> updateShortLabel(String id, String shortLabel) async {
// throw UnimplementedError("updateShortcutItem() has not been implemented.");
// }

// Future<void> updateLongLabel(String id, String longLabel) async {
// throw UnimplementedError("updateShortcutItem() has not been implemented.");
// }

Future<void> changeShortcutItemIcon(String id, String icon) async {
throw UnimplementedError(
"changeShortcutItemIcon() has not been implemented.");
Expand Down

0 comments on commit f8ee0fe

Please sign in to comment.