blob: 10e8c8d5b8dd19eeba6ae44dc97b2d612a937661 [file] [log] [blame]
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -08001/*
2 * Copyright (C) 2016 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
17package android.permission;
18
Philip P. Moltmann78689522018-12-17 20:45:40 -080019import static com.android.internal.util.Preconditions.checkArgument;
Joel Galenson5f63b832018-12-19 15:38:04 -080020import static com.android.internal.util.Preconditions.checkArgumentNonnegative;
Philip P. Moltmann08cac8e2018-12-04 15:09:59 -080021import static com.android.internal.util.Preconditions.checkCollectionElementsNotNull;
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080022import static com.android.internal.util.Preconditions.checkNotNull;
23import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage;
24
Philip P. Moltmannbc054d82018-12-21 09:41:58 -080025import android.Manifest;
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080026import android.annotation.NonNull;
27import android.annotation.SystemApi;
28import android.app.Service;
29import android.content.Context;
30import android.content.Intent;
Philip P. Moltmann78689522018-12-17 20:45:40 -080031import android.content.pm.PackageInfo;
32import android.content.pm.PackageManager;
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080033import android.os.Bundle;
34import android.os.Handler;
35import android.os.IBinder;
Philip P. Moltmann97142e22019-01-10 17:03:42 -080036import android.os.ParcelFileDescriptor;
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080037import android.os.RemoteCallback;
Philip P. Moltmann97142e22019-01-10 17:03:42 -080038import android.os.UserHandle;
Philip P. Moltmann78689522018-12-17 20:45:40 -080039import android.util.ArrayMap;
Philip P. Moltmann97142e22019-01-10 17:03:42 -080040import android.util.Log;
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080041
Philip P. Moltmann78689522018-12-17 20:45:40 -080042import com.android.internal.util.Preconditions;
43
Philip P. Moltmann97142e22019-01-10 17:03:42 -080044import java.io.IOException;
45import java.io.OutputStream;
Philip P. Moltmann78689522018-12-17 20:45:40 -080046import java.util.ArrayList;
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080047import java.util.List;
Philip P. Moltmann78689522018-12-17 20:45:40 -080048import java.util.Map;
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080049
50/**
Philip P. Moltmannbc054d82018-12-21 09:41:58 -080051 * This service is meant to be implemented by the app controlling permissions.
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080052 *
Philip P. Moltmannbc054d82018-12-21 09:41:58 -080053 * @see PermissionController
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080054 *
55 * @hide
56 */
57@SystemApi
Philip P. Moltmannbc054d82018-12-21 09:41:58 -080058public abstract class PermissionControllerService extends Service {
Philip P. Moltmann97142e22019-01-10 17:03:42 -080059 private static final String LOG_TAG = PermissionControllerService.class.getSimpleName();
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080060
61 /**
62 * The {@link Intent} action that must be declared as handled by a service
63 * in its manifest for the system to recognize it as a runtime permission
64 * presenter service.
65 */
Philip P. Moltmannbc054d82018-12-21 09:41:58 -080066 public static final String SERVICE_INTERFACE = "android.permission.PermissionControllerService";
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -080067
68 // No need for locking - always set first and never modified
69 private Handler mHandler;
70
71 @Override
72 public final void attachBaseContext(Context base) {
73 super.attachBaseContext(base);
74 mHandler = new Handler(base.getMainLooper());
75 }
76
77 /**
Philip P. Moltmann78689522018-12-17 20:45:40 -080078 * Revoke a set of runtime permissions for various apps.
79 *
80 * @param requests The permissions to revoke as {@code Map<packageName, List<permission>>}
81 * @param doDryRun Compute the permissions that would be revoked, but not actually revoke them
82 * @param reason Why the permission should be revoked
83 * @param callerPackageName The package name of the calling app
84 *
85 * @return the actually removed permissions as {@code Map<packageName, List<permission>>}
86 */
87 public abstract @NonNull Map<String, List<String>> onRevokeRuntimePermissions(
88 @NonNull Map<String, List<String>> requests, boolean doDryRun,
89 @PermissionControllerManager.Reason int reason, @NonNull String callerPackageName);
90
91 /**
Philip P. Moltmann97142e22019-01-10 17:03:42 -080092 * Create a backup of the runtime permissions.
93 *
94 * @param user The user to back up
95 * @param out The stream to write the backup to
96 */
97 public abstract void onGetRuntimePermissionsBackup(@NonNull UserHandle user,
98 @NonNull OutputStream out);
99
100 /**
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -0800101 * Gets the runtime permissions for an app.
102 *
103 * @param packageName The package for which to query.
104 *
105 * @return descriptions of the runtime permissions of the app
106 */
107 public abstract @NonNull List<RuntimePermissionPresentationInfo> onGetAppPermissions(
108 @NonNull String packageName);
109
110 /**
111 * Revokes the permission {@code permissionName} for app {@code packageName}
112 *
113 * @param packageName The package for which to revoke
114 * @param permissionName The permission to revoke
115 */
116 public abstract void onRevokeRuntimePermission(@NonNull String packageName,
117 @NonNull String permissionName);
118
Philip P. Moltmann08cac8e2018-12-04 15:09:59 -0800119 /**
120 * Count how many apps have one of a set of permissions.
121 *
122 * @param permissionNames The permissions the app might have
123 * @param countOnlyGranted Count an app only if the permission is granted to the app
124 * @param countSystem Also count system apps
125 *
126 * @return the number of apps that have one of the permissions
127 */
128 public abstract int onCountPermissionApps(@NonNull List<String> permissionNames,
129 boolean countOnlyGranted, boolean countSystem);
130
Joel Galenson5f63b832018-12-19 15:38:04 -0800131 /**
132 * Count how many apps have used permissions.
133 *
134 * @param countSystem Also count system apps
135 * @param numMillis The number of milliseconds in the past to check for uses
136 *
137 * @return descriptions of the users of permissions
138 */
139 public abstract @NonNull List<RuntimePermissionUsageInfo>
140 onPermissionUsageResult(boolean countSystem, long numMillis);
141
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -0800142 @Override
143 public final IBinder onBind(Intent intent) {
Philip P. Moltmannbc054d82018-12-21 09:41:58 -0800144 return new IPermissionController.Stub() {
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -0800145 @Override
Philip P. Moltmann78689522018-12-17 20:45:40 -0800146 public void revokeRuntimePermissions(
147 Bundle bundleizedRequest, boolean doDryRun, int reason,
148 String callerPackageName, RemoteCallback callback) {
149 checkNotNull(bundleizedRequest, "bundleizedRequest");
150 checkNotNull(callerPackageName);
151 checkNotNull(callback);
152
153 Map<String, List<String>> request = new ArrayMap<>();
154 for (String packageName : bundleizedRequest.keySet()) {
155 Preconditions.checkNotNull(packageName);
156
157 ArrayList<String> permissions =
158 bundleizedRequest.getStringArrayList(packageName);
159 Preconditions.checkCollectionElementsNotNull(permissions, "permissions");
160
161 request.put(packageName, permissions);
162 }
163
164 enforceCallingPermission(Manifest.permission.REVOKE_RUNTIME_PERMISSIONS, null);
165
166 // Verify callerPackageName
167 try {
168 PackageInfo pkgInfo = getPackageManager().getPackageInfo(callerPackageName, 0);
169 checkArgument(getCallingUid() == pkgInfo.applicationInfo.uid);
170 } catch (PackageManager.NameNotFoundException e) {
171 throw new RuntimeException(e);
172 }
173
174 mHandler.sendMessage(obtainMessage(
175 PermissionControllerService::revokeRuntimePermissions,
176 PermissionControllerService.this, request, doDryRun, reason,
177 callerPackageName, callback));
178 }
179
180 @Override
Philip P. Moltmann97142e22019-01-10 17:03:42 -0800181 public void getRuntimePermissionBackup(UserHandle user, ParcelFileDescriptor pipe) {
182 checkNotNull(user);
183 checkNotNull(pipe);
184
185 enforceCallingPermission(Manifest.permission.GET_RUNTIME_PERMISSIONS, null);
186
187 mHandler.sendMessage(obtainMessage(
188 PermissionControllerService::getRuntimePermissionsBackup,
189 PermissionControllerService.this, user, pipe));
190 }
191
192 @Override
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -0800193 public void getAppPermissions(String packageName, RemoteCallback callback) {
194 checkNotNull(packageName, "packageName");
195 checkNotNull(callback, "callback");
196
Philip P. Moltmannbc054d82018-12-21 09:41:58 -0800197 enforceCallingPermission(Manifest.permission.GET_RUNTIME_PERMISSIONS, null);
198
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -0800199 mHandler.sendMessage(
Philip P. Moltmannbc054d82018-12-21 09:41:58 -0800200 obtainMessage(PermissionControllerService::getAppPermissions,
201 PermissionControllerService.this, packageName, callback));
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -0800202 }
203
204 @Override
205 public void revokeRuntimePermission(String packageName, String permissionName) {
206 checkNotNull(packageName, "packageName");
207 checkNotNull(permissionName, "permissionName");
208
Philip P. Moltmannbc054d82018-12-21 09:41:58 -0800209 enforceCallingPermission(Manifest.permission.REVOKE_RUNTIME_PERMISSIONS, null);
210
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -0800211 mHandler.sendMessage(
Philip P. Moltmannbc054d82018-12-21 09:41:58 -0800212 obtainMessage(PermissionControllerService::onRevokeRuntimePermission,
213 PermissionControllerService.this, packageName, permissionName));
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -0800214 }
Philip P. Moltmann08cac8e2018-12-04 15:09:59 -0800215
216 @Override
217 public void countPermissionApps(List<String> permissionNames, boolean countOnlyGranted,
218 boolean countSystem, RemoteCallback callback) {
219 checkCollectionElementsNotNull(permissionNames, "permissionNames");
220 checkNotNull(callback, "callback");
221
Philip P. Moltmannbc054d82018-12-21 09:41:58 -0800222 enforceCallingPermission(Manifest.permission.GET_RUNTIME_PERMISSIONS, null);
223
Philip P. Moltmann08cac8e2018-12-04 15:09:59 -0800224 mHandler.sendMessage(
Philip P. Moltmannbc054d82018-12-21 09:41:58 -0800225 obtainMessage(PermissionControllerService::countPermissionApps,
226 PermissionControllerService.this, permissionNames, countOnlyGranted,
227 countSystem, callback));
Philip P. Moltmann08cac8e2018-12-04 15:09:59 -0800228 }
Joel Galenson5f63b832018-12-19 15:38:04 -0800229
230 @Override
231 public void getPermissionUsages(boolean countSystem, long numMillis,
232 RemoteCallback callback) {
233 checkArgumentNonnegative(numMillis);
234 checkNotNull(callback, "callback");
235
236 enforceCallingPermission(Manifest.permission.GET_RUNTIME_PERMISSIONS, null);
237
238 mHandler.sendMessage(
239 obtainMessage(PermissionControllerService::getPermissionUsages,
240 PermissionControllerService.this, countSystem, numMillis,
241 callback));
242 }
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -0800243 };
244 }
245
Philip P. Moltmann78689522018-12-17 20:45:40 -0800246 private void revokeRuntimePermissions(@NonNull Map<String, List<String>> requests,
247 boolean doDryRun, @PermissionControllerManager.Reason int reason,
248 @NonNull String callerPackageName, @NonNull RemoteCallback callback) {
249 Map<String, List<String>> revoked = onRevokeRuntimePermissions(requests,
250 doDryRun, reason, callerPackageName);
251
252 checkNotNull(revoked);
253 Bundle bundledizedRevoked = new Bundle();
254 for (Map.Entry<String, List<String>> appRevocation : revoked.entrySet()) {
255 checkNotNull(appRevocation.getKey());
256 checkCollectionElementsNotNull(appRevocation.getValue(), "permissions");
257
258 bundledizedRevoked.putStringArrayList(appRevocation.getKey(),
259 new ArrayList<>(appRevocation.getValue()));
260 }
261
262 Bundle result = new Bundle();
263 result.putBundle(PermissionControllerManager.KEY_RESULT, bundledizedRevoked);
264 callback.sendResult(result);
265 }
266
Philip P. Moltmann97142e22019-01-10 17:03:42 -0800267 private void getRuntimePermissionsBackup(@NonNull UserHandle user,
268 @NonNull ParcelFileDescriptor outFile) {
269 try (OutputStream out = new ParcelFileDescriptor.AutoCloseOutputStream(outFile)) {
270 onGetRuntimePermissionsBackup(user, out);
271 } catch (IOException e) {
272 Log.e(LOG_TAG, "Could not open pipe to write backup tp", e);
273 }
274 }
275
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -0800276 private void getAppPermissions(@NonNull String packageName, @NonNull RemoteCallback callback) {
277 List<RuntimePermissionPresentationInfo> permissions = onGetAppPermissions(packageName);
278 if (permissions != null && !permissions.isEmpty()) {
279 Bundle result = new Bundle();
Philip P. Moltmannbc054d82018-12-21 09:41:58 -0800280 result.putParcelableList(PermissionControllerManager.KEY_RESULT, permissions);
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -0800281 callback.sendResult(result);
282 } else {
283 callback.sendResult(null);
284 }
285 }
Philip P. Moltmann08cac8e2018-12-04 15:09:59 -0800286
287 private void countPermissionApps(@NonNull List<String> permissionNames,
288 boolean countOnlyGranted, boolean countSystem, @NonNull RemoteCallback callback) {
289 int numApps = onCountPermissionApps(permissionNames, countOnlyGranted, countSystem);
290
291 Bundle result = new Bundle();
Philip P. Moltmannbc054d82018-12-21 09:41:58 -0800292 result.putInt(PermissionControllerManager.KEY_RESULT, numApps);
Philip P. Moltmann08cac8e2018-12-04 15:09:59 -0800293 callback.sendResult(result);
294 }
Joel Galenson5f63b832018-12-19 15:38:04 -0800295
296 private void getPermissionUsages(boolean countSystem, long numMillis,
297 @NonNull RemoteCallback callback) {
298 List<RuntimePermissionUsageInfo> users =
299 onPermissionUsageResult(countSystem, numMillis);
300 if (users != null && !users.isEmpty()) {
301 Bundle result = new Bundle();
302 result.putParcelableList(PermissionControllerManager.KEY_RESULT, users);
303 callback.sendResult(result);
304 } else {
305 callback.sendResult(null);
306 }
307 }
Philip P. Moltmanndbf78b82018-12-04 13:44:27 -0800308}