Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.permission; |
| 18 | |
Philip P. Moltmann | 7868952 | 2018-12-17 20:45:40 -0800 | [diff] [blame] | 19 | import static com.android.internal.util.Preconditions.checkArgument; |
Joel Galenson | 5f63b83 | 2018-12-19 15:38:04 -0800 | [diff] [blame] | 20 | import static com.android.internal.util.Preconditions.checkArgumentNonnegative; |
Philip P. Moltmann | 08cac8e | 2018-12-04 15:09:59 -0800 | [diff] [blame] | 21 | import static com.android.internal.util.Preconditions.checkCollectionElementsNotNull; |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 22 | import static com.android.internal.util.Preconditions.checkNotNull; |
| 23 | import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage; |
| 24 | |
Philip P. Moltmann | bc054d8 | 2018-12-21 09:41:58 -0800 | [diff] [blame] | 25 | import android.Manifest; |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 26 | import android.annotation.NonNull; |
| 27 | import android.annotation.SystemApi; |
| 28 | import android.app.Service; |
| 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
Philip P. Moltmann | 7868952 | 2018-12-17 20:45:40 -0800 | [diff] [blame] | 31 | import android.content.pm.PackageInfo; |
| 32 | import android.content.pm.PackageManager; |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 33 | import android.os.Bundle; |
| 34 | import android.os.Handler; |
| 35 | import android.os.IBinder; |
Philip P. Moltmann | 97142e2 | 2019-01-10 17:03:42 -0800 | [diff] [blame] | 36 | import android.os.ParcelFileDescriptor; |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 37 | import android.os.RemoteCallback; |
Philip P. Moltmann | 97142e2 | 2019-01-10 17:03:42 -0800 | [diff] [blame] | 38 | import android.os.UserHandle; |
Philip P. Moltmann | 7868952 | 2018-12-17 20:45:40 -0800 | [diff] [blame] | 39 | import android.util.ArrayMap; |
Philip P. Moltmann | 97142e2 | 2019-01-10 17:03:42 -0800 | [diff] [blame] | 40 | import android.util.Log; |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 41 | |
Philip P. Moltmann | 7868952 | 2018-12-17 20:45:40 -0800 | [diff] [blame] | 42 | import com.android.internal.util.Preconditions; |
| 43 | |
Philip P. Moltmann | 97142e2 | 2019-01-10 17:03:42 -0800 | [diff] [blame] | 44 | import java.io.IOException; |
| 45 | import java.io.OutputStream; |
Philip P. Moltmann | 7868952 | 2018-12-17 20:45:40 -0800 | [diff] [blame] | 46 | import java.util.ArrayList; |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 47 | import java.util.List; |
Philip P. Moltmann | 7868952 | 2018-12-17 20:45:40 -0800 | [diff] [blame] | 48 | import java.util.Map; |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
Philip P. Moltmann | bc054d8 | 2018-12-21 09:41:58 -0800 | [diff] [blame] | 51 | * This service is meant to be implemented by the app controlling permissions. |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 52 | * |
Philip P. Moltmann | bc054d8 | 2018-12-21 09:41:58 -0800 | [diff] [blame] | 53 | * @see PermissionController |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 54 | * |
| 55 | * @hide |
| 56 | */ |
| 57 | @SystemApi |
Philip P. Moltmann | bc054d8 | 2018-12-21 09:41:58 -0800 | [diff] [blame] | 58 | public abstract class PermissionControllerService extends Service { |
Philip P. Moltmann | 97142e2 | 2019-01-10 17:03:42 -0800 | [diff] [blame] | 59 | private static final String LOG_TAG = PermissionControllerService.class.getSimpleName(); |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 60 | |
| 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. Moltmann | bc054d8 | 2018-12-21 09:41:58 -0800 | [diff] [blame] | 66 | public static final String SERVICE_INTERFACE = "android.permission.PermissionControllerService"; |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 67 | |
| 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. Moltmann | 7868952 | 2018-12-17 20:45:40 -0800 | [diff] [blame] | 78 | * 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. Moltmann | 97142e2 | 2019-01-10 17:03:42 -0800 | [diff] [blame] | 92 | * 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. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 101 | * 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. Moltmann | 08cac8e | 2018-12-04 15:09:59 -0800 | [diff] [blame] | 119 | /** |
| 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 Galenson | 5f63b83 | 2018-12-19 15:38:04 -0800 | [diff] [blame] | 131 | /** |
| 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. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 142 | @Override |
| 143 | public final IBinder onBind(Intent intent) { |
Philip P. Moltmann | bc054d8 | 2018-12-21 09:41:58 -0800 | [diff] [blame] | 144 | return new IPermissionController.Stub() { |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 145 | @Override |
Philip P. Moltmann | 7868952 | 2018-12-17 20:45:40 -0800 | [diff] [blame] | 146 | 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. Moltmann | 97142e2 | 2019-01-10 17:03:42 -0800 | [diff] [blame] | 181 | 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. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 193 | public void getAppPermissions(String packageName, RemoteCallback callback) { |
| 194 | checkNotNull(packageName, "packageName"); |
| 195 | checkNotNull(callback, "callback"); |
| 196 | |
Philip P. Moltmann | bc054d8 | 2018-12-21 09:41:58 -0800 | [diff] [blame] | 197 | enforceCallingPermission(Manifest.permission.GET_RUNTIME_PERMISSIONS, null); |
| 198 | |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 199 | mHandler.sendMessage( |
Philip P. Moltmann | bc054d8 | 2018-12-21 09:41:58 -0800 | [diff] [blame] | 200 | obtainMessage(PermissionControllerService::getAppPermissions, |
| 201 | PermissionControllerService.this, packageName, callback)); |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public void revokeRuntimePermission(String packageName, String permissionName) { |
| 206 | checkNotNull(packageName, "packageName"); |
| 207 | checkNotNull(permissionName, "permissionName"); |
| 208 | |
Philip P. Moltmann | bc054d8 | 2018-12-21 09:41:58 -0800 | [diff] [blame] | 209 | enforceCallingPermission(Manifest.permission.REVOKE_RUNTIME_PERMISSIONS, null); |
| 210 | |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 211 | mHandler.sendMessage( |
Philip P. Moltmann | bc054d8 | 2018-12-21 09:41:58 -0800 | [diff] [blame] | 212 | obtainMessage(PermissionControllerService::onRevokeRuntimePermission, |
| 213 | PermissionControllerService.this, packageName, permissionName)); |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 214 | } |
Philip P. Moltmann | 08cac8e | 2018-12-04 15:09:59 -0800 | [diff] [blame] | 215 | |
| 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. Moltmann | bc054d8 | 2018-12-21 09:41:58 -0800 | [diff] [blame] | 222 | enforceCallingPermission(Manifest.permission.GET_RUNTIME_PERMISSIONS, null); |
| 223 | |
Philip P. Moltmann | 08cac8e | 2018-12-04 15:09:59 -0800 | [diff] [blame] | 224 | mHandler.sendMessage( |
Philip P. Moltmann | bc054d8 | 2018-12-21 09:41:58 -0800 | [diff] [blame] | 225 | obtainMessage(PermissionControllerService::countPermissionApps, |
| 226 | PermissionControllerService.this, permissionNames, countOnlyGranted, |
| 227 | countSystem, callback)); |
Philip P. Moltmann | 08cac8e | 2018-12-04 15:09:59 -0800 | [diff] [blame] | 228 | } |
Joel Galenson | 5f63b83 | 2018-12-19 15:38:04 -0800 | [diff] [blame] | 229 | |
| 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. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 243 | }; |
| 244 | } |
| 245 | |
Philip P. Moltmann | 7868952 | 2018-12-17 20:45:40 -0800 | [diff] [blame] | 246 | 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. Moltmann | 97142e2 | 2019-01-10 17:03:42 -0800 | [diff] [blame] | 267 | 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. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 276 | 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. Moltmann | bc054d8 | 2018-12-21 09:41:58 -0800 | [diff] [blame] | 280 | result.putParcelableList(PermissionControllerManager.KEY_RESULT, permissions); |
Philip P. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 281 | callback.sendResult(result); |
| 282 | } else { |
| 283 | callback.sendResult(null); |
| 284 | } |
| 285 | } |
Philip P. Moltmann | 08cac8e | 2018-12-04 15:09:59 -0800 | [diff] [blame] | 286 | |
| 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. Moltmann | bc054d8 | 2018-12-21 09:41:58 -0800 | [diff] [blame] | 292 | result.putInt(PermissionControllerManager.KEY_RESULT, numApps); |
Philip P. Moltmann | 08cac8e | 2018-12-04 15:09:59 -0800 | [diff] [blame] | 293 | callback.sendResult(result); |
| 294 | } |
Joel Galenson | 5f63b83 | 2018-12-19 15:38:04 -0800 | [diff] [blame] | 295 | |
| 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. Moltmann | dbf78b8 | 2018-12-04 13:44:27 -0800 | [diff] [blame] | 308 | } |