Skip to content

Commit ee28e3f

Browse files
committed
v0.0.1
1 parent 9fe0b16 commit ee28e3f

File tree

5 files changed

+128
-135
lines changed

5 files changed

+128
-135
lines changed

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

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
import android.app.Activity;
44
import android.content.Context;
55
import android.content.Intent;
6+
import android.content.pm.LauncherApps;
67
import android.content.pm.ShortcutInfo;
78
import android.content.pm.ShortcutManager;
89
import android.content.res.Resources;
10+
import android.graphics.drawable.Drawable;
911
import android.graphics.drawable.Icon;
1012
import android.os.Build;
13+
import android.os.PersistableBundle;
1114
import android.util.Log;
1215
import android.widget.Toast;
1316

1417
import androidx.annotation.NonNull;
1518
import androidx.annotation.RequiresApi;
1619

20+
import java.io.InputStream;
1721
import java.util.ArrayList;
1822
import java.util.HashMap;
1923
import java.util.List;
@@ -62,15 +66,18 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
6266
case "pushShortcutItem":
6367
pushShortcutItem(call,shortcutManager);
6468
break;
65-
case "addShortcutItems":
66-
addShortcutItems(call,shortcutManager);
69+
case "pushShortcutItems":
70+
pushShortcutItems(call,shortcutManager);
6771
break;
6872
case "updateShortcutItems":
6973
updateShortcutItems(call,shortcutManager);
7074
break;
7175
case "updateShortcutItem":
7276
updateShortcutItem(call,shortcutManager);
7377
break;
78+
case "updateShortLabel":
79+
updateShortLabel(call,shortcutManager);
80+
break;
7481
case "changeShortcutItemIcon":
7582
changeShortcutItemIcon(call,shortcutManager);
7683
break;
@@ -142,7 +149,7 @@ private void pushShortcutItem(MethodCall call, ShortcutManager shortcutManager)
142149
}
143150

144151
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
145-
private void addShortcutItems(MethodCall call, ShortcutManager shortcutManager) {
152+
private void pushShortcutItems(MethodCall call, ShortcutManager shortcutManager) {
146153
final List<Map<String, String>> args = call.arguments();
147154
List<ShortcutInfo> shortcuts;
148155
try {
@@ -198,30 +205,41 @@ private void updateShortcutItem(MethodCall call, ShortcutManager shortcutManager
198205
}
199206
}
200207

201-
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
202-
private void changeShortcutItemIcon(MethodCall call, ShortcutManager shortcutManager) {
208+
private void updateShortLabel(MethodCall call, ShortcutManager shortcutManager) {
203209
final List<String> args = call.arguments();
204210
final String refId = args.get(0);
205-
final String changeIcon = args.get(1);
206-
Map<String,String> items = deserializeShortcutInfoAtId(refId,changeIcon,shortcutManager);
207-
ShortcutInfo shortcutInfo = createShortcutInfo(items);
208-
List<ShortcutInfo> dynamicShortcuts = shortcutManager.getDynamicShortcuts();
209-
final List<ShortcutInfo> shortcutList = new ArrayList<>();
210-
int flag = 1;
211-
for(ShortcutInfo si : dynamicShortcuts) {
212-
if(si.getId().equalsIgnoreCase(refId)) {
213-
shortcutList.add(shortcutInfo);
214-
flag = 0;
215-
continue;
216-
}
217-
shortcutList.add(si);
218-
}
219-
if (flag == 1) {
220-
Log.e(TAG, "ID did not match any shortcut");
221-
return;
222-
}
211+
final String title = args.get(1);
212+
213+
}
214+
215+
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
216+
private void changeShortcutItemIcon(MethodCall call, ShortcutManager shortcutManager) {
223217
try {
224-
shortcutManager.updateShortcuts(shortcutList);
218+
final List<String> args = call.arguments();
219+
final String refId = args.get(0);
220+
final String changeIcon = args.get(1);
221+
Map<String,String> items = deserializeShortcutInfoAtId(refId,changeIcon,shortcutManager);
222+
ShortcutInfo shortcutInfo = createShortcutInfo(items);
223+
List<ShortcutInfo> dynamicShortcuts = shortcutManager.getDynamicShortcuts();
224+
final List<ShortcutInfo> shortcutList = new ArrayList<>();
225+
int flag = 1;
226+
for(ShortcutInfo si : dynamicShortcuts) {
227+
if(si.getId().equalsIgnoreCase(refId)) {
228+
shortcutList.add(shortcutInfo);
229+
flag = 0;
230+
continue;
231+
}
232+
shortcutList.add(si);
233+
}
234+
if (flag == 1) {
235+
Log.e(TAG, "ID did not match any shortcut");
236+
return;
237+
}
238+
try {
239+
shortcutManager.updateShortcuts(shortcutList);
240+
} catch(Exception e) {
241+
Log.e(TAG,e.toString());
242+
}
225243
} catch(Exception e) {
226244
Log.e(TAG,e.toString());
227245
}
@@ -235,7 +253,7 @@ private Map<String,String> deserializeShortcutInfoAtId(String id, String icon, S
235253
for(ShortcutInfo si : dynamicShortcuts) {
236254
if(si.getId().equalsIgnoreCase(id)) {
237255
map.put("id", si.getId());
238-
map.put("title", String.valueOf(si.getShortLabel()));
256+
map.put("shortLabel", String.valueOf(si.getShortLabel()));
239257
map.put("icon", icon);
240258
map.put("action",si.getIntent().getStringExtra(EXTRA_ACTION));
241259
}

example/lib/main.dart

Lines changed: 28 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class MyApp extends StatefulWidget {
1313
class _MyAppState extends State<MyApp> {
1414
String action = 'No Action';
1515
final FlutterShortcuts flutterShortcuts = FlutterShortcuts();
16+
int maxLimit;
1617

1718
@override
1819
void initState() {
@@ -52,15 +53,18 @@ class _MyAppState extends State<MyApp> {
5253
),
5354
),
5455
),
55-
Text("Flutter Shortcuts Max Limit"),
56+
Text(
57+
"Flutter Shortcuts Max Limit -> ${maxLimit == null ? '' : maxLimit}"),
5658
Row(
5759
children: [
5860
ElevatedButton(
5961
child: Text("Get Max Shortcut Limit"),
6062
onPressed: () async {
6163
int result =
6264
await flutterShortcuts.getMaxShortcutLimit();
63-
print("======> max: $result");
65+
setState(() {
66+
maxLimit = result;
67+
});
6468
},
6569
),
6670
],
@@ -77,14 +81,14 @@ class _MyAppState extends State<MyApp> {
7781
shortcutItems: <FlutterShortcutItem>[
7882
const FlutterShortcutItem(
7983
id: "1",
80-
action: 'Homepage',
81-
shortLabel: 'Play Ludo',
84+
action: 'Home page action',
85+
shortLabel: 'Home Page',
8286
icon: 'ic_launcher',
8387
),
8488
const FlutterShortcutItem(
8589
id: "2",
86-
action: 'Secondpage',
87-
shortLabel: 'Play Snake Ladder',
90+
action: 'Second page action',
91+
shortLabel: 'Second Page',
8892
icon: 'ic_launcher',
8993
),
9094
],
@@ -115,9 +119,9 @@ class _MyAppState extends State<MyApp> {
115119
onPressed: () async {
116120
await flutterShortcuts.pushShortcutItem(
117121
shortcut: FlutterShortcutItem(
118-
id: "2",
119-
action: "fifthaction",
120-
shortLabel: "shortLabel",
122+
id: "5",
123+
action: "Fifth action",
124+
shortLabel: "Shortcut 5",
121125
),
122126
);
123127
},
@@ -129,14 +133,14 @@ class _MyAppState extends State<MyApp> {
129133
shortcutList: <FlutterShortcutItem>[
130134
const FlutterShortcutItem(
131135
id: "1",
132-
action: 'Homepage',
133-
shortLabel: 'Home Page 1',
136+
action: 'Home page action updated',
137+
shortLabel: 'Home Page updated',
134138
icon: 'ic_launcher',
135139
),
136140
const FlutterShortcutItem(
137141
id: "2",
138-
action: 'Secondpage',
139-
shortLabel: 'Second Page 2',
142+
action: 'Second page action updated',
143+
shortLabel: 'Second Page updated',
140144
icon: 'ic_launcher',
141145
),
142146
],
@@ -151,24 +155,24 @@ class _MyAppState extends State<MyApp> {
151155
ElevatedButton(
152156
child: Text("Add Shortcut"),
153157
onPressed: () async {
154-
await flutterShortcuts.addShortcutItems(
158+
await flutterShortcuts.pushShortcutItems(
155159
shortcutList: <FlutterShortcutItem>[
156160
const FlutterShortcutItem(
157161
id: "1",
158-
action: 'Homepage',
159-
shortLabel: 'Home Page 1',
162+
action: 'homepage new action',
163+
shortLabel: 'Home Page',
160164
icon: 'ic_launcher',
161165
),
162166
const FlutterShortcutItem(
163167
id: "2",
164-
action: 'Secondpage',
165-
shortLabel: 'Second Page 2',
168+
action: 'second page new action',
169+
shortLabel: 'Second Page',
166170
icon: 'ic_launcher',
167171
),
168172
const FlutterShortcutItem(
169173
id: "3",
170174
action: 'Thirdpage',
171-
shortLabel: 'Third Page 3',
175+
shortLabel: 'Third Page',
172176
icon: 'ic_launcher',
173177
),
174178
]);
@@ -181,8 +185,8 @@ class _MyAppState extends State<MyApp> {
181185
id: "1",
182186
shortcut: FlutterShortcutItem(
183187
id: "1",
184-
action: 'Fourthpage',
185-
shortLabel: 'Fourth Page 4',
188+
action: 'action updated with ID',
189+
shortLabel: 'Homepage',
186190
icon: 'ic_launcher',
187191
),
188192
);
@@ -191,82 +195,22 @@ class _MyAppState extends State<MyApp> {
191195
],
192196
),
193197
Divider(),
194-
Text("Long/Short Label"),
195-
Row(
196-
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
197-
children: [
198-
ElevatedButton(
199-
child: Text("Change Short Title"),
200-
onPressed: () {
201-
// flutterShortcuts.updateShortcutItemTitle(id, title);
202-
},
203-
),
204-
ElevatedButton(
205-
child: Text("Change Long Title"),
206-
onPressed: () {
207-
// flutterShortcuts.updateShortcutItemTitle(id, title);
208-
},
209-
),
210-
],
211-
),
212-
Divider(),
213198
Text("Icons"),
214199
Row(
215200
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
216201
children: [
217-
ElevatedButton(
218-
child: Text("Icon Properties"),
219-
onPressed: () async {
220-
Map<String, int> properties =
221-
await flutterShortcuts.getIconProperties();
222-
print(properties);
223-
},
224-
),
225202
ElevatedButton(
226203
child: Text("Change icon of 2nd Shortcut"),
227204
onPressed: () {
228205
flutterShortcuts.changeShortcutItemIcon(
229-
id: "2", icon: "bookmark_icon");
206+
id: "2",
207+
icon: "bookmark_icon",
208+
);
230209
},
231210
),
232211
],
233212
),
234213
Divider(),
235-
ElevatedButton(
236-
child: Text("change icon color of 2nd Shortcut"),
237-
onPressed: () {
238-
// disable title
239-
// flutterShortcuts.changeIconColor(id, color);
240-
},
241-
),
242-
ElevatedButton(
243-
child: Text("change icon backgroud color of 2nd Shortcut"),
244-
onPressed: () {
245-
// disable title
246-
// flutterShortcuts.changeIconBackgroundColor(id, color);
247-
},
248-
),
249-
ElevatedButton(
250-
child: Text("set animated icon of 2nd Shortcut"),
251-
onPressed: () {
252-
// disable title
253-
// flutterShortcuts.setAnimatedIcon(id, AnimatedIcon);
254-
},
255-
),
256-
ElevatedButton(
257-
child: Text("set icon backgroud gradient of 2nd Shortcut"),
258-
onPressed: () {
259-
// disable title
260-
// flutterShortcuts.iconBackgroundGradient(id,[start,end], [start color,end color]);
261-
},
262-
),
263-
ElevatedButton(
264-
child: Text("Toggle disable state of 2nd Shortcut"),
265-
onPressed: () {
266-
// disable title
267-
// flutterShortcuts.updateDisableStateShortcutItem(id, state(bool), disable title);
268-
},
269-
),
270214
],
271215
),
272216
),

lib/flutter_shortcuts.dart

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ class FlutterShortcuts {
2727
return FlutterShortcutsPlatform.instance.getMaxShortcutLimit();
2828
}
2929

30-
/// [getIconProperties] returns the "maxHeight" and "maxWidth" of the shortcut icon.
31-
/// Example: {"maxHeight": 250, "maxWidth": 200}
32-
Future<Map<String, int>> getIconProperties() {
33-
return FlutterShortcutsPlatform.instance.getIconProperties();
34-
}
30+
// /// [getIconProperties] returns the "maxHeight" and "maxWidth" of the shortcut icon.
31+
// /// Example: {"maxHeight": 250, "maxWidth": 200}
32+
// Future<Map<String, int>> getIconProperties() {
33+
// return FlutterShortcutsPlatform.instance.getIconProperties();
34+
// }
3535

3636
/// [setShortcutItems] will set all the shortcut items.
3737
Future<void> setShortcutItems(
@@ -53,9 +53,9 @@ class FlutterShortcuts {
5353

5454
/// [addShortcutItems] updates dynamic or pinned shortcuts with same IDs
5555
/// and pushes new shortcuts with different IDs.
56-
Future<void> addShortcutItems(
56+
Future<void> pushShortcutItems(
5757
{List<FlutterShortcutItem> shortcutList}) async {
58-
return FlutterShortcutsPlatform.instance.addShortcutItems(shortcutList);
58+
return FlutterShortcutsPlatform.instance.pushShortcutItems(shortcutList);
5959
}
6060

6161
/// [updateShortcutItems] updates shortcut items.
@@ -72,6 +72,18 @@ class FlutterShortcuts {
7272
return FlutterShortcutsPlatform.instance.updateShortcutItem(id, shortcut);
7373
}
7474

75+
// /// [updateShortLabel] updates short label of the shortcut item based on id.
76+
// /// If the ID of the shortcut is not same, no changes will be reflected.
77+
// Future<void> updateShortLabel({String id, String shortLabel}) async {
78+
// return FlutterShortcutsPlatform.instance.updateShortLabel(id, shortLabel);
79+
// }
80+
81+
// /// [updateLongLabel] updates long label of the shortcut item based on id.
82+
// /// If the ID of the shortcut is not same, no changes will be reflected.
83+
// Future<void> updateLongLabel({String id, String longLabel}) async {
84+
// return FlutterShortcutsPlatform.instance.updateLongLabel(id, longLabel);
85+
// }
86+
7587
/// [changeShortcutItemIcon] will change the icon of the shortcut based on id.
7688
/// If the ID of the shortcut is not same, no changes will be reflected.
7789
Future<void> changeShortcutItemIcon({String id, String icon}) async {

0 commit comments

Comments
 (0)