From d82a39da1c61b19d2749725f9c70c3baf2dab6bf Mon Sep 17 00:00:00 2001 From: Tony Wickham Date: Fri, 1 Jul 2016 15:44:13 -0700 Subject: [PATCH] Add status to DeepShortcutManager api calls. This way we can handle SecurityExceptions differently. For instance, if a SecurityException causes us to fail to get details for pinned shortcuts, we keep the pinned shortcuts in the database so that they will be loaded next time launcher has the correct permissions. Change-Id: I1102fce612ade10ad7f577b44a99c8cf087d5ccd --- src/com/android/launcher3/LauncherModel.java | 13 +++++-- .../launcher3/shortcuts/DeepShortcutManager.java | 42 +++++++++++++--------- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/com/android/launcher3/LauncherModel.java b/src/com/android/launcher3/LauncherModel.java index 39e28c0b0..a75edb715 100644 --- a/src/com/android/launcher3/LauncherModel.java +++ b/src/com/android/launcher3/LauncherModel.java @@ -71,10 +71,10 @@ import com.android.launcher3.util.FlagOp; import com.android.launcher3.util.GridOccupancy; import com.android.launcher3.util.LongArrayMap; import com.android.launcher3.util.ManagedProfileHeuristic; +import com.android.launcher3.util.MultiHashMap; import com.android.launcher3.util.PackageManagerHelper; import com.android.launcher3.util.Preconditions; import com.android.launcher3.util.StringFilter; -import com.android.launcher3.util.MultiHashMap; import com.android.launcher3.util.Thunk; import com.android.launcher3.util.ViewOnDrawExecutor; @@ -1942,7 +1942,16 @@ public class LauncherModel extends BroadcastReceiver List fullDetails = mDeepShortcutManager .queryForFullDetails(packageName, Collections.singletonList(shortcutId), user); - if (fullDetails != null && !fullDetails.isEmpty()) { + if (fullDetails == null || fullDetails.isEmpty()) { + // There are no details for the shortcut. If this is due + // to a SecurityException, keep it in the database so + // we can restore the icon when the launcher regains + // permission. Otherwise remove the icon from the db. + if (!mDeepShortcutManager.wasLastCallSuccess()) { + itemsToRemove.add(id); + continue; + } + } else { pinnedShortcut = fullDetails.get(0); shouldPin = true; } diff --git a/src/com/android/launcher3/shortcuts/DeepShortcutManager.java b/src/com/android/launcher3/shortcuts/DeepShortcutManager.java index 8bceda7f1..d1f0c5c27 100644 --- a/src/com/android/launcher3/shortcuts/DeepShortcutManager.java +++ b/src/com/android/launcher3/shortcuts/DeepShortcutManager.java @@ -25,7 +25,6 @@ import android.content.pm.ShortcutInfo; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.os.Bundle; -import android.os.UserHandle; import android.util.Log; import com.android.launcher3.ItemInfo; @@ -33,7 +32,6 @@ import com.android.launcher3.LauncherSettings; import com.android.launcher3.Utilities; import com.android.launcher3.compat.UserHandleCompat; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -53,6 +51,7 @@ public class DeepShortcutManager { FLAG_MATCH_DYNAMIC | FLAG_MATCH_PINNED | FLAG_MATCH_MANIFEST; private final LauncherApps mLauncherApps; + private boolean mWasLastCallSuccess; public DeepShortcutManager(Context context, ShortcutCache shortcutCache) { mLauncherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE); @@ -63,6 +62,10 @@ public class DeepShortcutManager { || info.itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; } + public boolean wasLastCallSuccess() { + return mWasLastCallSuccess; + } + public void onShortcutsChanged(List shortcuts) { // mShortcutCache.removeShortcuts(shortcuts); } @@ -100,8 +103,10 @@ public class DeepShortcutManager { pinnedIds.remove(id); try { mLauncherApps.pinShortcuts(packageName, pinnedIds, user.getUser()); + mWasLastCallSuccess = true; } catch (SecurityException e) { - Log.e(TAG, Log.getStackTraceString(e)); + Log.w(TAG, "Failed to unpin shortcut", e); + mWasLastCallSuccess = false; } } } @@ -120,8 +125,10 @@ public class DeepShortcutManager { pinnedIds.add(id); try { mLauncherApps.pinShortcuts(packageName, pinnedIds, user.getUser()); + mWasLastCallSuccess = true; } catch (SecurityException e) { - Log.e(TAG, Log.getStackTraceString(e)); + Log.w(TAG, "Failed to pin shortcut", e); + mWasLastCallSuccess = false; } } } @@ -131,16 +138,12 @@ public class DeepShortcutManager { Bundle startActivityOptions, UserHandleCompat user) { if (Utilities.isNycMR1OrAbove()) { try { - // TODO: remove reflection once updated SDK is ready. - // mLauncherApps.startShortcut(packageName, id, sourceBounds, - // startActivityOptions, user.getUser()); - mLauncherApps.getClass().getMethod("startShortcut", String.class, String.class, - Rect.class, Bundle.class, UserHandle.class).invoke(mLauncherApps, - packageName, id, sourceBounds, startActivityOptions, user.getUser()); + mLauncherApps.startShortcut(packageName, id, sourceBounds, + startActivityOptions, user.getUser()); + mWasLastCallSuccess = true; } catch (SecurityException e) { - Log.e(TAG, Log.getStackTraceString(e)); - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { - e.printStackTrace(); + Log.e(TAG, "Failed to start shortcut", e); + mWasLastCallSuccess = false; } } } @@ -149,10 +152,13 @@ public class DeepShortcutManager { public Drawable getShortcutIconDrawable(ShortcutInfoCompat shortcutInfo, int density) { if (Utilities.isNycMR1OrAbove()) { try { - return mLauncherApps.getShortcutIconDrawable(shortcutInfo.getShortcutInfo(), - density); + Drawable icon = mLauncherApps.getShortcutIconDrawable( + shortcutInfo.getShortcutInfo(), density); + mWasLastCallSuccess = true; + return icon; } catch (SecurityException e) { - Log.e(TAG, Log.getStackTraceString(e)); + Log.e(TAG, "Failed to get shortcut icon", e); + mWasLastCallSuccess = false; } } return null; @@ -200,8 +206,10 @@ public class DeepShortcutManager { List shortcutInfos = null; try { shortcutInfos = mLauncherApps.getShortcuts(q, user.getUser()); + mWasLastCallSuccess = true; } catch (SecurityException e) { - Log.e(TAG, Log.getStackTraceString(e)); + Log.e(TAG, "Failed to query for shortcuts", e); + mWasLastCallSuccess = false; } if (shortcutInfos == null) { return Collections.EMPTY_LIST; -- 2.11.0