blob: 03ff0ca63282cd1870497517f1ede3af09f4d739 [file] [log] [blame]
Gustav Sennton364e1602016-12-14 09:10:50 +00001/*
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;
Gustav Sennton564c2fd2017-01-30 18:08:01 +000023import android.os.Build;
Gustav Sennton364e1602016-12-14 09:10:50 +000024import android.os.UserManager;
25
26import java.util.ArrayList;
27import java.util.List;
28
29/**
30 * Utility class for storing a (user,PackageInfo) mapping.
31 * @hide
32 */
33public class UserPackage {
34 private final UserInfo mUserInfo;
35 private final PackageInfo mPackageInfo;
36
Shimi Zhang295c5a12017-12-14 19:03:30 -080037 public static final int MINIMUM_SUPPORTED_SDK = Build.VERSION_CODES.P;
Gustav Senntone5468f72017-11-20 19:42:43 +000038
Gustav Sennton364e1602016-12-14 09:10:50 +000039 public UserPackage(UserInfo user, PackageInfo packageInfo) {
40 this.mUserInfo = user;
41 this.mPackageInfo = packageInfo;
42 }
43
44 /**
45 * Returns a list of (User,PackageInfo) pairs corresponding to the PackageInfos for all
46 * device users for the package named {@param packageName}.
47 */
48 public static List<UserPackage> getPackageInfosAllUsers(Context context,
49 String packageName, int packageFlags) {
50 List<UserInfo> users = getAllUsers(context);
51 List<UserPackage> userPackages = new ArrayList<UserPackage>(users.size());
52 for (UserInfo user : users) {
53 PackageInfo packageInfo = null;
54 try {
55 packageInfo = context.getPackageManager().getPackageInfoAsUser(
56 packageName, packageFlags, user.id);
57 } catch (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 */
68 public boolean isEnabledPackage() {
69 if (mPackageInfo == null) return false;
70 return mPackageInfo.applicationInfo.enabled;
71 }
72
73 /**
Nate Fischer0a6140d2017-09-05 12:37:49 -070074 * Return {@code true} if the package is installed and not hidden
Gustav Sennton364e1602016-12-14 09:10:50 +000075 */
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
Gustav Sennton564c2fd2017-01-30 18:08:01 +000083 /**
84 * Returns whether the package represented by {@param packageInfo} targets a sdk version
85 * supported by the current framework version.
86 */
87 public static boolean hasCorrectTargetSdkVersion(PackageInfo packageInfo) {
Gustav Senntone5468f72017-11-20 19:42:43 +000088 return packageInfo.applicationInfo.targetSdkVersion >= MINIMUM_SUPPORTED_SDK;
Gustav Sennton564c2fd2017-01-30 18:08:01 +000089 }
90
Gustav Sennton364e1602016-12-14 09:10:50 +000091 public UserInfo getUserInfo() {
92 return mUserInfo;
93 }
94
95 public PackageInfo getPackageInfo() {
96 return mPackageInfo;
97 }
98
99
100 private static List<UserInfo> getAllUsers(Context context) {
101 UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
102 return userManager.getUsers(false);
103 }
104
105}