1515import androidx .annotation .RequiresApi ;
1616
1717import java .util .ArrayList ;
18+ import java .util .HashMap ;
1819import java .util .List ;
1920import java .util .Map ;
2021
@@ -46,31 +47,16 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
4647 (ShortcutManager ) context .getSystemService (Context .SHORTCUT_SERVICE );
4748 switch (call .method ) {
4849 case "setShortcutItems" :
49- List <Map <String , String >> setShortcutItemsArgs = call .arguments ();
50- List <ShortcutInfo > shortcuts = processShortcuts (setShortcutItemsArgs );
51- shortcutManager .setDynamicShortcuts (shortcuts );
52- Toast .makeText (context , "Shortcut Created" , Toast .LENGTH_SHORT ).show ();
50+ setShortcutItems (call ,shortcutManager );
5351 break ;
5452 case "updateAllShortcutItems" :
55- List <Map <String , String >> updateAllShortcutArgs = call .arguments ();
56- List <ShortcutInfo > updateShortcuts = processShortcuts (updateAllShortcutArgs );
57- boolean updated = shortcutManager .updateShortcuts (updateShortcuts );
58- Toast .makeText (context , "Shortcut Updated: " + updated , Toast .LENGTH_SHORT ).show ();
53+ updateAllShortcutItems (call ,shortcutManager );
5954 break ;
6055 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 );
56+ updateShortcutItem (call ,shortcutManager );
57+ break ;
58+ case "changeShortcutItemIcon" :
59+ changeShortcutItemIcon (call ,shortcutManager );
7460 break ;
7561 case "clearShortcutItems" :
7662 shortcutManager .removeAllDynamicShortcuts ();
@@ -98,6 +84,76 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
9884 result .success (null );
9985 }
10086
87+
88+
89+ @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
90+ private void setShortcutItems (MethodCall call ,ShortcutManager shortcutManager ) {
91+ List <Map <String , String >> setShortcutItemsArgs = call .arguments ();
92+ List <ShortcutInfo > shortcuts = processShortcuts (setShortcutItemsArgs );
93+ shortcutManager .setDynamicShortcuts (shortcuts );
94+ Toast .makeText (context , "Shortcut Created" , Toast .LENGTH_SHORT ).show ();
95+ }
96+
97+ @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
98+ private void updateAllShortcutItems (MethodCall call , ShortcutManager shortcutManager ) {
99+ List <Map <String , String >> updateAllShortcutArgs = call .arguments ();
100+ List <ShortcutInfo > updateShortcuts = processShortcuts (updateAllShortcutArgs );
101+ boolean updated = shortcutManager .updateShortcuts (updateShortcuts );
102+ Toast .makeText (context , "Shortcut Updated: " + updated , Toast .LENGTH_SHORT ).show ();
103+ }
104+
105+ @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
106+ private void updateShortcutItem (MethodCall call , ShortcutManager shortcutManager ) {
107+ final List <Map <String , String >> args = call .arguments ();
108+ Map <String , String > info = args .get (0 );
109+ List <ShortcutInfo > dynamicShortcuts = shortcutManager .getDynamicShortcuts ();
110+ final List <ShortcutInfo > shortcutList = new ArrayList <>();
111+ for (ShortcutInfo si : dynamicShortcuts ) {
112+ if (si .getId ().equalsIgnoreCase (info .get ("id" ))) {
113+ ShortcutInfo shortcutInfo = createShortcutInfo (info );
114+ shortcutList .add (shortcutInfo );
115+ continue ;
116+ }
117+ shortcutList .add (si );
118+ }
119+ shortcutManager .updateShortcuts (shortcutList );
120+ }
121+
122+ @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
123+ private void changeShortcutItemIcon (MethodCall call , ShortcutManager shortcutManager ) {
124+ final List <String > args = call .arguments ();
125+ final String refId = args .get (0 );
126+ final String changeIcon = args .get (1 );
127+ Map <String ,String > items = deserializeShortcutInfoAtId (refId ,changeIcon ,shortcutManager );
128+ ShortcutInfo shortcutInfo = createShortcutInfo (items );
129+ List <ShortcutInfo > dynamicShortcuts = shortcutManager .getDynamicShortcuts ();
130+ final List <ShortcutInfo > shortcutList = new ArrayList <>();
131+ for (ShortcutInfo si : dynamicShortcuts ) {
132+ if (si .getId ().equalsIgnoreCase (refId )) {
133+ shortcutList .add (shortcutInfo );
134+ continue ;
135+ }
136+ shortcutList .add (si );
137+ }
138+ shortcutManager .updateShortcuts (shortcutList );
139+ }
140+
141+ @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
142+ private Map <String ,String > deserializeShortcutInfoAtId (String id , String icon , ShortcutManager shortcutManager ) {
143+ HashMap <String , String > map = new HashMap <String , String >();
144+ List <ShortcutInfo > dynamicShortcuts = shortcutManager .getDynamicShortcuts ();
145+
146+ for (ShortcutInfo si : dynamicShortcuts ) {
147+ if (si .getId ().equalsIgnoreCase (id )) {
148+ map .put ("id" , si .getId ());
149+ map .put ("title" , String .valueOf (si .getShortLabel ()));
150+ map .put ("icon" , icon );
151+ map .put ("action" ,si .getIntent ().getStringExtra (EXTRA_ACTION ));
152+ }
153+ }
154+ return map ;
155+ }
156+
101157 @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
102158 private List <ShortcutInfo > processShortcuts (List <Map <String , String >> shortcuts ) {
103159 final List <ShortcutInfo > shortcutList = new ArrayList <>();
@@ -116,9 +172,7 @@ private ShortcutInfo createShortcutInfo(Map<String, String> shortcut) {
116172 final String action = shortcut .get ("action" );
117173 final String title = shortcut .get ("title" );
118174 final ShortcutInfo .Builder shortcutBuilder ;
119-
120- shortcutBuilder = new ShortcutInfo .Builder (context , id );
121-
175+ shortcutBuilder = new ShortcutInfo .Builder (context , id );
122176
123177 final int resourceId = loadResourceId (context , icon );
124178 final Intent intent = getIntentToOpenMainActivity (action );
0 commit comments