• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.server;
17 
18 import static java.util.stream.Collectors.toList;
19 import static java.util.stream.Collectors.toMap;
20 
21 import android.Manifest;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.pm.PackageManagerInternal;
25 import android.content.pm.SignedPackage;
26 import android.content.pm.SignedPackageParcel;
27 import android.os.Binder;
28 import android.os.ISystemConfig;
29 import android.util.ArrayMap;
30 import android.util.ArraySet;
31 import android.util.SparseArray;
32 
33 import com.android.internal.util.ArrayUtils;
34 
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Map;
38 
39 /**
40  * Service class that runs inside the system_server process to handle queries to
41  * {@link com.android.server.SystemConfig}.
42  * @hide
43  */
44 public class SystemConfigService extends SystemService {
45     private final Context mContext;
46 
47     private final ISystemConfig.Stub mInterface = new ISystemConfig.Stub() {
48         @Override
49         public List<String> getDisabledUntilUsedPreinstalledCarrierApps() {
50             mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_CARRIER_APP_INFO,
51                     "getDisabledUntilUsedPreInstalledCarrierApps requires READ_CARRIER_APP_INFO");
52             return new ArrayList<>(
53                     SystemConfig.getInstance().getDisabledUntilUsedPreinstalledCarrierApps());
54         }
55 
56         @Override
57         public Map getDisabledUntilUsedPreinstalledCarrierAssociatedApps() {
58             mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_CARRIER_APP_INFO,
59                     "getDisabledUntilUsedPreInstalledCarrierAssociatedApps requires"
60                             + " READ_CARRIER_APP_INFO");
61             return SystemConfig.getInstance()
62                     .getDisabledUntilUsedPreinstalledCarrierAssociatedApps().entrySet().stream()
63                     .collect(toMap(
64                             Map.Entry::getKey,
65                             e -> e.getValue().stream().map(app -> app.packageName)
66                                     .collect(toList())));
67         }
68 
69         @Override
70         public Map getDisabledUntilUsedPreinstalledCarrierAssociatedAppEntries() {
71             mContext.enforceCallingOrSelfPermission(Manifest.permission.READ_CARRIER_APP_INFO,
72                     "getDisabledUntilUsedPreInstalledCarrierAssociatedAppEntries requires"
73                             + " READ_CARRIER_APP_INFO");
74             return SystemConfig.getInstance()
75                     .getDisabledUntilUsedPreinstalledCarrierAssociatedApps();
76         }
77 
78         @Override
79         public int[] getSystemPermissionUids(String permissionName) {
80             mContext.enforceCallingOrSelfPermission(Manifest.permission.GET_RUNTIME_PERMISSIONS,
81                     "getSystemPermissionUids requires GET_RUNTIME_PERMISSIONS");
82             final List<Integer> uids = new ArrayList<>();
83             final SparseArray<ArraySet<String>> systemPermissions =
84                     SystemConfig.getInstance().getSystemPermissions();
85             for (int i = 0; i < systemPermissions.size(); i++) {
86                 final ArraySet<String> permissions = systemPermissions.valueAt(i);
87                 if (permissions != null && permissions.contains(permissionName)) {
88                     uids.add(systemPermissions.keyAt(i));
89                 }
90             }
91             return ArrayUtils.convertToIntArray(uids);
92         }
93 
94         @Override
95         public List<ComponentName> getEnabledComponentOverrides(String packageName) {
96             ArrayMap<String, Boolean> systemComponents = SystemConfig.getInstance()
97                     .getComponentsEnabledStates(packageName);
98             List<ComponentName> enabledComponent = new ArrayList<>();
99             if (systemComponents != null) {
100                 for (Map.Entry<String, Boolean> entry : systemComponents.entrySet()) {
101                     if (Boolean.TRUE.equals(entry.getValue())) {
102                         enabledComponent.add(new ComponentName(packageName, entry.getKey()));
103                     }
104                 }
105             }
106             return enabledComponent;
107         }
108 
109         @Override
110         public List<ComponentName> getDefaultVrComponents() {
111             getContext().enforceCallingOrSelfPermission(Manifest.permission.QUERY_ALL_PACKAGES,
112                     "Caller must hold " + Manifest.permission.QUERY_ALL_PACKAGES);
113             return new ArrayList<>(SystemConfig.getInstance().getDefaultVrComponents());
114         }
115 
116         @Override
117         public List<String> getPreventUserDisablePackages() {
118             PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class);
119             return SystemConfig.getInstance().getPreventUserDisablePackages().stream()
120                     .filter(preventUserDisablePackage ->
121                             pmi.canQueryPackage(Binder.getCallingUid(), preventUserDisablePackage))
122                     .collect(toList());
123         }
124 
125         @Override
126         public List<SignedPackageParcel> getEnhancedConfirmationTrustedPackages() {
127             getContext().enforceCallingOrSelfPermission(
128                     Manifest.permission.MANAGE_ENHANCED_CONFIRMATION_STATES,
129                     "Caller must hold " + Manifest.permission.MANAGE_ENHANCED_CONFIRMATION_STATES);
130 
131             return SystemConfig.getInstance().getEnhancedConfirmationTrustedPackages().stream()
132                     .map(SignedPackage::getData).toList();
133         }
134 
135         @Override
136         public List<SignedPackageParcel> getEnhancedConfirmationTrustedInstallers() {
137             getContext().enforceCallingOrSelfPermission(
138                     Manifest.permission.MANAGE_ENHANCED_CONFIRMATION_STATES,
139                     "Caller must hold " + Manifest.permission.MANAGE_ENHANCED_CONFIRMATION_STATES);
140 
141             return SystemConfig.getInstance().getEnhancedConfirmationTrustedInstallers().stream()
142                     .map(SignedPackage::getData).toList();
143         }
144     };
145 
SystemConfigService(Context context)146     public SystemConfigService(Context context) {
147         super(context);
148         mContext = context;
149     }
150 
151     @Override
onStart()152     public void onStart() {
153         publishBinderService(Context.SYSTEM_CONFIG_SERVICE, mInterface);
154     }
155 }
156