From 51c7433d1db989e8c583a5f1359254765a5e0bb1 Mon Sep 17 00:00:00 2001 From: Peretz30 Date: Wed, 13 May 2020 11:02:50 +0300 Subject: [PATCH] added static shortcuts android --- .../AppShortcutsModule.java | 57 +++++++++++-------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/android/src/main/java/com/reactNativeQuickActions/AppShortcutsModule.java b/android/src/main/java/com/reactNativeQuickActions/AppShortcutsModule.java index 3ab3e81..ef53aa8 100644 --- a/android/src/main/java/com/reactNativeQuickActions/AppShortcutsModule.java +++ b/android/src/main/java/com/reactNativeQuickActions/AppShortcutsModule.java @@ -21,6 +21,7 @@ import com.facebook.react.bridge.WritableMap; import com.facebook.react.module.annotations.ReactModule; import com.facebook.react.modules.core.DeviceEventManagerModule; +import com.facebook.react.bridge.WritableNativeMap; import java.util.ArrayList; import java.util.List; @@ -32,6 +33,7 @@ class AppShortcutsModule extends ReactContextBaseJavaModule { private static final String ACTION_SHORTCUT = "ACTION_SHORTCUT"; private static final String SHORTCUT_ITEM = "SHORTCUT_ITEM"; + private static final String ACTION_SHORTCUT_STATIC = "ACTION_SHORTCUT_STATIC"; AppShortcutsModule(ReactApplicationContext reactContext) { super(reactContext); @@ -59,21 +61,27 @@ public String getName() { public void popInitialAction(Promise promise) { try { Activity currentActivity = getCurrentActivity(); - WritableMap map = null; + if (currentActivity != null) { Intent intent = currentActivity.getIntent(); - + if (ACTION_SHORTCUT.equals(intent.getAction())) { + WritableMap map = null; PersistableBundle bundle = intent.getParcelableExtra(SHORTCUT_ITEM); if (bundle != null) { ShortcutItem item = ShortcutItem.fromPersistableBundle(bundle); map = item.toWritableMap(); } + promise.resolve(map); + } else if (ACTION_SHORTCUT_STATIC.equals(intent.getAction())) { + WritableMap map = new WritableNativeMap(); + map.putString("type", intent.getStringExtra("SHORTCUT_ITEM_TYPE")); + map.putString("title", intent.getStringExtra("SHORTCUT_ITEM_TITLE")); + promise.resolve(map); } + } - - promise.resolve(map); } catch (Exception e) { promise.reject(new JSApplicationIllegalArgumentException( "AppShortcuts.popInitialAction error. " + e.getMessage())); @@ -98,18 +106,13 @@ public void setShortcutItems(ReadableArray items) { for (int i = 0; i < items.size(); i++) { ShortcutItem item = ShortcutItem.fromReadableMap(items.getMap(i)); - int iconResId = context.getResources() - .getIdentifier(item.icon, "drawable", context.getPackageName()); + int iconResId = context.getResources().getIdentifier(item.icon, "drawable", context.getPackageName()); Intent intent = new Intent(context, currentActivity.getClass()); intent.setAction(ACTION_SHORTCUT); intent.putExtra(SHORTCUT_ITEM, item.toPersistableBundle()); - shortcuts.add(new ShortcutInfo.Builder(context, "id" + i) - .setShortLabel(item.title) - .setLongLabel(item.title) - .setIcon(Icon.createWithResource(context, iconResId)) - .setIntent(intent) - .build()); + shortcuts.add(new ShortcutInfo.Builder(context, "id" + i).setShortLabel(item.title).setLongLabel(item.title) + .setIcon(Icon.createWithResource(context, iconResId)).setIntent(intent).build()); } getReactApplicationContext().getSystemService(ShortcutManager.class).setDynamicShortcuts(shortcuts); @@ -137,19 +140,27 @@ private boolean isShortcutSupported() { } private void sendJSEvent(Intent intent) { - if (!ACTION_SHORTCUT.equals(intent.getAction()) || !isShortcutSupported()) { + if ((!ACTION_SHORTCUT.equals(intent.getAction()) && !ACTION_SHORTCUT_STATIC.equals(intent.getAction())) + || !isShortcutSupported()) { return; } - - ShortcutItem item = null; - PersistableBundle bundle = intent.getParcelableExtra(SHORTCUT_ITEM); - if (bundle != null) { - item = ShortcutItem.fromPersistableBundle(bundle); - } - if (item != null) { - getReactApplicationContext() - .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) - .emit("quickActionShortcut", item.toWritableMap()); + if (ACTION_SHORTCUT.equals(intent.getAction())) { + ShortcutItem item = null; + PersistableBundle bundle = intent.getParcelableExtra(SHORTCUT_ITEM); + if (bundle != null) { + item = ShortcutItem.fromPersistableBundle(bundle); + } + if (item != null) { + getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) + .emit("quickActionShortcut", item.toWritableMap()); + } + } else { + WritableMap map = new WritableNativeMap(); + map.putString("type", intent.getStringExtra("SHORTCUT_ITEM_TYPE")); + map.putString("title", intent.getStringExtra("SHORTCUT_ITEM_TITLE")); + getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) + .emit("quickActionShortcut", map); } + } }