Skip to content

Commit 8bab8f7

Browse files
committed
setShortLabel in progress
1 parent 5a4d67c commit 8bab8f7

File tree

6 files changed

+117
-64
lines changed

6 files changed

+117
-64
lines changed

android/.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</natures>
2323
<filteredResources>
2424
<filter>
25-
<id>1621653958543</id>
25+
<id>0</id>
2626
<name></name>
2727
<type>30</type>
2828
<matcher>

android/src/main/java/com/divyanshushekhar/flutter_shortcuts/MethodCallImplementation.java

Lines changed: 81 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
9191
case "updateShortcutItem":
9292
updateShortcutItem(call);
9393
break;
94+
case "changeShortcutItemShortLabel":
95+
changeShortcutItemShortLabel(call);
96+
break;
9497
case "changeShortcutItemIcon":
9598
changeShortcutItemIcon(call);
9699
break;
@@ -204,14 +207,20 @@ private void updateShortcutItems(MethodCall call) {
204207
private void updateShortcutItem(MethodCall call) {
205208
final List<Map<String, String>> args = call.arguments();
206209
Map<String, String> info = args.get(0);
207-
final String refId = info.get("id");
210+
final String id = info.get("id");
211+
final String icon = info.get("icon");
212+
final String action = info.get("action");
213+
final String shortLabel = info.get("shortLabel");
214+
final String longLabel = info.get("LongLabel");
215+
final int iconType = Integer.parseInt(Objects.requireNonNull(info.get("shortcutIconType")));
216+
208217
List<ShortcutInfoCompat> dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(context);
209218
final List<ShortcutInfoCompat> shortcutList = new ArrayList<>();
210219
int flag = 1;
211220
for(ShortcutInfoCompat si : dynamicShortcuts) {
212-
if(si.getId().equalsIgnoreCase(refId)) {
213-
ShortcutInfoCompat shortcutInfo = buildShortcutUsingCompat(info);
214-
shortcutList.add(shortcutInfo);
221+
if(si.getId().equals(id)) {
222+
ShortcutInfoCompat.Builder shortcutInfo = buildShortcutUsingCompat(id,icon,action,shortLabel,longLabel,iconType);
223+
shortcutList.add(shortcutInfo.build());
215224
flag = 0;
216225
continue;
217226
}
@@ -229,24 +238,64 @@ private void updateShortcutItem(MethodCall call) {
229238
}
230239
}
231240

241+
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
242+
@SuppressLint("RestrictedApi")
243+
private void changeShortcutItemShortLabel(MethodCall call) {
244+
final List<String> args = call.arguments();
245+
final String refId = args.get(0);
246+
final String shortLabel = args.get(1);
247+
248+
List<ShortcutInfoCompat> dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(context);
249+
final List<ShortcutInfoCompat> shortcutList = new ArrayList<>();
250+
251+
int flag = 1;
252+
for(ShortcutInfoCompat si : dynamicShortcuts) {
253+
String id = si.getId();
254+
if(id.equals(refId)) {
255+
String longLabel = (String) si.getLongLabel();
256+
ShortcutInfoCompat.Builder shortcutInfoCompat = buildShortcutUsingCompat(id,null,null,shortLabel,longLabel,0);
257+
shortcutInfoCompat.setIcon(si.getIcon()).setIntent(si.getIntent());
258+
shortcutList.add(shortcutInfoCompat.build());
259+
flag = 0;
260+
continue;
261+
}
262+
shortcutList.add(si);
263+
}
264+
265+
if (flag == 1) {
266+
Log.e(TAG, "ID did not match any shortcut");
267+
return;
268+
}
269+
try {
270+
ShortcutManagerCompat.updateShortcuts(context,shortcutList);
271+
debugPrint("Shortcut Short Label Changed.");
272+
} catch(Exception e) {
273+
Log.e(TAG,e.toString());
274+
}
275+
}
276+
232277
@SuppressLint("RestrictedApi")
233278
@RequiresApi(api = Build.VERSION_CODES.O)
234279
private void changeShortcutItemIcon(MethodCall call) {
235280
try {
236281
final List<String> args = call.arguments();
237282
final String refId = args.get(0);
238283
final String changeIcon = args.get(1);
239-
Map<String,String> items = shortcutWithoutIcon(refId);
240-
ShortcutInfoCompat.Builder shortcutInfo = createShortcutInfoUsingIconResID(items);
241284
Icon icon = getIconFromFlutterAsset(context,changeIcon);
285+
IconCompat iconCompat = IconCompat.createFromIcon(context,icon);
242286
List<ShortcutInfoCompat> dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(context);
243287

244288
final List<ShortcutInfoCompat> shortcutList = new ArrayList<>();
245289
int flag = 1;
246290
for(ShortcutInfoCompat si : dynamicShortcuts) {
247-
if(si.getId().equalsIgnoreCase(refId)) {
248-
IconCompat iconCompat = IconCompat.createFromIcon(context,icon);
249-
shortcutList.add(shortcutInfo.setIcon(iconCompat).build());
291+
String id = si.getId();
292+
if(id.equals(refId)) {
293+
String shortLabel = (String) si.getShortLabel();
294+
String longLabel = (String) si.getLongLabel();
295+
296+
ShortcutInfoCompat.Builder shortcutInfo = buildShortcutUsingCompat(id,null,null,shortLabel,longLabel,0);
297+
shortcutInfo.setIcon(iconCompat).setIntent(si.getIntent());
298+
shortcutList.add(shortcutInfo.build());
250299
flag = 0;
251300
continue;
252301
}
@@ -270,79 +319,48 @@ private void changeShortcutItemIcon(MethodCall call) {
270319

271320
/* ******************** Utility Functions ********************* */
272321

273-
@SuppressLint("RestrictedApi")
274-
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
275-
private Map<String,String> shortcutWithoutIcon(String id) {
276-
HashMap<String, String> map = new HashMap<String, String>();
277-
List<ShortcutInfoCompat> dynamicShortcuts = ShortcutManagerCompat.getDynamicShortcuts(context);
278-
for(ShortcutInfoCompat si : dynamicShortcuts) {
279-
if(si.getId().equalsIgnoreCase(id)) {
280-
map.put("id", si.getId());
281-
map.put("shortLabel", (String) si.getShortLabel());
282-
map.put("longLabel", (String) si.getLongLabel());
283-
map.put("action",si.getIntent().getStringExtra(EXTRA_ACTION));
284-
}
285-
}
286-
return map;
287-
}
288-
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-
311322
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
312323
private List<ShortcutInfoCompat> shortcutInfoCompatList(List<Map<String, String>> shortcuts) {
313324
final List<ShortcutInfoCompat> shortcutList = new ArrayList<>();
314325

315326
for (Map<String, String> shortcut : shortcuts) {
316-
ShortcutInfoCompat shortcutInfoCompat = buildShortcutUsingCompat(shortcut);
317-
shortcutList.add(shortcutInfoCompat);
327+
final String id = shortcut.get("id");
328+
final String icon = shortcut.get("icon");
329+
final String action = shortcut.get("action");
330+
final String shortLabel = shortcut.get("shortLabel");
331+
final String longLabel = shortcut.get("LongLabel");
332+
final int iconType = Integer.parseInt(Objects.requireNonNull(shortcut.get("shortcutIconType")));
333+
ShortcutInfoCompat.Builder shortcutInfoCompat = buildShortcutUsingCompat(id,icon,action,shortLabel,longLabel,iconType);
334+
shortcutList.add(shortcutInfoCompat.build());
318335
}
319336
return shortcutList;
320337
}
321338

322339
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
323-
private ShortcutInfoCompat buildShortcutUsingCompat(Map<String, String> shortcut) {
324-
final String id = shortcut.get("id");
325-
final String icon = shortcut.get("icon");
326-
final String action = shortcut.get("action");
327-
final String shortLabel = shortcut.get("shortLabel");
328-
final String longLabel = shortcut.get("LongLabel");
329-
final int iconType = Integer.parseInt(Objects.requireNonNull(shortcut.get("shortcutIconType")));
340+
private ShortcutInfoCompat.Builder buildShortcutUsingCompat(String id, String icon, String action, String shortLabel, String longLabel, int iconType) {
330341

331342
assert id != null;
332343
ShortcutInfoCompat.Builder shortcutInfoCompat = new ShortcutInfoCompat.Builder(context, id);
333344

334-
final Intent intent = getIntentToOpenMainActivity(action);
345+
Intent intent = null;
346+
if(action != null) {
347+
intent = getIntentToOpenMainActivity(action);
348+
shortcutInfoCompat.setIntent(intent);
349+
}
335350

336351
if(longLabel != null) {
337352
shortcutInfoCompat.setLongLabel(longLabel);
338353
}
339-
setIconCompat(iconType, icon,shortcutInfoCompat);
340354

341-
assert shortLabel!=null;
342-
return shortcutInfoCompat
343-
.setShortLabel(shortLabel)
344-
.setIntent(intent)
345-
.build();
355+
if(icon != null) {
356+
setIconCompat(iconType, icon, shortcutInfoCompat);
357+
}
358+
359+
if(shortLabel != null) {
360+
shortcutInfoCompat.setShortLabel(shortLabel);
361+
}
362+
363+
return shortcutInfoCompat;
346364
}
347365

348366
private void setIconCompat(int iconType,String icon,ShortcutInfoCompat.Builder shortcutBuilderCompat) {

example/lib/main.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,20 @@ class _MyAppState extends State<MyApp> {
222222
],
223223
),
224224
Divider(),
225+
Text("Label"),
226+
Row(
227+
children: [
228+
ElevatedButton(
229+
child: Text("Change ShortLabel"),
230+
onPressed: () async {
231+
await flutterShortcuts.changeShortcutItemShortLabel(
232+
id: "2",
233+
shortLabel: "Resume Game",
234+
);
235+
},
236+
),
237+
],
238+
),
225239
],
226240
),
227241
),

lib/flutter_shortcuts.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ class FlutterShortcuts {
7777
return FlutterShortcutsPlatform.instance.updateShortcutItem(shortcut);
7878
}
7979

80+
Future<void> changeShortcutItemShortLabel({
81+
required String id,
82+
required String shortLabel,
83+
}) async {
84+
return FlutterShortcutsPlatform.instance
85+
.changeShortcutItemShortLabel(id, shortLabel);
86+
}
87+
8088
/// [changeShortcutItemIcon] will change the icon of the shortcut based on id.
8189
/// If the ID of the shortcut is not same, no changes will be reflected.
8290
Future<void> changeShortcutItemIcon(

lib/src/method_call/flutter_shortcuts_method_call_handler.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ class FlutterShortcutsMethodCallHandler extends FlutterShortcutsPlatform {
8989
await channel.invokeMethod<void>('updateShortcutItem', [item]);
9090
}
9191

92+
@override
93+
Future<void> changeShortcutItemShortLabel(
94+
String id, String shortLabel) async {
95+
await channel
96+
.invokeMethod<void>('changeShortcutItemShortLabel', [id, shortLabel]);
97+
}
98+
9299
@override
93100
Future<void> changeShortcutItemIcon(String id, String icon) async {
94101
await channel.invokeMethod<void>('changeShortcutItemIcon', [id, icon]);

lib/src/platform/flutter_shortcuts_platform.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ abstract class FlutterShortcutsPlatform extends PlatformInterface {
6868
throw UnimplementedError("updateShortcutItem() has not been implemented.");
6969
}
7070

71+
Future<void> changeShortcutItemShortLabel(
72+
String id, String shortLabel) async {
73+
throw UnimplementedError(
74+
"changeShortcutItemShortLabel() has not been implemented.");
75+
}
76+
7177
Future<void> changeShortcutItemIcon(String id, String icon) async {
7278
throw UnimplementedError(
7379
"changeShortcutItemIcon() has not been implemented.");

0 commit comments

Comments
 (0)