1010import android .content .res .Resources ;
1111import android .graphics .Bitmap ;
1212import android .graphics .BitmapFactory ;
13- import android .graphics .drawable .Drawable ;
1413import android .graphics .drawable .Icon ;
1514import android .os .Build ;
1615import android .util .Log ;
2322import java .util .HashMap ;
2423import java .util .List ;
2524import java .util .Map ;
25+ import java .util .Objects ;
2626
2727import io .flutter .FlutterInjector ;
2828import io .flutter .embedding .engine .loader .FlutterLoader ;
@@ -36,6 +36,14 @@ public class MethodCallImplementation implements MethodChannel.MethodCallHandler
3636 private final Context context ;
3737 private Activity activity ;
3838
39+ private boolean debug ;
40+
41+ void debugPrint (String message ) {
42+ if (debug ) {
43+ Log .d (TAG ,message );
44+ }
45+ }
46+
3947 MethodCallImplementation (Context context , Activity activity ) {
4048 this .context = context ;
4149 this .activity = activity ;
@@ -55,6 +63,12 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
5563 (ShortcutManager ) context .getSystemService (Context .SHORTCUT_SERVICE );
5664
5765 switch (call .method ) {
66+ case "initialize" :
67+ initialize (call );
68+ break ;
69+ case "getLaunchAction" :
70+ getLaunchAction (shortcutManager ,result );
71+ break ;
5872 case "getMaxShortcutLimit" :
5973 final int maxLimit = getMaxShortcutLimit ();
6074 result .success (maxLimit );
@@ -87,28 +101,37 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
87101 case "clearShortcutItems" :
88102 shortcutManager .removeAllDynamicShortcuts ();
89103 break ;
90- case "getLaunchAction" :
91- if (activity == null ) {
92- result .error (
93- "flutter_shortcuts_no_activity" ,
94- "There is no activity available when launching action" ,
95- null );
96- return ;
97- }
98- final Intent intent = activity .getIntent ();
99- final String launchAction = intent .getStringExtra (EXTRA_ACTION );
100- if (launchAction != null && !launchAction .isEmpty ()) {
101- shortcutManager .reportShortcutUsed (launchAction );
102- intent .removeExtra (EXTRA_ACTION );
103- }
104- result .success (launchAction );
105- break ;
106104 default :
107105 result .notImplemented ();
108106 break ;
109107 }
110108 }
111109
110+ private void initialize (MethodCall call ) {
111+ List <Map <String , String >> args = call .arguments ();
112+ this .debug = Boolean .parseBoolean (args .get (0 ).get ("debug" ));
113+ debugPrint ("Flutter Shortcuts Initialized" );
114+ }
115+
116+ @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
117+ private void getLaunchAction (ShortcutManager shortcutManager , MethodChannel .Result result ) {
118+ if (activity == null ) {
119+ result .error (
120+ "flutter_shortcuts_no_activity" ,
121+ "There is no activity available when launching action" ,
122+ null );
123+ return ;
124+ }
125+ final Intent intent = activity .getIntent ();
126+ final String launchAction = intent .getStringExtra (EXTRA_ACTION );
127+ if (launchAction != null && !launchAction .isEmpty ()) {
128+ shortcutManager .reportShortcutUsed (launchAction );
129+ intent .removeExtra (EXTRA_ACTION );
130+ }
131+ result .success (launchAction );
132+ debugPrint ("Launch Action: " + launchAction );
133+ }
134+
112135 @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
113136 private int getMaxShortcutLimit () {
114137 ShortcutManager shortcutManager =
@@ -128,11 +151,12 @@ private Map<String, Integer> getIconProperties() {
128151
129152 @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
130153 private void setShortcutItems (MethodCall call ,ShortcutManager shortcutManager ) {
131- List <Map <String , String >> setShortcutItemsArgs = call .arguments ();
154+ List <Map <String , String >> args = call .arguments ();
132155 List <ShortcutInfo > shortcuts ;
133156 try {
134- shortcuts = processShortcuts (setShortcutItemsArgs );
157+ shortcuts = processShortcuts (args );
135158 shortcutManager .setDynamicShortcuts (shortcuts );
159+ debugPrint ("Shortcuts created" );
136160 } catch (Exception e ) {
137161 Log .e (TAG ,e .toString ());
138162 }
@@ -145,6 +169,7 @@ private void pushShortcutItem(MethodCall call, ShortcutManager shortcutManager)
145169 try {
146170 shortcuts = processShortcuts (args );
147171 shortcutManager .addDynamicShortcuts (shortcuts );
172+ debugPrint ("Shortcut pushed" );
148173 } catch (Exception e ) {
149174 Log .e (TAG ,e .toString ());
150175 }
@@ -157,25 +182,26 @@ private void pushShortcutItems(MethodCall call, ShortcutManager shortcutManager)
157182 try {
158183 shortcuts = processShortcuts (args );
159184 shortcutManager .addDynamicShortcuts (shortcuts );
185+ debugPrint ("Shortcuts pushed" );
160186 } catch (Exception e ) {
161187 Log .e (TAG ,e .toString ());
162188 }
163189 }
164190
165191 @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
166192 private void updateShortcutItems (MethodCall call , ShortcutManager shortcutManager ) {
167- List <Map <String , String >> updateAllShortcutArgs = call .arguments ();
193+ List <Map <String , String >> args = call .arguments ();
168194 boolean updated = false ;
169195 try {
170- List <ShortcutInfo > updateShortcuts = processShortcuts (updateAllShortcutArgs );
196+ List <ShortcutInfo > updateShortcuts = processShortcuts (args );
171197 updated = shortcutManager .updateShortcuts (updateShortcuts );
172198 } catch (Exception e ) {
173199 Log .e (TAG , e .toString ());
174200 }
175201 if (updated ) {
176- Log . d ( TAG , "All Shortcuts updated" );
202+ debugPrint ( " Shortcuts updated" );
177203 } else {
178- Log . d ( TAG , "Unable to update all shortcuts" );
204+ debugPrint ( "Unable to update shortcuts" );
179205 }
180206 }
181207
@@ -202,6 +228,7 @@ private void updateShortcutItem(MethodCall call, ShortcutManager shortcutManager
202228 }
203229 try {
204230 shortcutManager .updateShortcuts (shortcutList );
231+ debugPrint ("Shortcut updated" );
205232 } catch (Exception e ) {
206233 Log .e (TAG ,e .toString ());
207234 }
@@ -211,7 +238,6 @@ private void updateShortLabel(MethodCall call, ShortcutManager shortcutManager)
211238 final List <String > args = call .arguments ();
212239 final String refId = args .get (0 );
213240 final String title = args .get (1 );
214-
215241 }
216242
217243 @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
@@ -239,6 +265,7 @@ private void changeShortcutItemIcon(MethodCall call, ShortcutManager shortcutMan
239265 }
240266 try {
241267 shortcutManager .updateShortcuts (shortcutList );
268+ debugPrint ("Shortcut Icon Changed." );
242269 } catch (Exception e ) {
243270 Log .e (TAG ,e .toString ());
244271 }
@@ -281,7 +308,7 @@ private ShortcutInfo createShortcutInfo(Map<String, String> shortcut) {
281308 final String action = shortcut .get ("action" );
282309 final String shortLabel = shortcut .get ("shortLabel" );
283310 final String longLabel = shortcut .get ("LongLabel" );
284- final int iconType = Integer .parseInt (shortcut .get ("shortcutIconType" ));
311+ final int iconType = Integer .parseInt (Objects . requireNonNull ( shortcut .get ("shortcutIconType" ) ));
285312 final ShortcutInfo .Builder shortcutBuilder ;
286313 shortcutBuilder = new ShortcutInfo .Builder (context , id );
287314
@@ -340,6 +367,7 @@ private Icon getIconFromFlutterAsset(Context context, String path) {
340367 }
341368 Bitmap image = null ;
342369 try {
370+ assert fd != null ;
343371 image = BitmapFactory .decodeStream (fd .createInputStream ());
344372 } catch (IOException e ) {
345373 e .printStackTrace ();
0 commit comments