1 /*
2  * Copyright (C) 2020 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.os;
17 
18 import android.Manifest;
19 import android.annotation.NonNull;
20 import android.annotation.RequiresPermission;
21 import android.annotation.SystemApi;
22 import android.annotation.SystemService;
23 import android.content.Context;
24 import android.util.ArraySet;
25 import android.util.Log;
26 
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31 
32 
33 /**
34  * Allows apps outside the system process to access various bits of configuration defined in
35  * /etc/sysconfig and its counterparts on OEM and vendor partitions.
36  *
37  * TODO: Intended for access by system mainline modules only. Marking as SystemApi until the
38  * module-only API surface is available.
39  * @hide
40  */
41 @SystemApi
42 @SystemService(Context.SYSTEM_CONFIG_SERVICE)
43 public class SystemConfigManager {
44     private static final String TAG = SystemConfigManager.class.getSimpleName();
45 
46     private final ISystemConfig mInterface;
47 
48     /** @hide **/
SystemConfigManager()49     public SystemConfigManager() {
50         mInterface = ISystemConfig.Stub.asInterface(
51                 ServiceManager.getService(Context.SYSTEM_CONFIG_SERVICE));
52     }
53 
54     /**
55      * Returns a set of package names for carrier apps that are preinstalled on the device but
56      * should be disabled until the matching carrier's SIM is inserted into the device.
57      * @return A set of package names.
58      */
59     @RequiresPermission(Manifest.permission.READ_CARRIER_APP_INFO)
getDisabledUntilUsedPreinstalledCarrierApps()60     public @NonNull Set<String> getDisabledUntilUsedPreinstalledCarrierApps() {
61         try {
62             List<String> apps = mInterface.getDisabledUntilUsedPreinstalledCarrierApps();
63             return new ArraySet<>(apps);
64         } catch (RemoteException e) {
65             Log.e(TAG, "Caught remote exception");
66             return Collections.emptySet();
67         }
68     }
69 
70     /**
71      * Returns a map that describes helper apps associated with carrier apps that, like the apps
72      * returned by {@link #getDisabledUntilUsedPreinstalledCarrierApps()}, should be disabled until
73      * the correct SIM is inserted into the device.
74      * @return A map with keys corresponding to package names returned by
75      *         {@link #getDisabledUntilUsedPreinstalledCarrierApps()} and values as lists of package
76      *         names of helper apps.
77      */
78     @RequiresPermission(Manifest.permission.READ_CARRIER_APP_INFO)
79     public @NonNull Map<String, List<String>>
getDisabledUntilUsedPreinstalledCarrierAssociatedApps()80             getDisabledUntilUsedPreinstalledCarrierAssociatedApps() {
81         try {
82             return (Map<String, List<String>>)
83                     mInterface.getDisabledUntilUsedPreinstalledCarrierAssociatedApps();
84         } catch (RemoteException e) {
85             Log.e(TAG, "Caught remote exception");
86             return Collections.emptyMap();
87         }
88     }
89 
90     /**
91      * Returns a map that describes helper apps associated with carrier apps that, like the apps
92      * returned by {@link #getDisabledUntilUsedPreinstalledCarrierApps()}, should be disabled until
93      * the correct SIM is inserted into the device.
94      *
95      * <p>TODO(b/159069037) expose this and get rid of the other method that omits SDK version.
96      *
97      * @return A map with keys corresponding to package names returned by
98      *         {@link #getDisabledUntilUsedPreinstalledCarrierApps()} and values as lists of package
99      *         names of helper apps and the SDK versions when they were first added.
100      *
101      * @hide
102      */
103     @RequiresPermission(Manifest.permission.READ_CARRIER_APP_INFO)
104     public @NonNull Map<String, List<CarrierAssociatedAppEntry>>
getDisabledUntilUsedPreinstalledCarrierAssociatedAppEntries()105             getDisabledUntilUsedPreinstalledCarrierAssociatedAppEntries() {
106         try {
107             return (Map<String, List<CarrierAssociatedAppEntry>>)
108                     mInterface.getDisabledUntilUsedPreinstalledCarrierAssociatedAppEntries();
109         } catch (RemoteException e) {
110             Log.e(TAG, "Caught remote exception", e);
111             return Collections.emptyMap();
112         }
113     }
114 
115     /**
116      * Get uids which have been granted given permission in system configuration.
117      *
118      * The uids and assigning permissions are defined on data/etc/platform.xml
119      *
120      * @param permissionName The target permission.
121      * @return The uids have been granted given permission in system configuration.
122      */
123     @RequiresPermission(Manifest.permission.GET_RUNTIME_PERMISSIONS)
124     @NonNull
getSystemPermissionUids(@onNull String permissionName)125     public int[] getSystemPermissionUids(@NonNull String permissionName) {
126         try {
127             return mInterface.getSystemPermissionUids(permissionName);
128         } catch (RemoteException e) {
129             throw e.rethrowFromSystemServer();
130         }
131     }
132 }
133