Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): implement Shortcut proxy #11759

Merged
merged 19 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public void handleCreationDict(KrollDict dict)
id = dict.getString(TiC.PROPERTY_ID);
shortcutBuilder = new ShortcutInfo.Builder(context, id);
} else {
Log.e(TAG, "id is required to create a shortcut!");
return;
throw new Error("id is required to create a shortcut!");
}

// create shortcut intent
Expand All @@ -87,7 +86,7 @@ public void handleCreationDict(KrollDict dict)
int resId = ((Number) icon).intValue();
shortcutBuilder.setIcon(Icon.createWithResource(context, resId));
} else if (icon instanceof String) {
String uri = resolveUrl(null, dict.getString(TiC.PROPERTY_ICON));
String uri = resolveUrl(null, (String) icon);
shortcutBuilder.setIcon(Icon.createWithResource(context, TiUIHelper.getResourceId(uri)));
} else {
Log.w(TAG, "icon invalid, expecting resourceId (Number) or path (String)!");
Expand All @@ -102,40 +101,33 @@ public void handleCreationDict(KrollDict dict)
this.shortcuts.remove(shortcut);
}
}
this.shortcutManager.setDynamicShortcuts(shortcuts);
for (ShortcutInfo shortcut : this.shortcutManager.getDynamicShortcuts()) {
if (shortcut.getId().equals(this.shortcut.getId())) {
if (shortcut.isEnabled()) {
this.show();
}
} else {
this.shortcuts.add(shortcut);
}
}
this.shortcuts.add(shortcut);

super.handleCreationDict(dict);
}

@SuppressLint("NewApi")
@Deprecated
@Kroll.method
public void show()
{
if (shortcut != null) {
if (!shortcuts.contains(shortcut)) {
shortcuts.add(shortcut);
shortcutManager.setDynamicShortcuts(shortcuts);
shortcutManager.addDynamicShortcuts(shortcuts);
}
}
}

@SuppressLint("NewApi")
@Deprecated
@Kroll.method
public void hide()
{
if (shortcut != null) {
if (shortcuts.contains(shortcut)) {
shortcuts.remove(shortcut);
shortcutManager.setDynamicShortcuts(shortcuts);
shortcutManager.addDynamicShortcuts(shortcuts);
}
}
}
Expand All @@ -159,17 +151,32 @@ public void pin()
}
}

@SuppressLint("NewApi")
@Kroll.method
@Kroll.getProperty
public String getId()
{
if (shortcut != null) {
return shortcut.getId();
}
return null;
return properties.getString(TiC.PROPERTY_ID);
}

public String getTitle()
{
return properties.getString(TiC.PROPERTY_TITLE);
}

public String getDescription()
{
return properties.getString(TiC.PROPERTY_DESCRIPTION);
}

public Object getIcon()
{
return properties.get(TiC.PROPERTY_ICON);
}

public KrollDict getData()
{
return properties.getKrollDict(TiC.PROPERTY_DATA);
}

@Deprecated
@Kroll.method
@Kroll.getProperty
public boolean getVisible()
Expand All @@ -186,7 +193,6 @@ public void release()
{
if (shortcut != null) {
shortcuts.remove(shortcut);
shortcutManager.setDynamicShortcuts(shortcuts);
shortcut = null;
}
super.release();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2020 by Axway, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package ti.modules.titanium.ui;

import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;

/**
* This module exist so `Ti.UI.Shortcut.addEventListener()` is defined.
* `Ti.UI.createShortcut` is manually defined in `UIModule`.
*/
@Kroll.module(parentModule = UIModule.class)
public class ShortcutModule extends KrollModule
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels a bit odd to have this module stub so we can hang event listeners. Looks like we also always fire the event at the same time to Ti.App shortcutitemclick event - so you could probably hack this pretty easily in a js extension (basically add a dumb Ti.UI.Shortcut add/removeEventListener that registers for Ti.App event and forwards it)

{
private static final String TAG = "ShortcutModule";

public ShortcutModule()
{
super("Shortcut");
}

@Override
public String getApiName()
{
return "Ti.UI.Shortcut";
}
}
210 changes: 210 additions & 0 deletions android/modules/ui/src/java/ti/modules/titanium/ui/ShortcutProxy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2020 by Axway, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package ti.modules.titanium.ui;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.os.PersistableBundle;

import androidx.annotation.RequiresApi;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.util.TiUIHelper;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

@Kroll.proxy(
// Rename to not override `ShortcutModule` definition.
name = "_Shortcut"
)
public class ShortcutProxy extends KrollProxy
{
private static final String TAG = "ShortcutProxy";

// Only supported on Android 7.1 and above.
private static final boolean shortcutCapability = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1;

private static ShortcutManager shortcutManager = null;

public ShortcutProxy()
{
super();

if (!shortcutCapability) {
return;
}

// Obtain `ShortcutManager` instance.
if (shortcutManager == null) {
final Context context = TiApplication.getInstance();
shortcutManager = (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
}
}

/**
* Create a `ShortcutItemProxy` array from a `ShortcutInfo` list.
* @param shortcuts List of `ShortcutInfo` objects.
* @return Array of `ShortcutItemProxy` objects.
*/
@RequiresApi(api = Build.VERSION_CODES.N_MR1)
private ShortcutItemProxy[] toShortcutItems(List<ShortcutInfo> shortcuts)
{
final List<ShortcutItemProxy> items = new ArrayList<>();

for (ShortcutInfo shortcut : shortcuts) {
final ShortcutItemProxy shortcutItemProxy = new ShortcutItemProxy();
final KrollDict creationDict = new KrollDict();

// Translate into ShortcutItemProxy.
creationDict.put(TiC.PROPERTY_ID, shortcut.getId());
creationDict.put(TiC.PROPERTY_TITLE, shortcut.getShortLabel());
creationDict.put(TiC.PROPERTY_DESCRIPTION, shortcut.getLongLabel());

// Obtain additional stored data.
if (shortcut.getExtras() != null) {
final PersistableBundle bundle = shortcut.getExtras();

if (bundle.containsKey(TiC.PROPERTY_ICON)) {
creationDict.put(TiC.PROPERTY_ICON, bundle.get(TiC.PROPERTY_ICON));
}
if (bundle.containsKey(TiC.PROPERTY_DATA)) {
try {
final KrollDict data = new KrollDict(new JSONObject(bundle.getString(TiC.PROPERTY_DATA)));
creationDict.put(TiC.PROPERTY_DATA, data);
} catch (JSONException e) {
Log.w(TAG, "Could not parse bundle data property.");
}
}
}

// Initialize ShortcutItemProxy.
shortcutItemProxy.handleCreationDict(creationDict);
items.add(shortcutItemProxy);
}

return items.toArray(new ShortcutItemProxy[items.size()]);
}

/**
* Obtain array of current active shortcuts.
* @return Array of `ShortcutItemProxy` objects.
*/
@Kroll.getProperty
garymathews marked this conversation as resolved.
Show resolved Hide resolved
public ShortcutItemProxy[] items()
{
if (!shortcutCapability || shortcutManager == null) {
return null;
}

final List<ShortcutInfo> shortcuts = shortcutManager.getDynamicShortcuts();
return toShortcutItems(shortcuts);
}

/**
* Obtain array of current active static shortcuts.
* @return Array of `ShortcutItemProxy` objects.
*/
@Kroll.getProperty
public ShortcutItemProxy[] staticItems()
garymathews marked this conversation as resolved.
Show resolved Hide resolved
{
if (!shortcutCapability || shortcutManager == null) {
return null;
}

return toShortcutItems(shortcutManager.getPinnedShortcuts());
}

/**
* Add `ShortcutItemProxy` instance.
*/
@Kroll.method
public void add(ShortcutItemProxy shortcut)
{
if (!shortcutCapability) {
return;
}

// NOTE: Create the `ShortcutInfo` instance here, as the creation of `ShortcutInfo`
// in `ShortcutItemProxy.handleCreationDict()` will soon be deprecated a removed.
final Context context = TiApplication.getInstance();
final ShortcutInfo.Builder shortcutBuilder = new ShortcutInfo.Builder(context, shortcut.getId());

// Create shortcut intent.
final Intent intent = new Intent(TiApplication.getAppRootOrCurrentActivity().getIntent());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra("shortcut", shortcut.getId());
intent.putExtra("properties", shortcut.getProperties().toString());
shortcutBuilder.setIntent(intent);

shortcutBuilder.setShortLabel(shortcut.getTitle());
shortcutBuilder.setLongLabel(shortcut.getDescription());

// Handle shortcut icon.
final Object icon = shortcut.getIcon();
if (icon instanceof Number) {
int resId = ((Number) icon).intValue();
shortcutBuilder.setIcon(Icon.createWithResource(context, resId));
} else if (icon instanceof String) {
String uri = resolveUrl(null, (String) icon);
shortcutBuilder.setIcon(Icon.createWithResource(context, TiUIHelper.getResourceId(uri)));
}

// Add shortcut.
final List<ShortcutInfo> shortcuts = new ArrayList<>();
shortcuts.add(shortcutBuilder.build());
shortcutManager.addDynamicShortcuts(shortcuts);
}

/**
* Remove `ShortcutItemProxy` instance.
*/
@Kroll.method
public void remove(ShortcutItemProxy shortcut)
{
if (!shortcutCapability) {
return;
}

// Remove shortcut.
final List<String> ids = new ArrayList<>();
ids.add(shortcut.getId());
shortcutManager.removeDynamicShortcuts(ids);
}

/**
* Remove all active shortcuts.
*/
@Kroll.method
public void removeAll()
{
if (!shortcutCapability) {
return;
}

// Remove all shortcuts.
shortcutManager.removeAllDynamicShortcuts();
}

@Override
public String getApiName()
{
return "Ti.UI.Shortcut";
}
}
10 changes: 10 additions & 0 deletions android/modules/ui/src/java/ti/modules/titanium/ui/UIModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -554,4 +554,14 @@ public void onReceive(Context context, Intent intent)
this.module.fireEvent(TiC.EVENT_USER_INTERFACE_STYLE, event);
}
}

/**
* Manually define `createShortcut` to allow `ShortcutModule` to exist for 'click' events.
* @return Shortcut proxy instance.
*/
@Kroll.method
public KrollProxy createShortcut()
{
return new ShortcutProxy();
}
}