OSDN Git Service

Add status to DeepShortcutManager api calls.
authorTony Wickham <twickham@google.com>
Fri, 1 Jul 2016 22:44:13 +0000 (15:44 -0700)
committerTony Wickham <twickham@google.com>
Wed, 13 Jul 2016 19:02:33 +0000 (12:02 -0700)
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
src/com/android/launcher3/shortcuts/DeepShortcutManager.java

index 39e28c0..a75edb7 100644 (file)
@@ -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<ShortcutInfoCompat> 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;
                                         }
index 8bceda7..d1f0c5c 100644 (file)
@@ -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<ShortcutInfoCompat> 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<ShortcutInfo> 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;