Skip to content

Commit

Permalink
[TIMOB-24765] Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Gary Mathews committed Oct 2, 2018
1 parent cefaaa0 commit 6bff032
Show file tree
Hide file tree
Showing 2 changed files with 1,867 additions and 1,856 deletions.
323 changes: 159 additions & 164 deletions android/modules/ui/src/java/ti/modules/titanium/ui/ShortcutItemProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,168 +26,163 @@
import java.util.Arrays;
import java.util.List;

@Kroll.proxy(creatableInModule=UIModule.class)
public class ShortcutItemProxy extends KrollProxy
{
private static final String TAG = "ShortcutItemProxy";

private Context context = null;
private static ShortcutManager shortcutManager = null;
private static List<ShortcutInfo> shortcuts = new ArrayList<ShortcutInfo>();

private ShortcutInfo shortcut;
private ShortcutInfo.Builder shortcutBuilder;

public ShortcutItemProxy()
{
super();

context = TiApplication.getAppRootOrCurrentActivity();
if (shortcutManager == null) {
shortcutManager = (ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE);
}
}

public void handleCreationDict(KrollDict dict)
{
// only supported on Android 7.1 and above
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
return;
}

String id = null;
if (dict.containsKey(TiC.PROPERTY_ID)) {
id = dict.getString(TiC.PROPERTY_ID);
shortcutBuilder = new ShortcutInfo.Builder(context, id);
} else {
Log.e(TAG, "id is required to create a shortcut!");
return;
}

// create shortcut intent
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", id);
shortcutBuilder.setIntent(intent);

if (dict.containsKey(TiC.PROPERTY_TITLE)) {
shortcutBuilder.setShortLabel(dict.getString(TiC.PROPERTY_TITLE));
}
if (dict.containsKey(TiC.PROPERTY_SUBTITLE)) {
shortcutBuilder.setLongLabel(dict.getString(TiC.PROPERTY_SUBTITLE));
}
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)!");
}
}

shortcut = shortcutBuilder.build();

// obtain and update any pre-existing shortcuts
for (ShortcutInfo shortcut : this.shortcuts) {
if (shortcut.getId().equals(this.shortcut.getId())) {
this.shortcuts.remove(shortcut);
break;
}
}
for (ShortcutInfo shortcut : this.shortcutManager.getDynamicShortcuts()) {
if (shortcut.getId().equals(this.shortcut.getId())) {
this.shortcutManager.removeDynamicShortcuts(Arrays.asList(shortcut.getId()));
this.shortcuts.remove(shortcut);
if (shortcut.isEnabled()) {
this.show();
}
} else {
this.shortcuts.add(shortcut);
}
}

super.handleCreationDict(dict);
}

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

@Kroll.method
public void hide()
{
if (shortcut != null) {
if (shortcuts.contains(shortcut)) {
shortcuts.remove(shortcut);
shortcutManager.setDynamicShortcuts(shortcuts);
}
}
}

@Kroll.method
public void pin()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
shortcut != null) {
if (shortcutManager.isRequestPinShortcutSupported()) {
boolean shortcutExists = false;
for (ShortcutInfo shortcut : shortcutManager.getPinnedShortcuts()) {
if (shortcut.getId().equals(this.shortcut.getId())) {
shortcutExists = true;
break;
}
}
if (!shortcutExists) {
shortcutManager.requestPinShortcut(this.shortcut, null);
}
}
}
}

@Kroll.getProperty
public String getId()
{
if (shortcut != null) {
return shortcut.getId();
}
return null;
}

@Kroll.getProperty
public boolean getVisible()
{
if (shortcut != null) {
return shortcuts.contains(shortcut);
}
return false;
}

@Override
public void release()
{
if (shortcut != null) {
shortcuts.remove(shortcut);
shortcutManager.setDynamicShortcuts(shortcuts);
shortcut = null;
}
super.release();
}

@Override
public String getApiName()
{
return "Ti.UI.ShortcutItem";
}
@Kroll.proxy(creatableInModule = UIModule.class)
public class ShortcutItemProxy extends KrollProxy {
private static final String TAG = "ShortcutItemProxy";

private Context context = null;
private static ShortcutManager shortcutManager = null;
private static List<ShortcutInfo> shortcuts = new ArrayList<ShortcutInfo>();

private ShortcutInfo shortcut;
private ShortcutInfo.Builder shortcutBuilder;

public ShortcutItemProxy() {
super();

context = TiApplication.getAppRootOrCurrentActivity();
if (shortcutManager == null) {
shortcutManager =
(ShortcutManager)context.getSystemService(Context.SHORTCUT_SERVICE);
}
}

public void handleCreationDict(KrollDict dict) {
// only supported on Android 7.1 and above
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
return;
}

String id = null;
if (dict.containsKey(TiC.PROPERTY_ID)) {
id = dict.getString(TiC.PROPERTY_ID);
shortcutBuilder = new ShortcutInfo.Builder(context, id);
} else {
Log.e(TAG, "id is required to create a shortcut!");
return;
}

// create shortcut intent
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", id);
shortcutBuilder.setIntent(intent);

if (dict.containsKey(TiC.PROPERTY_TITLE)) {
shortcutBuilder.setShortLabel(dict.getString(TiC.PROPERTY_TITLE));
}
if (dict.containsKey(TiC.PROPERTY_SUBTITLE)) {
shortcutBuilder.setLongLabel(dict.getString(TiC.PROPERTY_SUBTITLE));
}
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)!");
}
}

shortcut = shortcutBuilder.build();

// obtain and update any pre-existing shortcuts
for (ShortcutInfo shortcut : this.shortcuts) {
if (shortcut.getId().equals(this.shortcut.getId())) {
this.shortcuts.remove(shortcut);
break;
}
}
for (ShortcutInfo shortcut : this.shortcutManager.getDynamicShortcuts()) {
if (shortcut.getId().equals(this.shortcut.getId())) {
this.shortcutManager.removeDynamicShortcuts(
Arrays.asList(shortcut.getId()));
this.shortcuts.remove(shortcut);
if (shortcut.isEnabled()) {
this.show();
}
} else {
this.shortcuts.add(shortcut);
}
}

super.handleCreationDict(dict);
}

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

@Kroll.method
public void hide() {
if (shortcut != null) {
if (shortcuts.contains(shortcut)) {
shortcuts.remove(shortcut);
shortcutManager.setDynamicShortcuts(shortcuts);
}
}
}

@Kroll.method
public void pin() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && shortcut != null) {
if (shortcutManager.isRequestPinShortcutSupported()) {
boolean shortcutExists = false;
for (ShortcutInfo shortcut : shortcutManager.getPinnedShortcuts()) {
if (shortcut.getId().equals(this.shortcut.getId())) {
shortcutExists = true;
break;
}
}
if (!shortcutExists) {
shortcutManager.requestPinShortcut(this.shortcut, null);
}
}
}
}

@Kroll.getProperty
public String getId() {
if (shortcut != null) {
return shortcut.getId();
}
return null;
}

@Kroll.getProperty
public boolean getVisible() {
if (shortcut != null) {
return shortcuts.contains(shortcut);
}
return false;
}

@Override
public void release() {
if (shortcut != null) {
shortcuts.remove(shortcut);
shortcutManager.setDynamicShortcuts(shortcuts);
shortcut = null;
}
super.release();
}

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

0 comments on commit 6bff032

Please sign in to comment.