From 6dec396db88df467666433b9cb0f0665e4102183 Mon Sep 17 00:00:00 2001 From: Alberto Blanco Date: Mon, 18 Dec 2017 15:46:33 +0100 Subject: [PATCH] Use PersistableBundle to attach the whole ShortCutItem object --- .../AppShortcutsModule.java | 38 ++++++------------- .../reactNativeQuickActions/ShortcutItem.java | 20 ++++++++++ .../com/reactNativeQuickActions/UserInfo.java | 14 +++++++ 3 files changed, 46 insertions(+), 26 deletions(-) diff --git a/android/src/main/java/com/reactNativeQuickActions/AppShortcutsModule.java b/android/src/main/java/com/reactNativeQuickActions/AppShortcutsModule.java index d7caba1..3ab3e81 100644 --- a/android/src/main/java/com/reactNativeQuickActions/AppShortcutsModule.java +++ b/android/src/main/java/com/reactNativeQuickActions/AppShortcutsModule.java @@ -8,9 +8,9 @@ import android.content.pm.ShortcutManager; import android.graphics.drawable.Icon; import android.os.Build; +import android.os.PersistableBundle; import com.facebook.react.bridge.ActivityEventListener; -import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.JSApplicationIllegalArgumentException; import com.facebook.react.bridge.Promise; @@ -31,9 +31,7 @@ class AppShortcutsModule extends ReactContextBaseJavaModule { static final String REACT_NAME = "ReactAppShortcuts"; private static final String ACTION_SHORTCUT = "ACTION_SHORTCUT"; - private static final String SHORTCUT_TYPE = "SHORTCUT_TYPE"; - - private List mShortcutItems; + private static final String SHORTCUT_ITEM = "SHORTCUT_ITEM"; AppShortcutsModule(ReactApplicationContext reactContext) { super(reactContext); @@ -67,10 +65,10 @@ public void popInitialAction(Promise promise) { Intent intent = currentActivity.getIntent(); if (ACTION_SHORTCUT.equals(intent.getAction())) { - String type = intent.getStringExtra(SHORTCUT_TYPE); - if (type != null) { - map = Arguments.createMap(); - map.putString("type", type); + PersistableBundle bundle = intent.getParcelableExtra(SHORTCUT_ITEM); + if (bundle != null) { + ShortcutItem item = ShortcutItem.fromPersistableBundle(bundle); + map = item.toWritableMap(); } } } @@ -95,18 +93,16 @@ public void setShortcutItems(ReadableArray items) { } Context context = getReactApplicationContext(); - mShortcutItems = new ArrayList<>(items.size()); List shortcuts = new ArrayList<>(items.size()); for (int i = 0; i < items.size(); i++) { ShortcutItem item = ShortcutItem.fromReadableMap(items.getMap(i)); - mShortcutItems.add(item); int iconResId = context.getResources() .getIdentifier(item.icon, "drawable", context.getPackageName()); Intent intent = new Intent(context, currentActivity.getClass()); intent.setAction(ACTION_SHORTCUT); - intent.putExtra(SHORTCUT_TYPE, item.type); + intent.putExtra(SHORTCUT_ITEM, item.toPersistableBundle()); shortcuts.add(new ShortcutInfo.Builder(context, "id" + i) .setShortLabel(item.title) @@ -127,7 +123,6 @@ public void clearShortcutItems() { } getReactApplicationContext().getSystemService(ShortcutManager.class).removeAllDynamicShortcuts(); - mShortcutItems = null; } @ReactMethod @@ -146,24 +141,15 @@ private void sendJSEvent(Intent intent) { return; } - String type = intent.getStringExtra(SHORTCUT_TYPE); - ShortcutItem item = getShortcutItem(type); + 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()); } } - - private ShortcutItem getShortcutItem(String type) { - if (mShortcutItems != null && type != null) { - for (ShortcutItem item : mShortcutItems) { - if (item.type.equals(type)) { - return item; - } - } - } - return null; - } - } diff --git a/android/src/main/java/com/reactNativeQuickActions/ShortcutItem.java b/android/src/main/java/com/reactNativeQuickActions/ShortcutItem.java index 667b06b..3919ac1 100644 --- a/android/src/main/java/com/reactNativeQuickActions/ShortcutItem.java +++ b/android/src/main/java/com/reactNativeQuickActions/ShortcutItem.java @@ -1,5 +1,7 @@ package com.reactNativeQuickActions; +import android.os.PersistableBundle; + import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.WritableMap; @@ -19,6 +21,24 @@ static ShortcutItem fromReadableMap(ReadableMap map) { return item; } + static ShortcutItem fromPersistableBundle(PersistableBundle bundle) { + final ShortcutItem item = new ShortcutItem(); + item.type = bundle.getString("type"); + item.title = bundle.getString("title"); + item.icon = bundle.getString("icon"); + item.userInfo = UserInfo.fromPersistableBundle(bundle.getPersistableBundle("userInfo")); + return item; + } + + PersistableBundle toPersistableBundle() { + PersistableBundle bundle = new PersistableBundle(); + bundle.putString("type", type); + bundle.putString("title", title); + bundle.putString("icon", icon); + bundle.putPersistableBundle("userInfo", userInfo.toPersistableBundle()); + return bundle; + } + WritableMap toWritableMap() { WritableMap map = Arguments.createMap(); map.putString("type", type); diff --git a/android/src/main/java/com/reactNativeQuickActions/UserInfo.java b/android/src/main/java/com/reactNativeQuickActions/UserInfo.java index bacab37..df8eb39 100644 --- a/android/src/main/java/com/reactNativeQuickActions/UserInfo.java +++ b/android/src/main/java/com/reactNativeQuickActions/UserInfo.java @@ -1,5 +1,7 @@ package com.reactNativeQuickActions; +import android.os.PersistableBundle; + import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.WritableMap; @@ -14,6 +16,18 @@ static UserInfo fromReadableMap(ReadableMap map) { return info; } + static UserInfo fromPersistableBundle(PersistableBundle bundle) { + final UserInfo info = new UserInfo(); + info.url = bundle.getString("url"); + return info; + } + + PersistableBundle toPersistableBundle() { + PersistableBundle bundle = new PersistableBundle(); + bundle.putString("url", url); + return bundle; + } + WritableMap toWritableMap() { WritableMap map = Arguments.createMap(); map.putString("url", url);