What is rate limiting for android app shortcuts?

6.4k views Asked by At

As per documentation for app shortcuts

Rate Limiting When using the setDynamicShortcuts(), addDynamicShortcuts(), or updateShortcuts() methods, keep in mind that you might only be able to call these methods a specific number of times in a background app, an app with no activities or services currently in the foreground. In a production environment, you can reset this rate limiting by bringing your app to the foreground.

What is rate limiting in concern with app shortcuts? when isRateLimitingActive() should be used?

2

There are 2 answers

0
Rolf ツ On BEST ANSWER
  1. Looking at the source code it seems that the isRateLimitingActive() method returns false if you do not have any remaining calls left to the ShortcutManager API (hence the "0"). I guess rate limiting is needed because the API is resource intensive. I can imagine that at least the following will happen if you update a shortcut:

    • The launcher app (and other listeners) needs to be notified and starts updating it's UI or whatever is needed (depends on the launcher);
    • The system needs to store the new dynamic shortcut information;
  2. You could use this method to find out if a call to setDynamicShortcuts(), addDynamicShortcuts() or updateShortcuts() will succeed before even trying to do so.

Source:

/**
 * Return {@code true} when rate-limiting is active for the caller application.
 *
 * <p>See the class level javadoc for details.
 *
 * @throws IllegalStateException when the user is locked.
 */
public boolean isRateLimitingActive() {
    try {
        return mService.getRemainingCallCount(mContext.getPackageName(), injectMyUserId())
                == 0;
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}

Bonus: setDynamicShortcuts(), addDynamicShortcuts() or updateShortcuts() return false if they did not succeed due to Rate Limiting.

2
Sunil On

The recommended maximum number of shortcuts is 4, although it is possible to publish up to 5. You can read more here.