Skip to content

Commit dfd03ba

Browse files
committed
change shortcut icon
1 parent 548e50c commit dfd03ba

File tree

6 files changed

+95
-26
lines changed

6 files changed

+95
-26
lines changed

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

Lines changed: 78 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import androidx.annotation.RequiresApi;
1616

1717
import java.util.ArrayList;
18+
import java.util.HashMap;
1819
import java.util.List;
1920
import 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);
1.85 KB
Loading

example/lib/main.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ class _MyAppState extends State<MyApp> {
117117
ElevatedButton(
118118
child: Text("Change icon of 2nd Shortcut"),
119119
onPressed: () {
120-
// flutterShortcuts.updateShortcutItemIcon(id, icon);
120+
flutterShortcuts.changeShortcutItemIcon(
121+
id: "2", icon: "bookmark_icon");
121122
},
122123
),
123124
ElevatedButton(

lib/flutter_shortcuts.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,8 @@ class FlutterShortcuts {
3333
}) async {
3434
return FlutterShortcutsPlatform.instance.updateShortcutItem(id, shortcut);
3535
}
36+
37+
Future<void> changeShortcutItemIcon({String id, String icon}) async {
38+
return FlutterShortcutsPlatform.instance.changeShortcutItemIcon(id, icon);
39+
}
3640
}

lib/src/method_call/flutter_shortcuts_method_call_handler.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class FlutterShortcutsMethodCallHandler extends FlutterShortcutsPlatform {
4646
await channel.invokeMethod<void>('updateShortcutItem', [item]);
4747
}
4848

49+
@override
50+
Future<void> changeShortcutItemIcon(String id, String icon) async {
51+
await channel.invokeMethod<void>('changeShortcutItemIcon', [id, icon]);
52+
}
53+
4954
Map<String, String> _serializeItem(FlutterShortcutItem item) {
5055
return <String, String>{
5156
'id': item.id,

lib/src/platform/flutter_shortcuts_platform.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ abstract class FlutterShortcutsPlatform extends PlatformInterface {
3636

3737
Future<void> updateShortcutItem(
3838
String id, FlutterShortcutItem shortcut) async {
39-
throw UnimplementedError("updateShortcutItems() has not been implemented.");
39+
throw UnimplementedError("updateShortcutItem() has not been implemented.");
40+
}
41+
42+
Future<void> changeShortcutItemIcon(String id, String icon) async {
43+
throw UnimplementedError(
44+
"changeShortcutItemIcon() has not been implemented.");
4045
}
4146
}

0 commit comments

Comments
 (0)