@@ -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 ) {
0 commit comments