Skip to content

Commit 6d9de31

Browse files
committed
#5 #6
1 parent b75068a commit 6d9de31

File tree

5 files changed

+79
-43
lines changed

5 files changed

+79
-43
lines changed

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

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import android.content.res.Resources;
1111
import android.graphics.Bitmap;
1212
import android.graphics.BitmapFactory;
13-
import android.graphics.drawable.Drawable;
1413
import android.graphics.drawable.Icon;
1514
import android.os.Build;
1615
import android.util.Log;
@@ -23,6 +22,7 @@
2322
import java.util.HashMap;
2423
import java.util.List;
2524
import java.util.Map;
25+
import java.util.Objects;
2626

2727
import io.flutter.FlutterInjector;
2828
import io.flutter.embedding.engine.loader.FlutterLoader;
@@ -36,6 +36,14 @@ public class MethodCallImplementation implements MethodChannel.MethodCallHandler
3636
private final Context context;
3737
private Activity activity;
3838

39+
private boolean debug;
40+
41+
void debugPrint(String message) {
42+
if(debug) {
43+
Log.d(TAG,message);
44+
}
45+
}
46+
3947
MethodCallImplementation(Context context, Activity activity) {
4048
this.context = context;
4149
this.activity = activity;
@@ -55,6 +63,12 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
5563
(ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
5664

5765
switch (call.method) {
66+
case "initialize":
67+
initialize(call);
68+
break;
69+
case "getLaunchAction":
70+
getLaunchAction(shortcutManager,result);
71+
break;
5872
case "getMaxShortcutLimit":
5973
final int maxLimit = getMaxShortcutLimit();
6074
result.success(maxLimit);
@@ -87,28 +101,37 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
87101
case "clearShortcutItems":
88102
shortcutManager.removeAllDynamicShortcuts();
89103
break;
90-
case "getLaunchAction":
91-
if (activity == null) {
92-
result.error(
93-
"flutter_shortcuts_no_activity",
94-
"There is no activity available when launching action",
95-
null);
96-
return;
97-
}
98-
final Intent intent = activity.getIntent();
99-
final String launchAction = intent.getStringExtra(EXTRA_ACTION);
100-
if (launchAction != null && !launchAction.isEmpty()) {
101-
shortcutManager.reportShortcutUsed(launchAction);
102-
intent.removeExtra(EXTRA_ACTION);
103-
}
104-
result.success(launchAction);
105-
break;
106104
default:
107105
result.notImplemented();
108106
break;
109107
}
110108
}
111109

110+
private void initialize(MethodCall call) {
111+
List<Map<String, String>> args = call.arguments();
112+
this.debug = Boolean.parseBoolean(args.get(0).get("debug"));
113+
debugPrint("Flutter Shortcuts Initialized");
114+
}
115+
116+
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
117+
private void getLaunchAction(ShortcutManager shortcutManager, MethodChannel.Result result) {
118+
if (activity == null) {
119+
result.error(
120+
"flutter_shortcuts_no_activity",
121+
"There is no activity available when launching action",
122+
null);
123+
return;
124+
}
125+
final Intent intent = activity.getIntent();
126+
final String launchAction = intent.getStringExtra(EXTRA_ACTION);
127+
if (launchAction != null && !launchAction.isEmpty()) {
128+
shortcutManager.reportShortcutUsed(launchAction);
129+
intent.removeExtra(EXTRA_ACTION);
130+
}
131+
result.success(launchAction);
132+
debugPrint("Launch Action: " + launchAction);
133+
}
134+
112135
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
113136
private int getMaxShortcutLimit() {
114137
ShortcutManager shortcutManager =
@@ -128,11 +151,12 @@ private Map<String, Integer> getIconProperties() {
128151

129152
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
130153
private void setShortcutItems(MethodCall call,ShortcutManager shortcutManager) {
131-
List<Map<String, String>> setShortcutItemsArgs = call.arguments();
154+
List<Map<String, String>> args = call.arguments();
132155
List<ShortcutInfo> shortcuts;
133156
try {
134-
shortcuts = processShortcuts(setShortcutItemsArgs);
157+
shortcuts = processShortcuts(args);
135158
shortcutManager.setDynamicShortcuts(shortcuts);
159+
debugPrint("Shortcuts created");
136160
} catch (Exception e) {
137161
Log.e(TAG,e.toString());
138162
}
@@ -145,6 +169,7 @@ private void pushShortcutItem(MethodCall call, ShortcutManager shortcutManager)
145169
try {
146170
shortcuts = processShortcuts(args);
147171
shortcutManager.addDynamicShortcuts(shortcuts);
172+
debugPrint("Shortcut pushed");
148173
} catch (Exception e) {
149174
Log.e(TAG,e.toString());
150175
}
@@ -157,25 +182,26 @@ private void pushShortcutItems(MethodCall call, ShortcutManager shortcutManager)
157182
try {
158183
shortcuts = processShortcuts(args);
159184
shortcutManager.addDynamicShortcuts(shortcuts);
185+
debugPrint("Shortcuts pushed");
160186
} catch (Exception e) {
161187
Log.e(TAG,e.toString());
162188
}
163189
}
164190

165191
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
166192
private void updateShortcutItems(MethodCall call, ShortcutManager shortcutManager) {
167-
List<Map<String, String>> updateAllShortcutArgs = call.arguments();
193+
List<Map<String, String>> args = call.arguments();
168194
boolean updated = false;
169195
try {
170-
List<ShortcutInfo> updateShortcuts = processShortcuts(updateAllShortcutArgs);
196+
List<ShortcutInfo> updateShortcuts = processShortcuts(args);
171197
updated = shortcutManager.updateShortcuts(updateShortcuts);
172198
} catch(Exception e) {
173199
Log.e(TAG, e.toString());
174200
}
175201
if(updated) {
176-
Log.d(TAG,"All Shortcuts updated");
202+
debugPrint("Shortcuts updated");
177203
} else {
178-
Log.d(TAG,"Unable to update all shortcuts");
204+
debugPrint("Unable to update shortcuts");
179205
}
180206
}
181207

@@ -202,6 +228,7 @@ private void updateShortcutItem(MethodCall call, ShortcutManager shortcutManager
202228
}
203229
try {
204230
shortcutManager.updateShortcuts(shortcutList);
231+
debugPrint("Shortcut updated");
205232
} catch(Exception e) {
206233
Log.e(TAG,e.toString());
207234
}
@@ -211,7 +238,6 @@ private void updateShortLabel(MethodCall call, ShortcutManager shortcutManager)
211238
final List<String> args = call.arguments();
212239
final String refId = args.get(0);
213240
final String title = args.get(1);
214-
215241
}
216242

217243
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
@@ -239,6 +265,7 @@ private void changeShortcutItemIcon(MethodCall call, ShortcutManager shortcutMan
239265
}
240266
try {
241267
shortcutManager.updateShortcuts(shortcutList);
268+
debugPrint("Shortcut Icon Changed.");
242269
} catch(Exception e) {
243270
Log.e(TAG,e.toString());
244271
}
@@ -281,7 +308,7 @@ private ShortcutInfo createShortcutInfo(Map<String, String> shortcut) {
281308
final String action = shortcut.get("action");
282309
final String shortLabel = shortcut.get("shortLabel");
283310
final String longLabel = shortcut.get("LongLabel");
284-
final int iconType = Integer.parseInt(shortcut.get("shortcutIconType"));
311+
final int iconType = Integer.parseInt(Objects.requireNonNull(shortcut.get("shortcutIconType")));
285312
final ShortcutInfo.Builder shortcutBuilder;
286313
shortcutBuilder = new ShortcutInfo.Builder(context, id);
287314

@@ -340,6 +367,7 @@ private Icon getIconFromFlutterAsset(Context context, String path) {
340367
}
341368
Bitmap image = null;
342369
try {
370+
assert fd != null;
343371
image = BitmapFactory.decodeStream(fd.createInputStream());
344372
} catch (IOException e) {
345373
e.printStackTrace();

lib/flutter_shortcuts.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ import 'package:flutter_shortcuts/src/helper/helper.dart';
1616
export 'package:flutter_shortcuts/src/helper/helper.dart';
1717

1818
class FlutterShortcuts {
19-
/// [initialize] performs action when shortcut is initiated.
20-
Future<void> initialize(FlutterShortcutAction action) async {
21-
FlutterShortcutsPlatform.instance.initialize(action);
19+
/// [initialize] initializes the flutter_shortcuts plugin.
20+
Future<void> initialize({bool debug = true}) async {
21+
FlutterShortcutsPlatform.instance.initialize(debug);
22+
}
23+
24+
/// [listenAction] performs action when shortcut is initiated.
25+
Future<void> listenAction(FlutterShortcutAction action) async {
26+
FlutterShortcutsPlatform.instance.listenAction(action);
2227
}
2328

2429
/// [getMaxShortcutLimit] returns the maximum number of static or dynamic
@@ -51,7 +56,7 @@ class FlutterShortcuts {
5156
return FlutterShortcutsPlatform.instance.pushShortcutItem(shortcut);
5257
}
5358

54-
/// [addShortcutItems] updates dynamic or pinned shortcuts with same IDs
59+
/// [pushShortcutItems] updates dynamic or pinned shortcuts with same IDs
5560
/// and pushes new shortcuts with different IDs.
5661
Future<void> pushShortcutItems(
5762
{required List<FlutterShortcutItem> shortcutList}) async {

lib/src/helper/model/flutter_shortcut_item.dart

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,12 @@ import '../enums/shortcut_icon_asset.dart';
1313
class FlutterShortcutItem {
1414
/// Create a flutter shortcut item.
1515
/// Eg.
16-
/// ```
16+
/// ```dart
1717
/// const FlutterShortcutItem(
18-
/// ```
19-
/// ```
2018
/// id: "1",
21-
/// ```
22-
/// ```
2319
/// action: 'Home page action',
24-
/// ```
25-
/// ```
2620
/// shortLabel: 'Home Page',
27-
/// ```
28-
/// ```
2921
/// icon: 'assets/icons/home.png',
30-
/// ```
31-
/// ```
3222
/// );
3323
/// ```
3424
@@ -56,6 +46,6 @@ class FlutterShortcutItem {
5646
/// Flutter asset path. Only Supports image files. Eg. .png/.jpg
5747
final String? icon;
5848

59-
/// `ShortcutIconType.native` or `ShortcutIconType.flutterAsset`
49+
/// `ShortcutIconType.androidAsset` or `ShortcutIconType.flutterAsset`
6050
final ShortcutIconAsset shortcutIconAsset;
6151
}

lib/src/method_call/flutter_shortcuts_method_call_handler.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ for more details.
1111
import 'package:flutter/services.dart';
1212
import 'package:flutter_shortcuts/src/platform/flutter_shortcuts_platform.dart';
1313
import 'package:flutter_shortcuts/src/helper/helper.dart';
14+
import 'package:flutter/foundation.dart';
1415

1516
class FlutterShortcutsMethodCallHandler extends FlutterShortcutsPlatform {
1617
final MethodChannel _channel =
@@ -19,7 +20,15 @@ class FlutterShortcutsMethodCallHandler extends FlutterShortcutsPlatform {
1920
MethodChannel get channel => _channel;
2021

2122
@override
22-
Future<void> initialize(FlutterShortcutAction actionHandler) async {
23+
Future<void> initialize(bool debug) async {
24+
Map<String, String> initValue = {
25+
'debug': kReleaseMode ? false.toString() : debug.toString(),
26+
};
27+
await channel.invokeMethod('initialize', [initValue]);
28+
}
29+
30+
@override
31+
Future<void> listenAction(FlutterShortcutAction actionHandler) async {
2332
channel.setMethodCallHandler((MethodCall call) async {
2433
assert(call.method == 'launch');
2534
actionHandler(call.arguments);

lib/src/platform/flutter_shortcuts_platform.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ abstract class FlutterShortcutsPlatform extends PlatformInterface {
2828
_instance = instance;
2929
}
3030

31-
Future<void> initialize(FlutterShortcutAction action) async {
31+
Future<void> initialize(bool debug) async {
3232
throw UnimplementedError("initialize() has not been implemented.");
3333
}
3434

35+
Future<void> listenAction(FlutterShortcutAction action) async {
36+
throw UnimplementedError("listenAction() has not been implemented.");
37+
}
38+
3539
Future<int?> getMaxShortcutLimit() {
3640
throw UnimplementedError("getMaxShortcutLimit() has not been implemented.");
3741
}

0 commit comments

Comments
 (0)