blob: 42816c064ab04a57d2846b9a839140d6c7fdc145 [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;
Svet Ganovd8eb8b22019-04-05 18:52:08 -070022import android.annotation.RequiresPermission;
Philip P. Moltmann039678e2018-09-18 13:04:38 -070023import android.annotation.SystemApi;
24import android.annotation.SystemService;
Svet Ganovd8eb8b22019-04-05 18:52:08 -070025import android.annotation.TestApi;
Philip P. Moltmann039678e2018-09-18 13:04:38 -070026import android.content.Context;
Svet Ganovd8eb8b22019-04-05 18:52:08 -070027import android.content.pm.IPackageManager;
Svet Ganovd8eb8b22019-04-05 18:52:08 -070028import android.os.RemoteException;
Philip P. Moltmann039678e2018-09-18 13:04:38 -070029
30import com.android.internal.annotations.Immutable;
Zimuzocc2932f2018-10-29 16:04:41 +000031import com.android.server.SystemConfig;
Philip P. Moltmann039678e2018-09-18 13:04:38 -070032
Zimuzocc2932f2018-10-29 16:04:41 +000033import java.util.ArrayList;
Philip P. Moltmann039678e2018-09-18 13:04:38 -070034import java.util.List;
Philip P. Moltmanna3ba4d92018-10-08 11:50:07 -070035import java.util.Objects;
Philip P. Moltmann039678e2018-09-18 13:04:38 -070036
37/**
38 * System level service for accessing the permission capabilities of the platform.
39 *
40 * @hide
41 */
Winsonf27394e2019-06-07 14:44:40 -070042@TestApi
Philip P. Moltmann039678e2018-09-18 13:04:38 -070043@SystemApi
44@SystemService(Context.PERMISSION_SERVICE)
45public final class PermissionManager {
Todd Kennedy7e3dd3a2019-07-08 10:34:29 -070046 /** @hide */
Todd Kennedyc971a452019-07-08 16:04:52 -070047 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 Kennedy7e3dd3a2019-07-08 10:34:29 -070053
Philip P. Moltmann039678e2018-09-18 13:04:38 -070054 /**
55 * {@link android.content.pm.PackageParser} needs access without having a {@link Context}.
56 *
57 * @hide
58 */
Zimuzocc2932f2018-10-29 16:04:41 +000059 public static final ArrayList<SplitPermissionInfo> SPLIT_PERMISSIONS =
60 SystemConfig.getInstance().getSplitPermissions();
Philip P. Moltmann039678e2018-09-18 13:04:38 -070061
62 private final @NonNull Context mContext;
63
Svet Ganovd8eb8b22019-04-05 18:52:08 -070064 private final IPackageManager mPackageManager;
65
Philip P. Moltmann039678e2018-09-18 13:04:38 -070066 /**
67 * Creates a new instance.
68 *
69 * @param context The current context in which to operate.
70 * @hide
71 */
Svet Ganovd8eb8b22019-04-05 18:52:08 -070072 public PermissionManager(@NonNull Context context, IPackageManager packageManager) {
Philip P. Moltmann039678e2018-09-18 13:04:38 -070073 mContext = context;
Svet Ganovd8eb8b22019-04-05 18:52:08 -070074 mPackageManager = packageManager;
75 }
76
77 /**
78 * Gets the version of the runtime permission database.
79 *
Philip P. Moltmann1ae81a52019-05-21 15:31:36 -070080 * @return The database version, -1 when this is an upgrade from pre-Q, 0 when this is a fresh
81 * install.
Svet Ganovd8eb8b22019-04-05 18:52:08 -070082 *
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. Moltmann039678e2018-09-18 13:04:38 -0700112 }
113
114 /**
Philip P. Moltmanna3ba4d92018-10-08 11:50:07 -0700115 * Get set of permissions that have been split into more granular or dependent permissions.
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700116 *
Howard Chenfff50fe2019-04-29 14:46:32 +0800117 * <p>E.g. before {@link android.os.Build.VERSION_CODES#Q} an app that was granted
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700118 * {@link Manifest.permission#ACCESS_COARSE_LOCATION} could access he location while it was in
Howard Chenfff50fe2019-04-29 14:46:32 +0800119 * foreground and background. On platforms after {@link android.os.Build.VERSION_CODES#Q}
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700120 * the location permission only grants location access while the app is in foreground. This
Howard Chenfff50fe2019-04-29 14:46:32 +0800121 * would break apps that target before {@link android.os.Build.VERSION_CODES#Q}. Hence whenever
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700122 * such an old app asks for a location permission (i.e. the
Philip P. Moltmanna3ba4d92018-10-08 11:50:07 -0700123 * {@link SplitPermissionInfo#getSplitPermission()}), then the
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700124 * {@link Manifest.permission#ACCESS_BACKGROUND_LOCATION} permission (inside
Philip P. Moltmanne1b277a2018-11-01 16:22:50 -0700125 * {@link SplitPermissionInfo#getNewPermissions}) is added.
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700126 *
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. Moltmanne1b277a2018-11-01 16:22:50 -0700132 public @NonNull List<SplitPermissionInfo> getSplitPermissions() {
Philip P. Moltmanna3ba4d92018-10-08 11:50:07 -0700133 return SPLIT_PERMISSIONS;
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700134 }
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. Moltmanna3ba4d92018-10-08 11:50:07 -0700142 private final @NonNull String mSplitPerm;
143 private final @NonNull List<String> mNewPerms;
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700144 private final int mTargetSdk;
145
Philip P. Moltmanna3ba4d92018-10-08 11:50:07 -0700146 @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
Winsonf27394e2019-06-07 14:44:40 -0700152 && mSplitPerm.equals(that.mSplitPerm)
153 && mNewPerms.equals(that.mNewPerms);
Philip P. Moltmanna3ba4d92018-10-08 11:50:07 -0700154 }
155
156 @Override
157 public int hashCode() {
Winsonf27394e2019-06-07 14:44:40 -0700158 return Objects.hash(mSplitPerm, mNewPerms, mTargetSdk);
Philip P. Moltmanna3ba4d92018-10-08 11:50:07 -0700159 }
160
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700161 /**
162 * Get the permission that is split.
163 */
Philip P. Moltmanna3ba4d92018-10-08 11:50:07 -0700164 public @NonNull String getSplitPermission() {
165 return mSplitPerm;
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700166 }
167
168 /**
169 * Get the permissions that are added.
170 */
Philip P. Moltmanna3ba4d92018-10-08 11:50:07 -0700171 public @NonNull List<String> getNewPermissions() {
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700172 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
Zimuzocc2932f2018-10-29 16:04:41 +0000182 /**
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. Moltmann039678e2018-09-18 13:04:38 -0700192 int targetSdk) {
Zimuzocc2932f2018-10-29 16:04:41 +0000193 mSplitPerm = splitPerm;
Philip P. Moltmann039678e2018-09-18 13:04:38 -0700194 mNewPerms = newPerms;
195 mTargetSdk = targetSdk;
196 }
197 }
198}