Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 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 | |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 17 | package com.android.server.appop; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 18 | |
Hui Yu | 85679b4 | 2020-01-15 15:52:08 -0800 | [diff] [blame] | 19 | import static android.app.ActivityManager.PROCESS_CAPABILITY_FOREGROUND_CAMERA; |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 20 | import static android.app.ActivityManager.PROCESS_CAPABILITY_FOREGROUND_LOCATION; |
Hui Yu | 85679b4 | 2020-01-15 15:52:08 -0800 | [diff] [blame] | 21 | import static android.app.ActivityManager.PROCESS_CAPABILITY_FOREGROUND_MICROPHONE; |
Philip P. Moltmann | 7641cd0 | 2020-03-11 14:42:43 -0700 | [diff] [blame] | 22 | import static android.app.AppOpsManager.CALL_BACK_ON_SWITCHED_OP; |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 23 | import static android.app.AppOpsManager.FILTER_BY_ATTRIBUTION_TAG; |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 24 | import static android.app.AppOpsManager.FILTER_BY_OP_NAMES; |
| 25 | import static android.app.AppOpsManager.FILTER_BY_PACKAGE_NAME; |
| 26 | import static android.app.AppOpsManager.FILTER_BY_UID; |
| 27 | import static android.app.AppOpsManager.HistoricalOpsRequestFilter; |
Philip P. Moltmann | e565c7b | 2020-01-28 13:15:52 -0800 | [diff] [blame] | 28 | import static android.app.AppOpsManager.KEY_BG_STATE_SETTLE_TIME; |
| 29 | import static android.app.AppOpsManager.KEY_FG_SERVICE_STATE_SETTLE_TIME; |
| 30 | import static android.app.AppOpsManager.KEY_TOP_STATE_SETTLE_TIME; |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 31 | import static android.app.AppOpsManager.MODE_ALLOWED; |
Hui Yu | bb866ad | 2020-03-04 12:33:40 -0800 | [diff] [blame] | 32 | import static android.app.AppOpsManager.MODE_FOREGROUND; |
| 33 | import static android.app.AppOpsManager.MODE_IGNORED; |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 34 | import static android.app.AppOpsManager.NoteOpEvent; |
Suprabh Shukla | 7e01792 | 2019-08-05 17:13:23 -0700 | [diff] [blame] | 35 | import static android.app.AppOpsManager.OP_CAMERA; |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 36 | import static android.app.AppOpsManager.OP_FLAGS_ALL; |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 37 | import static android.app.AppOpsManager.OP_FLAG_SELF; |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 38 | import static android.app.AppOpsManager.OP_FLAG_TRUSTED_PROXIED; |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 39 | import static android.app.AppOpsManager.OP_NONE; |
Philip P. Moltmann | dde0785 | 2019-01-25 16:42:36 -0800 | [diff] [blame] | 40 | import static android.app.AppOpsManager.OP_PLAY_AUDIO; |
Suprabh Shukla | 7e01792 | 2019-08-05 17:13:23 -0700 | [diff] [blame] | 41 | import static android.app.AppOpsManager.OP_RECORD_AUDIO; |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 42 | import static android.app.AppOpsManager.OpEventProxyInfo; |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 43 | import static android.app.AppOpsManager.RestrictionBypass; |
Stanislav Zholnin | 4c32385 | 2020-03-04 18:03:24 +0000 | [diff] [blame] | 44 | import static android.app.AppOpsManager.SAMPLING_STRATEGY_BOOT_TIME_SAMPLING; |
Muhammad Qureshi | 317061a | 2020-03-02 13:15:51 -0800 | [diff] [blame] | 45 | import static android.app.AppOpsManager.SAMPLING_STRATEGY_RARELY_USED; |
| 46 | import static android.app.AppOpsManager.SAMPLING_STRATEGY_UNIFORM; |
Hai Zhang | 2b98fb3 | 2018-09-21 15:18:46 -0700 | [diff] [blame] | 47 | import static android.app.AppOpsManager.UID_STATE_BACKGROUND; |
| 48 | import static android.app.AppOpsManager.UID_STATE_CACHED; |
| 49 | import static android.app.AppOpsManager.UID_STATE_FOREGROUND; |
| 50 | import static android.app.AppOpsManager.UID_STATE_FOREGROUND_SERVICE; |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 51 | import static android.app.AppOpsManager.UID_STATE_MAX_LAST_NON_RESTRICTED; |
Hai Zhang | 2b98fb3 | 2018-09-21 15:18:46 -0700 | [diff] [blame] | 52 | import static android.app.AppOpsManager.UID_STATE_PERSISTENT; |
| 53 | import static android.app.AppOpsManager.UID_STATE_TOP; |
Philip P. Moltmann | 584c23a | 2020-01-24 14:21:12 -0800 | [diff] [blame] | 54 | import static android.app.AppOpsManager.WATCH_FOREGROUND_CHANGES; |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 55 | import static android.app.AppOpsManager._NUM_OP; |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 56 | import static android.app.AppOpsManager.extractFlagsFromKey; |
| 57 | import static android.app.AppOpsManager.extractUidStateFromKey; |
| 58 | import static android.app.AppOpsManager.makeKey; |
Eugene Susla | e4ee2c2 | 2018-11-05 12:23:30 -0800 | [diff] [blame] | 59 | import static android.app.AppOpsManager.modeToName; |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 60 | import static android.app.AppOpsManager.opAllowSystemBypassRestriction; |
Eugene Susla | e4ee2c2 | 2018-11-05 12:23:30 -0800 | [diff] [blame] | 61 | import static android.app.AppOpsManager.opToName; |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 62 | import static android.app.AppOpsManager.opToPublicName; |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 63 | import static android.app.AppOpsManager.resolveFirstUnrestrictedUidState; |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 64 | import static android.content.Intent.ACTION_PACKAGE_REMOVED; |
| 65 | import static android.content.Intent.EXTRA_REPLACING; |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 66 | import static android.content.pm.PermissionInfo.PROTECTION_DANGEROUS; |
Philip P. Moltmann | 87fd8bc | 2020-02-13 13:16:13 -0800 | [diff] [blame] | 67 | import static android.content.pm.PermissionInfo.PROTECTION_FLAG_APPOP; |
Hai Zhang | 2b98fb3 | 2018-09-21 15:18:46 -0700 | [diff] [blame] | 68 | |
Hui Yu | 77fd819 | 2020-03-27 00:22:57 -0700 | [diff] [blame] | 69 | import static com.android.server.am.OomAdjuster.DEBUG_PROCESS_CAPABILITY_FOREGROUND_CAMERA; |
| 70 | import static com.android.server.am.OomAdjuster.DEBUG_PROCESS_CAPABILITY_FOREGROUND_CAMERA_Q; |
| 71 | import static com.android.server.am.OomAdjuster.DEBUG_PROCESS_CAPABILITY_FOREGROUND_LOCATION; |
| 72 | import static com.android.server.am.OomAdjuster.DEBUG_PROCESS_CAPABILITY_FOREGROUND_MICROPHONE; |
| 73 | import static com.android.server.am.OomAdjuster.DEBUG_PROCESS_CAPABILITY_FOREGROUND_MICROPHONE_Q; |
Philip P. Moltmann | b2bc651 | 2020-01-24 15:34:34 -0800 | [diff] [blame] | 74 | import static com.android.server.appop.AppOpsService.ModeCallback.ALL_OPS; |
| 75 | |
Philip P. Moltmann | 463979e | 2020-01-24 14:41:22 -0800 | [diff] [blame] | 76 | import static java.lang.Long.max; |
| 77 | |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 78 | import android.Manifest; |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 79 | import android.annotation.IntRange; |
Svet Ganov | ad0a49b | 2018-10-29 10:07:08 -0700 | [diff] [blame] | 80 | import android.annotation.NonNull; |
| 81 | import android.annotation.Nullable; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 82 | import android.app.ActivityManager; |
Nate Myren | 697650b | 2020-01-23 13:25:06 -0800 | [diff] [blame] | 83 | import android.app.ActivityManagerInternal; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 84 | import android.app.AppGlobals; |
| 85 | import android.app.AppOpsManager; |
Philip P. Moltmann | 5ab15b0 | 2020-03-13 15:44:51 -0700 | [diff] [blame] | 86 | import android.app.AppOpsManager.AttributedOpEntry; |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 87 | import android.app.AppOpsManager.HistoricalOps; |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 88 | import android.app.AppOpsManager.Mode; |
| 89 | import android.app.AppOpsManager.OpEntry; |
| 90 | import android.app.AppOpsManager.OpFlags; |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 91 | import android.app.AppOpsManagerInternal; |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 92 | import android.app.AppOpsManagerInternal.CheckOpsDelegate; |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 93 | import android.app.AsyncNotedAppOp; |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 94 | import android.app.RuntimeAppOpAccessMessage; |
| 95 | import android.app.SyncNotedAppOp; |
Suprabh Shukla | 3017fe4 | 2018-11-08 19:00:01 -0800 | [diff] [blame] | 96 | import android.content.BroadcastReceiver; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 97 | import android.content.ContentResolver; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 98 | import android.content.Context; |
Suprabh Shukla | 3017fe4 | 2018-11-08 19:00:01 -0800 | [diff] [blame] | 99 | import android.content.Intent; |
| 100 | import android.content.IntentFilter; |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 101 | import android.content.pm.PackageInfo; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 102 | import android.content.pm.PackageManager; |
| 103 | import android.content.pm.PackageManagerInternal; |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 104 | import android.content.pm.PermissionInfo; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 105 | import android.content.pm.UserInfo; |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 106 | import android.content.pm.parsing.component.ParsedAttribution; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 107 | import android.database.ContentObserver; |
Yin-Chia Yeh | 51d8516 | 2019-08-06 15:31:39 -0700 | [diff] [blame] | 108 | import android.hardware.camera2.CameraDevice.CAMERA_AUDIO_RESTRICTION; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 109 | import android.net.Uri; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 110 | import android.os.AsyncTask; |
| 111 | import android.os.Binder; |
Hai Zhang | 23d30a2 | 2020-02-04 11:06:31 -0800 | [diff] [blame] | 112 | import android.os.Build; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 113 | import android.os.Bundle; |
| 114 | import android.os.Handler; |
| 115 | import android.os.IBinder; |
| 116 | import android.os.Process; |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 117 | import android.os.RemoteCallback; |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 118 | import android.os.RemoteCallbackList; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 119 | import android.os.RemoteException; |
| 120 | import android.os.ResultReceiver; |
| 121 | import android.os.ServiceManager; |
| 122 | import android.os.ShellCallback; |
| 123 | import android.os.ShellCommand; |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 124 | import android.os.SystemClock; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 125 | import android.os.UserHandle; |
| 126 | import android.os.UserManager; |
Sudheer Shanka | 98cb3f0 | 2018-08-17 16:10:29 -0700 | [diff] [blame] | 127 | import android.os.storage.StorageManager; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 128 | import android.os.storage.StorageManagerInternal; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 129 | import android.provider.Settings; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 130 | import android.util.ArrayMap; |
| 131 | import android.util.ArraySet; |
| 132 | import android.util.AtomicFile; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 133 | import android.util.KeyValueListParser; |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 134 | import android.util.LongSparseArray; |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 135 | import android.util.Pair; |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 136 | import android.util.Pools.SimplePool; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 137 | import android.util.Slog; |
| 138 | import android.util.SparseArray; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 139 | import android.util.SparseBooleanArray; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 140 | import android.util.SparseIntArray; |
| 141 | import android.util.TimeUtils; |
| 142 | import android.util.Xml; |
| 143 | |
Todd Kennedy | 556efba | 2018-11-15 07:43:55 -0800 | [diff] [blame] | 144 | import com.android.internal.annotations.GuardedBy; |
David Cheung | 2ead966 | 2020-02-19 16:11:06 -0800 | [diff] [blame] | 145 | import com.android.internal.annotations.Immutable; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 146 | import com.android.internal.annotations.VisibleForTesting; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 147 | import com.android.internal.app.IAppOpsActiveCallback; |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 148 | import com.android.internal.app.IAppOpsAsyncNotedCallback; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 149 | import com.android.internal.app.IAppOpsCallback; |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 150 | import com.android.internal.app.IAppOpsNotedCallback; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 151 | import com.android.internal.app.IAppOpsService; |
Adam Bookatz | 182862e | 2020-04-27 21:58:22 -0700 | [diff] [blame] | 152 | import com.android.internal.app.IAppOpsStartedCallback; |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 153 | import com.android.internal.app.MessageSamplingConfig; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 154 | import com.android.internal.os.Zygote; |
| 155 | import com.android.internal.util.ArrayUtils; |
| 156 | import com.android.internal.util.DumpUtils; |
| 157 | import com.android.internal.util.FastXmlSerializer; |
| 158 | import com.android.internal.util.Preconditions; |
| 159 | import com.android.internal.util.XmlUtils; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 160 | import com.android.internal.util.function.pooled.PooledLambda; |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 161 | import com.android.server.LocalServices; |
| 162 | import com.android.server.LockGuard; |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 163 | import com.android.server.SystemServerInitThreadPool; |
David Cheung | 2ead966 | 2020-02-19 16:11:06 -0800 | [diff] [blame] | 164 | import com.android.server.SystemServiceManager; |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 165 | import com.android.server.pm.PackageList; |
Winson | f00c755 | 2020-01-28 12:52:01 -0800 | [diff] [blame] | 166 | import com.android.server.pm.parsing.pkg.AndroidPackage; |
Philip P. Moltmann | dde0785 | 2019-01-25 16:42:36 -0800 | [diff] [blame] | 167 | |
Philip P. Moltmann | 02cd91b | 2019-11-05 14:29:52 -0800 | [diff] [blame] | 168 | import libcore.util.EmptyArray; |
| 169 | |
David Cheung | 2ead966 | 2020-02-19 16:11:06 -0800 | [diff] [blame] | 170 | import org.json.JSONException; |
| 171 | import org.json.JSONObject; |
Philip P. Moltmann | 02cd91b | 2019-11-05 14:29:52 -0800 | [diff] [blame] | 172 | import org.xmlpull.v1.XmlPullParser; |
| 173 | import org.xmlpull.v1.XmlPullParserException; |
| 174 | import org.xmlpull.v1.XmlSerializer; |
| 175 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 176 | import java.io.File; |
| 177 | import java.io.FileDescriptor; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 178 | import java.io.FileInputStream; |
| 179 | import java.io.FileNotFoundException; |
| 180 | import java.io.FileOutputStream; |
David Cheung | 2ead966 | 2020-02-19 16:11:06 -0800 | [diff] [blame] | 181 | import java.io.FileWriter; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 182 | import java.io.IOException; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 183 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 184 | import java.nio.charset.StandardCharsets; |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 185 | import java.text.SimpleDateFormat; |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 186 | import java.time.Instant; |
| 187 | import java.time.temporal.ChronoUnit; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 188 | import java.util.ArrayList; |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 189 | import java.util.Arrays; |
Svetoslav | 215b44a | 2015-08-04 19:03:40 -0700 | [diff] [blame] | 190 | import java.util.Collections; |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 191 | import java.util.Date; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 192 | import java.util.HashMap; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 193 | import java.util.Iterator; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 194 | import java.util.List; |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 195 | import java.util.Map; |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 196 | import java.util.Objects; |
David Cheung | 2ead966 | 2020-02-19 16:11:06 -0800 | [diff] [blame] | 197 | import java.util.Scanner; |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 198 | import java.util.concurrent.ThreadLocalRandom; |
| 199 | import java.util.function.Consumer; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 200 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 201 | public class AppOpsService extends IAppOpsService.Stub { |
| 202 | static final String TAG = "AppOps"; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 203 | static final boolean DEBUG = false; |
| 204 | |
David Cheung | 2ead966 | 2020-02-19 16:11:06 -0800 | [diff] [blame] | 205 | /** |
| 206 | * Used for data access validation collection, we wish to only log a specific access once |
| 207 | */ |
| 208 | private final ArraySet<NoteOpTrace> mNoteOpCallerStacktraces = new ArraySet<>(); |
| 209 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 210 | private static final int NO_VERSION = -1; |
| 211 | /** Increment by one every time and add the corresponding upgrade logic in |
| 212 | * {@link #upgradeLocked(int)} below. The first version was 1 */ |
| 213 | private static final int CURRENT_VERSION = 1; |
| 214 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 215 | // Write at most every 30 minutes. |
| 216 | static final long WRITE_DELAY = DEBUG ? 1000 : 30*60*1000; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 217 | |
Svet Ganov | 3a95f83 | 2018-03-23 17:44:30 -0700 | [diff] [blame] | 218 | // Constant meaning that any UID should be matched when dispatching callbacks |
| 219 | private static final int UID_ANY = -2; |
| 220 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 221 | // Map from process states to the uid states we track. |
| 222 | private static final int[] PROCESS_STATE_TO_UID_STATE = new int[] { |
| 223 | UID_STATE_PERSISTENT, // ActivityManager.PROCESS_STATE_PERSISTENT |
| 224 | UID_STATE_PERSISTENT, // ActivityManager.PROCESS_STATE_PERSISTENT_UI |
| 225 | UID_STATE_TOP, // ActivityManager.PROCESS_STATE_TOP |
Amith Yamasani | f235d0b | 2019-03-20 22:49:43 -0700 | [diff] [blame] | 226 | UID_STATE_FOREGROUND, // ActivityManager.PROCESS_STATE_BOUND_TOP |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 227 | UID_STATE_FOREGROUND_SERVICE, // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE |
| 228 | UID_STATE_FOREGROUND, // ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 229 | UID_STATE_BACKGROUND, // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 230 | UID_STATE_BACKGROUND, // ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND |
| 231 | UID_STATE_BACKGROUND, // ActivityManager.PROCESS_STATE_TRANSIENT_BACKGROUND |
| 232 | UID_STATE_BACKGROUND, // ActivityManager.PROCESS_STATE_BACKUP |
| 233 | UID_STATE_BACKGROUND, // ActivityManager.PROCESS_STATE_SERVICE |
| 234 | UID_STATE_BACKGROUND, // ActivityManager.PROCESS_STATE_RECEIVER |
| 235 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_TOP_SLEEPING |
| 236 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_HEAVY_WEIGHT |
| 237 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_HOME |
| 238 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_LAST_ACTIVITY |
| 239 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY |
| 240 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT |
| 241 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_CACHED_RECENT |
| 242 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_CACHED_EMPTY |
| 243 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_NONEXISTENT |
| 244 | }; |
| 245 | |
Suprabh Shukla | 7e01792 | 2019-08-05 17:13:23 -0700 | [diff] [blame] | 246 | private static final int[] OPS_RESTRICTED_ON_SUSPEND = { |
| 247 | OP_PLAY_AUDIO, |
| 248 | OP_RECORD_AUDIO, |
| 249 | OP_CAMERA, |
| 250 | }; |
| 251 | |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 252 | private static final int MAX_UNFORWARED_OPS = 10; |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 253 | private static final int MAX_UNUSED_POOLED_OBJECTS = 3; |
Stanislav Zholnin | 4c32385 | 2020-03-04 18:03:24 +0000 | [diff] [blame] | 254 | private static final int RARELY_USED_PACKAGES_INITIALIZATION_DELAY_MILLIS = 300000; |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 255 | |
Hui Yu | 0620a33 | 2020-01-27 14:59:07 -0800 | [diff] [blame] | 256 | //TODO: remove this when development is done. |
Hui Yu | e9b40ba | 2020-03-05 17:16:27 -0800 | [diff] [blame] | 257 | private static final int DEBUG_FGS_ALLOW_WHILE_IN_USE = 0; |
| 258 | private static final int DEBUG_FGS_ENFORCE_TYPE = 1; |
| 259 | |
Hui Yu | 0620a33 | 2020-01-27 14:59:07 -0800 | [diff] [blame] | 260 | |
Philip P. Moltmann | fe2fc3a | 2020-02-05 16:59:39 -0800 | [diff] [blame] | 261 | final Context mContext; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 262 | final AtomicFile mFile; |
David Cheung | 2ead966 | 2020-02-19 16:11:06 -0800 | [diff] [blame] | 263 | private final @Nullable File mNoteOpCallerStacktracesFile; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 264 | final Handler mHandler; |
| 265 | |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 266 | /** Pool for {@link OpEventProxyInfoPool} to avoid to constantly reallocate new objects */ |
| 267 | @GuardedBy("this") |
| 268 | private final OpEventProxyInfoPool mOpEventProxyInfoPool = new OpEventProxyInfoPool(); |
| 269 | |
| 270 | /** Pool for {@link InProgressStartOpEventPool} to avoid to constantly reallocate new objects */ |
| 271 | @GuardedBy("this") |
| 272 | private final InProgressStartOpEventPool mInProgressStartOpEventPool = |
| 273 | new InProgressStartOpEventPool(); |
| 274 | |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 275 | private final AppOpsManagerInternalImpl mAppOpsManagerInternal |
| 276 | = new AppOpsManagerInternalImpl(); |
| 277 | |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 278 | /** |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 279 | * Registered callbacks, called from {@link #collectAsyncNotedOp}. |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 280 | * |
| 281 | * <p>(package name, uid) -> callbacks |
| 282 | * |
| 283 | * @see #getAsyncNotedOpsKey(String, int) |
| 284 | */ |
| 285 | @GuardedBy("this") |
| 286 | private final ArrayMap<Pair<String, Integer>, RemoteCallbackList<IAppOpsAsyncNotedCallback>> |
| 287 | mAsyncOpWatchers = new ArrayMap<>(); |
| 288 | |
| 289 | /** |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 290 | * Async note-ops collected from {@link #collectAsyncNotedOp} that have not been delivered to a |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 291 | * callback yet. |
| 292 | * |
| 293 | * <p>(package name, uid) -> list<ops> |
| 294 | * |
| 295 | * @see #getAsyncNotedOpsKey(String, int) |
| 296 | */ |
| 297 | @GuardedBy("this") |
| 298 | private final ArrayMap<Pair<String, Integer>, ArrayList<AsyncNotedAppOp>> |
| 299 | mUnforwardedAsyncNotedOps = new ArrayMap<>(); |
| 300 | |
David Cheung | 2ead966 | 2020-02-19 16:11:06 -0800 | [diff] [blame] | 301 | boolean mWriteNoteOpsScheduled; |
| 302 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 303 | boolean mWriteScheduled; |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 304 | boolean mFastWriteScheduled; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 305 | final Runnable mWriteRunner = new Runnable() { |
| 306 | public void run() { |
| 307 | synchronized (AppOpsService.this) { |
| 308 | mWriteScheduled = false; |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 309 | mFastWriteScheduled = false; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 310 | AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() { |
| 311 | @Override protected Void doInBackground(Void... params) { |
| 312 | writeState(); |
| 313 | return null; |
| 314 | } |
| 315 | }; |
| 316 | task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null); |
| 317 | } |
| 318 | } |
| 319 | }; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 320 | |
Eugene Susla | 463d592 | 2019-07-17 18:14:15 -0700 | [diff] [blame] | 321 | @GuardedBy("this") |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 322 | @VisibleForTesting |
| 323 | final SparseArray<UidState> mUidStates = new SparseArray<>(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 324 | |
Svet Ganov | 4764107 | 2019-06-06 12:57:12 -0700 | [diff] [blame] | 325 | final HistoricalRegistry mHistoricalRegistry = new HistoricalRegistry(this); |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 326 | |
Dianne Hackborn | 9fb9350 | 2018-06-18 12:29:44 -0700 | [diff] [blame] | 327 | long mLastRealtime; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 328 | |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 329 | /* |
| 330 | * These are app op restrictions imposed per user from various parties. |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 331 | */ |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 332 | private final ArrayMap<IBinder, ClientRestrictionState> mOpUserRestrictions = new ArrayMap<>(); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 333 | |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 334 | SparseIntArray mProfileOwners; |
| 335 | |
Todd Kennedy | 556efba | 2018-11-15 07:43:55 -0800 | [diff] [blame] | 336 | @GuardedBy("this") |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 337 | private CheckOpsDelegate mCheckOpsDelegate; |
| 338 | |
Philip P. Moltmann | b2bc651 | 2020-01-24 15:34:34 -0800 | [diff] [blame] | 339 | /** |
| 340 | * Reverse lookup for {@link AppOpsManager#opToSwitch(int)}. Initialized once and never |
| 341 | * changed |
| 342 | */ |
| 343 | private final SparseArray<int[]> mSwitchedOps = new SparseArray<>(); |
Hai Zhang | e53e4c5 | 2019-10-08 18:57:01 -0700 | [diff] [blame] | 344 | |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 345 | private ActivityManagerInternal mActivityManagerInternal; |
| 346 | |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 347 | /** Package sampled for message collection in the current session */ |
| 348 | @GuardedBy("this") |
| 349 | private String mSampledPackage = null; |
| 350 | |
| 351 | /** Appop sampled for message collection in the current session */ |
| 352 | @GuardedBy("this") |
| 353 | private int mSampledAppOpCode = OP_NONE; |
| 354 | |
| 355 | /** Maximum distance for appop to be considered for message collection in the current session */ |
| 356 | @GuardedBy("this") |
| 357 | private int mAcceptableLeftDistance = 0; |
| 358 | |
| 359 | /** Number of messages collected for sampled package and appop in the current session */ |
| 360 | @GuardedBy("this") |
| 361 | private float mMessagesCollectedCount; |
| 362 | |
| 363 | /** List of rarely used packages priorities for message collection */ |
| 364 | @GuardedBy("this") |
| 365 | private ArraySet<String> mRarelyUsedPackages = new ArraySet<>(); |
| 366 | |
| 367 | /** Sampling strategy used for current session */ |
| 368 | @GuardedBy("this") |
| 369 | @AppOpsManager.SamplingStrategy |
| 370 | private int mSamplingStrategy; |
| 371 | |
| 372 | /** Last runtime permission access message collected and ready for reporting */ |
| 373 | @GuardedBy("this") |
| 374 | private RuntimeAppOpAccessMessage mCollectedRuntimePermissionMessage; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 375 | /** |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 376 | * An unsynchronized pool of {@link OpEventProxyInfo} objects. |
| 377 | */ |
| 378 | private class OpEventProxyInfoPool extends SimplePool<OpEventProxyInfo> { |
| 379 | OpEventProxyInfoPool() { |
| 380 | super(MAX_UNUSED_POOLED_OBJECTS); |
| 381 | } |
| 382 | |
| 383 | OpEventProxyInfo acquire(@IntRange(from = 0) int uid, @Nullable String packageName, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 384 | @Nullable String attributionTag) { |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 385 | OpEventProxyInfo recycled = acquire(); |
| 386 | if (recycled != null) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 387 | recycled.reinit(uid, packageName, attributionTag); |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 388 | return recycled; |
| 389 | } |
| 390 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 391 | return new OpEventProxyInfo(uid, packageName, attributionTag); |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * An unsynchronized pool of {@link InProgressStartOpEvent} objects. |
| 397 | */ |
| 398 | private class InProgressStartOpEventPool extends SimplePool<InProgressStartOpEvent> { |
| 399 | InProgressStartOpEventPool() { |
| 400 | super(MAX_UNUSED_POOLED_OBJECTS); |
| 401 | } |
| 402 | |
| 403 | InProgressStartOpEvent acquire(long startTime, long elapsedTime, @NonNull IBinder clientId, |
| 404 | @NonNull Runnable onDeath, int uidState) throws RemoteException { |
| 405 | InProgressStartOpEvent recycled = acquire(); |
| 406 | if (recycled != null) { |
| 407 | recycled.reinit(startTime, elapsedTime, clientId, onDeath, uidState); |
| 408 | return recycled; |
| 409 | } |
| 410 | |
| 411 | return new InProgressStartOpEvent(startTime, elapsedTime, clientId, onDeath, uidState); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /** |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 416 | * All times are in milliseconds. These constants are kept synchronized with the system |
| 417 | * global Settings. Any access to this class or its fields should be done while |
| 418 | * holding the AppOpsService lock. |
| 419 | */ |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 420 | @VisibleForTesting |
| 421 | final class Constants extends ContentObserver { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 422 | |
| 423 | /** |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 424 | * How long we want for a drop in uid state from top to settle before applying it. |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 425 | * @see Settings.Global#APP_OPS_CONSTANTS |
Philip P. Moltmann | e565c7b | 2020-01-28 13:15:52 -0800 | [diff] [blame] | 426 | * @see AppOpsManager#KEY_TOP_STATE_SETTLE_TIME |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 427 | */ |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 428 | public long TOP_STATE_SETTLE_TIME; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 429 | |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 430 | /** |
| 431 | * How long we want for a drop in uid state from foreground to settle before applying it. |
| 432 | * @see Settings.Global#APP_OPS_CONSTANTS |
Philip P. Moltmann | e565c7b | 2020-01-28 13:15:52 -0800 | [diff] [blame] | 433 | * @see AppOpsManager#KEY_FG_SERVICE_STATE_SETTLE_TIME |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 434 | */ |
| 435 | public long FG_SERVICE_STATE_SETTLE_TIME; |
| 436 | |
| 437 | /** |
| 438 | * How long we want for a drop in uid state from background to settle before applying it. |
| 439 | * @see Settings.Global#APP_OPS_CONSTANTS |
Philip P. Moltmann | e565c7b | 2020-01-28 13:15:52 -0800 | [diff] [blame] | 440 | * @see AppOpsManager#KEY_BG_STATE_SETTLE_TIME |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 441 | */ |
| 442 | public long BG_STATE_SETTLE_TIME; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 443 | |
| 444 | private final KeyValueListParser mParser = new KeyValueListParser(','); |
| 445 | private ContentResolver mResolver; |
| 446 | |
| 447 | public Constants(Handler handler) { |
| 448 | super(handler); |
| 449 | updateConstants(); |
| 450 | } |
| 451 | |
| 452 | public void startMonitoring(ContentResolver resolver) { |
| 453 | mResolver = resolver; |
| 454 | mResolver.registerContentObserver( |
Dianne Hackborn | 45c79b0 | 2018-05-11 09:46:13 -0700 | [diff] [blame] | 455 | Settings.Global.getUriFor(Settings.Global.APP_OPS_CONSTANTS), |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 456 | false, this); |
| 457 | updateConstants(); |
| 458 | } |
| 459 | |
| 460 | @Override |
| 461 | public void onChange(boolean selfChange, Uri uri) { |
| 462 | updateConstants(); |
| 463 | } |
| 464 | |
| 465 | private void updateConstants() { |
Dianne Hackborn | 45c79b0 | 2018-05-11 09:46:13 -0700 | [diff] [blame] | 466 | String value = mResolver != null ? Settings.Global.getString(mResolver, |
| 467 | Settings.Global.APP_OPS_CONSTANTS) : ""; |
| 468 | |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 469 | synchronized (AppOpsService.this) { |
| 470 | try { |
Dianne Hackborn | 45c79b0 | 2018-05-11 09:46:13 -0700 | [diff] [blame] | 471 | mParser.setString(value); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 472 | } catch (IllegalArgumentException e) { |
| 473 | // Failed to parse the settings string, log this and move on |
| 474 | // with defaults. |
| 475 | Slog.e(TAG, "Bad app ops settings", e); |
| 476 | } |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 477 | TOP_STATE_SETTLE_TIME = mParser.getDurationMillis( |
Philip P. Moltmann | 88583dd | 2020-02-12 16:55:32 -0800 | [diff] [blame] | 478 | KEY_TOP_STATE_SETTLE_TIME, 5 * 1000L); |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 479 | FG_SERVICE_STATE_SETTLE_TIME = mParser.getDurationMillis( |
Philip P. Moltmann | 88583dd | 2020-02-12 16:55:32 -0800 | [diff] [blame] | 480 | KEY_FG_SERVICE_STATE_SETTLE_TIME, 5 * 1000L); |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 481 | BG_STATE_SETTLE_TIME = mParser.getDurationMillis( |
| 482 | KEY_BG_STATE_SETTLE_TIME, 1 * 1000L); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
| 486 | void dump(PrintWriter pw) { |
| 487 | pw.println(" Settings:"); |
| 488 | |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 489 | pw.print(" "); pw.print(KEY_TOP_STATE_SETTLE_TIME); pw.print("="); |
| 490 | TimeUtils.formatDuration(TOP_STATE_SETTLE_TIME, pw); |
Dianne Hackborn | b94d82f | 2018-05-16 17:03:01 -0700 | [diff] [blame] | 491 | pw.println(); |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 492 | pw.print(" "); pw.print(KEY_FG_SERVICE_STATE_SETTLE_TIME); pw.print("="); |
| 493 | TimeUtils.formatDuration(FG_SERVICE_STATE_SETTLE_TIME, pw); |
Dianne Hackborn | b94d82f | 2018-05-16 17:03:01 -0700 | [diff] [blame] | 494 | pw.println(); |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 495 | pw.print(" "); pw.print(KEY_BG_STATE_SETTLE_TIME); pw.print("="); |
| 496 | TimeUtils.formatDuration(BG_STATE_SETTLE_TIME, pw); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 497 | pw.println(); |
| 498 | } |
| 499 | } |
| 500 | |
Amith Yamasani | 23d4cd7 | 2019-04-10 17:57:00 -0700 | [diff] [blame] | 501 | @VisibleForTesting |
| 502 | final Constants mConstants; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 503 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 504 | @VisibleForTesting |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 505 | final class UidState { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 506 | public final int uid; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 507 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 508 | public int state = UID_STATE_CACHED; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 509 | public int pendingState = UID_STATE_CACHED; |
| 510 | public long pendingStateCommitTime; |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 511 | public int capability; |
| 512 | public int pendingCapability; |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 513 | public boolean appWidgetVisible; |
| 514 | public boolean pendingAppWidgetVisible; |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 515 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 516 | public ArrayMap<String, Ops> pkgOps; |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 517 | public SparseIntArray opModes; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 518 | |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 519 | // true indicates there is an interested observer, false there isn't but it has such an op |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 520 | public SparseBooleanArray foregroundOps; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 521 | public boolean hasForegroundWatchers; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 522 | |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 523 | public long lastTimeShowDebugToast; |
| 524 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 525 | public UidState(int uid) { |
| 526 | this.uid = uid; |
| 527 | } |
| 528 | |
| 529 | public void clear() { |
| 530 | pkgOps = null; |
| 531 | opModes = null; |
| 532 | } |
| 533 | |
| 534 | public boolean isDefault() { |
| 535 | return (pkgOps == null || pkgOps.isEmpty()) |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 536 | && (opModes == null || opModes.size() <= 0) |
| 537 | && (state == UID_STATE_CACHED |
| 538 | && (pendingState == UID_STATE_CACHED)); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 539 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 540 | |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 541 | int evalMode(int op, int mode) { |
Hui Yu | bb866ad | 2020-03-04 12:33:40 -0800 | [diff] [blame] | 542 | if (mode == MODE_FOREGROUND) { |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 543 | if (appWidgetVisible) { |
| 544 | return MODE_ALLOWED; |
| 545 | } else if (state <= UID_STATE_TOP) { |
Hui Yu | bb866ad | 2020-03-04 12:33:40 -0800 | [diff] [blame] | 546 | // process is in TOP. |
| 547 | return MODE_ALLOWED; |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 548 | } else if (state <= AppOpsManager.resolveFirstUnrestrictedUidState(op)) { |
| 549 | // process is in foreground, check its capability. |
| 550 | switch (op) { |
| 551 | case AppOpsManager.OP_FINE_LOCATION: |
| 552 | case AppOpsManager.OP_COARSE_LOCATION: |
| 553 | case AppOpsManager.OP_MONITOR_LOCATION: |
| 554 | case AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION: |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 555 | if ((capability & PROCESS_CAPABILITY_FOREGROUND_LOCATION) != 0) { |
Hui Yu | bb866ad | 2020-03-04 12:33:40 -0800 | [diff] [blame] | 556 | return MODE_ALLOWED; |
Hui Yu | 0620a33 | 2020-01-27 14:59:07 -0800 | [diff] [blame] | 557 | } else if ((capability |
Hui Yu | 77fd819 | 2020-03-27 00:22:57 -0700 | [diff] [blame] | 558 | & DEBUG_PROCESS_CAPABILITY_FOREGROUND_LOCATION) != 0) { |
Hui Yu | 0620a33 | 2020-01-27 14:59:07 -0800 | [diff] [blame] | 559 | // The FGS has the location capability, but due to FGS BG start |
| 560 | // restriction it lost the capability, use temp location capability |
| 561 | // to mark this case. |
Hui Yu | e9b40ba | 2020-03-05 17:16:27 -0800 | [diff] [blame] | 562 | maybeShowWhileInUseDebugToast(op, DEBUG_FGS_ALLOW_WHILE_IN_USE); |
Hui Yu | bb866ad | 2020-03-04 12:33:40 -0800 | [diff] [blame] | 563 | return MODE_IGNORED; |
Hui Yu | 0620a33 | 2020-01-27 14:59:07 -0800 | [diff] [blame] | 564 | } else { |
Hui Yu | bb866ad | 2020-03-04 12:33:40 -0800 | [diff] [blame] | 565 | return MODE_IGNORED; |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 566 | } |
Hui Yu | 85679b4 | 2020-01-15 15:52:08 -0800 | [diff] [blame] | 567 | case OP_CAMERA: |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 568 | if ((capability & PROCESS_CAPABILITY_FOREGROUND_CAMERA) != 0) { |
Hui Yu | bb866ad | 2020-03-04 12:33:40 -0800 | [diff] [blame] | 569 | return MODE_ALLOWED; |
Hui Yu | 77fd819 | 2020-03-27 00:22:57 -0700 | [diff] [blame] | 570 | } else if ((capability & DEBUG_PROCESS_CAPABILITY_FOREGROUND_CAMERA_Q) |
Hui Yu | e9b40ba | 2020-03-05 17:16:27 -0800 | [diff] [blame] | 571 | != 0) { |
Hui Yu | e9b40ba | 2020-03-05 17:16:27 -0800 | [diff] [blame] | 572 | maybeShowWhileInUseDebugToast(op, DEBUG_FGS_ENFORCE_TYPE); |
| 573 | return MODE_ALLOWED; |
Hui Yu | 77fd819 | 2020-03-27 00:22:57 -0700 | [diff] [blame] | 574 | } else if ((capability & DEBUG_PROCESS_CAPABILITY_FOREGROUND_CAMERA) |
| 575 | != 0) { |
| 576 | maybeShowWhileInUseDebugToast(op, DEBUG_FGS_ENFORCE_TYPE); |
| 577 | return MODE_IGNORED; |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 578 | } else { |
Hui Yu | e9b40ba | 2020-03-05 17:16:27 -0800 | [diff] [blame] | 579 | maybeShowWhileInUseDebugToast(op, DEBUG_FGS_ALLOW_WHILE_IN_USE); |
Hui Yu | bb866ad | 2020-03-04 12:33:40 -0800 | [diff] [blame] | 580 | return MODE_IGNORED; |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 581 | } |
Hui Yu | 85679b4 | 2020-01-15 15:52:08 -0800 | [diff] [blame] | 582 | case OP_RECORD_AUDIO: |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 583 | if ((capability & PROCESS_CAPABILITY_FOREGROUND_MICROPHONE) != 0) { |
Hui Yu | bb866ad | 2020-03-04 12:33:40 -0800 | [diff] [blame] | 584 | return MODE_ALLOWED; |
Hui Yu | 77fd819 | 2020-03-27 00:22:57 -0700 | [diff] [blame] | 585 | } else if ((capability |
| 586 | & DEBUG_PROCESS_CAPABILITY_FOREGROUND_MICROPHONE_Q) != 0) { |
Hui Yu | e9b40ba | 2020-03-05 17:16:27 -0800 | [diff] [blame] | 587 | maybeShowWhileInUseDebugToast(op, DEBUG_FGS_ENFORCE_TYPE); |
| 588 | return MODE_ALLOWED; |
Hui Yu | 77fd819 | 2020-03-27 00:22:57 -0700 | [diff] [blame] | 589 | } else if ((capability |
| 590 | & DEBUG_PROCESS_CAPABILITY_FOREGROUND_MICROPHONE) != 0) { |
| 591 | maybeShowWhileInUseDebugToast(op, DEBUG_FGS_ENFORCE_TYPE); |
| 592 | return MODE_IGNORED; |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 593 | } else { |
Hui Yu | e9b40ba | 2020-03-05 17:16:27 -0800 | [diff] [blame] | 594 | maybeShowWhileInUseDebugToast(op, DEBUG_FGS_ALLOW_WHILE_IN_USE); |
Hui Yu | bb866ad | 2020-03-04 12:33:40 -0800 | [diff] [blame] | 595 | return MODE_IGNORED; |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 596 | } |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 597 | default: |
Hui Yu | bb866ad | 2020-03-04 12:33:40 -0800 | [diff] [blame] | 598 | return MODE_ALLOWED; |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 599 | } |
| 600 | } else { |
| 601 | // process is not in foreground. |
Hui Yu | bb866ad | 2020-03-04 12:33:40 -0800 | [diff] [blame] | 602 | return MODE_IGNORED; |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 603 | } |
Hui Yu | bb866ad | 2020-03-04 12:33:40 -0800 | [diff] [blame] | 604 | } else if (mode == MODE_ALLOWED) { |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 605 | switch (op) { |
| 606 | case OP_CAMERA: |
| 607 | if ((capability & PROCESS_CAPABILITY_FOREGROUND_CAMERA) != 0) { |
Hui Yu | bb866ad | 2020-03-04 12:33:40 -0800 | [diff] [blame] | 608 | return MODE_ALLOWED; |
Hui Yu | 77fd819 | 2020-03-27 00:22:57 -0700 | [diff] [blame] | 609 | } else if ((capability |
| 610 | & DEBUG_PROCESS_CAPABILITY_FOREGROUND_CAMERA_Q) != 0) { |
Hui Yu | e9b40ba | 2020-03-05 17:16:27 -0800 | [diff] [blame] | 611 | maybeShowWhileInUseDebugToast(op, DEBUG_FGS_ENFORCE_TYPE); |
| 612 | return MODE_ALLOWED; |
Hui Yu | 77fd819 | 2020-03-27 00:22:57 -0700 | [diff] [blame] | 613 | } else if ((capability & DEBUG_PROCESS_CAPABILITY_FOREGROUND_CAMERA) != 0) { |
| 614 | maybeShowWhileInUseDebugToast(op, DEBUG_FGS_ENFORCE_TYPE); |
| 615 | return MODE_IGNORED; |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 616 | } else { |
Hui Yu | e9b40ba | 2020-03-05 17:16:27 -0800 | [diff] [blame] | 617 | maybeShowWhileInUseDebugToast(op, DEBUG_FGS_ALLOW_WHILE_IN_USE); |
Hui Yu | bb866ad | 2020-03-04 12:33:40 -0800 | [diff] [blame] | 618 | return MODE_IGNORED; |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 619 | } |
| 620 | case OP_RECORD_AUDIO: |
| 621 | if ((capability & PROCESS_CAPABILITY_FOREGROUND_MICROPHONE) != 0) { |
Hui Yu | bb866ad | 2020-03-04 12:33:40 -0800 | [diff] [blame] | 622 | return MODE_ALLOWED; |
Hui Yu | 77fd819 | 2020-03-27 00:22:57 -0700 | [diff] [blame] | 623 | } else if ((capability & DEBUG_PROCESS_CAPABILITY_FOREGROUND_MICROPHONE_Q) |
Hui Yu | e9b40ba | 2020-03-05 17:16:27 -0800 | [diff] [blame] | 624 | != 0) { |
Hui Yu | e9b40ba | 2020-03-05 17:16:27 -0800 | [diff] [blame] | 625 | maybeShowWhileInUseDebugToast(op, DEBUG_FGS_ENFORCE_TYPE); |
| 626 | return MODE_ALLOWED; |
Hui Yu | 77fd819 | 2020-03-27 00:22:57 -0700 | [diff] [blame] | 627 | } else if ((capability & DEBUG_PROCESS_CAPABILITY_FOREGROUND_MICROPHONE) |
| 628 | != 0) { |
| 629 | maybeShowWhileInUseDebugToast(op, DEBUG_FGS_ENFORCE_TYPE); |
| 630 | return MODE_IGNORED; |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 631 | } else { |
Hui Yu | e9b40ba | 2020-03-05 17:16:27 -0800 | [diff] [blame] | 632 | maybeShowWhileInUseDebugToast(op, DEBUG_FGS_ALLOW_WHILE_IN_USE); |
Hui Yu | bb866ad | 2020-03-04 12:33:40 -0800 | [diff] [blame] | 633 | return MODE_IGNORED; |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 634 | } |
| 635 | default: |
| 636 | return MODE_ALLOWED; |
| 637 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 638 | } |
| 639 | return mode; |
| 640 | } |
| 641 | |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 642 | private void evalForegroundWatchers(int op, SparseArray<ArraySet<ModeCallback>> watchers, |
| 643 | SparseBooleanArray which) { |
| 644 | boolean curValue = which.get(op, false); |
| 645 | ArraySet<ModeCallback> callbacks = watchers.get(op); |
| 646 | if (callbacks != null) { |
| 647 | for (int cbi = callbacks.size() - 1; !curValue && cbi >= 0; cbi--) { |
| 648 | if ((callbacks.valueAt(cbi).mFlags |
| 649 | & AppOpsManager.WATCH_FOREGROUND_CHANGES) != 0) { |
| 650 | hasForegroundWatchers = true; |
| 651 | curValue = true; |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | which.put(op, curValue); |
| 656 | } |
| 657 | |
| 658 | public void evalForegroundOps(SparseArray<ArraySet<ModeCallback>> watchers) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 659 | SparseBooleanArray which = null; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 660 | hasForegroundWatchers = false; |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 661 | if (opModes != null) { |
| 662 | for (int i = opModes.size() - 1; i >= 0; i--) { |
| 663 | if (opModes.valueAt(i) == AppOpsManager.MODE_FOREGROUND) { |
| 664 | if (which == null) { |
| 665 | which = new SparseBooleanArray(); |
| 666 | } |
| 667 | evalForegroundWatchers(opModes.keyAt(i), watchers, which); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 671 | if (pkgOps != null) { |
| 672 | for (int i = pkgOps.size() - 1; i >= 0; i--) { |
| 673 | Ops ops = pkgOps.valueAt(i); |
| 674 | for (int j = ops.size() - 1; j >= 0; j--) { |
| 675 | if (ops.valueAt(j).mode == AppOpsManager.MODE_FOREGROUND) { |
| 676 | if (which == null) { |
| 677 | which = new SparseBooleanArray(); |
| 678 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 679 | evalForegroundWatchers(ops.keyAt(j), watchers, which); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | foregroundOps = which; |
| 685 | } |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 686 | |
| 687 | // TODO: remove this toast after feature development is done |
Hui Yu | d3de0b9 | 2020-03-18 16:33:37 -0700 | [diff] [blame] | 688 | // For DEBUG_FGS_ALLOW_WHILE_IN_USE, if the procstate is foreground service and while-in-use |
| 689 | // permission is denied, show a toast message and generate a WTF log so we know |
| 690 | // how many apps are impacted by the new background started foreground service while-in-use |
| 691 | // permission restriction. |
| 692 | // For DEBUG_FGS_ENFORCE_TYPE, The process has a foreground service that does not have |
| 693 | // camera/microphone foregroundServiceType in manifest file, and the process is asking |
| 694 | // AppOps for camera/microphone ops, show a toast message and generate a WTF log. |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 695 | void maybeShowWhileInUseDebugToast(int op, int mode) { |
Hui Yu | d3de0b9 | 2020-03-18 16:33:37 -0700 | [diff] [blame] | 696 | if (mode == DEBUG_FGS_ALLOW_WHILE_IN_USE && state != UID_STATE_FOREGROUND_SERVICE) { |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 697 | return; |
| 698 | } |
| 699 | final long now = System.currentTimeMillis(); |
Hui Yu | d3de0b9 | 2020-03-18 16:33:37 -0700 | [diff] [blame] | 700 | if (lastTimeShowDebugToast == 0 || now - lastTimeShowDebugToast > 600000) { |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 701 | lastTimeShowDebugToast = now; |
| 702 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 703 | ActivityManagerInternal::showWhileInUseDebugToast, |
| 704 | mActivityManagerInternal, uid, op, mode)); |
| 705 | } |
| 706 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 709 | final static class Ops extends SparseArray<Op> { |
| 710 | final String packageName; |
| 711 | final UidState uidState; |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 712 | |
| 713 | /** |
| 714 | * The restriction properties of the package. If {@code null} it could not have been read |
| 715 | * yet and has to be refreshed. |
| 716 | */ |
| 717 | @Nullable RestrictionBypass bypass; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 718 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 719 | /** Lazily populated cache of attributionTags of this package */ |
| 720 | final @NonNull ArraySet<String> knownAttributionTags = new ArraySet<>(); |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 721 | |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 722 | Ops(String _packageName, UidState _uidState) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 723 | packageName = _packageName; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 724 | uidState = _uidState; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 728 | /** A in progress startOp->finishOp event */ |
| 729 | private static final class InProgressStartOpEvent implements IBinder.DeathRecipient { |
Philip P. Moltmann | e6ece90 | 2020-01-02 13:31:10 -0800 | [diff] [blame] | 730 | /** Wall clock time of startOp event (not monotonic) */ |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 731 | private long mStartTime; |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 732 | |
Philip P. Moltmann | e6ece90 | 2020-01-02 13:31:10 -0800 | [diff] [blame] | 733 | /** Elapsed time since boot of startOp event */ |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 734 | private long mStartElapsedTime; |
Philip P. Moltmann | e6ece90 | 2020-01-02 13:31:10 -0800 | [diff] [blame] | 735 | |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 736 | /** Id of the client that started the event */ |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 737 | private @NonNull IBinder mClientId; |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 738 | |
| 739 | /** To call when client dies */ |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 740 | private @NonNull Runnable mOnDeath; |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 741 | |
| 742 | /** uidstate used when calling startOp */ |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 743 | private @AppOpsManager.UidState int mUidState; |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 744 | |
| 745 | /** How many times the op was started but not finished yet */ |
| 746 | int numUnfinishedStarts; |
| 747 | |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 748 | /** |
| 749 | * Create a new {@link InProgressStartOpEvent}. |
| 750 | * |
| 751 | * @param startTime The time {@link #startOperation} was called |
| 752 | * @param startElapsedTime The elapsed time when {@link #startOperation} was called |
| 753 | * @param clientId The client id of the caller of {@link #startOperation} |
| 754 | * @param onDeath The code to execute on client death |
| 755 | * @param uidState The uidstate of the app {@link #startOperation} was called for |
| 756 | * |
| 757 | * @throws RemoteException If the client is dying |
| 758 | */ |
Philip P. Moltmann | e6ece90 | 2020-01-02 13:31:10 -0800 | [diff] [blame] | 759 | private InProgressStartOpEvent(long startTime, long startElapsedTime, |
| 760 | @NonNull IBinder clientId, @NonNull Runnable onDeath, int uidState) |
| 761 | throws RemoteException { |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 762 | mStartTime = startTime; |
| 763 | mStartElapsedTime = startElapsedTime; |
| 764 | mClientId = clientId; |
| 765 | mOnDeath = onDeath; |
| 766 | mUidState = uidState; |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 767 | |
| 768 | clientId.linkToDeath(this, 0); |
| 769 | } |
| 770 | |
| 771 | /** Clean up event */ |
| 772 | public void finish() { |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 773 | mClientId.unlinkToDeath(this, 0); |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | @Override |
| 777 | public void binderDied() { |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 778 | mOnDeath.run(); |
| 779 | } |
| 780 | |
| 781 | /** |
| 782 | * Reinit existing object with new state. |
| 783 | * |
| 784 | * @param startTime The time {@link #startOperation} was called |
| 785 | * @param startElapsedTime The elapsed time when {@link #startOperation} was called |
| 786 | * @param clientId The client id of the caller of {@link #startOperation} |
| 787 | * @param onDeath The code to execute on client death |
| 788 | * @param uidState The uidstate of the app {@link #startOperation} was called for |
| 789 | * |
| 790 | * @throws RemoteException If the client is dying |
| 791 | */ |
| 792 | public void reinit(long startTime, long startElapsedTime, @NonNull IBinder clientId, |
| 793 | @NonNull Runnable onDeath, int uidState) throws RemoteException { |
| 794 | mStartTime = startTime; |
| 795 | mStartElapsedTime = startElapsedTime; |
| 796 | mClientId = clientId; |
| 797 | mOnDeath = onDeath; |
| 798 | mUidState = uidState; |
| 799 | |
| 800 | clientId.linkToDeath(this, 0); |
| 801 | } |
| 802 | |
| 803 | /** @return Wall clock time of startOp event */ |
| 804 | public long getStartTime() { |
| 805 | return mStartTime; |
| 806 | } |
| 807 | |
| 808 | /** @return Elapsed time since boot of startOp event */ |
| 809 | public long getStartElapsedTime() { |
| 810 | return mStartElapsedTime; |
| 811 | } |
| 812 | |
| 813 | /** @return Id of the client that started the event */ |
| 814 | public @NonNull IBinder getClientId() { |
| 815 | return mClientId; |
| 816 | } |
| 817 | |
| 818 | /** @return uidstate used when calling startOp */ |
| 819 | public int getUidState() { |
| 820 | return mUidState; |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 821 | } |
| 822 | } |
| 823 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 824 | private final class AttributedOp { |
| 825 | public final @Nullable String tag; |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 826 | public final @NonNull Op parent; |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 827 | |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 828 | /** |
| 829 | * Last successful accesses (noteOp + finished startOp) for each uidState/opFlag combination |
| 830 | * |
| 831 | * <p>Key is {@link AppOpsManager#makeKey} |
| 832 | */ |
| 833 | @GuardedBy("AppOpsService.this") |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 834 | private @Nullable LongSparseArray<NoteOpEvent> mAccessEvents; |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 835 | |
| 836 | /** |
| 837 | * Last rejected accesses for each uidState/opFlag combination |
| 838 | * |
| 839 | * <p>Key is {@link AppOpsManager#makeKey} |
| 840 | */ |
| 841 | @GuardedBy("AppOpsService.this") |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 842 | private @Nullable LongSparseArray<NoteOpEvent> mRejectEvents; |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 843 | |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 844 | /** |
| 845 | * Currently in progress startOp events |
| 846 | * |
| 847 | * <p>Key is clientId |
| 848 | */ |
| 849 | @GuardedBy("AppOpsService.this") |
| 850 | private @Nullable ArrayMap<IBinder, InProgressStartOpEvent> mInProgressEvents; |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 851 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 852 | AttributedOp(@Nullable String tag, @NonNull Op parent) { |
| 853 | this.tag = tag; |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 854 | this.parent = parent; |
| 855 | } |
| 856 | |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 857 | /** |
| 858 | * Update state when noteOp was rejected or startOp->finishOp event finished |
| 859 | * |
| 860 | * @param proxyUid The uid of the proxy |
| 861 | * @param proxyPackageName The package name of the proxy |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 862 | * @param proxyAttributionTag the attributionTag in the proxies package |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 863 | * @param uidState UID state of the app noteOp/startOp was called for |
| 864 | * @param flags OpFlags of the call |
| 865 | */ |
| 866 | public void accessed(int proxyUid, @Nullable String proxyPackageName, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 867 | @Nullable String proxyAttributionTag, @AppOpsManager.UidState int uidState, |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 868 | @OpFlags int flags) { |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 869 | accessed(System.currentTimeMillis(), -1, proxyUid, proxyPackageName, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 870 | proxyAttributionTag, uidState, flags); |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 871 | |
| 872 | mHistoricalRegistry.incrementOpAccessedCount(parent.op, parent.uid, parent.packageName, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 873 | tag, uidState, flags); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 876 | /** |
| 877 | * Add an access that was previously collected. |
| 878 | * |
| 879 | * @param noteTime The time of the event |
| 880 | * @param duration The duration of the event |
| 881 | * @param proxyUid The uid of the proxy |
| 882 | * @param proxyPackageName The package name of the proxy |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 883 | * @param proxyAttributionTag the attributionTag in the proxies package |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 884 | * @param uidState UID state of the app noteOp/startOp was called for |
| 885 | * @param flags OpFlags of the call |
| 886 | */ |
| 887 | public void accessed(long noteTime, long duration, int proxyUid, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 888 | @Nullable String proxyPackageName, @Nullable String proxyAttributionTag, |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 889 | @AppOpsManager.UidState int uidState, @OpFlags int flags) { |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 890 | long key = makeKey(uidState, flags); |
| 891 | |
| 892 | if (mAccessEvents == null) { |
| 893 | mAccessEvents = new LongSparseArray<>(1); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 894 | } |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 895 | |
| 896 | OpEventProxyInfo proxyInfo = null; |
| 897 | if (proxyUid != Process.INVALID_UID) { |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 898 | proxyInfo = mOpEventProxyInfoPool.acquire(proxyUid, proxyPackageName, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 899 | proxyAttributionTag); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 900 | } |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 901 | |
| 902 | NoteOpEvent existingEvent = mAccessEvents.get(key); |
| 903 | if (existingEvent != null) { |
| 904 | existingEvent.reinit(noteTime, duration, proxyInfo, mOpEventProxyInfoPool); |
| 905 | } else { |
| 906 | mAccessEvents.put(key, new NoteOpEvent(noteTime, duration, proxyInfo)); |
| 907 | } |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 910 | /** |
| 911 | * Update state when noteOp/startOp was rejected. |
| 912 | * |
| 913 | * @param uidState UID state of the app noteOp is called for |
| 914 | * @param flags OpFlags of the call |
| 915 | */ |
| 916 | public void rejected(@AppOpsManager.UidState int uidState, @OpFlags int flags) { |
| 917 | rejected(System.currentTimeMillis(), uidState, flags); |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 918 | |
| 919 | mHistoricalRegistry.incrementOpRejected(parent.op, parent.uid, parent.packageName, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 920 | tag, uidState, flags); |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | /** |
| 924 | * Add an rejection that was previously collected |
| 925 | * |
| 926 | * @param noteTime The time of the event |
| 927 | * @param uidState UID state of the app noteOp/startOp was called for |
| 928 | * @param flags OpFlags of the call |
| 929 | */ |
| 930 | public void rejected(long noteTime, @AppOpsManager.UidState int uidState, |
| 931 | @OpFlags int flags) { |
| 932 | long key = makeKey(uidState, flags); |
| 933 | |
| 934 | if (mRejectEvents == null) { |
| 935 | mRejectEvents = new LongSparseArray<>(1); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 938 | // We do not collect proxy information for rejections yet |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 939 | NoteOpEvent existingEvent = mRejectEvents.get(key); |
| 940 | if (existingEvent != null) { |
| 941 | existingEvent.reinit(noteTime, -1, null, mOpEventProxyInfoPool); |
| 942 | } else { |
| 943 | mRejectEvents.put(key, new NoteOpEvent(noteTime, -1, null)); |
| 944 | } |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 945 | } |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 946 | |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 947 | /** |
| 948 | * Update state when start was called |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 949 | * |
| 950 | * @param clientId Id of the startOp caller |
| 951 | * @param uidState UID state of the app startOp is called for |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 952 | */ |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 953 | public void started(@NonNull IBinder clientId, @AppOpsManager.UidState int uidState) |
| 954 | throws RemoteException { |
Philip P. Moltmann | 494b4ab | 2020-01-31 09:33:20 -0800 | [diff] [blame] | 955 | started(clientId, uidState, true); |
| 956 | } |
| 957 | |
| 958 | private void started(@NonNull IBinder clientId, @AppOpsManager.UidState int uidState, |
| 959 | boolean triggerCallbackIfNeeded) throws RemoteException { |
| 960 | if (triggerCallbackIfNeeded && !parent.isRunning()) { |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 961 | scheduleOpActiveChangedIfNeededLocked(parent.op, parent.uid, |
| 962 | parent.packageName, true); |
| 963 | } |
| 964 | |
| 965 | if (mInProgressEvents == null) { |
| 966 | mInProgressEvents = new ArrayMap<>(1); |
| 967 | } |
| 968 | |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 969 | InProgressStartOpEvent event = mInProgressEvents.get(clientId); |
| 970 | if (event == null) { |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 971 | event = mInProgressStartOpEventPool.acquire(System.currentTimeMillis(), |
| 972 | SystemClock.elapsedRealtime(), clientId, |
| 973 | PooledLambda.obtainRunnable(AppOpsService::onClientDeath, this, clientId), |
| 974 | uidState); |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 975 | mInProgressEvents.put(clientId, event); |
| 976 | } else { |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 977 | if (uidState != event.mUidState) { |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 978 | onUidStateChanged(uidState); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | event.numUnfinishedStarts++; |
| 983 | |
| 984 | // startOp events don't support proxy, hence use flags==SELF |
| 985 | mHistoricalRegistry.incrementOpAccessedCount(parent.op, parent.uid, parent.packageName, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 986 | tag, uidState, OP_FLAG_SELF); |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 987 | } |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 988 | |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 989 | /** |
| 990 | * Update state when finishOp was called |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 991 | * |
| 992 | * @param clientId Id of the finishOp caller |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 993 | */ |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 994 | public void finished(@NonNull IBinder clientId) { |
| 995 | finished(clientId, true); |
| 996 | } |
| 997 | |
| 998 | private void finished(@NonNull IBinder clientId, boolean triggerCallbackIfNeeded) { |
| 999 | if (mInProgressEvents == null) { |
| 1000 | Slog.wtf(TAG, "No ops running"); |
| 1001 | return; |
| 1002 | } |
| 1003 | |
| 1004 | int indexOfToken = mInProgressEvents.indexOfKey(clientId); |
| 1005 | if (indexOfToken < 0) { |
| 1006 | Slog.wtf(TAG, "No op running for the client"); |
| 1007 | return; |
| 1008 | } |
| 1009 | |
| 1010 | InProgressStartOpEvent event = mInProgressEvents.valueAt(indexOfToken); |
| 1011 | event.numUnfinishedStarts--; |
| 1012 | if (event.numUnfinishedStarts == 0) { |
| 1013 | event.finish(); |
| 1014 | mInProgressEvents.removeAt(indexOfToken); |
| 1015 | |
| 1016 | if (mAccessEvents == null) { |
| 1017 | mAccessEvents = new LongSparseArray<>(1); |
| 1018 | } |
| 1019 | |
| 1020 | // startOp events don't support proxy, hence use flags==SELF |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 1021 | NoteOpEvent finishedEvent = new NoteOpEvent(event.getStartTime(), |
| 1022 | SystemClock.elapsedRealtime() - event.getStartElapsedTime(), null); |
| 1023 | mAccessEvents.put(makeKey(event.getUidState(), OP_FLAG_SELF), finishedEvent); |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 1024 | |
| 1025 | mHistoricalRegistry.increaseOpAccessDuration(parent.op, parent.uid, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1026 | parent.packageName, tag, event.getUidState(), |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 1027 | AppOpsManager.OP_FLAG_SELF, finishedEvent.getDuration()); |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 1028 | |
| 1029 | mInProgressStartOpEventPool.release(event); |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 1030 | |
| 1031 | if (mInProgressEvents.isEmpty()) { |
| 1032 | mInProgressEvents = null; |
| 1033 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1034 | // TODO moltmann: Also callback for single attribution tag activity changes |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 1035 | if (triggerCallbackIfNeeded && !parent.isRunning()) { |
| 1036 | scheduleOpActiveChangedIfNeededLocked(parent.op, parent.uid, |
| 1037 | parent.packageName, false); |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | /** |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 1044 | * Called in the case the client dies without calling finish first |
| 1045 | * |
| 1046 | * @param clientId The client that died |
| 1047 | */ |
| 1048 | void onClientDeath(@NonNull IBinder clientId) { |
| 1049 | synchronized (AppOpsService.this) { |
| 1050 | if (mInProgressEvents == null) { |
| 1051 | return; |
| 1052 | } |
| 1053 | |
| 1054 | InProgressStartOpEvent deadEvent = mInProgressEvents.get(clientId); |
| 1055 | if (deadEvent != null) { |
| 1056 | deadEvent.numUnfinishedStarts = 1; |
| 1057 | } |
| 1058 | |
| 1059 | finished(clientId); |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | /** |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 1064 | * Notify that the state of the uid changed |
| 1065 | * |
| 1066 | * @param newState The new state |
| 1067 | */ |
| 1068 | public void onUidStateChanged(@AppOpsManager.UidState int newState) { |
| 1069 | if (mInProgressEvents == null) { |
| 1070 | return; |
| 1071 | } |
| 1072 | |
| 1073 | int numInProgressEvents = mInProgressEvents.size(); |
| 1074 | for (int i = 0; i < numInProgressEvents; i++) { |
| 1075 | InProgressStartOpEvent event = mInProgressEvents.valueAt(i); |
| 1076 | |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 1077 | if (event.getUidState() != newState) { |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 1078 | try { |
Philip P. Moltmann | 494b4ab | 2020-01-31 09:33:20 -0800 | [diff] [blame] | 1079 | // Remove all but one unfinished start count and then call finished() to |
| 1080 | // remove start event object |
| 1081 | int numPreviousUnfinishedStarts = event.numUnfinishedStarts; |
| 1082 | event.numUnfinishedStarts = 1; |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 1083 | finished(event.getClientId(), false); |
Philip P. Moltmann | 494b4ab | 2020-01-31 09:33:20 -0800 | [diff] [blame] | 1084 | |
| 1085 | // Call started() to add a new start event object and then add the |
| 1086 | // previously removed unfinished start counts back |
| 1087 | started(event.getClientId(), newState, false); |
| 1088 | event.numUnfinishedStarts += numPreviousUnfinishedStarts - 1; |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 1089 | } catch (RemoteException e) { |
| 1090 | if (DEBUG) Slog.e(TAG, "Cannot switch to new uidState " + newState); |
| 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 1096 | /** |
| 1097 | * Combine {@code a} and {@code b} and return the result. The result might be {@code a} |
| 1098 | * or {@code b}. If there is an event for the same key in both the later event is retained. |
| 1099 | */ |
| 1100 | private @Nullable LongSparseArray<NoteOpEvent> add(@Nullable LongSparseArray<NoteOpEvent> a, |
| 1101 | @Nullable LongSparseArray<NoteOpEvent> b) { |
| 1102 | if (a == null) { |
| 1103 | return b; |
| 1104 | } |
| 1105 | |
| 1106 | if (b == null) { |
| 1107 | return a; |
| 1108 | } |
| 1109 | |
| 1110 | int numEventsToAdd = b.size(); |
| 1111 | for (int i = 0; i < numEventsToAdd; i++) { |
| 1112 | long keyOfEventToAdd = b.keyAt(i); |
| 1113 | NoteOpEvent bEvent = b.valueAt(i); |
| 1114 | NoteOpEvent aEvent = a.get(keyOfEventToAdd); |
| 1115 | |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 1116 | if (aEvent == null || bEvent.getNoteTime() > aEvent.getNoteTime()) { |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 1117 | a.put(keyOfEventToAdd, bEvent); |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | return a; |
| 1122 | } |
| 1123 | |
| 1124 | /** |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1125 | * Add all data from the {@code opToAdd} to this op. |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 1126 | * |
| 1127 | * <p>If there is an event for the same key in both the later event is retained. |
| 1128 | * <p>{@code opToAdd} should not be used after this method is called. |
| 1129 | * |
| 1130 | * @param opToAdd The op to add |
| 1131 | */ |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1132 | public void add(@NonNull AttributedOp opToAdd) { |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 1133 | if (opToAdd.mInProgressEvents != null) { |
| 1134 | Slog.w(TAG, "Ignoring " + opToAdd.mInProgressEvents.size() + " running app-ops"); |
| 1135 | |
| 1136 | int numInProgressEvents = opToAdd.mInProgressEvents.size(); |
| 1137 | for (int i = 0; i < numInProgressEvents; i++) { |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 1138 | InProgressStartOpEvent event = opToAdd.mInProgressEvents.valueAt(i); |
| 1139 | |
| 1140 | event.finish(); |
| 1141 | mInProgressStartOpEventPool.release(event); |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | mAccessEvents = add(mAccessEvents, opToAdd.mAccessEvents); |
| 1146 | mRejectEvents = add(mRejectEvents, opToAdd.mRejectEvents); |
| 1147 | } |
| 1148 | |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 1149 | public boolean isRunning() { |
| 1150 | return mInProgressEvents != null; |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | boolean hasAnyTime() { |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 1154 | return (mAccessEvents != null && mAccessEvents.size() > 0) |
| 1155 | || (mRejectEvents != null && mRejectEvents.size() > 0); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 1158 | /** |
| 1159 | * Clone a {@link LongSparseArray} and clone all values. |
| 1160 | */ |
| 1161 | private @Nullable LongSparseArray<NoteOpEvent> deepClone( |
| 1162 | @Nullable LongSparseArray<NoteOpEvent> original) { |
| 1163 | if (original == null) { |
| 1164 | return original; |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 1167 | int size = original.size(); |
| 1168 | LongSparseArray<NoteOpEvent> clone = new LongSparseArray<>(size); |
| 1169 | for (int i = 0; i < size; i++) { |
| 1170 | clone.put(original.keyAt(i), new NoteOpEvent(original.valueAt(i))); |
| 1171 | } |
| 1172 | |
| 1173 | return clone; |
| 1174 | } |
| 1175 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1176 | @NonNull AttributedOpEntry createAttributedOpEntryLocked() { |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 1177 | LongSparseArray<NoteOpEvent> accessEvents = deepClone(mAccessEvents); |
| 1178 | |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 1179 | // Add in progress events as access events |
| 1180 | if (mInProgressEvents != null) { |
Philip P. Moltmann | e6ece90 | 2020-01-02 13:31:10 -0800 | [diff] [blame] | 1181 | long now = SystemClock.elapsedRealtime(); |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 1182 | int numInProgressEvents = mInProgressEvents.size(); |
| 1183 | |
| 1184 | if (accessEvents == null) { |
| 1185 | accessEvents = new LongSparseArray<>(numInProgressEvents); |
| 1186 | } |
| 1187 | |
| 1188 | for (int i = 0; i < numInProgressEvents; i++) { |
| 1189 | InProgressStartOpEvent event = mInProgressEvents.valueAt(i); |
| 1190 | |
| 1191 | // startOp events don't support proxy |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 1192 | accessEvents.append(makeKey(event.getUidState(), OP_FLAG_SELF), |
| 1193 | new NoteOpEvent(event.getStartTime(), now - event.getStartElapsedTime(), |
| 1194 | null)); |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 1195 | } |
| 1196 | } |
| 1197 | |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 1198 | LongSparseArray<NoteOpEvent> rejectEvents = deepClone(mRejectEvents); |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 1199 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1200 | return new AttributedOpEntry(parent.op, isRunning(), accessEvents, rejectEvents); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 1204 | final class Op { |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 1205 | int op; |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 1206 | int uid; |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 1207 | final UidState uidState; |
| 1208 | final @NonNull String packageName; |
| 1209 | |
| 1210 | private @Mode int mode; |
| 1211 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1212 | /** attributionTag -> AttributedOp */ |
| 1213 | final ArrayMap<String, AttributedOp> mAttributions = new ArrayMap<>(1); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1214 | |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 1215 | Op(UidState uidState, String packageName, int op, int uid) { |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 1216 | this.op = op; |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 1217 | this.uid = uid; |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 1218 | this.uidState = uidState; |
| 1219 | this.packageName = packageName; |
| 1220 | this.mode = AppOpsManager.opToDefaultMode(op); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1221 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 1222 | |
| 1223 | int getMode() { |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 1224 | return mode; |
| 1225 | } |
| 1226 | |
| 1227 | int evalMode() { |
| 1228 | return uidState.evalMode(op, mode); |
| 1229 | } |
| 1230 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1231 | void removeAttributionsWithNoTime() { |
| 1232 | for (int i = mAttributions.size() - 1; i >= 0; i--) { |
| 1233 | if (!mAttributions.valueAt(i).hasAnyTime()) { |
| 1234 | mAttributions.removeAt(i); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 1235 | } |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 1236 | } |
| 1237 | } |
| 1238 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1239 | private @NonNull AttributedOp getOrCreateAttribution(@NonNull Op parent, |
| 1240 | @Nullable String attributionTag) { |
| 1241 | AttributedOp attributedOp; |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 1242 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1243 | attributedOp = mAttributions.get(attributionTag); |
| 1244 | if (attributedOp == null) { |
| 1245 | attributedOp = new AttributedOp(attributionTag, parent); |
| 1246 | mAttributions.put(attributionTag, attributedOp); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 1247 | } |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 1248 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1249 | return attributedOp; |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 1250 | } |
| 1251 | |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 1252 | @NonNull OpEntry createEntryLocked() { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1253 | final int numAttributions = mAttributions.size(); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 1254 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1255 | final ArrayMap<String, AppOpsManager.AttributedOpEntry> attributionEntries = |
| 1256 | new ArrayMap<>(numAttributions); |
| 1257 | for (int i = 0; i < numAttributions; i++) { |
| 1258 | attributionEntries.put(mAttributions.keyAt(i), |
| 1259 | mAttributions.valueAt(i).createAttributedOpEntryLocked()); |
Philip P. Moltmann | 4052d36 | 2019-09-19 14:52:38 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1262 | return new OpEntry(op, mode, attributionEntries); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 1263 | } |
| 1264 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1265 | @NonNull OpEntry createSingleAttributionEntryLocked(@Nullable String attributionTag) { |
| 1266 | final int numAttributions = mAttributions.size(); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 1267 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1268 | final ArrayMap<String, AttributedOpEntry> attributionEntries = new ArrayMap<>(1); |
| 1269 | for (int i = 0; i < numAttributions; i++) { |
| 1270 | if (Objects.equals(mAttributions.keyAt(i), attributionTag)) { |
| 1271 | attributionEntries.put(mAttributions.keyAt(i), |
| 1272 | mAttributions.valueAt(i).createAttributedOpEntryLocked()); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 1273 | break; |
| 1274 | } |
| 1275 | } |
| 1276 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1277 | return new OpEntry(op, mode, attributionEntries); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 1278 | } |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 1279 | |
| 1280 | boolean isRunning() { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1281 | final int numAttributions = mAttributions.size(); |
| 1282 | for (int i = 0; i < numAttributions; i++) { |
| 1283 | if (mAttributions.valueAt(i).isRunning()) { |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 1284 | return true; |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | return false; |
| 1289 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1290 | } |
| 1291 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1292 | final SparseArray<ArraySet<ModeCallback>> mOpModeWatchers = new SparseArray<>(); |
| 1293 | final ArrayMap<String, ArraySet<ModeCallback>> mPackageModeWatchers = new ArrayMap<>(); |
| 1294 | final ArrayMap<IBinder, ModeCallback> mModeWatchers = new ArrayMap<>(); |
| 1295 | final ArrayMap<IBinder, SparseArray<ActiveCallback>> mActiveWatchers = new ArrayMap<>(); |
Adam Bookatz | 182862e | 2020-04-27 21:58:22 -0700 | [diff] [blame] | 1296 | final ArrayMap<IBinder, SparseArray<StartedCallback>> mStartedWatchers = new ArrayMap<>(); |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 1297 | final ArrayMap<IBinder, SparseArray<NotedCallback>> mNotedWatchers = new ArrayMap<>(); |
Yin-Chia Yeh | 51d8516 | 2019-08-06 15:31:39 -0700 | [diff] [blame] | 1298 | final AudioRestrictionManager mAudioRestrictionManager = new AudioRestrictionManager(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1299 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1300 | final class ModeCallback implements DeathRecipient { |
Philip P. Moltmann | b2bc651 | 2020-01-24 15:34:34 -0800 | [diff] [blame] | 1301 | /** If mWatchedOpCode==ALL_OPS notify for ops affected by the switch-op */ |
| 1302 | public static final int ALL_OPS = -2; |
| 1303 | |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1304 | final IAppOpsCallback mCallback; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1305 | final int mWatchingUid; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 1306 | final int mFlags; |
Philip P. Moltmann | b2bc651 | 2020-01-24 15:34:34 -0800 | [diff] [blame] | 1307 | final int mWatchedOpCode; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1308 | final int mCallingUid; |
| 1309 | final int mCallingPid; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1310 | |
Philip P. Moltmann | b2bc651 | 2020-01-24 15:34:34 -0800 | [diff] [blame] | 1311 | ModeCallback(IAppOpsCallback callback, int watchingUid, int flags, int watchedOp, |
| 1312 | int callingUid, int callingPid) { |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1313 | mCallback = callback; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1314 | mWatchingUid = watchingUid; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 1315 | mFlags = flags; |
Philip P. Moltmann | b2bc651 | 2020-01-24 15:34:34 -0800 | [diff] [blame] | 1316 | mWatchedOpCode = watchedOp; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1317 | mCallingUid = callingUid; |
| 1318 | mCallingPid = callingPid; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1319 | try { |
| 1320 | mCallback.asBinder().linkToDeath(this, 0); |
| 1321 | } catch (RemoteException e) { |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 1322 | /*ignored*/ |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1323 | } |
| 1324 | } |
| 1325 | |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 1326 | public boolean isWatchingUid(int uid) { |
| 1327 | return uid == UID_ANY || mWatchingUid < 0 || mWatchingUid == uid; |
| 1328 | } |
| 1329 | |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1330 | @Override |
| 1331 | public String toString() { |
| 1332 | StringBuilder sb = new StringBuilder(128); |
| 1333 | sb.append("ModeCallback{"); |
| 1334 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 1335 | sb.append(" watchinguid="); |
| 1336 | UserHandle.formatUid(sb, mWatchingUid); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 1337 | sb.append(" flags=0x"); |
| 1338 | sb.append(Integer.toHexString(mFlags)); |
Philip P. Moltmann | b992f8c | 2020-02-14 10:35:24 -0800 | [diff] [blame] | 1339 | switch (mWatchedOpCode) { |
| 1340 | case OP_NONE: |
| 1341 | break; |
| 1342 | case ALL_OPS: |
| 1343 | sb.append(" op=(all)"); |
| 1344 | break; |
| 1345 | default: |
| 1346 | sb.append(" op="); |
| 1347 | sb.append(opToName(mWatchedOpCode)); |
| 1348 | break; |
Philip P. Moltmann | b2bc651 | 2020-01-24 15:34:34 -0800 | [diff] [blame] | 1349 | } |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1350 | sb.append(" from uid="); |
| 1351 | UserHandle.formatUid(sb, mCallingUid); |
| 1352 | sb.append(" pid="); |
| 1353 | sb.append(mCallingPid); |
| 1354 | sb.append('}'); |
| 1355 | return sb.toString(); |
| 1356 | } |
| 1357 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1358 | void unlinkToDeath() { |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1359 | mCallback.asBinder().unlinkToDeath(this, 0); |
| 1360 | } |
| 1361 | |
| 1362 | @Override |
| 1363 | public void binderDied() { |
| 1364 | stopWatchingMode(mCallback); |
| 1365 | } |
| 1366 | } |
| 1367 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1368 | final class ActiveCallback implements DeathRecipient { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1369 | final IAppOpsActiveCallback mCallback; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1370 | final int mWatchingUid; |
| 1371 | final int mCallingUid; |
| 1372 | final int mCallingPid; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1373 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1374 | ActiveCallback(IAppOpsActiveCallback callback, int watchingUid, int callingUid, |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1375 | int callingPid) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1376 | mCallback = callback; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1377 | mWatchingUid = watchingUid; |
| 1378 | mCallingUid = callingUid; |
| 1379 | mCallingPid = callingPid; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1380 | try { |
| 1381 | mCallback.asBinder().linkToDeath(this, 0); |
| 1382 | } catch (RemoteException e) { |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 1383 | /*ignored*/ |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1384 | } |
| 1385 | } |
| 1386 | |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1387 | @Override |
| 1388 | public String toString() { |
| 1389 | StringBuilder sb = new StringBuilder(128); |
| 1390 | sb.append("ActiveCallback{"); |
| 1391 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 1392 | sb.append(" watchinguid="); |
| 1393 | UserHandle.formatUid(sb, mWatchingUid); |
| 1394 | sb.append(" from uid="); |
| 1395 | UserHandle.formatUid(sb, mCallingUid); |
| 1396 | sb.append(" pid="); |
| 1397 | sb.append(mCallingPid); |
| 1398 | sb.append('}'); |
| 1399 | return sb.toString(); |
| 1400 | } |
| 1401 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1402 | void destroy() { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1403 | mCallback.asBinder().unlinkToDeath(this, 0); |
| 1404 | } |
| 1405 | |
| 1406 | @Override |
| 1407 | public void binderDied() { |
| 1408 | stopWatchingActive(mCallback); |
| 1409 | } |
| 1410 | } |
| 1411 | |
Adam Bookatz | 182862e | 2020-04-27 21:58:22 -0700 | [diff] [blame] | 1412 | final class StartedCallback implements DeathRecipient { |
| 1413 | final IAppOpsStartedCallback mCallback; |
| 1414 | final int mWatchingUid; |
| 1415 | final int mCallingUid; |
| 1416 | final int mCallingPid; |
| 1417 | |
| 1418 | StartedCallback(IAppOpsStartedCallback callback, int watchingUid, int callingUid, |
| 1419 | int callingPid) { |
| 1420 | mCallback = callback; |
| 1421 | mWatchingUid = watchingUid; |
| 1422 | mCallingUid = callingUid; |
| 1423 | mCallingPid = callingPid; |
| 1424 | try { |
| 1425 | mCallback.asBinder().linkToDeath(this, 0); |
| 1426 | } catch (RemoteException e) { |
| 1427 | /*ignored*/ |
| 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | @Override |
| 1432 | public String toString() { |
| 1433 | StringBuilder sb = new StringBuilder(128); |
| 1434 | sb.append("StartedCallback{"); |
| 1435 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 1436 | sb.append(" watchinguid="); |
| 1437 | UserHandle.formatUid(sb, mWatchingUid); |
| 1438 | sb.append(" from uid="); |
| 1439 | UserHandle.formatUid(sb, mCallingUid); |
| 1440 | sb.append(" pid="); |
| 1441 | sb.append(mCallingPid); |
| 1442 | sb.append('}'); |
| 1443 | return sb.toString(); |
| 1444 | } |
| 1445 | |
| 1446 | void destroy() { |
| 1447 | mCallback.asBinder().unlinkToDeath(this, 0); |
| 1448 | } |
| 1449 | |
| 1450 | @Override |
| 1451 | public void binderDied() { |
| 1452 | stopWatchingStarted(mCallback); |
| 1453 | } |
| 1454 | } |
| 1455 | |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 1456 | final class NotedCallback implements DeathRecipient { |
| 1457 | final IAppOpsNotedCallback mCallback; |
| 1458 | final int mWatchingUid; |
| 1459 | final int mCallingUid; |
| 1460 | final int mCallingPid; |
| 1461 | |
| 1462 | NotedCallback(IAppOpsNotedCallback callback, int watchingUid, int callingUid, |
| 1463 | int callingPid) { |
| 1464 | mCallback = callback; |
| 1465 | mWatchingUid = watchingUid; |
| 1466 | mCallingUid = callingUid; |
| 1467 | mCallingPid = callingPid; |
| 1468 | try { |
| 1469 | mCallback.asBinder().linkToDeath(this, 0); |
| 1470 | } catch (RemoteException e) { |
| 1471 | /*ignored*/ |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | @Override |
| 1476 | public String toString() { |
| 1477 | StringBuilder sb = new StringBuilder(128); |
| 1478 | sb.append("NotedCallback{"); |
| 1479 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 1480 | sb.append(" watchinguid="); |
| 1481 | UserHandle.formatUid(sb, mWatchingUid); |
| 1482 | sb.append(" from uid="); |
| 1483 | UserHandle.formatUid(sb, mCallingUid); |
| 1484 | sb.append(" pid="); |
| 1485 | sb.append(mCallingPid); |
| 1486 | sb.append('}'); |
| 1487 | return sb.toString(); |
| 1488 | } |
| 1489 | |
| 1490 | void destroy() { |
| 1491 | mCallback.asBinder().unlinkToDeath(this, 0); |
| 1492 | } |
| 1493 | |
| 1494 | @Override |
| 1495 | public void binderDied() { |
| 1496 | stopWatchingNoted(mCallback); |
| 1497 | } |
| 1498 | } |
| 1499 | |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 1500 | /** |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1501 | * Call {@link AttributedOp#onClientDeath attributedOp.onClientDeath(clientId)}. |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 1502 | */ |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1503 | private static void onClientDeath(@NonNull AttributedOp attributedOp, |
| 1504 | @NonNull IBinder clientId) { |
| 1505 | attributedOp.onClientDeath(clientId); |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 1506 | } |
| 1507 | |
David Cheung | 2ead966 | 2020-02-19 16:11:06 -0800 | [diff] [blame] | 1508 | |
| 1509 | /** |
| 1510 | * Loads the OpsValidation file results into a hashmap {@link #mNoteOpCallerStacktraces} |
| 1511 | * so that we do not log the same operation twice between instances |
| 1512 | */ |
| 1513 | private void readNoteOpCallerStackTraces() { |
| 1514 | try { |
| 1515 | if (!mNoteOpCallerStacktracesFile.exists()) { |
| 1516 | mNoteOpCallerStacktracesFile.createNewFile(); |
| 1517 | return; |
| 1518 | } |
| 1519 | |
| 1520 | try (Scanner read = new Scanner(mNoteOpCallerStacktracesFile)) { |
| 1521 | read.useDelimiter("\\},"); |
| 1522 | while (read.hasNext()) { |
| 1523 | String jsonOps = read.next(); |
| 1524 | mNoteOpCallerStacktraces.add(NoteOpTrace.fromJson(jsonOps)); |
| 1525 | } |
| 1526 | } |
| 1527 | } catch (Exception e) { |
| 1528 | Slog.e(TAG, "Cannot parse traces noteOps", e); |
| 1529 | } |
| 1530 | } |
| 1531 | |
Philip P. Moltmann | fe2fc3a | 2020-02-05 16:59:39 -0800 | [diff] [blame] | 1532 | public AppOpsService(File storagePath, Handler handler, Context context) { |
| 1533 | mContext = context; |
| 1534 | |
Jeff Sharkey | 5f3e934 | 2017-03-13 14:53:11 -0600 | [diff] [blame] | 1535 | LockGuard.installLock(this, LockGuard.INDEX_APP_OPS); |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 1536 | mFile = new AtomicFile(storagePath, "appops"); |
David Cheung | 2ead966 | 2020-02-19 16:11:06 -0800 | [diff] [blame] | 1537 | if (AppOpsManager.NOTE_OP_COLLECTION_ENABLED) { |
| 1538 | mNoteOpCallerStacktracesFile = new File(SystemServiceManager.ensureSystemDir(), |
| 1539 | "noteOpStackTraces.json"); |
| 1540 | readNoteOpCallerStackTraces(); |
| 1541 | } else { |
| 1542 | mNoteOpCallerStacktracesFile = null; |
| 1543 | } |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 1544 | mHandler = handler; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 1545 | mConstants = new Constants(mHandler); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1546 | readState(); |
Philip P. Moltmann | b2bc651 | 2020-01-24 15:34:34 -0800 | [diff] [blame] | 1547 | |
| 1548 | for (int switchedCode = 0; switchedCode < _NUM_OP; switchedCode++) { |
| 1549 | int switchCode = AppOpsManager.opToSwitch(switchedCode); |
| 1550 | mSwitchedOps.put(switchCode, |
| 1551 | ArrayUtils.appendInt(mSwitchedOps.get(switchCode), switchedCode)); |
| 1552 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1553 | } |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 1554 | |
Philip P. Moltmann | fe2fc3a | 2020-02-05 16:59:39 -0800 | [diff] [blame] | 1555 | public void publish() { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1556 | ServiceManager.addService(Context.APP_OPS_SERVICE, asBinder()); |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 1557 | LocalServices.addService(AppOpsManagerInternal.class, mAppOpsManagerInternal); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1558 | } |
| 1559 | |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 1560 | /** Handler for work when packages are removed or updated */ |
| 1561 | private BroadcastReceiver mOnPackageUpdatedReceiver = new BroadcastReceiver() { |
| 1562 | @Override |
| 1563 | public void onReceive(Context context, Intent intent) { |
| 1564 | String action = intent.getAction(); |
| 1565 | String pkgName = intent.getData().getEncodedSchemeSpecificPart(); |
| 1566 | int uid = intent.getIntExtra(Intent.EXTRA_UID, Process.INVALID_UID); |
| 1567 | |
| 1568 | if (action.equals(ACTION_PACKAGE_REMOVED) && !intent.hasExtra(EXTRA_REPLACING)) { |
| 1569 | synchronized (AppOpsService.this) { |
| 1570 | UidState uidState = mUidStates.get(uid); |
| 1571 | if (uidState == null || uidState.pkgOps == null) { |
| 1572 | return; |
| 1573 | } |
| 1574 | |
| 1575 | Ops removedOps = uidState.pkgOps.remove(pkgName); |
| 1576 | if (removedOps != null) { |
| 1577 | scheduleFastWriteLocked(); |
| 1578 | } |
| 1579 | } |
| 1580 | } else if (action.equals(Intent.ACTION_PACKAGE_REPLACED)) { |
| 1581 | AndroidPackage pkg = LocalServices.getService( |
| 1582 | PackageManagerInternal.class).getPackage(pkgName); |
| 1583 | if (pkg == null) { |
| 1584 | return; |
| 1585 | } |
| 1586 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1587 | ArrayMap<String, String> dstAttributionTags = new ArrayMap<>(); |
| 1588 | ArraySet<String> attributionTags = new ArraySet<>(); |
| 1589 | attributionTags.add(null); |
| 1590 | if (pkg.getAttributions() != null) { |
| 1591 | int numAttributions = pkg.getAttributions().size(); |
| 1592 | for (int attributionNum = 0; attributionNum < numAttributions; |
| 1593 | attributionNum++) { |
| 1594 | ParsedAttribution attribution = pkg.getAttributions().get(attributionNum); |
| 1595 | attributionTags.add(attribution.tag); |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 1596 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1597 | int numInheritFrom = attribution.inheritFrom.size(); |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 1598 | for (int inheritFromNum = 0; inheritFromNum < numInheritFrom; |
| 1599 | inheritFromNum++) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1600 | dstAttributionTags.put(attribution.inheritFrom.get(inheritFromNum), |
| 1601 | attribution.tag); |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 1602 | } |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | synchronized (AppOpsService.this) { |
| 1607 | UidState uidState = mUidStates.get(uid); |
| 1608 | if (uidState == null || uidState.pkgOps == null) { |
| 1609 | return; |
| 1610 | } |
| 1611 | |
| 1612 | Ops ops = uidState.pkgOps.get(pkgName); |
| 1613 | if (ops == null) { |
| 1614 | return; |
| 1615 | } |
| 1616 | |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 1617 | // Reset cached package properties to re-initialize when needed |
| 1618 | ops.bypass = null; |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1619 | ops.knownAttributionTags.clear(); |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 1620 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1621 | // Merge data collected for removed attributions into their successor |
| 1622 | // attributions |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 1623 | int numOps = ops.size(); |
| 1624 | for (int opNum = 0; opNum < numOps; opNum++) { |
| 1625 | Op op = ops.valueAt(opNum); |
| 1626 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1627 | int numAttributions = op.mAttributions.size(); |
| 1628 | for (int attributionNum = numAttributions - 1; attributionNum >= 0; |
| 1629 | attributionNum--) { |
| 1630 | String attributionTag = op.mAttributions.keyAt(attributionNum); |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 1631 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1632 | if (attributionTags.contains(attributionTag)) { |
| 1633 | // attribution still exist after upgrade |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 1634 | continue; |
| 1635 | } |
| 1636 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1637 | String newAttributionTag = dstAttributionTags.get(attributionTag); |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 1638 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1639 | AttributedOp newAttributedOp = op.getOrCreateAttribution(op, |
| 1640 | newAttributionTag); |
| 1641 | newAttributedOp.add(op.mAttributions.valueAt(attributionNum)); |
| 1642 | op.mAttributions.removeAt(attributionNum); |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 1643 | |
| 1644 | scheduleFastWriteLocked(); |
| 1645 | } |
| 1646 | } |
| 1647 | } |
| 1648 | } |
| 1649 | } |
| 1650 | }; |
| 1651 | |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1652 | public void systemReady() { |
Dianne Hackborn | 45c79b0 | 2018-05-11 09:46:13 -0700 | [diff] [blame] | 1653 | mConstants.startMonitoring(mContext.getContentResolver()); |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 1654 | mHistoricalRegistry.systemReady(mContext.getContentResolver()); |
Dianne Hackborn | 45c79b0 | 2018-05-11 09:46:13 -0700 | [diff] [blame] | 1655 | |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 1656 | IntentFilter packageUpdateFilter = new IntentFilter(); |
| 1657 | packageUpdateFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 1658 | packageUpdateFilter.addAction(Intent.ACTION_PACKAGE_REPLACED); |
| 1659 | packageUpdateFilter.addDataScheme("package"); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1660 | |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 1661 | mContext.registerReceiver(mOnPackageUpdatedReceiver, packageUpdateFilter); |
| 1662 | |
| 1663 | synchronized (this) { |
| 1664 | for (int uidNum = mUidStates.size() - 1; uidNum >= 0; uidNum--) { |
| 1665 | int uid = mUidStates.keyAt(uidNum); |
| 1666 | UidState uidState = mUidStates.valueAt(uidNum); |
| 1667 | |
| 1668 | String[] pkgsInUid = getPackagesForUid(uidState.uid); |
| 1669 | if (ArrayUtils.isEmpty(pkgsInUid)) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1670 | uidState.clear(); |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 1671 | mUidStates.removeAt(uidNum); |
| 1672 | scheduleFastWriteLocked(); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1673 | continue; |
| 1674 | } |
| 1675 | |
| 1676 | ArrayMap<String, Ops> pkgs = uidState.pkgOps; |
| 1677 | if (pkgs == null) { |
| 1678 | continue; |
| 1679 | } |
| 1680 | |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 1681 | int numPkgs = pkgs.size(); |
| 1682 | for (int pkgNum = 0; pkgNum < numPkgs; pkgNum++) { |
| 1683 | String pkg = pkgs.keyAt(pkgNum); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1684 | |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 1685 | String action; |
| 1686 | if (!ArrayUtils.contains(pkgsInUid, pkg)) { |
| 1687 | action = Intent.ACTION_PACKAGE_REMOVED; |
| 1688 | } else { |
| 1689 | action = Intent.ACTION_PACKAGE_REPLACED; |
| 1690 | } |
| 1691 | |
| 1692 | SystemServerInitThreadPool.submit( |
| 1693 | () -> mOnPackageUpdatedReceiver.onReceive(mContext, new Intent(action) |
| 1694 | .setData(Uri.fromParts("package", pkg, null)) |
| 1695 | .putExtra(Intent.EXTRA_UID, uid)), |
| 1696 | "Update app-ops uidState in case package " + pkg + " changed"); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1697 | } |
| 1698 | } |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1699 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 1700 | |
Suprabh Shukla | 3017fe4 | 2018-11-08 19:00:01 -0800 | [diff] [blame] | 1701 | final IntentFilter packageSuspendFilter = new IntentFilter(); |
| 1702 | packageSuspendFilter.addAction(Intent.ACTION_PACKAGES_UNSUSPENDED); |
| 1703 | packageSuspendFilter.addAction(Intent.ACTION_PACKAGES_SUSPENDED); |
| 1704 | mContext.registerReceiver(new BroadcastReceiver() { |
| 1705 | @Override |
| 1706 | public void onReceive(Context context, Intent intent) { |
| 1707 | final int[] changedUids = intent.getIntArrayExtra(Intent.EXTRA_CHANGED_UID_LIST); |
| 1708 | final String[] changedPkgs = intent.getStringArrayExtra( |
| 1709 | Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Suprabh Shukla | b614a22 | 2019-09-12 14:42:46 -0700 | [diff] [blame] | 1710 | for (int code : OPS_RESTRICTED_ON_SUSPEND) { |
| 1711 | ArraySet<ModeCallback> callbacks; |
| 1712 | synchronized (AppOpsService.this) { |
| 1713 | callbacks = mOpModeWatchers.get(code); |
| 1714 | if (callbacks == null) { |
| 1715 | continue; |
| 1716 | } |
| 1717 | callbacks = new ArraySet<>(callbacks); |
Dianne Hackborn | 65e8de3 | 2019-04-04 11:01:41 -0700 | [diff] [blame] | 1718 | } |
Suprabh Shukla | b614a22 | 2019-09-12 14:42:46 -0700 | [diff] [blame] | 1719 | for (int i = 0; i < changedUids.length; i++) { |
| 1720 | final int changedUid = changedUids[i]; |
| 1721 | final String changedPkg = changedPkgs[i]; |
| 1722 | // We trust packagemanager to insert matching uid and packageNames in the |
| 1723 | // extras |
Suprabh Shukla | 7e01792 | 2019-08-05 17:13:23 -0700 | [diff] [blame] | 1724 | notifyOpChanged(callbacks, code, changedUid, changedPkg); |
| 1725 | } |
Suprabh Shukla | 3017fe4 | 2018-11-08 19:00:01 -0800 | [diff] [blame] | 1726 | } |
| 1727 | } |
| 1728 | }, packageSuspendFilter); |
| 1729 | |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 1730 | final IntentFilter packageAddedFilter = new IntentFilter(); |
| 1731 | packageAddedFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 1732 | packageAddedFilter.addDataScheme("package"); |
| 1733 | mContext.registerReceiver(new BroadcastReceiver() { |
| 1734 | @Override |
| 1735 | public void onReceive(Context context, Intent intent) { |
| 1736 | final Uri data = intent.getData(); |
| 1737 | |
| 1738 | final String packageName = data.getSchemeSpecificPart(); |
| 1739 | PackageInfo pi = LocalServices.getService( |
| 1740 | PackageManagerInternal.class).getPackageInfo(packageName, |
| 1741 | PackageManager.GET_PERMISSIONS, Process.myUid(), mContext.getUserId()); |
| 1742 | if (isSamplingTarget(pi)) { |
| 1743 | synchronized (this) { |
| 1744 | mRarelyUsedPackages.add(packageName); |
| 1745 | } |
| 1746 | } |
| 1747 | } |
| 1748 | }, packageAddedFilter); |
| 1749 | |
Stanislav Zholnin | 4c32385 | 2020-03-04 18:03:24 +0000 | [diff] [blame] | 1750 | mHandler.postDelayed(new Runnable() { |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 1751 | @Override |
| 1752 | public void run() { |
Stanislav Zholnin | 4c32385 | 2020-03-04 18:03:24 +0000 | [diff] [blame] | 1753 | List<String> packageNames = getPackageNamesForSampling(); |
| 1754 | resamplePackageAndAppOpLocked(packageNames); |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 1755 | initializeRarelyUsedPackagesList(new ArraySet<>(packageNames)); |
| 1756 | } |
Stanislav Zholnin | 4c32385 | 2020-03-04 18:03:24 +0000 | [diff] [blame] | 1757 | }, RARELY_USED_PACKAGES_INITIALIZATION_DELAY_MILLIS); |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 1758 | |
Suprabh Shukla | aef2513 | 2017-01-23 18:09:03 -0800 | [diff] [blame] | 1759 | PackageManagerInternal packageManagerInternal = LocalServices.getService( |
| 1760 | PackageManagerInternal.class); |
| 1761 | packageManagerInternal.setExternalSourcesPolicy( |
| 1762 | new PackageManagerInternal.ExternalSourcesPolicy() { |
| 1763 | @Override |
| 1764 | public int getPackageTrustedToInstallApps(String packageName, int uid) { |
| 1765 | int appOpMode = checkOperation(AppOpsManager.OP_REQUEST_INSTALL_PACKAGES, |
| 1766 | uid, packageName); |
| 1767 | switch (appOpMode) { |
| 1768 | case AppOpsManager.MODE_ALLOWED: |
| 1769 | return PackageManagerInternal.ExternalSourcesPolicy.USER_TRUSTED; |
| 1770 | case AppOpsManager.MODE_ERRORED: |
| 1771 | return PackageManagerInternal.ExternalSourcesPolicy.USER_BLOCKED; |
| 1772 | default: |
| 1773 | return PackageManagerInternal.ExternalSourcesPolicy.USER_DEFAULT; |
| 1774 | } |
| 1775 | } |
| 1776 | }); |
| 1777 | |
Jeff Sharkey | 10ec9d8 | 2018-11-28 14:52:45 -0700 | [diff] [blame] | 1778 | if (!StorageManager.hasIsolatedStorage()) { |
Sudheer Shanka | 98cb3f0 | 2018-08-17 16:10:29 -0700 | [diff] [blame] | 1779 | StorageManagerInternal storageManagerInternal = LocalServices.getService( |
| 1780 | StorageManagerInternal.class); |
| 1781 | storageManagerInternal.addExternalStoragePolicy( |
| 1782 | new StorageManagerInternal.ExternalStorageMountPolicy() { |
| 1783 | @Override |
| 1784 | public int getMountMode(int uid, String packageName) { |
| 1785 | if (Process.isIsolated(uid)) { |
| 1786 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 1787 | } |
| 1788 | if (noteOperation(AppOpsManager.OP_READ_EXTERNAL_STORAGE, uid, |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 1789 | packageName, null, true, "External storage policy") |
| 1790 | != AppOpsManager.MODE_ALLOWED) { |
Sudheer Shanka | 98cb3f0 | 2018-08-17 16:10:29 -0700 | [diff] [blame] | 1791 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 1792 | } |
| 1793 | if (noteOperation(AppOpsManager.OP_WRITE_EXTERNAL_STORAGE, uid, |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 1794 | packageName, null, true, "External storage policy") |
| 1795 | != AppOpsManager.MODE_ALLOWED) { |
Sudheer Shanka | 98cb3f0 | 2018-08-17 16:10:29 -0700 | [diff] [blame] | 1796 | return Zygote.MOUNT_EXTERNAL_READ; |
| 1797 | } |
| 1798 | return Zygote.MOUNT_EXTERNAL_WRITE; |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 1799 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 1800 | |
Sudheer Shanka | 98cb3f0 | 2018-08-17 16:10:29 -0700 | [diff] [blame] | 1801 | @Override |
| 1802 | public boolean hasExternalStorage(int uid, String packageName) { |
| 1803 | final int mountMode = getMountMode(uid, packageName); |
| 1804 | return mountMode == Zygote.MOUNT_EXTERNAL_READ |
| 1805 | || mountMode == Zygote.MOUNT_EXTERNAL_WRITE; |
| 1806 | } |
| 1807 | }); |
| 1808 | } |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 1809 | mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | public void packageRemoved(int uid, String packageName) { |
| 1813 | synchronized (this) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1814 | UidState uidState = mUidStates.get(uid); |
| 1815 | if (uidState == null) { |
| 1816 | return; |
| 1817 | } |
| 1818 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1819 | Ops ops = null; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1820 | |
| 1821 | // Remove any package state if such. |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1822 | if (uidState.pkgOps != null) { |
| 1823 | ops = uidState.pkgOps.remove(packageName); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | // If we just nuked the last package state check if the UID is valid. |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1827 | if (ops != null && uidState.pkgOps.isEmpty() |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1828 | && getPackagesForUid(uid).length <= 0) { |
| 1829 | mUidStates.remove(uid); |
| 1830 | } |
| 1831 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1832 | if (ops != null) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1833 | scheduleFastWriteLocked(); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1834 | |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 1835 | final int numOps = ops.size(); |
| 1836 | for (int opNum = 0; opNum < numOps; opNum++) { |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 1837 | final Op op = ops.valueAt(opNum); |
| 1838 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1839 | final int numAttributions = op.mAttributions.size(); |
| 1840 | for (int attributionNum = 0; attributionNum < numAttributions; |
| 1841 | attributionNum++) { |
| 1842 | AttributedOp attributedOp = op.mAttributions.valueAt(attributionNum); |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 1843 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1844 | while (attributedOp.mInProgressEvents != null) { |
| 1845 | attributedOp.finished(attributedOp.mInProgressEvents.keyAt(0)); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 1846 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1847 | } |
| 1848 | } |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1849 | } |
Winson | 470b15b | 2019-05-07 16:29:59 -0700 | [diff] [blame] | 1850 | |
| 1851 | mHistoricalRegistry.clearHistory(uid, packageName); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1852 | } |
| 1853 | } |
| 1854 | |
| 1855 | public void uidRemoved(int uid) { |
| 1856 | synchronized (this) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1857 | if (mUidStates.indexOfKey(uid) >= 0) { |
| 1858 | mUidStates.remove(uid); |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 1859 | scheduleFastWriteLocked(); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1860 | } |
| 1861 | } |
| 1862 | } |
| 1863 | |
Philip P. Moltmann | 463979e | 2020-01-24 14:41:22 -0800 | [diff] [blame] | 1864 | /** |
| 1865 | * Update the pending state for the uid |
| 1866 | * |
| 1867 | * @param currentTime The current elapsed real time |
| 1868 | * @param uid The uid that has a pending state |
| 1869 | */ |
| 1870 | private void updatePendingState(long currentTime, int uid) { |
| 1871 | synchronized (this) { |
| 1872 | mLastRealtime = max(currentTime, mLastRealtime); |
| 1873 | updatePendingStateIfNeededLocked(mUidStates.get(uid)); |
| 1874 | } |
| 1875 | } |
| 1876 | |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 1877 | public void updateUidProcState(int uid, int procState, |
| 1878 | @ActivityManager.ProcessCapability int capability) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1879 | synchronized (this) { |
| 1880 | final UidState uidState = getUidStateLocked(uid, true); |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 1881 | final int newState = PROCESS_STATE_TO_UID_STATE[procState]; |
| 1882 | if (uidState != null && (uidState.pendingState != newState |
| 1883 | || uidState.pendingCapability != capability)) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 1884 | final int oldPendingState = uidState.pendingState; |
| 1885 | uidState.pendingState = newState; |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 1886 | uidState.pendingCapability = capability; |
Wei Wang | 878d0b6 | 2019-03-28 18:12:18 -0700 | [diff] [blame] | 1887 | if (newState < uidState.state |
| 1888 | || (newState <= UID_STATE_MAX_LAST_NON_RESTRICTED |
| 1889 | && uidState.state > UID_STATE_MAX_LAST_NON_RESTRICTED)) { |
| 1890 | // We are moving to a more important state, or the new state may be in the |
| 1891 | // foreground and the old state is in the background, then always do it |
| 1892 | // immediately. |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 1893 | commitUidPendingStateLocked(uidState); |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 1894 | } else if (newState == uidState.state && capability != uidState.capability) { |
| 1895 | // No change on process state, but process capability has changed. |
| 1896 | commitUidPendingStateLocked(uidState); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 1897 | } else if (uidState.pendingStateCommitTime == 0) { |
| 1898 | // We are moving to a less important state for the first time, |
| 1899 | // delay the application for a bit. |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 1900 | final long settleTime; |
| 1901 | if (uidState.state <= UID_STATE_TOP) { |
| 1902 | settleTime = mConstants.TOP_STATE_SETTLE_TIME; |
| 1903 | } else if (uidState.state <= UID_STATE_FOREGROUND_SERVICE) { |
| 1904 | settleTime = mConstants.FG_SERVICE_STATE_SETTLE_TIME; |
| 1905 | } else { |
| 1906 | settleTime = mConstants.BG_STATE_SETTLE_TIME; |
| 1907 | } |
Philip P. Moltmann | 463979e | 2020-01-24 14:41:22 -0800 | [diff] [blame] | 1908 | final long commitTime = SystemClock.elapsedRealtime() + settleTime; |
| 1909 | uidState.pendingStateCommitTime = commitTime; |
| 1910 | |
| 1911 | mHandler.sendMessageDelayed( |
| 1912 | PooledLambda.obtainMessage(AppOpsService::updatePendingState, this, |
| 1913 | commitTime + 1, uid), settleTime + 1); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 1914 | } |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 1915 | |
| 1916 | if (uidState.pkgOps != null) { |
| 1917 | int numPkgs = uidState.pkgOps.size(); |
| 1918 | for (int pkgNum = 0; pkgNum < numPkgs; pkgNum++) { |
| 1919 | Ops ops = uidState.pkgOps.valueAt(pkgNum); |
| 1920 | |
| 1921 | int numOps = ops.size(); |
| 1922 | for (int opNum = 0; opNum < numOps; opNum++) { |
| 1923 | Op op = ops.valueAt(opNum); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 1924 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1925 | int numAttributions = op.mAttributions.size(); |
| 1926 | for (int attributionNum = 0; attributionNum < numAttributions; |
| 1927 | attributionNum++) { |
| 1928 | AttributedOp attributedOp = op.mAttributions.valueAt( |
| 1929 | attributionNum); |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 1930 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 1931 | attributedOp.onUidStateChanged(newState); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1932 | } |
| 1933 | } |
| 1934 | } |
| 1935 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1936 | } |
| 1937 | } |
| 1938 | } |
| 1939 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1940 | public void shutdown() { |
| 1941 | Slog.w(TAG, "Writing app ops before shutdown..."); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1942 | boolean doWrite = false; |
| 1943 | synchronized (this) { |
| 1944 | if (mWriteScheduled) { |
| 1945 | mWriteScheduled = false; |
| 1946 | doWrite = true; |
| 1947 | } |
| 1948 | } |
| 1949 | if (doWrite) { |
| 1950 | writeState(); |
| 1951 | } |
David Cheung | 2ead966 | 2020-02-19 16:11:06 -0800 | [diff] [blame] | 1952 | if (AppOpsManager.NOTE_OP_COLLECTION_ENABLED && mWriteNoteOpsScheduled) { |
| 1953 | writeNoteOps(); |
| 1954 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1955 | } |
| 1956 | |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 1957 | private ArrayList<AppOpsManager.OpEntry> collectOps(Ops pkgOps, int[] ops) { |
| 1958 | ArrayList<AppOpsManager.OpEntry> resOps = null; |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1959 | final long elapsedNow = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 1960 | if (ops == null) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1961 | resOps = new ArrayList<>(); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 1962 | for (int j=0; j<pkgOps.size(); j++) { |
| 1963 | Op curOp = pkgOps.valueAt(j); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 1964 | resOps.add(getOpEntryForResult(curOp, elapsedNow)); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 1965 | } |
| 1966 | } else { |
| 1967 | for (int j=0; j<ops.length; j++) { |
| 1968 | Op curOp = pkgOps.get(ops[j]); |
| 1969 | if (curOp != null) { |
| 1970 | if (resOps == null) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1971 | resOps = new ArrayList<>(); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 1972 | } |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 1973 | resOps.add(getOpEntryForResult(curOp, elapsedNow)); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 1974 | } |
| 1975 | } |
| 1976 | } |
| 1977 | return resOps; |
| 1978 | } |
| 1979 | |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 1980 | @Nullable |
| 1981 | private ArrayList<AppOpsManager.OpEntry> collectUidOps(@NonNull UidState uidState, |
| 1982 | @Nullable int[] ops) { |
| 1983 | if (uidState.opModes == null) { |
| 1984 | return null; |
| 1985 | } |
| 1986 | |
| 1987 | int opModeCount = uidState.opModes.size(); |
| 1988 | if (opModeCount == 0) { |
Eugene Susla | e4ee2c2 | 2018-11-05 12:23:30 -0800 | [diff] [blame] | 1989 | return null; |
| 1990 | } |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 1991 | ArrayList<AppOpsManager.OpEntry> resOps = null; |
| 1992 | if (ops == null) { |
| 1993 | resOps = new ArrayList<>(); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 1994 | for (int i = 0; i < opModeCount; i++) { |
| 1995 | int code = uidState.opModes.keyAt(i); |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 1996 | resOps.add(new OpEntry(code, uidState.opModes.get(code), Collections.emptyMap())); |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 1997 | } |
| 1998 | } else { |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 1999 | for (int j=0; j<ops.length; j++) { |
Philip P. Moltmann | f56fe2c | 2019-10-30 08:42:47 -0700 | [diff] [blame] | 2000 | int code = ops[j]; |
| 2001 | if (uidState.opModes.indexOfKey(code) >= 0) { |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2002 | if (resOps == null) { |
| 2003 | resOps = new ArrayList<>(); |
| 2004 | } |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 2005 | resOps.add(new OpEntry(code, uidState.opModes.get(code), |
| 2006 | Collections.emptyMap())); |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2007 | } |
| 2008 | } |
| 2009 | } |
| 2010 | return resOps; |
| 2011 | } |
| 2012 | |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 2013 | private static @NonNull OpEntry getOpEntryForResult(@NonNull Op op, long elapsedNow) { |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2014 | return op.createEntryLocked(); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 2015 | } |
| 2016 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2017 | @Override |
| 2018 | public List<AppOpsManager.PackageOps> getPackagesForOps(int[] ops) { |
| 2019 | mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS, |
| 2020 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 2021 | ArrayList<AppOpsManager.PackageOps> res = null; |
| 2022 | synchronized (this) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2023 | final int uidStateCount = mUidStates.size(); |
| 2024 | for (int i = 0; i < uidStateCount; i++) { |
| 2025 | UidState uidState = mUidStates.valueAt(i); |
| 2026 | if (uidState.pkgOps == null || uidState.pkgOps.isEmpty()) { |
| 2027 | continue; |
| 2028 | } |
| 2029 | ArrayMap<String, Ops> packages = uidState.pkgOps; |
| 2030 | final int packageCount = packages.size(); |
| 2031 | for (int j = 0; j < packageCount; j++) { |
| 2032 | Ops pkgOps = packages.valueAt(j); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 2033 | ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2034 | if (resOps != null) { |
| 2035 | if (res == null) { |
| 2036 | res = new ArrayList<AppOpsManager.PackageOps>(); |
| 2037 | } |
| 2038 | AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps( |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2039 | pkgOps.packageName, pkgOps.uidState.uid, resOps); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2040 | res.add(resPackage); |
| 2041 | } |
| 2042 | } |
| 2043 | } |
| 2044 | } |
| 2045 | return res; |
| 2046 | } |
| 2047 | |
| 2048 | @Override |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 2049 | public List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName, |
| 2050 | int[] ops) { |
| 2051 | mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS, |
| 2052 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 2053 | String resolvedPackageName = resolvePackageName(uid, packageName); |
| 2054 | if (resolvedPackageName == null) { |
| 2055 | return Collections.emptyList(); |
| 2056 | } |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 2057 | synchronized (this) { |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 2058 | Ops pkgOps = getOpsLocked(uid, resolvedPackageName, null, null, false /* edit */); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 2059 | if (pkgOps == null) { |
| 2060 | return null; |
| 2061 | } |
| 2062 | ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops); |
| 2063 | if (resOps == null) { |
| 2064 | return null; |
| 2065 | } |
| 2066 | ArrayList<AppOpsManager.PackageOps> res = new ArrayList<AppOpsManager.PackageOps>(); |
| 2067 | AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps( |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2068 | pkgOps.packageName, pkgOps.uidState.uid, resOps); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 2069 | res.add(resPackage); |
| 2070 | return res; |
| 2071 | } |
| 2072 | } |
| 2073 | |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 2074 | /** |
| 2075 | * Verify that historical appop request arguments are valid. |
| 2076 | */ |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 2077 | private void ensureHistoricalOpRequestIsValid(int uid, String packageName, |
| 2078 | String attributionTag, List<String> opNames, int filter, long beginTimeMillis, |
| 2079 | long endTimeMillis, int flags) { |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 2080 | if ((filter & FILTER_BY_UID) != 0) { |
| 2081 | Preconditions.checkArgument(uid != Process.INVALID_UID); |
| 2082 | } else { |
| 2083 | Preconditions.checkArgument(uid == Process.INVALID_UID); |
| 2084 | } |
| 2085 | |
| 2086 | if ((filter & FILTER_BY_PACKAGE_NAME) != 0) { |
| 2087 | Objects.requireNonNull(packageName); |
| 2088 | } else { |
| 2089 | Preconditions.checkArgument(packageName == null); |
| 2090 | } |
| 2091 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 2092 | if ((filter & FILTER_BY_ATTRIBUTION_TAG) == 0) { |
| 2093 | Preconditions.checkArgument(attributionTag == null); |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | if ((filter & FILTER_BY_OP_NAMES) != 0) { |
| 2097 | Objects.requireNonNull(opNames); |
| 2098 | } else { |
| 2099 | Preconditions.checkArgument(opNames == null); |
| 2100 | } |
| 2101 | |
| 2102 | Preconditions.checkFlagsArgument(filter, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 2103 | FILTER_BY_UID | FILTER_BY_PACKAGE_NAME | FILTER_BY_ATTRIBUTION_TAG |
| 2104 | | FILTER_BY_OP_NAMES); |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 2105 | Preconditions.checkArgumentNonnegative(beginTimeMillis); |
| 2106 | Preconditions.checkArgument(endTimeMillis > beginTimeMillis); |
| 2107 | Preconditions.checkFlagsArgument(flags, OP_FLAGS_ALL); |
| 2108 | } |
| 2109 | |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2110 | @Override |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 2111 | public void getHistoricalOps(int uid, String packageName, String attributionTag, |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 2112 | List<String> opNames, int filter, long beginTimeMillis, long endTimeMillis, |
| 2113 | int flags, RemoteCallback callback) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 2114 | ensureHistoricalOpRequestIsValid(uid, packageName, attributionTag, opNames, filter, |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 2115 | beginTimeMillis, endTimeMillis, flags); |
Daulet Zhanguzin | 9bd1370 | 2020-01-03 09:44:10 +0000 | [diff] [blame] | 2116 | Objects.requireNonNull(callback, "callback cannot be null"); |
Svet Ganov | ad0a49b | 2018-10-29 10:07:08 -0700 | [diff] [blame] | 2117 | |
Nate Myren | 697650b | 2020-01-23 13:25:06 -0800 | [diff] [blame] | 2118 | ActivityManagerInternal ami = LocalServices.getService(ActivityManagerInternal.class); |
| 2119 | boolean isCallerInstrumented = ami.isUidCurrentlyInstrumented(Binder.getCallingUid()); |
Stanislav Zholnin | 727e0e7 | 2020-02-11 17:45:22 +0000 | [diff] [blame] | 2120 | boolean isCallerSystem = Binder.getCallingPid() == Process.myPid(); |
Nate Myren | 697650b | 2020-01-23 13:25:06 -0800 | [diff] [blame] | 2121 | |
Stanislav Zholnin | 727e0e7 | 2020-02-11 17:45:22 +0000 | [diff] [blame] | 2122 | if (!isCallerSystem && !isCallerInstrumented) { |
Nate Myren | 697650b | 2020-01-23 13:25:06 -0800 | [diff] [blame] | 2123 | mHandler.post(() -> callback.sendResult(new Bundle())); |
| 2124 | return; |
| 2125 | } |
| 2126 | |
Svet Ganov | ad0a49b | 2018-10-29 10:07:08 -0700 | [diff] [blame] | 2127 | mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS, |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 2128 | Binder.getCallingPid(), Binder.getCallingUid(), "getHistoricalOps"); |
Svet Ganov | ad0a49b | 2018-10-29 10:07:08 -0700 | [diff] [blame] | 2129 | |
Svet Ganov | 23c88db | 2019-01-22 20:38:11 -0800 | [diff] [blame] | 2130 | final String[] opNamesArray = (opNames != null) |
| 2131 | ? opNames.toArray(new String[opNames.size()]) : null; |
Svet Ganov | ad0a49b | 2018-10-29 10:07:08 -0700 | [diff] [blame] | 2132 | |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 2133 | // Must not hold the appops lock |
Svet Ganov | 73fd86e | 2020-01-09 20:26:14 -0800 | [diff] [blame] | 2134 | mHandler.post(PooledLambda.obtainRunnable(HistoricalRegistry::getHistoricalOps, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 2135 | mHistoricalRegistry, uid, packageName, attributionTag, opNamesArray, filter, |
Svet Ganov | 73fd86e | 2020-01-09 20:26:14 -0800 | [diff] [blame] | 2136 | beginTimeMillis, endTimeMillis, flags, callback).recycleOnUse()); |
Svet Ganov | ad0a49b | 2018-10-29 10:07:08 -0700 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | @Override |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 2140 | public void getHistoricalOpsFromDiskRaw(int uid, String packageName, String attributionTag, |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 2141 | List<String> opNames, int filter, long beginTimeMillis, long endTimeMillis, |
| 2142 | int flags, RemoteCallback callback) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 2143 | ensureHistoricalOpRequestIsValid(uid, packageName, attributionTag, opNames, filter, |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 2144 | beginTimeMillis, endTimeMillis, flags); |
Daulet Zhanguzin | 9bd1370 | 2020-01-03 09:44:10 +0000 | [diff] [blame] | 2145 | Objects.requireNonNull(callback, "callback cannot be null"); |
Svet Ganov | ad0a49b | 2018-10-29 10:07:08 -0700 | [diff] [blame] | 2146 | |
Svet Ganov | 8e5bf96 | 2019-03-19 23:59:03 -0700 | [diff] [blame] | 2147 | mContext.enforcePermission(Manifest.permission.MANAGE_APPOPS, |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 2148 | Binder.getCallingPid(), Binder.getCallingUid(), "getHistoricalOps"); |
Svet Ganov | ad0a49b | 2018-10-29 10:07:08 -0700 | [diff] [blame] | 2149 | |
Svet Ganov | 23c88db | 2019-01-22 20:38:11 -0800 | [diff] [blame] | 2150 | final String[] opNamesArray = (opNames != null) |
| 2151 | ? opNames.toArray(new String[opNames.size()]) : null; |
| 2152 | |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 2153 | // Must not hold the appops lock |
Svet Ganov | 73fd86e | 2020-01-09 20:26:14 -0800 | [diff] [blame] | 2154 | mHandler.post(PooledLambda.obtainRunnable(HistoricalRegistry::getHistoricalOpsFromDiskRaw, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 2155 | mHistoricalRegistry, uid, packageName, attributionTag, opNamesArray, |
Svet Ganov | 73fd86e | 2020-01-09 20:26:14 -0800 | [diff] [blame] | 2156 | filter, beginTimeMillis, endTimeMillis, flags, callback).recycleOnUse()); |
Svet Ganov | ad0a49b | 2018-10-29 10:07:08 -0700 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | @Override |
Svet Ganov | 8e5bf96 | 2019-03-19 23:59:03 -0700 | [diff] [blame] | 2160 | public void reloadNonHistoricalState() { |
| 2161 | mContext.enforcePermission(Manifest.permission.MANAGE_APPOPS, |
| 2162 | Binder.getCallingPid(), Binder.getCallingUid(), "reloadNonHistoricalState"); |
| 2163 | writeState(); |
| 2164 | readState(); |
| 2165 | } |
| 2166 | |
| 2167 | @Override |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2168 | public List<AppOpsManager.PackageOps> getUidOps(int uid, int[] ops) { |
| 2169 | mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS, |
| 2170 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 2171 | synchronized (this) { |
| 2172 | UidState uidState = getUidStateLocked(uid, false); |
| 2173 | if (uidState == null) { |
| 2174 | return null; |
| 2175 | } |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2176 | ArrayList<AppOpsManager.OpEntry> resOps = collectUidOps(uidState, ops); |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2177 | if (resOps == null) { |
| 2178 | return null; |
| 2179 | } |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 2180 | ArrayList<AppOpsManager.PackageOps> res = new ArrayList<AppOpsManager.PackageOps>(); |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2181 | AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps( |
| 2182 | null, uidState.uid, resOps); |
| 2183 | res.add(resPackage); |
| 2184 | return res; |
| 2185 | } |
| 2186 | } |
| 2187 | |
Eugene Susla | 463d592 | 2019-07-17 18:14:15 -0700 | [diff] [blame] | 2188 | private void pruneOpLocked(Op op, int uid, String packageName) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 2189 | op.removeAttributionsWithNoTime(); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2190 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 2191 | if (op.mAttributions.isEmpty()) { |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 2192 | Ops ops = getOpsLocked(uid, packageName, null, null, false /* edit */); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2193 | if (ops != null) { |
| 2194 | ops.remove(op.op); |
| 2195 | if (ops.size() <= 0) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2196 | UidState uidState = ops.uidState; |
| 2197 | ArrayMap<String, Ops> pkgOps = uidState.pkgOps; |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2198 | if (pkgOps != null) { |
| 2199 | pkgOps.remove(ops.packageName); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2200 | if (pkgOps.isEmpty()) { |
| 2201 | uidState.pkgOps = null; |
| 2202 | } |
| 2203 | if (uidState.isDefault()) { |
| 2204 | mUidStates.remove(uid); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2205 | } |
| 2206 | } |
| 2207 | } |
| 2208 | } |
| 2209 | } |
| 2210 | } |
| 2211 | |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 2212 | private void enforceManageAppOpsModes(int callingPid, int callingUid, int targetUid) { |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 2213 | if (callingPid == Process.myPid()) { |
| 2214 | return; |
| 2215 | } |
| 2216 | final int callingUser = UserHandle.getUserId(callingUid); |
| 2217 | synchronized (this) { |
| 2218 | if (mProfileOwners != null && mProfileOwners.get(callingUser, -1) == callingUid) { |
| 2219 | if (targetUid >= 0 && callingUser == UserHandle.getUserId(targetUid)) { |
| 2220 | // Profile owners are allowed to change modes but only for apps |
| 2221 | // within their user. |
| 2222 | return; |
| 2223 | } |
| 2224 | } |
| 2225 | } |
| 2226 | mContext.enforcePermission(android.Manifest.permission.MANAGE_APP_OPS_MODES, |
| 2227 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 2228 | } |
| 2229 | |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 2230 | @Override |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2231 | public void setUidMode(int code, int uid, int mode) { |
Hai Zhang | f4ef039 | 2020-02-03 14:49:35 -0800 | [diff] [blame] | 2232 | setUidMode(code, uid, mode, null); |
| 2233 | } |
| 2234 | |
| 2235 | private void setUidMode(int code, int uid, int mode, |
Hai Zhang | 8846aaa | 2020-02-13 15:19:54 -0800 | [diff] [blame] | 2236 | @Nullable IAppOpsCallback permissionPolicyCallback) { |
Eugene Susla | e4ee2c2 | 2018-11-05 12:23:30 -0800 | [diff] [blame] | 2237 | if (DEBUG) { |
| 2238 | Slog.i(TAG, "uid " + uid + " OP_" + opToName(code) + " := " + modeToName(mode) |
| 2239 | + " by uid " + Binder.getCallingUid()); |
| 2240 | } |
| 2241 | |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 2242 | enforceManageAppOpsModes(Binder.getCallingPid(), Binder.getCallingUid(), uid); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2243 | verifyIncomingOp(code); |
| 2244 | code = AppOpsManager.opToSwitch(code); |
| 2245 | |
Hai Zhang | 8846aaa | 2020-02-13 15:19:54 -0800 | [diff] [blame] | 2246 | if (permissionPolicyCallback == null) { |
| 2247 | updatePermissionRevokedCompat(uid, code, mode); |
| 2248 | } |
Hai Zhang | e53e4c5 | 2019-10-08 18:57:01 -0700 | [diff] [blame] | 2249 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2250 | synchronized (this) { |
| 2251 | final int defaultMode = AppOpsManager.opToDefaultMode(code); |
| 2252 | |
| 2253 | UidState uidState = getUidStateLocked(uid, false); |
| 2254 | if (uidState == null) { |
| 2255 | if (mode == defaultMode) { |
| 2256 | return; |
| 2257 | } |
| 2258 | uidState = new UidState(uid); |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 2259 | uidState.opModes = new SparseIntArray(); |
| 2260 | uidState.opModes.put(code, mode); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2261 | mUidStates.put(uid, uidState); |
| 2262 | scheduleWriteLocked(); |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 2263 | } else if (uidState.opModes == null) { |
| 2264 | if (mode != defaultMode) { |
| 2265 | uidState.opModes = new SparseIntArray(); |
| 2266 | uidState.opModes.put(code, mode); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2267 | scheduleWriteLocked(); |
| 2268 | } |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 2269 | } else { |
| 2270 | if (uidState.opModes.indexOfKey(code) >= 0 && uidState.opModes.get(code) == mode) { |
| 2271 | return; |
| 2272 | } |
| 2273 | if (mode == defaultMode) { |
| 2274 | uidState.opModes.delete(code); |
| 2275 | if (uidState.opModes.size() <= 0) { |
| 2276 | uidState.opModes = null; |
| 2277 | } |
| 2278 | } else { |
| 2279 | uidState.opModes.put(code, mode); |
| 2280 | } |
| 2281 | scheduleWriteLocked(); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2282 | } |
Wei Wang | 711eb66 | 2019-03-21 18:24:17 -0700 | [diff] [blame] | 2283 | uidState.evalForegroundOps(mOpModeWatchers); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
Hai Zhang | 8846aaa | 2020-02-13 15:19:54 -0800 | [diff] [blame] | 2286 | notifyOpChangedForAllPkgsInUid(code, uid, false, permissionPolicyCallback); |
Philip P. Moltmann | 584c23a | 2020-01-24 14:21:12 -0800 | [diff] [blame] | 2287 | notifyOpChangedSync(code, uid, null, mode); |
| 2288 | } |
| 2289 | |
| 2290 | /** |
| 2291 | * Notify that an op changed for all packages in an uid. |
| 2292 | * |
| 2293 | * @param code The op that changed |
| 2294 | * @param uid The uid the op was changed for |
| 2295 | * @param onlyForeground Only notify watchers that watch for foreground changes |
| 2296 | */ |
| 2297 | private void notifyOpChangedForAllPkgsInUid(int code, int uid, boolean onlyForeground, |
| 2298 | @Nullable IAppOpsCallback callbackToIgnore) { |
Svetoslav | 215b44a | 2015-08-04 19:03:40 -0700 | [diff] [blame] | 2299 | String[] uidPackageNames = getPackagesForUid(uid); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2300 | ArrayMap<ModeCallback, ArraySet<String>> callbackSpecs = null; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2301 | |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 2302 | synchronized (this) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2303 | ArraySet<ModeCallback> callbacks = mOpModeWatchers.get(code); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2304 | if (callbacks != null) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2305 | final int callbackCount = callbacks.size(); |
| 2306 | for (int i = 0; i < callbackCount; i++) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2307 | ModeCallback callback = callbacks.valueAt(i); |
Philip P. Moltmann | 584c23a | 2020-01-24 14:21:12 -0800 | [diff] [blame] | 2308 | if (onlyForeground && (callback.mFlags & WATCH_FOREGROUND_CHANGES) == 0) { |
| 2309 | continue; |
| 2310 | } |
| 2311 | |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 2312 | ArraySet<String> changedPackages = new ArraySet<>(); |
| 2313 | Collections.addAll(changedPackages, uidPackageNames); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 2314 | if (callbackSpecs == null) { |
| 2315 | callbackSpecs = new ArrayMap<>(); |
| 2316 | } |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 2317 | callbackSpecs.put(callback, changedPackages); |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | for (String uidPackageName : uidPackageNames) { |
| 2322 | callbacks = mPackageModeWatchers.get(uidPackageName); |
| 2323 | if (callbacks != null) { |
| 2324 | if (callbackSpecs == null) { |
| 2325 | callbackSpecs = new ArrayMap<>(); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2326 | } |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 2327 | final int callbackCount = callbacks.size(); |
| 2328 | for (int i = 0; i < callbackCount; i++) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2329 | ModeCallback callback = callbacks.valueAt(i); |
Philip P. Moltmann | 584c23a | 2020-01-24 14:21:12 -0800 | [diff] [blame] | 2330 | if (onlyForeground && (callback.mFlags & WATCH_FOREGROUND_CHANGES) == 0) { |
| 2331 | continue; |
| 2332 | } |
| 2333 | |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 2334 | ArraySet<String> changedPackages = callbackSpecs.get(callback); |
| 2335 | if (changedPackages == null) { |
| 2336 | changedPackages = new ArraySet<>(); |
| 2337 | callbackSpecs.put(callback, changedPackages); |
| 2338 | } |
| 2339 | changedPackages.add(uidPackageName); |
| 2340 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2341 | } |
| 2342 | } |
Hai Zhang | f4ef039 | 2020-02-03 14:49:35 -0800 | [diff] [blame] | 2343 | |
| 2344 | if (callbackSpecs != null && callbackToIgnore != null) { |
| 2345 | callbackSpecs.remove(mModeWatchers.get(callbackToIgnore.asBinder())); |
| 2346 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | if (callbackSpecs == null) { |
| 2350 | return; |
| 2351 | } |
| 2352 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2353 | for (int i = 0; i < callbackSpecs.size(); i++) { |
| 2354 | final ModeCallback callback = callbackSpecs.keyAt(i); |
| 2355 | final ArraySet<String> reportedPackageNames = callbackSpecs.valueAt(i); |
| 2356 | if (reportedPackageNames == null) { |
| 2357 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 2358 | AppOpsService::notifyOpChanged, |
| 2359 | this, callback, code, uid, (String) null)); |
| 2360 | |
| 2361 | } else { |
| 2362 | final int reportedPackageCount = reportedPackageNames.size(); |
| 2363 | for (int j = 0; j < reportedPackageCount; j++) { |
| 2364 | final String reportedPackageName = reportedPackageNames.valueAt(j); |
| 2365 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 2366 | AppOpsService::notifyOpChanged, |
| 2367 | this, callback, code, uid, reportedPackageName)); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2368 | } |
| 2369 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2370 | } |
Sudheer Shanka | b161398 | 2019-05-16 16:55:50 -0700 | [diff] [blame] | 2371 | } |
| 2372 | |
Hai Zhang | e53e4c5 | 2019-10-08 18:57:01 -0700 | [diff] [blame] | 2373 | private void updatePermissionRevokedCompat(int uid, int switchCode, int mode) { |
| 2374 | PackageManager packageManager = mContext.getPackageManager(); |
Philip P. Moltmann | fe2fc3a | 2020-02-05 16:59:39 -0800 | [diff] [blame] | 2375 | if (packageManager == null) { |
| 2376 | // This can only happen during early boot. At this time the permission state and appop |
| 2377 | // state are in sync |
| 2378 | return; |
| 2379 | } |
| 2380 | |
Hai Zhang | e53e4c5 | 2019-10-08 18:57:01 -0700 | [diff] [blame] | 2381 | String[] packageNames = packageManager.getPackagesForUid(uid); |
| 2382 | if (ArrayUtils.isEmpty(packageNames)) { |
| 2383 | return; |
| 2384 | } |
| 2385 | String packageName = packageNames[0]; |
| 2386 | |
Philip P. Moltmann | b2bc651 | 2020-01-24 15:34:34 -0800 | [diff] [blame] | 2387 | int[] ops = mSwitchedOps.get(switchCode); |
| 2388 | for (int code : ops) { |
Hai Zhang | e53e4c5 | 2019-10-08 18:57:01 -0700 | [diff] [blame] | 2389 | String permissionName = AppOpsManager.opToPermission(code); |
| 2390 | if (permissionName == null) { |
| 2391 | continue; |
| 2392 | } |
| 2393 | |
Hai Zhang | 8642553 | 2020-02-03 19:45:15 -0800 | [diff] [blame] | 2394 | if (packageManager.checkPermission(permissionName, packageName) |
| 2395 | != PackageManager.PERMISSION_GRANTED) { |
| 2396 | continue; |
| 2397 | } |
| 2398 | |
Hai Zhang | e53e4c5 | 2019-10-08 18:57:01 -0700 | [diff] [blame] | 2399 | PermissionInfo permissionInfo; |
| 2400 | try { |
| 2401 | permissionInfo = packageManager.getPermissionInfo(permissionName, 0); |
| 2402 | } catch (PackageManager.NameNotFoundException e) { |
| 2403 | e.printStackTrace(); |
| 2404 | continue; |
| 2405 | } |
| 2406 | |
| 2407 | if (!permissionInfo.isRuntime()) { |
| 2408 | continue; |
| 2409 | } |
| 2410 | |
Hai Zhang | 23d30a2 | 2020-02-04 11:06:31 -0800 | [diff] [blame] | 2411 | PackageManagerInternal packageManagerInternal = LocalServices.getService( |
| 2412 | PackageManagerInternal.class); |
| 2413 | boolean supportsRuntimePermissions = packageManagerInternal.getUidTargetSdkVersion(uid) |
| 2414 | >= Build.VERSION_CODES.M; |
| 2415 | |
Hai Zhang | e53e4c5 | 2019-10-08 18:57:01 -0700 | [diff] [blame] | 2416 | UserHandle user = UserHandle.getUserHandleForUid(uid); |
| 2417 | boolean isRevokedCompat; |
| 2418 | if (permissionInfo.backgroundPermission != null) { |
Hai Zhang | 29674dd | 2020-02-05 01:36:25 -0800 | [diff] [blame] | 2419 | if (packageManager.checkPermission(permissionInfo.backgroundPermission, packageName) |
| 2420 | == PackageManager.PERMISSION_GRANTED) { |
| 2421 | boolean isBackgroundRevokedCompat = mode != AppOpsManager.MODE_ALLOWED; |
Hai Zhang | 23d30a2 | 2020-02-04 11:06:31 -0800 | [diff] [blame] | 2422 | |
Hai Zhang | 29674dd | 2020-02-05 01:36:25 -0800 | [diff] [blame] | 2423 | if (isBackgroundRevokedCompat && supportsRuntimePermissions) { |
| 2424 | Slog.w(TAG, "setUidMode() called with a mode inconsistent with runtime" |
| 2425 | + " permission state, this is discouraged and you should revoke the" |
| 2426 | + " runtime permission instead: uid=" + uid + ", switchCode=" |
| 2427 | + switchCode + ", mode=" + mode + ", permission=" |
| 2428 | + permissionInfo.backgroundPermission); |
| 2429 | } |
Hai Zhang | 23d30a2 | 2020-02-04 11:06:31 -0800 | [diff] [blame] | 2430 | |
Hai Zhang | 29674dd | 2020-02-05 01:36:25 -0800 | [diff] [blame] | 2431 | long identity = Binder.clearCallingIdentity(); |
| 2432 | try { |
| 2433 | packageManager.updatePermissionFlags(permissionInfo.backgroundPermission, |
| 2434 | packageName, PackageManager.FLAG_PERMISSION_REVOKED_COMPAT, |
| 2435 | isBackgroundRevokedCompat |
| 2436 | ? PackageManager.FLAG_PERMISSION_REVOKED_COMPAT : 0, user); |
| 2437 | } finally { |
| 2438 | Binder.restoreCallingIdentity(identity); |
| 2439 | } |
Hai Zhang | e53e4c5 | 2019-10-08 18:57:01 -0700 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | isRevokedCompat = mode != AppOpsManager.MODE_ALLOWED |
| 2443 | && mode != AppOpsManager.MODE_FOREGROUND; |
| 2444 | } else { |
| 2445 | isRevokedCompat = mode != AppOpsManager.MODE_ALLOWED; |
| 2446 | } |
| 2447 | |
Hai Zhang | 23d30a2 | 2020-02-04 11:06:31 -0800 | [diff] [blame] | 2448 | if (isRevokedCompat && supportsRuntimePermissions) { |
| 2449 | Slog.w(TAG, "setUidMode() called with a mode inconsistent with runtime" |
| 2450 | + " permission state, this is discouraged and you should revoke the" |
| 2451 | + " runtime permission instead: uid=" + uid + ", switchCode=" |
| 2452 | + switchCode + ", mode=" + mode + ", permission=" + permissionName); |
| 2453 | } |
| 2454 | |
Hai Zhang | e53e4c5 | 2019-10-08 18:57:01 -0700 | [diff] [blame] | 2455 | long identity = Binder.clearCallingIdentity(); |
| 2456 | try { |
| 2457 | packageManager.updatePermissionFlags(permissionName, packageName, |
| 2458 | PackageManager.FLAG_PERMISSION_REVOKED_COMPAT, isRevokedCompat |
| 2459 | ? PackageManager.FLAG_PERMISSION_REVOKED_COMPAT : 0, user); |
| 2460 | } finally { |
| 2461 | Binder.restoreCallingIdentity(identity); |
| 2462 | } |
| 2463 | } |
| 2464 | } |
| 2465 | |
Sudheer Shanka | b161398 | 2019-05-16 16:55:50 -0700 | [diff] [blame] | 2466 | private void notifyOpChangedSync(int code, int uid, @NonNull String packageName, int mode) { |
| 2467 | final StorageManagerInternal storageManagerInternal = |
| 2468 | LocalServices.getService(StorageManagerInternal.class); |
| 2469 | if (storageManagerInternal != null) { |
| 2470 | storageManagerInternal.onAppOpsChanged(code, uid, packageName, mode); |
| 2471 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2472 | } |
| 2473 | |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 2474 | /** |
| 2475 | * Sets the mode for a certain op and uid. |
| 2476 | * |
| 2477 | * @param code The op code to set |
| 2478 | * @param uid The UID for which to set |
| 2479 | * @param packageName The package for which to set |
| 2480 | * @param mode The new mode to set |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 2481 | */ |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 2482 | @Override |
| 2483 | public void setMode(int code, int uid, @NonNull String packageName, int mode) { |
Hai Zhang | f4ef039 | 2020-02-03 14:49:35 -0800 | [diff] [blame] | 2484 | setMode(code, uid, packageName, mode, null); |
| 2485 | } |
| 2486 | |
| 2487 | private void setMode(int code, int uid, @NonNull String packageName, int mode, |
Hai Zhang | 8846aaa | 2020-02-13 15:19:54 -0800 | [diff] [blame] | 2488 | @Nullable IAppOpsCallback permissionPolicyCallback) { |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 2489 | enforceManageAppOpsModes(Binder.getCallingPid(), Binder.getCallingUid(), uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 2490 | verifyIncomingOp(code); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2491 | ArraySet<ModeCallback> repCbs = null; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2492 | code = AppOpsManager.opToSwitch(code); |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 2493 | |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 2494 | RestrictionBypass bypass; |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 2495 | try { |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 2496 | bypass = verifyAndGetBypass(uid, packageName, null); |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 2497 | } catch (SecurityException e) { |
| 2498 | Slog.e(TAG, "Cannot setMode", e); |
| 2499 | return; |
| 2500 | } |
| 2501 | |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 2502 | synchronized (this) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2503 | UidState uidState = getUidStateLocked(uid, false); |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 2504 | Op op = getOpLocked(code, uid, packageName, null, bypass, true); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 2505 | if (op != null) { |
| 2506 | if (op.mode != mode) { |
| 2507 | op.mode = mode; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 2508 | if (uidState != null) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 2509 | uidState.evalForegroundOps(mOpModeWatchers); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 2510 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2511 | ArraySet<ModeCallback> cbs = mOpModeWatchers.get(code); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2512 | if (cbs != null) { |
| 2513 | if (repCbs == null) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2514 | repCbs = new ArraySet<>(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2515 | } |
| 2516 | repCbs.addAll(cbs); |
| 2517 | } |
| 2518 | cbs = mPackageModeWatchers.get(packageName); |
| 2519 | if (cbs != null) { |
| 2520 | if (repCbs == null) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2521 | repCbs = new ArraySet<>(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2522 | } |
| 2523 | repCbs.addAll(cbs); |
| 2524 | } |
Hai Zhang | 8846aaa | 2020-02-13 15:19:54 -0800 | [diff] [blame] | 2525 | if (repCbs != null && permissionPolicyCallback != null) { |
| 2526 | repCbs.remove(mModeWatchers.get(permissionPolicyCallback.asBinder())); |
Hai Zhang | f4ef039 | 2020-02-03 14:49:35 -0800 | [diff] [blame] | 2527 | } |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 2528 | if (mode == AppOpsManager.opToDefaultMode(op.op)) { |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 2529 | // If going into the default mode, prune this op |
| 2530 | // if there is nothing else interesting in it. |
Eugene Susla | 463d592 | 2019-07-17 18:14:15 -0700 | [diff] [blame] | 2531 | pruneOpLocked(op, uid, packageName); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 2532 | } |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 2533 | scheduleFastWriteLocked(); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 2534 | } |
| 2535 | } |
| 2536 | } |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2537 | if (repCbs != null) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2538 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 2539 | AppOpsService::notifyOpChanged, |
| 2540 | this, repCbs, code, uid, packageName)); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2541 | } |
Sudheer Shanka | b161398 | 2019-05-16 16:55:50 -0700 | [diff] [blame] | 2542 | |
| 2543 | notifyOpChangedSync(code, uid, packageName, mode); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2544 | } |
| 2545 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2546 | private void notifyOpChanged(ArraySet<ModeCallback> callbacks, int code, |
| 2547 | int uid, String packageName) { |
| 2548 | for (int i = 0; i < callbacks.size(); i++) { |
| 2549 | final ModeCallback callback = callbacks.valueAt(i); |
| 2550 | notifyOpChanged(callback, code, uid, packageName); |
| 2551 | } |
| 2552 | } |
| 2553 | |
| 2554 | private void notifyOpChanged(ModeCallback callback, int code, |
| 2555 | int uid, String packageName) { |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 2556 | if (uid != UID_ANY && callback.mWatchingUid >= 0 && callback.mWatchingUid != uid) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2557 | return; |
| 2558 | } |
Philip P. Moltmann | b2bc651 | 2020-01-24 15:34:34 -0800 | [diff] [blame] | 2559 | |
| 2560 | // See CALL_BACK_ON_CHANGED_LISTENER_WITH_SWITCHED_OP_CHANGE |
| 2561 | int[] switchedCodes; |
| 2562 | if (callback.mWatchedOpCode == ALL_OPS) { |
| 2563 | switchedCodes = mSwitchedOps.get(code); |
| 2564 | } else if (callback.mWatchedOpCode == OP_NONE) { |
| 2565 | switchedCodes = new int[]{code}; |
| 2566 | } else { |
| 2567 | switchedCodes = new int[]{callback.mWatchedOpCode}; |
| 2568 | } |
| 2569 | |
| 2570 | for (int switchedCode : switchedCodes) { |
| 2571 | // There are features watching for mode changes such as window manager |
| 2572 | // and location manager which are in our process. The callbacks in these |
| 2573 | // features may require permissions our remote caller does not have. |
| 2574 | final long identity = Binder.clearCallingIdentity(); |
| 2575 | try { |
| 2576 | callback.mCallback.opChanged(switchedCode, uid, packageName); |
| 2577 | } catch (RemoteException e) { |
| 2578 | /* ignore */ |
| 2579 | } finally { |
| 2580 | Binder.restoreCallingIdentity(identity); |
| 2581 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2582 | } |
| 2583 | } |
| 2584 | |
| 2585 | private static HashMap<ModeCallback, ArrayList<ChangeRec>> addCallbacks( |
| 2586 | HashMap<ModeCallback, ArrayList<ChangeRec>> callbacks, |
| 2587 | int op, int uid, String packageName, ArraySet<ModeCallback> cbs) { |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2588 | if (cbs == null) { |
| 2589 | return callbacks; |
| 2590 | } |
| 2591 | if (callbacks == null) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 2592 | callbacks = new HashMap<>(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2593 | } |
Dianne Hackborn | 68d7655 | 2017-02-27 15:32:03 -0800 | [diff] [blame] | 2594 | final int N = cbs.size(); |
| 2595 | for (int i=0; i<N; i++) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2596 | ModeCallback cb = cbs.valueAt(i); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 2597 | ArrayList<ChangeRec> reports = callbacks.get(cb); |
Narayan Kamath | 1dfc1ab | 2020-05-01 15:17:03 +0100 | [diff] [blame] | 2598 | boolean duplicate = false; |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2599 | if (reports == null) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 2600 | reports = new ArrayList<>(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2601 | callbacks.put(cb, reports); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2602 | } else { |
| 2603 | final int reportCount = reports.size(); |
| 2604 | for (int j = 0; j < reportCount; j++) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 2605 | ChangeRec report = reports.get(j); |
| 2606 | if (report.op == op && report.pkg.equals(packageName)) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2607 | duplicate = true; |
| 2608 | break; |
| 2609 | } |
| 2610 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2611 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2612 | if (!duplicate) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 2613 | reports.add(new ChangeRec(op, uid, packageName)); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2614 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2615 | } |
| 2616 | return callbacks; |
| 2617 | } |
| 2618 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 2619 | static final class ChangeRec { |
| 2620 | final int op; |
| 2621 | final int uid; |
| 2622 | final String pkg; |
| 2623 | |
| 2624 | ChangeRec(int _op, int _uid, String _pkg) { |
| 2625 | op = _op; |
| 2626 | uid = _uid; |
| 2627 | pkg = _pkg; |
| 2628 | } |
| 2629 | } |
| 2630 | |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2631 | @Override |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 2632 | public void resetAllModes(int reqUserId, String reqPackageName) { |
| 2633 | final int callingPid = Binder.getCallingPid(); |
| 2634 | final int callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 2635 | reqUserId = ActivityManager.handleIncomingUser(callingPid, callingUid, reqUserId, |
| 2636 | true, true, "resetAllModes", null); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2637 | |
| 2638 | int reqUid = -1; |
| 2639 | if (reqPackageName != null) { |
| 2640 | try { |
| 2641 | reqUid = AppGlobals.getPackageManager().getPackageUid( |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 2642 | reqPackageName, PackageManager.MATCH_UNINSTALLED_PACKAGES, reqUserId); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2643 | } catch (RemoteException e) { |
| 2644 | /* ignore - local call */ |
| 2645 | } |
| 2646 | } |
| 2647 | |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 2648 | enforceManageAppOpsModes(callingPid, callingUid, reqUid); |
| 2649 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2650 | HashMap<ModeCallback, ArrayList<ChangeRec>> callbacks = null; |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2651 | synchronized (this) { |
| 2652 | boolean changed = false; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2653 | for (int i = mUidStates.size() - 1; i >= 0; i--) { |
| 2654 | UidState uidState = mUidStates.valueAt(i); |
| 2655 | |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 2656 | SparseIntArray opModes = uidState.opModes; |
| 2657 | if (opModes != null && (uidState.uid == reqUid || reqUid == -1)) { |
| 2658 | final int uidOpCount = opModes.size(); |
| 2659 | for (int j = uidOpCount - 1; j >= 0; j--) { |
| 2660 | final int code = opModes.keyAt(j); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2661 | if (AppOpsManager.opAllowsReset(code)) { |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 2662 | opModes.removeAt(j); |
| 2663 | if (opModes.size() <= 0) { |
| 2664 | uidState.opModes = null; |
| 2665 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2666 | for (String packageName : getPackagesForUid(uidState.uid)) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 2667 | callbacks = addCallbacks(callbacks, code, uidState.uid, packageName, |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2668 | mOpModeWatchers.get(code)); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 2669 | callbacks = addCallbacks(callbacks, code, uidState.uid, packageName, |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2670 | mPackageModeWatchers.get(packageName)); |
| 2671 | } |
| 2672 | } |
| 2673 | } |
| 2674 | } |
| 2675 | |
| 2676 | if (uidState.pkgOps == null) { |
| 2677 | continue; |
| 2678 | } |
| 2679 | |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 2680 | if (reqUserId != UserHandle.USER_ALL |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2681 | && reqUserId != UserHandle.getUserId(uidState.uid)) { |
Alexandra Gherghina | d6a9897 | 2014-08-04 17:05:34 +0100 | [diff] [blame] | 2682 | // Skip any ops for a different user |
| 2683 | continue; |
| 2684 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2685 | |
| 2686 | Map<String, Ops> packages = uidState.pkgOps; |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 2687 | Iterator<Map.Entry<String, Ops>> it = packages.entrySet().iterator(); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 2688 | boolean uidChanged = false; |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 2689 | while (it.hasNext()) { |
| 2690 | Map.Entry<String, Ops> ent = it.next(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2691 | String packageName = ent.getKey(); |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 2692 | if (reqPackageName != null && !reqPackageName.equals(packageName)) { |
| 2693 | // Skip any ops for a different package |
| 2694 | continue; |
| 2695 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2696 | Ops pkgOps = ent.getValue(); |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 2697 | for (int j=pkgOps.size()-1; j>=0; j--) { |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2698 | Op curOp = pkgOps.valueAt(j); |
Dianne Hackborn | 8828d3a | 2013-09-25 16:47:10 -0700 | [diff] [blame] | 2699 | if (AppOpsManager.opAllowsReset(curOp.op) |
| 2700 | && curOp.mode != AppOpsManager.opToDefaultMode(curOp.op)) { |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 2701 | curOp.mode = AppOpsManager.opToDefaultMode(curOp.op); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2702 | changed = true; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 2703 | uidChanged = true; |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 2704 | final int uid = curOp.uidState.uid; |
| 2705 | callbacks = addCallbacks(callbacks, curOp.op, uid, packageName, |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2706 | mOpModeWatchers.get(curOp.op)); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 2707 | callbacks = addCallbacks(callbacks, curOp.op, uid, packageName, |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2708 | mPackageModeWatchers.get(packageName)); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2709 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 2710 | curOp.removeAttributionsWithNoTime(); |
| 2711 | if (curOp.mAttributions.isEmpty()) { |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 2712 | pkgOps.removeAt(j); |
| 2713 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2714 | } |
| 2715 | } |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 2716 | if (pkgOps.size() == 0) { |
| 2717 | it.remove(); |
| 2718 | } |
| 2719 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2720 | if (uidState.isDefault()) { |
| 2721 | mUidStates.remove(uidState.uid); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2722 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 2723 | if (uidChanged) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 2724 | uidState.evalForegroundOps(mOpModeWatchers); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 2725 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2726 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2727 | |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2728 | if (changed) { |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 2729 | scheduleFastWriteLocked(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2730 | } |
| 2731 | } |
| 2732 | if (callbacks != null) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2733 | for (Map.Entry<ModeCallback, ArrayList<ChangeRec>> ent : callbacks.entrySet()) { |
| 2734 | ModeCallback cb = ent.getKey(); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 2735 | ArrayList<ChangeRec> reports = ent.getValue(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2736 | for (int i=0; i<reports.size(); i++) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 2737 | ChangeRec rep = reports.get(i); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2738 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 2739 | AppOpsService::notifyOpChanged, |
| 2740 | this, cb, rep.op, rep.uid, rep.pkg)); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 2741 | } |
| 2742 | } |
| 2743 | } |
| 2744 | } |
| 2745 | |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 2746 | private void evalAllForegroundOpsLocked() { |
| 2747 | for (int uidi = mUidStates.size() - 1; uidi >= 0; uidi--) { |
| 2748 | final UidState uidState = mUidStates.valueAt(uidi); |
| 2749 | if (uidState.foregroundOps != null) { |
| 2750 | uidState.evalForegroundOps(mOpModeWatchers); |
| 2751 | } |
| 2752 | } |
| 2753 | } |
| 2754 | |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2755 | @Override |
| 2756 | public void startWatchingMode(int op, String packageName, IAppOpsCallback callback) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 2757 | startWatchingModeWithFlags(op, packageName, 0, callback); |
| 2758 | } |
| 2759 | |
| 2760 | @Override |
| 2761 | public void startWatchingModeWithFlags(int op, String packageName, int flags, |
| 2762 | IAppOpsCallback callback) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2763 | int watchedUid = -1; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 2764 | final int callingUid = Binder.getCallingUid(); |
| 2765 | final int callingPid = Binder.getCallingPid(); |
Dianne Hackborn | 5376edd | 2018-06-05 13:21:16 -0700 | [diff] [blame] | 2766 | // TODO: should have a privileged permission to protect this. |
| 2767 | // Also, if the caller has requested WATCH_FOREGROUND_CHANGES, should we require |
| 2768 | // the USAGE_STATS permission since this can provide information about when an |
| 2769 | // app is in the foreground? |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2770 | Preconditions.checkArgumentInRange(op, AppOpsManager.OP_NONE, |
| 2771 | AppOpsManager._NUM_OP - 1, "Invalid op code: " + op); |
Svetoslav Ganov | 8de5971 | 2015-12-09 18:25:13 -0800 | [diff] [blame] | 2772 | if (callback == null) { |
| 2773 | return; |
| 2774 | } |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2775 | synchronized (this) { |
Philip P. Moltmann | b2bc651 | 2020-01-24 15:34:34 -0800 | [diff] [blame] | 2776 | int switchOp = (op != AppOpsManager.OP_NONE) ? AppOpsManager.opToSwitch(op) : op; |
| 2777 | |
Philip P. Moltmann | b2bc651 | 2020-01-24 15:34:34 -0800 | [diff] [blame] | 2778 | int notifiedOps; |
Philip P. Moltmann | 7641cd0 | 2020-03-11 14:42:43 -0700 | [diff] [blame] | 2779 | if ((flags & CALL_BACK_ON_SWITCHED_OP) == 0) { |
Philip P. Moltmann | b2bc651 | 2020-01-24 15:34:34 -0800 | [diff] [blame] | 2780 | if (op == OP_NONE) { |
| 2781 | notifiedOps = ALL_OPS; |
| 2782 | } else { |
| 2783 | notifiedOps = op; |
| 2784 | } |
| 2785 | } else { |
| 2786 | notifiedOps = switchOp; |
| 2787 | } |
| 2788 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2789 | ModeCallback cb = mModeWatchers.get(callback.asBinder()); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2790 | if (cb == null) { |
Philip P. Moltmann | b2bc651 | 2020-01-24 15:34:34 -0800 | [diff] [blame] | 2791 | cb = new ModeCallback(callback, watchedUid, flags, notifiedOps, callingUid, |
| 2792 | callingPid); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2793 | mModeWatchers.put(callback.asBinder(), cb); |
| 2794 | } |
Philip P. Moltmann | b2bc651 | 2020-01-24 15:34:34 -0800 | [diff] [blame] | 2795 | if (switchOp != AppOpsManager.OP_NONE) { |
| 2796 | ArraySet<ModeCallback> cbs = mOpModeWatchers.get(switchOp); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2797 | if (cbs == null) { |
Dianne Hackborn | 68d7655 | 2017-02-27 15:32:03 -0800 | [diff] [blame] | 2798 | cbs = new ArraySet<>(); |
Philip P. Moltmann | b2bc651 | 2020-01-24 15:34:34 -0800 | [diff] [blame] | 2799 | mOpModeWatchers.put(switchOp, cbs); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2800 | } |
| 2801 | cbs.add(cb); |
| 2802 | } |
| 2803 | if (packageName != null) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2804 | ArraySet<ModeCallback> cbs = mPackageModeWatchers.get(packageName); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2805 | if (cbs == null) { |
Dianne Hackborn | 68d7655 | 2017-02-27 15:32:03 -0800 | [diff] [blame] | 2806 | cbs = new ArraySet<>(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2807 | mPackageModeWatchers.put(packageName, cbs); |
| 2808 | } |
| 2809 | cbs.add(cb); |
| 2810 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 2811 | evalAllForegroundOpsLocked(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2812 | } |
| 2813 | } |
| 2814 | |
| 2815 | @Override |
| 2816 | public void stopWatchingMode(IAppOpsCallback callback) { |
Svetoslav Ganov | 8de5971 | 2015-12-09 18:25:13 -0800 | [diff] [blame] | 2817 | if (callback == null) { |
| 2818 | return; |
| 2819 | } |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2820 | synchronized (this) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2821 | ModeCallback cb = mModeWatchers.remove(callback.asBinder()); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2822 | if (cb != null) { |
| 2823 | cb.unlinkToDeath(); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2824 | for (int i=mOpModeWatchers.size()-1; i>=0; i--) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2825 | ArraySet<ModeCallback> cbs = mOpModeWatchers.valueAt(i); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2826 | cbs.remove(cb); |
| 2827 | if (cbs.size() <= 0) { |
| 2828 | mOpModeWatchers.removeAt(i); |
| 2829 | } |
| 2830 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2831 | for (int i=mPackageModeWatchers.size()-1; i>=0; i--) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2832 | ArraySet<ModeCallback> cbs = mPackageModeWatchers.valueAt(i); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2833 | cbs.remove(cb); |
| 2834 | if (cbs.size() <= 0) { |
| 2835 | mPackageModeWatchers.removeAt(i); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2836 | } |
| 2837 | } |
| 2838 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 2839 | evalAllForegroundOpsLocked(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 2840 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 2841 | } |
| 2842 | |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 2843 | public CheckOpsDelegate getAppOpsServiceDelegate() { |
| 2844 | synchronized (this) { |
| 2845 | return mCheckOpsDelegate; |
| 2846 | } |
| 2847 | } |
| 2848 | |
| 2849 | public void setAppOpsServiceDelegate(CheckOpsDelegate delegate) { |
| 2850 | synchronized (this) { |
| 2851 | mCheckOpsDelegate = delegate; |
| 2852 | } |
| 2853 | } |
| 2854 | |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2855 | @Override |
Svet Ganov | 9d528a1 | 2018-12-19 17:23:11 -0800 | [diff] [blame] | 2856 | public int checkOperationRaw(int code, int uid, String packageName) { |
| 2857 | return checkOperationInternal(code, uid, packageName, true /*raw*/); |
| 2858 | } |
| 2859 | |
| 2860 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2861 | public int checkOperation(int code, int uid, String packageName) { |
Svet Ganov | 9d528a1 | 2018-12-19 17:23:11 -0800 | [diff] [blame] | 2862 | return checkOperationInternal(code, uid, packageName, false /*raw*/); |
| 2863 | } |
| 2864 | |
| 2865 | private int checkOperationInternal(int code, int uid, String packageName, boolean raw) { |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 2866 | final CheckOpsDelegate delegate; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2867 | synchronized (this) { |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 2868 | delegate = mCheckOpsDelegate; |
| 2869 | } |
Todd Kennedy | 556efba | 2018-11-15 07:43:55 -0800 | [diff] [blame] | 2870 | if (delegate == null) { |
Svet Ganov | 9d528a1 | 2018-12-19 17:23:11 -0800 | [diff] [blame] | 2871 | return checkOperationImpl(code, uid, packageName, raw); |
Todd Kennedy | 556efba | 2018-11-15 07:43:55 -0800 | [diff] [blame] | 2872 | } |
Svet Ganov | 9d528a1 | 2018-12-19 17:23:11 -0800 | [diff] [blame] | 2873 | return delegate.checkOperation(code, uid, packageName, raw, |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 2874 | AppOpsService.this::checkOperationImpl); |
| 2875 | } |
| 2876 | |
Svet Ganov | 9d528a1 | 2018-12-19 17:23:11 -0800 | [diff] [blame] | 2877 | private int checkOperationImpl(int code, int uid, String packageName, |
| 2878 | boolean raw) { |
Todd Kennedy | 556efba | 2018-11-15 07:43:55 -0800 | [diff] [blame] | 2879 | verifyIncomingOp(code); |
| 2880 | String resolvedPackageName = resolvePackageName(uid, packageName); |
| 2881 | if (resolvedPackageName == null) { |
| 2882 | return AppOpsManager.MODE_IGNORED; |
| 2883 | } |
Svet Ganov | 9d528a1 | 2018-12-19 17:23:11 -0800 | [diff] [blame] | 2884 | return checkOperationUnchecked(code, uid, resolvedPackageName, raw); |
Todd Kennedy | 556efba | 2018-11-15 07:43:55 -0800 | [diff] [blame] | 2885 | } |
| 2886 | |
Philip P. Moltmann | 724150d | 2019-03-11 17:01:05 -0700 | [diff] [blame] | 2887 | /** |
Philip P. Moltmann | 724150d | 2019-03-11 17:01:05 -0700 | [diff] [blame] | 2888 | * Get the mode of an app-op. |
| 2889 | * |
| 2890 | * @param code The code of the op |
| 2891 | * @param uid The uid of the package the op belongs to |
| 2892 | * @param packageName The package the op belongs to |
| 2893 | * @param raw If the raw state of eval-ed state should be checked. |
Philip P. Moltmann | 724150d | 2019-03-11 17:01:05 -0700 | [diff] [blame] | 2894 | * |
| 2895 | * @return The mode of the op |
| 2896 | */ |
| 2897 | private @Mode int checkOperationUnchecked(int code, int uid, @NonNull String packageName, |
Philip P. Moltmann | d4ab19e | 2019-05-07 08:57:35 -0700 | [diff] [blame] | 2898 | boolean raw) { |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 2899 | RestrictionBypass bypass; |
Philip P. Moltmann | c5f504a | 2019-06-12 16:31:29 -0700 | [diff] [blame] | 2900 | try { |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 2901 | bypass = verifyAndGetBypass(uid, packageName, null); |
Philip P. Moltmann | c5f504a | 2019-06-12 16:31:29 -0700 | [diff] [blame] | 2902 | } catch (SecurityException e) { |
| 2903 | Slog.e(TAG, "checkOperation", e); |
| 2904 | return AppOpsManager.opToDefaultMode(code); |
| 2905 | } |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 2906 | |
Suprabh Shukla | 7e01792 | 2019-08-05 17:13:23 -0700 | [diff] [blame] | 2907 | if (isOpRestrictedDueToSuspend(code, packageName, uid)) { |
| 2908 | return AppOpsManager.MODE_IGNORED; |
| 2909 | } |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 2910 | synchronized (this) { |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 2911 | if (isOpRestrictedLocked(uid, code, packageName, bypass)) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 2912 | return AppOpsManager.MODE_IGNORED; |
| 2913 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2914 | code = AppOpsManager.opToSwitch(code); |
| 2915 | UidState uidState = getUidStateLocked(uid, false); |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 2916 | if (uidState != null && uidState.opModes != null |
| 2917 | && uidState.opModes.indexOfKey(code) >= 0) { |
| 2918 | final int rawMode = uidState.opModes.get(code); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 2919 | return raw ? rawMode : uidState.evalMode(code, rawMode); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2920 | } |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 2921 | Op op = getOpLocked(code, uid, packageName, null, bypass, false); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2922 | if (op == null) { |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 2923 | return AppOpsManager.opToDefaultMode(code); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2924 | } |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 2925 | return raw ? op.mode : op.evalMode(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2926 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2927 | } |
| 2928 | |
| 2929 | @Override |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 2930 | public int checkAudioOperation(int code, int usage, int uid, String packageName) { |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 2931 | final CheckOpsDelegate delegate; |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 2932 | synchronized (this) { |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 2933 | delegate = mCheckOpsDelegate; |
| 2934 | } |
Todd Kennedy | 556efba | 2018-11-15 07:43:55 -0800 | [diff] [blame] | 2935 | if (delegate == null) { |
| 2936 | return checkAudioOperationImpl(code, usage, uid, packageName); |
| 2937 | } |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 2938 | return delegate.checkAudioOperation(code, usage, uid, packageName, |
| 2939 | AppOpsService.this::checkAudioOperationImpl); |
| 2940 | } |
| 2941 | |
| 2942 | private int checkAudioOperationImpl(int code, int usage, int uid, String packageName) { |
Yin-Chia Yeh | 51d8516 | 2019-08-06 15:31:39 -0700 | [diff] [blame] | 2943 | final int mode = mAudioRestrictionManager.checkAudioOperation( |
| 2944 | code, usage, uid, packageName); |
| 2945 | if (mode != AppOpsManager.MODE_ALLOWED) { |
| 2946 | return mode; |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 2947 | } |
| 2948 | return checkOperation(code, uid, packageName); |
| 2949 | } |
| 2950 | |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 2951 | @Override |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 2952 | public void setAudioRestriction(int code, int usage, int uid, int mode, |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 2953 | String[] exceptionPackages) { |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 2954 | enforceManageAppOpsModes(Binder.getCallingPid(), Binder.getCallingUid(), uid); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 2955 | verifyIncomingUid(uid); |
| 2956 | verifyIncomingOp(code); |
Yin-Chia Yeh | 51d8516 | 2019-08-06 15:31:39 -0700 | [diff] [blame] | 2957 | |
| 2958 | mAudioRestrictionManager.setZenModeAudioRestriction( |
| 2959 | code, usage, uid, mode, exceptionPackages); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2960 | |
| 2961 | mHandler.sendMessage(PooledLambda.obtainMessage( |
Svet Ganov | 3a95f83 | 2018-03-23 17:44:30 -0700 | [diff] [blame] | 2962 | AppOpsService::notifyWatchersOfChange, this, code, UID_ANY)); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 2963 | } |
| 2964 | |
Yin-Chia Yeh | 51d8516 | 2019-08-06 15:31:39 -0700 | [diff] [blame] | 2965 | |
| 2966 | @Override |
| 2967 | public void setCameraAudioRestriction(@CAMERA_AUDIO_RESTRICTION int mode) { |
| 2968 | enforceManageAppOpsModes(Binder.getCallingPid(), Binder.getCallingUid(), -1); |
| 2969 | |
| 2970 | mAudioRestrictionManager.setCameraAudioRestriction(mode); |
| 2971 | |
| 2972 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 2973 | AppOpsService::notifyWatchersOfChange, this, |
| 2974 | AppOpsManager.OP_PLAY_AUDIO, UID_ANY)); |
| 2975 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 2976 | AppOpsService::notifyWatchersOfChange, this, |
| 2977 | AppOpsManager.OP_VIBRATE, UID_ANY)); |
| 2978 | } |
| 2979 | |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 2980 | @Override |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 2981 | public int checkPackage(int uid, String packageName) { |
Daulet Zhanguzin | 9bd1370 | 2020-01-03 09:44:10 +0000 | [diff] [blame] | 2982 | Objects.requireNonNull(packageName); |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 2983 | try { |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 2984 | verifyAndGetBypass(uid, packageName, null); |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 2985 | |
| 2986 | return AppOpsManager.MODE_ALLOWED; |
| 2987 | } catch (SecurityException ignored) { |
| 2988 | return AppOpsManager.MODE_ERRORED; |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 2989 | } |
| 2990 | } |
| 2991 | |
| 2992 | @Override |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 2993 | public int noteProxyOperation(int code, int proxiedUid, String proxiedPackageName, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 2994 | String proxiedAttributionTag, int proxyUid, String proxyPackageName, |
| 2995 | String proxyAttributionTag, boolean shouldCollectAsyncNotedOp, String message) { |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 2996 | verifyIncomingUid(proxyUid); |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 2997 | verifyIncomingOp(code); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 2998 | |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 2999 | String resolveProxyPackageName = resolvePackageName(proxyUid, proxyPackageName); |
| 3000 | if (resolveProxyPackageName == null) { |
| 3001 | return AppOpsManager.MODE_IGNORED; |
| 3002 | } |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 3003 | |
| 3004 | final boolean isProxyTrusted = mContext.checkPermission( |
| 3005 | Manifest.permission.UPDATE_APP_OPS_STATS, -1, proxyUid) |
| 3006 | == PackageManager.PERMISSION_GRANTED; |
| 3007 | |
| 3008 | final int proxyFlags = isProxyTrusted ? AppOpsManager.OP_FLAG_TRUSTED_PROXY |
| 3009 | : AppOpsManager.OP_FLAG_UNTRUSTED_PROXY; |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 3010 | final int proxyMode = noteOperationUnchecked(code, proxyUid, resolveProxyPackageName, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3011 | proxyAttributionTag, Process.INVALID_UID, null, null, proxyFlags, |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 3012 | !isProxyTrusted, "proxy " + message); |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 3013 | if (proxyMode != AppOpsManager.MODE_ALLOWED || Binder.getCallingUid() == proxiedUid) { |
| 3014 | return proxyMode; |
| 3015 | } |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 3016 | |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 3017 | String resolveProxiedPackageName = resolvePackageName(proxiedUid, proxiedPackageName); |
| 3018 | if (resolveProxiedPackageName == null) { |
| 3019 | return AppOpsManager.MODE_IGNORED; |
| 3020 | } |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 3021 | final int proxiedFlags = isProxyTrusted ? AppOpsManager.OP_FLAG_TRUSTED_PROXIED |
| 3022 | : AppOpsManager.OP_FLAG_UNTRUSTED_PROXIED; |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 3023 | return noteOperationUnchecked(code, proxiedUid, resolveProxiedPackageName, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3024 | proxiedAttributionTag, proxyUid, resolveProxyPackageName, proxyAttributionTag, |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 3025 | proxiedFlags, shouldCollectAsyncNotedOp, message); |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 3026 | } |
| 3027 | |
| 3028 | @Override |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3029 | public int noteOperation(int code, int uid, String packageName, String attributionTag, |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 3030 | boolean shouldCollectAsyncNotedOp, String message) { |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 3031 | final CheckOpsDelegate delegate; |
| 3032 | synchronized (this) { |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 3033 | delegate = mCheckOpsDelegate; |
| 3034 | } |
Todd Kennedy | 556efba | 2018-11-15 07:43:55 -0800 | [diff] [blame] | 3035 | if (delegate == null) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3036 | return noteOperationImpl(code, uid, packageName, attributionTag, |
| 3037 | shouldCollectAsyncNotedOp, message); |
Todd Kennedy | 556efba | 2018-11-15 07:43:55 -0800 | [diff] [blame] | 3038 | } |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3039 | return delegate.noteOperation(code, uid, packageName, attributionTag, |
| 3040 | shouldCollectAsyncNotedOp, message, AppOpsService.this::noteOperationImpl); |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 3041 | } |
| 3042 | |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 3043 | private int noteOperationImpl(int code, int uid, @Nullable String packageName, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3044 | @Nullable String attributionTag, boolean shouldCollectAsyncNotedOp, |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 3045 | @Nullable String message) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 3046 | verifyIncomingUid(uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 3047 | verifyIncomingOp(code); |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 3048 | String resolvedPackageName = resolvePackageName(uid, packageName); |
| 3049 | if (resolvedPackageName == null) { |
| 3050 | return AppOpsManager.MODE_IGNORED; |
| 3051 | } |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3052 | return noteOperationUnchecked(code, uid, resolvedPackageName, attributionTag, |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 3053 | Process.INVALID_UID, null, null, AppOpsManager.OP_FLAG_SELF, |
| 3054 | shouldCollectAsyncNotedOp, message); |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 3055 | } |
| 3056 | |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 3057 | private int noteOperationUnchecked(int code, int uid, @NonNull String packageName, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3058 | @Nullable String attributionTag, int proxyUid, String proxyPackageName, |
| 3059 | @Nullable String proxyAttributionTag, @OpFlags int flags, |
| 3060 | boolean shouldCollectAsyncNotedOp, @Nullable String message) { |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3061 | RestrictionBypass bypass; |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3062 | try { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3063 | bypass = verifyAndGetBypass(uid, packageName, attributionTag); |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3064 | } catch (SecurityException e) { |
Philip P. Moltmann | c5f504a | 2019-06-12 16:31:29 -0700 | [diff] [blame] | 3065 | Slog.e(TAG, "noteOperation", e); |
| 3066 | return AppOpsManager.MODE_ERRORED; |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3067 | } |
| 3068 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3069 | synchronized (this) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3070 | final Ops ops = getOpsLocked(uid, packageName, attributionTag, bypass, |
| 3071 | true /* edit */); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 3072 | if (ops == null) { |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 3073 | scheduleOpNotedIfNeededLocked(code, uid, packageName, |
| 3074 | AppOpsManager.MODE_IGNORED); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3075 | if (DEBUG) Slog.d(TAG, "noteOperation: no op for code " + code + " uid " + uid |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 3076 | + " package " + packageName); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 3077 | return AppOpsManager.MODE_ERRORED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3078 | } |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 3079 | final Op op = getOpLocked(ops, code, uid, true); |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3080 | if (isOpRestrictedLocked(uid, code, packageName, bypass)) { |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 3081 | scheduleOpNotedIfNeededLocked(code, uid, packageName, |
| 3082 | AppOpsManager.MODE_IGNORED); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 3083 | return AppOpsManager.MODE_IGNORED; |
| 3084 | } |
Adam Bookatz | 182862e | 2020-04-27 21:58:22 -0700 | [diff] [blame] | 3085 | final AttributedOp attributedOp = op.getOrCreateAttribution(op, attributionTag); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3086 | if (attributedOp.isRunning()) { |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 3087 | Slog.w(TAG, "Noting op not finished: uid " + uid + " pkg " + packageName + " code " |
| 3088 | + code + " startTime of in progress event=" |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3089 | + attributedOp.mInProgressEvents.valueAt(0).getStartTime()); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3090 | } |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 3091 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 3092 | final int switchCode = AppOpsManager.opToSwitch(code); |
Adam Bookatz | 182862e | 2020-04-27 21:58:22 -0700 | [diff] [blame] | 3093 | final UidState uidState = ops.uidState; |
Svetoslav Ganov | 1984bba | 2016-04-05 13:39:25 -0700 | [diff] [blame] | 3094 | // If there is a non-default per UID policy (we set UID op mode only if |
| 3095 | // non-default) it takes over, otherwise use the per package policy. |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 3096 | if (uidState.opModes != null && uidState.opModes.indexOfKey(switchCode) >= 0) { |
| 3097 | final int uidMode = uidState.evalMode(code, uidState.opModes.get(switchCode)); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3098 | if (uidMode != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3099 | if (DEBUG) Slog.d(TAG, "noteOperation: uid reject #" + uidMode + " for code " |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3100 | + switchCode + " (" + code + ") uid " + uid + " package " |
| 3101 | + packageName); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3102 | attributedOp.rejected(uidState.state, flags); |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 3103 | scheduleOpNotedIfNeededLocked(code, uid, packageName, uidMode); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3104 | return uidMode; |
| 3105 | } |
Svetoslav Ganov | 1984bba | 2016-04-05 13:39:25 -0700 | [diff] [blame] | 3106 | } else { |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 3107 | final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, uid, true) |
| 3108 | : op; |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 3109 | final int mode = switchOp.evalMode(); |
Philip P. Moltmann | 721c968 | 2019-12-20 11:41:46 -0800 | [diff] [blame] | 3110 | if (mode != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3111 | if (DEBUG) Slog.d(TAG, "noteOperation: reject #" + mode + " for code " |
Svetoslav Ganov | 1984bba | 2016-04-05 13:39:25 -0700 | [diff] [blame] | 3112 | + switchCode + " (" + code + ") uid " + uid + " package " |
| 3113 | + packageName); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3114 | attributedOp.rejected(uidState.state, flags); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 3115 | scheduleOpNotedIfNeededLocked(code, uid, packageName, mode); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3116 | return mode; |
Svetoslav Ganov | 1984bba | 2016-04-05 13:39:25 -0700 | [diff] [blame] | 3117 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 3118 | } |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3119 | if (DEBUG) { |
| 3120 | Slog.d(TAG, |
| 3121 | "noteOperation: allowing code " + code + " uid " + uid + " package " |
| 3122 | + packageName + (attributionTag == null ? "" |
| 3123 | : "." + attributionTag)); |
| 3124 | } |
Adam Bookatz | 182862e | 2020-04-27 21:58:22 -0700 | [diff] [blame] | 3125 | scheduleOpNotedIfNeededLocked(code, uid, packageName, AppOpsManager.MODE_ALLOWED); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3126 | attributedOp.accessed(proxyUid, proxyPackageName, proxyAttributionTag, uidState.state, |
| 3127 | flags); |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 3128 | |
| 3129 | if (shouldCollectAsyncNotedOp) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3130 | collectAsyncNotedOp(uid, packageName, code, attributionTag, message); |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 3131 | } |
| 3132 | |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 3133 | return AppOpsManager.MODE_ALLOWED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3134 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3135 | } |
| 3136 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3137 | // TODO moltmann: Allow watching for attribution ops |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3138 | @Override |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3139 | public void startWatchingActive(int[] ops, IAppOpsActiveCallback callback) { |
Adam Bookatz | 182862e | 2020-04-27 21:58:22 -0700 | [diff] [blame] | 3140 | int watchedUid = Process.INVALID_UID; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 3141 | final int callingUid = Binder.getCallingUid(); |
| 3142 | final int callingPid = Binder.getCallingPid(); |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 3143 | if (mContext.checkCallingOrSelfPermission(Manifest.permission.WATCH_APPOPS) |
| 3144 | != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 3145 | watchedUid = callingUid; |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 3146 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3147 | if (ops != null) { |
| 3148 | Preconditions.checkArrayElementsInRange(ops, 0, |
| 3149 | AppOpsManager._NUM_OP - 1, "Invalid op code in: " + Arrays.toString(ops)); |
| 3150 | } |
| 3151 | if (callback == null) { |
| 3152 | return; |
| 3153 | } |
| 3154 | synchronized (this) { |
| 3155 | SparseArray<ActiveCallback> callbacks = mActiveWatchers.get(callback.asBinder()); |
| 3156 | if (callbacks == null) { |
| 3157 | callbacks = new SparseArray<>(); |
| 3158 | mActiveWatchers.put(callback.asBinder(), callbacks); |
| 3159 | } |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 3160 | final ActiveCallback activeCallback = new ActiveCallback(callback, watchedUid, |
| 3161 | callingUid, callingPid); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3162 | for (int op : ops) { |
| 3163 | callbacks.put(op, activeCallback); |
| 3164 | } |
| 3165 | } |
| 3166 | } |
| 3167 | |
| 3168 | @Override |
| 3169 | public void stopWatchingActive(IAppOpsActiveCallback callback) { |
| 3170 | if (callback == null) { |
| 3171 | return; |
| 3172 | } |
| 3173 | synchronized (this) { |
| 3174 | final SparseArray<ActiveCallback> activeCallbacks = |
| 3175 | mActiveWatchers.remove(callback.asBinder()); |
| 3176 | if (activeCallbacks == null) { |
| 3177 | return; |
| 3178 | } |
| 3179 | final int callbackCount = activeCallbacks.size(); |
| 3180 | for (int i = 0; i < callbackCount; i++) { |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 3181 | activeCallbacks.valueAt(i).destroy(); |
| 3182 | } |
| 3183 | } |
| 3184 | } |
| 3185 | |
| 3186 | @Override |
Adam Bookatz | 182862e | 2020-04-27 21:58:22 -0700 | [diff] [blame] | 3187 | public void startWatchingStarted(int[] ops, @NonNull IAppOpsStartedCallback callback) { |
| 3188 | int watchedUid = Process.INVALID_UID; |
| 3189 | final int callingUid = Binder.getCallingUid(); |
| 3190 | final int callingPid = Binder.getCallingPid(); |
| 3191 | if (mContext.checkCallingOrSelfPermission(Manifest.permission.WATCH_APPOPS) |
| 3192 | != PackageManager.PERMISSION_GRANTED) { |
| 3193 | watchedUid = callingUid; |
| 3194 | } |
| 3195 | |
| 3196 | Preconditions.checkArgument(!ArrayUtils.isEmpty(ops), "Ops cannot be null or empty"); |
| 3197 | Preconditions.checkArrayElementsInRange(ops, 0, AppOpsManager._NUM_OP - 1, |
| 3198 | "Invalid op code in: " + Arrays.toString(ops)); |
| 3199 | Objects.requireNonNull(callback, "Callback cannot be null"); |
| 3200 | |
| 3201 | synchronized (this) { |
| 3202 | SparseArray<StartedCallback> callbacks = mStartedWatchers.get(callback.asBinder()); |
| 3203 | if (callbacks == null) { |
| 3204 | callbacks = new SparseArray<>(); |
| 3205 | mStartedWatchers.put(callback.asBinder(), callbacks); |
| 3206 | } |
| 3207 | |
| 3208 | final StartedCallback startedCallback = new StartedCallback(callback, watchedUid, |
| 3209 | callingUid, callingPid); |
| 3210 | for (int op : ops) { |
| 3211 | callbacks.put(op, startedCallback); |
| 3212 | } |
| 3213 | } |
| 3214 | } |
| 3215 | |
| 3216 | @Override |
| 3217 | public void stopWatchingStarted(IAppOpsStartedCallback callback) { |
| 3218 | Objects.requireNonNull(callback, "Callback cannot be null"); |
| 3219 | |
| 3220 | synchronized (this) { |
| 3221 | final SparseArray<StartedCallback> startedCallbacks = |
| 3222 | mStartedWatchers.remove(callback.asBinder()); |
| 3223 | if (startedCallbacks == null) { |
| 3224 | return; |
| 3225 | } |
| 3226 | |
| 3227 | final int callbackCount = startedCallbacks.size(); |
| 3228 | for (int i = 0; i < callbackCount; i++) { |
| 3229 | startedCallbacks.valueAt(i).destroy(); |
| 3230 | } |
| 3231 | } |
| 3232 | } |
| 3233 | |
| 3234 | @Override |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 3235 | public void startWatchingNoted(@NonNull int[] ops, @NonNull IAppOpsNotedCallback callback) { |
| 3236 | int watchedUid = Process.INVALID_UID; |
| 3237 | final int callingUid = Binder.getCallingUid(); |
| 3238 | final int callingPid = Binder.getCallingPid(); |
| 3239 | if (mContext.checkCallingOrSelfPermission(Manifest.permission.WATCH_APPOPS) |
| 3240 | != PackageManager.PERMISSION_GRANTED) { |
| 3241 | watchedUid = callingUid; |
| 3242 | } |
| 3243 | Preconditions.checkArgument(!ArrayUtils.isEmpty(ops), "Ops cannot be null or empty"); |
| 3244 | Preconditions.checkArrayElementsInRange(ops, 0, AppOpsManager._NUM_OP - 1, |
| 3245 | "Invalid op code in: " + Arrays.toString(ops)); |
Daulet Zhanguzin | 9bd1370 | 2020-01-03 09:44:10 +0000 | [diff] [blame] | 3246 | Objects.requireNonNull(callback, "Callback cannot be null"); |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 3247 | synchronized (this) { |
| 3248 | SparseArray<NotedCallback> callbacks = mNotedWatchers.get(callback.asBinder()); |
| 3249 | if (callbacks == null) { |
| 3250 | callbacks = new SparseArray<>(); |
| 3251 | mNotedWatchers.put(callback.asBinder(), callbacks); |
| 3252 | } |
| 3253 | final NotedCallback notedCallback = new NotedCallback(callback, watchedUid, |
| 3254 | callingUid, callingPid); |
| 3255 | for (int op : ops) { |
| 3256 | callbacks.put(op, notedCallback); |
| 3257 | } |
| 3258 | } |
| 3259 | } |
| 3260 | |
| 3261 | @Override |
| 3262 | public void stopWatchingNoted(IAppOpsNotedCallback callback) { |
Daulet Zhanguzin | 9bd1370 | 2020-01-03 09:44:10 +0000 | [diff] [blame] | 3263 | Objects.requireNonNull(callback, "Callback cannot be null"); |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 3264 | synchronized (this) { |
| 3265 | final SparseArray<NotedCallback> notedCallbacks = |
| 3266 | mNotedWatchers.remove(callback.asBinder()); |
| 3267 | if (notedCallbacks == null) { |
| 3268 | return; |
| 3269 | } |
| 3270 | final int callbackCount = notedCallbacks.size(); |
| 3271 | for (int i = 0; i < callbackCount; i++) { |
| 3272 | notedCallbacks.valueAt(i).destroy(); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3273 | } |
| 3274 | } |
| 3275 | } |
| 3276 | |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 3277 | /** |
| 3278 | * Collect an {@link AsyncNotedAppOp}. |
| 3279 | * |
| 3280 | * @param uid The uid the op was noted for |
| 3281 | * @param packageName The package the op was noted for |
| 3282 | * @param opCode The code of the op noted |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3283 | * @param attributionTag attribution tag the op was noted for |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 3284 | * @param message The message for the op noting |
| 3285 | */ |
| 3286 | private void collectAsyncNotedOp(int uid, @NonNull String packageName, int opCode, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3287 | @Nullable String attributionTag, @NonNull String message) { |
Daulet Zhanguzin | 9bd1370 | 2020-01-03 09:44:10 +0000 | [diff] [blame] | 3288 | Objects.requireNonNull(message); |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 3289 | |
| 3290 | int callingUid = Binder.getCallingUid(); |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 3291 | |
| 3292 | long token = Binder.clearCallingIdentity(); |
| 3293 | try { |
| 3294 | synchronized (this) { |
| 3295 | Pair<String, Integer> key = getAsyncNotedOpsKey(packageName, uid); |
| 3296 | |
| 3297 | RemoteCallbackList<IAppOpsAsyncNotedCallback> callbacks = mAsyncOpWatchers.get(key); |
| 3298 | AsyncNotedAppOp asyncNotedOp = new AsyncNotedAppOp(opCode, callingUid, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3299 | attributionTag, message, System.currentTimeMillis()); |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 3300 | final boolean[] wasNoteForwarded = {false}; |
| 3301 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3302 | reportRuntimeAppOpAccessMessageAsyncLocked(uid, packageName, opCode, attributionTag, |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 3303 | message); |
| 3304 | |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 3305 | if (callbacks != null) { |
| 3306 | callbacks.broadcast((cb) -> { |
| 3307 | try { |
| 3308 | cb.opNoted(asyncNotedOp); |
| 3309 | wasNoteForwarded[0] = true; |
| 3310 | } catch (RemoteException e) { |
| 3311 | Slog.e(TAG, |
| 3312 | "Could not forward noteOp of " + opCode + " to " + packageName |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3313 | + "/" + uid + "(" + attributionTag + ")", e); |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 3314 | } |
| 3315 | }); |
| 3316 | } |
| 3317 | |
| 3318 | if (!wasNoteForwarded[0]) { |
| 3319 | ArrayList<AsyncNotedAppOp> unforwardedOps = mUnforwardedAsyncNotedOps.get(key); |
| 3320 | if (unforwardedOps == null) { |
| 3321 | unforwardedOps = new ArrayList<>(1); |
| 3322 | mUnforwardedAsyncNotedOps.put(key, unforwardedOps); |
| 3323 | } |
| 3324 | |
| 3325 | unforwardedOps.add(asyncNotedOp); |
| 3326 | if (unforwardedOps.size() > MAX_UNFORWARED_OPS) { |
| 3327 | unforwardedOps.remove(0); |
| 3328 | } |
| 3329 | } |
| 3330 | } |
| 3331 | } finally { |
| 3332 | Binder.restoreCallingIdentity(token); |
| 3333 | } |
| 3334 | } |
| 3335 | |
| 3336 | /** |
| 3337 | * Compute a key to be used in {@link #mAsyncOpWatchers} and {@link #mUnforwardedAsyncNotedOps} |
| 3338 | * |
| 3339 | * @param packageName The package name of the app |
| 3340 | * @param uid The uid of the app |
| 3341 | * |
| 3342 | * @return They key uniquely identifying the app |
| 3343 | */ |
| 3344 | private @NonNull Pair<String, Integer> getAsyncNotedOpsKey(@NonNull String packageName, |
| 3345 | int uid) { |
| 3346 | return new Pair<>(packageName, uid); |
| 3347 | } |
| 3348 | |
| 3349 | @Override |
| 3350 | public void startWatchingAsyncNoted(String packageName, IAppOpsAsyncNotedCallback callback) { |
Daulet Zhanguzin | 9bd1370 | 2020-01-03 09:44:10 +0000 | [diff] [blame] | 3351 | Objects.requireNonNull(packageName); |
| 3352 | Objects.requireNonNull(callback); |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 3353 | |
| 3354 | int uid = Binder.getCallingUid(); |
| 3355 | Pair<String, Integer> key = getAsyncNotedOpsKey(packageName, uid); |
| 3356 | |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3357 | verifyAndGetBypass(uid, packageName, null); |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 3358 | |
| 3359 | synchronized (this) { |
| 3360 | RemoteCallbackList<IAppOpsAsyncNotedCallback> callbacks = mAsyncOpWatchers.get(key); |
| 3361 | if (callbacks == null) { |
| 3362 | callbacks = new RemoteCallbackList<IAppOpsAsyncNotedCallback>() { |
| 3363 | @Override |
| 3364 | public void onCallbackDied(IAppOpsAsyncNotedCallback callback) { |
| 3365 | synchronized (AppOpsService.this) { |
| 3366 | if (getRegisteredCallbackCount() == 0) { |
| 3367 | mAsyncOpWatchers.remove(key); |
| 3368 | } |
| 3369 | } |
| 3370 | } |
| 3371 | }; |
| 3372 | mAsyncOpWatchers.put(key, callbacks); |
| 3373 | } |
| 3374 | |
| 3375 | callbacks.register(callback); |
| 3376 | } |
| 3377 | } |
| 3378 | |
| 3379 | @Override |
| 3380 | public void stopWatchingAsyncNoted(String packageName, IAppOpsAsyncNotedCallback callback) { |
Daulet Zhanguzin | 9bd1370 | 2020-01-03 09:44:10 +0000 | [diff] [blame] | 3381 | Objects.requireNonNull(packageName); |
| 3382 | Objects.requireNonNull(callback); |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 3383 | |
| 3384 | int uid = Binder.getCallingUid(); |
| 3385 | Pair<String, Integer> key = getAsyncNotedOpsKey(packageName, uid); |
| 3386 | |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3387 | verifyAndGetBypass(uid, packageName, null); |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 3388 | |
| 3389 | synchronized (this) { |
| 3390 | RemoteCallbackList<IAppOpsAsyncNotedCallback> callbacks = mAsyncOpWatchers.get(key); |
| 3391 | if (callbacks != null) { |
| 3392 | callbacks.unregister(callback); |
| 3393 | if (callbacks.getRegisteredCallbackCount() == 0) { |
| 3394 | mAsyncOpWatchers.remove(key); |
| 3395 | } |
| 3396 | } |
| 3397 | } |
| 3398 | } |
| 3399 | |
| 3400 | @Override |
| 3401 | public List<AsyncNotedAppOp> extractAsyncOps(String packageName) { |
Daulet Zhanguzin | 9bd1370 | 2020-01-03 09:44:10 +0000 | [diff] [blame] | 3402 | Objects.requireNonNull(packageName); |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 3403 | |
| 3404 | int uid = Binder.getCallingUid(); |
| 3405 | |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3406 | verifyAndGetBypass(uid, packageName, null); |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 3407 | |
| 3408 | synchronized (this) { |
| 3409 | return mUnforwardedAsyncNotedOps.remove(getAsyncNotedOpsKey(packageName, uid)); |
| 3410 | } |
| 3411 | } |
| 3412 | |
| 3413 | @Override |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 3414 | public int startOperation(IBinder clientId, int code, int uid, String packageName, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3415 | String attributionTag, boolean startIfModeDefault, boolean shouldCollectAsyncNotedOp, |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 3416 | String message) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 3417 | verifyIncomingUid(uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 3418 | verifyIncomingOp(code); |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 3419 | String resolvedPackageName = resolvePackageName(uid, packageName); |
| 3420 | if (resolvedPackageName == null) { |
| 3421 | return AppOpsManager.MODE_IGNORED; |
| 3422 | } |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3423 | |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3424 | RestrictionBypass bypass; |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3425 | try { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3426 | bypass = verifyAndGetBypass(uid, packageName, attributionTag); |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3427 | } catch (SecurityException e) { |
Philip P. Moltmann | c5f504a | 2019-06-12 16:31:29 -0700 | [diff] [blame] | 3428 | Slog.e(TAG, "startOperation", e); |
| 3429 | return AppOpsManager.MODE_ERRORED; |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3430 | } |
| 3431 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3432 | synchronized (this) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3433 | final Ops ops = getOpsLocked(uid, resolvedPackageName, attributionTag, bypass, |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3434 | true /* edit */); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 3435 | if (ops == null) { |
Adam Bookatz | 182862e | 2020-04-27 21:58:22 -0700 | [diff] [blame] | 3436 | scheduleOpStartedIfNeededLocked(code, uid, packageName, AppOpsManager.MODE_IGNORED); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3437 | if (DEBUG) Slog.d(TAG, "startOperation: no op for code " + code + " uid " + uid |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 3438 | + " package " + resolvedPackageName); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 3439 | return AppOpsManager.MODE_ERRORED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3440 | } |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 3441 | final Op op = getOpLocked(ops, code, uid, true); |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3442 | if (isOpRestrictedLocked(uid, code, resolvedPackageName, bypass)) { |
Adam Bookatz | 182862e | 2020-04-27 21:58:22 -0700 | [diff] [blame] | 3443 | scheduleOpStartedIfNeededLocked(code, uid, packageName, AppOpsManager.MODE_IGNORED); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 3444 | return AppOpsManager.MODE_IGNORED; |
| 3445 | } |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3446 | final AttributedOp attributedOp = op.getOrCreateAttribution(op, attributionTag); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 3447 | final int switchCode = AppOpsManager.opToSwitch(code); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 3448 | final UidState uidState = ops.uidState; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3449 | // If there is a non-default per UID policy (we set UID op mode only if |
| 3450 | // non-default) it takes over, otherwise use the per package policy. |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 3451 | if (uidState.opModes != null && uidState.opModes.indexOfKey(switchCode) >= 0) { |
| 3452 | final int uidMode = uidState.evalMode(code, uidState.opModes.get(switchCode)); |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 3453 | if (uidMode != AppOpsManager.MODE_ALLOWED |
| 3454 | && (!startIfModeDefault || uidMode != AppOpsManager.MODE_DEFAULT)) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3455 | if (DEBUG) Slog.d(TAG, "noteOperation: uid reject #" + uidMode + " for code " |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3456 | + switchCode + " (" + code + ") uid " + uid + " package " |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 3457 | + resolvedPackageName); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3458 | attributedOp.rejected(uidState.state, AppOpsManager.OP_FLAG_SELF); |
Adam Bookatz | 182862e | 2020-04-27 21:58:22 -0700 | [diff] [blame] | 3459 | scheduleOpStartedIfNeededLocked(code, uid, packageName, uidMode); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3460 | return uidMode; |
| 3461 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3462 | } else { |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 3463 | final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, uid, true) |
| 3464 | : op; |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 3465 | final int mode = switchOp.evalMode(); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3466 | if (mode != AppOpsManager.MODE_ALLOWED |
| 3467 | && (!startIfModeDefault || mode != AppOpsManager.MODE_DEFAULT)) { |
| 3468 | if (DEBUG) Slog.d(TAG, "startOperation: reject #" + mode + " for code " |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3469 | + switchCode + " (" + code + ") uid " + uid + " package " |
| 3470 | + resolvedPackageName); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3471 | attributedOp.rejected(uidState.state, AppOpsManager.OP_FLAG_SELF); |
Adam Bookatz | 182862e | 2020-04-27 21:58:22 -0700 | [diff] [blame] | 3472 | scheduleOpStartedIfNeededLocked(code, uid, packageName, mode); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3473 | return mode; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3474 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3475 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3476 | if (DEBUG) Slog.d(TAG, "startOperation: allowing code " + code + " uid " + uid |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 3477 | + " package " + resolvedPackageName); |
Adam Bookatz | 182862e | 2020-04-27 21:58:22 -0700 | [diff] [blame] | 3478 | scheduleOpStartedIfNeededLocked(code, uid, packageName, AppOpsManager.MODE_ALLOWED); |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 3479 | try { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3480 | attributedOp.started(clientId, uidState.state); |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 3481 | } catch (RemoteException e) { |
| 3482 | throw new RuntimeException(e); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3483 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3484 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3485 | |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 3486 | if (shouldCollectAsyncNotedOp) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3487 | collectAsyncNotedOp(uid, packageName, code, attributionTag, message); |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 3488 | } |
| 3489 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3490 | return AppOpsManager.MODE_ALLOWED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | @Override |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 3494 | public void finishOperation(IBinder clientId, int code, int uid, String packageName, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3495 | String attributionTag) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 3496 | verifyIncomingUid(uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 3497 | verifyIncomingOp(code); |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 3498 | String resolvedPackageName = resolvePackageName(uid, packageName); |
| 3499 | if (resolvedPackageName == null) { |
| 3500 | return; |
| 3501 | } |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3502 | |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3503 | RestrictionBypass bypass; |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3504 | try { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3505 | bypass = verifyAndGetBypass(uid, packageName, attributionTag); |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3506 | } catch (SecurityException e) { |
| 3507 | Slog.e(TAG, "Cannot finishOperation", e); |
| 3508 | return; |
| 3509 | } |
| 3510 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3511 | synchronized (this) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3512 | Op op = getOpLocked(code, uid, resolvedPackageName, attributionTag, bypass, true); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3513 | if (op == null) { |
Philip P. Moltmann | 5ab15b0 | 2020-03-13 15:44:51 -0700 | [diff] [blame] | 3514 | Slog.e(TAG, "Operation not found: uid=" + uid + " pkg=" + packageName + "(" |
| 3515 | + attributionTag + ") op=" + AppOpsManager.opToName(code)); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3516 | return; |
| 3517 | } |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3518 | final AttributedOp attributedOp = op.mAttributions.get(attributionTag); |
| 3519 | if (attributedOp == null) { |
Philip P. Moltmann | 5ab15b0 | 2020-03-13 15:44:51 -0700 | [diff] [blame] | 3520 | Slog.e(TAG, "Attribution not found: uid=" + uid + " pkg=" + packageName + "(" |
| 3521 | + attributionTag + ") op=" + AppOpsManager.opToName(code)); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 3522 | return; |
| 3523 | } |
| 3524 | |
Philip P. Moltmann | 5ab15b0 | 2020-03-13 15:44:51 -0700 | [diff] [blame] | 3525 | if (attributedOp.isRunning()) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3526 | attributedOp.finished(clientId); |
Philip P. Moltmann | 5ab15b0 | 2020-03-13 15:44:51 -0700 | [diff] [blame] | 3527 | } else { |
| 3528 | Slog.e(TAG, "Operation not started: uid=" + uid + " pkg=" + packageName + "(" |
| 3529 | + attributionTag + ") op=" + AppOpsManager.opToName(code)); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3530 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3531 | } |
| 3532 | } |
| 3533 | |
| 3534 | private void scheduleOpActiveChangedIfNeededLocked(int code, int uid, String packageName, |
| 3535 | boolean active) { |
| 3536 | ArraySet<ActiveCallback> dispatchedCallbacks = null; |
| 3537 | final int callbackListCount = mActiveWatchers.size(); |
| 3538 | for (int i = 0; i < callbackListCount; i++) { |
| 3539 | final SparseArray<ActiveCallback> callbacks = mActiveWatchers.valueAt(i); |
| 3540 | ActiveCallback callback = callbacks.get(code); |
| 3541 | if (callback != null) { |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 3542 | if (callback.mWatchingUid >= 0 && callback.mWatchingUid != uid) { |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 3543 | continue; |
| 3544 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3545 | if (dispatchedCallbacks == null) { |
| 3546 | dispatchedCallbacks = new ArraySet<>(); |
| 3547 | } |
| 3548 | dispatchedCallbacks.add(callback); |
| 3549 | } |
| 3550 | } |
| 3551 | if (dispatchedCallbacks == null) { |
| 3552 | return; |
| 3553 | } |
| 3554 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 3555 | AppOpsService::notifyOpActiveChanged, |
| 3556 | this, dispatchedCallbacks, code, uid, packageName, active)); |
| 3557 | } |
| 3558 | |
| 3559 | private void notifyOpActiveChanged(ArraySet<ActiveCallback> callbacks, |
| 3560 | int code, int uid, String packageName, boolean active) { |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 3561 | // There are features watching for mode changes such as window manager |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3562 | // and location manager which are in our process. The callbacks in these |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 3563 | // features may require permissions our remote caller does not have. |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3564 | final long identity = Binder.clearCallingIdentity(); |
| 3565 | try { |
| 3566 | final int callbackCount = callbacks.size(); |
| 3567 | for (int i = 0; i < callbackCount; i++) { |
| 3568 | final ActiveCallback callback = callbacks.valueAt(i); |
| 3569 | try { |
| 3570 | callback.mCallback.opActiveChanged(code, uid, packageName, active); |
| 3571 | } catch (RemoteException e) { |
| 3572 | /* do nothing */ |
| 3573 | } |
| 3574 | } |
| 3575 | } finally { |
| 3576 | Binder.restoreCallingIdentity(identity); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 3577 | } |
| 3578 | } |
| 3579 | |
Adam Bookatz | 182862e | 2020-04-27 21:58:22 -0700 | [diff] [blame] | 3580 | private void scheduleOpStartedIfNeededLocked(int code, int uid, String pkgName, int result) { |
| 3581 | ArraySet<StartedCallback> dispatchedCallbacks = null; |
| 3582 | final int callbackListCount = mStartedWatchers.size(); |
| 3583 | for (int i = 0; i < callbackListCount; i++) { |
| 3584 | final SparseArray<StartedCallback> callbacks = mStartedWatchers.valueAt(i); |
| 3585 | |
| 3586 | StartedCallback callback = callbacks.get(code); |
| 3587 | if (callback != null) { |
| 3588 | if (callback.mWatchingUid >= 0 && callback.mWatchingUid != uid) { |
| 3589 | continue; |
| 3590 | } |
| 3591 | |
| 3592 | if (dispatchedCallbacks == null) { |
| 3593 | dispatchedCallbacks = new ArraySet<>(); |
| 3594 | } |
| 3595 | dispatchedCallbacks.add(callback); |
| 3596 | } |
| 3597 | } |
| 3598 | |
| 3599 | if (dispatchedCallbacks == null) { |
| 3600 | return; |
| 3601 | } |
| 3602 | |
| 3603 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 3604 | AppOpsService::notifyOpStarted, |
| 3605 | this, dispatchedCallbacks, code, uid, pkgName, result)); |
| 3606 | } |
| 3607 | |
| 3608 | private void notifyOpStarted(ArraySet<StartedCallback> callbacks, |
| 3609 | int code, int uid, String packageName, int result) { |
| 3610 | final long identity = Binder.clearCallingIdentity(); |
| 3611 | try { |
| 3612 | final int callbackCount = callbacks.size(); |
| 3613 | for (int i = 0; i < callbackCount; i++) { |
| 3614 | final StartedCallback callback = callbacks.valueAt(i); |
| 3615 | try { |
| 3616 | callback.mCallback.opStarted(code, uid, packageName, result); |
| 3617 | } catch (RemoteException e) { |
| 3618 | /* do nothing */ |
| 3619 | } |
| 3620 | } |
| 3621 | } finally { |
| 3622 | Binder.restoreCallingIdentity(identity); |
| 3623 | } |
| 3624 | } |
| 3625 | |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 3626 | private void scheduleOpNotedIfNeededLocked(int code, int uid, String packageName, |
| 3627 | int result) { |
| 3628 | ArraySet<NotedCallback> dispatchedCallbacks = null; |
| 3629 | final int callbackListCount = mNotedWatchers.size(); |
| 3630 | for (int i = 0; i < callbackListCount; i++) { |
| 3631 | final SparseArray<NotedCallback> callbacks = mNotedWatchers.valueAt(i); |
| 3632 | final NotedCallback callback = callbacks.get(code); |
| 3633 | if (callback != null) { |
| 3634 | if (callback.mWatchingUid >= 0 && callback.mWatchingUid != uid) { |
| 3635 | continue; |
| 3636 | } |
| 3637 | if (dispatchedCallbacks == null) { |
| 3638 | dispatchedCallbacks = new ArraySet<>(); |
| 3639 | } |
| 3640 | dispatchedCallbacks.add(callback); |
| 3641 | } |
| 3642 | } |
| 3643 | if (dispatchedCallbacks == null) { |
| 3644 | return; |
| 3645 | } |
| 3646 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 3647 | AppOpsService::notifyOpChecked, |
| 3648 | this, dispatchedCallbacks, code, uid, packageName, result)); |
| 3649 | } |
| 3650 | |
| 3651 | private void notifyOpChecked(ArraySet<NotedCallback> callbacks, |
| 3652 | int code, int uid, String packageName, int result) { |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 3653 | // There are features watching for checks in our process. The callbacks in |
| 3654 | // these features may require permissions our remote caller does not have. |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 3655 | final long identity = Binder.clearCallingIdentity(); |
| 3656 | try { |
| 3657 | final int callbackCount = callbacks.size(); |
| 3658 | for (int i = 0; i < callbackCount; i++) { |
| 3659 | final NotedCallback callback = callbacks.valueAt(i); |
| 3660 | try { |
| 3661 | callback.mCallback.opNoted(code, uid, packageName, result); |
| 3662 | } catch (RemoteException e) { |
| 3663 | /* do nothing */ |
| 3664 | } |
| 3665 | } |
| 3666 | } finally { |
| 3667 | Binder.restoreCallingIdentity(identity); |
| 3668 | } |
| 3669 | } |
| 3670 | |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 3671 | @Override |
| 3672 | public int permissionToOpCode(String permission) { |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 3673 | if (permission == null) { |
| 3674 | return AppOpsManager.OP_NONE; |
| 3675 | } |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 3676 | return AppOpsManager.permissionToOpCode(permission); |
| 3677 | } |
| 3678 | |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 3679 | @Override |
| 3680 | public boolean shouldCollectNotes(int opCode) { |
| 3681 | Preconditions.checkArgumentInRange(opCode, 0, _NUM_OP - 1, "opCode"); |
| 3682 | |
| 3683 | String perm = AppOpsManager.opToPermission(opCode); |
| 3684 | if (perm == null) { |
| 3685 | return false; |
| 3686 | } |
| 3687 | |
| 3688 | PermissionInfo permInfo; |
| 3689 | try { |
| 3690 | permInfo = mContext.getPackageManager().getPermissionInfo(perm, 0); |
| 3691 | } catch (PackageManager.NameNotFoundException e) { |
| 3692 | return false; |
| 3693 | } |
| 3694 | |
Philip P. Moltmann | 87fd8bc | 2020-02-13 13:16:13 -0800 | [diff] [blame] | 3695 | return permInfo.getProtection() == PROTECTION_DANGEROUS |
| 3696 | || (permInfo.getProtectionFlags() & PROTECTION_FLAG_APPOP) != 0; |
Philip P. Moltmann | 2b08aaf | 2019-06-10 08:49:11 -0700 | [diff] [blame] | 3697 | } |
| 3698 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 3699 | private void verifyIncomingUid(int uid) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3700 | if (uid == Binder.getCallingUid()) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 3701 | return; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3702 | } |
| 3703 | if (Binder.getCallingPid() == Process.myPid()) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 3704 | return; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3705 | } |
| 3706 | mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS, |
| 3707 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3708 | } |
| 3709 | |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 3710 | private void verifyIncomingOp(int op) { |
| 3711 | if (op >= 0 && op < AppOpsManager._NUM_OP) { |
| 3712 | return; |
| 3713 | } |
| 3714 | throw new IllegalArgumentException("Bad operation #" + op); |
| 3715 | } |
| 3716 | |
Philip P. Moltmann | 724150d | 2019-03-11 17:01:05 -0700 | [diff] [blame] | 3717 | private @Nullable UidState getUidStateLocked(int uid, boolean edit) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3718 | UidState uidState = mUidStates.get(uid); |
| 3719 | if (uidState == null) { |
| 3720 | if (!edit) { |
| 3721 | return null; |
| 3722 | } |
| 3723 | uidState = new UidState(uid); |
| 3724 | mUidStates.put(uid, uidState); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3725 | } else { |
Philip P. Moltmann | 463979e | 2020-01-24 14:41:22 -0800 | [diff] [blame] | 3726 | updatePendingStateIfNeededLocked(uidState); |
| 3727 | } |
| 3728 | return uidState; |
| 3729 | } |
| 3730 | |
| 3731 | /** |
| 3732 | * Check if the pending state should be updated and do so if needed |
| 3733 | * |
| 3734 | * @param uidState The uidState that might have a pending state |
| 3735 | */ |
| 3736 | private void updatePendingStateIfNeededLocked(@NonNull UidState uidState) { |
| 3737 | if (uidState != null) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3738 | if (uidState.pendingStateCommitTime != 0) { |
Dianne Hackborn | 9fb9350 | 2018-06-18 12:29:44 -0700 | [diff] [blame] | 3739 | if (uidState.pendingStateCommitTime < mLastRealtime) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3740 | commitUidPendingStateLocked(uidState); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3741 | } else { |
Dianne Hackborn | 9fb9350 | 2018-06-18 12:29:44 -0700 | [diff] [blame] | 3742 | mLastRealtime = SystemClock.elapsedRealtime(); |
| 3743 | if (uidState.pendingStateCommitTime < mLastRealtime) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3744 | commitUidPendingStateLocked(uidState); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3745 | } |
| 3746 | } |
| 3747 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3748 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3749 | } |
| 3750 | |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3751 | private void commitUidPendingStateLocked(UidState uidState) { |
Wei Wang | 878d0b6 | 2019-03-28 18:12:18 -0700 | [diff] [blame] | 3752 | if (uidState.hasForegroundWatchers) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3753 | for (int fgi = uidState.foregroundOps.size() - 1; fgi >= 0; fgi--) { |
| 3754 | if (!uidState.foregroundOps.valueAt(fgi)) { |
| 3755 | continue; |
| 3756 | } |
| 3757 | final int code = uidState.foregroundOps.keyAt(fgi); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 3758 | // For location ops we consider fg state only if the fg service |
| 3759 | // is of location type, for all other ops any fg service will do. |
Wei Wang | 711eb66 | 2019-03-21 18:24:17 -0700 | [diff] [blame] | 3760 | final long firstUnrestrictedUidState = resolveFirstUnrestrictedUidState(code); |
| 3761 | final boolean resolvedLastFg = uidState.state <= firstUnrestrictedUidState; |
| 3762 | final boolean resolvedNowFg = uidState.pendingState <= firstUnrestrictedUidState; |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 3763 | if (resolvedLastFg == resolvedNowFg |
| 3764 | && uidState.capability == uidState.pendingCapability |
| 3765 | && uidState.appWidgetVisible == uidState.pendingAppWidgetVisible) { |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 3766 | continue; |
| 3767 | } |
Philip P. Moltmann | 584c23a | 2020-01-24 14:21:12 -0800 | [diff] [blame] | 3768 | |
| 3769 | if (uidState.opModes != null |
| 3770 | && uidState.opModes.indexOfKey(code) >= 0 |
| 3771 | && uidState.opModes.get(code) == AppOpsManager.MODE_FOREGROUND) { |
| 3772 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 3773 | AppOpsService::notifyOpChangedForAllPkgsInUid, |
| 3774 | this, code, uidState.uid, true, null)); |
| 3775 | } else { |
| 3776 | final ArraySet<ModeCallback> callbacks = mOpModeWatchers.get(code); |
| 3777 | if (callbacks != null) { |
| 3778 | for (int cbi = callbacks.size() - 1; cbi >= 0; cbi--) { |
| 3779 | final ModeCallback callback = callbacks.valueAt(cbi); |
| 3780 | if ((callback.mFlags & AppOpsManager.WATCH_FOREGROUND_CHANGES) == 0 |
| 3781 | || !callback.isWatchingUid(uidState.uid)) { |
| 3782 | continue; |
| 3783 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3784 | for (int pkgi = uidState.pkgOps.size() - 1; pkgi >= 0; pkgi--) { |
| 3785 | final Op op = uidState.pkgOps.valueAt(pkgi).get(code); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 3786 | if (op == null) { |
| 3787 | continue; |
| 3788 | } |
Philip P. Moltmann | 584c23a | 2020-01-24 14:21:12 -0800 | [diff] [blame] | 3789 | if (op.mode == AppOpsManager.MODE_FOREGROUND) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3790 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 3791 | AppOpsService::notifyOpChanged, |
| 3792 | this, callback, code, uidState.uid, |
| 3793 | uidState.pkgOps.keyAt(pkgi))); |
| 3794 | } |
| 3795 | } |
| 3796 | } |
| 3797 | } |
| 3798 | } |
| 3799 | } |
| 3800 | } |
Wei Wang | 711eb66 | 2019-03-21 18:24:17 -0700 | [diff] [blame] | 3801 | uidState.state = uidState.pendingState; |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 3802 | uidState.capability = uidState.pendingCapability; |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 3803 | uidState.appWidgetVisible = uidState.pendingAppWidgetVisible; |
Wei Wang | 711eb66 | 2019-03-21 18:24:17 -0700 | [diff] [blame] | 3804 | uidState.pendingStateCommitTime = 0; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3805 | } |
| 3806 | |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 3807 | private void updateAppWidgetVisibility(SparseArray<String> uidPackageNames, boolean visible) { |
| 3808 | synchronized (this) { |
| 3809 | for (int i = uidPackageNames.size() - 1; i >= 0; i--) { |
| 3810 | final int uid = uidPackageNames.keyAt(i); |
| 3811 | final UidState uidState = getUidStateLocked(uid, true); |
| 3812 | if (uidState != null && (uidState.pendingAppWidgetVisible != visible)) { |
| 3813 | uidState.pendingAppWidgetVisible = visible; |
| 3814 | if (uidState.pendingAppWidgetVisible != uidState.appWidgetVisible) { |
| 3815 | commitUidPendingStateLocked(uidState); |
| 3816 | } |
| 3817 | } |
| 3818 | } |
| 3819 | } |
| 3820 | } |
| 3821 | |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3822 | /** |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3823 | * Create a restriction description matching the properties of the package. |
| 3824 | * |
| 3825 | * @param context A context to use |
| 3826 | * @param pkg The package to create the restriction description for |
| 3827 | * |
| 3828 | * @return The restriction matching the package |
| 3829 | */ |
| 3830 | private RestrictionBypass getBypassforPackage(@NonNull AndroidPackage pkg) { |
| 3831 | return new RestrictionBypass(pkg.isPrivileged(), mContext.checkPermission( |
| 3832 | android.Manifest.permission.EXEMPT_FROM_AUDIO_RECORD_RESTRICTIONS, -1, pkg.getUid()) |
| 3833 | == PackageManager.PERMISSION_GRANTED); |
| 3834 | } |
| 3835 | |
| 3836 | /** |
| 3837 | * Verify that package belongs to uid and return the {@link RestrictionBypass bypass |
| 3838 | * description} for the package. |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3839 | * |
| 3840 | * @param uid The uid the package belongs to |
| 3841 | * @param packageName The package the might belong to the uid |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3842 | * @param attributionTag attribution tag or {@code null} if no need to verify |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3843 | * |
| 3844 | * @return {@code true} iff the package is privileged |
| 3845 | */ |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3846 | private @Nullable RestrictionBypass verifyAndGetBypass(int uid, String packageName, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3847 | @Nullable String attributionTag) { |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3848 | if (uid == Process.ROOT_UID) { |
| 3849 | // For backwards compatibility, don't check package name for root UID. |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3850 | return null; |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3851 | } |
| 3852 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3853 | // Do not check if uid/packageName/attributionTag is already known |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3854 | synchronized (this) { |
| 3855 | UidState uidState = mUidStates.get(uid); |
| 3856 | if (uidState != null && uidState.pkgOps != null) { |
| 3857 | Ops ops = uidState.pkgOps.get(packageName); |
| 3858 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3859 | if (ops != null && (attributionTag == null || ops.knownAttributionTags.contains( |
| 3860 | attributionTag)) && ops.bypass != null) { |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3861 | return ops.bypass; |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3862 | } |
| 3863 | } |
| 3864 | } |
| 3865 | |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3866 | RestrictionBypass bypass = null; |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3867 | final long ident = Binder.clearCallingIdentity(); |
| 3868 | try { |
| 3869 | int pkgUid; |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 3870 | AndroidPackage pkg = LocalServices.getService(PackageManagerInternal.class).getPackage( |
| 3871 | packageName); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3872 | boolean isAttributionTagValid = false; |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3873 | |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 3874 | if (pkg != null) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3875 | if (attributionTag == null) { |
| 3876 | isAttributionTagValid = true; |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 3877 | } else { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3878 | if (pkg.getAttributions() != null) { |
| 3879 | int numAttributions = pkg.getAttributions().size(); |
| 3880 | for (int i = 0; i < numAttributions; i++) { |
| 3881 | if (pkg.getAttributions().get(i).tag.equals(attributionTag)) { |
| 3882 | isAttributionTagValid = true; |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 3883 | } |
| 3884 | } |
| 3885 | } |
| 3886 | } |
| 3887 | |
| 3888 | pkgUid = UserHandle.getUid( |
| 3889 | UserHandle.getUserId(uid), UserHandle.getAppId(pkg.getUid())); |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3890 | bypass = getBypassforPackage(pkg); |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3891 | } else { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3892 | // Allow any attribution tag for resolvable uids |
| 3893 | isAttributionTagValid = true; |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 3894 | |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3895 | pkgUid = resolveUid(packageName); |
| 3896 | if (pkgUid >= 0) { |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3897 | bypass = RestrictionBypass.UNRESTRICTED; |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3898 | } |
| 3899 | } |
| 3900 | if (pkgUid != uid) { |
| 3901 | throw new SecurityException("Specified package " + packageName + " under uid " + uid |
| 3902 | + " but it is really " + pkgUid); |
| 3903 | } |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 3904 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3905 | if (!isAttributionTagValid) { |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 3906 | // TODO moltmann: Switch from logging to enforcement |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3907 | Slog.e(TAG, "attributionTag " + attributionTag + " not declared in manifest of " |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 3908 | + packageName); |
| 3909 | } |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3910 | } finally { |
| 3911 | Binder.restoreCallingIdentity(ident); |
| 3912 | } |
| 3913 | |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3914 | return bypass; |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3915 | } |
| 3916 | |
| 3917 | /** |
| 3918 | * Get (and potentially create) ops. |
| 3919 | * |
| 3920 | * @param uid The uid the package belongs to |
| 3921 | * @param packageName The name of the package |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3922 | * @param attributionTag attribution tag |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3923 | * @param bypass When to bypass certain op restrictions (can be null if edit == false) |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3924 | * @param edit If an ops does not exist, create the ops? |
| 3925 | |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3926 | * @return The ops |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3927 | */ |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3928 | private Ops getOpsLocked(int uid, String packageName, @Nullable String attributionTag, |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3929 | @Nullable RestrictionBypass bypass, boolean edit) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3930 | UidState uidState = getUidStateLocked(uid, edit); |
| 3931 | if (uidState == null) { |
| 3932 | return null; |
| 3933 | } |
| 3934 | |
| 3935 | if (uidState.pkgOps == null) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 3936 | if (!edit) { |
| 3937 | return null; |
| 3938 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3939 | uidState.pkgOps = new ArrayMap<>(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3940 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3941 | |
| 3942 | Ops ops = uidState.pkgOps.get(packageName); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3943 | if (ops == null) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 3944 | if (!edit) { |
| 3945 | return null; |
| 3946 | } |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3947 | ops = new Ops(packageName, uidState); |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 3948 | uidState.pkgOps.put(packageName, ops); |
| 3949 | } |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 3950 | |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3951 | if (edit) { |
| 3952 | if (bypass != null) { |
| 3953 | ops.bypass = bypass; |
| 3954 | } |
| 3955 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3956 | if (attributionTag != null) { |
| 3957 | ops.knownAttributionTags.add(attributionTag); |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3958 | } |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 3959 | } |
| 3960 | |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 3961 | return ops; |
| 3962 | } |
| 3963 | |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 3964 | private void scheduleWriteLocked() { |
| 3965 | if (!mWriteScheduled) { |
| 3966 | mWriteScheduled = true; |
| 3967 | mHandler.postDelayed(mWriteRunner, WRITE_DELAY); |
| 3968 | } |
| 3969 | } |
| 3970 | |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 3971 | private void scheduleFastWriteLocked() { |
| 3972 | if (!mFastWriteScheduled) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 3973 | mWriteScheduled = true; |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 3974 | mFastWriteScheduled = true; |
| 3975 | mHandler.removeCallbacks(mWriteRunner); |
| 3976 | mHandler.postDelayed(mWriteRunner, 10*1000); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 3977 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 3978 | } |
| 3979 | |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 3980 | /** |
| 3981 | * Get the state of an op for a uid. |
| 3982 | * |
| 3983 | * @param code The code of the op |
| 3984 | * @param uid The uid the of the package |
| 3985 | * @param packageName The package name for which to get the state for |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3986 | * @param attributionTag The attribution tag |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 3987 | * @param bypass When to bypass certain op restrictions (can be null if edit == false) |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 3988 | * @param edit Iff {@code true} create the {@link Op} object if not yet created |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 3989 | * |
| 3990 | * @return The {@link Op state} of the op |
| 3991 | */ |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 3992 | private @Nullable Op getOpLocked(int code, int uid, @NonNull String packageName, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 3993 | @Nullable String attributionTag, @Nullable RestrictionBypass bypass, boolean edit) { |
| 3994 | Ops ops = getOpsLocked(uid, packageName, attributionTag, bypass, edit); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 3995 | if (ops == null) { |
| 3996 | return null; |
| 3997 | } |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 3998 | return getOpLocked(ops, code, uid, edit); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 3999 | } |
| 4000 | |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 4001 | private Op getOpLocked(Ops ops, int code, int uid, boolean edit) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 4002 | Op op = ops.get(code); |
| 4003 | if (op == null) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4004 | if (!edit) { |
| 4005 | return null; |
| 4006 | } |
Philip P. Moltmann | 9046d82 | 2019-12-13 15:59:49 -0800 | [diff] [blame] | 4007 | op = new Op(ops.uidState, ops.packageName, code, uid); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 4008 | ops.put(code, op); |
| 4009 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 4010 | if (edit) { |
| 4011 | scheduleWriteLocked(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4012 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 4013 | return op; |
| 4014 | } |
| 4015 | |
Suprabh Shukla | 7e01792 | 2019-08-05 17:13:23 -0700 | [diff] [blame] | 4016 | private boolean isOpRestrictedDueToSuspend(int code, String packageName, int uid) { |
Suprabh Shukla | b614a22 | 2019-09-12 14:42:46 -0700 | [diff] [blame] | 4017 | if (!ArrayUtils.contains(OPS_RESTRICTED_ON_SUSPEND, code)) { |
| 4018 | return false; |
| 4019 | } |
Suprabh Shukla | 7e01792 | 2019-08-05 17:13:23 -0700 | [diff] [blame] | 4020 | final PackageManagerInternal pmi = LocalServices.getService(PackageManagerInternal.class); |
Suprabh Shukla | b614a22 | 2019-09-12 14:42:46 -0700 | [diff] [blame] | 4021 | return pmi.isPackageSuspended(packageName, UserHandle.getUserId(uid)); |
Suprabh Shukla | 7e01792 | 2019-08-05 17:13:23 -0700 | [diff] [blame] | 4022 | } |
| 4023 | |
Philip P. Moltmann | ec142a5 | 2019-04-09 13:38:07 -0700 | [diff] [blame] | 4024 | private boolean isOpRestrictedLocked(int uid, int code, String packageName, |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 4025 | @Nullable RestrictionBypass appBypass) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 4026 | int userHandle = UserHandle.getUserId(uid); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 4027 | final int restrictionSetCount = mOpUserRestrictions.size(); |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 4028 | |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 4029 | for (int i = 0; i < restrictionSetCount; i++) { |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 4030 | // For each client, check that the given op is not restricted, or that the given |
| 4031 | // package is exempt from the restriction. |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 4032 | ClientRestrictionState restrictionState = mOpUserRestrictions.valueAt(i); |
Suprabh Shukla | ffddadb | 2016-05-20 16:37:26 -0700 | [diff] [blame] | 4033 | if (restrictionState.hasRestriction(code, packageName, userHandle)) { |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 4034 | RestrictionBypass opBypass = opAllowSystemBypassRestriction(code); |
| 4035 | if (opBypass != null) { |
Suprabh Shukla | ffddadb | 2016-05-20 16:37:26 -0700 | [diff] [blame] | 4036 | // If we are the system, bypass user restrictions for certain codes |
| 4037 | synchronized (this) { |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 4038 | if (opBypass.isPrivileged && appBypass != null && appBypass.isPrivileged) { |
| 4039 | return false; |
| 4040 | } |
| 4041 | if (opBypass.isRecordAudioRestrictionExcept && appBypass != null |
| 4042 | && appBypass.isRecordAudioRestrictionExcept) { |
Suprabh Shukla | ffddadb | 2016-05-20 16:37:26 -0700 | [diff] [blame] | 4043 | return false; |
| 4044 | } |
Ruben Brunk | 32f0fa4 | 2016-03-11 19:07:07 -0800 | [diff] [blame] | 4045 | } |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 4046 | } |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 4047 | return true; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 4048 | } |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 4049 | } |
| 4050 | return false; |
| 4051 | } |
| 4052 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4053 | void readState() { |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 4054 | int oldVersion = NO_VERSION; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4055 | synchronized (mFile) { |
| 4056 | synchronized (this) { |
| 4057 | FileInputStream stream; |
| 4058 | try { |
| 4059 | stream = mFile.openRead(); |
| 4060 | } catch (FileNotFoundException e) { |
| 4061 | Slog.i(TAG, "No existing app ops " + mFile.getBaseFile() + "; starting empty"); |
| 4062 | return; |
| 4063 | } |
| 4064 | boolean success = false; |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 4065 | mUidStates.clear(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4066 | try { |
| 4067 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 4068 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4069 | int type; |
| 4070 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 4071 | && type != XmlPullParser.END_DOCUMENT) { |
| 4072 | ; |
| 4073 | } |
| 4074 | |
| 4075 | if (type != XmlPullParser.START_TAG) { |
| 4076 | throw new IllegalStateException("no start tag found"); |
| 4077 | } |
| 4078 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 4079 | final String versionString = parser.getAttributeValue(null, "v"); |
| 4080 | if (versionString != null) { |
| 4081 | oldVersion = Integer.parseInt(versionString); |
| 4082 | } |
| 4083 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4084 | int outerDepth = parser.getDepth(); |
| 4085 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 4086 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 4087 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 4088 | continue; |
| 4089 | } |
| 4090 | |
| 4091 | String tagName = parser.getName(); |
| 4092 | if (tagName.equals("pkg")) { |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 4093 | readPackage(parser); |
Svetoslav | 215b44a | 2015-08-04 19:03:40 -0700 | [diff] [blame] | 4094 | } else if (tagName.equals("uid")) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 4095 | readUidOps(parser); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4096 | } else { |
| 4097 | Slog.w(TAG, "Unknown element under <app-ops>: " |
| 4098 | + parser.getName()); |
| 4099 | XmlUtils.skipCurrentTag(parser); |
| 4100 | } |
| 4101 | } |
| 4102 | success = true; |
| 4103 | } catch (IllegalStateException e) { |
| 4104 | Slog.w(TAG, "Failed parsing " + e); |
| 4105 | } catch (NullPointerException e) { |
| 4106 | Slog.w(TAG, "Failed parsing " + e); |
| 4107 | } catch (NumberFormatException e) { |
| 4108 | Slog.w(TAG, "Failed parsing " + e); |
| 4109 | } catch (XmlPullParserException e) { |
| 4110 | Slog.w(TAG, "Failed parsing " + e); |
| 4111 | } catch (IOException e) { |
| 4112 | Slog.w(TAG, "Failed parsing " + e); |
| 4113 | } catch (IndexOutOfBoundsException e) { |
| 4114 | Slog.w(TAG, "Failed parsing " + e); |
| 4115 | } finally { |
| 4116 | if (!success) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 4117 | mUidStates.clear(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4118 | } |
| 4119 | try { |
| 4120 | stream.close(); |
| 4121 | } catch (IOException e) { |
| 4122 | } |
| 4123 | } |
| 4124 | } |
| 4125 | } |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 4126 | synchronized (this) { |
| 4127 | upgradeLocked(oldVersion); |
| 4128 | } |
| 4129 | } |
| 4130 | |
| 4131 | private void upgradeRunAnyInBackgroundLocked() { |
| 4132 | for (int i = 0; i < mUidStates.size(); i++) { |
| 4133 | final UidState uidState = mUidStates.valueAt(i); |
| 4134 | if (uidState == null) { |
| 4135 | continue; |
| 4136 | } |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 4137 | if (uidState.opModes != null) { |
| 4138 | final int idx = uidState.opModes.indexOfKey(AppOpsManager.OP_RUN_IN_BACKGROUND); |
| 4139 | if (idx >= 0) { |
| 4140 | uidState.opModes.put(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, |
| 4141 | uidState.opModes.valueAt(idx)); |
| 4142 | } |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 4143 | } |
| 4144 | if (uidState.pkgOps == null) { |
| 4145 | continue; |
| 4146 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 4147 | boolean changed = false; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 4148 | for (int j = 0; j < uidState.pkgOps.size(); j++) { |
| 4149 | Ops ops = uidState.pkgOps.valueAt(j); |
| 4150 | if (ops != null) { |
| 4151 | final Op op = ops.get(AppOpsManager.OP_RUN_IN_BACKGROUND); |
| 4152 | if (op != null && op.mode != AppOpsManager.opToDefaultMode(op.op)) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 4153 | final Op copy = new Op(op.uidState, op.packageName, |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 4154 | AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, uidState.uid); |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 4155 | copy.mode = op.mode; |
| 4156 | ops.put(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, copy); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 4157 | changed = true; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 4158 | } |
| 4159 | } |
| 4160 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 4161 | if (changed) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 4162 | uidState.evalForegroundOps(mOpModeWatchers); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 4163 | } |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 4164 | } |
| 4165 | } |
| 4166 | |
| 4167 | private void upgradeLocked(int oldVersion) { |
| 4168 | if (oldVersion >= CURRENT_VERSION) { |
| 4169 | return; |
| 4170 | } |
| 4171 | Slog.d(TAG, "Upgrading app-ops xml from version " + oldVersion + " to " + CURRENT_VERSION); |
| 4172 | switch (oldVersion) { |
| 4173 | case NO_VERSION: |
| 4174 | upgradeRunAnyInBackgroundLocked(); |
| 4175 | // fall through |
| 4176 | case 1: |
| 4177 | // for future upgrades |
| 4178 | } |
| 4179 | scheduleFastWriteLocked(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4180 | } |
| 4181 | |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 4182 | private void readUidOps(XmlPullParser parser) throws NumberFormatException, |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 4183 | XmlPullParserException, IOException { |
| 4184 | final int uid = Integer.parseInt(parser.getAttributeValue(null, "n")); |
| 4185 | int outerDepth = parser.getDepth(); |
| 4186 | int type; |
| 4187 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 4188 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 4189 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 4190 | continue; |
| 4191 | } |
| 4192 | |
| 4193 | String tagName = parser.getName(); |
| 4194 | if (tagName.equals("op")) { |
| 4195 | final int code = Integer.parseInt(parser.getAttributeValue(null, "n")); |
| 4196 | final int mode = Integer.parseInt(parser.getAttributeValue(null, "m")); |
Philip P. Moltmann | 94faa4a | 2020-01-30 14:16:40 -0800 | [diff] [blame] | 4197 | setUidMode(code, uid, mode); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 4198 | } else { |
| 4199 | Slog.w(TAG, "Unknown element under <uid-ops>: " |
| 4200 | + parser.getName()); |
| 4201 | XmlUtils.skipCurrentTag(parser); |
| 4202 | } |
| 4203 | } |
| 4204 | } |
| 4205 | |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 4206 | private void readPackage(XmlPullParser parser) |
| 4207 | throws NumberFormatException, XmlPullParserException, IOException { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4208 | String pkgName = parser.getAttributeValue(null, "n"); |
| 4209 | int outerDepth = parser.getDepth(); |
| 4210 | int type; |
| 4211 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 4212 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 4213 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 4214 | continue; |
| 4215 | } |
| 4216 | |
| 4217 | String tagName = parser.getName(); |
| 4218 | if (tagName.equals("uid")) { |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 4219 | readUid(parser, pkgName); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4220 | } else { |
| 4221 | Slog.w(TAG, "Unknown element under <pkg>: " |
| 4222 | + parser.getName()); |
| 4223 | XmlUtils.skipCurrentTag(parser); |
| 4224 | } |
| 4225 | } |
| 4226 | } |
| 4227 | |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 4228 | private void readUid(XmlPullParser parser, String pkgName) |
| 4229 | throws NumberFormatException, XmlPullParserException, IOException { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4230 | int uid = Integer.parseInt(parser.getAttributeValue(null, "n")); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 4231 | final UidState uidState = getUidStateLocked(uid, true); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4232 | int outerDepth = parser.getDepth(); |
| 4233 | int type; |
| 4234 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 4235 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 4236 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 4237 | continue; |
| 4238 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4239 | String tagName = parser.getName(); |
| 4240 | if (tagName.equals("op")) { |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 4241 | readOp(parser, uidState, pkgName); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4242 | } else { |
| 4243 | Slog.w(TAG, "Unknown element under <pkg>: " |
| 4244 | + parser.getName()); |
| 4245 | XmlUtils.skipCurrentTag(parser); |
| 4246 | } |
| 4247 | } |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 4248 | uidState.evalForegroundOps(mOpModeWatchers); |
| 4249 | } |
| 4250 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4251 | private void readAttributionOp(XmlPullParser parser, @NonNull Op parent, |
| 4252 | @Nullable String attribution) throws NumberFormatException, IOException { |
| 4253 | final AttributedOp attributedOp = parent.getOrCreateAttribution(parent, attribution); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4254 | |
| 4255 | final long key = XmlUtils.readLongAttribute(parser, "n"); |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 4256 | final int uidState = extractUidStateFromKey(key); |
| 4257 | final int opFlags = extractFlagsFromKey(key); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4258 | |
| 4259 | final long accessTime = XmlUtils.readLongAttribute(parser, "t", 0); |
| 4260 | final long rejectTime = XmlUtils.readLongAttribute(parser, "r", 0); |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 4261 | final long accessDuration = XmlUtils.readLongAttribute(parser, "d", -1); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4262 | final String proxyPkg = XmlUtils.readStringAttribute(parser, "pp"); |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 4263 | final int proxyUid = XmlUtils.readIntAttribute(parser, "pu", Process.INVALID_UID); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4264 | final String proxyAttributionTag = XmlUtils.readStringAttribute(parser, "pc"); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4265 | |
| 4266 | if (accessTime > 0) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4267 | attributedOp.accessed(accessTime, accessDuration, proxyUid, proxyPkg, |
| 4268 | proxyAttributionTag, uidState, opFlags); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4269 | } |
| 4270 | if (rejectTime > 0) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4271 | attributedOp.rejected(rejectTime, uidState, opFlags); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4272 | } |
| 4273 | } |
| 4274 | |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 4275 | private void readOp(XmlPullParser parser, @NonNull UidState uidState, @NonNull String pkgName) |
| 4276 | throws NumberFormatException, |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4277 | XmlPullParserException, IOException { |
shafik | 3ea8035 | 2020-02-21 15:29:58 +0000 | [diff] [blame] | 4278 | int opCode = Integer.parseInt(parser.getAttributeValue(null, "n")); |
shafik | 3ea8035 | 2020-02-21 15:29:58 +0000 | [diff] [blame] | 4279 | Op op = new Op(uidState, pkgName, opCode, uidState.uid); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 4280 | |
| 4281 | final int mode = XmlUtils.readIntAttribute(parser, "m", |
| 4282 | AppOpsManager.opToDefaultMode(op.op)); |
| 4283 | op.mode = mode; |
| 4284 | |
| 4285 | int outerDepth = parser.getDepth(); |
| 4286 | int type; |
| 4287 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 4288 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 4289 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 4290 | continue; |
| 4291 | } |
| 4292 | String tagName = parser.getName(); |
| 4293 | if (tagName.equals("st")) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4294 | readAttributionOp(parser, op, XmlUtils.readStringAttribute(parser, "id")); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 4295 | } else { |
| 4296 | Slog.w(TAG, "Unknown element under <op>: " |
| 4297 | + parser.getName()); |
| 4298 | XmlUtils.skipCurrentTag(parser); |
| 4299 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 4300 | } |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 4301 | |
| 4302 | if (uidState.pkgOps == null) { |
| 4303 | uidState.pkgOps = new ArrayMap<>(); |
| 4304 | } |
| 4305 | Ops ops = uidState.pkgOps.get(pkgName); |
| 4306 | if (ops == null) { |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 4307 | ops = new Ops(pkgName, uidState); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 4308 | uidState.pkgOps.put(pkgName, ops); |
| 4309 | } |
| 4310 | ops.put(op.op, op); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4311 | } |
| 4312 | |
| 4313 | void writeState() { |
| 4314 | synchronized (mFile) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4315 | FileOutputStream stream; |
| 4316 | try { |
| 4317 | stream = mFile.startWrite(); |
| 4318 | } catch (IOException e) { |
| 4319 | Slog.w(TAG, "Failed to write state: " + e); |
| 4320 | return; |
| 4321 | } |
| 4322 | |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 4323 | List<AppOpsManager.PackageOps> allOps = getPackagesForOps(null); |
| 4324 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4325 | try { |
| 4326 | XmlSerializer out = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 4327 | out.setOutput(stream, StandardCharsets.UTF_8.name()); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4328 | out.startDocument(null, true); |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 4329 | out.startTag(null, "app-ops"); |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 4330 | out.attribute(null, "v", String.valueOf(CURRENT_VERSION)); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 4331 | |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 4332 | SparseArray<SparseIntArray> uidStatesClone; |
Eugene Susla | 463d592 | 2019-07-17 18:14:15 -0700 | [diff] [blame] | 4333 | synchronized (this) { |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 4334 | uidStatesClone = new SparseArray<>(mUidStates.size()); |
| 4335 | |
| 4336 | final int uidStateCount = mUidStates.size(); |
| 4337 | for (int uidStateNum = 0; uidStateNum < uidStateCount; uidStateNum++) { |
| 4338 | UidState uidState = mUidStates.valueAt(uidStateNum); |
| 4339 | int uid = mUidStates.keyAt(uidStateNum); |
| 4340 | |
| 4341 | SparseIntArray opModes = uidState.opModes; |
| 4342 | if (opModes != null && opModes.size() > 0) { |
| 4343 | uidStatesClone.put(uid, new SparseIntArray(opModes.size())); |
| 4344 | |
| 4345 | final int opCount = opModes.size(); |
| 4346 | for (int opCountNum = 0; opCountNum < opCount; opCountNum++) { |
| 4347 | uidStatesClone.get(uid).put( |
| 4348 | opModes.keyAt(opCountNum), |
| 4349 | opModes.valueAt(opCountNum)); |
| 4350 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 4351 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 4352 | } |
| 4353 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4354 | |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 4355 | final int uidStateCount = uidStatesClone.size(); |
| 4356 | for (int uidStateNum = 0; uidStateNum < uidStateCount; uidStateNum++) { |
| 4357 | SparseIntArray opModes = uidStatesClone.valueAt(uidStateNum); |
| 4358 | if (opModes != null && opModes.size() > 0) { |
| 4359 | out.startTag(null, "uid"); |
| 4360 | out.attribute(null, "n", |
| 4361 | Integer.toString(uidStatesClone.keyAt(uidStateNum))); |
| 4362 | final int opCount = opModes.size(); |
| 4363 | for (int opCountNum = 0; opCountNum < opCount; opCountNum++) { |
| 4364 | final int op = opModes.keyAt(opCountNum); |
| 4365 | final int mode = opModes.valueAt(opCountNum); |
| 4366 | out.startTag(null, "op"); |
| 4367 | out.attribute(null, "n", Integer.toString(op)); |
| 4368 | out.attribute(null, "m", Integer.toString(mode)); |
| 4369 | out.endTag(null, "op"); |
| 4370 | } |
| 4371 | out.endTag(null, "uid"); |
Eugene Susla | 11b706c | 2019-08-28 14:28:32 -0700 | [diff] [blame] | 4372 | } |
| 4373 | } |
| 4374 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4375 | if (allOps != null) { |
| 4376 | String lastPkg = null; |
| 4377 | for (int i=0; i<allOps.size(); i++) { |
| 4378 | AppOpsManager.PackageOps pkg = allOps.get(i); |
| 4379 | if (!pkg.getPackageName().equals(lastPkg)) { |
| 4380 | if (lastPkg != null) { |
| 4381 | out.endTag(null, "pkg"); |
| 4382 | } |
| 4383 | lastPkg = pkg.getPackageName(); |
| 4384 | out.startTag(null, "pkg"); |
| 4385 | out.attribute(null, "n", lastPkg); |
| 4386 | } |
| 4387 | out.startTag(null, "uid"); |
| 4388 | out.attribute(null, "n", Integer.toString(pkg.getUid())); |
| 4389 | List<AppOpsManager.OpEntry> ops = pkg.getOps(); |
| 4390 | for (int j=0; j<ops.size(); j++) { |
| 4391 | AppOpsManager.OpEntry op = ops.get(j); |
| 4392 | out.startTag(null, "op"); |
| 4393 | out.attribute(null, "n", Integer.toString(op.getOp())); |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 4394 | if (op.getMode() != AppOpsManager.opToDefaultMode(op.getOp())) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 4395 | out.attribute(null, "m", Integer.toString(op.getMode())); |
| 4396 | } |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 4397 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4398 | for (String attributionTag : op.getAttributedOpEntries().keySet()) { |
| 4399 | final AttributedOpEntry attribution = |
| 4400 | op.getAttributedOpEntries().get(attributionTag); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 4401 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4402 | final ArraySet<Long> keys = attribution.collectKeys(); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 4403 | |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 4404 | final int keyCount = keys.size(); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4405 | for (int k = 0; k < keyCount; k++) { |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 4406 | final long key = keys.valueAt(k); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4407 | |
| 4408 | final int uidState = AppOpsManager.extractUidStateFromKey(key); |
| 4409 | final int flags = AppOpsManager.extractFlagsFromKey(key); |
| 4410 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4411 | final long accessTime = attribution.getLastAccessTime(uidState, |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4412 | uidState, flags); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4413 | final long rejectTime = attribution.getLastRejectTime(uidState, |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 4414 | uidState, flags); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4415 | final long accessDuration = attribution.getLastDuration( |
| 4416 | uidState, uidState, flags); |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 4417 | // Proxy information for rejections is not backed up |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4418 | final OpEventProxyInfo proxy = attribution.getLastProxyInfo( |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 4419 | uidState, uidState, flags); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4420 | |
| 4421 | if (accessTime <= 0 && rejectTime <= 0 && accessDuration <= 0 |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 4422 | && proxy == null) { |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4423 | continue; |
| 4424 | } |
| 4425 | |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 4426 | String proxyPkg = null; |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4427 | String proxyAttributionTag = null; |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 4428 | int proxyUid = Process.INVALID_UID; |
| 4429 | if (proxy != null) { |
| 4430 | proxyPkg = proxy.getPackageName(); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4431 | proxyAttributionTag = proxy.getAttributionTag(); |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 4432 | proxyUid = proxy.getUid(); |
| 4433 | } |
| 4434 | |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4435 | out.startTag(null, "st"); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4436 | if (attributionTag != null) { |
| 4437 | out.attribute(null, "id", attributionTag); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4438 | } |
| 4439 | out.attribute(null, "n", Long.toString(key)); |
| 4440 | if (accessTime > 0) { |
| 4441 | out.attribute(null, "t", Long.toString(accessTime)); |
| 4442 | } |
| 4443 | if (rejectTime > 0) { |
| 4444 | out.attribute(null, "r", Long.toString(rejectTime)); |
| 4445 | } |
| 4446 | if (accessDuration > 0) { |
| 4447 | out.attribute(null, "d", Long.toString(accessDuration)); |
| 4448 | } |
| 4449 | if (proxyPkg != null) { |
| 4450 | out.attribute(null, "pp", proxyPkg); |
| 4451 | } |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4452 | if (proxyAttributionTag != null) { |
| 4453 | out.attribute(null, "pc", proxyAttributionTag); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4454 | } |
| 4455 | if (proxyUid >= 0) { |
| 4456 | out.attribute(null, "pu", Integer.toString(proxyUid)); |
| 4457 | } |
| 4458 | out.endTag(null, "st"); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 4459 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 4460 | } |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 4461 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 4462 | out.endTag(null, "op"); |
| 4463 | } |
| 4464 | out.endTag(null, "uid"); |
| 4465 | } |
| 4466 | if (lastPkg != null) { |
| 4467 | out.endTag(null, "pkg"); |
| 4468 | } |
| 4469 | } |
| 4470 | |
| 4471 | out.endTag(null, "app-ops"); |
| 4472 | out.endDocument(); |
| 4473 | mFile.finishWrite(stream); |
| 4474 | } catch (IOException e) { |
| 4475 | Slog.w(TAG, "Failed to write state, restoring backup.", e); |
| 4476 | mFile.failWrite(stream); |
| 4477 | } |
| 4478 | } |
| 4479 | } |
| 4480 | |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4481 | static class Shell extends ShellCommand { |
| 4482 | final IAppOpsService mInterface; |
| 4483 | final AppOpsService mInternal; |
| 4484 | |
| 4485 | int userId = UserHandle.USER_SYSTEM; |
| 4486 | String packageName; |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4487 | String attributionTag; |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4488 | String opStr; |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 4489 | String modeStr; |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4490 | int op; |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 4491 | int mode; |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4492 | int packageUid; |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 4493 | int nonpackageUid; |
Julia Reynolds | 6cb5fcc | 2018-02-27 17:33:52 -0500 | [diff] [blame] | 4494 | final static Binder sBinder = new Binder(); |
| 4495 | IBinder mToken; |
Svet Ganov | d563e93 | 2019-04-14 13:07:41 -0700 | [diff] [blame] | 4496 | boolean targetsUid; |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4497 | |
| 4498 | Shell(IAppOpsService iface, AppOpsService internal) { |
| 4499 | mInterface = iface; |
| 4500 | mInternal = internal; |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 4501 | mToken = AppOpsManager.getClientId(); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4502 | } |
| 4503 | |
| 4504 | @Override |
| 4505 | public int onCommand(String cmd) { |
| 4506 | return onShellCommand(this, cmd); |
| 4507 | } |
| 4508 | |
| 4509 | @Override |
| 4510 | public void onHelp() { |
| 4511 | PrintWriter pw = getOutPrintWriter(); |
| 4512 | dumpCommandHelp(pw); |
| 4513 | } |
| 4514 | |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 4515 | static private int strOpToOp(String op, PrintWriter err) { |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4516 | try { |
| 4517 | return AppOpsManager.strOpToOp(op); |
| 4518 | } catch (IllegalArgumentException e) { |
| 4519 | } |
| 4520 | try { |
| 4521 | return Integer.parseInt(op); |
| 4522 | } catch (NumberFormatException e) { |
| 4523 | } |
| 4524 | try { |
| 4525 | return AppOpsManager.strDebugOpToOp(op); |
| 4526 | } catch (IllegalArgumentException e) { |
| 4527 | err.println("Error: " + e.getMessage()); |
| 4528 | return -1; |
| 4529 | } |
| 4530 | } |
| 4531 | |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 4532 | static int strModeToMode(String modeStr, PrintWriter err) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 4533 | for (int i = AppOpsManager.MODE_NAMES.length - 1; i >= 0; i--) { |
| 4534 | if (AppOpsManager.MODE_NAMES[i].equals(modeStr)) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 4535 | return i; |
| 4536 | } |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 4537 | } |
| 4538 | try { |
| 4539 | return Integer.parseInt(modeStr); |
| 4540 | } catch (NumberFormatException e) { |
| 4541 | } |
| 4542 | err.println("Error: Mode " + modeStr + " is not valid"); |
| 4543 | return -1; |
| 4544 | } |
| 4545 | |
| 4546 | int parseUserOpMode(int defMode, PrintWriter err) throws RemoteException { |
| 4547 | userId = UserHandle.USER_CURRENT; |
| 4548 | opStr = null; |
| 4549 | modeStr = null; |
| 4550 | for (String argument; (argument = getNextArg()) != null;) { |
| 4551 | if ("--user".equals(argument)) { |
| 4552 | userId = UserHandle.parseUserArg(getNextArgRequired()); |
| 4553 | } else { |
| 4554 | if (opStr == null) { |
| 4555 | opStr = argument; |
| 4556 | } else if (modeStr == null) { |
| 4557 | modeStr = argument; |
| 4558 | break; |
| 4559 | } |
| 4560 | } |
| 4561 | } |
| 4562 | if (opStr == null) { |
| 4563 | err.println("Error: Operation not specified."); |
| 4564 | return -1; |
| 4565 | } |
| 4566 | op = strOpToOp(opStr, err); |
| 4567 | if (op < 0) { |
| 4568 | return -1; |
| 4569 | } |
| 4570 | if (modeStr != null) { |
| 4571 | if ((mode=strModeToMode(modeStr, err)) < 0) { |
| 4572 | return -1; |
| 4573 | } |
| 4574 | } else { |
| 4575 | mode = defMode; |
| 4576 | } |
| 4577 | return 0; |
| 4578 | } |
| 4579 | |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4580 | int parseUserPackageOp(boolean reqOp, PrintWriter err) throws RemoteException { |
| 4581 | userId = UserHandle.USER_CURRENT; |
| 4582 | packageName = null; |
| 4583 | opStr = null; |
| 4584 | for (String argument; (argument = getNextArg()) != null;) { |
| 4585 | if ("--user".equals(argument)) { |
| 4586 | userId = UserHandle.parseUserArg(getNextArgRequired()); |
Svet Ganov | d563e93 | 2019-04-14 13:07:41 -0700 | [diff] [blame] | 4587 | } else if ("--uid".equals(argument)) { |
| 4588 | targetsUid = true; |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4589 | } else if ("--attribution".equals(argument)) { |
| 4590 | attributionTag = getNextArgRequired(); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4591 | } else { |
| 4592 | if (packageName == null) { |
| 4593 | packageName = argument; |
| 4594 | } else if (opStr == null) { |
| 4595 | opStr = argument; |
| 4596 | break; |
| 4597 | } |
| 4598 | } |
| 4599 | } |
| 4600 | if (packageName == null) { |
| 4601 | err.println("Error: Package name not specified."); |
| 4602 | return -1; |
| 4603 | } else if (opStr == null && reqOp) { |
| 4604 | err.println("Error: Operation not specified."); |
| 4605 | return -1; |
| 4606 | } |
| 4607 | if (opStr != null) { |
| 4608 | op = strOpToOp(opStr, err); |
| 4609 | if (op < 0) { |
| 4610 | return -1; |
| 4611 | } |
| 4612 | } else { |
| 4613 | op = AppOpsManager.OP_NONE; |
| 4614 | } |
| 4615 | if (userId == UserHandle.USER_CURRENT) { |
| 4616 | userId = ActivityManager.getCurrentUser(); |
| 4617 | } |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 4618 | nonpackageUid = -1; |
| 4619 | try { |
| 4620 | nonpackageUid = Integer.parseInt(packageName); |
| 4621 | } catch (NumberFormatException e) { |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4622 | } |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 4623 | if (nonpackageUid == -1 && packageName.length() > 1 && packageName.charAt(0) == 'u' |
| 4624 | && packageName.indexOf('.') < 0) { |
| 4625 | int i = 1; |
| 4626 | while (i < packageName.length() && packageName.charAt(i) >= '0' |
| 4627 | && packageName.charAt(i) <= '9') { |
| 4628 | i++; |
| 4629 | } |
| 4630 | if (i > 1 && i < packageName.length()) { |
| 4631 | String userStr = packageName.substring(1, i); |
| 4632 | try { |
| 4633 | int user = Integer.parseInt(userStr); |
| 4634 | char type = packageName.charAt(i); |
| 4635 | i++; |
| 4636 | int startTypeVal = i; |
| 4637 | while (i < packageName.length() && packageName.charAt(i) >= '0' |
| 4638 | && packageName.charAt(i) <= '9') { |
| 4639 | i++; |
| 4640 | } |
| 4641 | if (i > startTypeVal) { |
| 4642 | String typeValStr = packageName.substring(startTypeVal, i); |
| 4643 | try { |
| 4644 | int typeVal = Integer.parseInt(typeValStr); |
| 4645 | if (type == 'a') { |
| 4646 | nonpackageUid = UserHandle.getUid(user, |
| 4647 | typeVal + Process.FIRST_APPLICATION_UID); |
| 4648 | } else if (type == 's') { |
| 4649 | nonpackageUid = UserHandle.getUid(user, typeVal); |
| 4650 | } |
| 4651 | } catch (NumberFormatException e) { |
| 4652 | } |
| 4653 | } |
| 4654 | } catch (NumberFormatException e) { |
| 4655 | } |
| 4656 | } |
| 4657 | } |
| 4658 | if (nonpackageUid != -1) { |
| 4659 | packageName = null; |
| 4660 | } else { |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 4661 | packageUid = resolveUid(packageName); |
| 4662 | if (packageUid < 0) { |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 4663 | packageUid = AppGlobals.getPackageManager().getPackageUid(packageName, |
| 4664 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 4665 | } |
| 4666 | if (packageUid < 0) { |
| 4667 | err.println("Error: No UID for " + packageName + " in user " + userId); |
| 4668 | return -1; |
| 4669 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4670 | } |
| 4671 | return 0; |
| 4672 | } |
| 4673 | } |
| 4674 | |
| 4675 | @Override public void onShellCommand(FileDescriptor in, FileDescriptor out, |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 4676 | FileDescriptor err, String[] args, ShellCallback callback, |
| 4677 | ResultReceiver resultReceiver) { |
| 4678 | (new Shell(this, this)).exec(this, in, out, err, args, callback, resultReceiver); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4679 | } |
| 4680 | |
| 4681 | static void dumpCommandHelp(PrintWriter pw) { |
| 4682 | pw.println("AppOps service (appops) commands:"); |
| 4683 | pw.println(" help"); |
| 4684 | pw.println(" Print this help text."); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4685 | pw.println(" start [--user <USER_ID>] [--attribution <ATTRIBUTION_TAG>] <PACKAGE | UID> " |
| 4686 | + "<OP> "); |
Julia Reynolds | 6cb5fcc | 2018-02-27 17:33:52 -0500 | [diff] [blame] | 4687 | pw.println(" Starts a given operation for a particular application."); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4688 | pw.println(" stop [--user <USER_ID>] [--attribution <ATTRIBUTION_TAG>] <PACKAGE | UID> " |
| 4689 | + "<OP> "); |
Julia Reynolds | 6cb5fcc | 2018-02-27 17:33:52 -0500 | [diff] [blame] | 4690 | pw.println(" Stops a given operation for a particular application."); |
Svet Ganov | b687fad | 2019-04-30 17:32:44 -0700 | [diff] [blame] | 4691 | pw.println(" set [--user <USER_ID>] <[--uid] PACKAGE | UID> <OP> <MODE>"); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4692 | pw.println(" Set the mode for a particular application and operation."); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4693 | pw.println(" get [--user <USER_ID>] [--attribution <ATTRIBUTION_TAG>] <PACKAGE | UID> " |
| 4694 | + "[<OP>]"); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4695 | pw.println(" Return the mode for a particular application and optional operation."); |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 4696 | pw.println(" query-op [--user <USER_ID>] <OP> [<MODE>]"); |
| 4697 | pw.println(" Print all packages that currently have the given op in the given mode."); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4698 | pw.println(" reset [--user <USER_ID>] [<PACKAGE>]"); |
| 4699 | pw.println(" Reset the given application or all applications to default modes."); |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 4700 | pw.println(" write-settings"); |
| 4701 | pw.println(" Immediately write pending changes to storage."); |
| 4702 | pw.println(" read-settings"); |
| 4703 | pw.println(" Read the last written settings, replacing current state in RAM."); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4704 | pw.println(" options:"); |
Svet Ganov | b687fad | 2019-04-30 17:32:44 -0700 | [diff] [blame] | 4705 | pw.println(" <PACKAGE> an Android package name or its UID if prefixed by --uid"); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4706 | pw.println(" <OP> an AppOps operation."); |
| 4707 | pw.println(" <MODE> one of allow, ignore, deny, or default"); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4708 | pw.println(" <USER_ID> the user id under which the package is installed. If --user is"); |
| 4709 | pw.println(" not specified, the current user is assumed."); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | static int onShellCommand(Shell shell, String cmd) { |
| 4713 | if (cmd == null) { |
| 4714 | return shell.handleDefaultCommands(cmd); |
| 4715 | } |
| 4716 | PrintWriter pw = shell.getOutPrintWriter(); |
| 4717 | PrintWriter err = shell.getErrPrintWriter(); |
| 4718 | try { |
| 4719 | switch (cmd) { |
| 4720 | case "set": { |
| 4721 | int res = shell.parseUserPackageOp(true, err); |
| 4722 | if (res < 0) { |
| 4723 | return res; |
| 4724 | } |
| 4725 | String modeStr = shell.getNextArg(); |
| 4726 | if (modeStr == null) { |
| 4727 | err.println("Error: Mode not specified."); |
| 4728 | return -1; |
| 4729 | } |
| 4730 | |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 4731 | final int mode = shell.strModeToMode(modeStr, err); |
| 4732 | if (mode < 0) { |
| 4733 | return -1; |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4734 | } |
| 4735 | |
Svet Ganov | d563e93 | 2019-04-14 13:07:41 -0700 | [diff] [blame] | 4736 | if (!shell.targetsUid && shell.packageName != null) { |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 4737 | shell.mInterface.setMode(shell.op, shell.packageUid, shell.packageName, |
| 4738 | mode); |
Svet Ganov | d563e93 | 2019-04-14 13:07:41 -0700 | [diff] [blame] | 4739 | } else if (shell.targetsUid && shell.packageName != null) { |
| 4740 | try { |
| 4741 | final int uid = shell.mInternal.mContext.getPackageManager() |
Abhijeet Kaur | 62477e5 | 2020-01-29 18:55:49 +0000 | [diff] [blame] | 4742 | .getPackageUidAsUser(shell.packageName, shell.userId); |
Svet Ganov | d563e93 | 2019-04-14 13:07:41 -0700 | [diff] [blame] | 4743 | shell.mInterface.setUidMode(shell.op, uid, mode); |
| 4744 | } catch (PackageManager.NameNotFoundException e) { |
| 4745 | return -1; |
| 4746 | } |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 4747 | } else { |
| 4748 | shell.mInterface.setUidMode(shell.op, shell.nonpackageUid, mode); |
| 4749 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4750 | return 0; |
| 4751 | } |
| 4752 | case "get": { |
| 4753 | int res = shell.parseUserPackageOp(false, err); |
| 4754 | if (res < 0) { |
| 4755 | return res; |
| 4756 | } |
| 4757 | |
Eugene Susla | e4ee2c2 | 2018-11-05 12:23:30 -0800 | [diff] [blame] | 4758 | List<AppOpsManager.PackageOps> ops = new ArrayList<>(); |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 4759 | if (shell.packageName != null) { |
Eugene Susla | e4ee2c2 | 2018-11-05 12:23:30 -0800 | [diff] [blame] | 4760 | // Uid mode overrides package mode, so make sure it's also reported |
| 4761 | List<AppOpsManager.PackageOps> r = shell.mInterface.getUidOps( |
| 4762 | shell.packageUid, |
| 4763 | shell.op != AppOpsManager.OP_NONE ? new int[]{shell.op} : null); |
| 4764 | if (r != null) { |
| 4765 | ops.addAll(r); |
| 4766 | } |
| 4767 | r = shell.mInterface.getOpsForPackage( |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 4768 | shell.packageUid, shell.packageName, |
| 4769 | shell.op != AppOpsManager.OP_NONE ? new int[]{shell.op} : null); |
Eugene Susla | e4ee2c2 | 2018-11-05 12:23:30 -0800 | [diff] [blame] | 4770 | if (r != null) { |
| 4771 | ops.addAll(r); |
| 4772 | } |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 4773 | } else { |
| 4774 | ops = shell.mInterface.getUidOps( |
| 4775 | shell.nonpackageUid, |
| 4776 | shell.op != AppOpsManager.OP_NONE ? new int[]{shell.op} : null); |
| 4777 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4778 | if (ops == null || ops.size() <= 0) { |
| 4779 | pw.println("No operations."); |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 4780 | if (shell.op > AppOpsManager.OP_NONE && shell.op < AppOpsManager._NUM_OP) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 4781 | pw.println("Default mode: " + AppOpsManager.modeToName( |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 4782 | AppOpsManager.opToDefaultMode(shell.op))); |
| 4783 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4784 | return 0; |
| 4785 | } |
| 4786 | final long now = System.currentTimeMillis(); |
| 4787 | for (int i=0; i<ops.size(); i++) { |
Eugene Susla | e4ee2c2 | 2018-11-05 12:23:30 -0800 | [diff] [blame] | 4788 | AppOpsManager.PackageOps packageOps = ops.get(i); |
| 4789 | if (packageOps.getPackageName() == null) { |
| 4790 | pw.print("Uid mode: "); |
| 4791 | } |
| 4792 | List<AppOpsManager.OpEntry> entries = packageOps.getOps(); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4793 | for (int j=0; j<entries.size(); j++) { |
| 4794 | AppOpsManager.OpEntry ent = entries.get(j); |
| 4795 | pw.print(AppOpsManager.opToName(ent.getOp())); |
| 4796 | pw.print(": "); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 4797 | pw.print(AppOpsManager.modeToName(ent.getMode())); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4798 | if (shell.attributionTag == null) { |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 4799 | if (ent.getLastAccessTime(OP_FLAGS_ALL) != -1) { |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4800 | pw.print("; time="); |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 4801 | TimeUtils.formatDuration( |
| 4802 | now - ent.getLastAccessTime(OP_FLAGS_ALL), pw); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4803 | pw.print(" ago"); |
| 4804 | } |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 4805 | if (ent.getLastRejectTime(OP_FLAGS_ALL) != -1) { |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4806 | pw.print("; rejectTime="); |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 4807 | TimeUtils.formatDuration( |
| 4808 | now - ent.getLastRejectTime(OP_FLAGS_ALL), pw); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4809 | pw.print(" ago"); |
| 4810 | } |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 4811 | if (ent.isRunning()) { |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4812 | pw.print(" (running)"); |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 4813 | } else if (ent.getLastDuration(OP_FLAGS_ALL) != -1) { |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4814 | pw.print("; duration="); |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 4815 | TimeUtils.formatDuration(ent.getLastDuration(OP_FLAGS_ALL), pw); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4816 | } |
| 4817 | } else { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4818 | final AppOpsManager.AttributedOpEntry attributionEnt = |
| 4819 | ent.getAttributedOpEntries().get(shell.attributionTag); |
| 4820 | if (attributionEnt != null) { |
| 4821 | if (attributionEnt.getLastAccessTime(OP_FLAGS_ALL) != -1) { |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4822 | pw.print("; time="); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4823 | TimeUtils.formatDuration( |
| 4824 | now - attributionEnt.getLastAccessTime( |
| 4825 | OP_FLAGS_ALL), pw); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4826 | pw.print(" ago"); |
| 4827 | } |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4828 | if (attributionEnt.getLastRejectTime(OP_FLAGS_ALL) != -1) { |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4829 | pw.print("; rejectTime="); |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 4830 | TimeUtils.formatDuration( |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4831 | now - attributionEnt.getLastRejectTime( |
| 4832 | OP_FLAGS_ALL), pw); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4833 | pw.print(" ago"); |
| 4834 | } |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4835 | if (attributionEnt.isRunning()) { |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4836 | pw.print(" (running)"); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4837 | } else if (attributionEnt.getLastDuration(OP_FLAGS_ALL) |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 4838 | != -1) { |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4839 | pw.print("; duration="); |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 4840 | TimeUtils.formatDuration( |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4841 | attributionEnt.getLastDuration(OP_FLAGS_ALL), pw); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4842 | } |
| 4843 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4844 | } |
| 4845 | pw.println(); |
| 4846 | } |
| 4847 | } |
| 4848 | return 0; |
| 4849 | } |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 4850 | case "query-op": { |
| 4851 | int res = shell.parseUserOpMode(AppOpsManager.MODE_IGNORED, err); |
| 4852 | if (res < 0) { |
| 4853 | return res; |
| 4854 | } |
| 4855 | List<AppOpsManager.PackageOps> ops = shell.mInterface.getPackagesForOps( |
| 4856 | new int[] {shell.op}); |
| 4857 | if (ops == null || ops.size() <= 0) { |
| 4858 | pw.println("No operations."); |
| 4859 | return 0; |
| 4860 | } |
| 4861 | for (int i=0; i<ops.size(); i++) { |
| 4862 | final AppOpsManager.PackageOps pkg = ops.get(i); |
| 4863 | boolean hasMatch = false; |
| 4864 | final List<AppOpsManager.OpEntry> entries = ops.get(i).getOps(); |
| 4865 | for (int j=0; j<entries.size(); j++) { |
| 4866 | AppOpsManager.OpEntry ent = entries.get(j); |
| 4867 | if (ent.getOp() == shell.op && ent.getMode() == shell.mode) { |
| 4868 | hasMatch = true; |
| 4869 | break; |
| 4870 | } |
| 4871 | } |
| 4872 | if (hasMatch) { |
| 4873 | pw.println(pkg.getPackageName()); |
| 4874 | } |
| 4875 | } |
| 4876 | return 0; |
| 4877 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4878 | case "reset": { |
| 4879 | String packageName = null; |
| 4880 | int userId = UserHandle.USER_CURRENT; |
| 4881 | for (String argument; (argument = shell.getNextArg()) != null;) { |
| 4882 | if ("--user".equals(argument)) { |
| 4883 | String userStr = shell.getNextArgRequired(); |
| 4884 | userId = UserHandle.parseUserArg(userStr); |
| 4885 | } else { |
| 4886 | if (packageName == null) { |
| 4887 | packageName = argument; |
| 4888 | } else { |
| 4889 | err.println("Error: Unsupported argument: " + argument); |
| 4890 | return -1; |
| 4891 | } |
| 4892 | } |
| 4893 | } |
| 4894 | |
| 4895 | if (userId == UserHandle.USER_CURRENT) { |
| 4896 | userId = ActivityManager.getCurrentUser(); |
| 4897 | } |
| 4898 | |
| 4899 | shell.mInterface.resetAllModes(userId, packageName); |
| 4900 | pw.print("Reset all modes for: "); |
| 4901 | if (userId == UserHandle.USER_ALL) { |
| 4902 | pw.print("all users"); |
| 4903 | } else { |
| 4904 | pw.print("user "); pw.print(userId); |
| 4905 | } |
| 4906 | pw.print(", "); |
| 4907 | if (packageName == null) { |
| 4908 | pw.println("all packages"); |
| 4909 | } else { |
| 4910 | pw.print("package "); pw.println(packageName); |
| 4911 | } |
| 4912 | return 0; |
| 4913 | } |
| 4914 | case "write-settings": { |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 4915 | shell.mInternal.enforceManageAppOpsModes(Binder.getCallingPid(), |
| 4916 | Binder.getCallingUid(), -1); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4917 | long token = Binder.clearCallingIdentity(); |
| 4918 | try { |
| 4919 | synchronized (shell.mInternal) { |
| 4920 | shell.mInternal.mHandler.removeCallbacks(shell.mInternal.mWriteRunner); |
| 4921 | } |
| 4922 | shell.mInternal.writeState(); |
| 4923 | pw.println("Current settings written."); |
| 4924 | } finally { |
| 4925 | Binder.restoreCallingIdentity(token); |
| 4926 | } |
| 4927 | return 0; |
| 4928 | } |
| 4929 | case "read-settings": { |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 4930 | shell.mInternal.enforceManageAppOpsModes(Binder.getCallingPid(), |
| 4931 | Binder.getCallingUid(), -1); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4932 | long token = Binder.clearCallingIdentity(); |
| 4933 | try { |
| 4934 | shell.mInternal.readState(); |
| 4935 | pw.println("Last settings read."); |
| 4936 | } finally { |
| 4937 | Binder.restoreCallingIdentity(token); |
| 4938 | } |
| 4939 | return 0; |
| 4940 | } |
Julia Reynolds | 6cb5fcc | 2018-02-27 17:33:52 -0500 | [diff] [blame] | 4941 | case "start": { |
| 4942 | int res = shell.parseUserPackageOp(true, err); |
| 4943 | if (res < 0) { |
| 4944 | return res; |
| 4945 | } |
| 4946 | |
| 4947 | if (shell.packageName != null) { |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 4948 | shell.mInterface.startOperation(shell.mToken, shell.op, shell.packageUid, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4949 | shell.packageName, shell.attributionTag, true, true, |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 4950 | "appops start shell command"); |
Julia Reynolds | 6cb5fcc | 2018-02-27 17:33:52 -0500 | [diff] [blame] | 4951 | } else { |
| 4952 | return -1; |
| 4953 | } |
| 4954 | return 0; |
| 4955 | } |
| 4956 | case "stop": { |
| 4957 | int res = shell.parseUserPackageOp(true, err); |
| 4958 | if (res < 0) { |
| 4959 | return res; |
| 4960 | } |
| 4961 | |
| 4962 | if (shell.packageName != null) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4963 | shell.mInterface.finishOperation(shell.mToken, shell.op, shell.packageUid, |
| 4964 | shell.packageName, shell.attributionTag); |
Julia Reynolds | 6cb5fcc | 2018-02-27 17:33:52 -0500 | [diff] [blame] | 4965 | } else { |
| 4966 | return -1; |
| 4967 | } |
| 4968 | return 0; |
| 4969 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 4970 | default: |
| 4971 | return shell.handleDefaultCommands(cmd); |
| 4972 | } |
| 4973 | } catch (RemoteException e) { |
| 4974 | pw.println("Remote exception: " + e); |
| 4975 | } |
| 4976 | return -1; |
| 4977 | } |
| 4978 | |
| 4979 | private void dumpHelp(PrintWriter pw) { |
| 4980 | pw.println("AppOps service (appops) dump options:"); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 4981 | pw.println(" -h"); |
| 4982 | pw.println(" Print this help text."); |
| 4983 | pw.println(" --op [OP]"); |
| 4984 | pw.println(" Limit output to data associated with the given app op code."); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 4985 | pw.println(" --mode [MODE]"); |
| 4986 | pw.println(" Limit output to data associated with the given app op mode."); |
| 4987 | pw.println(" --package [PACKAGE]"); |
| 4988 | pw.println(" Limit output to data associated with the given package name."); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4989 | pw.println(" --attributionTag [attributionTag]"); |
| 4990 | pw.println(" Limit output to data associated with the given attribution tag."); |
Dianne Hackborn | 125dc53 | 2019-01-09 13:31:48 -0800 | [diff] [blame] | 4991 | pw.println(" --watchers"); |
| 4992 | pw.println(" Only output the watcher sections."); |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 4993 | } |
| 4994 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4995 | private void dumpStatesLocked(@NonNull PrintWriter pw, @Nullable String filterAttributionTag, |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 4996 | @HistoricalOpsRequestFilter int filter, long nowElapsed, @NonNull Op op, long now, |
| 4997 | @NonNull SimpleDateFormat sdf, @NonNull Date date, @NonNull String prefix) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 4998 | final int numAttributions = op.mAttributions.size(); |
| 4999 | for (int i = 0; i < numAttributions; i++) { |
| 5000 | if ((filter & FILTER_BY_ATTRIBUTION_TAG) != 0 && !Objects.equals( |
| 5001 | op.mAttributions.keyAt(i), filterAttributionTag)) { |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 5002 | continue; |
| 5003 | } |
| 5004 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5005 | pw.print(prefix + op.mAttributions.keyAt(i) + "=[\n"); |
| 5006 | dumpStatesLocked(pw, nowElapsed, op, op.mAttributions.keyAt(i), now, sdf, date, |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 5007 | prefix + " "); |
| 5008 | pw.print(prefix + "]\n"); |
| 5009 | } |
| 5010 | } |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5011 | |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 5012 | private void dumpStatesLocked(@NonNull PrintWriter pw, long nowElapsed, @NonNull Op op, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5013 | @Nullable String attributionTag, long now, @NonNull SimpleDateFormat sdf, |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 5014 | @NonNull Date date, @NonNull String prefix) { |
| 5015 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5016 | final AttributedOpEntry entry = op.createSingleAttributionEntryLocked( |
| 5017 | attributionTag).getAttributedOpEntries().get(attributionTag); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5018 | |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 5019 | final ArraySet<Long> keys = entry.collectKeys(); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5020 | |
| 5021 | final int keyCount = keys.size(); |
| 5022 | for (int k = 0; k < keyCount; k++) { |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 5023 | final long key = keys.valueAt(k); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5024 | |
| 5025 | final int uidState = AppOpsManager.extractUidStateFromKey(key); |
| 5026 | final int flags = AppOpsManager.extractFlagsFromKey(key); |
| 5027 | |
Philip P. Moltmann | 49bd9e1 | 2019-11-26 15:18:16 -0800 | [diff] [blame] | 5028 | final long accessTime = entry.getLastAccessTime(uidState, uidState, flags); |
| 5029 | final long rejectTime = entry.getLastRejectTime(uidState, uidState, flags); |
| 5030 | final long accessDuration = entry.getLastDuration(uidState, uidState, flags); |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 5031 | final OpEventProxyInfo proxy = entry.getLastProxyInfo(uidState, uidState, flags); |
| 5032 | |
| 5033 | String proxyPkg = null; |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5034 | String proxyAttributionTag = null; |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 5035 | int proxyUid = Process.INVALID_UID; |
| 5036 | if (proxy != null) { |
| 5037 | proxyPkg = proxy.getPackageName(); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5038 | proxyAttributionTag = proxy.getAttributionTag(); |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 5039 | proxyUid = proxy.getUid(); |
| 5040 | } |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5041 | |
| 5042 | if (accessTime > 0) { |
| 5043 | pw.print(prefix); |
| 5044 | pw.print("Access: "); |
| 5045 | pw.print(AppOpsManager.keyToString(key)); |
| 5046 | pw.print(" "); |
| 5047 | date.setTime(accessTime); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 5048 | pw.print(sdf.format(date)); |
| 5049 | pw.print(" ("); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5050 | TimeUtils.formatDuration(accessTime - now, pw); |
| 5051 | pw.print(")"); |
| 5052 | if (accessDuration > 0) { |
| 5053 | pw.print(" duration="); |
| 5054 | TimeUtils.formatDuration(accessDuration, pw); |
| 5055 | } |
| 5056 | if (proxyUid >= 0) { |
| 5057 | pw.print(" proxy["); |
| 5058 | pw.print("uid="); |
| 5059 | pw.print(proxyUid); |
| 5060 | pw.print(", pkg="); |
| 5061 | pw.print(proxyPkg); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5062 | pw.print(", attributionTag="); |
| 5063 | pw.print(proxyAttributionTag); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5064 | pw.print("]"); |
| 5065 | } |
| 5066 | pw.println(); |
| 5067 | } |
| 5068 | |
| 5069 | if (rejectTime > 0) { |
| 5070 | pw.print(prefix); |
| 5071 | pw.print("Reject: "); |
| 5072 | pw.print(AppOpsManager.keyToString(key)); |
| 5073 | date.setTime(rejectTime); |
| 5074 | pw.print(sdf.format(date)); |
| 5075 | pw.print(" ("); |
| 5076 | TimeUtils.formatDuration(rejectTime - now, pw); |
| 5077 | pw.print(")"); |
| 5078 | if (proxyUid >= 0) { |
| 5079 | pw.print(" proxy["); |
| 5080 | pw.print("uid="); |
| 5081 | pw.print(proxyUid); |
| 5082 | pw.print(", pkg="); |
| 5083 | pw.print(proxyPkg); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5084 | pw.print(", attributionTag="); |
| 5085 | pw.print(proxyAttributionTag); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5086 | pw.print("]"); |
| 5087 | } |
| 5088 | pw.println(); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 5089 | } |
| 5090 | } |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 5091 | |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5092 | final AttributedOp attributedOp = op.mAttributions.get(attributionTag); |
| 5093 | if (attributedOp.isRunning()) { |
Philip P. Moltmann | e6ece90 | 2020-01-02 13:31:10 -0800 | [diff] [blame] | 5094 | long earliestElapsedTime = Long.MAX_VALUE; |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 5095 | long maxNumStarts = 0; |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5096 | int numInProgressEvents = attributedOp.mInProgressEvents.size(); |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 5097 | for (int i = 0; i < numInProgressEvents; i++) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5098 | InProgressStartOpEvent event = attributedOp.mInProgressEvents.valueAt(i); |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 5099 | |
Philip P. Moltmann | d8c0bda | 2019-12-19 19:55:35 -0800 | [diff] [blame] | 5100 | earliestElapsedTime = Math.min(earliestElapsedTime, event.getStartElapsedTime()); |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 5101 | maxNumStarts = Math.max(maxNumStarts, event.numUnfinishedStarts); |
| 5102 | } |
| 5103 | |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 5104 | pw.print(prefix + "Running start at: "); |
Philip P. Moltmann | e6ece90 | 2020-01-02 13:31:10 -0800 | [diff] [blame] | 5105 | TimeUtils.formatDuration(nowElapsed - earliestElapsedTime, pw); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 5106 | pw.println(); |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 5107 | |
| 5108 | if (maxNumStarts > 1) { |
| 5109 | pw.print(prefix + "startNesting="); |
| 5110 | pw.println(maxNumStarts); |
| 5111 | } |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 5112 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 5113 | } |
| 5114 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 5115 | @Override |
| 5116 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 6df866a | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 5117 | if (!DumpUtils.checkDumpAndUsageStatsPermission(mContext, TAG, pw)) return; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 5118 | |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 5119 | int dumpOp = OP_NONE; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5120 | String dumpPackage = null; |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5121 | String dumpAttributionTag = null; |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 5122 | int dumpUid = Process.INVALID_UID; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5123 | int dumpMode = -1; |
Dianne Hackborn | 125dc53 | 2019-01-09 13:31:48 -0800 | [diff] [blame] | 5124 | boolean dumpWatchers = false; |
Nate Myren | 697650b | 2020-01-23 13:25:06 -0800 | [diff] [blame] | 5125 | // TODO ntmyren: Remove the dumpHistory and dumpFilter |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5126 | boolean dumpHistory = false; |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 5127 | @HistoricalOpsRequestFilter int dumpFilter = 0; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5128 | |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 5129 | if (args != null) { |
| 5130 | for (int i=0; i<args.length; i++) { |
| 5131 | String arg = args[i]; |
| 5132 | if ("-h".equals(arg)) { |
| 5133 | dumpHelp(pw); |
| 5134 | return; |
Tim Kilbourn | 8f1ea83 | 2015-08-26 15:07:37 -0700 | [diff] [blame] | 5135 | } else if ("-a".equals(arg)) { |
| 5136 | // dump all data |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5137 | } else if ("--op".equals(arg)) { |
| 5138 | i++; |
| 5139 | if (i >= args.length) { |
| 5140 | pw.println("No argument for --op option"); |
| 5141 | return; |
| 5142 | } |
| 5143 | dumpOp = Shell.strOpToOp(args[i], pw); |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 5144 | dumpFilter |= FILTER_BY_OP_NAMES; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5145 | if (dumpOp < 0) { |
| 5146 | return; |
| 5147 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5148 | } else if ("--package".equals(arg)) { |
| 5149 | i++; |
| 5150 | if (i >= args.length) { |
| 5151 | pw.println("No argument for --package option"); |
| 5152 | return; |
| 5153 | } |
| 5154 | dumpPackage = args[i]; |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 5155 | dumpFilter |= FILTER_BY_PACKAGE_NAME; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5156 | try { |
| 5157 | dumpUid = AppGlobals.getPackageManager().getPackageUid(dumpPackage, |
| 5158 | PackageManager.MATCH_KNOWN_PACKAGES | PackageManager.MATCH_INSTANT, |
| 5159 | 0); |
| 5160 | } catch (RemoteException e) { |
| 5161 | } |
| 5162 | if (dumpUid < 0) { |
| 5163 | pw.println("Unknown package: " + dumpPackage); |
| 5164 | return; |
| 5165 | } |
| 5166 | dumpUid = UserHandle.getAppId(dumpUid); |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 5167 | dumpFilter |= FILTER_BY_UID; |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5168 | } else if ("--attributionTag".equals(arg)) { |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 5169 | i++; |
| 5170 | if (i >= args.length) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5171 | pw.println("No argument for --attributionTag option"); |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 5172 | return; |
| 5173 | } |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5174 | dumpAttributionTag = args[i]; |
| 5175 | dumpFilter |= FILTER_BY_ATTRIBUTION_TAG; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5176 | } else if ("--mode".equals(arg)) { |
| 5177 | i++; |
| 5178 | if (i >= args.length) { |
| 5179 | pw.println("No argument for --mode option"); |
| 5180 | return; |
| 5181 | } |
| 5182 | dumpMode = Shell.strModeToMode(args[i], pw); |
| 5183 | if (dumpMode < 0) { |
| 5184 | return; |
| 5185 | } |
Dianne Hackborn | 125dc53 | 2019-01-09 13:31:48 -0800 | [diff] [blame] | 5186 | } else if ("--watchers".equals(arg)) { |
| 5187 | dumpWatchers = true; |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 5188 | } else if (arg.length() > 0 && arg.charAt(0) == '-'){ |
| 5189 | pw.println("Unknown option: " + arg); |
| 5190 | return; |
| 5191 | } else { |
| 5192 | pw.println("Unknown command: " + arg); |
| 5193 | return; |
| 5194 | } |
| 5195 | } |
| 5196 | } |
| 5197 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 5198 | synchronized (this) { |
| 5199 | pw.println("Current AppOps Service state:"); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5200 | if (!dumpHistory && !dumpWatchers) { |
| 5201 | mConstants.dump(pw); |
| 5202 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5203 | pw.println(); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 5204 | final long now = System.currentTimeMillis(); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 5205 | final long nowElapsed = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5206 | final long nowUptime = SystemClock.uptimeMillis(); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 5207 | final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); |
| 5208 | final Date date = new Date(); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 5209 | boolean needSep = false; |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 5210 | if (dumpFilter == 0 && dumpMode < 0 && mProfileOwners != null && !dumpWatchers |
| 5211 | && !dumpHistory) { |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 5212 | pw.println(" Profile owners:"); |
| 5213 | for (int poi = 0; poi < mProfileOwners.size(); poi++) { |
| 5214 | pw.print(" User #"); |
| 5215 | pw.print(mProfileOwners.keyAt(poi)); |
| 5216 | pw.print(": "); |
| 5217 | UserHandle.formatUid(pw, mProfileOwners.valueAt(poi)); |
| 5218 | pw.println(); |
| 5219 | } |
| 5220 | pw.println(); |
| 5221 | } |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5222 | if (mOpModeWatchers.size() > 0 && !dumpHistory) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5223 | boolean printedHeader = false; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 5224 | for (int i=0; i<mOpModeWatchers.size(); i++) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5225 | if (dumpOp >= 0 && dumpOp != mOpModeWatchers.keyAt(i)) { |
| 5226 | continue; |
| 5227 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5228 | boolean printedOpHeader = false; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 5229 | ArraySet<ModeCallback> callbacks = mOpModeWatchers.valueAt(i); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 5230 | for (int j=0; j<callbacks.size(); j++) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5231 | final ModeCallback cb = callbacks.valueAt(j); |
Dianne Hackborn | 125dc53 | 2019-01-09 13:31:48 -0800 | [diff] [blame] | 5232 | if (dumpPackage != null |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5233 | && dumpUid != UserHandle.getAppId(cb.mWatchingUid)) { |
| 5234 | continue; |
| 5235 | } |
| 5236 | needSep = true; |
| 5237 | if (!printedHeader) { |
| 5238 | pw.println(" Op mode watchers:"); |
| 5239 | printedHeader = true; |
| 5240 | } |
| 5241 | if (!printedOpHeader) { |
| 5242 | pw.print(" Op "); |
| 5243 | pw.print(AppOpsManager.opToName(mOpModeWatchers.keyAt(i))); |
| 5244 | pw.println(":"); |
| 5245 | printedOpHeader = true; |
| 5246 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 5247 | pw.print(" #"); pw.print(j); pw.print(": "); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5248 | pw.println(cb); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 5249 | } |
| 5250 | } |
| 5251 | } |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5252 | if (mPackageModeWatchers.size() > 0 && dumpOp < 0 && !dumpHistory) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5253 | boolean printedHeader = false; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 5254 | for (int i=0; i<mPackageModeWatchers.size(); i++) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5255 | if (dumpPackage != null && !dumpPackage.equals(mPackageModeWatchers.keyAt(i))) { |
| 5256 | continue; |
| 5257 | } |
| 5258 | needSep = true; |
| 5259 | if (!printedHeader) { |
| 5260 | pw.println(" Package mode watchers:"); |
| 5261 | printedHeader = true; |
| 5262 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 5263 | pw.print(" Pkg "); pw.print(mPackageModeWatchers.keyAt(i)); |
| 5264 | pw.println(":"); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 5265 | ArraySet<ModeCallback> callbacks = mPackageModeWatchers.valueAt(i); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 5266 | for (int j=0; j<callbacks.size(); j++) { |
| 5267 | pw.print(" #"); pw.print(j); pw.print(": "); |
Dianne Hackborn | 68d7655 | 2017-02-27 15:32:03 -0800 | [diff] [blame] | 5268 | pw.println(callbacks.valueAt(j)); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 5269 | } |
| 5270 | } |
| 5271 | } |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5272 | if (mModeWatchers.size() > 0 && dumpOp < 0 && !dumpHistory) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5273 | boolean printedHeader = false; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 5274 | for (int i=0; i<mModeWatchers.size(); i++) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5275 | final ModeCallback cb = mModeWatchers.valueAt(i); |
Dianne Hackborn | 125dc53 | 2019-01-09 13:31:48 -0800 | [diff] [blame] | 5276 | if (dumpPackage != null |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5277 | && dumpUid != UserHandle.getAppId(cb.mWatchingUid)) { |
| 5278 | continue; |
| 5279 | } |
| 5280 | needSep = true; |
| 5281 | if (!printedHeader) { |
| 5282 | pw.println(" All op mode watchers:"); |
| 5283 | printedHeader = true; |
| 5284 | } |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 5285 | pw.print(" "); |
| 5286 | pw.print(Integer.toHexString(System.identityHashCode(mModeWatchers.keyAt(i)))); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5287 | pw.print(": "); pw.println(cb); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 5288 | } |
| 5289 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5290 | if (mActiveWatchers.size() > 0 && dumpMode < 0) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 5291 | needSep = true; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5292 | boolean printedHeader = false; |
Philip P. Moltmann | ba13646 | 2019-05-21 10:20:38 -0700 | [diff] [blame] | 5293 | for (int watcherNum = 0; watcherNum < mActiveWatchers.size(); watcherNum++) { |
| 5294 | final SparseArray<ActiveCallback> activeWatchers = |
| 5295 | mActiveWatchers.valueAt(watcherNum); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 5296 | if (activeWatchers.size() <= 0) { |
| 5297 | continue; |
| 5298 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5299 | final ActiveCallback cb = activeWatchers.valueAt(0); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5300 | if (dumpOp >= 0 && activeWatchers.indexOfKey(dumpOp) < 0) { |
| 5301 | continue; |
| 5302 | } |
Dianne Hackborn | 125dc53 | 2019-01-09 13:31:48 -0800 | [diff] [blame] | 5303 | if (dumpPackage != null |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5304 | && dumpUid != UserHandle.getAppId(cb.mWatchingUid)) { |
| 5305 | continue; |
| 5306 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5307 | if (!printedHeader) { |
| 5308 | pw.println(" All op active watchers:"); |
| 5309 | printedHeader = true; |
| 5310 | } |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 5311 | pw.print(" "); |
| 5312 | pw.print(Integer.toHexString(System.identityHashCode( |
Philip P. Moltmann | ba13646 | 2019-05-21 10:20:38 -0700 | [diff] [blame] | 5313 | mActiveWatchers.keyAt(watcherNum)))); |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 5314 | pw.println(" ->"); |
| 5315 | pw.print(" ["); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 5316 | final int opCount = activeWatchers.size(); |
Philip P. Moltmann | ba13646 | 2019-05-21 10:20:38 -0700 | [diff] [blame] | 5317 | for (int opNum = 0; opNum < opCount; opNum++) { |
| 5318 | if (opNum > 0) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5319 | pw.print(' '); |
| 5320 | } |
Philip P. Moltmann | ba13646 | 2019-05-21 10:20:38 -0700 | [diff] [blame] | 5321 | pw.print(AppOpsManager.opToName(activeWatchers.keyAt(opNum))); |
| 5322 | if (opNum < opCount - 1) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 5323 | pw.print(','); |
| 5324 | } |
| 5325 | } |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 5326 | pw.println("]"); |
| 5327 | pw.print(" "); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5328 | pw.println(cb); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 5329 | } |
| 5330 | } |
Adam Bookatz | 182862e | 2020-04-27 21:58:22 -0700 | [diff] [blame] | 5331 | if (mStartedWatchers.size() > 0 && dumpMode < 0) { |
| 5332 | needSep = true; |
| 5333 | boolean printedHeader = false; |
| 5334 | |
| 5335 | final int watchersSize = mStartedWatchers.size(); |
| 5336 | for (int watcherNum = 0; watcherNum < watchersSize; watcherNum++) { |
| 5337 | final SparseArray<StartedCallback> startedWatchers = |
| 5338 | mStartedWatchers.valueAt(watcherNum); |
| 5339 | if (startedWatchers.size() <= 0) { |
| 5340 | continue; |
| 5341 | } |
| 5342 | |
| 5343 | final StartedCallback cb = startedWatchers.valueAt(0); |
| 5344 | if (dumpOp >= 0 && startedWatchers.indexOfKey(dumpOp) < 0) { |
| 5345 | continue; |
| 5346 | } |
| 5347 | |
| 5348 | if (dumpPackage != null |
| 5349 | && dumpUid != UserHandle.getAppId(cb.mWatchingUid)) { |
| 5350 | continue; |
| 5351 | } |
| 5352 | |
| 5353 | if (!printedHeader) { |
| 5354 | pw.println(" All op started watchers:"); |
| 5355 | printedHeader = true; |
| 5356 | } |
| 5357 | |
| 5358 | pw.print(" "); |
| 5359 | pw.print(Integer.toHexString(System.identityHashCode( |
| 5360 | mStartedWatchers.keyAt(watcherNum)))); |
| 5361 | pw.println(" ->"); |
| 5362 | |
| 5363 | pw.print(" ["); |
| 5364 | final int opCount = startedWatchers.size(); |
| 5365 | for (int opNum = 0; opNum < opCount; opNum++) { |
| 5366 | if (opNum > 0) { |
| 5367 | pw.print(' '); |
| 5368 | } |
| 5369 | |
| 5370 | pw.print(AppOpsManager.opToName(startedWatchers.keyAt(opNum))); |
| 5371 | if (opNum < opCount - 1) { |
| 5372 | pw.print(','); |
| 5373 | } |
| 5374 | } |
| 5375 | pw.println("]"); |
| 5376 | |
| 5377 | pw.print(" "); |
| 5378 | pw.println(cb); |
| 5379 | } |
| 5380 | } |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 5381 | if (mNotedWatchers.size() > 0 && dumpMode < 0) { |
| 5382 | needSep = true; |
| 5383 | boolean printedHeader = false; |
Adam Bookatz | b80bf4e | 2020-04-22 17:59:29 -0700 | [diff] [blame] | 5384 | for (int watcherNum = 0; watcherNum < mNotedWatchers.size(); watcherNum++) { |
| 5385 | final SparseArray<NotedCallback> notedWatchers = |
| 5386 | mNotedWatchers.valueAt(watcherNum); |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 5387 | if (notedWatchers.size() <= 0) { |
| 5388 | continue; |
| 5389 | } |
| 5390 | final NotedCallback cb = notedWatchers.valueAt(0); |
| 5391 | if (dumpOp >= 0 && notedWatchers.indexOfKey(dumpOp) < 0) { |
| 5392 | continue; |
| 5393 | } |
Dianne Hackborn | 125dc53 | 2019-01-09 13:31:48 -0800 | [diff] [blame] | 5394 | if (dumpPackage != null |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 5395 | && dumpUid != UserHandle.getAppId(cb.mWatchingUid)) { |
| 5396 | continue; |
| 5397 | } |
| 5398 | if (!printedHeader) { |
| 5399 | pw.println(" All op noted watchers:"); |
| 5400 | printedHeader = true; |
| 5401 | } |
| 5402 | pw.print(" "); |
| 5403 | pw.print(Integer.toHexString(System.identityHashCode( |
Adam Bookatz | b80bf4e | 2020-04-22 17:59:29 -0700 | [diff] [blame] | 5404 | mNotedWatchers.keyAt(watcherNum)))); |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 5405 | pw.println(" ->"); |
| 5406 | pw.print(" ["); |
| 5407 | final int opCount = notedWatchers.size(); |
Adam Bookatz | b80bf4e | 2020-04-22 17:59:29 -0700 | [diff] [blame] | 5408 | for (int opNum = 0; opNum < opCount; opNum++) { |
| 5409 | if (opNum > 0) { |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 5410 | pw.print(' '); |
| 5411 | } |
Adam Bookatz | b80bf4e | 2020-04-22 17:59:29 -0700 | [diff] [blame] | 5412 | pw.print(AppOpsManager.opToName(notedWatchers.keyAt(opNum))); |
| 5413 | if (opNum < opCount - 1) { |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 5414 | pw.print(','); |
| 5415 | } |
| 5416 | } |
| 5417 | pw.println("]"); |
| 5418 | pw.print(" "); |
| 5419 | pw.println(cb); |
| 5420 | } |
| 5421 | } |
Yin-Chia Yeh | 51d8516 | 2019-08-06 15:31:39 -0700 | [diff] [blame] | 5422 | if (mAudioRestrictionManager.hasActiveRestrictions() && dumpOp < 0 |
| 5423 | && dumpPackage != null && dumpMode < 0 && !dumpWatchers && !dumpWatchers) { |
| 5424 | needSep = mAudioRestrictionManager.dump(pw) | needSep ; |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 5425 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 5426 | if (needSep) { |
| 5427 | pw.println(); |
| 5428 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 5429 | for (int i=0; i<mUidStates.size(); i++) { |
| 5430 | UidState uidState = mUidStates.valueAt(i); |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 5431 | final SparseIntArray opModes = uidState.opModes; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5432 | final ArrayMap<String, Ops> pkgOps = uidState.pkgOps; |
| 5433 | |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5434 | if (dumpWatchers || dumpHistory) { |
Dianne Hackborn | 125dc53 | 2019-01-09 13:31:48 -0800 | [diff] [blame] | 5435 | continue; |
| 5436 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5437 | if (dumpOp >= 0 || dumpPackage != null || dumpMode >= 0) { |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 5438 | boolean hasOp = dumpOp < 0 || (uidState.opModes != null |
| 5439 | && uidState.opModes.indexOfKey(dumpOp) >= 0); |
Philip P. Moltmann | ceffd59 | 2020-01-02 12:05:15 -0800 | [diff] [blame] | 5440 | boolean hasPackage = dumpPackage == null || dumpUid == mUidStates.keyAt(i); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5441 | boolean hasMode = dumpMode < 0; |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 5442 | if (!hasMode && opModes != null) { |
| 5443 | for (int opi = 0; !hasMode && opi < opModes.size(); opi++) { |
| 5444 | if (opModes.valueAt(opi) == dumpMode) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5445 | hasMode = true; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5446 | } |
| 5447 | } |
| 5448 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5449 | if (pkgOps != null) { |
| 5450 | for (int pkgi = 0; |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 5451 | (!hasOp || !hasPackage || !hasMode) && pkgi < pkgOps.size(); |
| 5452 | pkgi++) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5453 | Ops ops = pkgOps.valueAt(pkgi); |
| 5454 | if (!hasOp && ops != null && ops.indexOfKey(dumpOp) >= 0) { |
| 5455 | hasOp = true; |
| 5456 | } |
| 5457 | if (!hasMode) { |
| 5458 | for (int opi = 0; !hasMode && opi < ops.size(); opi++) { |
| 5459 | if (ops.valueAt(opi).mode == dumpMode) { |
| 5460 | hasMode = true; |
| 5461 | } |
| 5462 | } |
| 5463 | } |
| 5464 | if (!hasPackage && dumpPackage.equals(ops.packageName)) { |
| 5465 | hasPackage = true; |
| 5466 | } |
| 5467 | } |
| 5468 | } |
| 5469 | if (uidState.foregroundOps != null && !hasOp) { |
| 5470 | if (uidState.foregroundOps.indexOfKey(dumpOp) > 0) { |
| 5471 | hasOp = true; |
| 5472 | } |
| 5473 | } |
| 5474 | if (!hasOp || !hasPackage || !hasMode) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5475 | continue; |
| 5476 | } |
| 5477 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 5478 | |
| 5479 | pw.print(" Uid "); UserHandle.formatUid(pw, uidState.uid); pw.println(":"); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 5480 | pw.print(" state="); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5481 | pw.println(AppOpsManager.getUidStateName(uidState.state)); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5482 | if (uidState.state != uidState.pendingState) { |
| 5483 | pw.print(" pendingState="); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5484 | pw.println(AppOpsManager.getUidStateName(uidState.pendingState)); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5485 | } |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 5486 | pw.print(" capability="); |
| 5487 | pw.println(uidState.capability); |
| 5488 | if (uidState.capability != uidState.pendingCapability) { |
| 5489 | pw.print(" pendingCapability="); |
| 5490 | pw.println(uidState.pendingCapability); |
| 5491 | } |
Hui Yu | 4803268 | 2020-04-10 14:18:51 -0700 | [diff] [blame] | 5492 | pw.print(" appWidgetVisible="); |
| 5493 | pw.println(uidState.appWidgetVisible); |
| 5494 | if (uidState.appWidgetVisible != uidState.pendingAppWidgetVisible) { |
| 5495 | pw.print(" pendingAppWidgetVisible="); |
| 5496 | pw.println(uidState.pendingAppWidgetVisible); |
| 5497 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5498 | if (uidState.pendingStateCommitTime != 0) { |
| 5499 | pw.print(" pendingStateCommitTime="); |
Dianne Hackborn | 9fb9350 | 2018-06-18 12:29:44 -0700 | [diff] [blame] | 5500 | TimeUtils.formatDuration(uidState.pendingStateCommitTime, nowElapsed, pw); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5501 | pw.println(); |
| 5502 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5503 | if (uidState.foregroundOps != null && (dumpMode < 0 |
| 5504 | || dumpMode == AppOpsManager.MODE_FOREGROUND)) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5505 | pw.println(" foregroundOps:"); |
| 5506 | for (int j = 0; j < uidState.foregroundOps.size(); j++) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5507 | if (dumpOp >= 0 && dumpOp != uidState.foregroundOps.keyAt(j)) { |
| 5508 | continue; |
| 5509 | } |
| 5510 | pw.print(" "); |
| 5511 | pw.print(AppOpsManager.opToName(uidState.foregroundOps.keyAt(j))); |
| 5512 | pw.print(": "); |
| 5513 | pw.println(uidState.foregroundOps.valueAt(j) ? "WATCHER" : "SILENT"); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5514 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5515 | pw.print(" hasForegroundWatchers="); |
| 5516 | pw.println(uidState.hasForegroundWatchers); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5517 | } |
Svet Ganov | ee438d4 | 2017-01-19 18:04:38 -0800 | [diff] [blame] | 5518 | needSep = true; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 5519 | |
Hai Zhang | 93540ca | 2019-09-28 00:04:18 +0000 | [diff] [blame] | 5520 | if (opModes != null) { |
| 5521 | final int opModeCount = opModes.size(); |
| 5522 | for (int j = 0; j < opModeCount; j++) { |
| 5523 | final int code = opModes.keyAt(j); |
| 5524 | final int mode = opModes.valueAt(j); |
| 5525 | if (dumpOp >= 0 && dumpOp != code) { |
| 5526 | continue; |
| 5527 | } |
| 5528 | if (dumpMode >= 0 && dumpMode != mode) { |
| 5529 | continue; |
| 5530 | } |
| 5531 | pw.print(" "); pw.print(AppOpsManager.opToName(code)); |
| 5532 | pw.print(": mode="); pw.println(AppOpsManager.modeToName(mode)); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 5533 | } |
| 5534 | } |
| 5535 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 5536 | if (pkgOps == null) { |
| 5537 | continue; |
| 5538 | } |
| 5539 | |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5540 | for (int pkgi = 0; pkgi < pkgOps.size(); pkgi++) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5541 | final Ops ops = pkgOps.valueAt(pkgi); |
| 5542 | if (dumpPackage != null && !dumpPackage.equals(ops.packageName)) { |
| 5543 | continue; |
| 5544 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5545 | boolean printedPackage = false; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 5546 | for (int j=0; j<ops.size(); j++) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5547 | final Op op = ops.valueAt(j); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5548 | final int opCode = op.op; |
| 5549 | if (dumpOp >= 0 && dumpOp != opCode) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5550 | continue; |
| 5551 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5552 | if (dumpMode >= 0 && dumpMode != op.mode) { |
| 5553 | continue; |
| 5554 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5555 | if (!printedPackage) { |
| 5556 | pw.print(" Package "); pw.print(ops.packageName); pw.println(":"); |
| 5557 | printedPackage = true; |
| 5558 | } |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5559 | pw.print(" "); pw.print(AppOpsManager.opToName(opCode)); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5560 | pw.print(" ("); pw.print(AppOpsManager.modeToName(op.mode)); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5561 | final int switchOp = AppOpsManager.opToSwitch(opCode); |
| 5562 | if (switchOp != opCode) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5563 | pw.print(" / switch "); |
| 5564 | pw.print(AppOpsManager.opToName(switchOp)); |
| 5565 | final Op switchObj = ops.get(switchOp); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5566 | int mode = switchObj != null ? switchObj.mode |
| 5567 | : AppOpsManager.opToDefaultMode(switchOp); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 5568 | pw.print("="); pw.print(AppOpsManager.modeToName(mode)); |
| 5569 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 5570 | pw.println("): "); |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5571 | dumpStatesLocked(pw, dumpAttributionTag, dumpFilter, nowElapsed, op, now, |
| 5572 | sdf, date, " "); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 5573 | } |
| 5574 | } |
| 5575 | } |
Svet Ganov | ee438d4 | 2017-01-19 18:04:38 -0800 | [diff] [blame] | 5576 | if (needSep) { |
| 5577 | pw.println(); |
| 5578 | } |
| 5579 | |
| 5580 | final int userRestrictionCount = mOpUserRestrictions.size(); |
| 5581 | for (int i = 0; i < userRestrictionCount; i++) { |
| 5582 | IBinder token = mOpUserRestrictions.keyAt(i); |
| 5583 | ClientRestrictionState restrictionState = mOpUserRestrictions.valueAt(i); |
Dianne Hackborn | 125dc53 | 2019-01-09 13:31:48 -0800 | [diff] [blame] | 5584 | boolean printedTokenHeader = false; |
| 5585 | |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5586 | if (dumpMode >= 0 || dumpWatchers || dumpHistory) { |
Dianne Hackborn | 125dc53 | 2019-01-09 13:31:48 -0800 | [diff] [blame] | 5587 | continue; |
| 5588 | } |
Svet Ganov | ee438d4 | 2017-01-19 18:04:38 -0800 | [diff] [blame] | 5589 | |
| 5590 | final int restrictionCount = restrictionState.perUserRestrictions != null |
| 5591 | ? restrictionState.perUserRestrictions.size() : 0; |
Dianne Hackborn | 125dc53 | 2019-01-09 13:31:48 -0800 | [diff] [blame] | 5592 | if (restrictionCount > 0 && dumpPackage == null) { |
| 5593 | boolean printedOpsHeader = false; |
Svet Ganov | ee438d4 | 2017-01-19 18:04:38 -0800 | [diff] [blame] | 5594 | for (int j = 0; j < restrictionCount; j++) { |
| 5595 | int userId = restrictionState.perUserRestrictions.keyAt(j); |
| 5596 | boolean[] restrictedOps = restrictionState.perUserRestrictions.valueAt(j); |
| 5597 | if (restrictedOps == null) { |
| 5598 | continue; |
| 5599 | } |
Dianne Hackborn | 125dc53 | 2019-01-09 13:31:48 -0800 | [diff] [blame] | 5600 | if (dumpOp >= 0 && (dumpOp >= restrictedOps.length |
| 5601 | || !restrictedOps[dumpOp])) { |
| 5602 | continue; |
| 5603 | } |
| 5604 | if (!printedTokenHeader) { |
| 5605 | pw.println(" User restrictions for token " + token + ":"); |
| 5606 | printedTokenHeader = true; |
| 5607 | } |
| 5608 | if (!printedOpsHeader) { |
| 5609 | pw.println(" Restricted ops:"); |
| 5610 | printedOpsHeader = true; |
| 5611 | } |
Svet Ganov | ee438d4 | 2017-01-19 18:04:38 -0800 | [diff] [blame] | 5612 | StringBuilder restrictedOpsValue = new StringBuilder(); |
| 5613 | restrictedOpsValue.append("["); |
| 5614 | final int restrictedOpCount = restrictedOps.length; |
| 5615 | for (int k = 0; k < restrictedOpCount; k++) { |
| 5616 | if (restrictedOps[k]) { |
| 5617 | if (restrictedOpsValue.length() > 1) { |
| 5618 | restrictedOpsValue.append(", "); |
| 5619 | } |
| 5620 | restrictedOpsValue.append(AppOpsManager.opToName(k)); |
| 5621 | } |
| 5622 | } |
| 5623 | restrictedOpsValue.append("]"); |
| 5624 | pw.print(" "); pw.print("user: "); pw.print(userId); |
| 5625 | pw.print(" restricted ops: "); pw.println(restrictedOpsValue); |
| 5626 | } |
| 5627 | } |
| 5628 | |
| 5629 | final int excludedPackageCount = restrictionState.perUserExcludedPackages != null |
| 5630 | ? restrictionState.perUserExcludedPackages.size() : 0; |
Dianne Hackborn | 125dc53 | 2019-01-09 13:31:48 -0800 | [diff] [blame] | 5631 | if (excludedPackageCount > 0 && dumpOp < 0) { |
| 5632 | boolean printedPackagesHeader = false; |
Svet Ganov | ee438d4 | 2017-01-19 18:04:38 -0800 | [diff] [blame] | 5633 | for (int j = 0; j < excludedPackageCount; j++) { |
| 5634 | int userId = restrictionState.perUserExcludedPackages.keyAt(j); |
| 5635 | String[] packageNames = restrictionState.perUserExcludedPackages.valueAt(j); |
Dianne Hackborn | 125dc53 | 2019-01-09 13:31:48 -0800 | [diff] [blame] | 5636 | if (packageNames == null) { |
| 5637 | continue; |
| 5638 | } |
| 5639 | boolean hasPackage; |
| 5640 | if (dumpPackage != null) { |
| 5641 | hasPackage = false; |
| 5642 | for (String pkg : packageNames) { |
| 5643 | if (dumpPackage.equals(pkg)) { |
| 5644 | hasPackage = true; |
| 5645 | break; |
| 5646 | } |
| 5647 | } |
| 5648 | } else { |
| 5649 | hasPackage = true; |
| 5650 | } |
| 5651 | if (!hasPackage) { |
| 5652 | continue; |
| 5653 | } |
| 5654 | if (!printedTokenHeader) { |
| 5655 | pw.println(" User restrictions for token " + token + ":"); |
| 5656 | printedTokenHeader = true; |
| 5657 | } |
| 5658 | if (!printedPackagesHeader) { |
| 5659 | pw.println(" Excluded packages:"); |
| 5660 | printedPackagesHeader = true; |
| 5661 | } |
Svet Ganov | ee438d4 | 2017-01-19 18:04:38 -0800 | [diff] [blame] | 5662 | pw.print(" "); pw.print("user: "); pw.print(userId); |
| 5663 | pw.print(" packages: "); pw.println(Arrays.toString(packageNames)); |
| 5664 | } |
| 5665 | } |
| 5666 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 5667 | } |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 5668 | |
| 5669 | // Must not hold the appops lock |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5670 | if (dumpHistory && !dumpWatchers) { |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5671 | mHistoricalRegistry.dump(" ", pw, dumpUid, dumpPackage, dumpAttributionTag, dumpOp, |
Philip P. Moltmann | 4aacd71 | 2020-01-03 12:32:20 -0800 | [diff] [blame] | 5672 | dumpFilter); |
Svet Ganov | af189e3 | 2019-02-15 18:45:29 -0800 | [diff] [blame] | 5673 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 5674 | } |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 5675 | |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 5676 | @Override |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 5677 | public void setUserRestrictions(Bundle restrictions, IBinder token, int userHandle) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 5678 | checkSystemUid("setUserRestrictions"); |
Daulet Zhanguzin | 9bd1370 | 2020-01-03 09:44:10 +0000 | [diff] [blame] | 5679 | Objects.requireNonNull(restrictions); |
| 5680 | Objects.requireNonNull(token); |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 5681 | for (int i = 0; i < AppOpsManager._NUM_OP; i++) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 5682 | String restriction = AppOpsManager.opToRestriction(i); |
Suprabh Shukla | 64e0dcb | 2016-05-24 16:23:11 -0700 | [diff] [blame] | 5683 | if (restriction != null) { |
| 5684 | setUserRestrictionNoCheck(i, restrictions.getBoolean(restriction, false), token, |
| 5685 | userHandle, null); |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 5686 | } |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 5687 | } |
| 5688 | } |
| 5689 | |
| 5690 | @Override |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 5691 | public void setUserRestriction(int code, boolean restricted, IBinder token, int userHandle, |
| 5692 | String[] exceptionPackages) { |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 5693 | if (Binder.getCallingPid() != Process.myPid()) { |
| 5694 | mContext.enforcePermission(Manifest.permission.MANAGE_APP_OPS_RESTRICTIONS, |
| 5695 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 5696 | } |
| 5697 | if (userHandle != UserHandle.getCallingUserId()) { |
| 5698 | if (mContext.checkCallingOrSelfPermission(Manifest.permission |
| 5699 | .INTERACT_ACROSS_USERS_FULL) != PackageManager.PERMISSION_GRANTED |
| 5700 | && mContext.checkCallingOrSelfPermission(Manifest.permission |
| 5701 | .INTERACT_ACROSS_USERS) != PackageManager.PERMISSION_GRANTED) { |
| 5702 | throw new SecurityException("Need INTERACT_ACROSS_USERS_FULL or" |
| 5703 | + " INTERACT_ACROSS_USERS to interact cross user "); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 5704 | } |
| 5705 | } |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 5706 | verifyIncomingOp(code); |
Daulet Zhanguzin | 9bd1370 | 2020-01-03 09:44:10 +0000 | [diff] [blame] | 5707 | Objects.requireNonNull(token); |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 5708 | setUserRestrictionNoCheck(code, restricted, token, userHandle, exceptionPackages); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 5709 | } |
| 5710 | |
| 5711 | private void setUserRestrictionNoCheck(int code, boolean restricted, IBinder token, |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 5712 | int userHandle, String[] exceptionPackages) { |
Svet Ganov | 442ed57 | 2016-08-17 17:29:43 -0700 | [diff] [blame] | 5713 | synchronized (AppOpsService.this) { |
| 5714 | ClientRestrictionState restrictionState = mOpUserRestrictions.get(token); |
| 5715 | |
| 5716 | if (restrictionState == null) { |
| 5717 | try { |
| 5718 | restrictionState = new ClientRestrictionState(token); |
| 5719 | } catch (RemoteException e) { |
| 5720 | return; |
| 5721 | } |
| 5722 | mOpUserRestrictions.put(token, restrictionState); |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 5723 | } |
Svet Ganov | 442ed57 | 2016-08-17 17:29:43 -0700 | [diff] [blame] | 5724 | |
| 5725 | if (restrictionState.setRestriction(code, restricted, exceptionPackages, userHandle)) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 5726 | mHandler.sendMessage(PooledLambda.obtainMessage( |
Svet Ganov | 3a95f83 | 2018-03-23 17:44:30 -0700 | [diff] [blame] | 5727 | AppOpsService::notifyWatchersOfChange, this, code, UID_ANY)); |
Svet Ganov | 442ed57 | 2016-08-17 17:29:43 -0700 | [diff] [blame] | 5728 | } |
| 5729 | |
| 5730 | if (restrictionState.isDefault()) { |
| 5731 | mOpUserRestrictions.remove(token); |
| 5732 | restrictionState.destroy(); |
| 5733 | } |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 5734 | } |
Julia Reynolds | bb21c25 | 2016-04-05 16:01:49 -0400 | [diff] [blame] | 5735 | } |
| 5736 | |
Svet Ganov | 3a95f83 | 2018-03-23 17:44:30 -0700 | [diff] [blame] | 5737 | private void notifyWatchersOfChange(int code, int uid) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 5738 | final ArraySet<ModeCallback> clonedCallbacks; |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 5739 | synchronized (this) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 5740 | ArraySet<ModeCallback> callbacks = mOpModeWatchers.get(code); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 5741 | if (callbacks == null) { |
| 5742 | return; |
| 5743 | } |
Dianne Hackborn | 68d7655 | 2017-02-27 15:32:03 -0800 | [diff] [blame] | 5744 | clonedCallbacks = new ArraySet<>(callbacks); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 5745 | } |
| 5746 | |
Svet Ganov | 3a95f83 | 2018-03-23 17:44:30 -0700 | [diff] [blame] | 5747 | notifyOpChanged(clonedCallbacks, code, uid, null); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 5748 | } |
| 5749 | |
| 5750 | @Override |
| 5751 | public void removeUser(int userHandle) throws RemoteException { |
| 5752 | checkSystemUid("removeUser"); |
Svet Ganov | 442ed57 | 2016-08-17 17:29:43 -0700 | [diff] [blame] | 5753 | synchronized (AppOpsService.this) { |
| 5754 | final int tokenCount = mOpUserRestrictions.size(); |
| 5755 | for (int i = tokenCount - 1; i >= 0; i--) { |
| 5756 | ClientRestrictionState opRestrictions = mOpUserRestrictions.valueAt(i); |
| 5757 | opRestrictions.removeUser(userHandle); |
| 5758 | } |
Sudheer Shanka | bc2fadd | 2016-09-27 17:36:39 -0700 | [diff] [blame] | 5759 | removeUidsForUserLocked(userHandle); |
| 5760 | } |
| 5761 | } |
| 5762 | |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 5763 | @Override |
| 5764 | public boolean isOperationActive(int code, int uid, String packageName) { |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 5765 | if (Binder.getCallingUid() != uid) { |
| 5766 | if (mContext.checkCallingOrSelfPermission(Manifest.permission.WATCH_APPOPS) |
| 5767 | != PackageManager.PERMISSION_GRANTED) { |
| 5768 | return false; |
| 5769 | } |
| 5770 | } |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 5771 | verifyIncomingOp(code); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 5772 | final String resolvedPackageName = resolvePackageName(uid, packageName); |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 5773 | if (resolvedPackageName == null) { |
| 5774 | return false; |
| 5775 | } |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5776 | // TODO moltmann: Allow to check for attribution op activeness |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 5777 | synchronized (AppOpsService.this) { |
Philip P. Moltmann | ad787aa | 2020-03-10 09:49:22 -0700 | [diff] [blame] | 5778 | Ops pkgOps = getOpsLocked(uid, resolvedPackageName, null, null, false); |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 5779 | if (pkgOps == null) { |
| 5780 | return false; |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 5781 | } |
Philip P. Moltmann | 6c6403e | 2019-12-09 10:08:29 -0800 | [diff] [blame] | 5782 | |
| 5783 | Op op = pkgOps.get(code); |
| 5784 | if (op == null) { |
| 5785 | return false; |
| 5786 | } |
| 5787 | |
| 5788 | return op.isRunning(); |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 5789 | } |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 5790 | } |
| 5791 | |
Svet Ganov | 8455ba2 | 2019-01-02 13:05:56 -0800 | [diff] [blame] | 5792 | @Override |
| 5793 | public void setHistoryParameters(@AppOpsManager.HistoricalMode int mode, |
| 5794 | long baseSnapshotInterval, int compressionStep) { |
| 5795 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_APPOPS, |
| 5796 | "setHistoryParameters"); |
| 5797 | // Must not hold the appops lock |
| 5798 | mHistoricalRegistry.setHistoryParameters(mode, baseSnapshotInterval, compressionStep); |
| 5799 | } |
| 5800 | |
| 5801 | @Override |
| 5802 | public void offsetHistory(long offsetMillis) { |
| 5803 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_APPOPS, |
| 5804 | "offsetHistory"); |
| 5805 | // Must not hold the appops lock |
| 5806 | mHistoricalRegistry.offsetHistory(offsetMillis); |
| 5807 | } |
| 5808 | |
| 5809 | @Override |
| 5810 | public void addHistoricalOps(HistoricalOps ops) { |
| 5811 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_APPOPS, |
| 5812 | "addHistoricalOps"); |
| 5813 | // Must not hold the appops lock |
| 5814 | mHistoricalRegistry.addHistoricalOps(ops); |
| 5815 | } |
| 5816 | |
| 5817 | @Override |
| 5818 | public void resetHistoryParameters() { |
| 5819 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_APPOPS, |
| 5820 | "resetHistoryParameters"); |
| 5821 | // Must not hold the appops lock |
| 5822 | mHistoricalRegistry.resetHistoryParameters(); |
| 5823 | } |
| 5824 | |
| 5825 | @Override |
| 5826 | public void clearHistory() { |
| 5827 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_APPOPS, |
| 5828 | "clearHistory"); |
| 5829 | // Must not hold the appops lock |
| 5830 | mHistoricalRegistry.clearHistory(); |
| 5831 | } |
| 5832 | |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 5833 | /** |
| 5834 | * Report runtime access to AppOp together with message (including stack trace) |
| 5835 | * |
| 5836 | * @param packageName The package which reported the op |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5837 | * @param notedAppOp contains code of op and attributionTag provided by developer |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 5838 | * @param message Message describing AppOp access (can be stack trace) |
| 5839 | * |
| 5840 | * @return Config for future sampling to reduce amount of reporting |
| 5841 | */ |
| 5842 | @Override |
| 5843 | public MessageSamplingConfig reportRuntimeAppOpAccessMessageAndGetConfig( |
| 5844 | String packageName, SyncNotedAppOp notedAppOp, String message) { |
| 5845 | int uid = Binder.getCallingUid(); |
| 5846 | Objects.requireNonNull(packageName); |
| 5847 | synchronized (this) { |
Stanislav Zholnin | 4c32385 | 2020-03-04 18:03:24 +0000 | [diff] [blame] | 5848 | switchPackageIfBootTimeOrRarelyUsedLocked(packageName); |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 5849 | if (!packageName.equals(mSampledPackage)) { |
| 5850 | return new MessageSamplingConfig(OP_NONE, 0, |
| 5851 | Instant.now().plus(1, ChronoUnit.HOURS).toEpochMilli()); |
| 5852 | } |
| 5853 | |
| 5854 | Objects.requireNonNull(notedAppOp); |
| 5855 | Objects.requireNonNull(message); |
| 5856 | |
| 5857 | reportRuntimeAppOpAccessMessageInternalLocked(uid, packageName, |
| 5858 | AppOpsManager.strOpToOp(notedAppOp.getOp()), |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5859 | notedAppOp.getAttributionTag(), message); |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 5860 | |
| 5861 | return new MessageSamplingConfig(mSampledAppOpCode, mAcceptableLeftDistance, |
| 5862 | Instant.now().plus(1, ChronoUnit.HOURS).toEpochMilli()); |
| 5863 | } |
| 5864 | } |
| 5865 | |
| 5866 | /** |
| 5867 | * Report runtime access to AppOp together with message (entry point for reporting |
| 5868 | * asynchronous access) |
| 5869 | * @param uid Uid of the package which reported the op |
| 5870 | * @param packageName The package which reported the op |
| 5871 | * @param opCode Code of AppOp |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5872 | * @param attributionTag FeautreId of AppOp reported |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 5873 | * @param message Message describing AppOp access (can be stack trace) |
| 5874 | */ |
| 5875 | private void reportRuntimeAppOpAccessMessageAsyncLocked(int uid, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5876 | @NonNull String packageName, int opCode, @Nullable String attributionTag, |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 5877 | @NonNull String message) { |
Stanislav Zholnin | 4c32385 | 2020-03-04 18:03:24 +0000 | [diff] [blame] | 5878 | switchPackageIfBootTimeOrRarelyUsedLocked(packageName); |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 5879 | if (!Objects.equals(mSampledPackage, packageName)) { |
| 5880 | return; |
| 5881 | } |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5882 | reportRuntimeAppOpAccessMessageInternalLocked(uid, packageName, opCode, attributionTag, |
| 5883 | message); |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 5884 | } |
| 5885 | |
| 5886 | /** |
| 5887 | * Decides whether reported message is within the range of watched AppOps and picks it for |
| 5888 | * reporting uniformly at random across all received messages. |
| 5889 | */ |
| 5890 | private void reportRuntimeAppOpAccessMessageInternalLocked(int uid, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5891 | @NonNull String packageName, int opCode, @Nullable String attributionTag, |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 5892 | @NonNull String message) { |
| 5893 | int newLeftDistance = AppOpsManager.leftCircularDistance(opCode, |
| 5894 | mSampledAppOpCode, _NUM_OP); |
| 5895 | |
| 5896 | if (mAcceptableLeftDistance < newLeftDistance) { |
| 5897 | return; |
| 5898 | } |
| 5899 | |
| 5900 | if (mAcceptableLeftDistance > newLeftDistance) { |
| 5901 | mAcceptableLeftDistance = newLeftDistance; |
| 5902 | mMessagesCollectedCount = 0.0f; |
| 5903 | } |
| 5904 | |
| 5905 | mMessagesCollectedCount += 1.0f; |
| 5906 | if (ThreadLocalRandom.current().nextFloat() <= 1.0f / mMessagesCollectedCount) { |
| 5907 | mCollectedRuntimePermissionMessage = new RuntimeAppOpAccessMessage(uid, opCode, |
Philip P. Moltmann | 12ac3f4 | 2020-03-05 15:01:29 -0800 | [diff] [blame] | 5908 | packageName, attributionTag, message, mSamplingStrategy); |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 5909 | } |
| 5910 | return; |
| 5911 | } |
| 5912 | |
| 5913 | /** Pulls current AppOps access report and resamples package and app op to watch */ |
| 5914 | @Override |
| 5915 | public @Nullable RuntimeAppOpAccessMessage collectRuntimeAppOpAccessMessage() { |
| 5916 | mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS, |
| 5917 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 5918 | RuntimeAppOpAccessMessage result; |
| 5919 | List<String> packageNames = getPackageNamesForSampling(); |
| 5920 | synchronized (this) { |
| 5921 | result = mCollectedRuntimePermissionMessage; |
| 5922 | resamplePackageAndAppOpLocked(packageNames); |
| 5923 | } |
| 5924 | return result; |
| 5925 | } |
| 5926 | |
| 5927 | /** |
| 5928 | * Checks if package is in the list of rarely used package and starts watching the new package |
Stanislav Zholnin | 4c32385 | 2020-03-04 18:03:24 +0000 | [diff] [blame] | 5929 | * to collect incoming message or if collection is happening in first minutes since boot. |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 5930 | * @param packageName |
| 5931 | */ |
Stanislav Zholnin | 4c32385 | 2020-03-04 18:03:24 +0000 | [diff] [blame] | 5932 | private void switchPackageIfBootTimeOrRarelyUsedLocked(@NonNull String packageName) { |
| 5933 | if (mSampledPackage == null) { |
Stanislav Zholnin | 1549209 | 2020-04-19 06:21:36 +0100 | [diff] [blame] | 5934 | if (ThreadLocalRandom.current().nextFloat() < 0.5f) { |
Stanislav Zholnin | 4c32385 | 2020-03-04 18:03:24 +0000 | [diff] [blame] | 5935 | mSamplingStrategy = SAMPLING_STRATEGY_BOOT_TIME_SAMPLING; |
| 5936 | resampleAppOpForPackageLocked(packageName); |
| 5937 | } |
| 5938 | } else if (mRarelyUsedPackages.contains(packageName)) { |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 5939 | mRarelyUsedPackages.remove(packageName); |
| 5940 | if (ThreadLocalRandom.current().nextFloat() < 0.5f) { |
Muhammad Qureshi | 317061a | 2020-03-02 13:15:51 -0800 | [diff] [blame] | 5941 | mSamplingStrategy = SAMPLING_STRATEGY_RARELY_USED; |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 5942 | resampleAppOpForPackageLocked(packageName); |
| 5943 | } |
| 5944 | } |
| 5945 | } |
| 5946 | |
| 5947 | /** Resamples package and appop to watch from the list provided. */ |
| 5948 | private void resamplePackageAndAppOpLocked(@NonNull List<String> packageNames) { |
| 5949 | if (!packageNames.isEmpty()) { |
Muhammad Qureshi | 317061a | 2020-03-02 13:15:51 -0800 | [diff] [blame] | 5950 | mSamplingStrategy = SAMPLING_STRATEGY_UNIFORM; |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 5951 | resampleAppOpForPackageLocked(packageNames.get( |
| 5952 | ThreadLocalRandom.current().nextInt(packageNames.size()))); |
| 5953 | } |
| 5954 | } |
| 5955 | |
| 5956 | /** Resamples appop for the chosen package and initializes sampling state */ |
| 5957 | private void resampleAppOpForPackageLocked(@NonNull String packageName) { |
| 5958 | mMessagesCollectedCount = 0.0f; |
| 5959 | mSampledAppOpCode = ThreadLocalRandom.current().nextInt(_NUM_OP); |
| 5960 | mAcceptableLeftDistance = _NUM_OP; |
| 5961 | mSampledPackage = packageName; |
| 5962 | mCollectedRuntimePermissionMessage = null; |
| 5963 | } |
| 5964 | |
| 5965 | /** |
| 5966 | * Creates list of rarely used packages - packages which were not used over last week or |
| 5967 | * which declared but did not use permissions over last week. |
| 5968 | * */ |
| 5969 | private void initializeRarelyUsedPackagesList(@NonNull ArraySet<String> candidates) { |
| 5970 | AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class); |
| 5971 | List<String> runtimeAppOpsList = getRuntimeAppOpsList(); |
| 5972 | AppOpsManager.HistoricalOpsRequest histOpsRequest = |
| 5973 | new AppOpsManager.HistoricalOpsRequest.Builder( |
| 5974 | Instant.now().minus(7, ChronoUnit.DAYS).toEpochMilli(), |
| 5975 | Long.MAX_VALUE).setOpNames(runtimeAppOpsList).setFlags( |
| 5976 | OP_FLAG_SELF | OP_FLAG_TRUSTED_PROXIED).build(); |
| 5977 | appOps.getHistoricalOps(histOpsRequest, AsyncTask.THREAD_POOL_EXECUTOR, |
| 5978 | new Consumer<HistoricalOps>() { |
| 5979 | @Override |
| 5980 | public void accept(HistoricalOps histOps) { |
| 5981 | int uidCount = histOps.getUidCount(); |
| 5982 | for (int uidIdx = 0; uidIdx < uidCount; uidIdx++) { |
| 5983 | final AppOpsManager.HistoricalUidOps uidOps = histOps.getUidOpsAt( |
| 5984 | uidIdx); |
| 5985 | int pkgCount = uidOps.getPackageCount(); |
| 5986 | for (int pkgIdx = 0; pkgIdx < pkgCount; pkgIdx++) { |
| 5987 | String packageName = uidOps.getPackageOpsAt( |
| 5988 | pkgIdx).getPackageName(); |
| 5989 | if (!candidates.contains(packageName)) { |
| 5990 | continue; |
| 5991 | } |
| 5992 | AppOpsManager.HistoricalPackageOps packageOps = |
| 5993 | uidOps.getPackageOpsAt(pkgIdx); |
| 5994 | if (packageOps.getOpCount() != 0) { |
| 5995 | candidates.remove(packageName); |
| 5996 | } |
| 5997 | } |
| 5998 | } |
| 5999 | synchronized (this) { |
Stanislav Zholnin | 4c32385 | 2020-03-04 18:03:24 +0000 | [diff] [blame] | 6000 | int numPkgs = mRarelyUsedPackages.size(); |
| 6001 | for (int i = 0; i < numPkgs; i++) { |
| 6002 | candidates.add(mRarelyUsedPackages.valueAt(i)); |
| 6003 | } |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 6004 | mRarelyUsedPackages = candidates; |
| 6005 | } |
| 6006 | } |
| 6007 | }); |
| 6008 | } |
| 6009 | |
| 6010 | /** List of app ops related to runtime permissions */ |
| 6011 | private List<String> getRuntimeAppOpsList() { |
| 6012 | ArrayList<String> result = new ArrayList(); |
| 6013 | for (int i = 0; i < _NUM_OP; i++) { |
| 6014 | if (shouldCollectNotes(i)) { |
| 6015 | result.add(opToPublicName(i)); |
| 6016 | } |
| 6017 | } |
| 6018 | return result; |
| 6019 | } |
| 6020 | |
| 6021 | /** Returns list of packages to be used for package sampling */ |
| 6022 | private @NonNull List<String> getPackageNamesForSampling() { |
| 6023 | List<String> packageNames = new ArrayList<>(); |
| 6024 | PackageManagerInternal packageManagerInternal = LocalServices.getService( |
| 6025 | PackageManagerInternal.class); |
| 6026 | PackageList packages = packageManagerInternal.getPackageList(); |
| 6027 | for (String packageName : packages.getPackageNames()) { |
| 6028 | PackageInfo pkg = packageManagerInternal.getPackageInfo(packageName, |
| 6029 | PackageManager.GET_PERMISSIONS, Process.myUid(), mContext.getUserId()); |
| 6030 | if (isSamplingTarget(pkg)) { |
| 6031 | packageNames.add(pkg.packageName); |
| 6032 | } |
| 6033 | } |
| 6034 | return packageNames; |
| 6035 | } |
| 6036 | |
| 6037 | /** Checks whether package should be included in sampling pool */ |
| 6038 | private boolean isSamplingTarget(@Nullable PackageInfo pkg) { |
| 6039 | if (pkg == null) { |
| 6040 | return false; |
| 6041 | } |
Stanislav Zholnin | 90516b9 | 2020-01-20 14:03:06 +0000 | [diff] [blame] | 6042 | String[] requestedPermissions = pkg.requestedPermissions; |
| 6043 | if (requestedPermissions == null) { |
| 6044 | return false; |
| 6045 | } |
| 6046 | for (String permission : requestedPermissions) { |
| 6047 | PermissionInfo permissionInfo; |
| 6048 | try { |
| 6049 | permissionInfo = mContext.getPackageManager().getPermissionInfo(permission, 0); |
| 6050 | } catch (PackageManager.NameNotFoundException ignored) { |
| 6051 | continue; |
| 6052 | } |
| 6053 | if (permissionInfo.getProtection() == PROTECTION_DANGEROUS) { |
| 6054 | return true; |
| 6055 | } |
| 6056 | } |
| 6057 | return false; |
| 6058 | } |
| 6059 | |
Sudheer Shanka | bc2fadd | 2016-09-27 17:36:39 -0700 | [diff] [blame] | 6060 | private void removeUidsForUserLocked(int userHandle) { |
| 6061 | for (int i = mUidStates.size() - 1; i >= 0; --i) { |
| 6062 | final int uid = mUidStates.keyAt(i); |
| 6063 | if (UserHandle.getUserId(uid) == userHandle) { |
| 6064 | mUidStates.removeAt(i); |
| 6065 | } |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 6066 | } |
| 6067 | } |
| 6068 | |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 6069 | private void checkSystemUid(String function) { |
| 6070 | int uid = Binder.getCallingUid(); |
| 6071 | if (uid != Process.SYSTEM_UID) { |
| 6072 | throw new SecurityException(function + " must by called by the system"); |
| 6073 | } |
| 6074 | } |
| 6075 | |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 6076 | private static String resolvePackageName(int uid, String packageName) { |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 6077 | if (uid == Process.ROOT_UID) { |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 6078 | return "root"; |
| 6079 | } else if (uid == Process.SHELL_UID) { |
| 6080 | return "com.android.shell"; |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 6081 | } else if (uid == Process.MEDIA_UID) { |
| 6082 | return "media"; |
| 6083 | } else if (uid == Process.AUDIOSERVER_UID) { |
| 6084 | return "audioserver"; |
| 6085 | } else if (uid == Process.CAMERASERVER_UID) { |
| 6086 | return "cameraserver"; |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 6087 | } else if (uid == Process.SYSTEM_UID && packageName == null) { |
| 6088 | return "android"; |
| 6089 | } |
| 6090 | return packageName; |
| 6091 | } |
| 6092 | |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 6093 | private static int resolveUid(String packageName) { |
| 6094 | if (packageName == null) { |
| 6095 | return -1; |
| 6096 | } |
| 6097 | switch (packageName) { |
| 6098 | case "root": |
| 6099 | return Process.ROOT_UID; |
| 6100 | case "shell": |
| 6101 | return Process.SHELL_UID; |
| 6102 | case "media": |
| 6103 | return Process.MEDIA_UID; |
| 6104 | case "audioserver": |
| 6105 | return Process.AUDIOSERVER_UID; |
| 6106 | case "cameraserver": |
| 6107 | return Process.CAMERASERVER_UID; |
| 6108 | } |
| 6109 | return -1; |
| 6110 | } |
| 6111 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 6112 | private static String[] getPackagesForUid(int uid) { |
Svet Ganov | f3807aa | 2015-08-02 10:09:56 -0700 | [diff] [blame] | 6113 | String[] packageNames = null; |
Philip P. Moltmann | 159d98b | 2018-12-20 08:30:53 -0800 | [diff] [blame] | 6114 | |
| 6115 | // Very early during boot the package manager is not yet or not yet fully started. At this |
| 6116 | // time there are no packages yet. |
| 6117 | if (AppGlobals.getPackageManager() != null) { |
| 6118 | try { |
| 6119 | packageNames = AppGlobals.getPackageManager().getPackagesForUid(uid); |
| 6120 | } catch (RemoteException e) { |
| 6121 | /* ignore - local call */ |
| 6122 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 6123 | } |
Svet Ganov | f3807aa | 2015-08-02 10:09:56 -0700 | [diff] [blame] | 6124 | if (packageNames == null) { |
| 6125 | return EmptyArray.STRING; |
| 6126 | } |
| 6127 | return packageNames; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 6128 | } |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 6129 | |
| 6130 | private final class ClientRestrictionState implements DeathRecipient { |
| 6131 | private final IBinder token; |
| 6132 | SparseArray<boolean[]> perUserRestrictions; |
| 6133 | SparseArray<String[]> perUserExcludedPackages; |
| 6134 | |
| 6135 | public ClientRestrictionState(IBinder token) |
| 6136 | throws RemoteException { |
| 6137 | token.linkToDeath(this, 0); |
| 6138 | this.token = token; |
| 6139 | } |
| 6140 | |
| 6141 | public boolean setRestriction(int code, boolean restricted, |
| 6142 | String[] excludedPackages, int userId) { |
| 6143 | boolean changed = false; |
| 6144 | |
| 6145 | if (perUserRestrictions == null && restricted) { |
| 6146 | perUserRestrictions = new SparseArray<>(); |
| 6147 | } |
| 6148 | |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 6149 | int[] users; |
| 6150 | if (userId == UserHandle.USER_ALL) { |
| 6151 | List<UserInfo> liveUsers = UserManager.get(mContext).getUsers(false); |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 6152 | |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 6153 | users = new int[liveUsers.size()]; |
| 6154 | for (int i = 0; i < liveUsers.size(); i++) { |
| 6155 | users[i] = liveUsers.get(i).id; |
| 6156 | } |
| 6157 | } else { |
| 6158 | users = new int[]{userId}; |
| 6159 | } |
| 6160 | |
| 6161 | if (perUserRestrictions != null) { |
| 6162 | int numUsers = users.length; |
| 6163 | |
| 6164 | for (int i = 0; i < numUsers; i++) { |
| 6165 | int thisUserId = users[i]; |
| 6166 | |
| 6167 | boolean[] userRestrictions = perUserRestrictions.get(thisUserId); |
| 6168 | if (userRestrictions == null && restricted) { |
| 6169 | userRestrictions = new boolean[AppOpsManager._NUM_OP]; |
| 6170 | perUserRestrictions.put(thisUserId, userRestrictions); |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 6171 | } |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 6172 | if (userRestrictions != null && userRestrictions[code] != restricted) { |
| 6173 | userRestrictions[code] = restricted; |
| 6174 | if (!restricted && isDefault(userRestrictions)) { |
| 6175 | perUserRestrictions.remove(thisUserId); |
| 6176 | userRestrictions = null; |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 6177 | } |
| 6178 | changed = true; |
| 6179 | } |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 6180 | |
| 6181 | if (userRestrictions != null) { |
| 6182 | final boolean noExcludedPackages = ArrayUtils.isEmpty(excludedPackages); |
| 6183 | if (perUserExcludedPackages == null && !noExcludedPackages) { |
| 6184 | perUserExcludedPackages = new SparseArray<>(); |
| 6185 | } |
| 6186 | if (perUserExcludedPackages != null && !Arrays.equals(excludedPackages, |
| 6187 | perUserExcludedPackages.get(thisUserId))) { |
| 6188 | if (noExcludedPackages) { |
| 6189 | perUserExcludedPackages.remove(thisUserId); |
| 6190 | if (perUserExcludedPackages.size() <= 0) { |
| 6191 | perUserExcludedPackages = null; |
| 6192 | } |
| 6193 | } else { |
| 6194 | perUserExcludedPackages.put(thisUserId, excludedPackages); |
| 6195 | } |
| 6196 | changed = true; |
| 6197 | } |
| 6198 | } |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 6199 | } |
| 6200 | } |
| 6201 | |
| 6202 | return changed; |
| 6203 | } |
| 6204 | |
| 6205 | public boolean hasRestriction(int restriction, String packageName, int userId) { |
| 6206 | if (perUserRestrictions == null) { |
| 6207 | return false; |
| 6208 | } |
| 6209 | boolean[] restrictions = perUserRestrictions.get(userId); |
| 6210 | if (restrictions == null) { |
| 6211 | return false; |
| 6212 | } |
| 6213 | if (!restrictions[restriction]) { |
| 6214 | return false; |
| 6215 | } |
| 6216 | if (perUserExcludedPackages == null) { |
| 6217 | return true; |
| 6218 | } |
| 6219 | String[] perUserExclusions = perUserExcludedPackages.get(userId); |
| 6220 | if (perUserExclusions == null) { |
| 6221 | return true; |
| 6222 | } |
| 6223 | return !ArrayUtils.contains(perUserExclusions, packageName); |
| 6224 | } |
| 6225 | |
| 6226 | public void removeUser(int userId) { |
| 6227 | if (perUserExcludedPackages != null) { |
| 6228 | perUserExcludedPackages.remove(userId); |
| 6229 | if (perUserExcludedPackages.size() <= 0) { |
| 6230 | perUserExcludedPackages = null; |
| 6231 | } |
| 6232 | } |
Sudheer Shanka | bc2fadd | 2016-09-27 17:36:39 -0700 | [diff] [blame] | 6233 | if (perUserRestrictions != null) { |
| 6234 | perUserRestrictions.remove(userId); |
| 6235 | if (perUserRestrictions.size() <= 0) { |
| 6236 | perUserRestrictions = null; |
| 6237 | } |
| 6238 | } |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 6239 | } |
| 6240 | |
| 6241 | public boolean isDefault() { |
| 6242 | return perUserRestrictions == null || perUserRestrictions.size() <= 0; |
| 6243 | } |
| 6244 | |
| 6245 | @Override |
| 6246 | public void binderDied() { |
| 6247 | synchronized (AppOpsService.this) { |
| 6248 | mOpUserRestrictions.remove(token); |
| 6249 | if (perUserRestrictions == null) { |
| 6250 | return; |
| 6251 | } |
| 6252 | final int userCount = perUserRestrictions.size(); |
| 6253 | for (int i = 0; i < userCount; i++) { |
| 6254 | final boolean[] restrictions = perUserRestrictions.valueAt(i); |
| 6255 | final int restrictionCount = restrictions.length; |
| 6256 | for (int j = 0; j < restrictionCount; j++) { |
| 6257 | if (restrictions[j]) { |
| 6258 | final int changedCode = j; |
Svet Ganov | 3a95f83 | 2018-03-23 17:44:30 -0700 | [diff] [blame] | 6259 | mHandler.post(() -> notifyWatchersOfChange(changedCode, UID_ANY)); |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 6260 | } |
| 6261 | } |
| 6262 | } |
| 6263 | destroy(); |
| 6264 | } |
| 6265 | } |
| 6266 | |
| 6267 | public void destroy() { |
| 6268 | token.unlinkToDeath(this, 0); |
| 6269 | } |
| 6270 | |
| 6271 | private boolean isDefault(boolean[] array) { |
| 6272 | if (ArrayUtils.isEmpty(array)) { |
| 6273 | return true; |
| 6274 | } |
| 6275 | for (boolean value : array) { |
| 6276 | if (value) { |
| 6277 | return false; |
| 6278 | } |
| 6279 | } |
| 6280 | return true; |
| 6281 | } |
| 6282 | } |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 6283 | |
| 6284 | private final class AppOpsManagerInternalImpl extends AppOpsManagerInternal { |
| 6285 | @Override public void setDeviceAndProfileOwners(SparseIntArray owners) { |
| 6286 | synchronized (AppOpsService.this) { |
| 6287 | mProfileOwners = owners; |
| 6288 | } |
| 6289 | } |
Hui Yu | 88910de | 2019-12-16 14:35:27 -0800 | [diff] [blame] | 6290 | |
| 6291 | @Override |
| 6292 | public void updateAppWidgetVisibility(SparseArray<String> uidPackageNames, |
| 6293 | boolean visible) { |
| 6294 | AppOpsService.this.updateAppWidgetVisibility(uidPackageNames, visible); |
| 6295 | } |
Hai Zhang | f4ef039 | 2020-02-03 14:49:35 -0800 | [diff] [blame] | 6296 | |
| 6297 | @Override |
Hai Zhang | 8846aaa | 2020-02-13 15:19:54 -0800 | [diff] [blame] | 6298 | public void setUidModeFromPermissionPolicy(int code, int uid, int mode, |
| 6299 | @Nullable IAppOpsCallback callback) { |
| 6300 | setUidMode(code, uid, mode, callback); |
Hai Zhang | f4ef039 | 2020-02-03 14:49:35 -0800 | [diff] [blame] | 6301 | } |
| 6302 | |
| 6303 | @Override |
Hai Zhang | 8846aaa | 2020-02-13 15:19:54 -0800 | [diff] [blame] | 6304 | public void setModeFromPermissionPolicy(int code, int uid, @NonNull String packageName, |
| 6305 | int mode, @Nullable IAppOpsCallback callback) { |
| 6306 | setMode(code, uid, packageName, mode, callback); |
Hai Zhang | f4ef039 | 2020-02-03 14:49:35 -0800 | [diff] [blame] | 6307 | } |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 6308 | } |
David Cheung | 2ead966 | 2020-02-19 16:11:06 -0800 | [diff] [blame] | 6309 | |
| 6310 | |
| 6311 | /** |
| 6312 | * Async task for writing note op stack trace, op code, package name and version to file |
| 6313 | * More specifically, writes all the collected ops from {@link #mNoteOpCallerStacktraces} |
| 6314 | */ |
| 6315 | private void writeNoteOps() { |
| 6316 | synchronized (this) { |
| 6317 | mWriteNoteOpsScheduled = false; |
| 6318 | } |
| 6319 | synchronized (mNoteOpCallerStacktracesFile) { |
| 6320 | try (FileWriter writer = new FileWriter(mNoteOpCallerStacktracesFile)) { |
| 6321 | int numTraces = mNoteOpCallerStacktraces.size(); |
| 6322 | for (int i = 0; i < numTraces; i++) { |
| 6323 | // Writing json formatted string into file |
| 6324 | writer.write(mNoteOpCallerStacktraces.valueAt(i).asJson()); |
| 6325 | // Comma separation, so we can wrap the entire log as a JSON object |
| 6326 | // when all results are collected |
| 6327 | writer.write(","); |
| 6328 | } |
| 6329 | } catch (IOException e) { |
| 6330 | Slog.w(TAG, "Failed to load opsValidation file for FileWriter", e); |
| 6331 | } |
| 6332 | } |
| 6333 | } |
| 6334 | |
| 6335 | /** |
| 6336 | * This class represents a NoteOp Trace object amd contains the necessary fields that will |
| 6337 | * be written to file to use for permissions data validation in JSON format |
| 6338 | */ |
| 6339 | @Immutable |
| 6340 | static class NoteOpTrace { |
| 6341 | static final String STACKTRACE = "stackTrace"; |
| 6342 | static final String OP = "op"; |
| 6343 | static final String PACKAGENAME = "packageName"; |
| 6344 | static final String VERSION = "version"; |
| 6345 | |
| 6346 | private final @NonNull String mStackTrace; |
| 6347 | private final int mOp; |
| 6348 | private final @Nullable String mPackageName; |
| 6349 | private final long mVersion; |
| 6350 | |
| 6351 | /** |
| 6352 | * Initialize a NoteOp object using a JSON object containing the necessary fields |
| 6353 | * |
| 6354 | * @param jsonTrace JSON object represented as a string |
| 6355 | * |
| 6356 | * @return NoteOpTrace object initialized with JSON fields |
| 6357 | */ |
| 6358 | static NoteOpTrace fromJson(String jsonTrace) { |
| 6359 | try { |
| 6360 | // Re-add closing bracket which acted as a delimiter by the reader |
| 6361 | JSONObject obj = new JSONObject(jsonTrace.concat("}")); |
| 6362 | return new NoteOpTrace(obj.getString(STACKTRACE), obj.getInt(OP), |
| 6363 | obj.getString(PACKAGENAME), obj.getLong(VERSION)); |
| 6364 | } catch (JSONException e) { |
| 6365 | // Swallow error, only meant for logging ops, should not affect flow of the code |
| 6366 | Slog.e(TAG, "Error constructing NoteOpTrace object " |
| 6367 | + "JSON trace format incorrect", e); |
| 6368 | return null; |
| 6369 | } |
| 6370 | } |
| 6371 | |
| 6372 | NoteOpTrace(String stackTrace, int op, String packageName, long version) { |
| 6373 | mStackTrace = stackTrace; |
| 6374 | mOp = op; |
| 6375 | mPackageName = packageName; |
| 6376 | mVersion = version; |
| 6377 | } |
| 6378 | |
| 6379 | @Override |
| 6380 | public boolean equals(Object o) { |
| 6381 | if (this == o) return true; |
| 6382 | if (o == null || getClass() != o.getClass()) return false; |
| 6383 | NoteOpTrace that = (NoteOpTrace) o; |
| 6384 | return mOp == that.mOp |
| 6385 | && mVersion == that.mVersion |
| 6386 | && mStackTrace.equals(that.mStackTrace) |
| 6387 | && Objects.equals(mPackageName, that.mPackageName); |
| 6388 | } |
| 6389 | |
| 6390 | @Override |
| 6391 | public int hashCode() { |
| 6392 | return Objects.hash(mStackTrace, mOp, mPackageName, mVersion); |
| 6393 | } |
| 6394 | |
| 6395 | /** |
| 6396 | * The object is formatted as a JSON object and returned as a String |
| 6397 | * |
| 6398 | * @return JSON formatted string |
| 6399 | */ |
| 6400 | public String asJson() { |
| 6401 | return "{" |
| 6402 | + "\"" + STACKTRACE + "\":\"" + mStackTrace.replace("\n", "\\n") |
| 6403 | + '\"' + ",\"" + OP + "\":" + mOp |
| 6404 | + ",\"" + PACKAGENAME + "\":\"" + mPackageName + '\"' |
| 6405 | + ",\"" + VERSION + "\":" + mVersion |
| 6406 | + '}'; |
| 6407 | } |
| 6408 | } |
| 6409 | |
| 6410 | /** |
| 6411 | * Collects noteOps, noteProxyOps and startOps from AppOpsManager and writes it into a file |
| 6412 | * which will be used for permissions data validation, the given parameters to this method |
| 6413 | * will be logged in json format |
| 6414 | * |
| 6415 | * @param stackTrace stacktrace from the most recent call in AppOpsManager |
| 6416 | * @param op op code |
| 6417 | * @param packageName package making call |
| 6418 | * @param version android version for this call |
| 6419 | */ |
| 6420 | @Override |
| 6421 | public void collectNoteOpCallsForValidation(String stackTrace, int op, String packageName, |
| 6422 | long version) { |
| 6423 | if (!AppOpsManager.NOTE_OP_COLLECTION_ENABLED) { |
| 6424 | return; |
| 6425 | } |
| 6426 | |
| 6427 | Objects.requireNonNull(stackTrace); |
| 6428 | Preconditions.checkArgument(op >= 0); |
| 6429 | Preconditions.checkArgument(op < AppOpsManager._NUM_OP); |
| 6430 | Objects.requireNonNull(version); |
| 6431 | |
| 6432 | NoteOpTrace noteOpTrace = new NoteOpTrace(stackTrace, op, packageName, version); |
| 6433 | |
| 6434 | boolean noteOpSetWasChanged; |
| 6435 | synchronized (this) { |
| 6436 | noteOpSetWasChanged = mNoteOpCallerStacktraces.add(noteOpTrace); |
| 6437 | if (noteOpSetWasChanged && !mWriteNoteOpsScheduled) { |
| 6438 | mWriteNoteOpsScheduled = true; |
| 6439 | mHandler.postDelayed(PooledLambda.obtainRunnable((that) -> { |
| 6440 | AsyncTask.execute(() -> { |
| 6441 | that.writeNoteOps(); |
| 6442 | }); |
| 6443 | }, this), 2500); |
| 6444 | } |
| 6445 | } |
| 6446 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 6447 | } |