AppOpsManager.java revision e9d9b4b9a27f419fbd6096698f692b474939cb48
1/* 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 19import android.Manifest; 20import android.annotation.NonNull; 21import android.annotation.RequiresPermission; 22import android.annotation.SystemApi; 23import android.annotation.SystemService; 24import android.annotation.TestApi; 25import android.app.usage.UsageStatsManager; 26import android.content.Context; 27import android.media.AudioAttributes.AttributeUsage; 28import android.os.Binder; 29import android.os.IBinder; 30import android.os.Parcel; 31import android.os.Parcelable; 32import android.os.Process; 33import android.os.RemoteException; 34import android.os.UserManager; 35import android.util.ArrayMap; 36 37import com.android.internal.app.IAppOpsActiveCallback; 38import com.android.internal.app.IAppOpsCallback; 39import com.android.internal.app.IAppOpsService; 40import com.android.internal.util.Preconditions; 41 42import java.util.ArrayList; 43import java.util.Arrays; 44import java.util.HashMap; 45import java.util.List; 46 47/** 48 * API for interacting with "application operation" tracking. 49 * 50 * <p>This API is not generally intended for third party application developers; most 51 * features are only available to system applications. 52 */ 53@SystemService(Context.APP_OPS_SERVICE) 54public class AppOpsManager { 55 /** 56 * <p>App ops allows callers to:</p> 57 * 58 * <ul> 59 * <li> Note when operations are happening, and find out if they are allowed for the current 60 * caller.</li> 61 * <li> Disallow specific apps from doing specific operations.</li> 62 * <li> Collect all of the current information about operations that have been executed or 63 * are not being allowed.</li> 64 * <li> Monitor for changes in whether an operation is allowed.</li> 65 * </ul> 66 * 67 * <p>Each operation is identified by a single integer; these integers are a fixed set of 68 * operations, enumerated by the OP_* constants. 69 * 70 * <p></p>When checking operations, the result is a "mode" integer indicating the current 71 * setting for the operation under that caller: MODE_ALLOWED, MODE_IGNORED (don't execute 72 * the operation but fake its behavior enough so that the caller doesn't crash), 73 * MODE_ERRORED (throw a SecurityException back to the caller; the normal operation calls 74 * will do this for you). 75 */ 76 77 final Context mContext; 78 final IAppOpsService mService; 79 final ArrayMap<OnOpChangedListener, IAppOpsCallback> mModeWatchers = new ArrayMap<>(); 80 final ArrayMap<OnOpActiveChangedListener, IAppOpsActiveCallback> mActiveWatchers = 81 new ArrayMap<>(); 82 83 static IBinder sToken; 84 85 /** 86 * Result from {@link #checkOp}, {@link #noteOp}, {@link #startOp}: the given caller is 87 * allowed to perform the given operation. 88 */ 89 public static final int MODE_ALLOWED = 0; 90 91 /** 92 * Result from {@link #checkOp}, {@link #noteOp}, {@link #startOp}: the given caller is 93 * not allowed to perform the given operation, and this attempt should 94 * <em>silently fail</em> (it should not cause the app to crash). 95 */ 96 public static final int MODE_IGNORED = 1; 97 98 /** 99 * Result from {@link #checkOpNoThrow}, {@link #noteOpNoThrow}, {@link #startOpNoThrow}: the 100 * given caller is not allowed to perform the given operation, and this attempt should 101 * cause it to have a fatal error, typically a {@link SecurityException}. 102 */ 103 public static final int MODE_ERRORED = 2; 104 105 /** 106 * Result from {@link #checkOp}, {@link #noteOp}, {@link #startOp}: the given caller should 107 * use its default security check. This mode is not normally used; it should only be used 108 * with appop permissions, and callers must explicitly check for it and deal with it. 109 */ 110 public static final int MODE_DEFAULT = 3; 111 112 // when adding one of these: 113 // - increment _NUM_OP 114 // - define an OPSTR_* constant (marked as @SystemApi) 115 // - add rows to sOpToSwitch, sOpToString, sOpNames, sOpToPerms, sOpDefault 116 // - add descriptive strings to Settings/res/values/arrays.xml 117 // - add the op to the appropriate template in AppOpsState.OpsTemplate (settings app) 118 119 /** @hide No operation specified. */ 120 public static final int OP_NONE = -1; 121 /** @hide Access to coarse location information. */ 122 public static final int OP_COARSE_LOCATION = 0; 123 /** @hide Access to fine location information. */ 124 public static final int OP_FINE_LOCATION = 1; 125 /** @hide Causing GPS to run. */ 126 public static final int OP_GPS = 2; 127 /** @hide */ 128 public static final int OP_VIBRATE = 3; 129 /** @hide */ 130 public static final int OP_READ_CONTACTS = 4; 131 /** @hide */ 132 public static final int OP_WRITE_CONTACTS = 5; 133 /** @hide */ 134 public static final int OP_READ_CALL_LOG = 6; 135 /** @hide */ 136 public static final int OP_WRITE_CALL_LOG = 7; 137 /** @hide */ 138 public static final int OP_READ_CALENDAR = 8; 139 /** @hide */ 140 public static final int OP_WRITE_CALENDAR = 9; 141 /** @hide */ 142 public static final int OP_WIFI_SCAN = 10; 143 /** @hide */ 144 public static final int OP_POST_NOTIFICATION = 11; 145 /** @hide */ 146 public static final int OP_NEIGHBORING_CELLS = 12; 147 /** @hide */ 148 public static final int OP_CALL_PHONE = 13; 149 /** @hide */ 150 public static final int OP_READ_SMS = 14; 151 /** @hide */ 152 public static final int OP_WRITE_SMS = 15; 153 /** @hide */ 154 public static final int OP_RECEIVE_SMS = 16; 155 /** @hide */ 156 public static final int OP_RECEIVE_EMERGECY_SMS = 17; 157 /** @hide */ 158 public static final int OP_RECEIVE_MMS = 18; 159 /** @hide */ 160 public static final int OP_RECEIVE_WAP_PUSH = 19; 161 /** @hide */ 162 public static final int OP_SEND_SMS = 20; 163 /** @hide */ 164 public static final int OP_READ_ICC_SMS = 21; 165 /** @hide */ 166 public static final int OP_WRITE_ICC_SMS = 22; 167 /** @hide */ 168 public static final int OP_WRITE_SETTINGS = 23; 169 /** @hide Required to draw on top of other apps. */ 170 @TestApi 171 public static final int OP_SYSTEM_ALERT_WINDOW = 24; 172 /** @hide */ 173 public static final int OP_ACCESS_NOTIFICATIONS = 25; 174 /** @hide */ 175 public static final int OP_CAMERA = 26; 176 /** @hide */ 177 @TestApi 178 public static final int OP_RECORD_AUDIO = 27; 179 /** @hide */ 180 public static final int OP_PLAY_AUDIO = 28; 181 /** @hide */ 182 public static final int OP_READ_CLIPBOARD = 29; 183 /** @hide */ 184 public static final int OP_WRITE_CLIPBOARD = 30; 185 /** @hide */ 186 public static final int OP_TAKE_MEDIA_BUTTONS = 31; 187 /** @hide */ 188 public static final int OP_TAKE_AUDIO_FOCUS = 32; 189 /** @hide */ 190 public static final int OP_AUDIO_MASTER_VOLUME = 33; 191 /** @hide */ 192 public static final int OP_AUDIO_VOICE_VOLUME = 34; 193 /** @hide */ 194 public static final int OP_AUDIO_RING_VOLUME = 35; 195 /** @hide */ 196 public static final int OP_AUDIO_MEDIA_VOLUME = 36; 197 /** @hide */ 198 public static final int OP_AUDIO_ALARM_VOLUME = 37; 199 /** @hide */ 200 public static final int OP_AUDIO_NOTIFICATION_VOLUME = 38; 201 /** @hide */ 202 public static final int OP_AUDIO_BLUETOOTH_VOLUME = 39; 203 /** @hide */ 204 public static final int OP_WAKE_LOCK = 40; 205 /** @hide Continually monitoring location data. */ 206 public static final int OP_MONITOR_LOCATION = 41; 207 /** @hide Continually monitoring location data with a relatively high power request. */ 208 public static final int OP_MONITOR_HIGH_POWER_LOCATION = 42; 209 /** @hide Retrieve current usage stats via {@link UsageStatsManager}. */ 210 public static final int OP_GET_USAGE_STATS = 43; 211 /** @hide */ 212 public static final int OP_MUTE_MICROPHONE = 44; 213 /** @hide */ 214 public static final int OP_TOAST_WINDOW = 45; 215 /** @hide Capture the device's display contents and/or audio */ 216 public static final int OP_PROJECT_MEDIA = 46; 217 /** @hide Activate a VPN connection without user intervention. */ 218 public static final int OP_ACTIVATE_VPN = 47; 219 /** @hide Access the WallpaperManagerAPI to write wallpapers. */ 220 public static final int OP_WRITE_WALLPAPER = 48; 221 /** @hide Received the assist structure from an app. */ 222 public static final int OP_ASSIST_STRUCTURE = 49; 223 /** @hide Received a screenshot from assist. */ 224 public static final int OP_ASSIST_SCREENSHOT = 50; 225 /** @hide Read the phone state. */ 226 public static final int OP_READ_PHONE_STATE = 51; 227 /** @hide Add voicemail messages to the voicemail content provider. */ 228 public static final int OP_ADD_VOICEMAIL = 52; 229 /** @hide Access APIs for SIP calling over VOIP or WiFi. */ 230 public static final int OP_USE_SIP = 53; 231 /** @hide Intercept outgoing calls. */ 232 public static final int OP_PROCESS_OUTGOING_CALLS = 54; 233 /** @hide User the fingerprint API. */ 234 public static final int OP_USE_FINGERPRINT = 55; 235 /** @hide Access to body sensors such as heart rate, etc. */ 236 public static final int OP_BODY_SENSORS = 56; 237 /** @hide Read previously received cell broadcast messages. */ 238 public static final int OP_READ_CELL_BROADCASTS = 57; 239 /** @hide Inject mock location into the system. */ 240 public static final int OP_MOCK_LOCATION = 58; 241 /** @hide Read external storage. */ 242 public static final int OP_READ_EXTERNAL_STORAGE = 59; 243 /** @hide Write external storage. */ 244 public static final int OP_WRITE_EXTERNAL_STORAGE = 60; 245 /** @hide Turned on the screen. */ 246 public static final int OP_TURN_SCREEN_ON = 61; 247 /** @hide Get device accounts. */ 248 public static final int OP_GET_ACCOUNTS = 62; 249 /** @hide Control whether an application is allowed to run in the background. */ 250 public static final int OP_RUN_IN_BACKGROUND = 63; 251 /** @hide */ 252 public static final int OP_AUDIO_ACCESSIBILITY_VOLUME = 64; 253 /** @hide Read the phone number. */ 254 public static final int OP_READ_PHONE_NUMBERS = 65; 255 /** @hide Request package installs through package installer */ 256 public static final int OP_REQUEST_INSTALL_PACKAGES = 66; 257 /** @hide Enter picture-in-picture. */ 258 public static final int OP_PICTURE_IN_PICTURE = 67; 259 /** @hide Instant app start foreground service. */ 260 public static final int OP_INSTANT_APP_START_FOREGROUND = 68; 261 /** @hide Answer incoming phone calls */ 262 public static final int OP_ANSWER_PHONE_CALLS = 69; 263 /** @hide Run jobs when in background */ 264 public static final int OP_RUN_ANY_IN_BACKGROUND = 70; 265 /** @hide Change Wi-Fi connectivity state */ 266 public static final int OP_CHANGE_WIFI_STATE = 71; 267 /** @hide Request package deletion through package installer */ 268 public static final int OP_REQUEST_DELETE_PACKAGES = 72; 269 /** @hide Bind an accessibility service. */ 270 public static final int OP_BIND_ACCESSIBILITY_SERVICE = 73; 271 /** @hide Continue handover of a call from another app */ 272 public static final int OP_ACCEPT_HANDOVER = 74; 273 /** @hide Create and Manage IPsec Tunnels */ 274 public static final int OP_MANAGE_IPSEC_TUNNELS = 75; 275 /** @hide Any app start foreground service. */ 276 public static final int OP_START_FOREGROUND = 76; 277 /** @hide */ 278 public static final int _NUM_OP = 77; 279 280 /** Access to coarse location information. */ 281 public static final String OPSTR_COARSE_LOCATION = "android:coarse_location"; 282 /** Access to fine location information. */ 283 public static final String OPSTR_FINE_LOCATION = 284 "android:fine_location"; 285 /** Continually monitoring location data. */ 286 public static final String OPSTR_MONITOR_LOCATION 287 = "android:monitor_location"; 288 /** Continually monitoring location data with a relatively high power request. */ 289 public static final String OPSTR_MONITOR_HIGH_POWER_LOCATION 290 = "android:monitor_location_high_power"; 291 /** Access to {@link android.app.usage.UsageStatsManager}. */ 292 public static final String OPSTR_GET_USAGE_STATS 293 = "android:get_usage_stats"; 294 /** Activate a VPN connection without user intervention. @hide */ 295 @SystemApi @TestApi 296 public static final String OPSTR_ACTIVATE_VPN 297 = "android:activate_vpn"; 298 /** Allows an application to read the user's contacts data. */ 299 public static final String OPSTR_READ_CONTACTS 300 = "android:read_contacts"; 301 /** Allows an application to write to the user's contacts data. */ 302 public static final String OPSTR_WRITE_CONTACTS 303 = "android:write_contacts"; 304 /** Allows an application to read the user's call log. */ 305 public static final String OPSTR_READ_CALL_LOG 306 = "android:read_call_log"; 307 /** Allows an application to write to the user's call log. */ 308 public static final String OPSTR_WRITE_CALL_LOG 309 = "android:write_call_log"; 310 /** Allows an application to read the user's calendar data. */ 311 public static final String OPSTR_READ_CALENDAR 312 = "android:read_calendar"; 313 /** Allows an application to write to the user's calendar data. */ 314 public static final String OPSTR_WRITE_CALENDAR 315 = "android:write_calendar"; 316 /** Allows an application to initiate a phone call. */ 317 public static final String OPSTR_CALL_PHONE 318 = "android:call_phone"; 319 /** Allows an application to read SMS messages. */ 320 public static final String OPSTR_READ_SMS 321 = "android:read_sms"; 322 /** Allows an application to receive SMS messages. */ 323 public static final String OPSTR_RECEIVE_SMS 324 = "android:receive_sms"; 325 /** Allows an application to receive MMS messages. */ 326 public static final String OPSTR_RECEIVE_MMS 327 = "android:receive_mms"; 328 /** Allows an application to receive WAP push messages. */ 329 public static final String OPSTR_RECEIVE_WAP_PUSH 330 = "android:receive_wap_push"; 331 /** Allows an application to send SMS messages. */ 332 public static final String OPSTR_SEND_SMS 333 = "android:send_sms"; 334 /** Required to be able to access the camera device. */ 335 public static final String OPSTR_CAMERA 336 = "android:camera"; 337 /** Required to be able to access the microphone device. */ 338 public static final String OPSTR_RECORD_AUDIO 339 = "android:record_audio"; 340 /** Required to access phone state related information. */ 341 public static final String OPSTR_READ_PHONE_STATE 342 = "android:read_phone_state"; 343 /** Required to access phone state related information. */ 344 public static final String OPSTR_ADD_VOICEMAIL 345 = "android:add_voicemail"; 346 /** Access APIs for SIP calling over VOIP or WiFi */ 347 public static final String OPSTR_USE_SIP 348 = "android:use_sip"; 349 /** Access APIs for diverting outgoing calls */ 350 public static final String OPSTR_PROCESS_OUTGOING_CALLS 351 = "android:process_outgoing_calls"; 352 /** Use the fingerprint API. */ 353 public static final String OPSTR_USE_FINGERPRINT 354 = "android:use_fingerprint"; 355 /** Access to body sensors such as heart rate, etc. */ 356 public static final String OPSTR_BODY_SENSORS 357 = "android:body_sensors"; 358 /** Read previously received cell broadcast messages. */ 359 public static final String OPSTR_READ_CELL_BROADCASTS 360 = "android:read_cell_broadcasts"; 361 /** Inject mock location into the system. */ 362 public static final String OPSTR_MOCK_LOCATION 363 = "android:mock_location"; 364 /** Read external storage. */ 365 public static final String OPSTR_READ_EXTERNAL_STORAGE 366 = "android:read_external_storage"; 367 /** Write external storage. */ 368 public static final String OPSTR_WRITE_EXTERNAL_STORAGE 369 = "android:write_external_storage"; 370 /** Required to draw on top of other apps. */ 371 public static final String OPSTR_SYSTEM_ALERT_WINDOW 372 = "android:system_alert_window"; 373 /** Required to write/modify/update system settingss. */ 374 public static final String OPSTR_WRITE_SETTINGS 375 = "android:write_settings"; 376 /** @hide Get device accounts. */ 377 @SystemApi @TestApi 378 public static final String OPSTR_GET_ACCOUNTS 379 = "android:get_accounts"; 380 public static final String OPSTR_READ_PHONE_NUMBERS 381 = "android:read_phone_numbers"; 382 /** Access to picture-in-picture. */ 383 public static final String OPSTR_PICTURE_IN_PICTURE 384 = "android:picture_in_picture"; 385 /** @hide */ 386 @SystemApi @TestApi 387 public static final String OPSTR_INSTANT_APP_START_FOREGROUND 388 = "android:instant_app_start_foreground"; 389 /** Answer incoming phone calls */ 390 public static final String OPSTR_ANSWER_PHONE_CALLS 391 = "android:answer_phone_calls"; 392 /** 393 * Accept call handover 394 * @hide 395 */ 396 @SystemApi @TestApi 397 public static final String OPSTR_ACCEPT_HANDOVER 398 = "android:accept_handover"; 399 /** @hide */ 400 @SystemApi @TestApi 401 public static final String OPSTR_GPS = "android:gps"; 402 /** @hide */ 403 @SystemApi @TestApi 404 public static final String OPSTR_VIBRATE = "android:vibrate"; 405 /** @hide */ 406 @SystemApi @TestApi 407 public static final String OPSTR_WIFI_SCAN = "android:wifi_scan"; 408 /** @hide */ 409 @SystemApi @TestApi 410 public static final String OPSTR_POST_NOTIFICATION = "android:post_notification"; 411 /** @hide */ 412 @SystemApi @TestApi 413 public static final String OPSTR_NEIGHBORING_CELLS = "android:neighboring_cells"; 414 /** @hide */ 415 @SystemApi @TestApi 416 public static final String OPSTR_WRITE_SMS = "android:write_sms"; 417 /** @hide */ 418 @SystemApi @TestApi 419 public static final String OPSTR_RECEIVE_EMERGENCY_BROADCAST = 420 "android:receive_emergency_broadcast"; 421 /** @hide */ 422 @SystemApi @TestApi 423 public static final String OPSTR_READ_ICC_SMS = "android:read_icc_sms"; 424 /** @hide */ 425 @SystemApi @TestApi 426 public static final String OPSTR_WRITE_ICC_SMS = "android:write_icc_sms"; 427 /** @hide */ 428 @SystemApi @TestApi 429 public static final String OPSTR_ACCESS_NOTIFICATIONS = "android:access_notifications"; 430 /** @hide */ 431 @SystemApi @TestApi 432 public static final String OPSTR_PLAY_AUDIO = "android:play_audio"; 433 /** @hide */ 434 @SystemApi @TestApi 435 public static final String OPSTR_READ_CLIPBOARD = "android:read_clipboard"; 436 /** @hide */ 437 @SystemApi @TestApi 438 public static final String OPSTR_WRITE_CLIPBOARD = "android:write_clipboard"; 439 /** @hide */ 440 @SystemApi @TestApi 441 public static final String OPSTR_TAKE_MEDIA_BUTTONS = "android:take_media_buttons"; 442 /** @hide */ 443 @SystemApi @TestApi 444 public static final String OPSTR_TAKE_AUDIO_FOCUS = "android:take_audio_focus"; 445 /** @hide */ 446 @SystemApi @TestApi 447 public static final String OPSTR_AUDIO_MASTER_VOLUME = "android:audio_master_volume"; 448 /** @hide */ 449 @SystemApi @TestApi 450 public static final String OPSTR_AUDIO_VOICE_VOLUME = "android:audio_voice_volume"; 451 /** @hide */ 452 @SystemApi @TestApi 453 public static final String OPSTR_AUDIO_RING_VOLUME = "android:audio_ring_volume"; 454 /** @hide */ 455 @SystemApi @TestApi 456 public static final String OPSTR_AUDIO_MEDIA_VOLUME = "android:audio_media_volume"; 457 /** @hide */ 458 @SystemApi @TestApi 459 public static final String OPSTR_AUDIO_ALARM_VOLUME = "android:audio_alarm_volume"; 460 /** @hide */ 461 @SystemApi @TestApi 462 public static final String OPSTR_AUDIO_NOTIFICATION_VOLUME = 463 "android:audio_notification_volume"; 464 /** @hide */ 465 @SystemApi @TestApi 466 public static final String OPSTR_AUDIO_BLUETOOTH_VOLUME = "android:audio_bluetooth_volume"; 467 /** @hide */ 468 @SystemApi @TestApi 469 public static final String OPSTR_WAKE_LOCK = "android:wake_lock"; 470 /** @hide */ 471 @SystemApi @TestApi 472 public static final String OPSTR_MUTE_MICROPHONE = "android:mute_microphone"; 473 /** @hide */ 474 @SystemApi @TestApi 475 public static final String OPSTR_TOAST_WINDOW = "android:toast_window"; 476 /** @hide */ 477 @SystemApi @TestApi 478 public static final String OPSTR_PROJECT_MEDIA = "android:project_media"; 479 /** @hide */ 480 @SystemApi @TestApi 481 public static final String OPSTR_WRITE_WALLPAPER = "android:write_wallpaper"; 482 /** @hide */ 483 @SystemApi @TestApi 484 public static final String OPSTR_ASSIST_STRUCTURE = "android:assist_structure"; 485 /** @hide */ 486 @SystemApi @TestApi 487 public static final String OPSTR_ASSIST_SCREENSHOT = "android:assist_screenshot"; 488 /** @hide */ 489 @SystemApi @TestApi 490 public static final String OPSTR_TURN_SCREEN_ON = "android:turn_screen_on"; 491 /** @hide */ 492 @SystemApi @TestApi 493 public static final String OPSTR_RUN_IN_BACKGROUND = "android:run_in_background"; 494 /** @hide */ 495 @SystemApi @TestApi 496 public static final String OPSTR_AUDIO_ACCESSIBILITY_VOLUME = 497 "android:audio_accessibility_volume"; 498 /** @hide */ 499 @SystemApi @TestApi 500 public static final String OPSTR_REQUEST_INSTALL_PACKAGES = "android:request_install_packages"; 501 /** @hide */ 502 @SystemApi @TestApi 503 public static final String OPSTR_RUN_ANY_IN_BACKGROUND = "android:run_any_in_background"; 504 /** @hide */ 505 @SystemApi @TestApi 506 public static final String OPSTR_CHANGE_WIFI_STATE = "android:change_wifi_state"; 507 /** @hide */ 508 @SystemApi @TestApi 509 public static final String OPSTR_REQUEST_DELETE_PACKAGES = "android:request_delete_packages"; 510 /** @hide */ 511 @SystemApi @TestApi 512 public static final String OPSTR_BIND_ACCESSIBILITY_SERVICE = 513 "android:bind_accessibility_service"; 514 /** @hide */ 515 @SystemApi @TestApi 516 public static final String OPSTR_MANAGE_IPSEC_TUNNELS = "android:manage_ipsec_tunnels"; 517 /** @hide */ 518 @SystemApi @TestApi 519 public static final String OPSTR_START_FOREGROUND = "android:start_foreground"; 520 521 // Warning: If an permission is added here it also has to be added to 522 // com.android.packageinstaller.permission.utils.EventLogger 523 private static final int[] RUNTIME_AND_APPOP_PERMISSIONS_OPS = { 524 // RUNTIME PERMISSIONS 525 // Contacts 526 OP_READ_CONTACTS, 527 OP_WRITE_CONTACTS, 528 OP_GET_ACCOUNTS, 529 // Calendar 530 OP_READ_CALENDAR, 531 OP_WRITE_CALENDAR, 532 // SMS 533 OP_SEND_SMS, 534 OP_RECEIVE_SMS, 535 OP_READ_SMS, 536 OP_RECEIVE_WAP_PUSH, 537 OP_RECEIVE_MMS, 538 OP_READ_CELL_BROADCASTS, 539 // Storage 540 OP_READ_EXTERNAL_STORAGE, 541 OP_WRITE_EXTERNAL_STORAGE, 542 // Location 543 OP_COARSE_LOCATION, 544 OP_FINE_LOCATION, 545 // Phone 546 OP_READ_PHONE_STATE, 547 OP_READ_PHONE_NUMBERS, 548 OP_CALL_PHONE, 549 OP_READ_CALL_LOG, 550 OP_WRITE_CALL_LOG, 551 OP_ADD_VOICEMAIL, 552 OP_USE_SIP, 553 OP_PROCESS_OUTGOING_CALLS, 554 OP_ANSWER_PHONE_CALLS, 555 OP_ACCEPT_HANDOVER, 556 // Microphone 557 OP_RECORD_AUDIO, 558 // Camera 559 OP_CAMERA, 560 // Body sensors 561 OP_BODY_SENSORS, 562 563 // APPOP PERMISSIONS 564 OP_ACCESS_NOTIFICATIONS, 565 OP_SYSTEM_ALERT_WINDOW, 566 OP_WRITE_SETTINGS, 567 OP_REQUEST_INSTALL_PACKAGES, 568 OP_START_FOREGROUND, 569 }; 570 571 /** 572 * This maps each operation to the operation that serves as the 573 * switch to determine whether it is allowed. Generally this is 574 * a 1:1 mapping, but for some things (like location) that have 575 * multiple low-level operations being tracked that should be 576 * presented to the user as one switch then this can be used to 577 * make them all controlled by the same single operation. 578 */ 579 private static int[] sOpToSwitch = new int[] { 580 OP_COARSE_LOCATION, 581 OP_COARSE_LOCATION, 582 OP_COARSE_LOCATION, 583 OP_VIBRATE, 584 OP_READ_CONTACTS, 585 OP_WRITE_CONTACTS, 586 OP_READ_CALL_LOG, 587 OP_WRITE_CALL_LOG, 588 OP_READ_CALENDAR, 589 OP_WRITE_CALENDAR, 590 OP_COARSE_LOCATION, 591 OP_POST_NOTIFICATION, 592 OP_COARSE_LOCATION, 593 OP_CALL_PHONE, 594 OP_READ_SMS, 595 OP_WRITE_SMS, 596 OP_RECEIVE_SMS, 597 OP_RECEIVE_SMS, 598 OP_RECEIVE_MMS, 599 OP_RECEIVE_WAP_PUSH, 600 OP_SEND_SMS, 601 OP_READ_SMS, 602 OP_WRITE_SMS, 603 OP_WRITE_SETTINGS, 604 OP_SYSTEM_ALERT_WINDOW, 605 OP_ACCESS_NOTIFICATIONS, 606 OP_CAMERA, 607 OP_RECORD_AUDIO, 608 OP_PLAY_AUDIO, 609 OP_READ_CLIPBOARD, 610 OP_WRITE_CLIPBOARD, 611 OP_TAKE_MEDIA_BUTTONS, 612 OP_TAKE_AUDIO_FOCUS, 613 OP_AUDIO_MASTER_VOLUME, 614 OP_AUDIO_VOICE_VOLUME, 615 OP_AUDIO_RING_VOLUME, 616 OP_AUDIO_MEDIA_VOLUME, 617 OP_AUDIO_ALARM_VOLUME, 618 OP_AUDIO_NOTIFICATION_VOLUME, 619 OP_AUDIO_BLUETOOTH_VOLUME, 620 OP_WAKE_LOCK, 621 OP_COARSE_LOCATION, 622 OP_COARSE_LOCATION, 623 OP_GET_USAGE_STATS, 624 OP_MUTE_MICROPHONE, 625 OP_TOAST_WINDOW, 626 OP_PROJECT_MEDIA, 627 OP_ACTIVATE_VPN, 628 OP_WRITE_WALLPAPER, 629 OP_ASSIST_STRUCTURE, 630 OP_ASSIST_SCREENSHOT, 631 OP_READ_PHONE_STATE, 632 OP_ADD_VOICEMAIL, 633 OP_USE_SIP, 634 OP_PROCESS_OUTGOING_CALLS, 635 OP_USE_FINGERPRINT, 636 OP_BODY_SENSORS, 637 OP_READ_CELL_BROADCASTS, 638 OP_MOCK_LOCATION, 639 OP_READ_EXTERNAL_STORAGE, 640 OP_WRITE_EXTERNAL_STORAGE, 641 OP_TURN_SCREEN_ON, 642 OP_GET_ACCOUNTS, 643 OP_RUN_IN_BACKGROUND, 644 OP_AUDIO_ACCESSIBILITY_VOLUME, 645 OP_READ_PHONE_NUMBERS, 646 OP_REQUEST_INSTALL_PACKAGES, 647 OP_PICTURE_IN_PICTURE, 648 OP_INSTANT_APP_START_FOREGROUND, 649 OP_ANSWER_PHONE_CALLS, 650 OP_RUN_ANY_IN_BACKGROUND, 651 OP_CHANGE_WIFI_STATE, 652 OP_REQUEST_DELETE_PACKAGES, 653 OP_BIND_ACCESSIBILITY_SERVICE, 654 OP_ACCEPT_HANDOVER, 655 OP_MANAGE_IPSEC_TUNNELS, 656 OP_START_FOREGROUND, 657 }; 658 659 /** 660 * This maps each operation to the public string constant for it. 661 */ 662 private static String[] sOpToString = new String[]{ 663 OPSTR_COARSE_LOCATION, 664 OPSTR_FINE_LOCATION, 665 OPSTR_GPS, 666 OPSTR_VIBRATE, 667 OPSTR_READ_CONTACTS, 668 OPSTR_WRITE_CONTACTS, 669 OPSTR_READ_CALL_LOG, 670 OPSTR_WRITE_CALL_LOG, 671 OPSTR_READ_CALENDAR, 672 OPSTR_WRITE_CALENDAR, 673 OPSTR_WIFI_SCAN, 674 OPSTR_POST_NOTIFICATION, 675 OPSTR_NEIGHBORING_CELLS, 676 OPSTR_CALL_PHONE, 677 OPSTR_READ_SMS, 678 OPSTR_WRITE_SMS, 679 OPSTR_RECEIVE_SMS, 680 OPSTR_RECEIVE_EMERGENCY_BROADCAST, 681 OPSTR_RECEIVE_MMS, 682 OPSTR_RECEIVE_WAP_PUSH, 683 OPSTR_SEND_SMS, 684 OPSTR_READ_ICC_SMS, 685 OPSTR_WRITE_ICC_SMS, 686 OPSTR_WRITE_SETTINGS, 687 OPSTR_SYSTEM_ALERT_WINDOW, 688 OPSTR_ACCESS_NOTIFICATIONS, 689 OPSTR_CAMERA, 690 OPSTR_RECORD_AUDIO, 691 OPSTR_PLAY_AUDIO, 692 OPSTR_READ_CLIPBOARD, 693 OPSTR_WRITE_CLIPBOARD, 694 OPSTR_TAKE_MEDIA_BUTTONS, 695 OPSTR_TAKE_AUDIO_FOCUS, 696 OPSTR_AUDIO_MASTER_VOLUME, 697 OPSTR_AUDIO_VOICE_VOLUME, 698 OPSTR_AUDIO_RING_VOLUME, 699 OPSTR_AUDIO_MEDIA_VOLUME, 700 OPSTR_AUDIO_ALARM_VOLUME, 701 OPSTR_AUDIO_NOTIFICATION_VOLUME, 702 OPSTR_AUDIO_BLUETOOTH_VOLUME, 703 OPSTR_WAKE_LOCK, 704 OPSTR_MONITOR_LOCATION, 705 OPSTR_MONITOR_HIGH_POWER_LOCATION, 706 OPSTR_GET_USAGE_STATS, 707 OPSTR_MUTE_MICROPHONE, 708 OPSTR_TOAST_WINDOW, 709 OPSTR_PROJECT_MEDIA, 710 OPSTR_ACTIVATE_VPN, 711 OPSTR_WRITE_WALLPAPER, 712 OPSTR_ASSIST_STRUCTURE, 713 OPSTR_ASSIST_SCREENSHOT, 714 OPSTR_READ_PHONE_STATE, 715 OPSTR_ADD_VOICEMAIL, 716 OPSTR_USE_SIP, 717 OPSTR_PROCESS_OUTGOING_CALLS, 718 OPSTR_USE_FINGERPRINT, 719 OPSTR_BODY_SENSORS, 720 OPSTR_READ_CELL_BROADCASTS, 721 OPSTR_MOCK_LOCATION, 722 OPSTR_READ_EXTERNAL_STORAGE, 723 OPSTR_WRITE_EXTERNAL_STORAGE, 724 OPSTR_TURN_SCREEN_ON, 725 OPSTR_GET_ACCOUNTS, 726 OPSTR_RUN_IN_BACKGROUND, 727 OPSTR_AUDIO_ACCESSIBILITY_VOLUME, 728 OPSTR_READ_PHONE_NUMBERS, 729 OPSTR_REQUEST_INSTALL_PACKAGES, 730 OPSTR_PICTURE_IN_PICTURE, 731 OPSTR_INSTANT_APP_START_FOREGROUND, 732 OPSTR_ANSWER_PHONE_CALLS, 733 OPSTR_RUN_ANY_IN_BACKGROUND, 734 OPSTR_CHANGE_WIFI_STATE, 735 OPSTR_REQUEST_DELETE_PACKAGES, 736 OPSTR_BIND_ACCESSIBILITY_SERVICE, 737 OPSTR_ACCEPT_HANDOVER, 738 OPSTR_MANAGE_IPSEC_TUNNELS, 739 OPSTR_START_FOREGROUND, 740 }; 741 742 /** 743 * This provides a simple name for each operation to be used 744 * in debug output. 745 */ 746 private static String[] sOpNames = new String[] { 747 "COARSE_LOCATION", 748 "FINE_LOCATION", 749 "GPS", 750 "VIBRATE", 751 "READ_CONTACTS", 752 "WRITE_CONTACTS", 753 "READ_CALL_LOG", 754 "WRITE_CALL_LOG", 755 "READ_CALENDAR", 756 "WRITE_CALENDAR", 757 "WIFI_SCAN", 758 "POST_NOTIFICATION", 759 "NEIGHBORING_CELLS", 760 "CALL_PHONE", 761 "READ_SMS", 762 "WRITE_SMS", 763 "RECEIVE_SMS", 764 "RECEIVE_EMERGECY_SMS", 765 "RECEIVE_MMS", 766 "RECEIVE_WAP_PUSH", 767 "SEND_SMS", 768 "READ_ICC_SMS", 769 "WRITE_ICC_SMS", 770 "WRITE_SETTINGS", 771 "SYSTEM_ALERT_WINDOW", 772 "ACCESS_NOTIFICATIONS", 773 "CAMERA", 774 "RECORD_AUDIO", 775 "PLAY_AUDIO", 776 "READ_CLIPBOARD", 777 "WRITE_CLIPBOARD", 778 "TAKE_MEDIA_BUTTONS", 779 "TAKE_AUDIO_FOCUS", 780 "AUDIO_MASTER_VOLUME", 781 "AUDIO_VOICE_VOLUME", 782 "AUDIO_RING_VOLUME", 783 "AUDIO_MEDIA_VOLUME", 784 "AUDIO_ALARM_VOLUME", 785 "AUDIO_NOTIFICATION_VOLUME", 786 "AUDIO_BLUETOOTH_VOLUME", 787 "WAKE_LOCK", 788 "MONITOR_LOCATION", 789 "MONITOR_HIGH_POWER_LOCATION", 790 "GET_USAGE_STATS", 791 "MUTE_MICROPHONE", 792 "TOAST_WINDOW", 793 "PROJECT_MEDIA", 794 "ACTIVATE_VPN", 795 "WRITE_WALLPAPER", 796 "ASSIST_STRUCTURE", 797 "ASSIST_SCREENSHOT", 798 "OP_READ_PHONE_STATE", 799 "ADD_VOICEMAIL", 800 "USE_SIP", 801 "PROCESS_OUTGOING_CALLS", 802 "USE_FINGERPRINT", 803 "BODY_SENSORS", 804 "READ_CELL_BROADCASTS", 805 "MOCK_LOCATION", 806 "READ_EXTERNAL_STORAGE", 807 "WRITE_EXTERNAL_STORAGE", 808 "TURN_ON_SCREEN", 809 "GET_ACCOUNTS", 810 "RUN_IN_BACKGROUND", 811 "AUDIO_ACCESSIBILITY_VOLUME", 812 "READ_PHONE_NUMBERS", 813 "REQUEST_INSTALL_PACKAGES", 814 "PICTURE_IN_PICTURE", 815 "INSTANT_APP_START_FOREGROUND", 816 "ANSWER_PHONE_CALLS", 817 "RUN_ANY_IN_BACKGROUND", 818 "CHANGE_WIFI_STATE", 819 "REQUEST_DELETE_PACKAGES", 820 "BIND_ACCESSIBILITY_SERVICE", 821 "ACCEPT_HANDOVER", 822 "MANAGE_IPSEC_TUNNELS", 823 "START_FOREGROUND", 824 }; 825 826 /** 827 * This optionally maps a permission to an operation. If there 828 * is no permission associated with an operation, it is null. 829 */ 830 private static String[] sOpPerms = new String[] { 831 android.Manifest.permission.ACCESS_COARSE_LOCATION, 832 android.Manifest.permission.ACCESS_FINE_LOCATION, 833 null, 834 android.Manifest.permission.VIBRATE, 835 android.Manifest.permission.READ_CONTACTS, 836 android.Manifest.permission.WRITE_CONTACTS, 837 android.Manifest.permission.READ_CALL_LOG, 838 android.Manifest.permission.WRITE_CALL_LOG, 839 android.Manifest.permission.READ_CALENDAR, 840 android.Manifest.permission.WRITE_CALENDAR, 841 android.Manifest.permission.ACCESS_WIFI_STATE, 842 null, // no permission required for notifications 843 null, // neighboring cells shares the coarse location perm 844 android.Manifest.permission.CALL_PHONE, 845 android.Manifest.permission.READ_SMS, 846 null, // no permission required for writing sms 847 android.Manifest.permission.RECEIVE_SMS, 848 android.Manifest.permission.RECEIVE_EMERGENCY_BROADCAST, 849 android.Manifest.permission.RECEIVE_MMS, 850 android.Manifest.permission.RECEIVE_WAP_PUSH, 851 android.Manifest.permission.SEND_SMS, 852 android.Manifest.permission.READ_SMS, 853 null, // no permission required for writing icc sms 854 android.Manifest.permission.WRITE_SETTINGS, 855 android.Manifest.permission.SYSTEM_ALERT_WINDOW, 856 android.Manifest.permission.ACCESS_NOTIFICATIONS, 857 android.Manifest.permission.CAMERA, 858 android.Manifest.permission.RECORD_AUDIO, 859 null, // no permission for playing audio 860 null, // no permission for reading clipboard 861 null, // no permission for writing clipboard 862 null, // no permission for taking media buttons 863 null, // no permission for taking audio focus 864 null, // no permission for changing master volume 865 null, // no permission for changing voice volume 866 null, // no permission for changing ring volume 867 null, // no permission for changing media volume 868 null, // no permission for changing alarm volume 869 null, // no permission for changing notification volume 870 null, // no permission for changing bluetooth volume 871 android.Manifest.permission.WAKE_LOCK, 872 null, // no permission for generic location monitoring 873 null, // no permission for high power location monitoring 874 android.Manifest.permission.PACKAGE_USAGE_STATS, 875 null, // no permission for muting/unmuting microphone 876 null, // no permission for displaying toasts 877 null, // no permission for projecting media 878 null, // no permission for activating vpn 879 null, // no permission for supporting wallpaper 880 null, // no permission for receiving assist structure 881 null, // no permission for receiving assist screenshot 882 Manifest.permission.READ_PHONE_STATE, 883 Manifest.permission.ADD_VOICEMAIL, 884 Manifest.permission.USE_SIP, 885 Manifest.permission.PROCESS_OUTGOING_CALLS, 886 Manifest.permission.USE_FINGERPRINT, 887 Manifest.permission.BODY_SENSORS, 888 Manifest.permission.READ_CELL_BROADCASTS, 889 null, 890 Manifest.permission.READ_EXTERNAL_STORAGE, 891 Manifest.permission.WRITE_EXTERNAL_STORAGE, 892 null, // no permission for turning the screen on 893 Manifest.permission.GET_ACCOUNTS, 894 null, // no permission for running in background 895 null, // no permission for changing accessibility volume 896 Manifest.permission.READ_PHONE_NUMBERS, 897 Manifest.permission.REQUEST_INSTALL_PACKAGES, 898 null, // no permission for entering picture-in-picture on hide 899 Manifest.permission.INSTANT_APP_FOREGROUND_SERVICE, 900 Manifest.permission.ANSWER_PHONE_CALLS, 901 null, // no permission for OP_RUN_ANY_IN_BACKGROUND 902 Manifest.permission.CHANGE_WIFI_STATE, 903 Manifest.permission.REQUEST_DELETE_PACKAGES, 904 Manifest.permission.BIND_ACCESSIBILITY_SERVICE, 905 Manifest.permission.ACCEPT_HANDOVER, 906 null, // no permission for OP_MANAGE_IPSEC_TUNNELS 907 Manifest.permission.FOREGROUND_SERVICE, 908 }; 909 910 /** 911 * Specifies whether an Op should be restricted by a user restriction. 912 * Each Op should be filled with a restriction string from UserManager or 913 * null to specify it is not affected by any user restriction. 914 */ 915 private static String[] sOpRestrictions = new String[] { 916 UserManager.DISALLOW_SHARE_LOCATION, //COARSE_LOCATION 917 UserManager.DISALLOW_SHARE_LOCATION, //FINE_LOCATION 918 UserManager.DISALLOW_SHARE_LOCATION, //GPS 919 null, //VIBRATE 920 null, //READ_CONTACTS 921 null, //WRITE_CONTACTS 922 UserManager.DISALLOW_OUTGOING_CALLS, //READ_CALL_LOG 923 UserManager.DISALLOW_OUTGOING_CALLS, //WRITE_CALL_LOG 924 null, //READ_CALENDAR 925 null, //WRITE_CALENDAR 926 UserManager.DISALLOW_SHARE_LOCATION, //WIFI_SCAN 927 null, //POST_NOTIFICATION 928 null, //NEIGHBORING_CELLS 929 null, //CALL_PHONE 930 UserManager.DISALLOW_SMS, //READ_SMS 931 UserManager.DISALLOW_SMS, //WRITE_SMS 932 UserManager.DISALLOW_SMS, //RECEIVE_SMS 933 null, //RECEIVE_EMERGENCY_SMS 934 UserManager.DISALLOW_SMS, //RECEIVE_MMS 935 null, //RECEIVE_WAP_PUSH 936 UserManager.DISALLOW_SMS, //SEND_SMS 937 UserManager.DISALLOW_SMS, //READ_ICC_SMS 938 UserManager.DISALLOW_SMS, //WRITE_ICC_SMS 939 null, //WRITE_SETTINGS 940 UserManager.DISALLOW_CREATE_WINDOWS, //SYSTEM_ALERT_WINDOW 941 null, //ACCESS_NOTIFICATIONS 942 UserManager.DISALLOW_CAMERA, //CAMERA 943 UserManager.DISALLOW_RECORD_AUDIO, //RECORD_AUDIO 944 null, //PLAY_AUDIO 945 null, //READ_CLIPBOARD 946 null, //WRITE_CLIPBOARD 947 null, //TAKE_MEDIA_BUTTONS 948 null, //TAKE_AUDIO_FOCUS 949 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_MASTER_VOLUME 950 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_VOICE_VOLUME 951 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_RING_VOLUME 952 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_MEDIA_VOLUME 953 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_ALARM_VOLUME 954 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_NOTIFICATION_VOLUME 955 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_BLUETOOTH_VOLUME 956 null, //WAKE_LOCK 957 UserManager.DISALLOW_SHARE_LOCATION, //MONITOR_LOCATION 958 UserManager.DISALLOW_SHARE_LOCATION, //MONITOR_HIGH_POWER_LOCATION 959 null, //GET_USAGE_STATS 960 UserManager.DISALLOW_UNMUTE_MICROPHONE, // MUTE_MICROPHONE 961 UserManager.DISALLOW_CREATE_WINDOWS, // TOAST_WINDOW 962 null, //PROJECT_MEDIA 963 null, // ACTIVATE_VPN 964 UserManager.DISALLOW_WALLPAPER, // WRITE_WALLPAPER 965 null, // ASSIST_STRUCTURE 966 null, // ASSIST_SCREENSHOT 967 null, // READ_PHONE_STATE 968 null, // ADD_VOICEMAIL 969 null, // USE_SIP 970 null, // PROCESS_OUTGOING_CALLS 971 null, // USE_FINGERPRINT 972 null, // BODY_SENSORS 973 null, // READ_CELL_BROADCASTS 974 null, // MOCK_LOCATION 975 null, // READ_EXTERNAL_STORAGE 976 null, // WRITE_EXTERNAL_STORAGE 977 null, // TURN_ON_SCREEN 978 null, // GET_ACCOUNTS 979 null, // RUN_IN_BACKGROUND 980 UserManager.DISALLOW_ADJUST_VOLUME, //AUDIO_ACCESSIBILITY_VOLUME 981 null, // READ_PHONE_NUMBERS 982 null, // REQUEST_INSTALL_PACKAGES 983 null, // ENTER_PICTURE_IN_PICTURE_ON_HIDE 984 null, // INSTANT_APP_START_FOREGROUND 985 null, // ANSWER_PHONE_CALLS 986 null, // OP_RUN_ANY_IN_BACKGROUND 987 null, // OP_CHANGE_WIFI_STATE 988 null, // REQUEST_DELETE_PACKAGES 989 null, // OP_BIND_ACCESSIBILITY_SERVICE 990 null, // ACCEPT_HANDOVER 991 null, // MANAGE_IPSEC_TUNNELS 992 null, // START_FOREGROUND 993 }; 994 995 /** 996 * This specifies whether each option should allow the system 997 * (and system ui) to bypass the user restriction when active. 998 */ 999 private static boolean[] sOpAllowSystemRestrictionBypass = new boolean[] { 1000 true, //COARSE_LOCATION 1001 true, //FINE_LOCATION 1002 false, //GPS 1003 false, //VIBRATE 1004 false, //READ_CONTACTS 1005 false, //WRITE_CONTACTS 1006 false, //READ_CALL_LOG 1007 false, //WRITE_CALL_LOG 1008 false, //READ_CALENDAR 1009 false, //WRITE_CALENDAR 1010 true, //WIFI_SCAN 1011 false, //POST_NOTIFICATION 1012 false, //NEIGHBORING_CELLS 1013 false, //CALL_PHONE 1014 false, //READ_SMS 1015 false, //WRITE_SMS 1016 false, //RECEIVE_SMS 1017 false, //RECEIVE_EMERGECY_SMS 1018 false, //RECEIVE_MMS 1019 false, //RECEIVE_WAP_PUSH 1020 false, //SEND_SMS 1021 false, //READ_ICC_SMS 1022 false, //WRITE_ICC_SMS 1023 false, //WRITE_SETTINGS 1024 true, //SYSTEM_ALERT_WINDOW 1025 false, //ACCESS_NOTIFICATIONS 1026 false, //CAMERA 1027 false, //RECORD_AUDIO 1028 false, //PLAY_AUDIO 1029 false, //READ_CLIPBOARD 1030 false, //WRITE_CLIPBOARD 1031 false, //TAKE_MEDIA_BUTTONS 1032 false, //TAKE_AUDIO_FOCUS 1033 false, //AUDIO_MASTER_VOLUME 1034 false, //AUDIO_VOICE_VOLUME 1035 false, //AUDIO_RING_VOLUME 1036 false, //AUDIO_MEDIA_VOLUME 1037 false, //AUDIO_ALARM_VOLUME 1038 false, //AUDIO_NOTIFICATION_VOLUME 1039 false, //AUDIO_BLUETOOTH_VOLUME 1040 false, //WAKE_LOCK 1041 false, //MONITOR_LOCATION 1042 false, //MONITOR_HIGH_POWER_LOCATION 1043 false, //GET_USAGE_STATS 1044 false, //MUTE_MICROPHONE 1045 true, //TOAST_WINDOW 1046 false, //PROJECT_MEDIA 1047 false, //ACTIVATE_VPN 1048 false, //WALLPAPER 1049 false, //ASSIST_STRUCTURE 1050 false, //ASSIST_SCREENSHOT 1051 false, //READ_PHONE_STATE 1052 false, //ADD_VOICEMAIL 1053 false, // USE_SIP 1054 false, // PROCESS_OUTGOING_CALLS 1055 false, // USE_FINGERPRINT 1056 false, // BODY_SENSORS 1057 false, // READ_CELL_BROADCASTS 1058 false, // MOCK_LOCATION 1059 false, // READ_EXTERNAL_STORAGE 1060 false, // WRITE_EXTERNAL_STORAGE 1061 false, // TURN_ON_SCREEN 1062 false, // GET_ACCOUNTS 1063 false, // RUN_IN_BACKGROUND 1064 false, // AUDIO_ACCESSIBILITY_VOLUME 1065 false, // READ_PHONE_NUMBERS 1066 false, // REQUEST_INSTALL_PACKAGES 1067 false, // ENTER_PICTURE_IN_PICTURE_ON_HIDE 1068 false, // INSTANT_APP_START_FOREGROUND 1069 false, // ANSWER_PHONE_CALLS 1070 false, // OP_RUN_ANY_IN_BACKGROUND 1071 false, // OP_CHANGE_WIFI_STATE 1072 false, // OP_REQUEST_DELETE_PACKAGES 1073 false, // OP_BIND_ACCESSIBILITY_SERVICE 1074 false, // ACCEPT_HANDOVER 1075 false, // MANAGE_IPSEC_HANDOVERS 1076 false, // START_FOREGROUND 1077 }; 1078 1079 /** 1080 * This specifies the default mode for each operation. 1081 */ 1082 private static int[] sOpDefaultMode = new int[] { 1083 AppOpsManager.MODE_ALLOWED, 1084 AppOpsManager.MODE_ALLOWED, 1085 AppOpsManager.MODE_ALLOWED, 1086 AppOpsManager.MODE_ALLOWED, 1087 AppOpsManager.MODE_ALLOWED, 1088 AppOpsManager.MODE_ALLOWED, 1089 AppOpsManager.MODE_ALLOWED, 1090 AppOpsManager.MODE_ALLOWED, 1091 AppOpsManager.MODE_ALLOWED, 1092 AppOpsManager.MODE_ALLOWED, 1093 AppOpsManager.MODE_ALLOWED, 1094 AppOpsManager.MODE_ALLOWED, 1095 AppOpsManager.MODE_ALLOWED, 1096 AppOpsManager.MODE_ALLOWED, 1097 AppOpsManager.MODE_ALLOWED, 1098 AppOpsManager.MODE_IGNORED, // OP_WRITE_SMS 1099 AppOpsManager.MODE_ALLOWED, 1100 AppOpsManager.MODE_ALLOWED, 1101 AppOpsManager.MODE_ALLOWED, 1102 AppOpsManager.MODE_ALLOWED, 1103 AppOpsManager.MODE_ALLOWED, 1104 AppOpsManager.MODE_ALLOWED, 1105 AppOpsManager.MODE_ALLOWED, 1106 AppOpsManager.MODE_DEFAULT, // OP_WRITE_SETTINGS 1107 AppOpsManager.MODE_DEFAULT, // OP_SYSTEM_ALERT_WINDOW 1108 AppOpsManager.MODE_ALLOWED, 1109 AppOpsManager.MODE_ALLOWED, 1110 AppOpsManager.MODE_ALLOWED, 1111 AppOpsManager.MODE_ALLOWED, 1112 AppOpsManager.MODE_ALLOWED, 1113 AppOpsManager.MODE_ALLOWED, 1114 AppOpsManager.MODE_ALLOWED, 1115 AppOpsManager.MODE_ALLOWED, 1116 AppOpsManager.MODE_ALLOWED, 1117 AppOpsManager.MODE_ALLOWED, 1118 AppOpsManager.MODE_ALLOWED, 1119 AppOpsManager.MODE_ALLOWED, 1120 AppOpsManager.MODE_ALLOWED, 1121 AppOpsManager.MODE_ALLOWED, 1122 AppOpsManager.MODE_ALLOWED, 1123 AppOpsManager.MODE_ALLOWED, 1124 AppOpsManager.MODE_ALLOWED, 1125 AppOpsManager.MODE_ALLOWED, 1126 AppOpsManager.MODE_DEFAULT, // OP_GET_USAGE_STATS 1127 AppOpsManager.MODE_ALLOWED, 1128 AppOpsManager.MODE_ALLOWED, 1129 AppOpsManager.MODE_IGNORED, // OP_PROJECT_MEDIA 1130 AppOpsManager.MODE_IGNORED, // OP_ACTIVATE_VPN 1131 AppOpsManager.MODE_ALLOWED, 1132 AppOpsManager.MODE_ALLOWED, 1133 AppOpsManager.MODE_ALLOWED, 1134 AppOpsManager.MODE_ALLOWED, 1135 AppOpsManager.MODE_ALLOWED, 1136 AppOpsManager.MODE_ALLOWED, 1137 AppOpsManager.MODE_ALLOWED, 1138 AppOpsManager.MODE_ALLOWED, 1139 AppOpsManager.MODE_ALLOWED, 1140 AppOpsManager.MODE_ALLOWED, 1141 AppOpsManager.MODE_ERRORED, // OP_MOCK_LOCATION 1142 AppOpsManager.MODE_ALLOWED, 1143 AppOpsManager.MODE_ALLOWED, 1144 AppOpsManager.MODE_ALLOWED, // OP_TURN_ON_SCREEN 1145 AppOpsManager.MODE_ALLOWED, 1146 AppOpsManager.MODE_ALLOWED, // OP_RUN_IN_BACKGROUND 1147 AppOpsManager.MODE_ALLOWED, // OP_AUDIO_ACCESSIBILITY_VOLUME 1148 AppOpsManager.MODE_ALLOWED, 1149 AppOpsManager.MODE_DEFAULT, // OP_REQUEST_INSTALL_PACKAGES 1150 AppOpsManager.MODE_ALLOWED, // OP_PICTURE_IN_PICTURE 1151 AppOpsManager.MODE_DEFAULT, // OP_INSTANT_APP_START_FOREGROUND 1152 AppOpsManager.MODE_ALLOWED, // ANSWER_PHONE_CALLS 1153 AppOpsManager.MODE_ALLOWED, // OP_RUN_ANY_IN_BACKGROUND 1154 AppOpsManager.MODE_ALLOWED, // OP_CHANGE_WIFI_STATE 1155 AppOpsManager.MODE_ALLOWED, // REQUEST_DELETE_PACKAGES 1156 AppOpsManager.MODE_ALLOWED, // OP_BIND_ACCESSIBILITY_SERVICE 1157 AppOpsManager.MODE_ALLOWED, // ACCEPT_HANDOVER 1158 AppOpsManager.MODE_ERRORED, // MANAGE_IPSEC_TUNNELS 1159 AppOpsManager.MODE_ALLOWED, // OP_START_FOREGROUND 1160 }; 1161 1162 /** 1163 * This specifies whether each option is allowed to be reset 1164 * when resetting all app preferences. Disable reset for 1165 * app ops that are under strong control of some part of the 1166 * system (such as OP_WRITE_SMS, which should be allowed only 1167 * for whichever app is selected as the current SMS app). 1168 */ 1169 private static boolean[] sOpDisableReset = new boolean[] { 1170 false, 1171 false, 1172 false, 1173 false, 1174 false, 1175 false, 1176 false, 1177 false, 1178 false, 1179 false, 1180 false, 1181 false, 1182 false, 1183 false, 1184 false, 1185 true, // OP_WRITE_SMS 1186 false, 1187 false, 1188 false, 1189 false, 1190 false, 1191 false, 1192 false, 1193 false, 1194 false, 1195 false, 1196 false, 1197 false, 1198 false, 1199 false, 1200 false, 1201 false, 1202 false, 1203 false, 1204 false, 1205 false, 1206 false, 1207 false, 1208 false, 1209 false, 1210 false, 1211 false, 1212 false, 1213 false, 1214 false, 1215 false, 1216 false, 1217 false, 1218 false, 1219 false, 1220 false, 1221 false, 1222 false, 1223 false, 1224 false, 1225 false, 1226 false, 1227 false, 1228 false, 1229 false, 1230 false, 1231 false, 1232 false, 1233 false, 1234 false, // OP_AUDIO_ACCESSIBILITY_VOLUME 1235 false, 1236 false, // OP_REQUEST_INSTALL_PACKAGES 1237 false, // OP_PICTURE_IN_PICTURE 1238 false, 1239 false, // ANSWER_PHONE_CALLS 1240 false, // OP_RUN_ANY_IN_BACKGROUND 1241 false, // OP_CHANGE_WIFI_STATE 1242 false, // OP_REQUEST_DELETE_PACKAGES 1243 false, // OP_BIND_ACCESSIBILITY_SERVICE 1244 false, // ACCEPT_HANDOVER 1245 false, // MANAGE_IPSEC_TUNNELS 1246 false, // START_FOREGROUND 1247 }; 1248 1249 /** 1250 * Mapping from an app op name to the app op code. 1251 */ 1252 private static HashMap<String, Integer> sOpStrToOp = new HashMap<>(); 1253 1254 /** 1255 * Mapping from a permission to the corresponding app op. 1256 */ 1257 private static HashMap<String, Integer> sPermToOp = new HashMap<>(); 1258 1259 static { 1260 if (sOpToSwitch.length != _NUM_OP) { 1261 throw new IllegalStateException("sOpToSwitch length " + sOpToSwitch.length 1262 + " should be " + _NUM_OP); 1263 } 1264 if (sOpToString.length != _NUM_OP) { 1265 throw new IllegalStateException("sOpToString length " + sOpToString.length 1266 + " should be " + _NUM_OP); 1267 } 1268 if (sOpNames.length != _NUM_OP) { 1269 throw new IllegalStateException("sOpNames length " + sOpNames.length 1270 + " should be " + _NUM_OP); 1271 } 1272 if (sOpPerms.length != _NUM_OP) { 1273 throw new IllegalStateException("sOpPerms length " + sOpPerms.length 1274 + " should be " + _NUM_OP); 1275 } 1276 if (sOpDefaultMode.length != _NUM_OP) { 1277 throw new IllegalStateException("sOpDefaultMode length " + sOpDefaultMode.length 1278 + " should be " + _NUM_OP); 1279 } 1280 if (sOpDisableReset.length != _NUM_OP) { 1281 throw new IllegalStateException("sOpDisableReset length " + sOpDisableReset.length 1282 + " should be " + _NUM_OP); 1283 } 1284 if (sOpRestrictions.length != _NUM_OP) { 1285 throw new IllegalStateException("sOpRestrictions length " + sOpRestrictions.length 1286 + " should be " + _NUM_OP); 1287 } 1288 if (sOpAllowSystemRestrictionBypass.length != _NUM_OP) { 1289 throw new IllegalStateException("sOpAllowSYstemRestrictionsBypass length " 1290 + sOpRestrictions.length + " should be " + _NUM_OP); 1291 } 1292 for (int i=0; i<_NUM_OP; i++) { 1293 if (sOpToString[i] != null) { 1294 sOpStrToOp.put(sOpToString[i], i); 1295 } 1296 } 1297 for (int op : RUNTIME_AND_APPOP_PERMISSIONS_OPS) { 1298 if (sOpPerms[op] != null) { 1299 sPermToOp.put(sOpPerms[op], op); 1300 } 1301 } 1302 } 1303 1304 /** 1305 * Retrieve the op switch that controls the given operation. 1306 * @hide 1307 */ 1308 public static int opToSwitch(int op) { 1309 return sOpToSwitch[op]; 1310 } 1311 1312 /** 1313 * Retrieve a non-localized name for the operation, for debugging output. 1314 * @hide 1315 */ 1316 public static String opToName(int op) { 1317 if (op == OP_NONE) return "NONE"; 1318 return op < sOpNames.length ? sOpNames[op] : ("Unknown(" + op + ")"); 1319 } 1320 1321 /** 1322 * @hide 1323 */ 1324 public static int strDebugOpToOp(String op) { 1325 for (int i=0; i<sOpNames.length; i++) { 1326 if (sOpNames[i].equals(op)) { 1327 return i; 1328 } 1329 } 1330 throw new IllegalArgumentException("Unknown operation string: " + op); 1331 } 1332 1333 /** 1334 * Retrieve the permission associated with an operation, or null if there is not one. 1335 * @hide 1336 */ 1337 public static String opToPermission(int op) { 1338 return sOpPerms[op]; 1339 } 1340 1341 /** 1342 * Retrieve the user restriction associated with an operation, or null if there is not one. 1343 * @hide 1344 */ 1345 public static String opToRestriction(int op) { 1346 return sOpRestrictions[op]; 1347 } 1348 1349 /** 1350 * Retrieve the app op code for a permission, or null if there is not one. 1351 * This API is intended to be used for mapping runtime or appop permissions 1352 * to the corresponding app op. 1353 * @hide 1354 */ 1355 public static int permissionToOpCode(String permission) { 1356 Integer boxedOpCode = sPermToOp.get(permission); 1357 return boxedOpCode != null ? boxedOpCode : OP_NONE; 1358 } 1359 1360 /** 1361 * Retrieve whether the op allows the system (and system ui) to 1362 * bypass the user restriction. 1363 * @hide 1364 */ 1365 public static boolean opAllowSystemBypassRestriction(int op) { 1366 return sOpAllowSystemRestrictionBypass[op]; 1367 } 1368 1369 /** 1370 * Retrieve the default mode for the operation. 1371 * @hide 1372 */ 1373 public static int opToDefaultMode(int op) { 1374 return sOpDefaultMode[op]; 1375 } 1376 1377 /** 1378 * Retrieve the human readable mode. 1379 * @hide 1380 */ 1381 public static String modeToString(int mode) { 1382 switch (mode) { 1383 case MODE_ALLOWED: 1384 return "allow"; 1385 case MODE_IGNORED: 1386 return "ignore"; 1387 case MODE_ERRORED: 1388 return "deny"; 1389 case MODE_DEFAULT: 1390 return "default"; 1391 default: 1392 return "mode=" + mode; 1393 } 1394 } 1395 1396 /** 1397 * Retrieve whether the op allows itself to be reset. 1398 * @hide 1399 */ 1400 public static boolean opAllowsReset(int op) { 1401 return !sOpDisableReset[op]; 1402 } 1403 1404 /** 1405 * Class holding all of the operation information associated with an app. 1406 * @hide 1407 */ 1408 public static class PackageOps implements Parcelable { 1409 private final String mPackageName; 1410 private final int mUid; 1411 private final List<OpEntry> mEntries; 1412 1413 public PackageOps(String packageName, int uid, List<OpEntry> entries) { 1414 mPackageName = packageName; 1415 mUid = uid; 1416 mEntries = entries; 1417 } 1418 1419 public String getPackageName() { 1420 return mPackageName; 1421 } 1422 1423 public int getUid() { 1424 return mUid; 1425 } 1426 1427 public List<OpEntry> getOps() { 1428 return mEntries; 1429 } 1430 1431 @Override 1432 public int describeContents() { 1433 return 0; 1434 } 1435 1436 @Override 1437 public void writeToParcel(Parcel dest, int flags) { 1438 dest.writeString(mPackageName); 1439 dest.writeInt(mUid); 1440 dest.writeInt(mEntries.size()); 1441 for (int i=0; i<mEntries.size(); i++) { 1442 mEntries.get(i).writeToParcel(dest, flags); 1443 } 1444 } 1445 1446 PackageOps(Parcel source) { 1447 mPackageName = source.readString(); 1448 mUid = source.readInt(); 1449 mEntries = new ArrayList<OpEntry>(); 1450 final int N = source.readInt(); 1451 for (int i=0; i<N; i++) { 1452 mEntries.add(OpEntry.CREATOR.createFromParcel(source)); 1453 } 1454 } 1455 1456 public static final Creator<PackageOps> CREATOR = new Creator<PackageOps>() { 1457 @Override public PackageOps createFromParcel(Parcel source) { 1458 return new PackageOps(source); 1459 } 1460 1461 @Override public PackageOps[] newArray(int size) { 1462 return new PackageOps[size]; 1463 } 1464 }; 1465 } 1466 1467 /** 1468 * Class holding the information about one unique operation of an application. 1469 * @hide 1470 */ 1471 public static class OpEntry implements Parcelable { 1472 private final int mOp; 1473 private final int mMode; 1474 private final long mTime; 1475 private final long mRejectTime; 1476 private final int mDuration; 1477 private final int mProxyUid; 1478 private final String mProxyPackageName; 1479 1480 public OpEntry(int op, int mode, long time, long rejectTime, int duration, 1481 int proxyUid, String proxyPackage) { 1482 mOp = op; 1483 mMode = mode; 1484 mTime = time; 1485 mRejectTime = rejectTime; 1486 mDuration = duration; 1487 mProxyUid = proxyUid; 1488 mProxyPackageName = proxyPackage; 1489 } 1490 1491 public int getOp() { 1492 return mOp; 1493 } 1494 1495 public int getMode() { 1496 return mMode; 1497 } 1498 1499 public long getTime() { 1500 return mTime; 1501 } 1502 1503 public long getRejectTime() { 1504 return mRejectTime; 1505 } 1506 1507 public boolean isRunning() { 1508 return mDuration == -1; 1509 } 1510 1511 public int getDuration() { 1512 return mDuration == -1 ? (int)(System.currentTimeMillis()-mTime) : mDuration; 1513 } 1514 1515 public int getProxyUid() { 1516 return mProxyUid; 1517 } 1518 1519 public String getProxyPackageName() { 1520 return mProxyPackageName; 1521 } 1522 1523 @Override 1524 public int describeContents() { 1525 return 0; 1526 } 1527 1528 @Override 1529 public void writeToParcel(Parcel dest, int flags) { 1530 dest.writeInt(mOp); 1531 dest.writeInt(mMode); 1532 dest.writeLong(mTime); 1533 dest.writeLong(mRejectTime); 1534 dest.writeInt(mDuration); 1535 dest.writeInt(mProxyUid); 1536 dest.writeString(mProxyPackageName); 1537 } 1538 1539 OpEntry(Parcel source) { 1540 mOp = source.readInt(); 1541 mMode = source.readInt(); 1542 mTime = source.readLong(); 1543 mRejectTime = source.readLong(); 1544 mDuration = source.readInt(); 1545 mProxyUid = source.readInt(); 1546 mProxyPackageName = source.readString(); 1547 } 1548 1549 public static final Creator<OpEntry> CREATOR = new Creator<OpEntry>() { 1550 @Override public OpEntry createFromParcel(Parcel source) { 1551 return new OpEntry(source); 1552 } 1553 1554 @Override public OpEntry[] newArray(int size) { 1555 return new OpEntry[size]; 1556 } 1557 }; 1558 } 1559 1560 /** 1561 * Callback for notification of changes to operation state. 1562 */ 1563 public interface OnOpChangedListener { 1564 public void onOpChanged(String op, String packageName); 1565 } 1566 1567 /** 1568 * Callback for notification of changes to operation active state. 1569 * 1570 * @hide 1571 */ 1572 @TestApi 1573 public interface OnOpActiveChangedListener { 1574 /** 1575 * Called when the active state of an app op changes. 1576 * 1577 * @param code The op code. 1578 * @param uid The UID performing the operation. 1579 * @param packageName The package performing the operation. 1580 * @param active Whether the operation became active or inactive. 1581 */ 1582 void onOpActiveChanged(int code, int uid, String packageName, boolean active); 1583 } 1584 1585 /** 1586 * Callback for notification of changes to operation state. 1587 * This allows you to see the raw op codes instead of strings. 1588 * @hide 1589 */ 1590 public static class OnOpChangedInternalListener implements OnOpChangedListener { 1591 public void onOpChanged(String op, String packageName) { } 1592 public void onOpChanged(int op, String packageName) { } 1593 } 1594 1595 AppOpsManager(Context context, IAppOpsService service) { 1596 mContext = context; 1597 mService = service; 1598 } 1599 1600 /** 1601 * Retrieve current operation state for all applications. 1602 * 1603 * @param ops The set of operations you are interested in, or null if you want all of them. 1604 * @hide 1605 */ 1606 public List<AppOpsManager.PackageOps> getPackagesForOps(int[] ops) { 1607 try { 1608 return mService.getPackagesForOps(ops); 1609 } catch (RemoteException e) { 1610 throw e.rethrowFromSystemServer(); 1611 } 1612 } 1613 1614 /** 1615 * Retrieve current operation state for one application. 1616 * 1617 * @param uid The uid of the application of interest. 1618 * @param packageName The name of the application of interest. 1619 * @param ops The set of operations you are interested in, or null if you want all of them. 1620 * @hide 1621 */ 1622 public List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName, int[] ops) { 1623 try { 1624 return mService.getOpsForPackage(uid, packageName, ops); 1625 } catch (RemoteException e) { 1626 throw e.rethrowFromSystemServer(); 1627 } 1628 } 1629 1630 /** 1631 * Sets given app op in the specified mode for app ops in the UID. 1632 * This applies to all apps currently in the UID or installed in 1633 * this UID in the future. 1634 * 1635 * @param code The app op. 1636 * @param uid The UID for which to set the app. 1637 * @param mode The app op mode to set. 1638 * @hide 1639 */ 1640 @RequiresPermission(android.Manifest.permission.MANAGE_APP_OPS_MODES) 1641 public void setUidMode(int code, int uid, int mode) { 1642 try { 1643 mService.setUidMode(code, uid, mode); 1644 } catch (RemoteException e) { 1645 throw e.rethrowFromSystemServer(); 1646 } 1647 } 1648 1649 /** 1650 * Sets given app op in the specified mode for app ops in the UID. 1651 * This applies to all apps currently in the UID or installed in 1652 * this UID in the future. 1653 * 1654 * @param appOp The app op. 1655 * @param uid The UID for which to set the app. 1656 * @param mode The app op mode to set. 1657 * @hide 1658 */ 1659 @SystemApi 1660 @RequiresPermission(android.Manifest.permission.MANAGE_APP_OPS_MODES) 1661 public void setUidMode(String appOp, int uid, int mode) { 1662 try { 1663 mService.setUidMode(AppOpsManager.strOpToOp(appOp), uid, mode); 1664 } catch (RemoteException e) { 1665 throw e.rethrowFromSystemServer(); 1666 } 1667 } 1668 1669 /** @hide */ 1670 public void setUserRestriction(int code, boolean restricted, IBinder token) { 1671 setUserRestriction(code, restricted, token, /*exceptionPackages*/null); 1672 } 1673 1674 /** @hide */ 1675 public void setUserRestriction(int code, boolean restricted, IBinder token, 1676 String[] exceptionPackages) { 1677 setUserRestrictionForUser(code, restricted, token, exceptionPackages, mContext.getUserId()); 1678 } 1679 1680 /** @hide */ 1681 public void setUserRestrictionForUser(int code, boolean restricted, IBinder token, 1682 String[] exceptionPackages, int userId) { 1683 try { 1684 mService.setUserRestriction(code, restricted, token, userId, exceptionPackages); 1685 } catch (RemoteException e) { 1686 throw e.rethrowFromSystemServer(); 1687 } 1688 } 1689 1690 /** @hide */ 1691 @TestApi 1692 @RequiresPermission(android.Manifest.permission.MANAGE_APP_OPS_MODES) 1693 public void setMode(int code, int uid, String packageName, int mode) { 1694 try { 1695 mService.setMode(code, uid, packageName, mode); 1696 } catch (RemoteException e) { 1697 throw e.rethrowFromSystemServer(); 1698 } 1699 } 1700 1701 /** 1702 * Change the operating mode for the given op in the given app package. You must pass 1703 * in both the uid and name of the application whose mode is being modified; if these 1704 * do not match, the modification will not be applied. 1705 * 1706 * @param op The operation to modify. One of the OPSTR_* constants. 1707 * @param uid The user id of the application whose mode will be changed. 1708 * @param packageName The name of the application package name whose mode will 1709 * be changed. 1710 * @hide 1711 */ 1712 @SystemApi 1713 @RequiresPermission(android.Manifest.permission.MANAGE_APP_OPS_MODES) 1714 public void setMode(String op, int uid, String packageName, int mode) { 1715 try { 1716 mService.setMode(strOpToOp(op), uid, packageName, mode); 1717 } catch (RemoteException e) { 1718 throw e.rethrowFromSystemServer(); 1719 } 1720 } 1721 1722 /** 1723 * Set a non-persisted restriction on an audio operation at a stream-level. 1724 * Restrictions are temporary additional constraints imposed on top of the persisted rules 1725 * defined by {@link #setMode}. 1726 * 1727 * @param code The operation to restrict. 1728 * @param usage The {@link android.media.AudioAttributes} usage value. 1729 * @param mode The restriction mode (MODE_IGNORED,MODE_ERRORED) or MODE_ALLOWED to unrestrict. 1730 * @param exceptionPackages Optional list of packages to exclude from the restriction. 1731 * @hide 1732 */ 1733 @RequiresPermission(android.Manifest.permission.MANAGE_APP_OPS_MODES) 1734 public void setRestriction(int code, @AttributeUsage int usage, int mode, 1735 String[] exceptionPackages) { 1736 try { 1737 final int uid = Binder.getCallingUid(); 1738 mService.setAudioRestriction(code, usage, uid, mode, exceptionPackages); 1739 } catch (RemoteException e) { 1740 throw e.rethrowFromSystemServer(); 1741 } 1742 } 1743 1744 /** @hide */ 1745 @RequiresPermission(android.Manifest.permission.MANAGE_APP_OPS_MODES) 1746 public void resetAllModes() { 1747 try { 1748 mService.resetAllModes(mContext.getUserId(), null); 1749 } catch (RemoteException e) { 1750 throw e.rethrowFromSystemServer(); 1751 } 1752 } 1753 1754 /** 1755 * Gets the app op name associated with a given permission. 1756 * The app op name is one of the public constants defined 1757 * in this class such as {@link #OPSTR_COARSE_LOCATION}. 1758 * This API is intended to be used for mapping runtime 1759 * permissions to the corresponding app op. 1760 * 1761 * @param permission The permission. 1762 * @return The app op associated with the permission or null. 1763 */ 1764 public static String permissionToOp(String permission) { 1765 final Integer opCode = sPermToOp.get(permission); 1766 if (opCode == null) { 1767 return null; 1768 } 1769 return sOpToString[opCode]; 1770 } 1771 1772 /** 1773 * Monitor for changes to the operating mode for the given op in the given app package. 1774 * You can watch op changes only for your UID. 1775 * 1776 * @param op The operation to monitor, one of OPSTR_*. 1777 * @param packageName The name of the application to monitor. 1778 * @param callback Where to report changes. 1779 */ 1780 public void startWatchingMode(String op, String packageName, 1781 final OnOpChangedListener callback) { 1782 startWatchingMode(strOpToOp(op), packageName, callback); 1783 } 1784 1785 /** 1786 * Monitor for changes to the operating mode for the given op in the given app package. 1787 * 1788 * <p> If you don't hold the {@link android.Manifest.permission#WATCH_APPOPS} permission 1789 * you can watch changes only for your UID. 1790 * 1791 * @param op The operation to monitor, one of OP_*. 1792 * @param packageName The name of the application to monitor. 1793 * @param callback Where to report changes. 1794 * @hide 1795 */ 1796 @RequiresPermission(value=android.Manifest.permission.WATCH_APPOPS, conditional=true) 1797 public void startWatchingMode(int op, String packageName, final OnOpChangedListener callback) { 1798 synchronized (mModeWatchers) { 1799 IAppOpsCallback cb = mModeWatchers.get(callback); 1800 if (cb == null) { 1801 cb = new IAppOpsCallback.Stub() { 1802 public void opChanged(int op, int uid, String packageName) { 1803 if (callback instanceof OnOpChangedInternalListener) { 1804 ((OnOpChangedInternalListener)callback).onOpChanged(op, packageName); 1805 } 1806 if (sOpToString[op] != null) { 1807 callback.onOpChanged(sOpToString[op], packageName); 1808 } 1809 } 1810 }; 1811 mModeWatchers.put(callback, cb); 1812 } 1813 try { 1814 mService.startWatchingMode(op, packageName, cb); 1815 } catch (RemoteException e) { 1816 throw e.rethrowFromSystemServer(); 1817 } 1818 } 1819 } 1820 1821 /** 1822 * Stop monitoring that was previously started with {@link #startWatchingMode}. All 1823 * monitoring associated with this callback will be removed. 1824 */ 1825 public void stopWatchingMode(OnOpChangedListener callback) { 1826 synchronized (mModeWatchers) { 1827 IAppOpsCallback cb = mModeWatchers.get(callback); 1828 if (cb != null) { 1829 try { 1830 mService.stopWatchingMode(cb); 1831 } catch (RemoteException e) { 1832 throw e.rethrowFromSystemServer(); 1833 } 1834 } 1835 } 1836 } 1837 1838 /** 1839 * Start watching for changes to the active state of app ops. An app op may be 1840 * long running and it has a clear start and stop delimiters. If an op is being 1841 * started or stopped by any package you will get a callback. To change the 1842 * watched ops for a registered callback you need to unregister and register it 1843 * again. 1844 * 1845 * <p> If you don't hold the {@link android.Manifest.permission#WATCH_APPOPS} permission 1846 * you can watch changes only for your UID. 1847 * 1848 * @param ops The ops to watch. 1849 * @param callback Where to report changes. 1850 * 1851 * @see #isOperationActive(int, int, String) 1852 * @see #stopWatchingActive(OnOpActiveChangedListener) 1853 * @see #startOp(int, int, String) 1854 * @see #finishOp(int, int, String) 1855 * 1856 * @hide 1857 */ 1858 @TestApi 1859 // TODO: Uncomment below annotation once b/73559440 is fixed 1860 // @RequiresPermission(value=Manifest.permission.WATCH_APPOPS, conditional=true) 1861 public void startWatchingActive(@NonNull int[] ops, 1862 @NonNull OnOpActiveChangedListener callback) { 1863 Preconditions.checkNotNull(ops, "ops cannot be null"); 1864 Preconditions.checkNotNull(callback, "callback cannot be null"); 1865 IAppOpsActiveCallback cb; 1866 synchronized (mActiveWatchers) { 1867 cb = mActiveWatchers.get(callback); 1868 if (cb != null) { 1869 return; 1870 } 1871 cb = new IAppOpsActiveCallback.Stub() { 1872 @Override 1873 public void opActiveChanged(int op, int uid, String packageName, boolean active) { 1874 callback.onOpActiveChanged(op, uid, packageName, active); 1875 } 1876 }; 1877 mActiveWatchers.put(callback, cb); 1878 } 1879 try { 1880 mService.startWatchingActive(ops, cb); 1881 } catch (RemoteException e) { 1882 throw e.rethrowFromSystemServer(); 1883 } 1884 } 1885 1886 /** 1887 * Stop watching for changes to the active state of an app op. An app op may be 1888 * long running and it has a clear start and stop delimiters. Unregistering a 1889 * non-registered callback has no effect. 1890 * 1891 * @see #isOperationActive#(int, int, String) 1892 * @see #startWatchingActive(int[], OnOpActiveChangedListener) 1893 * @see #startOp(int, int, String) 1894 * @see #finishOp(int, int, String) 1895 * 1896 * @hide 1897 */ 1898 @TestApi 1899 public void stopWatchingActive(@NonNull OnOpActiveChangedListener callback) { 1900 synchronized (mActiveWatchers) { 1901 final IAppOpsActiveCallback cb = mActiveWatchers.get(callback); 1902 if (cb != null) { 1903 try { 1904 mService.stopWatchingActive(cb); 1905 } catch (RemoteException e) { 1906 throw e.rethrowFromSystemServer(); 1907 } 1908 } 1909 } 1910 } 1911 1912 private String buildSecurityExceptionMsg(int op, int uid, String packageName) { 1913 return packageName + " from uid " + uid + " not allowed to perform " + sOpNames[op]; 1914 } 1915 1916 /** 1917 * {@hide} 1918 */ 1919 public static int strOpToOp(String op) { 1920 Integer val = sOpStrToOp.get(op); 1921 if (val == null) { 1922 throw new IllegalArgumentException("Unknown operation string: " + op); 1923 } 1924 return val; 1925 } 1926 1927 /** 1928 * Do a quick check for whether an application might be able to perform an operation. 1929 * This is <em>not</em> a security check; you must use {@link #noteOp(String, int, String)} 1930 * or {@link #startOp(String, int, String)} for your actual security checks, which also 1931 * ensure that the given uid and package name are consistent. This function can just be 1932 * used for a quick check to see if an operation has been disabled for the application, 1933 * as an early reject of some work. This does not modify the time stamp or other data 1934 * about the operation. 1935 * @param op The operation to check. One of the OPSTR_* constants. 1936 * @param uid The user id of the application attempting to perform the operation. 1937 * @param packageName The name of the application attempting to perform the operation. 1938 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or 1939 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without 1940 * causing the app to crash). 1941 * @throws SecurityException If the app has been configured to crash on this op. 1942 */ 1943 public int checkOp(String op, int uid, String packageName) { 1944 return checkOp(strOpToOp(op), uid, packageName); 1945 } 1946 1947 /** 1948 * Like {@link #checkOp} but instead of throwing a {@link SecurityException} it 1949 * returns {@link #MODE_ERRORED}. 1950 */ 1951 public int checkOpNoThrow(String op, int uid, String packageName) { 1952 return checkOpNoThrow(strOpToOp(op), uid, packageName); 1953 } 1954 1955 /** 1956 * Make note of an application performing an operation. Note that you must pass 1957 * in both the uid and name of the application to be checked; this function will verify 1958 * that these two match, and if not, return {@link #MODE_IGNORED}. If this call 1959 * succeeds, the last execution time of the operation for this app will be updated to 1960 * the current time. 1961 * @param op The operation to note. One of the OPSTR_* constants. 1962 * @param uid The user id of the application attempting to perform the operation. 1963 * @param packageName The name of the application attempting to perform the operation. 1964 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or 1965 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without 1966 * causing the app to crash). 1967 * @throws SecurityException If the app has been configured to crash on this op. 1968 */ 1969 public int noteOp(String op, int uid, String packageName) { 1970 return noteOp(strOpToOp(op), uid, packageName); 1971 } 1972 1973 /** 1974 * Like {@link #noteOp} but instead of throwing a {@link SecurityException} it 1975 * returns {@link #MODE_ERRORED}. 1976 */ 1977 public int noteOpNoThrow(String op, int uid, String packageName) { 1978 return noteOpNoThrow(strOpToOp(op), uid, packageName); 1979 } 1980 1981 /** 1982 * Make note of an application performing an operation on behalf of another 1983 * application when handling an IPC. Note that you must pass the package name 1984 * of the application that is being proxied while its UID will be inferred from 1985 * the IPC state; this function will verify that the calling uid and proxied 1986 * package name match, and if not, return {@link #MODE_IGNORED}. If this call 1987 * succeeds, the last execution time of the operation for the proxied app and 1988 * your app will be updated to the current time. 1989 * @param op The operation to note. One of the OPSTR_* constants. 1990 * @param proxiedPackageName The name of the application calling into the proxy application. 1991 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or 1992 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without 1993 * causing the app to crash). 1994 * @throws SecurityException If the app has been configured to crash on this op. 1995 */ 1996 public int noteProxyOp(String op, String proxiedPackageName) { 1997 return noteProxyOp(strOpToOp(op), proxiedPackageName); 1998 } 1999 2000 /** 2001 * Like {@link #noteProxyOp(String, String)} but instead 2002 * of throwing a {@link SecurityException} it returns {@link #MODE_ERRORED}. 2003 */ 2004 public int noteProxyOpNoThrow(String op, String proxiedPackageName) { 2005 return noteProxyOpNoThrow(strOpToOp(op), proxiedPackageName); 2006 } 2007 2008 /** 2009 * Report that an application has started executing a long-running operation. Note that you 2010 * must pass in both the uid and name of the application to be checked; this function will 2011 * verify that these two match, and if not, return {@link #MODE_IGNORED}. If this call 2012 * succeeds, the last execution time of the operation for this app will be updated to 2013 * the current time and the operation will be marked as "running". In this case you must 2014 * later call {@link #finishOp(String, int, String)} to report when the application is no 2015 * longer performing the operation. 2016 * @param op The operation to start. One of the OPSTR_* constants. 2017 * @param uid The user id of the application attempting to perform the operation. 2018 * @param packageName The name of the application attempting to perform the operation. 2019 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or 2020 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without 2021 * causing the app to crash). 2022 * @throws SecurityException If the app has been configured to crash on this op. 2023 */ 2024 public int startOp(String op, int uid, String packageName) { 2025 return startOp(strOpToOp(op), uid, packageName); 2026 } 2027 2028 /** 2029 * Like {@link #startOp} but instead of throwing a {@link SecurityException} it 2030 * returns {@link #MODE_ERRORED}. 2031 */ 2032 public int startOpNoThrow(String op, int uid, String packageName) { 2033 return startOpNoThrow(strOpToOp(op), uid, packageName); 2034 } 2035 2036 /** 2037 * Report that an application is no longer performing an operation that had previously 2038 * been started with {@link #startOp(String, int, String)}. There is no validation of input 2039 * or result; the parameters supplied here must be the exact same ones previously passed 2040 * in when starting the operation. 2041 */ 2042 public void finishOp(String op, int uid, String packageName) { 2043 finishOp(strOpToOp(op), uid, packageName); 2044 } 2045 2046 /** 2047 * Do a quick check for whether an application might be able to perform an operation. 2048 * This is <em>not</em> a security check; you must use {@link #noteOp(int, int, String)} 2049 * or {@link #startOp(int, int, String)} for your actual security checks, which also 2050 * ensure that the given uid and package name are consistent. This function can just be 2051 * used for a quick check to see if an operation has been disabled for the application, 2052 * as an early reject of some work. This does not modify the time stamp or other data 2053 * about the operation. 2054 * @param op The operation to check. One of the OP_* constants. 2055 * @param uid The user id of the application attempting to perform the operation. 2056 * @param packageName The name of the application attempting to perform the operation. 2057 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or 2058 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without 2059 * causing the app to crash). 2060 * @throws SecurityException If the app has been configured to crash on this op. 2061 * @hide 2062 */ 2063 public int checkOp(int op, int uid, String packageName) { 2064 try { 2065 int mode = mService.checkOperation(op, uid, packageName); 2066 if (mode == MODE_ERRORED) { 2067 throw new SecurityException(buildSecurityExceptionMsg(op, uid, packageName)); 2068 } 2069 return mode; 2070 } catch (RemoteException e) { 2071 throw e.rethrowFromSystemServer(); 2072 } 2073 } 2074 2075 /** 2076 * Like {@link #checkOp} but instead of throwing a {@link SecurityException} it 2077 * returns {@link #MODE_ERRORED}. 2078 * @hide 2079 */ 2080 public int checkOpNoThrow(int op, int uid, String packageName) { 2081 try { 2082 return mService.checkOperation(op, uid, packageName); 2083 } catch (RemoteException e) { 2084 throw e.rethrowFromSystemServer(); 2085 } 2086 } 2087 2088 /** 2089 * Do a quick check to validate if a package name belongs to a UID. 2090 * 2091 * @throws SecurityException if the package name doesn't belong to the given 2092 * UID, or if ownership cannot be verified. 2093 */ 2094 public void checkPackage(int uid, String packageName) { 2095 try { 2096 if (mService.checkPackage(uid, packageName) != MODE_ALLOWED) { 2097 throw new SecurityException( 2098 "Package " + packageName + " does not belong to " + uid); 2099 } 2100 } catch (RemoteException e) { 2101 throw e.rethrowFromSystemServer(); 2102 } 2103 } 2104 2105 /** 2106 * Like {@link #checkOp} but at a stream-level for audio operations. 2107 * @hide 2108 */ 2109 public int checkAudioOp(int op, int stream, int uid, String packageName) { 2110 try { 2111 final int mode = mService.checkAudioOperation(op, stream, uid, packageName); 2112 if (mode == MODE_ERRORED) { 2113 throw new SecurityException(buildSecurityExceptionMsg(op, uid, packageName)); 2114 } 2115 return mode; 2116 } catch (RemoteException e) { 2117 throw e.rethrowFromSystemServer(); 2118 } 2119 } 2120 2121 /** 2122 * Like {@link #checkAudioOp} but instead of throwing a {@link SecurityException} it 2123 * returns {@link #MODE_ERRORED}. 2124 * @hide 2125 */ 2126 public int checkAudioOpNoThrow(int op, int stream, int uid, String packageName) { 2127 try { 2128 return mService.checkAudioOperation(op, stream, uid, packageName); 2129 } catch (RemoteException e) { 2130 throw e.rethrowFromSystemServer(); 2131 } 2132 } 2133 2134 /** 2135 * Make note of an application performing an operation. Note that you must pass 2136 * in both the uid and name of the application to be checked; this function will verify 2137 * that these two match, and if not, return {@link #MODE_IGNORED}. If this call 2138 * succeeds, the last execution time of the operation for this app will be updated to 2139 * the current time. 2140 * @param op The operation to note. One of the OP_* constants. 2141 * @param uid The user id of the application attempting to perform the operation. 2142 * @param packageName The name of the application attempting to perform the operation. 2143 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or 2144 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without 2145 * causing the app to crash). 2146 * @throws SecurityException If the app has been configured to crash on this op. 2147 * @hide 2148 */ 2149 public int noteOp(int op, int uid, String packageName) { 2150 final int mode = noteOpNoThrow(op, uid, packageName); 2151 if (mode == MODE_ERRORED) { 2152 throw new SecurityException(buildSecurityExceptionMsg(op, uid, packageName)); 2153 } 2154 return mode; 2155 } 2156 2157 /** 2158 * Make note of an application performing an operation on behalf of another 2159 * application when handling an IPC. Note that you must pass the package name 2160 * of the application that is being proxied while its UID will be inferred from 2161 * the IPC state; this function will verify that the calling uid and proxied 2162 * package name match, and if not, return {@link #MODE_IGNORED}. If this call 2163 * succeeds, the last execution time of the operation for the proxied app and 2164 * your app will be updated to the current time. 2165 * @param op The operation to note. One of the OPSTR_* constants. 2166 * @param proxiedPackageName The name of the application calling into the proxy application. 2167 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or 2168 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without 2169 * causing the app to crash). 2170 * @throws SecurityException If the proxy or proxied app has been configured to 2171 * crash on this op. 2172 * 2173 * @hide 2174 */ 2175 public int noteProxyOp(int op, String proxiedPackageName) { 2176 int mode = noteProxyOpNoThrow(op, proxiedPackageName); 2177 if (mode == MODE_ERRORED) { 2178 throw new SecurityException("Proxy package " + mContext.getOpPackageName() 2179 + " from uid " + Process.myUid() + " or calling package " 2180 + proxiedPackageName + " from uid " + Binder.getCallingUid() 2181 + " not allowed to perform " + sOpNames[op]); 2182 } 2183 return mode; 2184 } 2185 2186 /** 2187 * Like {@link #noteProxyOp(int, String)} but instead 2188 * of throwing a {@link SecurityException} it returns {@link #MODE_ERRORED}. 2189 * @hide 2190 */ 2191 public int noteProxyOpNoThrow(int op, String proxiedPackageName) { 2192 try { 2193 return mService.noteProxyOperation(op, mContext.getOpPackageName(), 2194 Binder.getCallingUid(), proxiedPackageName); 2195 } catch (RemoteException e) { 2196 throw e.rethrowFromSystemServer(); 2197 } 2198 } 2199 2200 /** 2201 * Like {@link #noteOp} but instead of throwing a {@link SecurityException} it 2202 * returns {@link #MODE_ERRORED}. 2203 * @hide 2204 */ 2205 public int noteOpNoThrow(int op, int uid, String packageName) { 2206 try { 2207 return mService.noteOperation(op, uid, packageName); 2208 } catch (RemoteException e) { 2209 throw e.rethrowFromSystemServer(); 2210 } 2211 } 2212 2213 /** @hide */ 2214 public int noteOp(int op) { 2215 return noteOp(op, Process.myUid(), mContext.getOpPackageName()); 2216 } 2217 2218 /** @hide */ 2219 public static IBinder getToken(IAppOpsService service) { 2220 synchronized (AppOpsManager.class) { 2221 if (sToken != null) { 2222 return sToken; 2223 } 2224 try { 2225 sToken = service.getToken(new Binder()); 2226 } catch (RemoteException e) { 2227 throw e.rethrowFromSystemServer(); 2228 } 2229 return sToken; 2230 } 2231 } 2232 2233 /** @hide */ 2234 public int startOp(int op) { 2235 return startOp(op, Process.myUid(), mContext.getOpPackageName()); 2236 } 2237 2238 /** 2239 * Report that an application has started executing a long-running operation. Note that you 2240 * must pass in both the uid and name of the application to be checked; this function will 2241 * verify that these two match, and if not, return {@link #MODE_IGNORED}. If this call 2242 * succeeds, the last execution time of the operation for this app will be updated to 2243 * the current time and the operation will be marked as "running". In this case you must 2244 * later call {@link #finishOp(int, int, String)} to report when the application is no 2245 * longer performing the operation. 2246 * 2247 * @param op The operation to start. One of the OP_* constants. 2248 * @param uid The user id of the application attempting to perform the operation. 2249 * @param packageName The name of the application attempting to perform the operation. 2250 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or 2251 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without 2252 * causing the app to crash). 2253 * @throws SecurityException If the app has been configured to crash on this op. 2254 * @hide 2255 */ 2256 public int startOp(int op, int uid, String packageName) { 2257 return startOp(op, uid, packageName, false); 2258 } 2259 2260 /** 2261 * Report that an application has started executing a long-running operation. Similar 2262 * to {@link #startOp(String, int, String) except that if the mode is {@link #MODE_DEFAULT} 2263 * the operation should succeed since the caller has performed its standard permission 2264 * checks which passed and would perform the protected operation for this mode. 2265 * 2266 * @param op The operation to start. One of the OP_* constants. 2267 * @param uid The user id of the application attempting to perform the operation. 2268 * @param packageName The name of the application attempting to perform the operation. 2269 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or 2270 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without 2271 * causing the app to crash). 2272 * @param startIfModeDefault Whether to start if mode is {@link #MODE_DEFAULT}. 2273 * 2274 * @throws SecurityException If the app has been configured to crash on this op or 2275 * the package is not in the passed in UID. 2276 * 2277 * @hide 2278 */ 2279 public int startOp(int op, int uid, String packageName, boolean startIfModeDefault) { 2280 final int mode = startOpNoThrow(op, uid, packageName, startIfModeDefault); 2281 if (mode == MODE_ERRORED) { 2282 throw new SecurityException(buildSecurityExceptionMsg(op, uid, packageName)); 2283 } 2284 return mode; 2285 } 2286 2287 /** 2288 * Like {@link #startOp} but instead of throwing a {@link SecurityException} it 2289 * returns {@link #MODE_ERRORED}. 2290 * @hide 2291 */ 2292 public int startOpNoThrow(int op, int uid, String packageName) { 2293 return startOpNoThrow(op, uid, packageName, false); 2294 } 2295 2296 /** 2297 * Like {@link #startOp(int, int, String, boolean)} but instead of throwing a 2298 * {@link SecurityException} it returns {@link #MODE_ERRORED}. 2299 * 2300 * @param op The operation to start. One of the OP_* constants. 2301 * @param uid The user id of the application attempting to perform the operation. 2302 * @param packageName The name of the application attempting to perform the operation. 2303 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or 2304 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without 2305 * causing the app to crash). 2306 * @param startIfModeDefault Whether to start if mode is {@link #MODE_DEFAULT}. 2307 * 2308 * @hide 2309 */ 2310 public int startOpNoThrow(int op, int uid, String packageName, boolean startIfModeDefault) { 2311 try { 2312 return mService.startOperation(getToken(mService), op, uid, packageName, 2313 startIfModeDefault); 2314 } catch (RemoteException e) { 2315 throw e.rethrowFromSystemServer(); 2316 } 2317 } 2318 2319 /** 2320 * Report that an application is no longer performing an operation that had previously 2321 * been started with {@link #startOp(int, int, String)}. There is no validation of input 2322 * or result; the parameters supplied here must be the exact same ones previously passed 2323 * in when starting the operation. 2324 * @hide 2325 */ 2326 public void finishOp(int op, int uid, String packageName) { 2327 try { 2328 mService.finishOperation(getToken(mService), op, uid, packageName); 2329 } catch (RemoteException e) { 2330 throw e.rethrowFromSystemServer(); 2331 } 2332 } 2333 2334 /** @hide */ 2335 public void finishOp(int op) { 2336 finishOp(op, Process.myUid(), mContext.getOpPackageName()); 2337 } 2338 2339 /** 2340 * Checks whether the given op for a UID and package is active. 2341 * 2342 * <p> If you don't hold the {@link android.Manifest.permission#WATCH_APPOPS} permission 2343 * you can query only for your UID. 2344 * 2345 * @see #startWatchingActive(int[], OnOpActiveChangedListener) 2346 * @see #stopWatchingMode(OnOpChangedListener) 2347 * @see #finishOp(int) 2348 * @see #startOp(int) 2349 * 2350 * @hide */ 2351 @TestApi 2352 // TODO: Uncomment below annotation once b/73559440 is fixed 2353 // @RequiresPermission(value=Manifest.permission.WATCH_APPOPS, conditional=true) 2354 public boolean isOperationActive(int code, int uid, String packageName) { 2355 try { 2356 return mService.isOperationActive(code, uid, packageName); 2357 } catch (RemoteException e) { 2358 throw e.rethrowFromSystemServer(); 2359 } 2360 } 2361 2362 /** 2363 * Returns all supported operation names. 2364 * @hide 2365 */ 2366 @SystemApi 2367 @TestApi 2368 public static String[] getOpStrs() { 2369 return Arrays.copyOf(sOpToString, sOpToString.length); 2370 } 2371} 2372