Skip to content
This repository has been archived by the owner on Apr 15, 2023. It is now read-only.

[Android] Use PersistableBundle to persist the whole ShortCutItem object #54

Merged
merged 1 commit into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ShortcutItem> mShortcutItems;
private static final String SHORTCUT_ITEM = "SHORTCUT_ITEM";

AppShortcutsModule(ReactApplicationContext reactContext) {
super(reactContext);
Expand Down Expand Up @@ -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();
}
}
}
Expand All @@ -95,18 +93,16 @@ public void setShortcutItems(ReadableArray items) {
}

Context context = getReactApplicationContext();
mShortcutItems = new ArrayList<>(items.size());
List<ShortcutInfo> 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)
Expand All @@ -127,7 +123,6 @@ public void clearShortcutItems() {
}

getReactApplicationContext().getSystemService(ShortcutManager.class).removeAllDynamicShortcuts();
mShortcutItems = null;
}

@ReactMethod
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions android/src/main/java/com/reactNativeQuickActions/UserInfo.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down