Skip to content

Commit

Permalink
Merge pull request #7 from divshekhar/dev
Browse files Browse the repository at this point in the history
label api discarded
  • Loading branch information
divshekhar authored May 30, 2021
2 parents 5a4d67c + 8f4e040 commit 752d6e6
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 67 deletions.
2 changes: 1 addition & 1 deletion android/.project
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</natures>
<filteredResources>
<filter>
<id>1621653958543</id>
<id>0</id>
<name></name>
<type>30</type>
<matcher>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
case "updateShortcutItem":
updateShortcutItem(call);
break;
// case "changeShortcutItemLabel":
// changeShortcutItemLabel(call);
// break;
case "changeShortcutItemIcon":
changeShortcutItemIcon(call);
break;
Expand Down Expand Up @@ -204,14 +207,20 @@ private void updateShortcutItems(MethodCall call) {
private void updateShortcutItem(MethodCall call) {
final List<Map<String, String>> args = call.arguments();
Map<String, String> info = args.get(0);
final String refId = info.get("id");
final String id = info.get("id");
final String icon = info.get("icon");
final String action = info.get("action");
final String shortLabel = info.get("shortLabel");
final String longLabel = info.get("LongLabel");
final int iconType = Integer.parseInt(Objects.requireNonNull(info.get("shortcutIconType")));

List<ShortcutInfoCompat> dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(context);
final List<ShortcutInfoCompat> shortcutList = new ArrayList<>();
int flag = 1;
for(ShortcutInfoCompat si : dynamicShortcuts) {
if(si.getId().equalsIgnoreCase(refId)) {
ShortcutInfoCompat shortcutInfo = buildShortcutUsingCompat(info);
shortcutList.add(shortcutInfo);
if(si.getId().equals(id)) {
ShortcutInfoCompat.Builder shortcutInfo = buildShortcutUsingCompat(id,icon,action,shortLabel,longLabel,iconType);
shortcutList.add(shortcutInfo.build());
flag = 0;
continue;
}
Expand All @@ -229,24 +238,86 @@ private void updateShortcutItem(MethodCall call) {
}
}

// @SuppressLint("RestrictedApi")
// @RequiresApi(api = Build.VERSION_CODES.O)
// private void changeShortcutItemLabel(MethodCall call) {
// try {
// final List<String> args = call.arguments();
// final String refId = args.get(0);
// final String shortLabel = args.get(1);
// final String longLabel = args.get(2);
//
//// Icon icon = getIconFromFlutterAsset(context,"assets/icons/next.png");
//// IconCompat iconCompat = IconCompat.createFromIcon(context,icon);
//
// List<ShortcutInfoCompat> dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(context);
// final List<ShortcutInfoCompat> shortcutList = new ArrayList<>();
//
// int flag = 1;
// for(ShortcutInfoCompat si : dynamicShortcuts) {
// String id = si.getId();
// if(id.equals(refId)) {
//
// ShortcutInfoCompat.Builder shortcutInfo = buildShortcutUsingCompat(id,null,null,null,null,0);
// shortcutInfo.setIntent(si.getIntent());
//
// if(shortLabel != null ) {
// shortcutInfo.setShortLabel("Changed Label");
// } else {
// shortcutInfo.setShortLabel(si.getShortLabel());
// }
//
// if(longLabel != null) {
// shortcutInfo.setLongLabel(longLabel);
// } else if(si.getLongLabel() != null) {
// shortcutInfo.setLongLabel(si.getLongLabel());
// }
//
// shortcutInfo.setIcon(si.getIcon());
//
// shortcutList.add(shortcutInfo.build());
// flag = 0;
// continue;
// }
// shortcutList.add(si);
// }
// if (flag == 1) {
// Log.e(TAG, "ID did not match any shortcut");
// return;
// }
// try {
// ShortcutManagerCompat.updateShortcuts(context,shortcutList);
// debugPrint("Shortcut Label Changed.");
// } catch(Exception e) {
// Log.e(TAG,e.toString());
// }
// } catch(Exception e) {
// Log.e(TAG,e.toString());
// }
// }

@SuppressLint("RestrictedApi")
@RequiresApi(api = Build.VERSION_CODES.O)
private void changeShortcutItemIcon(MethodCall call) {
try {
final List<String> args = call.arguments();
final String refId = args.get(0);
final String changeIcon = args.get(1);
Map<String,String> items = shortcutWithoutIcon(refId);
ShortcutInfoCompat.Builder shortcutInfo = createShortcutInfoUsingIconResID(items);
Icon icon = getIconFromFlutterAsset(context,changeIcon);
IconCompat iconCompat = IconCompat.createFromIcon(context,icon);
List<ShortcutInfoCompat> dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(context);

final List<ShortcutInfoCompat> shortcutList = new ArrayList<>();
int flag = 1;
for(ShortcutInfoCompat si : dynamicShortcuts) {
if(si.getId().equalsIgnoreCase(refId)) {
IconCompat iconCompat = IconCompat.createFromIcon(context,icon);
shortcutList.add(shortcutInfo.setIcon(iconCompat).build());
String id = si.getId();
if(id.equals(refId)) {
String shortLabel = (String) si.getShortLabel();
String longLabel = (String) si.getLongLabel();

ShortcutInfoCompat.Builder shortcutInfo = buildShortcutUsingCompat(id,null,null,shortLabel,longLabel,0);
shortcutInfo.setIcon(iconCompat).setIntent(si.getIntent());
shortcutList.add(shortcutInfo.build());
flag = 0;
continue;
}
Expand All @@ -270,79 +341,48 @@ private void changeShortcutItemIcon(MethodCall call) {

/* ******************** Utility Functions ********************* */

@SuppressLint("RestrictedApi")
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private Map<String,String> shortcutWithoutIcon(String id) {
HashMap<String, String> map = new HashMap<String, String>();
List<ShortcutInfoCompat> dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(context);
for(ShortcutInfoCompat si : dynamicShortcuts) {
if(si.getId().equalsIgnoreCase(id)) {
map.put("id", si.getId());
map.put("shortLabel", (String) si.getShortLabel());
map.put("longLabel", (String) si.getLongLabel());
map.put("action",si.getIntent().getStringExtra(EXTRA_ACTION));
}
}
return map;
}

@SuppressLint("RestrictedApi")
private ShortcutInfoCompat.Builder createShortcutInfoUsingIconResID(Map<String, String> shortcut) {
final String id = shortcut.get("id");
final String action = shortcut.get("action");
final String shortLabel = shortcut.get("shortLabel");
final String longLabel = shortcut.get("LongLabel");

assert id != null;
ShortcutInfoCompat.Builder shortcutInfoCompat = new ShortcutInfoCompat.Builder(context, id);

final Intent intent = getIntentToOpenMainActivity(action);

if(longLabel != null) {
shortcutInfoCompat.setLongLabel(longLabel);
}

return shortcutInfoCompat
.setShortLabel(shortLabel)
.setIntent(intent);

}

@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private List<ShortcutInfoCompat> shortcutInfoCompatList(List<Map<String, String>> shortcuts) {
final List<ShortcutInfoCompat> shortcutList = new ArrayList<>();

for (Map<String, String> shortcut : shortcuts) {
ShortcutInfoCompat shortcutInfoCompat = buildShortcutUsingCompat(shortcut);
shortcutList.add(shortcutInfoCompat);
final String id = shortcut.get("id");
final String icon = shortcut.get("icon");
final String action = shortcut.get("action");
final String shortLabel = shortcut.get("shortLabel");
final String longLabel = shortcut.get("LongLabel");
final int iconType = Integer.parseInt(Objects.requireNonNull(shortcut.get("shortcutIconType")));
ShortcutInfoCompat.Builder shortcutInfoCompat = buildShortcutUsingCompat(id,icon,action,shortLabel,longLabel,iconType);
shortcutList.add(shortcutInfoCompat.build());
}
return shortcutList;
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private ShortcutInfoCompat buildShortcutUsingCompat(Map<String, String> shortcut) {
final String id = shortcut.get("id");
final String icon = shortcut.get("icon");
final String action = shortcut.get("action");
final String shortLabel = shortcut.get("shortLabel");
final String longLabel = shortcut.get("LongLabel");
final int iconType = Integer.parseInt(Objects.requireNonNull(shortcut.get("shortcutIconType")));
private ShortcutInfoCompat.Builder buildShortcutUsingCompat(String id, String icon, String action, String shortLabel, String longLabel, int iconType) {

assert id != null;
ShortcutInfoCompat.Builder shortcutInfoCompat = new ShortcutInfoCompat.Builder(context, id);

final Intent intent = getIntentToOpenMainActivity(action);
if(action != null) {
Intent intent;
intent = getIntentToOpenMainActivity(action);
shortcutInfoCompat.setIntent(intent);
}

if(longLabel != null) {
shortcutInfoCompat.setLongLabel(longLabel);
}
setIconCompat(iconType, icon,shortcutInfoCompat);

assert shortLabel!=null;
return shortcutInfoCompat
.setShortLabel(shortLabel)
.setIntent(intent)
.build();
if(icon != null) {
setIconCompat(iconType, icon, shortcutInfoCompat);
}

if(shortLabel != null) {
shortcutInfoCompat.setShortLabel(shortLabel);
}

return shortcutInfoCompat;
}

private void setIconCompat(int iconType,String icon,ShortcutInfoCompat.Builder shortcutBuilderCompat) {
Expand Down
24 changes: 24 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,30 @@ class _MyAppState extends State<MyApp> {
],
),
Divider(),
// Text("Label"),
// Row(
// mainAxisAlignment: MainAxisAlignment.spaceEvenly,
// children: [
// ElevatedButton(
// child: Text("Change ShortLabel"),
// onPressed: () async {
// await flutterShortcuts.changeShortcutItemLabel(
// id: "1",
// shortLabel: "Resume Game",
// );
// },
// ),
// ElevatedButton(
// child: Text("Change longLabel"),
// onPressed: () async {
// await flutterShortcuts.changeShortcutItemLabel(
// id: "2",
// longLabel: "Long label for shortcut item with id '2'",
// );
// },
// ),
// ],
// ),
],
),
),
Expand Down
9 changes: 9 additions & 0 deletions lib/flutter_shortcuts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ class FlutterShortcuts {
return FlutterShortcutsPlatform.instance.updateShortcutItem(shortcut);
}

// Future<void> changeShortcutItemLabel({
// required String id,
// String? shortLabel,
// String? longLabel,
// }) async {
// return FlutterShortcutsPlatform.instance
// .changeShortcutItemLabel(id, shortLabel, 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
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ class FlutterShortcutsMethodCallHandler extends FlutterShortcutsPlatform {
await channel.invokeMethod<void>('updateShortcutItem', [item]);
}

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

@override
Future<void> changeShortcutItemIcon(String id, String icon) async {
await channel.invokeMethod<void>('changeShortcutItemIcon', [id, icon]);
Expand Down
6 changes: 6 additions & 0 deletions lib/src/platform/flutter_shortcuts_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ abstract class FlutterShortcutsPlatform extends PlatformInterface {
throw UnimplementedError("updateShortcutItem() has not been implemented.");
}

// Future<void> changeShortcutItemLabel(
// String id, String? shortLabel, String? longLabel) async {
// throw UnimplementedError(
// "changeShortcutItemShortLabel() has not been implemented.");
// }

Future<void> changeShortcutItemIcon(String id, String icon) async {
throw UnimplementedError(
"changeShortcutItemIcon() has not been implemented.");
Expand Down
6 changes: 3 additions & 3 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
version: "2.6.1"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -106,7 +106,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -141,7 +141,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
version: "0.3.0"
typed_data:
dependency: transitive
description:
Expand Down

0 comments on commit 752d6e6

Please sign in to comment.