11package com .divyanshushekhar .flutter_shortcuts ;
22
3+ import android .annotation .SuppressLint ;
34import android .app .Activity ;
45import android .content .Context ;
56import android .content .Intent ;
2021
2122import java .io .IOException ;
2223import java .util .ArrayList ;
23- import java .util .Arrays ;
24- import java .util .Collections ;
2524import java .util .HashMap ;
2625import java .util .List ;
2726import java .util .Map ;
@@ -56,6 +55,7 @@ void setActivity(Activity activity) {
5655 this .activity = activity ;
5756 }
5857
58+ @ RequiresApi (api = Build .VERSION_CODES .O )
5959 @ Override
6060 public void onMethodCall (@ NonNull MethodCall call , @ NonNull MethodChannel .Result result ) {
6161 if (Build .VERSION .SDK_INT < Build .VERSION_CODES .N_MR1 ) {
@@ -96,6 +96,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
9696 break ;
9797 case "clearShortcutItems" :
9898 ShortcutManagerCompat .removeAllDynamicShortcuts (context );
99+ debugPrint ("Removed all shortcuts." );
99100 break ;
100101 default :
101102 result .notImplemented ();
@@ -228,20 +229,24 @@ private void updateShortcutItem(MethodCall call) {
228229 }
229230 }
230231
231- @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
232+ @ SuppressLint ("RestrictedApi" )
233+ @ RequiresApi (api = Build .VERSION_CODES .O )
232234 private void changeShortcutItemIcon (MethodCall call ) {
233235 try {
234236 final List <String > args = call .arguments ();
235237 final String refId = args .get (0 );
236238 final String changeIcon = args .get (1 );
237- Map <String ,String > items = deserializeShortcutInfoAtId (refId ,changeIcon );
238- ShortcutInfoCompat shortcutInfo = buildShortcutUsingCompat (items );
239+ Map <String ,String > items = shortcutWithoutIcon (refId );
240+ ShortcutInfoCompat .Builder shortcutInfo = createShortcutInfoUsingIconResID (items );
241+ Icon icon = getIconFromFlutterAsset (context ,changeIcon );
239242 List <ShortcutInfoCompat > dynamicShortcuts = ShortcutManagerCompat .getDynamicShortcuts (context );
243+
240244 final List <ShortcutInfoCompat > shortcutList = new ArrayList <>();
241245 int flag = 1 ;
242246 for (ShortcutInfoCompat si : dynamicShortcuts ) {
243247 if (si .getId ().equalsIgnoreCase (refId )) {
244- shortcutList .add (shortcutInfo );
248+ IconCompat iconCompat = IconCompat .createFromIcon (context ,icon );
249+ shortcutList .add (shortcutInfo .setIcon (iconCompat ).build ());
245250 flag = 0 ;
246251 continue ;
247252 }
@@ -262,21 +267,47 @@ private void changeShortcutItemIcon(MethodCall call) {
262267 }
263268 }
264269
270+
271+ /* ******************** Utility Functions ********************* */
272+
273+ @ SuppressLint ("RestrictedApi" )
265274 @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
266- private Map <String ,String > deserializeShortcutInfoAtId (String id , String icon ) {
275+ private Map <String ,String > shortcutWithoutIcon (String id ) {
267276 HashMap <String , String > map = new HashMap <String , String >();
268277 List <ShortcutInfoCompat > dynamicShortcuts = ShortcutManagerCompat .getDynamicShortcuts (context );
269278 for (ShortcutInfoCompat si : dynamicShortcuts ) {
270279 if (si .getId ().equalsIgnoreCase (id )) {
271280 map .put ("id" , si .getId ());
272- map .put ("shortLabel" , String . valueOf ( si .getShortLabel () ));
273- map .put ("icon " , icon );
281+ map .put ("shortLabel" , ( String ) si .getShortLabel ());
282+ map .put ("longLabel " , ( String ) si . getLongLabel () );
274283 map .put ("action" ,si .getIntent ().getStringExtra (EXTRA_ACTION ));
275284 }
276285 }
277286 return map ;
278287 }
279288
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+
280311 @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
281312 private List <ShortcutInfoCompat > shortcutInfoCompatList (List <Map <String , String >> shortcuts ) {
282313 final List <ShortcutInfoCompat > shortcutList = new ArrayList <>();
@@ -297,7 +328,6 @@ private ShortcutInfoCompat buildShortcutUsingCompat(Map<String, String> shortcut
297328 final String longLabel = shortcut .get ("LongLabel" );
298329 final int iconType = Integer .parseInt (Objects .requireNonNull (shortcut .get ("shortcutIconType" )));
299330
300-
301331 assert id != null ;
302332 ShortcutInfoCompat .Builder shortcutInfoCompat = new ShortcutInfoCompat .Builder (context , id );
303333
@@ -312,7 +342,6 @@ private ShortcutInfoCompat buildShortcutUsingCompat(Map<String, String> shortcut
312342 return shortcutInfoCompat
313343 .setShortLabel (shortLabel )
314344 .setIntent (intent )
315- .addCapabilityBinding ("actions.intent.OPEN_APP_FEATURE" )
316345 .build ();
317346 }
318347
0 commit comments