Skip to content

Commit

Permalink
feat(android): implement Shortcut proxy (#11759)
Browse files Browse the repository at this point in the history
* feat(android): implement Shortcut proxy

* docs: update documentation

* test: include Shortcut tests

* test(android): limit to android

* docs: minor fixes

* test: improve Ti.UI.Shortcut tests

* fix(android): implement Ti.UI.Shortcut click event

* test: simplify ShortcutItem test

* docs: include click event

* docs: update example

* fix(android): validate max shortcuts

* fix(android): include missing getById() method

Co-authored-by: Sohail <ssaddique@axway.com>
  • Loading branch information
garymathews and ssaddique committed Jul 7, 2020
1 parent 901f991 commit 5432efc
Show file tree
Hide file tree
Showing 9 changed files with 654 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiBlob;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.util.TiUIHelper;
import org.appcelerator.titanium.io.TiFileFactory;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.Bitmap;
import android.graphics.drawable.Icon;
import android.os.Build;

Expand Down Expand Up @@ -64,8 +66,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 @@ -83,14 +84,24 @@ public void handleCreationDict(KrollDict dict)
}
if (dict.containsKey(TiC.PROPERTY_ICON)) {
Object icon = dict.get(TiC.PROPERTY_ICON);
if (icon instanceof Number) {
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));
shortcutBuilder.setIcon(Icon.createWithResource(context, TiUIHelper.getResourceId(uri)));
} else {
Log.w(TAG, "icon invalid, expecting resourceId (Number) or path (String)!");
try {
if (icon instanceof Number) {
int resId = ((Number) icon).intValue();
shortcutBuilder.setIcon(Icon.createWithResource(context, resId));

} else if (icon instanceof String) {
final String uri = resolveUrl(null, (String) icon);
final TiBlob blob = TiBlob.blobFromFile(TiFileFactory.createTitaniumFile(uri, false));
final Bitmap bitmap = blob.getImage();

if (bitmap != null) {
shortcutBuilder.setIcon(Icon.createWithBitmap(bitmap));
}
} else {
Log.w(TAG, "icon invalid, expecting resourceId (Number) or path (String)!");
}
} catch (Exception e) {
Log.w(TAG, e.getMessage());
}
}

Expand All @@ -102,40 +113,45 @@ 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);
try {
shortcutManager.addDynamicShortcuts(shortcuts);
} catch (Exception e) {
Log.w(TAG, e.getMessage());
}
}
}
}

@SuppressLint("NewApi")
@Deprecated
@Kroll.method
public void hide()
{
if (shortcut != null) {
if (shortcuts.contains(shortcut)) {
final List<String> shortcutIds = new ArrayList<>();

shortcutIds.add(shortcut.getId());
shortcuts.remove(shortcut);
shortcutManager.setDynamicShortcuts(shortcuts);

try {
shortcutManager.removeDynamicShortcuts(shortcutIds);
} catch (Exception e) {
Log.w(TAG, e.getMessage());
}
}
}
}
Expand All @@ -159,17 +175,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 +217,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
{
private static final String TAG = "ShortcutModule";

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

@Override
public String getApiName()
{
return "Ti.UI.Shortcut";
}
}
Loading

0 comments on commit 5432efc

Please sign in to comment.