1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.launcher3.compat; 18 19 import android.annotation.TargetApi; 20 import android.content.Context; 21 import android.content.pm.ApplicationInfo; 22 import android.content.pm.LauncherActivityInfo; 23 import android.content.pm.LauncherApps; 24 import android.content.pm.PackageManager; 25 import android.os.Build; 26 import android.os.Process; 27 import android.os.UserHandle; 28 import android.support.annotation.Nullable; 29 import android.util.Log; 30 31 import com.android.launcher3.compat.ShortcutConfigActivityInfo.ShortcutConfigActivityInfoVO; 32 import com.android.launcher3.util.PackageUserKey; 33 34 import java.lang.reflect.Method; 35 import java.util.ArrayList; 36 import java.util.List; 37 38 @TargetApi(26) 39 public class LauncherAppsCompatVO extends LauncherAppsCompatVL { 40 LauncherAppsCompatVO(Context context)41 LauncherAppsCompatVO(Context context) { 42 super(context); 43 } 44 45 @Override getApplicationInfo(String packageName, int flags, UserHandle user)46 public ApplicationInfo getApplicationInfo(String packageName, int flags, UserHandle user) { 47 try { 48 // TODO: Temporary workaround until the API signature is updated 49 if (false) { 50 throw new PackageManager.NameNotFoundException(); 51 } 52 53 ApplicationInfo info = mLauncherApps.getApplicationInfo(packageName, flags, user); 54 return (info.flags & ApplicationInfo.FLAG_INSTALLED) == 0 || !info.enabled 55 ? null : info; 56 } catch (PackageManager.NameNotFoundException e) { 57 return null; 58 } 59 } 60 61 @Override getCustomShortcutActivityList( @ullable PackageUserKey packageUser)62 public List<ShortcutConfigActivityInfo> getCustomShortcutActivityList( 63 @Nullable PackageUserKey packageUser) { 64 List<ShortcutConfigActivityInfo> result = new ArrayList<>(); 65 UserHandle myUser = Process.myUserHandle(); 66 67 try { 68 Method m = LauncherApps.class.getDeclaredMethod("getShortcutConfigActivityList", 69 String.class, UserHandle.class); 70 final List<UserHandle> users; 71 final String packageName; 72 if (packageUser == null) { 73 users = UserManagerCompat.getInstance(mContext).getUserProfiles(); 74 packageName = null; 75 } else { 76 users = new ArrayList<>(1); 77 users.add(packageUser.mUser); 78 packageName = packageUser.mPackageName; 79 } 80 for (UserHandle user : users) { 81 boolean ignoreTargetSdk = myUser.equals(user); 82 List<LauncherActivityInfo> activities = 83 (List<LauncherActivityInfo>) m.invoke(mLauncherApps, packageName, user); 84 for (LauncherActivityInfo activityInfo : activities) { 85 if (ignoreTargetSdk || activityInfo.getApplicationInfo().targetSdkVersion >= 86 Build.VERSION_CODES.O) { 87 result.add(new ShortcutConfigActivityInfoVO(activityInfo)); 88 } 89 } 90 } 91 } catch (Exception e) { 92 Log.e("LauncherAppsCompatVO", "Error calling new API", e); 93 } 94 95 return result; 96 } 97 } 98