Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 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.content.Context; |
| 19 | import android.content.pm.ApplicationInfo; |
| 20 | import android.content.pm.PackageInfo; |
| 21 | import android.content.pm.PackageManager.NameNotFoundException; |
| 22 | import android.content.pm.UserInfo; |
Gustav Sennton | 564c2fd | 2017-01-30 18:08:01 +0000 | [diff] [blame] | 23 | import android.os.Build; |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 24 | import android.os.UserManager; |
| 25 | |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.List; |
| 28 | |
| 29 | /** |
| 30 | * Utility class for storing a (user,PackageInfo) mapping. |
| 31 | * @hide |
| 32 | */ |
| 33 | public class UserPackage { |
| 34 | private final UserInfo mUserInfo; |
| 35 | private final PackageInfo mPackageInfo; |
| 36 | |
| 37 | public UserPackage(UserInfo user, PackageInfo packageInfo) { |
| 38 | this.mUserInfo = user; |
| 39 | this.mPackageInfo = packageInfo; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Returns a list of (User,PackageInfo) pairs corresponding to the PackageInfos for all |
| 44 | * device users for the package named {@param packageName}. |
| 45 | */ |
| 46 | public static List<UserPackage> getPackageInfosAllUsers(Context context, |
| 47 | String packageName, int packageFlags) { |
| 48 | List<UserInfo> users = getAllUsers(context); |
| 49 | List<UserPackage> userPackages = new ArrayList<UserPackage>(users.size()); |
| 50 | for (UserInfo user : users) { |
| 51 | PackageInfo packageInfo = null; |
| 52 | try { |
| 53 | packageInfo = context.getPackageManager().getPackageInfoAsUser( |
| 54 | packageName, packageFlags, user.id); |
| 55 | } catch (NameNotFoundException e) { |
| 56 | } |
| 57 | userPackages.add(new UserPackage(user, packageInfo)); |
| 58 | } |
| 59 | return userPackages; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns whether the given package is enabled. |
| 64 | * This state can be changed by the user from Settings->Apps |
| 65 | */ |
| 66 | public boolean isEnabledPackage() { |
| 67 | if (mPackageInfo == null) return false; |
| 68 | return mPackageInfo.applicationInfo.enabled; |
| 69 | } |
| 70 | |
| 71 | /** |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 72 | * Return {@code true} if the package is installed and not hidden |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 73 | */ |
| 74 | public boolean isInstalledPackage() { |
| 75 | if (mPackageInfo == null) return false; |
| 76 | return (((mPackageInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0) |
| 77 | && ((mPackageInfo.applicationInfo.privateFlags |
| 78 | & ApplicationInfo.PRIVATE_FLAG_HIDDEN) == 0)); |
| 79 | } |
| 80 | |
Gustav Sennton | 564c2fd | 2017-01-30 18:08:01 +0000 | [diff] [blame] | 81 | /** |
| 82 | * Returns whether the package represented by {@param packageInfo} targets a sdk version |
| 83 | * supported by the current framework version. |
| 84 | */ |
| 85 | public static boolean hasCorrectTargetSdkVersion(PackageInfo packageInfo) { |
Nate Fischer | 220d86d | 2017-08-28 12:15:56 -0700 | [diff] [blame] | 86 | return packageInfo.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.O_MR1; |
Gustav Sennton | 564c2fd | 2017-01-30 18:08:01 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 89 | public UserInfo getUserInfo() { |
| 90 | return mUserInfo; |
| 91 | } |
| 92 | |
| 93 | public PackageInfo getPackageInfo() { |
| 94 | return mPackageInfo; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | private static List<UserInfo> getAllUsers(Context context) { |
| 99 | UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 100 | return userManager.getUsers(false); |
| 101 | } |
| 102 | |
| 103 | } |