OSDN Git Service

Making some shortcut manager calls protected by MR1 version check
authorSunny Goyal <sunnygoyal@google.com>
Thu, 23 Jun 2016 21:17:24 +0000 (14:17 -0700)
committerSunny Goyal <sunnygoyal@google.com>
Thu, 23 Jun 2016 21:55:56 +0000 (14:55 -0700)
Change-Id: Ifdfa95a58aa18a825c1838c61055928dbe0ea3be

src/com/android/launcher3/Launcher.java
src/com/android/launcher3/LauncherModel.java
src/com/android/launcher3/ShortcutInfo.java
src/com/android/launcher3/Utilities.java
src/com/android/launcher3/compat/LauncherAppsCompat.java
src/com/android/launcher3/compat/LauncherAppsCompatV16.java
src/com/android/launcher3/compat/LauncherAppsCompatVNMR1.java [deleted file]
src/com/android/launcher3/shortcuts/DeepShortcutManager.java

index d25eca0..97af37c 100644 (file)
@@ -2801,8 +2801,8 @@ public class Launcher extends Activity
                 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
                     String id = ((ShortcutInfo) info).getDeepShortcutId();
                     String packageName = intent.getPackage();
-                    LauncherAppsCompat.getInstance(this).startShortcut(
-                                packageName, id, intent.getSourceBounds(), optsBundle, info.user);
+                    LauncherAppState.getInstance().getShortcutManager().startShortcut(
+                            packageName, id, intent.getSourceBounds(), optsBundle, info.user);
                 } else {
                     // Could be launching some bookkeeping activity
                     startActivity(intent, optsBundle);
index 8e404a7..1e597d3 100644 (file)
@@ -1941,8 +1941,8 @@ public class LauncherModel extends BroadcastReceiver
                                         }
                                     }
                                     incrementPinnedShortcutCount(key, shouldPin);
-                                    info = ShortcutInfo.fromDeepShortcutInfo(pinnedShortcut,
-                                            context, launcherApps);
+                                    info = ShortcutInfo.fromDeepShortcutInfo(
+                                            pinnedShortcut, context);
                                 } else { // item type == ITEM_TYPE_SHORTCUT
                                     info = getShortcutInfo(c, context, titleIndex, cursorIconInfo);
 
@@ -3317,7 +3317,7 @@ public class LauncherModel extends BroadcastReceiver
                 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
                         .get(fullDetails.getId());
                 for (ShortcutInfo shortcutInfo : shortcutInfos) {
-                    shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context, mLauncherApps);
+                    shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
                     updatedShortcutInfos.add(shortcutInfo);
                 }
             }
index 63f49e0..3756ba1 100644 (file)
@@ -282,18 +282,17 @@ public class ShortcutInfo extends ItemInfo {
      */
     @TargetApi(Build.VERSION_CODES.N)
     public static ShortcutInfo fromDeepShortcutInfo(ShortcutInfoCompat shortcutInfo,
-            Context context, LauncherAppsCompat launcherApps) {
+            Context context) {
         ShortcutInfo si = new ShortcutInfo();
         si.user = shortcutInfo.getUserHandle();
         si.itemType = LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT;
         si.intent = shortcutInfo.makeIntent(context);
         si.flags = 0;
-        si.updateFromDeepShortcutInfo(shortcutInfo, context, launcherApps);
+        si.updateFromDeepShortcutInfo(shortcutInfo, context);
         return si;
     }
 
-    public void updateFromDeepShortcutInfo(ShortcutInfoCompat shortcutInfo,
-            Context context, LauncherAppsCompat launcherApps) {
+    public void updateFromDeepShortcutInfo(ShortcutInfoCompat shortcutInfo, Context context) {
         title = shortcutInfo.getShortLabel();
 
         CharSequence label = shortcutInfo.getLongLabel();
@@ -304,8 +303,9 @@ public class ShortcutInfo extends ItemInfo {
                 .getBadgedLabelForUser(label, user);
 
         LauncherAppState launcherAppState = LauncherAppState.getInstance();
-        Drawable unbadgedIcon = launcherApps.getShortcutIconDrawable(shortcutInfo, launcherAppState
-                .getInvariantDeviceProfile().fillResIconDpi);
+        Drawable unbadgedIcon = launcherAppState.getShortcutManager()
+                .getShortcutIconDrawable(shortcutInfo,
+                        launcherAppState.getInvariantDeviceProfile().fillResIconDpi);
         Bitmap icon = unbadgedIcon == null ? null
                 : Utilities.createBadgedIconBitmap(unbadgedIcon, user, context);
         setIcon(icon != null ? icon : launcherAppState.getIconCache().getDefaultIcon(user));
index 0306d57..b24e189 100644 (file)
@@ -44,8 +44,10 @@ import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.PaintDrawable;
 import android.os.Build;
+import android.os.Build.VERSION;
 import android.os.Bundle;
 import android.os.PowerManager;
+import android.support.v4.os.BuildCompat;
 import android.text.Spannable;
 import android.text.SpannableString;
 import android.text.TextUtils;
@@ -100,6 +102,12 @@ public final class Utilities {
     private static final int[] sLoc0 = new int[2];
     private static final int[] sLoc1 = new int[2];
 
+    public static boolean isNycMR1OrAbove() {
+        // TODO: Use the check from support lib
+        return !"REL".equals(VERSION.CODENAME)
+                && "NMR1".compareTo(VERSION.CODENAME) <= 0;
+    }
+
     // TODO: use Build.VERSION_CODES when available
     public static final boolean ATLEAST_MARSHMALLOW = Build.VERSION.SDK_INT >= 23;
 
index 3381064..3c1013d 100644 (file)
@@ -61,9 +61,7 @@ public abstract class LauncherAppsCompat {
     public static LauncherAppsCompat getInstance(Context context) {
         synchronized (sInstanceLock) {
             if (sInstance == null) {
-                if (Utilities.isNycOrAbove()) {
-                    sInstance = new LauncherAppsCompatVNMR1(context.getApplicationContext());
-                } else if (Utilities.ATLEAST_LOLLIPOP) {
+                if (Utilities.ATLEAST_LOLLIPOP) {
                     sInstance = new LauncherAppsCompatVL(context.getApplicationContext());
                 } else {
                     sInstance = new LauncherAppsCompatV16(context.getApplicationContext());
@@ -86,11 +84,4 @@ public abstract class LauncherAppsCompat {
     public abstract boolean isActivityEnabledForProfile(ComponentName component,
             UserHandleCompat user);
     public abstract boolean isPackageSuspendedForProfile(String packageName, UserHandleCompat user);
-    public abstract List<ShortcutInfoCompat> getShortcuts(LauncherApps.ShortcutQuery q,
-            UserHandleCompat userHandle);
-    public abstract void pinShortcuts(String packageName, List<String> pinnedIds,
-            UserHandleCompat userHandle);
-    public abstract void startShortcut(String packageName, String id, Rect sourceBounds,
-            Bundle startActivityOptions, UserHandleCompat user);
-    public abstract Drawable getShortcutIconDrawable(ShortcutInfoCompat shortcutInfo, int density);
 }
index 1a144e8..49a0df6 100644 (file)
@@ -133,29 +133,6 @@ public class LauncherAppsCompatV16 extends LauncherAppsCompat {
         return false;
     }
 
-    @Override
-    public List<ShortcutInfoCompat> getShortcuts(LauncherApps.ShortcutQuery q,
-            UserHandleCompat userHandle) {
-        return null;
-    }
-
-    @Override
-    public void pinShortcuts(String packageName, List<String> pinnedIds,
-            UserHandleCompat userHandle) {
-        // Not supported, so do nothing.
-    }
-
-    @Override
-    public void startShortcut(String packageName, String id, Rect sourceBounds,
-            Bundle startActivityOptions, UserHandleCompat user) {
-        // Not supported, so do nothing.
-    }
-
-    @Override
-    public Drawable getShortcutIconDrawable(ShortcutInfoCompat shortcutInfo, int density) {
-        return null;
-    }
-
     private void unregisterForPackageIntents() {
         mContext.unregisterReceiver(mPackageMonitor);
     }
diff --git a/src/com/android/launcher3/compat/LauncherAppsCompatVNMR1.java b/src/com/android/launcher3/compat/LauncherAppsCompatVNMR1.java
deleted file mode 100644 (file)
index 0c1db13..0000000
+++ /dev/null
@@ -1,123 +0,0 @@
-/*
- * Copyright (C) 2016 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.launcher3.compat;
-
-import android.annotation.TargetApi;
-import android.content.Context;
-import android.content.pm.LauncherApps;
-import android.content.pm.ShortcutInfo;
-import android.graphics.Rect;
-import android.graphics.drawable.Drawable;
-import android.os.Build;
-import android.os.Bundle;
-import android.os.UserHandle;
-
-import com.android.launcher3.shortcuts.ShortcutInfoCompat;
-
-import java.util.ArrayList;
-import java.util.List;
-
-@TargetApi(Build.VERSION_CODES.N)
-public class LauncherAppsCompatVNMR1 extends LauncherAppsCompatVL {
-
-    LauncherAppsCompatVNMR1(Context context) {
-        super(context);
-    }
-
-    @Override
-    public List<ShortcutInfoCompat> getShortcuts(LauncherApps.ShortcutQuery q,
-            UserHandleCompat userHandle) {
-        List<ShortcutInfo> shortcutInfos = mLauncherApps.getShortcuts(q, userHandle.getUser());
-        if (shortcutInfos == null) {
-            return null;
-        }
-        List<ShortcutInfoCompat> shortcutInfoCompats = new ArrayList<>(shortcutInfos.size());
-        for (ShortcutInfo shortcutInfo : shortcutInfos) {
-            shortcutInfoCompats.add(new ShortcutInfoCompat(shortcutInfo));
-        }
-        return shortcutInfoCompats;
-    }
-
-    @Override
-    public void pinShortcuts(String packageName, List<String> pinnedIds,
-            UserHandleCompat userHandle) {
-        mLauncherApps.pinShortcuts(packageName, pinnedIds, userHandle.getUser());
-    }
-
-    @Override
-    public void startShortcut(String packageName, String id, Rect sourceBounds,
-            Bundle startActivityOptions, UserHandleCompat user) {
-        mLauncherApps.startShortcut(packageName, id, sourceBounds,
-                startActivityOptions, user.getUser());
-    }
-
-    @Override
-    public Drawable getShortcutIconDrawable(ShortcutInfoCompat shortcutInfo, int density) {
-        return mLauncherApps.getShortcutIconDrawable(shortcutInfo.getShortcutInfo(), density);
-    }
-
-    private static class WrappedCallback extends LauncherApps.Callback {
-        private OnAppsChangedCallbackCompat mCallback;
-
-        public WrappedCallback(OnAppsChangedCallbackCompat callback) {
-            mCallback = callback;
-        }
-
-        public void onPackageRemoved(String packageName, UserHandle user) {
-            mCallback.onPackageRemoved(packageName, UserHandleCompat.fromUser(user));
-        }
-
-        public void onPackageAdded(String packageName, UserHandle user) {
-            mCallback.onPackageAdded(packageName, UserHandleCompat.fromUser(user));
-        }
-
-        public void onPackageChanged(String packageName, UserHandle user) {
-            mCallback.onPackageChanged(packageName, UserHandleCompat.fromUser(user));
-        }
-
-        public void onPackagesAvailable(String[] packageNames, UserHandle user, boolean replacing) {
-            mCallback.onPackagesAvailable(packageNames, UserHandleCompat.fromUser(user), replacing);
-        }
-
-        public void onPackagesUnavailable(String[] packageNames, UserHandle user,
-                boolean replacing) {
-            mCallback.onPackagesUnavailable(packageNames, UserHandleCompat.fromUser(user),
-                    replacing);
-        }
-
-        public void onPackagesSuspended(String[] packageNames, UserHandle user) {
-            mCallback.onPackagesSuspended(packageNames, UserHandleCompat.fromUser(user));
-        }
-
-        public void onPackagesUnsuspended(String[] packageNames, UserHandle user) {
-            mCallback.onPackagesUnsuspended(packageNames, UserHandleCompat.fromUser(user));
-        }
-
-        @Override
-        public void onShortcutsChanged(String packageName, List<ShortcutInfo> shortcuts,
-                UserHandle user) {
-            List<ShortcutInfoCompat> shortcutInfoCompats = new ArrayList<>(shortcuts.size());
-            for (ShortcutInfo shortcutInfo : shortcuts) {
-                shortcutInfoCompats.add(new ShortcutInfoCompat(shortcutInfo));
-            }
-
-            mCallback.onShortcutsChanged(packageName, shortcutInfoCompats,
-                    UserHandleCompat.fromUser(user));
-        }
-    }
-}
-
index e2e06af..46ca931 100644 (file)
@@ -19,30 +19,37 @@ package com.android.launcher3.shortcuts;
 import android.annotation.TargetApi;
 import android.content.ComponentName;
 import android.content.Context;
+import android.content.pm.LauncherApps;
 import android.content.pm.LauncherApps.ShortcutQuery;
-import android.os.Build;
-import android.os.Process;
-import android.util.Log;
+import android.content.pm.ShortcutInfo;
+import android.graphics.Rect;
+import android.graphics.drawable.Drawable;
+import android.os.Bundle;
 
-import com.android.launcher3.compat.LauncherAppsCompat;
+import com.android.launcher3.Utilities;
 import com.android.launcher3.compat.UserHandleCompat;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
-import java.util.Map;
 
 /**
  * Performs operations related to deep shortcuts, such as querying for them, pinning them, etc.
  */
-@TargetApi(Build.VERSION_CODES.N)
 public class DeepShortcutManager {
-    private static final int FLAG_GET_ALL = ShortcutQuery.FLAG_GET_DYNAMIC
-            | ShortcutQuery.FLAG_GET_PINNED | ShortcutQuery.FLAG_GET_MANIFEST;
 
-    private final LauncherAppsCompat mLauncherApps;
+    // TODO: Replace this with platform constants when the new sdk is available.
+    public static final int FLAG_MATCH_DYNAMIC = 1 << 0;
+    public static final int FLAG_MATCH_MANIFEST = 1 << 3;
+    public static final int FLAG_MATCH_PINNED = 1 << 1;
+
+    private static final int FLAG_GET_ALL =
+            FLAG_MATCH_DYNAMIC | FLAG_MATCH_PINNED | FLAG_MATCH_MANIFEST;
+
+    private final LauncherApps mLauncherApps;
 
     public DeepShortcutManager(Context context, ShortcutCache shortcutCache) {
-        mLauncherApps = LauncherAppsCompat.getInstance(context);
+        mLauncherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
     }
 
     public void onShortcutsChanged(List<ShortcutInfoCompat> shortcuts) {
@@ -72,26 +79,48 @@ public class DeepShortcutManager {
      * Removes the given shortcut from the current list of pinned shortcuts.
      * (Runs on background thread)
      */
+    @TargetApi(25)
     public void unpinShortcut(final ShortcutKey key) {
-        String packageName = key.componentName.getPackageName();
-        String id = key.id;
-        UserHandleCompat user = key.user;
-        List<String> pinnedIds = extractIds(queryForPinnedShortcuts(packageName, user));
-        pinnedIds.remove(id);
-        mLauncherApps.pinShortcuts(packageName, pinnedIds, user);
+        if (Utilities.isNycMR1OrAbove()) {
+            String packageName = key.componentName.getPackageName();
+            String id = key.id;
+            UserHandleCompat user = key.user;
+            List<String> pinnedIds = extractIds(queryForPinnedShortcuts(packageName, user));
+            pinnedIds.remove(id);
+            mLauncherApps.pinShortcuts(packageName, pinnedIds, user.getUser());
+        }
     }
 
     /**
      * Adds the given shortcut to the current list of pinned shortcuts.
      * (Runs on background thread)
      */
+    @TargetApi(25)
     public void pinShortcut(final ShortcutKey key) {
-        String packageName = key.componentName.getPackageName();
-        String id = key.id;
-        UserHandleCompat user = key.user;
-        List<String> pinnedIds = extractIds(queryForPinnedShortcuts(packageName, user));
-        pinnedIds.add(id);
-        mLauncherApps.pinShortcuts(packageName, pinnedIds, user);
+        if (Utilities.isNycMR1OrAbove()) {
+            String packageName = key.componentName.getPackageName();
+            String id = key.id;
+            UserHandleCompat user = key.user;
+            List<String> pinnedIds = extractIds(queryForPinnedShortcuts(packageName, user));
+            pinnedIds.add(id);
+            mLauncherApps.pinShortcuts(packageName, pinnedIds, user.getUser());
+        }
+    }
+
+    @TargetApi(25)
+    public void startShortcut(String packageName, String id, Rect sourceBounds,
+          Bundle startActivityOptions, UserHandleCompat user) {
+        if (Utilities.isNycMR1OrAbove()) {
+            mLauncherApps.startShortcut(packageName, id, sourceBounds,
+                    startActivityOptions, user.getUser());
+        }
+    }
+
+    @TargetApi(25)
+    public Drawable getShortcutIconDrawable(ShortcutInfoCompat shortcutInfo, int density) {
+        return Utilities.isNycMR1OrAbove()
+                ? mLauncherApps.getShortcutIconDrawable(shortcutInfo.getShortcutInfo(), density)
+                : null;
     }
 
     /**
@@ -101,7 +130,7 @@ public class DeepShortcutManager {
      */
     public List<ShortcutInfoCompat> queryForPinnedShortcuts(String packageName,
             UserHandleCompat user) {
-        return query(ShortcutQuery.FLAG_GET_PINNED, packageName, null, null, user);
+        return query(FLAG_MATCH_PINNED, packageName, null, null, user);
     }
 
     public List<ShortcutInfoCompat> queryForAllShortcuts(UserHandleCompat user) {
@@ -122,15 +151,28 @@ public class DeepShortcutManager {
      *
      * TODO: Use the cache to optimize this so we don't make an RPC every time.
      */
+    @TargetApi(25)
     private List<ShortcutInfoCompat> query(int flags, String packageName,
             ComponentName activity, List<String> shortcutIds, UserHandleCompat user) {
-        ShortcutQuery q = new ShortcutQuery();
-        q.setQueryFlags(flags);
-        if (packageName != null) {
-            q.setPackage(packageName);
-            q.setActivity(activity);
-            q.setShortcutIds(shortcutIds);
+        if (Utilities.isNycMR1OrAbove()) {
+            ShortcutQuery q = new ShortcutQuery();
+            q.setQueryFlags(flags);
+            if (packageName != null) {
+                q.setPackage(packageName);
+                q.setActivity(activity);
+                q.setShortcutIds(shortcutIds);
+            }
+            List<ShortcutInfo> shortcutInfos = mLauncherApps.getShortcuts(q, user.getUser());
+            if (shortcutInfos == null) {
+                return Collections.EMPTY_LIST;
+            }
+            List<ShortcutInfoCompat> shortcutInfoCompats = new ArrayList<>(shortcutInfos.size());
+            for (ShortcutInfo shortcutInfo : shortcutInfos) {
+                shortcutInfoCompats.add(new ShortcutInfoCompat(shortcutInfo));
+            }
+            return shortcutInfoCompats;
+        } else {
+            return Collections.EMPTY_LIST;
         }
-        return mLauncherApps.getShortcuts(q, user);
     }
 }