From 782f0c9a896db58aeaa60d15f291831b8d7b4c93 Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Thu, 19 Jan 2017 10:27:54 -0800 Subject: Adding support for new APIs in O related to configurable shortcuts > Config activities can now return PinItemRequest which can be used to pin deep shortcuts Bug: 33584624 Change-Id: Ic0df436bd79e069615b9d60d24eb7594b824b2da --- .../launcher3/compat/LauncherAppsCompat.java | 8 +- .../launcher3/compat/LauncherAppsCompatVL.java | 18 ++- .../launcher3/compat/LauncherAppsCompatVO.java | 56 ++++++++ .../compat/ShortcutConfigActivityInfo.java | 148 +++++++++++++++++++++ 4 files changed, 228 insertions(+), 2 deletions(-) create mode 100644 src/com/android/launcher3/compat/LauncherAppsCompatVO.java create mode 100644 src/com/android/launcher3/compat/ShortcutConfigActivityInfo.java (limited to 'src/com/android/launcher3/compat') diff --git a/src/com/android/launcher3/compat/LauncherAppsCompat.java b/src/com/android/launcher3/compat/LauncherAppsCompat.java index 5c6eef8c6..281069af0 100644 --- a/src/com/android/launcher3/compat/LauncherAppsCompat.java +++ b/src/com/android/launcher3/compat/LauncherAppsCompat.java @@ -24,6 +24,7 @@ import android.graphics.Rect; import android.os.Bundle; import android.os.UserHandle; +import com.android.launcher3.Utilities; import com.android.launcher3.shortcuts.ShortcutInfoCompat; import java.util.List; @@ -51,7 +52,11 @@ public abstract class LauncherAppsCompat { public static LauncherAppsCompat getInstance(Context context) { synchronized (sInstanceLock) { if (sInstance == null) { - sInstance = new LauncherAppsCompatVL(context.getApplicationContext()); + if (Utilities.isAtLeastO()) { + sInstance = new LauncherAppsCompatVO(context.getApplicationContext()); + } else { + sInstance = new LauncherAppsCompatVL(context.getApplicationContext()); + } } return sInstance; } @@ -69,4 +74,5 @@ public abstract class LauncherAppsCompat { public abstract boolean isPackageEnabledForProfile(String packageName, UserHandle user); public abstract boolean isActivityEnabledForProfile(ComponentName component, UserHandle user); + public abstract List getCustomShortcutActivityList(); } diff --git a/src/com/android/launcher3/compat/LauncherAppsCompatVL.java b/src/com/android/launcher3/compat/LauncherAppsCompatVL.java index e2739c10e..3cb721ccc 100644 --- a/src/com/android/launcher3/compat/LauncherAppsCompatVL.java +++ b/src/com/android/launcher3/compat/LauncherAppsCompatVL.java @@ -21,11 +21,14 @@ import android.content.Context; import android.content.Intent; import android.content.pm.LauncherActivityInfo; import android.content.pm.LauncherApps; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.content.pm.ShortcutInfo; import android.graphics.Rect; import android.os.Bundle; import android.os.UserHandle; +import com.android.launcher3.compat.ShortcutConfigActivityInfo.ShortcutConfigActivityInfoVL; import com.android.launcher3.shortcuts.ShortcutInfoCompat; import java.util.ArrayList; @@ -35,11 +38,13 @@ import java.util.Map; public class LauncherAppsCompatVL extends LauncherAppsCompat { - protected LauncherApps mLauncherApps; + protected final LauncherApps mLauncherApps; + protected final Context mContext; private Map mCallbacks = new HashMap<>(); LauncherAppsCompatVL(Context context) { + mContext = context; mLauncherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE); } @@ -140,5 +145,16 @@ public class LauncherAppsCompatVL extends LauncherAppsCompat { mCallback.onShortcutsChanged(packageName, shortcutInfoCompats, user); } } + + @Override + public List getCustomShortcutActivityList() { + PackageManager pm = mContext.getPackageManager(); + List result = new ArrayList<>(); + for (ResolveInfo info : + pm.queryIntentActivities(new Intent(Intent.ACTION_CREATE_SHORTCUT), 0)) { + result.add(new ShortcutConfigActivityInfoVL(info.activityInfo, pm)); + } + return result; + } } diff --git a/src/com/android/launcher3/compat/LauncherAppsCompatVO.java b/src/com/android/launcher3/compat/LauncherAppsCompatVO.java new file mode 100644 index 000000000..0610726a7 --- /dev/null +++ b/src/com/android/launcher3/compat/LauncherAppsCompatVO.java @@ -0,0 +1,56 @@ +/* + * 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.content.Context; +import android.content.pm.LauncherActivityInfo; +import android.content.pm.LauncherApps; +import android.os.UserHandle; +import android.util.Log; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import com.android.launcher3.compat.ShortcutConfigActivityInfo.*; + +public class LauncherAppsCompatVO extends LauncherAppsCompatVL { + + LauncherAppsCompatVO(Context context) { + super(context); + } + + @Override + public List getCustomShortcutActivityList() { + List result = new ArrayList<>(); + + try { + Method m = LauncherApps.class.getDeclaredMethod("getShortcutConfigActivityList", + String.class, UserHandle.class); + for (UserHandle user : UserManagerCompat.getInstance(mContext).getUserProfiles()) { + List activities = + (List) m.invoke(mLauncherApps, null, user); + for (LauncherActivityInfo activityInfo : activities) { + result.add(new ShortcutConfigActivityInfoVO(activityInfo)); + } + } + } catch (Exception e) { + Log.e("LauncherAppsCompatVO", "Error calling new API", e); + } + + return result; + } +} diff --git a/src/com/android/launcher3/compat/ShortcutConfigActivityInfo.java b/src/com/android/launcher3/compat/ShortcutConfigActivityInfo.java new file mode 100644 index 000000000..dd375dfa4 --- /dev/null +++ b/src/com/android/launcher3/compat/ShortcutConfigActivityInfo.java @@ -0,0 +1,148 @@ +/* + * 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.annotation.TargetApi; +import android.app.Activity; +import android.content.ActivityNotFoundException; +import android.content.ComponentName; +import android.content.Intent; +import android.content.IntentSender; +import android.content.pm.ActivityInfo; +import android.content.pm.LauncherActivityInfo; +import android.content.pm.LauncherApps; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.os.Process; +import android.os.UserHandle; +import android.util.Log; +import android.widget.Toast; + +import com.android.launcher3.IconCache; +import com.android.launcher3.R; +import com.android.launcher3.Utilities; + +import java.lang.reflect.Method; + +/** + * Wrapper class for representing a shortcut configure activity. + */ +public abstract class ShortcutConfigActivityInfo { + + private static final String TAG = "SCActivityInfo"; + + private final ComponentName mCn; + private final UserHandle mUser; + + private ShortcutConfigActivityInfo(ComponentName cn, UserHandle user) { + mCn = cn; + mUser = user; + } + + public ComponentName getComponent() { + return mCn; + } + + public UserHandle getUser() { + return mUser; + } + + public abstract CharSequence getLabel(); + + public abstract Drawable getFullResIcon(IconCache cache); + + public boolean startConfigActivity(Activity activity, int requestCode) { + Intent intent = new Intent(Intent.ACTION_CREATE_SHORTCUT) + .setComponent(getComponent()); + try { + activity.startActivityForResult(intent, requestCode); + return true; + } catch (ActivityNotFoundException e) { + Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); + } catch (SecurityException e) { + Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); + Log.e(TAG, "Launcher does not have the permission to launch " + intent + + ". Make sure to create a MAIN intent-filter for the corresponding activity " + + "or use the exported attribute for this activity.", e); + } + return false; + } + + static class ShortcutConfigActivityInfoVL extends ShortcutConfigActivityInfo { + + private final ActivityInfo mInfo; + private final PackageManager mPm; + + + public ShortcutConfigActivityInfoVL(ActivityInfo info, PackageManager pm) { + super(new ComponentName(info.packageName, info.name), Process.myUserHandle()); + mInfo = info; + mPm = pm; + } + + @Override + public CharSequence getLabel() { + return mInfo.loadLabel(mPm); + } + + @Override + public Drawable getFullResIcon(IconCache cache) { + return cache.getFullResIcon(mInfo); + } + } + + @TargetApi(26) + static class ShortcutConfigActivityInfoVO extends ShortcutConfigActivityInfo { + + private final LauncherActivityInfo mInfo; + + public ShortcutConfigActivityInfoVO(LauncherActivityInfo info) { + super(info.getComponentName(), info.getUser()); + mInfo = info; + } + + @Override + public CharSequence getLabel() { + return mInfo.getLabel(); + } + + @Override + public Drawable getFullResIcon(IconCache cache) { + return cache.getFullResIcon(mInfo); + } + + @Override + public boolean startConfigActivity(Activity activity, int requestCode) { + if (getUser().equals(Process.myUserHandle())) { + return super.startConfigActivity(activity, requestCode); + } + 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); + return false; + } + } + } +} -- cgit v1.2.3