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