blob: a2ab0c15b1502a8e23311192f9db05b70951c544 [file] [log] [blame]
Philip P. Moltmann039678e2018-09-18 13:04:38 -07001/*
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
17package android.permission;
18
Svet Ganovd8eb8b22019-04-05 18:52:08 -070019import android.Manifest;
20import android.annotation.IntRange;
Philip P. Moltmann039678e2018-09-18 13:04:38 -070021import android.annotation.NonNull;
Aurimas Liutikas00be9512019-08-28 13:01:05 -070022import android.annotation.Nullable;
Svet Ganovd8eb8b22019-04-05 18:52:08 -070023import android.annotation.RequiresPermission;
Philip P. Moltmann039678e2018-09-18 13:04:38 -070024import android.annotation.SystemApi;
25import android.annotation.SystemService;
Svet Ganovd8eb8b22019-04-05 18:52:08 -070026import android.annotation.TestApi;
Philip P. Moltmann039678e2018-09-18 13:04:38 -070027import android.content.Context;
Svet Ganovd8eb8b22019-04-05 18:52:08 -070028import android.content.pm.IPackageManager;
Svet Ganovd8eb8b22019-04-05 18:52:08 -070029import android.os.RemoteException;
Philip P. Moltmann039678e2018-09-18 13:04:38 -070030
31import com.android.internal.annotations.Immutable;
Zimuzocc2932f2018-10-29 16:04:41 +000032import com.android.server.SystemConfig;
Philip P. Moltmann039678e2018-09-18 13:04:38 -070033
Zimuzocc2932f2018-10-29 16:04:41 +000034import java.util.ArrayList;
Philip P. Moltmann039678e2018-09-18 13:04:38 -070035import java.util.List;
Philip P. Moltmanna3ba4d92018-10-08 11:50:07 -070036import java.util.Objects;
Philip P. Moltmann039678e2018-09-18 13:04:38 -070037
38/**
39 * System level service for accessing the permission capabilities of the platform.
40 *
41 * @hide
42 */
Winsonf27394e2019-06-07 14:44:40 -070043@TestApi
Philip P. Moltmann039678e2018-09-18 13:04:38 -070044@SystemApi
45@SystemService(Context.PERMISSION_SERVICE)
46public final class PermissionManager {
47 /**
48 * {@link android.content.pm.PackageParser} needs access without having a {@link Context}.
49 *
50 * @hide
51 */
Zimuzocc2932f2018-10-29 16:04:41 +000052 public static final ArrayList<SplitPermissionInfo> SPLIT_PERMISSIONS =
53 SystemConfig.getInstance().getSplitPermissions();
Philip P. Moltmann039678e2018-09-18 13:04:38 -070054
55 private final @NonNull Context mContext;
56
Svet Ganovd8eb8b22019-04-05 18:52:08 -070057 private final IPackageManager mPackageManager;
58
Philip P. Moltmann039678e2018-09-18 13:04:38 -070059 /**
60 * Creates a new instance.
61 *
62 * @param context The current context in which to operate.
63 * @hide
64 */
Svet Ganovd8eb8b22019-04-05 18:52:08 -070065 public PermissionManager(@NonNull Context context, IPackageManager packageManager) {
Philip P. Moltmann039678e2018-09-18 13:04:38 -070066 mContext = context;
Svet Ganovd8eb8b22019-04-05 18:52:08 -070067 mPackageManager = packageManager;
68 }
69
70 /**
71 * Gets the version of the runtime permission database.
72 *
Philip P. Moltmann1ae81a52019-05-21 15:31:36 -070073 * @return The database version, -1 when this is an upgrade from pre-Q, 0 when this is a fresh
74 * install.
Svet Ganovd8eb8b22019-04-05 18:52:08 -070075 *
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. Moltmann039678e2018-09-18 13:04:38 -0700105 }
106
107 /**
Philip P. Moltmanna3ba4d92018-10-08 11:50:07 -0700108 * Get set of permissions that have been split into more granular or dependent permissions.
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700109 *
Howard Chenfff50fe2019-04-29 14:46:32 +0800110 * <p>E.g. before {@link android.os.Build.VERSION_CODES#Q} an app that was granted
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700111 * {@link Manifest.permission#ACCESS_COARSE_LOCATION} could access he location while it was in
Howard Chenfff50fe2019-04-29 14:46:32 +0800112 * foreground and background. On platforms after {@link android.os.Build.VERSION_CODES#Q}
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700113 * the location permission only grants location access while the app is in foreground. This
Howard Chenfff50fe2019-04-29 14:46:32 +0800114 * would break apps that target before {@link android.os.Build.VERSION_CODES#Q}. Hence whenever
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700115 * such an old app asks for a location permission (i.e. the
Philip P. Moltmanna3ba4d92018-10-08 11:50:07 -0700116 * {@link SplitPermissionInfo#getSplitPermission()}), then the
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700117 * {@link Manifest.permission#ACCESS_BACKGROUND_LOCATION} permission (inside
Philip P. Moltmanne1b277a2018-11-01 16:22:50 -0700118 * {@link SplitPermissionInfo#getNewPermissions}) is added.
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700119 *
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. Moltmanne1b277a2018-11-01 16:22:50 -0700125 public @NonNull List<SplitPermissionInfo> getSplitPermissions() {
Philip P. Moltmanna3ba4d92018-10-08 11:50:07 -0700126 return SPLIT_PERMISSIONS;
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700127 }
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. Moltmanna3ba4d92018-10-08 11:50:07 -0700135 private final @NonNull String mSplitPerm;
136 private final @NonNull List<String> mNewPerms;
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700137 private final int mTargetSdk;
138
Philip P. Moltmanna3ba4d92018-10-08 11:50:07 -0700139 @Override
Aurimas Liutikas00be9512019-08-28 13:01:05 -0700140 public boolean equals(@Nullable Object o) {
Philip P. Moltmanna3ba4d92018-10-08 11:50:07 -0700141 if (this == o) return true;
142 if (o == null || getClass() != o.getClass()) return false;
143 SplitPermissionInfo that = (SplitPermissionInfo) o;
144 return mTargetSdk == that.mTargetSdk
Winsonf27394e2019-06-07 14:44:40 -0700145 && mSplitPerm.equals(that.mSplitPerm)
146 && mNewPerms.equals(that.mNewPerms);
Philip P. Moltmanna3ba4d92018-10-08 11:50:07 -0700147 }
148
149 @Override
150 public int hashCode() {
Winsonf27394e2019-06-07 14:44:40 -0700151 return Objects.hash(mSplitPerm, mNewPerms, mTargetSdk);
Philip P. Moltmanna3ba4d92018-10-08 11:50:07 -0700152 }
153
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700154 /**
155 * Get the permission that is split.
156 */
Philip P. Moltmanna3ba4d92018-10-08 11:50:07 -0700157 public @NonNull String getSplitPermission() {
158 return mSplitPerm;
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700159 }
160
161 /**
162 * Get the permissions that are added.
163 */
Philip P. Moltmanna3ba4d92018-10-08 11:50:07 -0700164 public @NonNull List<String> getNewPermissions() {
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700165 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
Zimuzocc2932f2018-10-29 16:04:41 +0000175 /**
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. Moltmann039678e2018-09-18 13:04:38 -0700185 int targetSdk) {
Zimuzocc2932f2018-10-29 16:04:41 +0000186 mSplitPerm = splitPerm;
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700187 mNewPerms = newPerms;
188 mTargetSdk = targetSdk;
189 }
190 }
191}