1 /*
2  * Copyright (C) 2018 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.internal.os;
18 
19 
20 import android.app.AppGlobals;
21 import android.content.pm.PackageInfo;
22 import android.content.pm.PackageManager;
23 import android.os.RemoteException;
24 import android.os.UserHandle;
25 
26 import com.android.internal.annotations.VisibleForTesting;
27 
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 
32 /** Maps AppIds to their package names. */
33 public final class AppIdToPackageMap {
34     private final Map<Integer, String> mAppIdToPackageMap;
35 
36     @VisibleForTesting
AppIdToPackageMap(Map<Integer, String> appIdToPackageMap)37     public AppIdToPackageMap(Map<Integer, String> appIdToPackageMap) {
38         mAppIdToPackageMap = appIdToPackageMap;
39     }
40 
41     /** Creates a new {@link AppIdToPackageMap} for currently installed packages. */
getSnapshot()42     public static AppIdToPackageMap getSnapshot() {
43         List<PackageInfo> packages;
44         try {
45             packages = AppGlobals.getPackageManager()
46                     .getInstalledPackages(PackageManager.MATCH_UNINSTALLED_PACKAGES
47                                     | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
48                                     | PackageManager.MATCH_DIRECT_BOOT_AWARE,
49                             UserHandle.USER_SYSTEM).getList();
50         } catch (RemoteException e) {
51             throw e.rethrowFromSystemServer();
52         }
53         final Map<Integer, String> map = new HashMap<>();
54         for (PackageInfo pkg : packages) {
55             final int uid = pkg.applicationInfo.uid;
56             if (pkg.sharedUserId != null && map.containsKey(uid)) {
57                 // Use sharedUserId string as package name if there are collisions
58                 map.put(uid, "shared:" + pkg.sharedUserId);
59             } else {
60                 map.put(uid, pkg.packageName);
61             }
62         }
63         return new AppIdToPackageMap(map);
64     }
65 
66     /** Maps the AppId to a package name. */
mapAppId(int appId)67     public String mapAppId(int appId) {
68         String pkgName = mAppIdToPackageMap.get(appId);
69         return pkgName == null ? String.valueOf(appId) : pkgName;
70     }
71 
72     /** Maps the UID to a package name. */
mapUid(int uid)73     public String mapUid(int uid) {
74         final int appId = UserHandle.getAppId(uid);
75         final String pkgName = mAppIdToPackageMap.get(appId);
76         final String uidStr = UserHandle.formatUid(uid);
77         return pkgName == null ? uidStr : pkgName + '/' + uidStr;
78     }
79 }
80