1515
1616import androidx .annotation .NonNull ;
1717import androidx .annotation .RequiresApi ;
18+ import androidx .core .app .Person ;
1819import androidx .core .content .pm .ShortcutInfoCompat ;
1920import androidx .core .content .pm .ShortcutManagerCompat ;
2021import androidx .core .graphics .drawable .IconCompat ;
@@ -146,7 +147,7 @@ private Map<String, Integer> getIconProperties() {
146147
147148 @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
148149 private void setShortcutItems (MethodCall call ) {
149- List <Map <String , String >> args = call .arguments ();
150+ List <Map <String , Object >> args = call .arguments ();
150151 List <ShortcutInfoCompat > shortcuts ;
151152 try {
152153 shortcuts = shortcutInfoCompatList (args );
@@ -159,7 +160,7 @@ private void setShortcutItems(MethodCall call) {
159160
160161 @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
161162 private void pushShortcutItem (MethodCall call ) {
162- final List <Map <String , String >> args = call .arguments ();
163+ final List <Map <String , Object >> args = call .arguments ();
163164 List <ShortcutInfoCompat > shortcuts ;
164165 try {
165166 shortcuts = shortcutInfoCompatList (args );
@@ -172,7 +173,7 @@ private void pushShortcutItem(MethodCall call) {
172173
173174 @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
174175 private void pushShortcutItems (MethodCall call ) {
175- final List <Map <String , String >> args = call .arguments ();
176+ final List <Map <String , Object >> args = call .arguments ();
176177 List <ShortcutInfoCompat > shortcuts ;
177178 try {
178179 shortcuts = shortcutInfoCompatList (args );
@@ -185,7 +186,7 @@ private void pushShortcutItems(MethodCall call) {
185186
186187 @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
187188 private void updateShortcutItems (MethodCall call ) {
188- List <Map <String , String >> args = call .arguments ();
189+ List <Map <String , Object >> args = call .arguments ();
189190 boolean updated = false ;
190191 try {
191192 List <ShortcutInfoCompat > updateShortcuts = shortcutInfoCompatList (args );
@@ -202,14 +203,18 @@ private void updateShortcutItems(MethodCall call) {
202203
203204 @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
204205 private void updateShortcutItem (MethodCall call ) {
205- final List <Map <String , String >> args = call .arguments ();
206- Map <String , String > info = args .get (0 );
207- final String id = info .get ("id" );
208- final String icon = info .get ("icon" );
209- final String action = info .get ("action" );
210- final String shortLabel = info .get ("shortLabel" );
211- final String longLabel = info .get ("LongLabel" );
212- final int iconType = Integer .parseInt (Objects .requireNonNull (info .get ("shortcutIconType" )));
206+ final List <Map <String , Object >> args = call .arguments ();
207+ Map <String , Object > info = args .get (0 );
208+ final String id = (String ) info .get ("id" );
209+ final String icon = (String ) info .get ("icon" );
210+ final String action = (String ) info .get ("action" );
211+ final String shortLabel = (String ) info .get ("shortLabel" );
212+ final String longLabel = (String ) info .get ("LongLabel" );
213+ final int iconType = Integer .parseInt (Objects .requireNonNull ( (String ) info .get ("shortcutIconType" )));
214+
215+ final boolean isImportant = (boolean ) info .get ("isImportant" );
216+ final boolean isBot = (boolean ) info .get ("isBot" );
217+ final boolean isConversationShortcut = (boolean ) info .get ("conversationShortcut" );
213218
214219 List <ShortcutInfoCompat > dynamicShortcuts = ShortcutManagerCompat .getDynamicShortcuts (context );
215220 final List <ShortcutInfoCompat > shortcutList = new ArrayList <>();
@@ -218,6 +223,24 @@ private void updateShortcutItem(MethodCall call) {
218223 if (si .getId ().equals (id )) {
219224 ShortcutInfoCompat .Builder shortcutInfo = buildShortcutUsingCompat (
220225 id ,icon ,action ,shortLabel ,longLabel ,iconType );
226+
227+ // Set Person if isConversationShortcut is true
228+ if (isConversationShortcut ) {
229+ Person .Builder personBuilder = new Person .Builder ()
230+ .setKey (id )
231+ .setName (shortLabel )
232+ .setImportant (isImportant )
233+ .setBot (isBot );
234+
235+ // Setting Icon for person builder
236+ setIconCompat (iconType , icon , personBuilder );
237+
238+ shortcutInfo
239+ .setLongLived (true )
240+ .setPerson (personBuilder .build ());
241+
242+ }
243+
221244 shortcutList .add (shortcutInfo .build ());
222245 flag = 0 ;
223246 continue ;
@@ -255,7 +278,9 @@ private void changeShortcutItemIcon(MethodCall call) {
255278 String shortLabel = (String ) si .getShortLabel ();
256279 String longLabel = (String ) si .getLongLabel ();
257280
258- ShortcutInfoCompat .Builder shortcutInfo = buildShortcutUsingCompat (id ,null ,null ,shortLabel ,longLabel ,0 );
281+ ShortcutInfoCompat .Builder shortcutInfo = buildShortcutUsingCompat (
282+ id ,null ,null ,shortLabel ,longLabel ,0 );
283+
259284 shortcutInfo .setIcon (iconCompat ).setIntent (si .getIntent ());
260285 shortcutList .add (shortcutInfo .build ());
261286 flag = 0 ;
@@ -278,40 +303,61 @@ private void changeShortcutItemIcon(MethodCall call) {
278303 }
279304 }
280305
281-
282306 /* ******************** Utility Functions ********************* */
283307
284308 @ RequiresApi (api = Build .VERSION_CODES .N_MR1 )
285- private List <ShortcutInfoCompat > shortcutInfoCompatList (List <Map <String , String >> shortcuts ) {
309+ private List <ShortcutInfoCompat > shortcutInfoCompatList (
310+ List <Map <String , Object >> shortcuts ) {
286311 final List <ShortcutInfoCompat > shortcutList = new ArrayList <>();
287312
288- for (Map <String , String > shortcut : shortcuts ) {
289- final String id = shortcut .get ("id" );
290- final String icon = shortcut .get ("icon" );
291- final String action = shortcut .get ("action" );
292- final String shortLabel = shortcut .get ("shortLabel" );
293- final String longLabel = shortcut .get ("LongLabel" );
313+ for (Map <String , Object > shortcut : shortcuts ) {
314+ final String id = (String ) shortcut .get ("id" );
315+ final String icon = (String ) shortcut .get ("icon" );
316+ final String action = (String ) shortcut .get ("action" );
317+
318+ // Short Label for the shortcut
319+ // Name for Person
320+ final String shortLabel = (String ) shortcut .get ("shortLabel" );
321+
322+ // Long Label for the shortcut
323+ final String longLabel = (String ) shortcut .get ("LongLabel" );
324+
325+ final boolean isImportant = (boolean ) shortcut .get ("isImportant" );
326+ final boolean isBot = (boolean ) shortcut .get ("isBot" );
327+ final boolean isConversationShortcut = (boolean ) shortcut .get ("conversationShortcut" );
328+
294329 final int iconType = Integer .parseInt (Objects .requireNonNull (
295- shortcut .get ("shortcutIconType" )));
330+ (String ) shortcut .get ("shortcutIconType" )));
331+
296332 ShortcutInfoCompat .Builder shortcutInfoCompat = buildShortcutUsingCompat (
297- id ,
298- icon ,
299- action ,
300- shortLabel ,
301- longLabel ,
302- iconType );
333+ id , icon , action , shortLabel , longLabel , iconType );
334+
335+ // Set Person if isConversationShortcut is true
336+ if (isConversationShortcut ) {
337+ Person .Builder personBuilder = new Person .Builder ()
338+ .setKey (id )
339+ .setName (shortLabel )
340+ .setImportant (isImportant )
341+ .setBot (isBot );
342+
343+ // Setting Icon for person builder
344+ setIconCompat (iconType , icon , personBuilder );
345+
346+ shortcutInfoCompat
347+ .setLongLived (true )
348+ .setPerson (personBuilder .build ());
349+
350+ }
351+
303352 shortcutList .add (shortcutInfoCompat .build ());
304353 }
305354 return shortcutList ;
306355 }
307356
308357 @ RequiresApi (api = Build .VERSION_CODES .KITKAT )
309- private ShortcutInfoCompat .Builder buildShortcutUsingCompat (String id ,
310- String icon ,
311- String action ,
312- String shortLabel ,
313- String longLabel ,
314- int iconType ) {
358+ private ShortcutInfoCompat .Builder buildShortcutUsingCompat (
359+ String id , String icon , String action , String shortLabel ,
360+ String longLabel , int iconType ) {
315361
316362 assert id != null ;
317363 ShortcutInfoCompat .Builder shortcutInfoCompat = new ShortcutInfoCompat .Builder (context , id );
@@ -337,6 +383,7 @@ private ShortcutInfoCompat.Builder buildShortcutUsingCompat(String id,
337383 return shortcutInfoCompat ;
338384 }
339385
386+ /* *********************** ShortcutInfoCompat ******************* */
340387 private void setIconCompat (int iconType ,String icon ,ShortcutInfoCompat .Builder shortcutBuilderCompat ) {
341388 // 0 - ShortcutIconType.androidAsset
342389 // 1 - ShortcutIconType.flutterAsset
@@ -367,6 +414,37 @@ private void setIconFromFlutterCompat(ShortcutInfoCompat.Builder shortcutBuilder
367414 }
368415 }
369416
417+ /* *********************** Person ******************* */
418+ private void setIconCompat (int iconType ,String icon ,Person .Builder personBuilderCompat ) {
419+ // 0 - ShortcutIconType.androidAsset
420+ // 1 - ShortcutIconType.flutterAsset
421+ switch (iconType ) {
422+ case 0 :
423+ setIconFromNativeCompat (personBuilderCompat , icon );
424+ break ;
425+ case 1 :
426+ setIconFromFlutterCompat (personBuilderCompat , icon );
427+ break ;
428+ default :
429+ break ;
430+ }
431+ }
432+
433+ private void setIconFromNativeCompat (Person .Builder personBuilder , String icon ) {
434+ final int resourceId = loadResourceId (context , icon );
435+ if (resourceId > 0 ) {
436+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N_MR1 ) {
437+ personBuilder .setIcon (IconCompat .createFromIcon (context ,Icon .createWithResource (context , resourceId )));
438+ }
439+ }
440+ }
441+
442+ private void setIconFromFlutterCompat (Person .Builder personBuilder , String icon ) {
443+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
444+ personBuilder .setIcon (IconCompat .createFromIcon (context ,getIconFromFlutterAsset (context ,icon )));
445+ }
446+ }
447+
370448 @ RequiresApi (api = Build .VERSION_CODES .O )
371449 private Icon getIconFromFlutterAsset (Context context , String path ) {
372450 AssetManager assetManager = context .getAssets ();
0 commit comments