Skip to content

Commit 50ab94d

Browse files
committed
get Max Shortcut Limit
1 parent d4c6886 commit 50ab94d

File tree

5 files changed

+228
-138
lines changed

5 files changed

+228
-138
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,21 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
4444
}
4545
ShortcutManager shortcutManager =
4646
(ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
47+
4748
switch (call.method) {
49+
case "getMaxShortcutLimit":
50+
final int maxLimit = getMaxShortcutLimit();
51+
result.success(maxLimit);
52+
break;
4853
case "setShortcutItems":
4954
setShortcutItems(call,shortcutManager);
5055
break;
5156
case "pushShortcutItem":
5257
pushShortcutItem(call,shortcutManager);
5358
break;
59+
case "addShortcutItems":
60+
addShortcutItems(call,shortcutManager);
61+
break;
5462
case "updateAllShortcutItems":
5563
updateAllShortcutItems(call,shortcutManager);
5664
break;
@@ -78,14 +86,21 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
7886
intent.removeExtra(EXTRA_ACTION);
7987
}
8088
result.success(launchAction);
81-
return;
89+
break;
8290
default:
8391
result.notImplemented();
8492
return;
8593
}
8694
result.success(null);
8795
}
8896

97+
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
98+
private int getMaxShortcutLimit() {
99+
ShortcutManager shortcutManager =
100+
(ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
101+
return shortcutManager.getMaxShortcutCountPerActivity();
102+
}
103+
89104
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
90105
private void setShortcutItems(MethodCall call,ShortcutManager shortcutManager) {
91106
List<Map<String, String>> setShortcutItemsArgs = call.arguments();
@@ -101,6 +116,13 @@ private void pushShortcutItem(MethodCall call, ShortcutManager shortcutManager)
101116
shortcutManager.addDynamicShortcuts(shortcutInfo);
102117
}
103118

119+
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
120+
private void addShortcutItems(MethodCall call, ShortcutManager shortcutManager) {
121+
final List<Map<String, String>> args = call.arguments();
122+
List<ShortcutInfo> shortcutInfo = processShortcuts(args);
123+
shortcutManager.addDynamicShortcuts(shortcutInfo);
124+
}
125+
104126
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
105127
private void updateAllShortcutItems(MethodCall call, ShortcutManager shortcutManager) {
106128
List<Map<String, String>> updateAllShortcutArgs = call.arguments();

example/lib/main.dart

Lines changed: 157 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -34,139 +34,174 @@ class _MyAppState extends State<MyApp> {
3434
appBar: AppBar(
3535
title: const Text('Flutter Shortcuts Plugin'),
3636
),
37-
body: Column(
38-
children: [
39-
Center(
40-
child: Text('ShortcutItem action : $action\n'),
41-
),
42-
ElevatedButton(
43-
child: Text("Set Shortcuts"),
44-
onPressed: () async {
45-
await flutterShortcuts.setShortcutItems(
46-
shortcutItems: <FlutterShortcutItem>[
37+
body: SingleChildScrollView(
38+
child: Column(
39+
children: [
40+
Center(
41+
child: Text('ShortcutItem action : $action\n'),
42+
),
43+
ElevatedButton(
44+
child: Text("Get Max Shortcut Limit"),
45+
onPressed: () async {
46+
int result = await flutterShortcuts.getMaxShortcutLimit();
47+
print("======> max: $result");
48+
},
49+
),
50+
ElevatedButton(
51+
child: Text("Set Shortcuts"),
52+
onPressed: () async {
53+
await flutterShortcuts.setShortcutItems(
54+
shortcutItems: <FlutterShortcutItem>[
55+
const FlutterShortcutItem(
56+
id: "1",
57+
action: 'Homepage',
58+
shortLabel: 'Play Ludo',
59+
icon: 'ic_launcher',
60+
),
61+
const FlutterShortcutItem(
62+
id: "2",
63+
action: 'Secondpage',
64+
shortLabel: 'Play Snake Ladder',
65+
icon: 'ic_launcher',
66+
),
67+
],
68+
).then((value) {
69+
setState(() {
70+
if (action == 'No Action') {
71+
action = 'Flutter Shortcuts Ready';
72+
}
73+
});
74+
});
75+
},
76+
),
77+
ElevatedButton(
78+
child: Text("Clear All Shortcuts"),
79+
onPressed: () {
80+
flutterShortcuts.clearShortcutItems();
81+
},
82+
),
83+
ElevatedButton(
84+
child: Text("push Shortcut Item"),
85+
onPressed: () async {
86+
await flutterShortcuts.pushShortcutItem(
87+
shortcut: FlutterShortcutItem(
88+
id: "2",
89+
action: "fifthaction",
90+
shortLabel: "shortLabel",
91+
),
92+
);
93+
},
94+
),
95+
ElevatedButton(
96+
child: Text("Update all shortcuts"),
97+
onPressed: () async {
98+
await flutterShortcuts.updateAllShortcutItems(
99+
shortcutList: <FlutterShortcutItem>[
100+
const FlutterShortcutItem(
101+
id: "1",
102+
action: 'Homepage',
103+
shortLabel: 'Home Page 1',
104+
icon: 'ic_launcher',
105+
),
106+
const FlutterShortcutItem(
107+
id: "2",
108+
action: 'Secondpage',
109+
shortLabel: 'Second Page 2',
110+
icon: 'ic_launcher',
111+
),
112+
]);
113+
},
114+
),
115+
ElevatedButton(
116+
child: Text("Update and add New shortcuts"),
117+
onPressed: () async {
118+
await flutterShortcuts
119+
.addShortcutItems(shortcutList: <FlutterShortcutItem>[
47120
const FlutterShortcutItem(
48121
id: "1",
49122
action: 'Homepage',
50-
shortLabel: 'Play Ludo',
123+
shortLabel: 'Home Page 1',
51124
icon: 'ic_launcher',
52125
),
53126
const FlutterShortcutItem(
54127
id: "2",
55128
action: 'Secondpage',
56-
shortLabel: 'Play Snake Ladder',
129+
shortLabel: 'Second Page 2',
57130
icon: 'ic_launcher',
58131
),
59-
],
60-
).then((value) {
61-
setState(() {
62-
if (action == 'No Action') {
63-
action = 'Flutter Shortcuts Ready';
64-
}
65-
});
66-
});
67-
},
68-
),
69-
ElevatedButton(
70-
child: Text("Clear All Shortcuts"),
71-
onPressed: () {
72-
flutterShortcuts.clearShortcutItems();
73-
},
74-
),
75-
ElevatedButton(
76-
child: Text("push Shortcut Item"),
77-
onPressed: () async {
78-
await flutterShortcuts.pushShortcutItem(
79-
shortcut: FlutterShortcutItem(
80-
id: "5",
81-
action: "fifthaction",
82-
shortLabel: "shortLabel",
83-
),
84-
);
85-
},
86-
),
87-
ElevatedButton(
88-
child: Text("Update all shortcuts"),
89-
onPressed: () async {
90-
await flutterShortcuts
91-
.updateAllShortcutItems(shortcutList: <FlutterShortcutItem>[
92-
const FlutterShortcutItem(
93-
id: "1",
94-
action: 'Homepage',
95-
shortLabel: 'Home Page 1',
96-
icon: 'ic_launcher',
97-
),
98-
const FlutterShortcutItem(
99-
id: "2",
100-
action: 'Secondpage',
101-
shortLabel: 'Second Page 1',
102-
icon: 'ic_launcher',
103-
),
104-
]);
105-
},
106-
),
107-
ElevatedButton(
108-
child: Text("Update 2nd Shortcut"),
109-
onPressed: () {
110-
flutterShortcuts.updateShortcutItem(
111-
id: "1",
112-
shortcut: FlutterShortcutItem(
132+
const FlutterShortcutItem(
133+
id: "3",
134+
action: 'Thirdpage',
135+
shortLabel: 'Third Page 3',
136+
icon: 'ic_launcher',
137+
),
138+
]);
139+
},
140+
),
141+
ElevatedButton(
142+
child: Text("Update 2nd Shortcut"),
143+
onPressed: () {
144+
flutterShortcuts.updateShortcutItem(
113145
id: "1",
114-
action: 'Fourthpage',
115-
shortLabel: 'Fourth Page 4',
116-
icon: 'ic_launcher',
117-
),
118-
);
119-
},
120-
),
121-
ElevatedButton(
122-
child: Text("Change icon of 2nd Shortcut"),
123-
onPressed: () {
124-
flutterShortcuts.changeShortcutItemIcon(
125-
id: "2", icon: "bookmark_icon");
126-
},
127-
),
128-
ElevatedButton(
129-
child: Text("change icon color of 2nd Shortcut"),
130-
onPressed: () {
131-
// disable title
132-
// flutterShortcuts.changeIconColor(id, color);
133-
},
134-
),
135-
ElevatedButton(
136-
child: Text("change icon backgroud color of 2nd Shortcut"),
137-
onPressed: () {
138-
// disable title
139-
// flutterShortcuts.changeIconBackgroundColor(id, color);
140-
},
141-
),
142-
ElevatedButton(
143-
child: Text("set animated icon of 2nd Shortcut"),
144-
onPressed: () {
145-
// disable title
146-
// flutterShortcuts.setAnimatedIcon(id, AnimatedIcon);
147-
},
148-
),
149-
ElevatedButton(
150-
child: Text("set icon backgroud gradient of 2nd Shortcut"),
151-
onPressed: () {
152-
// disable title
153-
// flutterShortcuts.iconBackgroundGradient(id,[start,end], [start color,end color]);
154-
},
155-
),
156-
ElevatedButton(
157-
child: Text("Change title of 2nd Shortcut"),
158-
onPressed: () {
159-
// flutterShortcuts.updateShortcutItemTitle(id, title);
160-
},
161-
),
162-
ElevatedButton(
163-
child: Text("Toggle disable state of 2nd Shortcut"),
164-
onPressed: () {
165-
// disable title
166-
// flutterShortcuts.updateDisableStateShortcutItem(id, state(bool), disable title);
167-
},
168-
),
169-
],
146+
shortcut: FlutterShortcutItem(
147+
id: "1",
148+
action: 'Fourthpage',
149+
shortLabel: 'Fourth Page 4',
150+
icon: 'ic_launcher',
151+
),
152+
);
153+
},
154+
),
155+
ElevatedButton(
156+
child: Text("Change icon of 2nd Shortcut"),
157+
onPressed: () {
158+
flutterShortcuts.changeShortcutItemIcon(
159+
id: "2", icon: "bookmark_icon");
160+
},
161+
),
162+
ElevatedButton(
163+
child: Text("change icon color of 2nd Shortcut"),
164+
onPressed: () {
165+
// disable title
166+
// flutterShortcuts.changeIconColor(id, color);
167+
},
168+
),
169+
ElevatedButton(
170+
child: Text("change icon backgroud color of 2nd Shortcut"),
171+
onPressed: () {
172+
// disable title
173+
// flutterShortcuts.changeIconBackgroundColor(id, color);
174+
},
175+
),
176+
ElevatedButton(
177+
child: Text("set animated icon of 2nd Shortcut"),
178+
onPressed: () {
179+
// disable title
180+
// flutterShortcuts.setAnimatedIcon(id, AnimatedIcon);
181+
},
182+
),
183+
ElevatedButton(
184+
child: Text("set icon backgroud gradient of 2nd Shortcut"),
185+
onPressed: () {
186+
// disable title
187+
// flutterShortcuts.iconBackgroundGradient(id,[start,end], [start color,end color]);
188+
},
189+
),
190+
ElevatedButton(
191+
child: Text("Change title of 2nd Shortcut"),
192+
onPressed: () {
193+
// flutterShortcuts.updateShortcutItemTitle(id, title);
194+
},
195+
),
196+
ElevatedButton(
197+
child: Text("Toggle disable state of 2nd Shortcut"),
198+
onPressed: () {
199+
// disable title
200+
// flutterShortcuts.updateDisableStateShortcutItem(id, state(bool), disable title);
201+
},
202+
),
203+
],
204+
),
170205
),
171206
),
172207
);

0 commit comments

Comments
 (0)