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