Skip to content

Commit

Permalink
fix(android): validate max shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
garymathews committed Jun 29, 2020
1 parent b50ca72 commit 6bfa28a
Showing 1 changed file with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ public ShortcutItemProxy()
}
}

@SuppressLint("NewApi")
private void setShortcuts()
{
if (shortcuts.size() > shortcutManager.getMaxShortcutCountPerActivity()) {
Log.w(TAG, "Could not set shortcuts, max shortcuts exceeded.");
return;
}

try {
shortcutManager.setDynamicShortcuts(shortcuts);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}

public void handleCreationDict(KrollDict dict)
{
// only supported on Android 7.1 and above
Expand Down Expand Up @@ -102,7 +117,7 @@ public void handleCreationDict(KrollDict dict)
this.shortcuts.remove(shortcut);
}
}
createShortcut();
setShortcuts();
for (ShortcutInfo shortcut : this.shortcutManager.getDynamicShortcuts()) {
if (shortcut.getId().equals(this.shortcut.getId())) {
if (shortcut.isEnabled()) {
Expand All @@ -116,26 +131,24 @@ public void handleCreationDict(KrollDict dict)
super.handleCreationDict(dict);
}

@SuppressLint("NewApi")
@Kroll.method
public void show()
{
if (shortcut != null) {
if (!shortcuts.contains(shortcut)) {
shortcuts.add(shortcut);
createShortcut();
setShortcuts();
}
}
}

@SuppressLint("NewApi")
@Kroll.method
public void hide()
{
if (shortcut != null) {
if (shortcuts.contains(shortcut)) {
shortcuts.remove(shortcut);
createShortcut();
setShortcuts();
}
}
}
Expand Down Expand Up @@ -180,28 +193,17 @@ public boolean getVisible()
return false;
}

@SuppressLint("NewApi")
@Override
public void release()
{
if (shortcut != null) {
shortcuts.remove(shortcut);
createShortcut();
setShortcuts();
shortcut = null;
}
super.release();
}

@SuppressLint("NewApi")
private void createShortcut()
{
try {
shortcutManager.setDynamicShortcuts(shortcuts);
} catch (Exception ex) {
Log.e(TAG, "Shortcut count exceeded");
}
}

@Override
public String getApiName()
{
Expand Down

0 comments on commit 6bfa28a

Please sign in to comment.