diff --git a/Android.mk b/Android.mk index dbce33f8b4..6e8365fc32 100644 --- a/Android.mk +++ b/Android.mk @@ -36,7 +36,8 @@ LOCAL_AAPT_FLAGS := --auto-add-overlay LOCAL_PROTOC_OPTIMIZE_TYPE := nano LOCAL_PROTOC_FLAGS := --proto_path=$(LOCAL_PATH)/protos/ -LOCAL_SDK_VERSION := 19 +# STOPSHIP(kennyguy): change to 21 once the L SDK is baked. +LOCAL_SDK_VERSION := current LOCAL_PACKAGE_NAME := Launcher3 #LOCAL_CERTIFICATE := shared diff --git a/src/com/android/launcher3/AppInfo.java b/src/com/android/launcher3/AppInfo.java index c85626b106..bfcad84b37 100644 --- a/src/com/android/launcher3/AppInfo.java +++ b/src/com/android/launcher3/AppInfo.java @@ -94,7 +94,7 @@ public class AppInfo extends ItemInfo { } private static int initFlags(LauncherActivityInfoCompat info) { - int appFlags = info.getApplicationFlags(); + int appFlags = info.getApplicationInfo().flags; int flags = 0; if ((appFlags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) == 0) { flags |= DOWNLOADED_FLAG; diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java index d76b2382e8..3f3ce4a73e 100644 --- a/src/com/android/launcher3/Launcher.java +++ b/src/com/android/launcher3/Launcher.java @@ -2753,8 +2753,8 @@ public class Launcher extends Activity // Could be launching some bookkeeping activity startActivity(intent, optsBundle); } else { - launcherApps.startActivityForProfile(intent.getComponent(), - intent.getSourceBounds(), optsBundle, user); + launcherApps.startActivityForProfile(intent.getComponent(), user, + intent.getSourceBounds(), optsBundle); } return true; } catch (SecurityException e) { diff --git a/src/com/android/launcher3/LauncherAppState.java b/src/com/android/launcher3/LauncherAppState.java index 79bc084248..27bcd81721 100644 --- a/src/com/android/launcher3/LauncherAppState.java +++ b/src/com/android/launcher3/LauncherAppState.java @@ -95,7 +95,7 @@ public class LauncherAppState implements DeviceProfile.DeviceProfileCallbacks { mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class)); mModel = new LauncherModel(this, mIconCache, mAppFilter); final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext); - launcherApps.addOnAppsChangedListener(mModel); + launcherApps.addOnAppsChangedCallback(mModel); // Register intent receivers IntentFilter filter = new IntentFilter(); @@ -128,7 +128,7 @@ public class LauncherAppState implements DeviceProfile.DeviceProfileCallbacks { public void onTerminate() { sContext.unregisterReceiver(mModel); final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext); - launcherApps.removeOnAppsChangedListener(mModel); + launcherApps.removeOnAppsChangedCallback(mModel); ContentResolver resolver = sContext.getContentResolver(); resolver.unregisterContentObserver(mFavoritesObserver); diff --git a/src/com/android/launcher3/LauncherModel.java b/src/com/android/launcher3/LauncherModel.java index 141368c208..a975da4598 100644 --- a/src/com/android/launcher3/LauncherModel.java +++ b/src/com/android/launcher3/LauncherModel.java @@ -73,7 +73,7 @@ import java.util.concurrent.atomic.AtomicBoolean; * for the Launcher. */ public class LauncherModel extends BroadcastReceiver - implements LauncherAppsCompat.OnAppsChangedListenerCompat { + implements LauncherAppsCompat.OnAppsChangedCallbackCompat { static final boolean DEBUG_LOADERS = false; private static final boolean DEBUG_RECEIVER = true; // STOPSHIP(cwren) temporary for debugging @@ -1186,28 +1186,28 @@ public class LauncherModel extends BroadcastReceiver } @Override - public void onPackageChanged(UserHandleCompat user, String packageName) { + public void onPackageChanged(String packageName, UserHandleCompat user) { int op = PackageUpdatedTask.OP_UPDATE; enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }, user)); } @Override - public void onPackageRemoved(UserHandleCompat user, String packageName) { + public void onPackageRemoved(String packageName, UserHandleCompat user) { int op = PackageUpdatedTask.OP_REMOVE; enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }, user)); } @Override - public void onPackageAdded(UserHandleCompat user, String packageName) { + public void onPackageAdded(String packageName, UserHandleCompat user) { int op = PackageUpdatedTask.OP_ADD; enqueuePackageUpdated(new PackageUpdatedTask(op, new String[] { packageName }, user)); } @Override - public void onPackagesAvailable(UserHandleCompat user, String[] packageNames, + public void onPackagesAvailable(String[] packageNames, UserHandleCompat user, boolean replacing) { if (!replacing) { enqueuePackageUpdated(new PackageUpdatedTask(PackageUpdatedTask.OP_ADD, packageNames, @@ -1226,7 +1226,7 @@ public class LauncherModel extends BroadcastReceiver } @Override - public void onPackagesUnavailable(UserHandleCompat user, String[] packageNames, + public void onPackagesUnavailable(String[] packageNames, UserHandleCompat user, boolean replacing) { if (!replacing) { enqueuePackageUpdated(new PackageUpdatedTask( diff --git a/src/com/android/launcher3/compat/LauncherActivityInfoCompat.java b/src/com/android/launcher3/compat/LauncherActivityInfoCompat.java index 3ba93ea912..90a4d1a1f7 100644 --- a/src/com/android/launcher3/compat/LauncherActivityInfoCompat.java +++ b/src/com/android/launcher3/compat/LauncherActivityInfoCompat.java @@ -17,6 +17,7 @@ package com.android.launcher3.compat; import android.content.ComponentName; +import android.content.pm.ApplicationInfo; import android.graphics.drawable.Drawable; public abstract class LauncherActivityInfoCompat { @@ -28,7 +29,7 @@ public abstract class LauncherActivityInfoCompat { public abstract UserHandleCompat getUser(); public abstract CharSequence getLabel(); public abstract Drawable getIcon(int density); - public abstract int getApplicationFlags(); + public abstract ApplicationInfo getApplicationInfo(); public abstract long getFirstInstallTime(); public abstract Drawable getBadgedIcon(int density); } diff --git a/src/com/android/launcher3/compat/LauncherActivityInfoCompatV16.java b/src/com/android/launcher3/compat/LauncherActivityInfoCompatV16.java index 052d4343ec..1d41a6ff6b 100644 --- a/src/com/android/launcher3/compat/LauncherActivityInfoCompatV16.java +++ b/src/com/android/launcher3/compat/LauncherActivityInfoCompatV16.java @@ -18,6 +18,7 @@ package com.android.launcher3.compat; import android.content.ComponentName; import android.content.Context; +import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; @@ -75,8 +76,8 @@ public class LauncherActivityInfoCompatV16 extends LauncherActivityInfoCompat { return d; } - public int getApplicationFlags() { - return mActivityInfo.applicationInfo.flags; + public ApplicationInfo getApplicationInfo() { + return mActivityInfo.applicationInfo; } public long getFirstInstallTime() { diff --git a/src/com/android/launcher3/compat/LauncherActivityInfoCompatVL.java b/src/com/android/launcher3/compat/LauncherActivityInfoCompatVL.java index 76125bd6a1..b52cf1de2b 100644 --- a/src/com/android/launcher3/compat/LauncherActivityInfoCompatVL.java +++ b/src/com/android/launcher3/compat/LauncherActivityInfoCompatVL.java @@ -17,66 +17,44 @@ package com.android.launcher3.compat; import android.content.ComponentName; +import android.content.pm.ApplicationInfo; +import android.content.pm.LauncherActivityInfo; import android.graphics.drawable.Drawable; import android.os.UserHandle; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - public class LauncherActivityInfoCompatVL extends LauncherActivityInfoCompat { - private Object mLauncherActivityInfo; - private Class mLauncherActivityInfoClass; - private Method mGetComponentName; - private Method mGetUser; - private Method mGetLabel; - private Method mGetIcon; - private Method mGetApplicationFlags; - private Method mGetFirstInstallTime; - private Method mGetBadgedIcon; + private LauncherActivityInfo mLauncherActivityInfo; - LauncherActivityInfoCompatVL(Object launcherActivityInfo) { + LauncherActivityInfoCompatVL(LauncherActivityInfo launcherActivityInfo) { super(); mLauncherActivityInfo = launcherActivityInfo; - mLauncherActivityInfoClass = ReflectUtils.getClassForName( - "android.content.pm.LauncherActivityInfo"); - mGetComponentName = ReflectUtils.getMethod(mLauncherActivityInfoClass, "getComponentName"); - mGetUser = ReflectUtils.getMethod(mLauncherActivityInfoClass, "getUser"); - mGetLabel = ReflectUtils.getMethod(mLauncherActivityInfoClass, "getLabel"); - mGetIcon = ReflectUtils.getMethod(mLauncherActivityInfoClass, "getIcon", int.class); - mGetApplicationFlags = ReflectUtils.getMethod(mLauncherActivityInfoClass, - "getApplicationFlags"); - mGetFirstInstallTime = ReflectUtils.getMethod(mLauncherActivityInfoClass, - "getFirstInstallTime"); - mGetBadgedIcon = ReflectUtils.getMethod(mLauncherActivityInfoClass, "getBadgedIcon", - int.class); } public ComponentName getComponentName() { - return (ComponentName) ReflectUtils.invokeMethod(mLauncherActivityInfo, mGetComponentName); + return mLauncherActivityInfo.getComponentName(); } public UserHandleCompat getUser() { - return UserHandleCompat.fromUser((UserHandle) ReflectUtils.invokeMethod( - mLauncherActivityInfo, mGetUser)); + return UserHandleCompat.fromUser(mLauncherActivityInfo.getUser()); } public CharSequence getLabel() { - return (CharSequence) ReflectUtils.invokeMethod(mLauncherActivityInfo, mGetLabel); + return mLauncherActivityInfo.getLabel(); } public Drawable getIcon(int density) { - return (Drawable) ReflectUtils.invokeMethod(mLauncherActivityInfo, mGetIcon, density); + return mLauncherActivityInfo.getIcon(density); } - public int getApplicationFlags() { - return (Integer) ReflectUtils.invokeMethod(mLauncherActivityInfo, mGetApplicationFlags); + public ApplicationInfo getApplicationInfo() { + return mLauncherActivityInfo.getApplicationInfo(); } public long getFirstInstallTime() { - return (Long) ReflectUtils.invokeMethod(mLauncherActivityInfo, mGetFirstInstallTime); + return mLauncherActivityInfo.getFirstInstallTime(); } public Drawable getBadgedIcon(int density) { - return (Drawable) ReflectUtils.invokeMethod(mLauncherActivityInfo, mGetBadgedIcon, density); + return mLauncherActivityInfo.getBadgedIcon(density); } } diff --git a/src/com/android/launcher3/compat/LauncherAppsCompat.java b/src/com/android/launcher3/compat/LauncherAppsCompat.java index 069e3dea7d..8d978d4a3b 100644 --- a/src/com/android/launcher3/compat/LauncherAppsCompat.java +++ b/src/com/android/launcher3/compat/LauncherAppsCompat.java @@ -39,40 +39,42 @@ public abstract class LauncherAppsCompat { public static final String ACTION_MANAGED_PROFILE_REMOVED = "android.intent.action.MANAGED_PROFILE_REMOVED"; - public interface OnAppsChangedListenerCompat { - void onPackageRemoved(UserHandleCompat user, String packageName); - void onPackageAdded(UserHandleCompat user, String packageName); - void onPackageChanged(UserHandleCompat user, String packageName); - void onPackagesAvailable(UserHandleCompat user, String[] packageNames, boolean replacing); - void onPackagesUnavailable(UserHandleCompat user, String[] packageNames, boolean replacing); + public interface OnAppsChangedCallbackCompat { + void onPackageRemoved(String packageName, UserHandleCompat user); + void onPackageAdded(String packageName, UserHandleCompat user); + void onPackageChanged(String packageName, UserHandleCompat user); + void onPackagesAvailable(String[] packageNames, UserHandleCompat user, boolean replacing); + void onPackagesUnavailable(String[] packageNames, UserHandleCompat user, boolean replacing); } protected LauncherAppsCompat() { } + private static LauncherAppsCompat sInstance; + private static Object sInstanceLock = new Object(); + public static LauncherAppsCompat getInstance(Context context) { - // TODO change this to use api version once L gets an API number. - if ("L".equals(Build.VERSION.CODENAME)) { - Object launcherApps = context.getSystemService("launcherapps"); - if (launcherApps != null) { - LauncherAppsCompatVL compat = LauncherAppsCompatVL.build(context, launcherApps); - if (compat != null) { - return compat; + synchronized (sInstanceLock) { + // TODO change this to use api version once L gets an API number. + if (sInstance == null) { + if ("L".equals(Build.VERSION.CODENAME)) { + sInstance = new LauncherAppsCompatVL(context); + } else { + sInstance = new LauncherAppsCompatV16(context); } } + return sInstance; } - // Pre L or lunacher apps service not running, or reflection failed to find something. - return new LauncherAppsCompatV16(context); } public abstract List getActivityList(String packageName, UserHandleCompat user); public abstract LauncherActivityInfoCompat resolveActivity(Intent intent, UserHandleCompat user); - public abstract void startActivityForProfile(ComponentName component, Rect sourceBounds, - Bundle opts, UserHandleCompat user); - public abstract void addOnAppsChangedListener(OnAppsChangedListenerCompat listener); - public abstract void removeOnAppsChangedListener(OnAppsChangedListenerCompat listener); + public abstract void startActivityForProfile(ComponentName component, UserHandleCompat user, + Rect sourceBounds, Bundle opts); + public abstract void addOnAppsChangedCallback(OnAppsChangedCallbackCompat listener); + public abstract void removeOnAppsChangedCallback(OnAppsChangedCallbackCompat listener); public abstract boolean isPackageEnabledForProfile(String packageName, UserHandleCompat user); public abstract boolean isActivityEnabledForProfile(ComponentName component, UserHandleCompat user); diff --git a/src/com/android/launcher3/compat/LauncherAppsCompatV16.java b/src/com/android/launcher3/compat/LauncherAppsCompatV16.java index c739eb9d2b..f30c28b82a 100644 --- a/src/com/android/launcher3/compat/LauncherAppsCompatV16.java +++ b/src/com/android/launcher3/compat/LauncherAppsCompatV16.java @@ -39,8 +39,8 @@ public class LauncherAppsCompatV16 extends LauncherAppsCompat { private PackageManager mPm; private Context mContext; - private List mListeners - = new ArrayList(); + private List mCallbacks + = new ArrayList(); private PackageMonitor mPackageMonitor; LauncherAppsCompatV16(Context context) { @@ -71,8 +71,8 @@ public class LauncherAppsCompatV16 extends LauncherAppsCompat { return null; } - public void startActivityForProfile(ComponentName component, Rect sourceBounds, - Bundle opts, UserHandleCompat user) { + public void startActivityForProfile(ComponentName component, UserHandleCompat user, + Rect sourceBounds, Bundle opts) { Intent launchIntent = new Intent(Intent.ACTION_MAIN); launchIntent.addCategory(Intent.CATEGORY_LAUNCHER); launchIntent.setComponent(component); @@ -81,18 +81,18 @@ public class LauncherAppsCompatV16 extends LauncherAppsCompat { mContext.startActivity(launchIntent, opts); } - public synchronized void addOnAppsChangedListener(OnAppsChangedListenerCompat listener) { - if (listener != null && !mListeners.contains(listener)) { - mListeners.add(listener); - if (mListeners.size() == 1) { + public synchronized void addOnAppsChangedCallback(OnAppsChangedCallbackCompat callback) { + if (callback != null && !mCallbacks.contains(callback)) { + mCallbacks.add(callback); + if (mCallbacks.size() == 1) { registerForPackageIntents(); } } } - public synchronized void removeOnAppsChangedListener(OnAppsChangedListenerCompat listener) { - mListeners.remove(listener); - if (mListeners.size() == 0) { + public synchronized void removeOnAppsChangedCallback(OnAppsChangedCallbackCompat callback) { + mCallbacks.remove(callback); + if (mCallbacks.size() == 0) { unregisterForPackageIntents(); } } @@ -131,8 +131,8 @@ public class LauncherAppsCompatV16 extends LauncherAppsCompat { mContext.registerReceiver(mPackageMonitor, filter); } - private synchronized List getListeners() { - return new ArrayList(mListeners); + private synchronized List getCallbacks() { + return new ArrayList(mCallbacks); } private class PackageMonitor extends BroadcastReceiver { @@ -151,39 +151,39 @@ public class LauncherAppsCompatV16 extends LauncherAppsCompat { return; } if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) { - for (OnAppsChangedListenerCompat listener : getListeners()) { - listener.onPackageChanged(user, packageName); + for (OnAppsChangedCallbackCompat callback : getCallbacks()) { + callback.onPackageChanged(packageName, user); } } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) { if (!replacing) { - for (OnAppsChangedListenerCompat listener : getListeners()) { - listener.onPackageRemoved(user, packageName); + for (OnAppsChangedCallbackCompat callback : getCallbacks()) { + callback.onPackageRemoved(packageName, user); } } // else, we are replacing the package, so a PACKAGE_ADDED will be sent // later, we will update the package at this time } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) { if (!replacing) { - for (OnAppsChangedListenerCompat listener : getListeners()) { - listener.onPackageAdded(user, packageName); + for (OnAppsChangedCallbackCompat callback : getCallbacks()) { + callback.onPackageAdded(packageName, user); } } else { - for (OnAppsChangedListenerCompat listener : getListeners()) { - listener.onPackageChanged(user, packageName); + for (OnAppsChangedCallbackCompat callback : getCallbacks()) { + callback.onPackageChanged(packageName, user); } } } } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) { final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); - for (OnAppsChangedListenerCompat listener : getListeners()) { - listener.onPackagesAvailable(user, packages, replacing); + for (OnAppsChangedCallbackCompat callback : getCallbacks()) { + callback.onPackagesAvailable(packages, user, replacing); } } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) { final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); String[] packages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); - for (OnAppsChangedListenerCompat listener : getListeners()) { - listener.onPackagesUnavailable(user, packages, replacing); + for (OnAppsChangedCallbackCompat callback : getCallbacks()) { + callback.onPackagesUnavailable(packages, user, replacing); } } } diff --git a/src/com/android/launcher3/compat/LauncherAppsCompatVL.java b/src/com/android/launcher3/compat/LauncherAppsCompatVL.java index 21f2659ba5..ca9b671460 100644 --- a/src/com/android/launcher3/compat/LauncherAppsCompatVL.java +++ b/src/com/android/launcher3/compat/LauncherAppsCompatVL.java @@ -19,99 +19,48 @@ package com.android.launcher3.compat; import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.pm.LauncherActivityInfo; +import android.content.pm.LauncherApps; import android.graphics.Rect; import android.os.Build; import android.os.Bundle; import android.os.UserHandle; -import java.lang.reflect.InvocationHandler; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.lang.reflect.Proxy; -import java.lang.reflect.Method; - public class LauncherAppsCompatVL extends LauncherAppsCompat { - private Object mLauncherApps; - private Class mLauncherAppsClass; - private Class mListenerClass; - private Method mGetActivityList; - private Method mResolveActivity; - private Method mStartActivityForProfile; - private Method mAddOnAppsChangedListener; - private Method mRemoveOnAppsChangedListener; - private Method mIsPackageEnabledForProfile; - private Method mIsActivityEnabledForProfile; + private LauncherApps mLauncherApps; - private Map mListeners - = new HashMap(); + private Map mCallbacks + = new HashMap(); - static LauncherAppsCompatVL build(Context context, Object launcherApps) { - LauncherAppsCompatVL compat = new LauncherAppsCompatVL(context, launcherApps); - - compat.mListenerClass = ReflectUtils.getClassForName( - "android.content.pm.LauncherApps$OnAppsChangedListener"); - compat.mLauncherAppsClass = ReflectUtils.getClassForName("android.content.pm.LauncherApps"); - - compat.mGetActivityList = ReflectUtils.getMethod(compat.mLauncherAppsClass, - "getActivityList", - String.class, UserHandle.class); - compat.mResolveActivity = ReflectUtils.getMethod(compat.mLauncherAppsClass, - "resolveActivity", - Intent.class, UserHandle.class); - compat.mStartActivityForProfile = ReflectUtils.getMethod(compat.mLauncherAppsClass, - "startActivityForProfile", - ComponentName.class, Rect.class, Bundle.class, UserHandle.class); - compat.mAddOnAppsChangedListener = ReflectUtils.getMethod(compat.mLauncherAppsClass, - "addOnAppsChangedListener", compat.mListenerClass); - compat.mRemoveOnAppsChangedListener = ReflectUtils.getMethod(compat.mLauncherAppsClass, - "removeOnAppsChangedListener", compat.mListenerClass); - compat.mIsPackageEnabledForProfile = ReflectUtils.getMethod(compat.mLauncherAppsClass, - "isPackageEnabledForProfile", String.class, UserHandle.class); - compat.mIsActivityEnabledForProfile = ReflectUtils.getMethod(compat.mLauncherAppsClass, - "isActivityEnabledForProfile", ComponentName.class, UserHandle.class); - - if (compat.mListenerClass != null - && compat.mLauncherAppsClass != null - && compat.mGetActivityList != null - && compat.mResolveActivity != null - && compat.mStartActivityForProfile != null - && compat.mAddOnAppsChangedListener != null - && compat.mRemoveOnAppsChangedListener != null - && compat.mIsPackageEnabledForProfile != null - && compat.mIsActivityEnabledForProfile != null) { - return compat; - } - return null; - } - - private LauncherAppsCompatVL(Context context, Object launcherApps) { + LauncherAppsCompatVL(Context context) { super(); - mLauncherApps = launcherApps; + mLauncherApps = (LauncherApps) context.getSystemService("launcherapps"); } public List getActivityList(String packageName, UserHandleCompat user) { - List list = (List) ReflectUtils.invokeMethod(mLauncherApps, - mGetActivityList, packageName, user.getUser()); + List list = mLauncherApps.getActivityList(packageName, + user.getUser()); if (list.size() == 0) { return Collections.EMPTY_LIST; } ArrayList compatList = new ArrayList(list.size()); - for (Object info : list) { + for (LauncherActivityInfo info : list) { compatList.add(new LauncherActivityInfoCompatVL(info)); } return compatList; } public LauncherActivityInfoCompat resolveActivity(Intent intent, UserHandleCompat user) { - Object activity = ReflectUtils.invokeMethod(mLauncherApps, mResolveActivity, - intent, user.getUser()); + LauncherActivityInfo activity = mLauncherApps.resolveActivity(intent, user.getUser()); if (activity != null) { return new LauncherActivityInfoCompatVL(activity); } else { @@ -119,89 +68,64 @@ public class LauncherAppsCompatVL extends LauncherAppsCompat { } } - public void startActivityForProfile(ComponentName component, Rect sourceBounds, - Bundle opts, UserHandleCompat user) { - ReflectUtils.invokeMethod(mLauncherApps, mStartActivityForProfile, - component, sourceBounds, opts, user.getUser()); + public void startActivityForProfile(ComponentName component, UserHandleCompat user, + Rect sourceBounds, Bundle opts) { + mLauncherApps.startActivityForProfile(component, user.getUser(), sourceBounds, opts); } - public void addOnAppsChangedListener(LauncherAppsCompat.OnAppsChangedListenerCompat listener) { - Object wrappedListener = Proxy.newProxyInstance(mListenerClass.getClassLoader(), - new Class[]{mListenerClass}, new WrappedListener(listener)); - synchronized (mListeners) { - mListeners.put(listener, wrappedListener); + public void addOnAppsChangedCallback(LauncherAppsCompat.OnAppsChangedCallbackCompat callback) { + WrappedCallback wrappedCallback = new WrappedCallback(callback); + synchronized (mCallbacks) { + mCallbacks.put(callback, wrappedCallback); } - ReflectUtils.invokeMethod(mLauncherApps, mAddOnAppsChangedListener, wrappedListener); + mLauncherApps.addOnAppsChangedCallback(wrappedCallback); } - public void removeOnAppsChangedListener( - LauncherAppsCompat.OnAppsChangedListenerCompat listener) { - Object wrappedListener = null; - synchronized (mListeners) { - wrappedListener = mListeners.remove(listener); + public void removeOnAppsChangedCallback( + LauncherAppsCompat.OnAppsChangedCallbackCompat callback) { + WrappedCallback wrappedCallback = null; + synchronized (mCallbacks) { + wrappedCallback = mCallbacks.remove(callback); } - if (wrappedListener != null) { - ReflectUtils.invokeMethod(mLauncherApps, mRemoveOnAppsChangedListener, wrappedListener); + if (wrappedCallback != null) { + mLauncherApps.removeOnAppsChangedCallback(wrappedCallback); } } public boolean isPackageEnabledForProfile(String packageName, UserHandleCompat user) { - return (Boolean) ReflectUtils.invokeMethod(mLauncherApps, mIsPackageEnabledForProfile, - packageName, user.getUser()); + return mLauncherApps.isPackageEnabledForProfile(packageName, user.getUser()); } public boolean isActivityEnabledForProfile(ComponentName component, UserHandleCompat user) { - return (Boolean) ReflectUtils.invokeMethod(mLauncherApps, mIsActivityEnabledForProfile, - component, user.getUser()); + return mLauncherApps.isActivityEnabledForProfile(component, user.getUser()); } - private static class WrappedListener implements InvocationHandler { - private LauncherAppsCompat.OnAppsChangedListenerCompat mListener; + private static class WrappedCallback extends LauncherApps.OnAppsChangedCallback { + private LauncherAppsCompat.OnAppsChangedCallbackCompat mCallback; - public WrappedListener(LauncherAppsCompat.OnAppsChangedListenerCompat listener) { - mListener = listener; + public WrappedCallback(LauncherAppsCompat.OnAppsChangedCallbackCompat callback) { + mCallback = callback; } - public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { - try { - String methodName = m.getName(); - if ("onPackageRemoved".equals(methodName)) { - onPackageRemoved((UserHandle) args[0], (String) args[1]); - } else if ("onPackageAdded".equals(methodName)) { - onPackageAdded((UserHandle) args[0], (String) args[1]); - } else if ("onPackageChanged".equals(methodName)) { - onPackageChanged((UserHandle) args[0], (String) args[1]); - } else if ("onPackagesAvailable".equals(methodName)) { - onPackagesAvailable((UserHandle) args[0], (String []) args[1], - (Boolean) args[2]); - } else if ("onPackagesUnavailable".equals(methodName)) { - onPackagesUnavailable((UserHandle) args[0], (String []) args[1], - (Boolean) args[2]); - } - } finally { - return null; - } + public void onPackageRemoved(String packageName, UserHandle user) { + mCallback.onPackageRemoved(packageName, UserHandleCompat.fromUser(user)); } - public void onPackageRemoved(UserHandle user, String packageName) { - mListener.onPackageRemoved(UserHandleCompat.fromUser(user), packageName); + public void onPackageAdded(String packageName, UserHandle user) { + mCallback.onPackageAdded(packageName, UserHandleCompat.fromUser(user)); } - public void onPackageAdded(UserHandle user, String packageName) { - mListener.onPackageAdded(UserHandleCompat.fromUser(user), packageName); + public void onPackageChanged(String packageName, UserHandle user) { + mCallback.onPackageChanged(packageName, UserHandleCompat.fromUser(user)); } - public void onPackageChanged(UserHandle user, String packageName) { - mListener.onPackageChanged(UserHandleCompat.fromUser(user), packageName); + public void onPackagesAvailable(String[] packageNames, UserHandle user, boolean replacing) { + mCallback.onPackagesAvailable(packageNames, UserHandleCompat.fromUser(user), replacing); } - public void onPackagesAvailable(UserHandle user, String[] packageNames, boolean replacing) { - mListener.onPackagesAvailable(UserHandleCompat.fromUser(user), packageNames, replacing); - } - - public void onPackagesUnavailable(UserHandle user, String[] packageNames, + public void onPackagesUnavailable(String[] packageNames, UserHandle user, boolean replacing) { - mListener.onPackagesUnavailable(UserHandleCompat.fromUser(user), packageNames, + mCallback.onPackagesUnavailable(packageNames, UserHandleCompat.fromUser(user), replacing); } } diff --git a/src/com/android/launcher3/compat/ReflectUtils.java b/src/com/android/launcher3/compat/ReflectUtils.java deleted file mode 100644 index e1b8a1f950..0000000000 --- a/src/com/android/launcher3/compat/ReflectUtils.java +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Copyright (C) 2014 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.util.Log; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -public class ReflectUtils { - private static final String TAG = "LauncherReflect"; - - public static Class getClassForName(String className) { - try { - return Class.forName(className); - } catch (ClassNotFoundException e) { - Log.e(TAG, "Couldn't find class " + className, e); - return null; - } - } - - public static Method getMethod(Class clazz, String method) { - try { - return clazz.getMethod(method); - } catch (NoSuchMethodException e) { - Log.e(TAG, "Couldn't find methid " + clazz.getName() + " " + method, e); - return null; - } - } - - public static Method getMethod(Class clazz, String method, Class param1) { - try { - return clazz.getMethod(method, param1); - } catch (NoSuchMethodException e) { - Log.e(TAG, "Couldn't find methid " + clazz.getName() + " " + method, e); - return null; - } - } - - public static Method getMethod(Class clazz, String method, Class param1, Class param2) { - try { - return clazz.getMethod(method, param1, param2); - } catch (NoSuchMethodException e) { - Log.e(TAG, "Couldn't find methid " + clazz.getName() + " " + method, e); - return null; - } - } - - public static Method getMethod(Class clazz, String method, Class param1, Class param2, - Class param3) { - try { - return clazz.getMethod(method, param1, param2, param3); - } catch (NoSuchMethodException e) { - Log.e(TAG, "Couldn't find methid " + clazz.getName() + " " + method, e); - return null; - } - } - - public static Method getMethod(Class clazz, String method, Class param1, Class param2, - Class param3, Class param4) { - try { - return clazz.getMethod(method, param1, param2, param3, param4); - } catch (NoSuchMethodException e) { - Log.e(TAG, "Couldn't find methid " + clazz.getName() + " " + method, e); - return null; - } - } - - public static Object invokeMethod(Object object, Method method) { - try { - return method.invoke(object); - } catch (SecurityException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } catch (IllegalArgumentException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } catch (IllegalAccessException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } catch (InvocationTargetException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } - return null; - } - - public static Object invokeMethod(Object object, Method method, Object param1) { - try { - return method.invoke(object, param1); - } catch (SecurityException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } catch (IllegalArgumentException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } catch (IllegalAccessException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } catch (InvocationTargetException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } - return null; - } - - public static Object invokeMethod(Object object, Method method, Object param1, Object param2) { - try { - return method.invoke(object, param1, param2); - } catch (SecurityException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } catch (IllegalArgumentException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } catch (IllegalAccessException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } catch (InvocationTargetException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } - return null; - } - - public static Object invokeMethod(Object object, Method method, Object param1, Object param2, - Object param3) { - try { - return method.invoke(object, param1, param2, param3); - } catch (SecurityException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } catch (IllegalArgumentException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } catch (IllegalAccessException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } catch (InvocationTargetException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } - return null; - } - - public static Object invokeMethod(Object object, Method method, Object param1, Object param2, - Object param3, Object param4) { - try { - return method.invoke(object, param1, param2, param3, param4); - } catch (SecurityException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } catch (IllegalArgumentException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } catch (IllegalAccessException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } catch (InvocationTargetException e) { - Log.e(TAG, "Couldn't invoke method " + method, e); - } - return null; - } -} \ No newline at end of file diff --git a/src/com/android/launcher3/compat/UserManagerCompat.java b/src/com/android/launcher3/compat/UserManagerCompat.java index 256e04a7bc..c10a74307b 100644 --- a/src/com/android/launcher3/compat/UserManagerCompat.java +++ b/src/com/android/launcher3/compat/UserManagerCompat.java @@ -41,4 +41,5 @@ public abstract class UserManagerCompat { public abstract long getSerialNumberForUser(UserHandleCompat user); public abstract UserHandleCompat getUserForSerialNumber(long serialNumber); public abstract Drawable getBadgedDrawableForUser(Drawable unbadged, UserHandleCompat user); + public abstract String getBadgedLabelForUser(String label, UserHandleCompat user); } diff --git a/src/com/android/launcher3/compat/UserManagerCompatV16.java b/src/com/android/launcher3/compat/UserManagerCompatV16.java index 2009e4e274..03dd0972c5 100644 --- a/src/com/android/launcher3/compat/UserManagerCompatV16.java +++ b/src/com/android/launcher3/compat/UserManagerCompatV16.java @@ -44,4 +44,8 @@ public class UserManagerCompatV16 extends UserManagerCompat { public long getSerialNumberForUser(UserHandleCompat user) { return 0; } + + public String getBadgedLabelForUser(String label, UserHandleCompat user) { + return label; + } } diff --git a/src/com/android/launcher3/compat/UserManagerCompatVL.java b/src/com/android/launcher3/compat/UserManagerCompatVL.java index 8d3ca8577d..e071a8f71c 100644 --- a/src/com/android/launcher3/compat/UserManagerCompatVL.java +++ b/src/com/android/launcher3/compat/UserManagerCompatVL.java @@ -22,9 +22,8 @@ import android.graphics.drawable.Drawable; import android.os.UserHandle; import android.os.UserManager; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class UserManagerCompatVL extends UserManagerCompatV17 { @@ -34,35 +33,27 @@ public class UserManagerCompatVL extends UserManagerCompatV17 { } public List getUserProfiles() { - Method method = ReflectUtils.getMethod(mUserManager.getClass(), "getUserProfiles"); - if (method != null) { - List users = (List) ReflectUtils.invokeMethod( - mUserManager, method); - if (users != null) { - ArrayList compatUsers = new ArrayList( - users.size()); - for (UserHandle user : users) { - compatUsers.add(UserHandleCompat.fromUser(user)); - } - return compatUsers; - } + List users = mUserManager.getUserProfiles(); + if (users == null) { + return Collections.EMPTY_LIST; } - // Fall back to non L version. - return super.getUserProfiles(); + ArrayList compatUsers = new ArrayList( + users.size()); + for (UserHandle user : users) { + compatUsers.add(UserHandleCompat.fromUser(user)); + } + return compatUsers; } public Drawable getBadgedDrawableForUser(Drawable unbadged, UserHandleCompat user) { - Method method = ReflectUtils.getMethod(mUserManager.getClass(), "getBadgedDrawableForUser", - Drawable.class, UserHandle.class); - if (method != null) { - Drawable d = (Drawable) ReflectUtils.invokeMethod(mUserManager, method, unbadged, - user.getUser()); - if (d != null) { - return d; - } + return mUserManager.getBadgedDrawableForUser(unbadged, user.getUser()); + } + + public String getBadgedLabelForUser(String label, UserHandleCompat user) { + if (user == null) { + return label; } - // Fall back to non L version. - return super.getBadgedDrawableForUser(unbadged, user); + return mUserManager.getBadgedLabelForUser(label, user.getUser()); } }