Skip to content

Commit

Permalink
#5 #6
Browse files Browse the repository at this point in the history
  • Loading branch information
divshekhar committed May 25, 2021
1 parent b75068a commit 6d9de31
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.util.Log;
Expand All @@ -23,6 +22,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

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

private boolean debug;

void debugPrint(String message) {
if(debug) {
Log.d(TAG,message);
}
}

MethodCallImplementation(Context context, Activity activity) {
this.context = context;
this.activity = activity;
Expand All @@ -55,6 +63,12 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
(ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);

switch (call.method) {
case "initialize":
initialize(call);
break;
case "getLaunchAction":
getLaunchAction(shortcutManager,result);
break;
case "getMaxShortcutLimit":
final int maxLimit = getMaxShortcutLimit();
result.success(maxLimit);
Expand Down Expand Up @@ -87,28 +101,37 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
case "clearShortcutItems":
shortcutManager.removeAllDynamicShortcuts();
break;
case "getLaunchAction":
if (activity == null) {
result.error(
"flutter_shortcuts_no_activity",
"There is no activity available when launching action",
null);
return;
}
final Intent intent = activity.getIntent();
final String launchAction = intent.getStringExtra(EXTRA_ACTION);
if (launchAction != null && !launchAction.isEmpty()) {
shortcutManager.reportShortcutUsed(launchAction);
intent.removeExtra(EXTRA_ACTION);
}
result.success(launchAction);
break;
default:
result.notImplemented();
break;
}
}

private void initialize(MethodCall call) {
List<Map<String, String>> args = call.arguments();
this.debug = Boolean.parseBoolean(args.get(0).get("debug"));
debugPrint("Flutter Shortcuts Initialized");
}

@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private void getLaunchAction(ShortcutManager shortcutManager, MethodChannel.Result result) {
if (activity == null) {
result.error(
"flutter_shortcuts_no_activity",
"There is no activity available when launching action",
null);
return;
}
final Intent intent = activity.getIntent();
final String launchAction = intent.getStringExtra(EXTRA_ACTION);
if (launchAction != null && !launchAction.isEmpty()) {
shortcutManager.reportShortcutUsed(launchAction);
intent.removeExtra(EXTRA_ACTION);
}
result.success(launchAction);
debugPrint("Launch Action: " + launchAction);
}

@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private int getMaxShortcutLimit() {
ShortcutManager shortcutManager =
Expand All @@ -128,11 +151,12 @@ private Map<String, Integer> getIconProperties() {

@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private void setShortcutItems(MethodCall call,ShortcutManager shortcutManager) {
List<Map<String, String>> setShortcutItemsArgs = call.arguments();
List<Map<String, String>> args = call.arguments();
List<ShortcutInfo> shortcuts;
try {
shortcuts = processShortcuts(setShortcutItemsArgs);
shortcuts = processShortcuts(args);
shortcutManager.setDynamicShortcuts(shortcuts);
debugPrint("Shortcuts created");
} catch (Exception e) {
Log.e(TAG,e.toString());
}
Expand All @@ -145,6 +169,7 @@ private void pushShortcutItem(MethodCall call, ShortcutManager shortcutManager)
try {
shortcuts = processShortcuts(args);
shortcutManager.addDynamicShortcuts(shortcuts);
debugPrint("Shortcut pushed");
} catch (Exception e) {
Log.e(TAG,e.toString());
}
Expand All @@ -157,25 +182,26 @@ private void pushShortcutItems(MethodCall call, ShortcutManager shortcutManager)
try {
shortcuts = processShortcuts(args);
shortcutManager.addDynamicShortcuts(shortcuts);
debugPrint("Shortcuts pushed");
} catch (Exception e) {
Log.e(TAG,e.toString());
}
}

@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private void updateShortcutItems(MethodCall call, ShortcutManager shortcutManager) {
List<Map<String, String>> updateAllShortcutArgs = call.arguments();
List<Map<String, String>> args = call.arguments();
boolean updated = false;
try {
List<ShortcutInfo> updateShortcuts = processShortcuts(updateAllShortcutArgs);
List<ShortcutInfo> updateShortcuts = processShortcuts(args);
updated = shortcutManager.updateShortcuts(updateShortcuts);
} catch(Exception e) {
Log.e(TAG, e.toString());
}
if(updated) {
Log.d(TAG,"All Shortcuts updated");
debugPrint("Shortcuts updated");
} else {
Log.d(TAG,"Unable to update all shortcuts");
debugPrint("Unable to update shortcuts");
}
}

Expand All @@ -202,6 +228,7 @@ private void updateShortcutItem(MethodCall call, ShortcutManager shortcutManager
}
try {
shortcutManager.updateShortcuts(shortcutList);
debugPrint("Shortcut updated");
} catch(Exception e) {
Log.e(TAG,e.toString());
}
Expand All @@ -211,7 +238,6 @@ private void updateShortLabel(MethodCall call, ShortcutManager shortcutManager)
final List<String> args = call.arguments();
final String refId = args.get(0);
final String title = args.get(1);

}

@RequiresApi(api = Build.VERSION_CODES.N_MR1)
Expand Down Expand Up @@ -239,6 +265,7 @@ private void changeShortcutItemIcon(MethodCall call, ShortcutManager shortcutMan
}
try {
shortcutManager.updateShortcuts(shortcutList);
debugPrint("Shortcut Icon Changed.");
} catch(Exception e) {
Log.e(TAG,e.toString());
}
Expand Down Expand Up @@ -281,7 +308,7 @@ private ShortcutInfo createShortcutInfo(Map<String, String> shortcut) {
final String action = shortcut.get("action");
final String shortLabel = shortcut.get("shortLabel");
final String longLabel = shortcut.get("LongLabel");
final int iconType = Integer.parseInt(shortcut.get("shortcutIconType"));
final int iconType = Integer.parseInt(Objects.requireNonNull(shortcut.get("shortcutIconType")));
final ShortcutInfo.Builder shortcutBuilder;
shortcutBuilder = new ShortcutInfo.Builder(context, id);

Expand Down Expand Up @@ -340,6 +367,7 @@ private Icon getIconFromFlutterAsset(Context context, String path) {
}
Bitmap image = null;
try {
assert fd != null;
image = BitmapFactory.decodeStream(fd.createInputStream());
} catch (IOException e) {
e.printStackTrace();
Expand Down
13 changes: 9 additions & 4 deletions lib/flutter_shortcuts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ import 'package:flutter_shortcuts/src/helper/helper.dart';
export 'package:flutter_shortcuts/src/helper/helper.dart';

class FlutterShortcuts {
/// [initialize] performs action when shortcut is initiated.
Future<void> initialize(FlutterShortcutAction action) async {
FlutterShortcutsPlatform.instance.initialize(action);
/// [initialize] initializes the flutter_shortcuts plugin.
Future<void> initialize({bool debug = true}) async {
FlutterShortcutsPlatform.instance.initialize(debug);
}

/// [listenAction] performs action when shortcut is initiated.
Future<void> listenAction(FlutterShortcutAction action) async {
FlutterShortcutsPlatform.instance.listenAction(action);
}

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

/// [addShortcutItems] updates dynamic or pinned shortcuts with same IDs
/// [pushShortcutItems] updates dynamic or pinned shortcuts with same IDs
/// and pushes new shortcuts with different IDs.
Future<void> pushShortcutItems(
{required List<FlutterShortcutItem> shortcutList}) async {
Expand Down
14 changes: 2 additions & 12 deletions lib/src/helper/model/flutter_shortcut_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,12 @@ import '../enums/shortcut_icon_asset.dart';
class FlutterShortcutItem {
/// Create a flutter shortcut item.
/// Eg.
/// ```
/// ```dart
/// const FlutterShortcutItem(
/// ```
/// ```
/// id: "1",
/// ```
/// ```
/// action: 'Home page action',
/// ```
/// ```
/// shortLabel: 'Home Page',
/// ```
/// ```
/// icon: 'assets/icons/home.png',
/// ```
/// ```
/// );
/// ```
Expand Down Expand Up @@ -56,6 +46,6 @@ class FlutterShortcutItem {
/// Flutter asset path. Only Supports image files. Eg. .png/.jpg
final String? icon;

/// `ShortcutIconType.native` or `ShortcutIconType.flutterAsset`
/// `ShortcutIconType.androidAsset` or `ShortcutIconType.flutterAsset`
final ShortcutIconAsset shortcutIconAsset;
}
11 changes: 10 additions & 1 deletion lib/src/method_call/flutter_shortcuts_method_call_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ for more details.
import 'package:flutter/services.dart';
import 'package:flutter_shortcuts/src/platform/flutter_shortcuts_platform.dart';
import 'package:flutter_shortcuts/src/helper/helper.dart';
import 'package:flutter/foundation.dart';

class FlutterShortcutsMethodCallHandler extends FlutterShortcutsPlatform {
final MethodChannel _channel =
Expand All @@ -19,7 +20,15 @@ class FlutterShortcutsMethodCallHandler extends FlutterShortcutsPlatform {
MethodChannel get channel => _channel;

@override
Future<void> initialize(FlutterShortcutAction actionHandler) async {
Future<void> initialize(bool debug) async {
Map<String, String> initValue = {
'debug': kReleaseMode ? false.toString() : debug.toString(),
};
await channel.invokeMethod('initialize', [initValue]);
}

@override
Future<void> listenAction(FlutterShortcutAction actionHandler) async {
channel.setMethodCallHandler((MethodCall call) async {
assert(call.method == 'launch');
actionHandler(call.arguments);
Expand Down
6 changes: 5 additions & 1 deletion lib/src/platform/flutter_shortcuts_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ abstract class FlutterShortcutsPlatform extends PlatformInterface {
_instance = instance;
}

Future<void> initialize(FlutterShortcutAction action) async {
Future<void> initialize(bool debug) async {
throw UnimplementedError("initialize() has not been implemented.");
}

Future<void> listenAction(FlutterShortcutAction action) async {
throw UnimplementedError("listenAction() has not been implemented.");
}

Future<int?> getMaxShortcutLimit() {
throw UnimplementedError("getMaxShortcutLimit() has not been implemented.");
}
Expand Down

0 comments on commit 6d9de31

Please sign in to comment.