Skip to content

Commit 752d6e6

Browse files
authored
Merge pull request #7 from divshekhar/dev
label api discarded
2 parents 5a4d67c + 8f4e040 commit 752d6e6

File tree

7 files changed

+153
-67
lines changed

7 files changed

+153
-67
lines changed

android/.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</natures>
2323
<filteredResources>
2424
<filter>
25-
<id>1621653958543</id>
25+
<id>0</id>
2626
<name></name>
2727
<type>30</type>
2828
<matcher>

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

Lines changed: 103 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
9191
case "updateShortcutItem":
9292
updateShortcutItem(call);
9393
break;
94+
// case "changeShortcutItemLabel":
95+
// changeShortcutItemLabel(call);
96+
// break;
9497
case "changeShortcutItemIcon":
9598
changeShortcutItemIcon(call);
9699
break;
@@ -204,14 +207,20 @@ private void updateShortcutItems(MethodCall call) {
204207
private void updateShortcutItem(MethodCall call) {
205208
final List<Map<String, String>> args = call.arguments();
206209
Map<String, String> info = args.get(0);
207-
final String refId = info.get("id");
210+
final String id = info.get("id");
211+
final String icon = info.get("icon");
212+
final String action = info.get("action");
213+
final String shortLabel = info.get("shortLabel");
214+
final String longLabel = info.get("LongLabel");
215+
final int iconType = Integer.parseInt(Objects.requireNonNull(info.get("shortcutIconType")));
216+
208217
List<ShortcutInfoCompat> dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(context);
209218
final List<ShortcutInfoCompat> shortcutList = new ArrayList<>();
210219
int flag = 1;
211220
for(ShortcutInfoCompat si : dynamicShortcuts) {
212-
if(si.getId().equalsIgnoreCase(refId)) {
213-
ShortcutInfoCompat shortcutInfo = buildShortcutUsingCompat(info);
214-
shortcutList.add(shortcutInfo);
221+
if(si.getId().equals(id)) {
222+
ShortcutInfoCompat.Builder shortcutInfo = buildShortcutUsingCompat(id,icon,action,shortLabel,longLabel,iconType);
223+
shortcutList.add(shortcutInfo.build());
215224
flag = 0;
216225
continue;
217226
}
@@ -229,24 +238,86 @@ private void updateShortcutItem(MethodCall call) {
229238
}
230239
}
231240

241+
// @SuppressLint("RestrictedApi")
242+
// @RequiresApi(api = Build.VERSION_CODES.O)
243+
// private void changeShortcutItemLabel(MethodCall call) {
244+
// try {
245+
// final List<String> args = call.arguments();
246+
// final String refId = args.get(0);
247+
// final String shortLabel = args.get(1);
248+
// final String longLabel = args.get(2);
249+
//
250+
//// Icon icon = getIconFromFlutterAsset(context,"assets/icons/next.png");
251+
//// IconCompat iconCompat = IconCompat.createFromIcon(context,icon);
252+
//
253+
// List<ShortcutInfoCompat> dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(context);
254+
// final List<ShortcutInfoCompat> shortcutList = new ArrayList<>();
255+
//
256+
// int flag = 1;
257+
// for(ShortcutInfoCompat si : dynamicShortcuts) {
258+
// String id = si.getId();
259+
// if(id.equals(refId)) {
260+
//
261+
// ShortcutInfoCompat.Builder shortcutInfo = buildShortcutUsingCompat(id,null,null,null,null,0);
262+
// shortcutInfo.setIntent(si.getIntent());
263+
//
264+
// if(shortLabel != null ) {
265+
// shortcutInfo.setShortLabel("Changed Label");
266+
// } else {
267+
// shortcutInfo.setShortLabel(si.getShortLabel());
268+
// }
269+
//
270+
// if(longLabel != null) {
271+
// shortcutInfo.setLongLabel(longLabel);
272+
// } else if(si.getLongLabel() != null) {
273+
// shortcutInfo.setLongLabel(si.getLongLabel());
274+
// }
275+
//
276+
// shortcutInfo.setIcon(si.getIcon());
277+
//
278+
// shortcutList.add(shortcutInfo.build());
279+
// flag = 0;
280+
// continue;
281+
// }
282+
// shortcutList.add(si);
283+
// }
284+
// if (flag == 1) {
285+
// Log.e(TAG, "ID did not match any shortcut");
286+
// return;
287+
// }
288+
// try {
289+
// ShortcutManagerCompat.updateShortcuts(context,shortcutList);
290+
// debugPrint("Shortcut Label Changed.");
291+
// } catch(Exception e) {
292+
// Log.e(TAG,e.toString());
293+
// }
294+
// } catch(Exception e) {
295+
// Log.e(TAG,e.toString());
296+
// }
297+
// }
298+
232299
@SuppressLint("RestrictedApi")
233300
@RequiresApi(api = Build.VERSION_CODES.O)
234301
private void changeShortcutItemIcon(MethodCall call) {
235302
try {
236303
final List<String> args = call.arguments();
237304
final String refId = args.get(0);
238305
final String changeIcon = args.get(1);
239-
Map<String,String> items = shortcutWithoutIcon(refId);
240-
ShortcutInfoCompat.Builder shortcutInfo = createShortcutInfoUsingIconResID(items);
241306
Icon icon = getIconFromFlutterAsset(context,changeIcon);
307+
IconCompat iconCompat = IconCompat.createFromIcon(context,icon);
242308
List<ShortcutInfoCompat> dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(context);
243309

244310
final List<ShortcutInfoCompat> shortcutList = new ArrayList<>();
245311
int flag = 1;
246312
for(ShortcutInfoCompat si : dynamicShortcuts) {
247-
if(si.getId().equalsIgnoreCase(refId)) {
248-
IconCompat iconCompat = IconCompat.createFromIcon(context,icon);
249-
shortcutList.add(shortcutInfo.setIcon(iconCompat).build());
313+
String id = si.getId();
314+
if(id.equals(refId)) {
315+
String shortLabel = (String) si.getShortLabel();
316+
String longLabel = (String) si.getLongLabel();
317+
318+
ShortcutInfoCompat.Builder shortcutInfo = buildShortcutUsingCompat(id,null,null,shortLabel,longLabel,0);
319+
shortcutInfo.setIcon(iconCompat).setIntent(si.getIntent());
320+
shortcutList.add(shortcutInfo.build());
250321
flag = 0;
251322
continue;
252323
}
@@ -270,79 +341,48 @@ private void changeShortcutItemIcon(MethodCall call) {
270341

271342
/* ******************** Utility Functions ********************* */
272343

273-
@SuppressLint("RestrictedApi")
274-
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
275-
private Map<String,String> shortcutWithoutIcon(String id) {
276-
HashMap<String, String> map = new HashMap<String, String>();
277-
List<ShortcutInfoCompat> dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(context);
278-
for(ShortcutInfoCompat si : dynamicShortcuts) {
279-
if(si.getId().equalsIgnoreCase(id)) {
280-
map.put("id", si.getId());
281-
map.put("shortLabel", (String) si.getShortLabel());
282-
map.put("longLabel", (String) si.getLongLabel());
283-
map.put("action",si.getIntent().getStringExtra(EXTRA_ACTION));
284-
}
285-
}
286-
return map;
287-
}
288-
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-
311344
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
312345
private List<ShortcutInfoCompat> shortcutInfoCompatList(List<Map<String, String>> shortcuts) {
313346
final List<ShortcutInfoCompat> shortcutList = new ArrayList<>();
314347

315348
for (Map<String, String> shortcut : shortcuts) {
316-
ShortcutInfoCompat shortcutInfoCompat = buildShortcutUsingCompat(shortcut);
317-
shortcutList.add(shortcutInfoCompat);
349+
final String id = shortcut.get("id");
350+
final String icon = shortcut.get("icon");
351+
final String action = shortcut.get("action");
352+
final String shortLabel = shortcut.get("shortLabel");
353+
final String longLabel = shortcut.get("LongLabel");
354+
final int iconType = Integer.parseInt(Objects.requireNonNull(shortcut.get("shortcutIconType")));
355+
ShortcutInfoCompat.Builder shortcutInfoCompat = buildShortcutUsingCompat(id,icon,action,shortLabel,longLabel,iconType);
356+
shortcutList.add(shortcutInfoCompat.build());
318357
}
319358
return shortcutList;
320359
}
321360

322361
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
323-
private ShortcutInfoCompat buildShortcutUsingCompat(Map<String, String> shortcut) {
324-
final String id = shortcut.get("id");
325-
final String icon = shortcut.get("icon");
326-
final String action = shortcut.get("action");
327-
final String shortLabel = shortcut.get("shortLabel");
328-
final String longLabel = shortcut.get("LongLabel");
329-
final int iconType = Integer.parseInt(Objects.requireNonNull(shortcut.get("shortcutIconType")));
362+
private ShortcutInfoCompat.Builder buildShortcutUsingCompat(String id, String icon, String action, String shortLabel, String longLabel, int iconType) {
330363

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

334-
final Intent intent = getIntentToOpenMainActivity(action);
367+
if(action != null) {
368+
Intent intent;
369+
intent = getIntentToOpenMainActivity(action);
370+
shortcutInfoCompat.setIntent(intent);
371+
}
335372

336373
if(longLabel != null) {
337374
shortcutInfoCompat.setLongLabel(longLabel);
338375
}
339-
setIconCompat(iconType, icon,shortcutInfoCompat);
340376

341-
assert shortLabel!=null;
342-
return shortcutInfoCompat
343-
.setShortLabel(shortLabel)
344-
.setIntent(intent)
345-
.build();
377+
if(icon != null) {
378+
setIconCompat(iconType, icon, shortcutInfoCompat);
379+
}
380+
381+
if(shortLabel != null) {
382+
shortcutInfoCompat.setShortLabel(shortLabel);
383+
}
384+
385+
return shortcutInfoCompat;
346386
}
347387

348388
private void setIconCompat(int iconType,String icon,ShortcutInfoCompat.Builder shortcutBuilderCompat) {

example/lib/main.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,30 @@ class _MyAppState extends State<MyApp> {
222222
],
223223
),
224224
Divider(),
225+
// Text("Label"),
226+
// Row(
227+
// mainAxisAlignment: MainAxisAlignment.spaceEvenly,
228+
// children: [
229+
// ElevatedButton(
230+
// child: Text("Change ShortLabel"),
231+
// onPressed: () async {
232+
// await flutterShortcuts.changeShortcutItemLabel(
233+
// id: "1",
234+
// shortLabel: "Resume Game",
235+
// );
236+
// },
237+
// ),
238+
// ElevatedButton(
239+
// child: Text("Change longLabel"),
240+
// onPressed: () async {
241+
// await flutterShortcuts.changeShortcutItemLabel(
242+
// id: "2",
243+
// longLabel: "Long label for shortcut item with id '2'",
244+
// );
245+
// },
246+
// ),
247+
// ],
248+
// ),
225249
],
226250
),
227251
),

lib/flutter_shortcuts.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ class FlutterShortcuts {
7777
return FlutterShortcutsPlatform.instance.updateShortcutItem(shortcut);
7878
}
7979

80+
// Future<void> changeShortcutItemLabel({
81+
// required String id,
82+
// String? shortLabel,
83+
// String? longLabel,
84+
// }) async {
85+
// return FlutterShortcutsPlatform.instance
86+
// .changeShortcutItemLabel(id, shortLabel, longLabel);
87+
// }
88+
8089
/// [changeShortcutItemIcon] will change the icon of the shortcut based on id.
8190
/// If the ID of the shortcut is not same, no changes will be reflected.
8291
Future<void> changeShortcutItemIcon(

lib/src/method_call/flutter_shortcuts_method_call_handler.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ class FlutterShortcutsMethodCallHandler extends FlutterShortcutsPlatform {
8989
await channel.invokeMethod<void>('updateShortcutItem', [item]);
9090
}
9191

92+
// @override
93+
// Future<void> changeShortcutItemLabel(
94+
// String id, String? shortLabel, String? longLabel) async {
95+
// await channel.invokeMethod<void>(
96+
// 'changeShortcutItemLabel', [id, shortLabel, longLabel]);
97+
// }
98+
9299
@override
93100
Future<void> changeShortcutItemIcon(String id, String icon) async {
94101
await channel.invokeMethod<void>('changeShortcutItemIcon', [id, icon]);

lib/src/platform/flutter_shortcuts_platform.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ abstract class FlutterShortcutsPlatform extends PlatformInterface {
6868
throw UnimplementedError("updateShortcutItem() has not been implemented.");
6969
}
7070

71+
// Future<void> changeShortcutItemLabel(
72+
// String id, String? shortLabel, String? longLabel) async {
73+
// throw UnimplementedError(
74+
// "changeShortcutItemShortLabel() has not been implemented.");
75+
// }
76+
7177
Future<void> changeShortcutItemIcon(String id, String icon) async {
7278
throw UnimplementedError(
7379
"changeShortcutItemIcon() has not been implemented.");

pubspec.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages:
77
name: async
88
url: "https://pub.dartlang.org"
99
source: hosted
10-
version: "2.5.0"
10+
version: "2.6.1"
1111
boolean_selector:
1212
dependency: transitive
1313
description:
@@ -106,7 +106,7 @@ packages:
106106
name: source_span
107107
url: "https://pub.dartlang.org"
108108
source: hosted
109-
version: "1.8.0"
109+
version: "1.8.1"
110110
stack_trace:
111111
dependency: transitive
112112
description:
@@ -141,7 +141,7 @@ packages:
141141
name: test_api
142142
url: "https://pub.dartlang.org"
143143
source: hosted
144-
version: "0.2.19"
144+
version: "0.3.0"
145145
typed_data:
146146
dependency: transitive
147147
description:

0 commit comments

Comments
 (0)