@@ -46,17 +46,32 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
4646 (ShortcutManager ) context .getSystemService (Context .SHORTCUT_SERVICE );
4747 switch (call .method ) {
4848 case "setShortcutItems" :
49- List <Map <String , String >> arg = call .arguments ();
50- List <ShortcutInfo > shortcuts = processShortcuts (arg );
49+ List <Map <String , String >> setShortcutItemsArgs = call .arguments ();
50+ List <ShortcutInfo > shortcuts = processShortcuts (setShortcutItemsArgs );
5151 shortcutManager .setDynamicShortcuts (shortcuts );
5252 Toast .makeText (context , "Shortcut Created" , Toast .LENGTH_SHORT ).show ();
5353 break ;
54- case "updateShortcutItems " :
55- List <Map <String , String >> updateShortcutArgs = call .arguments ();
56- List <ShortcutInfo > updateShortcuts = processShortcuts (updateShortcutArgs );
54+ case "updateAllShortcutItems " :
55+ List <Map <String , String >> updateAllShortcutArgs = call .arguments ();
56+ List <ShortcutInfo > updateShortcuts = processShortcuts (updateAllShortcutArgs );
5757 boolean updated = shortcutManager .updateShortcuts (updateShortcuts );
5858 Toast .makeText (context , "Shortcut Updated: " + updated , Toast .LENGTH_SHORT ).show ();
5959 break ;
60+ case "updateShortcutItem" :
61+ final List <Map <String , String >> updateShortcutItemArgs = call .arguments ();
62+ Map <String , String > info = updateShortcutItemArgs .get (0 );
63+ List <ShortcutInfo > previousDynamicShortcuts = shortcutManager .getDynamicShortcuts ();
64+ final List <ShortcutInfo > shortcutList = new ArrayList <>();
65+ for (ShortcutInfo si : previousDynamicShortcuts ) {
66+ if (si .getId ().equalsIgnoreCase (info .get ("id" ))) {
67+ ShortcutInfo shortcutInfo = createShortcutInfo (info );
68+ shortcutList .add (shortcutInfo );
69+ continue ;
70+ }
71+ shortcutList .add (si );
72+ }
73+ shortcutManager .updateShortcuts (shortcutList );
74+ break ;
6075 case "clearShortcutItems" :
6176 shortcutManager .removeAllDynamicShortcuts ();
6277 break ;
@@ -84,32 +99,41 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
8499 }
85100
86101 @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
87- @ TargetApi (Build .VERSION_CODES .N_MR1 )
88102 private List <ShortcutInfo > processShortcuts (List <Map <String , String >> shortcuts ) {
89103 final List <ShortcutInfo > shortcutList = new ArrayList <>();
90104
91105 for (Map <String , String > shortcut : shortcuts ) {
92- final String id = shortcut .get ("id" );
93- final String icon = shortcut .get ("icon" );
94- final String action = shortcut .get ("action" );
95- final String title = shortcut .get ("title" );
96- final ShortcutInfo .Builder shortcutBuilder = new ShortcutInfo .Builder (context , id );
106+ ShortcutInfo shortcutInfo = createShortcutInfo (shortcut );
107+ shortcutList .add (shortcutInfo );
108+ }
109+ return shortcutList ;
110+ }
111+
112+ @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
113+ private ShortcutInfo createShortcutInfo (Map <String , String > shortcut ) {
114+ final String id = shortcut .get ("id" );
115+ final String icon = shortcut .get ("icon" );
116+ final String action = shortcut .get ("action" );
117+ final String title = shortcut .get ("title" );
118+ final ShortcutInfo .Builder shortcutBuilder ;
119+
120+ shortcutBuilder = new ShortcutInfo .Builder (context , id );
97121
98- final int resourceId = loadResourceId (context , icon );
99- final Intent intent = getIntentToOpenMainActivity (action );
100122
101- if (resourceId > 0 ) {
123+ final int resourceId = loadResourceId (context , icon );
124+ final Intent intent = getIntentToOpenMainActivity (action );
125+
126+ if (resourceId > 0 ) {
127+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N_MR1 ) {
102128 shortcutBuilder .setIcon (Icon .createWithResource (context , resourceId ));
103129 }
104-
105- final ShortcutInfo shortcutInfo = shortcutBuilder
106- .setLongLabel (title )
107- .setShortLabel (title )
108- .setIntent (intent )
109- .build ();
110- shortcutList .add (shortcutInfo );
111130 }
112- return shortcutList ;
131+
132+ return shortcutBuilder
133+ .setLongLabel (title )
134+ .setShortLabel (title )
135+ .setIntent (intent )
136+ .build ();
113137 }
114138
115139 private int loadResourceId (Context context , String icon ) {
0 commit comments