blob: 64586a6d8415c49191c8b5a87d61c7f1e0030e79 [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 Davidson05542602014-08-11 14:07:27 -070020import android.annotation.SystemApi;
21import android.app.usage.UsageStatsManager;
22import android.content.Context;
John Spurlock7b414672014-07-18 13:02:39 -040023import android.media.AudioAttributes.AttributeUsage;
Dianne Hackborne98f5db2013-07-17 17:23:25 -070024import android.os.Binder;
25import android.os.IBinder;
Dianne Hackborn35654b62013-01-14 17:38:02 -080026import android.os.Parcel;
27import android.os.Parcelable;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080028import android.os.Process;
29import android.os.RemoteException;
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -080030import android.os.UserHandle;
Jeff Davidson05542602014-08-11 14:07:27 -070031import android.os.UserManager;
32import android.util.ArrayMap;
33
34import com.android.internal.app.IAppOpsCallback;
35import com.android.internal.app.IAppOpsService;
36
37import java.util.ArrayList;
38import java.util.HashMap;
39import java.util.List;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080040
Dianne Hackbornd7d28e62013-02-12 14:59:53 -080041/**
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -070042 * API for interacting with "application operation" tracking.
Dianne Hackbornd7d28e62013-02-12 14:59:53 -080043 *
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -070044 * <p>This API is not generally intended for third party application developers; most
John Spurlock925b85e2014-03-10 16:52:11 -040045 * features are only available to system applications. Obtain an instance of it through
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -070046 * {@link Context#getSystemService(String) Context.getSystemService} with
47 * {@link Context#APP_OPS_SERVICE Context.APP_OPS_SERVICE}.</p>
Dianne Hackbornd7d28e62013-02-12 14:59:53 -080048 */
Dianne Hackborna06de0f2012-12-11 16:34:47 -080049public class AppOpsManager {
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -070050 /**
51 * <p>App ops allows callers to:</p>
52 *
53 * <ul>
54 * <li> Note when operations are happening, and find out if they are allowed for the current
55 * caller.</li>
56 * <li> Disallow specific apps from doing specific operations.</li>
57 * <li> Collect all of the current information about operations that have been executed or
58 * are not being allowed.</li>
59 * <li> Monitor for changes in whether an operation is allowed.</li>
60 * </ul>
61 *
62 * <p>Each operation is identified by a single integer; these integers are a fixed set of
63 * operations, enumerated by the OP_* constants.
64 *
65 * <p></p>When checking operations, the result is a "mode" integer indicating the current
66 * setting for the operation under that caller: MODE_ALLOWED, MODE_IGNORED (don't execute
67 * the operation but fake its behavior enough so that the caller doesn't crash),
68 * MODE_ERRORED (throw a SecurityException back to the caller; the normal operation calls
69 * will do this for you).
70 */
71
Dianne Hackborna06de0f2012-12-11 16:34:47 -080072 final Context mContext;
73 final IAppOpsService mService;
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -070074 final ArrayMap<OnOpChangedListener, IAppOpsCallback> mModeWatchers
75 = new ArrayMap<OnOpChangedListener, IAppOpsCallback>();
Dianne Hackborna06de0f2012-12-11 16:34:47 -080076
Dianne Hackborne98f5db2013-07-17 17:23:25 -070077 static IBinder sToken;
78
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -070079 /**
80 * Result from {@link #checkOp}, {@link #noteOp}, {@link #startOp}: the given caller is
81 * allowed to perform the given operation.
82 */
Dianne Hackborna06de0f2012-12-11 16:34:47 -080083 public static final int MODE_ALLOWED = 0;
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -070084
85 /**
86 * Result from {@link #checkOp}, {@link #noteOp}, {@link #startOp}: the given caller is
87 * not allowed to perform the given operation, and this attempt should
88 * <em>silently fail</em> (it should not cause the app to crash).
89 */
Dianne Hackborna06de0f2012-12-11 16:34:47 -080090 public static final int MODE_IGNORED = 1;
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -070091
92 /**
93 * Result from {@link #checkOpNoThrow}, {@link #noteOpNoThrow}, {@link #startOpNoThrow}: the
94 * given caller is not allowed to perform the given operation, and this attempt should
95 * cause it to have a fatal error, typically a {@link SecurityException}.
96 */
Dianne Hackborna06de0f2012-12-11 16:34:47 -080097 public static final int MODE_ERRORED = 2;
98
Dianne Hackborn33f5ddd2014-07-21 15:35:45 -070099 /**
100 * Result from {@link #checkOp}, {@link #noteOp}, {@link #startOp}: the given caller should
101 * use its default security check. This mode is not normally used; it should only be used
102 * with appop permissions, and callers must explicitly check for it and deal with it.
103 */
104 public static final int MODE_DEFAULT = 3;
105
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500106 // when adding one of these:
107 // - increment _NUM_OP
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700108 // - add rows to sOpToSwitch, sOpToString, sOpNames, sOpPerms, sOpDefaultMode
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500109 // - add descriptive strings to Settings/res/values/arrays.xml
David Christie0b837452013-07-29 16:02:13 -0700110 // - add the op to the appropriate template in AppOpsState.OpsTemplate (settings app)
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700111
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700112 /** @hide No operation specified. */
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800113 public static final int OP_NONE = -1;
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700114 /** @hide Access to coarse location information. */
Dianne Hackborn35654b62013-01-14 17:38:02 -0800115 public static final int OP_COARSE_LOCATION = 0;
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700116 /** @hide Access to fine location information. */
Dianne Hackborn35654b62013-01-14 17:38:02 -0800117 public static final int OP_FINE_LOCATION = 1;
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700118 /** @hide Causing GPS to run. */
Dianne Hackborn35654b62013-01-14 17:38:02 -0800119 public static final int OP_GPS = 2;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800120 /** @hide */
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700121 public static final int OP_VIBRATE = 3;
122 /** @hide */
123 public static final int OP_READ_CONTACTS = 4;
124 /** @hide */
125 public static final int OP_WRITE_CONTACTS = 5;
126 /** @hide */
127 public static final int OP_READ_CALL_LOG = 6;
128 /** @hide */
129 public static final int OP_WRITE_CALL_LOG = 7;
130 /** @hide */
131 public static final int OP_READ_CALENDAR = 8;
132 /** @hide */
133 public static final int OP_WRITE_CALENDAR = 9;
134 /** @hide */
135 public static final int OP_WIFI_SCAN = 10;
136 /** @hide */
137 public static final int OP_POST_NOTIFICATION = 11;
138 /** @hide */
139 public static final int OP_NEIGHBORING_CELLS = 12;
140 /** @hide */
141 public static final int OP_CALL_PHONE = 13;
142 /** @hide */
143 public static final int OP_READ_SMS = 14;
144 /** @hide */
145 public static final int OP_WRITE_SMS = 15;
146 /** @hide */
147 public static final int OP_RECEIVE_SMS = 16;
148 /** @hide */
149 public static final int OP_RECEIVE_EMERGECY_SMS = 17;
150 /** @hide */
151 public static final int OP_RECEIVE_MMS = 18;
152 /** @hide */
153 public static final int OP_RECEIVE_WAP_PUSH = 19;
154 /** @hide */
155 public static final int OP_SEND_SMS = 20;
156 /** @hide */
157 public static final int OP_READ_ICC_SMS = 21;
158 /** @hide */
159 public static final int OP_WRITE_ICC_SMS = 22;
160 /** @hide */
161 public static final int OP_WRITE_SETTINGS = 23;
162 /** @hide */
163 public static final int OP_SYSTEM_ALERT_WINDOW = 24;
164 /** @hide */
165 public static final int OP_ACCESS_NOTIFICATIONS = 25;
166 /** @hide */
167 public static final int OP_CAMERA = 26;
168 /** @hide */
169 public static final int OP_RECORD_AUDIO = 27;
170 /** @hide */
171 public static final int OP_PLAY_AUDIO = 28;
172 /** @hide */
173 public static final int OP_READ_CLIPBOARD = 29;
174 /** @hide */
175 public static final int OP_WRITE_CLIPBOARD = 30;
176 /** @hide */
177 public static final int OP_TAKE_MEDIA_BUTTONS = 31;
178 /** @hide */
179 public static final int OP_TAKE_AUDIO_FOCUS = 32;
180 /** @hide */
181 public static final int OP_AUDIO_MASTER_VOLUME = 33;
182 /** @hide */
183 public static final int OP_AUDIO_VOICE_VOLUME = 34;
184 /** @hide */
185 public static final int OP_AUDIO_RING_VOLUME = 35;
186 /** @hide */
187 public static final int OP_AUDIO_MEDIA_VOLUME = 36;
188 /** @hide */
189 public static final int OP_AUDIO_ALARM_VOLUME = 37;
190 /** @hide */
191 public static final int OP_AUDIO_NOTIFICATION_VOLUME = 38;
192 /** @hide */
193 public static final int OP_AUDIO_BLUETOOTH_VOLUME = 39;
194 /** @hide */
195 public static final int OP_WAKE_LOCK = 40;
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700196 /** @hide Continually monitoring location data. */
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700197 public static final int OP_MONITOR_LOCATION = 41;
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700198 /** @hide Continually monitoring location data with a relatively high power request. */
David Christie0b837452013-07-29 16:02:13 -0700199 public static final int OP_MONITOR_HIGH_POWER_LOCATION = 42;
Dianne Hackborne22b3b12014-05-07 18:06:44 -0700200 /** @hide Retrieve current usage stats via {@link UsageStatsManager}. */
201 public static final int OP_GET_USAGE_STATS = 43;
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700202 /** @hide */
Emily Bernier22c921a2014-05-28 11:01:32 -0400203 public static final int OP_MUTE_MICROPHONE = 44;
204 /** @hide */
Jason Monk1c7c3192014-06-26 12:52:18 -0400205 public static final int OP_TOAST_WINDOW = 45;
Michael Wrightc39d47a2014-07-08 18:07:36 -0700206 /** @hide Capture the device's display contents and/or audio */
207 public static final int OP_PROJECT_MEDIA = 46;
Jeff Davidson05542602014-08-11 14:07:27 -0700208 /** @hide Activate a VPN connection without user intervention. */
209 public static final int OP_ACTIVATE_VPN = 47;
Benjamin Franzf3ece362015-02-11 10:51:10 +0000210 /** @hide Access the WallpaperManagerAPI to write wallpapers. */
211 public static final int OP_WRITE_WALLPAPER = 48;
Dianne Hackbornd59a5d52015-04-04 14:52:14 -0700212 /** @hide Received the assist structure from an app. */
213 public static final int OP_ASSIST_STRUCTURE = 49;
214 /** @hide Received a screenshot from assist. */
215 public static final int OP_ASSIST_SCREENSHOT = 50;
Svet Ganov16a16892015-04-16 10:32:04 -0700216 /** @hide Read the phone state. */
217 public static final int OP_READ_PHONE_STATE = 51;
Svet Ganovc3300092015-04-17 09:07:22 -0700218 /** @hide Add voicemail messages to the voicemail content provider. */
219 public static final int OP_ADD_VOICEMAIL = 52;
Svetoslav5335b672015-04-29 12:00:51 -0700220 /** @hide Access APIs for SIP calling over VOIP or WiFi. */
221 public static final int OP_USE_SIP = 53;
Svetoslavc656e6f2015-04-29 14:08:16 -0700222 /** @hide Intercept outgoing calls. */
223 public static final int OP_PROCESS_OUTGOING_CALLS = 54;
Svetoslav4af76a52015-04-29 15:29:46 -0700224 /** @hide User the fingerprint API. */
225 public static final int OP_USE_FINGERPRINT = 55;
Svet Ganovb9d71a62015-04-30 10:38:13 -0700226 /** @hide Access to body sensors such as heart rate, etc. */
227 public static final int OP_BODY_SENSORS = 56;
Svet Ganovede43162015-05-02 17:42:44 -0700228 /** @hide Read previously received cell broadcast messages. */
229 public static final int OP_READ_CELL_BROADCASTS = 57;
Svet Ganovf7e9cf42015-05-13 10:40:31 -0700230 /** @hide Inject mock location into the system. */
231 public static final int OP_MOCK_LOCATION = 58;
Svet Ganov921c7df2015-06-29 21:51:41 -0700232 /** @hide Read external storage. */
233 public static final int OP_READ_EXTERNAL_STORAGE = 59;
234 /** @hide Write external storage. */
235 public static final int OP_WRITE_EXTERNAL_STORAGE = 60;
Dianne Hackborn280a64e2015-07-13 14:48:08 -0700236 /** @hide Turned on the screen. */
237 public static final int OP_TURN_SCREEN_ON = 61;
Svetoslavf3f02ac2015-09-08 14:36:35 -0700238 /** @hide Get device accounts. */
239 public static final int OP_GET_ACCOUNTS = 62;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700240 /** @hide Control whether an application is allowed to run in the background. */
241 public static final int OP_RUN_IN_BACKGROUND = 63;
Jason Monk1c7c3192014-06-26 12:52:18 -0400242 /** @hide */
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700243 public static final int _NUM_OP = 64;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800244
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700245 /** Access to coarse location information. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -0700246 public static final String OPSTR_COARSE_LOCATION = "android:coarse_location";
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700247 /** Access to fine location information. */
248 public static final String OPSTR_FINE_LOCATION =
249 "android:fine_location";
250 /** Continually monitoring location data. */
251 public static final String OPSTR_MONITOR_LOCATION
252 = "android:monitor_location";
253 /** Continually monitoring location data with a relatively high power request. */
254 public static final String OPSTR_MONITOR_HIGH_POWER_LOCATION
255 = "android:monitor_location_high_power";
Dianne Hackborn5064e7c2014-09-02 10:57:16 -0700256 /** Access to {@link android.app.usage.UsageStatsManager}. */
257 public static final String OPSTR_GET_USAGE_STATS
258 = "android:get_usage_stats";
Jeff Davidson05542602014-08-11 14:07:27 -0700259 /** Activate a VPN connection without user intervention. @hide */
260 @SystemApi
Svet Ganov6e8f67c2015-04-29 17:35:19 -0700261 public static final String OPSTR_ACTIVATE_VPN
262 = "android:activate_vpn";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700263 /** Allows an application to read the user's contacts data. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -0700264 public static final String OPSTR_READ_CONTACTS
265 = "android:read_contacts";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700266 /** Allows an application to write to the user's contacts data. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -0700267 public static final String OPSTR_WRITE_CONTACTS
268 = "android:write_contacts";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700269 /** Allows an application to read the user's call log. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -0700270 public static final String OPSTR_READ_CALL_LOG
271 = "android:read_call_log";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700272 /** Allows an application to write to the user's call log. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -0700273 public static final String OPSTR_WRITE_CALL_LOG
274 = "android:write_call_log";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700275 /** Allows an application to read the user's calendar data. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -0700276 public static final String OPSTR_READ_CALENDAR
277 = "android:read_calendar";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700278 /** Allows an application to write to the user's calendar data. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -0700279 public static final String OPSTR_WRITE_CALENDAR
280 = "android:write_calendar";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700281 /** Allows an application to initiate a phone call. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -0700282 public static final String OPSTR_CALL_PHONE
283 = "android:call_phone";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700284 /** Allows an application to read SMS messages. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -0700285 public static final String OPSTR_READ_SMS
286 = "android:read_sms";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700287 /** Allows an application to receive SMS messages. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -0700288 public static final String OPSTR_RECEIVE_SMS
289 = "android:receive_sms";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700290 /** Allows an application to receive MMS messages. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -0700291 public static final String OPSTR_RECEIVE_MMS
292 = "android:receive_mms";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700293 /** Allows an application to receive WAP push messages. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -0700294 public static final String OPSTR_RECEIVE_WAP_PUSH
295 = "android:receive_wap_push";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700296 /** Allows an application to send SMS messages. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -0700297 public static final String OPSTR_SEND_SMS
298 = "android:send_sms";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700299 /** Required to be able to access the camera device. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -0700300 public static final String OPSTR_CAMERA
301 = "android:camera";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700302 /** Required to be able to access the microphone device. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -0700303 public static final String OPSTR_RECORD_AUDIO
304 = "android:record_audio";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700305 /** Required to access phone state related information. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -0700306 public static final String OPSTR_READ_PHONE_STATE
307 = "android:read_phone_state";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700308 /** Required to access phone state related information. */
Svet Ganov6e8f67c2015-04-29 17:35:19 -0700309 public static final String OPSTR_ADD_VOICEMAIL
310 = "android:add_voicemail";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700311 /** Access APIs for SIP calling over VOIP or WiFi */
Svet Ganovb9d71a62015-04-30 10:38:13 -0700312 public static final String OPSTR_USE_SIP
313 = "android:use_sip";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700314 /** Use the fingerprint API. */
Svet Ganovb9d71a62015-04-30 10:38:13 -0700315 public static final String OPSTR_USE_FINGERPRINT
316 = "android:use_fingerprint";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700317 /** Access to body sensors such as heart rate, etc. */
Svet Ganovb9d71a62015-04-30 10:38:13 -0700318 public static final String OPSTR_BODY_SENSORS
319 = "android:body_sensors";
Svet Ganov715cf2a2015-06-13 17:31:29 -0700320 /** Read previously received cell broadcast messages. */
Svet Ganovede43162015-05-02 17:42:44 -0700321 public static final String OPSTR_READ_CELL_BROADCASTS
322 = "android:read_cell_broadcasts";
Svet Ganovf7e9cf42015-05-13 10:40:31 -0700323 /** Inject mock location into the system. */
324 public static final String OPSTR_MOCK_LOCATION
325 = "android:mock_location";
Svet Ganov921c7df2015-06-29 21:51:41 -0700326 /** Read external storage. */
327 public static final String OPSTR_READ_EXTERNAL_STORAGE
328 = "android:read_external_storage";
329 /** Write external storage. */
330 public static final String OPSTR_WRITE_EXTERNAL_STORAGE
331 = "android:write_external_storage";
Billy Lau24b9c832015-07-20 17:34:09 +0100332 /** Required to draw on top of other apps. */
333 public static final String OPSTR_SYSTEM_ALERT_WINDOW
334 = "android:system_alert_window";
335 /** Required to write/modify/update system settingss. */
336 public static final String OPSTR_WRITE_SETTINGS
337 = "android:write_settings";
Svetoslavf3f02ac2015-09-08 14:36:35 -0700338 /** @hide Get device accounts. */
339 public static final String OPSTR_GET_ACCOUNTS
340 = "android:get_accounts";
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700341
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800342 /**
343 * This maps each operation to the operation that serves as the
344 * switch to determine whether it is allowed. Generally this is
345 * a 1:1 mapping, but for some things (like location) that have
346 * multiple low-level operations being tracked that should be
David Christie0b837452013-07-29 16:02:13 -0700347 * presented to the user as one switch then this can be used to
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800348 * make them all controlled by the same single operation.
349 */
350 private static int[] sOpToSwitch = new int[] {
351 OP_COARSE_LOCATION,
352 OP_COARSE_LOCATION,
353 OP_COARSE_LOCATION,
354 OP_VIBRATE,
355 OP_READ_CONTACTS,
356 OP_WRITE_CONTACTS,
357 OP_READ_CALL_LOG,
358 OP_WRITE_CALL_LOG,
359 OP_READ_CALENDAR,
360 OP_WRITE_CALENDAR,
361 OP_COARSE_LOCATION,
362 OP_POST_NOTIFICATION,
363 OP_COARSE_LOCATION,
364 OP_CALL_PHONE,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800365 OP_READ_SMS,
366 OP_WRITE_SMS,
David Braun18966a82013-09-10 13:14:46 -0700367 OP_RECEIVE_SMS,
368 OP_RECEIVE_SMS,
369 OP_RECEIVE_SMS,
370 OP_RECEIVE_SMS,
371 OP_SEND_SMS,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800372 OP_READ_SMS,
373 OP_WRITE_SMS,
Dianne Hackborn961321f2013-02-05 17:22:41 -0800374 OP_WRITE_SETTINGS,
Dianne Hackbornc2293022013-02-06 23:14:49 -0800375 OP_SYSTEM_ALERT_WINDOW,
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500376 OP_ACCESS_NOTIFICATIONS,
Dianne Hackbornd7d28e62013-02-12 14:59:53 -0800377 OP_CAMERA,
378 OP_RECORD_AUDIO,
379 OP_PLAY_AUDIO,
Dianne Hackbornefcc1a22013-02-25 18:02:35 -0800380 OP_READ_CLIPBOARD,
381 OP_WRITE_CLIPBOARD,
Dianne Hackbornba50b97c2013-04-30 15:04:46 -0700382 OP_TAKE_MEDIA_BUTTONS,
383 OP_TAKE_AUDIO_FOCUS,
384 OP_AUDIO_MASTER_VOLUME,
385 OP_AUDIO_VOICE_VOLUME,
386 OP_AUDIO_RING_VOLUME,
387 OP_AUDIO_MEDIA_VOLUME,
388 OP_AUDIO_ALARM_VOLUME,
389 OP_AUDIO_NOTIFICATION_VOLUME,
390 OP_AUDIO_BLUETOOTH_VOLUME,
Dianne Hackborn713df152013-05-17 11:27:57 -0700391 OP_WAKE_LOCK,
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700392 OP_COARSE_LOCATION,
David Christie0b837452013-07-29 16:02:13 -0700393 OP_COARSE_LOCATION,
Dianne Hackborne22b3b12014-05-07 18:06:44 -0700394 OP_GET_USAGE_STATS,
Jason Monk1c7c3192014-06-26 12:52:18 -0400395 OP_MUTE_MICROPHONE,
396 OP_TOAST_WINDOW,
Michael Wrightc39d47a2014-07-08 18:07:36 -0700397 OP_PROJECT_MEDIA,
Jeff Davidson05542602014-08-11 14:07:27 -0700398 OP_ACTIVATE_VPN,
Benjamin Franzf3ece362015-02-11 10:51:10 +0000399 OP_WRITE_WALLPAPER,
Dianne Hackbornd59a5d52015-04-04 14:52:14 -0700400 OP_ASSIST_STRUCTURE,
401 OP_ASSIST_SCREENSHOT,
Svet Ganovc3300092015-04-17 09:07:22 -0700402 OP_READ_PHONE_STATE,
Svetoslav5335b672015-04-29 12:00:51 -0700403 OP_ADD_VOICEMAIL,
Svetoslavc656e6f2015-04-29 14:08:16 -0700404 OP_USE_SIP,
Svetoslav4af76a52015-04-29 15:29:46 -0700405 OP_PROCESS_OUTGOING_CALLS,
Svet Ganovb9d71a62015-04-30 10:38:13 -0700406 OP_USE_FINGERPRINT,
Svet Ganovede43162015-05-02 17:42:44 -0700407 OP_BODY_SENSORS,
Svet Ganovf7e9cf42015-05-13 10:40:31 -0700408 OP_READ_CELL_BROADCASTS,
Svet Ganov921c7df2015-06-29 21:51:41 -0700409 OP_MOCK_LOCATION,
410 OP_READ_EXTERNAL_STORAGE,
Dianne Hackborn280a64e2015-07-13 14:48:08 -0700411 OP_WRITE_EXTERNAL_STORAGE,
412 OP_TURN_SCREEN_ON,
Svetoslavf3f02ac2015-09-08 14:36:35 -0700413 OP_GET_ACCOUNTS,
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700414 OP_RUN_IN_BACKGROUND,
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800415 };
416
417 /**
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700418 * This maps each operation to the public string constant for it.
419 * If it doesn't have a public string constant, it maps to null.
420 */
421 private static String[] sOpToString = new String[] {
422 OPSTR_COARSE_LOCATION,
423 OPSTR_FINE_LOCATION,
424 null,
425 null,
Svet Ganovb9d71a62015-04-30 10:38:13 -0700426 OPSTR_READ_CONTACTS,
427 OPSTR_WRITE_CONTACTS,
428 OPSTR_READ_CALL_LOG,
429 OPSTR_WRITE_CALL_LOG,
430 OPSTR_READ_CALENDAR,
431 OPSTR_WRITE_CALENDAR,
432 null,
433 null,
434 null,
435 OPSTR_CALL_PHONE,
436 OPSTR_READ_SMS,
437 null,
438 OPSTR_RECEIVE_SMS,
439 null,
440 OPSTR_RECEIVE_MMS,
441 OPSTR_RECEIVE_WAP_PUSH,
442 OPSTR_SEND_SMS,
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700443 null,
444 null,
Billy Lau24b9c832015-07-20 17:34:09 +0100445 OPSTR_WRITE_SETTINGS,
446 OPSTR_SYSTEM_ALERT_WINDOW,
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700447 null,
Svet Ganovb9d71a62015-04-30 10:38:13 -0700448 OPSTR_CAMERA,
449 OPSTR_RECORD_AUDIO,
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700450 null,
451 null,
452 null,
453 null,
454 null,
455 null,
456 null,
457 null,
458 null,
459 null,
460 null,
461 null,
462 null,
463 OPSTR_MONITOR_LOCATION,
464 OPSTR_MONITOR_HIGH_POWER_LOCATION,
Dianne Hackborn5064e7c2014-09-02 10:57:16 -0700465 OPSTR_GET_USAGE_STATS,
Emily Bernier22c921a2014-05-28 11:01:32 -0400466 null,
Jason Monk1c7c3192014-06-26 12:52:18 -0400467 null,
Michael Wrightc39d47a2014-07-08 18:07:36 -0700468 null,
Jeff Davidson05542602014-08-11 14:07:27 -0700469 OPSTR_ACTIVATE_VPN,
Benjamin Franzf3ece362015-02-11 10:51:10 +0000470 null,
Dianne Hackbornd59a5d52015-04-04 14:52:14 -0700471 null,
472 null,
Svet Ganovb9d71a62015-04-30 10:38:13 -0700473 OPSTR_READ_PHONE_STATE,
474 OPSTR_ADD_VOICEMAIL,
475 OPSTR_USE_SIP,
Svet Ganovc3300092015-04-17 09:07:22 -0700476 null,
Svet Ganovb9d71a62015-04-30 10:38:13 -0700477 OPSTR_USE_FINGERPRINT,
Svet Ganovede43162015-05-02 17:42:44 -0700478 OPSTR_BODY_SENSORS,
Svet Ganovf7e9cf42015-05-13 10:40:31 -0700479 OPSTR_READ_CELL_BROADCASTS,
Svet Ganov921c7df2015-06-29 21:51:41 -0700480 OPSTR_MOCK_LOCATION,
481 OPSTR_READ_EXTERNAL_STORAGE,
Dianne Hackborn280a64e2015-07-13 14:48:08 -0700482 OPSTR_WRITE_EXTERNAL_STORAGE,
483 null,
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700484 OPSTR_GET_ACCOUNTS,
485 null,
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700486 };
487
488 /**
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800489 * This provides a simple name for each operation to be used
490 * in debug output.
491 */
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800492 private static String[] sOpNames = new String[] {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800493 "COARSE_LOCATION",
494 "FINE_LOCATION",
495 "GPS",
496 "VIBRATE",
497 "READ_CONTACTS",
498 "WRITE_CONTACTS",
499 "READ_CALL_LOG",
500 "WRITE_CALL_LOG",
501 "READ_CALENDAR",
502 "WRITE_CALENDAR",
503 "WIFI_SCAN",
504 "POST_NOTIFICATION",
505 "NEIGHBORING_CELLS",
506 "CALL_PHONE",
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800507 "READ_SMS",
508 "WRITE_SMS",
509 "RECEIVE_SMS",
510 "RECEIVE_EMERGECY_SMS",
511 "RECEIVE_MMS",
512 "RECEIVE_WAP_PUSH",
513 "SEND_SMS",
514 "READ_ICC_SMS",
515 "WRITE_ICC_SMS",
Dianne Hackborn961321f2013-02-05 17:22:41 -0800516 "WRITE_SETTINGS",
Dianne Hackbornc2293022013-02-06 23:14:49 -0800517 "SYSTEM_ALERT_WINDOW",
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500518 "ACCESS_NOTIFICATIONS",
Dianne Hackbornd7d28e62013-02-12 14:59:53 -0800519 "CAMERA",
520 "RECORD_AUDIO",
521 "PLAY_AUDIO",
Dianne Hackbornefcc1a22013-02-25 18:02:35 -0800522 "READ_CLIPBOARD",
523 "WRITE_CLIPBOARD",
Dianne Hackbornba50b97c2013-04-30 15:04:46 -0700524 "TAKE_MEDIA_BUTTONS",
525 "TAKE_AUDIO_FOCUS",
526 "AUDIO_MASTER_VOLUME",
527 "AUDIO_VOICE_VOLUME",
528 "AUDIO_RING_VOLUME",
529 "AUDIO_MEDIA_VOLUME",
530 "AUDIO_ALARM_VOLUME",
531 "AUDIO_NOTIFICATION_VOLUME",
532 "AUDIO_BLUETOOTH_VOLUME",
Dianne Hackborn713df152013-05-17 11:27:57 -0700533 "WAKE_LOCK",
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700534 "MONITOR_LOCATION",
David Christie0b837452013-07-29 16:02:13 -0700535 "MONITOR_HIGH_POWER_LOCATION",
Emily Bernier22c921a2014-05-28 11:01:32 -0400536 "GET_USAGE_STATS",
Michael Wrightc39d47a2014-07-08 18:07:36 -0700537 "MUTE_MICROPHONE",
Jason Monk1c7c3192014-06-26 12:52:18 -0400538 "TOAST_WINDOW",
Michael Wrightc39d47a2014-07-08 18:07:36 -0700539 "PROJECT_MEDIA",
Jeff Davidson05542602014-08-11 14:07:27 -0700540 "ACTIVATE_VPN",
Benjamin Franzf3ece362015-02-11 10:51:10 +0000541 "WRITE_WALLPAPER",
Dianne Hackbornd59a5d52015-04-04 14:52:14 -0700542 "ASSIST_STRUCTURE",
Svet Ganov16a16892015-04-16 10:32:04 -0700543 "ASSIST_SCREENSHOT",
Svet Ganovc3300092015-04-17 09:07:22 -0700544 "OP_READ_PHONE_STATE",
Svetoslav5335b672015-04-29 12:00:51 -0700545 "ADD_VOICEMAIL",
Svetoslavc656e6f2015-04-29 14:08:16 -0700546 "USE_SIP",
Svetoslav4af76a52015-04-29 15:29:46 -0700547 "PROCESS_OUTGOING_CALLS",
Svet Ganovb9d71a62015-04-30 10:38:13 -0700548 "USE_FINGERPRINT",
Svet Ganovede43162015-05-02 17:42:44 -0700549 "BODY_SENSORS",
Svet Ganovf7e9cf42015-05-13 10:40:31 -0700550 "READ_CELL_BROADCASTS",
Svet Ganov921c7df2015-06-29 21:51:41 -0700551 "MOCK_LOCATION",
Dianne Hackborn280a64e2015-07-13 14:48:08 -0700552 "READ_EXTERNAL_STORAGE",
553 "WRITE_EXTERNAL_STORAGE",
554 "TURN_ON_SCREEN",
Svetoslavf3f02ac2015-09-08 14:36:35 -0700555 "GET_ACCOUNTS",
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700556 "RUN_IN_BACKGROUND",
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800557 };
558
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800559 /**
560 * This optionally maps a permission to an operation. If there
561 * is no permission associated with an operation, it is null.
562 */
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800563 private static String[] sOpPerms = new String[] {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800564 android.Manifest.permission.ACCESS_COARSE_LOCATION,
565 android.Manifest.permission.ACCESS_FINE_LOCATION,
566 null,
567 android.Manifest.permission.VIBRATE,
568 android.Manifest.permission.READ_CONTACTS,
569 android.Manifest.permission.WRITE_CONTACTS,
570 android.Manifest.permission.READ_CALL_LOG,
571 android.Manifest.permission.WRITE_CALL_LOG,
572 android.Manifest.permission.READ_CALENDAR,
573 android.Manifest.permission.WRITE_CALENDAR,
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800574 android.Manifest.permission.ACCESS_WIFI_STATE,
Robert Craigf97616c2013-10-07 12:32:02 -0400575 null, // no permission required for notifications
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800576 null, // neighboring cells shares the coarse location perm
577 android.Manifest.permission.CALL_PHONE,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800578 android.Manifest.permission.READ_SMS,
Svetoslav6c589572015-04-16 16:19:24 -0700579 null, // no permission required for writing sms
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800580 android.Manifest.permission.RECEIVE_SMS,
581 android.Manifest.permission.RECEIVE_EMERGENCY_BROADCAST,
582 android.Manifest.permission.RECEIVE_MMS,
583 android.Manifest.permission.RECEIVE_WAP_PUSH,
584 android.Manifest.permission.SEND_SMS,
585 android.Manifest.permission.READ_SMS,
Svetoslav6c589572015-04-16 16:19:24 -0700586 null, // no permission required for writing icc sms
Dianne Hackborn961321f2013-02-05 17:22:41 -0800587 android.Manifest.permission.WRITE_SETTINGS,
Dianne Hackbornc2293022013-02-06 23:14:49 -0800588 android.Manifest.permission.SYSTEM_ALERT_WINDOW,
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500589 android.Manifest.permission.ACCESS_NOTIFICATIONS,
Dianne Hackbornd7d28e62013-02-12 14:59:53 -0800590 android.Manifest.permission.CAMERA,
591 android.Manifest.permission.RECORD_AUDIO,
592 null, // no permission for playing audio
Dianne Hackbornefcc1a22013-02-25 18:02:35 -0800593 null, // no permission for reading clipboard
594 null, // no permission for writing clipboard
Dianne Hackbornba50b97c2013-04-30 15:04:46 -0700595 null, // no permission for taking media buttons
596 null, // no permission for taking audio focus
597 null, // no permission for changing master volume
598 null, // no permission for changing voice volume
599 null, // no permission for changing ring volume
600 null, // no permission for changing media volume
601 null, // no permission for changing alarm volume
602 null, // no permission for changing notification volume
603 null, // no permission for changing bluetooth volume
Dianne Hackborn713df152013-05-17 11:27:57 -0700604 android.Manifest.permission.WAKE_LOCK,
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700605 null, // no permission for generic location monitoring
David Christie0b837452013-07-29 16:02:13 -0700606 null, // no permission for high power location monitoring
Dianne Hackborne22b3b12014-05-07 18:06:44 -0700607 android.Manifest.permission.PACKAGE_USAGE_STATS,
Emily Bernier22c921a2014-05-28 11:01:32 -0400608 null, // no permission for muting/unmuting microphone
Jason Monk1c7c3192014-06-26 12:52:18 -0400609 null, // no permission for displaying toasts
Michael Wrightc39d47a2014-07-08 18:07:36 -0700610 null, // no permission for projecting media
Jeff Davidson05542602014-08-11 14:07:27 -0700611 null, // no permission for activating vpn
Benjamin Franzf3ece362015-02-11 10:51:10 +0000612 null, // no permission for supporting wallpaper
Dianne Hackbornd59a5d52015-04-04 14:52:14 -0700613 null, // no permission for receiving assist structure
614 null, // no permission for receiving assist screenshot
Svet Ganovc3300092015-04-17 09:07:22 -0700615 Manifest.permission.READ_PHONE_STATE,
Svetoslav5335b672015-04-29 12:00:51 -0700616 Manifest.permission.ADD_VOICEMAIL,
Svetoslavc656e6f2015-04-29 14:08:16 -0700617 Manifest.permission.USE_SIP,
Svetoslav4af76a52015-04-29 15:29:46 -0700618 Manifest.permission.PROCESS_OUTGOING_CALLS,
Svet Ganovb9d71a62015-04-30 10:38:13 -0700619 Manifest.permission.USE_FINGERPRINT,
Svet Ganovede43162015-05-02 17:42:44 -0700620 Manifest.permission.BODY_SENSORS,
Svet Ganovf7e9cf42015-05-13 10:40:31 -0700621 Manifest.permission.READ_CELL_BROADCASTS,
Svet Ganov921c7df2015-06-29 21:51:41 -0700622 null,
623 Manifest.permission.READ_EXTERNAL_STORAGE,
624 Manifest.permission.WRITE_EXTERNAL_STORAGE,
Dianne Hackborn280a64e2015-07-13 14:48:08 -0700625 null, // no permission for turning the screen on
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700626 Manifest.permission.GET_ACCOUNTS,
627 null, // no permission for running in background
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800628 };
629
Dianne Hackbornd7d28e62013-02-12 14:59:53 -0800630 /**
Jason Monk62062992014-05-06 09:55:28 -0400631 * Specifies whether an Op should be restricted by a user restriction.
632 * Each Op should be filled with a restriction string from UserManager or
633 * null to specify it is not affected by any user restriction.
634 */
635 private static String[] sOpRestrictions = new String[] {
Julia Reynolds9854d572014-07-02 14:46:02 -0400636 UserManager.DISALLOW_SHARE_LOCATION, //COARSE_LOCATION
637 UserManager.DISALLOW_SHARE_LOCATION, //FINE_LOCATION
638 UserManager.DISALLOW_SHARE_LOCATION, //GPS
Jason Monk62062992014-05-06 09:55:28 -0400639 null, //VIBRATE
640 null, //READ_CONTACTS
641 null, //WRITE_CONTACTS
Yorke Lee15f83c62014-08-13 14:14:29 -0700642 UserManager.DISALLOW_OUTGOING_CALLS, //READ_CALL_LOG
643 UserManager.DISALLOW_OUTGOING_CALLS, //WRITE_CALL_LOG
Jason Monk62062992014-05-06 09:55:28 -0400644 null, //READ_CALENDAR
645 null, //WRITE_CALENDAR
Julia Reynolds9854d572014-07-02 14:46:02 -0400646 UserManager.DISALLOW_SHARE_LOCATION, //WIFI_SCAN
Jason Monk62062992014-05-06 09:55:28 -0400647 null, //POST_NOTIFICATION
648 null, //NEIGHBORING_CELLS
649 null, //CALL_PHONE
Amith Yamasani41c1ded2014-08-05 11:15:05 -0700650 UserManager.DISALLOW_SMS, //READ_SMS
651 UserManager.DISALLOW_SMS, //WRITE_SMS
652 UserManager.DISALLOW_SMS, //RECEIVE_SMS
653 null, //RECEIVE_EMERGENCY_SMS
654 UserManager.DISALLOW_SMS, //RECEIVE_MMS
Jason Monk62062992014-05-06 09:55:28 -0400655 null, //RECEIVE_WAP_PUSH
Amith Yamasani41c1ded2014-08-05 11:15:05 -0700656 UserManager.DISALLOW_SMS, //SEND_SMS
657 UserManager.DISALLOW_SMS, //READ_ICC_SMS
658 UserManager.DISALLOW_SMS, //WRITE_ICC_SMS
Jason Monk62062992014-05-06 09:55:28 -0400659 null, //WRITE_SETTINGS
Jason Monk1c7c3192014-06-26 12:52:18 -0400660 UserManager.DISALLOW_CREATE_WINDOWS, //SYSTEM_ALERT_WINDOW
Jason Monk62062992014-05-06 09:55:28 -0400661 null, //ACCESS_NOTIFICATIONS
Makoto Onuki759a7632015-10-28 16:43:10 -0700662 UserManager.DISALLOW_CAMERA, //CAMERA
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700663 UserManager.DISALLOW_RECORD_AUDIO, //RECORD_AUDIO
Jason Monk62062992014-05-06 09:55:28 -0400664 null, //PLAY_AUDIO
665 null, //READ_CLIPBOARD
666 null, //WRITE_CLIPBOARD
667 null, //TAKE_MEDIA_BUTTONS
668 null, //TAKE_AUDIO_FOCUS
Emily Bernier45775c42014-05-16 15:12:04 -0400669 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_MASTER_VOLUME
670 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_VOICE_VOLUME
671 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_RING_VOLUME
672 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_MEDIA_VOLUME
673 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_ALARM_VOLUME
674 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_NOTIFICATION_VOLUME
675 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_BLUETOOTH_VOLUME
Jason Monk62062992014-05-06 09:55:28 -0400676 null, //WAKE_LOCK
Julia Reynolds9854d572014-07-02 14:46:02 -0400677 UserManager.DISALLOW_SHARE_LOCATION, //MONITOR_LOCATION
678 UserManager.DISALLOW_SHARE_LOCATION, //MONITOR_HIGH_POWER_LOCATION
Jason Monk62062992014-05-06 09:55:28 -0400679 null, //GET_USAGE_STATS
Emily Bernier22c921a2014-05-28 11:01:32 -0400680 UserManager.DISALLOW_UNMUTE_MICROPHONE, // MUTE_MICROPHONE
Jason Monk1c7c3192014-06-26 12:52:18 -0400681 UserManager.DISALLOW_CREATE_WINDOWS, // TOAST_WINDOW
Michael Wrightc39d47a2014-07-08 18:07:36 -0700682 null, //PROJECT_MEDIA
Jeff Davidson05542602014-08-11 14:07:27 -0700683 UserManager.DISALLOW_CONFIG_VPN, // ACTIVATE_VPN
Benjamin Franzf3ece362015-02-11 10:51:10 +0000684 UserManager.DISALLOW_WALLPAPER, // WRITE_WALLPAPER
Dianne Hackbornd59a5d52015-04-04 14:52:14 -0700685 null, // ASSIST_STRUCTURE
686 null, // ASSIST_SCREENSHOT
Svet Ganovc3300092015-04-17 09:07:22 -0700687 null, // READ_PHONE_STATE
Svetoslav5335b672015-04-29 12:00:51 -0700688 null, // ADD_VOICEMAIL
Svetoslavc656e6f2015-04-29 14:08:16 -0700689 null, // USE_SIP
Svetoslav4af76a52015-04-29 15:29:46 -0700690 null, // PROCESS_OUTGOING_CALLS
Svet Ganovb9d71a62015-04-30 10:38:13 -0700691 null, // USE_FINGERPRINT
Svet Ganovede43162015-05-02 17:42:44 -0700692 null, // BODY_SENSORS
Svet Ganovf7e9cf42015-05-13 10:40:31 -0700693 null, // READ_CELL_BROADCASTS
Svet Ganov921c7df2015-06-29 21:51:41 -0700694 null, // MOCK_LOCATION
695 null, // READ_EXTERNAL_STORAGE
Dianne Hackborn280a64e2015-07-13 14:48:08 -0700696 null, // WRITE_EXTERNAL_STORAGE
697 null, // TURN_ON_SCREEN
Svetoslavf3f02ac2015-09-08 14:36:35 -0700698 null, // GET_ACCOUNTS
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700699 null, // RUN_IN_BACKGROUND
Jason Monk1c7c3192014-06-26 12:52:18 -0400700 };
701
702 /**
703 * This specifies whether each option should allow the system
704 * (and system ui) to bypass the user restriction when active.
705 */
706 private static boolean[] sOpAllowSystemRestrictionBypass = new boolean[] {
Fyodor Kupolov639e73d2016-02-25 11:58:21 -0800707 true, //COARSE_LOCATION
708 true, //FINE_LOCATION
Jason Monk1c7c3192014-06-26 12:52:18 -0400709 false, //GPS
710 false, //VIBRATE
711 false, //READ_CONTACTS
712 false, //WRITE_CONTACTS
713 false, //READ_CALL_LOG
714 false, //WRITE_CALL_LOG
715 false, //READ_CALENDAR
716 false, //WRITE_CALENDAR
Julia Reynolds9854d572014-07-02 14:46:02 -0400717 true, //WIFI_SCAN
Jason Monk1c7c3192014-06-26 12:52:18 -0400718 false, //POST_NOTIFICATION
719 false, //NEIGHBORING_CELLS
720 false, //CALL_PHONE
721 false, //READ_SMS
722 false, //WRITE_SMS
723 false, //RECEIVE_SMS
724 false, //RECEIVE_EMERGECY_SMS
725 false, //RECEIVE_MMS
726 false, //RECEIVE_WAP_PUSH
727 false, //SEND_SMS
728 false, //READ_ICC_SMS
729 false, //WRITE_ICC_SMS
730 false, //WRITE_SETTINGS
731 true, //SYSTEM_ALERT_WINDOW
732 false, //ACCESS_NOTIFICATIONS
733 false, //CAMERA
734 false, //RECORD_AUDIO
735 false, //PLAY_AUDIO
736 false, //READ_CLIPBOARD
737 false, //WRITE_CLIPBOARD
738 false, //TAKE_MEDIA_BUTTONS
739 false, //TAKE_AUDIO_FOCUS
740 false, //AUDIO_MASTER_VOLUME
741 false, //AUDIO_VOICE_VOLUME
742 false, //AUDIO_RING_VOLUME
743 false, //AUDIO_MEDIA_VOLUME
744 false, //AUDIO_ALARM_VOLUME
745 false, //AUDIO_NOTIFICATION_VOLUME
746 false, //AUDIO_BLUETOOTH_VOLUME
747 false, //WAKE_LOCK
748 false, //MONITOR_LOCATION
749 false, //MONITOR_HIGH_POWER_LOCATION
750 false, //GET_USAGE_STATS
Michael Wrightc39d47a2014-07-08 18:07:36 -0700751 false, //MUTE_MICROPHONE
752 true, //TOAST_WINDOW
753 false, //PROJECT_MEDIA
Jeff Davidson05542602014-08-11 14:07:27 -0700754 false, //ACTIVATE_VPN
Benjamin Franzf3ece362015-02-11 10:51:10 +0000755 false, //WALLPAPER
Dianne Hackbornd59a5d52015-04-04 14:52:14 -0700756 false, //ASSIST_STRUCTURE
757 false, //ASSIST_SCREENSHOT
Svet Ganov16a16892015-04-16 10:32:04 -0700758 false, //READ_PHONE_STATE
Svetoslav5335b672015-04-29 12:00:51 -0700759 false, //ADD_VOICEMAIL
Svetoslavc656e6f2015-04-29 14:08:16 -0700760 false, // USE_SIP
Svetoslav4af76a52015-04-29 15:29:46 -0700761 false, // PROCESS_OUTGOING_CALLS
Svet Ganovb9d71a62015-04-30 10:38:13 -0700762 false, // USE_FINGERPRINT
Svet Ganovede43162015-05-02 17:42:44 -0700763 false, // BODY_SENSORS
Svet Ganovf7e9cf42015-05-13 10:40:31 -0700764 false, // READ_CELL_BROADCASTS
Svet Ganov921c7df2015-06-29 21:51:41 -0700765 false, // MOCK_LOCATION
766 false, // READ_EXTERNAL_STORAGE
Dianne Hackborn280a64e2015-07-13 14:48:08 -0700767 false, // WRITE_EXTERNAL_STORAGE
768 false, // TURN_ON_SCREEN
Svetoslavf3f02ac2015-09-08 14:36:35 -0700769 false, // GET_ACCOUNTS
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700770 false, // RUN_IN_BACKGROUND
Jason Monk62062992014-05-06 09:55:28 -0400771 };
772
773 /**
David Braunf5d83192013-09-16 13:43:51 -0700774 * This specifies the default mode for each operation.
775 */
776 private static int[] sOpDefaultMode = new int[] {
777 AppOpsManager.MODE_ALLOWED,
778 AppOpsManager.MODE_ALLOWED,
779 AppOpsManager.MODE_ALLOWED,
780 AppOpsManager.MODE_ALLOWED,
781 AppOpsManager.MODE_ALLOWED,
782 AppOpsManager.MODE_ALLOWED,
783 AppOpsManager.MODE_ALLOWED,
784 AppOpsManager.MODE_ALLOWED,
785 AppOpsManager.MODE_ALLOWED,
786 AppOpsManager.MODE_ALLOWED,
787 AppOpsManager.MODE_ALLOWED,
788 AppOpsManager.MODE_ALLOWED,
789 AppOpsManager.MODE_ALLOWED,
790 AppOpsManager.MODE_ALLOWED,
791 AppOpsManager.MODE_ALLOWED,
792 AppOpsManager.MODE_IGNORED, // OP_WRITE_SMS
793 AppOpsManager.MODE_ALLOWED,
794 AppOpsManager.MODE_ALLOWED,
795 AppOpsManager.MODE_ALLOWED,
796 AppOpsManager.MODE_ALLOWED,
797 AppOpsManager.MODE_ALLOWED,
798 AppOpsManager.MODE_ALLOWED,
799 AppOpsManager.MODE_ALLOWED,
Billy Lau6ad2d662015-07-18 00:26:58 +0100800 AppOpsManager.MODE_DEFAULT, // OP_WRITE_SETTINGS
Billy Lau060275f2015-07-15 22:29:19 +0100801 AppOpsManager.MODE_DEFAULT, // OP_SYSTEM_ALERT_WINDOW
David Braunf5d83192013-09-16 13:43:51 -0700802 AppOpsManager.MODE_ALLOWED,
803 AppOpsManager.MODE_ALLOWED,
804 AppOpsManager.MODE_ALLOWED,
805 AppOpsManager.MODE_ALLOWED,
806 AppOpsManager.MODE_ALLOWED,
807 AppOpsManager.MODE_ALLOWED,
808 AppOpsManager.MODE_ALLOWED,
809 AppOpsManager.MODE_ALLOWED,
810 AppOpsManager.MODE_ALLOWED,
811 AppOpsManager.MODE_ALLOWED,
812 AppOpsManager.MODE_ALLOWED,
813 AppOpsManager.MODE_ALLOWED,
814 AppOpsManager.MODE_ALLOWED,
815 AppOpsManager.MODE_ALLOWED,
816 AppOpsManager.MODE_ALLOWED,
817 AppOpsManager.MODE_ALLOWED,
818 AppOpsManager.MODE_ALLOWED,
819 AppOpsManager.MODE_ALLOWED,
Dianne Hackborn33f5ddd2014-07-21 15:35:45 -0700820 AppOpsManager.MODE_DEFAULT, // OP_GET_USAGE_STATS
Emily Bernier22c921a2014-05-28 11:01:32 -0400821 AppOpsManager.MODE_ALLOWED,
Jason Monk1c7c3192014-06-26 12:52:18 -0400822 AppOpsManager.MODE_ALLOWED,
Michael Wrightc39d47a2014-07-08 18:07:36 -0700823 AppOpsManager.MODE_IGNORED, // OP_PROJECT_MEDIA
Jeff Davidson05542602014-08-11 14:07:27 -0700824 AppOpsManager.MODE_IGNORED, // OP_ACTIVATE_VPN
Benjamin Franzf3ece362015-02-11 10:51:10 +0000825 AppOpsManager.MODE_ALLOWED,
Dianne Hackbornd59a5d52015-04-04 14:52:14 -0700826 AppOpsManager.MODE_ALLOWED,
827 AppOpsManager.MODE_ALLOWED,
Svet Ganovc3300092015-04-17 09:07:22 -0700828 AppOpsManager.MODE_ALLOWED,
Svetoslav5335b672015-04-29 12:00:51 -0700829 AppOpsManager.MODE_ALLOWED,
Svetoslavc656e6f2015-04-29 14:08:16 -0700830 AppOpsManager.MODE_ALLOWED,
Svetoslav4af76a52015-04-29 15:29:46 -0700831 AppOpsManager.MODE_ALLOWED,
Svet Ganovb9d71a62015-04-30 10:38:13 -0700832 AppOpsManager.MODE_ALLOWED,
Svet Ganovede43162015-05-02 17:42:44 -0700833 AppOpsManager.MODE_ALLOWED,
Svet Ganovf7e9cf42015-05-13 10:40:31 -0700834 AppOpsManager.MODE_ALLOWED,
Svet Ganov921c7df2015-06-29 21:51:41 -0700835 AppOpsManager.MODE_ERRORED, // OP_MOCK_LOCATION
836 AppOpsManager.MODE_ALLOWED,
Dianne Hackborn280a64e2015-07-13 14:48:08 -0700837 AppOpsManager.MODE_ALLOWED,
838 AppOpsManager.MODE_ALLOWED, // OP_TURN_ON_SCREEN
Svetoslavf3f02ac2015-09-08 14:36:35 -0700839 AppOpsManager.MODE_ALLOWED,
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700840 AppOpsManager.MODE_ALLOWED, // OP_RUN_IN_BACKGROUND
David Braunf5d83192013-09-16 13:43:51 -0700841 };
842
Dianne Hackborn8828d3a2013-09-25 16:47:10 -0700843 /**
844 * This specifies whether each option is allowed to be reset
845 * when resetting all app preferences. Disable reset for
846 * app ops that are under strong control of some part of the
847 * system (such as OP_WRITE_SMS, which should be allowed only
848 * for whichever app is selected as the current SMS app).
849 */
850 private static boolean[] sOpDisableReset = new boolean[] {
851 false,
852 false,
853 false,
854 false,
855 false,
856 false,
857 false,
858 false,
859 false,
860 false,
861 false,
862 false,
863 false,
864 false,
865 false,
866 true, // OP_WRITE_SMS
867 false,
868 false,
869 false,
870 false,
871 false,
872 false,
873 false,
874 false,
875 false,
876 false,
877 false,
878 false,
879 false,
880 false,
881 false,
882 false,
883 false,
884 false,
885 false,
886 false,
887 false,
888 false,
889 false,
890 false,
891 false,
892 false,
893 false,
Dianne Hackborne22b3b12014-05-07 18:06:44 -0700894 false,
Emily Bernier22c921a2014-05-28 11:01:32 -0400895 false,
Jason Monk1c7c3192014-06-26 12:52:18 -0400896 false,
Michael Wrightc39d47a2014-07-08 18:07:36 -0700897 false,
Jeff Davidson05542602014-08-11 14:07:27 -0700898 false,
Benjamin Franzf3ece362015-02-11 10:51:10 +0000899 false,
Dianne Hackbornd59a5d52015-04-04 14:52:14 -0700900 false,
901 false,
Svet Ganovc3300092015-04-17 09:07:22 -0700902 false,
Svetoslav5335b672015-04-29 12:00:51 -0700903 false,
Svetoslavc656e6f2015-04-29 14:08:16 -0700904 false,
Svetoslav4af76a52015-04-29 15:29:46 -0700905 false,
Svet Ganovb9d71a62015-04-30 10:38:13 -0700906 false,
Svet Ganovede43162015-05-02 17:42:44 -0700907 false,
Svet Ganovf7e9cf42015-05-13 10:40:31 -0700908 false,
Svet Ganov921c7df2015-06-29 21:51:41 -0700909 false,
910 false,
Dianne Hackborn280a64e2015-07-13 14:48:08 -0700911 false,
912 false,
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700913 false,
914 false,
Dianne Hackborn8828d3a2013-09-25 16:47:10 -0700915 };
916
Svet Ganovfbf01f72015-04-28 18:39:06 -0700917 /**
Svet Ganovb9d71a62015-04-30 10:38:13 -0700918 * Mapping from an app op name to the app op code.
Svet Ganovfbf01f72015-04-28 18:39:06 -0700919 */
Svet Ganovb9d71a62015-04-30 10:38:13 -0700920 private static HashMap<String, Integer> sOpStrToOp = new HashMap<>();
Svet Ganovfbf01f72015-04-28 18:39:06 -0700921
Svet Ganovb9d71a62015-04-30 10:38:13 -0700922 /**
923 * Mapping from a permission to the corresponding app op.
924 */
925 private static HashMap<String, Integer> sPermToOp = new HashMap<>();
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700926
927 static {
928 if (sOpToSwitch.length != _NUM_OP) {
Dianne Hackborn8828d3a2013-09-25 16:47:10 -0700929 throw new IllegalStateException("sOpToSwitch length " + sOpToSwitch.length
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700930 + " should be " + _NUM_OP);
931 }
932 if (sOpToString.length != _NUM_OP) {
Dianne Hackborn8828d3a2013-09-25 16:47:10 -0700933 throw new IllegalStateException("sOpToString length " + sOpToString.length
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700934 + " should be " + _NUM_OP);
935 }
936 if (sOpNames.length != _NUM_OP) {
Dianne Hackborn8828d3a2013-09-25 16:47:10 -0700937 throw new IllegalStateException("sOpNames length " + sOpNames.length
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700938 + " should be " + _NUM_OP);
939 }
940 if (sOpPerms.length != _NUM_OP) {
Dianne Hackborn8828d3a2013-09-25 16:47:10 -0700941 throw new IllegalStateException("sOpPerms length " + sOpPerms.length
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700942 + " should be " + _NUM_OP);
943 }
944 if (sOpDefaultMode.length != _NUM_OP) {
Dianne Hackborn8828d3a2013-09-25 16:47:10 -0700945 throw new IllegalStateException("sOpDefaultMode length " + sOpDefaultMode.length
946 + " should be " + _NUM_OP);
947 }
948 if (sOpDisableReset.length != _NUM_OP) {
949 throw new IllegalStateException("sOpDisableReset length " + sOpDisableReset.length
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700950 + " should be " + _NUM_OP);
951 }
Jason Monk62062992014-05-06 09:55:28 -0400952 if (sOpRestrictions.length != _NUM_OP) {
953 throw new IllegalStateException("sOpRestrictions length " + sOpRestrictions.length
954 + " should be " + _NUM_OP);
955 }
Jason Monk1c7c3192014-06-26 12:52:18 -0400956 if (sOpAllowSystemRestrictionBypass.length != _NUM_OP) {
957 throw new IllegalStateException("sOpAllowSYstemRestrictionsBypass length "
958 + sOpRestrictions.length + " should be " + _NUM_OP);
959 }
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700960 for (int i=0; i<_NUM_OP; i++) {
961 if (sOpToString[i] != null) {
962 sOpStrToOp.put(sOpToString[i], i);
963 }
964 }
Svet Ganovb9d71a62015-04-30 10:38:13 -0700965 for (int i=0; i<_NUM_OP; i++) {
966 if (sOpPerms[i] != null) {
967 sPermToOp.put(sOpPerms[i], i);
968 }
969 }
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700970 }
971
David Braunf5d83192013-09-16 13:43:51 -0700972 /**
Dianne Hackbornd7d28e62013-02-12 14:59:53 -0800973 * Retrieve the op switch that controls the given operation.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -0700974 * @hide
Dianne Hackbornd7d28e62013-02-12 14:59:53 -0800975 */
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800976 public static int opToSwitch(int op) {
977 return sOpToSwitch[op];
978 }
979
Dianne Hackbornd7d28e62013-02-12 14:59:53 -0800980 /**
981 * Retrieve a non-localized name for the operation, for debugging output.
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -0700982 * @hide
Dianne Hackbornd7d28e62013-02-12 14:59:53 -0800983 */
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800984 public static String opToName(int op) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800985 if (op == OP_NONE) return "NONE";
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800986 return op < sOpNames.length ? sOpNames[op] : ("Unknown(" + op + ")");
987 }
988
Dianne Hackbornd7d28e62013-02-12 14:59:53 -0800989 /**
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800990 * @hide
991 */
992 public static int strDebugOpToOp(String op) {
993 for (int i=0; i<sOpNames.length; i++) {
994 if (sOpNames[i].equals(op)) {
995 return i;
996 }
997 }
998 throw new IllegalArgumentException("Unknown operation string: " + op);
999 }
1000
1001 /**
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08001002 * Retrieve the permission associated with an operation, or null if there is not one.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001003 * @hide
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08001004 */
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001005 public static String opToPermission(int op) {
1006 return sOpPerms[op];
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001007 }
1008
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08001009 /**
Jason Monk62062992014-05-06 09:55:28 -04001010 * Retrieve the user restriction associated with an operation, or null if there is not one.
1011 * @hide
1012 */
1013 public static String opToRestriction(int op) {
1014 return sOpRestrictions[op];
1015 }
1016
1017 /**
Svet Ganovb9d71a62015-04-30 10:38:13 -07001018 * Retrieve the app op code for a permission, or null if there is not one.
1019 * @hide
1020 */
1021 public static int permissionToOpCode(String permission) {
Svet Ganov019d2302015-05-04 11:07:38 -07001022 Integer boxedOpCode = sPermToOp.get(permission);
1023 return boxedOpCode != null ? boxedOpCode : OP_NONE;
Svet Ganovb9d71a62015-04-30 10:38:13 -07001024 }
1025
1026 /**
Jason Monk1c7c3192014-06-26 12:52:18 -04001027 * Retrieve whether the op allows the system (and system ui) to
1028 * bypass the user restriction.
1029 * @hide
1030 */
1031 public static boolean opAllowSystemBypassRestriction(int op) {
1032 return sOpAllowSystemRestrictionBypass[op];
1033 }
1034
1035 /**
David Braunf5d83192013-09-16 13:43:51 -07001036 * Retrieve the default mode for the operation.
1037 * @hide
1038 */
1039 public static int opToDefaultMode(int op) {
1040 return sOpDefaultMode[op];
1041 }
1042
1043 /**
Dianne Hackborn8828d3a2013-09-25 16:47:10 -07001044 * Retrieve whether the op allows itself to be reset.
1045 * @hide
1046 */
1047 public static boolean opAllowsReset(int op) {
1048 return !sOpDisableReset[op];
1049 }
1050
1051 /**
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08001052 * Class holding all of the operation information associated with an app.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001053 * @hide
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08001054 */
Dianne Hackborn35654b62013-01-14 17:38:02 -08001055 public static class PackageOps implements Parcelable {
1056 private final String mPackageName;
1057 private final int mUid;
1058 private final List<OpEntry> mEntries;
1059
1060 public PackageOps(String packageName, int uid, List<OpEntry> entries) {
1061 mPackageName = packageName;
1062 mUid = uid;
1063 mEntries = entries;
1064 }
1065
1066 public String getPackageName() {
1067 return mPackageName;
1068 }
1069
1070 public int getUid() {
1071 return mUid;
1072 }
1073
1074 public List<OpEntry> getOps() {
1075 return mEntries;
1076 }
1077
1078 @Override
1079 public int describeContents() {
1080 return 0;
1081 }
1082
1083 @Override
1084 public void writeToParcel(Parcel dest, int flags) {
1085 dest.writeString(mPackageName);
1086 dest.writeInt(mUid);
1087 dest.writeInt(mEntries.size());
1088 for (int i=0; i<mEntries.size(); i++) {
1089 mEntries.get(i).writeToParcel(dest, flags);
1090 }
1091 }
1092
1093 PackageOps(Parcel source) {
1094 mPackageName = source.readString();
1095 mUid = source.readInt();
1096 mEntries = new ArrayList<OpEntry>();
1097 final int N = source.readInt();
1098 for (int i=0; i<N; i++) {
1099 mEntries.add(OpEntry.CREATOR.createFromParcel(source));
1100 }
1101 }
1102
1103 public static final Creator<PackageOps> CREATOR = new Creator<PackageOps>() {
1104 @Override public PackageOps createFromParcel(Parcel source) {
1105 return new PackageOps(source);
1106 }
1107
1108 @Override public PackageOps[] newArray(int size) {
1109 return new PackageOps[size];
1110 }
1111 };
1112 }
1113
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08001114 /**
1115 * Class holding the information about one unique operation of an application.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001116 * @hide
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08001117 */
Dianne Hackborn35654b62013-01-14 17:38:02 -08001118 public static class OpEntry implements Parcelable {
1119 private final int mOp;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001120 private final int mMode;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001121 private final long mTime;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001122 private final long mRejectTime;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001123 private final int mDuration;
Svet Ganov99b60432015-06-27 13:15:22 -07001124 private final int mProxyUid;
1125 private final String mProxyPackageName;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001126
Svet Ganov99b60432015-06-27 13:15:22 -07001127 public OpEntry(int op, int mode, long time, long rejectTime, int duration,
1128 int proxyUid, String proxyPackage) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001129 mOp = op;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001130 mMode = mode;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001131 mTime = time;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001132 mRejectTime = rejectTime;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001133 mDuration = duration;
Svet Ganov99b60432015-06-27 13:15:22 -07001134 mProxyUid = proxyUid;
1135 mProxyPackageName = proxyPackage;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001136 }
1137
1138 public int getOp() {
1139 return mOp;
1140 }
1141
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001142 public int getMode() {
1143 return mMode;
1144 }
1145
Dianne Hackborn35654b62013-01-14 17:38:02 -08001146 public long getTime() {
1147 return mTime;
1148 }
1149
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001150 public long getRejectTime() {
1151 return mRejectTime;
1152 }
1153
Dianne Hackborn35654b62013-01-14 17:38:02 -08001154 public boolean isRunning() {
1155 return mDuration == -1;
1156 }
1157
1158 public int getDuration() {
1159 return mDuration == -1 ? (int)(System.currentTimeMillis()-mTime) : mDuration;
1160 }
1161
Svet Ganov99b60432015-06-27 13:15:22 -07001162 public int getProxyUid() {
1163 return mProxyUid;
1164 }
1165
1166 public String getProxyPackageName() {
1167 return mProxyPackageName;
1168 }
1169
Dianne Hackborn35654b62013-01-14 17:38:02 -08001170 @Override
1171 public int describeContents() {
1172 return 0;
1173 }
1174
1175 @Override
1176 public void writeToParcel(Parcel dest, int flags) {
1177 dest.writeInt(mOp);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001178 dest.writeInt(mMode);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001179 dest.writeLong(mTime);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001180 dest.writeLong(mRejectTime);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001181 dest.writeInt(mDuration);
Svet Ganov99b60432015-06-27 13:15:22 -07001182 dest.writeInt(mProxyUid);
1183 dest.writeString(mProxyPackageName);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001184 }
1185
1186 OpEntry(Parcel source) {
1187 mOp = source.readInt();
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001188 mMode = source.readInt();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001189 mTime = source.readLong();
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001190 mRejectTime = source.readLong();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001191 mDuration = source.readInt();
Svet Ganov99b60432015-06-27 13:15:22 -07001192 mProxyUid = source.readInt();
1193 mProxyPackageName = source.readString();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001194 }
1195
1196 public static final Creator<OpEntry> CREATOR = new Creator<OpEntry>() {
1197 @Override public OpEntry createFromParcel(Parcel source) {
1198 return new OpEntry(source);
1199 }
1200
1201 @Override public OpEntry[] newArray(int size) {
1202 return new OpEntry[size];
1203 }
1204 };
1205 }
1206
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08001207 /**
1208 * Callback for notification of changes to operation state.
1209 */
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001210 public interface OnOpChangedListener {
1211 public void onOpChanged(String op, String packageName);
1212 }
1213
1214 /**
1215 * Callback for notification of changes to operation state.
1216 * This allows you to see the raw op codes instead of strings.
1217 * @hide
1218 */
1219 public static class OnOpChangedInternalListener implements OnOpChangedListener {
1220 public void onOpChanged(String op, String packageName) { }
1221 public void onOpChanged(int op, String packageName) { }
Dianne Hackbornc2293022013-02-06 23:14:49 -08001222 }
1223
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001224 AppOpsManager(Context context, IAppOpsService service) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001225 mContext = context;
1226 mService = service;
1227 }
1228
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08001229 /**
1230 * Retrieve current operation state for all applications.
1231 *
1232 * @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 -07001233 * @hide
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08001234 */
Dianne Hackborn35654b62013-01-14 17:38:02 -08001235 public List<AppOpsManager.PackageOps> getPackagesForOps(int[] ops) {
1236 try {
1237 return mService.getPackagesForOps(ops);
1238 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001239 throw e.rethrowFromSystemServer();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001240 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001241 }
1242
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08001243 /**
1244 * Retrieve current operation state for one application.
1245 *
1246 * @param uid The uid of the application of interest.
1247 * @param packageName The name of the application of interest.
1248 * @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 -07001249 * @hide
Dianne Hackbornd7d28e62013-02-12 14:59:53 -08001250 */
Dianne Hackborn72e39832013-01-18 18:36:09 -08001251 public List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName, int[] ops) {
1252 try {
1253 return mService.getOpsForPackage(uid, packageName, ops);
1254 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001255 throw e.rethrowFromSystemServer();
Dianne Hackborn72e39832013-01-18 18:36:09 -08001256 }
Dianne Hackborn72e39832013-01-18 18:36:09 -08001257 }
1258
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001259 /** @hide */
Svet Ganov2af57082015-07-30 08:44:20 -07001260 public void setUidMode(int code, int uid, int mode) {
1261 try {
1262 mService.setUidMode(code, uid, mode);
1263 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001264 throw e.rethrowFromSystemServer();
Svet Ganov2af57082015-07-30 08:44:20 -07001265 }
1266 }
1267
1268 /** @hide */
Svet Ganov9cea80cd2016-02-16 11:47:00 -08001269 public void setUserRestriction(int code, boolean restricted, IBinder token) {
Ruben Brunk29931bc2016-03-11 00:24:26 -08001270 setUserRestriction(code, restricted, token, /*exceptionPackages*/null);
1271 }
1272
1273 /** @hide */
1274 public void setUserRestriction(int code, boolean restricted, IBinder token,
1275 String[] exceptionPackages) {
Svet Ganov9cea80cd2016-02-16 11:47:00 -08001276 try {
Ruben Brunk29931bc2016-03-11 00:24:26 -08001277 mService.setUserRestriction(code, restricted, token, mContext.getUserId(),
1278 exceptionPackages);
Svet Ganov9cea80cd2016-02-16 11:47:00 -08001279 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001280 throw e.rethrowFromSystemServer();
Svet Ganov9cea80cd2016-02-16 11:47:00 -08001281 }
1282 }
1283
1284 /** @hide */
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001285 public void setMode(int code, int uid, String packageName, int mode) {
1286 try {
1287 mService.setMode(code, uid, packageName, mode);
1288 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001289 throw e.rethrowFromSystemServer();
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001290 }
1291 }
1292
John Spurlock1af30c72014-03-10 08:33:35 -04001293 /**
1294 * Set a non-persisted restriction on an audio operation at a stream-level.
1295 * Restrictions are temporary additional constraints imposed on top of the persisted rules
1296 * defined by {@link #setMode}.
1297 *
1298 * @param code The operation to restrict.
John Spurlock7b414672014-07-18 13:02:39 -04001299 * @param usage The {@link android.media.AudioAttributes} usage value.
John Spurlock1af30c72014-03-10 08:33:35 -04001300 * @param mode The restriction mode (MODE_IGNORED,MODE_ERRORED) or MODE_ALLOWED to unrestrict.
1301 * @param exceptionPackages Optional list of packages to exclude from the restriction.
1302 * @hide
1303 */
John Spurlock7b414672014-07-18 13:02:39 -04001304 public void setRestriction(int code, @AttributeUsage int usage, int mode,
1305 String[] exceptionPackages) {
John Spurlock1af30c72014-03-10 08:33:35 -04001306 try {
1307 final int uid = Binder.getCallingUid();
John Spurlock7b414672014-07-18 13:02:39 -04001308 mService.setAudioRestriction(code, usage, uid, mode, exceptionPackages);
John Spurlock1af30c72014-03-10 08:33:35 -04001309 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001310 throw e.rethrowFromSystemServer();
John Spurlock1af30c72014-03-10 08:33:35 -04001311 }
1312 }
1313
Dianne Hackborn607b4142013-08-02 18:10:10 -07001314 /** @hide */
1315 public void resetAllModes() {
1316 try {
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -08001317 mService.resetAllModes(UserHandle.myUserId(), null);
Dianne Hackborn607b4142013-08-02 18:10:10 -07001318 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001319 throw e.rethrowFromSystemServer();
Dianne Hackborn607b4142013-08-02 18:10:10 -07001320 }
1321 }
1322
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001323 /**
Svet Ganovfbf01f72015-04-28 18:39:06 -07001324 * Gets the app op name associated with a given permission.
1325 * The app op name is one of the public constants defined
1326 * in this class such as {@link #OPSTR_COARSE_LOCATION}.
1327 *
1328 * @param permission The permission.
1329 * @return The app op associated with the permission or null.
Svet Ganovfbf01f72015-04-28 18:39:06 -07001330 */
Svet Ganovfbf01f72015-04-28 18:39:06 -07001331 public static String permissionToOp(String permission) {
Svet Ganovb9d71a62015-04-30 10:38:13 -07001332 final Integer opCode = sPermToOp.get(permission);
1333 if (opCode == null) {
1334 return null;
1335 }
1336 return sOpToString[opCode];
Svet Ganovfbf01f72015-04-28 18:39:06 -07001337 }
1338
1339 /**
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001340 * Monitor for changes to the operating mode for the given op in the given app package.
Dianne Hackborne4cb66f2013-10-02 10:34:02 -07001341 * @param op The operation to monitor, one of OPSTR_*.
1342 * @param packageName The name of the application to monitor.
1343 * @param callback Where to report changes.
1344 */
1345 public void startWatchingMode(String op, String packageName,
1346 final OnOpChangedListener callback) {
1347 startWatchingMode(strOpToOp(op), packageName, callback);
1348 }
1349
1350 /**
1351 * Monitor for changes to the operating mode for the given op in the given app package.
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001352 * @param op The operation to monitor, one of OP_*.
1353 * @param packageName The name of the application to monitor.
1354 * @param callback Where to report changes.
Dianne Hackborne4cb66f2013-10-02 10:34:02 -07001355 * @hide
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001356 */
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001357 public void startWatchingMode(int op, String packageName, final OnOpChangedListener callback) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08001358 synchronized (mModeWatchers) {
1359 IAppOpsCallback cb = mModeWatchers.get(callback);
1360 if (cb == null) {
1361 cb = new IAppOpsCallback.Stub() {
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07001362 public void opChanged(int op, int uid, String packageName) {
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001363 if (callback instanceof OnOpChangedInternalListener) {
1364 ((OnOpChangedInternalListener)callback).onOpChanged(op, packageName);
1365 }
1366 if (sOpToString[op] != null) {
1367 callback.onOpChanged(sOpToString[op], packageName);
1368 }
Dianne Hackbornc2293022013-02-06 23:14:49 -08001369 }
1370 };
1371 mModeWatchers.put(callback, cb);
1372 }
1373 try {
1374 mService.startWatchingMode(op, packageName, cb);
1375 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001376 throw e.rethrowFromSystemServer();
Dianne Hackbornc2293022013-02-06 23:14:49 -08001377 }
1378 }
1379 }
1380
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001381 /**
1382 * Stop monitoring that was previously started with {@link #startWatchingMode}. All
1383 * monitoring associated with this callback will be removed.
1384 */
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001385 public void stopWatchingMode(OnOpChangedListener callback) {
Dianne Hackbornc2293022013-02-06 23:14:49 -08001386 synchronized (mModeWatchers) {
1387 IAppOpsCallback cb = mModeWatchers.get(callback);
1388 if (cb != null) {
1389 try {
1390 mService.stopWatchingMode(cb);
1391 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001392 throw e.rethrowFromSystemServer();
Dianne Hackbornc2293022013-02-06 23:14:49 -08001393 }
1394 }
1395 }
1396 }
1397
Dianne Hackborn95d78532013-09-11 09:51:14 -07001398 private String buildSecurityExceptionMsg(int op, int uid, String packageName) {
1399 return packageName + " from uid " + uid + " not allowed to perform " + sOpNames[op];
1400 }
1401
Adam Lesinskib5cf61b2014-08-18 16:10:28 -07001402 /**
1403 * {@hide}
1404 */
1405 public static int strOpToOp(String op) {
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001406 Integer val = sOpStrToOp.get(op);
1407 if (val == null) {
1408 throw new IllegalArgumentException("Unknown operation string: " + op);
1409 }
1410 return val;
1411 }
1412
1413 /**
1414 * Do a quick check for whether an application might be able to perform an operation.
1415 * This is <em>not</em> a security check; you must use {@link #noteOp(String, int, String)}
1416 * or {@link #startOp(String, int, String)} for your actual security checks, which also
1417 * ensure that the given uid and package name are consistent. This function can just be
1418 * used for a quick check to see if an operation has been disabled for the application,
1419 * as an early reject of some work. This does not modify the time stamp or other data
1420 * about the operation.
1421 * @param op The operation to check. One of the OPSTR_* constants.
1422 * @param uid The user id of the application attempting to perform the operation.
1423 * @param packageName The name of the application attempting to perform the operation.
1424 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
1425 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
1426 * causing the app to crash).
1427 * @throws SecurityException If the app has been configured to crash on this op.
1428 */
1429 public int checkOp(String op, int uid, String packageName) {
1430 return checkOp(strOpToOp(op), uid, packageName);
1431 }
1432
1433 /**
John Spurlock925b85e2014-03-10 16:52:11 -04001434 * Like {@link #checkOp} but instead of throwing a {@link SecurityException} it
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001435 * returns {@link #MODE_ERRORED}.
1436 */
1437 public int checkOpNoThrow(String op, int uid, String packageName) {
1438 return checkOpNoThrow(strOpToOp(op), uid, packageName);
1439 }
1440
1441 /**
1442 * Make note of an application performing an operation. Note that you must pass
1443 * in both the uid and name of the application to be checked; this function will verify
1444 * that these two match, and if not, return {@link #MODE_IGNORED}. If this call
1445 * succeeds, the last execution time of the operation for this app will be updated to
1446 * the current time.
1447 * @param op The operation to note. One of the OPSTR_* constants.
1448 * @param uid The user id of the application attempting to perform the operation.
1449 * @param packageName The name of the application attempting to perform the operation.
1450 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
1451 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
1452 * causing the app to crash).
1453 * @throws SecurityException If the app has been configured to crash on this op.
1454 */
1455 public int noteOp(String op, int uid, String packageName) {
1456 return noteOp(strOpToOp(op), uid, packageName);
1457 }
1458
1459 /**
1460 * Like {@link #noteOp} but instead of throwing a {@link SecurityException} it
1461 * returns {@link #MODE_ERRORED}.
1462 */
1463 public int noteOpNoThrow(String op, int uid, String packageName) {
1464 return noteOpNoThrow(strOpToOp(op), uid, packageName);
1465 }
1466
1467 /**
Svet Ganov99b60432015-06-27 13:15:22 -07001468 * Make note of an application performing an operation on behalf of another
1469 * application when handling an IPC. Note that you must pass the package name
1470 * of the application that is being proxied while its UID will be inferred from
1471 * the IPC state; this function will verify that the calling uid and proxied
1472 * package name match, and if not, return {@link #MODE_IGNORED}. If this call
1473 * succeeds, the last execution time of the operation for the proxied app and
1474 * your app will be updated to the current time.
1475 * @param op The operation to note. One of the OPSTR_* constants.
1476 * @param proxiedPackageName The name of the application calling into the proxy application.
1477 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
1478 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
1479 * causing the app to crash).
1480 * @throws SecurityException If the app has been configured to crash on this op.
1481 */
1482 public int noteProxyOp(String op, String proxiedPackageName) {
1483 return noteProxyOp(strOpToOp(op), proxiedPackageName);
1484 }
1485
1486 /**
1487 * Like {@link #noteProxyOp(String, String)} but instead
1488 * of throwing a {@link SecurityException} it returns {@link #MODE_ERRORED}.
1489 */
1490 public int noteProxyOpNoThrow(String op, String proxiedPackageName) {
1491 return noteProxyOpNoThrow(strOpToOp(op), proxiedPackageName);
1492 }
1493
1494 /**
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001495 * Report that an application has started executing a long-running operation. Note that you
1496 * must pass in both the uid and name of the application to be checked; this function will
1497 * verify that these two match, and if not, return {@link #MODE_IGNORED}. If this call
1498 * succeeds, the last execution time of the operation for this app will be updated to
1499 * the current time and the operation will be marked as "running". In this case you must
1500 * later call {@link #finishOp(String, int, String)} to report when the application is no
1501 * longer performing the operation.
1502 * @param op The operation to start. One of the OPSTR_* constants.
1503 * @param uid The user id of the application attempting to perform the operation.
1504 * @param packageName The name of the application attempting to perform the operation.
1505 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
1506 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
1507 * causing the app to crash).
1508 * @throws SecurityException If the app has been configured to crash on this op.
1509 */
1510 public int startOp(String op, int uid, String packageName) {
1511 return startOp(strOpToOp(op), uid, packageName);
1512 }
1513
1514 /**
1515 * Like {@link #startOp} but instead of throwing a {@link SecurityException} it
1516 * returns {@link #MODE_ERRORED}.
1517 */
1518 public int startOpNoThrow(String op, int uid, String packageName) {
1519 return startOpNoThrow(strOpToOp(op), uid, packageName);
1520 }
1521
1522 /**
1523 * Report that an application is no longer performing an operation that had previously
1524 * been started with {@link #startOp(String, int, String)}. There is no validation of input
1525 * or result; the parameters supplied here must be the exact same ones previously passed
1526 * in when starting the operation.
1527 */
1528 public void finishOp(String op, int uid, String packageName) {
1529 finishOp(strOpToOp(op), uid, packageName);
1530 }
1531
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001532 /**
1533 * Do a quick check for whether an application might be able to perform an operation.
1534 * This is <em>not</em> a security check; you must use {@link #noteOp(int, int, String)}
1535 * or {@link #startOp(int, int, String)} for your actual security checks, which also
1536 * ensure that the given uid and package name are consistent. This function can just be
1537 * used for a quick check to see if an operation has been disabled for the application,
1538 * as an early reject of some work. This does not modify the time stamp or other data
1539 * about the operation.
1540 * @param op The operation to check. One of the OP_* constants.
1541 * @param uid The user id of the application attempting to perform the operation.
1542 * @param packageName The name of the application attempting to perform the operation.
1543 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
1544 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
1545 * causing the app to crash).
1546 * @throws SecurityException If the app has been configured to crash on this op.
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001547 * @hide
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001548 */
Dianne Hackborn35654b62013-01-14 17:38:02 -08001549 public int checkOp(int op, int uid, String packageName) {
1550 try {
1551 int mode = mService.checkOperation(op, uid, packageName);
1552 if (mode == MODE_ERRORED) {
Dianne Hackborn95d78532013-09-11 09:51:14 -07001553 throw new SecurityException(buildSecurityExceptionMsg(op, uid, packageName));
Dianne Hackborn35654b62013-01-14 17:38:02 -08001554 }
1555 return mode;
1556 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001557 throw e.rethrowFromSystemServer();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001558 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001559 }
1560
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001561 /**
1562 * Like {@link #checkOp} but instead of throwing a {@link SecurityException} it
1563 * returns {@link #MODE_ERRORED}.
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001564 * @hide
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001565 */
Dianne Hackborn35654b62013-01-14 17:38:02 -08001566 public int checkOpNoThrow(int op, int uid, String packageName) {
1567 try {
1568 return mService.checkOperation(op, uid, packageName);
1569 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001570 throw e.rethrowFromSystemServer();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001571 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001572 }
1573
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001574 /**
Jeff Sharkey911d7f42013-09-05 18:11:45 -07001575 * Do a quick check to validate if a package name belongs to a UID.
1576 *
1577 * @throws SecurityException if the package name doesn't belong to the given
1578 * UID, or if ownership cannot be verified.
1579 */
1580 public void checkPackage(int uid, String packageName) {
1581 try {
1582 if (mService.checkPackage(uid, packageName) != MODE_ALLOWED) {
1583 throw new SecurityException(
1584 "Package " + packageName + " does not belong to " + uid);
1585 }
1586 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001587 throw e.rethrowFromSystemServer();
Jeff Sharkey911d7f42013-09-05 18:11:45 -07001588 }
1589 }
1590
1591 /**
John Spurlock1af30c72014-03-10 08:33:35 -04001592 * Like {@link #checkOp} but at a stream-level for audio operations.
1593 * @hide
1594 */
1595 public int checkAudioOp(int op, int stream, int uid, String packageName) {
1596 try {
1597 final int mode = mService.checkAudioOperation(op, stream, uid, packageName);
1598 if (mode == MODE_ERRORED) {
1599 throw new SecurityException(buildSecurityExceptionMsg(op, uid, packageName));
1600 }
1601 return mode;
1602 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001603 throw e.rethrowFromSystemServer();
John Spurlock1af30c72014-03-10 08:33:35 -04001604 }
John Spurlock1af30c72014-03-10 08:33:35 -04001605 }
1606
1607 /**
1608 * Like {@link #checkAudioOp} but instead of throwing a {@link SecurityException} it
1609 * returns {@link #MODE_ERRORED}.
1610 * @hide
1611 */
1612 public int checkAudioOpNoThrow(int op, int stream, int uid, String packageName) {
1613 try {
1614 return mService.checkAudioOperation(op, stream, uid, packageName);
1615 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001616 throw e.rethrowFromSystemServer();
John Spurlock1af30c72014-03-10 08:33:35 -04001617 }
John Spurlock1af30c72014-03-10 08:33:35 -04001618 }
1619
1620 /**
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001621 * Make note of an application performing an operation. Note that you must pass
1622 * in both the uid and name of the application to be checked; this function will verify
1623 * that these two match, and if not, return {@link #MODE_IGNORED}. If this call
1624 * succeeds, the last execution time of the operation for this app will be updated to
1625 * the current time.
1626 * @param op The operation to note. One of the OP_* constants.
1627 * @param uid The user id of the application attempting to perform the operation.
1628 * @param packageName The name of the application attempting to perform the operation.
1629 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
1630 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
1631 * causing the app to crash).
1632 * @throws SecurityException If the app has been configured to crash on this op.
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001633 * @hide
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001634 */
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001635 public int noteOp(int op, int uid, String packageName) {
1636 try {
1637 int mode = mService.noteOperation(op, uid, packageName);
1638 if (mode == MODE_ERRORED) {
Dianne Hackborn95d78532013-09-11 09:51:14 -07001639 throw new SecurityException(buildSecurityExceptionMsg(op, uid, packageName));
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001640 }
1641 return mode;
1642 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001643 throw e.rethrowFromSystemServer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001644 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001645 }
1646
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001647 /**
Svet Ganov99b60432015-06-27 13:15:22 -07001648 * Make note of an application performing an operation on behalf of another
1649 * application when handling an IPC. Note that you must pass the package name
1650 * of the application that is being proxied while its UID will be inferred from
1651 * the IPC state; this function will verify that the calling uid and proxied
1652 * package name match, and if not, return {@link #MODE_IGNORED}. If this call
1653 * succeeds, the last execution time of the operation for the proxied app and
1654 * your app will be updated to the current time.
1655 * @param op The operation to note. One of the OPSTR_* constants.
1656 * @param proxiedPackageName The name of the application calling into the proxy application.
1657 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
1658 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
1659 * causing the app to crash).
1660 * @throws SecurityException If the proxy or proxied app has been configured to
1661 * crash on this op.
1662 *
1663 * @hide
1664 */
1665 public int noteProxyOp(int op, String proxiedPackageName) {
1666 int mode = noteProxyOpNoThrow(op, proxiedPackageName);
1667 if (mode == MODE_ERRORED) {
1668 throw new SecurityException("Proxy package " + mContext.getOpPackageName()
1669 + " from uid " + Process.myUid() + " or calling package "
1670 + proxiedPackageName + " from uid " + Binder.getCallingUid()
1671 + " not allowed to perform " + sOpNames[op]);
1672 }
1673 return mode;
1674 }
1675
1676 /**
1677 * Like {@link #noteProxyOp(int, String)} but instead
1678 * of throwing a {@link SecurityException} it returns {@link #MODE_ERRORED}.
1679 * @hide
1680 */
1681 public int noteProxyOpNoThrow(int op, String proxiedPackageName) {
1682 try {
1683 return mService.noteProxyOperation(op, mContext.getOpPackageName(),
1684 Binder.getCallingUid(), proxiedPackageName);
1685 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001686 throw e.rethrowFromSystemServer();
Svet Ganov99b60432015-06-27 13:15:22 -07001687 }
Svet Ganov99b60432015-06-27 13:15:22 -07001688 }
1689
1690 /**
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001691 * Like {@link #noteOp} but instead of throwing a {@link SecurityException} it
1692 * returns {@link #MODE_ERRORED}.
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001693 * @hide
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001694 */
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001695 public int noteOpNoThrow(int op, int uid, String packageName) {
1696 try {
1697 return mService.noteOperation(op, uid, packageName);
1698 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001699 throw e.rethrowFromSystemServer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001700 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001701 }
1702
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001703 /** @hide */
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001704 public int noteOp(int op) {
Dianne Hackborn95d78532013-09-11 09:51:14 -07001705 return noteOp(op, Process.myUid(), mContext.getOpPackageName());
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001706 }
1707
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001708 /** @hide */
1709 public static IBinder getToken(IAppOpsService service) {
1710 synchronized (AppOpsManager.class) {
1711 if (sToken != null) {
1712 return sToken;
1713 }
1714 try {
1715 sToken = service.getToken(new Binder());
1716 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001717 throw e.rethrowFromSystemServer();
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001718 }
1719 return sToken;
1720 }
1721 }
1722
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001723 /**
1724 * Report that an application has started executing a long-running operation. Note that you
1725 * must pass in both the uid and name of the application to be checked; this function will
1726 * verify that these two match, and if not, return {@link #MODE_IGNORED}. If this call
1727 * succeeds, the last execution time of the operation for this app will be updated to
1728 * the current time and the operation will be marked as "running". In this case you must
1729 * later call {@link #finishOp(int, int, String)} to report when the application is no
1730 * longer performing the operation.
1731 * @param op The operation to start. One of the OP_* constants.
1732 * @param uid The user id of the application attempting to perform the operation.
1733 * @param packageName The name of the application attempting to perform the operation.
1734 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
1735 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
1736 * causing the app to crash).
1737 * @throws SecurityException If the app has been configured to crash on this op.
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001738 * @hide
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001739 */
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001740 public int startOp(int op, int uid, String packageName) {
1741 try {
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001742 int mode = mService.startOperation(getToken(mService), op, uid, packageName);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001743 if (mode == MODE_ERRORED) {
Dianne Hackborn95d78532013-09-11 09:51:14 -07001744 throw new SecurityException(buildSecurityExceptionMsg(op, uid, packageName));
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001745 }
1746 return mode;
1747 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001748 throw e.rethrowFromSystemServer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001749 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001750 }
1751
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001752 /**
1753 * Like {@link #startOp} but instead of throwing a {@link SecurityException} it
1754 * returns {@link #MODE_ERRORED}.
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001755 * @hide
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001756 */
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001757 public int startOpNoThrow(int op, int uid, String packageName) {
1758 try {
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001759 return mService.startOperation(getToken(mService), op, uid, packageName);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001760 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001761 throw e.rethrowFromSystemServer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001762 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001763 }
1764
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001765 /** @hide */
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001766 public int startOp(int op) {
Dianne Hackborn95d78532013-09-11 09:51:14 -07001767 return startOp(op, Process.myUid(), mContext.getOpPackageName());
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001768 }
1769
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001770 /**
1771 * Report that an application is no longer performing an operation that had previously
1772 * been started with {@link #startOp(int, int, String)}. There is no validation of input
1773 * or result; the parameters supplied here must be the exact same ones previously passed
1774 * in when starting the operation.
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001775 * @hide
Dianne Hackborn1304f4a2013-07-09 18:17:27 -07001776 */
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001777 public void finishOp(int op, int uid, String packageName) {
1778 try {
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001779 mService.finishOperation(getToken(mService), op, uid, packageName);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001780 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001781 throw e.rethrowFromSystemServer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001782 }
1783 }
1784
Dianne Hackborn9bb0ee92013-09-22 12:31:38 -07001785 /** @hide */
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001786 public void finishOp(int op) {
Dianne Hackborn95d78532013-09-11 09:51:14 -07001787 finishOp(op, Process.myUid(), mContext.getOpPackageName());
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001788 }
1789}