Skip to content

Commit

Permalink
fix(android): handle duplicate ShortcutItems (#10943)
Browse files Browse the repository at this point in the history
* fix(android): handle duplicate ShortcutItems

* fix(android): missing import

* test(android): add Ti.UI.ShortcutItem tests
  • Loading branch information
garymathews authored and lokeshchdhry committed Jun 26, 2019
1 parent 5b78f90 commit d9a8b90
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.util.TiUIHelper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
Expand Down Expand Up @@ -97,16 +98,14 @@ public void handleCreationDict(KrollDict dict)
shortcut = shortcutBuilder.build();

// obtain and update any pre-existing shortcuts
for (ShortcutInfo shortcut : this.shortcuts) {
for (ShortcutInfo shortcut : new ArrayList<>(this.shortcuts)) {
if (shortcut.getId().equals(this.shortcut.getId())) {
this.shortcuts.remove(shortcut);
break;
}
}
this.shortcutManager.setDynamicShortcuts(shortcuts);
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();
}
Expand All @@ -118,6 +117,7 @@ public void handleCreationDict(KrollDict dict)
super.handleCreationDict(dict);
}

@SuppressLint("NewApi")
@Kroll.method
public void show()
{
Expand All @@ -129,6 +129,7 @@ public void show()
}
}

@SuppressLint("NewApi")
@Kroll.method
public void hide()
{
Expand Down Expand Up @@ -160,6 +161,7 @@ public void pin()
}

// clang-format off
@SuppressLint("NewApi")
@Kroll.method
@Kroll.getProperty
public String getId()
Expand All @@ -183,6 +185,7 @@ public boolean getVisible()
return false;
}

@SuppressLint("NewApi")
@Override
public void release()
{
Expand Down
73 changes: 73 additions & 0 deletions tests/Resources/ti.ui.shortcutitem.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Appcelerator Titanium Mobile
* Copyright (c) 2019 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.
*/
/* eslint-env mocha */
/* global Ti */
/* eslint no-unused-expressions: "off" */
'use strict';

const should = require('./utilities/assertions');

describe('Titanium.UI.ShortcutItem', () => {

it.android('Ti.UI.ShortcutItem', () => {
should(Ti.UI.ShortcutItem).not.be.undefined;
});

it.android('createShortcutItem', () => {
should(Ti.UI.createShortcutItem).not.be.undefined;
should(Ti.UI.createShortcutItem).be.a.Function;

// create shortcut
const shortcut = Ti.UI.createShortcutItem({
id: 'test_shortcut',
title: 'Test Shortcut',
description: 'Test shortcut description',
icon: Ti.Android.R.drawable.ic_menu_send
});
should(shortcut).be.a.Object;

// verify `apiName`
should(shortcut).have.readOnlyProperty('apiName').which.is.a.String;
should(shortcut.apiName).be.eql('Ti.UI.ShortcutItem');

// verify `id`
should(shortcut.id).be.eql('test_shortcut');

// verify `title`
should(shortcut.title).be.eql('Test Shortcut');

// verify `description`
should(shortcut.description).be.eql('Test shortcut description');

// verify `icon`
should(shortcut.icon).be.eql(Ti.Android.R.drawable.ic_menu_send);

// verify `show()`
should(shortcut.show).not.be.undefined;
should(shortcut.show).be.a.Function;

// verify `hide()`
should(shortcut.hide).not.be.undefined;
should(shortcut.hide).be.a.Function;

// verify `pin`
should(shortcut.pin).not.be.undefined;
should(shortcut.pin).be.a.Function;
});

it.android('handle duplicate shortcuts', () => {
for (let i = 0; i < 16; i++) {
const shortcut = Ti.UI.createShortcutItem({
id: 'test_shortcut',
title: 'Test Shortcut',
description: 'Test shortcut description',
icon: Ti.Android.R.drawable.ic_menu_send
});
shortcut.show();
}
});
});

0 comments on commit d9a8b90

Please sign in to comment.