@@ -91,6 +91,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
9191 case "updateShortcutItem" :
9292 updateShortcutItem (call );
9393 break ;
94+ case "changeShortcutItemShortLabel" :
95+ changeShortcutItemShortLabel (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,64 @@ private void updateShortcutItem(MethodCall call) {
229238 }
230239 }
231240
241+ @ RequiresApi (api = Build .VERSION_CODES .KITKAT )
242+ @ SuppressLint ("RestrictedApi" )
243+ private void changeShortcutItemShortLabel (MethodCall call ) {
244+ final List <String > args = call .arguments ();
245+ final String refId = args .get (0 );
246+ final String shortLabel = args .get (1 );
247+
248+ List <ShortcutInfoCompat > dynamicShortcuts = ShortcutManagerCompat .getDynamicShortcuts (context );
249+ final List <ShortcutInfoCompat > shortcutList = new ArrayList <>();
250+
251+ int flag = 1 ;
252+ for (ShortcutInfoCompat si : dynamicShortcuts ) {
253+ String id = si .getId ();
254+ if (id .equals (refId )) {
255+ String longLabel = (String ) si .getLongLabel ();
256+ ShortcutInfoCompat .Builder shortcutInfoCompat = buildShortcutUsingCompat (id ,null ,null ,shortLabel ,longLabel ,0 );
257+ shortcutInfoCompat .setIcon (si .getIcon ()).setIntent (si .getIntent ());
258+ shortcutList .add (shortcutInfoCompat .build ());
259+ flag = 0 ;
260+ continue ;
261+ }
262+ shortcutList .add (si );
263+ }
264+
265+ if (flag == 1 ) {
266+ Log .e (TAG , "ID did not match any shortcut" );
267+ return ;
268+ }
269+ try {
270+ ShortcutManagerCompat .updateShortcuts (context ,shortcutList );
271+ debugPrint ("Shortcut Short Label Changed." );
272+ } catch (Exception e ) {
273+ Log .e (TAG ,e .toString ());
274+ }
275+ }
276+
232277 @ SuppressLint ("RestrictedApi" )
233278 @ RequiresApi (api = Build .VERSION_CODES .O )
234279 private void changeShortcutItemIcon (MethodCall call ) {
235280 try {
236281 final List <String > args = call .arguments ();
237282 final String refId = args .get (0 );
238283 final String changeIcon = args .get (1 );
239- Map <String ,String > items = shortcutWithoutIcon (refId );
240- ShortcutInfoCompat .Builder shortcutInfo = createShortcutInfoUsingIconResID (items );
241284 Icon icon = getIconFromFlutterAsset (context ,changeIcon );
285+ IconCompat iconCompat = IconCompat .createFromIcon (context ,icon );
242286 List <ShortcutInfoCompat > dynamicShortcuts = ShortcutManagerCompat .getDynamicShortcuts (context );
243287
244288 final List <ShortcutInfoCompat > shortcutList = new ArrayList <>();
245289 int flag = 1 ;
246290 for (ShortcutInfoCompat si : dynamicShortcuts ) {
247- if (si .getId ().equalsIgnoreCase (refId )) {
248- IconCompat iconCompat = IconCompat .createFromIcon (context ,icon );
249- shortcutList .add (shortcutInfo .setIcon (iconCompat ).build ());
291+ String id = si .getId ();
292+ if (id .equals (refId )) {
293+ String shortLabel = (String ) si .getShortLabel ();
294+ String longLabel = (String ) si .getLongLabel ();
295+
296+ ShortcutInfoCompat .Builder shortcutInfo = buildShortcutUsingCompat (id ,null ,null ,shortLabel ,longLabel ,0 );
297+ shortcutInfo .setIcon (iconCompat ).setIntent (si .getIntent ());
298+ shortcutList .add (shortcutInfo .build ());
250299 flag = 0 ;
251300 continue ;
252301 }
@@ -270,79 +319,48 @@ private void changeShortcutItemIcon(MethodCall call) {
270319
271320 /* ******************** Utility Functions ********************* */
272321
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-
311322 @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
312323 private List <ShortcutInfoCompat > shortcutInfoCompatList (List <Map <String , String >> shortcuts ) {
313324 final List <ShortcutInfoCompat > shortcutList = new ArrayList <>();
314325
315326 for (Map <String , String > shortcut : shortcuts ) {
316- ShortcutInfoCompat shortcutInfoCompat = buildShortcutUsingCompat (shortcut );
317- shortcutList .add (shortcutInfoCompat );
327+ final String id = shortcut .get ("id" );
328+ final String icon = shortcut .get ("icon" );
329+ final String action = shortcut .get ("action" );
330+ final String shortLabel = shortcut .get ("shortLabel" );
331+ final String longLabel = shortcut .get ("LongLabel" );
332+ final int iconType = Integer .parseInt (Objects .requireNonNull (shortcut .get ("shortcutIconType" )));
333+ ShortcutInfoCompat .Builder shortcutInfoCompat = buildShortcutUsingCompat (id ,icon ,action ,shortLabel ,longLabel ,iconType );
334+ shortcutList .add (shortcutInfoCompat .build ());
318335 }
319336 return shortcutList ;
320337 }
321338
322339 @ 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" )));
340+ private ShortcutInfoCompat .Builder buildShortcutUsingCompat (String id , String icon , String action , String shortLabel , String longLabel , int iconType ) {
330341
331342 assert id != null ;
332343 ShortcutInfoCompat .Builder shortcutInfoCompat = new ShortcutInfoCompat .Builder (context , id );
333344
334- final Intent intent = getIntentToOpenMainActivity (action );
345+ Intent intent = null ;
346+ if (action != null ) {
347+ intent = getIntentToOpenMainActivity (action );
348+ shortcutInfoCompat .setIntent (intent );
349+ }
335350
336351 if (longLabel != null ) {
337352 shortcutInfoCompat .setLongLabel (longLabel );
338353 }
339- setIconCompat (iconType , icon ,shortcutInfoCompat );
340354
341- assert shortLabel !=null ;
342- return shortcutInfoCompat
343- .setShortLabel (shortLabel )
344- .setIntent (intent )
345- .build ();
355+ if (icon != null ) {
356+ setIconCompat (iconType , icon , shortcutInfoCompat );
357+ }
358+
359+ if (shortLabel != null ) {
360+ shortcutInfoCompat .setShortLabel (shortLabel );
361+ }
362+
363+ return shortcutInfoCompat ;
346364 }
347365
348366 private void setIconCompat (int iconType ,String icon ,ShortcutInfoCompat .Builder shortcutBuilderCompat ) {
0 commit comments