diff options
author | Sunny Goyal <sunnygoyal@google.com> | 2017-05-18 15:03:13 -0700 |
---|---|---|
committer | Sunny Goyal <sunnygoyal@google.com> | 2017-05-22 11:00:29 -0700 |
commit | 22ca9ec137ec8eb70026f90591ae01af7669a3b7 (patch) | |
tree | bc1cb3cc2953626bc30bf6f159e8174c1edd5db0 /src/com/android/launcher3/compat | |
parent | 8bca47adc52d38a58cc7b4bf01c80172d1757243 (diff) | |
download | android_packages_apps_Trebuchet-22ca9ec137ec8eb70026f90591ae01af7669a3b7.tar.gz android_packages_apps_Trebuchet-22ca9ec137ec8eb70026f90591ae01af7669a3b7.tar.bz2 android_packages_apps_Trebuchet-22ca9ec137ec8eb70026f90591ae01af7669a3b7.zip |
Removing various reflection calls with final APIs
Change-Id: Ibf48d6015d808f86bc79ccf64e3077eb5b6ccaff
Diffstat (limited to 'src/com/android/launcher3/compat')
4 files changed, 92 insertions, 226 deletions
diff --git a/src/com/android/launcher3/compat/LauncherAppsCompat.java b/src/com/android/launcher3/compat/LauncherAppsCompat.java index 472cfc9ec..26f4ae721 100644 --- a/src/com/android/launcher3/compat/LauncherAppsCompat.java +++ b/src/com/android/launcher3/compat/LauncherAppsCompat.java @@ -26,13 +26,8 @@ import android.os.Bundle; import android.os.UserHandle; import android.support.annotation.Nullable; -import com.android.launcher3.LauncherAppState; -import com.android.launcher3.LauncherModel; -import com.android.launcher3.ShortcutInfo; import com.android.launcher3.Utilities; -import com.android.launcher3.graphics.LauncherIcons; import com.android.launcher3.shortcuts.ShortcutInfoCompat; -import com.android.launcher3.util.LooperExecutor; import com.android.launcher3.util.PackageUserKey; import java.util.List; @@ -88,62 +83,6 @@ public abstract class LauncherAppsCompat { public abstract List<ShortcutConfigActivityInfo> getCustomShortcutActivityList( @Nullable PackageUserKey packageUser); - /** - * request.accept() will initiate the following flow: - * -> go-to-system-process for actual processing (a) - * -> callback-to-launcher on UI thread (b) - * -> post callback on the worker thread (c) - * -> Update model and unpin (in system) any shortcut not in out model. (d) - * - * Note that (b) will take at-least one frame as it involves posting callback from binder - * thread to UI thread. - * If (d) happens before we add this shortcut to our model, we will end up unpinning - * the shortcut in the system. - * Here its the caller's responsibility to add the newly created ShortcutInfo immediately - * to the model (which may involves a single post-to-worker-thread). That will guarantee - * that (d) happens after model is updated. - */ - @Nullable - public static ShortcutInfo createShortcutInfoFromPinItemRequest( - Context context, final PinItemRequestCompat request, final long acceptDelay) { - if (request != null && - request.getRequestType() == PinItemRequestCompat.REQUEST_TYPE_SHORTCUT && - request.isValid()) { - - if (acceptDelay <= 0) { - if (!request.accept()) { - return null; - } - } else { - // Block the worker thread until the accept() is called. - new LooperExecutor(LauncherModel.getWorkerLooper()).execute(new Runnable() { - @Override - public void run() { - try { - Thread.sleep(acceptDelay); - } catch (InterruptedException e) { - // Ignore - } - if (request.isValid()) { - request.accept(); - } - } - }); - } - - ShortcutInfoCompat compat = new ShortcutInfoCompat(request.getShortcutInfo()); - ShortcutInfo info = new ShortcutInfo(compat, context); - // Apply the unbadged icon and fetch the actual icon asynchronously. - info.iconBitmap = LauncherIcons - .createShortcutIcon(compat, context, false /* badged */); - LauncherAppState.getInstance(context).getModel() - .updateAndBindShortcutInfo(info, compat); - return info; - } else { - return null; - } - } - public void showAppDetailsForProfile(ComponentName component, UserHandle user) { showAppDetailsForProfile(component, user, null, null); } diff --git a/src/com/android/launcher3/compat/LauncherAppsCompatVO.java b/src/com/android/launcher3/compat/LauncherAppsCompatVO.java index d145539c7..3214b4672 100644 --- a/src/com/android/launcher3/compat/LauncherAppsCompatVO.java +++ b/src/com/android/launcher3/compat/LauncherAppsCompatVO.java @@ -18,20 +18,27 @@ package com.android.launcher3.compat; import android.annotation.TargetApi; import android.content.Context; +import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.LauncherActivityInfo; import android.content.pm.LauncherApps; +import android.content.pm.LauncherApps.PinItemRequest; import android.content.pm.PackageManager; import android.os.Build; +import android.os.Parcelable; import android.os.Process; import android.os.UserHandle; import android.support.annotation.Nullable; -import android.util.Log; +import com.android.launcher3.LauncherAppState; +import com.android.launcher3.LauncherModel; +import com.android.launcher3.ShortcutInfo; import com.android.launcher3.compat.ShortcutConfigActivityInfo.ShortcutConfigActivityInfoVO; +import com.android.launcher3.graphics.LauncherIcons; +import com.android.launcher3.shortcuts.ShortcutInfoCompat; +import com.android.launcher3.util.LooperExecutor; import com.android.launcher3.util.PackageUserKey; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; @@ -45,11 +52,6 @@ public class LauncherAppsCompatVO extends LauncherAppsCompatVL { @Override public ApplicationInfo getApplicationInfo(String packageName, int flags, UserHandle user) { try { - // TODO: Temporary workaround until the API signature is updated - if (false) { - throw new PackageManager.NameNotFoundException(); - } - ApplicationInfo info = mLauncherApps.getApplicationInfo(packageName, flags, user); return (info.flags & ApplicationInfo.FLAG_INSTALLED) == 0 || !info.enabled ? null : info; @@ -64,34 +66,89 @@ public class LauncherAppsCompatVO extends LauncherAppsCompatVL { List<ShortcutConfigActivityInfo> result = new ArrayList<>(); UserHandle myUser = Process.myUserHandle(); - try { - Method m = LauncherApps.class.getDeclaredMethod("getShortcutConfigActivityList", - String.class, UserHandle.class); - final List<UserHandle> users; - final String packageName; - if (packageUser == null) { - users = UserManagerCompat.getInstance(mContext).getUserProfiles(); - packageName = null; - } else { - users = new ArrayList<>(1); - users.add(packageUser.mUser); - packageName = packageUser.mPackageName; - } - for (UserHandle user : users) { - boolean ignoreTargetSdk = myUser.equals(user); - List<LauncherActivityInfo> activities = - (List<LauncherActivityInfo>) m.invoke(mLauncherApps, packageName, user); - for (LauncherActivityInfo activityInfo : activities) { - if (ignoreTargetSdk || activityInfo.getApplicationInfo().targetSdkVersion >= - Build.VERSION_CODES.O) { - result.add(new ShortcutConfigActivityInfoVO(activityInfo)); - } + final List<UserHandle> users; + final String packageName; + if (packageUser == null) { + users = UserManagerCompat.getInstance(mContext).getUserProfiles(); + packageName = null; + } else { + users = new ArrayList<>(1); + users.add(packageUser.mUser); + packageName = packageUser.mPackageName; + } + for (UserHandle user : users) { + boolean ignoreTargetSdk = myUser.equals(user); + List<LauncherActivityInfo> activities = + mLauncherApps.getShortcutConfigActivityList(packageName, user); + for (LauncherActivityInfo activityInfo : activities) { + if (ignoreTargetSdk || activityInfo.getApplicationInfo().targetSdkVersion >= + Build.VERSION_CODES.O) { + result.add(new ShortcutConfigActivityInfoVO(activityInfo)); } } - } catch (Exception e) { - Log.e("LauncherAppsCompatVO", "Error calling new API", e); } return result; } + + /** + * request.accept() will initiate the following flow: + * -> go-to-system-process for actual processing (a) + * -> callback-to-launcher on UI thread (b) + * -> post callback on the worker thread (c) + * -> Update model and unpin (in system) any shortcut not in out model. (d) + * + * Note that (b) will take at-least one frame as it involves posting callback from binder + * thread to UI thread. + * If (d) happens before we add this shortcut to our model, we will end up unpinning + * the shortcut in the system. + * Here its the caller's responsibility to add the newly created ShortcutInfo immediately + * to the model (which may involves a single post-to-worker-thread). That will guarantee + * that (d) happens after model is updated. + */ + @Nullable + public static ShortcutInfo createShortcutInfoFromPinItemRequest( + Context context, final PinItemRequest request, final long acceptDelay) { + if (request != null && + request.getRequestType() == PinItemRequest.REQUEST_TYPE_SHORTCUT && + request.isValid()) { + + if (acceptDelay <= 0) { + if (!request.accept()) { + return null; + } + } else { + // Block the worker thread until the accept() is called. + new LooperExecutor(LauncherModel.getWorkerLooper()).execute(new Runnable() { + @Override + public void run() { + try { + Thread.sleep(acceptDelay); + } catch (InterruptedException e) { + // Ignore + } + if (request.isValid()) { + request.accept(); + } + } + }); + } + + ShortcutInfoCompat compat = new ShortcutInfoCompat(request.getShortcutInfo()); + ShortcutInfo info = new ShortcutInfo(compat, context); + // Apply the unbadged icon and fetch the actual icon asynchronously. + info.iconBitmap = LauncherIcons + .createShortcutIcon(compat, context, false /* badged */); + LauncherAppState.getInstance(context).getModel() + .updateAndBindShortcutInfo(info, compat); + return info; + } else { + return null; + } + } + + public static PinItemRequest getPinItemRequest(Intent intent) { + Parcelable extra = intent.getParcelableExtra(LauncherApps.EXTRA_PIN_ITEM_REQUEST); + return extra instanceof PinItemRequest ? (PinItemRequest) extra : null; + } } diff --git a/src/com/android/launcher3/compat/PinItemRequestCompat.java b/src/com/android/launcher3/compat/PinItemRequestCompat.java deleted file mode 100644 index 1308cba97..000000000 --- a/src/com/android/launcher3/compat/PinItemRequestCompat.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (C) 2017 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.appwidget.AppWidgetProviderInfo; -import android.content.Context; -import android.content.Intent; -import android.content.pm.ShortcutInfo; -import android.os.Bundle; -import android.os.Parcel; -import android.os.Parcelable; - -import com.android.launcher3.Utilities; - -/** - * A wrapper around platform implementation of PinItemRequestCompat until the - * updated SDK is available. - */ -public class PinItemRequestCompat implements Parcelable { - - public static final String EXTRA_PIN_ITEM_REQUEST = "android.content.pm.extra.PIN_ITEM_REQUEST"; - - public static final int REQUEST_TYPE_SHORTCUT = 1; - public static final int REQUEST_TYPE_APPWIDGET = 2; - - private final Parcelable mObject; - - private PinItemRequestCompat(Parcelable object) { - mObject = object; - } - - public int getRequestType() { - return (Integer) invokeMethod("getRequestType"); - } - - public ShortcutInfo getShortcutInfo() { - return (ShortcutInfo) invokeMethod("getShortcutInfo"); - } - - public AppWidgetProviderInfo getAppWidgetProviderInfo(Context context) { - try { - return (AppWidgetProviderInfo) mObject.getClass() - .getDeclaredMethod("getAppWidgetProviderInfo", Context.class) - .invoke(mObject, context); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - public boolean isValid() { - return (Boolean) invokeMethod("isValid"); - } - - public boolean accept() { - return (Boolean) invokeMethod("accept"); - } - - public boolean accept(Bundle options) { - try { - return (Boolean) mObject.getClass().getDeclaredMethod("accept", Bundle.class) - .invoke(mObject, options); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - public Bundle getExtras() { - try { - return (Bundle) mObject.getClass().getDeclaredMethod("getExtras").invoke(mObject); - } catch (Exception e) { - return null; - } - } - - private Object invokeMethod(String methodName) { - try { - return mObject.getClass().getDeclaredMethod(methodName).invoke(mObject); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(Parcel parcel, int i) { - parcel.writeParcelable(mObject, i); - } - - public static final Parcelable.Creator<PinItemRequestCompat> CREATOR = - new Parcelable.Creator<PinItemRequestCompat>() { - public PinItemRequestCompat createFromParcel(Parcel source) { - Parcelable object = source.readParcelable(null); - return new PinItemRequestCompat(object); - } - - public PinItemRequestCompat[] newArray(int size) { - return new PinItemRequestCompat[size]; - } - }; - - public static PinItemRequestCompat getPinItemRequest(Intent intent) { - if (!Utilities.isAtLeastO()) { - return null; - } - Parcelable extra = intent.getParcelableExtra(EXTRA_PIN_ITEM_REQUEST); - return extra == null ? null : new PinItemRequestCompat(extra); - } -} diff --git a/src/com/android/launcher3/compat/ShortcutConfigActivityInfo.java b/src/com/android/launcher3/compat/ShortcutConfigActivityInfo.java index 4a55e8ca0..6bdc62743 100644 --- a/src/com/android/launcher3/compat/ShortcutConfigActivityInfo.java +++ b/src/com/android/launcher3/compat/ShortcutConfigActivityInfo.java @@ -37,8 +37,6 @@ import com.android.launcher3.LauncherSettings; import com.android.launcher3.R; import com.android.launcher3.ShortcutInfo; -import java.lang.reflect.Method; - /** * Wrapper class for representing a shortcut configure activity. */ @@ -151,15 +149,13 @@ public abstract class ShortcutConfigActivityInfo { if (getUser().equals(Process.myUserHandle())) { return super.startConfigActivity(activity, requestCode); } + IntentSender is = activity.getSystemService(LauncherApps.class) + .getShortcutConfigActivityIntent(mInfo); try { - Method m = LauncherApps.class.getDeclaredMethod( - "getShortcutConfigActivityIntent", LauncherActivityInfo.class); - IntentSender is = (IntentSender) m.invoke( - activity.getSystemService(LauncherApps.class), mInfo); activity.startIntentSenderForResult(is, requestCode, null, 0, 0, 0); return true; - } catch (Exception e) { - Log.e(TAG, "Error calling new API", e); + } catch (IntentSender.SendIntentException e) { + Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); return false; } } |