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 | |
| 17 | package com.android.server; |
| 18 | |
Suprabh Shukla | 3017fe4 | 2018-11-08 19:00:01 -0800 | [diff] [blame] | 19 | import static android.app.AppOpsManager.OP_PLAY_AUDIO; |
Hai Zhang | 2b98fb3 | 2018-09-21 15:18:46 -0700 | [diff] [blame] | 20 | import static android.app.AppOpsManager.UID_STATE_BACKGROUND; |
| 21 | import static android.app.AppOpsManager.UID_STATE_CACHED; |
| 22 | import static android.app.AppOpsManager.UID_STATE_FOREGROUND; |
| 23 | import static android.app.AppOpsManager.UID_STATE_FOREGROUND_SERVICE; |
| 24 | import static android.app.AppOpsManager.UID_STATE_LAST_NON_RESTRICTED; |
| 25 | import static android.app.AppOpsManager.UID_STATE_PERSISTENT; |
| 26 | import static android.app.AppOpsManager.UID_STATE_TOP; |
| 27 | import static android.app.AppOpsManager._NUM_UID_STATE; |
| 28 | |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 29 | import android.Manifest; |
Svet Ganov | ad0a49b | 2018-10-29 10:07:08 -0700 | [diff] [blame] | 30 | import android.annotation.NonNull; |
| 31 | import android.annotation.Nullable; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 32 | import android.app.ActivityManager; |
| 33 | import android.app.ActivityThread; |
| 34 | import android.app.AppGlobals; |
| 35 | import android.app.AppOpsManager; |
Svet Ganov | ad0a49b | 2018-10-29 10:07:08 -0700 | [diff] [blame] | 36 | import android.app.AppOpsManager.HistoricalOpEntry; |
| 37 | import android.app.AppOpsManager.HistoricalPackageOps; |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 38 | import android.app.AppOpsManagerInternal; |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 39 | import android.app.AppOpsManagerInternal.CheckOpsDelegate; |
Suprabh Shukla | 3017fe4 | 2018-11-08 19:00:01 -0800 | [diff] [blame] | 40 | import android.content.BroadcastReceiver; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 41 | import android.content.ContentResolver; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 42 | import android.content.Context; |
Suprabh Shukla | 3017fe4 | 2018-11-08 19:00:01 -0800 | [diff] [blame] | 43 | import android.content.Intent; |
| 44 | import android.content.IntentFilter; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 45 | import android.content.pm.ApplicationInfo; |
| 46 | import android.content.pm.IPackageManager; |
| 47 | import android.content.pm.PackageManager; |
| 48 | import android.content.pm.PackageManagerInternal; |
Svet Ganov | ad0a49b | 2018-10-29 10:07:08 -0700 | [diff] [blame] | 49 | import android.content.pm.ParceledListSlice; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 50 | import android.content.pm.UserInfo; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 51 | import android.database.ContentObserver; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 52 | import android.media.AudioAttributes; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 53 | import android.net.Uri; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 54 | import android.os.AsyncTask; |
| 55 | import android.os.Binder; |
| 56 | import android.os.Bundle; |
| 57 | import android.os.Handler; |
| 58 | import android.os.IBinder; |
| 59 | import android.os.Process; |
| 60 | import android.os.RemoteException; |
| 61 | import android.os.ResultReceiver; |
| 62 | import android.os.ServiceManager; |
| 63 | import android.os.ShellCallback; |
| 64 | import android.os.ShellCommand; |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 65 | import android.os.SystemClock; |
Sudheer Shanka | 98cb3f0 | 2018-08-17 16:10:29 -0700 | [diff] [blame] | 66 | import android.os.SystemProperties; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 67 | import android.os.UserHandle; |
| 68 | import android.os.UserManager; |
Sudheer Shanka | 98cb3f0 | 2018-08-17 16:10:29 -0700 | [diff] [blame] | 69 | import android.os.storage.StorageManager; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 70 | import android.os.storage.StorageManagerInternal; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 71 | import android.provider.Settings; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 72 | import android.util.ArrayMap; |
| 73 | import android.util.ArraySet; |
| 74 | import android.util.AtomicFile; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 75 | import android.util.KeyValueListParser; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 76 | import android.util.Slog; |
| 77 | import android.util.SparseArray; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 78 | import android.util.SparseBooleanArray; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 79 | import android.util.SparseIntArray; |
| 80 | import android.util.TimeUtils; |
| 81 | import android.util.Xml; |
| 82 | |
Todd Kennedy | 556efba | 2018-11-15 07:43:55 -0800 | [diff] [blame^] | 83 | import com.android.internal.annotations.GuardedBy; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 84 | import com.android.internal.annotations.VisibleForTesting; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 85 | import com.android.internal.app.IAppOpsActiveCallback; |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 86 | import com.android.internal.app.IAppOpsCallback; |
| 87 | import com.android.internal.app.IAppOpsService; |
| 88 | import com.android.internal.os.Zygote; |
| 89 | import com.android.internal.util.ArrayUtils; |
| 90 | import com.android.internal.util.DumpUtils; |
| 91 | import com.android.internal.util.FastXmlSerializer; |
| 92 | import com.android.internal.util.Preconditions; |
| 93 | import com.android.internal.util.XmlUtils; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 94 | import com.android.internal.util.function.pooled.PooledLambda; |
Julia Reynolds | 6cb5fcc | 2018-02-27 17:33:52 -0500 | [diff] [blame] | 95 | |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 96 | import libcore.util.EmptyArray; |
| 97 | |
| 98 | import org.xmlpull.v1.XmlPullParser; |
| 99 | import org.xmlpull.v1.XmlPullParserException; |
| 100 | import org.xmlpull.v1.XmlSerializer; |
| 101 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 102 | import java.io.File; |
| 103 | import java.io.FileDescriptor; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 104 | import java.io.FileInputStream; |
| 105 | import java.io.FileNotFoundException; |
| 106 | import java.io.FileOutputStream; |
| 107 | import java.io.IOException; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 108 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 109 | import java.nio.charset.StandardCharsets; |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 110 | import java.text.SimpleDateFormat; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 111 | import java.util.ArrayList; |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 112 | import java.util.Arrays; |
Svetoslav | 215b44a | 2015-08-04 19:03:40 -0700 | [diff] [blame] | 113 | import java.util.Collections; |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 114 | import java.util.Date; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 115 | import java.util.HashMap; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 116 | import java.util.Iterator; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 117 | import java.util.List; |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 118 | import java.util.Map; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 119 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 120 | public class AppOpsService extends IAppOpsService.Stub { |
| 121 | static final String TAG = "AppOps"; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 122 | static final boolean DEBUG = false; |
| 123 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 124 | private static final int NO_VERSION = -1; |
| 125 | /** Increment by one every time and add the corresponding upgrade logic in |
| 126 | * {@link #upgradeLocked(int)} below. The first version was 1 */ |
| 127 | private static final int CURRENT_VERSION = 1; |
| 128 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 129 | // Write at most every 30 minutes. |
| 130 | static final long WRITE_DELAY = DEBUG ? 1000 : 30*60*1000; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 131 | |
Svet Ganov | 3a95f83 | 2018-03-23 17:44:30 -0700 | [diff] [blame] | 132 | // Constant meaning that any UID should be matched when dispatching callbacks |
| 133 | private static final int UID_ANY = -2; |
| 134 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 135 | // Map from process states to the uid states we track. |
| 136 | private static final int[] PROCESS_STATE_TO_UID_STATE = new int[] { |
| 137 | UID_STATE_PERSISTENT, // ActivityManager.PROCESS_STATE_PERSISTENT |
| 138 | UID_STATE_PERSISTENT, // ActivityManager.PROCESS_STATE_PERSISTENT_UI |
| 139 | UID_STATE_TOP, // ActivityManager.PROCESS_STATE_TOP |
| 140 | UID_STATE_FOREGROUND_SERVICE, // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE |
| 141 | UID_STATE_FOREGROUND, // ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE |
| 142 | UID_STATE_FOREGROUND, // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND |
| 143 | UID_STATE_BACKGROUND, // ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND |
| 144 | UID_STATE_BACKGROUND, // ActivityManager.PROCESS_STATE_TRANSIENT_BACKGROUND |
| 145 | UID_STATE_BACKGROUND, // ActivityManager.PROCESS_STATE_BACKUP |
| 146 | UID_STATE_BACKGROUND, // ActivityManager.PROCESS_STATE_SERVICE |
| 147 | UID_STATE_BACKGROUND, // ActivityManager.PROCESS_STATE_RECEIVER |
| 148 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_TOP_SLEEPING |
| 149 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_HEAVY_WEIGHT |
| 150 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_HOME |
| 151 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_LAST_ACTIVITY |
| 152 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY |
| 153 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT |
| 154 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_CACHED_RECENT |
| 155 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_CACHED_EMPTY |
| 156 | UID_STATE_CACHED, // ActivityManager.PROCESS_STATE_NONEXISTENT |
| 157 | }; |
| 158 | |
| 159 | static final String[] UID_STATE_NAMES = new String[] { |
| 160 | "pers ", // UID_STATE_PERSISTENT |
| 161 | "top ", // UID_STATE_TOP |
| 162 | "fgsvc", // UID_STATE_FOREGROUND_SERVICE |
| 163 | "fg ", // UID_STATE_FOREGROUND |
| 164 | "bg ", // UID_STATE_BACKGROUND |
| 165 | "cch ", // UID_STATE_CACHED |
| 166 | }; |
| 167 | |
| 168 | static final String[] UID_STATE_TIME_ATTRS = new String[] { |
| 169 | "tp", // UID_STATE_PERSISTENT |
| 170 | "tt", // UID_STATE_TOP |
| 171 | "tfs", // UID_STATE_FOREGROUND_SERVICE |
| 172 | "tf", // UID_STATE_FOREGROUND |
| 173 | "tb", // UID_STATE_BACKGROUND |
| 174 | "tc", // UID_STATE_CACHED |
| 175 | }; |
| 176 | |
| 177 | static final String[] UID_STATE_REJECT_ATTRS = new String[] { |
| 178 | "rp", // UID_STATE_PERSISTENT |
| 179 | "rt", // UID_STATE_TOP |
| 180 | "rfs", // UID_STATE_FOREGROUND_SERVICE |
| 181 | "rf", // UID_STATE_FOREGROUND |
| 182 | "rb", // UID_STATE_BACKGROUND |
| 183 | "rc", // UID_STATE_CACHED |
| 184 | }; |
| 185 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 186 | Context mContext; |
| 187 | final AtomicFile mFile; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 188 | final Handler mHandler; |
| 189 | |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 190 | private final AppOpsManagerInternalImpl mAppOpsManagerInternal |
| 191 | = new AppOpsManagerInternalImpl(); |
| 192 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 193 | boolean mWriteScheduled; |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 194 | boolean mFastWriteScheduled; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 195 | final Runnable mWriteRunner = new Runnable() { |
| 196 | public void run() { |
| 197 | synchronized (AppOpsService.this) { |
| 198 | mWriteScheduled = false; |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 199 | mFastWriteScheduled = false; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 200 | AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() { |
| 201 | @Override protected Void doInBackground(Void... params) { |
| 202 | writeState(); |
| 203 | return null; |
| 204 | } |
| 205 | }; |
| 206 | task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null); |
| 207 | } |
| 208 | } |
| 209 | }; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 210 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 211 | @VisibleForTesting |
| 212 | final SparseArray<UidState> mUidStates = new SparseArray<>(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 213 | |
Dianne Hackborn | 9fb9350 | 2018-06-18 12:29:44 -0700 | [diff] [blame] | 214 | long mLastRealtime; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 215 | |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 216 | /* |
| 217 | * These are app op restrictions imposed per user from various parties. |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 218 | */ |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 219 | private final ArrayMap<IBinder, ClientRestrictionState> mOpUserRestrictions = new ArrayMap<>(); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 220 | |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 221 | SparseIntArray mProfileOwners; |
| 222 | |
Todd Kennedy | 556efba | 2018-11-15 07:43:55 -0800 | [diff] [blame^] | 223 | @GuardedBy("this") |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 224 | private CheckOpsDelegate mCheckOpsDelegate; |
| 225 | |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 226 | /** |
| 227 | * All times are in milliseconds. These constants are kept synchronized with the system |
| 228 | * global Settings. Any access to this class or its fields should be done while |
| 229 | * holding the AppOpsService lock. |
| 230 | */ |
| 231 | private final class Constants extends ContentObserver { |
| 232 | // Key names stored in the settings value. |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 233 | private static final String KEY_TOP_STATE_SETTLE_TIME = "top_state_settle_time"; |
| 234 | private static final String KEY_FG_SERVICE_STATE_SETTLE_TIME |
| 235 | = "fg_service_state_settle_time"; |
| 236 | private static final String KEY_BG_STATE_SETTLE_TIME = "bg_state_settle_time"; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 237 | |
| 238 | /** |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 239 | * 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] | 240 | * @see Settings.Global#APP_OPS_CONSTANTS |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 241 | * @see #KEY_TOP_STATE_SETTLE_TIME |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 242 | */ |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 243 | public long TOP_STATE_SETTLE_TIME; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 244 | |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 245 | /** |
| 246 | * How long we want for a drop in uid state from foreground to settle before applying it. |
| 247 | * @see Settings.Global#APP_OPS_CONSTANTS |
| 248 | * @see #KEY_FG_SERVICE_STATE_SETTLE_TIME |
| 249 | */ |
| 250 | public long FG_SERVICE_STATE_SETTLE_TIME; |
| 251 | |
| 252 | /** |
| 253 | * How long we want for a drop in uid state from background to settle before applying it. |
| 254 | * @see Settings.Global#APP_OPS_CONSTANTS |
| 255 | * @see #KEY_BG_STATE_SETTLE_TIME |
| 256 | */ |
| 257 | public long BG_STATE_SETTLE_TIME; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 258 | |
| 259 | private final KeyValueListParser mParser = new KeyValueListParser(','); |
| 260 | private ContentResolver mResolver; |
| 261 | |
| 262 | public Constants(Handler handler) { |
| 263 | super(handler); |
| 264 | updateConstants(); |
| 265 | } |
| 266 | |
| 267 | public void startMonitoring(ContentResolver resolver) { |
| 268 | mResolver = resolver; |
| 269 | mResolver.registerContentObserver( |
Dianne Hackborn | 45c79b0 | 2018-05-11 09:46:13 -0700 | [diff] [blame] | 270 | Settings.Global.getUriFor(Settings.Global.APP_OPS_CONSTANTS), |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 271 | false, this); |
| 272 | updateConstants(); |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public void onChange(boolean selfChange, Uri uri) { |
| 277 | updateConstants(); |
| 278 | } |
| 279 | |
| 280 | private void updateConstants() { |
Dianne Hackborn | 45c79b0 | 2018-05-11 09:46:13 -0700 | [diff] [blame] | 281 | String value = mResolver != null ? Settings.Global.getString(mResolver, |
| 282 | Settings.Global.APP_OPS_CONSTANTS) : ""; |
| 283 | |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 284 | synchronized (AppOpsService.this) { |
| 285 | try { |
Dianne Hackborn | 45c79b0 | 2018-05-11 09:46:13 -0700 | [diff] [blame] | 286 | mParser.setString(value); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 287 | } catch (IllegalArgumentException e) { |
| 288 | // Failed to parse the settings string, log this and move on |
| 289 | // with defaults. |
| 290 | Slog.e(TAG, "Bad app ops settings", e); |
| 291 | } |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 292 | TOP_STATE_SETTLE_TIME = mParser.getDurationMillis( |
| 293 | KEY_TOP_STATE_SETTLE_TIME, 30 * 1000L); |
| 294 | FG_SERVICE_STATE_SETTLE_TIME = mParser.getDurationMillis( |
| 295 | KEY_FG_SERVICE_STATE_SETTLE_TIME, 10 * 1000L); |
| 296 | BG_STATE_SETTLE_TIME = mParser.getDurationMillis( |
| 297 | KEY_BG_STATE_SETTLE_TIME, 1 * 1000L); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | void dump(PrintWriter pw) { |
| 302 | pw.println(" Settings:"); |
| 303 | |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 304 | pw.print(" "); pw.print(KEY_TOP_STATE_SETTLE_TIME); pw.print("="); |
| 305 | TimeUtils.formatDuration(TOP_STATE_SETTLE_TIME, pw); |
Dianne Hackborn | b94d82f | 2018-05-16 17:03:01 -0700 | [diff] [blame] | 306 | pw.println(); |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 307 | pw.print(" "); pw.print(KEY_FG_SERVICE_STATE_SETTLE_TIME); pw.print("="); |
| 308 | TimeUtils.formatDuration(FG_SERVICE_STATE_SETTLE_TIME, pw); |
Dianne Hackborn | b94d82f | 2018-05-16 17:03:01 -0700 | [diff] [blame] | 309 | pw.println(); |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 310 | pw.print(" "); pw.print(KEY_BG_STATE_SETTLE_TIME); pw.print("="); |
| 311 | TimeUtils.formatDuration(BG_STATE_SETTLE_TIME, pw); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 312 | pw.println(); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | private final Constants mConstants; |
| 317 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 318 | @VisibleForTesting |
| 319 | static final class UidState { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 320 | public final int uid; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 321 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 322 | public int state = UID_STATE_CACHED; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 323 | public int pendingState = UID_STATE_CACHED; |
| 324 | public long pendingStateCommitTime; |
| 325 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 326 | public int startNesting; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 327 | public ArrayMap<String, Ops> pkgOps; |
| 328 | public SparseIntArray opModes; |
| 329 | |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 330 | // 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] | 331 | public SparseBooleanArray foregroundOps; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 332 | public boolean hasForegroundWatchers; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 333 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 334 | public UidState(int uid) { |
| 335 | this.uid = uid; |
| 336 | } |
| 337 | |
| 338 | public void clear() { |
| 339 | pkgOps = null; |
| 340 | opModes = null; |
| 341 | } |
| 342 | |
| 343 | public boolean isDefault() { |
| 344 | return (pkgOps == null || pkgOps.isEmpty()) |
| 345 | && (opModes == null || opModes.size() <= 0); |
| 346 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 347 | |
| 348 | int evalMode(int mode) { |
| 349 | if (mode == AppOpsManager.MODE_FOREGROUND) { |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 350 | return state <= UID_STATE_LAST_NON_RESTRICTED |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 351 | ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED; |
| 352 | } |
| 353 | return mode; |
| 354 | } |
| 355 | |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 356 | private void evalForegroundWatchers(int op, SparseArray<ArraySet<ModeCallback>> watchers, |
| 357 | SparseBooleanArray which) { |
| 358 | boolean curValue = which.get(op, false); |
| 359 | ArraySet<ModeCallback> callbacks = watchers.get(op); |
| 360 | if (callbacks != null) { |
| 361 | for (int cbi = callbacks.size() - 1; !curValue && cbi >= 0; cbi--) { |
| 362 | if ((callbacks.valueAt(cbi).mFlags |
| 363 | & AppOpsManager.WATCH_FOREGROUND_CHANGES) != 0) { |
| 364 | hasForegroundWatchers = true; |
| 365 | curValue = true; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | which.put(op, curValue); |
| 370 | } |
| 371 | |
| 372 | public void evalForegroundOps(SparseArray<ArraySet<ModeCallback>> watchers) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 373 | SparseBooleanArray which = null; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 374 | hasForegroundWatchers = false; |
| 375 | if (opModes != null) { |
| 376 | for (int i = opModes.size() - 1; i >= 0; i--) { |
| 377 | if (opModes.valueAt(i) == AppOpsManager.MODE_FOREGROUND) { |
| 378 | if (which == null) { |
| 379 | which = new SparseBooleanArray(); |
| 380 | } |
| 381 | evalForegroundWatchers(opModes.keyAt(i), watchers, which); |
| 382 | } |
| 383 | } |
| 384 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 385 | if (pkgOps != null) { |
| 386 | for (int i = pkgOps.size() - 1; i >= 0; i--) { |
| 387 | Ops ops = pkgOps.valueAt(i); |
| 388 | for (int j = ops.size() - 1; j >= 0; j--) { |
| 389 | if (ops.valueAt(j).mode == AppOpsManager.MODE_FOREGROUND) { |
| 390 | if (which == null) { |
| 391 | which = new SparseBooleanArray(); |
| 392 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 393 | evalForegroundWatchers(ops.keyAt(j), watchers, which); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | foregroundOps = which; |
| 399 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 402 | final static class Ops extends SparseArray<Op> { |
| 403 | final String packageName; |
| 404 | final UidState uidState; |
| 405 | final boolean isPrivileged; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 406 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 407 | Ops(String _packageName, UidState _uidState, boolean _isPrivileged) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 408 | packageName = _packageName; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 409 | uidState = _uidState; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 410 | isPrivileged = _isPrivileged; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 414 | final static class Op { |
| 415 | final UidState uidState; |
| 416 | final int uid; |
| 417 | final String packageName; |
| 418 | final int op; |
| 419 | int proxyUid = -1; |
| 420 | String proxyPackageName; |
| 421 | int mode; |
| 422 | int duration; |
| 423 | long time[] = new long[_NUM_UID_STATE]; |
| 424 | long rejectTime[] = new long[_NUM_UID_STATE]; |
| 425 | int startNesting; |
| 426 | long startRealtime; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 427 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 428 | Op(UidState _uidState, String _packageName, int _op) { |
| 429 | uidState = _uidState; |
| 430 | uid = _uidState.uid; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 431 | packageName = _packageName; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 432 | op = _op; |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 433 | mode = AppOpsManager.opToDefaultMode(op); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 434 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 435 | |
| 436 | boolean hasAnyTime() { |
| 437 | for (int i = 0; i < AppOpsManager._NUM_UID_STATE; i++) { |
| 438 | if (time[i] != 0) { |
| 439 | return true; |
| 440 | } |
| 441 | if (rejectTime[i] != 0) { |
| 442 | return true; |
| 443 | } |
| 444 | } |
| 445 | return false; |
| 446 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 447 | |
| 448 | int getMode() { |
| 449 | return uidState.evalMode(mode); |
| 450 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 453 | final SparseArray<ArraySet<ModeCallback>> mOpModeWatchers = new SparseArray<>(); |
| 454 | final ArrayMap<String, ArraySet<ModeCallback>> mPackageModeWatchers = new ArrayMap<>(); |
| 455 | final ArrayMap<IBinder, ModeCallback> mModeWatchers = new ArrayMap<>(); |
| 456 | final ArrayMap<IBinder, SparseArray<ActiveCallback>> mActiveWatchers = new ArrayMap<>(); |
Dianne Hackborn | 68d7655 | 2017-02-27 15:32:03 -0800 | [diff] [blame] | 457 | final SparseArray<SparseArray<Restriction>> mAudioRestrictions = new SparseArray<>(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 458 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 459 | final class ModeCallback implements DeathRecipient { |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 460 | final IAppOpsCallback mCallback; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 461 | final int mWatchingUid; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 462 | final int mFlags; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 463 | final int mCallingUid; |
| 464 | final int mCallingPid; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 465 | |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 466 | ModeCallback(IAppOpsCallback callback, int watchingUid, int flags, int callingUid, |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 467 | int callingPid) { |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 468 | mCallback = callback; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 469 | mWatchingUid = watchingUid; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 470 | mFlags = flags; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 471 | mCallingUid = callingUid; |
| 472 | mCallingPid = callingPid; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 473 | try { |
| 474 | mCallback.asBinder().linkToDeath(this, 0); |
| 475 | } catch (RemoteException e) { |
| 476 | } |
| 477 | } |
| 478 | |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 479 | public boolean isWatchingUid(int uid) { |
| 480 | return uid == UID_ANY || mWatchingUid < 0 || mWatchingUid == uid; |
| 481 | } |
| 482 | |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 483 | @Override |
| 484 | public String toString() { |
| 485 | StringBuilder sb = new StringBuilder(128); |
| 486 | sb.append("ModeCallback{"); |
| 487 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 488 | sb.append(" watchinguid="); |
| 489 | UserHandle.formatUid(sb, mWatchingUid); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 490 | sb.append(" flags=0x"); |
| 491 | sb.append(Integer.toHexString(mFlags)); |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 492 | sb.append(" from uid="); |
| 493 | UserHandle.formatUid(sb, mCallingUid); |
| 494 | sb.append(" pid="); |
| 495 | sb.append(mCallingPid); |
| 496 | sb.append('}'); |
| 497 | return sb.toString(); |
| 498 | } |
| 499 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 500 | void unlinkToDeath() { |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 501 | mCallback.asBinder().unlinkToDeath(this, 0); |
| 502 | } |
| 503 | |
| 504 | @Override |
| 505 | public void binderDied() { |
| 506 | stopWatchingMode(mCallback); |
| 507 | } |
| 508 | } |
| 509 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 510 | final class ActiveCallback implements DeathRecipient { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 511 | final IAppOpsActiveCallback mCallback; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 512 | final int mWatchingUid; |
| 513 | final int mCallingUid; |
| 514 | final int mCallingPid; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 515 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 516 | ActiveCallback(IAppOpsActiveCallback callback, int watchingUid, int callingUid, |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 517 | int callingPid) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 518 | mCallback = callback; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 519 | mWatchingUid = watchingUid; |
| 520 | mCallingUid = callingUid; |
| 521 | mCallingPid = callingPid; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 522 | try { |
| 523 | mCallback.asBinder().linkToDeath(this, 0); |
| 524 | } catch (RemoteException e) { |
| 525 | } |
| 526 | } |
| 527 | |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 528 | @Override |
| 529 | public String toString() { |
| 530 | StringBuilder sb = new StringBuilder(128); |
| 531 | sb.append("ActiveCallback{"); |
| 532 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 533 | sb.append(" watchinguid="); |
| 534 | UserHandle.formatUid(sb, mWatchingUid); |
| 535 | sb.append(" from uid="); |
| 536 | UserHandle.formatUid(sb, mCallingUid); |
| 537 | sb.append(" pid="); |
| 538 | sb.append(mCallingPid); |
| 539 | sb.append('}'); |
| 540 | return sb.toString(); |
| 541 | } |
| 542 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 543 | void destroy() { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 544 | mCallback.asBinder().unlinkToDeath(this, 0); |
| 545 | } |
| 546 | |
| 547 | @Override |
| 548 | public void binderDied() { |
| 549 | stopWatchingActive(mCallback); |
| 550 | } |
| 551 | } |
| 552 | |
Svet Ganov | a7a0db6 | 2018-02-27 20:08:01 -0800 | [diff] [blame] | 553 | final ArrayMap<IBinder, ClientState> mClients = new ArrayMap<>(); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 554 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 555 | final class ClientState extends Binder implements DeathRecipient { |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 556 | final ArrayList<Op> mStartedOps = new ArrayList<>(); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 557 | final IBinder mAppToken; |
| 558 | final int mPid; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 559 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 560 | ClientState(IBinder appToken) { |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 561 | mAppToken = appToken; |
| 562 | mPid = Binder.getCallingPid(); |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 563 | // Watch only for remote processes dying |
| 564 | if (!(appToken instanceof Binder)) { |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 565 | try { |
| 566 | mAppToken.linkToDeath(this, 0); |
| 567 | } catch (RemoteException e) { |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 568 | /* do nothing */ |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | @Override |
| 574 | public String toString() { |
| 575 | return "ClientState{" + |
| 576 | "mAppToken=" + mAppToken + |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 577 | ", " + "pid=" + mPid + |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 578 | '}'; |
| 579 | } |
| 580 | |
| 581 | @Override |
| 582 | public void binderDied() { |
| 583 | synchronized (AppOpsService.this) { |
| 584 | for (int i=mStartedOps.size()-1; i>=0; i--) { |
Svet Ganov | a7a0db6 | 2018-02-27 20:08:01 -0800 | [diff] [blame] | 585 | finishOperationLocked(mStartedOps.get(i), /*finishNested*/ true); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 586 | } |
| 587 | mClients.remove(mAppToken); |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 592 | public AppOpsService(File storagePath, Handler handler) { |
Jeff Sharkey | 5f3e934 | 2017-03-13 14:53:11 -0600 | [diff] [blame] | 593 | LockGuard.installLock(this, LockGuard.INDEX_APP_OPS); |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 594 | mFile = new AtomicFile(storagePath, "appops"); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 595 | mHandler = handler; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 596 | mConstants = new Constants(mHandler); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 597 | readState(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 598 | } |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 599 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 600 | public void publish(Context context) { |
| 601 | mContext = context; |
| 602 | ServiceManager.addService(Context.APP_OPS_SERVICE, asBinder()); |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 603 | LocalServices.addService(AppOpsManagerInternal.class, mAppOpsManagerInternal); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 604 | } |
| 605 | |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 606 | public void systemReady() { |
Dianne Hackborn | 45c79b0 | 2018-05-11 09:46:13 -0700 | [diff] [blame] | 607 | mConstants.startMonitoring(mContext.getContentResolver()); |
| 608 | |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 609 | synchronized (this) { |
| 610 | boolean changed = false; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 611 | for (int i = mUidStates.size() - 1; i >= 0; i--) { |
| 612 | UidState uidState = mUidStates.valueAt(i); |
| 613 | |
| 614 | String[] packageNames = getPackagesForUid(uidState.uid); |
| 615 | if (ArrayUtils.isEmpty(packageNames)) { |
| 616 | uidState.clear(); |
| 617 | mUidStates.removeAt(i); |
| 618 | changed = true; |
| 619 | continue; |
| 620 | } |
| 621 | |
| 622 | ArrayMap<String, Ops> pkgs = uidState.pkgOps; |
| 623 | if (pkgs == null) { |
| 624 | continue; |
| 625 | } |
| 626 | |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 627 | Iterator<Ops> it = pkgs.values().iterator(); |
| 628 | while (it.hasNext()) { |
| 629 | Ops ops = it.next(); |
Jeff Sharkey | e2ed23e | 2015-10-29 19:00:44 -0700 | [diff] [blame] | 630 | int curUid = -1; |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 631 | try { |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 632 | curUid = AppGlobals.getPackageManager().getPackageUid(ops.packageName, |
| 633 | PackageManager.MATCH_UNINSTALLED_PACKAGES, |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 634 | UserHandle.getUserId(ops.uidState.uid)); |
Jeff Sharkey | e2ed23e | 2015-10-29 19:00:44 -0700 | [diff] [blame] | 635 | } catch (RemoteException ignored) { |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 636 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 637 | if (curUid != ops.uidState.uid) { |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 638 | Slog.i(TAG, "Pruning old package " + ops.packageName |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 639 | + "/" + ops.uidState + ": new uid=" + curUid); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 640 | it.remove(); |
| 641 | changed = true; |
| 642 | } |
| 643 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 644 | |
| 645 | if (uidState.isDefault()) { |
| 646 | mUidStates.removeAt(i); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | if (changed) { |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 650 | scheduleFastWriteLocked(); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 651 | } |
| 652 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 653 | |
Suprabh Shukla | 3017fe4 | 2018-11-08 19:00:01 -0800 | [diff] [blame] | 654 | final IntentFilter packageSuspendFilter = new IntentFilter(); |
| 655 | packageSuspendFilter.addAction(Intent.ACTION_PACKAGES_UNSUSPENDED); |
| 656 | packageSuspendFilter.addAction(Intent.ACTION_PACKAGES_SUSPENDED); |
| 657 | mContext.registerReceiver(new BroadcastReceiver() { |
| 658 | @Override |
| 659 | public void onReceive(Context context, Intent intent) { |
| 660 | final int[] changedUids = intent.getIntArrayExtra(Intent.EXTRA_CHANGED_UID_LIST); |
| 661 | final String[] changedPkgs = intent.getStringArrayExtra( |
| 662 | Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 663 | final ArraySet<ModeCallback> callbacks = mOpModeWatchers.get(OP_PLAY_AUDIO); |
| 664 | for (int i = 0; i < changedUids.length; i++) { |
| 665 | final int changedUid = changedUids[i]; |
| 666 | final String changedPkg = changedPkgs[i]; |
| 667 | // We trust packagemanager to insert matching uid and packageNames in the extras |
| 668 | mHandler.sendMessage(PooledLambda.obtainMessage(AppOpsService::notifyOpChanged, |
| 669 | AppOpsService.this, callbacks, OP_PLAY_AUDIO, changedUid, changedPkg)); |
| 670 | } |
| 671 | } |
| 672 | }, packageSuspendFilter); |
| 673 | |
Suprabh Shukla | aef2513 | 2017-01-23 18:09:03 -0800 | [diff] [blame] | 674 | PackageManagerInternal packageManagerInternal = LocalServices.getService( |
| 675 | PackageManagerInternal.class); |
| 676 | packageManagerInternal.setExternalSourcesPolicy( |
| 677 | new PackageManagerInternal.ExternalSourcesPolicy() { |
| 678 | @Override |
| 679 | public int getPackageTrustedToInstallApps(String packageName, int uid) { |
| 680 | int appOpMode = checkOperation(AppOpsManager.OP_REQUEST_INSTALL_PACKAGES, |
| 681 | uid, packageName); |
| 682 | switch (appOpMode) { |
| 683 | case AppOpsManager.MODE_ALLOWED: |
| 684 | return PackageManagerInternal.ExternalSourcesPolicy.USER_TRUSTED; |
| 685 | case AppOpsManager.MODE_ERRORED: |
| 686 | return PackageManagerInternal.ExternalSourcesPolicy.USER_BLOCKED; |
| 687 | default: |
| 688 | return PackageManagerInternal.ExternalSourcesPolicy.USER_DEFAULT; |
| 689 | } |
| 690 | } |
| 691 | }); |
| 692 | |
Sudheer Shanka | 98cb3f0 | 2018-08-17 16:10:29 -0700 | [diff] [blame] | 693 | if (!SystemProperties.getBoolean(StorageManager.PROP_ISOLATED_STORAGE, false)) { |
| 694 | StorageManagerInternal storageManagerInternal = LocalServices.getService( |
| 695 | StorageManagerInternal.class); |
| 696 | storageManagerInternal.addExternalStoragePolicy( |
| 697 | new StorageManagerInternal.ExternalStorageMountPolicy() { |
| 698 | @Override |
| 699 | public int getMountMode(int uid, String packageName) { |
| 700 | if (Process.isIsolated(uid)) { |
| 701 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 702 | } |
| 703 | if (noteOperation(AppOpsManager.OP_READ_EXTERNAL_STORAGE, uid, |
| 704 | packageName) != AppOpsManager.MODE_ALLOWED) { |
| 705 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 706 | } |
| 707 | if (noteOperation(AppOpsManager.OP_WRITE_EXTERNAL_STORAGE, uid, |
| 708 | packageName) != AppOpsManager.MODE_ALLOWED) { |
| 709 | return Zygote.MOUNT_EXTERNAL_READ; |
| 710 | } |
| 711 | return Zygote.MOUNT_EXTERNAL_WRITE; |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 712 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 713 | |
Sudheer Shanka | 98cb3f0 | 2018-08-17 16:10:29 -0700 | [diff] [blame] | 714 | @Override |
| 715 | public boolean hasExternalStorage(int uid, String packageName) { |
| 716 | final int mountMode = getMountMode(uid, packageName); |
| 717 | return mountMode == Zygote.MOUNT_EXTERNAL_READ |
| 718 | || mountMode == Zygote.MOUNT_EXTERNAL_WRITE; |
| 719 | } |
| 720 | }); |
| 721 | } |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | public void packageRemoved(int uid, String packageName) { |
| 725 | synchronized (this) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 726 | UidState uidState = mUidStates.get(uid); |
| 727 | if (uidState == null) { |
| 728 | return; |
| 729 | } |
| 730 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 731 | Ops ops = null; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 732 | |
| 733 | // Remove any package state if such. |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 734 | if (uidState.pkgOps != null) { |
| 735 | ops = uidState.pkgOps.remove(packageName); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | // 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] | 739 | if (ops != null && uidState.pkgOps.isEmpty() |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 740 | && getPackagesForUid(uid).length <= 0) { |
| 741 | mUidStates.remove(uid); |
| 742 | } |
| 743 | |
Svet Ganov | a7a0db6 | 2018-02-27 20:08:01 -0800 | [diff] [blame] | 744 | // Finish ops other packages started on behalf of the package. |
| 745 | final int clientCount = mClients.size(); |
| 746 | for (int i = 0; i < clientCount; i++) { |
| 747 | final ClientState client = mClients.valueAt(i); |
| 748 | if (client.mStartedOps == null) { |
| 749 | continue; |
| 750 | } |
| 751 | final int opCount = client.mStartedOps.size(); |
| 752 | for (int j = opCount - 1; j >= 0; j--) { |
| 753 | final Op op = client.mStartedOps.get(j); |
| 754 | if (uid == op.uid && packageName.equals(op.packageName)) { |
| 755 | finishOperationLocked(op, /*finishNested*/ true); |
| 756 | client.mStartedOps.remove(j); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 757 | if (op.startNesting <= 0) { |
Svet Ganov | a7a0db6 | 2018-02-27 20:08:01 -0800 | [diff] [blame] | 758 | scheduleOpActiveChangedIfNeededLocked(op.op, |
| 759 | uid, packageName, false); |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 765 | if (ops != null) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 766 | scheduleFastWriteLocked(); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 767 | |
| 768 | final int opCount = ops.size(); |
| 769 | for (int i = 0; i < opCount; i++) { |
| 770 | final Op op = ops.valueAt(i); |
| 771 | if (op.duration == -1) { |
| 772 | scheduleOpActiveChangedIfNeededLocked( |
| 773 | op.op, op.uid, op.packageName, false); |
| 774 | } |
| 775 | } |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | public void uidRemoved(int uid) { |
| 781 | synchronized (this) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 782 | if (mUidStates.indexOfKey(uid) >= 0) { |
| 783 | mUidStates.remove(uid); |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 784 | scheduleFastWriteLocked(); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | } |
| 788 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 789 | public void updateUidProcState(int uid, int procState) { |
| 790 | synchronized (this) { |
| 791 | final UidState uidState = getUidStateLocked(uid, true); |
| 792 | final int newState = PROCESS_STATE_TO_UID_STATE[procState]; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 793 | if (uidState != null && uidState.pendingState != newState) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 794 | final int oldPendingState = uidState.pendingState; |
| 795 | uidState.pendingState = newState; |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 796 | if (newState < uidState.state || newState <= UID_STATE_LAST_NON_RESTRICTED) { |
| 797 | // We are moving to a more important state, or the new state is in the |
| 798 | // foreground, then always do it immediately. |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 799 | commitUidPendingStateLocked(uidState); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 800 | } else if (uidState.pendingStateCommitTime == 0) { |
| 801 | // We are moving to a less important state for the first time, |
| 802 | // delay the application for a bit. |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 803 | final long settleTime; |
| 804 | if (uidState.state <= UID_STATE_TOP) { |
| 805 | settleTime = mConstants.TOP_STATE_SETTLE_TIME; |
| 806 | } else if (uidState.state <= UID_STATE_FOREGROUND_SERVICE) { |
| 807 | settleTime = mConstants.FG_SERVICE_STATE_SETTLE_TIME; |
| 808 | } else { |
| 809 | settleTime = mConstants.BG_STATE_SETTLE_TIME; |
| 810 | } |
Dianne Hackborn | 9fb9350 | 2018-06-18 12:29:44 -0700 | [diff] [blame] | 811 | uidState.pendingStateCommitTime = SystemClock.elapsedRealtime() + settleTime; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 812 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 813 | if (uidState.startNesting != 0) { |
| 814 | // There is some actively running operation... need to find it |
| 815 | // and appropriately update its state. |
| 816 | final long now = System.currentTimeMillis(); |
| 817 | for (int i = uidState.pkgOps.size() - 1; i >= 0; i--) { |
| 818 | final Ops ops = uidState.pkgOps.valueAt(i); |
| 819 | for (int j = ops.size() - 1; j >= 0; j--) { |
| 820 | final Op op = ops.valueAt(j); |
| 821 | if (op.startNesting > 0) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 822 | op.time[oldPendingState] = now; |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 823 | op.time[newState] = now; |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | } |
| 831 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 832 | public void shutdown() { |
| 833 | Slog.w(TAG, "Writing app ops before shutdown..."); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 834 | boolean doWrite = false; |
| 835 | synchronized (this) { |
| 836 | if (mWriteScheduled) { |
| 837 | mWriteScheduled = false; |
| 838 | doWrite = true; |
| 839 | } |
| 840 | } |
| 841 | if (doWrite) { |
| 842 | writeState(); |
| 843 | } |
| 844 | } |
| 845 | |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 846 | private ArrayList<AppOpsManager.OpEntry> collectOps(Ops pkgOps, int[] ops) { |
| 847 | ArrayList<AppOpsManager.OpEntry> resOps = null; |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 848 | final long elapsedNow = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 849 | if (ops == null) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 850 | resOps = new ArrayList<>(); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 851 | for (int j=0; j<pkgOps.size(); j++) { |
| 852 | Op curOp = pkgOps.valueAt(j); |
Amith Yamasani | a1ce963 | 2018-05-28 20:50:48 -0700 | [diff] [blame] | 853 | final boolean running = curOp.duration == -1; |
| 854 | long duration = running |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 855 | ? (elapsedNow - curOp.startRealtime) |
| 856 | : curOp.duration; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 857 | resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time, |
Amith Yamasani | a1ce963 | 2018-05-28 20:50:48 -0700 | [diff] [blame] | 858 | curOp.rejectTime, (int) duration, running, curOp.proxyUid, |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 859 | curOp.proxyPackageName)); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 860 | } |
| 861 | } else { |
| 862 | for (int j=0; j<ops.length; j++) { |
| 863 | Op curOp = pkgOps.get(ops[j]); |
| 864 | if (curOp != null) { |
| 865 | if (resOps == null) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 866 | resOps = new ArrayList<>(); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 867 | } |
Amith Yamasani | a1ce963 | 2018-05-28 20:50:48 -0700 | [diff] [blame] | 868 | final boolean running = curOp.duration == -1; |
| 869 | final long duration = running |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 870 | ? (elapsedNow - curOp.startRealtime) |
| 871 | : curOp.duration; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 872 | resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time, |
Amith Yamasani | a1ce963 | 2018-05-28 20:50:48 -0700 | [diff] [blame] | 873 | curOp.rejectTime, (int) duration, running, curOp.proxyUid, |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 874 | curOp.proxyPackageName)); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | } |
| 878 | return resOps; |
| 879 | } |
| 880 | |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 881 | private ArrayList<AppOpsManager.OpEntry> collectOps(SparseIntArray uidOps, int[] ops) { |
| 882 | ArrayList<AppOpsManager.OpEntry> resOps = null; |
| 883 | if (ops == null) { |
| 884 | resOps = new ArrayList<>(); |
| 885 | for (int j=0; j<uidOps.size(); j++) { |
| 886 | resOps.add(new AppOpsManager.OpEntry(uidOps.keyAt(j), uidOps.valueAt(j), |
| 887 | 0, 0, 0, -1, null)); |
| 888 | } |
| 889 | } else { |
| 890 | for (int j=0; j<ops.length; j++) { |
| 891 | int index = uidOps.indexOfKey(ops[j]); |
| 892 | if (index >= 0) { |
| 893 | if (resOps == null) { |
| 894 | resOps = new ArrayList<>(); |
| 895 | } |
| 896 | resOps.add(new AppOpsManager.OpEntry(uidOps.keyAt(index), uidOps.valueAt(index), |
| 897 | 0, 0, 0, -1, null)); |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | return resOps; |
| 902 | } |
| 903 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 904 | @Override |
| 905 | public List<AppOpsManager.PackageOps> getPackagesForOps(int[] ops) { |
| 906 | mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS, |
| 907 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 908 | ArrayList<AppOpsManager.PackageOps> res = null; |
| 909 | synchronized (this) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 910 | final int uidStateCount = mUidStates.size(); |
| 911 | for (int i = 0; i < uidStateCount; i++) { |
| 912 | UidState uidState = mUidStates.valueAt(i); |
| 913 | if (uidState.pkgOps == null || uidState.pkgOps.isEmpty()) { |
| 914 | continue; |
| 915 | } |
| 916 | ArrayMap<String, Ops> packages = uidState.pkgOps; |
| 917 | final int packageCount = packages.size(); |
| 918 | for (int j = 0; j < packageCount; j++) { |
| 919 | Ops pkgOps = packages.valueAt(j); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 920 | ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 921 | if (resOps != null) { |
| 922 | if (res == null) { |
| 923 | res = new ArrayList<AppOpsManager.PackageOps>(); |
| 924 | } |
| 925 | AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps( |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 926 | pkgOps.packageName, pkgOps.uidState.uid, resOps); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 927 | res.add(resPackage); |
| 928 | } |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | return res; |
| 933 | } |
| 934 | |
| 935 | @Override |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 936 | public List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName, |
| 937 | int[] ops) { |
| 938 | mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS, |
| 939 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 940 | String resolvedPackageName = resolvePackageName(uid, packageName); |
| 941 | if (resolvedPackageName == null) { |
| 942 | return Collections.emptyList(); |
| 943 | } |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 944 | synchronized (this) { |
Yohei Yukawa | a965d65 | 2017-10-12 15:02:26 -0700 | [diff] [blame] | 945 | Ops pkgOps = getOpsRawLocked(uid, resolvedPackageName, false /* edit */, |
| 946 | false /* uidMismatchExpected */); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 947 | if (pkgOps == null) { |
| 948 | return null; |
| 949 | } |
| 950 | ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops); |
| 951 | if (resOps == null) { |
| 952 | return null; |
| 953 | } |
| 954 | ArrayList<AppOpsManager.PackageOps> res = new ArrayList<AppOpsManager.PackageOps>(); |
| 955 | AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps( |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 956 | pkgOps.packageName, pkgOps.uidState.uid, resOps); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 957 | res.add(resPackage); |
| 958 | return res; |
| 959 | } |
| 960 | } |
| 961 | |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 962 | @Override |
Svet Ganov | ad0a49b | 2018-10-29 10:07:08 -0700 | [diff] [blame] | 963 | public @Nullable ParceledListSlice getAllHistoricalPackagesOps(@Nullable String[] opNames, |
| 964 | long beginTimeMillis, long endTimeMillis) { |
| 965 | Preconditions.checkArgument(beginTimeMillis >= 0 && beginTimeMillis < endTimeMillis, |
| 966 | "beginTimeMillis must be non negative and lesser than endTimeMillis"); |
| 967 | |
| 968 | mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS, |
| 969 | Binder.getCallingPid(), Binder.getCallingUid(), "getAllHistoricalPackagesOps"); |
| 970 | |
| 971 | ArrayList<HistoricalPackageOps> historicalPackageOpsList = null; |
| 972 | |
| 973 | final int uidStateCount = mUidStates.size(); |
| 974 | for (int i = 0; i < uidStateCount; i++) { |
| 975 | final UidState uidState = mUidStates.valueAt(i); |
| 976 | if (uidState.pkgOps == null || uidState.pkgOps.isEmpty()) { |
| 977 | continue; |
| 978 | } |
| 979 | final ArrayMap<String, Ops> packages = uidState.pkgOps; |
| 980 | final int packageCount = packages.size(); |
| 981 | for (int j = 0; j < packageCount; j++) { |
| 982 | final Ops pkgOps = packages.valueAt(j); |
| 983 | final AppOpsManager.HistoricalPackageOps historicalPackageOps = |
| 984 | createHistoricalPackageOps(uidState.uid, pkgOps, opNames, |
| 985 | beginTimeMillis, endTimeMillis); |
| 986 | if (historicalPackageOps != null) { |
| 987 | if (historicalPackageOpsList == null) { |
| 988 | historicalPackageOpsList = new ArrayList<>(); |
| 989 | } |
| 990 | historicalPackageOpsList.add(historicalPackageOps); |
| 991 | } |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | if (historicalPackageOpsList == null) { |
| 996 | return null; |
| 997 | } |
| 998 | |
| 999 | return new ParceledListSlice<>(historicalPackageOpsList); |
| 1000 | } |
| 1001 | |
| 1002 | private static @Nullable HistoricalPackageOps createHistoricalPackageOps(int uid, |
| 1003 | @Nullable Ops pkgOps, @Nullable String[] opNames, long beginTimeMillis, |
| 1004 | long endTimeMillis) { |
| 1005 | // TODO: Implement historical data collection |
| 1006 | if (pkgOps == null) { |
| 1007 | return null; |
| 1008 | } |
| 1009 | |
| 1010 | final HistoricalPackageOps historicalPackageOps = new HistoricalPackageOps(uid, |
| 1011 | pkgOps.packageName); |
| 1012 | |
| 1013 | if (opNames == null) { |
| 1014 | opNames = AppOpsManager.getOpStrs(); |
| 1015 | } |
| 1016 | for (String opName : opNames) { |
| 1017 | addHistoricOpEntry(AppOpsManager.strOpToOp(opName), pkgOps, historicalPackageOps); |
| 1018 | } |
| 1019 | |
| 1020 | return historicalPackageOps; |
| 1021 | } |
| 1022 | |
| 1023 | @Override |
| 1024 | public @Nullable HistoricalPackageOps getHistoricalPackagesOps(int uid, |
| 1025 | @NonNull String packageName, @Nullable String[] opNames, |
| 1026 | long beginTimeMillis, long endTimeMillis) { |
| 1027 | Preconditions.checkNotNull(packageName, |
| 1028 | "packageName cannot be null"); |
| 1029 | Preconditions.checkArgument(beginTimeMillis >= 0 && beginTimeMillis < endTimeMillis, |
| 1030 | "beginTimeMillis must be non negative and lesser than endTimeMillis"); |
| 1031 | |
| 1032 | mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS, |
| 1033 | Binder.getCallingPid(), Binder.getCallingUid(), "getHistoricalPackagesOps"); |
| 1034 | |
| 1035 | final String resolvedPackageName = resolvePackageName(uid, packageName); |
| 1036 | if (resolvedPackageName == null) { |
| 1037 | return null; |
| 1038 | } |
| 1039 | |
| 1040 | // TODO: Implement historical data collection |
| 1041 | final Ops pkgOps = getOpsRawLocked(uid, resolvedPackageName, false /* edit */, |
| 1042 | false /* uidMismatchExpected */); |
| 1043 | return createHistoricalPackageOps(uid, pkgOps, opNames, beginTimeMillis, endTimeMillis); |
| 1044 | } |
| 1045 | |
| 1046 | private static void addHistoricOpEntry(int opCode, @NonNull Ops ops, |
| 1047 | @NonNull HistoricalPackageOps outHistoricalPackageOps) { |
| 1048 | final Op op = ops.get(opCode); |
| 1049 | if (op == null) { |
| 1050 | return; |
| 1051 | } |
| 1052 | |
| 1053 | final HistoricalOpEntry historicalOpEntry = new HistoricalOpEntry(opCode); |
| 1054 | |
| 1055 | // TODO: Keep per UID state duration |
| 1056 | for (int uidState = 0; uidState < AppOpsManager._NUM_UID_STATE; uidState++) { |
| 1057 | final int acceptCount; |
| 1058 | final int rejectCount; |
| 1059 | if (op.rejectTime[uidState] == 0) { |
| 1060 | acceptCount = 1; |
| 1061 | rejectCount = 0; |
| 1062 | } else { |
| 1063 | acceptCount = 0; |
| 1064 | rejectCount = 1; |
| 1065 | } |
| 1066 | historicalOpEntry.addEntry(uidState, acceptCount, rejectCount, 0); |
| 1067 | } |
| 1068 | |
| 1069 | outHistoricalPackageOps.addEntry(historicalOpEntry); |
| 1070 | } |
| 1071 | |
| 1072 | @Override |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 1073 | public List<AppOpsManager.PackageOps> getUidOps(int uid, int[] ops) { |
| 1074 | mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS, |
| 1075 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 1076 | synchronized (this) { |
| 1077 | UidState uidState = getUidStateLocked(uid, false); |
| 1078 | if (uidState == null) { |
| 1079 | return null; |
| 1080 | } |
| 1081 | ArrayList<AppOpsManager.OpEntry> resOps = collectOps(uidState.opModes, ops); |
| 1082 | if (resOps == null) { |
| 1083 | return null; |
| 1084 | } |
| 1085 | ArrayList<AppOpsManager.PackageOps> res = new ArrayList<AppOpsManager.PackageOps>(); |
| 1086 | AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps( |
| 1087 | null, uidState.uid, resOps); |
| 1088 | res.add(resPackage); |
| 1089 | return res; |
| 1090 | } |
| 1091 | } |
| 1092 | |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1093 | private void pruneOp(Op op, int uid, String packageName) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1094 | if (!op.hasAnyTime()) { |
Yohei Yukawa | a965d65 | 2017-10-12 15:02:26 -0700 | [diff] [blame] | 1095 | Ops ops = getOpsRawLocked(uid, packageName, false /* edit */, |
| 1096 | false /* uidMismatchExpected */); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1097 | if (ops != null) { |
| 1098 | ops.remove(op.op); |
| 1099 | if (ops.size() <= 0) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1100 | UidState uidState = ops.uidState; |
| 1101 | ArrayMap<String, Ops> pkgOps = uidState.pkgOps; |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1102 | if (pkgOps != null) { |
| 1103 | pkgOps.remove(ops.packageName); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1104 | if (pkgOps.isEmpty()) { |
| 1105 | uidState.pkgOps = null; |
| 1106 | } |
| 1107 | if (uidState.isDefault()) { |
| 1108 | mUidStates.remove(uid); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 1116 | void enforceManageAppOpsModes(int callingPid, int callingUid, int targetUid) { |
| 1117 | if (callingPid == Process.myPid()) { |
| 1118 | return; |
| 1119 | } |
| 1120 | final int callingUser = UserHandle.getUserId(callingUid); |
| 1121 | synchronized (this) { |
| 1122 | if (mProfileOwners != null && mProfileOwners.get(callingUser, -1) == callingUid) { |
| 1123 | if (targetUid >= 0 && callingUser == UserHandle.getUserId(targetUid)) { |
| 1124 | // Profile owners are allowed to change modes but only for apps |
| 1125 | // within their user. |
| 1126 | return; |
| 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | mContext.enforcePermission(android.Manifest.permission.MANAGE_APP_OPS_MODES, |
| 1131 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 1132 | } |
| 1133 | |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 1134 | @Override |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1135 | public void setUidMode(int code, int uid, int mode) { |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 1136 | enforceManageAppOpsModes(Binder.getCallingPid(), Binder.getCallingUid(), uid); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1137 | verifyIncomingOp(code); |
| 1138 | code = AppOpsManager.opToSwitch(code); |
| 1139 | |
| 1140 | synchronized (this) { |
| 1141 | final int defaultMode = AppOpsManager.opToDefaultMode(code); |
| 1142 | |
| 1143 | UidState uidState = getUidStateLocked(uid, false); |
| 1144 | if (uidState == null) { |
| 1145 | if (mode == defaultMode) { |
| 1146 | return; |
| 1147 | } |
| 1148 | uidState = new UidState(uid); |
| 1149 | uidState.opModes = new SparseIntArray(); |
| 1150 | uidState.opModes.put(code, mode); |
| 1151 | mUidStates.put(uid, uidState); |
| 1152 | scheduleWriteLocked(); |
| 1153 | } else if (uidState.opModes == null) { |
| 1154 | if (mode != defaultMode) { |
| 1155 | uidState.opModes = new SparseIntArray(); |
| 1156 | uidState.opModes.put(code, mode); |
| 1157 | scheduleWriteLocked(); |
| 1158 | } |
| 1159 | } else { |
Hai Zhang | 2b98fb3 | 2018-09-21 15:18:46 -0700 | [diff] [blame] | 1160 | if (uidState.opModes.indexOfKey(code) >= 0 && uidState.opModes.get(code) == mode) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1161 | return; |
| 1162 | } |
| 1163 | if (mode == defaultMode) { |
| 1164 | uidState.opModes.delete(code); |
| 1165 | if (uidState.opModes.size() <= 0) { |
| 1166 | uidState.opModes = null; |
| 1167 | } |
| 1168 | } else { |
| 1169 | uidState.opModes.put(code, mode); |
| 1170 | } |
| 1171 | scheduleWriteLocked(); |
| 1172 | } |
| 1173 | } |
| 1174 | |
Svetoslav | 215b44a | 2015-08-04 19:03:40 -0700 | [diff] [blame] | 1175 | String[] uidPackageNames = getPackagesForUid(uid); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1176 | ArrayMap<ModeCallback, ArraySet<String>> callbackSpecs = null; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1177 | |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 1178 | synchronized (this) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1179 | ArraySet<ModeCallback> callbacks = mOpModeWatchers.get(code); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1180 | if (callbacks != null) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1181 | final int callbackCount = callbacks.size(); |
| 1182 | for (int i = 0; i < callbackCount; i++) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1183 | ModeCallback callback = callbacks.valueAt(i); |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 1184 | ArraySet<String> changedPackages = new ArraySet<>(); |
| 1185 | Collections.addAll(changedPackages, uidPackageNames); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 1186 | if (callbackSpecs == null) { |
| 1187 | callbackSpecs = new ArrayMap<>(); |
| 1188 | } |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 1189 | callbackSpecs.put(callback, changedPackages); |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | for (String uidPackageName : uidPackageNames) { |
| 1194 | callbacks = mPackageModeWatchers.get(uidPackageName); |
| 1195 | if (callbacks != null) { |
| 1196 | if (callbackSpecs == null) { |
| 1197 | callbackSpecs = new ArrayMap<>(); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1198 | } |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 1199 | final int callbackCount = callbacks.size(); |
| 1200 | for (int i = 0; i < callbackCount; i++) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1201 | ModeCallback callback = callbacks.valueAt(i); |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 1202 | ArraySet<String> changedPackages = callbackSpecs.get(callback); |
| 1203 | if (changedPackages == null) { |
| 1204 | changedPackages = new ArraySet<>(); |
| 1205 | callbackSpecs.put(callback, changedPackages); |
| 1206 | } |
| 1207 | changedPackages.add(uidPackageName); |
| 1208 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | if (callbackSpecs == null) { |
| 1214 | return; |
| 1215 | } |
| 1216 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1217 | for (int i = 0; i < callbackSpecs.size(); i++) { |
| 1218 | final ModeCallback callback = callbackSpecs.keyAt(i); |
| 1219 | final ArraySet<String> reportedPackageNames = callbackSpecs.valueAt(i); |
| 1220 | if (reportedPackageNames == null) { |
| 1221 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 1222 | AppOpsService::notifyOpChanged, |
| 1223 | this, callback, code, uid, (String) null)); |
| 1224 | |
| 1225 | } else { |
| 1226 | final int reportedPackageCount = reportedPackageNames.size(); |
| 1227 | for (int j = 0; j < reportedPackageCount; j++) { |
| 1228 | final String reportedPackageName = reportedPackageNames.valueAt(j); |
| 1229 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 1230 | AppOpsService::notifyOpChanged, |
| 1231 | this, callback, code, uid, reportedPackageName)); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1232 | } |
| 1233 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | @Override |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1238 | public void setMode(int code, int uid, String packageName, int mode) { |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 1239 | setMode(code, uid, packageName, mode, true, false); |
| 1240 | } |
| 1241 | |
| 1242 | /** |
| 1243 | * Sets the mode for a certain op and uid. |
| 1244 | * |
| 1245 | * @param code The op code to set |
| 1246 | * @param uid The UID for which to set |
| 1247 | * @param packageName The package for which to set |
| 1248 | * @param mode The new mode to set |
| 1249 | * @param verifyUid Iff {@code true}, check that the package name belongs to the uid |
| 1250 | * @param isPrivileged Whether the package is privileged. (Only used if {@code verifyUid == |
| 1251 | * false}) |
| 1252 | */ |
| 1253 | private void setMode(int code, int uid, @NonNull String packageName, int mode, |
| 1254 | boolean verifyUid, boolean isPrivileged) { |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 1255 | enforceManageAppOpsModes(Binder.getCallingPid(), Binder.getCallingUid(), uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 1256 | verifyIncomingOp(code); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1257 | ArraySet<ModeCallback> repCbs = null; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1258 | code = AppOpsManager.opToSwitch(code); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1259 | synchronized (this) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1260 | UidState uidState = getUidStateLocked(uid, false); |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 1261 | Op op = getOpLocked(code, uid, packageName, true, verifyUid, isPrivileged); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1262 | if (op != null) { |
| 1263 | if (op.mode != mode) { |
| 1264 | op.mode = mode; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 1265 | if (uidState != null) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 1266 | uidState.evalForegroundOps(mOpModeWatchers); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 1267 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1268 | ArraySet<ModeCallback> cbs = mOpModeWatchers.get(code); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1269 | if (cbs != null) { |
| 1270 | if (repCbs == null) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1271 | repCbs = new ArraySet<>(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1272 | } |
| 1273 | repCbs.addAll(cbs); |
| 1274 | } |
| 1275 | cbs = mPackageModeWatchers.get(packageName); |
| 1276 | if (cbs != null) { |
| 1277 | if (repCbs == null) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1278 | repCbs = new ArraySet<>(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1279 | } |
| 1280 | repCbs.addAll(cbs); |
| 1281 | } |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 1282 | if (mode == AppOpsManager.opToDefaultMode(op.op)) { |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1283 | // If going into the default mode, prune this op |
| 1284 | // if there is nothing else interesting in it. |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1285 | pruneOp(op, uid, packageName); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1286 | } |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 1287 | scheduleFastWriteLocked(); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1288 | } |
| 1289 | } |
| 1290 | } |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1291 | if (repCbs != null) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1292 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 1293 | AppOpsService::notifyOpChanged, |
| 1294 | this, repCbs, code, uid, packageName)); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1295 | } |
| 1296 | } |
| 1297 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1298 | private void notifyOpChanged(ArraySet<ModeCallback> callbacks, int code, |
| 1299 | int uid, String packageName) { |
| 1300 | for (int i = 0; i < callbacks.size(); i++) { |
| 1301 | final ModeCallback callback = callbacks.valueAt(i); |
| 1302 | notifyOpChanged(callback, code, uid, packageName); |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | private void notifyOpChanged(ModeCallback callback, int code, |
| 1307 | int uid, String packageName) { |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1308 | if (uid != UID_ANY && callback.mWatchingUid >= 0 && callback.mWatchingUid != uid) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1309 | return; |
| 1310 | } |
| 1311 | // There are components watching for mode changes such as window manager |
| 1312 | // and location manager which are in our process. The callbacks in these |
| 1313 | // components may require permissions our remote caller does not have. |
| 1314 | final long identity = Binder.clearCallingIdentity(); |
| 1315 | try { |
| 1316 | callback.mCallback.opChanged(code, uid, packageName); |
| 1317 | } catch (RemoteException e) { |
| 1318 | /* ignore */ |
| 1319 | } finally { |
| 1320 | Binder.restoreCallingIdentity(identity); |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | private static HashMap<ModeCallback, ArrayList<ChangeRec>> addCallbacks( |
| 1325 | HashMap<ModeCallback, ArrayList<ChangeRec>> callbacks, |
| 1326 | int op, int uid, String packageName, ArraySet<ModeCallback> cbs) { |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1327 | if (cbs == null) { |
| 1328 | return callbacks; |
| 1329 | } |
| 1330 | if (callbacks == null) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1331 | callbacks = new HashMap<>(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1332 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1333 | boolean duplicate = false; |
Dianne Hackborn | 68d7655 | 2017-02-27 15:32:03 -0800 | [diff] [blame] | 1334 | final int N = cbs.size(); |
| 1335 | for (int i=0; i<N; i++) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1336 | ModeCallback cb = cbs.valueAt(i); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1337 | ArrayList<ChangeRec> reports = callbacks.get(cb); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1338 | if (reports == null) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1339 | reports = new ArrayList<>(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1340 | callbacks.put(cb, reports); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1341 | } else { |
| 1342 | final int reportCount = reports.size(); |
| 1343 | for (int j = 0; j < reportCount; j++) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1344 | ChangeRec report = reports.get(j); |
| 1345 | if (report.op == op && report.pkg.equals(packageName)) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1346 | duplicate = true; |
| 1347 | break; |
| 1348 | } |
| 1349 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1350 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1351 | if (!duplicate) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1352 | reports.add(new ChangeRec(op, uid, packageName)); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1353 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1354 | } |
| 1355 | return callbacks; |
| 1356 | } |
| 1357 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1358 | static final class ChangeRec { |
| 1359 | final int op; |
| 1360 | final int uid; |
| 1361 | final String pkg; |
| 1362 | |
| 1363 | ChangeRec(int _op, int _uid, String _pkg) { |
| 1364 | op = _op; |
| 1365 | uid = _uid; |
| 1366 | pkg = _pkg; |
| 1367 | } |
| 1368 | } |
| 1369 | |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1370 | @Override |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 1371 | public void resetAllModes(int reqUserId, String reqPackageName) { |
| 1372 | final int callingPid = Binder.getCallingPid(); |
| 1373 | final int callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 1374 | reqUserId = ActivityManager.handleIncomingUser(callingPid, callingUid, reqUserId, |
| 1375 | true, true, "resetAllModes", null); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1376 | |
| 1377 | int reqUid = -1; |
| 1378 | if (reqPackageName != null) { |
| 1379 | try { |
| 1380 | reqUid = AppGlobals.getPackageManager().getPackageUid( |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 1381 | reqPackageName, PackageManager.MATCH_UNINSTALLED_PACKAGES, reqUserId); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1382 | } catch (RemoteException e) { |
| 1383 | /* ignore - local call */ |
| 1384 | } |
| 1385 | } |
| 1386 | |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 1387 | enforceManageAppOpsModes(callingPid, callingUid, reqUid); |
| 1388 | |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1389 | HashMap<ModeCallback, ArrayList<ChangeRec>> callbacks = null; |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1390 | synchronized (this) { |
| 1391 | boolean changed = false; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1392 | for (int i = mUidStates.size() - 1; i >= 0; i--) { |
| 1393 | UidState uidState = mUidStates.valueAt(i); |
| 1394 | |
| 1395 | SparseIntArray opModes = uidState.opModes; |
| 1396 | if (opModes != null && (uidState.uid == reqUid || reqUid == -1)) { |
| 1397 | final int uidOpCount = opModes.size(); |
| 1398 | for (int j = uidOpCount - 1; j >= 0; j--) { |
| 1399 | final int code = opModes.keyAt(j); |
| 1400 | if (AppOpsManager.opAllowsReset(code)) { |
| 1401 | opModes.removeAt(j); |
| 1402 | if (opModes.size() <= 0) { |
| 1403 | uidState.opModes = null; |
| 1404 | } |
| 1405 | for (String packageName : getPackagesForUid(uidState.uid)) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1406 | callbacks = addCallbacks(callbacks, code, uidState.uid, packageName, |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1407 | mOpModeWatchers.get(code)); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1408 | callbacks = addCallbacks(callbacks, code, uidState.uid, packageName, |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1409 | mPackageModeWatchers.get(packageName)); |
| 1410 | } |
| 1411 | } |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | if (uidState.pkgOps == null) { |
| 1416 | continue; |
| 1417 | } |
| 1418 | |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 1419 | if (reqUserId != UserHandle.USER_ALL |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1420 | && reqUserId != UserHandle.getUserId(uidState.uid)) { |
Alexandra Gherghina | d6a9897 | 2014-08-04 17:05:34 +0100 | [diff] [blame] | 1421 | // Skip any ops for a different user |
| 1422 | continue; |
| 1423 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1424 | |
| 1425 | Map<String, Ops> packages = uidState.pkgOps; |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 1426 | Iterator<Map.Entry<String, Ops>> it = packages.entrySet().iterator(); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 1427 | boolean uidChanged = false; |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 1428 | while (it.hasNext()) { |
| 1429 | Map.Entry<String, Ops> ent = it.next(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1430 | String packageName = ent.getKey(); |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 1431 | if (reqPackageName != null && !reqPackageName.equals(packageName)) { |
| 1432 | // Skip any ops for a different package |
| 1433 | continue; |
| 1434 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1435 | Ops pkgOps = ent.getValue(); |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 1436 | for (int j=pkgOps.size()-1; j>=0; j--) { |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1437 | Op curOp = pkgOps.valueAt(j); |
Dianne Hackborn | 8828d3a | 2013-09-25 16:47:10 -0700 | [diff] [blame] | 1438 | if (AppOpsManager.opAllowsReset(curOp.op) |
| 1439 | && curOp.mode != AppOpsManager.opToDefaultMode(curOp.op)) { |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 1440 | curOp.mode = AppOpsManager.opToDefaultMode(curOp.op); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1441 | changed = true; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 1442 | uidChanged = true; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1443 | callbacks = addCallbacks(callbacks, curOp.op, curOp.uid, packageName, |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1444 | mOpModeWatchers.get(curOp.op)); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1445 | callbacks = addCallbacks(callbacks, curOp.op, curOp.uid, packageName, |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1446 | mPackageModeWatchers.get(packageName)); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1447 | if (!curOp.hasAnyTime()) { |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 1448 | pkgOps.removeAt(j); |
| 1449 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1450 | } |
| 1451 | } |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 1452 | if (pkgOps.size() == 0) { |
| 1453 | it.remove(); |
| 1454 | } |
| 1455 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1456 | if (uidState.isDefault()) { |
| 1457 | mUidStates.remove(uidState.uid); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1458 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 1459 | if (uidChanged) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 1460 | uidState.evalForegroundOps(mOpModeWatchers); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 1461 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1462 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1463 | |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1464 | if (changed) { |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 1465 | scheduleFastWriteLocked(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1466 | } |
| 1467 | } |
| 1468 | if (callbacks != null) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1469 | for (Map.Entry<ModeCallback, ArrayList<ChangeRec>> ent : callbacks.entrySet()) { |
| 1470 | ModeCallback cb = ent.getKey(); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1471 | ArrayList<ChangeRec> reports = ent.getValue(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1472 | for (int i=0; i<reports.size(); i++) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1473 | ChangeRec rep = reports.get(i); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1474 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 1475 | AppOpsService::notifyOpChanged, |
| 1476 | this, cb, rep.op, rep.uid, rep.pkg)); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | } |
| 1481 | |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 1482 | private void evalAllForegroundOpsLocked() { |
| 1483 | for (int uidi = mUidStates.size() - 1; uidi >= 0; uidi--) { |
| 1484 | final UidState uidState = mUidStates.valueAt(uidi); |
| 1485 | if (uidState.foregroundOps != null) { |
| 1486 | uidState.evalForegroundOps(mOpModeWatchers); |
| 1487 | } |
| 1488 | } |
| 1489 | } |
| 1490 | |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1491 | @Override |
| 1492 | public void startWatchingMode(int op, String packageName, IAppOpsCallback callback) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 1493 | startWatchingModeWithFlags(op, packageName, 0, callback); |
| 1494 | } |
| 1495 | |
| 1496 | @Override |
| 1497 | public void startWatchingModeWithFlags(int op, String packageName, int flags, |
| 1498 | IAppOpsCallback callback) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1499 | int watchedUid = -1; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1500 | final int callingUid = Binder.getCallingUid(); |
| 1501 | final int callingPid = Binder.getCallingPid(); |
Dianne Hackborn | 5376edd | 2018-06-05 13:21:16 -0700 | [diff] [blame] | 1502 | // TODO: should have a privileged permission to protect this. |
| 1503 | // Also, if the caller has requested WATCH_FOREGROUND_CHANGES, should we require |
| 1504 | // the USAGE_STATS permission since this can provide information about when an |
| 1505 | // app is in the foreground? |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1506 | Preconditions.checkArgumentInRange(op, AppOpsManager.OP_NONE, |
| 1507 | AppOpsManager._NUM_OP - 1, "Invalid op code: " + op); |
Svetoslav Ganov | 8de5971 | 2015-12-09 18:25:13 -0800 | [diff] [blame] | 1508 | if (callback == null) { |
| 1509 | return; |
| 1510 | } |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1511 | synchronized (this) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1512 | op = (op != AppOpsManager.OP_NONE) ? AppOpsManager.opToSwitch(op) : op; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1513 | ModeCallback cb = mModeWatchers.get(callback.asBinder()); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1514 | if (cb == null) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 1515 | cb = new ModeCallback(callback, watchedUid, flags, callingUid, callingPid); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1516 | mModeWatchers.put(callback.asBinder(), cb); |
| 1517 | } |
| 1518 | if (op != AppOpsManager.OP_NONE) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1519 | ArraySet<ModeCallback> cbs = mOpModeWatchers.get(op); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1520 | if (cbs == null) { |
Dianne Hackborn | 68d7655 | 2017-02-27 15:32:03 -0800 | [diff] [blame] | 1521 | cbs = new ArraySet<>(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1522 | mOpModeWatchers.put(op, cbs); |
| 1523 | } |
| 1524 | cbs.add(cb); |
| 1525 | } |
| 1526 | if (packageName != null) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1527 | ArraySet<ModeCallback> cbs = mPackageModeWatchers.get(packageName); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1528 | if (cbs == null) { |
Dianne Hackborn | 68d7655 | 2017-02-27 15:32:03 -0800 | [diff] [blame] | 1529 | cbs = new ArraySet<>(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1530 | mPackageModeWatchers.put(packageName, cbs); |
| 1531 | } |
| 1532 | cbs.add(cb); |
| 1533 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 1534 | evalAllForegroundOpsLocked(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | @Override |
| 1539 | public void stopWatchingMode(IAppOpsCallback callback) { |
Svetoslav Ganov | 8de5971 | 2015-12-09 18:25:13 -0800 | [diff] [blame] | 1540 | if (callback == null) { |
| 1541 | return; |
| 1542 | } |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1543 | synchronized (this) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1544 | ModeCallback cb = mModeWatchers.remove(callback.asBinder()); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1545 | if (cb != null) { |
| 1546 | cb.unlinkToDeath(); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1547 | for (int i=mOpModeWatchers.size()-1; i>=0; i--) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1548 | ArraySet<ModeCallback> cbs = mOpModeWatchers.valueAt(i); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1549 | cbs.remove(cb); |
| 1550 | if (cbs.size() <= 0) { |
| 1551 | mOpModeWatchers.removeAt(i); |
| 1552 | } |
| 1553 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1554 | for (int i=mPackageModeWatchers.size()-1; i>=0; i--) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1555 | ArraySet<ModeCallback> cbs = mPackageModeWatchers.valueAt(i); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1556 | cbs.remove(cb); |
| 1557 | if (cbs.size() <= 0) { |
| 1558 | mPackageModeWatchers.removeAt(i); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1559 | } |
| 1560 | } |
| 1561 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 1562 | evalAllForegroundOpsLocked(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 1563 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | @Override |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1567 | public IBinder getToken(IBinder clientToken) { |
| 1568 | synchronized (this) { |
| 1569 | ClientState cs = mClients.get(clientToken); |
| 1570 | if (cs == null) { |
| 1571 | cs = new ClientState(clientToken); |
| 1572 | mClients.put(clientToken, cs); |
| 1573 | } |
| 1574 | return cs; |
| 1575 | } |
| 1576 | } |
| 1577 | |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 1578 | public CheckOpsDelegate getAppOpsServiceDelegate() { |
| 1579 | synchronized (this) { |
| 1580 | return mCheckOpsDelegate; |
| 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | public void setAppOpsServiceDelegate(CheckOpsDelegate delegate) { |
| 1585 | synchronized (this) { |
| 1586 | mCheckOpsDelegate = delegate; |
| 1587 | } |
| 1588 | } |
| 1589 | |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1590 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1591 | public int checkOperation(int code, int uid, String packageName) { |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 1592 | final CheckOpsDelegate delegate; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1593 | synchronized (this) { |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 1594 | delegate = mCheckOpsDelegate; |
| 1595 | } |
Todd Kennedy | 556efba | 2018-11-15 07:43:55 -0800 | [diff] [blame^] | 1596 | if (delegate == null) { |
| 1597 | return checkOperationImpl(code, uid, packageName); |
| 1598 | } |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 1599 | return delegate.checkOperation(code, uid, packageName, |
| 1600 | AppOpsService.this::checkOperationImpl); |
| 1601 | } |
| 1602 | |
| 1603 | private int checkOperationImpl(int code, int uid, String packageName) { |
Todd Kennedy | 556efba | 2018-11-15 07:43:55 -0800 | [diff] [blame^] | 1604 | verifyIncomingUid(uid); |
| 1605 | verifyIncomingOp(code); |
| 1606 | String resolvedPackageName = resolvePackageName(uid, packageName); |
| 1607 | if (resolvedPackageName == null) { |
| 1608 | return AppOpsManager.MODE_IGNORED; |
| 1609 | } |
| 1610 | return checkOperationUnchecked(code, uid, resolvedPackageName); |
| 1611 | } |
| 1612 | |
| 1613 | private int checkOperationUnchecked(int code, int uid, String packageName) { |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 1614 | synchronized (this) { |
Todd Kennedy | 556efba | 2018-11-15 07:43:55 -0800 | [diff] [blame^] | 1615 | if (isOpRestrictedLocked(uid, code, packageName)) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 1616 | return AppOpsManager.MODE_IGNORED; |
| 1617 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1618 | code = AppOpsManager.opToSwitch(code); |
| 1619 | UidState uidState = getUidStateLocked(uid, false); |
Svet Ganov | ee438d4 | 2017-01-19 18:04:38 -0800 | [diff] [blame] | 1620 | if (uidState != null && uidState.opModes != null |
| 1621 | && uidState.opModes.indexOfKey(code) >= 0) { |
| 1622 | return uidState.opModes.get(code); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1623 | } |
Todd Kennedy | 556efba | 2018-11-15 07:43:55 -0800 | [diff] [blame^] | 1624 | Op op = getOpLocked(code, uid, packageName, false, true, false); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1625 | if (op == null) { |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 1626 | return AppOpsManager.opToDefaultMode(code); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1627 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 1628 | return op.mode; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1629 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | @Override |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 1633 | public int checkAudioOperation(int code, int usage, int uid, String packageName) { |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 1634 | final CheckOpsDelegate delegate; |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1635 | synchronized (this) { |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 1636 | delegate = mCheckOpsDelegate; |
| 1637 | } |
Todd Kennedy | 556efba | 2018-11-15 07:43:55 -0800 | [diff] [blame^] | 1638 | if (delegate == null) { |
| 1639 | return checkAudioOperationImpl(code, usage, uid, packageName); |
| 1640 | } |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 1641 | return delegate.checkAudioOperation(code, usage, uid, packageName, |
| 1642 | AppOpsService.this::checkAudioOperationImpl); |
| 1643 | } |
| 1644 | |
| 1645 | private int checkAudioOperationImpl(int code, int usage, int uid, String packageName) { |
Todd Kennedy | 556efba | 2018-11-15 07:43:55 -0800 | [diff] [blame^] | 1646 | boolean suspended; |
| 1647 | try { |
| 1648 | suspended = isPackageSuspendedForUser(packageName, uid); |
| 1649 | } catch (IllegalArgumentException ex) { |
| 1650 | // Package not found. |
| 1651 | suspended = false; |
| 1652 | } |
| 1653 | |
| 1654 | if (suspended) { |
| 1655 | Slog.i(TAG, "Audio disabled for suspended package=" + packageName |
| 1656 | + " for uid=" + uid); |
| 1657 | return AppOpsManager.MODE_IGNORED; |
| 1658 | } |
| 1659 | |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 1660 | synchronized (this) { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 1661 | final int mode = checkRestrictionLocked(code, usage, uid, packageName); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1662 | if (mode != AppOpsManager.MODE_ALLOWED) { |
| 1663 | return mode; |
| 1664 | } |
| 1665 | } |
| 1666 | return checkOperation(code, uid, packageName); |
| 1667 | } |
| 1668 | |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 1669 | private boolean isPackageSuspendedForUser(String pkg, int uid) { |
Andrei Stingaceanu | 2bc2feb | 2016-02-11 16:23:49 +0000 | [diff] [blame] | 1670 | try { |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 1671 | return AppGlobals.getPackageManager().isPackageSuspendedForUser( |
| 1672 | pkg, UserHandle.getUserId(uid)); |
Andrei Stingaceanu | 2bc2feb | 2016-02-11 16:23:49 +0000 | [diff] [blame] | 1673 | } catch (RemoteException re) { |
| 1674 | throw new SecurityException("Could not talk to package manager service"); |
| 1675 | } |
Andrei Stingaceanu | 2bc2feb | 2016-02-11 16:23:49 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 1678 | private int checkRestrictionLocked(int code, int usage, int uid, String packageName) { |
| 1679 | final SparseArray<Restriction> usageRestrictions = mAudioRestrictions.get(code); |
| 1680 | if (usageRestrictions != null) { |
| 1681 | final Restriction r = usageRestrictions.get(usage); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1682 | if (r != null && !r.exceptionPackages.contains(packageName)) { |
| 1683 | return r.mode; |
| 1684 | } |
| 1685 | } |
| 1686 | return AppOpsManager.MODE_ALLOWED; |
| 1687 | } |
| 1688 | |
| 1689 | @Override |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 1690 | public void setAudioRestriction(int code, int usage, int uid, int mode, |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1691 | String[] exceptionPackages) { |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 1692 | enforceManageAppOpsModes(Binder.getCallingPid(), Binder.getCallingUid(), uid); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1693 | verifyIncomingUid(uid); |
| 1694 | verifyIncomingOp(code); |
| 1695 | synchronized (this) { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 1696 | SparseArray<Restriction> usageRestrictions = mAudioRestrictions.get(code); |
| 1697 | if (usageRestrictions == null) { |
| 1698 | usageRestrictions = new SparseArray<Restriction>(); |
| 1699 | mAudioRestrictions.put(code, usageRestrictions); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1700 | } |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 1701 | usageRestrictions.remove(usage); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1702 | if (mode != AppOpsManager.MODE_ALLOWED) { |
| 1703 | final Restriction r = new Restriction(); |
| 1704 | r.mode = mode; |
| 1705 | if (exceptionPackages != null) { |
| 1706 | final int N = exceptionPackages.length; |
| 1707 | r.exceptionPackages = new ArraySet<String>(N); |
| 1708 | for (int i = 0; i < N; i++) { |
| 1709 | final String pkg = exceptionPackages[i]; |
| 1710 | if (pkg != null) { |
| 1711 | r.exceptionPackages.add(pkg.trim()); |
| 1712 | } |
| 1713 | } |
| 1714 | } |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 1715 | usageRestrictions.put(usage, r); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1716 | } |
| 1717 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1718 | |
| 1719 | mHandler.sendMessage(PooledLambda.obtainMessage( |
Svet Ganov | 3a95f83 | 2018-03-23 17:44:30 -0700 | [diff] [blame] | 1720 | AppOpsService::notifyWatchersOfChange, this, code, UID_ANY)); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | @Override |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 1724 | public int checkPackage(int uid, String packageName) { |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1725 | Preconditions.checkNotNull(packageName); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 1726 | synchronized (this) { |
Yohei Yukawa | a965d65 | 2017-10-12 15:02:26 -0700 | [diff] [blame] | 1727 | Ops ops = getOpsRawLocked(uid, packageName, true /* edit */, |
| 1728 | true /* uidMismatchExpected */); |
| 1729 | if (ops != null) { |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 1730 | return AppOpsManager.MODE_ALLOWED; |
| 1731 | } else { |
| 1732 | return AppOpsManager.MODE_ERRORED; |
| 1733 | } |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | @Override |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 1738 | public int noteProxyOperation(int code, int proxyUid, |
| 1739 | String proxyPackageName, int proxiedUid, String proxiedPackageName) { |
| 1740 | verifyIncomingUid(proxyUid); |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1741 | verifyIncomingOp(code); |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1742 | String resolveProxyPackageName = resolvePackageName(proxyUid, proxyPackageName); |
| 1743 | if (resolveProxyPackageName == null) { |
| 1744 | return AppOpsManager.MODE_IGNORED; |
| 1745 | } |
| 1746 | final int proxyMode = noteOperationUnchecked(code, proxyUid, |
| 1747 | resolveProxyPackageName, -1, null); |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1748 | if (proxyMode != AppOpsManager.MODE_ALLOWED || Binder.getCallingUid() == proxiedUid) { |
| 1749 | return proxyMode; |
| 1750 | } |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1751 | String resolveProxiedPackageName = resolvePackageName(proxiedUid, proxiedPackageName); |
| 1752 | if (resolveProxiedPackageName == null) { |
| 1753 | return AppOpsManager.MODE_IGNORED; |
| 1754 | } |
| 1755 | return noteOperationUnchecked(code, proxiedUid, resolveProxiedPackageName, |
| 1756 | proxyMode, resolveProxyPackageName); |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | @Override |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1760 | public int noteOperation(int code, int uid, String packageName) { |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 1761 | final CheckOpsDelegate delegate; |
| 1762 | synchronized (this) { |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 1763 | delegate = mCheckOpsDelegate; |
| 1764 | } |
Todd Kennedy | 556efba | 2018-11-15 07:43:55 -0800 | [diff] [blame^] | 1765 | if (delegate == null) { |
| 1766 | return noteOperationImpl(code, uid, packageName); |
| 1767 | } |
Svet Ganov | d873ae6 | 2018-06-25 16:39:23 -0700 | [diff] [blame] | 1768 | return delegate.noteOperation(code, uid, packageName, |
| 1769 | AppOpsService.this::noteOperationImpl); |
| 1770 | } |
| 1771 | |
| 1772 | private int noteOperationImpl(int code, int uid, String packageName) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1773 | verifyIncomingUid(uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 1774 | verifyIncomingOp(code); |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1775 | String resolvedPackageName = resolvePackageName(uid, packageName); |
| 1776 | if (resolvedPackageName == null) { |
| 1777 | return AppOpsManager.MODE_IGNORED; |
| 1778 | } |
| 1779 | return noteOperationUnchecked(code, uid, resolvedPackageName, 0, null); |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | private int noteOperationUnchecked(int code, int uid, String packageName, |
| 1783 | int proxyUid, String proxyPackageName) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1784 | synchronized (this) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1785 | final Ops ops = getOpsRawLocked(uid, packageName, true /* edit */, |
Yohei Yukawa | a965d65 | 2017-10-12 15:02:26 -0700 | [diff] [blame] | 1786 | false /* uidMismatchExpected */); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1787 | if (ops == null) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1788 | 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] | 1789 | + " package " + packageName); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 1790 | return AppOpsManager.MODE_ERRORED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1791 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1792 | final Op op = getOpLocked(ops, code, true); |
Svet Ganov | 442ed57 | 2016-08-17 17:29:43 -0700 | [diff] [blame] | 1793 | if (isOpRestrictedLocked(uid, code, packageName)) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 1794 | return AppOpsManager.MODE_IGNORED; |
| 1795 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1796 | final UidState uidState = ops.uidState; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1797 | if (op.duration == -1) { |
| 1798 | Slog.w(TAG, "Noting op not finished: uid " + uid + " pkg " + packageName |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1799 | + " code " + code + " time=" + op.time[uidState.state] |
| 1800 | + " duration=" + op.duration); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1801 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1802 | op.duration = 0; |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1803 | final int switchCode = AppOpsManager.opToSwitch(code); |
Svetoslav Ganov | 1984bba | 2016-04-05 13:39:25 -0700 | [diff] [blame] | 1804 | // If there is a non-default per UID policy (we set UID op mode only if |
| 1805 | // non-default) it takes over, otherwise use the per package policy. |
| 1806 | if (uidState.opModes != null && uidState.opModes.indexOfKey(switchCode) >= 0) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 1807 | final int uidMode = uidState.evalMode(uidState.opModes.get(switchCode)); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1808 | if (uidMode != AppOpsManager.MODE_ALLOWED) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 1809 | if (DEBUG) Slog.d(TAG, "noteOperation: uid reject #" + uidMode + " for code " |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1810 | + switchCode + " (" + code + ") uid " + uid + " package " |
| 1811 | + packageName); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1812 | op.rejectTime[uidState.state] = System.currentTimeMillis(); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1813 | return uidMode; |
| 1814 | } |
Svetoslav Ganov | 1984bba | 2016-04-05 13:39:25 -0700 | [diff] [blame] | 1815 | } else { |
| 1816 | final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 1817 | final int mode = switchOp.getMode(); |
| 1818 | if (mode != AppOpsManager.MODE_ALLOWED) { |
| 1819 | if (DEBUG) Slog.d(TAG, "noteOperation: reject #" + mode + " for code " |
Svetoslav Ganov | 1984bba | 2016-04-05 13:39:25 -0700 | [diff] [blame] | 1820 | + switchCode + " (" + code + ") uid " + uid + " package " |
| 1821 | + packageName); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1822 | op.rejectTime[uidState.state] = System.currentTimeMillis(); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 1823 | return mode; |
Svetoslav Ganov | 1984bba | 2016-04-05 13:39:25 -0700 | [diff] [blame] | 1824 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1825 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1826 | if (DEBUG) Slog.d(TAG, "noteOperation: allowing code " + code + " uid " + uid |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1827 | + " package " + packageName); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1828 | op.time[uidState.state] = System.currentTimeMillis(); |
| 1829 | op.rejectTime[uidState.state] = 0; |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1830 | op.proxyUid = proxyUid; |
| 1831 | op.proxyPackageName = proxyPackageName; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1832 | return AppOpsManager.MODE_ALLOWED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1833 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | @Override |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1837 | public void startWatchingActive(int[] ops, IAppOpsActiveCallback callback) { |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 1838 | int watchedUid = -1; |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1839 | final int callingUid = Binder.getCallingUid(); |
| 1840 | final int callingPid = Binder.getCallingPid(); |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 1841 | if (mContext.checkCallingOrSelfPermission(Manifest.permission.WATCH_APPOPS) |
| 1842 | != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1843 | watchedUid = callingUid; |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 1844 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1845 | if (ops != null) { |
| 1846 | Preconditions.checkArrayElementsInRange(ops, 0, |
| 1847 | AppOpsManager._NUM_OP - 1, "Invalid op code in: " + Arrays.toString(ops)); |
| 1848 | } |
| 1849 | if (callback == null) { |
| 1850 | return; |
| 1851 | } |
| 1852 | synchronized (this) { |
| 1853 | SparseArray<ActiveCallback> callbacks = mActiveWatchers.get(callback.asBinder()); |
| 1854 | if (callbacks == null) { |
| 1855 | callbacks = new SparseArray<>(); |
| 1856 | mActiveWatchers.put(callback.asBinder(), callbacks); |
| 1857 | } |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 1858 | final ActiveCallback activeCallback = new ActiveCallback(callback, watchedUid, |
| 1859 | callingUid, callingPid); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1860 | for (int op : ops) { |
| 1861 | callbacks.put(op, activeCallback); |
| 1862 | } |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | @Override |
| 1867 | public void stopWatchingActive(IAppOpsActiveCallback callback) { |
| 1868 | if (callback == null) { |
| 1869 | return; |
| 1870 | } |
| 1871 | synchronized (this) { |
| 1872 | final SparseArray<ActiveCallback> activeCallbacks = |
| 1873 | mActiveWatchers.remove(callback.asBinder()); |
| 1874 | if (activeCallbacks == null) { |
| 1875 | return; |
| 1876 | } |
| 1877 | final int callbackCount = activeCallbacks.size(); |
| 1878 | for (int i = 0; i < callbackCount; i++) { |
| 1879 | // Apps ops are mapped to a singleton |
| 1880 | if (i == 0) { |
| 1881 | activeCallbacks.valueAt(i).destroy(); |
| 1882 | } |
| 1883 | } |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | @Override |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 1888 | public int startOperation(IBinder token, int code, int uid, String packageName, |
| 1889 | boolean startIfModeDefault) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1890 | verifyIncomingUid(uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 1891 | verifyIncomingOp(code); |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1892 | String resolvedPackageName = resolvePackageName(uid, packageName); |
| 1893 | if (resolvedPackageName == null) { |
| 1894 | return AppOpsManager.MODE_IGNORED; |
| 1895 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1896 | ClientState client = (ClientState)token; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1897 | synchronized (this) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1898 | final Ops ops = getOpsRawLocked(uid, resolvedPackageName, true /* edit */, |
Yohei Yukawa | a965d65 | 2017-10-12 15:02:26 -0700 | [diff] [blame] | 1899 | false /* uidMismatchExpected */); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1900 | if (ops == null) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1901 | 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] | 1902 | + " package " + resolvedPackageName); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 1903 | return AppOpsManager.MODE_ERRORED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1904 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1905 | final Op op = getOpLocked(ops, code, true); |
Svet Ganov | 442ed57 | 2016-08-17 17:29:43 -0700 | [diff] [blame] | 1906 | if (isOpRestrictedLocked(uid, code, resolvedPackageName)) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 1907 | return AppOpsManager.MODE_IGNORED; |
| 1908 | } |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1909 | final int switchCode = AppOpsManager.opToSwitch(code); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1910 | final UidState uidState = ops.uidState; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1911 | // If there is a non-default per UID policy (we set UID op mode only if |
| 1912 | // non-default) it takes over, otherwise use the per package policy. |
| 1913 | if (uidState.opModes != null && uidState.opModes.indexOfKey(switchCode) >= 0) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 1914 | final int uidMode = uidState.evalMode(uidState.opModes.get(switchCode)); |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 1915 | if (uidMode != AppOpsManager.MODE_ALLOWED |
| 1916 | && (!startIfModeDefault || uidMode != AppOpsManager.MODE_DEFAULT)) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 1917 | if (DEBUG) Slog.d(TAG, "noteOperation: uid reject #" + uidMode + " for code " |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1918 | + switchCode + " (" + code + ") uid " + uid + " package " |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1919 | + resolvedPackageName); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1920 | op.rejectTime[uidState.state] = System.currentTimeMillis(); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1921 | return uidMode; |
| 1922 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1923 | } else { |
| 1924 | final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 1925 | final int mode = switchOp.getMode(); |
| 1926 | if (mode != AppOpsManager.MODE_ALLOWED |
| 1927 | && (!startIfModeDefault || mode != AppOpsManager.MODE_DEFAULT)) { |
| 1928 | if (DEBUG) Slog.d(TAG, "startOperation: reject #" + mode + " for code " |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1929 | + switchCode + " (" + code + ") uid " + uid + " package " |
| 1930 | + resolvedPackageName); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1931 | op.rejectTime[uidState.state] = System.currentTimeMillis(); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 1932 | return mode; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1933 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1934 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1935 | if (DEBUG) Slog.d(TAG, "startOperation: allowing code " + code + " uid " + uid |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1936 | + " package " + resolvedPackageName); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1937 | if (op.startNesting == 0) { |
| 1938 | op.startRealtime = SystemClock.elapsedRealtime(); |
| 1939 | op.time[uidState.state] = System.currentTimeMillis(); |
| 1940 | op.rejectTime[uidState.state] = 0; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1941 | op.duration = -1; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1942 | scheduleOpActiveChangedIfNeededLocked(code, uid, packageName, true); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1943 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1944 | op.startNesting++; |
| 1945 | uidState.startNesting++; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1946 | if (client.mStartedOps != null) { |
| 1947 | client.mStartedOps.add(op); |
| 1948 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1949 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1950 | |
| 1951 | return AppOpsManager.MODE_ALLOWED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | @Override |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1955 | public void finishOperation(IBinder token, int code, int uid, String packageName) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1956 | verifyIncomingUid(uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 1957 | verifyIncomingOp(code); |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 1958 | String resolvedPackageName = resolvePackageName(uid, packageName); |
| 1959 | if (resolvedPackageName == null) { |
| 1960 | return; |
| 1961 | } |
| 1962 | if (!(token instanceof ClientState)) { |
| 1963 | return; |
| 1964 | } |
| 1965 | ClientState client = (ClientState) token; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1966 | synchronized (this) { |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 1967 | Op op = getOpLocked(code, uid, resolvedPackageName, true, true, false); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1968 | if (op == null) { |
| 1969 | return; |
| 1970 | } |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 1971 | if (!client.mStartedOps.remove(op)) { |
Svet Ganov | f5d5af1 | 2018-03-18 11:51:17 -0700 | [diff] [blame] | 1972 | // We finish ops when packages get removed to guarantee no dangling |
| 1973 | // started ops. However, some part of the system may asynchronously |
| 1974 | // finish ops for an already gone package. Hence, finishing an op |
| 1975 | // for a non existing package is fine and we don't log as a wtf. |
| 1976 | final long identity = Binder.clearCallingIdentity(); |
| 1977 | try { |
| 1978 | if (LocalServices.getService(PackageManagerInternal.class).getPackageUid( |
| 1979 | resolvedPackageName, 0, UserHandle.getUserId(uid)) < 0) { |
| 1980 | Slog.i(TAG, "Finishing op=" + AppOpsManager.opToName(code) |
| 1981 | + " for non-existing package=" + resolvedPackageName |
| 1982 | + " in uid=" + uid); |
| 1983 | return; |
| 1984 | } |
| 1985 | } finally { |
| 1986 | Binder.restoreCallingIdentity(identity); |
| 1987 | } |
| 1988 | Slog.wtf(TAG, "Operation not started: uid=" + op.uid + " pkg=" |
| 1989 | + op.packageName + " op=" + AppOpsManager.opToName(op.op)); |
Svet Ganov | 31d83ae | 2018-03-15 10:45:56 -0700 | [diff] [blame] | 1990 | return; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1991 | } |
Svet Ganov | a7a0db6 | 2018-02-27 20:08:01 -0800 | [diff] [blame] | 1992 | finishOperationLocked(op, /*finishNested*/ false); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 1993 | if (op.startNesting <= 0) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 1994 | scheduleOpActiveChangedIfNeededLocked(code, uid, packageName, false); |
| 1995 | } |
| 1996 | } |
| 1997 | } |
| 1998 | |
| 1999 | private void scheduleOpActiveChangedIfNeededLocked(int code, int uid, String packageName, |
| 2000 | boolean active) { |
| 2001 | ArraySet<ActiveCallback> dispatchedCallbacks = null; |
| 2002 | final int callbackListCount = mActiveWatchers.size(); |
| 2003 | for (int i = 0; i < callbackListCount; i++) { |
| 2004 | final SparseArray<ActiveCallback> callbacks = mActiveWatchers.valueAt(i); |
| 2005 | ActiveCallback callback = callbacks.get(code); |
| 2006 | if (callback != null) { |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 2007 | if (callback.mWatchingUid >= 0 && callback.mWatchingUid != uid) { |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 2008 | continue; |
| 2009 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 2010 | if (dispatchedCallbacks == null) { |
| 2011 | dispatchedCallbacks = new ArraySet<>(); |
| 2012 | } |
| 2013 | dispatchedCallbacks.add(callback); |
| 2014 | } |
| 2015 | } |
| 2016 | if (dispatchedCallbacks == null) { |
| 2017 | return; |
| 2018 | } |
| 2019 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 2020 | AppOpsService::notifyOpActiveChanged, |
| 2021 | this, dispatchedCallbacks, code, uid, packageName, active)); |
| 2022 | } |
| 2023 | |
| 2024 | private void notifyOpActiveChanged(ArraySet<ActiveCallback> callbacks, |
| 2025 | int code, int uid, String packageName, boolean active) { |
| 2026 | // There are components watching for mode changes such as window manager |
| 2027 | // and location manager which are in our process. The callbacks in these |
| 2028 | // components may require permissions our remote caller does not have. |
| 2029 | final long identity = Binder.clearCallingIdentity(); |
| 2030 | try { |
| 2031 | final int callbackCount = callbacks.size(); |
| 2032 | for (int i = 0; i < callbackCount; i++) { |
| 2033 | final ActiveCallback callback = callbacks.valueAt(i); |
| 2034 | try { |
| 2035 | callback.mCallback.opActiveChanged(code, uid, packageName, active); |
| 2036 | } catch (RemoteException e) { |
| 2037 | /* do nothing */ |
| 2038 | } |
| 2039 | } |
| 2040 | } finally { |
| 2041 | Binder.restoreCallingIdentity(identity); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2042 | } |
| 2043 | } |
| 2044 | |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 2045 | @Override |
| 2046 | public int permissionToOpCode(String permission) { |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 2047 | if (permission == null) { |
| 2048 | return AppOpsManager.OP_NONE; |
| 2049 | } |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 2050 | return AppOpsManager.permissionToOpCode(permission); |
| 2051 | } |
| 2052 | |
Svet Ganov | a7a0db6 | 2018-02-27 20:08:01 -0800 | [diff] [blame] | 2053 | void finishOperationLocked(Op op, boolean finishNested) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2054 | if (op.startNesting <= 1 || finishNested) { |
| 2055 | if (op.startNesting == 1 || finishNested) { |
| 2056 | op.duration = (int)(SystemClock.elapsedRealtime() - op.startRealtime); |
| 2057 | op.time[op.uidState.state] = System.currentTimeMillis(); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2058 | } else { |
| 2059 | Slog.w(TAG, "Finishing op nesting under-run: uid " + op.uid + " pkg " |
| 2060 | + op.packageName + " code " + op.op + " time=" + op.time |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2061 | + " duration=" + op.duration + " nesting=" + op.startNesting); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2062 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2063 | if (op.startNesting >= 1) { |
| 2064 | op.uidState.startNesting -= op.startNesting; |
| 2065 | } |
| 2066 | op.startNesting = 0; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2067 | } else { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2068 | op.startNesting--; |
| 2069 | op.uidState.startNesting--; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2070 | } |
| 2071 | } |
| 2072 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2073 | private void verifyIncomingUid(int uid) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2074 | if (uid == Binder.getCallingUid()) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2075 | return; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2076 | } |
| 2077 | if (Binder.getCallingPid() == Process.myPid()) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2078 | return; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2079 | } |
| 2080 | mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS, |
| 2081 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2082 | } |
| 2083 | |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 2084 | private void verifyIncomingOp(int op) { |
| 2085 | if (op >= 0 && op < AppOpsManager._NUM_OP) { |
| 2086 | return; |
| 2087 | } |
| 2088 | throw new IllegalArgumentException("Bad operation #" + op); |
| 2089 | } |
| 2090 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2091 | private UidState getUidStateLocked(int uid, boolean edit) { |
| 2092 | UidState uidState = mUidStates.get(uid); |
| 2093 | if (uidState == null) { |
| 2094 | if (!edit) { |
| 2095 | return null; |
| 2096 | } |
| 2097 | uidState = new UidState(uid); |
| 2098 | mUidStates.put(uid, uidState); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 2099 | } else { |
| 2100 | if (uidState.pendingStateCommitTime != 0) { |
Dianne Hackborn | 9fb9350 | 2018-06-18 12:29:44 -0700 | [diff] [blame] | 2101 | if (uidState.pendingStateCommitTime < mLastRealtime) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 2102 | commitUidPendingStateLocked(uidState); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 2103 | } else { |
Dianne Hackborn | 9fb9350 | 2018-06-18 12:29:44 -0700 | [diff] [blame] | 2104 | mLastRealtime = SystemClock.elapsedRealtime(); |
| 2105 | if (uidState.pendingStateCommitTime < mLastRealtime) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 2106 | commitUidPendingStateLocked(uidState); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 2107 | } |
| 2108 | } |
| 2109 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2110 | } |
| 2111 | return uidState; |
| 2112 | } |
| 2113 | |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 2114 | private void commitUidPendingStateLocked(UidState uidState) { |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 2115 | final boolean lastForeground = uidState.state <= UID_STATE_LAST_NON_RESTRICTED; |
| 2116 | final boolean nowForeground = uidState.pendingState <= UID_STATE_LAST_NON_RESTRICTED; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 2117 | uidState.state = uidState.pendingState; |
| 2118 | uidState.pendingStateCommitTime = 0; |
Dianne Hackborn | e93ab41 | 2018-05-14 17:52:30 -0700 | [diff] [blame] | 2119 | if (uidState.hasForegroundWatchers && lastForeground != nowForeground) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 2120 | for (int fgi = uidState.foregroundOps.size() - 1; fgi >= 0; fgi--) { |
| 2121 | if (!uidState.foregroundOps.valueAt(fgi)) { |
| 2122 | continue; |
| 2123 | } |
| 2124 | final int code = uidState.foregroundOps.keyAt(fgi); |
| 2125 | |
| 2126 | final ArraySet<ModeCallback> callbacks = mOpModeWatchers.get(code); |
| 2127 | if (callbacks != null) { |
| 2128 | for (int cbi = callbacks.size() - 1; cbi >= 0; cbi--) { |
| 2129 | final ModeCallback callback = callbacks.valueAt(cbi); |
| 2130 | if ((callback.mFlags & AppOpsManager.WATCH_FOREGROUND_CHANGES) == 0 |
| 2131 | || !callback.isWatchingUid(uidState.uid)) { |
| 2132 | continue; |
| 2133 | } |
| 2134 | boolean doAllPackages = uidState.opModes != null |
Hai Zhang | 2b98fb3 | 2018-09-21 15:18:46 -0700 | [diff] [blame] | 2135 | && uidState.opModes.indexOfKey(code) >= 0 |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 2136 | && uidState.opModes.get(code) == AppOpsManager.MODE_FOREGROUND; |
| 2137 | if (uidState.pkgOps != null) { |
| 2138 | for (int pkgi = uidState.pkgOps.size() - 1; pkgi >= 0; pkgi--) { |
| 2139 | final Op op = uidState.pkgOps.valueAt(pkgi).get(code); |
| 2140 | if (doAllPackages || (op != null |
| 2141 | && op.mode == AppOpsManager.MODE_FOREGROUND)) { |
| 2142 | mHandler.sendMessage(PooledLambda.obtainMessage( |
| 2143 | AppOpsService::notifyOpChanged, |
| 2144 | this, callback, code, uidState.uid, |
| 2145 | uidState.pkgOps.keyAt(pkgi))); |
| 2146 | } |
| 2147 | } |
| 2148 | } |
| 2149 | } |
| 2150 | } |
| 2151 | } |
| 2152 | } |
| 2153 | } |
| 2154 | |
Yohei Yukawa | a965d65 | 2017-10-12 15:02:26 -0700 | [diff] [blame] | 2155 | private Ops getOpsRawLocked(int uid, String packageName, boolean edit, |
| 2156 | boolean uidMismatchExpected) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2157 | UidState uidState = getUidStateLocked(uid, edit); |
| 2158 | if (uidState == null) { |
| 2159 | return null; |
| 2160 | } |
| 2161 | |
| 2162 | if (uidState.pkgOps == null) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2163 | if (!edit) { |
| 2164 | return null; |
| 2165 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2166 | uidState.pkgOps = new ArrayMap<>(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2167 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2168 | |
| 2169 | Ops ops = uidState.pkgOps.get(packageName); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2170 | if (ops == null) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2171 | if (!edit) { |
| 2172 | return null; |
| 2173 | } |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 2174 | boolean isPrivileged = false; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2175 | // This is the first time we have seen this package name under this uid, |
| 2176 | // so let's make sure it is valid. |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 2177 | if (uid != 0) { |
| 2178 | final long ident = Binder.clearCallingIdentity(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2179 | try { |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 2180 | int pkgUid = -1; |
| 2181 | try { |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 2182 | ApplicationInfo appInfo = ActivityThread.getPackageManager() |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 2183 | .getApplicationInfo(packageName, |
Svet Ganov | ad0a49b | 2018-10-29 10:07:08 -0700 | [diff] [blame] | 2184 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 2185 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 2186 | UserHandle.getUserId(uid)); |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 2187 | if (appInfo != null) { |
| 2188 | pkgUid = appInfo.uid; |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 2189 | isPrivileged = (appInfo.privateFlags |
| 2190 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 2191 | } else { |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 2192 | pkgUid = resolveUid(packageName); |
| 2193 | if (pkgUid >= 0) { |
Chien-Yu Chen | 75cade0 | 2016-01-11 10:56:21 -0800 | [diff] [blame] | 2194 | isPrivileged = false; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 2195 | } |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 2196 | } |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 2197 | } catch (RemoteException e) { |
| 2198 | Slog.w(TAG, "Could not contact PackageManager", e); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 2199 | } |
| 2200 | if (pkgUid != uid) { |
| 2201 | // Oops! The package name is not valid for the uid they are calling |
| 2202 | // under. Abort. |
Yohei Yukawa | a965d65 | 2017-10-12 15:02:26 -0700 | [diff] [blame] | 2203 | if (!uidMismatchExpected) { |
| 2204 | RuntimeException ex = new RuntimeException("here"); |
| 2205 | ex.fillInStackTrace(); |
| 2206 | Slog.w(TAG, "Bad call: specified package " + packageName |
| 2207 | + " under uid " + uid + " but it is really " + pkgUid, ex); |
| 2208 | } |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 2209 | return null; |
| 2210 | } |
| 2211 | } finally { |
| 2212 | Binder.restoreCallingIdentity(ident); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2213 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2214 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2215 | ops = new Ops(packageName, uidState, isPrivileged); |
| 2216 | uidState.pkgOps.put(packageName, ops); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2217 | } |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 2218 | return ops; |
| 2219 | } |
| 2220 | |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 2221 | /** |
| 2222 | * Get the state of all ops for a package, <b>don't verify that package belongs to uid</b>. |
| 2223 | * |
| 2224 | * <p>Usually callers should use {@link #getOpLocked} and not call this directly. |
| 2225 | * |
| 2226 | * @param uid The uid the of the package |
| 2227 | * @param packageName The package name for which to get the state for |
| 2228 | * @param edit Iff {@code true} create the {@link Ops} object if not yet created |
| 2229 | * @param isPrivileged Whether the package is privileged or not |
| 2230 | * |
| 2231 | * @return The {@link Ops state} of all ops for the package |
| 2232 | */ |
| 2233 | private @Nullable Ops getOpsRawNoVerifyLocked(int uid, @NonNull String packageName, |
| 2234 | boolean edit, boolean isPrivileged) { |
| 2235 | UidState uidState = getUidStateLocked(uid, edit); |
| 2236 | if (uidState == null) { |
| 2237 | return null; |
| 2238 | } |
| 2239 | |
| 2240 | if (uidState.pkgOps == null) { |
| 2241 | if (!edit) { |
| 2242 | return null; |
| 2243 | } |
| 2244 | uidState.pkgOps = new ArrayMap<>(); |
| 2245 | } |
| 2246 | |
| 2247 | Ops ops = uidState.pkgOps.get(packageName); |
| 2248 | if (ops == null) { |
| 2249 | if (!edit) { |
| 2250 | return null; |
| 2251 | } |
| 2252 | ops = new Ops(packageName, uidState, isPrivileged); |
| 2253 | uidState.pkgOps.put(packageName, ops); |
| 2254 | } |
| 2255 | return ops; |
| 2256 | } |
| 2257 | |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 2258 | private void scheduleWriteLocked() { |
| 2259 | if (!mWriteScheduled) { |
| 2260 | mWriteScheduled = true; |
| 2261 | mHandler.postDelayed(mWriteRunner, WRITE_DELAY); |
| 2262 | } |
| 2263 | } |
| 2264 | |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 2265 | private void scheduleFastWriteLocked() { |
| 2266 | if (!mFastWriteScheduled) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 2267 | mWriteScheduled = true; |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 2268 | mFastWriteScheduled = true; |
| 2269 | mHandler.removeCallbacks(mWriteRunner); |
| 2270 | mHandler.postDelayed(mWriteRunner, 10*1000); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 2271 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 2272 | } |
| 2273 | |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 2274 | /** |
| 2275 | * Get the state of an op for a uid. |
| 2276 | * |
| 2277 | * @param code The code of the op |
| 2278 | * @param uid The uid the of the package |
| 2279 | * @param packageName The package name for which to get the state for |
| 2280 | * @param edit Iff {@code true} create the {@link Op} object if not yet created |
| 2281 | * @param verifyUid Iff {@code true} check that the package belongs to the uid |
| 2282 | * @param isPrivileged Whether the package is privileged or not (only used if {@code verifyUid |
| 2283 | * == false}) |
| 2284 | * |
| 2285 | * @return The {@link Op state} of the op |
| 2286 | */ |
| 2287 | private @Nullable Op getOpLocked(int code, int uid, @NonNull String packageName, boolean edit, |
| 2288 | boolean verifyUid, boolean isPrivileged) { |
| 2289 | Ops ops; |
| 2290 | |
| 2291 | if (verifyUid) { |
| 2292 | ops = getOpsRawLocked(uid, packageName, edit, false /* uidMismatchExpected */); |
| 2293 | } else { |
| 2294 | ops = getOpsRawNoVerifyLocked(uid, packageName, edit, isPrivileged); |
| 2295 | } |
| 2296 | |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 2297 | if (ops == null) { |
| 2298 | return null; |
| 2299 | } |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 2300 | return getOpLocked(ops, code, edit); |
| 2301 | } |
| 2302 | |
| 2303 | private Op getOpLocked(Ops ops, int code, boolean edit) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2304 | Op op = ops.get(code); |
| 2305 | if (op == null) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2306 | if (!edit) { |
| 2307 | return null; |
| 2308 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2309 | op = new Op(ops.uidState, ops.packageName, code); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2310 | ops.put(code, op); |
| 2311 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 2312 | if (edit) { |
| 2313 | scheduleWriteLocked(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2314 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2315 | return op; |
| 2316 | } |
| 2317 | |
Svet Ganov | 442ed57 | 2016-08-17 17:29:43 -0700 | [diff] [blame] | 2318 | private boolean isOpRestrictedLocked(int uid, int code, String packageName) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 2319 | int userHandle = UserHandle.getUserId(uid); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2320 | final int restrictionSetCount = mOpUserRestrictions.size(); |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2321 | |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2322 | for (int i = 0; i < restrictionSetCount; i++) { |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2323 | // For each client, check that the given op is not restricted, or that the given |
| 2324 | // package is exempt from the restriction. |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 2325 | ClientRestrictionState restrictionState = mOpUserRestrictions.valueAt(i); |
Suprabh Shukla | ffddadb | 2016-05-20 16:37:26 -0700 | [diff] [blame] | 2326 | if (restrictionState.hasRestriction(code, packageName, userHandle)) { |
| 2327 | if (AppOpsManager.opAllowSystemBypassRestriction(code)) { |
| 2328 | // If we are the system, bypass user restrictions for certain codes |
| 2329 | synchronized (this) { |
Yohei Yukawa | a965d65 | 2017-10-12 15:02:26 -0700 | [diff] [blame] | 2330 | Ops ops = getOpsRawLocked(uid, packageName, true /* edit */, |
| 2331 | false /* uidMismatchExpected */); |
Suprabh Shukla | ffddadb | 2016-05-20 16:37:26 -0700 | [diff] [blame] | 2332 | if ((ops != null) && ops.isPrivileged) { |
| 2333 | return false; |
| 2334 | } |
Ruben Brunk | 32f0fa4 | 2016-03-11 19:07:07 -0800 | [diff] [blame] | 2335 | } |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2336 | } |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2337 | return true; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 2338 | } |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 2339 | } |
| 2340 | return false; |
| 2341 | } |
| 2342 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2343 | void readState() { |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 2344 | int oldVersion = NO_VERSION; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2345 | synchronized (mFile) { |
| 2346 | synchronized (this) { |
| 2347 | FileInputStream stream; |
| 2348 | try { |
| 2349 | stream = mFile.openRead(); |
| 2350 | } catch (FileNotFoundException e) { |
| 2351 | Slog.i(TAG, "No existing app ops " + mFile.getBaseFile() + "; starting empty"); |
| 2352 | return; |
| 2353 | } |
| 2354 | boolean success = false; |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 2355 | mUidStates.clear(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2356 | try { |
| 2357 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 2358 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2359 | int type; |
| 2360 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 2361 | && type != XmlPullParser.END_DOCUMENT) { |
| 2362 | ; |
| 2363 | } |
| 2364 | |
| 2365 | if (type != XmlPullParser.START_TAG) { |
| 2366 | throw new IllegalStateException("no start tag found"); |
| 2367 | } |
| 2368 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 2369 | final String versionString = parser.getAttributeValue(null, "v"); |
| 2370 | if (versionString != null) { |
| 2371 | oldVersion = Integer.parseInt(versionString); |
| 2372 | } |
| 2373 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2374 | int outerDepth = parser.getDepth(); |
| 2375 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 2376 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 2377 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 2378 | continue; |
| 2379 | } |
| 2380 | |
| 2381 | String tagName = parser.getName(); |
| 2382 | if (tagName.equals("pkg")) { |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 2383 | readPackage(parser); |
Svetoslav | 215b44a | 2015-08-04 19:03:40 -0700 | [diff] [blame] | 2384 | } else if (tagName.equals("uid")) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2385 | readUidOps(parser); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2386 | } else { |
| 2387 | Slog.w(TAG, "Unknown element under <app-ops>: " |
| 2388 | + parser.getName()); |
| 2389 | XmlUtils.skipCurrentTag(parser); |
| 2390 | } |
| 2391 | } |
| 2392 | success = true; |
| 2393 | } catch (IllegalStateException e) { |
| 2394 | Slog.w(TAG, "Failed parsing " + e); |
| 2395 | } catch (NullPointerException e) { |
| 2396 | Slog.w(TAG, "Failed parsing " + e); |
| 2397 | } catch (NumberFormatException e) { |
| 2398 | Slog.w(TAG, "Failed parsing " + e); |
| 2399 | } catch (XmlPullParserException e) { |
| 2400 | Slog.w(TAG, "Failed parsing " + e); |
| 2401 | } catch (IOException e) { |
| 2402 | Slog.w(TAG, "Failed parsing " + e); |
| 2403 | } catch (IndexOutOfBoundsException e) { |
| 2404 | Slog.w(TAG, "Failed parsing " + e); |
| 2405 | } finally { |
| 2406 | if (!success) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2407 | mUidStates.clear(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2408 | } |
| 2409 | try { |
| 2410 | stream.close(); |
| 2411 | } catch (IOException e) { |
| 2412 | } |
| 2413 | } |
| 2414 | } |
| 2415 | } |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 2416 | synchronized (this) { |
| 2417 | upgradeLocked(oldVersion); |
| 2418 | } |
| 2419 | } |
| 2420 | |
| 2421 | private void upgradeRunAnyInBackgroundLocked() { |
| 2422 | for (int i = 0; i < mUidStates.size(); i++) { |
| 2423 | final UidState uidState = mUidStates.valueAt(i); |
| 2424 | if (uidState == null) { |
| 2425 | continue; |
| 2426 | } |
| 2427 | if (uidState.opModes != null) { |
| 2428 | final int idx = uidState.opModes.indexOfKey(AppOpsManager.OP_RUN_IN_BACKGROUND); |
| 2429 | if (idx >= 0) { |
| 2430 | uidState.opModes.put(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, |
| 2431 | uidState.opModes.valueAt(idx)); |
| 2432 | } |
| 2433 | } |
| 2434 | if (uidState.pkgOps == null) { |
| 2435 | continue; |
| 2436 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 2437 | boolean changed = false; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 2438 | for (int j = 0; j < uidState.pkgOps.size(); j++) { |
| 2439 | Ops ops = uidState.pkgOps.valueAt(j); |
| 2440 | if (ops != null) { |
| 2441 | final Op op = ops.get(AppOpsManager.OP_RUN_IN_BACKGROUND); |
| 2442 | if (op != null && op.mode != AppOpsManager.opToDefaultMode(op.op)) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2443 | final Op copy = new Op(op.uidState, op.packageName, |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 2444 | AppOpsManager.OP_RUN_ANY_IN_BACKGROUND); |
| 2445 | copy.mode = op.mode; |
| 2446 | ops.put(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, copy); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 2447 | changed = true; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 2448 | } |
| 2449 | } |
| 2450 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 2451 | if (changed) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 2452 | uidState.evalForegroundOps(mOpModeWatchers); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 2453 | } |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 2454 | } |
| 2455 | } |
| 2456 | |
| 2457 | private void upgradeLocked(int oldVersion) { |
| 2458 | if (oldVersion >= CURRENT_VERSION) { |
| 2459 | return; |
| 2460 | } |
| 2461 | Slog.d(TAG, "Upgrading app-ops xml from version " + oldVersion + " to " + CURRENT_VERSION); |
| 2462 | switch (oldVersion) { |
| 2463 | case NO_VERSION: |
| 2464 | upgradeRunAnyInBackgroundLocked(); |
| 2465 | // fall through |
| 2466 | case 1: |
| 2467 | // for future upgrades |
| 2468 | } |
| 2469 | scheduleFastWriteLocked(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2470 | } |
| 2471 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2472 | void readUidOps(XmlPullParser parser) throws NumberFormatException, |
| 2473 | XmlPullParserException, IOException { |
| 2474 | final int uid = Integer.parseInt(parser.getAttributeValue(null, "n")); |
| 2475 | int outerDepth = parser.getDepth(); |
| 2476 | int type; |
| 2477 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 2478 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 2479 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 2480 | continue; |
| 2481 | } |
| 2482 | |
| 2483 | String tagName = parser.getName(); |
| 2484 | if (tagName.equals("op")) { |
| 2485 | final int code = Integer.parseInt(parser.getAttributeValue(null, "n")); |
| 2486 | final int mode = Integer.parseInt(parser.getAttributeValue(null, "m")); |
| 2487 | UidState uidState = getUidStateLocked(uid, true); |
| 2488 | if (uidState.opModes == null) { |
| 2489 | uidState.opModes = new SparseIntArray(); |
| 2490 | } |
| 2491 | uidState.opModes.put(code, mode); |
| 2492 | } else { |
| 2493 | Slog.w(TAG, "Unknown element under <uid-ops>: " |
| 2494 | + parser.getName()); |
| 2495 | XmlUtils.skipCurrentTag(parser); |
| 2496 | } |
| 2497 | } |
| 2498 | } |
| 2499 | |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 2500 | void readPackage(XmlPullParser parser) throws NumberFormatException, |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2501 | XmlPullParserException, IOException { |
| 2502 | String pkgName = parser.getAttributeValue(null, "n"); |
| 2503 | int outerDepth = parser.getDepth(); |
| 2504 | int type; |
| 2505 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 2506 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 2507 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 2508 | continue; |
| 2509 | } |
| 2510 | |
| 2511 | String tagName = parser.getName(); |
| 2512 | if (tagName.equals("uid")) { |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 2513 | readUid(parser, pkgName); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2514 | } else { |
| 2515 | Slog.w(TAG, "Unknown element under <pkg>: " |
| 2516 | + parser.getName()); |
| 2517 | XmlUtils.skipCurrentTag(parser); |
| 2518 | } |
| 2519 | } |
| 2520 | } |
| 2521 | |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 2522 | void readUid(XmlPullParser parser, String pkgName) throws NumberFormatException, |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2523 | XmlPullParserException, IOException { |
| 2524 | int uid = Integer.parseInt(parser.getAttributeValue(null, "n")); |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 2525 | String isPrivilegedString = parser.getAttributeValue(null, "p"); |
| 2526 | boolean isPrivileged = false; |
| 2527 | if (isPrivilegedString == null) { |
| 2528 | try { |
| 2529 | IPackageManager packageManager = ActivityThread.getPackageManager(); |
| 2530 | if (packageManager != null) { |
| 2531 | ApplicationInfo appInfo = ActivityThread.getPackageManager() |
| 2532 | .getApplicationInfo(pkgName, 0, UserHandle.getUserId(uid)); |
| 2533 | if (appInfo != null) { |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 2534 | isPrivileged = (appInfo.privateFlags |
| 2535 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 2536 | } |
| 2537 | } else { |
| 2538 | // Could not load data, don't add to cache so it will be loaded later. |
| 2539 | return; |
| 2540 | } |
| 2541 | } catch (RemoteException e) { |
| 2542 | Slog.w(TAG, "Could not contact PackageManager", e); |
| 2543 | } |
| 2544 | } else { |
| 2545 | isPrivileged = Boolean.parseBoolean(isPrivilegedString); |
| 2546 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2547 | int outerDepth = parser.getDepth(); |
| 2548 | int type; |
| 2549 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 2550 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 2551 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 2552 | continue; |
| 2553 | } |
| 2554 | |
| 2555 | String tagName = parser.getName(); |
| 2556 | if (tagName.equals("op")) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2557 | UidState uidState = getUidStateLocked(uid, true); |
| 2558 | if (uidState.pkgOps == null) { |
| 2559 | uidState.pkgOps = new ArrayMap<>(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2560 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2561 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2562 | Op op = new Op(uidState, pkgName, |
| 2563 | Integer.parseInt(parser.getAttributeValue(null, "n"))); |
| 2564 | |
| 2565 | for (int i = parser.getAttributeCount()-1; i >= 0; i--) { |
| 2566 | final String name = parser.getAttributeName(i); |
| 2567 | final String value = parser.getAttributeValue(i); |
| 2568 | switch (name) { |
| 2569 | case "m": |
| 2570 | op.mode = Integer.parseInt(value); |
| 2571 | break; |
| 2572 | case "d": |
| 2573 | op.duration = Integer.parseInt(value); |
| 2574 | break; |
| 2575 | case "pu": |
| 2576 | op.proxyUid = Integer.parseInt(value); |
| 2577 | break; |
| 2578 | case "pp": |
| 2579 | op.proxyPackageName = value; |
| 2580 | break; |
| 2581 | case "tp": |
| 2582 | op.time[AppOpsManager.UID_STATE_PERSISTENT] = Long.parseLong(value); |
| 2583 | break; |
| 2584 | case "tt": |
| 2585 | op.time[AppOpsManager.UID_STATE_TOP] = Long.parseLong(value); |
| 2586 | break; |
| 2587 | case "tfs": |
| 2588 | op.time[AppOpsManager.UID_STATE_FOREGROUND_SERVICE] |
| 2589 | = Long.parseLong(value); |
| 2590 | break; |
| 2591 | case "tf": |
| 2592 | op.time[AppOpsManager.UID_STATE_FOREGROUND] = Long.parseLong(value); |
| 2593 | break; |
| 2594 | case "tb": |
| 2595 | op.time[AppOpsManager.UID_STATE_BACKGROUND] = Long.parseLong(value); |
| 2596 | break; |
| 2597 | case "tc": |
| 2598 | op.time[AppOpsManager.UID_STATE_CACHED] = Long.parseLong(value); |
| 2599 | break; |
| 2600 | case "rp": |
| 2601 | op.rejectTime[AppOpsManager.UID_STATE_PERSISTENT] |
| 2602 | = Long.parseLong(value); |
| 2603 | break; |
| 2604 | case "rt": |
| 2605 | op.rejectTime[AppOpsManager.UID_STATE_TOP] = Long.parseLong(value); |
| 2606 | break; |
| 2607 | case "rfs": |
| 2608 | op.rejectTime[AppOpsManager.UID_STATE_FOREGROUND_SERVICE] |
| 2609 | = Long.parseLong(value); |
| 2610 | break; |
| 2611 | case "rf": |
| 2612 | op.rejectTime[AppOpsManager.UID_STATE_FOREGROUND] |
| 2613 | = Long.parseLong(value); |
| 2614 | break; |
| 2615 | case "rb": |
| 2616 | op.rejectTime[AppOpsManager.UID_STATE_BACKGROUND] |
| 2617 | = Long.parseLong(value); |
| 2618 | break; |
| 2619 | case "rc": |
| 2620 | op.rejectTime[AppOpsManager.UID_STATE_CACHED] |
| 2621 | = Long.parseLong(value); |
| 2622 | break; |
| 2623 | case "t": |
| 2624 | // Backwards compat. |
| 2625 | op.time[AppOpsManager.UID_STATE_TOP] = Long.parseLong(value); |
| 2626 | break; |
| 2627 | case "r": |
| 2628 | // Backwards compat. |
| 2629 | op.rejectTime[AppOpsManager.UID_STATE_TOP] = Long.parseLong(value); |
| 2630 | break; |
| 2631 | default: |
| 2632 | Slog.w(TAG, "Unknown attribute in 'op' tag: " + name); |
| 2633 | break; |
| 2634 | } |
| 2635 | } |
| 2636 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2637 | Ops ops = uidState.pkgOps.get(pkgName); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2638 | if (ops == null) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2639 | ops = new Ops(pkgName, uidState, isPrivileged); |
| 2640 | uidState.pkgOps.put(pkgName, ops); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2641 | } |
| 2642 | ops.put(op.op, op); |
| 2643 | } else { |
| 2644 | Slog.w(TAG, "Unknown element under <pkg>: " |
| 2645 | + parser.getName()); |
| 2646 | XmlUtils.skipCurrentTag(parser); |
| 2647 | } |
| 2648 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 2649 | UidState uidState = getUidStateLocked(uid, false); |
| 2650 | if (uidState != null) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 2651 | uidState.evalForegroundOps(mOpModeWatchers); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 2652 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2653 | } |
| 2654 | |
| 2655 | void writeState() { |
| 2656 | synchronized (mFile) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2657 | FileOutputStream stream; |
| 2658 | try { |
| 2659 | stream = mFile.startWrite(); |
| 2660 | } catch (IOException e) { |
| 2661 | Slog.w(TAG, "Failed to write state: " + e); |
| 2662 | return; |
| 2663 | } |
| 2664 | |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 2665 | List<AppOpsManager.PackageOps> allOps = getPackagesForOps(null); |
| 2666 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2667 | try { |
| 2668 | XmlSerializer out = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 2669 | out.setOutput(stream, StandardCharsets.UTF_8.name()); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2670 | out.startDocument(null, true); |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 2671 | out.startTag(null, "app-ops"); |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 2672 | out.attribute(null, "v", String.valueOf(CURRENT_VERSION)); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2673 | |
| 2674 | final int uidStateCount = mUidStates.size(); |
| 2675 | for (int i = 0; i < uidStateCount; i++) { |
| 2676 | UidState uidState = mUidStates.valueAt(i); |
| 2677 | if (uidState.opModes != null && uidState.opModes.size() > 0) { |
| 2678 | out.startTag(null, "uid"); |
| 2679 | out.attribute(null, "n", Integer.toString(uidState.uid)); |
| 2680 | SparseIntArray uidOpModes = uidState.opModes; |
| 2681 | final int opCount = uidOpModes.size(); |
| 2682 | for (int j = 0; j < opCount; j++) { |
| 2683 | final int op = uidOpModes.keyAt(j); |
| 2684 | final int mode = uidOpModes.valueAt(j); |
| 2685 | out.startTag(null, "op"); |
| 2686 | out.attribute(null, "n", Integer.toString(op)); |
| 2687 | out.attribute(null, "m", Integer.toString(mode)); |
| 2688 | out.endTag(null, "op"); |
| 2689 | } |
| 2690 | out.endTag(null, "uid"); |
| 2691 | } |
| 2692 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2693 | |
| 2694 | if (allOps != null) { |
| 2695 | String lastPkg = null; |
| 2696 | for (int i=0; i<allOps.size(); i++) { |
| 2697 | AppOpsManager.PackageOps pkg = allOps.get(i); |
| 2698 | if (!pkg.getPackageName().equals(lastPkg)) { |
| 2699 | if (lastPkg != null) { |
| 2700 | out.endTag(null, "pkg"); |
| 2701 | } |
| 2702 | lastPkg = pkg.getPackageName(); |
| 2703 | out.startTag(null, "pkg"); |
| 2704 | out.attribute(null, "n", lastPkg); |
| 2705 | } |
| 2706 | out.startTag(null, "uid"); |
| 2707 | out.attribute(null, "n", Integer.toString(pkg.getUid())); |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 2708 | synchronized (this) { |
Yohei Yukawa | a965d65 | 2017-10-12 15:02:26 -0700 | [diff] [blame] | 2709 | Ops ops = getOpsRawLocked(pkg.getUid(), pkg.getPackageName(), |
| 2710 | false /* edit */, false /* uidMismatchExpected */); |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 2711 | // Should always be present as the list of PackageOps is generated |
| 2712 | // from Ops. |
| 2713 | if (ops != null) { |
| 2714 | out.attribute(null, "p", Boolean.toString(ops.isPrivileged)); |
| 2715 | } else { |
| 2716 | out.attribute(null, "p", Boolean.toString(false)); |
| 2717 | } |
| 2718 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2719 | List<AppOpsManager.OpEntry> ops = pkg.getOps(); |
| 2720 | for (int j=0; j<ops.size(); j++) { |
| 2721 | AppOpsManager.OpEntry op = ops.get(j); |
| 2722 | out.startTag(null, "op"); |
| 2723 | out.attribute(null, "n", Integer.toString(op.getOp())); |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 2724 | if (op.getMode() != AppOpsManager.opToDefaultMode(op.getOp())) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 2725 | out.attribute(null, "m", Integer.toString(op.getMode())); |
| 2726 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2727 | for (int k = 0; k < _NUM_UID_STATE; k++) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 2728 | final long time = op.getLastTimeFor(k); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2729 | if (time != 0) { |
| 2730 | out.attribute(null, UID_STATE_TIME_ATTRS[k], |
| 2731 | Long.toString(time)); |
| 2732 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 2733 | final long rejectTime = op.getLastRejectTimeFor(k); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2734 | if (rejectTime != 0) { |
| 2735 | out.attribute(null, UID_STATE_REJECT_ATTRS[k], |
| 2736 | Long.toString(rejectTime)); |
| 2737 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 2738 | } |
| 2739 | int dur = op.getDuration(); |
| 2740 | if (dur != 0) { |
| 2741 | out.attribute(null, "d", Integer.toString(dur)); |
| 2742 | } |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 2743 | int proxyUid = op.getProxyUid(); |
| 2744 | if (proxyUid != -1) { |
| 2745 | out.attribute(null, "pu", Integer.toString(proxyUid)); |
| 2746 | } |
| 2747 | String proxyPackageName = op.getProxyPackageName(); |
| 2748 | if (proxyPackageName != null) { |
| 2749 | out.attribute(null, "pp", proxyPackageName); |
| 2750 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 2751 | out.endTag(null, "op"); |
| 2752 | } |
| 2753 | out.endTag(null, "uid"); |
| 2754 | } |
| 2755 | if (lastPkg != null) { |
| 2756 | out.endTag(null, "pkg"); |
| 2757 | } |
| 2758 | } |
| 2759 | |
| 2760 | out.endTag(null, "app-ops"); |
| 2761 | out.endDocument(); |
| 2762 | mFile.finishWrite(stream); |
| 2763 | } catch (IOException e) { |
| 2764 | Slog.w(TAG, "Failed to write state, restoring backup.", e); |
| 2765 | mFile.failWrite(stream); |
| 2766 | } |
| 2767 | } |
| 2768 | } |
| 2769 | |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2770 | static class Shell extends ShellCommand { |
| 2771 | final IAppOpsService mInterface; |
| 2772 | final AppOpsService mInternal; |
| 2773 | |
| 2774 | int userId = UserHandle.USER_SYSTEM; |
| 2775 | String packageName; |
| 2776 | String opStr; |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 2777 | String modeStr; |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2778 | int op; |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 2779 | int mode; |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2780 | int packageUid; |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2781 | int nonpackageUid; |
Julia Reynolds | 6cb5fcc | 2018-02-27 17:33:52 -0500 | [diff] [blame] | 2782 | final static Binder sBinder = new Binder(); |
| 2783 | IBinder mToken; |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2784 | |
| 2785 | Shell(IAppOpsService iface, AppOpsService internal) { |
| 2786 | mInterface = iface; |
| 2787 | mInternal = internal; |
Julia Reynolds | 6cb5fcc | 2018-02-27 17:33:52 -0500 | [diff] [blame] | 2788 | try { |
| 2789 | mToken = mInterface.getToken(sBinder); |
| 2790 | } catch (RemoteException e) { |
| 2791 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2792 | } |
| 2793 | |
| 2794 | @Override |
| 2795 | public int onCommand(String cmd) { |
| 2796 | return onShellCommand(this, cmd); |
| 2797 | } |
| 2798 | |
| 2799 | @Override |
| 2800 | public void onHelp() { |
| 2801 | PrintWriter pw = getOutPrintWriter(); |
| 2802 | dumpCommandHelp(pw); |
| 2803 | } |
| 2804 | |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 2805 | static private int strOpToOp(String op, PrintWriter err) { |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2806 | try { |
| 2807 | return AppOpsManager.strOpToOp(op); |
| 2808 | } catch (IllegalArgumentException e) { |
| 2809 | } |
| 2810 | try { |
| 2811 | return Integer.parseInt(op); |
| 2812 | } catch (NumberFormatException e) { |
| 2813 | } |
| 2814 | try { |
| 2815 | return AppOpsManager.strDebugOpToOp(op); |
| 2816 | } catch (IllegalArgumentException e) { |
| 2817 | err.println("Error: " + e.getMessage()); |
| 2818 | return -1; |
| 2819 | } |
| 2820 | } |
| 2821 | |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 2822 | static int strModeToMode(String modeStr, PrintWriter err) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 2823 | for (int i = AppOpsManager.MODE_NAMES.length - 1; i >= 0; i--) { |
| 2824 | if (AppOpsManager.MODE_NAMES[i].equals(modeStr)) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 2825 | return i; |
| 2826 | } |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 2827 | } |
| 2828 | try { |
| 2829 | return Integer.parseInt(modeStr); |
| 2830 | } catch (NumberFormatException e) { |
| 2831 | } |
| 2832 | err.println("Error: Mode " + modeStr + " is not valid"); |
| 2833 | return -1; |
| 2834 | } |
| 2835 | |
| 2836 | int parseUserOpMode(int defMode, PrintWriter err) throws RemoteException { |
| 2837 | userId = UserHandle.USER_CURRENT; |
| 2838 | opStr = null; |
| 2839 | modeStr = null; |
| 2840 | for (String argument; (argument = getNextArg()) != null;) { |
| 2841 | if ("--user".equals(argument)) { |
| 2842 | userId = UserHandle.parseUserArg(getNextArgRequired()); |
| 2843 | } else { |
| 2844 | if (opStr == null) { |
| 2845 | opStr = argument; |
| 2846 | } else if (modeStr == null) { |
| 2847 | modeStr = argument; |
| 2848 | break; |
| 2849 | } |
| 2850 | } |
| 2851 | } |
| 2852 | if (opStr == null) { |
| 2853 | err.println("Error: Operation not specified."); |
| 2854 | return -1; |
| 2855 | } |
| 2856 | op = strOpToOp(opStr, err); |
| 2857 | if (op < 0) { |
| 2858 | return -1; |
| 2859 | } |
| 2860 | if (modeStr != null) { |
| 2861 | if ((mode=strModeToMode(modeStr, err)) < 0) { |
| 2862 | return -1; |
| 2863 | } |
| 2864 | } else { |
| 2865 | mode = defMode; |
| 2866 | } |
| 2867 | return 0; |
| 2868 | } |
| 2869 | |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2870 | int parseUserPackageOp(boolean reqOp, PrintWriter err) throws RemoteException { |
| 2871 | userId = UserHandle.USER_CURRENT; |
| 2872 | packageName = null; |
| 2873 | opStr = null; |
| 2874 | for (String argument; (argument = getNextArg()) != null;) { |
| 2875 | if ("--user".equals(argument)) { |
| 2876 | userId = UserHandle.parseUserArg(getNextArgRequired()); |
| 2877 | } else { |
| 2878 | if (packageName == null) { |
| 2879 | packageName = argument; |
| 2880 | } else if (opStr == null) { |
| 2881 | opStr = argument; |
| 2882 | break; |
| 2883 | } |
| 2884 | } |
| 2885 | } |
| 2886 | if (packageName == null) { |
| 2887 | err.println("Error: Package name not specified."); |
| 2888 | return -1; |
| 2889 | } else if (opStr == null && reqOp) { |
| 2890 | err.println("Error: Operation not specified."); |
| 2891 | return -1; |
| 2892 | } |
| 2893 | if (opStr != null) { |
| 2894 | op = strOpToOp(opStr, err); |
| 2895 | if (op < 0) { |
| 2896 | return -1; |
| 2897 | } |
| 2898 | } else { |
| 2899 | op = AppOpsManager.OP_NONE; |
| 2900 | } |
| 2901 | if (userId == UserHandle.USER_CURRENT) { |
| 2902 | userId = ActivityManager.getCurrentUser(); |
| 2903 | } |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2904 | nonpackageUid = -1; |
| 2905 | try { |
| 2906 | nonpackageUid = Integer.parseInt(packageName); |
| 2907 | } catch (NumberFormatException e) { |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2908 | } |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2909 | if (nonpackageUid == -1 && packageName.length() > 1 && packageName.charAt(0) == 'u' |
| 2910 | && packageName.indexOf('.') < 0) { |
| 2911 | int i = 1; |
| 2912 | while (i < packageName.length() && packageName.charAt(i) >= '0' |
| 2913 | && packageName.charAt(i) <= '9') { |
| 2914 | i++; |
| 2915 | } |
| 2916 | if (i > 1 && i < packageName.length()) { |
| 2917 | String userStr = packageName.substring(1, i); |
| 2918 | try { |
| 2919 | int user = Integer.parseInt(userStr); |
| 2920 | char type = packageName.charAt(i); |
| 2921 | i++; |
| 2922 | int startTypeVal = i; |
| 2923 | while (i < packageName.length() && packageName.charAt(i) >= '0' |
| 2924 | && packageName.charAt(i) <= '9') { |
| 2925 | i++; |
| 2926 | } |
| 2927 | if (i > startTypeVal) { |
| 2928 | String typeValStr = packageName.substring(startTypeVal, i); |
| 2929 | try { |
| 2930 | int typeVal = Integer.parseInt(typeValStr); |
| 2931 | if (type == 'a') { |
| 2932 | nonpackageUid = UserHandle.getUid(user, |
| 2933 | typeVal + Process.FIRST_APPLICATION_UID); |
| 2934 | } else if (type == 's') { |
| 2935 | nonpackageUid = UserHandle.getUid(user, typeVal); |
| 2936 | } |
| 2937 | } catch (NumberFormatException e) { |
| 2938 | } |
| 2939 | } |
| 2940 | } catch (NumberFormatException e) { |
| 2941 | } |
| 2942 | } |
| 2943 | } |
| 2944 | if (nonpackageUid != -1) { |
| 2945 | packageName = null; |
| 2946 | } else { |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 2947 | packageUid = resolveUid(packageName); |
| 2948 | if (packageUid < 0) { |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2949 | packageUid = AppGlobals.getPackageManager().getPackageUid(packageName, |
| 2950 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
| 2951 | } |
| 2952 | if (packageUid < 0) { |
| 2953 | err.println("Error: No UID for " + packageName + " in user " + userId); |
| 2954 | return -1; |
| 2955 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2956 | } |
| 2957 | return 0; |
| 2958 | } |
| 2959 | } |
| 2960 | |
| 2961 | @Override public void onShellCommand(FileDescriptor in, FileDescriptor out, |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 2962 | FileDescriptor err, String[] args, ShellCallback callback, |
| 2963 | ResultReceiver resultReceiver) { |
| 2964 | (new Shell(this, this)).exec(this, in, out, err, args, callback, resultReceiver); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2965 | } |
| 2966 | |
| 2967 | static void dumpCommandHelp(PrintWriter pw) { |
| 2968 | pw.println("AppOps service (appops) commands:"); |
| 2969 | pw.println(" help"); |
| 2970 | pw.println(" Print this help text."); |
Julia Reynolds | 6cb5fcc | 2018-02-27 17:33:52 -0500 | [diff] [blame] | 2971 | pw.println(" start [--user <USER_ID>] <PACKAGE | UID> <OP> "); |
| 2972 | pw.println(" Starts a given operation for a particular application."); |
| 2973 | pw.println(" stop [--user <USER_ID>] <PACKAGE | UID> <OP> "); |
| 2974 | pw.println(" Stops a given operation for a particular application."); |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2975 | pw.println(" set [--user <USER_ID>] <PACKAGE | UID> <OP> <MODE>"); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2976 | pw.println(" Set the mode for a particular application and operation."); |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 2977 | pw.println(" get [--user <USER_ID>] <PACKAGE | UID> [<OP>]"); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2978 | pw.println(" Return the mode for a particular application and optional operation."); |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 2979 | pw.println(" query-op [--user <USER_ID>] <OP> [<MODE>]"); |
| 2980 | 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] | 2981 | pw.println(" reset [--user <USER_ID>] [<PACKAGE>]"); |
| 2982 | pw.println(" Reset the given application or all applications to default modes."); |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 2983 | pw.println(" write-settings"); |
| 2984 | pw.println(" Immediately write pending changes to storage."); |
| 2985 | pw.println(" read-settings"); |
| 2986 | pw.println(" Read the last written settings, replacing current state in RAM."); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 2987 | pw.println(" options:"); |
| 2988 | pw.println(" <PACKAGE> an Android package name."); |
| 2989 | pw.println(" <OP> an AppOps operation."); |
| 2990 | pw.println(" <MODE> one of allow, ignore, deny, or default"); |
| 2991 | pw.println(" <USER_ID> the user id under which the package is installed. If --user is not"); |
| 2992 | pw.println(" specified, the current user is assumed."); |
| 2993 | } |
| 2994 | |
| 2995 | static int onShellCommand(Shell shell, String cmd) { |
| 2996 | if (cmd == null) { |
| 2997 | return shell.handleDefaultCommands(cmd); |
| 2998 | } |
| 2999 | PrintWriter pw = shell.getOutPrintWriter(); |
| 3000 | PrintWriter err = shell.getErrPrintWriter(); |
| 3001 | try { |
| 3002 | switch (cmd) { |
| 3003 | case "set": { |
| 3004 | int res = shell.parseUserPackageOp(true, err); |
| 3005 | if (res < 0) { |
| 3006 | return res; |
| 3007 | } |
| 3008 | String modeStr = shell.getNextArg(); |
| 3009 | if (modeStr == null) { |
| 3010 | err.println("Error: Mode not specified."); |
| 3011 | return -1; |
| 3012 | } |
| 3013 | |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 3014 | final int mode = shell.strModeToMode(modeStr, err); |
| 3015 | if (mode < 0) { |
| 3016 | return -1; |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 3017 | } |
| 3018 | |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 3019 | if (shell.packageName != null) { |
| 3020 | shell.mInterface.setMode(shell.op, shell.packageUid, shell.packageName, |
| 3021 | mode); |
| 3022 | } else { |
| 3023 | shell.mInterface.setUidMode(shell.op, shell.nonpackageUid, mode); |
| 3024 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 3025 | return 0; |
| 3026 | } |
| 3027 | case "get": { |
| 3028 | int res = shell.parseUserPackageOp(false, err); |
| 3029 | if (res < 0) { |
| 3030 | return res; |
| 3031 | } |
| 3032 | |
Dianne Hackborn | c7214a3 | 2017-04-11 13:32:47 -0700 | [diff] [blame] | 3033 | List<AppOpsManager.PackageOps> ops; |
| 3034 | if (shell.packageName != null) { |
| 3035 | ops = shell.mInterface.getOpsForPackage( |
| 3036 | shell.packageUid, shell.packageName, |
| 3037 | shell.op != AppOpsManager.OP_NONE ? new int[]{shell.op} : null); |
| 3038 | } else { |
| 3039 | ops = shell.mInterface.getUidOps( |
| 3040 | shell.nonpackageUid, |
| 3041 | shell.op != AppOpsManager.OP_NONE ? new int[]{shell.op} : null); |
| 3042 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 3043 | if (ops == null || ops.size() <= 0) { |
| 3044 | pw.println("No operations."); |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 3045 | if (shell.op > AppOpsManager.OP_NONE && shell.op < AppOpsManager._NUM_OP) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3046 | pw.println("Default mode: " + AppOpsManager.modeToName( |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 3047 | AppOpsManager.opToDefaultMode(shell.op))); |
| 3048 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 3049 | return 0; |
| 3050 | } |
| 3051 | final long now = System.currentTimeMillis(); |
| 3052 | for (int i=0; i<ops.size(); i++) { |
| 3053 | List<AppOpsManager.OpEntry> entries = ops.get(i).getOps(); |
| 3054 | for (int j=0; j<entries.size(); j++) { |
| 3055 | AppOpsManager.OpEntry ent = entries.get(j); |
| 3056 | pw.print(AppOpsManager.opToName(ent.getOp())); |
| 3057 | pw.print(": "); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3058 | pw.print(AppOpsManager.modeToName(ent.getMode())); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 3059 | if (ent.getTime() != 0) { |
| 3060 | pw.print("; time="); |
| 3061 | TimeUtils.formatDuration(now - ent.getTime(), pw); |
| 3062 | pw.print(" ago"); |
| 3063 | } |
| 3064 | if (ent.getRejectTime() != 0) { |
| 3065 | pw.print("; rejectTime="); |
| 3066 | TimeUtils.formatDuration(now - ent.getRejectTime(), pw); |
| 3067 | pw.print(" ago"); |
| 3068 | } |
| 3069 | if (ent.getDuration() == -1) { |
| 3070 | pw.print(" (running)"); |
| 3071 | } else if (ent.getDuration() != 0) { |
| 3072 | pw.print("; duration="); |
| 3073 | TimeUtils.formatDuration(ent.getDuration(), pw); |
| 3074 | } |
| 3075 | pw.println(); |
| 3076 | } |
| 3077 | } |
| 3078 | return 0; |
| 3079 | } |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 3080 | case "query-op": { |
| 3081 | int res = shell.parseUserOpMode(AppOpsManager.MODE_IGNORED, err); |
| 3082 | if (res < 0) { |
| 3083 | return res; |
| 3084 | } |
| 3085 | List<AppOpsManager.PackageOps> ops = shell.mInterface.getPackagesForOps( |
| 3086 | new int[] {shell.op}); |
| 3087 | if (ops == null || ops.size() <= 0) { |
| 3088 | pw.println("No operations."); |
| 3089 | return 0; |
| 3090 | } |
| 3091 | for (int i=0; i<ops.size(); i++) { |
| 3092 | final AppOpsManager.PackageOps pkg = ops.get(i); |
| 3093 | boolean hasMatch = false; |
| 3094 | final List<AppOpsManager.OpEntry> entries = ops.get(i).getOps(); |
| 3095 | for (int j=0; j<entries.size(); j++) { |
| 3096 | AppOpsManager.OpEntry ent = entries.get(j); |
| 3097 | if (ent.getOp() == shell.op && ent.getMode() == shell.mode) { |
| 3098 | hasMatch = true; |
| 3099 | break; |
| 3100 | } |
| 3101 | } |
| 3102 | if (hasMatch) { |
| 3103 | pw.println(pkg.getPackageName()); |
| 3104 | } |
| 3105 | } |
| 3106 | return 0; |
| 3107 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 3108 | case "reset": { |
| 3109 | String packageName = null; |
| 3110 | int userId = UserHandle.USER_CURRENT; |
| 3111 | for (String argument; (argument = shell.getNextArg()) != null;) { |
| 3112 | if ("--user".equals(argument)) { |
| 3113 | String userStr = shell.getNextArgRequired(); |
| 3114 | userId = UserHandle.parseUserArg(userStr); |
| 3115 | } else { |
| 3116 | if (packageName == null) { |
| 3117 | packageName = argument; |
| 3118 | } else { |
| 3119 | err.println("Error: Unsupported argument: " + argument); |
| 3120 | return -1; |
| 3121 | } |
| 3122 | } |
| 3123 | } |
| 3124 | |
| 3125 | if (userId == UserHandle.USER_CURRENT) { |
| 3126 | userId = ActivityManager.getCurrentUser(); |
| 3127 | } |
| 3128 | |
| 3129 | shell.mInterface.resetAllModes(userId, packageName); |
| 3130 | pw.print("Reset all modes for: "); |
| 3131 | if (userId == UserHandle.USER_ALL) { |
| 3132 | pw.print("all users"); |
| 3133 | } else { |
| 3134 | pw.print("user "); pw.print(userId); |
| 3135 | } |
| 3136 | pw.print(", "); |
| 3137 | if (packageName == null) { |
| 3138 | pw.println("all packages"); |
| 3139 | } else { |
| 3140 | pw.print("package "); pw.println(packageName); |
| 3141 | } |
| 3142 | return 0; |
| 3143 | } |
| 3144 | case "write-settings": { |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 3145 | shell.mInternal.enforceManageAppOpsModes(Binder.getCallingPid(), |
| 3146 | Binder.getCallingUid(), -1); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 3147 | long token = Binder.clearCallingIdentity(); |
| 3148 | try { |
| 3149 | synchronized (shell.mInternal) { |
| 3150 | shell.mInternal.mHandler.removeCallbacks(shell.mInternal.mWriteRunner); |
| 3151 | } |
| 3152 | shell.mInternal.writeState(); |
| 3153 | pw.println("Current settings written."); |
| 3154 | } finally { |
| 3155 | Binder.restoreCallingIdentity(token); |
| 3156 | } |
| 3157 | return 0; |
| 3158 | } |
| 3159 | case "read-settings": { |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 3160 | shell.mInternal.enforceManageAppOpsModes(Binder.getCallingPid(), |
| 3161 | Binder.getCallingUid(), -1); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 3162 | long token = Binder.clearCallingIdentity(); |
| 3163 | try { |
| 3164 | shell.mInternal.readState(); |
| 3165 | pw.println("Last settings read."); |
| 3166 | } finally { |
| 3167 | Binder.restoreCallingIdentity(token); |
| 3168 | } |
| 3169 | return 0; |
| 3170 | } |
Julia Reynolds | 6cb5fcc | 2018-02-27 17:33:52 -0500 | [diff] [blame] | 3171 | case "start": { |
| 3172 | int res = shell.parseUserPackageOp(true, err); |
| 3173 | if (res < 0) { |
| 3174 | return res; |
| 3175 | } |
| 3176 | |
| 3177 | if (shell.packageName != null) { |
| 3178 | shell.mInterface.startOperation(shell.mToken, |
| 3179 | shell.op, shell.packageUid, shell.packageName, true); |
| 3180 | } else { |
| 3181 | return -1; |
| 3182 | } |
| 3183 | return 0; |
| 3184 | } |
| 3185 | case "stop": { |
| 3186 | int res = shell.parseUserPackageOp(true, err); |
| 3187 | if (res < 0) { |
| 3188 | return res; |
| 3189 | } |
| 3190 | |
| 3191 | if (shell.packageName != null) { |
| 3192 | shell.mInterface.finishOperation(shell.mToken, |
| 3193 | shell.op, shell.packageUid, shell.packageName); |
| 3194 | } else { |
| 3195 | return -1; |
| 3196 | } |
| 3197 | return 0; |
| 3198 | } |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 3199 | default: |
| 3200 | return shell.handleDefaultCommands(cmd); |
| 3201 | } |
| 3202 | } catch (RemoteException e) { |
| 3203 | pw.println("Remote exception: " + e); |
| 3204 | } |
| 3205 | return -1; |
| 3206 | } |
| 3207 | |
| 3208 | private void dumpHelp(PrintWriter pw) { |
| 3209 | pw.println("AppOps service (appops) dump options:"); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3210 | pw.println(" -h"); |
| 3211 | pw.println(" Print this help text."); |
| 3212 | pw.println(" --op [OP]"); |
| 3213 | 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] | 3214 | pw.println(" --mode [MODE]"); |
| 3215 | pw.println(" Limit output to data associated with the given app op mode."); |
| 3216 | pw.println(" --package [PACKAGE]"); |
| 3217 | pw.println(" Limit output to data associated with the given package name."); |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 3218 | } |
| 3219 | |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 3220 | private void dumpTimesLocked(PrintWriter pw, String firstPrefix, String prefix, long[] times, |
| 3221 | long now, SimpleDateFormat sdf, Date date) { |
| 3222 | boolean hasTime = false; |
| 3223 | for (int i = 0; i < _NUM_UID_STATE; i++) { |
| 3224 | if (times[i] != 0) { |
| 3225 | hasTime = true; |
| 3226 | break; |
| 3227 | } |
| 3228 | } |
| 3229 | if (!hasTime) { |
| 3230 | return; |
| 3231 | } |
| 3232 | boolean first = true; |
| 3233 | for (int i = 0; i < _NUM_UID_STATE; i++) { |
| 3234 | if (times[i] != 0) { |
| 3235 | pw.print(first ? firstPrefix : prefix); |
| 3236 | first = false; |
| 3237 | pw.print(UID_STATE_NAMES[i]); |
| 3238 | pw.print(" = "); |
| 3239 | date.setTime(times[i]); |
| 3240 | pw.print(sdf.format(date)); |
| 3241 | pw.print(" ("); |
| 3242 | TimeUtils.formatDuration(times[i]-now, pw); |
| 3243 | pw.println(")"); |
| 3244 | } |
| 3245 | } |
| 3246 | } |
| 3247 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3248 | @Override |
| 3249 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 6df866a | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 3250 | if (!DumpUtils.checkDumpAndUsageStatsPermission(mContext, TAG, pw)) return; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3251 | |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3252 | int dumpOp = -1; |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3253 | String dumpPackage = null; |
| 3254 | int dumpUid = -1; |
| 3255 | int dumpMode = -1; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3256 | |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 3257 | if (args != null) { |
| 3258 | for (int i=0; i<args.length; i++) { |
| 3259 | String arg = args[i]; |
| 3260 | if ("-h".equals(arg)) { |
| 3261 | dumpHelp(pw); |
| 3262 | return; |
Tim Kilbourn | 8f1ea83 | 2015-08-26 15:07:37 -0700 | [diff] [blame] | 3263 | } else if ("-a".equals(arg)) { |
| 3264 | // dump all data |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3265 | } else if ("--op".equals(arg)) { |
| 3266 | i++; |
| 3267 | if (i >= args.length) { |
| 3268 | pw.println("No argument for --op option"); |
| 3269 | return; |
| 3270 | } |
| 3271 | dumpOp = Shell.strOpToOp(args[i], pw); |
| 3272 | if (dumpOp < 0) { |
| 3273 | return; |
| 3274 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3275 | } else if ("--package".equals(arg)) { |
| 3276 | i++; |
| 3277 | if (i >= args.length) { |
| 3278 | pw.println("No argument for --package option"); |
| 3279 | return; |
| 3280 | } |
| 3281 | dumpPackage = args[i]; |
| 3282 | try { |
| 3283 | dumpUid = AppGlobals.getPackageManager().getPackageUid(dumpPackage, |
| 3284 | PackageManager.MATCH_KNOWN_PACKAGES | PackageManager.MATCH_INSTANT, |
| 3285 | 0); |
| 3286 | } catch (RemoteException e) { |
| 3287 | } |
| 3288 | if (dumpUid < 0) { |
| 3289 | pw.println("Unknown package: " + dumpPackage); |
| 3290 | return; |
| 3291 | } |
| 3292 | dumpUid = UserHandle.getAppId(dumpUid); |
| 3293 | } else if ("--mode".equals(arg)) { |
| 3294 | i++; |
| 3295 | if (i >= args.length) { |
| 3296 | pw.println("No argument for --mode option"); |
| 3297 | return; |
| 3298 | } |
| 3299 | dumpMode = Shell.strModeToMode(args[i], pw); |
| 3300 | if (dumpMode < 0) { |
| 3301 | return; |
| 3302 | } |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 3303 | } else if (arg.length() > 0 && arg.charAt(0) == '-'){ |
| 3304 | pw.println("Unknown option: " + arg); |
| 3305 | return; |
| 3306 | } else { |
| 3307 | pw.println("Unknown command: " + arg); |
| 3308 | return; |
| 3309 | } |
| 3310 | } |
| 3311 | } |
| 3312 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3313 | synchronized (this) { |
| 3314 | pw.println("Current AppOps Service state:"); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3315 | mConstants.dump(pw); |
| 3316 | pw.println(); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 3317 | final long now = System.currentTimeMillis(); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 3318 | final long nowElapsed = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3319 | final long nowUptime = SystemClock.uptimeMillis(); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 3320 | final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); |
| 3321 | final Date date = new Date(); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 3322 | boolean needSep = false; |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 3323 | if (dumpOp < 0 && dumpMode < 0 && dumpPackage == null && mProfileOwners != null) { |
| 3324 | pw.println(" Profile owners:"); |
| 3325 | for (int poi = 0; poi < mProfileOwners.size(); poi++) { |
| 3326 | pw.print(" User #"); |
| 3327 | pw.print(mProfileOwners.keyAt(poi)); |
| 3328 | pw.print(": "); |
| 3329 | UserHandle.formatUid(pw, mProfileOwners.valueAt(poi)); |
| 3330 | pw.println(); |
| 3331 | } |
| 3332 | pw.println(); |
| 3333 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 3334 | if (mOpModeWatchers.size() > 0) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3335 | boolean printedHeader = false; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 3336 | for (int i=0; i<mOpModeWatchers.size(); i++) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3337 | if (dumpOp >= 0 && dumpOp != mOpModeWatchers.keyAt(i)) { |
| 3338 | continue; |
| 3339 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3340 | boolean printedOpHeader = false; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3341 | ArraySet<ModeCallback> callbacks = mOpModeWatchers.valueAt(i); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 3342 | for (int j=0; j<callbacks.size(); j++) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3343 | final ModeCallback cb = callbacks.valueAt(j); |
| 3344 | if (dumpPackage != null && cb.mWatchingUid >= 0 |
| 3345 | && dumpUid != UserHandle.getAppId(cb.mWatchingUid)) { |
| 3346 | continue; |
| 3347 | } |
| 3348 | needSep = true; |
| 3349 | if (!printedHeader) { |
| 3350 | pw.println(" Op mode watchers:"); |
| 3351 | printedHeader = true; |
| 3352 | } |
| 3353 | if (!printedOpHeader) { |
| 3354 | pw.print(" Op "); |
| 3355 | pw.print(AppOpsManager.opToName(mOpModeWatchers.keyAt(i))); |
| 3356 | pw.println(":"); |
| 3357 | printedOpHeader = true; |
| 3358 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 3359 | pw.print(" #"); pw.print(j); pw.print(": "); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3360 | pw.println(cb); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 3361 | } |
| 3362 | } |
| 3363 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3364 | if (mPackageModeWatchers.size() > 0 && dumpOp < 0) { |
| 3365 | boolean printedHeader = false; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 3366 | for (int i=0; i<mPackageModeWatchers.size(); i++) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3367 | if (dumpPackage != null && !dumpPackage.equals(mPackageModeWatchers.keyAt(i))) { |
| 3368 | continue; |
| 3369 | } |
| 3370 | needSep = true; |
| 3371 | if (!printedHeader) { |
| 3372 | pw.println(" Package mode watchers:"); |
| 3373 | printedHeader = true; |
| 3374 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 3375 | pw.print(" Pkg "); pw.print(mPackageModeWatchers.keyAt(i)); |
| 3376 | pw.println(":"); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3377 | ArraySet<ModeCallback> callbacks = mPackageModeWatchers.valueAt(i); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 3378 | for (int j=0; j<callbacks.size(); j++) { |
| 3379 | pw.print(" #"); pw.print(j); pw.print(": "); |
Dianne Hackborn | 68d7655 | 2017-02-27 15:32:03 -0800 | [diff] [blame] | 3380 | pw.println(callbacks.valueAt(j)); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 3381 | } |
| 3382 | } |
| 3383 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3384 | if (mModeWatchers.size() > 0 && dumpOp < 0) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3385 | boolean printedHeader = false; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 3386 | for (int i=0; i<mModeWatchers.size(); i++) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3387 | final ModeCallback cb = mModeWatchers.valueAt(i); |
| 3388 | if (dumpPackage != null && cb.mWatchingUid >= 0 |
| 3389 | && dumpUid != UserHandle.getAppId(cb.mWatchingUid)) { |
| 3390 | continue; |
| 3391 | } |
| 3392 | needSep = true; |
| 3393 | if (!printedHeader) { |
| 3394 | pw.println(" All op mode watchers:"); |
| 3395 | printedHeader = true; |
| 3396 | } |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 3397 | pw.print(" "); |
| 3398 | pw.print(Integer.toHexString(System.identityHashCode(mModeWatchers.keyAt(i)))); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3399 | pw.print(": "); pw.println(cb); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 3400 | } |
| 3401 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3402 | if (mActiveWatchers.size() > 0 && dumpMode < 0) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3403 | needSep = true; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3404 | boolean printedHeader = false; |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3405 | for (int i = 0; i < mActiveWatchers.size(); i++) { |
| 3406 | final SparseArray<ActiveCallback> activeWatchers = mActiveWatchers.valueAt(i); |
| 3407 | if (activeWatchers.size() <= 0) { |
| 3408 | continue; |
| 3409 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3410 | final ActiveCallback cb = activeWatchers.valueAt(0); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3411 | if (dumpOp >= 0 && activeWatchers.indexOfKey(dumpOp) < 0) { |
| 3412 | continue; |
| 3413 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3414 | if (dumpPackage != null && cb.mWatchingUid >= 0 |
| 3415 | && dumpUid != UserHandle.getAppId(cb.mWatchingUid)) { |
| 3416 | continue; |
| 3417 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3418 | if (!printedHeader) { |
| 3419 | pw.println(" All op active watchers:"); |
| 3420 | printedHeader = true; |
| 3421 | } |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 3422 | pw.print(" "); |
| 3423 | pw.print(Integer.toHexString(System.identityHashCode( |
| 3424 | mActiveWatchers.keyAt(i)))); |
| 3425 | pw.println(" ->"); |
| 3426 | pw.print(" ["); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3427 | final int opCount = activeWatchers.size(); |
| 3428 | for (i = 0; i < opCount; i++) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3429 | if (i > 0) { |
| 3430 | pw.print(' '); |
| 3431 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3432 | pw.print(AppOpsManager.opToName(activeWatchers.keyAt(i))); |
| 3433 | if (i < opCount - 1) { |
| 3434 | pw.print(','); |
| 3435 | } |
| 3436 | } |
Dianne Hackborn | 3b563fc | 2018-04-16 17:17:14 -0700 | [diff] [blame] | 3437 | pw.println("]"); |
| 3438 | pw.print(" "); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3439 | pw.println(cb); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3440 | } |
| 3441 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3442 | if (mClients.size() > 0 && dumpMode < 0) { |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 3443 | needSep = true; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3444 | boolean printedHeader = false; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 3445 | for (int i=0; i<mClients.size(); i++) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3446 | boolean printedClient = false; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 3447 | ClientState cs = mClients.valueAt(i); |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 3448 | if (cs.mStartedOps.size() > 0) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3449 | boolean printedStarted = false; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 3450 | for (int j=0; j<cs.mStartedOps.size(); j++) { |
| 3451 | Op op = cs.mStartedOps.get(j); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3452 | if (dumpOp >= 0 && op.op != dumpOp) { |
| 3453 | continue; |
| 3454 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3455 | if (dumpPackage != null && !dumpPackage.equals(op.packageName)) { |
| 3456 | continue; |
| 3457 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3458 | if (!printedHeader) { |
| 3459 | pw.println(" Clients:"); |
| 3460 | printedHeader = true; |
| 3461 | } |
| 3462 | if (!printedClient) { |
| 3463 | pw.print(" "); pw.print(mClients.keyAt(i)); pw.println(":"); |
| 3464 | pw.print(" "); pw.println(cs); |
| 3465 | printedClient = true; |
| 3466 | } |
| 3467 | if (!printedStarted) { |
| 3468 | pw.println(" Started ops:"); |
| 3469 | printedStarted = true; |
| 3470 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 3471 | pw.print(" "); pw.print("uid="); pw.print(op.uid); |
| 3472 | pw.print(" pkg="); pw.print(op.packageName); |
| 3473 | pw.print(" op="); pw.println(AppOpsManager.opToName(op.op)); |
| 3474 | } |
| 3475 | } |
| 3476 | } |
| 3477 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3478 | if (mAudioRestrictions.size() > 0 && dumpOp < 0 && dumpPackage != null |
| 3479 | && dumpMode < 0) { |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 3480 | boolean printedHeader = false; |
| 3481 | for (int o=0; o<mAudioRestrictions.size(); o++) { |
| 3482 | final String op = AppOpsManager.opToName(mAudioRestrictions.keyAt(o)); |
| 3483 | final SparseArray<Restriction> restrictions = mAudioRestrictions.valueAt(o); |
| 3484 | for (int i=0; i<restrictions.size(); i++) { |
| 3485 | if (!printedHeader){ |
| 3486 | pw.println(" Audio Restrictions:"); |
| 3487 | printedHeader = true; |
| 3488 | needSep = true; |
| 3489 | } |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 3490 | final int usage = restrictions.keyAt(i); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 3491 | pw.print(" "); pw.print(op); |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 3492 | pw.print(" usage="); pw.print(AudioAttributes.usageToString(usage)); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 3493 | Restriction r = restrictions.valueAt(i); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3494 | pw.print(": mode="); pw.println(AppOpsManager.modeToName(r.mode)); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 3495 | if (!r.exceptionPackages.isEmpty()) { |
| 3496 | pw.println(" Exceptions:"); |
| 3497 | for (int j=0; j<r.exceptionPackages.size(); j++) { |
| 3498 | pw.print(" "); pw.println(r.exceptionPackages.valueAt(j)); |
| 3499 | } |
| 3500 | } |
| 3501 | } |
| 3502 | } |
| 3503 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 3504 | if (needSep) { |
| 3505 | pw.println(); |
| 3506 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3507 | for (int i=0; i<mUidStates.size(); i++) { |
| 3508 | UidState uidState = mUidStates.valueAt(i); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3509 | final SparseIntArray opModes = uidState.opModes; |
| 3510 | final ArrayMap<String, Ops> pkgOps = uidState.pkgOps; |
| 3511 | |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3512 | if (dumpOp >= 0 || dumpPackage != null || dumpMode >= 0) { |
| 3513 | boolean hasOp = dumpOp < 0 || (uidState.opModes != null |
| 3514 | && uidState.opModes.indexOfKey(dumpOp) >= 0); |
| 3515 | boolean hasPackage = dumpPackage == null; |
| 3516 | boolean hasMode = dumpMode < 0; |
| 3517 | if (!hasMode && opModes != null) { |
| 3518 | for (int opi = 0; !hasMode && opi < opModes.size(); opi++) { |
| 3519 | if (opModes.valueAt(opi) == dumpMode) { |
| 3520 | hasMode = true; |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3521 | } |
| 3522 | } |
| 3523 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3524 | if (pkgOps != null) { |
| 3525 | for (int pkgi = 0; |
| 3526 | (!hasOp || !hasPackage || !hasMode) && pkgi < pkgOps.size(); |
| 3527 | pkgi++) { |
| 3528 | Ops ops = pkgOps.valueAt(pkgi); |
| 3529 | if (!hasOp && ops != null && ops.indexOfKey(dumpOp) >= 0) { |
| 3530 | hasOp = true; |
| 3531 | } |
| 3532 | if (!hasMode) { |
| 3533 | for (int opi = 0; !hasMode && opi < ops.size(); opi++) { |
| 3534 | if (ops.valueAt(opi).mode == dumpMode) { |
| 3535 | hasMode = true; |
| 3536 | } |
| 3537 | } |
| 3538 | } |
| 3539 | if (!hasPackage && dumpPackage.equals(ops.packageName)) { |
| 3540 | hasPackage = true; |
| 3541 | } |
| 3542 | } |
| 3543 | } |
| 3544 | if (uidState.foregroundOps != null && !hasOp) { |
| 3545 | if (uidState.foregroundOps.indexOfKey(dumpOp) > 0) { |
| 3546 | hasOp = true; |
| 3547 | } |
| 3548 | } |
| 3549 | if (!hasOp || !hasPackage || !hasMode) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3550 | continue; |
| 3551 | } |
| 3552 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3553 | |
| 3554 | pw.print(" Uid "); UserHandle.formatUid(pw, uidState.uid); pw.println(":"); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 3555 | pw.print(" state="); |
| 3556 | pw.println(UID_STATE_NAMES[uidState.state]); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3557 | if (uidState.state != uidState.pendingState) { |
| 3558 | pw.print(" pendingState="); |
| 3559 | pw.println(UID_STATE_NAMES[uidState.pendingState]); |
| 3560 | } |
| 3561 | if (uidState.pendingStateCommitTime != 0) { |
| 3562 | pw.print(" pendingStateCommitTime="); |
Dianne Hackborn | 9fb9350 | 2018-06-18 12:29:44 -0700 | [diff] [blame] | 3563 | TimeUtils.formatDuration(uidState.pendingStateCommitTime, nowElapsed, pw); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3564 | pw.println(); |
| 3565 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 3566 | if (uidState.startNesting != 0) { |
| 3567 | pw.print(" startNesting="); |
| 3568 | pw.println(uidState.startNesting); |
| 3569 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3570 | if (uidState.foregroundOps != null && (dumpMode < 0 |
| 3571 | || dumpMode == AppOpsManager.MODE_FOREGROUND)) { |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3572 | pw.println(" foregroundOps:"); |
| 3573 | for (int j = 0; j < uidState.foregroundOps.size(); j++) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3574 | if (dumpOp >= 0 && dumpOp != uidState.foregroundOps.keyAt(j)) { |
| 3575 | continue; |
| 3576 | } |
| 3577 | pw.print(" "); |
| 3578 | pw.print(AppOpsManager.opToName(uidState.foregroundOps.keyAt(j))); |
| 3579 | pw.print(": "); |
| 3580 | pw.println(uidState.foregroundOps.valueAt(j) ? "WATCHER" : "SILENT"); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3581 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3582 | pw.print(" hasForegroundWatchers="); |
| 3583 | pw.println(uidState.hasForegroundWatchers); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3584 | } |
Svet Ganov | ee438d4 | 2017-01-19 18:04:38 -0800 | [diff] [blame] | 3585 | needSep = true; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3586 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3587 | if (opModes != null) { |
| 3588 | final int opModeCount = opModes.size(); |
| 3589 | for (int j = 0; j < opModeCount; j++) { |
| 3590 | final int code = opModes.keyAt(j); |
| 3591 | final int mode = opModes.valueAt(j); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3592 | if (dumpOp >= 0 && dumpOp != code) { |
| 3593 | continue; |
| 3594 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3595 | if (dumpMode >= 0 && dumpMode != mode) { |
| 3596 | continue; |
| 3597 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3598 | pw.print(" "); pw.print(AppOpsManager.opToName(code)); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3599 | pw.print(": mode="); pw.println(AppOpsManager.modeToName(mode)); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3600 | } |
| 3601 | } |
| 3602 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3603 | if (pkgOps == null) { |
| 3604 | continue; |
| 3605 | } |
| 3606 | |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3607 | for (int pkgi = 0; pkgi < pkgOps.size(); pkgi++) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3608 | final Ops ops = pkgOps.valueAt(pkgi); |
| 3609 | if (dumpPackage != null && !dumpPackage.equals(ops.packageName)) { |
| 3610 | continue; |
| 3611 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3612 | boolean printedPackage = false; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3613 | for (int j=0; j<ops.size(); j++) { |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3614 | final Op op = ops.valueAt(j); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3615 | if (dumpOp >= 0 && dumpOp != op.op) { |
| 3616 | continue; |
| 3617 | } |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3618 | if (dumpMode >= 0 && dumpMode != op.mode) { |
| 3619 | continue; |
| 3620 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3621 | if (!printedPackage) { |
| 3622 | pw.print(" Package "); pw.print(ops.packageName); pw.println(":"); |
| 3623 | printedPackage = true; |
| 3624 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 3625 | pw.print(" "); pw.print(AppOpsManager.opToName(op.op)); |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3626 | pw.print(" ("); pw.print(AppOpsManager.modeToName(op.mode)); |
Dianne Hackborn | 65a4f25 | 2018-05-08 17:30:48 -0700 | [diff] [blame] | 3627 | final int switchOp = AppOpsManager.opToSwitch(op.op); |
| 3628 | if (switchOp != op.op) { |
| 3629 | pw.print(" / switch "); |
| 3630 | pw.print(AppOpsManager.opToName(switchOp)); |
| 3631 | final Op switchObj = ops.get(switchOp); |
| 3632 | int mode = switchObj != null |
| 3633 | ? switchObj.mode : AppOpsManager.opToDefaultMode(switchOp); |
| 3634 | pw.print("="); pw.print(AppOpsManager.modeToName(mode)); |
| 3635 | } |
Dianne Hackborn | 2378a4a | 2018-04-26 13:46:22 -0700 | [diff] [blame] | 3636 | pw.println("): "); |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 3637 | dumpTimesLocked(pw, |
| 3638 | " Access: ", |
| 3639 | " ", op.time, now, sdf, date); |
| 3640 | dumpTimesLocked(pw, |
| 3641 | " Reject: ", |
| 3642 | " ", op.rejectTime, now, sdf, date); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3643 | if (op.duration == -1) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 3644 | pw.print(" Running start at: "); |
| 3645 | TimeUtils.formatDuration(nowElapsed-op.startRealtime, pw); |
| 3646 | pw.println(); |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 3647 | } else if (op.duration != 0) { |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 3648 | pw.print(" duration="); |
| 3649 | TimeUtils.formatDuration(op.duration, pw); |
| 3650 | pw.println(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3651 | } |
Dianne Hackborn | cd1f30b | 2018-04-23 17:38:09 -0700 | [diff] [blame] | 3652 | if (op.startNesting != 0) { |
| 3653 | pw.print(" startNesting="); |
| 3654 | pw.println(op.startNesting); |
| 3655 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3656 | } |
| 3657 | } |
| 3658 | } |
Svet Ganov | ee438d4 | 2017-01-19 18:04:38 -0800 | [diff] [blame] | 3659 | if (needSep) { |
| 3660 | pw.println(); |
| 3661 | } |
| 3662 | |
| 3663 | final int userRestrictionCount = mOpUserRestrictions.size(); |
| 3664 | for (int i = 0; i < userRestrictionCount; i++) { |
| 3665 | IBinder token = mOpUserRestrictions.keyAt(i); |
| 3666 | ClientRestrictionState restrictionState = mOpUserRestrictions.valueAt(i); |
| 3667 | pw.println(" User restrictions for token " + token + ":"); |
| 3668 | |
| 3669 | final int restrictionCount = restrictionState.perUserRestrictions != null |
| 3670 | ? restrictionState.perUserRestrictions.size() : 0; |
| 3671 | if (restrictionCount > 0) { |
| 3672 | pw.println(" Restricted ops:"); |
| 3673 | for (int j = 0; j < restrictionCount; j++) { |
| 3674 | int userId = restrictionState.perUserRestrictions.keyAt(j); |
| 3675 | boolean[] restrictedOps = restrictionState.perUserRestrictions.valueAt(j); |
| 3676 | if (restrictedOps == null) { |
| 3677 | continue; |
| 3678 | } |
| 3679 | StringBuilder restrictedOpsValue = new StringBuilder(); |
| 3680 | restrictedOpsValue.append("["); |
| 3681 | final int restrictedOpCount = restrictedOps.length; |
| 3682 | for (int k = 0; k < restrictedOpCount; k++) { |
| 3683 | if (restrictedOps[k]) { |
| 3684 | if (restrictedOpsValue.length() > 1) { |
| 3685 | restrictedOpsValue.append(", "); |
| 3686 | } |
| 3687 | restrictedOpsValue.append(AppOpsManager.opToName(k)); |
| 3688 | } |
| 3689 | } |
| 3690 | restrictedOpsValue.append("]"); |
| 3691 | pw.print(" "); pw.print("user: "); pw.print(userId); |
| 3692 | pw.print(" restricted ops: "); pw.println(restrictedOpsValue); |
| 3693 | } |
| 3694 | } |
| 3695 | |
| 3696 | final int excludedPackageCount = restrictionState.perUserExcludedPackages != null |
| 3697 | ? restrictionState.perUserExcludedPackages.size() : 0; |
| 3698 | if (excludedPackageCount > 0) { |
| 3699 | pw.println(" Excluded packages:"); |
| 3700 | for (int j = 0; j < excludedPackageCount; j++) { |
| 3701 | int userId = restrictionState.perUserExcludedPackages.keyAt(j); |
| 3702 | String[] packageNames = restrictionState.perUserExcludedPackages.valueAt(j); |
| 3703 | pw.print(" "); pw.print("user: "); pw.print(userId); |
| 3704 | pw.print(" packages: "); pw.println(Arrays.toString(packageNames)); |
| 3705 | } |
| 3706 | } |
| 3707 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 3708 | } |
| 3709 | } |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 3710 | |
| 3711 | private static final class Restriction { |
| 3712 | private static final ArraySet<String> NO_EXCEPTIONS = new ArraySet<String>(); |
| 3713 | int mode; |
| 3714 | ArraySet<String> exceptionPackages = NO_EXCEPTIONS; |
| 3715 | } |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 3716 | |
| 3717 | @Override |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3718 | public void setUserRestrictions(Bundle restrictions, IBinder token, int userHandle) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 3719 | checkSystemUid("setUserRestrictions"); |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 3720 | Preconditions.checkNotNull(restrictions); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3721 | Preconditions.checkNotNull(token); |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 3722 | for (int i = 0; i < AppOpsManager._NUM_OP; i++) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 3723 | String restriction = AppOpsManager.opToRestriction(i); |
Suprabh Shukla | 64e0dcb | 2016-05-24 16:23:11 -0700 | [diff] [blame] | 3724 | if (restriction != null) { |
| 3725 | setUserRestrictionNoCheck(i, restrictions.getBoolean(restriction, false), token, |
| 3726 | userHandle, null); |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 3727 | } |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3728 | } |
| 3729 | } |
| 3730 | |
| 3731 | @Override |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 3732 | public void setUserRestriction(int code, boolean restricted, IBinder token, int userHandle, |
| 3733 | String[] exceptionPackages) { |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3734 | if (Binder.getCallingPid() != Process.myPid()) { |
| 3735 | mContext.enforcePermission(Manifest.permission.MANAGE_APP_OPS_RESTRICTIONS, |
| 3736 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 3737 | } |
| 3738 | if (userHandle != UserHandle.getCallingUserId()) { |
| 3739 | if (mContext.checkCallingOrSelfPermission(Manifest.permission |
| 3740 | .INTERACT_ACROSS_USERS_FULL) != PackageManager.PERMISSION_GRANTED |
| 3741 | && mContext.checkCallingOrSelfPermission(Manifest.permission |
| 3742 | .INTERACT_ACROSS_USERS) != PackageManager.PERMISSION_GRANTED) { |
| 3743 | throw new SecurityException("Need INTERACT_ACROSS_USERS_FULL or" |
| 3744 | + " INTERACT_ACROSS_USERS to interact cross user "); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 3745 | } |
| 3746 | } |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3747 | verifyIncomingOp(code); |
| 3748 | Preconditions.checkNotNull(token); |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 3749 | setUserRestrictionNoCheck(code, restricted, token, userHandle, exceptionPackages); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3750 | } |
| 3751 | |
| 3752 | private void setUserRestrictionNoCheck(int code, boolean restricted, IBinder token, |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 3753 | int userHandle, String[] exceptionPackages) { |
Svet Ganov | 442ed57 | 2016-08-17 17:29:43 -0700 | [diff] [blame] | 3754 | synchronized (AppOpsService.this) { |
| 3755 | ClientRestrictionState restrictionState = mOpUserRestrictions.get(token); |
| 3756 | |
| 3757 | if (restrictionState == null) { |
| 3758 | try { |
| 3759 | restrictionState = new ClientRestrictionState(token); |
| 3760 | } catch (RemoteException e) { |
| 3761 | return; |
| 3762 | } |
| 3763 | mOpUserRestrictions.put(token, restrictionState); |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 3764 | } |
Svet Ganov | 442ed57 | 2016-08-17 17:29:43 -0700 | [diff] [blame] | 3765 | |
| 3766 | if (restrictionState.setRestriction(code, restricted, exceptionPackages, userHandle)) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3767 | mHandler.sendMessage(PooledLambda.obtainMessage( |
Svet Ganov | 3a95f83 | 2018-03-23 17:44:30 -0700 | [diff] [blame] | 3768 | AppOpsService::notifyWatchersOfChange, this, code, UID_ANY)); |
Svet Ganov | 442ed57 | 2016-08-17 17:29:43 -0700 | [diff] [blame] | 3769 | } |
| 3770 | |
| 3771 | if (restrictionState.isDefault()) { |
| 3772 | mOpUserRestrictions.remove(token); |
| 3773 | restrictionState.destroy(); |
| 3774 | } |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 3775 | } |
Julia Reynolds | bb21c25 | 2016-04-05 16:01:49 -0400 | [diff] [blame] | 3776 | } |
| 3777 | |
Svet Ganov | 3a95f83 | 2018-03-23 17:44:30 -0700 | [diff] [blame] | 3778 | private void notifyWatchersOfChange(int code, int uid) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3779 | final ArraySet<ModeCallback> clonedCallbacks; |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3780 | synchronized (this) { |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3781 | ArraySet<ModeCallback> callbacks = mOpModeWatchers.get(code); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3782 | if (callbacks == null) { |
| 3783 | return; |
| 3784 | } |
Dianne Hackborn | 68d7655 | 2017-02-27 15:32:03 -0800 | [diff] [blame] | 3785 | clonedCallbacks = new ArraySet<>(callbacks); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3786 | } |
| 3787 | |
Svet Ganov | 3a95f83 | 2018-03-23 17:44:30 -0700 | [diff] [blame] | 3788 | notifyOpChanged(clonedCallbacks, code, uid, null); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | @Override |
| 3792 | public void removeUser(int userHandle) throws RemoteException { |
| 3793 | checkSystemUid("removeUser"); |
Svet Ganov | 442ed57 | 2016-08-17 17:29:43 -0700 | [diff] [blame] | 3794 | synchronized (AppOpsService.this) { |
| 3795 | final int tokenCount = mOpUserRestrictions.size(); |
| 3796 | for (int i = tokenCount - 1; i >= 0; i--) { |
| 3797 | ClientRestrictionState opRestrictions = mOpUserRestrictions.valueAt(i); |
| 3798 | opRestrictions.removeUser(userHandle); |
| 3799 | } |
Sudheer Shanka | bc2fadd | 2016-09-27 17:36:39 -0700 | [diff] [blame] | 3800 | removeUidsForUserLocked(userHandle); |
| 3801 | } |
| 3802 | } |
| 3803 | |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3804 | @Override |
| 3805 | public boolean isOperationActive(int code, int uid, String packageName) { |
Svet Ganov | f7b4725 | 2018-02-26 11:11:27 -0800 | [diff] [blame] | 3806 | if (Binder.getCallingUid() != uid) { |
| 3807 | if (mContext.checkCallingOrSelfPermission(Manifest.permission.WATCH_APPOPS) |
| 3808 | != PackageManager.PERMISSION_GRANTED) { |
| 3809 | return false; |
| 3810 | } |
| 3811 | } |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3812 | verifyIncomingOp(code); |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3813 | final String resolvedPackageName = resolvePackageName(uid, packageName); |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3814 | if (resolvedPackageName == null) { |
| 3815 | return false; |
| 3816 | } |
Svetoslav Ganov | 2d20fb4 | 2018-02-08 15:52:10 -0800 | [diff] [blame] | 3817 | synchronized (AppOpsService.this) { |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3818 | for (int i = mClients.size() - 1; i >= 0; i--) { |
| 3819 | final ClientState client = mClients.valueAt(i); |
Jeff Sharkey | 35e46d2 | 2017-06-09 10:01:20 -0600 | [diff] [blame] | 3820 | for (int j = client.mStartedOps.size() - 1; j >= 0; j--) { |
| 3821 | final Op op = client.mStartedOps.get(j); |
| 3822 | if (op.op == code && op.uid == uid) return true; |
| 3823 | } |
| 3824 | } |
| 3825 | } |
| 3826 | return false; |
| 3827 | } |
| 3828 | |
Sudheer Shanka | bc2fadd | 2016-09-27 17:36:39 -0700 | [diff] [blame] | 3829 | private void removeUidsForUserLocked(int userHandle) { |
| 3830 | for (int i = mUidStates.size() - 1; i >= 0; --i) { |
| 3831 | final int uid = mUidStates.keyAt(i); |
| 3832 | if (UserHandle.getUserId(uid) == userHandle) { |
| 3833 | mUidStates.removeAt(i); |
| 3834 | } |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 3835 | } |
| 3836 | } |
| 3837 | |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 3838 | private void checkSystemUid(String function) { |
| 3839 | int uid = Binder.getCallingUid(); |
| 3840 | if (uid != Process.SYSTEM_UID) { |
| 3841 | throw new SecurityException(function + " must by called by the system"); |
| 3842 | } |
| 3843 | } |
| 3844 | |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 3845 | private static String resolvePackageName(int uid, String packageName) { |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 3846 | if (uid == Process.ROOT_UID) { |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 3847 | return "root"; |
| 3848 | } else if (uid == Process.SHELL_UID) { |
| 3849 | return "com.android.shell"; |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 3850 | } else if (uid == Process.MEDIA_UID) { |
| 3851 | return "media"; |
| 3852 | } else if (uid == Process.AUDIOSERVER_UID) { |
| 3853 | return "audioserver"; |
| 3854 | } else if (uid == Process.CAMERASERVER_UID) { |
| 3855 | return "cameraserver"; |
Svetoslav Ganov | f73adb6 | 2016-03-29 01:07:06 +0000 | [diff] [blame] | 3856 | } else if (uid == Process.SYSTEM_UID && packageName == null) { |
| 3857 | return "android"; |
| 3858 | } |
| 3859 | return packageName; |
| 3860 | } |
| 3861 | |
Svet Ganov | 82f09bc | 2018-01-12 22:08:40 -0800 | [diff] [blame] | 3862 | private static int resolveUid(String packageName) { |
| 3863 | if (packageName == null) { |
| 3864 | return -1; |
| 3865 | } |
| 3866 | switch (packageName) { |
| 3867 | case "root": |
| 3868 | return Process.ROOT_UID; |
| 3869 | case "shell": |
| 3870 | return Process.SHELL_UID; |
| 3871 | case "media": |
| 3872 | return Process.MEDIA_UID; |
| 3873 | case "audioserver": |
| 3874 | return Process.AUDIOSERVER_UID; |
| 3875 | case "cameraserver": |
| 3876 | return Process.CAMERASERVER_UID; |
| 3877 | } |
| 3878 | return -1; |
| 3879 | } |
| 3880 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3881 | private static String[] getPackagesForUid(int uid) { |
Svet Ganov | f3807aa | 2015-08-02 10:09:56 -0700 | [diff] [blame] | 3882 | String[] packageNames = null; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3883 | try { |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 3884 | packageNames = AppGlobals.getPackageManager().getPackagesForUid(uid); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3885 | } catch (RemoteException e) { |
| 3886 | /* ignore - local call */ |
| 3887 | } |
Svet Ganov | f3807aa | 2015-08-02 10:09:56 -0700 | [diff] [blame] | 3888 | if (packageNames == null) { |
| 3889 | return EmptyArray.STRING; |
| 3890 | } |
| 3891 | return packageNames; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 3892 | } |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 3893 | |
| 3894 | private final class ClientRestrictionState implements DeathRecipient { |
| 3895 | private final IBinder token; |
| 3896 | SparseArray<boolean[]> perUserRestrictions; |
| 3897 | SparseArray<String[]> perUserExcludedPackages; |
| 3898 | |
| 3899 | public ClientRestrictionState(IBinder token) |
| 3900 | throws RemoteException { |
| 3901 | token.linkToDeath(this, 0); |
| 3902 | this.token = token; |
| 3903 | } |
| 3904 | |
| 3905 | public boolean setRestriction(int code, boolean restricted, |
| 3906 | String[] excludedPackages, int userId) { |
| 3907 | boolean changed = false; |
| 3908 | |
| 3909 | if (perUserRestrictions == null && restricted) { |
| 3910 | perUserRestrictions = new SparseArray<>(); |
| 3911 | } |
| 3912 | |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 3913 | int[] users; |
| 3914 | if (userId == UserHandle.USER_ALL) { |
| 3915 | List<UserInfo> liveUsers = UserManager.get(mContext).getUsers(false); |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 3916 | |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 3917 | users = new int[liveUsers.size()]; |
| 3918 | for (int i = 0; i < liveUsers.size(); i++) { |
| 3919 | users[i] = liveUsers.get(i).id; |
| 3920 | } |
| 3921 | } else { |
| 3922 | users = new int[]{userId}; |
| 3923 | } |
| 3924 | |
| 3925 | if (perUserRestrictions != null) { |
| 3926 | int numUsers = users.length; |
| 3927 | |
| 3928 | for (int i = 0; i < numUsers; i++) { |
| 3929 | int thisUserId = users[i]; |
| 3930 | |
| 3931 | boolean[] userRestrictions = perUserRestrictions.get(thisUserId); |
| 3932 | if (userRestrictions == null && restricted) { |
| 3933 | userRestrictions = new boolean[AppOpsManager._NUM_OP]; |
| 3934 | perUserRestrictions.put(thisUserId, userRestrictions); |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 3935 | } |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 3936 | if (userRestrictions != null && userRestrictions[code] != restricted) { |
| 3937 | userRestrictions[code] = restricted; |
| 3938 | if (!restricted && isDefault(userRestrictions)) { |
| 3939 | perUserRestrictions.remove(thisUserId); |
| 3940 | userRestrictions = null; |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 3941 | } |
| 3942 | changed = true; |
| 3943 | } |
Philip P. Moltmann | e683f19 | 2017-06-23 14:05:04 -0700 | [diff] [blame] | 3944 | |
| 3945 | if (userRestrictions != null) { |
| 3946 | final boolean noExcludedPackages = ArrayUtils.isEmpty(excludedPackages); |
| 3947 | if (perUserExcludedPackages == null && !noExcludedPackages) { |
| 3948 | perUserExcludedPackages = new SparseArray<>(); |
| 3949 | } |
| 3950 | if (perUserExcludedPackages != null && !Arrays.equals(excludedPackages, |
| 3951 | perUserExcludedPackages.get(thisUserId))) { |
| 3952 | if (noExcludedPackages) { |
| 3953 | perUserExcludedPackages.remove(thisUserId); |
| 3954 | if (perUserExcludedPackages.size() <= 0) { |
| 3955 | perUserExcludedPackages = null; |
| 3956 | } |
| 3957 | } else { |
| 3958 | perUserExcludedPackages.put(thisUserId, excludedPackages); |
| 3959 | } |
| 3960 | changed = true; |
| 3961 | } |
| 3962 | } |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 3963 | } |
| 3964 | } |
| 3965 | |
| 3966 | return changed; |
| 3967 | } |
| 3968 | |
| 3969 | public boolean hasRestriction(int restriction, String packageName, int userId) { |
| 3970 | if (perUserRestrictions == null) { |
| 3971 | return false; |
| 3972 | } |
| 3973 | boolean[] restrictions = perUserRestrictions.get(userId); |
| 3974 | if (restrictions == null) { |
| 3975 | return false; |
| 3976 | } |
| 3977 | if (!restrictions[restriction]) { |
| 3978 | return false; |
| 3979 | } |
| 3980 | if (perUserExcludedPackages == null) { |
| 3981 | return true; |
| 3982 | } |
| 3983 | String[] perUserExclusions = perUserExcludedPackages.get(userId); |
| 3984 | if (perUserExclusions == null) { |
| 3985 | return true; |
| 3986 | } |
| 3987 | return !ArrayUtils.contains(perUserExclusions, packageName); |
| 3988 | } |
| 3989 | |
| 3990 | public void removeUser(int userId) { |
| 3991 | if (perUserExcludedPackages != null) { |
| 3992 | perUserExcludedPackages.remove(userId); |
| 3993 | if (perUserExcludedPackages.size() <= 0) { |
| 3994 | perUserExcludedPackages = null; |
| 3995 | } |
| 3996 | } |
Sudheer Shanka | bc2fadd | 2016-09-27 17:36:39 -0700 | [diff] [blame] | 3997 | if (perUserRestrictions != null) { |
| 3998 | perUserRestrictions.remove(userId); |
| 3999 | if (perUserRestrictions.size() <= 0) { |
| 4000 | perUserRestrictions = null; |
| 4001 | } |
| 4002 | } |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 4003 | } |
| 4004 | |
| 4005 | public boolean isDefault() { |
| 4006 | return perUserRestrictions == null || perUserRestrictions.size() <= 0; |
| 4007 | } |
| 4008 | |
| 4009 | @Override |
| 4010 | public void binderDied() { |
| 4011 | synchronized (AppOpsService.this) { |
| 4012 | mOpUserRestrictions.remove(token); |
| 4013 | if (perUserRestrictions == null) { |
| 4014 | return; |
| 4015 | } |
| 4016 | final int userCount = perUserRestrictions.size(); |
| 4017 | for (int i = 0; i < userCount; i++) { |
| 4018 | final boolean[] restrictions = perUserRestrictions.valueAt(i); |
| 4019 | final int restrictionCount = restrictions.length; |
| 4020 | for (int j = 0; j < restrictionCount; j++) { |
| 4021 | if (restrictions[j]) { |
| 4022 | final int changedCode = j; |
Svet Ganov | 3a95f83 | 2018-03-23 17:44:30 -0700 | [diff] [blame] | 4023 | mHandler.post(() -> notifyWatchersOfChange(changedCode, UID_ANY)); |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 4024 | } |
| 4025 | } |
| 4026 | } |
| 4027 | destroy(); |
| 4028 | } |
| 4029 | } |
| 4030 | |
| 4031 | public void destroy() { |
| 4032 | token.unlinkToDeath(this, 0); |
| 4033 | } |
| 4034 | |
| 4035 | private boolean isDefault(boolean[] array) { |
| 4036 | if (ArrayUtils.isEmpty(array)) { |
| 4037 | return true; |
| 4038 | } |
| 4039 | for (boolean value : array) { |
| 4040 | if (value) { |
| 4041 | return false; |
| 4042 | } |
| 4043 | } |
| 4044 | return true; |
| 4045 | } |
| 4046 | } |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 4047 | |
| 4048 | private final class AppOpsManagerInternalImpl extends AppOpsManagerInternal { |
| 4049 | @Override public void setDeviceAndProfileOwners(SparseIntArray owners) { |
| 4050 | synchronized (AppOpsService.this) { |
| 4051 | mProfileOwners = owners; |
| 4052 | } |
| 4053 | } |
Philip P. Moltmann | 17f65af | 2018-10-18 15:32:29 -0700 | [diff] [blame] | 4054 | |
| 4055 | @Override |
| 4056 | public void setMode(int code, int uid, @NonNull String packageName, int mode, |
| 4057 | boolean isPrivileged) { |
| 4058 | AppOpsService.this.setMode(code, uid, packageName, mode, false, isPrivileged); |
| 4059 | } |
Dianne Hackborn | d525441 | 2018-05-11 18:02:58 -0700 | [diff] [blame] | 4060 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 4061 | } |