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 package android.webkit; 17 18 import android.annotation.NonNull; 19 import android.annotation.Nullable; 20 import android.content.Context; 21 import android.content.pm.ApplicationInfo; 22 import android.content.pm.PackageInfo; 23 import android.content.pm.PackageManager; 24 import android.os.UserHandle; 25 import android.os.UserManager; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 30 /** 31 * Utility class for storing a (user,PackageInfo) mapping. 32 * @hide 33 */ 34 public class UserPackage { 35 private final UserHandle mUser; 36 private final PackageInfo mPackageInfo; 37 UserPackage(@onNull UserHandle user, @Nullable PackageInfo packageInfo)38 public UserPackage(@NonNull UserHandle user, @Nullable PackageInfo packageInfo) { 39 mUser = user; 40 mPackageInfo = packageInfo; 41 } 42 43 /** 44 * Returns a list of (User,PackageInfo) pairs corresponding to the PackageInfos for all 45 * device users for the package named {@param packageName}. 46 */ getPackageInfosAllUsers(@onNull Context context, @NonNull String packageName, int packageFlags)47 public static @NonNull List<UserPackage> getPackageInfosAllUsers(@NonNull Context context, 48 @NonNull String packageName, int packageFlags) { 49 UserManager userManager = context.getSystemService(UserManager.class); 50 List<UserHandle> users = userManager.getUserHandles(false); 51 List<UserPackage> userPackages = new ArrayList<UserPackage>(users.size()); 52 for (UserHandle user : users) { 53 PackageManager pm = context.createContextAsUser(user, 0).getPackageManager(); 54 PackageInfo packageInfo = null; 55 try { 56 packageInfo = pm.getPackageInfo(packageName, packageFlags); 57 } catch (PackageManager.NameNotFoundException e) { 58 } 59 userPackages.add(new UserPackage(user, packageInfo)); 60 } 61 return userPackages; 62 } 63 64 /** 65 * Returns whether the given package is enabled. 66 * This state can be changed by the user from Settings->Apps 67 */ isEnabledPackage()68 public boolean isEnabledPackage() { 69 if (mPackageInfo == null) return false; 70 return mPackageInfo.applicationInfo.enabled; 71 } 72 73 /** 74 * Return {@code true} if the package is installed and not hidden 75 */ isInstalledPackage()76 public boolean isInstalledPackage() { 77 if (mPackageInfo == null) return false; 78 return (((mPackageInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0) 79 && ((mPackageInfo.applicationInfo.privateFlags 80 & ApplicationInfo.PRIVATE_FLAG_HIDDEN) == 0)); 81 } 82 getUser()83 public UserHandle getUser() { 84 return mUser; 85 } 86 getPackageInfo()87 public PackageInfo getPackageInfo() { 88 return mPackageInfo; 89 } 90 } 91