blob: b058dcd714dd68f72ac41513c713eab7cc72b78f [file] [log] [blame]
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001/*
2 * Copyright (C) 2012 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.app;
18
Svet Ganov16a16892015-04-16 10:32:04 -070019import android.Manifest;
Jeff Sharkey7095ab92019-08-20 16:50:28 -060020import android.annotation.CallbackExecutor;
Svet Ganovad0a49b2018-10-29 10:07:08 -070021import android.annotation.IntDef;
Svet Ganov00a46ef2019-03-29 21:13:03 -070022import android.annotation.IntRange;
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -080023import android.annotation.NonNull;
Hai Zhangb7776682018-09-25 15:10:57 -070024import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060025import android.annotation.RequiresPermission;
Jeff Davidson05542602014-08-11 14:07:27 -070026import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060027import android.annotation.SystemService;
Peter Visontay5a2a1ef2017-12-18 20:34:03 +000028import android.annotation.TestApi;
Jeff Davidson05542602014-08-11 14:07:27 -070029import android.app.usage.UsageStatsManager;
Philip P. Moltmann7641cd02020-03-11 14:42:43 -070030import android.compat.Compatibility;
Philip P. Moltmannb2bc6512020-01-24 15:34:34 -080031import android.compat.annotation.ChangeId;
32import android.compat.annotation.EnabledAfter;
Artur Satayevc895b1b2019-12-10 17:47:51 +000033import android.compat.annotation.UnsupportedAppUsage;
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -070034import android.content.ContentResolver;
Jeff Davidson05542602014-08-11 14:07:27 -070035import android.content.Context;
Ng Zhi An65a99b62018-10-01 11:57:53 -070036import android.content.pm.PackageManager;
Svet Ganovad0a49b2018-10-29 10:07:08 -070037import android.content.pm.ParceledListSlice;
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -070038import android.database.DatabaseUtils;
John Spurlock7b414672014-07-18 13:02:39 -040039import android.media.AudioAttributes.AttributeUsage;
Dianne Hackborne98f5db2013-07-17 17:23:25 -070040import android.os.Binder;
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -070041import android.os.Build;
42import android.os.Handler;
43import android.os.HandlerExecutor;
44import android.os.HandlerThread;
Dianne Hackborne98f5db2013-07-17 17:23:25 -070045import android.os.IBinder;
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -070046import android.os.Looper;
Dianne Hackborn35654b62013-01-14 17:38:02 -080047import android.os.Parcel;
48import android.os.Parcelable;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080049import android.os.Process;
Svet Ganov8455ba22019-01-02 13:05:56 -080050import android.os.RemoteCallback;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080051import android.os.RemoteException;
Stanislav Zholnin90516b92020-01-20 14:03:06 +000052import android.os.ServiceManager;
53import android.os.SystemClock;
Jeff Davidson05542602014-08-11 14:07:27 -070054import android.os.UserManager;
55import android.util.ArrayMap;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -080056import android.util.ArraySet;
Svet Ganovaf189e32019-02-15 18:45:29 -080057import android.util.LongSparseArray;
58import android.util.LongSparseLongArray;
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -080059import android.util.Pools;
Ng Zhi An65a99b62018-10-01 11:57:53 -070060import android.util.SparseArray;
Jeff Davidson05542602014-08-11 14:07:27 -070061
Svet Ganovb3d2ae22018-12-17 22:06:15 -080062import com.android.internal.annotations.GuardedBy;
Svet Ganov23c88db2019-01-22 20:38:11 -080063import com.android.internal.annotations.Immutable;
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -080064import com.android.internal.app.IAppOpsActiveCallback;
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -070065import com.android.internal.app.IAppOpsAsyncNotedCallback;
Jeff Davidson05542602014-08-11 14:07:27 -070066import com.android.internal.app.IAppOpsCallback;
Svet Ganovb3d2ae22018-12-17 22:06:15 -080067import com.android.internal.app.IAppOpsNotedCallback;
Jeff Davidson05542602014-08-11 14:07:27 -070068import com.android.internal.app.IAppOpsService;
Adam Bookatz182862e2020-04-27 21:58:22 -070069import com.android.internal.app.IAppOpsStartedCallback;
Stanislav Zholnin90516b92020-01-20 14:03:06 +000070import com.android.internal.app.MessageSamplingConfig;
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -070071import com.android.internal.os.RuntimeInit;
72import com.android.internal.os.ZygoteInit;
Svet Ganov8455ba22019-01-02 13:05:56 -080073import com.android.internal.util.ArrayUtils;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -080074import com.android.internal.util.DataClass;
Muhammad Qureshi317061a2020-03-02 13:15:51 -080075import com.android.internal.util.FrameworkStatsLog;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -080076import com.android.internal.util.Parcelling;
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -080077import com.android.internal.util.Preconditions;
Jeff Davidson05542602014-08-11 14:07:27 -070078
Svet Ganovaf189e32019-02-15 18:45:29 -080079import java.lang.annotation.ElementType;
Svet Ganovad0a49b2018-10-29 10:07:08 -070080import java.lang.annotation.Retention;
81import java.lang.annotation.RetentionPolicy;
Svet Ganovaf189e32019-02-15 18:45:29 -080082import java.lang.annotation.Target;
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -070083import java.lang.reflect.Method;
Svet Ganov8455ba22019-01-02 13:05:56 -080084import java.math.BigDecimal;
85import java.math.RoundingMode;
Jeff Davidson05542602014-08-11 14:07:27 -070086import java.util.ArrayList;
Peter Visontay5a2a1ef2017-12-18 20:34:03 +000087import java.util.Arrays;
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -070088import java.util.BitSet;
Svet Ganovad0a49b2018-10-29 10:07:08 -070089import java.util.Collections;
Jeff Davidson05542602014-08-11 14:07:27 -070090import java.util.HashMap;
91import java.util.List;
Philip P. Moltmann59076d82019-08-19 15:00:40 -070092import java.util.Map;
Svet Ganovaf189e32019-02-15 18:45:29 -080093import java.util.Objects;
Svet Ganov8455ba22019-01-02 13:05:56 -080094import java.util.concurrent.Executor;
95import java.util.function.Consumer;
Svet Ganovaf189e32019-02-15 18:45:29 -080096import java.util.function.Supplier;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080097
Dianne Hackbornd7d28e62013-02-12 14:59:53 -080098/**
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -080099 * App-ops are used for two purposes: Access control and tracking.
Dianne Hackbornd7d28e62013-02-12 14:59:53 -0800100 *
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -0800101 * <p>App-ops cover a wide variety of functionality from helping with runtime permissions access
102 * control and tracking to battery consumption tracking.
Philip P. Moltmann9e367002020-02-03 17:01:05 -0800103 *
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -0800104 * <h2>Access control</h2>
105 *
106 * <p>App-ops can either be controlled for each uid or for each package. Which one is used depends
107 * on the API provider maintaining this app-op. For any security or privacy related app-op the
108 * provider needs to control the app-op for per uid as all security and privacy is based on uid in
109 * Android.
110 *
111 * <p>To control access the app-op can be set to a mode to:
112 * <dl>
113 * <dt>{@link #MODE_DEFAULT}
114 * <dd>Default behavior, might differ from app-op or app-op
115 * <dt>{@link #MODE_ALLOWED}
116 * <dd>Allow the access
117 * <dt>{@link #MODE_IGNORED}
118 * <dd>Don't allow the access, i.e. don't perform the requested action or return no or dummy
119 * data
120 * <dt>{@link #MODE_ERRORED}
121 * <dd>Throw a {@link SecurityException} on access. This can be suppressed by using a
122 * {@code ...noThrow} method to check the mode
123 * </dl>
124 *
125 * <p>API providers need to check the mode returned by {@link #noteOp} if they are are allowing
126 * access to operations gated by the app-op. {@link #unsafeCheckOp} should be used to check the
127 * mode if no access is granted. E.g. this can be used for displaying app-op state in the UI or
128 * when checking the state before later calling {@link #noteOp} anyway.
129 *
130 * <p>If an operation refers to a time span (e.g. a audio-recording session) the API provider
131 * should use {@link #startOp} and {@link #finishOp} instead of {@link #noteOp}.
132 *
133 * <h3>Runtime permissions and app-ops</h3>
134 *
135 * <p>Each platform defined runtime permission (beside background modifiers) has an associated app
136 * op which is used for tracking but also to allow for silent failures. I.e. if the runtime
137 * permission is denied the caller gets a {@link SecurityException}, but if the permission is
138 * granted and the app-op is {@link #MODE_IGNORED} then the callers gets dummy behavior, e.g.
139 * location callbacks would not happen.
140 *
141 * <h3>App-op permissions</h3>
142 *
143 * <p>App-ops permissions are platform defined permissions that can be overridden. The security
144 * check for app-op permissions should by {@link #MODE_DEFAULT default} check the permission grant
145 * state. If the app-op state is set to {@link #MODE_ALLOWED} or {@link #MODE_IGNORED} the app-op
146 * state should be checked instead of the permission grant state.
147 *
148 * <p>This functionality allows to grant access by default to apps fulfilling the requirements for
149 * a certain permission level. Still the behavior can be overridden when needed.
150 *
151 * <h2>Tracking</h2>
152 *
153 * <p>App-ops track many important events, including all accesses to runtime permission protected
154 * APIs. This is done by tracking when an app-op was {@link #noteOp noted} or
155 * {@link #startOp started}. The tracked data can only be read by system components.
156 *
157 * <p><b>Only {@link #noteOp}/{@link #startOp} are tracked; {@link #unsafeCheckOp} is not tracked.
158 * Hence it is important to eventually call {@link #noteOp} or {@link #startOp} when providing
159 * access to protected operations or data.</b>
160 *
161 * <p>Some apps are forwarding access to other apps. E.g. an app might get the location from the
162 * system's location provider and then send the location further to a 3rd app. In this case the
163 * app passing on the data needs to call {@link #noteProxyOp} to signal the access proxying. This
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -0800164 * might also make sense inside of a single app if the access is forwarded between two parts of
165 * the tagged with different attribution tags.
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -0800166 *
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -0800167 * <p>An app can register an {@link OnOpNotedCallback} to get informed about what accesses the
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -0800168 * system is tracking for it. As each runtime permission has an associated app-op this API is
169 * particularly useful for an app that want to find unexpected private data accesses.
Dianne Hackbornd7d28e62013-02-12 14:59:53 -0800170 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600171@SystemService(Context.APP_OPS_SERVICE)
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800172public class AppOpsManager {
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700173 /**
Philip P. Moltmannb2bc6512020-01-24 15:34:34 -0800174 * This is a subtle behavior change to {@link #startWatchingMode}.
175 *
176 * Before this change the system called back for the switched op. After the change the system
177 * will call back for the actually requested op or all switched ops if no op is specified.
178 *
179 * @hide
180 */
181 @ChangeId
182 @EnabledAfter(targetSdkVersion = Build.VERSION_CODES.Q)
183 public static final long CALL_BACK_ON_CHANGED_LISTENER_WITH_SWITCHED_OP_CHANGE = 148180766L;
184
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800185 final Context mContext;
Svet Ganovb3d2ae22018-12-17 22:06:15 -0800186
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100187 @UnsupportedAppUsage
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800188 final IAppOpsService mService;
Svet Ganovb3d2ae22018-12-17 22:06:15 -0800189
Stanislav Zholnin90516b92020-01-20 14:03:06 +0000190 /**
191 * Service for the application context, to be used by static methods via
192 * {@link #getService()}
193 */
194 @GuardedBy("sLock")
195 static IAppOpsService sService;
196
Svet Ganovb3d2ae22018-12-17 22:06:15 -0800197 @GuardedBy("mModeWatchers")
198 private final ArrayMap<OnOpChangedListener, IAppOpsCallback> mModeWatchers =
199 new ArrayMap<>();
200
201 @GuardedBy("mActiveWatchers")
202 private final ArrayMap<OnOpActiveChangedListener, IAppOpsActiveCallback> mActiveWatchers =
203 new ArrayMap<>();
204
Adam Bookatz182862e2020-04-27 21:58:22 -0700205 @GuardedBy("mStartedWatchers")
206 private final ArrayMap<OnOpStartedListener, IAppOpsStartedCallback> mStartedWatchers =
207 new ArrayMap<>();
208
Svet Ganovb3d2ae22018-12-17 22:06:15 -0800209 @GuardedBy("mNotedWatchers")
210 private final ArrayMap<OnOpNotedListener, IAppOpsNotedCallback> mNotedWatchers =
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -0800211 new ArrayMap<>();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800212
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -0700213 private static final Object sLock = new Object();
214
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -0800215 /** Current {@link OnOpNotedCallback}. Change via {@link #setOnOpNotedCallback} */
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -0700216 @GuardedBy("sLock")
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -0800217 private static @Nullable OnOpNotedCallback sOnOpNotedCallback;
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -0700218
Stanislav Zholnin90516b92020-01-20 14:03:06 +0000219 /**
220 * Additional collector that collect accesses and forwards a few of them them via
221 * {@link IAppOpsService#reportRuntimeAppOpAccessMessageAndGetConfig}.
222 */
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -0800223 private static OnOpNotedCallback sMessageCollector =
224 new OnOpNotedCallback() {
Stanislav Zholnin90516b92020-01-20 14:03:06 +0000225 @Override
226 public void onNoted(@NonNull SyncNotedAppOp op) {
227 reportStackTraceIfNeeded(op);
228 }
229
230 @Override
231 public void onAsyncNoted(@NonNull AsyncNotedAppOp asyncOp) {
232 // collected directly in AppOpsService
233 }
234
235 @Override
236 public void onSelfNoted(@NonNull SyncNotedAppOp op) {
237 reportStackTraceIfNeeded(op);
238 }
239
240 private void reportStackTraceIfNeeded(@NonNull SyncNotedAppOp op) {
Stanislav Zholnind4968a02020-04-18 21:01:13 +0100241 if (!isCollectingStackTraces()) {
Stanislav Zholnin90516b92020-01-20 14:03:06 +0000242 return;
243 }
Stanislav Zholnin90516b92020-01-20 14:03:06 +0000244 MessageSamplingConfig config = sConfig;
245 if (leftCircularDistance(strOpToOp(op.getOp()), config.getSampledOpCode(),
246 _NUM_OP) <= config.getAcceptableLeftDistance()
247 || config.getExpirationTimeSinceBootMillis()
248 < SystemClock.elapsedRealtime()) {
249 String stackTrace = getFormattedStackTrace();
250 try {
251 sConfig = getService().reportRuntimeAppOpAccessMessageAndGetConfig(
252 ActivityThread.currentOpPackageName(), op, stackTrace);
253 } catch (RemoteException e) {
254 e.rethrowFromSystemServer();
255 }
256 }
257 }
258 };
259
Philip P. Moltmann6c6403e2019-12-09 10:08:29 -0800260 static IBinder sClientId;
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700261
Philip P. Moltmanne565c7b2020-01-28 13:15:52 -0800262 /**
263 * How many seconds we want for a drop in uid state from top to settle before applying it.
264 *
265 * <>Set a parameter to {@link android.provider.Settings.Global#APP_OPS_CONSTANTS}
266 *
267 * @hide
268 */
269 @TestApi
270 public static final String KEY_TOP_STATE_SETTLE_TIME = "top_state_settle_time";
271
272 /**
273 * How many second we want for a drop in uid state from foreground to settle before applying it.
274 *
275 * <>Set a parameter to {@link android.provider.Settings.Global#APP_OPS_CONSTANTS}
276 *
277 * @hide
278 */
279 @TestApi
280 public static final String KEY_FG_SERVICE_STATE_SETTLE_TIME =
281 "fg_service_state_settle_time";
282
283 /**
284 * How many seconds we want for a drop in uid state from background to settle before applying
285 * it.
286 *
287 * <>Set a parameter to {@link android.provider.Settings.Global#APP_OPS_CONSTANTS}
288 *
289 * @hide
290 */
291 @TestApi
292 public static final String KEY_BG_STATE_SETTLE_TIME = "bg_state_settle_time";
293
Svet Ganov8455ba22019-01-02 13:05:56 -0800294 /** @hide */
295 @Retention(RetentionPolicy.SOURCE)
296 @IntDef(flag = true, prefix = { "HISTORICAL_MODE_" }, value = {
297 HISTORICAL_MODE_DISABLED,
298 HISTORICAL_MODE_ENABLED_ACTIVE,
299 HISTORICAL_MODE_ENABLED_PASSIVE
300 })
301 public @interface HistoricalMode {}
302
303 /**
304 * Mode in which app op history is completely disabled.
305 * @hide
306 */
307 @TestApi
308 public static final int HISTORICAL_MODE_DISABLED = 0;
309
310 /**
311 * Mode in which app op history is enabled and app ops performed by apps would
312 * be tracked. This is the mode in which the feature is completely enabled.
313 * @hide
314 */
315 @TestApi
316 public static final int HISTORICAL_MODE_ENABLED_ACTIVE = 1;
317
318 /**
319 * Mode in which app op history is enabled but app ops performed by apps would
320 * not be tracked and the only way to add ops to the history is via explicit calls
321 * to dedicated APIs. This mode is useful for testing to allow full control of
322 * the historical content.
323 * @hide
324 */
325 @TestApi
326 public static final int HISTORICAL_MODE_ENABLED_PASSIVE = 2;
327
328 /** @hide */
329 @Retention(RetentionPolicy.SOURCE)
330 @IntDef(flag = true, prefix = { "MODE_" }, value = {
331 MODE_ALLOWED,
332 MODE_IGNORED,
333 MODE_ERRORED,
334 MODE_DEFAULT,
335 MODE_FOREGROUND
336 })
337 public @interface Mode {}
338
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700339 /**
340 * Result from {@link #checkOp}, {@link #noteOp}, {@link #startOp}: the given caller is
341 * allowed to perform the given operation.
342 */
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800343 public static final int MODE_ALLOWED = 0;
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700344
345 /**
346 * Result from {@link #checkOp}, {@link #noteOp}, {@link #startOp}: the given caller is
347 * not allowed to perform the given operation, and this attempt should
348 * <em>silently fail</em> (it should not cause the app to crash).
349 */
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800350 public static final int MODE_IGNORED = 1;
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700351
352 /**
353 * Result from {@link #checkOpNoThrow}, {@link #noteOpNoThrow}, {@link #startOpNoThrow}: the
354 * given caller is not allowed to perform the given operation, and this attempt should
355 * cause it to have a fatal error, typically a {@link SecurityException}.
356 */
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800357 public static final int MODE_ERRORED = 2;
358
Dianne Hackborn33f5ddd2014-07-21 15:35:45 -0700359 /**
360 * Result from {@link #checkOp}, {@link #noteOp}, {@link #startOp}: the given caller should
361 * use its default security check. This mode is not normally used; it should only be used
362 * with appop permissions, and callers must explicitly check for it and deal with it.
363 */
364 public static final int MODE_DEFAULT = 3;
365
Dianne Hackborncd1f30b2018-04-23 17:38:09 -0700366 /**
Dianne Hackbornc216a262018-04-26 13:46:22 -0700367 * Special mode that means "allow only when app is in foreground." This is <b>not</b>
Dianne Hackbornf8709f52019-02-04 16:31:24 -0800368 * returned from {@link #unsafeCheckOp}, {@link #noteOp}, {@link #startOp}. Rather,
369 * {@link #unsafeCheckOp} will always return {@link #MODE_ALLOWED} (because it is always
370 * possible for it to be ultimately allowed, depending on the app's background state),
371 * and {@link #noteOp} and {@link #startOp} will return {@link #MODE_ALLOWED} when the app
372 * being checked is currently in the foreground, otherwise {@link #MODE_IGNORED}.
373 *
374 * <p>The only place you will this normally see this value is through
375 * {@link #unsafeCheckOpRaw}, which returns the actual raw mode of the op. Note that because
376 * you can't know the current state of the app being checked (and it can change at any
377 * point), you can only treat the result here as an indication that it will vary between
378 * {@link #MODE_ALLOWED} and {@link #MODE_IGNORED} depending on changes in the background
379 * state of the app. You thus must always use {@link #noteOp} or {@link #startOp} to do
380 * the actual check for access to the op.</p>
Dianne Hackbornc216a262018-04-26 13:46:22 -0700381 */
382 public static final int MODE_FOREGROUND = 4;
383
Dianne Hackborn65a4f252018-05-08 17:30:48 -0700384 /**
385 * Flag for {@link #startWatchingMode(String, String, int, OnOpChangedListener)}:
386 * Also get reports if the foreground state of an op's uid changes. This only works
387 * when watching a particular op, not when watching a package.
Dianne Hackborn65a4f252018-05-08 17:30:48 -0700388 */
389 public static final int WATCH_FOREGROUND_CHANGES = 1 << 0;
Dianne Hackbornc216a262018-04-26 13:46:22 -0700390
Philip P. Moltmann7641cd02020-03-11 14:42:43 -0700391 /**
392 * Flag for {@link #startWatchingMode} that causes the callback to happen on the switch-op
393 * instead the op the callback was registered. (This simulates pre-R behavior).
394 *
395 * @hide
396 */
397 public static final int CALL_BACK_ON_SWITCHED_OP = 1 << 1;
David Cheung2ead9662020-02-19 16:11:06 -0800398
399 /**
400 * Flag to determine whether we should log noteOp/startOp calls to make sure they
401 * are correctly used
402 *
403 * @hide
404 */
405 public static final boolean NOTE_OP_COLLECTION_ENABLED = false;
406
Dianne Hackbornc216a262018-04-26 13:46:22 -0700407 /**
408 * @hide
409 */
410 public static final String[] MODE_NAMES = new String[] {
411 "allow", // MODE_ALLOWED
412 "ignore", // MODE_IGNORED
413 "deny", // MODE_ERRORED
414 "default", // MODE_DEFAULT
415 "foreground", // MODE_FOREGROUND
416 };
417
Svet Ganovad0a49b2018-10-29 10:07:08 -0700418 /** @hide */
419 @Retention(RetentionPolicy.SOURCE)
Svet Ganovaf189e32019-02-15 18:45:29 -0800420 @IntDef(prefix = { "UID_STATE_" }, value = {
Svet Ganovad0a49b2018-10-29 10:07:08 -0700421 UID_STATE_PERSISTENT,
422 UID_STATE_TOP,
Amith Yamasania0a30a12019-01-22 11:38:06 -0800423 UID_STATE_FOREGROUND_SERVICE_LOCATION,
Svet Ganovad0a49b2018-10-29 10:07:08 -0700424 UID_STATE_FOREGROUND_SERVICE,
425 UID_STATE_FOREGROUND,
426 UID_STATE_BACKGROUND,
427 UID_STATE_CACHED
428 })
429 public @interface UidState {}
430
Dianne Hackbornc216a262018-04-26 13:46:22 -0700431 /**
Svet Ganov05fcd222019-07-08 16:30:45 -0700432 * Uid state: The UID is a foreground persistent app. The lower the UID
433 * state the more important the UID is for the user.
Dianne Hackborncd1f30b2018-04-23 17:38:09 -0700434 * @hide
435 */
Svet Ganov8455ba22019-01-02 13:05:56 -0800436 @TestApi
Svet Ganovad0a49b2018-10-29 10:07:08 -0700437 @SystemApi
Svet Ganovaf189e32019-02-15 18:45:29 -0800438 public static final int UID_STATE_PERSISTENT = 100;
Dianne Hackborncd1f30b2018-04-23 17:38:09 -0700439
440 /**
Svet Ganov05fcd222019-07-08 16:30:45 -0700441 * Uid state: The UID is top foreground app. The lower the UID
442 * state the more important the UID is for the user.
Dianne Hackborncd1f30b2018-04-23 17:38:09 -0700443 * @hide
444 */
Svet Ganov8455ba22019-01-02 13:05:56 -0800445 @TestApi
Svet Ganovad0a49b2018-10-29 10:07:08 -0700446 @SystemApi
Svet Ganovaf189e32019-02-15 18:45:29 -0800447 public static final int UID_STATE_TOP = 200;
Dianne Hackborncd1f30b2018-04-23 17:38:09 -0700448
449 /**
Svet Ganovaf189e32019-02-15 18:45:29 -0800450 * Uid state: The UID is running a foreground service of location type.
Svet Ganov05fcd222019-07-08 16:30:45 -0700451 * The lower the UID state the more important the UID is for the user.
Hui Yu26969322019-08-21 14:56:35 -0700452 * This uid state is a counterpart to PROCESS_STATE_FOREGROUND_SERVICE_LOCATION which has been
453 * deprecated.
Amith Yamasania0a30a12019-01-22 11:38:06 -0800454 * @hide
Hui Yu26969322019-08-21 14:56:35 -0700455 * @deprecated
Amith Yamasania0a30a12019-01-22 11:38:06 -0800456 */
457 @TestApi
458 @SystemApi
Hui Yu26969322019-08-21 14:56:35 -0700459 @Deprecated
Svet Ganovaf189e32019-02-15 18:45:29 -0800460 public static final int UID_STATE_FOREGROUND_SERVICE_LOCATION = 300;
Amith Yamasania0a30a12019-01-22 11:38:06 -0800461
462 /**
Svet Ganov05fcd222019-07-08 16:30:45 -0700463 * Uid state: The UID is running a foreground service. The lower the UID
464 * state the more important the UID is for the user.
Dianne Hackborncd1f30b2018-04-23 17:38:09 -0700465 * @hide
466 */
Svet Ganov8455ba22019-01-02 13:05:56 -0800467 @TestApi
Svet Ganovad0a49b2018-10-29 10:07:08 -0700468 @SystemApi
Svet Ganovaf189e32019-02-15 18:45:29 -0800469 public static final int UID_STATE_FOREGROUND_SERVICE = 400;
Dianne Hackborncd1f30b2018-04-23 17:38:09 -0700470
471 /**
Svet Ganov05fcd222019-07-08 16:30:45 -0700472 * Uid state: The UID is a foreground app. The lower the UID
473 * state the more important the UID is for the user.
Dianne Hackborncd1f30b2018-04-23 17:38:09 -0700474 * @hide
475 */
Svet Ganov8455ba22019-01-02 13:05:56 -0800476 @TestApi
Svet Ganovad0a49b2018-10-29 10:07:08 -0700477 @SystemApi
Svet Ganovaf189e32019-02-15 18:45:29 -0800478 public static final int UID_STATE_FOREGROUND = 500;
Dianne Hackborncd1f30b2018-04-23 17:38:09 -0700479
480 /**
Hui Yu26969322019-08-21 14:56:35 -0700481 * The max, which is min priority, UID state for which any app op
482 * would be considered as performed in the foreground.
483 * @hide
484 */
485 public static final int UID_STATE_MAX_LAST_NON_RESTRICTED = UID_STATE_FOREGROUND;
486
487 /**
Svet Ganov05fcd222019-07-08 16:30:45 -0700488 * Uid state: The UID is a background app. The lower the UID
489 * state the more important the UID is for the user.
Dianne Hackborncd1f30b2018-04-23 17:38:09 -0700490 * @hide
491 */
Svet Ganov8455ba22019-01-02 13:05:56 -0800492 @TestApi
Svet Ganovad0a49b2018-10-29 10:07:08 -0700493 @SystemApi
Svet Ganovaf189e32019-02-15 18:45:29 -0800494 public static final int UID_STATE_BACKGROUND = 600;
Dianne Hackborncd1f30b2018-04-23 17:38:09 -0700495
496 /**
Svet Ganov05fcd222019-07-08 16:30:45 -0700497 * Uid state: The UID is a cached app. The lower the UID
498 * state the more important the UID is for the user.
Dianne Hackborncd1f30b2018-04-23 17:38:09 -0700499 * @hide
500 */
Svet Ganov8455ba22019-01-02 13:05:56 -0800501 @TestApi
Svet Ganovad0a49b2018-10-29 10:07:08 -0700502 @SystemApi
Svet Ganovaf189e32019-02-15 18:45:29 -0800503 public static final int UID_STATE_CACHED = 700;
Dianne Hackborncd1f30b2018-04-23 17:38:09 -0700504
505 /**
Svet Ganovaf189e32019-02-15 18:45:29 -0800506 * Uid state: The UID state with the highest priority.
Dianne Hackborncd1f30b2018-04-23 17:38:09 -0700507 * @hide
508 */
Svet Ganovaf189e32019-02-15 18:45:29 -0800509 public static final int MAX_PRIORITY_UID_STATE = UID_STATE_PERSISTENT;
510
511 /**
512 * Uid state: The UID state with the lowest priority.
513 * @hide
514 */
515 public static final int MIN_PRIORITY_UID_STATE = UID_STATE_CACHED;
516
517 /**
Hui Yu26969322019-08-21 14:56:35 -0700518 * Resolves the first unrestricted state given an app op.
Svet Ganovaf189e32019-02-15 18:45:29 -0800519 * @param op The op to resolve.
520 * @return The last restricted UID state.
521 *
522 * @hide
523 */
524 public static int resolveFirstUnrestrictedUidState(int op) {
Hui Yu26969322019-08-21 14:56:35 -0700525 return UID_STATE_FOREGROUND;
Svet Ganovaf189e32019-02-15 18:45:29 -0800526 }
527
528 /**
Hui Yu26969322019-08-21 14:56:35 -0700529 * Resolves the last restricted state given an app op.
Svet Ganovaf189e32019-02-15 18:45:29 -0800530 * @param op The op to resolve.
531 * @return The last restricted UID state.
532 *
533 * @hide
534 */
535 public static int resolveLastRestrictedUidState(int op) {
Hui Yu26969322019-08-21 14:56:35 -0700536 return UID_STATE_BACKGROUND;
Svet Ganovaf189e32019-02-15 18:45:29 -0800537 }
538
539 /** @hide Note: Keep these sorted */
540 public static final int[] UID_STATES = {
541 UID_STATE_PERSISTENT,
542 UID_STATE_TOP,
543 UID_STATE_FOREGROUND_SERVICE_LOCATION,
544 UID_STATE_FOREGROUND_SERVICE,
545 UID_STATE_FOREGROUND,
546 UID_STATE_BACKGROUND,
547 UID_STATE_CACHED
548 };
549
550 /** @hide */
551 public static String getUidStateName(@UidState int uidState) {
552 switch (uidState) {
553 case UID_STATE_PERSISTENT:
554 return "pers";
555 case UID_STATE_TOP:
556 return "top";
557 case UID_STATE_FOREGROUND_SERVICE_LOCATION:
558 return "fgsvcl";
559 case UID_STATE_FOREGROUND_SERVICE:
560 return "fgsvc";
561 case UID_STATE_FOREGROUND:
562 return "fg";
563 case UID_STATE_BACKGROUND:
564 return "bg";
565 case UID_STATE_CACHED:
566 return "cch";
567 default:
568 return "unknown";
569 }
570 }
571
572 /**
573 * Flag: non proxy operations. These are operations
574 * performed on behalf of the app itself and not on behalf of
575 * another one.
576 *
577 * @hide
578 */
579 @TestApi
580 @SystemApi
581 public static final int OP_FLAG_SELF = 0x1;
582
583 /**
584 * Flag: trusted proxy operations. These are operations
585 * performed on behalf of another app by a trusted app.
586 * Which is work a trusted app blames on another app.
587 *
588 * @hide
589 */
590 @TestApi
591 @SystemApi
592 public static final int OP_FLAG_TRUSTED_PROXY = 0x2;
593
594 /**
595 * Flag: untrusted proxy operations. These are operations
596 * performed on behalf of another app by an untrusted app.
597 * Which is work an untrusted app blames on another app.
598 *
599 * @hide
600 */
601 @TestApi
602 @SystemApi
603 public static final int OP_FLAG_UNTRUSTED_PROXY = 0x4;
604
605 /**
606 * Flag: trusted proxied operations. These are operations
607 * performed by a trusted other app on behalf of an app.
608 * Which is work an app was blamed for by a trusted app.
609 *
610 * @hide
611 */
612 @TestApi
613 @SystemApi
614 public static final int OP_FLAG_TRUSTED_PROXIED = 0x8;
615
616 /**
617 * Flag: untrusted proxied operations. These are operations
618 * performed by an untrusted other app on behalf of an app.
619 * Which is work an app was blamed for by an untrusted app.
620 *
621 * @hide
622 */
623 @TestApi
624 @SystemApi
625 public static final int OP_FLAG_UNTRUSTED_PROXIED = 0x10;
626
627 /**
628 * Flags: all operations. These include operations matched
629 * by {@link #OP_FLAG_SELF}, {@link #OP_FLAG_TRUSTED_PROXIED},
630 * {@link #OP_FLAG_UNTRUSTED_PROXIED}, {@link #OP_FLAG_TRUSTED_PROXIED},
631 * {@link #OP_FLAG_UNTRUSTED_PROXIED}.
632 *
633 * @hide
634 */
635 @TestApi
636 @SystemApi
637 public static final int OP_FLAGS_ALL =
638 OP_FLAG_SELF
639 | OP_FLAG_TRUSTED_PROXY
640 | OP_FLAG_UNTRUSTED_PROXY
641 | OP_FLAG_TRUSTED_PROXIED
642 | OP_FLAG_UNTRUSTED_PROXIED;
643
644 /**
645 * Flags: all trusted operations which is ones either the app did {@link #OP_FLAG_SELF},
646 * or it was blamed for by a trusted app {@link #OP_FLAG_TRUSTED_PROXIED}, or ones the
647 * app if untrusted blamed on other apps {@link #OP_FLAG_UNTRUSTED_PROXY}.
648 *
649 * @hide
650 */
651 @SystemApi
652 public static final int OP_FLAGS_ALL_TRUSTED = AppOpsManager.OP_FLAG_SELF
653 | AppOpsManager.OP_FLAG_UNTRUSTED_PROXY
654 | AppOpsManager.OP_FLAG_TRUSTED_PROXIED;
655
656 /** @hide */
657 @Retention(RetentionPolicy.SOURCE)
658 @IntDef(flag = true, prefix = { "FLAG_" }, value = {
659 OP_FLAG_SELF,
660 OP_FLAG_TRUSTED_PROXY,
661 OP_FLAG_UNTRUSTED_PROXY,
662 OP_FLAG_TRUSTED_PROXIED,
663 OP_FLAG_UNTRUSTED_PROXIED
664 })
665 public @interface OpFlags {}
666
Svet Ganovaf189e32019-02-15 18:45:29 -0800667 /** @hide */
668 public static final String getFlagName(@OpFlags int flag) {
669 switch (flag) {
670 case OP_FLAG_SELF:
671 return "s";
672 case OP_FLAG_TRUSTED_PROXY:
673 return "tp";
674 case OP_FLAG_UNTRUSTED_PROXY:
675 return "up";
676 case OP_FLAG_TRUSTED_PROXIED:
677 return "tpd";
678 case OP_FLAG_UNTRUSTED_PROXIED:
679 return "upd";
680 default:
681 return "unknown";
682 }
683 }
684
Muhammad Qureshi317061a2020-03-02 13:15:51 -0800685 // These constants are redefined here to work around a metalava limitation/bug where
686 // @IntDef is not able to see @hide symbols when they are hidden via package hiding:
687 // frameworks/base/core/java/com/android/internal/package.html
688
689 /** @hide */
690 public static final int SAMPLING_STRATEGY_DEFAULT =
691 FrameworkStatsLog.RUNTIME_APP_OP_ACCESS__SAMPLING_STRATEGY__DEFAULT;
692
693 /** @hide */
694 public static final int SAMPLING_STRATEGY_UNIFORM =
695 FrameworkStatsLog.RUNTIME_APP_OP_ACCESS__SAMPLING_STRATEGY__UNIFORM;
696
697 /** @hide */
698 public static final int SAMPLING_STRATEGY_RARELY_USED =
699 FrameworkStatsLog.RUNTIME_APP_OP_ACCESS__SAMPLING_STRATEGY__RARELY_USED;
700
Stanislav Zholnin4c323852020-03-04 18:03:24 +0000701 /** @hide */
702 public static final int SAMPLING_STRATEGY_BOOT_TIME_SAMPLING =
703 FrameworkStatsLog.RUNTIME_APP_OP_ACCESS__SAMPLING_STRATEGY__BOOT_TIME_SAMPLING;
704
Stanislav Zholnin90516b92020-01-20 14:03:06 +0000705 /**
706 * Strategies used for message sampling
707 * @hide
708 */
709 @Retention(RetentionPolicy.SOURCE)
Muhammad Qureshi317061a2020-03-02 13:15:51 -0800710 @IntDef(prefix = {"SAMPLING_STRATEGY_"}, value = {
711 SAMPLING_STRATEGY_DEFAULT,
712 SAMPLING_STRATEGY_UNIFORM,
Stanislav Zholnin4c323852020-03-04 18:03:24 +0000713 SAMPLING_STRATEGY_RARELY_USED,
714 SAMPLING_STRATEGY_BOOT_TIME_SAMPLING
Stanislav Zholnin90516b92020-01-20 14:03:06 +0000715 })
716 public @interface SamplingStrategy {}
717
Svet Ganovaf189e32019-02-15 18:45:29 -0800718 private static final int UID_STATE_OFFSET = 31;
719 private static final int FLAGS_MASK = 0xFFFFFFFF;
720
721 /**
722 * Key for a data bucket storing app op state. The bucket
723 * is composed of the uid state and state flags. This way
724 * we can query data for given uid state and a set of flags where
725 * the flags control which type of data to get. For example,
726 * one can get the ops an app did on behalf of other apps
727 * while in the background.
728 *
729 * @hide
730 */
731 @Retention(RetentionPolicy.SOURCE)
732 @Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
733 public @interface DataBucketKey {
734 }
735
736 /** @hide */
737 public static String keyToString(@DataBucketKey long key) {
738 final int uidState = extractUidStateFromKey(key);
739 final int flags = extractFlagsFromKey(key);
740 return "[" + getUidStateName(uidState) + "-" + flagsToString(flags) + "]";
741 }
742
743 /** @hide */
744 public static @DataBucketKey long makeKey(@UidState int uidState, @OpFlags int flags) {
745 return ((long) uidState << UID_STATE_OFFSET) | flags;
746 }
747
748 /** @hide */
749 public static int extractUidStateFromKey(@DataBucketKey long key) {
750 return (int) (key >> UID_STATE_OFFSET);
751 }
752
753 /** @hide */
754 public static int extractFlagsFromKey(@DataBucketKey long key) {
755 return (int) (key & FLAGS_MASK);
756 }
757
758 /** @hide */
759 public static String flagsToString(@OpFlags int flags) {
760 final StringBuilder flagsBuilder = new StringBuilder();
761 while (flags != 0) {
762 final int flag = 1 << Integer.numberOfTrailingZeros(flags);
763 flags &= ~flag;
764 if (flagsBuilder.length() > 0) {
765 flagsBuilder.append('|');
766 }
767 flagsBuilder.append(getFlagName(flag));
768 }
769 return flagsBuilder.toString();
770 }
Dianne Hackborncd1f30b2018-04-23 17:38:09 -0700771
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500772 // when adding one of these:
773 // - increment _NUM_OP
Peter Visontay5a2a1ef2017-12-18 20:34:03 +0000774 // - define an OPSTR_* constant (marked as @SystemApi)
Svetoslav Ganoveaca4c52016-05-05 18:08:00 -0700775 // - add rows to sOpToSwitch, sOpToString, sOpNames, sOpToPerms, sOpDefault
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500776 // - add descriptive strings to Settings/res/values/arrays.xml
David Christie0b837452013-07-29 16:02:13 -0700777 // - add the op to the appropriate template in AppOpsState.OpsTemplate (settings app)
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700778
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700779 /** @hide No operation specified. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100780 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000781 public static final int OP_NONE = AppProtoEnums.APP_OP_NONE;
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700782 /** @hide Access to coarse location information. */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +0000783 @UnsupportedAppUsage
Svet Ganov8455ba22019-01-02 13:05:56 -0800784 @TestApi
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000785 public static final int OP_COARSE_LOCATION = AppProtoEnums.APP_OP_COARSE_LOCATION;
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700786 /** @hide Access to fine location information. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100787 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000788 public static final int OP_FINE_LOCATION = AppProtoEnums.APP_OP_FINE_LOCATION;
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700789 /** @hide Causing GPS to run. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100790 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000791 public static final int OP_GPS = AppProtoEnums.APP_OP_GPS;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800792 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100793 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000794 public static final int OP_VIBRATE = AppProtoEnums.APP_OP_VIBRATE;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700795 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100796 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000797 public static final int OP_READ_CONTACTS = AppProtoEnums.APP_OP_READ_CONTACTS;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700798 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100799 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000800 public static final int OP_WRITE_CONTACTS = AppProtoEnums.APP_OP_WRITE_CONTACTS;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700801 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100802 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000803 public static final int OP_READ_CALL_LOG = AppProtoEnums.APP_OP_READ_CALL_LOG;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700804 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100805 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000806 public static final int OP_WRITE_CALL_LOG = AppProtoEnums.APP_OP_WRITE_CALL_LOG;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700807 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100808 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000809 public static final int OP_READ_CALENDAR = AppProtoEnums.APP_OP_READ_CALENDAR;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700810 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100811 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000812 public static final int OP_WRITE_CALENDAR = AppProtoEnums.APP_OP_WRITE_CALENDAR;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700813 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100814 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000815 public static final int OP_WIFI_SCAN = AppProtoEnums.APP_OP_WIFI_SCAN;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700816 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100817 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000818 public static final int OP_POST_NOTIFICATION = AppProtoEnums.APP_OP_POST_NOTIFICATION;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700819 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100820 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000821 public static final int OP_NEIGHBORING_CELLS = AppProtoEnums.APP_OP_NEIGHBORING_CELLS;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700822 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100823 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000824 public static final int OP_CALL_PHONE = AppProtoEnums.APP_OP_CALL_PHONE;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700825 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100826 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000827 public static final int OP_READ_SMS = AppProtoEnums.APP_OP_READ_SMS;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700828 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100829 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000830 public static final int OP_WRITE_SMS = AppProtoEnums.APP_OP_WRITE_SMS;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700831 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100832 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000833 public static final int OP_RECEIVE_SMS = AppProtoEnums.APP_OP_RECEIVE_SMS;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700834 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100835 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000836 public static final int OP_RECEIVE_EMERGECY_SMS =
837 AppProtoEnums.APP_OP_RECEIVE_EMERGENCY_SMS;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700838 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100839 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000840 public static final int OP_RECEIVE_MMS = AppProtoEnums.APP_OP_RECEIVE_MMS;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700841 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100842 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000843 public static final int OP_RECEIVE_WAP_PUSH = AppProtoEnums.APP_OP_RECEIVE_WAP_PUSH;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700844 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100845 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000846 public static final int OP_SEND_SMS = AppProtoEnums.APP_OP_SEND_SMS;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700847 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100848 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000849 public static final int OP_READ_ICC_SMS = AppProtoEnums.APP_OP_READ_ICC_SMS;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700850 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100851 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000852 public static final int OP_WRITE_ICC_SMS = AppProtoEnums.APP_OP_WRITE_ICC_SMS;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700853 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100854 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000855 public static final int OP_WRITE_SETTINGS = AppProtoEnums.APP_OP_WRITE_SETTINGS;
Peter Visontay96449f62017-12-11 18:50:03 +0000856 /** @hide Required to draw on top of other apps. */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +0000857 @UnsupportedAppUsage
Svet Ganovf7b47252018-02-26 11:11:27 -0800858 @TestApi
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000859 public static final int OP_SYSTEM_ALERT_WINDOW = AppProtoEnums.APP_OP_SYSTEM_ALERT_WINDOW;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700860 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100861 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000862 public static final int OP_ACCESS_NOTIFICATIONS =
863 AppProtoEnums.APP_OP_ACCESS_NOTIFICATIONS;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700864 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100865 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000866 public static final int OP_CAMERA = AppProtoEnums.APP_OP_CAMERA;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700867 /** @hide */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +0000868 @UnsupportedAppUsage
Svet Ganova7a0db62018-02-27 20:08:01 -0800869 @TestApi
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000870 public static final int OP_RECORD_AUDIO = AppProtoEnums.APP_OP_RECORD_AUDIO;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700871 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100872 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000873 public static final int OP_PLAY_AUDIO = AppProtoEnums.APP_OP_PLAY_AUDIO;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700874 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100875 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000876 public static final int OP_READ_CLIPBOARD = AppProtoEnums.APP_OP_READ_CLIPBOARD;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700877 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100878 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000879 public static final int OP_WRITE_CLIPBOARD = AppProtoEnums.APP_OP_WRITE_CLIPBOARD;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700880 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100881 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000882 public static final int OP_TAKE_MEDIA_BUTTONS = AppProtoEnums.APP_OP_TAKE_MEDIA_BUTTONS;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700883 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100884 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000885 public static final int OP_TAKE_AUDIO_FOCUS = AppProtoEnums.APP_OP_TAKE_AUDIO_FOCUS;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700886 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100887 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000888 public static final int OP_AUDIO_MASTER_VOLUME = AppProtoEnums.APP_OP_AUDIO_MASTER_VOLUME;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700889 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100890 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000891 public static final int OP_AUDIO_VOICE_VOLUME = AppProtoEnums.APP_OP_AUDIO_VOICE_VOLUME;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700892 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100893 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000894 public static final int OP_AUDIO_RING_VOLUME = AppProtoEnums.APP_OP_AUDIO_RING_VOLUME;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700895 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100896 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000897 public static final int OP_AUDIO_MEDIA_VOLUME = AppProtoEnums.APP_OP_AUDIO_MEDIA_VOLUME;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700898 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100899 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000900 public static final int OP_AUDIO_ALARM_VOLUME = AppProtoEnums.APP_OP_AUDIO_ALARM_VOLUME;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700901 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100902 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000903 public static final int OP_AUDIO_NOTIFICATION_VOLUME =
904 AppProtoEnums.APP_OP_AUDIO_NOTIFICATION_VOLUME;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700905 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100906 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000907 public static final int OP_AUDIO_BLUETOOTH_VOLUME =
908 AppProtoEnums.APP_OP_AUDIO_BLUETOOTH_VOLUME;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700909 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100910 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000911 public static final int OP_WAKE_LOCK = AppProtoEnums.APP_OP_WAKE_LOCK;
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700912 /** @hide Continually monitoring location data. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100913 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000914 public static final int OP_MONITOR_LOCATION =
915 AppProtoEnums.APP_OP_MONITOR_LOCATION;
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700916 /** @hide Continually monitoring location data with a relatively high power request. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100917 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000918 public static final int OP_MONITOR_HIGH_POWER_LOCATION =
919 AppProtoEnums.APP_OP_MONITOR_HIGH_POWER_LOCATION;
Dianne Hackborne22b3b12014-05-07 18:06:44 -0700920 /** @hide Retrieve current usage stats via {@link UsageStatsManager}. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100921 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000922 public static final int OP_GET_USAGE_STATS = AppProtoEnums.APP_OP_GET_USAGE_STATS;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700923 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100924 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000925 public static final int OP_MUTE_MICROPHONE = AppProtoEnums.APP_OP_MUTE_MICROPHONE;
Emily Bernier22c921a2014-05-28 11:01:32 -0400926 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100927 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000928 public static final int OP_TOAST_WINDOW = AppProtoEnums.APP_OP_TOAST_WINDOW;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700929 /** @hide Capture the device's display contents and/or audio */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100930 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000931 public static final int OP_PROJECT_MEDIA = AppProtoEnums.APP_OP_PROJECT_MEDIA;
Benedict Wong3eb95202019-11-05 12:50:59 -0800932 /**
933 * Start (without additional user intervention) a VPN connection, as used by {@link
934 * android.net.VpnService} along with as Platform VPN connections, as used by {@link
935 * android.net.VpnManager}
936 *
937 * <p>This appop is granted to apps that have already been given user consent to start
938 * VpnService based VPN connections. As this is a superset of OP_ACTIVATE_PLATFORM_VPN, this
939 * appop also allows the starting of Platform VPNs.
940 *
941 * @hide
942 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100943 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000944 public static final int OP_ACTIVATE_VPN = AppProtoEnums.APP_OP_ACTIVATE_VPN;
Benjamin Franzf3ece362015-02-11 10:51:10 +0000945 /** @hide Access the WallpaperManagerAPI to write wallpapers. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100946 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000947 public static final int OP_WRITE_WALLPAPER = AppProtoEnums.APP_OP_WRITE_WALLPAPER;
Dianne Hackbornd59a5d52015-04-04 14:52:14 -0700948 /** @hide Received the assist structure from an app. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100949 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000950 public static final int OP_ASSIST_STRUCTURE = AppProtoEnums.APP_OP_ASSIST_STRUCTURE;
Dianne Hackbornd59a5d52015-04-04 14:52:14 -0700951 /** @hide Received a screenshot from assist. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100952 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000953 public static final int OP_ASSIST_SCREENSHOT = AppProtoEnums.APP_OP_ASSIST_SCREENSHOT;
Svet Ganov16a16892015-04-16 10:32:04 -0700954 /** @hide Read the phone state. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100955 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000956 public static final int OP_READ_PHONE_STATE = AppProtoEnums.APP_OP_READ_PHONE_STATE;
Svet Ganovc3300092015-04-17 09:07:22 -0700957 /** @hide Add voicemail messages to the voicemail content provider. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100958 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000959 public static final int OP_ADD_VOICEMAIL = AppProtoEnums.APP_OP_ADD_VOICEMAIL;
Svetoslav5335b672015-04-29 12:00:51 -0700960 /** @hide Access APIs for SIP calling over VOIP or WiFi. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100961 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000962 public static final int OP_USE_SIP = AppProtoEnums.APP_OP_USE_SIP;
Svetoslavc656e6f2015-04-29 14:08:16 -0700963 /** @hide Intercept outgoing calls. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100964 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000965 public static final int OP_PROCESS_OUTGOING_CALLS =
966 AppProtoEnums.APP_OP_PROCESS_OUTGOING_CALLS;
Svetoslav4af76a52015-04-29 15:29:46 -0700967 /** @hide User the fingerprint API. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100968 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000969 public static final int OP_USE_FINGERPRINT = AppProtoEnums.APP_OP_USE_FINGERPRINT;
Svet Ganovb9d71a62015-04-30 10:38:13 -0700970 /** @hide Access to body sensors such as heart rate, etc. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100971 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000972 public static final int OP_BODY_SENSORS = AppProtoEnums.APP_OP_BODY_SENSORS;
Svet Ganovede43162015-05-02 17:42:44 -0700973 /** @hide Read previously received cell broadcast messages. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100974 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000975 public static final int OP_READ_CELL_BROADCASTS = AppProtoEnums.APP_OP_READ_CELL_BROADCASTS;
Svet Ganovf7e9cf42015-05-13 10:40:31 -0700976 /** @hide Inject mock location into the system. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100977 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000978 public static final int OP_MOCK_LOCATION = AppProtoEnums.APP_OP_MOCK_LOCATION;
Svet Ganov921c7df2015-06-29 21:51:41 -0700979 /** @hide Read external storage. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100980 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000981 public static final int OP_READ_EXTERNAL_STORAGE = AppProtoEnums.APP_OP_READ_EXTERNAL_STORAGE;
Svet Ganov921c7df2015-06-29 21:51:41 -0700982 /** @hide Write external storage. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100983 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000984 public static final int OP_WRITE_EXTERNAL_STORAGE =
985 AppProtoEnums.APP_OP_WRITE_EXTERNAL_STORAGE;
Dianne Hackborn280a64e2015-07-13 14:48:08 -0700986 /** @hide Turned on the screen. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100987 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000988 public static final int OP_TURN_SCREEN_ON = AppProtoEnums.APP_OP_TURN_SCREEN_ON;
Svetoslavf3f02ac2015-09-08 14:36:35 -0700989 /** @hide Get device accounts. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100990 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000991 public static final int OP_GET_ACCOUNTS = AppProtoEnums.APP_OP_GET_ACCOUNTS;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700992 /** @hide Control whether an application is allowed to run in the background. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100993 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000994 public static final int OP_RUN_IN_BACKGROUND =
Stanislav Zholnin5c88c732020-03-29 21:46:35 +0100995 AppProtoEnums.APP_OP_RUN_IN_BACKGROUND;
Jason Monk1c7c3192014-06-26 12:52:18 -0400996 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100997 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +0000998 public static final int OP_AUDIO_ACCESSIBILITY_VOLUME =
999 AppProtoEnums.APP_OP_AUDIO_ACCESSIBILITY_VOLUME;
Chad Brubaker73ec8f92016-11-10 11:24:40 -08001000 /** @hide Read the phone number. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001001 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001002 public static final int OP_READ_PHONE_NUMBERS = AppProtoEnums.APP_OP_READ_PHONE_NUMBERS;
Suprabh Shukla2f34b1a2016-12-16 14:47:25 -08001003 /** @hide Request package installs through package installer */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001004 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001005 public static final int OP_REQUEST_INSTALL_PACKAGES =
1006 AppProtoEnums.APP_OP_REQUEST_INSTALL_PACKAGES;
Winson Chungf4ac0632017-03-17 12:34:12 -07001007 /** @hide Enter picture-in-picture. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001008 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001009 public static final int OP_PICTURE_IN_PICTURE = AppProtoEnums.APP_OP_PICTURE_IN_PICTURE;
Chad Brubaker97b383f2017-02-02 15:04:35 -08001010 /** @hide Instant app start foreground service. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001011 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001012 public static final int OP_INSTANT_APP_START_FOREGROUND =
1013 AppProtoEnums.APP_OP_INSTANT_APP_START_FOREGROUND;
Eugene Suslacae3d3e2017-01-31 11:08:11 -08001014 /** @hide Answer incoming phone calls */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001015 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001016 public static final int OP_ANSWER_PHONE_CALLS = AppProtoEnums.APP_OP_ANSWER_PHONE_CALLS;
Suprabh Shukla3ac1daa2017-07-14 12:15:27 -07001017 /** @hide Run jobs when in background */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001018 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001019 public static final int OP_RUN_ANY_IN_BACKGROUND = AppProtoEnums.APP_OP_RUN_ANY_IN_BACKGROUND;
Peter Visontay1246d9e2017-10-17 17:02:45 +01001020 /** @hide Change Wi-Fi connectivity state */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001021 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001022 public static final int OP_CHANGE_WIFI_STATE = AppProtoEnums.APP_OP_CHANGE_WIFI_STATE;
Peter Visontayf2e38362017-11-27 15:27:16 +00001023 /** @hide Request package deletion through package installer */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001024 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001025 public static final int OP_REQUEST_DELETE_PACKAGES =
1026 AppProtoEnums.APP_OP_REQUEST_DELETE_PACKAGES;
Peter Visontay11950832017-11-14 19:34:59 +00001027 /** @hide Bind an accessibility service. */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001028 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001029 public static final int OP_BIND_ACCESSIBILITY_SERVICE =
1030 AppProtoEnums.APP_OP_BIND_ACCESSIBILITY_SERVICE;
Tyler Gunn79bc1ec2018-01-22 15:17:54 -08001031 /** @hide Continue handover of a call from another app */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001032 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001033 public static final int OP_ACCEPT_HANDOVER = AppProtoEnums.APP_OP_ACCEPT_HANDOVER;
Nathan Harold1bb420672018-03-14 17:08:53 -07001034 /** @hide Create and Manage IPsec Tunnels */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001035 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001036 public static final int OP_MANAGE_IPSEC_TUNNELS = AppProtoEnums.APP_OP_MANAGE_IPSEC_TUNNELS;
Dianne Hackborne9d9b4b2018-03-28 13:51:46 -07001037 /** @hide Any app start foreground service. */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +00001038 @UnsupportedAppUsage
Svet Ganovaf189e32019-02-15 18:45:29 -08001039 @TestApi
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001040 public static final int OP_START_FOREGROUND = AppProtoEnums.APP_OP_START_FOREGROUND;
Jean-Michel Trivi3f0945a2016-11-11 10:05:18 -08001041 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001042 @UnsupportedAppUsage
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001043 public static final int OP_BLUETOOTH_SCAN = AppProtoEnums.APP_OP_BLUETOOTH_SCAN;
Kevin Chynb3c05aa2018-09-21 16:50:32 -07001044 /** @hide Use the BiometricPrompt/BiometricManager APIs. */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001045 public static final int OP_USE_BIOMETRIC = AppProtoEnums.APP_OP_USE_BIOMETRIC;
Zimuzo6cbf9cc2018-10-05 12:05:58 +01001046 /** @hide Physical activity recognition. */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001047 public static final int OP_ACTIVITY_RECOGNITION = AppProtoEnums.APP_OP_ACTIVITY_RECOGNITION;
Hongming Jin228cd012018-11-09 14:47:50 -08001048 /** @hide Financial app sms read. */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001049 public static final int OP_SMS_FINANCIAL_TRANSACTIONS =
1050 AppProtoEnums.APP_OP_SMS_FINANCIAL_TRANSACTIONS;
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07001051 /** @hide Read media of audio type. */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001052 public static final int OP_READ_MEDIA_AUDIO = AppProtoEnums.APP_OP_READ_MEDIA_AUDIO;
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07001053 /** @hide Write media of audio type. */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001054 public static final int OP_WRITE_MEDIA_AUDIO = AppProtoEnums.APP_OP_WRITE_MEDIA_AUDIO;
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07001055 /** @hide Read media of video type. */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001056 public static final int OP_READ_MEDIA_VIDEO = AppProtoEnums.APP_OP_READ_MEDIA_VIDEO;
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07001057 /** @hide Write media of video type. */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001058 public static final int OP_WRITE_MEDIA_VIDEO = AppProtoEnums.APP_OP_WRITE_MEDIA_VIDEO;
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07001059 /** @hide Read media of image type. */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001060 public static final int OP_READ_MEDIA_IMAGES = AppProtoEnums.APP_OP_READ_MEDIA_IMAGES;
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07001061 /** @hide Write media of image type. */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001062 public static final int OP_WRITE_MEDIA_IMAGES = AppProtoEnums.APP_OP_WRITE_MEDIA_IMAGES;
Jeff Sharkeye82cbb12018-12-06 15:53:11 -07001063 /** @hide Has a legacy (non-isolated) view of storage. */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001064 public static final int OP_LEGACY_STORAGE = AppProtoEnums.APP_OP_LEGACY_STORAGE;
Jackal Guo8dc791e2019-01-14 10:26:42 +08001065 /** @hide Accessing accessibility features */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001066 public static final int OP_ACCESS_ACCESSIBILITY = AppProtoEnums.APP_OP_ACCESS_ACCESSIBILITY;
Michael Groover656ef912019-04-09 17:09:57 -07001067 /** @hide Read the device identifiers (IMEI / MEID, IMSI, SIM / Build serial) */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001068 public static final int OP_READ_DEVICE_IDENTIFIERS =
1069 AppProtoEnums.APP_OP_READ_DEVICE_IDENTIFIERS;
Philip P. Moltmann89b044f2019-09-13 15:12:34 -07001070 /** @hide Read location metadata from media */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001071 public static final int OP_ACCESS_MEDIA_LOCATION = AppProtoEnums.APP_OP_ACCESS_MEDIA_LOCATION;
Patrick Baumann6c1c8092019-06-27 14:55:44 -07001072 /** @hide Query all apps on device, regardless of declarations in the calling app manifest */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001073 public static final int OP_QUERY_ALL_PACKAGES = AppProtoEnums.APP_OP_QUERY_ALL_PACKAGES;
Philip P. Moltmannb43dfe32019-11-11 14:14:49 -08001074 /** @hide Access all external storage */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001075 public static final int OP_MANAGE_EXTERNAL_STORAGE =
1076 AppProtoEnums.APP_OP_MANAGE_EXTERNAL_STORAGE;
kholoud mohamed20ded1f2019-12-03 17:52:38 +00001077 /** @hide Communicate cross-profile within the same profile group. */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001078 public static final int OP_INTERACT_ACROSS_PROFILES =
1079 AppProtoEnums.APP_OP_INTERACT_ACROSS_PROFILES;
Benedict Wong3eb95202019-11-05 12:50:59 -08001080 /**
1081 * Start (without additional user intervention) a Platform VPN connection, as used by {@link
1082 * android.net.VpnManager}
1083 *
1084 * <p>This appop is granted to apps that have already been given user consent to start Platform
1085 * VPN connections. This appop is insufficient to start VpnService based VPNs; OP_ACTIVATE_VPN
1086 * is needed for that.
1087 *
1088 * @hide
1089 */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001090 public static final int OP_ACTIVATE_PLATFORM_VPN = AppProtoEnums.APP_OP_ACTIVATE_PLATFORM_VPN;
Todd Kennedy0f006752020-04-29 09:37:10 -07001091 /** @hide Controls whether or not read logs are available for incremental installations. */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001092 public static final int OP_LOADER_USAGE_STATS = AppProtoEnums.APP_OP_LOADER_USAGE_STATS;
Ricardo Correaacdc8272020-03-24 11:02:54 -07001093
1094 // App op deprecated/removed.
1095 private static final int OP_DEPRECATED_1 = AppProtoEnums.APP_OP_DEPRECATED_1;
Patrick Baumann6c1c8092019-06-27 14:55:44 -07001096
Eugene Susla5fc2d762020-03-04 13:53:10 -08001097 /** @hide Auto-revoke app permissions if app is unused for an extended period */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001098 public static final int OP_AUTO_REVOKE_PERMISSIONS_IF_UNUSED =
1099 AppProtoEnums.APP_OP_AUTO_REVOKE_PERMISSIONS_IF_UNUSED;
Eugene Susla5fc2d762020-03-04 13:53:10 -08001100
Eugene Susla922cd082020-03-11 12:38:17 -07001101 /**
1102 * Whether {@link #OP_AUTO_REVOKE_PERMISSIONS_IF_UNUSED} is allowed to be changed by
1103 * the installer
1104 *
1105 * @hide
1106 */
Stanislav Zholnind5378e92020-03-15 18:37:12 +00001107 public static final int OP_AUTO_REVOKE_MANAGED_BY_INSTALLER =
1108 AppProtoEnums.APP_OP_AUTO_REVOKE_MANAGED_BY_INSTALLER;
Eugene Susla922cd082020-03-11 12:38:17 -07001109
Dianne Hackborne04f13d2018-05-02 12:51:52 -07001110 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001111 @UnsupportedAppUsage
Eugene Susla922cd082020-03-11 12:38:17 -07001112 public static final int _NUM_OP = 99;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001113
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001114 /** Access to coarse location information. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -07001115 public static final String OPSTR_COARSE_LOCATION = "android:coarse_location";
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001116 /** Access to fine location information. */
1117 public static final String OPSTR_FINE_LOCATION =
1118 "android:fine_location";
1119 /** Continually monitoring location data. */
1120 public static final String OPSTR_MONITOR_LOCATION
1121 = "android:monitor_location";
1122 /** Continually monitoring location data with a relatively high power request. */
1123 public static final String OPSTR_MONITOR_HIGH_POWER_LOCATION
1124 = "android:monitor_location_high_power";
Dianne Hackborn5064e7c2014-09-02 10:57:16 -07001125 /** Access to {@link android.app.usage.UsageStatsManager}. */
1126 public static final String OPSTR_GET_USAGE_STATS
1127 = "android:get_usage_stats";
Jeff Davidson05542602014-08-11 14:07:27 -07001128 /** Activate a VPN connection without user intervention. @hide */
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001129 @SystemApi @TestApi
Svet Ganov6e8f67c2015-04-29 17:35:19 -07001130 public static final String OPSTR_ACTIVATE_VPN
1131 = "android:activate_vpn";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001132 /** Allows an application to read the user's contacts data. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -07001133 public static final String OPSTR_READ_CONTACTS
1134 = "android:read_contacts";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001135 /** Allows an application to write to the user's contacts data. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -07001136 public static final String OPSTR_WRITE_CONTACTS
1137 = "android:write_contacts";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001138 /** Allows an application to read the user's call log. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -07001139 public static final String OPSTR_READ_CALL_LOG
1140 = "android:read_call_log";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001141 /** Allows an application to write to the user's call log. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -07001142 public static final String OPSTR_WRITE_CALL_LOG
1143 = "android:write_call_log";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001144 /** Allows an application to read the user's calendar data. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -07001145 public static final String OPSTR_READ_CALENDAR
1146 = "android:read_calendar";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001147 /** Allows an application to write to the user's calendar data. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -07001148 public static final String OPSTR_WRITE_CALENDAR
1149 = "android:write_calendar";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001150 /** Allows an application to initiate a phone call. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -07001151 public static final String OPSTR_CALL_PHONE
1152 = "android:call_phone";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001153 /** Allows an application to read SMS messages. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -07001154 public static final String OPSTR_READ_SMS
1155 = "android:read_sms";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001156 /** Allows an application to receive SMS messages. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -07001157 public static final String OPSTR_RECEIVE_SMS
1158 = "android:receive_sms";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001159 /** Allows an application to receive MMS messages. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -07001160 public static final String OPSTR_RECEIVE_MMS
1161 = "android:receive_mms";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001162 /** Allows an application to receive WAP push messages. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -07001163 public static final String OPSTR_RECEIVE_WAP_PUSH
1164 = "android:receive_wap_push";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001165 /** Allows an application to send SMS messages. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -07001166 public static final String OPSTR_SEND_SMS
1167 = "android:send_sms";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001168 /** Required to be able to access the camera device. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -07001169 public static final String OPSTR_CAMERA
1170 = "android:camera";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001171 /** Required to be able to access the microphone device. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -07001172 public static final String OPSTR_RECORD_AUDIO
1173 = "android:record_audio";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001174 /** Required to access phone state related information. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -07001175 public static final String OPSTR_READ_PHONE_STATE
1176 = "android:read_phone_state";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001177 /** Required to access phone state related information. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -07001178 public static final String OPSTR_ADD_VOICEMAIL
1179 = "android:add_voicemail";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001180 /** Access APIs for SIP calling over VOIP or WiFi */
Svet Ganovb9d71a62015-04-30 10:38:13 -07001181 public static final String OPSTR_USE_SIP
1182 = "android:use_sip";
Svet Ganove8e89422016-09-22 19:56:50 -07001183 /** Access APIs for diverting outgoing calls */
Svet Ganov824ad6e2016-09-22 19:36:53 -07001184 public static final String OPSTR_PROCESS_OUTGOING_CALLS
1185 = "android:process_outgoing_calls";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001186 /** Use the fingerprint API. */
Svet Ganovb9d71a62015-04-30 10:38:13 -07001187 public static final String OPSTR_USE_FINGERPRINT
1188 = "android:use_fingerprint";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001189 /** Access to body sensors such as heart rate, etc. */
Svet Ganovb9d71a62015-04-30 10:38:13 -07001190 public static final String OPSTR_BODY_SENSORS
1191 = "android:body_sensors";
Svet Ganov715cf2a2015-06-13 17:31:29 -07001192 /** Read previously received cell broadcast messages. */
Svet Ganovede43162015-05-02 17:42:44 -07001193 public static final String OPSTR_READ_CELL_BROADCASTS
1194 = "android:read_cell_broadcasts";
Svet Ganovf7e9cf42015-05-13 10:40:31 -07001195 /** Inject mock location into the system. */
1196 public static final String OPSTR_MOCK_LOCATION
1197 = "android:mock_location";
Svet Ganov921c7df2015-06-29 21:51:41 -07001198 /** Read external storage. */
1199 public static final String OPSTR_READ_EXTERNAL_STORAGE
1200 = "android:read_external_storage";
1201 /** Write external storage. */
1202 public static final String OPSTR_WRITE_EXTERNAL_STORAGE
1203 = "android:write_external_storage";
Billy Lau24b9c832015-07-20 17:34:09 +01001204 /** Required to draw on top of other apps. */
1205 public static final String OPSTR_SYSTEM_ALERT_WINDOW
1206 = "android:system_alert_window";
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -08001207 /** Required to write/modify/update system settings. */
Billy Lau24b9c832015-07-20 17:34:09 +01001208 public static final String OPSTR_WRITE_SETTINGS
1209 = "android:write_settings";
Svetoslavf3f02ac2015-09-08 14:36:35 -07001210 /** @hide Get device accounts. */
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001211 @SystemApi @TestApi
Svetoslavf3f02ac2015-09-08 14:36:35 -07001212 public static final String OPSTR_GET_ACCOUNTS
1213 = "android:get_accounts";
Chad Brubaker0c1651f2017-03-30 16:29:10 -07001214 public static final String OPSTR_READ_PHONE_NUMBERS
1215 = "android:read_phone_numbers";
Winson Chungf4ac0632017-03-17 12:34:12 -07001216 /** Access to picture-in-picture. */
1217 public static final String OPSTR_PICTURE_IN_PICTURE
1218 = "android:picture_in_picture";
Chad Brubaker97b383f2017-02-02 15:04:35 -08001219 /** @hide */
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001220 @SystemApi @TestApi
Chad Brubaker97b383f2017-02-02 15:04:35 -08001221 public static final String OPSTR_INSTANT_APP_START_FOREGROUND
1222 = "android:instant_app_start_foreground";
Eugene Suslacae3d3e2017-01-31 11:08:11 -08001223 /** Answer incoming phone calls */
1224 public static final String OPSTR_ANSWER_PHONE_CALLS
1225 = "android:answer_phone_calls";
Tyler Gunn79bc1ec2018-01-22 15:17:54 -08001226 /**
1227 * Accept call handover
1228 * @hide
1229 */
1230 @SystemApi @TestApi
1231 public static final String OPSTR_ACCEPT_HANDOVER
1232 = "android:accept_handover";
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001233 /** @hide */
1234 @SystemApi @TestApi
1235 public static final String OPSTR_GPS = "android:gps";
1236 /** @hide */
1237 @SystemApi @TestApi
1238 public static final String OPSTR_VIBRATE = "android:vibrate";
1239 /** @hide */
1240 @SystemApi @TestApi
1241 public static final String OPSTR_WIFI_SCAN = "android:wifi_scan";
1242 /** @hide */
1243 @SystemApi @TestApi
1244 public static final String OPSTR_POST_NOTIFICATION = "android:post_notification";
1245 /** @hide */
1246 @SystemApi @TestApi
1247 public static final String OPSTR_NEIGHBORING_CELLS = "android:neighboring_cells";
1248 /** @hide */
1249 @SystemApi @TestApi
1250 public static final String OPSTR_WRITE_SMS = "android:write_sms";
1251 /** @hide */
1252 @SystemApi @TestApi
1253 public static final String OPSTR_RECEIVE_EMERGENCY_BROADCAST =
1254 "android:receive_emergency_broadcast";
1255 /** @hide */
1256 @SystemApi @TestApi
1257 public static final String OPSTR_READ_ICC_SMS = "android:read_icc_sms";
1258 /** @hide */
1259 @SystemApi @TestApi
1260 public static final String OPSTR_WRITE_ICC_SMS = "android:write_icc_sms";
1261 /** @hide */
1262 @SystemApi @TestApi
1263 public static final String OPSTR_ACCESS_NOTIFICATIONS = "android:access_notifications";
1264 /** @hide */
1265 @SystemApi @TestApi
1266 public static final String OPSTR_PLAY_AUDIO = "android:play_audio";
1267 /** @hide */
1268 @SystemApi @TestApi
1269 public static final String OPSTR_READ_CLIPBOARD = "android:read_clipboard";
1270 /** @hide */
1271 @SystemApi @TestApi
1272 public static final String OPSTR_WRITE_CLIPBOARD = "android:write_clipboard";
1273 /** @hide */
1274 @SystemApi @TestApi
1275 public static final String OPSTR_TAKE_MEDIA_BUTTONS = "android:take_media_buttons";
1276 /** @hide */
1277 @SystemApi @TestApi
1278 public static final String OPSTR_TAKE_AUDIO_FOCUS = "android:take_audio_focus";
1279 /** @hide */
1280 @SystemApi @TestApi
1281 public static final String OPSTR_AUDIO_MASTER_VOLUME = "android:audio_master_volume";
1282 /** @hide */
1283 @SystemApi @TestApi
1284 public static final String OPSTR_AUDIO_VOICE_VOLUME = "android:audio_voice_volume";
1285 /** @hide */
1286 @SystemApi @TestApi
1287 public static final String OPSTR_AUDIO_RING_VOLUME = "android:audio_ring_volume";
1288 /** @hide */
1289 @SystemApi @TestApi
1290 public static final String OPSTR_AUDIO_MEDIA_VOLUME = "android:audio_media_volume";
1291 /** @hide */
1292 @SystemApi @TestApi
1293 public static final String OPSTR_AUDIO_ALARM_VOLUME = "android:audio_alarm_volume";
1294 /** @hide */
1295 @SystemApi @TestApi
1296 public static final String OPSTR_AUDIO_NOTIFICATION_VOLUME =
1297 "android:audio_notification_volume";
1298 /** @hide */
1299 @SystemApi @TestApi
1300 public static final String OPSTR_AUDIO_BLUETOOTH_VOLUME = "android:audio_bluetooth_volume";
1301 /** @hide */
1302 @SystemApi @TestApi
1303 public static final String OPSTR_WAKE_LOCK = "android:wake_lock";
1304 /** @hide */
1305 @SystemApi @TestApi
1306 public static final String OPSTR_MUTE_MICROPHONE = "android:mute_microphone";
1307 /** @hide */
1308 @SystemApi @TestApi
1309 public static final String OPSTR_TOAST_WINDOW = "android:toast_window";
1310 /** @hide */
1311 @SystemApi @TestApi
1312 public static final String OPSTR_PROJECT_MEDIA = "android:project_media";
1313 /** @hide */
1314 @SystemApi @TestApi
1315 public static final String OPSTR_WRITE_WALLPAPER = "android:write_wallpaper";
1316 /** @hide */
1317 @SystemApi @TestApi
1318 public static final String OPSTR_ASSIST_STRUCTURE = "android:assist_structure";
1319 /** @hide */
1320 @SystemApi @TestApi
1321 public static final String OPSTR_ASSIST_SCREENSHOT = "android:assist_screenshot";
1322 /** @hide */
1323 @SystemApi @TestApi
1324 public static final String OPSTR_TURN_SCREEN_ON = "android:turn_screen_on";
1325 /** @hide */
1326 @SystemApi @TestApi
1327 public static final String OPSTR_RUN_IN_BACKGROUND = "android:run_in_background";
1328 /** @hide */
1329 @SystemApi @TestApi
1330 public static final String OPSTR_AUDIO_ACCESSIBILITY_VOLUME =
1331 "android:audio_accessibility_volume";
1332 /** @hide */
1333 @SystemApi @TestApi
1334 public static final String OPSTR_REQUEST_INSTALL_PACKAGES = "android:request_install_packages";
1335 /** @hide */
1336 @SystemApi @TestApi
1337 public static final String OPSTR_RUN_ANY_IN_BACKGROUND = "android:run_any_in_background";
1338 /** @hide */
1339 @SystemApi @TestApi
Peter Visontaya382a8e2018-03-16 16:06:57 +00001340 public static final String OPSTR_CHANGE_WIFI_STATE = "android:change_wifi_state";
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001341 /** @hide */
1342 @SystemApi @TestApi
Peter Visontaya382a8e2018-03-16 16:06:57 +00001343 public static final String OPSTR_REQUEST_DELETE_PACKAGES = "android:request_delete_packages";
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001344 /** @hide */
1345 @SystemApi @TestApi
Peter Visontaya382a8e2018-03-16 16:06:57 +00001346 public static final String OPSTR_BIND_ACCESSIBILITY_SERVICE =
1347 "android:bind_accessibility_service";
Nathan Harold1bb420672018-03-14 17:08:53 -07001348 /** @hide */
1349 @SystemApi @TestApi
1350 public static final String OPSTR_MANAGE_IPSEC_TUNNELS = "android:manage_ipsec_tunnels";
Dianne Hackborne9d9b4b2018-03-28 13:51:46 -07001351 /** @hide */
1352 @SystemApi @TestApi
1353 public static final String OPSTR_START_FOREGROUND = "android:start_foreground";
Dianne Hackborne04f13d2018-05-02 12:51:52 -07001354 /** @hide */
1355 public static final String OPSTR_BLUETOOTH_SCAN = "android:bluetooth_scan";
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001356
Kevin Chynb3c05aa2018-09-21 16:50:32 -07001357 /** @hide Use the BiometricPrompt/BiometricManager APIs. */
1358 public static final String OPSTR_USE_BIOMETRIC = "android:use_biometric";
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001359
Zimuzo6cbf9cc2018-10-05 12:05:58 +01001360 /** @hide Recognize physical activity. */
1361 public static final String OPSTR_ACTIVITY_RECOGNITION = "android:activity_recognition";
1362
Hongming Jin228cd012018-11-09 14:47:50 -08001363 /** @hide Financial app read sms. */
1364 public static final String OPSTR_SMS_FINANCIAL_TRANSACTIONS =
1365 "android:sms_financial_transactions";
1366
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07001367 /** @hide Read media of audio type. */
Jeff Sharkey5997cc82019-08-21 11:14:40 -06001368 @SystemApi @TestApi
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07001369 public static final String OPSTR_READ_MEDIA_AUDIO = "android:read_media_audio";
1370 /** @hide Write media of audio type. */
Jeff Sharkey5997cc82019-08-21 11:14:40 -06001371 @SystemApi @TestApi
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07001372 public static final String OPSTR_WRITE_MEDIA_AUDIO = "android:write_media_audio";
1373 /** @hide Read media of video type. */
Jeff Sharkey5997cc82019-08-21 11:14:40 -06001374 @SystemApi @TestApi
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07001375 public static final String OPSTR_READ_MEDIA_VIDEO = "android:read_media_video";
1376 /** @hide Write media of video type. */
Jeff Sharkey5997cc82019-08-21 11:14:40 -06001377 @SystemApi @TestApi
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07001378 public static final String OPSTR_WRITE_MEDIA_VIDEO = "android:write_media_video";
1379 /** @hide Read media of image type. */
Jeff Sharkey5997cc82019-08-21 11:14:40 -06001380 @SystemApi @TestApi
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07001381 public static final String OPSTR_READ_MEDIA_IMAGES = "android:read_media_images";
1382 /** @hide Write media of image type. */
Jeff Sharkey5997cc82019-08-21 11:14:40 -06001383 @SystemApi @TestApi
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07001384 public static final String OPSTR_WRITE_MEDIA_IMAGES = "android:write_media_images";
Jeff Sharkeye82cbb12018-12-06 15:53:11 -07001385 /** @hide Has a legacy (non-isolated) view of storage. */
Jeff Sharkey5997cc82019-08-21 11:14:40 -06001386 @SystemApi @TestApi
Jeff Sharkeye82cbb12018-12-06 15:53:11 -07001387 public static final String OPSTR_LEGACY_STORAGE = "android:legacy_storage";
Philip P. Moltmann89b044f2019-09-13 15:12:34 -07001388 /** @hide Read location metadata from media */
1389 public static final String OPSTR_ACCESS_MEDIA_LOCATION = "android:access_media_location";
Jeff Sharkey5997cc82019-08-21 11:14:40 -06001390
Jackal Guo8dc791e2019-01-14 10:26:42 +08001391 /** @hide Interact with accessibility. */
Joel Galensonff4fe202019-02-06 14:43:58 -08001392 @SystemApi
Jackal Guo8dc791e2019-01-14 10:26:42 +08001393 public static final String OPSTR_ACCESS_ACCESSIBILITY = "android:access_accessibility";
Michael Groover656ef912019-04-09 17:09:57 -07001394 /** @hide Read device identifiers */
1395 public static final String OPSTR_READ_DEVICE_IDENTIFIERS = "android:read_device_identifiers";
Patrick Baumann6c1c8092019-06-27 14:55:44 -07001396 /** @hide Query all packages on device */
1397 public static final String OPSTR_QUERY_ALL_PACKAGES = "android:query_all_packages";
Philip P. Moltmannb43dfe32019-11-11 14:14:49 -08001398 /** @hide Access all external storage */
1399 @SystemApi
shafik2a207902020-04-08 17:49:47 +01001400 @TestApi
Philip P. Moltmannb43dfe32019-11-11 14:14:49 -08001401 public static final String OPSTR_MANAGE_EXTERNAL_STORAGE =
1402 "android:manage_external_storage";
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07001403
Eugene Susla5fc2d762020-03-04 13:53:10 -08001404 /** @hide Auto-revoke app permissions if app is unused for an extended period */
1405 @SystemApi
1406 public static final String OPSTR_AUTO_REVOKE_PERMISSIONS_IF_UNUSED =
1407 "android:auto_revoke_permissions_if_unused";
1408
Eugene Susla1d7a7912020-03-14 09:16:29 -07001409 /** @hide Auto-revoke app permissions if app is unused for an extended period */
Eugene Suslaf3ce46a2020-03-14 10:28:09 -07001410 @SystemApi
Eugene Susla1d7a7912020-03-14 09:16:29 -07001411 public static final String OPSTR_AUTO_REVOKE_MANAGED_BY_INSTALLER =
1412 "android:auto_revoke_managed_by_installer";
1413
kholoud mohamed20ded1f2019-12-03 17:52:38 +00001414 /** @hide Communicate cross-profile within the same profile group. */
1415 @SystemApi
1416 public static final String OPSTR_INTERACT_ACROSS_PROFILES = "android:interact_across_profiles";
Benedict Wong3eb95202019-11-05 12:50:59 -08001417 /** @hide Start Platform VPN without user intervention */
1418 public static final String OPSTR_ACTIVATE_PLATFORM_VPN = "android:activate_platform_vpn";
Todd Kennedyb0948d42020-01-27 15:20:07 -08001419 /** @hide */
1420 @SystemApi
1421 public static final String OPSTR_LOADER_USAGE_STATS = "android:loader_usage_stats";
kholoud mohamed20ded1f2019-12-03 17:52:38 +00001422
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07001423
1424 /** {@link #sAppOpsToNote} not initialized yet for this op */
1425 private static final byte SHOULD_COLLECT_NOTE_OP_NOT_INITIALIZED = 0;
1426 /** Should not collect noting of this app-op in {@link #sAppOpsToNote} */
1427 private static final byte SHOULD_NOT_COLLECT_NOTE_OP = 1;
1428 /** Should collect noting of this app-op in {@link #sAppOpsToNote} */
1429 private static final byte SHOULD_COLLECT_NOTE_OP = 2;
1430
1431 @Retention(RetentionPolicy.SOURCE)
1432 @IntDef(flag = true, prefix = { "SHOULD_" }, value = {
1433 SHOULD_COLLECT_NOTE_OP_NOT_INITIALIZED,
1434 SHOULD_NOT_COLLECT_NOTE_OP,
1435 SHOULD_COLLECT_NOTE_OP
1436 })
1437 private @interface ShouldCollectNoteOp {}
1438
Svet Ganovda0acdf2017-02-15 10:28:51 -08001439 private static final int[] RUNTIME_AND_APPOP_PERMISSIONS_OPS = {
1440 // RUNTIME PERMISSIONS
Svetoslav Ganoveaca4c52016-05-05 18:08:00 -07001441 // Contacts
1442 OP_READ_CONTACTS,
1443 OP_WRITE_CONTACTS,
1444 OP_GET_ACCOUNTS,
1445 // Calendar
1446 OP_READ_CALENDAR,
1447 OP_WRITE_CALENDAR,
1448 // SMS
1449 OP_SEND_SMS,
1450 OP_RECEIVE_SMS,
1451 OP_READ_SMS,
1452 OP_RECEIVE_WAP_PUSH,
1453 OP_RECEIVE_MMS,
1454 OP_READ_CELL_BROADCASTS,
1455 // Storage
1456 OP_READ_EXTERNAL_STORAGE,
1457 OP_WRITE_EXTERNAL_STORAGE,
Philip P. Moltmann89b044f2019-09-13 15:12:34 -07001458 OP_ACCESS_MEDIA_LOCATION,
Svetoslav Ganoveaca4c52016-05-05 18:08:00 -07001459 // Location
1460 OP_COARSE_LOCATION,
1461 OP_FINE_LOCATION,
1462 // Phone
1463 OP_READ_PHONE_STATE,
Chad Brubaker0c1651f2017-03-30 16:29:10 -07001464 OP_READ_PHONE_NUMBERS,
Svetoslav Ganoveaca4c52016-05-05 18:08:00 -07001465 OP_CALL_PHONE,
1466 OP_READ_CALL_LOG,
1467 OP_WRITE_CALL_LOG,
1468 OP_ADD_VOICEMAIL,
1469 OP_USE_SIP,
1470 OP_PROCESS_OUTGOING_CALLS,
Eugene Suslacae3d3e2017-01-31 11:08:11 -08001471 OP_ANSWER_PHONE_CALLS,
Tyler Gunn79bc1ec2018-01-22 15:17:54 -08001472 OP_ACCEPT_HANDOVER,
Svetoslav Ganoveaca4c52016-05-05 18:08:00 -07001473 // Microphone
1474 OP_RECORD_AUDIO,
1475 // Camera
1476 OP_CAMERA,
1477 // Body sensors
Svet Ganovda0acdf2017-02-15 10:28:51 -08001478 OP_BODY_SENSORS,
Zimuzo6cbf9cc2018-10-05 12:05:58 +01001479 // Activity recognition
1480 OP_ACTIVITY_RECOGNITION,
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07001481 // Aural
1482 OP_READ_MEDIA_AUDIO,
1483 OP_WRITE_MEDIA_AUDIO,
1484 // Visual
1485 OP_READ_MEDIA_VIDEO,
1486 OP_WRITE_MEDIA_VIDEO,
1487 OP_READ_MEDIA_IMAGES,
1488 OP_WRITE_MEDIA_IMAGES,
Svet Ganovda0acdf2017-02-15 10:28:51 -08001489
1490 // APPOP PERMISSIONS
1491 OP_ACCESS_NOTIFICATIONS,
1492 OP_SYSTEM_ALERT_WINDOW,
1493 OP_WRITE_SETTINGS,
1494 OP_REQUEST_INSTALL_PACKAGES,
Dianne Hackborne9d9b4b2018-03-28 13:51:46 -07001495 OP_START_FOREGROUND,
Hongming Jin228cd012018-11-09 14:47:50 -08001496 OP_SMS_FINANCIAL_TRANSACTIONS,
Philip P. Moltmann4b38fbe2019-09-13 21:33:07 -07001497 OP_MANAGE_IPSEC_TUNNELS,
shafik77cabc72020-01-03 16:22:50 +00001498 OP_INSTANT_APP_START_FOREGROUND,
1499 OP_MANAGE_EXTERNAL_STORAGE,
Todd Kennedyb0948d42020-01-27 15:20:07 -08001500 OP_INTERACT_ACROSS_PROFILES,
1501 OP_LOADER_USAGE_STATS,
Svetoslav Ganoveaca4c52016-05-05 18:08:00 -07001502 };
1503
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001504 /**
1505 * This maps each operation to the operation that serves as the
1506 * switch to determine whether it is allowed. Generally this is
1507 * a 1:1 mapping, but for some things (like location) that have
1508 * multiple low-level operations being tracked that should be
David Christie0b837452013-07-29 16:02:13 -07001509 * presented to the user as one switch then this can be used to
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001510 * make them all controlled by the same single operation.
1511 */
1512 private static int[] sOpToSwitch = new int[] {
Dianne Hackbornc216a262018-04-26 13:46:22 -07001513 OP_COARSE_LOCATION, // COARSE_LOCATION
1514 OP_COARSE_LOCATION, // FINE_LOCATION
1515 OP_COARSE_LOCATION, // GPS
1516 OP_VIBRATE, // VIBRATE
1517 OP_READ_CONTACTS, // READ_CONTACTS
1518 OP_WRITE_CONTACTS, // WRITE_CONTACTS
1519 OP_READ_CALL_LOG, // READ_CALL_LOG
1520 OP_WRITE_CALL_LOG, // WRITE_CALL_LOG
1521 OP_READ_CALENDAR, // READ_CALENDAR
1522 OP_WRITE_CALENDAR, // WRITE_CALENDAR
1523 OP_COARSE_LOCATION, // WIFI_SCAN
1524 OP_POST_NOTIFICATION, // POST_NOTIFICATION
1525 OP_COARSE_LOCATION, // NEIGHBORING_CELLS
1526 OP_CALL_PHONE, // CALL_PHONE
1527 OP_READ_SMS, // READ_SMS
1528 OP_WRITE_SMS, // WRITE_SMS
1529 OP_RECEIVE_SMS, // RECEIVE_SMS
1530 OP_RECEIVE_SMS, // RECEIVE_EMERGECY_SMS
1531 OP_RECEIVE_MMS, // RECEIVE_MMS
1532 OP_RECEIVE_WAP_PUSH, // RECEIVE_WAP_PUSH
1533 OP_SEND_SMS, // SEND_SMS
1534 OP_READ_SMS, // READ_ICC_SMS
1535 OP_WRITE_SMS, // WRITE_ICC_SMS
1536 OP_WRITE_SETTINGS, // WRITE_SETTINGS
1537 OP_SYSTEM_ALERT_WINDOW, // SYSTEM_ALERT_WINDOW
1538 OP_ACCESS_NOTIFICATIONS, // ACCESS_NOTIFICATIONS
1539 OP_CAMERA, // CAMERA
1540 OP_RECORD_AUDIO, // RECORD_AUDIO
1541 OP_PLAY_AUDIO, // PLAY_AUDIO
1542 OP_READ_CLIPBOARD, // READ_CLIPBOARD
1543 OP_WRITE_CLIPBOARD, // WRITE_CLIPBOARD
1544 OP_TAKE_MEDIA_BUTTONS, // TAKE_MEDIA_BUTTONS
1545 OP_TAKE_AUDIO_FOCUS, // TAKE_AUDIO_FOCUS
1546 OP_AUDIO_MASTER_VOLUME, // AUDIO_MASTER_VOLUME
1547 OP_AUDIO_VOICE_VOLUME, // AUDIO_VOICE_VOLUME
1548 OP_AUDIO_RING_VOLUME, // AUDIO_RING_VOLUME
1549 OP_AUDIO_MEDIA_VOLUME, // AUDIO_MEDIA_VOLUME
1550 OP_AUDIO_ALARM_VOLUME, // AUDIO_ALARM_VOLUME
1551 OP_AUDIO_NOTIFICATION_VOLUME, // AUDIO_NOTIFICATION_VOLUME
1552 OP_AUDIO_BLUETOOTH_VOLUME, // AUDIO_BLUETOOTH_VOLUME
1553 OP_WAKE_LOCK, // WAKE_LOCK
1554 OP_COARSE_LOCATION, // MONITOR_LOCATION
1555 OP_COARSE_LOCATION, // MONITOR_HIGH_POWER_LOCATION
1556 OP_GET_USAGE_STATS, // GET_USAGE_STATS
1557 OP_MUTE_MICROPHONE, // MUTE_MICROPHONE
1558 OP_TOAST_WINDOW, // TOAST_WINDOW
1559 OP_PROJECT_MEDIA, // PROJECT_MEDIA
1560 OP_ACTIVATE_VPN, // ACTIVATE_VPN
1561 OP_WRITE_WALLPAPER, // WRITE_WALLPAPER
1562 OP_ASSIST_STRUCTURE, // ASSIST_STRUCTURE
1563 OP_ASSIST_SCREENSHOT, // ASSIST_SCREENSHOT
1564 OP_READ_PHONE_STATE, // READ_PHONE_STATE
1565 OP_ADD_VOICEMAIL, // ADD_VOICEMAIL
1566 OP_USE_SIP, // USE_SIP
1567 OP_PROCESS_OUTGOING_CALLS, // PROCESS_OUTGOING_CALLS
1568 OP_USE_FINGERPRINT, // USE_FINGERPRINT
1569 OP_BODY_SENSORS, // BODY_SENSORS
1570 OP_READ_CELL_BROADCASTS, // READ_CELL_BROADCASTS
1571 OP_MOCK_LOCATION, // MOCK_LOCATION
1572 OP_READ_EXTERNAL_STORAGE, // READ_EXTERNAL_STORAGE
1573 OP_WRITE_EXTERNAL_STORAGE, // WRITE_EXTERNAL_STORAGE
1574 OP_TURN_SCREEN_ON, // TURN_SCREEN_ON
1575 OP_GET_ACCOUNTS, // GET_ACCOUNTS
1576 OP_RUN_IN_BACKGROUND, // RUN_IN_BACKGROUND
1577 OP_AUDIO_ACCESSIBILITY_VOLUME, // AUDIO_ACCESSIBILITY_VOLUME
1578 OP_READ_PHONE_NUMBERS, // READ_PHONE_NUMBERS
1579 OP_REQUEST_INSTALL_PACKAGES, // REQUEST_INSTALL_PACKAGES
1580 OP_PICTURE_IN_PICTURE, // ENTER_PICTURE_IN_PICTURE_ON_HIDE
1581 OP_INSTANT_APP_START_FOREGROUND, // INSTANT_APP_START_FOREGROUND
1582 OP_ANSWER_PHONE_CALLS, // ANSWER_PHONE_CALLS
1583 OP_RUN_ANY_IN_BACKGROUND, // OP_RUN_ANY_IN_BACKGROUND
1584 OP_CHANGE_WIFI_STATE, // OP_CHANGE_WIFI_STATE
1585 OP_REQUEST_DELETE_PACKAGES, // OP_REQUEST_DELETE_PACKAGES
1586 OP_BIND_ACCESSIBILITY_SERVICE, // OP_BIND_ACCESSIBILITY_SERVICE
1587 OP_ACCEPT_HANDOVER, // ACCEPT_HANDOVER
1588 OP_MANAGE_IPSEC_TUNNELS, // MANAGE_IPSEC_HANDOVERS
1589 OP_START_FOREGROUND, // START_FOREGROUND
Dianne Hackborne04f13d2018-05-02 12:51:52 -07001590 OP_COARSE_LOCATION, // BLUETOOTH_SCAN
Kevin Chynb3c05aa2018-09-21 16:50:32 -07001591 OP_USE_BIOMETRIC, // BIOMETRIC
Zimuzo6cbf9cc2018-10-05 12:05:58 +01001592 OP_ACTIVITY_RECOGNITION, // ACTIVITY_RECOGNITION
Hongming Jin228cd012018-11-09 14:47:50 -08001593 OP_SMS_FINANCIAL_TRANSACTIONS, // SMS_FINANCIAL_TRANSACTIONS
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07001594 OP_READ_MEDIA_AUDIO, // READ_MEDIA_AUDIO
1595 OP_WRITE_MEDIA_AUDIO, // WRITE_MEDIA_AUDIO
1596 OP_READ_MEDIA_VIDEO, // READ_MEDIA_VIDEO
1597 OP_WRITE_MEDIA_VIDEO, // WRITE_MEDIA_VIDEO
1598 OP_READ_MEDIA_IMAGES, // READ_MEDIA_IMAGES
1599 OP_WRITE_MEDIA_IMAGES, // WRITE_MEDIA_IMAGES
Jeff Sharkeye82cbb12018-12-06 15:53:11 -07001600 OP_LEGACY_STORAGE, // LEGACY_STORAGE
Jackal Guo8dc791e2019-01-14 10:26:42 +08001601 OP_ACCESS_ACCESSIBILITY, // ACCESS_ACCESSIBILITY
Michael Groover656ef912019-04-09 17:09:57 -07001602 OP_READ_DEVICE_IDENTIFIERS, // READ_DEVICE_IDENTIFIERS
Philip P. Moltmann89b044f2019-09-13 15:12:34 -07001603 OP_ACCESS_MEDIA_LOCATION, // ACCESS_MEDIA_LOCATION
Patrick Baumann6c1c8092019-06-27 14:55:44 -07001604 OP_QUERY_ALL_PACKAGES, // QUERY_ALL_PACKAGES
Philip P. Moltmannb43dfe32019-11-11 14:14:49 -08001605 OP_MANAGE_EXTERNAL_STORAGE, // MANAGE_EXTERNAL_STORAGE
kholoud mohamed20ded1f2019-12-03 17:52:38 +00001606 OP_INTERACT_ACROSS_PROFILES, //INTERACT_ACROSS_PROFILES
Benedict Wong3eb95202019-11-05 12:50:59 -08001607 OP_ACTIVATE_PLATFORM_VPN, // ACTIVATE_PLATFORM_VPN
Todd Kennedyb0948d42020-01-27 15:20:07 -08001608 OP_LOADER_USAGE_STATS, // LOADER_USAGE_STATS
Ricardo Correaacdc8272020-03-24 11:02:54 -07001609 OP_DEPRECATED_1, // deprecated
Eugene Susla5fc2d762020-03-04 13:53:10 -08001610 OP_AUTO_REVOKE_PERMISSIONS_IF_UNUSED, //AUTO_REVOKE_PERMISSIONS_IF_UNUSED
Eugene Susla922cd082020-03-11 12:38:17 -07001611 OP_AUTO_REVOKE_MANAGED_BY_INSTALLER, //OP_AUTO_REVOKE_MANAGED_BY_INSTALLER
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001612 };
1613
1614 /**
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001615 * This maps each operation to the public string constant for it.
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001616 */
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001617 private static String[] sOpToString = new String[]{
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001618 OPSTR_COARSE_LOCATION,
1619 OPSTR_FINE_LOCATION,
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001620 OPSTR_GPS,
1621 OPSTR_VIBRATE,
Svet Ganovb9d71a62015-04-30 10:38:13 -07001622 OPSTR_READ_CONTACTS,
1623 OPSTR_WRITE_CONTACTS,
1624 OPSTR_READ_CALL_LOG,
1625 OPSTR_WRITE_CALL_LOG,
1626 OPSTR_READ_CALENDAR,
1627 OPSTR_WRITE_CALENDAR,
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001628 OPSTR_WIFI_SCAN,
1629 OPSTR_POST_NOTIFICATION,
1630 OPSTR_NEIGHBORING_CELLS,
Svet Ganovb9d71a62015-04-30 10:38:13 -07001631 OPSTR_CALL_PHONE,
1632 OPSTR_READ_SMS,
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001633 OPSTR_WRITE_SMS,
Svet Ganovb9d71a62015-04-30 10:38:13 -07001634 OPSTR_RECEIVE_SMS,
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001635 OPSTR_RECEIVE_EMERGENCY_BROADCAST,
Svet Ganovb9d71a62015-04-30 10:38:13 -07001636 OPSTR_RECEIVE_MMS,
1637 OPSTR_RECEIVE_WAP_PUSH,
1638 OPSTR_SEND_SMS,
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001639 OPSTR_READ_ICC_SMS,
1640 OPSTR_WRITE_ICC_SMS,
Billy Lau24b9c832015-07-20 17:34:09 +01001641 OPSTR_WRITE_SETTINGS,
1642 OPSTR_SYSTEM_ALERT_WINDOW,
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001643 OPSTR_ACCESS_NOTIFICATIONS,
Svet Ganovb9d71a62015-04-30 10:38:13 -07001644 OPSTR_CAMERA,
1645 OPSTR_RECORD_AUDIO,
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001646 OPSTR_PLAY_AUDIO,
1647 OPSTR_READ_CLIPBOARD,
1648 OPSTR_WRITE_CLIPBOARD,
1649 OPSTR_TAKE_MEDIA_BUTTONS,
1650 OPSTR_TAKE_AUDIO_FOCUS,
1651 OPSTR_AUDIO_MASTER_VOLUME,
1652 OPSTR_AUDIO_VOICE_VOLUME,
1653 OPSTR_AUDIO_RING_VOLUME,
1654 OPSTR_AUDIO_MEDIA_VOLUME,
1655 OPSTR_AUDIO_ALARM_VOLUME,
1656 OPSTR_AUDIO_NOTIFICATION_VOLUME,
1657 OPSTR_AUDIO_BLUETOOTH_VOLUME,
1658 OPSTR_WAKE_LOCK,
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001659 OPSTR_MONITOR_LOCATION,
1660 OPSTR_MONITOR_HIGH_POWER_LOCATION,
Dianne Hackborn5064e7c2014-09-02 10:57:16 -07001661 OPSTR_GET_USAGE_STATS,
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001662 OPSTR_MUTE_MICROPHONE,
1663 OPSTR_TOAST_WINDOW,
1664 OPSTR_PROJECT_MEDIA,
Jeff Davidson05542602014-08-11 14:07:27 -07001665 OPSTR_ACTIVATE_VPN,
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001666 OPSTR_WRITE_WALLPAPER,
1667 OPSTR_ASSIST_STRUCTURE,
1668 OPSTR_ASSIST_SCREENSHOT,
Svet Ganovb9d71a62015-04-30 10:38:13 -07001669 OPSTR_READ_PHONE_STATE,
1670 OPSTR_ADD_VOICEMAIL,
1671 OPSTR_USE_SIP,
Svet Ganov824ad6e2016-09-22 19:36:53 -07001672 OPSTR_PROCESS_OUTGOING_CALLS,
Svet Ganovb9d71a62015-04-30 10:38:13 -07001673 OPSTR_USE_FINGERPRINT,
Svet Ganovede43162015-05-02 17:42:44 -07001674 OPSTR_BODY_SENSORS,
Svet Ganovf7e9cf42015-05-13 10:40:31 -07001675 OPSTR_READ_CELL_BROADCASTS,
Svet Ganov921c7df2015-06-29 21:51:41 -07001676 OPSTR_MOCK_LOCATION,
1677 OPSTR_READ_EXTERNAL_STORAGE,
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001678 OPSTR_WRITE_EXTERNAL_STORAGE,
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001679 OPSTR_TURN_SCREEN_ON,
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07001680 OPSTR_GET_ACCOUNTS,
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001681 OPSTR_RUN_IN_BACKGROUND,
1682 OPSTR_AUDIO_ACCESSIBILITY_VOLUME,
Chad Brubaker0c1651f2017-03-30 16:29:10 -07001683 OPSTR_READ_PHONE_NUMBERS,
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001684 OPSTR_REQUEST_INSTALL_PACKAGES,
Winson Chungf4ac0632017-03-17 12:34:12 -07001685 OPSTR_PICTURE_IN_PICTURE,
Chad Brubaker97b383f2017-02-02 15:04:35 -08001686 OPSTR_INSTANT_APP_START_FOREGROUND,
Eugene Suslacae3d3e2017-01-31 11:08:11 -08001687 OPSTR_ANSWER_PHONE_CALLS,
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00001688 OPSTR_RUN_ANY_IN_BACKGROUND,
1689 OPSTR_CHANGE_WIFI_STATE,
1690 OPSTR_REQUEST_DELETE_PACKAGES,
1691 OPSTR_BIND_ACCESSIBILITY_SERVICE,
Tyler Gunn79bc1ec2018-01-22 15:17:54 -08001692 OPSTR_ACCEPT_HANDOVER,
Nathan Harold1bb420672018-03-14 17:08:53 -07001693 OPSTR_MANAGE_IPSEC_TUNNELS,
Dianne Hackborne9d9b4b2018-03-28 13:51:46 -07001694 OPSTR_START_FOREGROUND,
Dianne Hackborne04f13d2018-05-02 12:51:52 -07001695 OPSTR_BLUETOOTH_SCAN,
Kevin Chynb3c05aa2018-09-21 16:50:32 -07001696 OPSTR_USE_BIOMETRIC,
Zimuzo6cbf9cc2018-10-05 12:05:58 +01001697 OPSTR_ACTIVITY_RECOGNITION,
Hongming Jin228cd012018-11-09 14:47:50 -08001698 OPSTR_SMS_FINANCIAL_TRANSACTIONS,
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07001699 OPSTR_READ_MEDIA_AUDIO,
1700 OPSTR_WRITE_MEDIA_AUDIO,
1701 OPSTR_READ_MEDIA_VIDEO,
1702 OPSTR_WRITE_MEDIA_VIDEO,
1703 OPSTR_READ_MEDIA_IMAGES,
1704 OPSTR_WRITE_MEDIA_IMAGES,
Jeff Sharkeye82cbb12018-12-06 15:53:11 -07001705 OPSTR_LEGACY_STORAGE,
Jackal Guo8dc791e2019-01-14 10:26:42 +08001706 OPSTR_ACCESS_ACCESSIBILITY,
Michael Groover656ef912019-04-09 17:09:57 -07001707 OPSTR_READ_DEVICE_IDENTIFIERS,
Philip P. Moltmann89b044f2019-09-13 15:12:34 -07001708 OPSTR_ACCESS_MEDIA_LOCATION,
Patrick Baumann6c1c8092019-06-27 14:55:44 -07001709 OPSTR_QUERY_ALL_PACKAGES,
Philip P. Moltmannb43dfe32019-11-11 14:14:49 -08001710 OPSTR_MANAGE_EXTERNAL_STORAGE,
kholoud mohamed20ded1f2019-12-03 17:52:38 +00001711 OPSTR_INTERACT_ACROSS_PROFILES,
Benedict Wong3eb95202019-11-05 12:50:59 -08001712 OPSTR_ACTIVATE_PLATFORM_VPN,
Todd Kennedyb0948d42020-01-27 15:20:07 -08001713 OPSTR_LOADER_USAGE_STATS,
Ricardo Correaacdc8272020-03-24 11:02:54 -07001714 "", // deprecated
Eugene Susla5fc2d762020-03-04 13:53:10 -08001715 OPSTR_AUTO_REVOKE_PERMISSIONS_IF_UNUSED,
Eugene Susla1d7a7912020-03-14 09:16:29 -07001716 OPSTR_AUTO_REVOKE_MANAGED_BY_INSTALLER,
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001717 };
1718
1719 /**
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001720 * This provides a simple name for each operation to be used
1721 * in debug output.
1722 */
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001723 private static String[] sOpNames = new String[] {
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001724 "COARSE_LOCATION",
1725 "FINE_LOCATION",
1726 "GPS",
1727 "VIBRATE",
1728 "READ_CONTACTS",
1729 "WRITE_CONTACTS",
1730 "READ_CALL_LOG",
1731 "WRITE_CALL_LOG",
1732 "READ_CALENDAR",
1733 "WRITE_CALENDAR",
1734 "WIFI_SCAN",
1735 "POST_NOTIFICATION",
1736 "NEIGHBORING_CELLS",
1737 "CALL_PHONE",
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001738 "READ_SMS",
1739 "WRITE_SMS",
1740 "RECEIVE_SMS",
1741 "RECEIVE_EMERGECY_SMS",
1742 "RECEIVE_MMS",
1743 "RECEIVE_WAP_PUSH",
1744 "SEND_SMS",
1745 "READ_ICC_SMS",
1746 "WRITE_ICC_SMS",
Dianne Hackborn961321f2013-02-05 17:22:41 -08001747 "WRITE_SETTINGS",
Dianne Hackbornc2293022013-02-06 23:14:49 -08001748 "SYSTEM_ALERT_WINDOW",
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001749 "ACCESS_NOTIFICATIONS",
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08001750 "CAMERA",
1751 "RECORD_AUDIO",
1752 "PLAY_AUDIO",
Dianne Hackbornefcc1a22013-02-25 18:02:35 -08001753 "READ_CLIPBOARD",
1754 "WRITE_CLIPBOARD",
Dianne Hackbornba50b97c2013-04-30 15:04:46 -07001755 "TAKE_MEDIA_BUTTONS",
1756 "TAKE_AUDIO_FOCUS",
1757 "AUDIO_MASTER_VOLUME",
1758 "AUDIO_VOICE_VOLUME",
1759 "AUDIO_RING_VOLUME",
1760 "AUDIO_MEDIA_VOLUME",
1761 "AUDIO_ALARM_VOLUME",
1762 "AUDIO_NOTIFICATION_VOLUME",
1763 "AUDIO_BLUETOOTH_VOLUME",
Dianne Hackborn713df152013-05-17 11:27:57 -07001764 "WAKE_LOCK",
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001765 "MONITOR_LOCATION",
David Christie0b837452013-07-29 16:02:13 -07001766 "MONITOR_HIGH_POWER_LOCATION",
Emily Bernier22c921a2014-05-28 11:01:32 -04001767 "GET_USAGE_STATS",
Michael Wrightc39d47a2014-07-08 18:07:36 -07001768 "MUTE_MICROPHONE",
Jason Monk1c7c3192014-06-26 12:52:18 -04001769 "TOAST_WINDOW",
Michael Wrightc39d47a2014-07-08 18:07:36 -07001770 "PROJECT_MEDIA",
Jeff Davidson05542602014-08-11 14:07:27 -07001771 "ACTIVATE_VPN",
Benjamin Franzf3ece362015-02-11 10:51:10 +00001772 "WRITE_WALLPAPER",
Dianne Hackbornd59a5d52015-04-04 14:52:14 -07001773 "ASSIST_STRUCTURE",
Svet Ganov16a16892015-04-16 10:32:04 -07001774 "ASSIST_SCREENSHOT",
Eugene Suslae4ee2c22018-11-05 12:23:30 -08001775 "READ_PHONE_STATE",
Svetoslav5335b672015-04-29 12:00:51 -07001776 "ADD_VOICEMAIL",
Svetoslavc656e6f2015-04-29 14:08:16 -07001777 "USE_SIP",
Svetoslav4af76a52015-04-29 15:29:46 -07001778 "PROCESS_OUTGOING_CALLS",
Svet Ganovb9d71a62015-04-30 10:38:13 -07001779 "USE_FINGERPRINT",
Svet Ganovede43162015-05-02 17:42:44 -07001780 "BODY_SENSORS",
Svet Ganovf7e9cf42015-05-13 10:40:31 -07001781 "READ_CELL_BROADCASTS",
Svet Ganov921c7df2015-06-29 21:51:41 -07001782 "MOCK_LOCATION",
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001783 "READ_EXTERNAL_STORAGE",
1784 "WRITE_EXTERNAL_STORAGE",
1785 "TURN_ON_SCREEN",
Svetoslavf3f02ac2015-09-08 14:36:35 -07001786 "GET_ACCOUNTS",
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07001787 "RUN_IN_BACKGROUND",
Jean-Michel Trivi3f0945a2016-11-11 10:05:18 -08001788 "AUDIO_ACCESSIBILITY_VOLUME",
Chad Brubaker0c1651f2017-03-30 16:29:10 -07001789 "READ_PHONE_NUMBERS",
Suprabh Shukla2f34b1a2016-12-16 14:47:25 -08001790 "REQUEST_INSTALL_PACKAGES",
Winson Chungf4ac0632017-03-17 12:34:12 -07001791 "PICTURE_IN_PICTURE",
Chad Brubaker97b383f2017-02-02 15:04:35 -08001792 "INSTANT_APP_START_FOREGROUND",
Eugene Suslacae3d3e2017-01-31 11:08:11 -08001793 "ANSWER_PHONE_CALLS",
Suprabh Shukla3ac1daa2017-07-14 12:15:27 -07001794 "RUN_ANY_IN_BACKGROUND",
Peter Visontay1246d9e2017-10-17 17:02:45 +01001795 "CHANGE_WIFI_STATE",
Peter Visontayf2e38362017-11-27 15:27:16 +00001796 "REQUEST_DELETE_PACKAGES",
Peter Visontay11950832017-11-14 19:34:59 +00001797 "BIND_ACCESSIBILITY_SERVICE",
Tyler Gunn79bc1ec2018-01-22 15:17:54 -08001798 "ACCEPT_HANDOVER",
Nathan Harold1bb420672018-03-14 17:08:53 -07001799 "MANAGE_IPSEC_TUNNELS",
Dianne Hackborne9d9b4b2018-03-28 13:51:46 -07001800 "START_FOREGROUND",
Dianne Hackborne04f13d2018-05-02 12:51:52 -07001801 "BLUETOOTH_SCAN",
Kevin Chynb3c05aa2018-09-21 16:50:32 -07001802 "USE_BIOMETRIC",
Zimuzo6cbf9cc2018-10-05 12:05:58 +01001803 "ACTIVITY_RECOGNITION",
Hongming Jin228cd012018-11-09 14:47:50 -08001804 "SMS_FINANCIAL_TRANSACTIONS",
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07001805 "READ_MEDIA_AUDIO",
1806 "WRITE_MEDIA_AUDIO",
1807 "READ_MEDIA_VIDEO",
1808 "WRITE_MEDIA_VIDEO",
1809 "READ_MEDIA_IMAGES",
1810 "WRITE_MEDIA_IMAGES",
Jeff Sharkeye82cbb12018-12-06 15:53:11 -07001811 "LEGACY_STORAGE",
Jackal Guo8dc791e2019-01-14 10:26:42 +08001812 "ACCESS_ACCESSIBILITY",
Michael Groover656ef912019-04-09 17:09:57 -07001813 "READ_DEVICE_IDENTIFIERS",
Philip P. Moltmann89b044f2019-09-13 15:12:34 -07001814 "ACCESS_MEDIA_LOCATION",
Patrick Baumann6c1c8092019-06-27 14:55:44 -07001815 "QUERY_ALL_PACKAGES",
kholoud mohamed20ded1f2019-12-03 17:52:38 +00001816 "MANAGE_EXTERNAL_STORAGE",
Benedict Wong3eb95202019-11-05 12:50:59 -08001817 "INTERACT_ACROSS_PROFILES",
1818 "ACTIVATE_PLATFORM_VPN",
Todd Kennedyb0948d42020-01-27 15:20:07 -08001819 "LOADER_USAGE_STATS",
Ricardo Correaacdc8272020-03-24 11:02:54 -07001820 "deprecated",
Eugene Susla5fc2d762020-03-04 13:53:10 -08001821 "AUTO_REVOKE_PERMISSIONS_IF_UNUSED",
Eugene Susla1d7a7912020-03-14 09:16:29 -07001822 "AUTO_REVOKE_MANAGED_BY_INSTALLER",
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001823 };
1824
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001825 /**
1826 * This optionally maps a permission to an operation. If there
1827 * is no permission associated with an operation, it is null.
1828 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001829 @UnsupportedAppUsage
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001830 private static String[] sOpPerms = new String[] {
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001831 android.Manifest.permission.ACCESS_COARSE_LOCATION,
1832 android.Manifest.permission.ACCESS_FINE_LOCATION,
1833 null,
1834 android.Manifest.permission.VIBRATE,
1835 android.Manifest.permission.READ_CONTACTS,
1836 android.Manifest.permission.WRITE_CONTACTS,
1837 android.Manifest.permission.READ_CALL_LOG,
1838 android.Manifest.permission.WRITE_CALL_LOG,
1839 android.Manifest.permission.READ_CALENDAR,
1840 android.Manifest.permission.WRITE_CALENDAR,
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001841 android.Manifest.permission.ACCESS_WIFI_STATE,
Robert Craigf97616c2013-10-07 12:32:02 -04001842 null, // no permission required for notifications
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001843 null, // neighboring cells shares the coarse location perm
1844 android.Manifest.permission.CALL_PHONE,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001845 android.Manifest.permission.READ_SMS,
Svetoslav6c589572015-04-16 16:19:24 -07001846 null, // no permission required for writing sms
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001847 android.Manifest.permission.RECEIVE_SMS,
1848 android.Manifest.permission.RECEIVE_EMERGENCY_BROADCAST,
1849 android.Manifest.permission.RECEIVE_MMS,
1850 android.Manifest.permission.RECEIVE_WAP_PUSH,
1851 android.Manifest.permission.SEND_SMS,
1852 android.Manifest.permission.READ_SMS,
Svetoslav6c589572015-04-16 16:19:24 -07001853 null, // no permission required for writing icc sms
Dianne Hackborn961321f2013-02-05 17:22:41 -08001854 android.Manifest.permission.WRITE_SETTINGS,
Dianne Hackbornc2293022013-02-06 23:14:49 -08001855 android.Manifest.permission.SYSTEM_ALERT_WINDOW,
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001856 android.Manifest.permission.ACCESS_NOTIFICATIONS,
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08001857 android.Manifest.permission.CAMERA,
1858 android.Manifest.permission.RECORD_AUDIO,
1859 null, // no permission for playing audio
Dianne Hackbornefcc1a22013-02-25 18:02:35 -08001860 null, // no permission for reading clipboard
1861 null, // no permission for writing clipboard
Dianne Hackbornba50b97c2013-04-30 15:04:46 -07001862 null, // no permission for taking media buttons
1863 null, // no permission for taking audio focus
1864 null, // no permission for changing master volume
1865 null, // no permission for changing voice volume
1866 null, // no permission for changing ring volume
1867 null, // no permission for changing media volume
1868 null, // no permission for changing alarm volume
1869 null, // no permission for changing notification volume
1870 null, // no permission for changing bluetooth volume
Dianne Hackborn713df152013-05-17 11:27:57 -07001871 android.Manifest.permission.WAKE_LOCK,
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001872 null, // no permission for generic location monitoring
David Christie0b837452013-07-29 16:02:13 -07001873 null, // no permission for high power location monitoring
Dianne Hackborne22b3b12014-05-07 18:06:44 -07001874 android.Manifest.permission.PACKAGE_USAGE_STATS,
Emily Bernier22c921a2014-05-28 11:01:32 -04001875 null, // no permission for muting/unmuting microphone
Jason Monk1c7c3192014-06-26 12:52:18 -04001876 null, // no permission for displaying toasts
Michael Wrightc39d47a2014-07-08 18:07:36 -07001877 null, // no permission for projecting media
Jeff Davidson05542602014-08-11 14:07:27 -07001878 null, // no permission for activating vpn
Benjamin Franzf3ece362015-02-11 10:51:10 +00001879 null, // no permission for supporting wallpaper
Dianne Hackbornd59a5d52015-04-04 14:52:14 -07001880 null, // no permission for receiving assist structure
1881 null, // no permission for receiving assist screenshot
Svet Ganovc3300092015-04-17 09:07:22 -07001882 Manifest.permission.READ_PHONE_STATE,
Svetoslav5335b672015-04-29 12:00:51 -07001883 Manifest.permission.ADD_VOICEMAIL,
Svetoslavc656e6f2015-04-29 14:08:16 -07001884 Manifest.permission.USE_SIP,
Svetoslav4af76a52015-04-29 15:29:46 -07001885 Manifest.permission.PROCESS_OUTGOING_CALLS,
Svet Ganovb9d71a62015-04-30 10:38:13 -07001886 Manifest.permission.USE_FINGERPRINT,
Svet Ganovede43162015-05-02 17:42:44 -07001887 Manifest.permission.BODY_SENSORS,
Svet Ganovf7e9cf42015-05-13 10:40:31 -07001888 Manifest.permission.READ_CELL_BROADCASTS,
Svet Ganov921c7df2015-06-29 21:51:41 -07001889 null,
1890 Manifest.permission.READ_EXTERNAL_STORAGE,
1891 Manifest.permission.WRITE_EXTERNAL_STORAGE,
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001892 null, // no permission for turning the screen on
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07001893 Manifest.permission.GET_ACCOUNTS,
1894 null, // no permission for running in background
Jean-Michel Trivi3f0945a2016-11-11 10:05:18 -08001895 null, // no permission for changing accessibility volume
Chad Brubaker0c1651f2017-03-30 16:29:10 -07001896 Manifest.permission.READ_PHONE_NUMBERS,
Suprabh Shukla2f34b1a2016-12-16 14:47:25 -08001897 Manifest.permission.REQUEST_INSTALL_PACKAGES,
Winson Chung59fda9e2017-01-20 16:14:51 -08001898 null, // no permission for entering picture-in-picture on hide
Chad Brubaker97b383f2017-02-02 15:04:35 -08001899 Manifest.permission.INSTANT_APP_FOREGROUND_SERVICE,
Eugene Suslacae3d3e2017-01-31 11:08:11 -08001900 Manifest.permission.ANSWER_PHONE_CALLS,
Suprabh Shukla3ac1daa2017-07-14 12:15:27 -07001901 null, // no permission for OP_RUN_ANY_IN_BACKGROUND
Peter Visontay1246d9e2017-10-17 17:02:45 +01001902 Manifest.permission.CHANGE_WIFI_STATE,
Peter Visontayf2e38362017-11-27 15:27:16 +00001903 Manifest.permission.REQUEST_DELETE_PACKAGES,
Peter Visontay11950832017-11-14 19:34:59 +00001904 Manifest.permission.BIND_ACCESSIBILITY_SERVICE,
Tyler Gunn79bc1ec2018-01-22 15:17:54 -08001905 Manifest.permission.ACCEPT_HANDOVER,
Philip P. Moltmann4b38fbe2019-09-13 21:33:07 -07001906 Manifest.permission.MANAGE_IPSEC_TUNNELS,
Dianne Hackborne9d9b4b2018-03-28 13:51:46 -07001907 Manifest.permission.FOREGROUND_SERVICE,
Dianne Hackborne04f13d2018-05-02 12:51:52 -07001908 null, // no permission for OP_BLUETOOTH_SCAN
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001909 Manifest.permission.USE_BIOMETRIC,
Zimuzo6cbf9cc2018-10-05 12:05:58 +01001910 Manifest.permission.ACTIVITY_RECOGNITION,
Hongming Jin228cd012018-11-09 14:47:50 -08001911 Manifest.permission.SMS_FINANCIAL_TRANSACTIONS,
Philip P. Moltmann129a0b02019-03-27 12:24:45 -07001912 null,
Jeff Sharkey9787a9452018-11-18 17:53:02 -07001913 null, // no permission for OP_WRITE_MEDIA_AUDIO
Philip P. Moltmann129a0b02019-03-27 12:24:45 -07001914 null,
Jeff Sharkey9787a9452018-11-18 17:53:02 -07001915 null, // no permission for OP_WRITE_MEDIA_VIDEO
Philip P. Moltmann129a0b02019-03-27 12:24:45 -07001916 null,
Jeff Sharkey9787a9452018-11-18 17:53:02 -07001917 null, // no permission for OP_WRITE_MEDIA_IMAGES
Jeff Sharkeye82cbb12018-12-06 15:53:11 -07001918 null, // no permission for OP_LEGACY_STORAGE
Jackal Guo8dc791e2019-01-14 10:26:42 +08001919 null, // no permission for OP_ACCESS_ACCESSIBILITY
Michael Groover656ef912019-04-09 17:09:57 -07001920 null, // no direct permission for OP_READ_DEVICE_IDENTIFIERS
Philip P. Moltmann89b044f2019-09-13 15:12:34 -07001921 Manifest.permission.ACCESS_MEDIA_LOCATION,
Patrick Baumann6c1c8092019-06-27 14:55:44 -07001922 null, // no permission for OP_QUERY_ALL_PACKAGES
shafik77cabc72020-01-03 16:22:50 +00001923 Manifest.permission.MANAGE_EXTERNAL_STORAGE,
kholoud mohamed20ded1f2019-12-03 17:52:38 +00001924 android.Manifest.permission.INTERACT_ACROSS_PROFILES,
Benedict Wong3eb95202019-11-05 12:50:59 -08001925 null, // no permission for OP_ACTIVATE_PLATFORM_VPN
Todd Kennedyb0948d42020-01-27 15:20:07 -08001926 android.Manifest.permission.LOADER_USAGE_STATS,
Ricardo Correaacdc8272020-03-24 11:02:54 -07001927 null, // deprecated operation
Eugene Susla5fc2d762020-03-04 13:53:10 -08001928 null, // no permission for OP_AUTO_REVOKE_PERMISSIONS_IF_UNUSED
Eugene Susla922cd082020-03-11 12:38:17 -07001929 null, // no permission for OP_AUTO_REVOKE_MANAGED_BY_INSTALLER
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001930 };
1931
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08001932 /**
Jason Monk62062992014-05-06 09:55:28 -04001933 * Specifies whether an Op should be restricted by a user restriction.
1934 * Each Op should be filled with a restriction string from UserManager or
1935 * null to specify it is not affected by any user restriction.
1936 */
1937 private static String[] sOpRestrictions = new String[] {
Julia Reynolds9854d572014-07-02 14:46:02 -04001938 UserManager.DISALLOW_SHARE_LOCATION, //COARSE_LOCATION
1939 UserManager.DISALLOW_SHARE_LOCATION, //FINE_LOCATION
1940 UserManager.DISALLOW_SHARE_LOCATION, //GPS
Jason Monk62062992014-05-06 09:55:28 -04001941 null, //VIBRATE
1942 null, //READ_CONTACTS
1943 null, //WRITE_CONTACTS
Yorke Lee15f83c62014-08-13 14:14:29 -07001944 UserManager.DISALLOW_OUTGOING_CALLS, //READ_CALL_LOG
1945 UserManager.DISALLOW_OUTGOING_CALLS, //WRITE_CALL_LOG
Jason Monk62062992014-05-06 09:55:28 -04001946 null, //READ_CALENDAR
1947 null, //WRITE_CALENDAR
Julia Reynolds9854d572014-07-02 14:46:02 -04001948 UserManager.DISALLOW_SHARE_LOCATION, //WIFI_SCAN
Jason Monk62062992014-05-06 09:55:28 -04001949 null, //POST_NOTIFICATION
1950 null, //NEIGHBORING_CELLS
1951 null, //CALL_PHONE
Amith Yamasani41c1ded2014-08-05 11:15:05 -07001952 UserManager.DISALLOW_SMS, //READ_SMS
1953 UserManager.DISALLOW_SMS, //WRITE_SMS
1954 UserManager.DISALLOW_SMS, //RECEIVE_SMS
1955 null, //RECEIVE_EMERGENCY_SMS
1956 UserManager.DISALLOW_SMS, //RECEIVE_MMS
Jason Monk62062992014-05-06 09:55:28 -04001957 null, //RECEIVE_WAP_PUSH
Amith Yamasani41c1ded2014-08-05 11:15:05 -07001958 UserManager.DISALLOW_SMS, //SEND_SMS
1959 UserManager.DISALLOW_SMS, //READ_ICC_SMS
1960 UserManager.DISALLOW_SMS, //WRITE_ICC_SMS
Jason Monk62062992014-05-06 09:55:28 -04001961 null, //WRITE_SETTINGS
Jason Monk1c7c3192014-06-26 12:52:18 -04001962 UserManager.DISALLOW_CREATE_WINDOWS, //SYSTEM_ALERT_WINDOW
Jason Monk62062992014-05-06 09:55:28 -04001963 null, //ACCESS_NOTIFICATIONS
Makoto Onuki759a7632015-10-28 16:43:10 -07001964 UserManager.DISALLOW_CAMERA, //CAMERA
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001965 UserManager.DISALLOW_RECORD_AUDIO, //RECORD_AUDIO
Jason Monk62062992014-05-06 09:55:28 -04001966 null, //PLAY_AUDIO
1967 null, //READ_CLIPBOARD
1968 null, //WRITE_CLIPBOARD
1969 null, //TAKE_MEDIA_BUTTONS
1970 null, //TAKE_AUDIO_FOCUS
Emily Bernier45775c42014-05-16 15:12:04 -04001971 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_MASTER_VOLUME
1972 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_VOICE_VOLUME
1973 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_RING_VOLUME
1974 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_MEDIA_VOLUME
1975 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_ALARM_VOLUME
1976 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_NOTIFICATION_VOLUME
1977 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_BLUETOOTH_VOLUME
Jason Monk62062992014-05-06 09:55:28 -04001978 null, //WAKE_LOCK
Julia Reynolds9854d572014-07-02 14:46:02 -04001979 UserManager.DISALLOW_SHARE_LOCATION, //MONITOR_LOCATION
1980 UserManager.DISALLOW_SHARE_LOCATION, //MONITOR_HIGH_POWER_LOCATION
Jason Monk62062992014-05-06 09:55:28 -04001981 null, //GET_USAGE_STATS
Emily Bernier22c921a2014-05-28 11:01:32 -04001982 UserManager.DISALLOW_UNMUTE_MICROPHONE, // MUTE_MICROPHONE
Jason Monk1c7c3192014-06-26 12:52:18 -04001983 UserManager.DISALLOW_CREATE_WINDOWS, // TOAST_WINDOW
Michael Wrightc39d47a2014-07-08 18:07:36 -07001984 null, //PROJECT_MEDIA
Tony Mak33d03a92016-06-02 15:01:16 +01001985 null, // ACTIVATE_VPN
Benjamin Franzf3ece362015-02-11 10:51:10 +00001986 UserManager.DISALLOW_WALLPAPER, // WRITE_WALLPAPER
Dianne Hackbornd59a5d52015-04-04 14:52:14 -07001987 null, // ASSIST_STRUCTURE
1988 null, // ASSIST_SCREENSHOT
Svet Ganovc3300092015-04-17 09:07:22 -07001989 null, // READ_PHONE_STATE
Svetoslav5335b672015-04-29 12:00:51 -07001990 null, // ADD_VOICEMAIL
Svetoslavc656e6f2015-04-29 14:08:16 -07001991 null, // USE_SIP
Svetoslav4af76a52015-04-29 15:29:46 -07001992 null, // PROCESS_OUTGOING_CALLS
Svet Ganovb9d71a62015-04-30 10:38:13 -07001993 null, // USE_FINGERPRINT
Svet Ganovede43162015-05-02 17:42:44 -07001994 null, // BODY_SENSORS
Svet Ganovf7e9cf42015-05-13 10:40:31 -07001995 null, // READ_CELL_BROADCASTS
Svet Ganov921c7df2015-06-29 21:51:41 -07001996 null, // MOCK_LOCATION
1997 null, // READ_EXTERNAL_STORAGE
Dianne Hackborn280a64e2015-07-13 14:48:08 -07001998 null, // WRITE_EXTERNAL_STORAGE
1999 null, // TURN_ON_SCREEN
Svetoslavf3f02ac2015-09-08 14:36:35 -07002000 null, // GET_ACCOUNTS
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07002001 null, // RUN_IN_BACKGROUND
Jean-Michel Trivi3f0945a2016-11-11 10:05:18 -08002002 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_ACCESSIBILITY_VOLUME
Chad Brubaker0c1651f2017-03-30 16:29:10 -07002003 null, // READ_PHONE_NUMBERS
Suprabh Shukla2f34b1a2016-12-16 14:47:25 -08002004 null, // REQUEST_INSTALL_PACKAGES
Winson Chung59fda9e2017-01-20 16:14:51 -08002005 null, // ENTER_PICTURE_IN_PICTURE_ON_HIDE
Chad Brubaker97b383f2017-02-02 15:04:35 -08002006 null, // INSTANT_APP_START_FOREGROUND
Eugene Suslacae3d3e2017-01-31 11:08:11 -08002007 null, // ANSWER_PHONE_CALLS
Suprabh Shukla3ac1daa2017-07-14 12:15:27 -07002008 null, // OP_RUN_ANY_IN_BACKGROUND
Peter Visontay1246d9e2017-10-17 17:02:45 +01002009 null, // OP_CHANGE_WIFI_STATE
Peter Visontayf2e38362017-11-27 15:27:16 +00002010 null, // REQUEST_DELETE_PACKAGES
Peter Visontay11950832017-11-14 19:34:59 +00002011 null, // OP_BIND_ACCESSIBILITY_SERVICE
Tyler Gunn79bc1ec2018-01-22 15:17:54 -08002012 null, // ACCEPT_HANDOVER
Nathan Harold1bb420672018-03-14 17:08:53 -07002013 null, // MANAGE_IPSEC_TUNNELS
Dianne Hackborne9d9b4b2018-03-28 13:51:46 -07002014 null, // START_FOREGROUND
Dianne Hackborne04f13d2018-05-02 12:51:52 -07002015 null, // maybe should be UserManager.DISALLOW_SHARE_LOCATION, //BLUETOOTH_SCAN
Kevin Chynb3c05aa2018-09-21 16:50:32 -07002016 null, // USE_BIOMETRIC
Zimuzo6cbf9cc2018-10-05 12:05:58 +01002017 null, // ACTIVITY_RECOGNITION
Hongming Jin228cd012018-11-09 14:47:50 -08002018 UserManager.DISALLOW_SMS, // SMS_FINANCIAL_TRANSACTIONS
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07002019 null, // READ_MEDIA_AUDIO
2020 null, // WRITE_MEDIA_AUDIO
2021 null, // READ_MEDIA_VIDEO
2022 null, // WRITE_MEDIA_VIDEO
2023 null, // READ_MEDIA_IMAGES
2024 null, // WRITE_MEDIA_IMAGES
Jeff Sharkeye82cbb12018-12-06 15:53:11 -07002025 null, // LEGACY_STORAGE
Jackal Guo8dc791e2019-01-14 10:26:42 +08002026 null, // ACCESS_ACCESSIBILITY
Michael Groover656ef912019-04-09 17:09:57 -07002027 null, // READ_DEVICE_IDENTIFIERS
Philip P. Moltmann89b044f2019-09-13 15:12:34 -07002028 null, // ACCESS_MEDIA_LOCATION
Patrick Baumann6c1c8092019-06-27 14:55:44 -07002029 null, // QUERY_ALL_PACKAGES
Philip P. Moltmannb43dfe32019-11-11 14:14:49 -08002030 null, // MANAGE_EXTERNAL_STORAGE
kholoud mohamed20ded1f2019-12-03 17:52:38 +00002031 null, // INTERACT_ACROSS_PROFILES
Benedict Wong3eb95202019-11-05 12:50:59 -08002032 null, // ACTIVATE_PLATFORM_VPN
Todd Kennedyb0948d42020-01-27 15:20:07 -08002033 null, // LOADER_USAGE_STATS
Ricardo Correaacdc8272020-03-24 11:02:54 -07002034 null, // deprecated operation
Eugene Susla5fc2d762020-03-04 13:53:10 -08002035 null, // AUTO_REVOKE_PERMISSIONS_IF_UNUSED
Eugene Susla1d7a7912020-03-14 09:16:29 -07002036 null, // AUTO_REVOKE_MANAGED_BY_INSTALLER
Jason Monk1c7c3192014-06-26 12:52:18 -04002037 };
2038
2039 /**
Philip P. Moltmannad787aa2020-03-10 09:49:22 -07002040 * In which cases should an app be allowed to bypass the {@link #setUserRestriction user
2041 * restriction} for a certain app-op.
Jason Monk1c7c3192014-06-26 12:52:18 -04002042 */
Philip P. Moltmannad787aa2020-03-10 09:49:22 -07002043 private static RestrictionBypass[] sOpAllowSystemRestrictionBypass = new RestrictionBypass[] {
2044 new RestrictionBypass(true, false), //COARSE_LOCATION
2045 new RestrictionBypass(true, false), //FINE_LOCATION
2046 null, //GPS
2047 null, //VIBRATE
2048 null, //READ_CONTACTS
2049 null, //WRITE_CONTACTS
2050 null, //READ_CALL_LOG
2051 null, //WRITE_CALL_LOG
2052 null, //READ_CALENDAR
2053 null, //WRITE_CALENDAR
2054 new RestrictionBypass(true, false), //WIFI_SCAN
2055 null, //POST_NOTIFICATION
2056 null, //NEIGHBORING_CELLS
2057 null, //CALL_PHONE
2058 null, //READ_SMS
2059 null, //WRITE_SMS
2060 null, //RECEIVE_SMS
2061 null, //RECEIVE_EMERGECY_SMS
2062 null, //RECEIVE_MMS
2063 null, //RECEIVE_WAP_PUSH
2064 null, //SEND_SMS
2065 null, //READ_ICC_SMS
2066 null, //WRITE_ICC_SMS
2067 null, //WRITE_SETTINGS
2068 new RestrictionBypass(true, false), //SYSTEM_ALERT_WINDOW
2069 null, //ACCESS_NOTIFICATIONS
2070 null, //CAMERA
2071 new RestrictionBypass(false, true), //RECORD_AUDIO
2072 null, //PLAY_AUDIO
2073 null, //READ_CLIPBOARD
2074 null, //WRITE_CLIPBOARD
2075 null, //TAKE_MEDIA_BUTTONS
2076 null, //TAKE_AUDIO_FOCUS
2077 null, //AUDIO_MASTER_VOLUME
2078 null, //AUDIO_VOICE_VOLUME
2079 null, //AUDIO_RING_VOLUME
2080 null, //AUDIO_MEDIA_VOLUME
2081 null, //AUDIO_ALARM_VOLUME
2082 null, //AUDIO_NOTIFICATION_VOLUME
2083 null, //AUDIO_BLUETOOTH_VOLUME
2084 null, //WAKE_LOCK
2085 null, //MONITOR_LOCATION
2086 null, //MONITOR_HIGH_POWER_LOCATION
2087 null, //GET_USAGE_STATS
2088 null, //MUTE_MICROPHONE
2089 new RestrictionBypass(true, false), //TOAST_WINDOW
2090 null, //PROJECT_MEDIA
2091 null, //ACTIVATE_VPN
2092 null, //WALLPAPER
2093 null, //ASSIST_STRUCTURE
2094 null, //ASSIST_SCREENSHOT
2095 null, //READ_PHONE_STATE
2096 null, //ADD_VOICEMAIL
2097 null, // USE_SIP
2098 null, // PROCESS_OUTGOING_CALLS
2099 null, // USE_FINGERPRINT
2100 null, // BODY_SENSORS
2101 null, // READ_CELL_BROADCASTS
2102 null, // MOCK_LOCATION
2103 null, // READ_EXTERNAL_STORAGE
2104 null, // WRITE_EXTERNAL_STORAGE
2105 null, // TURN_ON_SCREEN
2106 null, // GET_ACCOUNTS
2107 null, // RUN_IN_BACKGROUND
2108 null, // AUDIO_ACCESSIBILITY_VOLUME
2109 null, // READ_PHONE_NUMBERS
2110 null, // REQUEST_INSTALL_PACKAGES
2111 null, // ENTER_PICTURE_IN_PICTURE_ON_HIDE
2112 null, // INSTANT_APP_START_FOREGROUND
2113 null, // ANSWER_PHONE_CALLS
2114 null, // OP_RUN_ANY_IN_BACKGROUND
2115 null, // OP_CHANGE_WIFI_STATE
2116 null, // OP_REQUEST_DELETE_PACKAGES
2117 null, // OP_BIND_ACCESSIBILITY_SERVICE
2118 null, // ACCEPT_HANDOVER
2119 null, // MANAGE_IPSEC_HANDOVERS
2120 null, // START_FOREGROUND
2121 new RestrictionBypass(true, false), // BLUETOOTH_SCAN
2122 null, // USE_BIOMETRIC
2123 null, // ACTIVITY_RECOGNITION
2124 null, // SMS_FINANCIAL_TRANSACTIONS
2125 null, // READ_MEDIA_AUDIO
2126 null, // WRITE_MEDIA_AUDIO
2127 null, // READ_MEDIA_VIDEO
2128 null, // WRITE_MEDIA_VIDEO
2129 null, // READ_MEDIA_IMAGES
2130 null, // WRITE_MEDIA_IMAGES
2131 null, // LEGACY_STORAGE
2132 null, // ACCESS_ACCESSIBILITY
2133 null, // READ_DEVICE_IDENTIFIERS
2134 null, // ACCESS_MEDIA_LOCATION
2135 null, // QUERY_ALL_PACKAGES
2136 null, // MANAGE_EXTERNAL_STORAGE
2137 null, // INTERACT_ACROSS_PROFILES
2138 null, // ACTIVATE_PLATFORM_VPN
2139 null, // LOADER_USAGE_STATS
Ricardo Correaacdc8272020-03-24 11:02:54 -07002140 null, // deprecated operation
Philip P. Moltmannad787aa2020-03-10 09:49:22 -07002141 null, // AUTO_REVOKE_PERMISSIONS_IF_UNUSED
Eugene Susla1d7a7912020-03-14 09:16:29 -07002142 null, // AUTO_REVOKE_MANAGED_BY_INSTALLER
Jason Monk62062992014-05-06 09:55:28 -04002143 };
2144
2145 /**
David Braunf5d83192013-09-16 13:43:51 -07002146 * This specifies the default mode for each operation.
2147 */
2148 private static int[] sOpDefaultMode = new int[] {
Eugene Susla93519852018-06-13 16:44:31 -07002149 AppOpsManager.MODE_ALLOWED, // COARSE_LOCATION
2150 AppOpsManager.MODE_ALLOWED, // FINE_LOCATION
2151 AppOpsManager.MODE_ALLOWED, // GPS
2152 AppOpsManager.MODE_ALLOWED, // VIBRATE
2153 AppOpsManager.MODE_ALLOWED, // READ_CONTACTS
2154 AppOpsManager.MODE_ALLOWED, // WRITE_CONTACTS
Svet Ganovd563e932019-04-14 13:07:41 -07002155 AppOpsManager.MODE_ALLOWED, // READ_CALL_LOG
2156 AppOpsManager.MODE_ALLOWED, // WRITE_CALL_LOG
Eugene Susla93519852018-06-13 16:44:31 -07002157 AppOpsManager.MODE_ALLOWED, // READ_CALENDAR
2158 AppOpsManager.MODE_ALLOWED, // WRITE_CALENDAR
2159 AppOpsManager.MODE_ALLOWED, // WIFI_SCAN
2160 AppOpsManager.MODE_ALLOWED, // POST_NOTIFICATION
2161 AppOpsManager.MODE_ALLOWED, // NEIGHBORING_CELLS
2162 AppOpsManager.MODE_ALLOWED, // CALL_PHONE
Svet Ganovd563e932019-04-14 13:07:41 -07002163 AppOpsManager.MODE_ALLOWED, // READ_SMS
Eugene Suslaaaff0072018-10-30 13:35:03 -07002164 AppOpsManager.MODE_IGNORED, // WRITE_SMS
Svet Ganovd563e932019-04-14 13:07:41 -07002165 AppOpsManager.MODE_ALLOWED, // RECEIVE_SMS
Eugene Susla93519852018-06-13 16:44:31 -07002166 AppOpsManager.MODE_ALLOWED, // RECEIVE_EMERGENCY_BROADCAST
Svet Ganovd563e932019-04-14 13:07:41 -07002167 AppOpsManager.MODE_ALLOWED, // RECEIVE_MMS
2168 AppOpsManager.MODE_ALLOWED, // RECEIVE_WAP_PUSH
2169 AppOpsManager.MODE_ALLOWED, // SEND_SMS
Eugene Susla93519852018-06-13 16:44:31 -07002170 AppOpsManager.MODE_ALLOWED, // READ_ICC_SMS
2171 AppOpsManager.MODE_ALLOWED, // WRITE_ICC_SMS
2172 AppOpsManager.MODE_DEFAULT, // WRITE_SETTINGS
Ng Zhi An65a99b62018-10-01 11:57:53 -07002173 getSystemAlertWindowDefault(), // SYSTEM_ALERT_WINDOW
Eugene Susla93519852018-06-13 16:44:31 -07002174 AppOpsManager.MODE_ALLOWED, // ACCESS_NOTIFICATIONS
2175 AppOpsManager.MODE_ALLOWED, // CAMERA
2176 AppOpsManager.MODE_ALLOWED, // RECORD_AUDIO
2177 AppOpsManager.MODE_ALLOWED, // PLAY_AUDIO
2178 AppOpsManager.MODE_ALLOWED, // READ_CLIPBOARD
2179 AppOpsManager.MODE_ALLOWED, // WRITE_CLIPBOARD
2180 AppOpsManager.MODE_ALLOWED, // TAKE_MEDIA_BUTTONS
2181 AppOpsManager.MODE_ALLOWED, // TAKE_AUDIO_FOCUS
2182 AppOpsManager.MODE_ALLOWED, // AUDIO_MASTER_VOLUME
2183 AppOpsManager.MODE_ALLOWED, // AUDIO_VOICE_VOLUME
2184 AppOpsManager.MODE_ALLOWED, // AUDIO_RING_VOLUME
2185 AppOpsManager.MODE_ALLOWED, // AUDIO_MEDIA_VOLUME
2186 AppOpsManager.MODE_ALLOWED, // AUDIO_ALARM_VOLUME
2187 AppOpsManager.MODE_ALLOWED, // AUDIO_NOTIFICATION_VOLUME
2188 AppOpsManager.MODE_ALLOWED, // AUDIO_BLUETOOTH_VOLUME
2189 AppOpsManager.MODE_ALLOWED, // WAKE_LOCK
2190 AppOpsManager.MODE_ALLOWED, // MONITOR_LOCATION
2191 AppOpsManager.MODE_ALLOWED, // MONITOR_HIGH_POWER_LOCATION
2192 AppOpsManager.MODE_DEFAULT, // GET_USAGE_STATS
2193 AppOpsManager.MODE_ALLOWED, // MUTE_MICROPHONE
2194 AppOpsManager.MODE_ALLOWED, // TOAST_WINDOW
2195 AppOpsManager.MODE_IGNORED, // PROJECT_MEDIA
2196 AppOpsManager.MODE_IGNORED, // ACTIVATE_VPN
2197 AppOpsManager.MODE_ALLOWED, // WRITE_WALLPAPER
2198 AppOpsManager.MODE_ALLOWED, // ASSIST_STRUCTURE
2199 AppOpsManager.MODE_ALLOWED, // ASSIST_SCREENSHOT
2200 AppOpsManager.MODE_ALLOWED, // READ_PHONE_STATE
2201 AppOpsManager.MODE_ALLOWED, // ADD_VOICEMAIL
2202 AppOpsManager.MODE_ALLOWED, // USE_SIP
Svet Ganovd563e932019-04-14 13:07:41 -07002203 AppOpsManager.MODE_ALLOWED, // PROCESS_OUTGOING_CALLS
Eugene Susla93519852018-06-13 16:44:31 -07002204 AppOpsManager.MODE_ALLOWED, // USE_FINGERPRINT
2205 AppOpsManager.MODE_ALLOWED, // BODY_SENSORS
Svet Ganovd563e932019-04-14 13:07:41 -07002206 AppOpsManager.MODE_ALLOWED, // READ_CELL_BROADCASTS
Eugene Susla93519852018-06-13 16:44:31 -07002207 AppOpsManager.MODE_ERRORED, // MOCK_LOCATION
2208 AppOpsManager.MODE_ALLOWED, // READ_EXTERNAL_STORAGE
2209 AppOpsManager.MODE_ALLOWED, // WRITE_EXTERNAL_STORAGE
2210 AppOpsManager.MODE_ALLOWED, // TURN_SCREEN_ON
2211 AppOpsManager.MODE_ALLOWED, // GET_ACCOUNTS
2212 AppOpsManager.MODE_ALLOWED, // RUN_IN_BACKGROUND
2213 AppOpsManager.MODE_ALLOWED, // AUDIO_ACCESSIBILITY_VOLUME
2214 AppOpsManager.MODE_ALLOWED, // READ_PHONE_NUMBERS
2215 AppOpsManager.MODE_DEFAULT, // REQUEST_INSTALL_PACKAGES
2216 AppOpsManager.MODE_ALLOWED, // PICTURE_IN_PICTURE
2217 AppOpsManager.MODE_DEFAULT, // INSTANT_APP_START_FOREGROUND
2218 AppOpsManager.MODE_ALLOWED, // ANSWER_PHONE_CALLS
2219 AppOpsManager.MODE_ALLOWED, // RUN_ANY_IN_BACKGROUND
2220 AppOpsManager.MODE_ALLOWED, // CHANGE_WIFI_STATE
2221 AppOpsManager.MODE_ALLOWED, // REQUEST_DELETE_PACKAGES
2222 AppOpsManager.MODE_ALLOWED, // BIND_ACCESSIBILITY_SERVICE
2223 AppOpsManager.MODE_ALLOWED, // ACCEPT_HANDOVER
2224 AppOpsManager.MODE_ERRORED, // MANAGE_IPSEC_TUNNELS
2225 AppOpsManager.MODE_ALLOWED, // START_FOREGROUND
2226 AppOpsManager.MODE_ALLOWED, // BLUETOOTH_SCAN
2227 AppOpsManager.MODE_ALLOWED, // USE_BIOMETRIC
Zimuzo6cbf9cc2018-10-05 12:05:58 +01002228 AppOpsManager.MODE_ALLOWED, // ACTIVITY_RECOGNITION
Hongming Jin228cd012018-11-09 14:47:50 -08002229 AppOpsManager.MODE_DEFAULT, // SMS_FINANCIAL_TRANSACTIONS
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07002230 AppOpsManager.MODE_ALLOWED, // READ_MEDIA_AUDIO
Jeff Sharkey9787a9452018-11-18 17:53:02 -07002231 AppOpsManager.MODE_ERRORED, // WRITE_MEDIA_AUDIO
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07002232 AppOpsManager.MODE_ALLOWED, // READ_MEDIA_VIDEO
Jeff Sharkey9787a9452018-11-18 17:53:02 -07002233 AppOpsManager.MODE_ERRORED, // WRITE_MEDIA_VIDEO
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07002234 AppOpsManager.MODE_ALLOWED, // READ_MEDIA_IMAGES
Jeff Sharkey9787a9452018-11-18 17:53:02 -07002235 AppOpsManager.MODE_ERRORED, // WRITE_MEDIA_IMAGES
Jeff Sharkeye82cbb12018-12-06 15:53:11 -07002236 AppOpsManager.MODE_DEFAULT, // LEGACY_STORAGE
Jackal Guo8dc791e2019-01-14 10:26:42 +08002237 AppOpsManager.MODE_ALLOWED, // ACCESS_ACCESSIBILITY
Michael Groover656ef912019-04-09 17:09:57 -07002238 AppOpsManager.MODE_ERRORED, // READ_DEVICE_IDENTIFIERS
Philip P. Moltmann89b044f2019-09-13 15:12:34 -07002239 AppOpsManager.MODE_ALLOWED, // ALLOW_MEDIA_LOCATION
Patrick Baumann6c1c8092019-06-27 14:55:44 -07002240 AppOpsManager.MODE_DEFAULT, // QUERY_ALL_PACKAGES
Philip P. Moltmannb43dfe32019-11-11 14:14:49 -08002241 AppOpsManager.MODE_DEFAULT, // MANAGE_EXTERNAL_STORAGE
kholoud mohamed20ded1f2019-12-03 17:52:38 +00002242 AppOpsManager.MODE_DEFAULT, // INTERACT_ACROSS_PROFILES
Benedict Wong3eb95202019-11-05 12:50:59 -08002243 AppOpsManager.MODE_IGNORED, // ACTIVATE_PLATFORM_VPN
Todd Kennedyb0948d42020-01-27 15:20:07 -08002244 AppOpsManager.MODE_DEFAULT, // LOADER_USAGE_STATS
Ricardo Correaacdc8272020-03-24 11:02:54 -07002245 AppOpsManager.MODE_IGNORED, // deprecated operation
Eugene Susla5fc2d762020-03-04 13:53:10 -08002246 AppOpsManager.MODE_DEFAULT, // OP_AUTO_REVOKE_PERMISSIONS_IF_UNUSED
Eugene Susla922cd082020-03-11 12:38:17 -07002247 AppOpsManager.MODE_ALLOWED, // OP_AUTO_REVOKE_MANAGED_BY_INSTALLER
David Braunf5d83192013-09-16 13:43:51 -07002248 };
2249
Dianne Hackborn8828d3a2013-09-25 16:47:10 -07002250 /**
2251 * This specifies whether each option is allowed to be reset
2252 * when resetting all app preferences. Disable reset for
2253 * app ops that are under strong control of some part of the
2254 * system (such as OP_WRITE_SMS, which should be allowed only
2255 * for whichever app is selected as the current SMS app).
2256 */
2257 private static boolean[] sOpDisableReset = new boolean[] {
Eugene Susla93519852018-06-13 16:44:31 -07002258 false, // COARSE_LOCATION
2259 false, // FINE_LOCATION
2260 false, // GPS
2261 false, // VIBRATE
2262 false, // READ_CONTACTS
2263 false, // WRITE_CONTACTS
2264 false, // READ_CALL_LOG
2265 false, // WRITE_CALL_LOG
2266 false, // READ_CALENDAR
2267 false, // WRITE_CALENDAR
2268 false, // WIFI_SCAN
2269 false, // POST_NOTIFICATION
2270 false, // NEIGHBORING_CELLS
2271 false, // CALL_PHONE
2272 true, // READ_SMS
2273 true, // WRITE_SMS
2274 true, // RECEIVE_SMS
2275 false, // RECEIVE_EMERGENCY_BROADCAST
2276 false, // RECEIVE_MMS
2277 true, // RECEIVE_WAP_PUSH
2278 true, // SEND_SMS
2279 false, // READ_ICC_SMS
2280 false, // WRITE_ICC_SMS
2281 false, // WRITE_SETTINGS
2282 false, // SYSTEM_ALERT_WINDOW
2283 false, // ACCESS_NOTIFICATIONS
2284 false, // CAMERA
2285 false, // RECORD_AUDIO
2286 false, // PLAY_AUDIO
2287 false, // READ_CLIPBOARD
2288 false, // WRITE_CLIPBOARD
2289 false, // TAKE_MEDIA_BUTTONS
2290 false, // TAKE_AUDIO_FOCUS
2291 false, // AUDIO_MASTER_VOLUME
2292 false, // AUDIO_VOICE_VOLUME
2293 false, // AUDIO_RING_VOLUME
2294 false, // AUDIO_MEDIA_VOLUME
2295 false, // AUDIO_ALARM_VOLUME
2296 false, // AUDIO_NOTIFICATION_VOLUME
2297 false, // AUDIO_BLUETOOTH_VOLUME
2298 false, // WAKE_LOCK
2299 false, // MONITOR_LOCATION
2300 false, // MONITOR_HIGH_POWER_LOCATION
2301 false, // GET_USAGE_STATS
2302 false, // MUTE_MICROPHONE
2303 false, // TOAST_WINDOW
2304 false, // PROJECT_MEDIA
2305 false, // ACTIVATE_VPN
2306 false, // WRITE_WALLPAPER
2307 false, // ASSIST_STRUCTURE
2308 false, // ASSIST_SCREENSHOT
2309 false, // READ_PHONE_STATE
2310 false, // ADD_VOICEMAIL
2311 false, // USE_SIP
2312 false, // PROCESS_OUTGOING_CALLS
2313 false, // USE_FINGERPRINT
2314 false, // BODY_SENSORS
2315 true, // READ_CELL_BROADCASTS
2316 false, // MOCK_LOCATION
2317 false, // READ_EXTERNAL_STORAGE
2318 false, // WRITE_EXTERNAL_STORAGE
2319 false, // TURN_SCREEN_ON
2320 false, // GET_ACCOUNTS
2321 false, // RUN_IN_BACKGROUND
2322 false, // AUDIO_ACCESSIBILITY_VOLUME
2323 false, // READ_PHONE_NUMBERS
2324 false, // REQUEST_INSTALL_PACKAGES
2325 false, // PICTURE_IN_PICTURE
2326 false, // INSTANT_APP_START_FOREGROUND
Eugene Suslacae3d3e2017-01-31 11:08:11 -08002327 false, // ANSWER_PHONE_CALLS
Eugene Susla93519852018-06-13 16:44:31 -07002328 false, // RUN_ANY_IN_BACKGROUND
2329 false, // CHANGE_WIFI_STATE
2330 false, // REQUEST_DELETE_PACKAGES
2331 false, // BIND_ACCESSIBILITY_SERVICE
Tyler Gunn79bc1ec2018-01-22 15:17:54 -08002332 false, // ACCEPT_HANDOVER
Nathan Harold1bb420672018-03-14 17:08:53 -07002333 false, // MANAGE_IPSEC_TUNNELS
Dianne Hackborne9d9b4b2018-03-28 13:51:46 -07002334 false, // START_FOREGROUND
Dianne Hackborne04f13d2018-05-02 12:51:52 -07002335 false, // BLUETOOTH_SCAN
Kevin Chynb3c05aa2018-09-21 16:50:32 -07002336 false, // USE_BIOMETRIC
Zimuzo6cbf9cc2018-10-05 12:05:58 +01002337 false, // ACTIVITY_RECOGNITION
Hongming Jin228cd012018-11-09 14:47:50 -08002338 false, // SMS_FINANCIAL_TRANSACTIONS
Jeff Sharkeyd2a1f0d2018-11-19 10:32:52 -07002339 false, // READ_MEDIA_AUDIO
2340 false, // WRITE_MEDIA_AUDIO
2341 false, // READ_MEDIA_VIDEO
2342 false, // WRITE_MEDIA_VIDEO
2343 false, // READ_MEDIA_IMAGES
2344 false, // WRITE_MEDIA_IMAGES
Zimc72c9d12019-11-27 15:49:33 +00002345 true, // LEGACY_STORAGE
Jackal Guo8dc791e2019-01-14 10:26:42 +08002346 false, // ACCESS_ACCESSIBILITY
Michael Groover656ef912019-04-09 17:09:57 -07002347 false, // READ_DEVICE_IDENTIFIERS
Philip P. Moltmann89b044f2019-09-13 15:12:34 -07002348 false, // ACCESS_MEDIA_LOCATION
Patrick Baumann6c1c8092019-06-27 14:55:44 -07002349 false, // QUERY_ALL_PACKAGES
Philip P. Moltmannb43dfe32019-11-11 14:14:49 -08002350 false, // MANAGE_EXTERNAL_STORAGE
kholoud mohamed20ded1f2019-12-03 17:52:38 +00002351 false, // INTERACT_ACROSS_PROFILES
Benedict Wong3eb95202019-11-05 12:50:59 -08002352 false, // ACTIVATE_PLATFORM_VPN
Todd Kennedyb0948d42020-01-27 15:20:07 -08002353 false, // LOADER_USAGE_STATS
Ricardo Correaacdc8272020-03-24 11:02:54 -07002354 false, // deprecated operation
Eugene Susla5fc2d762020-03-04 13:53:10 -08002355 false, // AUTO_REVOKE_PERMISSIONS_IF_UNUSED
Eugene Susla1d7a7912020-03-14 09:16:29 -07002356 false, // AUTO_REVOKE_MANAGED_BY_INSTALLER
Dianne Hackborn8828d3a2013-09-25 16:47:10 -07002357 };
2358
Svet Ganovfbf01f72015-04-28 18:39:06 -07002359 /**
Svet Ganovb9d71a62015-04-30 10:38:13 -07002360 * Mapping from an app op name to the app op code.
Svet Ganovfbf01f72015-04-28 18:39:06 -07002361 */
Svet Ganovb9d71a62015-04-30 10:38:13 -07002362 private static HashMap<String, Integer> sOpStrToOp = new HashMap<>();
Svet Ganovfbf01f72015-04-28 18:39:06 -07002363
Svet Ganovb9d71a62015-04-30 10:38:13 -07002364 /**
2365 * Mapping from a permission to the corresponding app op.
2366 */
Svet Ganovda0acdf2017-02-15 10:28:51 -08002367 private static HashMap<String, Integer> sPermToOp = new HashMap<>();
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07002368
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07002369 /**
2370 * Set to the uid of the caller if this thread is currently executing a two-way binder
2371 * transaction. Not set if this thread is currently not executing a two way binder transaction.
2372 *
2373 * @see #startNotedAppOpsCollection
Philip P. Moltmannda554e42019-12-20 11:21:02 -08002374 * @see #getNotedOpCollectionMode
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07002375 */
2376 private static final ThreadLocal<Integer> sBinderThreadCallingUid = new ThreadLocal<>();
2377
2378 /**
2379 * If a thread is currently executing a two-way binder transaction, this stores the op-codes of
2380 * the app-ops that were noted during this transaction.
2381 *
Philip P. Moltmannda554e42019-12-20 11:21:02 -08002382 * @see #getNotedOpCollectionMode
2383 * @see #collectNotedOpSync
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07002384 */
Philip P. Moltmann59076d82019-08-19 15:00:40 -07002385 private static final ThreadLocal<ArrayMap<String, long[]>> sAppOpsNotedInThisBinderTransaction =
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07002386 new ThreadLocal<>();
2387
2388 /** Whether noting for an appop should be collected */
2389 private static final @ShouldCollectNoteOp byte[] sAppOpsToNote = new byte[_NUM_OP];
2390
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07002391 static {
2392 if (sOpToSwitch.length != _NUM_OP) {
Dianne Hackborn8828d3a2013-09-25 16:47:10 -07002393 throw new IllegalStateException("sOpToSwitch length " + sOpToSwitch.length
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07002394 + " should be " + _NUM_OP);
2395 }
2396 if (sOpToString.length != _NUM_OP) {
Dianne Hackborn8828d3a2013-09-25 16:47:10 -07002397 throw new IllegalStateException("sOpToString length " + sOpToString.length
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07002398 + " should be " + _NUM_OP);
2399 }
2400 if (sOpNames.length != _NUM_OP) {
Dianne Hackborn8828d3a2013-09-25 16:47:10 -07002401 throw new IllegalStateException("sOpNames length " + sOpNames.length
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07002402 + " should be " + _NUM_OP);
2403 }
2404 if (sOpPerms.length != _NUM_OP) {
Dianne Hackborn8828d3a2013-09-25 16:47:10 -07002405 throw new IllegalStateException("sOpPerms length " + sOpPerms.length
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07002406 + " should be " + _NUM_OP);
2407 }
2408 if (sOpDefaultMode.length != _NUM_OP) {
Dianne Hackborn8828d3a2013-09-25 16:47:10 -07002409 throw new IllegalStateException("sOpDefaultMode length " + sOpDefaultMode.length
2410 + " should be " + _NUM_OP);
2411 }
2412 if (sOpDisableReset.length != _NUM_OP) {
2413 throw new IllegalStateException("sOpDisableReset length " + sOpDisableReset.length
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07002414 + " should be " + _NUM_OP);
2415 }
Jason Monk62062992014-05-06 09:55:28 -04002416 if (sOpRestrictions.length != _NUM_OP) {
2417 throw new IllegalStateException("sOpRestrictions length " + sOpRestrictions.length
2418 + " should be " + _NUM_OP);
2419 }
Jason Monk1c7c3192014-06-26 12:52:18 -04002420 if (sOpAllowSystemRestrictionBypass.length != _NUM_OP) {
2421 throw new IllegalStateException("sOpAllowSYstemRestrictionsBypass length "
2422 + sOpRestrictions.length + " should be " + _NUM_OP);
2423 }
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07002424 for (int i=0; i<_NUM_OP; i++) {
2425 if (sOpToString[i] != null) {
2426 sOpStrToOp.put(sOpToString[i], i);
2427 }
2428 }
Svet Ganovda0acdf2017-02-15 10:28:51 -08002429 for (int op : RUNTIME_AND_APPOP_PERMISSIONS_OPS) {
Svetoslav Ganoveaca4c52016-05-05 18:08:00 -07002430 if (sOpPerms[op] != null) {
Svet Ganovda0acdf2017-02-15 10:28:51 -08002431 sPermToOp.put(sOpPerms[op], op);
Svet Ganovb9d71a62015-04-30 10:38:13 -07002432 }
2433 }
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07002434
2435 if ((_NUM_OP + Long.SIZE - 1) / Long.SIZE != 2) {
2436 // The code currently assumes that the length of sAppOpsNotedInThisBinderTransaction is
2437 // two longs
2438 throw new IllegalStateException("notedAppOps collection code assumes < 128 appops");
2439 }
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07002440 }
2441
Stanislav Zholnin90516b92020-01-20 14:03:06 +00002442 /** Config used to control app ops access messages sampling */
2443 private static MessageSamplingConfig sConfig =
2444 new MessageSamplingConfig(OP_NONE, 0, 0);
2445
Svet Ganov8455ba22019-01-02 13:05:56 -08002446 /** @hide */
2447 public static final String KEY_HISTORICAL_OPS = "historical_ops";
2448
Chad Brubaker7c6dba62019-01-23 15:51:43 -08002449 /** System properties for debug logging of noteOp call sites */
2450 private static final String DEBUG_LOGGING_ENABLE_PROP = "appops.logging_enabled";
2451 private static final String DEBUG_LOGGING_PACKAGES_PROP = "appops.logging_packages";
2452 private static final String DEBUG_LOGGING_OPS_PROP = "appops.logging_ops";
2453 private static final String DEBUG_LOGGING_TAG = "AppOpsManager";
2454
David Braunf5d83192013-09-16 13:43:51 -07002455 /**
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08002456 * Retrieve the op switch that controls the given operation.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07002457 * @hide
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08002458 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002459 @UnsupportedAppUsage
Dianne Hackbornf265ea92013-01-31 15:00:51 -08002460 public static int opToSwitch(int op) {
2461 return sOpToSwitch[op];
2462 }
2463
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08002464 /**
2465 * Retrieve a non-localized name for the operation, for debugging output.
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07002466 * @hide
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08002467 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002468 @UnsupportedAppUsage
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08002469 public static String opToName(int op) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08002470 if (op == OP_NONE) return "NONE";
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08002471 return op < sOpNames.length ? sOpNames[op] : ("Unknown(" + op + ")");
2472 }
2473
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08002474 /**
Svet Ganov65f1b9e2019-01-17 19:19:40 -08002475 * Retrieve a non-localized public name for the operation.
2476 *
2477 * @hide
2478 */
2479 public static @NonNull String opToPublicName(int op) {
2480 return sOpToString[op];
2481 }
2482
2483 /**
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -08002484 * @hide
2485 */
2486 public static int strDebugOpToOp(String op) {
2487 for (int i=0; i<sOpNames.length; i++) {
2488 if (sOpNames[i].equals(op)) {
2489 return i;
2490 }
2491 }
2492 throw new IllegalArgumentException("Unknown operation string: " + op);
2493 }
2494
2495 /**
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08002496 * Retrieve the permission associated with an operation, or null if there is not one.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07002497 * @hide
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08002498 */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +00002499 @UnsupportedAppUsage
Philip P. Moltmann33115152018-04-11 13:39:36 -07002500 @TestApi
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08002501 public static String opToPermission(int op) {
2502 return sOpPerms[op];
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002503 }
2504
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08002505 /**
Hai Zhangb7776682018-09-25 15:10:57 -07002506 * Retrieve the permission associated with an operation, or null if there is not one.
2507 *
2508 * @param op The operation name.
2509 *
2510 * @hide
2511 */
2512 @Nullable
2513 @SystemApi
2514 public static String opToPermission(@NonNull String op) {
2515 return opToPermission(strOpToOp(op));
2516 }
2517
2518 /**
Jason Monk62062992014-05-06 09:55:28 -04002519 * Retrieve the user restriction associated with an operation, or null if there is not one.
2520 * @hide
2521 */
2522 public static String opToRestriction(int op) {
2523 return sOpRestrictions[op];
2524 }
2525
2526 /**
Svet Ganovb9d71a62015-04-30 10:38:13 -07002527 * Retrieve the app op code for a permission, or null if there is not one.
Svet Ganovda0acdf2017-02-15 10:28:51 -08002528 * This API is intended to be used for mapping runtime or appop permissions
2529 * to the corresponding app op.
Svet Ganovb9d71a62015-04-30 10:38:13 -07002530 * @hide
2531 */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +00002532 @UnsupportedAppUsage
Philip P. Moltmann33115152018-04-11 13:39:36 -07002533 @TestApi
Svet Ganovb9d71a62015-04-30 10:38:13 -07002534 public static int permissionToOpCode(String permission) {
Svet Ganovda0acdf2017-02-15 10:28:51 -08002535 Integer boxedOpCode = sPermToOp.get(permission);
Svet Ganov019d2302015-05-04 11:07:38 -07002536 return boxedOpCode != null ? boxedOpCode : OP_NONE;
Svet Ganovb9d71a62015-04-30 10:38:13 -07002537 }
2538
2539 /**
Philip P. Moltmannad787aa2020-03-10 09:49:22 -07002540 * Retrieve whether the op allows to bypass the user restriction.
2541 *
Jason Monk1c7c3192014-06-26 12:52:18 -04002542 * @hide
2543 */
Philip P. Moltmannad787aa2020-03-10 09:49:22 -07002544 public static RestrictionBypass opAllowSystemBypassRestriction(int op) {
Jason Monk1c7c3192014-06-26 12:52:18 -04002545 return sOpAllowSystemRestrictionBypass[op];
2546 }
2547
2548 /**
David Braunf5d83192013-09-16 13:43:51 -07002549 * Retrieve the default mode for the operation.
2550 * @hide
2551 */
Svet Ganov8455ba22019-01-02 13:05:56 -08002552 public static @Mode int opToDefaultMode(int op) {
David Braunf5d83192013-09-16 13:43:51 -07002553 return sOpDefaultMode[op];
2554 }
2555
2556 /**
Hai Zhangc595f112018-11-06 14:20:38 -08002557 * Retrieve the default mode for the app op.
2558 *
2559 * @param appOp The app op name
2560 *
2561 * @return the default mode for the app op
2562 *
2563 * @hide
2564 */
Svet Ganov8e5bf962019-03-19 23:59:03 -07002565 @TestApi
Hai Zhangc595f112018-11-06 14:20:38 -08002566 @SystemApi
2567 public static int opToDefaultMode(@NonNull String appOp) {
2568 return opToDefaultMode(strOpToOp(appOp));
2569 }
2570
2571 /**
Svet Ganov82f09bc2018-01-12 22:08:40 -08002572 * Retrieve the human readable mode.
2573 * @hide
2574 */
Svet Ganov8455ba22019-01-02 13:05:56 -08002575 public static String modeToName(@Mode int mode) {
Dianne Hackbornc216a262018-04-26 13:46:22 -07002576 if (mode >= 0 && mode < MODE_NAMES.length) {
2577 return MODE_NAMES[mode];
Svet Ganov82f09bc2018-01-12 22:08:40 -08002578 }
Dianne Hackbornc216a262018-04-26 13:46:22 -07002579 return "mode=" + mode;
Svet Ganov82f09bc2018-01-12 22:08:40 -08002580 }
2581
2582 /**
Dianne Hackborn8828d3a2013-09-25 16:47:10 -07002583 * Retrieve whether the op allows itself to be reset.
2584 * @hide
2585 */
2586 public static boolean opAllowsReset(int op) {
2587 return !sOpDisableReset[op];
2588 }
2589
2590 /**
Soonil Nagarkar4523f962020-04-14 19:04:41 -07002591 * Returns a listenerId suitable for use with {@link #noteOp(int, int, String, String, String)}.
2592 *
2593 * This is intended for use client side, when the receiver id must be created before the
2594 * associated call is made to the system server. If using {@link PendingIntent} as the receiver,
2595 * avoid using this method as it will include a pointless additional x-process call. Instead to
2596 * prefer passing the PendingIntent to the system server, and then invoking
2597 * {@link #toReceiverId(PendingIntent)} instead.
2598 *
2599 * @param obj the receiver in use
2600 * @return a string representation of the receiver suitable for app ops use
2601 * @hide
2602 */
2603 // TODO: this should probably be @SystemApi as well
2604 public static @NonNull String toReceiverId(@NonNull Object obj) {
2605 if (obj instanceof PendingIntent) {
2606 return toReceiverId((PendingIntent) obj);
2607 } else {
2608 return obj.getClass().getName() + "@" + System.identityHashCode(obj);
2609 }
2610 }
2611
2612 /**
2613 * Returns a listenerId suitable for use with {@link #noteOp(int, int, String, String, String)}.
2614 *
2615 * This is intended for use server side, where ActivityManagerService can be referenced without
2616 * an additional x-process call.
2617 *
2618 * @param pendingIntent the pendingIntent in use
2619 * @return a string representation of the pending intent suitable for app ops use
2620 * @see #toReceiverId(Object)
2621 * @hide
2622 */
2623 // TODO: this should probably be @SystemApi as well
2624 public static @NonNull String toReceiverId(@NonNull PendingIntent pendingIntent) {
2625 return pendingIntent.getTag("");
2626 }
2627
2628 /**
Philip P. Moltmannad787aa2020-03-10 09:49:22 -07002629 * When to not enforce {@link #setUserRestriction restrictions}.
2630 *
2631 * @hide
2632 */
2633 public static class RestrictionBypass {
2634 /** Does the app need to be privileged to bypass the restriction */
2635 public boolean isPrivileged;
2636
2637 /**
2638 * Does the app need to have the EXEMPT_FROM_AUDIO_RESTRICTIONS permission to bypass the
2639 * restriction
2640 */
2641 public boolean isRecordAudioRestrictionExcept;
2642
2643 public RestrictionBypass(boolean isPrivileged, boolean isRecordAudioRestrictionExcept) {
2644 this.isPrivileged = isPrivileged;
2645 this.isRecordAudioRestrictionExcept = isRecordAudioRestrictionExcept;
2646 }
2647
2648 public static RestrictionBypass UNRESTRICTED = new RestrictionBypass(true, true);
2649 }
2650
2651 /**
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08002652 * Class holding all of the operation information associated with an app.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07002653 * @hide
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08002654 */
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002655 @TestApi
Dianne Hackbornf0c322a2018-06-13 15:24:04 -07002656 @SystemApi
2657 public static final class PackageOps implements Parcelable {
Dianne Hackborn35654b62013-01-14 17:38:02 -08002658 private final String mPackageName;
2659 private final int mUid;
2660 private final List<OpEntry> mEntries;
2661
Dianne Hackbornf0c322a2018-06-13 15:24:04 -07002662 /**
2663 * @hide
2664 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01002665 @UnsupportedAppUsage
Dianne Hackborn35654b62013-01-14 17:38:02 -08002666 public PackageOps(String packageName, int uid, List<OpEntry> entries) {
2667 mPackageName = packageName;
2668 mUid = uid;
2669 mEntries = entries;
2670 }
2671
Svet Ganovaf189e32019-02-15 18:45:29 -08002672 /**
2673 * @return The name of the package.
2674 */
2675 public @NonNull String getPackageName() {
Dianne Hackborn35654b62013-01-14 17:38:02 -08002676 return mPackageName;
2677 }
2678
Svet Ganovaf189e32019-02-15 18:45:29 -08002679 /**
2680 * @return The uid of the package.
2681 */
Dianne Hackborn35654b62013-01-14 17:38:02 -08002682 public int getUid() {
2683 return mUid;
2684 }
2685
Svet Ganovaf189e32019-02-15 18:45:29 -08002686 /**
2687 * @return The ops of the package.
2688 */
Dianne Hackborn62878492019-03-11 15:57:07 -07002689 public @NonNull List<OpEntry> getOps() {
Dianne Hackborn35654b62013-01-14 17:38:02 -08002690 return mEntries;
2691 }
2692
2693 @Override
2694 public int describeContents() {
2695 return 0;
2696 }
2697
2698 @Override
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002699 public void writeToParcel(@NonNull Parcel dest, int flags) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08002700 dest.writeString(mPackageName);
2701 dest.writeInt(mUid);
2702 dest.writeInt(mEntries.size());
2703 for (int i=0; i<mEntries.size(); i++) {
2704 mEntries.get(i).writeToParcel(dest, flags);
2705 }
2706 }
2707
2708 PackageOps(Parcel source) {
2709 mPackageName = source.readString();
2710 mUid = source.readInt();
2711 mEntries = new ArrayList<OpEntry>();
2712 final int N = source.readInt();
2713 for (int i=0; i<N; i++) {
2714 mEntries.add(OpEntry.CREATOR.createFromParcel(source));
2715 }
2716 }
2717
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07002718 public static final @android.annotation.NonNull Creator<PackageOps> CREATOR = new Creator<PackageOps>() {
Dianne Hackborn35654b62013-01-14 17:38:02 -08002719 @Override public PackageOps createFromParcel(Parcel source) {
2720 return new PackageOps(source);
2721 }
2722
2723 @Override public PackageOps[] newArray(int size) {
2724 return new PackageOps[size];
2725 }
2726 };
2727 }
2728
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08002729 /**
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002730 * Proxy information for a {@link #noteOp} event
2731 *
2732 * @hide
2733 */
2734 @TestApi
2735 @SystemApi
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002736 // @DataClass(genHiddenConstructor = true, genHiddenCopyConstructor = true)
2737 // genHiddenCopyConstructor does not work for @hide @SystemApi classes
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002738 public static final class OpEventProxyInfo implements Parcelable {
2739 /** UID of the proxy app that noted the op */
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002740 private @IntRange(from = 0) int mUid;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002741 /** Package of the proxy that noted the op */
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002742 private @Nullable String mPackageName;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002743 /** Attribution tag of the proxy that noted the op */
2744 private @Nullable String mAttributionTag;
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002745
2746 /**
2747 * Reinit existing object with new state.
2748 *
2749 * @param uid UID of the proxy app that noted the op
2750 * @param packageName Package of the proxy that noted the op
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002751 * @param attributionTag attribution tag of the proxy that noted the op
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002752 *
2753 * @hide
2754 */
2755 public void reinit(@IntRange(from = 0) int uid, @Nullable String packageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002756 @Nullable String attributionTag) {
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002757 mUid = Preconditions.checkArgumentNonnegative(uid);
2758 mPackageName = packageName;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002759 mAttributionTag = attributionTag;
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002760 }
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002761
2762
2763
2764 // Code below generated by codegen v1.0.14.
2765 //
2766 // DO NOT MODIFY!
2767 // CHECKSTYLE:OFF Generated code
2768 //
2769 // To regenerate run:
2770 // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/app/AppOpsManager.java
2771 //
2772 // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
2773 // Settings > Editor > Code Style > Formatter Control
2774 //@formatter:off
2775
2776
2777 /**
2778 * Creates a new OpEventProxyInfo.
2779 *
2780 * @param uid
2781 * UID of the proxy app that noted the op
2782 * @param packageName
2783 * Package of the proxy that noted the op
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002784 * @param attributionTag
2785 * Attribution tag of the proxy that noted the op
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002786 * @hide
2787 */
2788 @DataClass.Generated.Member
2789 public OpEventProxyInfo(
2790 @IntRange(from = 0) int uid,
2791 @Nullable String packageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002792 @Nullable String attributionTag) {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002793 this.mUid = uid;
2794 com.android.internal.util.AnnotationValidations.validate(
2795 IntRange.class, null, mUid,
2796 "from", 0);
2797 this.mPackageName = packageName;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002798 this.mAttributionTag = attributionTag;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002799
2800 // onConstructed(); // You can define this method to get a callback
2801 }
2802
2803 /**
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002804 * Copy constructor
2805 *
2806 * @hide
2807 */
2808 @DataClass.Generated.Member
2809 public OpEventProxyInfo(@NonNull OpEventProxyInfo orig) {
2810 mUid = orig.mUid;
2811 mPackageName = orig.mPackageName;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002812 mAttributionTag = orig.mAttributionTag;
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002813 }
2814
2815 /**
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002816 * UID of the proxy app that noted the op
2817 */
2818 @DataClass.Generated.Member
2819 public @IntRange(from = 0) int getUid() {
2820 return mUid;
2821 }
2822
2823 /**
2824 * Package of the proxy that noted the op
2825 */
2826 @DataClass.Generated.Member
2827 public @Nullable String getPackageName() {
2828 return mPackageName;
2829 }
2830
2831 /**
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002832 * Attribution tag of the proxy that noted the op
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002833 */
2834 @DataClass.Generated.Member
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002835 public @Nullable String getAttributionTag() {
2836 return mAttributionTag;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002837 }
2838
2839 @Override
2840 @DataClass.Generated.Member
2841 public void writeToParcel(@NonNull Parcel dest, int flags) {
2842 // You can override field parcelling by defining methods like:
2843 // void parcelFieldName(Parcel dest, int flags) { ... }
2844
2845 byte flg = 0;
2846 if (mPackageName != null) flg |= 0x2;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002847 if (mAttributionTag != null) flg |= 0x4;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002848 dest.writeByte(flg);
2849 dest.writeInt(mUid);
2850 if (mPackageName != null) dest.writeString(mPackageName);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002851 if (mAttributionTag != null) dest.writeString(mAttributionTag);
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002852 }
2853
2854 @Override
2855 @DataClass.Generated.Member
2856 public int describeContents() { return 0; }
2857
2858 /** @hide */
2859 @SuppressWarnings({"unchecked", "RedundantCast"})
2860 @DataClass.Generated.Member
2861 /* package-private */ OpEventProxyInfo(@NonNull Parcel in) {
2862 // You can override field unparcelling by defining methods like:
2863 // static FieldType unparcelFieldName(Parcel in) { ... }
2864
2865 byte flg = in.readByte();
2866 int uid = in.readInt();
2867 String packageName = (flg & 0x2) == 0 ? null : in.readString();
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002868 String attributionTag = (flg & 0x4) == 0 ? null : in.readString();
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002869
2870 this.mUid = uid;
2871 com.android.internal.util.AnnotationValidations.validate(
2872 IntRange.class, null, mUid,
2873 "from", 0);
2874 this.mPackageName = packageName;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002875 this.mAttributionTag = attributionTag;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002876
2877 // onConstructed(); // You can define this method to get a callback
2878 }
2879
2880 @DataClass.Generated.Member
2881 public static final @NonNull Parcelable.Creator<OpEventProxyInfo> CREATOR
2882 = new Parcelable.Creator<OpEventProxyInfo>() {
2883 @Override
2884 public OpEventProxyInfo[] newArray(int size) {
2885 return new OpEventProxyInfo[size];
2886 }
2887
2888 @Override
2889 public OpEventProxyInfo createFromParcel(@NonNull Parcel in) {
2890 return new OpEventProxyInfo(in);
2891 }
2892 };
2893
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002894 /*
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002895 @DataClass.Generated(
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002896 time = 1576814974615L,
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002897 codegenVersion = "1.0.14",
2898 sourceFile = "frameworks/base/core/java/android/app/AppOpsManager.java",
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08002899 inputSignatures = "private @android.annotation.IntRange(from=0L) int mUid\nprivate @android.annotation.Nullable java.lang.String mPackageName\nprivate @android.annotation.Nullable java.lang.String mAttributionTag\npublic void reinit(int,java.lang.String,java.lang.String)\nclass OpEventProxyInfo extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genHiddenConstructor=true, genHiddenCopyConstructor=true)")
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002900 @Deprecated
2901 private void __metadata() {}
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002902 */
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002903
2904 //@formatter:on
2905 // End of generated code
2906
2907 }
2908
2909 /**
2910 * Description of a {@link #noteOp} or {@link #startOp} event
2911 *
2912 * @hide
2913 */
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002914 //@DataClass codegen verifier is broken
2915 public static final class NoteOpEvent implements Parcelable {
2916 /** Time of noteOp event */
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002917 private @IntRange(from = 0) long mNoteTime;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002918 /** The duration of this event (in case this is a startOp event, -1 otherwise). */
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002919 private @IntRange(from = -1) long mDuration;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002920 /** Proxy information of the noteOp event */
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002921 private @Nullable OpEventProxyInfo mProxy;
2922
2923 /**
2924 * Reinit existing object with new state.
2925 *
2926 * @param noteTime Time of noteOp event
2927 * @param duration The duration of this event (in case this is a startOp event,
2928 * -1 otherwise).
2929 * @param proxy Proxy information of the noteOp event
2930 * @param proxyPool The pool to release previous {@link OpEventProxyInfo} to
2931 */
2932 public void reinit(@IntRange(from = 0) long noteTime,
2933 @IntRange(from = -1) long duration,
2934 @Nullable OpEventProxyInfo proxy,
2935 @NonNull Pools.Pool<OpEventProxyInfo> proxyPool) {
2936 mNoteTime = Preconditions.checkArgumentNonnegative(noteTime);
2937 mDuration = Preconditions.checkArgumentInRange(duration, -1L, Long.MAX_VALUE,
2938 "duration");
2939
2940 if (mProxy != null) {
2941 proxyPool.release(mProxy);
2942 }
2943 mProxy = proxy;
2944 }
2945
2946 /**
2947 * Copy constructor
2948 *
2949 * @hide
2950 */
2951 public NoteOpEvent(@NonNull NoteOpEvent original) {
2952 this(original.mNoteTime, original.mDuration,
2953 original.mProxy != null ? new OpEventProxyInfo(original.mProxy) : null);
2954 }
2955
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002956
2957
2958 // Code below generated by codegen v1.0.14.
2959 //
2960 // DO NOT MODIFY!
2961 // CHECKSTYLE:OFF Generated code
2962 //
2963 // To regenerate run:
2964 // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/app/AppOpsManager.java
2965 //
2966 // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
2967 // Settings > Editor > Code Style > Formatter Control
2968 //@formatter:off
2969
2970
2971 /**
2972 * Creates a new NoteOpEvent.
2973 *
2974 * @param noteTime
2975 * Time of noteOp event
2976 * @param duration
2977 * The duration of this event (in case this is a startOp event, -1 otherwise).
2978 * @param proxy
2979 * Proxy information of the noteOp event
2980 */
2981 @DataClass.Generated.Member
2982 public NoteOpEvent(
2983 @IntRange(from = 0) long noteTime,
2984 @IntRange(from = -1) long duration,
2985 @Nullable OpEventProxyInfo proxy) {
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002986 this.mNoteTime = noteTime;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002987 com.android.internal.util.AnnotationValidations.validate(
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002988 IntRange.class, null, mNoteTime,
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002989 "from", 0);
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002990 this.mDuration = duration;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002991 com.android.internal.util.AnnotationValidations.validate(
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002992 IntRange.class, null, mDuration,
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002993 "from", -1);
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002994 this.mProxy = proxy;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08002995
2996 // onConstructed(); // You can define this method to get a callback
2997 }
2998
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08002999 /**
3000 * Time of noteOp event
3001 */
3002 @DataClass.Generated.Member
3003 public @IntRange(from = 0) long getNoteTime() {
3004 return mNoteTime;
3005 }
3006
3007 /**
3008 * The duration of this event (in case this is a startOp event, -1 otherwise).
3009 */
3010 @DataClass.Generated.Member
3011 public @IntRange(from = -1) long getDuration() {
3012 return mDuration;
3013 }
3014
3015 /**
3016 * Proxy information of the noteOp event
3017 */
3018 @DataClass.Generated.Member
3019 public @Nullable OpEventProxyInfo getProxy() {
3020 return mProxy;
3021 }
3022
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003023 @Override
3024 @DataClass.Generated.Member
3025 public void writeToParcel(@NonNull Parcel dest, int flags) {
3026 // You can override field parcelling by defining methods like:
3027 // void parcelFieldName(Parcel dest, int flags) { ... }
3028
3029 byte flg = 0;
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08003030 if (mProxy != null) flg |= 0x4;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003031 dest.writeByte(flg);
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08003032 dest.writeLong(mNoteTime);
3033 dest.writeLong(mDuration);
3034 if (mProxy != null) dest.writeTypedObject(mProxy, flags);
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003035 }
3036
3037 @Override
3038 @DataClass.Generated.Member
3039 public int describeContents() { return 0; }
3040
3041 /** @hide */
3042 @SuppressWarnings({"unchecked", "RedundantCast"})
3043 @DataClass.Generated.Member
3044 /* package-private */ NoteOpEvent(@NonNull Parcel in) {
3045 // You can override field unparcelling by defining methods like:
3046 // static FieldType unparcelFieldName(Parcel in) { ... }
3047
3048 byte flg = in.readByte();
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08003049 long noteTime = in.readLong();
3050 long duration = in.readLong();
3051 OpEventProxyInfo proxy = (flg & 0x4) == 0 ? null : (OpEventProxyInfo) in.readTypedObject(OpEventProxyInfo.CREATOR);
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003052
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08003053 this.mNoteTime = noteTime;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003054 com.android.internal.util.AnnotationValidations.validate(
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08003055 IntRange.class, null, mNoteTime,
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003056 "from", 0);
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08003057 this.mDuration = duration;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003058 com.android.internal.util.AnnotationValidations.validate(
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08003059 IntRange.class, null, mDuration,
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003060 "from", -1);
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08003061 this.mProxy = proxy;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003062
3063 // onConstructed(); // You can define this method to get a callback
3064 }
3065
3066 @DataClass.Generated.Member
3067 public static final @NonNull Parcelable.Creator<NoteOpEvent> CREATOR
3068 = new Parcelable.Creator<NoteOpEvent>() {
3069 @Override
3070 public NoteOpEvent[] newArray(int size) {
3071 return new NoteOpEvent[size];
3072 }
3073
3074 @Override
3075 public NoteOpEvent createFromParcel(@NonNull Parcel in) {
3076 return new NoteOpEvent(in);
3077 }
3078 };
3079
3080 /*
3081 @DataClass.Generated(
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08003082 time = 1576811792274L,
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003083 codegenVersion = "1.0.14",
3084 sourceFile = "frameworks/base/core/java/android/app/AppOpsManager.java",
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08003085 inputSignatures = "private @android.annotation.IntRange(from=0L) long mNoteTime\nprivate @android.annotation.IntRange(from=-1) long mDuration\nprivate @android.annotation.Nullable android.app.OpEventProxyInfo mProxy\npublic void reinit(long,long,android.app.OpEventProxyInfo,android.util.Pools.Pool<android.app.OpEventProxyInfo>)\npublic @java.lang.Override java.lang.Object clone()\nclass NoteOpEvent extends java.lang.Object implements [android.os.Parcelable, java.lang.Cloneable]\n@com.android.internal.util.DataClass")
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003086 @Deprecated
3087 private void __metadata() {}
3088 */
3089
3090
3091 //@formatter:on
3092 // End of generated code
3093
3094 }
3095
3096 /**
3097 * Last {@link #noteOp} and {@link #startOp} events performed for a single op and a specific
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003098 * {@link Context#createAttributionContext(String) attribution} for all uidModes and opFlags.
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003099 *
3100 * @hide
3101 */
3102 @TestApi
3103 @SystemApi
3104 @Immutable
3105 // @DataClass(genHiddenConstructor = true) codegen verifier is broken
3106 @DataClass.Suppress({"getAccessEvents", "getRejectEvents", "getOp"})
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003107 public static final class AttributedOpEntry implements Parcelable {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003108 /** The code of the op */
3109 private final @IntRange(from = 0, to = _NUM_OP - 1) int mOp;
3110 /** Whether the op is running */
3111 private final boolean mRunning;
3112 /** The access events */
3113 @DataClass.ParcelWith(LongSparseArrayParceling.class)
3114 private final @Nullable LongSparseArray<NoteOpEvent> mAccessEvents;
3115 /** The rejection events */
3116 @DataClass.ParcelWith(LongSparseArrayParceling.class)
3117 private final @Nullable LongSparseArray<NoteOpEvent> mRejectEvents;
3118
3119 /**
3120 * Returns all keys for which we have events.
3121 *
3122 * @hide
3123 */
3124 public @NonNull ArraySet<Long> collectKeys() {
3125 ArraySet<Long> keys = new ArraySet<>();
3126
3127 if (mAccessEvents != null) {
3128 int numEvents = mAccessEvents.size();
3129 for (int i = 0; i < numEvents; i++) {
3130 keys.add(mAccessEvents.keyAt(i));
3131 }
3132 }
3133
3134 if (mRejectEvents != null) {
3135 int numEvents = mRejectEvents.size();
3136 for (int i = 0; i < numEvents; i++) {
3137 keys.add(mRejectEvents.keyAt(i));
3138 }
3139 }
3140
3141 return keys;
3142 }
3143
3144 /**
3145 * Return the last access time.
3146 *
3147 * @param flags The op flags
3148 *
3149 * @return the last access time (in milliseconds since epoch start (January 1, 1970
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003150 * 00:00:00.000 GMT - Gregorian)) or {@code -1} if there was no access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003151 *
3152 * @see #getLastAccessForegroundTime(int)
3153 * @see #getLastAccessBackgroundTime(int)
3154 * @see #getLastAccessTime(int, int, int)
3155 * @see OpEntry#getLastAccessTime(int)
3156 */
3157 public long getLastAccessTime(@OpFlags int flags) {
3158 return getLastAccessTime(MAX_PRIORITY_UID_STATE, MIN_PRIORITY_UID_STATE, flags);
3159 }
3160
3161 /**
3162 * Return the last foreground access time.
3163 *
3164 * @param flags The op flags
3165 *
3166 * @return the last access time (in milliseconds since epoch start (January 1, 1970
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003167 * 00:00:00.000 GMT - Gregorian)) or {@code -1} if there was no foreground access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003168 *
3169 * @see #getLastAccessTime(int)
3170 * @see #getLastAccessBackgroundTime(int)
3171 * @see #getLastAccessTime(int, int, int)
3172 * @see OpEntry#getLastAccessForegroundTime(int)
3173 */
3174 public long getLastAccessForegroundTime(@OpFlags int flags) {
3175 return getLastAccessTime(MAX_PRIORITY_UID_STATE, resolveFirstUnrestrictedUidState(mOp),
3176 flags);
3177 }
3178
3179 /**
3180 * Return the last background access time.
3181 *
3182 * @param flags The op flags
3183 *
3184 * @return the last access time (in milliseconds since epoch start (January 1, 1970
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003185 * 00:00:00.000 GMT - Gregorian)) or {@code -1} if there was no background access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003186 *
3187 * @see #getLastAccessTime(int)
3188 * @see #getLastAccessForegroundTime(int)
3189 * @see #getLastAccessTime(int, int, int)
3190 * @see OpEntry#getLastAccessBackgroundTime(int)
3191 */
3192 public long getLastAccessBackgroundTime(@OpFlags int flags) {
3193 return getLastAccessTime(resolveLastRestrictedUidState(mOp), MIN_PRIORITY_UID_STATE,
3194 flags);
3195 }
3196
3197 /**
3198 * Return the last access event.
3199 *
3200 * @param flags The op flags
3201 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003202 * @return the last access event of {@code null} if there was no access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003203 */
3204 private @Nullable NoteOpEvent getLastAccessEvent(@UidState int fromUidState,
3205 @UidState int toUidState, @OpFlags int flags) {
3206 return getLastEvent(mAccessEvents, fromUidState, toUidState, flags);
3207 }
3208
3209 /**
3210 * Return the last access time.
3211 *
3212 * @param fromUidState The lowest UID state for which to query
3213 * @param toUidState The highest UID state for which to query (inclusive)
3214 * @param flags The op flags
3215 *
3216 * @return the last access time (in milliseconds since epoch start (January 1, 1970
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003217 * 00:00:00.000 GMT - Gregorian)) or {@code -1} if there was no access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003218 *
3219 * @see #getLastAccessTime(int)
3220 * @see #getLastAccessForegroundTime(int)
3221 * @see #getLastAccessBackgroundTime(int)
3222 * @see OpEntry#getLastAccessTime(int, int, int)
3223 */
3224 public long getLastAccessTime(@UidState int fromUidState, @UidState int toUidState,
3225 @OpFlags int flags) {
3226 NoteOpEvent lastEvent = getLastAccessEvent(fromUidState, toUidState, flags);
3227 if (lastEvent == null) {
3228 return -1;
3229 }
3230
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08003231 return lastEvent.getNoteTime();
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003232 }
3233
3234 /**
3235 * Return the last rejection time.
3236 *
3237 * @param flags The op flags
3238 *
3239 * @return the last rejection time (in milliseconds since epoch start (January 1, 1970
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003240 * 00:00:00.000 GMT - Gregorian)) or {@code -1} if there was no rejection
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003241 *
3242 * @see #getLastRejectForegroundTime(int)
3243 * @see #getLastRejectBackgroundTime(int)
3244 * @see #getLastRejectTime(int, int, int)
3245 * @see OpEntry#getLastRejectTime(int)
3246 */
3247 public long getLastRejectTime(@OpFlags int flags) {
3248 return getLastRejectTime(MAX_PRIORITY_UID_STATE, MIN_PRIORITY_UID_STATE, flags);
3249 }
3250
3251 /**
3252 * Return the last foreground rejection time.
3253 *
3254 * @param flags The op flags
3255 *
3256 * @return the last rejection time (in milliseconds since epoch start (January 1, 1970
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003257 * 00:00:00.000 GMT - Gregorian)) or {@code -1} if there was no foreground rejection
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003258 *
3259 * @see #getLastRejectTime(int)
3260 * @see #getLastRejectBackgroundTime(int)
3261 * @see #getLastRejectTime(int, int, int)
3262 * @see OpEntry#getLastRejectForegroundTime(int)
3263 */
3264 public long getLastRejectForegroundTime(@OpFlags int flags) {
3265 return getLastRejectTime(MAX_PRIORITY_UID_STATE, resolveFirstUnrestrictedUidState(mOp),
3266 flags);
3267 }
3268
3269 /**
3270 * Return the last background rejection time.
3271 *
3272 * @param flags The op flags
3273 *
3274 * @return the last rejection time (in milliseconds since epoch start (January 1, 1970
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003275 * 00:00:00.000 GMT - Gregorian)) or {@code -1} if there was no background rejection
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003276 *
3277 * @see #getLastRejectTime(int)
3278 * @see #getLastRejectForegroundTime(int)
3279 * @see #getLastRejectTime(int, int, int)
3280 * @see OpEntry#getLastRejectBackgroundTime(int)
3281 */
3282 public long getLastRejectBackgroundTime(@OpFlags int flags) {
3283 return getLastRejectTime(resolveLastRestrictedUidState(mOp), MIN_PRIORITY_UID_STATE,
3284 flags);
3285 }
3286
3287 /**
3288 * Return the last background rejection event.
3289 *
3290 * @param flags The op flags
3291 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003292 * @return the last rejection event of {@code null} if there was no rejection
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003293 *
3294 * @see #getLastRejectTime(int)
3295 * @see #getLastRejectForegroundTime(int)
3296 * @see #getLastRejectBackgroundTime(int)
3297 * @see OpEntry#getLastRejectTime(int, int, int)
3298 */
3299 private @Nullable NoteOpEvent getLastRejectEvent(@UidState int fromUidState,
3300 @UidState int toUidState, @OpFlags int flags) {
3301 return getLastEvent(mRejectEvents, fromUidState, toUidState, flags);
3302 }
3303
3304 /**
3305 * Return the last rejection time.
3306 *
3307 * @param fromUidState The lowest UID state for which to query
3308 * @param toUidState The highest UID state for which to query (inclusive)
3309 * @param flags The op flags
3310 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003311 * @return the last access time (in milliseconds since epoch) or {@code -1} if there was no
3312 * rejection
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003313 *
3314 * @see #getLastRejectTime(int)
3315 * @see #getLastRejectForegroundTime(int)
3316 * @see #getLastRejectForegroundTime(int)
3317 * @see #getLastRejectTime(int, int, int)
3318 * @see OpEntry#getLastRejectTime(int, int, int)
3319 */
3320 public long getLastRejectTime(@UidState int fromUidState, @UidState int toUidState,
3321 @OpFlags int flags) {
3322 NoteOpEvent lastEvent = getLastRejectEvent(fromUidState, toUidState, flags);
3323 if (lastEvent == null) {
3324 return -1;
3325 }
3326
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08003327 return lastEvent.getNoteTime();
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003328 }
3329
3330 /**
3331 * Return the duration in milliseconds of the last the access.
3332 *
3333 * @param flags The op flags
3334 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003335 * @return the duration in milliseconds or {@code -1} if there was no rejection
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003336 *
3337 * @see #getLastForegroundDuration(int)
3338 * @see #getLastBackgroundDuration(int)
3339 * @see #getLastDuration(int, int, int)
3340 * @see OpEntry#getLastDuration(int)
3341 */
3342 public long getLastDuration(@OpFlags int flags) {
3343 return getLastDuration(MAX_PRIORITY_UID_STATE, MIN_PRIORITY_UID_STATE, flags);
3344 }
3345
3346 /**
3347 * Return the duration in milliseconds of the last foreground access.
3348 *
3349 * @param flags The op flags
3350 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003351 * @return the duration in milliseconds or {@code -1} if there was no foreground rejection
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003352 *
3353 * @see #getLastDuration(int)
3354 * @see #getLastBackgroundDuration(int)
3355 * @see #getLastDuration(int, int, int)
3356 * @see OpEntry#getLastForegroundDuration(int)
3357 */
3358 public long getLastForegroundDuration(@OpFlags int flags) {
3359 return getLastDuration(MAX_PRIORITY_UID_STATE, resolveFirstUnrestrictedUidState(mOp),
3360 flags);
3361 }
3362
3363 /**
3364 * Return the duration in milliseconds of the last background access.
3365 *
3366 * @param flags The op flags
3367 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003368 * @return the duration in milliseconds or {@code -1} if there was no background rejection
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003369 *
3370 * @see #getLastDuration(int)
3371 * @see #getLastForegroundDuration(int)
3372 * @see #getLastDuration(int, int, int)
3373 * @see OpEntry#getLastBackgroundDuration(int)
3374 */
3375 public long getLastBackgroundDuration(@OpFlags int flags) {
3376 return getLastDuration(resolveLastRestrictedUidState(mOp), MIN_PRIORITY_UID_STATE,
3377 flags);
3378 }
3379
3380 /**
3381 * Return the duration in milliseconds of the last access.
3382 *
3383 * @param fromUidState The lowest UID state for which to query
3384 * @param toUidState The highest UID state for which to query (inclusive)
3385 * @param flags The op flags
3386 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003387 * @return the duration in milliseconds or {@code -1} if there was no rejection
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003388 *
3389 * @see #getLastDuration(int)
3390 * @see #getLastForegroundDuration(int)
3391 * @see #getLastBackgroundDuration(int)
3392 * @see #getLastDuration(int, int, int)
3393 * @see OpEntry#getLastDuration(int, int, int)
3394 */
3395 public long getLastDuration(@UidState int fromUidState, @UidState int toUidState,
3396 @OpFlags int flags) {
3397 NoteOpEvent lastEvent = getLastAccessEvent(fromUidState, toUidState, flags);;
3398 if (lastEvent == null) {
3399 return -1;
3400 }
3401
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08003402 return lastEvent.getDuration();
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003403 }
3404
3405 /**
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003406 * Gets the proxy info of the app that performed the last access on behalf of this
3407 * attribution and as a result blamed the op on this attribution.
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003408 *
3409 * @param flags The op flags
3410 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003411 * @return The proxy info or {@code null} if there was no proxy access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003412 *
3413 * @see #getLastForegroundProxyInfo(int)
3414 * @see #getLastBackgroundProxyInfo(int)
3415 * @see #getLastProxyInfo(int, int, int)
3416 * @see OpEntry#getLastProxyInfo(int)
3417 */
3418 public @Nullable OpEventProxyInfo getLastProxyInfo(@OpFlags int flags) {
3419 return getLastProxyInfo(MAX_PRIORITY_UID_STATE, MIN_PRIORITY_UID_STATE, flags);
3420 }
3421
3422 /**
3423 * Gets the proxy info of the app that performed the last foreground access on behalf of
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003424 * this attribution and as a result blamed the op on this attribution.
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003425 *
3426 * @param flags The op flags
3427 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003428 * @return The proxy info or {@code null} if there was no proxy access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003429 *
3430 * @see #getLastProxyInfo(int)
3431 * @see #getLastBackgroundProxyInfo(int)
3432 * @see #getLastProxyInfo(int, int, int)
3433 * @see OpEntry#getLastForegroundProxyInfo(int)
3434 */
3435 public @Nullable OpEventProxyInfo getLastForegroundProxyInfo(@OpFlags int flags) {
3436 return getLastProxyInfo(MAX_PRIORITY_UID_STATE, resolveFirstUnrestrictedUidState(mOp),
3437 flags);
3438 }
3439
3440 /**
3441 * Gets the proxy info of the app that performed the last background access on behalf of
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003442 * this attribution and as a result blamed the op on this attribution.
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003443 *
3444 * @param flags The op flags
3445 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003446 * @return The proxy info or {@code null} if there was no proxy background access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003447 *
3448 * @see #getLastProxyInfo(int)
3449 * @see #getLastForegroundProxyInfo(int)
3450 * @see #getLastProxyInfo(int, int, int)
3451 * @see OpEntry#getLastBackgroundProxyInfo(int)
3452 */
3453 public @Nullable OpEventProxyInfo getLastBackgroundProxyInfo(@OpFlags int flags) {
3454 return getLastProxyInfo(resolveLastRestrictedUidState(mOp), MIN_PRIORITY_UID_STATE,
3455 flags);
3456 }
3457
3458 /**
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003459 * Gets the proxy info of the app that performed the last access on behalf of this
3460 * attribution and as a result blamed the op on this attribution.
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003461 *
3462 * @param fromUidState The lowest UID state for which to query
3463 * @param toUidState The highest UID state for which to query (inclusive)
3464 * @param flags The op flags
3465 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003466 * @return The proxy info or {@code null} if there was no proxy foreground access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003467 *
3468 * @see #getLastProxyInfo(int)
3469 * @see #getLastForegroundProxyInfo(int)
3470 * @see #getLastBackgroundProxyInfo(int)
3471 * @see OpEntry#getLastProxyInfo(int, int, int)
3472 */
3473 public @Nullable OpEventProxyInfo getLastProxyInfo(@UidState int fromUidState,
3474 @UidState int toUidState, @OpFlags int flags) {
3475 NoteOpEvent lastEvent = getLastAccessEvent(fromUidState, toUidState, flags);
3476 if (lastEvent == null) {
3477 return null;
3478 }
3479
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08003480 return lastEvent.getProxy();
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003481 }
3482
3483 private static class LongSparseArrayParceling implements
3484 Parcelling<LongSparseArray<NoteOpEvent>> {
3485 @Override
3486 public void parcel(@Nullable LongSparseArray<NoteOpEvent> array, @NonNull Parcel dest,
3487 int parcelFlags) {
3488 if (array == null) {
3489 dest.writeInt(-1);
3490 return;
3491 }
3492
3493 int numEntries = array.size();
3494 dest.writeInt(numEntries);
3495
3496 for (int i = 0; i < numEntries; i++) {
3497 dest.writeLong(array.keyAt(i));
3498 dest.writeParcelable(array.valueAt(i), parcelFlags);
3499 }
3500 }
3501
3502 @Override
3503 public @Nullable LongSparseArray<NoteOpEvent> unparcel(@NonNull Parcel source) {
3504 int numEntries = source.readInt();
3505 if (numEntries == -1) {
3506 return null;
3507 }
3508
3509 LongSparseArray<NoteOpEvent> array = new LongSparseArray<>(numEntries);
3510
3511 for (int i = 0; i < numEntries; i++) {
3512 array.put(source.readLong(), source.readParcelable(null));
3513 }
3514
3515 return array;
3516 }
3517 }
3518
3519
3520
3521 // Code below generated by codegen v1.0.14.
3522 //
3523 // DO NOT MODIFY!
3524 // CHECKSTYLE:OFF Generated code
3525 //
3526 // To regenerate run:
3527 // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/app/AppOpsManager.java
3528 //
3529 // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
3530 // Settings > Editor > Code Style > Formatter Control
3531 //@formatter:off
3532
3533
3534 /**
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003535 * Creates a new OpAttributionEntry.
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003536 *
3537 * @param op
3538 * The code of the op
3539 * @param running
3540 * Whether the op is running
3541 * @param accessEvents
3542 * The access events
3543 * @param rejectEvents
3544 * The rejection events
3545 * @hide
3546 */
3547 @DataClass.Generated.Member
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003548 public AttributedOpEntry(
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003549 @IntRange(from = 0, to = _NUM_OP - 1) int op,
3550 boolean running,
3551 @Nullable LongSparseArray<NoteOpEvent> accessEvents,
3552 @Nullable LongSparseArray<NoteOpEvent> rejectEvents) {
3553 this.mOp = op;
3554 com.android.internal.util.AnnotationValidations.validate(
3555 IntRange.class, null, mOp,
3556 "from", 0,
3557 "to", _NUM_OP - 1);
3558 this.mRunning = running;
3559 this.mAccessEvents = accessEvents;
3560 this.mRejectEvents = rejectEvents;
3561
3562 // onConstructed(); // You can define this method to get a callback
3563 }
3564
3565 /**
3566 * Whether the op is running
3567 */
3568 @DataClass.Generated.Member
3569 public boolean isRunning() {
3570 return mRunning;
3571 }
3572
3573 @DataClass.Generated.Member
3574 static Parcelling<LongSparseArray<NoteOpEvent>> sParcellingForAccessEvents =
3575 Parcelling.Cache.get(
3576 LongSparseArrayParceling.class);
3577 static {
3578 if (sParcellingForAccessEvents == null) {
3579 sParcellingForAccessEvents = Parcelling.Cache.put(
3580 new LongSparseArrayParceling());
3581 }
3582 }
3583
3584 @DataClass.Generated.Member
3585 static Parcelling<LongSparseArray<NoteOpEvent>> sParcellingForRejectEvents =
3586 Parcelling.Cache.get(
3587 LongSparseArrayParceling.class);
3588 static {
3589 if (sParcellingForRejectEvents == null) {
3590 sParcellingForRejectEvents = Parcelling.Cache.put(
3591 new LongSparseArrayParceling());
3592 }
3593 }
3594
3595 @Override
3596 @DataClass.Generated.Member
3597 public void writeToParcel(@NonNull Parcel dest, int flags) {
3598 // You can override field parcelling by defining methods like:
3599 // void parcelFieldName(Parcel dest, int flags) { ... }
3600
3601 byte flg = 0;
3602 if (mRunning) flg |= 0x2;
3603 if (mAccessEvents != null) flg |= 0x4;
3604 if (mRejectEvents != null) flg |= 0x8;
3605 dest.writeByte(flg);
3606 dest.writeInt(mOp);
3607 sParcellingForAccessEvents.parcel(mAccessEvents, dest, flags);
3608 sParcellingForRejectEvents.parcel(mRejectEvents, dest, flags);
3609 }
3610
3611 @Override
3612 @DataClass.Generated.Member
3613 public int describeContents() { return 0; }
3614
3615 /** @hide */
3616 @SuppressWarnings({"unchecked", "RedundantCast"})
3617 @DataClass.Generated.Member
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003618 /* package-private */ AttributedOpEntry(@NonNull Parcel in) {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003619 // You can override field unparcelling by defining methods like:
3620 // static FieldType unparcelFieldName(Parcel in) { ... }
3621
3622 byte flg = in.readByte();
3623 boolean running = (flg & 0x2) != 0;
3624 int op = in.readInt();
3625 LongSparseArray<NoteOpEvent> accessEvents = sParcellingForAccessEvents.unparcel(in);
3626 LongSparseArray<NoteOpEvent> rejectEvents = sParcellingForRejectEvents.unparcel(in);
3627
3628 this.mOp = op;
3629 com.android.internal.util.AnnotationValidations.validate(
3630 IntRange.class, null, mOp,
3631 "from", 0,
3632 "to", _NUM_OP - 1);
3633 this.mRunning = running;
3634 this.mAccessEvents = accessEvents;
3635 this.mRejectEvents = rejectEvents;
3636
3637 // onConstructed(); // You can define this method to get a callback
3638 }
3639
3640 @DataClass.Generated.Member
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003641 public static final @NonNull Parcelable.Creator<AttributedOpEntry> CREATOR
3642 = new Parcelable.Creator<AttributedOpEntry>() {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003643 @Override
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003644 public AttributedOpEntry[] newArray(int size) {
3645 return new AttributedOpEntry[size];
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003646 }
3647
3648 @Override
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003649 public AttributedOpEntry createFromParcel(@NonNull Parcel in) {
3650 return new AttributedOpEntry(in);
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003651 }
3652 };
3653
3654 /*
3655 @DataClass.Generated(
3656 time = 1574809856239L,
3657 codegenVersion = "1.0.14",
3658 sourceFile = "frameworks/base/core/java/android/app/AppOpsManager.java",
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003659 inputSignatures = "private final @android.annotation.IntRange(from=0L, to=_NUM_OP - 1) int mOp\nprivate final boolean mRunning\nprivate final @com.android.internal.util.DataClass.ParcelWith(android.app.OpAttributionEntry.LongSparseArrayParceling.class) @android.annotation.Nullable android.util.LongSparseArray<android.app.NoteOpEvent> mAccessEvents\nprivate final @com.android.internal.util.DataClass.ParcelWith(android.app.OpAttributionEntry.LongSparseArrayParceling.class) @android.annotation.Nullable android.util.LongSparseArray<android.app.NoteOpEvent> mRejectEvents\npublic @android.annotation.NonNull android.util.ArraySet<java.lang.Long> collectKeys()\npublic @android.app.UidState int getLastAccessUidState(int)\npublic @android.app.UidState int getLastForegroundAccessUidState(int)\npublic @android.app.UidState int getLastBackgroundAccessUidState(int)\npublic @android.app.UidState int getLastRejectUidState(int)\npublic @android.app.UidState int getLastForegroundRejectUidState(int)\npublic @android.app.UidState int getLastBackgroundRejectUidState(int)\npublic long getAccessTime(int,int)\npublic long getRejectTime(int,int)\npublic long getDuration(int,int)\npublic int getProxyUid(int,int)\npublic @android.annotation.Nullable java.lang.String getProxyPackageName(int,int)\npublic @android.annotation.Nullable java.lang.String getProxyAttributionTag(int,int)\nclass OpAttributionEntry extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genHiddenConstructor=true)")
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003660 @Deprecated
3661 private void __metadata() {}
3662 */
3663
3664
3665 //@formatter:on
3666 // End of generated code
3667
3668 }
3669
3670 /**
3671 * Last {@link #noteOp} and {@link #startOp} events performed for a single op for all uidModes
3672 * and opFlags.
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003673 *
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07003674 * @hide
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08003675 */
Svet Ganovaf189e32019-02-15 18:45:29 -08003676 @TestApi
3677 @Immutable
Dianne Hackbornf0c322a2018-06-13 15:24:04 -07003678 @SystemApi
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003679 // @DataClass(genHiddenConstructor = true) codegen verifier is broken
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003680 public static final class OpEntry implements Parcelable {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003681 /** The code of the op */
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003682 private final @IntRange(from = 0, to = _NUM_OP - 1) int mOp;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003683 /** The mode of the op */
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003684 private final @Mode int mMode;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003685 /** The attributed entries by attribution tag */
3686 private final @NonNull Map<String, AttributedOpEntry> mAttributedOpEntries;
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003687
3688 /**
3689 * @hide
3690 */
Philip P. Moltmannac443502020-05-11 13:15:12 -07003691 @UnsupportedAppUsage(/*maxTargetSdk = Build.VERSION_CODES.R,*/ publicAlternatives = "{@code "
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003692 + "#getOpStr()}")
3693 public int getOp() {
3694 return mOp;
3695 }
3696
3697 /**
3698 * @return This entry's op string name, such as {@link #OPSTR_COARSE_LOCATION}.
3699 */
3700 public @NonNull String getOpStr() {
3701 return sOpToString[mOp];
3702 }
3703
3704 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003705 * @hide
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003706 *
3707 * @deprecated Use {@link #getLastAccessTime(int)} instead
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003708 */
3709 @Deprecated
Philip P. Moltmannac443502020-05-11 13:15:12 -07003710 @UnsupportedAppUsage(/*maxTargetSdk = Build.VERSION_CODES.R,*/ publicAlternatives = "{@code "
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003711 + "#getLastAccessTime(int)}")
3712 public long getTime() {
3713 return getLastAccessTime(OP_FLAGS_ALL);
3714 }
3715
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003716 /**
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003717 * Return the last access time.
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003718 *
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003719 * @param flags The op flags
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003720 *
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003721 * @return the last access time (in milliseconds since epoch start (January 1, 1970
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003722 * 00:00:00.000 GMT - Gregorian)) or {@code -1} if there was no access
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003723 *
3724 * @see #getLastAccessForegroundTime(int)
3725 * @see #getLastAccessBackgroundTime(int)
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003726 * @see #getLastAccessTime(int, int, int)
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003727 * @see AttributedOpEntry#getLastAccessTime(int)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003728 */
3729 public long getLastAccessTime(@OpFlags int flags) {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003730 return getLastAccessTime(MAX_PRIORITY_UID_STATE, MIN_PRIORITY_UID_STATE, flags);
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003731 }
3732
3733 /**
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003734 * Return the last foreground access time.
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003735 *
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003736 * @param flags The op flags
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003737 *
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003738 * @return the last access time (in milliseconds since epoch start (January 1, 1970
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003739 * 00:00:00.000 GMT - Gregorian)) or {@code -1} if there was no foreground access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003740 *
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003741 * @see #getLastAccessTime(int)
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003742 * @see #getLastAccessBackgroundTime(int)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003743 * @see #getLastAccessTime(int, int, int)
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003744 * @see AttributedOpEntry#getLastAccessForegroundTime(int)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003745 */
3746 public long getLastAccessForegroundTime(@OpFlags int flags) {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003747 return getLastAccessTime(MAX_PRIORITY_UID_STATE, resolveFirstUnrestrictedUidState(mOp),
3748 flags);
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003749 }
3750
3751 /**
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003752 * Return the last background access time.
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003753 *
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003754 * @param flags The op flags
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003755 *
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003756 * @return the last access time (in milliseconds since epoch start (January 1, 1970
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003757 * 00:00:00.000 GMT - Gregorian)) or {@code -1} if there was no background access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003758 *
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003759 * @see #getLastAccessTime(int)
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003760 * @see #getLastAccessForegroundTime(int)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003761 * @see #getLastAccessTime(int, int, int)
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003762 * @see AttributedOpEntry#getLastAccessBackgroundTime(int)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003763 */
3764 public long getLastAccessBackgroundTime(@OpFlags int flags) {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003765 return getLastAccessTime(resolveLastRestrictedUidState(mOp), MIN_PRIORITY_UID_STATE,
3766 flags);
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003767 }
3768
3769 /**
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003770 * Return the last access event.
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003771 *
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003772 * @param flags The op flags
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003773 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003774 * @return the last access event of {@code null} if there was no access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003775 */
3776 private @Nullable NoteOpEvent getLastAccessEvent(@UidState int fromUidState,
3777 @UidState int toUidState, @OpFlags int flags) {
3778 NoteOpEvent lastAccessEvent = null;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003779 for (AttributedOpEntry attributionEntry : mAttributedOpEntries.values()) {
3780 NoteOpEvent lastAttributionAccessEvent = attributionEntry.getLastAccessEvent(
3781 fromUidState, toUidState, flags);
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003782
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003783 if (lastAccessEvent == null || (lastAttributionAccessEvent != null
3784 && lastAttributionAccessEvent.getNoteTime()
3785 > lastAccessEvent.getNoteTime())) {
3786 lastAccessEvent = lastAttributionAccessEvent;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003787 }
3788 }
3789
3790 return lastAccessEvent;
3791 }
3792
3793 /**
3794 * Return the last access time.
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003795 *
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003796 * @param fromUidState the lowest uid state to query
3797 * @param toUidState the highest uid state to query (inclusive)
3798 * @param flags The op flags
3799 *
3800 * @return the last access time (in milliseconds since epoch start (January 1, 1970
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003801 * 00:00:00.000 GMT - Gregorian)) or {@code -1} if there was no access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003802 *
3803 * @see #getLastAccessTime(int)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003804 * @see #getLastAccessForegroundTime(int)
3805 * @see #getLastAccessBackgroundTime(int)
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003806 * @see AttributedOpEntry#getLastAccessTime(int, int, int)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003807 */
3808 public long getLastAccessTime(@UidState int fromUidState, @UidState int toUidState,
3809 @OpFlags int flags) {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003810 NoteOpEvent lastEvent = getLastAccessEvent(fromUidState, toUidState, flags);;
3811
3812 if (lastEvent == null) {
3813 return -1;
3814 }
3815
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08003816 return lastEvent.getNoteTime();
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003817 }
3818
3819 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003820 * @hide
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003821 *
3822 * @deprecated Use {@link #getLastRejectTime(int)} instead
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003823 */
3824 @Deprecated
Philip P. Moltmannac443502020-05-11 13:15:12 -07003825 @UnsupportedAppUsage(/*maxTargetSdk = Build.VERSION_CODES.R,*/ publicAlternatives = "{@code "
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003826 + "#getLastRejectTime(int)}")
3827 public long getRejectTime() {
3828 return getLastRejectTime(OP_FLAGS_ALL);
3829 }
3830
3831 /**
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003832 * Return the last rejection time.
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003833 *
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003834 * @param flags The op flags
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003835 *
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003836 * @return the last rejection time (in milliseconds since epoch start (January 1, 1970
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003837 * 00:00:00.000 GMT - Gregorian)) or {@code -1} if there was no rejection
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003838 *
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003839 * @see #getLastRejectForegroundTime(int)
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003840 * @see #getLastRejectBackgroundTime(int)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003841 * @see #getLastRejectTime(int, int, int)
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003842 * @see AttributedOpEntry#getLastRejectTime(int)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003843 */
3844 public long getLastRejectTime(@OpFlags int flags) {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003845 return getLastRejectTime(MAX_PRIORITY_UID_STATE, MIN_PRIORITY_UID_STATE, flags);
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003846 }
3847
3848 /**
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003849 * Return the last foreground rejection time.
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003850 *
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003851 * @param flags The op flags
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003852 *
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003853 * @return the last rejection time (in milliseconds since epoch start (January 1, 1970
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003854 * 00:00:00.000 GMT - Gregorian)) or {@code -1} if there was no foreground rejection
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003855 *
3856 * @see #getLastRejectTime(int)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003857 * @see #getLastRejectBackgroundTime(int)
3858 * @see #getLastRejectTime(int, int, int)
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003859 * @see AttributedOpEntry#getLastRejectForegroundTime(int)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003860 */
3861 public long getLastRejectForegroundTime(@OpFlags int flags) {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003862 return getLastRejectTime(MAX_PRIORITY_UID_STATE, resolveFirstUnrestrictedUidState(mOp),
3863 flags);
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003864 }
3865
3866 /**
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003867 * Return the last background rejection time.
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003868 *
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003869 * @param flags The op flags
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003870 *
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003871 * @return the last rejection time (in milliseconds since epoch start (January 1, 1970
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003872 * 00:00:00.000 GMT - Gregorian)) or {@code -1} if there was no background rejection
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003873 *
3874 * @see #getLastRejectTime(int)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003875 * @see #getLastRejectForegroundTime(int)
3876 * @see #getLastRejectTime(int, int, int)
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003877 * @see AttributedOpEntry#getLastRejectBackgroundTime(int)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003878 */
3879 public long getLastRejectBackgroundTime(@OpFlags int flags) {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003880 return getLastRejectTime(resolveLastRestrictedUidState(mOp), MIN_PRIORITY_UID_STATE,
3881 flags);
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003882 }
3883
3884 /**
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003885 * Return the last rejection event.
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003886 *
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003887 * @param flags The op flags
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003888 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003889 * @return the last reject event of {@code null} if there was no rejection
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003890 */
3891 private @Nullable NoteOpEvent getLastRejectEvent(@UidState int fromUidState,
3892 @UidState int toUidState, @OpFlags int flags) {
3893 NoteOpEvent lastAccessEvent = null;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003894 for (AttributedOpEntry attributionEntry : mAttributedOpEntries.values()) {
3895 NoteOpEvent lastAttributionAccessEvent = attributionEntry.getLastRejectEvent(
3896 fromUidState, toUidState, flags);
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003897
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003898 if (lastAccessEvent == null || (lastAttributionAccessEvent != null
3899 && lastAttributionAccessEvent.getNoteTime()
3900 > lastAccessEvent.getNoteTime())) {
3901 lastAccessEvent = lastAttributionAccessEvent;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003902 }
3903 }
3904
3905 return lastAccessEvent;
3906 }
3907
3908 /**
3909 * Return the last rejection time.
3910 *
3911 * @param fromUidState the lowest uid state to query
3912 * @param toUidState the highest uid state to query (inclusive)
3913 * @param flags The op flags
3914 *
3915 * @return the last rejection time (in milliseconds since epoch start (January 1, 1970
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003916 * 00:00:00.000 GMT - Gregorian)) or {@code -1} if there was no rejection
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003917 *
3918 * @see #getLastRejectTime(int)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003919 * @see #getLastRejectForegroundTime(int)
3920 * @see #getLastRejectBackgroundTime(int)
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003921 * @see #getLastRejectTime(int, int, int)
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003922 * @see AttributedOpEntry#getLastRejectTime(int, int, int)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003923 */
3924 public long getLastRejectTime(@UidState int fromUidState, @UidState int toUidState,
3925 @OpFlags int flags) {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003926 NoteOpEvent lastEvent = getLastRejectEvent(fromUidState, toUidState, flags);
3927 if (lastEvent == null) {
3928 return -1;
3929 }
3930
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08003931 return lastEvent.getNoteTime();
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003932 }
3933
3934 /**
3935 * @return Whether the operation is running.
3936 */
3937 public boolean isRunning() {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003938 for (AttributedOpEntry opAttributionEntry : mAttributedOpEntries.values()) {
3939 if (opAttributionEntry.isRunning()) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003940 return true;
3941 }
3942 }
3943
3944 return false;
3945 }
3946
3947 /**
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003948 * @deprecated Use {@link #getLastDuration(int)} instead
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003949 */
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003950 @Deprecated
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003951 public long getDuration() {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003952 return getLastDuration(OP_FLAGS_ALL);
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003953 }
3954
3955 /**
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003956 * Return the duration in milliseconds of the last the access.
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003957 *
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003958 * @param flags The op flags
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003959 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003960 * @return the duration in milliseconds or {@code -1} if there was no access
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003961 *
3962 * @see #getLastForegroundDuration(int)
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003963 * @see #getLastBackgroundDuration(int)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003964 * @see #getLastDuration(int, int, int)
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003965 * @see AttributedOpEntry#getLastDuration(int)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003966 */
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003967 public long getLastDuration(@OpFlags int flags) {
3968 return getLastDuration(MAX_PRIORITY_UID_STATE, MIN_PRIORITY_UID_STATE, flags);
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003969 }
3970
3971 /**
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003972 * Return the duration in milliseconds of the last foreground access.
Philip P. Moltmann59076d82019-08-19 15:00:40 -07003973 *
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003974 * @param flags The op flags
3975 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003976 * @return the duration in milliseconds or {@code -1} if there was no foreground access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003977 *
3978 * @see #getLastDuration(int)
3979 * @see #getLastBackgroundDuration(int)
3980 * @see #getLastDuration(int, int, int)
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003981 * @see AttributedOpEntry#getLastForegroundDuration(int)
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003982 */
3983 public long getLastForegroundDuration(@OpFlags int flags) {
3984 return getLastDuration(MAX_PRIORITY_UID_STATE, resolveFirstUnrestrictedUidState(mOp),
3985 flags);
3986 }
3987
3988 /**
3989 * Return the duration in milliseconds of the last background access.
3990 *
3991 * @param flags The op flags
3992 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08003993 * @return the duration in milliseconds or {@code -1} if there was no background access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003994 *
3995 * @see #getLastDuration(int)
3996 * @see #getLastForegroundDuration(int)
3997 * @see #getLastDuration(int, int, int)
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08003998 * @see AttributedOpEntry#getLastBackgroundDuration(int)
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08003999 */
4000 public long getLastBackgroundDuration(@OpFlags int flags) {
4001 return getLastDuration(resolveLastRestrictedUidState(mOp), MIN_PRIORITY_UID_STATE,
4002 flags);
4003 }
4004
4005 /**
4006 * Return the duration in milliseconds of the last access.
4007 *
4008 * @param fromUidState The lowest UID state for which to query
4009 * @param toUidState The highest UID state for which to query (inclusive)
4010 * @param flags The op flags
4011 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08004012 * @return the duration in milliseconds or {@code -1} if there was no access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004013 *
4014 * @see #getLastDuration(int)
4015 * @see #getLastForegroundDuration(int)
4016 * @see #getLastBackgroundDuration(int)
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004017 * @see AttributedOpEntry#getLastDuration(int, int, int)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004018 */
4019 public long getLastDuration(@UidState int fromUidState, @UidState int toUidState,
4020 @OpFlags int flags) {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004021 NoteOpEvent lastEvent = getLastAccessEvent(fromUidState, toUidState, flags);
4022 if (lastEvent == null) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004023 return -1;
4024 }
4025
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08004026 return lastEvent.getDuration();
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004027 }
4028
4029 /**
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004030 * @deprecated Use {@link #getLastProxyInfo(int)} instead
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004031 */
4032 @Deprecated
4033 public int getProxyUid() {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004034 OpEventProxyInfo proxy = getLastProxyInfo(OP_FLAGS_ALL);
4035 if (proxy == null) {
4036 return Process.INVALID_UID;
4037 }
4038
4039 return proxy.getUid();
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004040 }
4041
4042 /**
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004043 * @deprecated Use {@link #getLastProxyInfo(int)} instead
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004044 */
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004045 @Deprecated
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004046 public int getProxyUid(@UidState int uidState, @OpFlags int flags) {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004047 OpEventProxyInfo proxy = getLastProxyInfo(uidState, uidState, flags);
4048 if (proxy == null) {
4049 return Process.INVALID_UID;
4050 }
4051
4052 return proxy.getUid();
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004053 }
4054
4055 /**
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004056 * @deprecated Use {@link #getLastProxyInfo(int)} instead
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004057 */
4058 @Deprecated
4059 public @Nullable String getProxyPackageName() {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004060 OpEventProxyInfo proxy = getLastProxyInfo(OP_FLAGS_ALL);
4061 if (proxy == null) {
4062 return null;
4063 }
4064
4065 return proxy.getPackageName();
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004066 }
4067
4068 /**
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004069 * @deprecated Use {@link #getLastProxyInfo(int)} instead
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004070 */
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004071 @Deprecated
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004072 public @Nullable String getProxyPackageName(@UidState int uidState, @OpFlags int flags) {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004073 OpEventProxyInfo proxy = getLastProxyInfo(uidState, uidState, flags);
4074 if (proxy == null) {
4075 return null;
4076 }
4077
4078 return proxy.getPackageName();
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004079 }
4080
4081 /**
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004082 * Gets the proxy info of the app that performed the last access on behalf of this app and
4083 * as a result blamed the op on this app.
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004084 *
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004085 * @param flags The op flags
4086 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08004087 * @return The proxy info or {@code null} if there was no proxy access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004088 *
4089 * @see #getLastForegroundProxyInfo(int)
4090 * @see #getLastBackgroundProxyInfo(int)
4091 * @see #getLastProxyInfo(int, int, int)
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004092 * @see AttributedOpEntry#getLastProxyInfo(int)
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004093 */
4094 public @Nullable OpEventProxyInfo getLastProxyInfo(@OpFlags int flags) {
4095 return getLastProxyInfo(MAX_PRIORITY_UID_STATE, MIN_PRIORITY_UID_STATE, flags);
4096 }
4097
4098 /**
4099 * Gets the proxy info of the app that performed the last foreground access on behalf of
4100 * this app and as a result blamed the op on this app.
4101 *
4102 * @param flags The op flags
4103 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08004104 * @return The proxy info or {@code null} if there was no foreground proxy access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004105 *
4106 * @see #getLastProxyInfo(int)
4107 * @see #getLastBackgroundProxyInfo(int)
4108 * @see #getLastProxyInfo(int, int, int)
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004109 * @see AttributedOpEntry#getLastForegroundProxyInfo(int)
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004110 */
4111 public @Nullable OpEventProxyInfo getLastForegroundProxyInfo(@OpFlags int flags) {
4112 return getLastProxyInfo(MAX_PRIORITY_UID_STATE, resolveFirstUnrestrictedUidState(mOp),
4113 flags);
4114 }
4115
4116 /**
4117 * Gets the proxy info of the app that performed the last background access on behalf of
4118 * this app and as a result blamed the op on this app.
4119 *
4120 * @param flags The op flags
4121 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08004122 * @return The proxy info or {@code null} if there was no background proxy access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004123 *
4124 * @see #getLastProxyInfo(int)
4125 * @see #getLastForegroundProxyInfo(int)
4126 * @see #getLastProxyInfo(int, int, int)
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004127 * @see AttributedOpEntry#getLastBackgroundProxyInfo(int)
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004128 */
4129 public @Nullable OpEventProxyInfo getLastBackgroundProxyInfo(@OpFlags int flags) {
4130 return getLastProxyInfo(resolveLastRestrictedUidState(mOp), MIN_PRIORITY_UID_STATE,
4131 flags);
4132 }
4133
4134 /**
4135 * Gets the proxy info of the app that performed the last access on behalf of this app and
4136 * as a result blamed the op on this app.
4137 *
4138 * @param fromUidState The lowest UID state for which to query
4139 * @param toUidState The highest UID state for which to query (inclusive)
4140 * @param flags The op flags
4141 *
Philip P. Moltmann7c0524f2020-02-06 09:21:31 -08004142 * @return The proxy info or {@code null} if there was no proxy access
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004143 *
4144 * @see #getLastProxyInfo(int)
4145 * @see #getLastForegroundProxyInfo(int)
4146 * @see #getLastBackgroundProxyInfo(int)
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004147 * @see AttributedOpEntry#getLastProxyInfo(int, int, int)
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004148 */
4149 public @Nullable OpEventProxyInfo getLastProxyInfo(@UidState int fromUidState,
4150 @UidState int toUidState, @OpFlags int flags) {
4151 NoteOpEvent lastEvent = getLastAccessEvent(fromUidState, toUidState, flags);
4152 if (lastEvent == null) {
4153 return null;
4154 }
4155
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08004156 return lastEvent.getProxy();
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004157 }
4158
4159
4160
4161 // Code below generated by codegen v1.0.14.
4162 //
4163 // DO NOT MODIFY!
4164 // CHECKSTYLE:OFF Generated code
4165 //
4166 // To regenerate run:
4167 // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/app/AppOpsManager.java
4168 //
4169 // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
4170 // Settings > Editor > Code Style > Formatter Control
4171 //@formatter:off
4172
4173
4174 /**
4175 * Creates a new OpEntry.
4176 *
4177 * @param op
4178 * The code of the op
4179 * @param mode
4180 * The mode of the op
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004181 * @param attributedOpEntries
4182 * The attributions that have been used when noting the op
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004183 * @hide
4184 */
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004185 @DataClass.Generated.Member
4186 public OpEntry(
4187 @IntRange(from = 0, to = _NUM_OP - 1) int op,
4188 @Mode int mode,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004189 @NonNull Map<String, AttributedOpEntry> attributedOpEntries) {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004190 this.mOp = op;
4191 com.android.internal.util.AnnotationValidations.validate(
4192 IntRange.class, null, mOp,
4193 "from", 0,
4194 "to", _NUM_OP - 1);
4195 this.mMode = mode;
4196 com.android.internal.util.AnnotationValidations.validate(
4197 Mode.class, null, mMode);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004198 this.mAttributedOpEntries = attributedOpEntries;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004199 com.android.internal.util.AnnotationValidations.validate(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004200 NonNull.class, null, mAttributedOpEntries);
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004201
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004202 // onConstructed(); // You can define this method to get a callback
4203 }
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004204
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004205 /**
4206 * The mode of the op
4207 */
4208 @DataClass.Generated.Member
4209 public @Mode int getMode() {
4210 return mMode;
4211 }
4212
4213 /**
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004214 * The attributed entries keyed by attribution tag.
Philip P. Moltmann3f9ee362020-02-06 09:32:28 -08004215 *
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004216 * @see Context#createAttributionContext(String)
Philip P. Moltmann3f9ee362020-02-06 09:32:28 -08004217 * @see #noteOp(String, int, String, String, String)
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004218 */
4219 @DataClass.Generated.Member
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004220 public @NonNull Map<String, AttributedOpEntry> getAttributedOpEntries() {
4221 return mAttributedOpEntries;
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004222 }
4223
Dianne Hackborn35654b62013-01-14 17:38:02 -08004224 @Override
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004225 @DataClass.Generated.Member
Dianne Hackborn35654b62013-01-14 17:38:02 -08004226 public void writeToParcel(Parcel dest, int flags) {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004227 // You can override field parcelling by defining methods like:
4228 // void parcelFieldName(Parcel dest, int flags) { ... }
4229
Dianne Hackborn35654b62013-01-14 17:38:02 -08004230 dest.writeInt(mOp);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08004231 dest.writeInt(mMode);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004232 dest.writeMap(mAttributedOpEntries);
Dianne Hackborn35654b62013-01-14 17:38:02 -08004233 }
4234
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004235 @Override
4236 @DataClass.Generated.Member
4237 public int describeContents() { return 0; }
4238
4239 /** @hide */
4240 @SuppressWarnings({"unchecked", "RedundantCast"})
4241 @DataClass.Generated.Member
4242 /* package-private */ OpEntry(@NonNull Parcel in) {
4243 // You can override field unparcelling by defining methods like:
4244 // static FieldType unparcelFieldName(Parcel in) { ... }
4245
4246 int op = in.readInt();
4247 int mode = in.readInt();
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004248 Map<String, AttributedOpEntry> attributions = new java.util.LinkedHashMap<>();
4249 in.readMap(attributions, AttributedOpEntry.class.getClassLoader());
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004250
4251 this.mOp = op;
4252 com.android.internal.util.AnnotationValidations.validate(
4253 IntRange.class, null, mOp,
4254 "from", 0,
4255 "to", _NUM_OP - 1);
4256 this.mMode = mode;
4257 com.android.internal.util.AnnotationValidations.validate(
4258 Mode.class, null, mMode);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004259 this.mAttributedOpEntries = attributions;
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004260 com.android.internal.util.AnnotationValidations.validate(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004261 NonNull.class, null, mAttributedOpEntries);
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004262
4263 // onConstructed(); // You can define this method to get a callback
4264 }
4265
4266 @DataClass.Generated.Member
4267 public static final @NonNull Parcelable.Creator<OpEntry> CREATOR
4268 = new Parcelable.Creator<OpEntry>() {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004269 @Override
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004270 public OpEntry[] newArray(int size) {
4271 return new OpEntry[size];
Dianne Hackborn35654b62013-01-14 17:38:02 -08004272 }
4273
Philip P. Moltmann59076d82019-08-19 15:00:40 -07004274 @Override
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004275 public OpEntry createFromParcel(@NonNull Parcel in) {
4276 return new OpEntry(in);
Dianne Hackborn35654b62013-01-14 17:38:02 -08004277 }
4278 };
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004279
4280 /*
4281 @DataClass.Generated(
4282 time = 1574809856259L,
4283 codegenVersion = "1.0.14",
4284 sourceFile = "frameworks/base/core/java/android/app/AppOpsManager.java",
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004285 inputSignatures = "private final @android.annotation.IntRange(from=0L, to=_NUM_OP - 1) int mOp\nprivate final @android.app.Mode int mMode\nprivate final @android.annotation.NonNull java.util.Map<java.lang.String,android.app.OpAttributionEntry> mAttributions\npublic @android.annotation.UnsupportedAppUsage(maxTargetSdk=Build.VERSION_CODES.Q, publicAlternatives=\"{@code \" + \"#getOpStr()}\") int getOp()\npublic @android.annotation.NonNull java.lang.String getOpStr()\npublic @java.lang.Deprecated @android.annotation.UnsupportedAppUsage(maxTargetSdk=Build.VERSION_CODES.Q, publicAlternatives=\"{@code \" + \"#getAccessTime(int, int)}\") long getTime()\npublic @java.lang.Deprecated long getLastAccessTime(int)\npublic @java.lang.Deprecated long getLastAccessForegroundTime(int)\npublic @java.lang.Deprecated long getLastAccessBackgroundTime(int)\npublic @java.lang.Deprecated long getLastAccessTime(int,int,int)\npublic @java.lang.Deprecated @android.annotation.UnsupportedAppUsage(maxTargetSdk=Build.VERSION_CODES.Q, publicAlternatives=\"{@code \" + \"#getLastRejectTime(int, int, int)}\") long getRejectTime()\npublic @java.lang.Deprecated long getLastRejectTime(int)\npublic @java.lang.Deprecated long getLastRejectForegroundTime(int)\npublic @java.lang.Deprecated long getLastRejectBackgroundTime(int)\npublic @java.lang.Deprecated long getLastRejectTime(int,int,int)\npublic long getAccessTime(int,int)\npublic long getRejectTime(int,int)\npublic boolean isRunning()\nprivate android.app.NoteOpEvent getLastAccessEvent(int,int,int)\npublic @java.lang.Deprecated long getDuration()\npublic @java.lang.Deprecated long getLastForegroundDuration(int)\npublic @java.lang.Deprecated long getLastBackgroundDuration(int)\npublic @java.lang.Deprecated long getLastDuration(int,int,int)\npublic @java.lang.Deprecated int getProxyUid()\npublic @java.lang.Deprecated @android.annotation.Nullable java.lang.String getProxyPackageName()\nprivate @android.app.UidState int getLastAccessUidStateForFlagsInStatesOfAllAttributions(int,int,int)\npublic @android.app.UidState int getLastAccessUidState(int)\npublic @android.app.UidState int getLastForegroundAccessUidState(int)\npublic @android.app.UidState int getLastBackgroundAccessUidState(int)\nprivate @android.app.UidState int getLastRejectUidStateForFlagsInStatesOfAllAttributions(int,int,int)\npublic @android.app.UidState int getLastRejectUidState(int)\npublic @android.app.UidState int getLastForegroundRejectUidState(int)\npublic @android.app.UidState int getLastBackgroundRejectUidState(int)\npublic long getDuration(int,int)\npublic int getProxyUid(int,int)\nprivate int getProxyUid(int,int,int)\npublic @android.annotation.Nullable java.lang.String getProxyPackageName(int,int)\nprivate @android.annotation.Nullable java.lang.String getProxyPackageName(int,int,int)\nclass OpEntry extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genHiddenConstructor=true)")
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08004286 @Deprecated
4287 private void __metadata() {}
4288 */
4289
4290
4291 //@formatter:on
4292 // End of generated code
4293
Dianne Hackborn35654b62013-01-14 17:38:02 -08004294 }
4295
Svet Ganov8455ba22019-01-02 13:05:56 -08004296 /** @hide */
4297 public interface HistoricalOpsVisitor {
4298 void visitHistoricalOps(@NonNull HistoricalOps ops);
4299 void visitHistoricalUidOps(@NonNull HistoricalUidOps ops);
4300 void visitHistoricalPackageOps(@NonNull HistoricalPackageOps ops);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004301 void visitHistoricalAttributionOps(@NonNull AttributedHistoricalOps ops);
Svet Ganov8455ba22019-01-02 13:05:56 -08004302 void visitHistoricalOp(@NonNull HistoricalOp ops);
4303 }
4304
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08004305 /**
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004306 * Specifies what parameters to filter historical appop requests for
4307 *
4308 * @hide
4309 */
4310 @Retention(RetentionPolicy.SOURCE)
4311 @IntDef(flag = true, prefix = { "FILTER_BY_" }, value = {
4312 FILTER_BY_UID,
4313 FILTER_BY_PACKAGE_NAME,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004314 FILTER_BY_ATTRIBUTION_TAG,
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004315 FILTER_BY_OP_NAMES
4316 })
4317 public @interface HistoricalOpsRequestFilter {}
4318
4319 /**
4320 * Filter historical appop request by uid.
4321 *
4322 * @hide
4323 */
4324 public static final int FILTER_BY_UID = 1<<0;
4325
4326 /**
4327 * Filter historical appop request by package name.
4328 *
4329 * @hide
4330 */
4331 public static final int FILTER_BY_PACKAGE_NAME = 1<<1;
4332
4333 /**
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004334 * Filter historical appop request by attribution tag.
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004335 *
4336 * @hide
4337 */
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004338 public static final int FILTER_BY_ATTRIBUTION_TAG = 1<<2;
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004339
4340 /**
4341 * Filter historical appop request by op names.
4342 *
4343 * @hide
4344 */
4345 public static final int FILTER_BY_OP_NAMES = 1<<3;
4346
4347 /**
Svet Ganov23c88db2019-01-22 20:38:11 -08004348 * Request for getting historical app op usage. The request acts
4349 * as a filtering criteria when querying historical op usage.
4350 *
4351 * @hide
4352 */
4353 @Immutable
4354 @TestApi
4355 @SystemApi
4356 public static final class HistoricalOpsRequest {
4357 private final int mUid;
4358 private final @Nullable String mPackageName;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004359 private final @Nullable String mAttributionTag;
Svet Ganov23c88db2019-01-22 20:38:11 -08004360 private final @Nullable List<String> mOpNames;
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004361 private final @HistoricalOpsRequestFilter int mFilter;
Svet Ganov23c88db2019-01-22 20:38:11 -08004362 private final long mBeginTimeMillis;
4363 private final long mEndTimeMillis;
Svet Ganovaf189e32019-02-15 18:45:29 -08004364 private final @OpFlags int mFlags;
Svet Ganov23c88db2019-01-22 20:38:11 -08004365
4366 private HistoricalOpsRequest(int uid, @Nullable String packageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004367 @Nullable String attributionTag, @Nullable List<String> opNames,
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004368 @HistoricalOpsRequestFilter int filter, long beginTimeMillis,
4369 long endTimeMillis, @OpFlags int flags) {
Svet Ganov23c88db2019-01-22 20:38:11 -08004370 mUid = uid;
4371 mPackageName = packageName;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004372 mAttributionTag = attributionTag;
Svet Ganov23c88db2019-01-22 20:38:11 -08004373 mOpNames = opNames;
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004374 mFilter = filter;
Svet Ganov23c88db2019-01-22 20:38:11 -08004375 mBeginTimeMillis = beginTimeMillis;
4376 mEndTimeMillis = endTimeMillis;
Svet Ganovaf189e32019-02-15 18:45:29 -08004377 mFlags = flags;
Svet Ganov23c88db2019-01-22 20:38:11 -08004378 }
4379
4380 /**
4381 * Builder for creating a {@link HistoricalOpsRequest}.
4382 *
4383 * @hide
4384 */
4385 @TestApi
4386 @SystemApi
4387 public static final class Builder {
4388 private int mUid = Process.INVALID_UID;
4389 private @Nullable String mPackageName;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004390 private @Nullable String mAttributionTag;
Svet Ganov23c88db2019-01-22 20:38:11 -08004391 private @Nullable List<String> mOpNames;
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004392 private @HistoricalOpsRequestFilter int mFilter;
Svet Ganov23c88db2019-01-22 20:38:11 -08004393 private final long mBeginTimeMillis;
4394 private final long mEndTimeMillis;
Svet Ganovaf189e32019-02-15 18:45:29 -08004395 private @OpFlags int mFlags = OP_FLAGS_ALL;
Svet Ganov23c88db2019-01-22 20:38:11 -08004396
4397 /**
4398 * Creates a new builder.
4399 *
4400 * @param beginTimeMillis The beginning of the interval in milliseconds since
4401 * epoch start (January 1, 1970 00:00:00.000 GMT - Gregorian). Must be non
4402 * negative.
4403 * @param endTimeMillis The end of the interval in milliseconds since
4404 * epoch start (January 1, 1970 00:00:00.000 GMT - Gregorian). Must be after
4405 * {@code beginTimeMillis}. Pass {@link Long#MAX_VALUE} to get the most recent
4406 * history including ops that happen while this call is in flight.
4407 */
4408 public Builder(long beginTimeMillis, long endTimeMillis) {
4409 Preconditions.checkArgument(beginTimeMillis >= 0 && beginTimeMillis < endTimeMillis,
4410 "beginTimeMillis must be non negative and lesser than endTimeMillis");
4411 mBeginTimeMillis = beginTimeMillis;
4412 mEndTimeMillis = endTimeMillis;
4413 }
4414
4415 /**
4416 * Sets the UID to query for.
4417 *
4418 * @param uid The uid. Pass {@link android.os.Process#INVALID_UID} for any uid.
4419 * @return This builder.
4420 */
4421 public @NonNull Builder setUid(int uid) {
4422 Preconditions.checkArgument(uid == Process.INVALID_UID || uid >= 0,
4423 "uid must be " + Process.INVALID_UID + " or non negative");
4424 mUid = uid;
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004425
4426 if (uid == Process.INVALID_UID) {
4427 mFilter &= ~FILTER_BY_UID;
4428 } else {
4429 mFilter |= FILTER_BY_UID;
4430 }
4431
Svet Ganov23c88db2019-01-22 20:38:11 -08004432 return this;
4433 }
4434
4435 /**
4436 * Sets the package to query for.
4437 *
4438 * @param packageName The package name. <code>Null</code> for any package.
4439 * @return This builder.
4440 */
4441 public @NonNull Builder setPackageName(@Nullable String packageName) {
4442 mPackageName = packageName;
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004443
4444 if (packageName == null) {
4445 mFilter &= ~FILTER_BY_PACKAGE_NAME;
4446 } else {
4447 mFilter |= FILTER_BY_PACKAGE_NAME;
4448 }
4449
4450 return this;
4451 }
4452
4453 /**
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004454 * Sets the attribution tag to query for.
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004455 *
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004456 * @param attributionTag attribution tag
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004457 * @return This builder.
4458 */
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004459 public @NonNull Builder setAttributionTag(@Nullable String attributionTag) {
4460 mAttributionTag = attributionTag;
4461 mFilter |= FILTER_BY_ATTRIBUTION_TAG;
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004462
Svet Ganov23c88db2019-01-22 20:38:11 -08004463 return this;
4464 }
4465
4466 /**
4467 * Sets the op names to query for.
4468 *
4469 * @param opNames The op names. <code>Null</code> for any op.
4470 * @return This builder.
4471 */
4472 public @NonNull Builder setOpNames(@Nullable List<String> opNames) {
4473 if (opNames != null) {
4474 final int opCount = opNames.size();
4475 for (int i = 0; i < opCount; i++) {
4476 Preconditions.checkArgument(AppOpsManager.strOpToOp(
4477 opNames.get(i)) != AppOpsManager.OP_NONE);
4478 }
4479 }
4480 mOpNames = opNames;
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004481
4482 if (mOpNames == null) {
4483 mFilter &= ~FILTER_BY_OP_NAMES;
4484 } else {
4485 mFilter |= FILTER_BY_OP_NAMES;
4486 }
4487
Svet Ganov23c88db2019-01-22 20:38:11 -08004488 return this;
4489 }
4490
4491 /**
Svet Ganovaf189e32019-02-15 18:45:29 -08004492 * Sets the op flags to query for. The flags specify the type of
4493 * op data being queried.
4494 *
4495 * @param flags The flags which are any combination of
4496 * {@link #OP_FLAG_SELF}, {@link #OP_FLAG_TRUSTED_PROXY},
4497 * {@link #OP_FLAG_UNTRUSTED_PROXY}, {@link #OP_FLAG_TRUSTED_PROXIED},
4498 * {@link #OP_FLAG_UNTRUSTED_PROXIED}. You can use {@link #OP_FLAGS_ALL}
4499 * for any flag.
4500 * @return This builder.
4501 */
4502 public @NonNull Builder setFlags(@OpFlags int flags) {
4503 Preconditions.checkFlagsArgument(flags, OP_FLAGS_ALL);
4504 mFlags = flags;
4505 return this;
4506 }
4507
4508 /**
Svet Ganov23c88db2019-01-22 20:38:11 -08004509 * @return a new {@link HistoricalOpsRequest}.
4510 */
4511 public @NonNull HistoricalOpsRequest build() {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004512 return new HistoricalOpsRequest(mUid, mPackageName, mAttributionTag, mOpNames,
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004513 mFilter, mBeginTimeMillis, mEndTimeMillis, mFlags);
Svet Ganov23c88db2019-01-22 20:38:11 -08004514 }
4515 }
4516 }
4517
4518 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08004519 * This class represents historical app op state of all UIDs for a given time interval.
4520 *
4521 * @hide
4522 */
4523 @TestApi
4524 @SystemApi
4525 public static final class HistoricalOps implements Parcelable {
4526 private long mBeginTimeMillis;
4527 private long mEndTimeMillis;
4528 private @Nullable SparseArray<HistoricalUidOps> mHistoricalUidOps;
4529
4530 /** @hide */
4531 @TestApi
4532 public HistoricalOps(long beginTimeMillis, long endTimeMillis) {
4533 Preconditions.checkState(beginTimeMillis <= endTimeMillis);
4534 mBeginTimeMillis = beginTimeMillis;
4535 mEndTimeMillis = endTimeMillis;
4536 }
4537
4538 /** @hide */
4539 public HistoricalOps(@NonNull HistoricalOps other) {
4540 mBeginTimeMillis = other.mBeginTimeMillis;
4541 mEndTimeMillis = other.mEndTimeMillis;
4542 Preconditions.checkState(mBeginTimeMillis <= mEndTimeMillis);
4543 if (other.mHistoricalUidOps != null) {
4544 final int opCount = other.getUidCount();
4545 for (int i = 0; i < opCount; i++) {
4546 final HistoricalUidOps origOps = other.getUidOpsAt(i);
4547 final HistoricalUidOps clonedOps = new HistoricalUidOps(origOps);
4548 if (mHistoricalUidOps == null) {
4549 mHistoricalUidOps = new SparseArray<>(opCount);
4550 }
4551 mHistoricalUidOps.put(clonedOps.getUid(), clonedOps);
4552 }
4553 }
4554 }
4555
4556 private HistoricalOps(Parcel parcel) {
4557 mBeginTimeMillis = parcel.readLong();
4558 mEndTimeMillis = parcel.readLong();
4559 final int[] uids = parcel.createIntArray();
4560 if (!ArrayUtils.isEmpty(uids)) {
4561 final ParceledListSlice<HistoricalUidOps> listSlice = parcel.readParcelable(
4562 HistoricalOps.class.getClassLoader());
4563 final List<HistoricalUidOps> uidOps = (listSlice != null)
4564 ? listSlice.getList() : null;
4565 if (uidOps == null) {
4566 return;
4567 }
4568 for (int i = 0; i < uids.length; i++) {
4569 if (mHistoricalUidOps == null) {
4570 mHistoricalUidOps = new SparseArray<>();
4571 }
4572 mHistoricalUidOps.put(uids[i], uidOps.get(i));
4573 }
4574 }
4575 }
4576
4577 /**
4578 * Splice a piece from the beginning of these ops.
4579 *
4580 * @param splicePoint The fraction of the data to be spliced off.
4581 *
4582 * @hide
4583 */
4584 public @NonNull HistoricalOps spliceFromBeginning(double splicePoint) {
4585 return splice(splicePoint, true);
4586 }
4587
4588 /**
4589 * Splice a piece from the end of these ops.
4590 *
4591 * @param fractionToRemove The fraction of the data to be spliced off.
4592 *
4593 * @hide
4594 */
4595 public @NonNull HistoricalOps spliceFromEnd(double fractionToRemove) {
4596 return splice(fractionToRemove, false);
4597 }
4598
4599 /**
4600 * Splice a piece from the beginning or end of these ops.
4601 *
4602 * @param fractionToRemove The fraction of the data to be spliced off.
4603 * @param beginning Whether to splice off the beginning or the end.
4604 *
4605 * @return The spliced off part.
4606 *
4607 * @hide
4608 */
4609 private @Nullable HistoricalOps splice(double fractionToRemove, boolean beginning) {
4610 final long spliceBeginTimeMills;
4611 final long spliceEndTimeMills;
4612 if (beginning) {
4613 spliceBeginTimeMills = mBeginTimeMillis;
4614 spliceEndTimeMills = (long) (mBeginTimeMillis
4615 + getDurationMillis() * fractionToRemove);
4616 mBeginTimeMillis = spliceEndTimeMills;
4617 } else {
4618 spliceBeginTimeMills = (long) (mEndTimeMillis
4619 - getDurationMillis() * fractionToRemove);
4620 spliceEndTimeMills = mEndTimeMillis;
4621 mEndTimeMillis = spliceBeginTimeMills;
4622 }
4623
4624 HistoricalOps splice = null;
4625 final int uidCount = getUidCount();
4626 for (int i = 0; i < uidCount; i++) {
4627 final HistoricalUidOps origOps = getUidOpsAt(i);
4628 final HistoricalUidOps spliceOps = origOps.splice(fractionToRemove);
4629 if (spliceOps != null) {
4630 if (splice == null) {
4631 splice = new HistoricalOps(spliceBeginTimeMills, spliceEndTimeMills);
4632 }
4633 if (splice.mHistoricalUidOps == null) {
4634 splice.mHistoricalUidOps = new SparseArray<>();
4635 }
4636 splice.mHistoricalUidOps.put(spliceOps.getUid(), spliceOps);
4637 }
4638 }
4639 return splice;
4640 }
4641
4642 /**
4643 * Merge the passed ops into the current ones. The time interval is a
4644 * union of the current and passed in one and the passed in data is
4645 * folded into the data of this instance.
4646 *
4647 * @hide
4648 */
4649 public void merge(@NonNull HistoricalOps other) {
4650 mBeginTimeMillis = Math.min(mBeginTimeMillis, other.mBeginTimeMillis);
4651 mEndTimeMillis = Math.max(mEndTimeMillis, other.mEndTimeMillis);
4652 final int uidCount = other.getUidCount();
4653 for (int i = 0; i < uidCount; i++) {
4654 final HistoricalUidOps otherUidOps = other.getUidOpsAt(i);
4655 final HistoricalUidOps thisUidOps = getUidOps(otherUidOps.getUid());
4656 if (thisUidOps != null) {
4657 thisUidOps.merge(otherUidOps);
4658 } else {
4659 if (mHistoricalUidOps == null) {
4660 mHistoricalUidOps = new SparseArray<>();
4661 }
4662 mHistoricalUidOps.put(otherUidOps.getUid(), otherUidOps);
4663 }
4664 }
4665 }
4666
4667 /**
4668 * AppPermissionUsage the ops to leave only the data we filter for.
4669 *
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004670 * @param uid Uid to filter for.
4671 * @param packageName Package to filter for.
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004672 * @param attributionTag attribution tag to filter for
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004673 * @param opNames Ops to filter for.
4674 * @param filter Which parameters to filter on.
Svet Ganov8455ba22019-01-02 13:05:56 -08004675 * @param beginTimeMillis The begin time to filter for or {@link Long#MIN_VALUE} for all.
4676 * @param endTimeMillis The end time to filter for or {@link Long#MAX_VALUE} for all.
4677 *
4678 * @hide
4679 */
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004680 public void filter(int uid, @Nullable String packageName, @Nullable String attributionTag,
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004681 @Nullable String[] opNames, @HistoricalOpsRequestFilter int filter,
Svet Ganov8455ba22019-01-02 13:05:56 -08004682 long beginTimeMillis, long endTimeMillis) {
4683 final long durationMillis = getDurationMillis();
4684 mBeginTimeMillis = Math.max(mBeginTimeMillis, beginTimeMillis);
4685 mEndTimeMillis = Math.min(mEndTimeMillis, endTimeMillis);
4686 final double scaleFactor = Math.min((double) (endTimeMillis - beginTimeMillis)
4687 / (double) durationMillis, 1);
4688 final int uidCount = getUidCount();
4689 for (int i = uidCount - 1; i >= 0; i--) {
4690 final HistoricalUidOps uidOp = mHistoricalUidOps.valueAt(i);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004691 if ((filter & FILTER_BY_UID) != 0 && uid != uidOp.getUid()) {
Svet Ganov8455ba22019-01-02 13:05:56 -08004692 mHistoricalUidOps.removeAt(i);
4693 } else {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004694 uidOp.filter(packageName, attributionTag, opNames, filter, scaleFactor);
Stanislav Zholnin0d9499c2020-01-16 13:39:50 +00004695 if (uidOp.getPackageCount() == 0) {
4696 mHistoricalUidOps.removeAt(i);
4697 }
Svet Ganov8455ba22019-01-02 13:05:56 -08004698 }
4699 }
4700 }
4701
4702 /** @hide */
4703 public boolean isEmpty() {
4704 if (getBeginTimeMillis() >= getEndTimeMillis()) {
4705 return true;
4706 }
4707 final int uidCount = getUidCount();
4708 for (int i = uidCount - 1; i >= 0; i--) {
4709 final HistoricalUidOps uidOp = mHistoricalUidOps.valueAt(i);
4710 if (!uidOp.isEmpty()) {
4711 return false;
4712 }
4713 }
4714 return true;
4715 }
4716
4717 /** @hide */
4718 public long getDurationMillis() {
4719 return mEndTimeMillis - mBeginTimeMillis;
4720 }
4721
4722 /** @hide */
4723 @TestApi
4724 public void increaseAccessCount(int opCode, int uid, @NonNull String packageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004725 @Nullable String attributionTag, @UidState int uidState, @OpFlags int flags,
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004726 long increment) {
Svet Ganov8455ba22019-01-02 13:05:56 -08004727 getOrCreateHistoricalUidOps(uid).increaseAccessCount(opCode,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004728 packageName, attributionTag, uidState, flags, increment);
Svet Ganov8455ba22019-01-02 13:05:56 -08004729 }
4730
4731 /** @hide */
4732 @TestApi
4733 public void increaseRejectCount(int opCode, int uid, @NonNull String packageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004734 @Nullable String attributionTag, @UidState int uidState, @OpFlags int flags,
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004735 long increment) {
Svet Ganov8455ba22019-01-02 13:05:56 -08004736 getOrCreateHistoricalUidOps(uid).increaseRejectCount(opCode,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004737 packageName, attributionTag, uidState, flags, increment);
Svet Ganov8455ba22019-01-02 13:05:56 -08004738 }
4739
4740 /** @hide */
4741 @TestApi
4742 public void increaseAccessDuration(int opCode, int uid, @NonNull String packageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004743 @Nullable String attributionTag, @UidState int uidState, @OpFlags int flags,
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08004744 long increment) {
Svet Ganov8455ba22019-01-02 13:05:56 -08004745 getOrCreateHistoricalUidOps(uid).increaseAccessDuration(opCode,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08004746 packageName, attributionTag, uidState, flags, increment);
Svet Ganov8455ba22019-01-02 13:05:56 -08004747 }
4748
4749 /** @hide */
4750 @TestApi
4751 public void offsetBeginAndEndTime(long offsetMillis) {
4752 mBeginTimeMillis += offsetMillis;
4753 mEndTimeMillis += offsetMillis;
4754 }
4755
4756 /** @hide */
4757 public void setBeginAndEndTime(long beginTimeMillis, long endTimeMillis) {
4758 mBeginTimeMillis = beginTimeMillis;
4759 mEndTimeMillis = endTimeMillis;
4760 }
4761
4762 /** @hide */
4763 public void setBeginTime(long beginTimeMillis) {
4764 mBeginTimeMillis = beginTimeMillis;
4765 }
4766
4767 /** @hide */
4768 public void setEndTime(long endTimeMillis) {
4769 mEndTimeMillis = endTimeMillis;
4770 }
4771
4772 /**
4773 * @return The beginning of the interval in milliseconds since
4774 * epoch start (January 1, 1970 00:00:00.000 GMT - Gregorian).
4775 */
4776 public long getBeginTimeMillis() {
4777 return mBeginTimeMillis;
4778 }
4779
4780 /**
4781 * @return The end of the interval in milliseconds since
4782 * epoch start (January 1, 1970 00:00:00.000 GMT - Gregorian).
4783 */
4784 public long getEndTimeMillis() {
4785 return mEndTimeMillis;
4786 }
4787
4788 /**
4789 * Gets number of UIDs with historical ops.
4790 *
4791 * @return The number of UIDs with historical ops.
4792 *
4793 * @see #getUidOpsAt(int)
4794 */
Svet Ganov00a46ef2019-03-29 21:13:03 -07004795 public @IntRange(from = 0) int getUidCount() {
Svet Ganov8455ba22019-01-02 13:05:56 -08004796 if (mHistoricalUidOps == null) {
4797 return 0;
4798 }
4799 return mHistoricalUidOps.size();
4800 }
4801
4802 /**
4803 * Gets the historical UID ops at a given index.
4804 *
4805 * @param index The index.
4806 *
4807 * @return The historical UID ops at the given index.
4808 *
4809 * @see #getUidCount()
4810 */
Svet Ganov00a46ef2019-03-29 21:13:03 -07004811 public @NonNull HistoricalUidOps getUidOpsAt(@IntRange(from = 0) int index) {
Svet Ganov8455ba22019-01-02 13:05:56 -08004812 if (mHistoricalUidOps == null) {
4813 throw new IndexOutOfBoundsException();
4814 }
4815 return mHistoricalUidOps.valueAt(index);
4816 }
4817
4818 /**
4819 * Gets the historical UID ops for a given UID.
4820 *
4821 * @param uid The UID.
4822 *
4823 * @return The historical ops for the UID.
4824 */
4825 public @Nullable HistoricalUidOps getUidOps(int uid) {
4826 if (mHistoricalUidOps == null) {
4827 return null;
4828 }
4829 return mHistoricalUidOps.get(uid);
4830 }
4831
Winson4e3b4352019-05-07 16:29:59 -07004832 /** @hide */
4833 public void clearHistory(int uid, @NonNull String packageName) {
4834 HistoricalUidOps historicalUidOps = getOrCreateHistoricalUidOps(uid);
4835 historicalUidOps.clearHistory(packageName);
4836 if (historicalUidOps.isEmpty()) {
4837 mHistoricalUidOps.remove(uid);
4838 }
4839 }
4840
Svet Ganov8455ba22019-01-02 13:05:56 -08004841 @Override
4842 public int describeContents() {
4843 return 0;
4844 }
4845
4846 @Override
4847 public void writeToParcel(Parcel parcel, int flags) {
4848 parcel.writeLong(mBeginTimeMillis);
4849 parcel.writeLong(mEndTimeMillis);
4850 if (mHistoricalUidOps != null) {
4851 final int uidCount = mHistoricalUidOps.size();
4852 parcel.writeInt(uidCount);
4853 for (int i = 0; i < uidCount; i++) {
4854 parcel.writeInt(mHistoricalUidOps.keyAt(i));
4855 }
4856 final List<HistoricalUidOps> opsList = new ArrayList<>(uidCount);
4857 for (int i = 0; i < uidCount; i++) {
4858 opsList.add(mHistoricalUidOps.valueAt(i));
4859 }
4860 parcel.writeParcelable(new ParceledListSlice<>(opsList), flags);
4861 } else {
4862 parcel.writeInt(-1);
4863 }
4864 }
4865
4866 /**
4867 * Accepts a visitor to traverse the ops tree.
4868 *
4869 * @param visitor The visitor.
4870 *
4871 * @hide
4872 */
4873 public void accept(@NonNull HistoricalOpsVisitor visitor) {
4874 visitor.visitHistoricalOps(this);
4875 final int uidCount = getUidCount();
4876 for (int i = 0; i < uidCount; i++) {
4877 getUidOpsAt(i).accept(visitor);
4878 }
4879 }
4880
4881 private @NonNull HistoricalUidOps getOrCreateHistoricalUidOps(int uid) {
4882 if (mHistoricalUidOps == null) {
4883 mHistoricalUidOps = new SparseArray<>();
4884 }
4885 HistoricalUidOps historicalUidOp = mHistoricalUidOps.get(uid);
4886 if (historicalUidOp == null) {
4887 historicalUidOp = new HistoricalUidOps(uid);
4888 mHistoricalUidOps.put(uid, historicalUidOp);
4889 }
4890 return historicalUidOp;
4891 }
4892
4893 /**
4894 * @return Rounded value up at the 0.5 boundary.
4895 *
4896 * @hide
4897 */
4898 public static double round(double value) {
4899 final BigDecimal decimalScale = new BigDecimal(value);
4900 return decimalScale.setScale(0, RoundingMode.HALF_UP).doubleValue();
4901 }
4902
4903 @Override
Aurimas Liutikas4d1699d2019-08-28 13:01:05 -07004904 public boolean equals(@Nullable Object obj) {
Svet Ganov8455ba22019-01-02 13:05:56 -08004905 if (this == obj) {
4906 return true;
4907 }
4908 if (obj == null || getClass() != obj.getClass()) {
4909 return false;
4910 }
4911 final HistoricalOps other = (HistoricalOps) obj;
4912 if (mBeginTimeMillis != other.mBeginTimeMillis) {
4913 return false;
4914 }
4915 if (mEndTimeMillis != other.mEndTimeMillis) {
4916 return false;
4917 }
4918 if (mHistoricalUidOps == null) {
4919 if (other.mHistoricalUidOps != null) {
4920 return false;
4921 }
4922 } else if (!mHistoricalUidOps.equals(other.mHistoricalUidOps)) {
4923 return false;
4924 }
4925 return true;
4926 }
4927
4928 @Override
4929 public int hashCode() {
4930 int result = (int) (mBeginTimeMillis ^ (mBeginTimeMillis >>> 32));
4931 result = 31 * result + mHistoricalUidOps.hashCode();
4932 return result;
4933 }
4934
Aurimas Liutikas4d1699d2019-08-28 13:01:05 -07004935 @NonNull
Svet Ganov8455ba22019-01-02 13:05:56 -08004936 @Override
4937 public String toString() {
4938 return getClass().getSimpleName() + "[from:"
4939 + mBeginTimeMillis + " to:" + mEndTimeMillis + "]";
4940 }
4941
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07004942 public static final @android.annotation.NonNull Creator<HistoricalOps> CREATOR = new Creator<HistoricalOps>() {
Svet Ganov8455ba22019-01-02 13:05:56 -08004943 @Override
4944 public @NonNull HistoricalOps createFromParcel(@NonNull Parcel parcel) {
4945 return new HistoricalOps(parcel);
4946 }
4947
4948 @Override
4949 public @NonNull HistoricalOps[] newArray(int size) {
4950 return new HistoricalOps[size];
4951 }
4952 };
4953 }
4954
4955 /**
4956 * This class represents historical app op state for a UID.
4957 *
4958 * @hide
4959 */
4960 @TestApi
4961 @SystemApi
4962 public static final class HistoricalUidOps implements Parcelable {
4963 private final int mUid;
4964 private @Nullable ArrayMap<String, HistoricalPackageOps> mHistoricalPackageOps;
4965
4966 /** @hide */
4967 public HistoricalUidOps(int uid) {
4968 mUid = uid;
4969 }
4970
4971 private HistoricalUidOps(@NonNull HistoricalUidOps other) {
4972 mUid = other.mUid;
4973 final int opCount = other.getPackageCount();
4974 for (int i = 0; i < opCount; i++) {
4975 final HistoricalPackageOps origOps = other.getPackageOpsAt(i);
4976 final HistoricalPackageOps cloneOps = new HistoricalPackageOps(origOps);
4977 if (mHistoricalPackageOps == null) {
4978 mHistoricalPackageOps = new ArrayMap<>(opCount);
4979 }
4980 mHistoricalPackageOps.put(cloneOps.getPackageName(), cloneOps);
4981 }
4982 }
4983
4984 private HistoricalUidOps(@NonNull Parcel parcel) {
4985 // No arg check since we always read from a trusted source.
4986 mUid = parcel.readInt();
4987 mHistoricalPackageOps = parcel.createTypedArrayMap(HistoricalPackageOps.CREATOR);
4988 }
4989
4990 private @Nullable HistoricalUidOps splice(double fractionToRemove) {
4991 HistoricalUidOps splice = null;
4992 final int packageCount = getPackageCount();
4993 for (int i = 0; i < packageCount; i++) {
4994 final HistoricalPackageOps origOps = getPackageOpsAt(i);
4995 final HistoricalPackageOps spliceOps = origOps.splice(fractionToRemove);
4996 if (spliceOps != null) {
4997 if (splice == null) {
4998 splice = new HistoricalUidOps(mUid);
4999 }
5000 if (splice.mHistoricalPackageOps == null) {
5001 splice.mHistoricalPackageOps = new ArrayMap<>();
5002 }
5003 splice.mHistoricalPackageOps.put(spliceOps.getPackageName(), spliceOps);
5004 }
5005 }
5006 return splice;
5007 }
5008
5009 private void merge(@NonNull HistoricalUidOps other) {
5010 final int packageCount = other.getPackageCount();
5011 for (int i = 0; i < packageCount; i++) {
5012 final HistoricalPackageOps otherPackageOps = other.getPackageOpsAt(i);
5013 final HistoricalPackageOps thisPackageOps = getPackageOps(
5014 otherPackageOps.getPackageName());
5015 if (thisPackageOps != null) {
5016 thisPackageOps.merge(otherPackageOps);
5017 } else {
5018 if (mHistoricalPackageOps == null) {
5019 mHistoricalPackageOps = new ArrayMap<>();
5020 }
5021 mHistoricalPackageOps.put(otherPackageOps.getPackageName(), otherPackageOps);
5022 }
5023 }
5024 }
5025
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005026 private void filter(@Nullable String packageName, @Nullable String attributionTag,
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005027 @Nullable String[] opNames, @HistoricalOpsRequestFilter int filter,
Svet Ganov8455ba22019-01-02 13:05:56 -08005028 double fractionToRemove) {
5029 final int packageCount = getPackageCount();
5030 for (int i = packageCount - 1; i >= 0; i--) {
5031 final HistoricalPackageOps packageOps = getPackageOpsAt(i);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005032 if ((filter & FILTER_BY_PACKAGE_NAME) != 0 && !packageName.equals(
5033 packageOps.getPackageName())) {
Svet Ganov8455ba22019-01-02 13:05:56 -08005034 mHistoricalPackageOps.removeAt(i);
5035 } else {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005036 packageOps.filter(attributionTag, opNames, filter, fractionToRemove);
5037 if (packageOps.getAttributedOpsCount() == 0) {
Stanislav Zholnin0d9499c2020-01-16 13:39:50 +00005038 mHistoricalPackageOps.removeAt(i);
5039 }
Svet Ganov8455ba22019-01-02 13:05:56 -08005040 }
5041 }
5042 }
5043
5044 private boolean isEmpty() {
5045 final int packageCount = getPackageCount();
5046 for (int i = packageCount - 1; i >= 0; i--) {
5047 final HistoricalPackageOps packageOps = mHistoricalPackageOps.valueAt(i);
5048 if (!packageOps.isEmpty()) {
5049 return false;
5050 }
5051 }
5052 return true;
5053 }
5054
5055 private void increaseAccessCount(int opCode, @NonNull String packageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005056 @Nullable String attributionTag, @UidState int uidState, @OpFlags int flags,
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005057 long increment) {
Svet Ganov8455ba22019-01-02 13:05:56 -08005058 getOrCreateHistoricalPackageOps(packageName).increaseAccessCount(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005059 opCode, attributionTag, uidState, flags, increment);
Svet Ganov8455ba22019-01-02 13:05:56 -08005060 }
5061
5062 private void increaseRejectCount(int opCode, @NonNull String packageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005063 @Nullable String attributionTag, @UidState int uidState, @OpFlags int flags,
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005064 long increment) {
Svet Ganov8455ba22019-01-02 13:05:56 -08005065 getOrCreateHistoricalPackageOps(packageName).increaseRejectCount(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005066 opCode, attributionTag, uidState, flags, increment);
Svet Ganov8455ba22019-01-02 13:05:56 -08005067 }
5068
5069 private void increaseAccessDuration(int opCode, @NonNull String packageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005070 @Nullable String attributionTag, @UidState int uidState, @OpFlags int flags,
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005071 long increment) {
Svet Ganov8455ba22019-01-02 13:05:56 -08005072 getOrCreateHistoricalPackageOps(packageName).increaseAccessDuration(
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005073 opCode, attributionTag, uidState, flags, increment);
Svet Ganov8455ba22019-01-02 13:05:56 -08005074 }
5075
5076 /**
5077 * @return The UID for which the data is related.
5078 */
5079 public int getUid() {
5080 return mUid;
5081 }
5082
5083 /**
5084 * Gets number of packages with historical ops.
5085 *
5086 * @return The number of packages with historical ops.
5087 *
5088 * @see #getPackageOpsAt(int)
5089 */
Svet Ganov00a46ef2019-03-29 21:13:03 -07005090 public @IntRange(from = 0) int getPackageCount() {
Svet Ganov8455ba22019-01-02 13:05:56 -08005091 if (mHistoricalPackageOps == null) {
5092 return 0;
5093 }
5094 return mHistoricalPackageOps.size();
5095 }
5096
5097 /**
5098 * Gets the historical package ops at a given index.
5099 *
5100 * @param index The index.
5101 *
5102 * @return The historical package ops at the given index.
5103 *
5104 * @see #getPackageCount()
5105 */
Svet Ganov00a46ef2019-03-29 21:13:03 -07005106 public @NonNull HistoricalPackageOps getPackageOpsAt(@IntRange(from = 0) int index) {
Svet Ganov8455ba22019-01-02 13:05:56 -08005107 if (mHistoricalPackageOps == null) {
5108 throw new IndexOutOfBoundsException();
5109 }
5110 return mHistoricalPackageOps.valueAt(index);
5111 }
5112
5113 /**
5114 * Gets the historical package ops for a given package.
5115 *
5116 * @param packageName The package.
5117 *
5118 * @return The historical ops for the package.
5119 */
5120 public @Nullable HistoricalPackageOps getPackageOps(@NonNull String packageName) {
5121 if (mHistoricalPackageOps == null) {
5122 return null;
5123 }
5124 return mHistoricalPackageOps.get(packageName);
5125 }
5126
Winson4e3b4352019-05-07 16:29:59 -07005127 private void clearHistory(@NonNull String packageName) {
5128 if (mHistoricalPackageOps != null) {
5129 mHistoricalPackageOps.remove(packageName);
5130 }
5131 }
5132
Svet Ganov8455ba22019-01-02 13:05:56 -08005133 @Override
5134 public int describeContents() {
5135 return 0;
5136 }
5137
5138 @Override
5139 public void writeToParcel(Parcel parcel, int flags) {
5140 parcel.writeInt(mUid);
5141 parcel.writeTypedArrayMap(mHistoricalPackageOps, flags);
5142 }
5143
5144 private void accept(@NonNull HistoricalOpsVisitor visitor) {
5145 visitor.visitHistoricalUidOps(this);
5146 final int packageCount = getPackageCount();
5147 for (int i = 0; i < packageCount; i++) {
5148 getPackageOpsAt(i).accept(visitor);
5149 }
5150 }
5151
5152 private @NonNull HistoricalPackageOps getOrCreateHistoricalPackageOps(
5153 @NonNull String packageName) {
5154 if (mHistoricalPackageOps == null) {
5155 mHistoricalPackageOps = new ArrayMap<>();
5156 }
5157 HistoricalPackageOps historicalPackageOp = mHistoricalPackageOps.get(packageName);
5158 if (historicalPackageOp == null) {
5159 historicalPackageOp = new HistoricalPackageOps(packageName);
5160 mHistoricalPackageOps.put(packageName, historicalPackageOp);
5161 }
5162 return historicalPackageOp;
5163 }
5164
5165
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -07005166 public static final @android.annotation.NonNull Creator<HistoricalUidOps> CREATOR = new Creator<HistoricalUidOps>() {
Svet Ganov8455ba22019-01-02 13:05:56 -08005167 @Override
5168 public @NonNull HistoricalUidOps createFromParcel(@NonNull Parcel parcel) {
5169 return new HistoricalUidOps(parcel);
5170 }
5171
5172 @Override
5173 public @NonNull HistoricalUidOps[] newArray(int size) {
5174 return new HistoricalUidOps[size];
5175 }
5176 };
5177
5178 @Override
Aurimas Liutikas4d1699d2019-08-28 13:01:05 -07005179 public boolean equals(@Nullable Object obj) {
Svet Ganov8455ba22019-01-02 13:05:56 -08005180 if (this == obj) {
5181 return true;
5182 }
5183 if (obj == null || getClass() != obj.getClass()) {
5184 return false;
5185 }
5186 final HistoricalUidOps other = (HistoricalUidOps) obj;
5187 if (mUid != other.mUid) {
5188 return false;
5189 }
5190 if (mHistoricalPackageOps == null) {
5191 if (other.mHistoricalPackageOps != null) {
5192 return false;
5193 }
5194 } else if (!mHistoricalPackageOps.equals(other.mHistoricalPackageOps)) {
5195 return false;
5196 }
5197 return true;
5198 }
5199
5200 @Override
5201 public int hashCode() {
5202 int result = mUid;
5203 result = 31 * result + (mHistoricalPackageOps != null
5204 ? mHistoricalPackageOps.hashCode() : 0);
5205 return result;
5206 }
5207 }
5208
5209 /**
5210 * This class represents historical app op information about a package.
Svet Ganovad0a49b2018-10-29 10:07:08 -07005211 *
5212 * @hide
5213 */
5214 @TestApi
5215 @SystemApi
5216 public static final class HistoricalPackageOps implements Parcelable {
Svet Ganovad0a49b2018-10-29 10:07:08 -07005217 private final @NonNull String mPackageName;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005218 private @Nullable ArrayMap<String, AttributedHistoricalOps> mAttributedHistoricalOps;
Svet Ganovad0a49b2018-10-29 10:07:08 -07005219
Svet Ganov8455ba22019-01-02 13:05:56 -08005220 /** @hide */
5221 public HistoricalPackageOps(@NonNull String packageName) {
Svet Ganovad0a49b2018-10-29 10:07:08 -07005222 mPackageName = packageName;
Svet Ganovad0a49b2018-10-29 10:07:08 -07005223 }
5224
Svet Ganov8455ba22019-01-02 13:05:56 -08005225 private HistoricalPackageOps(@NonNull HistoricalPackageOps other) {
5226 mPackageName = other.mPackageName;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005227 final int opCount = other.getAttributedOpsCount();
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005228 for (int i = 0; i < opCount; i++) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005229 final AttributedHistoricalOps origOps = other.getAttributedOpsAt(i);
5230 final AttributedHistoricalOps cloneOps = new AttributedHistoricalOps(origOps);
5231 if (mAttributedHistoricalOps == null) {
5232 mAttributedHistoricalOps = new ArrayMap<>(opCount);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005233 }
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005234 mAttributedHistoricalOps.put(cloneOps.getTag(), cloneOps);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005235 }
5236 }
5237
5238 private HistoricalPackageOps(@NonNull Parcel parcel) {
5239 mPackageName = parcel.readString();
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005240 mAttributedHistoricalOps = parcel.createTypedArrayMap(AttributedHistoricalOps.CREATOR);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005241 }
5242
5243 private @Nullable HistoricalPackageOps splice(double fractionToRemove) {
5244 HistoricalPackageOps splice = null;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005245 final int attributionCount = getAttributedOpsCount();
5246 for (int i = 0; i < attributionCount; i++) {
5247 final AttributedHistoricalOps origOps = getAttributedOpsAt(i);
5248 final AttributedHistoricalOps spliceOps = origOps.splice(fractionToRemove);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005249 if (spliceOps != null) {
5250 if (splice == null) {
5251 splice = new HistoricalPackageOps(mPackageName);
5252 }
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005253 if (splice.mAttributedHistoricalOps == null) {
5254 splice.mAttributedHistoricalOps = new ArrayMap<>();
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005255 }
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005256 splice.mAttributedHistoricalOps.put(spliceOps.getTag(), spliceOps);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005257 }
5258 }
5259 return splice;
5260 }
5261
5262 private void merge(@NonNull HistoricalPackageOps other) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005263 final int attributionCount = other.getAttributedOpsCount();
5264 for (int i = 0; i < attributionCount; i++) {
5265 final AttributedHistoricalOps otherAttributionOps = other.getAttributedOpsAt(i);
5266 final AttributedHistoricalOps thisAttributionOps = getAttributedOps(
5267 otherAttributionOps.getTag());
5268 if (thisAttributionOps != null) {
5269 thisAttributionOps.merge(otherAttributionOps);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005270 } else {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005271 if (mAttributedHistoricalOps == null) {
5272 mAttributedHistoricalOps = new ArrayMap<>();
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005273 }
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005274 mAttributedHistoricalOps.put(otherAttributionOps.getTag(),
5275 otherAttributionOps);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005276 }
5277 }
5278 }
5279
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005280 private void filter(@Nullable String attributionTag, @Nullable String[] opNames,
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005281 @HistoricalOpsRequestFilter int filter, double fractionToRemove) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005282 final int attributionCount = getAttributedOpsCount();
5283 for (int i = attributionCount - 1; i >= 0; i--) {
5284 final AttributedHistoricalOps attributionOps = getAttributedOpsAt(i);
5285 if ((filter & FILTER_BY_ATTRIBUTION_TAG) != 0 && !Objects.equals(attributionTag,
5286 attributionOps.getTag())) {
5287 mAttributedHistoricalOps.removeAt(i);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005288 } else {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005289 attributionOps.filter(opNames, filter, fractionToRemove);
5290 if (attributionOps.getOpCount() == 0) {
5291 mAttributedHistoricalOps.removeAt(i);
Stanislav Zholnin0d9499c2020-01-16 13:39:50 +00005292 }
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005293 }
5294 }
5295 }
5296
5297 private void accept(@NonNull HistoricalOpsVisitor visitor) {
5298 visitor.visitHistoricalPackageOps(this);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005299 final int attributionCount = getAttributedOpsCount();
5300 for (int i = 0; i < attributionCount; i++) {
5301 getAttributedOpsAt(i).accept(visitor);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005302 }
5303 }
5304
5305 private boolean isEmpty() {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005306 final int attributionCount = getAttributedOpsCount();
5307 for (int i = attributionCount - 1; i >= 0; i--) {
5308 final AttributedHistoricalOps attributionOps = mAttributedHistoricalOps.valueAt(i);
5309 if (!attributionOps.isEmpty()) {
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005310 return false;
5311 }
5312 }
5313 return true;
5314 }
5315
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005316 private void increaseAccessCount(int opCode, @Nullable String attributionTag,
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005317 @UidState int uidState, @OpFlags int flags, long increment) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005318 getOrCreateAttributedHistoricalOps(attributionTag).increaseAccessCount(
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005319 opCode, uidState, flags, increment);
5320 }
5321
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005322 private void increaseRejectCount(int opCode, @Nullable String attributionTag,
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005323 @UidState int uidState, @OpFlags int flags, long increment) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005324 getOrCreateAttributedHistoricalOps(attributionTag).increaseRejectCount(
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005325 opCode, uidState, flags, increment);
5326 }
5327
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005328 private void increaseAccessDuration(int opCode, @Nullable String attributionTag,
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005329 @UidState int uidState, @OpFlags int flags, long increment) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005330 getOrCreateAttributedHistoricalOps(attributionTag).increaseAccessDuration(
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005331 opCode, uidState, flags, increment);
5332 }
5333
5334 /**
5335 * Gets the package name which the data represents.
5336 *
5337 * @return The package name which the data represents.
5338 */
5339 public @NonNull String getPackageName() {
5340 return mPackageName;
5341 }
5342
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005343 private @NonNull AttributedHistoricalOps getOrCreateAttributedHistoricalOps(
5344 @Nullable String attributionTag) {
5345 if (mAttributedHistoricalOps == null) {
5346 mAttributedHistoricalOps = new ArrayMap<>();
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005347 }
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005348 AttributedHistoricalOps historicalAttributionOp = mAttributedHistoricalOps.get(
5349 attributionTag);
5350 if (historicalAttributionOp == null) {
5351 historicalAttributionOp = new AttributedHistoricalOps(attributionTag);
5352 mAttributedHistoricalOps.put(attributionTag, historicalAttributionOp);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005353 }
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005354 return historicalAttributionOp;
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005355 }
5356
5357 /**
5358 * Gets number historical app ops.
5359 *
5360 * @return The number historical app ops.
5361 * @see #getOpAt(int)
5362 */
5363 public @IntRange(from = 0) int getOpCount() {
5364 int numOps = 0;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005365 int numAttributions = getAttributedOpsCount();
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005366
5367 for (int code = 0; code < _NUM_OP; code++) {
5368 String opName = opToPublicName(code);
5369
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005370 for (int attributionNum = 0; attributionNum < numAttributions; attributionNum++) {
5371 if (getAttributedOpsAt(attributionNum).getOp(opName) != null) {
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005372 numOps++;
5373 break;
5374 }
5375 }
5376 }
5377
5378 return numOps;
5379 }
5380
5381 /**
5382 * Gets the historical op at a given index.
5383 *
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005384 * <p>This combines the counts from all attributions.
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005385 *
5386 * @param index The index to lookup.
5387 * @return The op at the given index.
5388 * @see #getOpCount()
5389 */
5390 public @NonNull HistoricalOp getOpAt(@IntRange(from = 0) int index) {
5391 int numOpsFound = 0;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005392 int numAttributions = getAttributedOpsCount();
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005393
5394 for (int code = 0; code < _NUM_OP; code++) {
5395 String opName = opToPublicName(code);
5396
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005397 for (int attributionNum = 0; attributionNum < numAttributions; attributionNum++) {
5398 if (getAttributedOpsAt(attributionNum).getOp(opName) != null) {
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005399 if (numOpsFound == index) {
5400 return getOp(opName);
5401 } else {
5402 numOpsFound++;
5403 break;
5404 }
5405 }
5406 }
5407 }
5408
5409 throw new IndexOutOfBoundsException();
5410 }
5411
5412 /**
5413 * Gets the historical entry for a given op name.
5414 *
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005415 * <p>This combines the counts from all attributions.
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005416 *
5417 * @param opName The op name.
5418 * @return The historical entry for that op name.
5419 */
5420 public @Nullable HistoricalOp getOp(@NonNull String opName) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005421 if (mAttributedHistoricalOps == null) {
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005422 return null;
5423 }
5424
5425 HistoricalOp combinedOp = null;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005426 int numAttributions = getAttributedOpsCount();
5427 for (int i = 0; i < numAttributions; i++) {
5428 HistoricalOp attributionOp = getAttributedOpsAt(i).getOp(opName);
5429 if (attributionOp != null) {
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005430 if (combinedOp == null) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005431 combinedOp = new HistoricalOp(attributionOp);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005432 } else {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005433 combinedOp.merge(attributionOp);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005434 }
5435 }
5436 }
5437
5438 return combinedOp;
5439 }
5440
5441 @Override
5442 public int describeContents() {
5443 return 0;
5444 }
5445
5446 @Override
5447 public void writeToParcel(@NonNull Parcel parcel, int flags) {
5448 parcel.writeString(mPackageName);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005449 parcel.writeTypedArrayMap(mAttributedHistoricalOps, flags);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005450 }
5451
5452 public static final @android.annotation.NonNull Creator<HistoricalPackageOps> CREATOR =
5453 new Creator<HistoricalPackageOps>() {
5454 @Override
5455 public @NonNull HistoricalPackageOps createFromParcel(@NonNull Parcel parcel) {
5456 return new HistoricalPackageOps(parcel);
5457 }
5458
5459 @Override
5460 public @NonNull HistoricalPackageOps[] newArray(int size) {
5461 return new HistoricalPackageOps[size];
5462 }
5463 };
5464
5465 @Override
5466 public boolean equals(@Nullable Object obj) {
5467 if (this == obj) {
5468 return true;
5469 }
5470 if (obj == null || getClass() != obj.getClass()) {
5471 return false;
5472 }
5473 final HistoricalPackageOps other = (HistoricalPackageOps) obj;
5474 if (!mPackageName.equals(other.mPackageName)) {
5475 return false;
5476 }
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005477 if (mAttributedHistoricalOps == null) {
5478 if (other.mAttributedHistoricalOps != null) {
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005479 return false;
5480 }
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005481 } else if (!mAttributedHistoricalOps.equals(other.mAttributedHistoricalOps)) {
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005482 return false;
5483 }
5484 return true;
5485 }
5486
5487 @Override
5488 public int hashCode() {
5489 int result = mPackageName != null ? mPackageName.hashCode() : 0;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005490 result = 31 * result + (mAttributedHistoricalOps != null
5491 ? mAttributedHistoricalOps.hashCode() : 0);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005492 return result;
5493 }
5494
5495 /**
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005496 * Gets number of attributed historical ops.
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005497 *
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005498 * @return The number of attribution with historical ops.
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005499 *
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005500 * @see #getAttributedOpsAt(int)
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005501 */
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005502 public @IntRange(from = 0) int getAttributedOpsCount() {
5503 if (mAttributedHistoricalOps == null) {
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005504 return 0;
5505 }
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005506 return mAttributedHistoricalOps.size();
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005507 }
5508
5509 /**
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005510 * Gets the attributed historical ops at a given index.
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005511 *
5512 * @param index The index.
5513 *
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005514 * @return The historical attribution ops at the given index.
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005515 *
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005516 * @see #getAttributedOpsCount()
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005517 */
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005518 public @NonNull AttributedHistoricalOps getAttributedOpsAt(@IntRange(from = 0) int index) {
5519 if (mAttributedHistoricalOps == null) {
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005520 throw new IndexOutOfBoundsException();
5521 }
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005522 return mAttributedHistoricalOps.valueAt(index);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005523 }
5524
5525 /**
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005526 * Gets the attributed historical ops for a given attribution tag.
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005527 *
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005528 * @param attributionTag The attribution tag.
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005529 *
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005530 * @return The historical ops for the attribution.
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005531 */
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005532 public @Nullable AttributedHistoricalOps getAttributedOps(@NonNull String attributionTag) {
5533 if (mAttributedHistoricalOps == null) {
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005534 return null;
5535 }
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005536 return mAttributedHistoricalOps.get(attributionTag);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005537 }
5538 }
5539
5540 /**
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005541 * This class represents historical app op information about a attribution in a package.
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005542 *
5543 * @hide
5544 */
5545 @TestApi
5546 @SystemApi
5547 /* codegen verifier cannot deal with nested class parameters
5548 @DataClass(genHiddenConstructor = true,
5549 genEqualsHashCode = true, genHiddenCopyConstructor = true) */
5550 @DataClass.Suppress("getHistoricalOps")
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005551 public static final class AttributedHistoricalOps implements Parcelable {
5552 /** {@link Context#createAttributionContext attribution} tag */
5553 private final @Nullable String mTag;
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005554
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005555 /** Ops for this attribution */
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005556 private @Nullable ArrayMap<String, HistoricalOp> mHistoricalOps;
5557
5558 /** @hide */
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005559 public AttributedHistoricalOps(@NonNull String tag) {
5560 mTag = tag;
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005561 }
5562
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005563 private AttributedHistoricalOps(@NonNull AttributedHistoricalOps other) {
5564 mTag = other.mTag;
Svet Ganov8455ba22019-01-02 13:05:56 -08005565 final int opCount = other.getOpCount();
5566 for (int i = 0; i < opCount; i++) {
5567 final HistoricalOp origOp = other.getOpAt(i);
5568 final HistoricalOp cloneOp = new HistoricalOp(origOp);
5569 if (mHistoricalOps == null) {
5570 mHistoricalOps = new ArrayMap<>(opCount);
5571 }
5572 mHistoricalOps.put(cloneOp.getOpName(), cloneOp);
5573 }
5574 }
5575
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005576 private @Nullable AttributedHistoricalOps splice(double fractionToRemove) {
5577 AttributedHistoricalOps splice = null;
Svet Ganov8455ba22019-01-02 13:05:56 -08005578 final int opCount = getOpCount();
5579 for (int i = 0; i < opCount; i++) {
5580 final HistoricalOp origOps = getOpAt(i);
5581 final HistoricalOp spliceOps = origOps.splice(fractionToRemove);
5582 if (spliceOps != null) {
5583 if (splice == null) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005584 splice = new AttributedHistoricalOps(mTag, null);
Svet Ganov8455ba22019-01-02 13:05:56 -08005585 }
5586 if (splice.mHistoricalOps == null) {
5587 splice.mHistoricalOps = new ArrayMap<>();
5588 }
5589 splice.mHistoricalOps.put(spliceOps.getOpName(), spliceOps);
5590 }
5591 }
5592 return splice;
5593 }
5594
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005595 private void merge(@NonNull AttributedHistoricalOps other) {
Svet Ganov8455ba22019-01-02 13:05:56 -08005596 final int opCount = other.getOpCount();
5597 for (int i = 0; i < opCount; i++) {
5598 final HistoricalOp otherOp = other.getOpAt(i);
5599 final HistoricalOp thisOp = getOp(otherOp.getOpName());
5600 if (thisOp != null) {
5601 thisOp.merge(otherOp);
5602 } else {
5603 if (mHistoricalOps == null) {
5604 mHistoricalOps = new ArrayMap<>();
5605 }
5606 mHistoricalOps.put(otherOp.getOpName(), otherOp);
5607 }
5608 }
5609 }
5610
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005611 private void filter(@Nullable String[] opNames, @HistoricalOpsRequestFilter int filter,
5612 double scaleFactor) {
Svet Ganov8455ba22019-01-02 13:05:56 -08005613 final int opCount = getOpCount();
5614 for (int i = opCount - 1; i >= 0; i--) {
5615 final HistoricalOp op = mHistoricalOps.valueAt(i);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005616 if ((filter & FILTER_BY_OP_NAMES) != 0 && !ArrayUtils.contains(opNames,
5617 op.getOpName())) {
Svet Ganov8455ba22019-01-02 13:05:56 -08005618 mHistoricalOps.removeAt(i);
5619 } else {
5620 op.filter(scaleFactor);
5621 }
5622 }
5623 }
5624
5625 private boolean isEmpty() {
5626 final int opCount = getOpCount();
5627 for (int i = opCount - 1; i >= 0; i--) {
5628 final HistoricalOp op = mHistoricalOps.valueAt(i);
5629 if (!op.isEmpty()) {
5630 return false;
5631 }
5632 }
5633 return true;
5634 }
5635
Svet Ganovaf189e32019-02-15 18:45:29 -08005636 private void increaseAccessCount(int opCode, @UidState int uidState,
5637 @OpFlags int flags, long increment) {
5638 getOrCreateHistoricalOp(opCode).increaseAccessCount(uidState, flags, increment);
Svet Ganov8455ba22019-01-02 13:05:56 -08005639 }
5640
Svet Ganovaf189e32019-02-15 18:45:29 -08005641 private void increaseRejectCount(int opCode, @UidState int uidState,
5642 @OpFlags int flags, long increment) {
5643 getOrCreateHistoricalOp(opCode).increaseRejectCount(uidState, flags, increment);
Svet Ganov8455ba22019-01-02 13:05:56 -08005644 }
5645
Svet Ganovaf189e32019-02-15 18:45:29 -08005646 private void increaseAccessDuration(int opCode, @UidState int uidState,
5647 @OpFlags int flags, long increment) {
5648 getOrCreateHistoricalOp(opCode).increaseAccessDuration(uidState, flags, increment);
Svet Ganovad0a49b2018-10-29 10:07:08 -07005649 }
5650
5651 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08005652 * Gets number historical app ops.
Svet Ganovad0a49b2018-10-29 10:07:08 -07005653 *
Svet Ganov8455ba22019-01-02 13:05:56 -08005654 * @return The number historical app ops.
Svet Ganov8455ba22019-01-02 13:05:56 -08005655 * @see #getOpAt(int)
Svet Ganovad0a49b2018-10-29 10:07:08 -07005656 */
Svet Ganov00a46ef2019-03-29 21:13:03 -07005657 public @IntRange(from = 0) int getOpCount() {
Svet Ganov8455ba22019-01-02 13:05:56 -08005658 if (mHistoricalOps == null) {
5659 return 0;
5660 }
5661 return mHistoricalOps.size();
Svet Ganovad0a49b2018-10-29 10:07:08 -07005662 }
5663
5664 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08005665 * Gets the historical op at a given index.
Svet Ganovad0a49b2018-10-29 10:07:08 -07005666 *
5667 * @param index The index to lookup.
Svet Ganov8455ba22019-01-02 13:05:56 -08005668 * @return The op at the given index.
Svet Ganov8455ba22019-01-02 13:05:56 -08005669 * @see #getOpCount()
Svet Ganovad0a49b2018-10-29 10:07:08 -07005670 */
Svet Ganov00a46ef2019-03-29 21:13:03 -07005671 public @NonNull HistoricalOp getOpAt(@IntRange(from = 0) int index) {
Svet Ganov8455ba22019-01-02 13:05:56 -08005672 if (mHistoricalOps == null) {
5673 throw new IndexOutOfBoundsException();
5674 }
5675 return mHistoricalOps.valueAt(index);
Svet Ganovad0a49b2018-10-29 10:07:08 -07005676 }
5677
5678 /**
5679 * Gets the historical entry for a given op name.
5680 *
5681 * @param opName The op name.
Svet Ganovad0a49b2018-10-29 10:07:08 -07005682 * @return The historical entry for that op name.
5683 */
Svet Ganov8455ba22019-01-02 13:05:56 -08005684 public @Nullable HistoricalOp getOp(@NonNull String opName) {
5685 if (mHistoricalOps == null) {
5686 return null;
Svet Ganovad0a49b2018-10-29 10:07:08 -07005687 }
Svet Ganov8455ba22019-01-02 13:05:56 -08005688 return mHistoricalOps.get(opName);
Svet Ganovad0a49b2018-10-29 10:07:08 -07005689 }
5690
Svet Ganov8455ba22019-01-02 13:05:56 -08005691 private void accept(@NonNull HistoricalOpsVisitor visitor) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005692 visitor.visitHistoricalAttributionOps(this);
Svet Ganov8455ba22019-01-02 13:05:56 -08005693 final int opCount = getOpCount();
5694 for (int i = 0; i < opCount; i++) {
5695 getOpAt(i).accept(visitor);
5696 }
5697 }
5698
5699 private @NonNull HistoricalOp getOrCreateHistoricalOp(int opCode) {
5700 if (mHistoricalOps == null) {
5701 mHistoricalOps = new ArrayMap<>();
5702 }
5703 final String opStr = sOpToString[opCode];
5704 HistoricalOp op = mHistoricalOps.get(opStr);
5705 if (op == null) {
5706 op = new HistoricalOp(opCode);
5707 mHistoricalOps.put(opStr, op);
5708 }
5709 return op;
Svet Ganovad0a49b2018-10-29 10:07:08 -07005710 }
5711
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005712
5713
5714 // Code below generated by codegen v1.0.14.
5715 //
5716 // DO NOT MODIFY!
5717 // CHECKSTYLE:OFF Generated code
5718 //
5719 // To regenerate run:
5720 // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/app/AppOpsManager.java
5721 //
5722 // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
5723 // Settings > Editor > Code Style > Formatter Control
5724 //@formatter:off
5725
5726
5727 /**
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005728 * Creates a new HistoricalAttributionOps.
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005729 *
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005730 * @param tag
5731 * {@link Context#createAttributionContext attribution} tag
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005732 * @param historicalOps
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005733 * Ops for this attribution
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005734 * @hide
5735 */
5736 @DataClass.Generated.Member
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005737 public AttributedHistoricalOps(
5738 @Nullable String tag,
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005739 @Nullable ArrayMap<String,HistoricalOp> historicalOps) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005740 this.mTag = tag;
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005741 this.mHistoricalOps = historicalOps;
5742
5743 // onConstructed(); // You can define this method to get a callback
5744 }
5745
5746 /**
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005747 * {@link Context#createAttributionContext attribution} tag
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005748 */
5749 @DataClass.Generated.Member
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005750 public @Nullable String getTag() {
5751 return mTag;
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005752 }
5753
5754 @Override
5755 @DataClass.Generated.Member
5756 public boolean equals(@Nullable Object o) {
5757 // You can override field equality logic by defining either of the methods like:
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005758 // boolean fieldNameEquals(HistoricalAttributionOps other) { ... }
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005759 // boolean fieldNameEquals(FieldType otherValue) { ... }
5760
5761 if (this == o) return true;
5762 if (o == null || getClass() != o.getClass()) return false;
5763 @SuppressWarnings("unchecked")
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005764 AttributedHistoricalOps that = (AttributedHistoricalOps) o;
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005765 //noinspection PointlessBooleanExpression
5766 return true
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005767 && Objects.equals(mTag, that.mTag)
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005768 && Objects.equals(mHistoricalOps, that.mHistoricalOps);
5769 }
5770
5771 @Override
5772 @DataClass.Generated.Member
5773 public int hashCode() {
5774 // You can override field hashCode logic by defining methods like:
5775 // int fieldNameHashCode() { ... }
5776
5777 int _hash = 1;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005778 _hash = 31 * _hash + Objects.hashCode(mTag);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005779 _hash = 31 * _hash + Objects.hashCode(mHistoricalOps);
5780 return _hash;
5781 }
5782
5783 @Override
5784 @DataClass.Generated.Member
5785 public void writeToParcel(@NonNull Parcel dest, int flags) {
5786 // You can override field parcelling by defining methods like:
5787 // void parcelFieldName(Parcel dest, int flags) { ... }
5788
5789 byte flg = 0;
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005790 if (mTag != null) flg |= 0x1;
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005791 if (mHistoricalOps != null) flg |= 0x2;
5792 dest.writeByte(flg);
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005793 if (mTag != null) dest.writeString(mTag);
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005794 if (mHistoricalOps != null) dest.writeMap(mHistoricalOps);
5795 }
5796
5797 @Override
5798 @DataClass.Generated.Member
5799 public int describeContents() { return 0; }
5800
5801 /** @hide */
5802 @SuppressWarnings({"unchecked", "RedundantCast"})
5803 @DataClass.Generated.Member
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005804 /* package-private */ AttributedHistoricalOps(@NonNull Parcel in) {
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005805 // You can override field unparcelling by defining methods like:
5806 // static FieldType unparcelFieldName(Parcel in) { ... }
5807
5808 byte flg = in.readByte();
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005809 String attributionTag = (flg & 0x1) == 0 ? null : in.readString();
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005810 ArrayMap<String,HistoricalOp> historicalOps = null;
5811 if ((flg & 0x2) != 0) {
5812 historicalOps = new ArrayMap();
5813 in.readMap(historicalOps, HistoricalOp.class.getClassLoader());
5814 }
5815
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005816 this.mTag = attributionTag;
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005817 this.mHistoricalOps = historicalOps;
5818
5819 // onConstructed(); // You can define this method to get a callback
5820 }
5821
5822 @DataClass.Generated.Member
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005823 public static final @NonNull Parcelable.Creator<AttributedHistoricalOps> CREATOR
5824 = new Parcelable.Creator<AttributedHistoricalOps>() {
Svet Ganovad0a49b2018-10-29 10:07:08 -07005825 @Override
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005826 public AttributedHistoricalOps[] newArray(int size) {
5827 return new AttributedHistoricalOps[size];
Svet Ganovad0a49b2018-10-29 10:07:08 -07005828 }
5829
5830 @Override
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005831 public AttributedHistoricalOps createFromParcel(@NonNull Parcel in) {
5832 return new AttributedHistoricalOps(in);
Svet Ganovad0a49b2018-10-29 10:07:08 -07005833 }
5834 };
Svet Ganov8455ba22019-01-02 13:05:56 -08005835
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005836 /*
5837 @DataClass.Generated(
5838 time = 1578113234821L,
5839 codegenVersion = "1.0.14",
5840 sourceFile = "frameworks/base/core/java/android/app/AppOpsManager.java",
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08005841 inputSignatures = "private final @android.annotation.Nullable java.lang.String mAttributionTag\nprivate @android.annotation.Nullable android.util.ArrayMap<java.lang.String,android.app.HistoricalOp> mHistoricalOps\nprivate @android.annotation.Nullable android.app.HistoricalAttributionOps splice(double)\nprivate void merge(android.app.HistoricalAttributionOps)\nprivate void filter(java.lang.String[],int,double)\nprivate boolean isEmpty()\nprivate void increaseAccessCount(int,int,int,long)\nprivate void increaseRejectCount(int,int,int,long)\nprivate void increaseAccessDuration(int,int,int,long)\npublic @android.annotation.IntRange(from=0L) int getOpCount()\npublic @android.annotation.NonNull android.app.HistoricalOp getOpAt(int)\npublic @android.annotation.Nullable android.app.HistoricalOp getOp(java.lang.String)\nprivate void accept(android.app.HistoricalOpsVisitor)\nprivate @android.annotation.NonNull android.app.HistoricalOp getOrCreateHistoricalOp(int)\nclass HistoricalAttributionOps extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genHiddenConstructor=true, genEqualsHashCode=true, genHiddenCopyConstructor=true)")
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005842 @Deprecated
5843 private void __metadata() {}
5844 */
Svet Ganov8455ba22019-01-02 13:05:56 -08005845
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08005846 //@formatter:on
5847 // End of generated code
5848
Svet Ganovad0a49b2018-10-29 10:07:08 -07005849 }
5850
5851 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08005852 * This class represents historical information about an app op.
Svet Ganovad0a49b2018-10-29 10:07:08 -07005853 *
5854 * @hide
5855 */
5856 @TestApi
5857 @SystemApi
Svet Ganov8455ba22019-01-02 13:05:56 -08005858 public static final class HistoricalOp implements Parcelable {
Svet Ganovad0a49b2018-10-29 10:07:08 -07005859 private final int mOp;
Svet Ganovaf189e32019-02-15 18:45:29 -08005860 private @Nullable LongSparseLongArray mAccessCount;
5861 private @Nullable LongSparseLongArray mRejectCount;
5862 private @Nullable LongSparseLongArray mAccessDuration;
Svet Ganovad0a49b2018-10-29 10:07:08 -07005863
Svet Ganov8455ba22019-01-02 13:05:56 -08005864 /** @hide */
5865 public HistoricalOp(int op) {
Svet Ganovad0a49b2018-10-29 10:07:08 -07005866 mOp = op;
Svet Ganovad0a49b2018-10-29 10:07:08 -07005867 }
5868
Svet Ganov8455ba22019-01-02 13:05:56 -08005869 private HistoricalOp(@NonNull HistoricalOp other) {
5870 mOp = other.mOp;
5871 if (other.mAccessCount != null) {
Svet Ganovaf189e32019-02-15 18:45:29 -08005872 mAccessCount = other.mAccessCount.clone();
Svet Ganov8455ba22019-01-02 13:05:56 -08005873 }
5874 if (other.mRejectCount != null) {
Svet Ganovaf189e32019-02-15 18:45:29 -08005875 mRejectCount = other.mRejectCount.clone();
Svet Ganov8455ba22019-01-02 13:05:56 -08005876 }
5877 if (other.mAccessDuration != null) {
Svet Ganovaf189e32019-02-15 18:45:29 -08005878 mAccessDuration = other.mAccessDuration.clone();
Svet Ganov8455ba22019-01-02 13:05:56 -08005879 }
5880 }
5881
5882 private HistoricalOp(@NonNull Parcel parcel) {
Svet Ganovad0a49b2018-10-29 10:07:08 -07005883 mOp = parcel.readInt();
Svet Ganovaf189e32019-02-15 18:45:29 -08005884 mAccessCount = readLongSparseLongArrayFromParcel(parcel);
5885 mRejectCount = readLongSparseLongArrayFromParcel(parcel);
5886 mAccessDuration = readLongSparseLongArrayFromParcel(parcel);
Svet Ganovad0a49b2018-10-29 10:07:08 -07005887 }
5888
Svet Ganov8455ba22019-01-02 13:05:56 -08005889 private void filter(double scaleFactor) {
5890 scale(mAccessCount, scaleFactor);
5891 scale(mRejectCount, scaleFactor);
5892 scale(mAccessDuration, scaleFactor);
5893 }
5894
5895 private boolean isEmpty() {
5896 return !hasData(mAccessCount)
5897 && !hasData(mRejectCount)
5898 && !hasData(mAccessDuration);
5899 }
5900
Svet Ganovaf189e32019-02-15 18:45:29 -08005901 private boolean hasData(@NonNull LongSparseLongArray array) {
5902 return (array != null && array.size() > 0);
Svet Ganov8455ba22019-01-02 13:05:56 -08005903 }
5904
5905 private @Nullable HistoricalOp splice(double fractionToRemove) {
Svet Ganovaf189e32019-02-15 18:45:29 -08005906 final HistoricalOp splice = new HistoricalOp(mOp);
5907 splice(mAccessCount, splice::getOrCreateAccessCount, fractionToRemove);
5908 splice(mRejectCount, splice::getOrCreateRejectCount, fractionToRemove);
5909 splice(mAccessDuration, splice::getOrCreateAccessDuration, fractionToRemove);
Svet Ganov8455ba22019-01-02 13:05:56 -08005910 return splice;
5911 }
5912
Svet Ganovaf189e32019-02-15 18:45:29 -08005913 private static void splice(@Nullable LongSparseLongArray sourceContainer,
5914 @NonNull Supplier<LongSparseLongArray> destContainerProvider,
5915 double fractionToRemove) {
5916 if (sourceContainer != null) {
5917 final int size = sourceContainer.size();
5918 for (int i = 0; i < size; i++) {
5919 final long key = sourceContainer.keyAt(i);
5920 final long value = sourceContainer.valueAt(i);
5921 final long removedFraction = Math.round(value * fractionToRemove);
5922 if (removedFraction > 0) {
5923 destContainerProvider.get().put(key, removedFraction);
5924 sourceContainer.put(key, value - removedFraction);
5925 }
5926 }
5927 }
5928 }
5929
Svet Ganov8455ba22019-01-02 13:05:56 -08005930 private void merge(@NonNull HistoricalOp other) {
Svet Ganovaf189e32019-02-15 18:45:29 -08005931 merge(this::getOrCreateAccessCount, other.mAccessCount);
5932 merge(this::getOrCreateRejectCount, other.mRejectCount);
5933 merge(this::getOrCreateAccessDuration, other.mAccessDuration);
Svet Ganov8455ba22019-01-02 13:05:56 -08005934 }
5935
Svet Ganovaf189e32019-02-15 18:45:29 -08005936 private void increaseAccessCount(@UidState int uidState, @OpFlags int flags,
5937 long increment) {
5938 increaseCount(getOrCreateAccessCount(), uidState, flags, increment);
Svet Ganov8455ba22019-01-02 13:05:56 -08005939 }
5940
Svet Ganovaf189e32019-02-15 18:45:29 -08005941 private void increaseRejectCount(@UidState int uidState, @OpFlags int flags,
5942 long increment) {
5943 increaseCount(getOrCreateRejectCount(), uidState, flags, increment);
Svet Ganov8455ba22019-01-02 13:05:56 -08005944 }
5945
Svet Ganovaf189e32019-02-15 18:45:29 -08005946 private void increaseAccessDuration(@UidState int uidState, @OpFlags int flags,
5947 long increment) {
5948 increaseCount(getOrCreateAccessDuration(), uidState, flags, increment);
5949 }
5950
5951 private void increaseCount(@NonNull LongSparseLongArray counts,
5952 @UidState int uidState, @OpFlags int flags, long increment) {
5953 while (flags != 0) {
5954 final int flag = 1 << Integer.numberOfTrailingZeros(flags);
5955 flags &= ~flag;
5956 final long key = makeKey(uidState, flag);
5957 counts.put(key, counts.get(key) + increment);
5958 }
Svet Ganovad0a49b2018-10-29 10:07:08 -07005959 }
5960
5961 /**
5962 * Gets the op name.
5963 *
5964 * @return The op name.
5965 */
Svet Ganov8455ba22019-01-02 13:05:56 -08005966 public @NonNull String getOpName() {
Svet Ganovad0a49b2018-10-29 10:07:08 -07005967 return sOpToString[mOp];
5968 }
5969
Svet Ganov8455ba22019-01-02 13:05:56 -08005970 /** @hide */
5971 public int getOpCode() {
5972 return mOp;
5973 }
5974
Svet Ganovad0a49b2018-10-29 10:07:08 -07005975 /**
5976 * Gets the number times the op was accessed (performed) in the foreground.
5977 *
Svet Ganovaf189e32019-02-15 18:45:29 -08005978 * @param flags The flags which are any combination of
5979 * {@link #OP_FLAG_SELF}, {@link #OP_FLAG_TRUSTED_PROXY},
5980 * {@link #OP_FLAG_UNTRUSTED_PROXY}, {@link #OP_FLAG_TRUSTED_PROXIED},
5981 * {@link #OP_FLAG_UNTRUSTED_PROXIED}. You can use {@link #OP_FLAGS_ALL}
5982 * for any flag.
Svet Ganovad0a49b2018-10-29 10:07:08 -07005983 * @return The times the op was accessed in the foreground.
5984 *
Svet Ganovaf189e32019-02-15 18:45:29 -08005985 * @see #getBackgroundAccessCount(int)
5986 * @see #getAccessCount(int, int, int)
Svet Ganovad0a49b2018-10-29 10:07:08 -07005987 */
Svet Ganovaf189e32019-02-15 18:45:29 -08005988 public long getForegroundAccessCount(@OpFlags int flags) {
5989 return sumForFlagsInStates(mAccessCount, MAX_PRIORITY_UID_STATE,
5990 resolveFirstUnrestrictedUidState(mOp), flags);
Svet Ganovad0a49b2018-10-29 10:07:08 -07005991 }
5992
5993 /**
5994 * Gets the number times the op was accessed (performed) in the background.
5995 *
Svet Ganovaf189e32019-02-15 18:45:29 -08005996 * @param flags The flags which are any combination of
5997 * {@link #OP_FLAG_SELF}, {@link #OP_FLAG_TRUSTED_PROXY},
5998 * {@link #OP_FLAG_UNTRUSTED_PROXY}, {@link #OP_FLAG_TRUSTED_PROXIED},
5999 * {@link #OP_FLAG_UNTRUSTED_PROXIED}. You can use {@link #OP_FLAGS_ALL}
6000 * for any flag.
Svet Ganovad0a49b2018-10-29 10:07:08 -07006001 * @return The times the op was accessed in the background.
6002 *
Svet Ganovaf189e32019-02-15 18:45:29 -08006003 * @see #getForegroundAccessCount(int)
6004 * @see #getAccessCount(int, int, int)
Svet Ganovad0a49b2018-10-29 10:07:08 -07006005 */
Svet Ganovaf189e32019-02-15 18:45:29 -08006006 public long getBackgroundAccessCount(@OpFlags int flags) {
6007 return sumForFlagsInStates(mAccessCount, resolveLastRestrictedUidState(mOp),
6008 MIN_PRIORITY_UID_STATE, flags);
Svet Ganovad0a49b2018-10-29 10:07:08 -07006009 }
6010
6011 /**
Svet Ganovaf189e32019-02-15 18:45:29 -08006012 * Gets the number times the op was accessed (performed) for a
6013 * range of uid states.
Svet Ganovad0a49b2018-10-29 10:07:08 -07006014 *
Svet Ganovaf189e32019-02-15 18:45:29 -08006015 * @param fromUidState The UID state from which to query. Could be one of
Svet Ganovad0a49b2018-10-29 10:07:08 -07006016 * {@link #UID_STATE_PERSISTENT}, {@link #UID_STATE_TOP},
6017 * {@link #UID_STATE_FOREGROUND_SERVICE}, {@link #UID_STATE_FOREGROUND},
6018 * {@link #UID_STATE_BACKGROUND}, {@link #UID_STATE_CACHED}.
Svet Ganovaf189e32019-02-15 18:45:29 -08006019 * @param toUidState The UID state to which to query.
6020 * @param flags The flags which are any combination of
6021 * {@link #OP_FLAG_SELF}, {@link #OP_FLAG_TRUSTED_PROXY},
6022 * {@link #OP_FLAG_UNTRUSTED_PROXY}, {@link #OP_FLAG_TRUSTED_PROXIED},
6023 * {@link #OP_FLAG_UNTRUSTED_PROXIED}. You can use {@link #OP_FLAGS_ALL}
6024 * for any flag.
Svet Ganovad0a49b2018-10-29 10:07:08 -07006025 *
6026 * @return The times the op was accessed for the given UID state.
6027 *
Svet Ganovaf189e32019-02-15 18:45:29 -08006028 * @see #getForegroundAccessCount(int)
6029 * @see #getBackgroundAccessCount(int)
Svet Ganovad0a49b2018-10-29 10:07:08 -07006030 */
Svet Ganovaf189e32019-02-15 18:45:29 -08006031 public long getAccessCount(@UidState int fromUidState, @UidState int toUidState,
6032 @OpFlags int flags) {
6033 return sumForFlagsInStates(mAccessCount, fromUidState, toUidState, flags);
Svet Ganovad0a49b2018-10-29 10:07:08 -07006034 }
6035
6036 /**
6037 * Gets the number times the op was rejected in the foreground.
6038 *
Svet Ganovaf189e32019-02-15 18:45:29 -08006039 * @param flags The flags which are any combination of
6040 * {@link #OP_FLAG_SELF}, {@link #OP_FLAG_TRUSTED_PROXY},
6041 * {@link #OP_FLAG_UNTRUSTED_PROXY}, {@link #OP_FLAG_TRUSTED_PROXIED},
6042 * {@link #OP_FLAG_UNTRUSTED_PROXIED}. You can use {@link #OP_FLAGS_ALL}
6043 * for any flag.
Svet Ganovad0a49b2018-10-29 10:07:08 -07006044 * @return The times the op was rejected in the foreground.
6045 *
Svet Ganovaf189e32019-02-15 18:45:29 -08006046 * @see #getBackgroundRejectCount(int)
6047 * @see #getRejectCount(int, int, int)
Svet Ganovad0a49b2018-10-29 10:07:08 -07006048 */
Svet Ganovaf189e32019-02-15 18:45:29 -08006049 public long getForegroundRejectCount(@OpFlags int flags) {
6050 return sumForFlagsInStates(mRejectCount, MAX_PRIORITY_UID_STATE,
6051 resolveFirstUnrestrictedUidState(mOp), flags);
Svet Ganovad0a49b2018-10-29 10:07:08 -07006052 }
6053
6054 /**
6055 * Gets the number times the op was rejected in the background.
6056 *
Svet Ganovaf189e32019-02-15 18:45:29 -08006057 * @param flags The flags which are any combination of
6058 * {@link #OP_FLAG_SELF}, {@link #OP_FLAG_TRUSTED_PROXY},
6059 * {@link #OP_FLAG_UNTRUSTED_PROXY}, {@link #OP_FLAG_TRUSTED_PROXIED},
6060 * {@link #OP_FLAG_UNTRUSTED_PROXIED}. You can use {@link #OP_FLAGS_ALL}
6061 * for any flag.
Svet Ganovad0a49b2018-10-29 10:07:08 -07006062 * @return The times the op was rejected in the background.
6063 *
Svet Ganovaf189e32019-02-15 18:45:29 -08006064 * @see #getForegroundRejectCount(int)
6065 * @see #getRejectCount(int, int, int)
Svet Ganovad0a49b2018-10-29 10:07:08 -07006066 */
Svet Ganovaf189e32019-02-15 18:45:29 -08006067 public long getBackgroundRejectCount(@OpFlags int flags) {
6068 return sumForFlagsInStates(mRejectCount, resolveLastRestrictedUidState(mOp),
6069 MIN_PRIORITY_UID_STATE, flags);
Svet Ganovad0a49b2018-10-29 10:07:08 -07006070 }
6071
6072 /**
Svet Ganovaf189e32019-02-15 18:45:29 -08006073 * Gets the number times the op was rejected for a given range of UID states.
Svet Ganovad0a49b2018-10-29 10:07:08 -07006074 *
Svet Ganovaf189e32019-02-15 18:45:29 -08006075 * @param fromUidState The UID state from which to query. Could be one of
Svet Ganovad0a49b2018-10-29 10:07:08 -07006076 * {@link #UID_STATE_PERSISTENT}, {@link #UID_STATE_TOP},
6077 * {@link #UID_STATE_FOREGROUND_SERVICE}, {@link #UID_STATE_FOREGROUND},
6078 * {@link #UID_STATE_BACKGROUND}, {@link #UID_STATE_CACHED}.
Svet Ganovaf189e32019-02-15 18:45:29 -08006079 * @param toUidState The UID state to which to query.
6080 * @param flags The flags which are any combination of
6081 * {@link #OP_FLAG_SELF}, {@link #OP_FLAG_TRUSTED_PROXY},
6082 * {@link #OP_FLAG_UNTRUSTED_PROXY}, {@link #OP_FLAG_TRUSTED_PROXIED},
6083 * {@link #OP_FLAG_UNTRUSTED_PROXIED}. You can use {@link #OP_FLAGS_ALL}
6084 * for any flag.
Svet Ganovad0a49b2018-10-29 10:07:08 -07006085 *
6086 * @return The times the op was rejected for the given UID state.
6087 *
Svet Ganovaf189e32019-02-15 18:45:29 -08006088 * @see #getForegroundRejectCount(int)
6089 * @see #getBackgroundRejectCount(int)
Svet Ganovad0a49b2018-10-29 10:07:08 -07006090 */
Svet Ganovaf189e32019-02-15 18:45:29 -08006091 public long getRejectCount(@UidState int fromUidState, @UidState int toUidState,
6092 @OpFlags int flags) {
6093 return sumForFlagsInStates(mRejectCount, fromUidState, toUidState, flags);
Svet Ganovad0a49b2018-10-29 10:07:08 -07006094 }
6095
6096 /**
6097 * Gets the total duration the app op was accessed (performed) in the foreground.
Svet Ganov6f672a32019-07-08 16:40:42 -07006098 * The duration is in wall time.
Svet Ganovad0a49b2018-10-29 10:07:08 -07006099 *
Svet Ganovaf189e32019-02-15 18:45:29 -08006100 * @param flags The flags which are any combination of
6101 * {@link #OP_FLAG_SELF}, {@link #OP_FLAG_TRUSTED_PROXY},
6102 * {@link #OP_FLAG_UNTRUSTED_PROXY}, {@link #OP_FLAG_TRUSTED_PROXIED},
6103 * {@link #OP_FLAG_UNTRUSTED_PROXIED}. You can use {@link #OP_FLAGS_ALL}
6104 * for any flag.
Svet Ganovad0a49b2018-10-29 10:07:08 -07006105 * @return The total duration the app op was accessed in the foreground.
6106 *
Svet Ganovaf189e32019-02-15 18:45:29 -08006107 * @see #getBackgroundAccessDuration(int)
6108 * @see #getAccessDuration(int, int, int)
Svet Ganovad0a49b2018-10-29 10:07:08 -07006109 */
Svet Ganovaf189e32019-02-15 18:45:29 -08006110 public long getForegroundAccessDuration(@OpFlags int flags) {
6111 return sumForFlagsInStates(mAccessDuration, MAX_PRIORITY_UID_STATE,
6112 resolveFirstUnrestrictedUidState(mOp), flags);
Svet Ganovad0a49b2018-10-29 10:07:08 -07006113 }
6114
6115 /**
6116 * Gets the total duration the app op was accessed (performed) in the background.
Svet Ganov6f672a32019-07-08 16:40:42 -07006117 * The duration is in wall time.
Svet Ganovad0a49b2018-10-29 10:07:08 -07006118 *
Svet Ganovaf189e32019-02-15 18:45:29 -08006119 * @param flags The flags which are any combination of
6120 * {@link #OP_FLAG_SELF}, {@link #OP_FLAG_TRUSTED_PROXY},
6121 * {@link #OP_FLAG_UNTRUSTED_PROXY}, {@link #OP_FLAG_TRUSTED_PROXIED},
6122 * {@link #OP_FLAG_UNTRUSTED_PROXIED}. You can use {@link #OP_FLAGS_ALL}
6123 * for any flag.
Svet Ganovad0a49b2018-10-29 10:07:08 -07006124 * @return The total duration the app op was accessed in the background.
6125 *
Svet Ganovaf189e32019-02-15 18:45:29 -08006126 * @see #getForegroundAccessDuration(int)
6127 * @see #getAccessDuration(int, int, int)
Svet Ganovad0a49b2018-10-29 10:07:08 -07006128 */
Svet Ganovaf189e32019-02-15 18:45:29 -08006129 public long getBackgroundAccessDuration(@OpFlags int flags) {
6130 return sumForFlagsInStates(mAccessDuration, resolveLastRestrictedUidState(mOp),
6131 MIN_PRIORITY_UID_STATE, flags);
Svet Ganovad0a49b2018-10-29 10:07:08 -07006132 }
6133
6134 /**
Svet Ganovaf189e32019-02-15 18:45:29 -08006135 * Gets the total duration the app op was accessed (performed) for a given
Svet Ganov6f672a32019-07-08 16:40:42 -07006136 * range of UID states. The duration is in wall time.
Svet Ganovad0a49b2018-10-29 10:07:08 -07006137 *
Svet Ganovaf189e32019-02-15 18:45:29 -08006138 * @param fromUidState The UID state from which to query. Could be one of
Svet Ganovad0a49b2018-10-29 10:07:08 -07006139 * {@link #UID_STATE_PERSISTENT}, {@link #UID_STATE_TOP},
6140 * {@link #UID_STATE_FOREGROUND_SERVICE}, {@link #UID_STATE_FOREGROUND},
6141 * {@link #UID_STATE_BACKGROUND}, {@link #UID_STATE_CACHED}.
Svet Ganovaf189e32019-02-15 18:45:29 -08006142 * @param toUidState The UID state from which to query.
6143 * @param flags The flags which are any combination of
6144 * {@link #OP_FLAG_SELF}, {@link #OP_FLAG_TRUSTED_PROXY},
6145 * {@link #OP_FLAG_UNTRUSTED_PROXY}, {@link #OP_FLAG_TRUSTED_PROXIED},
6146 * {@link #OP_FLAG_UNTRUSTED_PROXIED}. You can use {@link #OP_FLAGS_ALL}
6147 * for any flag.
Svet Ganovad0a49b2018-10-29 10:07:08 -07006148 *
6149 * @return The total duration the app op was accessed for the given UID state.
6150 *
Svet Ganovaf189e32019-02-15 18:45:29 -08006151 * @see #getForegroundAccessDuration(int)
6152 * @see #getBackgroundAccessDuration(int)
Svet Ganovad0a49b2018-10-29 10:07:08 -07006153 */
Svet Ganovaf189e32019-02-15 18:45:29 -08006154 public long getAccessDuration(@UidState int fromUidState, @UidState int toUidState,
6155 @OpFlags int flags) {
6156 return sumForFlagsInStates(mAccessDuration, fromUidState, toUidState, flags);
Svet Ganovad0a49b2018-10-29 10:07:08 -07006157 }
6158
6159 @Override
6160 public int describeContents() {
6161 return 0;
6162 }
6163
6164 @Override
6165 public void writeToParcel(Parcel parcel, int flags) {
6166 parcel.writeInt(mOp);
Svet Ganovaf189e32019-02-15 18:45:29 -08006167 writeLongSparseLongArrayToParcel(mAccessCount, parcel);
6168 writeLongSparseLongArrayToParcel(mRejectCount, parcel);
6169 writeLongSparseLongArrayToParcel(mAccessDuration, parcel);
Svet Ganovad0a49b2018-10-29 10:07:08 -07006170 }
6171
Svet Ganov8455ba22019-01-02 13:05:56 -08006172 @Override
Aurimas Liutikas4d1699d2019-08-28 13:01:05 -07006173 public boolean equals(@Nullable Object obj) {
Svet Ganov8455ba22019-01-02 13:05:56 -08006174 if (this == obj) {
6175 return true;
6176 }
6177 if (obj == null || getClass() != obj.getClass()) {
6178 return false;
6179 }
6180 final HistoricalOp other = (HistoricalOp) obj;
6181 if (mOp != other.mOp) {
6182 return false;
6183 }
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08006184 if (!equalsLongSparseLongArray(mAccessCount, other.mAccessCount)) {
Svet Ganov8455ba22019-01-02 13:05:56 -08006185 return false;
6186 }
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08006187 if (!equalsLongSparseLongArray(mRejectCount, other.mRejectCount)) {
Svet Ganov8455ba22019-01-02 13:05:56 -08006188 return false;
6189 }
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08006190 return equalsLongSparseLongArray(mAccessDuration, other.mAccessDuration);
Svet Ganov8455ba22019-01-02 13:05:56 -08006191 }
6192
6193 @Override
6194 public int hashCode() {
6195 int result = mOp;
Svet Ganovaf189e32019-02-15 18:45:29 -08006196 result = 31 * result + Objects.hashCode(mAccessCount);
6197 result = 31 * result + Objects.hashCode(mRejectCount);
6198 result = 31 * result + Objects.hashCode(mAccessDuration);
Svet Ganov8455ba22019-01-02 13:05:56 -08006199 return result;
6200 }
Svet Ganovaf189e32019-02-15 18:45:29 -08006201
6202 private void accept(@NonNull HistoricalOpsVisitor visitor) {
6203 visitor.visitHistoricalOp(this);
6204 }
6205
6206 private @NonNull LongSparseLongArray getOrCreateAccessCount() {
6207 if (mAccessCount == null) {
6208 mAccessCount = new LongSparseLongArray();
6209 }
6210 return mAccessCount;
6211 }
6212
6213 private @NonNull LongSparseLongArray getOrCreateRejectCount() {
6214 if (mRejectCount == null) {
6215 mRejectCount = new LongSparseLongArray();
6216 }
6217 return mRejectCount;
6218 }
6219
6220 private @NonNull LongSparseLongArray getOrCreateAccessDuration() {
6221 if (mAccessDuration == null) {
6222 mAccessDuration = new LongSparseLongArray();
6223 }
6224 return mAccessDuration;
6225 }
6226
6227 /**
6228 * Multiplies the entries in the array with the passed in scale factor and
6229 * rounds the result at up 0.5 boundary.
6230 *
6231 * @param data The data to scale.
6232 * @param scaleFactor The scale factor.
6233 */
6234 private static void scale(@NonNull LongSparseLongArray data, double scaleFactor) {
6235 if (data != null) {
6236 final int size = data.size();
6237 for (int i = 0; i < size; i++) {
6238 data.put(data.keyAt(i), (long) HistoricalOps.round(
6239 (double) data.valueAt(i) * scaleFactor));
6240 }
6241 }
6242 }
6243
6244 /**
6245 * Merges two arrays while lazily acquiring the destination.
6246 *
6247 * @param thisSupplier The destination supplier.
6248 * @param other The array to merge in.
6249 */
6250 private static void merge(@NonNull Supplier<LongSparseLongArray> thisSupplier,
6251 @Nullable LongSparseLongArray other) {
6252 if (other != null) {
6253 final int otherSize = other.size();
6254 for (int i = 0; i < otherSize; i++) {
6255 final LongSparseLongArray that = thisSupplier.get();
6256 final long otherKey = other.keyAt(i);
6257 final long otherValue = other.valueAt(i);
6258 that.put(otherKey, that.get(otherKey) + otherValue);
6259 }
6260 }
6261 }
6262
6263 /** @hide */
6264 public @Nullable LongSparseArray<Object> collectKeys() {
6265 LongSparseArray<Object> result = AppOpsManager.collectKeys(mAccessCount,
6266 null /*result*/);
6267 result = AppOpsManager.collectKeys(mRejectCount, result);
6268 result = AppOpsManager.collectKeys(mAccessDuration, result);
6269 return result;
6270 }
6271
6272 public static final @android.annotation.NonNull Creator<HistoricalOp> CREATOR =
6273 new Creator<HistoricalOp>() {
6274 @Override
6275 public @NonNull HistoricalOp createFromParcel(@NonNull Parcel source) {
6276 return new HistoricalOp(source);
6277 }
6278
6279 @Override
6280 public @NonNull HistoricalOp[] newArray(int size) {
6281 return new HistoricalOp[size];
6282 }
6283 };
6284 }
6285
6286 /**
6287 * Computes the sum of the counts for the given flags in between the begin and
6288 * end UID states.
6289 *
6290 * @param counts The data array.
Philip P. Moltmann4052d362019-09-19 14:52:38 -07006291 * @param beginUidState The beginning UID state (inclusive).
6292 * @param endUidState The end UID state (inclusive).
Svet Ganovaf189e32019-02-15 18:45:29 -08006293 * @param flags The UID flags.
6294 * @return The sum.
6295 */
6296 private static long sumForFlagsInStates(@Nullable LongSparseLongArray counts,
6297 @UidState int beginUidState, @UidState int endUidState, @OpFlags int flags) {
6298 if (counts == null) {
6299 return 0;
6300 }
6301 long sum = 0;
6302 while (flags != 0) {
6303 final int flag = 1 << Integer.numberOfTrailingZeros(flags);
6304 flags &= ~flag;
6305 for (int uidState : UID_STATES) {
6306 if (uidState < beginUidState || uidState > endUidState) {
6307 continue;
6308 }
6309 final long key = makeKey(uidState, flag);
6310 sum += counts.get(key);
6311 }
6312 }
6313 return sum;
6314 }
6315
6316 /**
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08006317 * Callback for notification of changes to operation state.
6318 */
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07006319 public interface OnOpChangedListener {
6320 public void onOpChanged(String op, String packageName);
6321 }
6322
6323 /**
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006324 * Callback for notification of changes to operation active state.
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006325 */
6326 public interface OnOpActiveChangedListener {
6327 /**
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -08006328 * Called when the active state of an app-op changes.
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006329 *
Jeff Sharkey7095ab92019-08-20 16:50:28 -06006330 * @param op The operation that changed.
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006331 * @param packageName The package performing the operation.
6332 * @param active Whether the operation became active or inactive.
6333 */
Jeff Sharkey7095ab92019-08-20 16:50:28 -06006334 void onOpActiveChanged(@NonNull String op, int uid, @NonNull String packageName,
6335 boolean active);
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006336 }
6337
6338 /**
Svet Ganovb3d2ae22018-12-17 22:06:15 -08006339 * Callback for notification of an op being noted.
6340 *
6341 * @hide
6342 */
6343 public interface OnOpNotedListener {
6344 /**
6345 * Called when an op was noted.
6346 *
6347 * @param code The op code.
6348 * @param uid The UID performing the operation.
6349 * @param packageName The package performing the operation.
6350 * @param result The result of the note.
6351 */
Fabian Kozynski0b4592c2018-12-20 12:58:45 -05006352 void onOpNoted(int code, int uid, String packageName, int result);
Svet Ganovb3d2ae22018-12-17 22:06:15 -08006353 }
6354
6355 /**
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07006356 * Callback for notification of changes to operation state.
6357 * This allows you to see the raw op codes instead of strings.
6358 * @hide
6359 */
6360 public static class OnOpChangedInternalListener implements OnOpChangedListener {
6361 public void onOpChanged(String op, String packageName) { }
6362 public void onOpChanged(int op, String packageName) { }
Dianne Hackbornc2293022013-02-06 23:14:49 -08006363 }
6364
Jeff Sharkey7095ab92019-08-20 16:50:28 -06006365 /**
6366 * Callback for notification of changes to operation state.
6367 * This allows you to see the raw op codes instead of strings.
6368 * @hide
6369 */
6370 public interface OnOpActiveChangedInternalListener extends OnOpActiveChangedListener {
6371 default void onOpActiveChanged(String op, int uid, String packageName, boolean active) { }
6372 default void onOpActiveChanged(int op, int uid, String packageName, boolean active) { }
6373 }
6374
Adam Bookatz182862e2020-04-27 21:58:22 -07006375 /**
6376 * Callback for notification of an op being started.
6377 *
6378 * @hide
6379 */
6380 public interface OnOpStartedListener {
6381 /**
6382 * Called when an op was started.
6383 *
6384 * Note: This is only for op starts. It is not called when an op is noted or stopped.
6385 *
6386 * @param op The op code.
6387 * @param uid The UID performing the operation.
6388 * @param packageName The package performing the operation.
6389 * @param result The result of the start.
6390 */
6391 void onOpStarted(int op, int uid, String packageName, int result);
6392 }
6393
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07006394 AppOpsManager(Context context, IAppOpsService service) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006395 mContext = context;
6396 mService = service;
6397 }
6398
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08006399 /**
6400 * Retrieve current operation state for all applications.
6401 *
Winson6a864b52019-05-10 10:34:14 -07006402 * The mode of the ops returned are set for the package but may not reflect their effective
6403 * state due to UID policy or because it's controlled by a different master op.
6404 *
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07006405 * Use {@link #unsafeCheckOp(String, int, String)}} or
Philip P. Moltmann59076d82019-08-19 15:00:40 -07006406 * {@link #noteOp(String, int, String, String, String)} if the effective mode is needed.
Winson6a864b52019-05-10 10:34:14 -07006407 *
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08006408 * @param ops The set of operations you are interested in, or null if you want all of them.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07006409 * @hide
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08006410 */
Svet Ganov8455ba22019-01-02 13:05:56 -08006411 @SystemApi
6412 @RequiresPermission(android.Manifest.permission.GET_APP_OPS_STATS)
6413 public @NonNull List<AppOpsManager.PackageOps> getPackagesForOps(@Nullable String[] ops) {
6414 final int opCount = ops.length;
6415 final int[] opCodes = new int[opCount];
6416 for (int i = 0; i < opCount; i++) {
6417 opCodes[i] = sOpStrToOp.get(ops[i]);
6418 }
6419 final List<AppOpsManager.PackageOps> result = getPackagesForOps(opCodes);
6420 return (result != null) ? result : Collections.emptyList();
6421 }
6422
6423 /**
6424 * Retrieve current operation state for all applications.
6425 *
Winson6a864b52019-05-10 10:34:14 -07006426 * The mode of the ops returned are set for the package but may not reflect their effective
6427 * state due to UID policy or because it's controlled by a different master op.
6428 *
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07006429 * Use {@link #unsafeCheckOp(String, int, String)}} or
Philip P. Moltmann59076d82019-08-19 15:00:40 -07006430 * {@link #noteOp(String, int, String, String, String)} if the effective mode is needed.
Winson6a864b52019-05-10 10:34:14 -07006431 *
Svet Ganov8455ba22019-01-02 13:05:56 -08006432 * @param ops The set of operations you are interested in, or null if you want all of them.
6433 * @hide
6434 */
Dianne Hackbornc216a262018-04-26 13:46:22 -07006435 @RequiresPermission(android.Manifest.permission.GET_APP_OPS_STATS)
Mathew Inwood61e8ae62018-08-14 14:17:44 +01006436 @UnsupportedAppUsage
Dianne Hackborn35654b62013-01-14 17:38:02 -08006437 public List<AppOpsManager.PackageOps> getPackagesForOps(int[] ops) {
6438 try {
6439 return mService.getPackagesForOps(ops);
6440 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07006441 throw e.rethrowFromSystemServer();
Dianne Hackborn35654b62013-01-14 17:38:02 -08006442 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08006443 }
6444
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08006445 /**
6446 * Retrieve current operation state for one application.
6447 *
Winson6a864b52019-05-10 10:34:14 -07006448 * The mode of the ops returned are set for the package but may not reflect their effective
6449 * state due to UID policy or because it's controlled by a different master op.
6450 *
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07006451 * Use {@link #unsafeCheckOp(String, int, String)}} or
Philip P. Moltmann59076d82019-08-19 15:00:40 -07006452 * {@link #noteOp(String, int, String, String, String)} if the effective mode is needed.
Winson6a864b52019-05-10 10:34:14 -07006453 *
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08006454 * @param uid The uid of the application of interest.
6455 * @param packageName The name of the application of interest.
6456 * @param ops The set of operations you are interested in, or null if you want all of them.
Svet Ganov8455ba22019-01-02 13:05:56 -08006457 *
6458 * @deprecated The int op codes are not stable and you should use the string based op
6459 * names which are stable and namespaced. Use
6460 * {@link #getOpsForPackage(int, String, String...)})}.
6461 *
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07006462 * @hide
Suprabh Shukla169bed72019-05-13 13:54:58 -07006463 * @removed
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08006464 */
Svet Ganov8455ba22019-01-02 13:05:56 -08006465 @Deprecated
Dianne Hackbornf0c322a2018-06-13 15:24:04 -07006466 @SystemApi
Dianne Hackbornc216a262018-04-26 13:46:22 -07006467 @RequiresPermission(android.Manifest.permission.GET_APP_OPS_STATS)
Dianne Hackborn62878492019-03-11 15:57:07 -07006468 public @NonNull List<PackageOps> getOpsForPackage(int uid, @NonNull String packageName,
6469 @Nullable int[] ops) {
Dianne Hackborn72e39832013-01-18 18:36:09 -08006470 try {
6471 return mService.getOpsForPackage(uid, packageName, ops);
6472 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07006473 throw e.rethrowFromSystemServer();
Dianne Hackborn72e39832013-01-18 18:36:09 -08006474 }
Dianne Hackborn72e39832013-01-18 18:36:09 -08006475 }
6476
Svet Ganovae0e03a2016-02-25 18:22:10 -08006477 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08006478 * Retrieve current operation state for one application. The UID and the
6479 * package must match.
6480 *
Winson6a864b52019-05-10 10:34:14 -07006481 * The mode of the ops returned are set for the package but may not reflect their effective
6482 * state due to UID policy or because it's controlled by a different master op.
6483 *
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07006484 * Use {@link #unsafeCheckOp(String, int, String)}} or
Philip P. Moltmann59076d82019-08-19 15:00:40 -07006485 * {@link #noteOp(String, int, String, String, String)} if the effective mode is needed.
Winson6a864b52019-05-10 10:34:14 -07006486 *
Svet Ganov8455ba22019-01-02 13:05:56 -08006487 * @param uid The uid of the application of interest.
6488 * @param packageName The name of the application of interest.
6489 * @param ops The set of operations you are interested in, or null if you want all of them.
6490 *
6491 * @hide
6492 */
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08006493 @TestApi
Svet Ganov8455ba22019-01-02 13:05:56 -08006494 @SystemApi
6495 @RequiresPermission(android.Manifest.permission.GET_APP_OPS_STATS)
6496 public @NonNull List<AppOpsManager.PackageOps> getOpsForPackage(int uid,
6497 @NonNull String packageName, @Nullable String... ops) {
6498 int[] opCodes = null;
6499 if (ops != null) {
6500 opCodes = new int[ops.length];
6501 for (int i = 0; i < ops.length; i++) {
6502 opCodes[i] = strOpToOp(ops[i]);
6503 }
6504 }
6505 try {
6506 final List<PackageOps> result = mService.getOpsForPackage(uid, packageName, opCodes);
6507 if (result == null) {
6508 return Collections.emptyList();
6509 }
6510 return result;
6511 } catch (RemoteException e) {
6512 throw e.rethrowFromSystemServer();
6513 }
6514 }
6515
6516 /**
6517 * Retrieve historical app op stats for a period.
6518 *
Svet Ganov23c88db2019-01-22 20:38:11 -08006519 * @param request A request object describing the data being queried for.
Svet Ganov8455ba22019-01-02 13:05:56 -08006520 * @param executor Executor on which to run the callback. If <code>null</code>
6521 * the callback is executed on the default executor running on the main thread.
6522 * @param callback Callback on which to deliver the result.
Svet Ganovad0a49b2018-10-29 10:07:08 -07006523 *
6524 * @throws IllegalArgumentException If any of the argument contracts is violated.
6525 *
6526 * @hide
6527 */
6528 @TestApi
6529 @SystemApi
6530 @RequiresPermission(android.Manifest.permission.GET_APP_OPS_STATS)
Svet Ganov23c88db2019-01-22 20:38:11 -08006531 public void getHistoricalOps(@NonNull HistoricalOpsRequest request,
Svet Ganov8455ba22019-01-02 13:05:56 -08006532 @NonNull Executor executor, @NonNull Consumer<HistoricalOps> callback) {
Daulet Zhanguzin0af97d62019-12-30 15:41:28 +00006533 Objects.requireNonNull(executor, "executor cannot be null");
6534 Objects.requireNonNull(callback, "callback cannot be null");
Svet Ganovad0a49b2018-10-29 10:07:08 -07006535 try {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08006536 mService.getHistoricalOps(request.mUid, request.mPackageName, request.mAttributionTag,
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08006537 request.mOpNames, request.mFilter, request.mBeginTimeMillis,
6538 request.mEndTimeMillis, request.mFlags, new RemoteCallback((result) -> {
Svet Ganov8455ba22019-01-02 13:05:56 -08006539 final HistoricalOps ops = result.getParcelable(KEY_HISTORICAL_OPS);
6540 final long identity = Binder.clearCallingIdentity();
6541 try {
6542 executor.execute(() -> callback.accept(ops));
6543 } finally {
6544 Binder.restoreCallingIdentity(identity);
6545 }
6546 }));
Svet Ganovad0a49b2018-10-29 10:07:08 -07006547 } catch (RemoteException e) {
6548 throw e.rethrowFromSystemServer();
6549 }
6550 }
6551
6552 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08006553 * Retrieve historical app op stats for a period.
Svet Ganov8455ba22019-01-02 13:05:56 -08006554 * <p>
6555 * This method queries only the on disk state and the returned ops are raw,
6556 * which is their times are relative to the history start as opposed to the
6557 * epoch start.
6558 *
Svet Ganov23c88db2019-01-22 20:38:11 -08006559 * @param request A request object describing the data being queried for.
Svet Ganov8455ba22019-01-02 13:05:56 -08006560 * @param executor Executor on which to run the callback. If <code>null</code>
6561 * the callback is executed on the default executor running on the main thread.
6562 * @param callback Callback on which to deliver the result.
Svet Ganovad0a49b2018-10-29 10:07:08 -07006563 *
6564 * @throws IllegalArgumentException If any of the argument contracts is violated.
6565 *
6566 * @hide
6567 */
6568 @TestApi
Svet Ganov8e5bf962019-03-19 23:59:03 -07006569 @RequiresPermission(Manifest.permission.MANAGE_APPOPS)
Svet Ganov23c88db2019-01-22 20:38:11 -08006570 public void getHistoricalOpsFromDiskRaw(@NonNull HistoricalOpsRequest request,
Svet Ganov8455ba22019-01-02 13:05:56 -08006571 @Nullable Executor executor, @NonNull Consumer<HistoricalOps> callback) {
Daulet Zhanguzin0af97d62019-12-30 15:41:28 +00006572 Objects.requireNonNull(executor, "executor cannot be null");
6573 Objects.requireNonNull(callback, "callback cannot be null");
Svet Ganovad0a49b2018-10-29 10:07:08 -07006574 try {
Svet Ganov23c88db2019-01-22 20:38:11 -08006575 mService.getHistoricalOpsFromDiskRaw(request.mUid, request.mPackageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08006576 request.mAttributionTag, request.mOpNames, request.mFilter,
6577 request.mBeginTimeMillis, request.mEndTimeMillis, request.mFlags,
6578 new RemoteCallback((result) -> {
Svet Ganov23c88db2019-01-22 20:38:11 -08006579 final HistoricalOps ops = result.getParcelable(KEY_HISTORICAL_OPS);
Svet Ganov8455ba22019-01-02 13:05:56 -08006580 final long identity = Binder.clearCallingIdentity();
6581 try {
6582 executor.execute(() -> callback.accept(ops));
6583 } finally {
6584 Binder.restoreCallingIdentity(identity);
6585 }
6586 }));
Svet Ganovad0a49b2018-10-29 10:07:08 -07006587 } catch (RemoteException e) {
6588 throw e.rethrowFromSystemServer();
6589 }
6590 }
6591
6592 /**
Svet Ganov8e5bf962019-03-19 23:59:03 -07006593 * Reloads the non historical state to allow testing the read/write path.
6594 *
6595 * @hide
6596 */
6597 @TestApi
6598 @RequiresPermission(Manifest.permission.MANAGE_APPOPS)
6599 public void reloadNonHistoricalState() {
6600 try {
6601 mService.reloadNonHistoricalState();
6602 } catch (RemoteException e) {
6603 throw e.rethrowFromSystemServer();
6604 }
6605 }
6606
6607 /**
Svet Ganovae0e03a2016-02-25 18:22:10 -08006608 * Sets given app op in the specified mode for app ops in the UID.
6609 * This applies to all apps currently in the UID or installed in
6610 * this UID in the future.
6611 *
6612 * @param code The app op.
6613 * @param uid The UID for which to set the app.
6614 * @param mode The app op mode to set.
6615 * @hide
6616 */
Dianne Hackbornbf1b57d2018-03-07 12:42:47 -08006617 @RequiresPermission(android.Manifest.permission.MANAGE_APP_OPS_MODES)
Svet Ganov8455ba22019-01-02 13:05:56 -08006618 public void setUidMode(int code, int uid, @Mode int mode) {
Svet Ganov2af57082015-07-30 08:44:20 -07006619 try {
6620 mService.setUidMode(code, uid, mode);
6621 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07006622 throw e.rethrowFromSystemServer();
Svet Ganov2af57082015-07-30 08:44:20 -07006623 }
6624 }
6625
Svet Ganovae0e03a2016-02-25 18:22:10 -08006626 /**
6627 * Sets given app op in the specified mode for app ops in the UID.
6628 * This applies to all apps currently in the UID or installed in
6629 * this UID in the future.
6630 *
6631 * @param appOp The app op.
6632 * @param uid The UID for which to set the app.
6633 * @param mode The app op mode to set.
6634 * @hide
6635 */
6636 @SystemApi
Eugene Susla93519852018-06-13 16:44:31 -07006637 @TestApi
Dianne Hackbornbf1b57d2018-03-07 12:42:47 -08006638 @RequiresPermission(android.Manifest.permission.MANAGE_APP_OPS_MODES)
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -08006639 public void setUidMode(@NonNull String appOp, int uid, @Mode int mode) {
Svet Ganovae0e03a2016-02-25 18:22:10 -08006640 try {
6641 mService.setUidMode(AppOpsManager.strOpToOp(appOp), uid, mode);
6642 } catch (RemoteException e) {
6643 throw e.rethrowFromSystemServer();
6644 }
6645 }
6646
Svet Ganov2af57082015-07-30 08:44:20 -07006647 /** @hide */
Svet Ganov9cea80cd2016-02-16 11:47:00 -08006648 public void setUserRestriction(int code, boolean restricted, IBinder token) {
Ruben Brunk29931bc2016-03-11 00:24:26 -08006649 setUserRestriction(code, restricted, token, /*exceptionPackages*/null);
6650 }
6651
6652 /** @hide */
6653 public void setUserRestriction(int code, boolean restricted, IBinder token,
6654 String[] exceptionPackages) {
Svetoslav Ganove33f6132016-06-01 16:25:31 -07006655 setUserRestrictionForUser(code, restricted, token, exceptionPackages, mContext.getUserId());
6656 }
6657
6658 /** @hide */
6659 public void setUserRestrictionForUser(int code, boolean restricted, IBinder token,
6660 String[] exceptionPackages, int userId) {
Svet Ganov9cea80cd2016-02-16 11:47:00 -08006661 try {
Svetoslav Ganove33f6132016-06-01 16:25:31 -07006662 mService.setUserRestriction(code, restricted, token, userId, exceptionPackages);
Svet Ganov9cea80cd2016-02-16 11:47:00 -08006663 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07006664 throw e.rethrowFromSystemServer();
Svet Ganov9cea80cd2016-02-16 11:47:00 -08006665 }
6666 }
6667
6668 /** @hide */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +00006669 @UnsupportedAppUsage
Peter Visontayb97fbc82017-12-21 16:23:55 +00006670 @TestApi
Dianne Hackbornbf1b57d2018-03-07 12:42:47 -08006671 @RequiresPermission(android.Manifest.permission.MANAGE_APP_OPS_MODES)
Svet Ganov8455ba22019-01-02 13:05:56 -08006672 public void setMode(int code, int uid, String packageName, @Mode int mode) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08006673 try {
6674 mService.setMode(code, uid, packageName, mode);
6675 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07006676 throw e.rethrowFromSystemServer();
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08006677 }
6678 }
6679
John Spurlock1af30c72014-03-10 08:33:35 -04006680 /**
Dianne Hackbornbf1b57d2018-03-07 12:42:47 -08006681 * Change the operating mode for the given op in the given app package. You must pass
6682 * in both the uid and name of the application whose mode is being modified; if these
6683 * do not match, the modification will not be applied.
6684 *
6685 * @param op The operation to modify. One of the OPSTR_* constants.
6686 * @param uid The user id of the application whose mode will be changed.
6687 * @param packageName The name of the application package name whose mode will
6688 * be changed.
6689 * @hide
6690 */
Svet Ganov8e5bf962019-03-19 23:59:03 -07006691 @TestApi
Dianne Hackbornbf1b57d2018-03-07 12:42:47 -08006692 @SystemApi
6693 @RequiresPermission(android.Manifest.permission.MANAGE_APP_OPS_MODES)
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -08006694 public void setMode(@NonNull String op, int uid, @Nullable String packageName,
6695 @Mode int mode) {
Dianne Hackbornbf1b57d2018-03-07 12:42:47 -08006696 try {
6697 mService.setMode(strOpToOp(op), uid, packageName, mode);
6698 } catch (RemoteException e) {
6699 throw e.rethrowFromSystemServer();
6700 }
6701 }
6702
6703 /**
John Spurlock1af30c72014-03-10 08:33:35 -04006704 * Set a non-persisted restriction on an audio operation at a stream-level.
6705 * Restrictions are temporary additional constraints imposed on top of the persisted rules
6706 * defined by {@link #setMode}.
6707 *
6708 * @param code The operation to restrict.
John Spurlock7b414672014-07-18 13:02:39 -04006709 * @param usage The {@link android.media.AudioAttributes} usage value.
John Spurlock1af30c72014-03-10 08:33:35 -04006710 * @param mode The restriction mode (MODE_IGNORED,MODE_ERRORED) or MODE_ALLOWED to unrestrict.
6711 * @param exceptionPackages Optional list of packages to exclude from the restriction.
6712 * @hide
6713 */
Dianne Hackbornbf1b57d2018-03-07 12:42:47 -08006714 @RequiresPermission(android.Manifest.permission.MANAGE_APP_OPS_MODES)
Mathew Inwood61e8ae62018-08-14 14:17:44 +01006715 @UnsupportedAppUsage
Svet Ganov8455ba22019-01-02 13:05:56 -08006716 public void setRestriction(int code, @AttributeUsage int usage, @Mode int mode,
John Spurlock7b414672014-07-18 13:02:39 -04006717 String[] exceptionPackages) {
John Spurlock1af30c72014-03-10 08:33:35 -04006718 try {
6719 final int uid = Binder.getCallingUid();
John Spurlock7b414672014-07-18 13:02:39 -04006720 mService.setAudioRestriction(code, usage, uid, mode, exceptionPackages);
John Spurlock1af30c72014-03-10 08:33:35 -04006721 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07006722 throw e.rethrowFromSystemServer();
John Spurlock1af30c72014-03-10 08:33:35 -04006723 }
6724 }
6725
Dianne Hackborn607b4142013-08-02 18:10:10 -07006726 /** @hide */
Dianne Hackbornbf1b57d2018-03-07 12:42:47 -08006727 @RequiresPermission(android.Manifest.permission.MANAGE_APP_OPS_MODES)
Mathew Inwood61e8ae62018-08-14 14:17:44 +01006728 @UnsupportedAppUsage
Dianne Hackborn607b4142013-08-02 18:10:10 -07006729 public void resetAllModes() {
6730 try {
Jeff Sharkeyad357d12018-02-02 13:25:31 -07006731 mService.resetAllModes(mContext.getUserId(), null);
Dianne Hackborn607b4142013-08-02 18:10:10 -07006732 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07006733 throw e.rethrowFromSystemServer();
Dianne Hackborn607b4142013-08-02 18:10:10 -07006734 }
6735 }
6736
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07006737 /**
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -08006738 * Gets the app-op name associated with a given permission.
6739 *
6740 * <p>The app-op name is one of the public constants defined
Svet Ganovfbf01f72015-04-28 18:39:06 -07006741 * in this class such as {@link #OPSTR_COARSE_LOCATION}.
Svetoslav Ganoveaca4c52016-05-05 18:08:00 -07006742 * This API is intended to be used for mapping runtime
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -08006743 * permissions to the corresponding app-op.
Svet Ganovfbf01f72015-04-28 18:39:06 -07006744 *
6745 * @param permission The permission.
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -08006746 * @return The app-op associated with the permission or {@code null}.
Svet Ganovfbf01f72015-04-28 18:39:06 -07006747 */
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -08006748 public static @Nullable String permissionToOp(@NonNull String permission) {
Svet Ganovda0acdf2017-02-15 10:28:51 -08006749 final Integer opCode = sPermToOp.get(permission);
Svet Ganovb9d71a62015-04-30 10:38:13 -07006750 if (opCode == null) {
6751 return null;
6752 }
6753 return sOpToString[opCode];
Svet Ganovfbf01f72015-04-28 18:39:06 -07006754 }
6755
6756 /**
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07006757 * Monitor for changes to the operating mode for the given op in the given app package.
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006758 * You can watch op changes only for your UID.
6759 *
Dianne Hackborne4cb66f2013-10-02 10:34:02 -07006760 * @param op The operation to monitor, one of OPSTR_*.
6761 * @param packageName The name of the application to monitor.
6762 * @param callback Where to report changes.
6763 */
Dianne Hackborn62878492019-03-11 15:57:07 -07006764 public void startWatchingMode(@NonNull String op, @Nullable String packageName,
6765 @NonNull final OnOpChangedListener callback) {
Dianne Hackborne4cb66f2013-10-02 10:34:02 -07006766 startWatchingMode(strOpToOp(op), packageName, callback);
6767 }
6768
6769 /**
6770 * Monitor for changes to the operating mode for the given op in the given app package.
Dianne Hackborn65a4f252018-05-08 17:30:48 -07006771 * You can watch op changes only for your UID.
6772 *
6773 * @param op The operation to monitor, one of OPSTR_*.
6774 * @param packageName The name of the application to monitor.
6775 * @param flags Option flags: any combination of {@link #WATCH_FOREGROUND_CHANGES} or 0.
6776 * @param callback Where to report changes.
Dianne Hackborn65a4f252018-05-08 17:30:48 -07006777 */
Dianne Hackborn62878492019-03-11 15:57:07 -07006778 public void startWatchingMode(@NonNull String op, @Nullable String packageName, int flags,
6779 @NonNull final OnOpChangedListener callback) {
Dianne Hackborn65a4f252018-05-08 17:30:48 -07006780 startWatchingMode(strOpToOp(op), packageName, flags, callback);
6781 }
6782
6783 /**
6784 * Monitor for changes to the operating mode for the given op in the given app package.
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006785 *
6786 * <p> If you don't hold the {@link android.Manifest.permission#WATCH_APPOPS} permission
Svet Ganovf7b47252018-02-26 11:11:27 -08006787 * you can watch changes only for your UID.
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006788 *
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07006789 * @param op The operation to monitor, one of OP_*.
6790 * @param packageName The name of the application to monitor.
6791 * @param callback Where to report changes.
Dianne Hackborne4cb66f2013-10-02 10:34:02 -07006792 * @hide
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07006793 */
Jeff Sharkeybf6b2132018-02-27 11:16:37 -07006794 @RequiresPermission(value=android.Manifest.permission.WATCH_APPOPS, conditional=true)
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07006795 public void startWatchingMode(int op, String packageName, final OnOpChangedListener callback) {
Dianne Hackborn65a4f252018-05-08 17:30:48 -07006796 startWatchingMode(op, packageName, 0, callback);
6797 }
6798
6799 /**
6800 * Monitor for changes to the operating mode for the given op in the given app package.
6801 *
6802 * <p> If you don't hold the {@link android.Manifest.permission#WATCH_APPOPS} permission
6803 * you can watch changes only for your UID.
6804 *
6805 * @param op The operation to monitor, one of OP_*.
6806 * @param packageName The name of the application to monitor.
6807 * @param flags Option flags: any combination of {@link #WATCH_FOREGROUND_CHANGES} or 0.
6808 * @param callback Where to report changes.
6809 * @hide
6810 */
6811 @RequiresPermission(value=android.Manifest.permission.WATCH_APPOPS, conditional=true)
6812 public void startWatchingMode(int op, String packageName, int flags,
6813 final OnOpChangedListener callback) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08006814 synchronized (mModeWatchers) {
6815 IAppOpsCallback cb = mModeWatchers.get(callback);
6816 if (cb == null) {
6817 cb = new IAppOpsCallback.Stub() {
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07006818 public void opChanged(int op, int uid, String packageName) {
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07006819 if (callback instanceof OnOpChangedInternalListener) {
6820 ((OnOpChangedInternalListener)callback).onOpChanged(op, packageName);
6821 }
6822 if (sOpToString[op] != null) {
6823 callback.onOpChanged(sOpToString[op], packageName);
6824 }
Dianne Hackbornc2293022013-02-06 23:14:49 -08006825 }
6826 };
6827 mModeWatchers.put(callback, cb);
6828 }
Philip P. Moltmann7641cd02020-03-11 14:42:43 -07006829
6830 // See CALL_BACK_ON_CHANGED_LISTENER_WITH_SWITCHED_OP_CHANGE
6831 if (!Compatibility.isChangeEnabled(
6832 CALL_BACK_ON_CHANGED_LISTENER_WITH_SWITCHED_OP_CHANGE)) {
6833 flags |= CALL_BACK_ON_SWITCHED_OP;
6834 }
6835
Dianne Hackbornc2293022013-02-06 23:14:49 -08006836 try {
Dianne Hackborn65a4f252018-05-08 17:30:48 -07006837 mService.startWatchingModeWithFlags(op, packageName, flags, cb);
Dianne Hackbornc2293022013-02-06 23:14:49 -08006838 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07006839 throw e.rethrowFromSystemServer();
Dianne Hackbornc2293022013-02-06 23:14:49 -08006840 }
6841 }
6842 }
6843
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07006844 /**
6845 * Stop monitoring that was previously started with {@link #startWatchingMode}. All
6846 * monitoring associated with this callback will be removed.
6847 */
Dianne Hackborn62878492019-03-11 15:57:07 -07006848 public void stopWatchingMode(@NonNull OnOpChangedListener callback) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08006849 synchronized (mModeWatchers) {
Svet Ganovb3d2ae22018-12-17 22:06:15 -08006850 IAppOpsCallback cb = mModeWatchers.remove(callback);
Dianne Hackbornc2293022013-02-06 23:14:49 -08006851 if (cb != null) {
6852 try {
6853 mService.stopWatchingMode(cb);
6854 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07006855 throw e.rethrowFromSystemServer();
Dianne Hackbornc2293022013-02-06 23:14:49 -08006856 }
6857 }
6858 }
6859 }
6860
Jeff Sharkey7095ab92019-08-20 16:50:28 -06006861 /** {@hide} */
6862 @Deprecated
6863 public void startWatchingActive(@NonNull int[] ops,
6864 @NonNull OnOpActiveChangedListener callback) {
6865 final String[] strOps = new String[ops.length];
6866 for (int i = 0; i < ops.length; i++) {
6867 strOps[i] = opToPublicName(ops[i]);
6868 }
6869 startWatchingActive(strOps, mContext.getMainExecutor(), callback);
6870 }
6871
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006872 /**
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -08006873 * Start watching for changes to the active state of app-ops. An app-op may be
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006874 * long running and it has a clear start and stop delimiters. If an op is being
6875 * started or stopped by any package you will get a callback. To change the
6876 * watched ops for a registered callback you need to unregister and register it
6877 * again.
6878 *
Jeff Sharkey7095ab92019-08-20 16:50:28 -06006879 * <p> If you don't hold the {@code android.Manifest.permission#WATCH_APPOPS} permission
Svet Ganovf7b47252018-02-26 11:11:27 -08006880 * you can watch changes only for your UID.
6881 *
Jeff Sharkey7095ab92019-08-20 16:50:28 -06006882 * @param ops The operations to watch.
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006883 * @param callback Where to report changes.
6884 *
Jeff Sharkey7095ab92019-08-20 16:50:28 -06006885 * @see #stopWatchingActive
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006886 */
Svet Ganovf7b47252018-02-26 11:11:27 -08006887 // TODO: Uncomment below annotation once b/73559440 is fixed
6888 // @RequiresPermission(value=Manifest.permission.WATCH_APPOPS, conditional=true)
Jeff Sharkey7095ab92019-08-20 16:50:28 -06006889 public void startWatchingActive(@NonNull String[] ops,
6890 @CallbackExecutor @NonNull Executor executor,
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006891 @NonNull OnOpActiveChangedListener callback) {
Jeff Sharkey7095ab92019-08-20 16:50:28 -06006892 Objects.requireNonNull(ops);
6893 Objects.requireNonNull(executor);
6894 Objects.requireNonNull(callback);
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006895 IAppOpsActiveCallback cb;
6896 synchronized (mActiveWatchers) {
6897 cb = mActiveWatchers.get(callback);
6898 if (cb != null) {
6899 return;
6900 }
6901 cb = new IAppOpsActiveCallback.Stub() {
6902 @Override
6903 public void opActiveChanged(int op, int uid, String packageName, boolean active) {
Jeff Sharkey7095ab92019-08-20 16:50:28 -06006904 executor.execute(() -> {
6905 if (callback instanceof OnOpActiveChangedInternalListener) {
6906 ((OnOpActiveChangedInternalListener) callback).onOpActiveChanged(op,
6907 uid, packageName, active);
6908 }
6909 if (sOpToString[op] != null) {
6910 callback.onOpActiveChanged(sOpToString[op], uid, packageName, active);
6911 }
6912 });
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006913 }
6914 };
6915 mActiveWatchers.put(callback, cb);
6916 }
Jeff Sharkey7095ab92019-08-20 16:50:28 -06006917 final int[] rawOps = new int[ops.length];
6918 for (int i = 0; i < ops.length; i++) {
6919 rawOps[i] = strOpToOp(ops[i]);
6920 }
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006921 try {
Jeff Sharkey7095ab92019-08-20 16:50:28 -06006922 mService.startWatchingActive(rawOps, cb);
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006923 } catch (RemoteException e) {
6924 throw e.rethrowFromSystemServer();
6925 }
6926 }
6927
6928 /**
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -08006929 * Stop watching for changes to the active state of an app-op. An app-op may be
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006930 * long running and it has a clear start and stop delimiters. Unregistering a
6931 * non-registered callback has no effect.
6932 *
Jeff Sharkey7095ab92019-08-20 16:50:28 -06006933 * @see #startWatchingActive
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006934 */
6935 public void stopWatchingActive(@NonNull OnOpActiveChangedListener callback) {
6936 synchronized (mActiveWatchers) {
Svet Ganovb3d2ae22018-12-17 22:06:15 -08006937 final IAppOpsActiveCallback cb = mActiveWatchers.remove(callback);
Svetoslav Ganov2d20fb42018-02-08 15:52:10 -08006938 if (cb != null) {
6939 try {
6940 mService.stopWatchingActive(cb);
6941 } catch (RemoteException e) {
6942 throw e.rethrowFromSystemServer();
6943 }
6944 }
6945 }
6946 }
6947
Svet Ganovb3d2ae22018-12-17 22:06:15 -08006948 /**
Adam Bookatz182862e2020-04-27 21:58:22 -07006949 * Start watching for started app-ops.
6950 * An app-op may be long running and it has a clear start delimiter.
6951 * If an op start is attempted by any package, you will get a callback.
6952 * To change the watched ops for a registered callback you need to unregister and register it
6953 * again.
6954 *
6955 * <p> If you don't hold the {@code android.Manifest.permission#WATCH_APPOPS} permission
6956 * you can watch changes only for your UID.
6957 *
6958 * @param ops The operations to watch.
6959 * @param callback Where to report changes.
6960 *
6961 * @see #stopWatchingStarted(OnOpStartedListener)
6962 * @see #startWatchingActive(int[], OnOpActiveChangedListener)
6963 * @see #startWatchingNoted(int[], OnOpNotedListener)
6964 * @see #startOp(int, int, String, boolean, String, String)
6965 * @see #finishOp(int, int, String, String)
6966 *
6967 * @hide
6968 */
6969 @RequiresPermission(value=Manifest.permission.WATCH_APPOPS, conditional=true)
6970 public void startWatchingStarted(@NonNull int[] ops, @NonNull OnOpStartedListener callback) {
6971 IAppOpsStartedCallback cb;
6972 synchronized (mStartedWatchers) {
6973 if (mStartedWatchers.containsKey(callback)) {
6974 return;
6975 }
6976 cb = new IAppOpsStartedCallback.Stub() {
6977 @Override
6978 public void opStarted(int op, int uid, String packageName, int mode) {
6979 callback.onOpStarted(op, uid, packageName, mode);
6980 }
6981 };
6982 mStartedWatchers.put(callback, cb);
6983 }
6984 try {
6985 mService.startWatchingStarted(ops, cb);
6986 } catch (RemoteException e) {
6987 throw e.rethrowFromSystemServer();
6988 }
6989 }
6990
6991 /**
6992 * Stop watching for started app-ops.
6993 * An app-op may be long running and it has a clear start delimiter.
6994 * Henceforth, if an op start is attempted by any package, you will not get a callback.
6995 * Unregistering a non-registered callback has no effect.
6996 *
6997 * @see #startWatchingStarted(int[], OnOpStartedListener)
6998 * @see #startOp(int, int, String, boolean, String, String)
6999 *
7000 * @hide
7001 */
7002 public void stopWatchingStarted(@NonNull OnOpStartedListener callback) {
7003 synchronized (mStartedWatchers) {
7004 final IAppOpsStartedCallback cb = mStartedWatchers.remove(callback);
7005 if (cb != null) {
7006 try {
7007 mService.stopWatchingStarted(cb);
7008 } catch (RemoteException e) {
7009 throw e.rethrowFromSystemServer();
7010 }
7011 }
7012 }
7013 }
7014
7015 /**
Svet Ganovb3d2ae22018-12-17 22:06:15 -08007016 * Start watching for noted app ops. An app op may be immediate or long running.
7017 * Immediate ops are noted while long running ones are started and stopped. This
7018 * method allows registering a listener to be notified when an app op is noted. If
7019 * an op is being noted by any package you will get a callback. To change the
7020 * watched ops for a registered callback you need to unregister and register it again.
7021 *
7022 * <p> If you don't hold the {@link android.Manifest.permission#WATCH_APPOPS} permission
7023 * you can watch changes only for your UID.
7024 *
7025 * @param ops The ops to watch.
7026 * @param callback Where to report changes.
7027 *
7028 * @see #startWatchingActive(int[], OnOpActiveChangedListener)
Adam Bookatz182862e2020-04-27 21:58:22 -07007029 * @see #startWatchingStarted(int[], OnOpStartedListener)
Svet Ganovb3d2ae22018-12-17 22:06:15 -08007030 * @see #stopWatchingNoted(OnOpNotedListener)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007031 * @see #noteOp(String, int, String, String, String)
Svet Ganovb3d2ae22018-12-17 22:06:15 -08007032 *
7033 * @hide
7034 */
7035 @RequiresPermission(value=Manifest.permission.WATCH_APPOPS, conditional=true)
Fabian Kozynski0b4592c2018-12-20 12:58:45 -05007036 public void startWatchingNoted(@NonNull int[] ops, @NonNull OnOpNotedListener callback) {
Svet Ganovb3d2ae22018-12-17 22:06:15 -08007037 IAppOpsNotedCallback cb;
7038 synchronized (mNotedWatchers) {
7039 cb = mNotedWatchers.get(callback);
7040 if (cb != null) {
7041 return;
7042 }
7043 cb = new IAppOpsNotedCallback.Stub() {
7044 @Override
7045 public void opNoted(int op, int uid, String packageName, int mode) {
Fabian Kozynski0b4592c2018-12-20 12:58:45 -05007046 callback.onOpNoted(op, uid, packageName, mode);
Svet Ganovb3d2ae22018-12-17 22:06:15 -08007047 }
7048 };
7049 mNotedWatchers.put(callback, cb);
7050 }
7051 try {
Fabian Kozynski0b4592c2018-12-20 12:58:45 -05007052 mService.startWatchingNoted(ops, cb);
Svet Ganovb3d2ae22018-12-17 22:06:15 -08007053 } catch (RemoteException e) {
7054 throw e.rethrowFromSystemServer();
7055 }
7056 }
7057
7058 /**
7059 * Stop watching for noted app ops. An app op may be immediate or long running.
7060 * Unregistering a non-registered callback has no effect.
7061 *
Fabian Kozynski0b4592c2018-12-20 12:58:45 -05007062 * @see #startWatchingNoted(int[], OnOpNotedListener)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007063 * @see #noteOp(String, int, String, String, String)
Svet Ganovb3d2ae22018-12-17 22:06:15 -08007064 *
7065 * @hide
7066 */
7067 public void stopWatchingNoted(@NonNull OnOpNotedListener callback) {
7068 synchronized (mNotedWatchers) {
Adam Bookatz182862e2020-04-27 21:58:22 -07007069 final IAppOpsNotedCallback cb = mNotedWatchers.remove(callback);
Svet Ganovb3d2ae22018-12-17 22:06:15 -08007070 if (cb != null) {
7071 try {
7072 mService.stopWatchingNoted(cb);
7073 } catch (RemoteException e) {
7074 throw e.rethrowFromSystemServer();
7075 }
7076 }
7077 }
7078 }
7079
Dianne Hackborn95d78532013-09-11 09:51:14 -07007080 private String buildSecurityExceptionMsg(int op, int uid, String packageName) {
7081 return packageName + " from uid " + uid + " not allowed to perform " + sOpNames[op];
7082 }
7083
Adam Lesinskib5cf61b2014-08-18 16:10:28 -07007084 /**
7085 * {@hide}
7086 */
Artur Satayevf0b7d0b2019-11-04 11:16:45 +00007087 @UnsupportedAppUsage
Philip P. Moltmann33115152018-04-11 13:39:36 -07007088 @TestApi
Dianne Hackborn62878492019-03-11 15:57:07 -07007089 public static int strOpToOp(@NonNull String op) {
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07007090 Integer val = sOpStrToOp.get(op);
7091 if (val == null) {
7092 throw new IllegalArgumentException("Unknown operation string: " + op);
7093 }
7094 return val;
7095 }
7096
7097 /**
7098 * Do a quick check for whether an application might be able to perform an operation.
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007099 * This is <em>not</em> a security check; you must use {@link #noteOp(String, int, String,
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007100 * String, String)} or {@link #startOp(String, int, String, String, String)} for your actual
7101 * security checks, which also ensure that the given uid and package name are consistent. This
7102 * function can just be used for a quick check to see if an operation has been disabled for the
7103 * application, as an early reject of some work. This does not modify the time stamp or other
7104 * data about the operation.
Dianne Hackbornc216a262018-04-26 13:46:22 -07007105 *
7106 * <p>Important things this will not do (which you need to ultimate use
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007107 * {@link #noteOp(String, int, String, String, String)} or
7108 * {@link #startOp(String, int, String, String, String)} to cover):</p>
Dianne Hackbornc216a262018-04-26 13:46:22 -07007109 * <ul>
7110 * <li>Verifying the uid and package are consistent, so callers can't spoof
7111 * their identity.</li>
7112 * <li>Taking into account the current foreground/background state of the
7113 * app; apps whose mode varies by this state will always be reported
7114 * as {@link #MODE_ALLOWED}.</li>
7115 * </ul>
7116 *
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07007117 * @param op The operation to check. One of the OPSTR_* constants.
7118 * @param uid The user id of the application attempting to perform the operation.
7119 * @param packageName The name of the application attempting to perform the operation.
7120 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
7121 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
7122 * causing the app to crash).
7123 * @throws SecurityException If the app has been configured to crash on this op.
7124 */
Dianne Hackborn62878492019-03-11 15:57:07 -07007125 public int unsafeCheckOp(@NonNull String op, int uid, @NonNull String packageName) {
Dianne Hackbornf0c322a2018-06-13 15:24:04 -07007126 return checkOp(strOpToOp(op), uid, packageName);
7127 }
7128
7129 /**
7130 * @deprecated Renamed to {@link #unsafeCheckOp(String, int, String)}.
7131 */
7132 @Deprecated
Dianne Hackborn62878492019-03-11 15:57:07 -07007133 public int checkOp(@NonNull String op, int uid, @NonNull String packageName) {
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07007134 return checkOp(strOpToOp(op), uid, packageName);
7135 }
7136
7137 /**
John Spurlock925b85e2014-03-10 16:52:11 -04007138 * Like {@link #checkOp} but instead of throwing a {@link SecurityException} it
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07007139 * returns {@link #MODE_ERRORED}.
7140 */
Dianne Hackborn62878492019-03-11 15:57:07 -07007141 public int unsafeCheckOpNoThrow(@NonNull String op, int uid, @NonNull String packageName) {
Dianne Hackbornf0c322a2018-06-13 15:24:04 -07007142 return checkOpNoThrow(strOpToOp(op), uid, packageName);
7143 }
7144
7145 /**
7146 * @deprecated Renamed to {@link #unsafeCheckOpNoThrow(String, int, String)}.
7147 */
7148 @Deprecated
Dianne Hackborn62878492019-03-11 15:57:07 -07007149 public int checkOpNoThrow(@NonNull String op, int uid, @NonNull String packageName) {
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07007150 return checkOpNoThrow(strOpToOp(op), uid, packageName);
7151 }
7152
7153 /**
Dianne Hackborn65a4f252018-05-08 17:30:48 -07007154 * Like {@link #checkOp} but returns the <em>raw</em> mode associated with the op.
7155 * Does not throw a security exception, does not translate {@link #MODE_FOREGROUND}.
Dianne Hackborn65a4f252018-05-08 17:30:48 -07007156 */
Dianne Hackborn62878492019-03-11 15:57:07 -07007157 public int unsafeCheckOpRaw(@NonNull String op, int uid, @NonNull String packageName) {
Adam Bookatz38767932020-02-05 12:13:40 -08007158 return unsafeCheckOpRawNoThrow(op, uid, packageName);
Svet Ganovaf189e32019-02-15 18:45:29 -08007159 }
7160
7161 /**
7162 * Like {@link #unsafeCheckOpNoThrow(String, int, String)} but returns the <em>raw</em>
7163 * mode associated with the op. Does not throw a security exception, does not translate
7164 * {@link #MODE_FOREGROUND}.
7165 */
7166 public int unsafeCheckOpRawNoThrow(@NonNull String op, int uid, @NonNull String packageName) {
Adam Bookatz38767932020-02-05 12:13:40 -08007167 return unsafeCheckOpRawNoThrow(strOpToOp(op), uid, packageName);
7168 }
7169
7170 /**
7171 * Returns the <em>raw</em> mode associated with the op.
7172 * Does not throw a security exception, does not translate {@link #MODE_FOREGROUND}.
7173 * @hide
7174 */
7175 public int unsafeCheckOpRawNoThrow(int op, int uid, @NonNull String packageName) {
Dianne Hackborn65a4f252018-05-08 17:30:48 -07007176 try {
Adam Bookatz38767932020-02-05 12:13:40 -08007177 return mService.checkOperationRaw(op, uid, packageName);
Dianne Hackborn65a4f252018-05-08 17:30:48 -07007178 } catch (RemoteException e) {
7179 throw e.rethrowFromSystemServer();
7180 }
7181 }
7182
7183 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007184 * @deprecated Use {@link #noteOp(String, int, String, String, String)} instead
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007185 */
7186 @Deprecated
7187 public int noteOp(@NonNull String op, int uid, @NonNull String packageName) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007188 return noteOp(op, uid, packageName, null, null);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007189 }
7190
7191 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007192 * @deprecated Use {@link #noteOp(String, int, String, String, String)} instead
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007193 *
7194 * @hide
7195 */
7196 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.Q, publicAlternatives = "Use {@link "
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007197 + "#noteOp(java.lang.String, int, java.lang.String, java.lang.String, "
7198 + "java.lang.String)} instead")
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007199 @Deprecated
7200 public int noteOp(int op) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007201 return noteOp(op, Process.myUid(), mContext.getOpPackageName(), null, null);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007202 }
7203
7204 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007205 * @deprecated Use {@link #noteOp(String, int, String, String, String)} instead
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007206 *
7207 * @hide
7208 */
7209 @Deprecated
7210 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.Q, publicAlternatives = "Use {@link "
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007211 + "#noteOp(java.lang.String, int, java.lang.String, java.lang.String, "
7212 + "java.lang.String)} instead")
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007213 public int noteOp(int op, int uid, @Nullable String packageName) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007214 return noteOp(op, uid, packageName, null, null);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007215 }
7216
7217 /**
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07007218 * Make note of an application performing an operation. Note that you must pass
7219 * in both the uid and name of the application to be checked; this function will verify
7220 * that these two match, and if not, return {@link #MODE_IGNORED}. If this call
7221 * succeeds, the last execution time of the operation for this app will be updated to
7222 * the current time.
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007223 *
Philip P. Moltmann9e367002020-02-03 17:01:05 -08007224 * <p>If this is a check that is not preceding the protected operation, use
7225 * {@link #unsafeCheckOp} instead.
7226 *
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07007227 * @param op The operation to note. One of the OPSTR_* constants.
7228 * @param uid The user id of the application attempting to perform the operation.
7229 * @param packageName The name of the application attempting to perform the operation.
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007230 * @param attributionTag The {@link Context#createAttributionContext attribution tag} or {@code
7231 * null} for default attribution
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007232 * @param message A message describing the reason the op was noted
Philip P. Moltmann0d05e482019-02-08 13:05:25 -08007233 *
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007234 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
7235 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
7236 * causing the app to crash).
7237 *
7238 * @throws SecurityException If the app has been configured to crash on this op.
Svet Ganov99b60432015-06-27 13:15:22 -07007239 */
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007240 public int noteOp(@NonNull String op, int uid, @Nullable String packageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007241 @Nullable String attributionTag, @Nullable String message) {
7242 return noteOp(strOpToOp(op), uid, packageName, attributionTag, message);
Svet Ganov99b60432015-06-27 13:15:22 -07007243 }
7244
7245 /**
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007246 * Make note of an application performing an operation. Note that you must pass
7247 * in both the uid and name of the application to be checked; this function will verify
7248 * that these two match, and if not, return {@link #MODE_IGNORED}. If this call
7249 * succeeds, the last execution time of the operation for this app will be updated to
7250 * the current time.
7251 *
Philip P. Moltmann9e367002020-02-03 17:01:05 -08007252 * <p>If this is a check that is not preceding the protected operation, use
7253 * {@link #unsafeCheckOp} instead.
7254 *
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007255 * @param op The operation to note. One of the OP_* constants.
7256 * @param uid The user id of the application attempting to perform the operation.
7257 * @param packageName The name of the application attempting to perform the operation.
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007258 * @param attributionTag The {@link Context#createAttributionContext attribution tag} or {@code
7259 * null} for default attribution
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007260 * @param message A message describing the reason the op was noted
7261 *
7262 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
7263 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
7264 * causing the app to crash).
7265 *
7266 * @throws SecurityException If the app has been configured to crash on this op.
7267 *
7268 * @hide
7269 */
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007270 public int noteOp(int op, int uid, @Nullable String packageName,
7271 @Nullable String attributionTag, @Nullable String message) {
7272 final int mode = noteOpNoThrow(op, uid, packageName, attributionTag, message);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007273 if (mode == MODE_ERRORED) {
7274 throw new SecurityException(buildSecurityExceptionMsg(op, uid, packageName));
7275 }
7276 return mode;
7277 }
7278
7279 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007280 * @deprecated Use {@link #noteOpNoThrow(String, int, String, String, String)} instead
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007281 */
7282 @Deprecated
7283 public int noteOpNoThrow(@NonNull String op, int uid, @NonNull String packageName) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007284 return noteOpNoThrow(op, uid, packageName, null, null);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007285 }
7286
7287 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007288 * @deprecated Use {@link #noteOpNoThrow(int, int, String, String, String)} instead
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007289 *
7290 * @hide
7291 */
7292 @Deprecated
7293 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.Q, publicAlternatives = "Use {@link "
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007294 + "#noteOpNoThrow(java.lang.String, int, java.lang.String, java.lang.String, "
7295 + "java.lang.String)} instead")
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007296 public int noteOpNoThrow(int op, int uid, String packageName) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007297 return noteOpNoThrow(op, uid, packageName, null, null);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007298 }
7299
7300 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007301 * Like {@link #noteOp(String, int, String, String, String)} but instead of throwing a
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007302 * {@link SecurityException} it returns {@link #MODE_ERRORED}.
7303 *
7304 * @param op The operation to note. One of the OPSTR_* constants.
7305 * @param uid The user id of the application attempting to perform the operation.
7306 * @param packageName The name of the application attempting to perform the operation.
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007307 * @param attributionTag The {@link Context#createAttributionContext attribution tag} or {@code
7308 * null} for default attribution
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007309 * @param message A message describing the reason the op was noted
7310 *
7311 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
7312 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
7313 * causing the app to crash).
7314 */
7315 public int noteOpNoThrow(@NonNull String op, int uid, @NonNull String packageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007316 @Nullable String attributionTag, @Nullable String message) {
7317 return noteOpNoThrow(strOpToOp(op), uid, packageName, attributionTag, message);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007318 }
7319
7320 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007321 * Like {@link #noteOp(String, int, String, String, String)} but instead of throwing a
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007322 * {@link SecurityException} it returns {@link #MODE_ERRORED}.
7323 *
7324 * @param op The operation to note. One of the OP_* constants.
7325 * @param uid The user id of the application attempting to perform the operation.
7326 * @param packageName The name of the application attempting to perform the operation.
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007327 * @param attributionTag The {@link Context#createAttributionContext attribution tag} or {@code
7328 * null} for default attribution
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007329 * @param message A message describing the reason the op was noted
7330 *
7331 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
7332 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
7333 * causing the app to crash).
7334 *
7335 * @hide
7336 */
7337 public int noteOpNoThrow(int op, int uid, @Nullable String packageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007338 @Nullable String attributionTag, @Nullable String message) {
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007339 try {
David Cheung2ead9662020-02-19 16:11:06 -08007340 collectNoteOpCallsForValidation(op);
Philip P. Moltmannda554e42019-12-20 11:21:02 -08007341 int collectionMode = getNotedOpCollectionMode(uid, packageName, op);
7342 if (collectionMode == COLLECT_ASYNC) {
7343 if (message == null) {
7344 // Set stack trace as default message
7345 message = getFormattedStackTrace();
7346 }
7347 }
7348
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007349 int mode = mService.noteOperation(op, uid, packageName, attributionTag,
Philip P. Moltmannda554e42019-12-20 11:21:02 -08007350 collectionMode == COLLECT_ASYNC, message);
7351
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007352 if (mode == MODE_ALLOWED) {
Philip P. Moltmannda554e42019-12-20 11:21:02 -08007353 if (collectionMode == COLLECT_SELF) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007354 collectNotedOpForSelf(op, attributionTag);
Philip P. Moltmannda554e42019-12-20 11:21:02 -08007355 } else if (collectionMode == COLLECT_SYNC) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007356 collectNotedOpSync(op, attributionTag);
Philip P. Moltmannda554e42019-12-20 11:21:02 -08007357 }
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007358 }
7359
7360 return mode;
7361 } catch (RemoteException e) {
7362 throw e.rethrowFromSystemServer();
7363 }
7364 }
7365
7366 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007367 * @deprecated Use {@link #noteProxyOp(String, String, int, String, String)} instead
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007368 */
7369 @Deprecated
7370 public int noteProxyOp(@NonNull String op, @NonNull String proxiedPackageName) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007371 return noteProxyOp(op, proxiedPackageName, Binder.getCallingUid(), null, null);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007372 }
7373
7374 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007375 * @deprecated Use {@link #noteProxyOp(String, String, int, String, String)} instead
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007376 *
7377 * @hide
7378 */
7379 @Deprecated
7380 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.Q, publicAlternatives = "Use {@link "
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007381 + "#noteProxyOp(java.lang.String, java.lang.String, int, java.lang.String, "
7382 + "java.lang.String)} instead")
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007383 public int noteProxyOp(int op, @Nullable String proxiedPackageName) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007384 return noteProxyOp(op, proxiedPackageName, Binder.getCallingUid(), null, null);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007385 }
7386
7387 /**
7388 * Make note of an application performing an operation on behalf of another application when
7389 * handling an IPC. This function will verify that the calling uid and proxied package name
7390 * match, and if not, return {@link #MODE_IGNORED}. If this call succeeds, the last execution
7391 * time of the operation for the proxied app and your app will be updated to the current time.
7392 *
7393 * @param op The operation to note. One of the OP_* constants.
7394 * @param proxiedPackageName The name of the application calling into the proxy application.
7395 * @param proxiedUid The uid of the proxied application
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007396 * @param proxiedAttributionTag The proxied {@link Context#createAttributionContext
7397 * attribution tag} or {@code null} for default attribution
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007398 * @param message A message describing the reason the op was noted
7399 *
7400 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or {@link #MODE_IGNORED}
7401 * if it is not allowed and should be silently ignored (without causing the app to crash).
7402 *
7403 * @throws SecurityException If the proxy or proxied app has been configured to crash on this
7404 * op.
7405 *
7406 * @hide
7407 */
7408 public int noteProxyOp(int op, @Nullable String proxiedPackageName, int proxiedUid,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007409 @Nullable String proxiedAttributionTag, @Nullable String message) {
7410 int mode = noteProxyOpNoThrow(op, proxiedPackageName, proxiedUid, proxiedAttributionTag,
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007411 message);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007412 if (mode == MODE_ERRORED) {
7413 throw new SecurityException("Proxy package " + mContext.getOpPackageName()
7414 + " from uid " + Process.myUid() + " or calling package " + proxiedPackageName
7415 + " from uid " + proxiedUid + " not allowed to perform " + sOpNames[op]);
7416 }
7417 return mode;
7418 }
7419
7420 /**
7421 * Make note of an application performing an operation on behalf of another application when
7422 * handling an IPC. This function will verify that the calling uid and proxied package name
7423 * match, and if not, return {@link #MODE_IGNORED}. If this call succeeds, the last execution
7424 * time of the operation for the proxied app and your app will be updated to the current time.
7425 *
7426 * @param op The operation to note. One of the OPSTR_* constants.
7427 * @param proxiedPackageName The name of the application calling into the proxy application.
7428 * @param proxiedUid The uid of the proxied application
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007429 * @param proxiedAttributionTag The proxied {@link Context#createAttributionContext
7430 * attribution tag} or {@code null} for default attribution
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007431 * @param message A message describing the reason the op was noted
7432 *
7433 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or {@link #MODE_IGNORED}
7434 * if it is not allowed and should be silently ignored (without causing the app to crash).
7435 *
7436 * @throws SecurityException If the proxy or proxied app has been configured to crash on this
7437 * op.
7438 */
7439 public int noteProxyOp(@NonNull String op, @Nullable String proxiedPackageName, int proxiedUid,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007440 @Nullable String proxiedAttributionTag, @Nullable String message) {
7441 return noteProxyOp(strOpToOp(op), proxiedPackageName, proxiedUid, proxiedAttributionTag,
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007442 message);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007443 }
7444
7445 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007446 * @deprecated Use {@link #noteProxyOpNoThrow(String, String, int, String, String)} instead
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007447 */
7448 @Deprecated
7449 public int noteProxyOpNoThrow(@NonNull String op, @NonNull String proxiedPackageName) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007450 return noteProxyOpNoThrow(op, proxiedPackageName, Binder.getCallingUid(), null, null);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007451 }
7452
7453 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007454 * @deprecated Use {@link #noteProxyOpNoThrow(String, String, int, String, String)} instead
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007455 */
7456 @Deprecated
7457 public int noteProxyOpNoThrow(@NonNull String op, @Nullable String proxiedPackageName,
7458 int proxiedUid) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007459 return noteProxyOpNoThrow(op, proxiedPackageName, proxiedUid, null, null);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007460 }
7461
7462 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007463 * Like {@link #noteProxyOp(String, String, int, String, String)} but instead
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007464 * of throwing a {@link SecurityException} it returns {@link #MODE_ERRORED}.
Philip P. Moltmann0d05e482019-02-08 13:05:25 -08007465 *
7466 * <p>This API requires package with the {@code proxiedPackageName} to belong to
7467 * {@code proxiedUid}.
7468 *
7469 * @param op The op to note
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007470 * @param proxiedPackageName The package to note the op for
7471 * @param proxiedUid The uid the package belongs to
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007472 * @param proxiedAttributionTag The proxied {@link Context#createAttributionContext
7473 * attribution tag} or {@code null} for default attribution
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007474 * @param message A message describing the reason the op was noted
7475 */
7476 public int noteProxyOpNoThrow(@NonNull String op, @Nullable String proxiedPackageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007477 int proxiedUid, @Nullable String proxiedAttributionTag, @Nullable String message) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007478 return noteProxyOpNoThrow(strOpToOp(op), proxiedPackageName, proxiedUid,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007479 proxiedAttributionTag, message);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007480 }
7481
7482 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007483 * Like {@link #noteProxyOp(int, String, int, String, String)} but instead
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007484 * of throwing a {@link SecurityException} it returns {@link #MODE_ERRORED}.
7485 *
7486 * @param op The op to note
Philip P. Moltmann0d05e482019-02-08 13:05:25 -08007487 * @param proxiedPackageName The package to note the op for or {@code null} if the op should be
7488 * noted for the "android" package
7489 * @param proxiedUid The uid the package belongs to
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007490 * @param proxiedAttributionTag The proxied {@link Context#createAttributionContext
7491 * attribution tag} or {@code null} for default attribution
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007492 * @param message A message describing the reason the op was noted
7493 *
7494 * @hide
Philip P. Moltmann0d05e482019-02-08 13:05:25 -08007495 */
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007496 public int noteProxyOpNoThrow(int op, @Nullable String proxiedPackageName, int proxiedUid,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007497 @Nullable String proxiedAttributionTag, @Nullable String message) {
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007498 int myUid = Process.myUid();
Philip P. Moltmann0d05e482019-02-08 13:05:25 -08007499
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007500 try {
David Cheung2ead9662020-02-19 16:11:06 -08007501 collectNoteOpCallsForValidation(op);
Philip P. Moltmannda554e42019-12-20 11:21:02 -08007502 int collectionMode = getNotedOpCollectionMode(proxiedUid, proxiedPackageName, op);
7503 if (collectionMode == COLLECT_ASYNC) {
7504 if (message == null) {
7505 // Set stack trace as default message
7506 message = getFormattedStackTrace();
7507 }
7508 }
7509
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007510 int mode = mService.noteProxyOperation(op, proxiedUid, proxiedPackageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007511 proxiedAttributionTag, myUid, mContext.getOpPackageName(),
7512 mContext.getAttributionTag(), collectionMode == COLLECT_ASYNC, message);
Philip P. Moltmannda554e42019-12-20 11:21:02 -08007513
7514 if (mode == MODE_ALLOWED) {
7515 if (collectionMode == COLLECT_SELF) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007516 collectNotedOpForSelf(op, proxiedAttributionTag);
Philip P. Moltmannda554e42019-12-20 11:21:02 -08007517 } else if (collectionMode == COLLECT_SYNC
7518 // Only collect app-ops when the proxy is trusted
7519 && mContext.checkPermission(Manifest.permission.UPDATE_APP_OPS_STATS, -1,
7520 myUid) == PackageManager.PERMISSION_GRANTED) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007521 collectNotedOpSync(op, proxiedAttributionTag);
Philip P. Moltmannda554e42019-12-20 11:21:02 -08007522 }
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007523 }
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07007524
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007525 return mode;
7526 } catch (RemoteException e) {
7527 throw e.rethrowFromSystemServer();
7528 }
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07007529 }
7530
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07007531 /**
7532 * Do a quick check for whether an application might be able to perform an operation.
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007533 * This is <em>not</em> a security check; you must use {@link #noteOp(String, int, String,
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007534 * String, String)} or {@link #startOp(int, int, String, boolean, String, String)} for your
7535 * actual security checks, which also ensure that the given uid and package name are consistent.
7536 * This function can just be used for a quick check to see if an operation has been disabled for
7537 * the application, as an early reject of some work. This does not modify the time stamp or
7538 * other data about the operation.
Dianne Hackbornc216a262018-04-26 13:46:22 -07007539 *
7540 * <p>Important things this will not do (which you need to ultimate use
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007541 * {@link #noteOp(String, int, String, String, String)} or
7542 * {@link #startOp(int, int, String, boolean, String, String)} to cover):</p>
Dianne Hackbornc216a262018-04-26 13:46:22 -07007543 * <ul>
7544 * <li>Verifying the uid and package are consistent, so callers can't spoof
7545 * their identity.</li>
7546 * <li>Taking into account the current foreground/background state of the
7547 * app; apps whose mode varies by this state will always be reported
7548 * as {@link #MODE_ALLOWED}.</li>
7549 * </ul>
7550 *
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07007551 * @param op The operation to check. One of the OP_* constants.
7552 * @param uid The user id of the application attempting to perform the operation.
7553 * @param packageName The name of the application attempting to perform the operation.
7554 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
7555 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
7556 * causing the app to crash).
7557 * @throws SecurityException If the app has been configured to crash on this op.
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07007558 * @hide
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07007559 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007560 @UnsupportedAppUsage
Dianne Hackborn35654b62013-01-14 17:38:02 -08007561 public int checkOp(int op, int uid, String packageName) {
7562 try {
7563 int mode = mService.checkOperation(op, uid, packageName);
7564 if (mode == MODE_ERRORED) {
Dianne Hackborn95d78532013-09-11 09:51:14 -07007565 throw new SecurityException(buildSecurityExceptionMsg(op, uid, packageName));
Dianne Hackborn35654b62013-01-14 17:38:02 -08007566 }
7567 return mode;
7568 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07007569 throw e.rethrowFromSystemServer();
Dianne Hackborn35654b62013-01-14 17:38:02 -08007570 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08007571 }
7572
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07007573 /**
7574 * Like {@link #checkOp} but instead of throwing a {@link SecurityException} it
7575 * returns {@link #MODE_ERRORED}.
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07007576 * @hide
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07007577 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01007578 @UnsupportedAppUsage
Dianne Hackborn35654b62013-01-14 17:38:02 -08007579 public int checkOpNoThrow(int op, int uid, String packageName) {
7580 try {
Dianne Hackborn65a4f252018-05-08 17:30:48 -07007581 int mode = mService.checkOperation(op, uid, packageName);
7582 return mode == AppOpsManager.MODE_FOREGROUND ? AppOpsManager.MODE_ALLOWED : mode;
Dianne Hackborn35654b62013-01-14 17:38:02 -08007583 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07007584 throw e.rethrowFromSystemServer();
Dianne Hackborn35654b62013-01-14 17:38:02 -08007585 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08007586 }
7587
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07007588 /**
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -08007589 * @deprecated Use {@link PackageManager#getPackageUid} instead
Jeff Sharkey911d7f42013-09-05 18:11:45 -07007590 */
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -08007591 @Deprecated
Dianne Hackborn62878492019-03-11 15:57:07 -07007592 public void checkPackage(int uid, @NonNull String packageName) {
Jeff Sharkey911d7f42013-09-05 18:11:45 -07007593 try {
7594 if (mService.checkPackage(uid, packageName) != MODE_ALLOWED) {
7595 throw new SecurityException(
7596 "Package " + packageName + " does not belong to " + uid);
7597 }
7598 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07007599 throw e.rethrowFromSystemServer();
Jeff Sharkey911d7f42013-09-05 18:11:45 -07007600 }
7601 }
7602
7603 /**
John Spurlock1af30c72014-03-10 08:33:35 -04007604 * Like {@link #checkOp} but at a stream-level for audio operations.
7605 * @hide
7606 */
7607 public int checkAudioOp(int op, int stream, int uid, String packageName) {
7608 try {
7609 final int mode = mService.checkAudioOperation(op, stream, uid, packageName);
7610 if (mode == MODE_ERRORED) {
7611 throw new SecurityException(buildSecurityExceptionMsg(op, uid, packageName));
7612 }
7613 return mode;
7614 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07007615 throw e.rethrowFromSystemServer();
John Spurlock1af30c72014-03-10 08:33:35 -04007616 }
John Spurlock1af30c72014-03-10 08:33:35 -04007617 }
7618
7619 /**
7620 * Like {@link #checkAudioOp} but instead of throwing a {@link SecurityException} it
7621 * returns {@link #MODE_ERRORED}.
7622 * @hide
7623 */
7624 public int checkAudioOpNoThrow(int op, int stream, int uid, String packageName) {
7625 try {
7626 return mService.checkAudioOperation(op, stream, uid, packageName);
7627 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07007628 throw e.rethrowFromSystemServer();
John Spurlock1af30c72014-03-10 08:33:35 -04007629 }
John Spurlock1af30c72014-03-10 08:33:35 -04007630 }
7631
Philip P. Moltmann6c6403e2019-12-09 10:08:29 -08007632 /**
7633 * @deprecated Use own local {@link android.os.Binder#Binder()}
7634 *
7635 * @hide
7636 */
7637 @Deprecated
7638 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.Q, publicAlternatives = "Create own "
7639 + "local {@link android.os.Binder}")
Dianne Hackborne98f5db2013-07-17 17:23:25 -07007640 public static IBinder getToken(IAppOpsService service) {
Philip P. Moltmann6c6403e2019-12-09 10:08:29 -08007641 return getClientId();
Dianne Hackborne98f5db2013-07-17 17:23:25 -07007642 }
7643
Philip P. Moltmann6c6403e2019-12-09 10:08:29 -08007644 /** @hide */
7645 public static IBinder getClientId() {
7646 synchronized (AppOpsManager.class) {
7647 if (sClientId == null) {
7648 sClientId = new Binder();
7649 }
7650
7651 return sClientId;
7652 }
7653 }
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007654
Stanislav Zholnin90516b92020-01-20 14:03:06 +00007655 /** @hide */
7656 private static IAppOpsService getService() {
7657 synchronized (sLock) {
7658 if (sService == null) {
7659 sService = IAppOpsService.Stub.asInterface(
7660 ServiceManager.getService(Context.APP_OPS_SERVICE));
7661 }
7662 return sService;
7663 }
7664 }
7665
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007666 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007667 * @deprecated use {@link #startOp(String, int, String, String, String)} instead
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007668 */
7669 @Deprecated
7670 public int startOp(@NonNull String op, int uid, @NonNull String packageName) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007671 return startOp(op, uid, packageName, null, null);
Svet Ganovf7b47252018-02-26 11:11:27 -08007672 }
7673
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07007674 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007675 * @deprecated Use {@link #startOp(int, int, String, boolean, String, String)} instead
Svet Ganovf7b47252018-02-26 11:11:27 -08007676 *
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07007677 * @hide
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07007678 */
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007679 @Deprecated
7680 public int startOp(int op) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007681 return startOp(op, Process.myUid(), mContext.getOpPackageName(), false, null, null);
Svet Ganovf7b47252018-02-26 11:11:27 -08007682 }
7683
7684 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007685 * @deprecated Use {@link #startOp(int, int, String, boolean, String, String)} instead
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007686 *
7687 * @hide
7688 */
7689 @Deprecated
7690 public int startOp(int op, int uid, String packageName) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007691 return startOp(op, uid, packageName, false, null, null);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007692 }
7693
7694 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007695 * @deprecated Use {@link #startOp(int, int, String, boolean, String, String)} instead
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007696 *
7697 * @hide
7698 */
7699 @Deprecated
7700 public int startOp(int op, int uid, String packageName, boolean startIfModeDefault) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007701 return startOp(op, uid, packageName, startIfModeDefault, null, null);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007702 }
7703
7704 /**
7705 * Report that an application has started executing a long-running operation.
7706 *
7707 * @param op The operation to start. One of the OPSTR_* constants.
7708 * @param uid The user id of the application attempting to perform the operation.
7709 * @param packageName The name of the application attempting to perform the operation.
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007710 * @param attributionTag The {@link Context#createAttributionContext attribution tag} or
7711 * {@code null} for default attribution
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007712 * @param message Description why op was started
7713 *
7714 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
7715 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
7716 * causing the app to crash).
7717 *
7718 * @throws SecurityException If the app has been configured to crash on this op or
7719 * the package is not in the passed in UID.
7720 */
7721 public int startOp(@NonNull String op, int uid, @Nullable String packageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007722 @Nullable String attributionTag, @Nullable String message) {
7723 return startOp(strOpToOp(op), uid, packageName, false, attributionTag, message);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007724 }
7725
7726 /**
7727 * Report that an application has started executing a long-running operation.
Svet Ganovf7b47252018-02-26 11:11:27 -08007728 *
7729 * @param op The operation to start. One of the OP_* constants.
7730 * @param uid The user id of the application attempting to perform the operation.
7731 * @param packageName The name of the application attempting to perform the operation.
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007732 * @param attributionTag The {@link Context#createAttributionContext attribution tag} or
7733 * {@code null} for default attribution
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007734 * @param startIfModeDefault Whether to start if mode is {@link #MODE_DEFAULT}.
7735 * @param message Description why op was started
7736 *
Svet Ganovf7b47252018-02-26 11:11:27 -08007737 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
7738 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
7739 * causing the app to crash).
Svet Ganovf7b47252018-02-26 11:11:27 -08007740 *
7741 * @throws SecurityException If the app has been configured to crash on this op or
7742 * the package is not in the passed in UID.
7743 *
7744 * @hide
7745 */
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007746 public int startOp(int op, int uid, @Nullable String packageName, boolean startIfModeDefault,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007747 @Nullable String attributionTag, @Nullable String message) {
7748 final int mode = startOpNoThrow(op, uid, packageName, startIfModeDefault, attributionTag,
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007749 message);
Svet Ganovf7b47252018-02-26 11:11:27 -08007750 if (mode == MODE_ERRORED) {
7751 throw new SecurityException(buildSecurityExceptionMsg(op, uid, packageName));
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007752 }
Svet Ganovf7b47252018-02-26 11:11:27 -08007753 return mode;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007754 }
7755
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07007756 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007757 * @deprecated use {@link #startOpNoThrow(String, int, String, String, String)} instead
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07007758 */
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007759 @Deprecated
7760 public int startOpNoThrow(@NonNull String op, int uid, @NonNull String packageName) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007761 return startOpNoThrow(op, uid, packageName, null, null);
Svet Ganovf7b47252018-02-26 11:11:27 -08007762 }
7763
7764 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007765 * @deprecated Use {@link #startOpNoThrow(int, int, String, boolean, String, String} instead
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007766 *
7767 * @hide
7768 */
7769 @Deprecated
7770 public int startOpNoThrow(int op, int uid, String packageName) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007771 return startOpNoThrow(op, uid, packageName, false, null, null);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007772 }
7773
7774 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007775 * @deprecated Use {@link #startOpNoThrow(int, int, String, boolean, String, String} instead
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007776 *
7777 * @hide
7778 */
7779 @Deprecated
7780 public int startOpNoThrow(int op, int uid, String packageName, boolean startIfModeDefault) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007781 return startOpNoThrow(op, uid, packageName, startIfModeDefault, null, null);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007782 }
7783
7784 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007785 * Like {@link #startOp(String, int, String, String, String)} but instead of throwing a
Svet Ganovf7b47252018-02-26 11:11:27 -08007786 * {@link SecurityException} it returns {@link #MODE_ERRORED}.
7787 *
7788 * @param op The operation to start. One of the OP_* constants.
7789 * @param uid The user id of the application attempting to perform the operation.
7790 * @param packageName The name of the application attempting to perform the operation.
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007791 * @param attributionTag The {@link Context#createAttributionContext attribution tag} or
7792 * {@code null} for default attribution
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007793 * @param message Description why op was started
7794 *
Svet Ganovf7b47252018-02-26 11:11:27 -08007795 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
7796 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
7797 * causing the app to crash).
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007798 */
7799 public int startOpNoThrow(@NonNull String op, int uid, @NonNull String packageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007800 @NonNull String attributionTag, @Nullable String message) {
7801 return startOpNoThrow(strOpToOp(op), uid, packageName, false, attributionTag, message);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007802 }
7803
7804 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007805 * Like {@link #startOp(int, int, String, boolean, String, String)} but instead of throwing a
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007806 * {@link SecurityException} it returns {@link #MODE_ERRORED}.
7807 *
7808 * @param op The operation to start. One of the OP_* constants.
7809 * @param uid The user id of the application attempting to perform the operation.
7810 * @param packageName The name of the application attempting to perform the operation.
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007811 * @param attributionTag The {@link Context#createAttributionContext attribution tag} or
7812 * {@code null} for default attribution
Svet Ganovf7b47252018-02-26 11:11:27 -08007813 * @param startIfModeDefault Whether to start if mode is {@link #MODE_DEFAULT}.
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007814 * @param message Description why op was started
7815 *
7816 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
7817 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
7818 * causing the app to crash).
Svet Ganovf7b47252018-02-26 11:11:27 -08007819 *
7820 * @hide
7821 */
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007822 public int startOpNoThrow(int op, int uid, @NonNull String packageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007823 boolean startIfModeDefault, @Nullable String attributionTag, @Nullable String message) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007824 try {
David Cheung2ead9662020-02-19 16:11:06 -08007825 collectNoteOpCallsForValidation(op);
Philip P. Moltmannda554e42019-12-20 11:21:02 -08007826 int collectionMode = getNotedOpCollectionMode(uid, packageName, op);
7827 if (collectionMode == COLLECT_ASYNC) {
7828 if (message == null) {
7829 // Set stack trace as default message
7830 message = getFormattedStackTrace();
7831 }
7832 }
7833
Philip P. Moltmann6c6403e2019-12-09 10:08:29 -08007834 int mode = mService.startOperation(getClientId(), op, uid, packageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007835 attributionTag, startIfModeDefault, collectionMode == COLLECT_ASYNC, message);
Philip P. Moltmannda554e42019-12-20 11:21:02 -08007836
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007837 if (mode == MODE_ALLOWED) {
Philip P. Moltmannda554e42019-12-20 11:21:02 -08007838 if (collectionMode == COLLECT_SELF) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007839 collectNotedOpForSelf(op, attributionTag);
Philip P. Moltmannda554e42019-12-20 11:21:02 -08007840 } else if (collectionMode == COLLECT_SYNC) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007841 collectNotedOpSync(op, attributionTag);
Philip P. Moltmannda554e42019-12-20 11:21:02 -08007842 }
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007843 }
7844
7845 return mode;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007846 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07007847 throw e.rethrowFromSystemServer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007848 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007849 }
7850
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07007851 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007852 * @deprecated Use {@link #finishOp(String, int, String, String)} instead
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007853 *
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07007854 * @hide
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07007855 */
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007856 @Deprecated
7857 public void finishOp(int op) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007858 finishOp(op, Process.myUid(), mContext.getOpPackageName(), null);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007859 }
7860
7861 /**
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007862 * @deprecated Use {@link #finishOp(String, int, String, String)} instead
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007863 */
7864 public void finishOp(@NonNull String op, int uid, @NonNull String packageName) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007865 finishOp(strOpToOp(op), uid, packageName, null);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007866 }
7867
7868 /**
7869 * Report that an application is no longer performing an operation that had previously
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007870 * been started with {@link #startOp(String, int, String, String, String)}. There is no
7871 * validation of input or result; the parameters supplied here must be the exact same ones
7872 * previously passed in when starting the operation.
7873 */
7874 public void finishOp(@NonNull String op, int uid, @NonNull String packageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007875 @Nullable String attributionTag) {
7876 finishOp(strOpToOp(op), uid, packageName, attributionTag);
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007877 }
7878
7879 /**
7880 * @deprecated Use {@link #finishOp(int, int, String, String)} instead
7881 *
7882 * @hide
7883 */
7884 public void finishOp(int op, int uid, @NonNull String packageName) {
7885 finishOp(op, uid, packageName, null);
7886 }
7887
7888 /**
7889 * Report that an application is no longer performing an operation that had previously
7890 * been started with {@link #startOp(int, int, String, boolean, String, String)}. There is no
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007891 * validation of input or result; the parameters supplied here must be the exact same ones
7892 * previously passed in when starting the operation.
7893 *
7894 * @hide
7895 */
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007896 public void finishOp(int op, int uid, @NonNull String packageName,
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007897 @Nullable String attributionTag) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007898 try {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08007899 mService.finishOperation(getClientId(), op, uid, packageName, attributionTag);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007900 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07007901 throw e.rethrowFromSystemServer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007902 }
7903 }
7904
Svet Ganovf7b47252018-02-26 11:11:27 -08007905 /**
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -08007906 * Checks whether the given op for a package is active, i.e. did someone call {@link #startOp}
7907 * without {@link #finishOp} yet.
Jeff Sharkey7095ab92019-08-20 16:50:28 -06007908 * <p>
7909 * If you don't hold the {@code android.Manifest.permission#WATCH_APPOPS}
7910 * permission you can query only for your UID.
7911 *
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007912 * @see #finishOp(String, int, String, String)
7913 * @see #startOp(String, int, String, String, String)
Jeff Sharkey7095ab92019-08-20 16:50:28 -06007914 */
7915 public boolean isOpActive(@NonNull String op, int uid, @NonNull String packageName) {
7916 return isOperationActive(strOpToOp(op), uid, packageName);
7917 }
7918
7919 /**
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007920 * Start collection of noted appops on this thread.
7921 *
7922 * <p>Called at the beginning of a two way binder transaction.
7923 *
7924 * @see #finishNotedAppOpsCollection()
7925 *
7926 * @hide
7927 */
7928 public static void startNotedAppOpsCollection(int callingUid) {
7929 sBinderThreadCallingUid.set(callingUid);
7930 }
7931
7932 /**
7933 * State of a temporarily paused noted app-ops collection.
7934 *
7935 * @see #pauseNotedAppOpsCollection()
7936 *
7937 * @hide
7938 */
7939 public static class PausedNotedAppOpsCollection {
7940 final int mUid;
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007941 final @Nullable ArrayMap<String, long[]> mCollectedNotedAppOps;
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007942
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007943 PausedNotedAppOpsCollection(int uid, @Nullable ArrayMap<String,
7944 long[]> collectedNotedAppOps) {
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007945 mUid = uid;
7946 mCollectedNotedAppOps = collectedNotedAppOps;
7947 }
7948 }
7949
7950 /**
7951 * Temporarily suspend collection of noted app-ops when binder-thread calls into the other
7952 * process. During such a call there might be call-backs coming back on the same thread which
7953 * should not be accounted to the current collection.
7954 *
7955 * @return a state needed to resume the collection
7956 *
7957 * @hide
7958 */
7959 public static @Nullable PausedNotedAppOpsCollection pauseNotedAppOpsCollection() {
7960 Integer previousUid = sBinderThreadCallingUid.get();
7961 if (previousUid != null) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07007962 ArrayMap<String, long[]> previousCollectedNotedAppOps =
7963 sAppOpsNotedInThisBinderTransaction.get();
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07007964
7965 sBinderThreadCallingUid.remove();
7966 sAppOpsNotedInThisBinderTransaction.remove();
7967
7968 return new PausedNotedAppOpsCollection(previousUid, previousCollectedNotedAppOps);
7969 }
7970
7971 return null;
7972 }
7973
7974 /**
7975 * Resume a collection paused via {@link #pauseNotedAppOpsCollection}.
7976 *
7977 * @param prevCollection The state of the previous collection
7978 *
7979 * @hide
7980 */
7981 public static void resumeNotedAppOpsCollection(
7982 @Nullable PausedNotedAppOpsCollection prevCollection) {
7983 if (prevCollection != null) {
7984 sBinderThreadCallingUid.set(prevCollection.mUid);
7985
7986 if (prevCollection.mCollectedNotedAppOps != null) {
7987 sAppOpsNotedInThisBinderTransaction.set(prevCollection.mCollectedNotedAppOps);
7988 }
7989 }
7990 }
7991
7992 /**
7993 * Finish collection of noted appops on this thread.
7994 *
7995 * <p>Called at the end of a two way binder transaction.
7996 *
7997 * @see #startNotedAppOpsCollection(int)
7998 *
7999 * @hide
8000 */
8001 public static void finishNotedAppOpsCollection() {
8002 sBinderThreadCallingUid.remove();
8003 sAppOpsNotedInThisBinderTransaction.remove();
8004 }
8005
8006 /**
Philip P. Moltmannda554e42019-12-20 11:21:02 -08008007 * Collect a noted op for the current process.
8008 *
8009 * @param op The noted op
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08008010 * @param attributionTag The attribution tag the op is noted for
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008011 */
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08008012 private void collectNotedOpForSelf(int op, @Nullable String attributionTag) {
Philip P. Moltmannda554e42019-12-20 11:21:02 -08008013 synchronized (sLock) {
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008014 if (sOnOpNotedCallback != null) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08008015 sOnOpNotedCallback.onSelfNoted(new SyncNotedAppOp(op, attributionTag));
Philip P. Moltmannda554e42019-12-20 11:21:02 -08008016 }
8017 }
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08008018 sMessageCollector.onSelfNoted(new SyncNotedAppOp(op, attributionTag));
Philip P. Moltmannda554e42019-12-20 11:21:02 -08008019 }
8020
8021 /**
8022 * Collect a noted op when inside of a two-way binder call.
8023 *
8024 * <p> Delivered to caller via {@link #prefixParcelWithAppOpsIfNeeded}
8025 *
8026 * @param op The noted op
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08008027 * @param attributionTag The attribution tag the op is noted for
Philip P. Moltmannda554e42019-12-20 11:21:02 -08008028 */
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08008029 private void collectNotedOpSync(int op, @Nullable String attributionTag) {
Philip P. Moltmannda554e42019-12-20 11:21:02 -08008030 // If this is inside of a two-way binder call:
8031 // We are inside of a two-way binder call. Delivered to caller via
8032 // {@link #prefixParcelWithAppOpsIfNeeded}
8033 ArrayMap<String, long[]> appOpsNoted = sAppOpsNotedInThisBinderTransaction.get();
8034 if (appOpsNoted == null) {
8035 appOpsNoted = new ArrayMap<>(1);
8036 sAppOpsNotedInThisBinderTransaction.set(appOpsNoted);
8037 }
8038
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08008039 long[] appOpsNotedForAttribution = appOpsNoted.get(attributionTag);
8040 if (appOpsNotedForAttribution == null) {
8041 appOpsNotedForAttribution = new long[2];
8042 appOpsNoted.put(attributionTag, appOpsNotedForAttribution);
Philip P. Moltmannda554e42019-12-20 11:21:02 -08008043 }
8044
8045 if (op < 64) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08008046 appOpsNotedForAttribution[0] |= 1L << op;
Philip P. Moltmannda554e42019-12-20 11:21:02 -08008047 } else {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08008048 appOpsNotedForAttribution[1] |= 1L << (op - 64);
Philip P. Moltmannda554e42019-12-20 11:21:02 -08008049 }
8050 }
8051
8052 /** @hide */
8053 @Retention(RetentionPolicy.SOURCE)
8054 @IntDef(value = {
8055 DONT_COLLECT,
8056 COLLECT_SELF,
8057 COLLECT_SYNC,
8058 COLLECT_ASYNC
8059 })
8060 private @interface NotedOpCollectionMode {}
8061 private static final int DONT_COLLECT = 0;
8062 private static final int COLLECT_SELF = 1;
8063 private static final int COLLECT_SYNC = 2;
8064 private static final int COLLECT_ASYNC = 3;
8065
8066 /**
8067 * Mark an app-op as noted.
8068 */
8069 private @NotedOpCollectionMode int getNotedOpCollectionMode(int uid,
8070 @Nullable String packageName, int op) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07008071 if (packageName == null) {
8072 packageName = "android";
8073 }
8074
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008075 // check it the appops needs to be collected and cache result
Philip P. Moltmannda554e42019-12-20 11:21:02 -08008076 if (sAppOpsToNote[op] == SHOULD_COLLECT_NOTE_OP_NOT_INITIALIZED) {
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008077 boolean shouldCollectNotes;
8078 try {
Philip P. Moltmannda554e42019-12-20 11:21:02 -08008079 shouldCollectNotes = mService.shouldCollectNotes(op);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008080 } catch (RemoteException e) {
Philip P. Moltmannda554e42019-12-20 11:21:02 -08008081 return DONT_COLLECT;
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008082 }
8083
8084 if (shouldCollectNotes) {
Philip P. Moltmannda554e42019-12-20 11:21:02 -08008085 sAppOpsToNote[op] = SHOULD_COLLECT_NOTE_OP;
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008086 } else {
Philip P. Moltmannda554e42019-12-20 11:21:02 -08008087 sAppOpsToNote[op] = SHOULD_NOT_COLLECT_NOTE_OP;
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008088 }
8089 }
8090
Philip P. Moltmannda554e42019-12-20 11:21:02 -08008091 if (sAppOpsToNote[op] != SHOULD_COLLECT_NOTE_OP) {
8092 return DONT_COLLECT;
8093 }
8094
8095 synchronized (sLock) {
8096 if (uid == Process.myUid()
8097 && packageName.equals(ActivityThread.currentOpPackageName())) {
8098 return COLLECT_SELF;
8099 }
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008100 }
8101
8102 Integer binderUid = sBinderThreadCallingUid.get();
8103
Philip P. Moltmann9bcc4062019-09-06 12:27:59 -07008104 if (binderUid != null && binderUid == uid) {
Philip P. Moltmannda554e42019-12-20 11:21:02 -08008105 return COLLECT_SYNC;
Philip P. Moltmann9bcc4062019-09-06 12:27:59 -07008106 } else {
Philip P. Moltmannda554e42019-12-20 11:21:02 -08008107 return COLLECT_ASYNC;
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008108 }
8109 }
8110
8111 /**
8112 * Append app-ops noted in the current two-way binder transaction to parcel.
8113 *
8114 * <p>This is called on the callee side of a two way binder transaction just before the
8115 * transaction returns.
8116 *
8117 * @param p the parcel to append the noted app-ops to
8118 *
8119 * @hide
8120 */
8121 public static void prefixParcelWithAppOpsIfNeeded(@NonNull Parcel p) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07008122 ArrayMap<String, long[]> notedAppOps = sAppOpsNotedInThisBinderTransaction.get();
8123 if (notedAppOps == null) {
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008124 return;
8125 }
8126
8127 p.writeInt(Parcel.EX_HAS_NOTED_APPOPS_REPLY_HEADER);
Philip P. Moltmann59076d82019-08-19 15:00:40 -07008128
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08008129 int numAttributionWithNotesAppOps = notedAppOps.size();
8130 p.writeInt(numAttributionWithNotesAppOps);
Philip P. Moltmann59076d82019-08-19 15:00:40 -07008131
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08008132 for (int i = 0; i < numAttributionWithNotesAppOps; i++) {
Philip P. Moltmann59076d82019-08-19 15:00:40 -07008133 p.writeString(notedAppOps.keyAt(i));
8134 p.writeLong(notedAppOps.valueAt(i)[0]);
8135 p.writeLong(notedAppOps.valueAt(i)[1]);
8136 }
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008137 }
8138
8139 /**
8140 * Read app-ops noted during a two-way binder transaction from parcel.
8141 *
8142 * <p>This is called on the calling side of a two way binder transaction just after the
8143 * transaction returns.
8144 *
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008145 * @param p The parcel to read from
8146 *
8147 * @hide
8148 */
8149 public static void readAndLogNotedAppops(@NonNull Parcel p) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08008150 int numAttributionsWithNotedAppOps = p.readInt();
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008151
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08008152 for (int i = 0; i < numAttributionsWithNotedAppOps; i++) {
8153 String attributionTag = p.readString();
Philip P. Moltmann59076d82019-08-19 15:00:40 -07008154 long[] rawNotedAppOps = new long[2];
8155 rawNotedAppOps[0] = p.readLong();
8156 rawNotedAppOps[1] = p.readLong();
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008157
Philip P. Moltmann59076d82019-08-19 15:00:40 -07008158 if (rawNotedAppOps[0] != 0 || rawNotedAppOps[1] != 0) {
8159 BitSet notedAppOps = BitSet.valueOf(rawNotedAppOps);
8160
8161 synchronized (sLock) {
8162 for (int code = notedAppOps.nextSetBit(0); code != -1;
8163 code = notedAppOps.nextSetBit(code + 1)) {
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008164 if (sOnOpNotedCallback != null) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08008165 sOnOpNotedCallback.onNoted(new SyncNotedAppOp(code, attributionTag));
Philip P. Moltmann59076d82019-08-19 15:00:40 -07008166 }
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008167 }
8168 }
Stanislav Zholnin90516b92020-01-20 14:03:06 +00008169 for (int code = notedAppOps.nextSetBit(0); code != -1;
8170 code = notedAppOps.nextSetBit(code + 1)) {
Philip P. Moltmann12ac3f42020-03-05 15:01:29 -08008171 sMessageCollector.onNoted(new SyncNotedAppOp(code, attributionTag));
Stanislav Zholnin90516b92020-01-20 14:03:06 +00008172 }
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008173 }
8174 }
8175 }
8176
8177 /**
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008178 * Set a new {@link OnOpNotedCallback}.
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008179 *
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008180 * <p>There can only ever be one collector per process. If there currently is another callback
8181 * set, this will fail.
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008182 *
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008183 * @param asyncExecutor executor to execute {@link OnOpNotedCallback#onAsyncNoted} on, {@code
8184 * null} to unset
8185 * @param callback listener to set, {@code null} to unset
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008186 *
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008187 * @throws IllegalStateException If another callback is already registered
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008188 */
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008189 public void setOnOpNotedCallback(@Nullable @CallbackExecutor Executor asyncExecutor,
8190 @Nullable OnOpNotedCallback callback) {
8191 Preconditions.checkState((callback == null) == (asyncExecutor == null));
8192
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008193 synchronized (sLock) {
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008194 if (callback == null) {
8195 Preconditions.checkState(sOnOpNotedCallback != null,
8196 "No callback is currently registered");
8197
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008198 try {
8199 mService.stopWatchingAsyncNoted(mContext.getPackageName(),
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008200 sOnOpNotedCallback.mAsyncCb);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008201 } catch (RemoteException e) {
8202 e.rethrowFromSystemServer();
8203 }
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008204
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008205 sOnOpNotedCallback = null;
8206 } else {
8207 Preconditions.checkState(sOnOpNotedCallback == null,
8208 "Another callback is already registered");
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008209
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008210 callback.mAsyncExecutor = asyncExecutor;
8211 sOnOpNotedCallback = callback;
8212
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008213 List<AsyncNotedAppOp> missedAsyncOps = null;
8214 try {
8215 mService.startWatchingAsyncNoted(mContext.getPackageName(),
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008216 sOnOpNotedCallback.mAsyncCb);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008217 missedAsyncOps = mService.extractAsyncOps(mContext.getPackageName());
8218 } catch (RemoteException e) {
8219 e.rethrowFromSystemServer();
8220 }
8221
8222 if (missedAsyncOps != null) {
8223 int numMissedAsyncOps = missedAsyncOps.size();
8224 for (int i = 0; i < numMissedAsyncOps; i++) {
8225 final AsyncNotedAppOp asyncNotedAppOp = missedAsyncOps.get(i);
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008226 if (sOnOpNotedCallback != null) {
8227 sOnOpNotedCallback.getAsyncNotedExecutor().execute(
8228 () -> sOnOpNotedCallback.onAsyncNoted(asyncNotedAppOp));
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008229 }
8230 }
8231 }
8232 }
8233 }
8234 }
8235
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008236 // TODO moltmann: Remove
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008237 /**
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008238 * Will be removed before R ships, leave it just to not break apps immediately.
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008239 *
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008240 * @removed
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008241 *
8242 * @hide
8243 */
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008244 @SystemApi
8245 @Deprecated
8246 public void setNotedAppOpsCollector(@Nullable AppOpsCollector collector) {
8247 synchronized (sLock) {
8248 if (collector != null) {
8249 if (isListeningForOpNoted()) {
8250 setOnOpNotedCallback(null, null);
8251 }
8252 setOnOpNotedCallback(new HandlerExecutor(Handler.getMain()), collector);
8253 } else if (sOnOpNotedCallback != null) {
8254 setOnOpNotedCallback(null, null);
8255 }
8256 }
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008257 }
8258
8259 /**
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008260 * @return {@code true} iff the process currently is currently collecting noted appops.
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008261 *
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008262 * @see #setOnOpNotedCallback
8263 *
8264 * @hide
8265 */
8266 public static boolean isListeningForOpNoted() {
Stanislav Zholnind4968a02020-04-18 21:01:13 +01008267 return sOnOpNotedCallback != null || isCollectingStackTraces();
8268 }
8269
8270 /**
8271 * @return {@code true} iff the process is currently sampled for stacktrace collection.
8272 *
8273 * @see #setOnOpNotedCallback
8274 *
8275 * @hide
8276 */
8277 private static boolean isCollectingStackTraces() {
8278 if (sConfig.getSampledOpCode() == OP_NONE &&
8279 sConfig.getExpirationTimeSinceBootMillis() >= SystemClock.elapsedRealtime()) {
8280 return false;
8281 }
8282 return true;
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008283 }
8284
8285 /**
8286 * Callback an app can {@link #setOnOpNotedCallback set} to monitor the app-ops the
8287 * system has tracked for it. I.e. each time any app calls {@link #noteOp} or {@link #startOp}
8288 * one of a method of this object is called.
8289 *
8290 * <p><b>There will be a call for all app-ops related to runtime permissions, but not
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -08008291 * necessarily for all other app-ops.
Philip P. Moltmann9e367002020-02-03 17:01:05 -08008292 *
8293 * <pre>
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008294 * setOnOpNotedCallback(getMainExecutor(), new OnOpNotedCallback() {
Philip P. Moltmann9e367002020-02-03 17:01:05 -08008295 * ArraySet<Pair<String, String>> opsNotedForThisProcess = new ArraySet<>();
8296 *
8297 * private synchronized void addAccess(String op, String accessLocation) {
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -08008298 * // Ops are often noted when runtime permission protected APIs were called.
Philip P. Moltmann9e367002020-02-03 17:01:05 -08008299 * // In this case permissionToOp() allows to resolve the permission<->op
8300 * opsNotedForThisProcess.add(new Pair(accessType, accessLocation));
8301 * }
8302 *
8303 * public void onNoted(SyncNotedAppOp op) {
8304 * // Accesses is currently happening, hence stack trace describes location of access
8305 * addAccess(op.getOp(), Arrays.toString(Thread.currentThread().getStackTrace()));
8306 * }
8307 *
8308 * public void onSelfNoted(SyncNotedAppOp op) {
8309 * onNoted(op);
8310 * }
8311 *
8312 * public void onAsyncNoted(AsyncNotedAppOp asyncOp) {
8313 * // Stack trace is not useful for async ops as accessed happened on different thread
8314 * addAccess(asyncOp.getOp(), asyncOp.getMessage());
8315 * }
8316 * });
8317 * </pre>
8318 *
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008319 * @see #setOnOpNotedCallback
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008320 */
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008321 public abstract static class OnOpNotedCallback {
8322 private @NonNull Executor mAsyncExecutor;
8323
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008324 /** Callback registered with the system. This will receive the async notes ops */
8325 private final IAppOpsAsyncNotedCallback mAsyncCb = new IAppOpsAsyncNotedCallback.Stub() {
8326 @Override
8327 public void opNoted(AsyncNotedAppOp op) {
Daulet Zhanguzin0af97d62019-12-30 15:41:28 +00008328 Objects.requireNonNull(op);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008329
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008330 long token = Binder.clearCallingIdentity();
8331 try {
8332 getAsyncNotedExecutor().execute(() -> onAsyncNoted(op));
8333 } finally {
8334 Binder.restoreCallingIdentity(token);
8335 }
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008336 }
8337 };
8338
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008339 // TODO moltmann: Remove
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008340 /**
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008341 * Will be removed before R ships.
8342 *
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008343 * @return The executor for the system to use when calling {@link #onAsyncNoted}.
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008344 *
8345 * @hide
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008346 */
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008347 protected @NonNull Executor getAsyncNotedExecutor() {
8348 return mAsyncExecutor;
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008349 }
8350
8351 /**
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -08008352 * Called when an app-op was {@link #noteOp noted} for this package inside of a synchronous
8353 * API call, i.e. a API call that returned data or waited until the action was performed.
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008354 *
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -08008355 * <p>Called on the calling thread before the API returns. This allows the app to e.g.
8356 * collect stack traces to figure out where the access came from.
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008357 *
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008358 * @param op op noted
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008359 */
8360 public abstract void onNoted(@NonNull SyncNotedAppOp op);
8361
8362 /**
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -08008363 * Called when this app noted an app-op for its own package,
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008364 *
Philip P. Moltmanna04b9ab2020-02-13 09:54:04 -08008365 * <p>This is very similar to {@link #onNoted} only that the tracking was not caused by the
8366 * API provider in a separate process, but by one in the app's own process.
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008367 *
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008368 * @param op op noted
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008369 */
8370 public abstract void onSelfNoted(@NonNull SyncNotedAppOp op);
8371
8372 /**
8373 * Called when an app-op was noted for this package which cannot be delivered via the other
8374 * two mechanisms.
8375 *
8376 * <p>Called as soon as possible after the app-op was noted, but the delivery delay is not
8377 * guaranteed. Due to how async calls work in Android this might even be delivered slightly
8378 * before the private data is delivered to the app.
8379 *
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008380 * <p>If the app is not running or no {@link OnOpNotedCallback} is registered a small amount
8381 * of noted app-ops are buffered and then delivered as soon as a listener is registered.
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008382 *
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008383 * @param asyncOp op noted
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008384 */
8385 public abstract void onAsyncNoted(@NonNull AsyncNotedAppOp asyncOp);
8386 }
8387
Philip P. Moltmann5892a8f2020-03-03 12:05:55 -08008388 // TODO moltmann: Remove
8389 /**
8390 * Will be removed before R ships, leave it just to not break apps immediately.
8391 *
8392 * @removed
8393 *
8394 * @hide
8395 */
8396 @SystemApi
8397 @Deprecated
8398 public abstract static class AppOpsCollector extends OnOpNotedCallback {
8399 public @NonNull Executor getAsyncNotedExecutor() {
8400 return new HandlerExecutor(Handler.getMain());
8401 }
8402 };
8403
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008404 /**
8405 * Generate a stack trace used for noted app-ops logging.
8406 *
8407 * <p>This strips away the first few and last few stack trace elements as they are not
8408 * interesting to apps.
8409 */
8410 private static String getFormattedStackTrace() {
8411 StackTraceElement[] trace = new Exception().getStackTrace();
8412
8413 int firstInteresting = 0;
8414 for (int i = 0; i < trace.length; i++) {
8415 if (trace[i].getClassName().startsWith(AppOpsManager.class.getName())
8416 || trace[i].getClassName().startsWith(Parcel.class.getName())
8417 || trace[i].getClassName().contains("$Stub$Proxy")
8418 || trace[i].getClassName().startsWith(DatabaseUtils.class.getName())
8419 || trace[i].getClassName().startsWith("android.content.ContentProviderProxy")
8420 || trace[i].getClassName().startsWith(ContentResolver.class.getName())) {
8421 firstInteresting = i;
8422 } else {
8423 break;
8424 }
8425 }
8426
8427 int lastInteresting = trace.length - 1;
8428 for (int i = trace.length - 1; i >= 0; i--) {
8429 if (trace[i].getClassName().startsWith(HandlerThread.class.getName())
8430 || trace[i].getClassName().startsWith(Handler.class.getName())
8431 || trace[i].getClassName().startsWith(Looper.class.getName())
8432 || trace[i].getClassName().startsWith(Binder.class.getName())
8433 || trace[i].getClassName().startsWith(RuntimeInit.class.getName())
8434 || trace[i].getClassName().startsWith(ZygoteInit.class.getName())
8435 || trace[i].getClassName().startsWith(ActivityThread.class.getName())
8436 || trace[i].getClassName().startsWith(Method.class.getName())
8437 || trace[i].getClassName().startsWith("com.android.server.SystemServer")) {
8438 lastInteresting = i;
8439 } else {
8440 break;
8441 }
8442 }
8443
8444 StringBuilder sb = new StringBuilder();
8445 for (int i = firstInteresting; i <= lastInteresting; i++) {
Stanislav Zholnin90516b92020-01-20 14:03:06 +00008446 if (i != firstInteresting) {
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008447 sb.append('\n');
8448 }
Stanislav Zholnin90516b92020-01-20 14:03:06 +00008449 if (sb.length() + trace[i].toString().length() > 600) {
8450 break;
8451 }
8452 sb.append(trace[i]);
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07008453 }
8454
8455 return sb.toString();
8456 }
8457
8458 /**
Svet Ganovf7b47252018-02-26 11:11:27 -08008459 * Checks whether the given op for a UID and package is active.
8460 *
8461 * <p> If you don't hold the {@link android.Manifest.permission#WATCH_APPOPS} permission
8462 * you can query only for your UID.
8463 *
8464 * @see #startWatchingActive(int[], OnOpActiveChangedListener)
8465 * @see #stopWatchingMode(OnOpChangedListener)
Philip P. Moltmann59076d82019-08-19 15:00:40 -07008466 * @see #finishOp(int, int, String, String)
8467 * @see #startOp(int, int, String, boolean, String, String)
Svet Ganovf7b47252018-02-26 11:11:27 -08008468 *
8469 * @hide */
8470 @TestApi
8471 // TODO: Uncomment below annotation once b/73559440 is fixed
8472 // @RequiresPermission(value=Manifest.permission.WATCH_APPOPS, conditional=true)
Jeff Sharkey35e46d22017-06-09 10:01:20 -06008473 public boolean isOperationActive(int code, int uid, String packageName) {
8474 try {
8475 return mService.isOperationActive(code, uid, packageName);
8476 } catch (RemoteException e) {
8477 throw e.rethrowFromSystemServer();
8478 }
8479 }
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00008480
8481 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08008482 * Configures the app ops persistence for testing.
8483 *
8484 * @param mode The mode in which the historical registry operates.
8485 * @param baseSnapshotInterval The base interval on which we would be persisting a snapshot of
8486 * the historical data. The history is recursive where every subsequent step encompasses
8487 * {@code compressionStep} longer interval with {@code compressionStep} distance between
8488 * snapshots.
8489 * @param compressionStep The compression step in every iteration.
8490 *
8491 * @see #HISTORICAL_MODE_DISABLED
8492 * @see #HISTORICAL_MODE_ENABLED_ACTIVE
8493 * @see #HISTORICAL_MODE_ENABLED_PASSIVE
8494 *
8495 * @hide
8496 */
8497 @TestApi
8498 @RequiresPermission(Manifest.permission.MANAGE_APPOPS)
8499 public void setHistoryParameters(@HistoricalMode int mode, long baseSnapshotInterval,
8500 int compressionStep) {
8501 try {
8502 mService.setHistoryParameters(mode, baseSnapshotInterval, compressionStep);
8503 } catch (RemoteException e) {
8504 throw e.rethrowFromSystemServer();
8505 }
8506 }
8507
8508 /**
8509 * Offsets the history by the given duration.
8510 *
8511 * @param offsetMillis The offset duration.
8512 *
8513 * @hide
8514 */
8515 @TestApi
8516 @RequiresPermission(Manifest.permission.MANAGE_APPOPS)
8517 public void offsetHistory(long offsetMillis) {
8518 try {
8519 mService.offsetHistory(offsetMillis);
8520 } catch (RemoteException e) {
8521 throw e.rethrowFromSystemServer();
8522 }
8523 }
8524
8525 /**
8526 * Adds ops to the history directly. This could be useful for testing especially
8527 * when the historical registry operates in {@link #HISTORICAL_MODE_ENABLED_PASSIVE}
8528 * mode.
8529 *
8530 * @param ops The ops to add to the history.
8531 *
8532 * @see #setHistoryParameters(int, long, int)
8533 * @see #HISTORICAL_MODE_ENABLED_PASSIVE
8534 *
8535 * @hide
8536 */
8537 @TestApi
8538 @RequiresPermission(Manifest.permission.MANAGE_APPOPS)
8539 public void addHistoricalOps(@NonNull HistoricalOps ops) {
8540 try {
8541 mService.addHistoricalOps(ops);
8542 } catch (RemoteException e) {
8543 throw e.rethrowFromSystemServer();
8544 }
8545 }
8546
8547 /**
8548 * Resets the app ops persistence for testing.
8549 *
8550 * @see #setHistoryParameters(int, long, int)
8551 *
8552 * @hide
8553 */
8554 @TestApi
8555 @RequiresPermission(Manifest.permission.MANAGE_APPOPS)
8556 public void resetHistoryParameters() {
8557 try {
8558 mService.resetHistoryParameters();
8559 } catch (RemoteException e) {
8560 throw e.rethrowFromSystemServer();
8561 }
8562 }
8563
8564 /**
8565 * Clears all app ops history.
8566 *
8567 * @hide
8568 */
8569 @TestApi
8570 @RequiresPermission(Manifest.permission.MANAGE_APPOPS)
8571 public void clearHistory() {
8572 try {
8573 mService.clearHistory();
8574 } catch (RemoteException e) {
8575 throw e.rethrowFromSystemServer();
8576 }
8577 }
8578
8579 /**
Stanislav Zholnin90516b92020-01-20 14:03:06 +00008580 * Pulls current AppOps access report and picks package and op to watch for next access report
Stanislav Zholninb2ed79a2020-03-24 14:14:45 +00008581 * Returns null if no reports were collected since last call. There is no guarantee of report
8582 * collection, hence this method should be called periodically even if no report was collected
8583 * to pick different package and op to watch.
Stanislav Zholnin90516b92020-01-20 14:03:06 +00008584 * @hide
8585 */
8586 @SystemApi
8587 @TestApi
8588 @RequiresPermission(Manifest.permission.GET_APP_OPS_STATS)
8589 public @Nullable RuntimeAppOpAccessMessage collectRuntimeAppOpAccessMessage() {
8590 try {
8591 return mService.collectRuntimeAppOpAccessMessage();
8592 } catch (RemoteException e) {
8593 throw e.rethrowFromSystemServer();
8594 }
8595 }
8596
8597 /**
Peter Visontay5a2a1ef2017-12-18 20:34:03 +00008598 * Returns all supported operation names.
8599 * @hide
8600 */
8601 @SystemApi
8602 @TestApi
8603 public static String[] getOpStrs() {
8604 return Arrays.copyOf(sOpToString, sOpToString.length);
8605 }
Dianne Hackbornc216a262018-04-26 13:46:22 -07008606
Philip P. Moltmann24576812018-05-07 10:42:05 -07008607
8608 /**
8609 * @return number of App ops
8610 * @hide
8611 */
8612 @TestApi
8613 public static int getNumOps() {
8614 return _NUM_OP;
8615 }
8616
Dianne Hackbornc216a262018-04-26 13:46:22 -07008617 /**
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08008618 * Gets the last of the event.
Svet Ganovaf189e32019-02-15 18:45:29 -08008619 *
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08008620 * @param events The events
8621 * @param flags The UID flags
8622 * @param beginUidState The maximum UID state (inclusive)
8623 * @param endUidState The minimum UID state (inclusive)
8624 *
8625 * @return The last event of {@code null}
Dianne Hackbornc216a262018-04-26 13:46:22 -07008626 */
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08008627 private static @Nullable NoteOpEvent getLastEvent(
8628 @Nullable LongSparseArray<NoteOpEvent> events, @UidState int beginUidState,
8629 @UidState int endUidState, @OpFlags int flags) {
8630 if (events == null) {
8631 return null;
Svet Ganovaf189e32019-02-15 18:45:29 -08008632 }
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08008633
8634 NoteOpEvent lastEvent = null;
Svet Ganovaf189e32019-02-15 18:45:29 -08008635 while (flags != 0) {
8636 final int flag = 1 << Integer.numberOfTrailingZeros(flags);
8637 flags &= ~flag;
8638 for (int uidState : UID_STATES) {
8639 if (uidState < beginUidState || uidState > endUidState) {
8640 continue;
8641 }
8642 final long key = makeKey(uidState, flag);
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08008643
8644 NoteOpEvent event = events.get(key);
Philip P. Moltmannd8c0bda2019-12-19 19:55:35 -08008645 if (lastEvent == null
8646 || event != null && event.getNoteTime() > lastEvent.getNoteTime()) {
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08008647 lastEvent = event;
8648 }
Dianne Hackbornc216a262018-04-26 13:46:22 -07008649 }
8650 }
Svet Ganovaf189e32019-02-15 18:45:29 -08008651
Philip P. Moltmann49bd9e12019-11-26 15:18:16 -08008652 return lastEvent;
8653 }
Svet Ganovaf189e32019-02-15 18:45:29 -08008654
Philip P. Moltmann4aacd712020-01-03 12:32:20 -08008655 private static boolean equalsLongSparseLongArray(@Nullable LongSparseLongArray a,
8656 @Nullable LongSparseLongArray b) {
8657 if (a == b) {
8658 return true;
8659 }
8660
8661 if (a == null || b == null) {
8662 return false;
8663 }
8664
8665 if (a.size() != b.size()) {
8666 return false;
8667 }
8668
8669 int numEntries = a.size();
8670 for (int i = 0; i < numEntries; i++) {
8671 if (a.keyAt(i) != b.keyAt(i) || a.valueAt(i) != b.valueAt(i)) {
8672 return false;
8673 }
8674 }
8675
8676 return true;
8677 }
8678
Svet Ganovaf189e32019-02-15 18:45:29 -08008679 private static void writeLongSparseLongArrayToParcel(
8680 @Nullable LongSparseLongArray array, @NonNull Parcel parcel) {
8681 if (array != null) {
8682 final int size = array.size();
8683 parcel.writeInt(size);
8684 for (int i = 0; i < size; i++) {
8685 parcel.writeLong(array.keyAt(i));
8686 parcel.writeLong(array.valueAt(i));
8687 }
8688 } else {
8689 parcel.writeInt(-1);
8690 }
8691 }
8692
8693 private static @Nullable LongSparseLongArray readLongSparseLongArrayFromParcel(
8694 @NonNull Parcel parcel) {
8695 final int size = parcel.readInt();
8696 if (size < 0) {
8697 return null;
8698 }
8699 final LongSparseLongArray array = new LongSparseLongArray(size);
8700 for (int i = 0; i < size; i++) {
8701 array.append(parcel.readLong(), parcel.readLong());
8702 }
8703 return array;
8704 }
8705
Svet Ganovaf189e32019-02-15 18:45:29 -08008706 /**
8707 * Collects the keys from an array to the result creating the result if needed.
8708 *
8709 * @param array The array whose keys to collect.
8710 * @param result The optional result store collected keys.
8711 * @return The result collected keys array.
8712 */
8713 private static LongSparseArray<Object> collectKeys(@Nullable LongSparseLongArray array,
8714 @Nullable LongSparseArray<Object> result) {
8715 if (array != null) {
8716 if (result == null) {
8717 result = new LongSparseArray<>();
8718 }
8719 final int accessSize = array.size();
8720 for (int i = 0; i < accessSize; i++) {
8721 result.put(array.keyAt(i), null);
8722 }
8723 }
8724 return result;
Dianne Hackbornc216a262018-04-26 13:46:22 -07008725 }
Svet Ganov8455ba22019-01-02 13:05:56 -08008726
8727 /** @hide */
8728 public static String uidStateToString(@UidState int uidState) {
8729 switch (uidState) {
8730 case UID_STATE_PERSISTENT: {
8731 return "UID_STATE_PERSISTENT";
8732 }
8733 case UID_STATE_TOP: {
8734 return "UID_STATE_TOP";
8735 }
Amith Yamasania0a30a12019-01-22 11:38:06 -08008736 case UID_STATE_FOREGROUND_SERVICE_LOCATION: {
8737 return "UID_STATE_FOREGROUND_SERVICE_LOCATION";
8738 }
Svet Ganov8455ba22019-01-02 13:05:56 -08008739 case UID_STATE_FOREGROUND_SERVICE: {
8740 return "UID_STATE_FOREGROUND_SERVICE";
8741 }
8742 case UID_STATE_FOREGROUND: {
8743 return "UID_STATE_FOREGROUND";
8744 }
8745 case UID_STATE_BACKGROUND: {
8746 return "UID_STATE_BACKGROUND";
8747 }
8748 case UID_STATE_CACHED: {
8749 return "UID_STATE_CACHED";
8750 }
8751 default: {
8752 return "UNKNOWN";
8753 }
8754 }
8755 }
8756
8757 /** @hide */
8758 public static int parseHistoricalMode(@NonNull String mode) {
8759 switch (mode) {
8760 case "HISTORICAL_MODE_ENABLED_ACTIVE": {
8761 return HISTORICAL_MODE_ENABLED_ACTIVE;
8762 }
8763 case "HISTORICAL_MODE_ENABLED_PASSIVE": {
8764 return HISTORICAL_MODE_ENABLED_PASSIVE;
8765 }
8766 default: {
8767 return HISTORICAL_MODE_DISABLED;
8768 }
8769 }
8770 }
8771
8772 /** @hide */
8773 public static String historicalModeToString(@HistoricalMode int mode) {
8774 switch (mode) {
8775 case HISTORICAL_MODE_DISABLED: {
8776 return "HISTORICAL_MODE_DISABLED";
8777 }
8778 case HISTORICAL_MODE_ENABLED_ACTIVE: {
8779 return "HISTORICAL_MODE_ENABLED_ACTIVE";
8780 }
8781 case HISTORICAL_MODE_ENABLED_PASSIVE: {
8782 return "HISTORICAL_MODE_ENABLED_PASSIVE";
8783 }
8784 default: {
8785 return "UNKNOWN";
8786 }
8787 }
8788 }
Ng Zhi An65a99b62018-10-01 11:57:53 -07008789
8790 private static int getSystemAlertWindowDefault() {
8791 final Context context = ActivityThread.currentApplication();
8792 if (context == null) {
8793 return AppOpsManager.MODE_DEFAULT;
8794 }
8795
8796 // system alert window is disable on low ram phones starting from Q
8797 final PackageManager pm = context.getPackageManager();
8798 // TVs are constantly plugged in and has less concern for memory/power
8799 if (ActivityManager.isLowRamDeviceStatic()
8800 && !pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK, 0)) {
8801 return AppOpsManager.MODE_IGNORED;
8802 }
8803
8804 return AppOpsManager.MODE_DEFAULT;
8805 }
Stanislav Zholnin90516b92020-01-20 14:03:06 +00008806
8807 /**
8808 * Calculate left circular distance for two numbers modulo size.
8809 * @hide
8810 */
8811 public static int leftCircularDistance(int from, int to, int size) {
8812 return (to + size - from) % size;
8813 }
David Cheung2ead9662020-02-19 16:11:06 -08008814
8815 /**
8816 * Helper method for noteOp, startOp and noteProxyOp to call AppOpsService to collect/log
8817 * stack traces
8818 *
8819 * <p> For each call, the stacktrace op code, package name and long version code will be
8820 * passed along where it will be logged/collected
8821 *
8822 * @param op The operation to note
8823 */
8824 private void collectNoteOpCallsForValidation(int op) {
8825 if (NOTE_OP_COLLECTION_ENABLED) {
8826 try {
8827 mService.collectNoteOpCallsForValidation(getFormattedStackTrace(),
8828 op, mContext.getOpPackageName(), mContext.getApplicationInfo().longVersionCode);
8829 } catch (RemoteException e) {
8830 // Swallow error, only meant for logging ops, should not affect flow of the code
8831 }
8832 }
8833 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08008834}