Skip to content

Commit 831f0fe

Browse files
committed
1 parent 38d9da6 commit 831f0fe

File tree

3 files changed

+64
-30
lines changed

3 files changed

+64
-30
lines changed

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

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,27 @@
55
import android.content.Intent;
66
import android.content.pm.ShortcutInfo;
77
import android.content.pm.ShortcutManager;
8+
import android.content.res.AssetFileDescriptor;
9+
import android.content.res.AssetManager;
810
import android.content.res.Resources;
11+
import android.graphics.Bitmap;
12+
import android.graphics.BitmapFactory;
13+
import android.graphics.drawable.Drawable;
914
import android.graphics.drawable.Icon;
1015
import android.os.Build;
1116
import android.util.Log;
1217

1318
import androidx.annotation.NonNull;
1419
import androidx.annotation.RequiresApi;
1520

21+
import java.io.IOException;
1622
import java.util.ArrayList;
1723
import java.util.HashMap;
1824
import java.util.List;
1925
import java.util.Map;
2026

27+
import io.flutter.FlutterInjector;
28+
import io.flutter.embedding.engine.loader.FlutterLoader;
2129
import io.flutter.plugin.common.MethodCall;
2230
import io.flutter.plugin.common.MethodChannel;
2331

@@ -276,13 +284,17 @@ private ShortcutInfo createShortcutInfo(Map<String, String> shortcut) {
276284
final ShortcutInfo.Builder shortcutBuilder;
277285
shortcutBuilder = new ShortcutInfo.Builder(context, id);
278286

279-
final int resourceId = loadResourceId(context, icon);
287+
// final int resourceId = loadResourceId(context, icon);
280288
final Intent intent = getIntentToOpenMainActivity(action);
281289

282-
if (resourceId > 0) {
283-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
284-
shortcutBuilder.setIcon(Icon.createWithResource(context, resourceId));
285-
}
290+
// if (resourceId > 0) {
291+
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
292+
// shortcutBuilder.setIcon(Icon.createWithResource(context, resourceId));
293+
// }
294+
// }
295+
296+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
297+
shortcutBuilder.setIcon(getIconFromFlutterAsset(context,icon));
286298
}
287299

288300
if(longLabel != null) {
@@ -296,6 +308,26 @@ private ShortcutInfo createShortcutInfo(Map<String, String> shortcut) {
296308
.build();
297309
}
298310

311+
@RequiresApi(api = Build.VERSION_CODES.O)
312+
private Icon getIconFromFlutterAsset(Context context, String path) {
313+
AssetManager assetManager = context.getAssets();
314+
FlutterLoader loader = FlutterInjector.instance().flutterLoader();
315+
String key = loader.getLookupKeyForAsset(path);
316+
AssetFileDescriptor fd = null;
317+
try {
318+
fd = assetManager.openFd(key);
319+
} catch (IOException e) {
320+
e.printStackTrace();
321+
}
322+
Bitmap image = null;
323+
try {
324+
image = BitmapFactory.decodeStream(fd.createInputStream());
325+
} catch (IOException e) {
326+
e.printStackTrace();
327+
}
328+
return Icon.createWithAdaptiveBitmap(image);
329+
}
330+
299331
private int loadResourceId(Context context, String icon) {
300332
if (icon == null) {
301333
return 0;

example/lib/main.dart

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ class _MyAppState extends State<MyApp> {
8080
id: "1",
8181
action: 'Home page action',
8282
shortLabel: 'Home Page',
83-
icon: 'ic_launcher',
83+
icon: 'assets/icons/home.png',
8484
),
8585
const FlutterShortcutItem(
8686
id: "2",
87-
action: 'Second page action',
88-
shortLabel: 'Second Page',
89-
icon: 'ic_launcher',
87+
action: 'Bookmark page action',
88+
shortLabel: 'Bookmark Page',
89+
icon: 'assets/icons/bookmark.png',
9090
),
9191
],
9292
).then((value) {
@@ -117,8 +117,9 @@ class _MyAppState extends State<MyApp> {
117117
await flutterShortcuts.pushShortcutItem(
118118
shortcut: FlutterShortcutItem(
119119
id: "5",
120-
action: "Fifth action",
121-
shortLabel: "Shortcut 5",
120+
action: "Play Music Action",
121+
shortLabel: "Play Music",
122+
icon: 'assets/icons/music.png',
122123
),
123124
);
124125
},
@@ -130,15 +131,15 @@ class _MyAppState extends State<MyApp> {
130131
shortcutList: <FlutterShortcutItem>[
131132
const FlutterShortcutItem(
132133
id: "1",
133-
action: 'Home page action updated',
134-
shortLabel: 'Home Page updated',
135-
icon: 'ic_launcher',
134+
action: 'Resume playing Action',
135+
shortLabel: 'Resume playing',
136+
icon: 'assets/icons/play.png',
136137
),
137138
const FlutterShortcutItem(
138139
id: "2",
139-
action: 'Second page action updated',
140-
shortLabel: 'Second Page updated',
141-
icon: 'ic_launcher',
140+
action: 'Search Songs Action',
141+
shortLabel: 'Search Songs',
142+
icon: 'assets/icons/search.png',
142143
),
143144
],
144145
);
@@ -156,21 +157,21 @@ class _MyAppState extends State<MyApp> {
156157
shortcutList: <FlutterShortcutItem>[
157158
const FlutterShortcutItem(
158159
id: "1",
159-
action: 'homepage new action',
160+
action: 'Home page new action',
160161
shortLabel: 'Home Page',
161-
icon: 'ic_launcher',
162+
icon: 'assets/icons/home.png',
162163
),
163164
const FlutterShortcutItem(
164165
id: "2",
165-
action: 'second page new action',
166-
shortLabel: 'Second Page',
167-
icon: 'ic_launcher',
166+
action: 'Bookmark page new action',
167+
shortLabel: 'Bookmark Page',
168+
icon: 'assets/icons/bookmark.png',
168169
),
169170
const FlutterShortcutItem(
170171
id: "3",
171-
action: 'Thirdpage',
172-
shortLabel: 'Third Page',
173-
icon: 'ic_launcher',
172+
action: 'Settings Action',
173+
shortLabel: 'Setting',
174+
icon: 'assets/icons/settings.png',
174175
),
175176
]);
176177
},
@@ -181,9 +182,9 @@ class _MyAppState extends State<MyApp> {
181182
flutterShortcuts.updateShortcutItem(
182183
shortcut: FlutterShortcutItem(
183184
id: "1",
184-
action: 'action updated with ID',
185-
shortLabel: 'Homepage',
186-
icon: 'ic_launcher',
185+
action: 'Go to url action',
186+
shortLabel: 'Visit Page',
187+
icon: 'assets/icons/url.png',
187188
),
188189
);
189190
},
@@ -210,7 +211,7 @@ class _MyAppState extends State<MyApp> {
210211
onPressed: () {
211212
flutterShortcuts.changeShortcutItemIcon(
212213
id: "2",
213-
icon: "bookmark_icon",
214+
icon: "assets/icons/next.png",
214215
);
215216
},
216217
),

example/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ flutter:
4040

4141
# To add assets to your application, add an assets section, like this:
4242
assets:
43-
- assets/icons/arrows.png
43+
- assets/icons/
44+
- assets/svg/
4445

4546
# An image asset can refer to one or more resolution-specific "variants", see
4647
# https://flutter.dev/assets-and-images/#resolution-aware.

0 commit comments

Comments
 (0)