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