Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 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 android.permission; |
| 18 | |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 19 | import android.Manifest; |
| 20 | import android.annotation.IntRange; |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 22 | import android.annotation.RequiresPermission; |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 23 | import android.annotation.SystemApi; |
| 24 | import android.annotation.SystemService; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 25 | import android.annotation.TestApi; |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 27 | import android.content.pm.IPackageManager; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 28 | import android.os.RemoteException; |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 29 | |
| 30 | import com.android.internal.annotations.Immutable; |
Zimuzo | cc2932f | 2018-10-29 16:04:41 +0000 | [diff] [blame] | 31 | import com.android.server.SystemConfig; |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 32 | |
Zimuzo | cc2932f | 2018-10-29 16:04:41 +0000 | [diff] [blame] | 33 | import java.util.ArrayList; |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 34 | import java.util.List; |
Philip P. Moltmann | a3ba4d9 | 2018-10-08 11:50:07 -0700 | [diff] [blame] | 35 | import java.util.Objects; |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * System level service for accessing the permission capabilities of the platform. |
| 39 | * |
| 40 | * @hide |
| 41 | */ |
Winson | f27394e | 2019-06-07 14:44:40 -0700 | [diff] [blame] | 42 | @TestApi |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 43 | @SystemApi |
| 44 | @SystemService(Context.PERMISSION_SERVICE) |
| 45 | public final class PermissionManager { |
Todd Kennedy | 7e3dd3a | 2019-07-08 10:34:29 -0700 | [diff] [blame] | 46 | /** @hide */ |
Todd Kennedy | c971a45 | 2019-07-08 16:04:52 -0700 | [diff] [blame] | 47 | public static final String KILL_APP_REASON_PERMISSIONS_REVOKED = |
| 48 | "permissions revoked"; |
| 49 | /** @hide */ |
| 50 | public static final String KILL_APP_REASON_GIDS_CHANGED = |
| 51 | "permission grant or revoke changed gids"; |
| 52 | |
Todd Kennedy | 7e3dd3a | 2019-07-08 10:34:29 -0700 | [diff] [blame] | 53 | |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 54 | /** |
| 55 | * {@link android.content.pm.PackageParser} needs access without having a {@link Context}. |
| 56 | * |
| 57 | * @hide |
| 58 | */ |
Zimuzo | cc2932f | 2018-10-29 16:04:41 +0000 | [diff] [blame] | 59 | public static final ArrayList<SplitPermissionInfo> SPLIT_PERMISSIONS = |
| 60 | SystemConfig.getInstance().getSplitPermissions(); |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 61 | |
| 62 | private final @NonNull Context mContext; |
| 63 | |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 64 | private final IPackageManager mPackageManager; |
| 65 | |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Creates a new instance. |
| 68 | * |
| 69 | * @param context The current context in which to operate. |
| 70 | * @hide |
| 71 | */ |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 72 | public PermissionManager(@NonNull Context context, IPackageManager packageManager) { |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 73 | mContext = context; |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 74 | mPackageManager = packageManager; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Gets the version of the runtime permission database. |
| 79 | * |
Philip P. Moltmann | 1ae81a5 | 2019-05-21 15:31:36 -0700 | [diff] [blame] | 80 | * @return The database version, -1 when this is an upgrade from pre-Q, 0 when this is a fresh |
| 81 | * install. |
Svet Ganov | d8eb8b2 | 2019-04-05 18:52:08 -0700 | [diff] [blame] | 82 | * |
| 83 | * @hide |
| 84 | */ |
| 85 | @TestApi |
| 86 | @SystemApi |
| 87 | @RequiresPermission(Manifest.permission.ADJUST_RUNTIME_PERMISSIONS_POLICY) |
| 88 | public @IntRange(from = 0) int getRuntimePermissionsVersion() { |
| 89 | try { |
| 90 | return mPackageManager.getRuntimePermissionsVersion(mContext.getUserId()); |
| 91 | } catch (RemoteException e) { |
| 92 | throw e.rethrowFromSystemServer(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Sets the version of the runtime permission database. |
| 98 | * |
| 99 | * @param version The new version. |
| 100 | * |
| 101 | * @hide |
| 102 | */ |
| 103 | @TestApi |
| 104 | @SystemApi |
| 105 | @RequiresPermission(Manifest.permission.ADJUST_RUNTIME_PERMISSIONS_POLICY) |
| 106 | public void setRuntimePermissionsVersion(@IntRange(from = 0) int version) { |
| 107 | try { |
| 108 | mPackageManager.setRuntimePermissionsVersion(version, mContext.getUserId()); |
| 109 | } catch (RemoteException e) { |
| 110 | throw e.rethrowFromSystemServer(); |
| 111 | } |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
Philip P. Moltmann | a3ba4d9 | 2018-10-08 11:50:07 -0700 | [diff] [blame] | 115 | * Get set of permissions that have been split into more granular or dependent permissions. |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 116 | * |
Howard Chen | fff50fe | 2019-04-29 14:46:32 +0800 | [diff] [blame] | 117 | * <p>E.g. before {@link android.os.Build.VERSION_CODES#Q} an app that was granted |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 118 | * {@link Manifest.permission#ACCESS_COARSE_LOCATION} could access he location while it was in |
Howard Chen | fff50fe | 2019-04-29 14:46:32 +0800 | [diff] [blame] | 119 | * foreground and background. On platforms after {@link android.os.Build.VERSION_CODES#Q} |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 120 | * the location permission only grants location access while the app is in foreground. This |
Howard Chen | fff50fe | 2019-04-29 14:46:32 +0800 | [diff] [blame] | 121 | * would break apps that target before {@link android.os.Build.VERSION_CODES#Q}. Hence whenever |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 122 | * such an old app asks for a location permission (i.e. the |
Philip P. Moltmann | a3ba4d9 | 2018-10-08 11:50:07 -0700 | [diff] [blame] | 123 | * {@link SplitPermissionInfo#getSplitPermission()}), then the |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 124 | * {@link Manifest.permission#ACCESS_BACKGROUND_LOCATION} permission (inside |
Philip P. Moltmann | e1b277a | 2018-11-01 16:22:50 -0700 | [diff] [blame] | 125 | * {@link SplitPermissionInfo#getNewPermissions}) is added. |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 126 | * |
| 127 | * <p>Note: Regular apps do not have to worry about this. The platform and permission controller |
| 128 | * automatically add the new permissions where needed. |
| 129 | * |
| 130 | * @return All permissions that are split. |
| 131 | */ |
Philip P. Moltmann | e1b277a | 2018-11-01 16:22:50 -0700 | [diff] [blame] | 132 | public @NonNull List<SplitPermissionInfo> getSplitPermissions() { |
Philip P. Moltmann | a3ba4d9 | 2018-10-08 11:50:07 -0700 | [diff] [blame] | 133 | return SPLIT_PERMISSIONS; |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * A permission that was added in a previous API level might have split into several |
| 138 | * permissions. This object describes one such split. |
| 139 | */ |
| 140 | @Immutable |
| 141 | public static final class SplitPermissionInfo { |
Philip P. Moltmann | a3ba4d9 | 2018-10-08 11:50:07 -0700 | [diff] [blame] | 142 | private final @NonNull String mSplitPerm; |
| 143 | private final @NonNull List<String> mNewPerms; |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 144 | private final int mTargetSdk; |
| 145 | |
Philip P. Moltmann | a3ba4d9 | 2018-10-08 11:50:07 -0700 | [diff] [blame] | 146 | @Override |
| 147 | public boolean equals(Object o) { |
| 148 | if (this == o) return true; |
| 149 | if (o == null || getClass() != o.getClass()) return false; |
| 150 | SplitPermissionInfo that = (SplitPermissionInfo) o; |
| 151 | return mTargetSdk == that.mTargetSdk |
Winson | f27394e | 2019-06-07 14:44:40 -0700 | [diff] [blame] | 152 | && mSplitPerm.equals(that.mSplitPerm) |
| 153 | && mNewPerms.equals(that.mNewPerms); |
Philip P. Moltmann | a3ba4d9 | 2018-10-08 11:50:07 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public int hashCode() { |
Winson | f27394e | 2019-06-07 14:44:40 -0700 | [diff] [blame] | 158 | return Objects.hash(mSplitPerm, mNewPerms, mTargetSdk); |
Philip P. Moltmann | a3ba4d9 | 2018-10-08 11:50:07 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 161 | /** |
| 162 | * Get the permission that is split. |
| 163 | */ |
Philip P. Moltmann | a3ba4d9 | 2018-10-08 11:50:07 -0700 | [diff] [blame] | 164 | public @NonNull String getSplitPermission() { |
| 165 | return mSplitPerm; |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Get the permissions that are added. |
| 170 | */ |
Philip P. Moltmann | a3ba4d9 | 2018-10-08 11:50:07 -0700 | [diff] [blame] | 171 | public @NonNull List<String> getNewPermissions() { |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 172 | return mNewPerms; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get the target API level when the permission was split. |
| 177 | */ |
| 178 | public int getTargetSdk() { |
| 179 | return mTargetSdk; |
| 180 | } |
| 181 | |
Zimuzo | cc2932f | 2018-10-29 16:04:41 +0000 | [diff] [blame] | 182 | /** |
| 183 | * Constructs a split permission. |
| 184 | * |
| 185 | * @param splitPerm old permission that will be split |
| 186 | * @param newPerms list of new permissions that {@code rootPerm} will be split into |
| 187 | * @param targetSdk apps targetting SDK versions below this will have {@code rootPerm} |
| 188 | * split into {@code newPerms} |
| 189 | * @hide |
| 190 | */ |
| 191 | public SplitPermissionInfo(@NonNull String splitPerm, @NonNull List<String> newPerms, |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 192 | int targetSdk) { |
Zimuzo | cc2932f | 2018-10-29 16:04:41 +0000 | [diff] [blame] | 193 | mSplitPerm = splitPerm; |
Philip P. Moltmann | 039678e | 2018-09-18 13:04:38 -0700 | [diff] [blame] | 194 | mNewPerms = newPerms; |
| 195 | mTargetSdk = targetSdk; |
| 196 | } |
| 197 | } |
| 198 | } |