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