blob: 238bf927c43eb903f1a67847ad98c1c888057444 [file] [log] [blame]
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import java.io.File;
20import java.io.FileDescriptor;
Dianne Hackborn35654b62013-01-14 17:38:02 -080021import java.io.FileInputStream;
22import java.io.FileNotFoundException;
23import java.io.FileOutputStream;
24import java.io.IOException;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080025import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010026import java.nio.charset.StandardCharsets;
Dianne Hackborn35654b62013-01-14 17:38:02 -080027import java.util.ArrayList;
Svetoslav Ganova8bbd762016-05-13 17:08:16 -070028import java.util.Arrays;
Svetoslav215b44a2015-08-04 19:03:40 -070029import java.util.Collections;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080030import java.util.HashMap;
Dianne Hackbornc2293022013-02-06 23:14:49 -080031import java.util.Iterator;
Dianne Hackborn35654b62013-01-14 17:38:02 -080032import java.util.List;
Dianne Hackborn607b4142013-08-02 18:10:10 -070033import java.util.Map;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080034
Svet Ganov9cea80cd2016-02-16 11:47:00 -080035import android.Manifest;
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -080036import android.app.ActivityManager;
Jason Monk1c7c3192014-06-26 12:52:18 -040037import android.app.ActivityThread;
Svet Ganov2af57082015-07-30 08:44:20 -070038import android.app.AppGlobals;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080039import android.app.AppOpsManager;
40import android.content.Context;
Jason Monk1c7c3192014-06-26 12:52:18 -040041import android.content.pm.ApplicationInfo;
42import android.content.pm.IPackageManager;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080043import android.content.pm.PackageManager;
John Spurlock7b414672014-07-18 13:02:39 -040044import android.media.AudioAttributes;
Dianne Hackborn35654b62013-01-14 17:38:02 -080045import android.os.AsyncTask;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080046import android.os.Binder;
Jason Monk62062992014-05-06 09:55:28 -040047import android.os.Bundle;
Dianne Hackborn35654b62013-01-14 17:38:02 -080048import android.os.Handler;
Dianne Hackbornc2293022013-02-06 23:14:49 -080049import android.os.IBinder;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080050import android.os.Process;
Dianne Hackbornc2293022013-02-06 23:14:49 -080051import android.os.RemoteException;
Dianne Hackborn268e4e32015-11-18 16:29:56 -080052import android.os.ResultReceiver;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080053import android.os.ServiceManager;
Dianne Hackborn268e4e32015-11-18 16:29:56 -080054import android.os.ShellCommand;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080055import android.os.UserHandle;
Svet Ganov6ee871e2015-07-10 14:29:33 -070056import android.os.storage.MountServiceInternal;
Dianne Hackborne98f5db2013-07-17 17:23:25 -070057import android.util.ArrayMap;
John Spurlock1af30c72014-03-10 08:33:35 -040058import android.util.ArraySet;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080059import android.util.AtomicFile;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -080060import android.util.Log;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080061import android.util.Slog;
62import android.util.SparseArray;
Svet Ganov2af57082015-07-30 08:44:20 -070063import android.util.SparseIntArray;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080064import android.util.TimeUtils;
Dianne Hackborn35654b62013-01-14 17:38:02 -080065import android.util.Xml;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080066
67import com.android.internal.app.IAppOpsService;
Dianne Hackbornc2293022013-02-06 23:14:49 -080068import com.android.internal.app.IAppOpsCallback;
Svet Ganov6ee871e2015-07-10 14:29:33 -070069import com.android.internal.os.Zygote;
Svet Ganov2af57082015-07-30 08:44:20 -070070import com.android.internal.util.ArrayUtils;
Dianne Hackborn35654b62013-01-14 17:38:02 -080071import com.android.internal.util.FastXmlSerializer;
Svet Ganov9cea80cd2016-02-16 11:47:00 -080072import com.android.internal.util.Preconditions;
Dianne Hackborn35654b62013-01-14 17:38:02 -080073import com.android.internal.util.XmlUtils;
74
Svet Ganov2af57082015-07-30 08:44:20 -070075import libcore.util.EmptyArray;
Dianne Hackborn35654b62013-01-14 17:38:02 -080076import org.xmlpull.v1.XmlPullParser;
77import org.xmlpull.v1.XmlPullParserException;
78import org.xmlpull.v1.XmlSerializer;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080079
80public class AppOpsService extends IAppOpsService.Stub {
81 static final String TAG = "AppOps";
Dianne Hackborn35654b62013-01-14 17:38:02 -080082 static final boolean DEBUG = false;
83
84 // Write at most every 30 minutes.
85 static final long WRITE_DELAY = DEBUG ? 1000 : 30*60*1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080086
87 Context mContext;
88 final AtomicFile mFile;
Dianne Hackborn35654b62013-01-14 17:38:02 -080089 final Handler mHandler;
90
91 boolean mWriteScheduled;
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -080092 boolean mFastWriteScheduled;
Dianne Hackborn35654b62013-01-14 17:38:02 -080093 final Runnable mWriteRunner = new Runnable() {
94 public void run() {
95 synchronized (AppOpsService.this) {
96 mWriteScheduled = false;
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -080097 mFastWriteScheduled = false;
Dianne Hackborn35654b62013-01-14 17:38:02 -080098 AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
99 @Override protected Void doInBackground(Void... params) {
100 writeState();
101 return null;
102 }
103 };
104 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
105 }
106 }
107 };
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800108
Svet Ganov9cea80cd2016-02-16 11:47:00 -0800109 private final SparseArray<UidState> mUidStates = new SparseArray<>();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800110
Ruben Brunk29931bc2016-03-11 00:24:26 -0800111 /*
112 * These are app op restrictions imposed per user from various parties.
Ruben Brunk29931bc2016-03-11 00:24:26 -0800113 */
Svetoslav Ganova8bbd762016-05-13 17:08:16 -0700114 private final ArrayMap<IBinder, ClientRestrictionState> mOpUserRestrictions = new ArrayMap<>();
Jason Monk62062992014-05-06 09:55:28 -0400115
Svet Ganov2af57082015-07-30 08:44:20 -0700116 private static final class UidState {
117 public final int uid;
118 public ArrayMap<String, Ops> pkgOps;
119 public SparseIntArray opModes;
120
121 public UidState(int uid) {
122 this.uid = uid;
123 }
124
125 public void clear() {
126 pkgOps = null;
127 opModes = null;
128 }
129
130 public boolean isDefault() {
131 return (pkgOps == null || pkgOps.isEmpty())
132 && (opModes == null || opModes.size() <= 0);
133 }
134 }
135
Dianne Hackbornc2293022013-02-06 23:14:49 -0800136 public final static class Ops extends SparseArray<Op> {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800137 public final String packageName;
Svet Ganov2af57082015-07-30 08:44:20 -0700138 public final UidState uidState;
Jason Monk1c7c3192014-06-26 12:52:18 -0400139 public final boolean isPrivileged;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800140
Svet Ganov2af57082015-07-30 08:44:20 -0700141 public Ops(String _packageName, UidState _uidState, boolean _isPrivileged) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800142 packageName = _packageName;
Svet Ganov2af57082015-07-30 08:44:20 -0700143 uidState = _uidState;
Jason Monk1c7c3192014-06-26 12:52:18 -0400144 isPrivileged = _isPrivileged;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800145 }
146 }
147
Dianne Hackbornc2293022013-02-06 23:14:49 -0800148 public final static class Op {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700149 public final int uid;
150 public final String packageName;
Svet Ganov99b60432015-06-27 13:15:22 -0700151 public int proxyUid = -1;
152 public String proxyPackageName;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800153 public final int op;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800154 public int mode;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800155 public int duration;
156 public long time;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800157 public long rejectTime;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800158 public int nesting;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800159
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700160 public Op(int _uid, String _packageName, int _op) {
161 uid = _uid;
162 packageName = _packageName;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800163 op = _op;
David Braunf5d83192013-09-16 13:43:51 -0700164 mode = AppOpsManager.opToDefaultMode(op);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800165 }
166 }
167
Dianne Hackbornc2293022013-02-06 23:14:49 -0800168 final SparseArray<ArrayList<Callback>> mOpModeWatchers
169 = new SparseArray<ArrayList<Callback>>();
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700170 final ArrayMap<String, ArrayList<Callback>> mPackageModeWatchers
171 = new ArrayMap<String, ArrayList<Callback>>();
172 final ArrayMap<IBinder, Callback> mModeWatchers
173 = new ArrayMap<IBinder, Callback>();
John Spurlock1af30c72014-03-10 08:33:35 -0400174 final SparseArray<SparseArray<Restriction>> mAudioRestrictions
175 = new SparseArray<SparseArray<Restriction>>();
Dianne Hackbornc2293022013-02-06 23:14:49 -0800176
177 public final class Callback implements DeathRecipient {
178 final IAppOpsCallback mCallback;
179
180 public Callback(IAppOpsCallback callback) {
181 mCallback = callback;
182 try {
183 mCallback.asBinder().linkToDeath(this, 0);
184 } catch (RemoteException e) {
185 }
186 }
187
188 public void unlinkToDeath() {
189 mCallback.asBinder().unlinkToDeath(this, 0);
190 }
191
192 @Override
193 public void binderDied() {
194 stopWatchingMode(mCallback);
195 }
196 }
197
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700198 final ArrayMap<IBinder, ClientState> mClients = new ArrayMap<IBinder, ClientState>();
199
200 public final class ClientState extends Binder implements DeathRecipient {
201 final IBinder mAppToken;
202 final int mPid;
203 final ArrayList<Op> mStartedOps;
204
205 public ClientState(IBinder appToken) {
206 mAppToken = appToken;
207 mPid = Binder.getCallingPid();
208 if (appToken instanceof Binder) {
209 // For local clients, there is no reason to track them.
210 mStartedOps = null;
211 } else {
212 mStartedOps = new ArrayList<Op>();
213 try {
214 mAppToken.linkToDeath(this, 0);
215 } catch (RemoteException e) {
216 }
217 }
218 }
219
220 @Override
221 public String toString() {
222 return "ClientState{" +
223 "mAppToken=" + mAppToken +
224 ", " + (mStartedOps != null ? ("pid=" + mPid) : "local") +
225 '}';
226 }
227
228 @Override
229 public void binderDied() {
230 synchronized (AppOpsService.this) {
231 for (int i=mStartedOps.size()-1; i>=0; i--) {
232 finishOperationLocked(mStartedOps.get(i));
233 }
234 mClients.remove(mAppToken);
235 }
236 }
237 }
238
Jeff Brown6f357d32014-01-15 20:40:55 -0800239 public AppOpsService(File storagePath, Handler handler) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800240 mFile = new AtomicFile(storagePath);
Jeff Brown6f357d32014-01-15 20:40:55 -0800241 mHandler = handler;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800242 readState();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800243 }
David Braunf5d83192013-09-16 13:43:51 -0700244
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800245 public void publish(Context context) {
246 mContext = context;
247 ServiceManager.addService(Context.APP_OPS_SERVICE, asBinder());
248 }
249
Dianne Hackborn514074f2013-02-11 10:52:46 -0800250 public void systemReady() {
251 synchronized (this) {
252 boolean changed = false;
Svet Ganov2af57082015-07-30 08:44:20 -0700253 for (int i = mUidStates.size() - 1; i >= 0; i--) {
254 UidState uidState = mUidStates.valueAt(i);
255
256 String[] packageNames = getPackagesForUid(uidState.uid);
257 if (ArrayUtils.isEmpty(packageNames)) {
258 uidState.clear();
259 mUidStates.removeAt(i);
260 changed = true;
261 continue;
262 }
263
264 ArrayMap<String, Ops> pkgs = uidState.pkgOps;
265 if (pkgs == null) {
266 continue;
267 }
268
Dianne Hackborn514074f2013-02-11 10:52:46 -0800269 Iterator<Ops> it = pkgs.values().iterator();
270 while (it.hasNext()) {
271 Ops ops = it.next();
Jeff Sharkeye2ed23e2015-10-29 19:00:44 -0700272 int curUid = -1;
Dianne Hackborn514074f2013-02-11 10:52:46 -0800273 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700274 curUid = AppGlobals.getPackageManager().getPackageUid(ops.packageName,
275 PackageManager.MATCH_UNINSTALLED_PACKAGES,
Svet Ganov2af57082015-07-30 08:44:20 -0700276 UserHandle.getUserId(ops.uidState.uid));
Jeff Sharkeye2ed23e2015-10-29 19:00:44 -0700277 } catch (RemoteException ignored) {
Dianne Hackborn514074f2013-02-11 10:52:46 -0800278 }
Svet Ganov2af57082015-07-30 08:44:20 -0700279 if (curUid != ops.uidState.uid) {
Dianne Hackborn514074f2013-02-11 10:52:46 -0800280 Slog.i(TAG, "Pruning old package " + ops.packageName
Svet Ganov2af57082015-07-30 08:44:20 -0700281 + "/" + ops.uidState + ": new uid=" + curUid);
Dianne Hackborn514074f2013-02-11 10:52:46 -0800282 it.remove();
283 changed = true;
284 }
285 }
Svet Ganov2af57082015-07-30 08:44:20 -0700286
287 if (uidState.isDefault()) {
288 mUidStates.removeAt(i);
Dianne Hackborn514074f2013-02-11 10:52:46 -0800289 }
290 }
291 if (changed) {
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800292 scheduleFastWriteLocked();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800293 }
294 }
Svet Ganov6ee871e2015-07-10 14:29:33 -0700295
296 MountServiceInternal mountServiceInternal = LocalServices.getService(
297 MountServiceInternal.class);
298 mountServiceInternal.addExternalStoragePolicy(
299 new MountServiceInternal.ExternalStorageMountPolicy() {
300 @Override
301 public int getMountMode(int uid, String packageName) {
302 if (Process.isIsolated(uid)) {
303 return Zygote.MOUNT_EXTERNAL_NONE;
304 }
305 if (noteOperation(AppOpsManager.OP_READ_EXTERNAL_STORAGE, uid,
306 packageName) != AppOpsManager.MODE_ALLOWED) {
307 return Zygote.MOUNT_EXTERNAL_NONE;
308 }
309 if (noteOperation(AppOpsManager.OP_WRITE_EXTERNAL_STORAGE, uid,
310 packageName) != AppOpsManager.MODE_ALLOWED) {
311 return Zygote.MOUNT_EXTERNAL_READ;
312 }
313 return Zygote.MOUNT_EXTERNAL_WRITE;
314 }
315
316 @Override
317 public boolean hasExternalStorage(int uid, String packageName) {
318 final int mountMode = getMountMode(uid, packageName);
319 return mountMode == Zygote.MOUNT_EXTERNAL_READ
320 || mountMode == Zygote.MOUNT_EXTERNAL_WRITE;
321 }
322 });
Dianne Hackborn514074f2013-02-11 10:52:46 -0800323 }
324
325 public void packageRemoved(int uid, String packageName) {
326 synchronized (this) {
Svet Ganov2af57082015-07-30 08:44:20 -0700327 UidState uidState = mUidStates.get(uid);
328 if (uidState == null) {
329 return;
330 }
331
332 boolean changed = false;
333
334 // Remove any package state if such.
335 if (uidState.pkgOps != null && uidState.pkgOps.remove(packageName) != null) {
336 changed = true;
337 }
338
339 // If we just nuked the last package state check if the UID is valid.
340 if (changed && uidState.pkgOps.isEmpty()
341 && getPackagesForUid(uid).length <= 0) {
342 mUidStates.remove(uid);
343 }
344
345 if (changed) {
346 scheduleFastWriteLocked();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800347 }
348 }
349 }
350
351 public void uidRemoved(int uid) {
352 synchronized (this) {
Svet Ganov2af57082015-07-30 08:44:20 -0700353 if (mUidStates.indexOfKey(uid) >= 0) {
354 mUidStates.remove(uid);
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800355 scheduleFastWriteLocked();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800356 }
357 }
358 }
359
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800360 public void shutdown() {
361 Slog.w(TAG, "Writing app ops before shutdown...");
Dianne Hackborn35654b62013-01-14 17:38:02 -0800362 boolean doWrite = false;
363 synchronized (this) {
364 if (mWriteScheduled) {
365 mWriteScheduled = false;
366 doWrite = true;
367 }
368 }
369 if (doWrite) {
370 writeState();
371 }
372 }
373
Dianne Hackborn72e39832013-01-18 18:36:09 -0800374 private ArrayList<AppOpsManager.OpEntry> collectOps(Ops pkgOps, int[] ops) {
375 ArrayList<AppOpsManager.OpEntry> resOps = null;
376 if (ops == null) {
377 resOps = new ArrayList<AppOpsManager.OpEntry>();
378 for (int j=0; j<pkgOps.size(); j++) {
379 Op curOp = pkgOps.valueAt(j);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800380 resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time,
Svet Ganov99b60432015-06-27 13:15:22 -0700381 curOp.rejectTime, curOp.duration, curOp.proxyUid,
382 curOp.proxyPackageName));
Dianne Hackborn72e39832013-01-18 18:36:09 -0800383 }
384 } else {
385 for (int j=0; j<ops.length; j++) {
386 Op curOp = pkgOps.get(ops[j]);
387 if (curOp != null) {
388 if (resOps == null) {
389 resOps = new ArrayList<AppOpsManager.OpEntry>();
390 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800391 resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time,
Svet Ganov99b60432015-06-27 13:15:22 -0700392 curOp.rejectTime, curOp.duration, curOp.proxyUid,
393 curOp.proxyPackageName));
Dianne Hackborn72e39832013-01-18 18:36:09 -0800394 }
395 }
396 }
397 return resOps;
398 }
399
Dianne Hackborn35654b62013-01-14 17:38:02 -0800400 @Override
401 public List<AppOpsManager.PackageOps> getPackagesForOps(int[] ops) {
402 mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS,
403 Binder.getCallingPid(), Binder.getCallingUid(), null);
404 ArrayList<AppOpsManager.PackageOps> res = null;
405 synchronized (this) {
Svet Ganov2af57082015-07-30 08:44:20 -0700406 final int uidStateCount = mUidStates.size();
407 for (int i = 0; i < uidStateCount; i++) {
408 UidState uidState = mUidStates.valueAt(i);
409 if (uidState.pkgOps == null || uidState.pkgOps.isEmpty()) {
410 continue;
411 }
412 ArrayMap<String, Ops> packages = uidState.pkgOps;
413 final int packageCount = packages.size();
414 for (int j = 0; j < packageCount; j++) {
415 Ops pkgOps = packages.valueAt(j);
Dianne Hackborn72e39832013-01-18 18:36:09 -0800416 ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800417 if (resOps != null) {
418 if (res == null) {
419 res = new ArrayList<AppOpsManager.PackageOps>();
420 }
421 AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps(
Svet Ganov2af57082015-07-30 08:44:20 -0700422 pkgOps.packageName, pkgOps.uidState.uid, resOps);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800423 res.add(resPackage);
424 }
425 }
426 }
427 }
428 return res;
429 }
430
431 @Override
Dianne Hackborn72e39832013-01-18 18:36:09 -0800432 public List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName,
433 int[] ops) {
434 mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS,
435 Binder.getCallingPid(), Binder.getCallingUid(), null);
Svetoslav Ganovf73adb62016-03-29 01:07:06 +0000436 String resolvedPackageName = resolvePackageName(uid, packageName);
437 if (resolvedPackageName == null) {
438 return Collections.emptyList();
439 }
Dianne Hackborn72e39832013-01-18 18:36:09 -0800440 synchronized (this) {
Svetoslav Ganovf73adb62016-03-29 01:07:06 +0000441 Ops pkgOps = getOpsRawLocked(uid, resolvedPackageName, false);
Dianne Hackborn72e39832013-01-18 18:36:09 -0800442 if (pkgOps == null) {
443 return null;
444 }
445 ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops);
446 if (resOps == null) {
447 return null;
448 }
449 ArrayList<AppOpsManager.PackageOps> res = new ArrayList<AppOpsManager.PackageOps>();
450 AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps(
Svet Ganov2af57082015-07-30 08:44:20 -0700451 pkgOps.packageName, pkgOps.uidState.uid, resOps);
Dianne Hackborn72e39832013-01-18 18:36:09 -0800452 res.add(resPackage);
453 return res;
454 }
455 }
456
Dianne Hackborn607b4142013-08-02 18:10:10 -0700457 private void pruneOp(Op op, int uid, String packageName) {
458 if (op.time == 0 && op.rejectTime == 0) {
Svetoslav Ganovf73adb62016-03-29 01:07:06 +0000459 Ops ops = getOpsRawLocked(uid, packageName, false);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700460 if (ops != null) {
461 ops.remove(op.op);
462 if (ops.size() <= 0) {
Svet Ganov2af57082015-07-30 08:44:20 -0700463 UidState uidState = ops.uidState;
464 ArrayMap<String, Ops> pkgOps = uidState.pkgOps;
Dianne Hackborn607b4142013-08-02 18:10:10 -0700465 if (pkgOps != null) {
466 pkgOps.remove(ops.packageName);
Svet Ganov2af57082015-07-30 08:44:20 -0700467 if (pkgOps.isEmpty()) {
468 uidState.pkgOps = null;
469 }
470 if (uidState.isDefault()) {
471 mUidStates.remove(uid);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700472 }
473 }
474 }
475 }
476 }
477 }
478
Dianne Hackborn72e39832013-01-18 18:36:09 -0800479 @Override
Svet Ganov2af57082015-07-30 08:44:20 -0700480 public void setUidMode(int code, int uid, int mode) {
481 if (Binder.getCallingPid() != Process.myPid()) {
482 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
483 Binder.getCallingPid(), Binder.getCallingUid(), null);
484 }
485 verifyIncomingOp(code);
486 code = AppOpsManager.opToSwitch(code);
487
488 synchronized (this) {
489 final int defaultMode = AppOpsManager.opToDefaultMode(code);
490
491 UidState uidState = getUidStateLocked(uid, false);
492 if (uidState == null) {
493 if (mode == defaultMode) {
494 return;
495 }
496 uidState = new UidState(uid);
497 uidState.opModes = new SparseIntArray();
498 uidState.opModes.put(code, mode);
499 mUidStates.put(uid, uidState);
500 scheduleWriteLocked();
501 } else if (uidState.opModes == null) {
502 if (mode != defaultMode) {
503 uidState.opModes = new SparseIntArray();
504 uidState.opModes.put(code, mode);
505 scheduleWriteLocked();
506 }
507 } else {
508 if (uidState.opModes.get(code) == mode) {
509 return;
510 }
511 if (mode == defaultMode) {
512 uidState.opModes.delete(code);
513 if (uidState.opModes.size() <= 0) {
514 uidState.opModes = null;
515 }
516 } else {
517 uidState.opModes.put(code, mode);
518 }
519 scheduleWriteLocked();
520 }
521 }
522
Svetoslav215b44a2015-08-04 19:03:40 -0700523 String[] uidPackageNames = getPackagesForUid(uid);
Svet Ganov2af57082015-07-30 08:44:20 -0700524 ArrayMap<Callback, ArraySet<String>> callbackSpecs = null;
525
riddle_hsu40b300f2015-11-23 13:22:03 +0800526 synchronized (this) {
527 ArrayList<Callback> callbacks = mOpModeWatchers.get(code);
Svet Ganov2af57082015-07-30 08:44:20 -0700528 if (callbacks != null) {
Svet Ganov2af57082015-07-30 08:44:20 -0700529 final int callbackCount = callbacks.size();
530 for (int i = 0; i < callbackCount; i++) {
531 Callback callback = callbacks.get(i);
riddle_hsu40b300f2015-11-23 13:22:03 +0800532 ArraySet<String> changedPackages = new ArraySet<>();
533 Collections.addAll(changedPackages, uidPackageNames);
534 callbackSpecs = new ArrayMap<>();
535 callbackSpecs.put(callback, changedPackages);
536 }
537 }
538
539 for (String uidPackageName : uidPackageNames) {
540 callbacks = mPackageModeWatchers.get(uidPackageName);
541 if (callbacks != null) {
542 if (callbackSpecs == null) {
543 callbackSpecs = new ArrayMap<>();
Svet Ganov2af57082015-07-30 08:44:20 -0700544 }
riddle_hsu40b300f2015-11-23 13:22:03 +0800545 final int callbackCount = callbacks.size();
546 for (int i = 0; i < callbackCount; i++) {
547 Callback callback = callbacks.get(i);
548 ArraySet<String> changedPackages = callbackSpecs.get(callback);
549 if (changedPackages == null) {
550 changedPackages = new ArraySet<>();
551 callbackSpecs.put(callback, changedPackages);
552 }
553 changedPackages.add(uidPackageName);
554 }
Svet Ganov2af57082015-07-30 08:44:20 -0700555 }
556 }
557 }
558
559 if (callbackSpecs == null) {
560 return;
561 }
562
563 // There are components watching for mode changes such as window manager
564 // and location manager which are in our process. The callbacks in these
565 // components may require permissions our remote caller does not have.
566 final long identity = Binder.clearCallingIdentity();
567 try {
568 for (int i = 0; i < callbackSpecs.size(); i++) {
569 Callback callback = callbackSpecs.keyAt(i);
570 ArraySet<String> reportedPackageNames = callbackSpecs.valueAt(i);
571 try {
572 if (reportedPackageNames == null) {
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700573 callback.mCallback.opChanged(code, uid, null);
Svet Ganov2af57082015-07-30 08:44:20 -0700574 } else {
575 final int reportedPackageCount = reportedPackageNames.size();
576 for (int j = 0; j < reportedPackageCount; j++) {
577 String reportedPackageName = reportedPackageNames.valueAt(j);
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700578 callback.mCallback.opChanged(code, uid, reportedPackageName);
Svet Ganov2af57082015-07-30 08:44:20 -0700579 }
580 }
581 } catch (RemoteException e) {
582 Log.w(TAG, "Error dispatching op op change", e);
583 }
584 }
585 } finally {
586 Binder.restoreCallingIdentity(identity);
587 }
588 }
589
590 @Override
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800591 public void setMode(int code, int uid, String packageName, int mode) {
Dianne Hackbornb64afe12014-07-22 16:29:04 -0700592 if (Binder.getCallingPid() != Process.myPid()) {
593 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
594 Binder.getCallingPid(), Binder.getCallingUid(), null);
Dianne Hackborn133b9df2014-07-01 13:06:10 -0700595 }
Dianne Hackborn961321f2013-02-05 17:22:41 -0800596 verifyIncomingOp(code);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800597 ArrayList<Callback> repCbs = null;
598 code = AppOpsManager.opToSwitch(code);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800599 synchronized (this) {
Svet Ganov2af57082015-07-30 08:44:20 -0700600 UidState uidState = getUidStateLocked(uid, false);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800601 Op op = getOpLocked(code, uid, packageName, true);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800602 if (op != null) {
603 if (op.mode != mode) {
604 op.mode = mode;
Dianne Hackbornc2293022013-02-06 23:14:49 -0800605 ArrayList<Callback> cbs = mOpModeWatchers.get(code);
606 if (cbs != null) {
607 if (repCbs == null) {
608 repCbs = new ArrayList<Callback>();
609 }
610 repCbs.addAll(cbs);
611 }
612 cbs = mPackageModeWatchers.get(packageName);
613 if (cbs != null) {
614 if (repCbs == null) {
615 repCbs = new ArrayList<Callback>();
616 }
617 repCbs.addAll(cbs);
618 }
David Braunf5d83192013-09-16 13:43:51 -0700619 if (mode == AppOpsManager.opToDefaultMode(op.op)) {
Dianne Hackborn514074f2013-02-11 10:52:46 -0800620 // If going into the default mode, prune this op
621 // if there is nothing else interesting in it.
Dianne Hackborn607b4142013-08-02 18:10:10 -0700622 pruneOp(op, uid, packageName);
Dianne Hackborn514074f2013-02-11 10:52:46 -0800623 }
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800624 scheduleFastWriteLocked();
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800625 }
626 }
627 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800628 if (repCbs != null) {
Svet Ganov38536112015-05-19 12:45:52 -0700629 // There are components watching for mode changes such as window manager
630 // and location manager which are in our process. The callbacks in these
631 // components may require permissions our remote caller does not have.
632 final long identity = Binder.clearCallingIdentity();
633 try {
634 for (int i = 0; i < repCbs.size(); i++) {
635 try {
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700636 repCbs.get(i).mCallback.opChanged(code, uid, packageName);
Svet Ganov38536112015-05-19 12:45:52 -0700637 } catch (RemoteException e) {
638 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800639 }
Svet Ganov38536112015-05-19 12:45:52 -0700640 } finally {
641 Binder.restoreCallingIdentity(identity);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800642 }
643 }
644 }
645
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700646 private static HashMap<Callback, ArrayList<ChangeRec>> addCallbacks(
647 HashMap<Callback, ArrayList<ChangeRec>> callbacks,
648 int op, int uid, String packageName, ArrayList<Callback> cbs) {
Dianne Hackborn607b4142013-08-02 18:10:10 -0700649 if (cbs == null) {
650 return callbacks;
651 }
652 if (callbacks == null) {
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700653 callbacks = new HashMap<>();
Dianne Hackborn607b4142013-08-02 18:10:10 -0700654 }
Svet Ganov2af57082015-07-30 08:44:20 -0700655 boolean duplicate = false;
Dianne Hackborn607b4142013-08-02 18:10:10 -0700656 for (int i=0; i<cbs.size(); i++) {
657 Callback cb = cbs.get(i);
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700658 ArrayList<ChangeRec> reports = callbacks.get(cb);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700659 if (reports == null) {
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700660 reports = new ArrayList<>();
Dianne Hackborn607b4142013-08-02 18:10:10 -0700661 callbacks.put(cb, reports);
Svet Ganov2af57082015-07-30 08:44:20 -0700662 } else {
663 final int reportCount = reports.size();
664 for (int j = 0; j < reportCount; j++) {
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700665 ChangeRec report = reports.get(j);
666 if (report.op == op && report.pkg.equals(packageName)) {
Svet Ganov2af57082015-07-30 08:44:20 -0700667 duplicate = true;
668 break;
669 }
670 }
Dianne Hackborn607b4142013-08-02 18:10:10 -0700671 }
Svet Ganov2af57082015-07-30 08:44:20 -0700672 if (!duplicate) {
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700673 reports.add(new ChangeRec(op, uid, packageName));
Svet Ganov2af57082015-07-30 08:44:20 -0700674 }
Dianne Hackborn607b4142013-08-02 18:10:10 -0700675 }
676 return callbacks;
677 }
678
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700679 static final class ChangeRec {
680 final int op;
681 final int uid;
682 final String pkg;
683
684 ChangeRec(int _op, int _uid, String _pkg) {
685 op = _op;
686 uid = _uid;
687 pkg = _pkg;
688 }
689 }
690
Dianne Hackborn607b4142013-08-02 18:10:10 -0700691 @Override
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800692 public void resetAllModes(int reqUserId, String reqPackageName) {
693 final int callingPid = Binder.getCallingPid();
694 final int callingUid = Binder.getCallingUid();
Dianne Hackborn607b4142013-08-02 18:10:10 -0700695 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800696 callingPid, callingUid, null);
697 reqUserId = ActivityManager.handleIncomingUser(callingPid, callingUid, reqUserId,
698 true, true, "resetAllModes", null);
Svet Ganov2af57082015-07-30 08:44:20 -0700699
700 int reqUid = -1;
701 if (reqPackageName != null) {
702 try {
703 reqUid = AppGlobals.getPackageManager().getPackageUid(
Jeff Sharkeycd654482016-01-08 17:42:11 -0700704 reqPackageName, PackageManager.MATCH_UNINSTALLED_PACKAGES, reqUserId);
Svet Ganov2af57082015-07-30 08:44:20 -0700705 } catch (RemoteException e) {
706 /* ignore - local call */
707 }
708 }
709
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700710 HashMap<Callback, ArrayList<ChangeRec>> callbacks = null;
Dianne Hackborn607b4142013-08-02 18:10:10 -0700711 synchronized (this) {
712 boolean changed = false;
Svet Ganov2af57082015-07-30 08:44:20 -0700713 for (int i = mUidStates.size() - 1; i >= 0; i--) {
714 UidState uidState = mUidStates.valueAt(i);
715
716 SparseIntArray opModes = uidState.opModes;
717 if (opModes != null && (uidState.uid == reqUid || reqUid == -1)) {
718 final int uidOpCount = opModes.size();
719 for (int j = uidOpCount - 1; j >= 0; j--) {
720 final int code = opModes.keyAt(j);
721 if (AppOpsManager.opAllowsReset(code)) {
722 opModes.removeAt(j);
723 if (opModes.size() <= 0) {
724 uidState.opModes = null;
725 }
726 for (String packageName : getPackagesForUid(uidState.uid)) {
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700727 callbacks = addCallbacks(callbacks, code, uidState.uid, packageName,
Svet Ganov2af57082015-07-30 08:44:20 -0700728 mOpModeWatchers.get(code));
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700729 callbacks = addCallbacks(callbacks, code, uidState.uid, packageName,
Svet Ganov2af57082015-07-30 08:44:20 -0700730 mPackageModeWatchers.get(packageName));
731 }
732 }
733 }
734 }
735
736 if (uidState.pkgOps == null) {
737 continue;
738 }
739
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800740 if (reqUserId != UserHandle.USER_ALL
Svet Ganov2af57082015-07-30 08:44:20 -0700741 && reqUserId != UserHandle.getUserId(uidState.uid)) {
Alexandra Gherghinad6a98972014-08-04 17:05:34 +0100742 // Skip any ops for a different user
743 continue;
744 }
Svet Ganov2af57082015-07-30 08:44:20 -0700745
746 Map<String, Ops> packages = uidState.pkgOps;
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700747 Iterator<Map.Entry<String, Ops>> it = packages.entrySet().iterator();
748 while (it.hasNext()) {
749 Map.Entry<String, Ops> ent = it.next();
Dianne Hackborn607b4142013-08-02 18:10:10 -0700750 String packageName = ent.getKey();
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800751 if (reqPackageName != null && !reqPackageName.equals(packageName)) {
752 // Skip any ops for a different package
753 continue;
754 }
Dianne Hackborn607b4142013-08-02 18:10:10 -0700755 Ops pkgOps = ent.getValue();
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700756 for (int j=pkgOps.size()-1; j>=0; j--) {
Dianne Hackborn607b4142013-08-02 18:10:10 -0700757 Op curOp = pkgOps.valueAt(j);
Dianne Hackborn8828d3a2013-09-25 16:47:10 -0700758 if (AppOpsManager.opAllowsReset(curOp.op)
759 && curOp.mode != AppOpsManager.opToDefaultMode(curOp.op)) {
David Braunf5d83192013-09-16 13:43:51 -0700760 curOp.mode = AppOpsManager.opToDefaultMode(curOp.op);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700761 changed = true;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700762 callbacks = addCallbacks(callbacks, curOp.op, curOp.uid, packageName,
Dianne Hackborn607b4142013-08-02 18:10:10 -0700763 mOpModeWatchers.get(curOp.op));
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700764 callbacks = addCallbacks(callbacks, curOp.op, curOp.uid, packageName,
Dianne Hackborn607b4142013-08-02 18:10:10 -0700765 mPackageModeWatchers.get(packageName));
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700766 if (curOp.time == 0 && curOp.rejectTime == 0) {
767 pkgOps.removeAt(j);
768 }
Dianne Hackborn607b4142013-08-02 18:10:10 -0700769 }
770 }
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700771 if (pkgOps.size() == 0) {
772 it.remove();
773 }
774 }
Svet Ganov2af57082015-07-30 08:44:20 -0700775 if (uidState.isDefault()) {
776 mUidStates.remove(uidState.uid);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700777 }
778 }
Svet Ganov2af57082015-07-30 08:44:20 -0700779
Dianne Hackborn607b4142013-08-02 18:10:10 -0700780 if (changed) {
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800781 scheduleFastWriteLocked();
Dianne Hackborn607b4142013-08-02 18:10:10 -0700782 }
783 }
784 if (callbacks != null) {
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700785 for (Map.Entry<Callback, ArrayList<ChangeRec>> ent : callbacks.entrySet()) {
Dianne Hackborn607b4142013-08-02 18:10:10 -0700786 Callback cb = ent.getKey();
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700787 ArrayList<ChangeRec> reports = ent.getValue();
Dianne Hackborn607b4142013-08-02 18:10:10 -0700788 for (int i=0; i<reports.size(); i++) {
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700789 ChangeRec rep = reports.get(i);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700790 try {
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700791 cb.mCallback.opChanged(rep.op, rep.uid, rep.pkg);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700792 } catch (RemoteException e) {
793 }
794 }
795 }
796 }
797 }
798
Dianne Hackbornc2293022013-02-06 23:14:49 -0800799 @Override
800 public void startWatchingMode(int op, String packageName, IAppOpsCallback callback) {
Svetoslav Ganov8de59712015-12-09 18:25:13 -0800801 if (callback == null) {
802 return;
803 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800804 synchronized (this) {
Svet Ganov2af57082015-07-30 08:44:20 -0700805 op = (op != AppOpsManager.OP_NONE) ? AppOpsManager.opToSwitch(op) : op;
Dianne Hackbornc2293022013-02-06 23:14:49 -0800806 Callback cb = mModeWatchers.get(callback.asBinder());
807 if (cb == null) {
808 cb = new Callback(callback);
809 mModeWatchers.put(callback.asBinder(), cb);
810 }
811 if (op != AppOpsManager.OP_NONE) {
812 ArrayList<Callback> cbs = mOpModeWatchers.get(op);
813 if (cbs == null) {
814 cbs = new ArrayList<Callback>();
815 mOpModeWatchers.put(op, cbs);
816 }
817 cbs.add(cb);
818 }
819 if (packageName != null) {
820 ArrayList<Callback> cbs = mPackageModeWatchers.get(packageName);
821 if (cbs == null) {
822 cbs = new ArrayList<Callback>();
823 mPackageModeWatchers.put(packageName, cbs);
824 }
825 cbs.add(cb);
826 }
827 }
828 }
829
830 @Override
831 public void stopWatchingMode(IAppOpsCallback callback) {
Svetoslav Ganov8de59712015-12-09 18:25:13 -0800832 if (callback == null) {
833 return;
834 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800835 synchronized (this) {
836 Callback cb = mModeWatchers.remove(callback.asBinder());
837 if (cb != null) {
838 cb.unlinkToDeath();
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700839 for (int i=mOpModeWatchers.size()-1; i>=0; i--) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800840 ArrayList<Callback> cbs = mOpModeWatchers.valueAt(i);
841 cbs.remove(cb);
842 if (cbs.size() <= 0) {
843 mOpModeWatchers.removeAt(i);
844 }
845 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700846 for (int i=mPackageModeWatchers.size()-1; i>=0; i--) {
847 ArrayList<Callback> cbs = mPackageModeWatchers.valueAt(i);
848 cbs.remove(cb);
849 if (cbs.size() <= 0) {
850 mPackageModeWatchers.removeAt(i);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800851 }
852 }
853 }
854 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800855 }
856
857 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700858 public IBinder getToken(IBinder clientToken) {
859 synchronized (this) {
860 ClientState cs = mClients.get(clientToken);
861 if (cs == null) {
862 cs = new ClientState(clientToken);
863 mClients.put(clientToken, cs);
864 }
865 return cs;
866 }
867 }
868
869 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -0800870 public int checkOperation(int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800871 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800872 verifyIncomingOp(code);
Svetoslav Ganovf73adb62016-03-29 01:07:06 +0000873 String resolvedPackageName = resolvePackageName(uid, packageName);
874 if (resolvedPackageName == null) {
875 return AppOpsManager.MODE_IGNORED;
876 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800877 synchronized (this) {
Svet Ganov442ed572016-08-17 17:29:43 -0700878 if (isOpRestrictedLocked(uid, code, resolvedPackageName)) {
Jason Monk62062992014-05-06 09:55:28 -0400879 return AppOpsManager.MODE_IGNORED;
880 }
Svet Ganov2af57082015-07-30 08:44:20 -0700881 code = AppOpsManager.opToSwitch(code);
882 UidState uidState = getUidStateLocked(uid, false);
883 if (uidState != null && uidState.opModes != null) {
884 final int uidMode = uidState.opModes.get(code);
885 if (uidMode != AppOpsManager.MODE_ALLOWED) {
886 return uidMode;
887 }
888 }
Svetoslav Ganovf73adb62016-03-29 01:07:06 +0000889 Op op = getOpLocked(code, uid, resolvedPackageName, false);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800890 if (op == null) {
David Braunf5d83192013-09-16 13:43:51 -0700891 return AppOpsManager.opToDefaultMode(code);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800892 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800893 return op.mode;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800894 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800895 }
896
897 @Override
John Spurlock7b414672014-07-18 13:02:39 -0400898 public int checkAudioOperation(int code, int usage, int uid, String packageName) {
Andrei Stingaceanuefc4a342016-03-22 14:43:01 +0000899 boolean suspended;
900 try {
901 suspended = isPackageSuspendedForUser(packageName, uid);
902 } catch (IllegalArgumentException ex) {
903 // Package not found.
904 suspended = false;
905 }
906
907 if (suspended) {
Andrei Stingaceanu2bc2feb2016-02-11 16:23:49 +0000908 Log.i(TAG, "Audio disabled for suspended package=" + packageName + " for uid=" + uid);
909 return AppOpsManager.MODE_IGNORED;
910 }
911
John Spurlock1af30c72014-03-10 08:33:35 -0400912 synchronized (this) {
John Spurlock7b414672014-07-18 13:02:39 -0400913 final int mode = checkRestrictionLocked(code, usage, uid, packageName);
John Spurlock1af30c72014-03-10 08:33:35 -0400914 if (mode != AppOpsManager.MODE_ALLOWED) {
915 return mode;
916 }
917 }
918 return checkOperation(code, uid, packageName);
919 }
920
Andrei Stingaceanu355b2322016-02-12 16:43:51 +0000921 private boolean isPackageSuspendedForUser(String pkg, int uid) {
Andrei Stingaceanu2bc2feb2016-02-11 16:23:49 +0000922 try {
Andrei Stingaceanu355b2322016-02-12 16:43:51 +0000923 return AppGlobals.getPackageManager().isPackageSuspendedForUser(
924 pkg, UserHandle.getUserId(uid));
Andrei Stingaceanu2bc2feb2016-02-11 16:23:49 +0000925 } catch (RemoteException re) {
926 throw new SecurityException("Could not talk to package manager service");
927 }
Andrei Stingaceanu2bc2feb2016-02-11 16:23:49 +0000928 }
929
John Spurlock7b414672014-07-18 13:02:39 -0400930 private int checkRestrictionLocked(int code, int usage, int uid, String packageName) {
931 final SparseArray<Restriction> usageRestrictions = mAudioRestrictions.get(code);
932 if (usageRestrictions != null) {
933 final Restriction r = usageRestrictions.get(usage);
John Spurlock1af30c72014-03-10 08:33:35 -0400934 if (r != null && !r.exceptionPackages.contains(packageName)) {
935 return r.mode;
936 }
937 }
938 return AppOpsManager.MODE_ALLOWED;
939 }
940
941 @Override
John Spurlock7b414672014-07-18 13:02:39 -0400942 public void setAudioRestriction(int code, int usage, int uid, int mode,
John Spurlock1af30c72014-03-10 08:33:35 -0400943 String[] exceptionPackages) {
944 verifyIncomingUid(uid);
945 verifyIncomingOp(code);
946 synchronized (this) {
John Spurlock7b414672014-07-18 13:02:39 -0400947 SparseArray<Restriction> usageRestrictions = mAudioRestrictions.get(code);
948 if (usageRestrictions == null) {
949 usageRestrictions = new SparseArray<Restriction>();
950 mAudioRestrictions.put(code, usageRestrictions);
John Spurlock1af30c72014-03-10 08:33:35 -0400951 }
John Spurlock7b414672014-07-18 13:02:39 -0400952 usageRestrictions.remove(usage);
John Spurlock1af30c72014-03-10 08:33:35 -0400953 if (mode != AppOpsManager.MODE_ALLOWED) {
954 final Restriction r = new Restriction();
955 r.mode = mode;
956 if (exceptionPackages != null) {
957 final int N = exceptionPackages.length;
958 r.exceptionPackages = new ArraySet<String>(N);
959 for (int i = 0; i < N; i++) {
960 final String pkg = exceptionPackages[i];
961 if (pkg != null) {
962 r.exceptionPackages.add(pkg.trim());
963 }
964 }
965 }
John Spurlock7b414672014-07-18 13:02:39 -0400966 usageRestrictions.put(usage, r);
John Spurlock1af30c72014-03-10 08:33:35 -0400967 }
968 }
Julia Reynoldsbb21c252016-04-05 16:01:49 -0400969 notifyWatchersOfChange(code);
John Spurlock1af30c72014-03-10 08:33:35 -0400970 }
971
972 @Override
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700973 public int checkPackage(int uid, String packageName) {
Svetoslav Ganovf73adb62016-03-29 01:07:06 +0000974 Preconditions.checkNotNull(packageName);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700975 synchronized (this) {
Dianne Hackborn0fcef842014-09-12 15:38:33 -0700976 if (getOpsRawLocked(uid, packageName, true) != null) {
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700977 return AppOpsManager.MODE_ALLOWED;
978 } else {
979 return AppOpsManager.MODE_ERRORED;
980 }
981 }
982 }
983
984 @Override
Svet Ganov99b60432015-06-27 13:15:22 -0700985 public int noteProxyOperation(int code, String proxyPackageName,
986 int proxiedUid, String proxiedPackageName) {
987 verifyIncomingOp(code);
Svetoslav Ganovf73adb62016-03-29 01:07:06 +0000988 final int proxyUid = Binder.getCallingUid();
989 String resolveProxyPackageName = resolvePackageName(proxyUid, proxyPackageName);
990 if (resolveProxyPackageName == null) {
991 return AppOpsManager.MODE_IGNORED;
992 }
993 final int proxyMode = noteOperationUnchecked(code, proxyUid,
994 resolveProxyPackageName, -1, null);
Svet Ganov99b60432015-06-27 13:15:22 -0700995 if (proxyMode != AppOpsManager.MODE_ALLOWED || Binder.getCallingUid() == proxiedUid) {
996 return proxyMode;
997 }
Svetoslav Ganovf73adb62016-03-29 01:07:06 +0000998 String resolveProxiedPackageName = resolvePackageName(proxiedUid, proxiedPackageName);
999 if (resolveProxiedPackageName == null) {
1000 return AppOpsManager.MODE_IGNORED;
1001 }
1002 return noteOperationUnchecked(code, proxiedUid, resolveProxiedPackageName,
1003 proxyMode, resolveProxyPackageName);
Svet Ganov99b60432015-06-27 13:15:22 -07001004 }
1005
1006 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001007 public int noteOperation(int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001008 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -08001009 verifyIncomingOp(code);
Svetoslav Ganovf73adb62016-03-29 01:07:06 +00001010 String resolvedPackageName = resolvePackageName(uid, packageName);
1011 if (resolvedPackageName == null) {
1012 return AppOpsManager.MODE_IGNORED;
1013 }
1014 return noteOperationUnchecked(code, uid, resolvedPackageName, 0, null);
Svet Ganov99b60432015-06-27 13:15:22 -07001015 }
1016
1017 private int noteOperationUnchecked(int code, int uid, String packageName,
1018 int proxyUid, String proxyPackageName) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001019 synchronized (this) {
Svetoslav Ganovf73adb62016-03-29 01:07:06 +00001020 Ops ops = getOpsRawLocked(uid, packageName, true);
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001021 if (ops == null) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001022 if (DEBUG) Log.d(TAG, "noteOperation: no op for code " + code + " uid " + uid
1023 + " package " + packageName);
Jeff Sharkey911d7f42013-09-05 18:11:45 -07001024 return AppOpsManager.MODE_ERRORED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001025 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001026 Op op = getOpLocked(ops, code, true);
Svet Ganov442ed572016-08-17 17:29:43 -07001027 if (isOpRestrictedLocked(uid, code, packageName)) {
Jason Monk62062992014-05-06 09:55:28 -04001028 return AppOpsManager.MODE_IGNORED;
1029 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001030 if (op.duration == -1) {
1031 Slog.w(TAG, "Noting op not finished: uid " + uid + " pkg " + packageName
1032 + " code " + code + " time=" + op.time + " duration=" + op.duration);
1033 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001034 op.duration = 0;
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001035 final int switchCode = AppOpsManager.opToSwitch(code);
Svet Ganov2af57082015-07-30 08:44:20 -07001036 UidState uidState = ops.uidState;
Svetoslav Ganov1984bba2016-04-05 13:39:25 -07001037 // If there is a non-default per UID policy (we set UID op mode only if
1038 // non-default) it takes over, otherwise use the per package policy.
1039 if (uidState.opModes != null && uidState.opModes.indexOfKey(switchCode) >= 0) {
Svet Ganov2af57082015-07-30 08:44:20 -07001040 final int uidMode = uidState.opModes.get(switchCode);
1041 if (uidMode != AppOpsManager.MODE_ALLOWED) {
1042 if (DEBUG) Log.d(TAG, "noteOperation: reject #" + op.mode + " for code "
1043 + switchCode + " (" + code + ") uid " + uid + " package "
1044 + packageName);
1045 op.rejectTime = System.currentTimeMillis();
1046 return uidMode;
1047 }
Svetoslav Ganov1984bba2016-04-05 13:39:25 -07001048 } else {
1049 final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
1050 if (switchOp.mode != AppOpsManager.MODE_ALLOWED) {
1051 if (DEBUG) Log.d(TAG, "noteOperation: reject #" + op.mode + " for code "
1052 + switchCode + " (" + code + ") uid " + uid + " package "
1053 + packageName);
1054 op.rejectTime = System.currentTimeMillis();
1055 return switchOp.mode;
1056 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001057 }
1058 if (DEBUG) Log.d(TAG, "noteOperation: allowing code " + code + " uid " + uid
1059 + " package " + packageName);
1060 op.time = System.currentTimeMillis();
Dianne Hackborn514074f2013-02-11 10:52:46 -08001061 op.rejectTime = 0;
Svet Ganov99b60432015-06-27 13:15:22 -07001062 op.proxyUid = proxyUid;
1063 op.proxyPackageName = proxyPackageName;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001064 return AppOpsManager.MODE_ALLOWED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001065 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001066 }
1067
1068 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001069 public int startOperation(IBinder token, int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001070 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -08001071 verifyIncomingOp(code);
Svetoslav Ganovf73adb62016-03-29 01:07:06 +00001072 String resolvedPackageName = resolvePackageName(uid, packageName);
1073 if (resolvedPackageName == null) {
1074 return AppOpsManager.MODE_IGNORED;
1075 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001076 ClientState client = (ClientState)token;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001077 synchronized (this) {
Svetoslav Ganovf73adb62016-03-29 01:07:06 +00001078 Ops ops = getOpsRawLocked(uid, resolvedPackageName, true);
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001079 if (ops == null) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001080 if (DEBUG) Log.d(TAG, "startOperation: no op for code " + code + " uid " + uid
Svetoslav Ganovf73adb62016-03-29 01:07:06 +00001081 + " package " + resolvedPackageName);
Jeff Sharkey911d7f42013-09-05 18:11:45 -07001082 return AppOpsManager.MODE_ERRORED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001083 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001084 Op op = getOpLocked(ops, code, true);
Svet Ganov442ed572016-08-17 17:29:43 -07001085 if (isOpRestrictedLocked(uid, code, resolvedPackageName)) {
Jason Monk62062992014-05-06 09:55:28 -04001086 return AppOpsManager.MODE_IGNORED;
1087 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001088 final int switchCode = AppOpsManager.opToSwitch(code);
Svet Ganov2af57082015-07-30 08:44:20 -07001089 UidState uidState = ops.uidState;
1090 if (uidState.opModes != null) {
1091 final int uidMode = uidState.opModes.get(switchCode);
1092 if (uidMode != AppOpsManager.MODE_ALLOWED) {
1093 if (DEBUG) Log.d(TAG, "noteOperation: reject #" + op.mode + " for code "
1094 + switchCode + " (" + code + ") uid " + uid + " package "
Svetoslav Ganovf73adb62016-03-29 01:07:06 +00001095 + resolvedPackageName);
Svet Ganov2af57082015-07-30 08:44:20 -07001096 op.rejectTime = System.currentTimeMillis();
1097 return uidMode;
1098 }
1099 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001100 final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
1101 if (switchOp.mode != AppOpsManager.MODE_ALLOWED) {
1102 if (DEBUG) Log.d(TAG, "startOperation: reject #" + op.mode + " for code "
Svetoslav Ganovf73adb62016-03-29 01:07:06 +00001103 + switchCode + " (" + code + ") uid " + uid + " package "
1104 + resolvedPackageName);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001105 op.rejectTime = System.currentTimeMillis();
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001106 return switchOp.mode;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001107 }
1108 if (DEBUG) Log.d(TAG, "startOperation: allowing code " + code + " uid " + uid
Svetoslav Ganovf73adb62016-03-29 01:07:06 +00001109 + " package " + resolvedPackageName);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001110 if (op.nesting == 0) {
1111 op.time = System.currentTimeMillis();
Dianne Hackborn514074f2013-02-11 10:52:46 -08001112 op.rejectTime = 0;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001113 op.duration = -1;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001114 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001115 op.nesting++;
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001116 if (client.mStartedOps != null) {
1117 client.mStartedOps.add(op);
1118 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001119 return AppOpsManager.MODE_ALLOWED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001120 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001121 }
1122
1123 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001124 public void finishOperation(IBinder token, int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001125 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -08001126 verifyIncomingOp(code);
Svetoslav Ganovf73adb62016-03-29 01:07:06 +00001127 String resolvedPackageName = resolvePackageName(uid, packageName);
1128 if (resolvedPackageName == null) {
1129 return;
1130 }
1131 if (!(token instanceof ClientState)) {
1132 return;
1133 }
1134 ClientState client = (ClientState) token;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001135 synchronized (this) {
Svetoslav Ganovf73adb62016-03-29 01:07:06 +00001136 Op op = getOpLocked(code, uid, resolvedPackageName, true);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001137 if (op == null) {
1138 return;
1139 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001140 if (client.mStartedOps != null) {
1141 if (!client.mStartedOps.remove(op)) {
1142 throw new IllegalStateException("Operation not started: uid" + op.uid
1143 + " pkg=" + op.packageName + " op=" + op.op);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001144 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001145 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001146 finishOperationLocked(op);
1147 }
1148 }
1149
Svet Ganovb9d71a62015-04-30 10:38:13 -07001150 @Override
1151 public int permissionToOpCode(String permission) {
Svetoslav Ganovf73adb62016-03-29 01:07:06 +00001152 if (permission == null) {
1153 return AppOpsManager.OP_NONE;
1154 }
Svet Ganovb9d71a62015-04-30 10:38:13 -07001155 return AppOpsManager.permissionToOpCode(permission);
1156 }
1157
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001158 void finishOperationLocked(Op op) {
1159 if (op.nesting <= 1) {
1160 if (op.nesting == 1) {
1161 op.duration = (int)(System.currentTimeMillis() - op.time);
1162 op.time += op.duration;
1163 } else {
1164 Slog.w(TAG, "Finishing op nesting under-run: uid " + op.uid + " pkg "
1165 + op.packageName + " code " + op.op + " time=" + op.time
1166 + " duration=" + op.duration + " nesting=" + op.nesting);
1167 }
1168 op.nesting = 0;
1169 } else {
1170 op.nesting--;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001171 }
1172 }
1173
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001174 private void verifyIncomingUid(int uid) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001175 if (uid == Binder.getCallingUid()) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001176 return;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001177 }
1178 if (Binder.getCallingPid() == Process.myPid()) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001179 return;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001180 }
1181 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
1182 Binder.getCallingPid(), Binder.getCallingUid(), null);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001183 }
1184
Dianne Hackborn961321f2013-02-05 17:22:41 -08001185 private void verifyIncomingOp(int op) {
1186 if (op >= 0 && op < AppOpsManager._NUM_OP) {
1187 return;
1188 }
1189 throw new IllegalArgumentException("Bad operation #" + op);
1190 }
1191
Svet Ganov2af57082015-07-30 08:44:20 -07001192 private UidState getUidStateLocked(int uid, boolean edit) {
1193 UidState uidState = mUidStates.get(uid);
1194 if (uidState == null) {
1195 if (!edit) {
1196 return null;
1197 }
1198 uidState = new UidState(uid);
1199 mUidStates.put(uid, uidState);
1200 }
1201 return uidState;
1202 }
1203
Dianne Hackborn0fcef842014-09-12 15:38:33 -07001204 private Ops getOpsRawLocked(int uid, String packageName, boolean edit) {
Svet Ganov2af57082015-07-30 08:44:20 -07001205 UidState uidState = getUidStateLocked(uid, edit);
1206 if (uidState == null) {
1207 return null;
1208 }
1209
1210 if (uidState.pkgOps == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001211 if (!edit) {
1212 return null;
1213 }
Svet Ganov2af57082015-07-30 08:44:20 -07001214 uidState.pkgOps = new ArrayMap<>();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001215 }
Svet Ganov2af57082015-07-30 08:44:20 -07001216
1217 Ops ops = uidState.pkgOps.get(packageName);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001218 if (ops == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001219 if (!edit) {
1220 return null;
1221 }
Jason Monk1c7c3192014-06-26 12:52:18 -04001222 boolean isPrivileged = false;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001223 // This is the first time we have seen this package name under this uid,
1224 // so let's make sure it is valid.
Dianne Hackborn514074f2013-02-11 10:52:46 -08001225 if (uid != 0) {
1226 final long ident = Binder.clearCallingIdentity();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001227 try {
Dianne Hackborn514074f2013-02-11 10:52:46 -08001228 int pkgUid = -1;
1229 try {
Jason Monk1c7c3192014-06-26 12:52:18 -04001230 ApplicationInfo appInfo = ActivityThread.getPackageManager()
Jeff Sharkeycd654482016-01-08 17:42:11 -07001231 .getApplicationInfo(packageName,
1232 PackageManager.MATCH_DEBUG_TRIAGED_MISSING,
1233 UserHandle.getUserId(uid));
Jason Monk1c7c3192014-06-26 12:52:18 -04001234 if (appInfo != null) {
1235 pkgUid = appInfo.uid;
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001236 isPrivileged = (appInfo.privateFlags
1237 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0;
Jason Monk1c7c3192014-06-26 12:52:18 -04001238 } else {
1239 if ("media".equals(packageName)) {
1240 pkgUid = Process.MEDIA_UID;
1241 isPrivileged = false;
Andy Hunged0ea402015-10-30 14:11:46 -07001242 } else if ("audioserver".equals(packageName)) {
1243 pkgUid = Process.AUDIOSERVER_UID;
1244 isPrivileged = false;
Chien-Yu Chen75cade02016-01-11 10:56:21 -08001245 } else if ("cameraserver".equals(packageName)) {
1246 pkgUid = Process.CAMERASERVER_UID;
1247 isPrivileged = false;
Jason Monk1c7c3192014-06-26 12:52:18 -04001248 }
Dianne Hackborn713df152013-05-17 11:27:57 -07001249 }
Jason Monk1c7c3192014-06-26 12:52:18 -04001250 } catch (RemoteException e) {
1251 Slog.w(TAG, "Could not contact PackageManager", e);
Dianne Hackborn514074f2013-02-11 10:52:46 -08001252 }
1253 if (pkgUid != uid) {
1254 // Oops! The package name is not valid for the uid they are calling
1255 // under. Abort.
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07001256 RuntimeException ex = new RuntimeException("here");
1257 ex.fillInStackTrace();
Dianne Hackborn514074f2013-02-11 10:52:46 -08001258 Slog.w(TAG, "Bad call: specified package " + packageName
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07001259 + " under uid " + uid + " but it is really " + pkgUid, ex);
Dianne Hackborn514074f2013-02-11 10:52:46 -08001260 return null;
1261 }
1262 } finally {
1263 Binder.restoreCallingIdentity(ident);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001264 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001265 }
Svet Ganov2af57082015-07-30 08:44:20 -07001266 ops = new Ops(packageName, uidState, isPrivileged);
1267 uidState.pkgOps.put(packageName, ops);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001268 }
Dianne Hackborn72e39832013-01-18 18:36:09 -08001269 return ops;
1270 }
1271
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001272 private void scheduleWriteLocked() {
1273 if (!mWriteScheduled) {
1274 mWriteScheduled = true;
1275 mHandler.postDelayed(mWriteRunner, WRITE_DELAY);
1276 }
1277 }
1278
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -08001279 private void scheduleFastWriteLocked() {
1280 if (!mFastWriteScheduled) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001281 mWriteScheduled = true;
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -08001282 mFastWriteScheduled = true;
1283 mHandler.removeCallbacks(mWriteRunner);
1284 mHandler.postDelayed(mWriteRunner, 10*1000);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001285 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001286 }
1287
Dianne Hackborn72e39832013-01-18 18:36:09 -08001288 private Op getOpLocked(int code, int uid, String packageName, boolean edit) {
Svetoslav Ganovf73adb62016-03-29 01:07:06 +00001289 Ops ops = getOpsRawLocked(uid, packageName, edit);
Dianne Hackborn72e39832013-01-18 18:36:09 -08001290 if (ops == null) {
1291 return null;
1292 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001293 return getOpLocked(ops, code, edit);
1294 }
1295
1296 private Op getOpLocked(Ops ops, int code, boolean edit) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001297 Op op = ops.get(code);
1298 if (op == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001299 if (!edit) {
1300 return null;
1301 }
Svet Ganov2af57082015-07-30 08:44:20 -07001302 op = new Op(ops.uidState.uid, ops.packageName, code);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001303 ops.put(code, op);
1304 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001305 if (edit) {
1306 scheduleWriteLocked();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001307 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001308 return op;
1309 }
1310
Svet Ganov442ed572016-08-17 17:29:43 -07001311 private boolean isOpRestrictedLocked(int uid, int code, String packageName) {
Jason Monk62062992014-05-06 09:55:28 -04001312 int userHandle = UserHandle.getUserId(uid);
Svet Ganov9cea80cd2016-02-16 11:47:00 -08001313 final int restrictionSetCount = mOpUserRestrictions.size();
Ruben Brunk29931bc2016-03-11 00:24:26 -08001314
Svet Ganov9cea80cd2016-02-16 11:47:00 -08001315 for (int i = 0; i < restrictionSetCount; i++) {
Ruben Brunk29931bc2016-03-11 00:24:26 -08001316 // For each client, check that the given op is not restricted, or that the given
1317 // package is exempt from the restriction.
Svetoslav Ganova8bbd762016-05-13 17:08:16 -07001318 ClientRestrictionState restrictionState = mOpUserRestrictions.valueAt(i);
Suprabh Shuklaffddadb2016-05-20 16:37:26 -07001319 if (restrictionState.hasRestriction(code, packageName, userHandle)) {
1320 if (AppOpsManager.opAllowSystemBypassRestriction(code)) {
1321 // If we are the system, bypass user restrictions for certain codes
1322 synchronized (this) {
1323 Ops ops = getOpsRawLocked(uid, packageName, true);
1324 if ((ops != null) && ops.isPrivileged) {
1325 return false;
1326 }
Ruben Brunk32f0fa42016-03-11 19:07:07 -08001327 }
Ruben Brunk29931bc2016-03-11 00:24:26 -08001328 }
Svet Ganov9cea80cd2016-02-16 11:47:00 -08001329 return true;
Jason Monk1c7c3192014-06-26 12:52:18 -04001330 }
Jason Monk62062992014-05-06 09:55:28 -04001331 }
1332 return false;
1333 }
1334
Dianne Hackborn35654b62013-01-14 17:38:02 -08001335 void readState() {
1336 synchronized (mFile) {
1337 synchronized (this) {
1338 FileInputStream stream;
1339 try {
1340 stream = mFile.openRead();
1341 } catch (FileNotFoundException e) {
1342 Slog.i(TAG, "No existing app ops " + mFile.getBaseFile() + "; starting empty");
1343 return;
1344 }
1345 boolean success = false;
Dianne Hackborn4d34bb82015-08-07 18:26:38 -07001346 mUidStates.clear();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001347 try {
1348 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001349 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackborn35654b62013-01-14 17:38:02 -08001350 int type;
1351 while ((type = parser.next()) != XmlPullParser.START_TAG
1352 && type != XmlPullParser.END_DOCUMENT) {
1353 ;
1354 }
1355
1356 if (type != XmlPullParser.START_TAG) {
1357 throw new IllegalStateException("no start tag found");
1358 }
1359
1360 int outerDepth = parser.getDepth();
1361 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1362 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1363 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1364 continue;
1365 }
1366
1367 String tagName = parser.getName();
1368 if (tagName.equals("pkg")) {
Dave Burke0997c5bd2013-08-02 20:25:02 +00001369 readPackage(parser);
Svetoslav215b44a2015-08-04 19:03:40 -07001370 } else if (tagName.equals("uid")) {
Svet Ganov2af57082015-07-30 08:44:20 -07001371 readUidOps(parser);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001372 } else {
1373 Slog.w(TAG, "Unknown element under <app-ops>: "
1374 + parser.getName());
1375 XmlUtils.skipCurrentTag(parser);
1376 }
1377 }
1378 success = true;
1379 } catch (IllegalStateException e) {
1380 Slog.w(TAG, "Failed parsing " + e);
1381 } catch (NullPointerException e) {
1382 Slog.w(TAG, "Failed parsing " + e);
1383 } catch (NumberFormatException e) {
1384 Slog.w(TAG, "Failed parsing " + e);
1385 } catch (XmlPullParserException e) {
1386 Slog.w(TAG, "Failed parsing " + e);
1387 } catch (IOException e) {
1388 Slog.w(TAG, "Failed parsing " + e);
1389 } catch (IndexOutOfBoundsException e) {
1390 Slog.w(TAG, "Failed parsing " + e);
1391 } finally {
1392 if (!success) {
Svet Ganov2af57082015-07-30 08:44:20 -07001393 mUidStates.clear();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001394 }
1395 try {
1396 stream.close();
1397 } catch (IOException e) {
1398 }
1399 }
1400 }
1401 }
1402 }
1403
Svet Ganov2af57082015-07-30 08:44:20 -07001404 void readUidOps(XmlPullParser parser) throws NumberFormatException,
1405 XmlPullParserException, IOException {
1406 final int uid = Integer.parseInt(parser.getAttributeValue(null, "n"));
1407 int outerDepth = parser.getDepth();
1408 int type;
1409 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1410 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1411 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1412 continue;
1413 }
1414
1415 String tagName = parser.getName();
1416 if (tagName.equals("op")) {
1417 final int code = Integer.parseInt(parser.getAttributeValue(null, "n"));
1418 final int mode = Integer.parseInt(parser.getAttributeValue(null, "m"));
1419 UidState uidState = getUidStateLocked(uid, true);
1420 if (uidState.opModes == null) {
1421 uidState.opModes = new SparseIntArray();
1422 }
1423 uidState.opModes.put(code, mode);
1424 } else {
1425 Slog.w(TAG, "Unknown element under <uid-ops>: "
1426 + parser.getName());
1427 XmlUtils.skipCurrentTag(parser);
1428 }
1429 }
1430 }
1431
Dave Burke0997c5bd2013-08-02 20:25:02 +00001432 void readPackage(XmlPullParser parser) throws NumberFormatException,
Dianne Hackborn35654b62013-01-14 17:38:02 -08001433 XmlPullParserException, IOException {
1434 String pkgName = parser.getAttributeValue(null, "n");
1435 int outerDepth = parser.getDepth();
1436 int type;
1437 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1438 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1439 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1440 continue;
1441 }
1442
1443 String tagName = parser.getName();
1444 if (tagName.equals("uid")) {
Dave Burke0997c5bd2013-08-02 20:25:02 +00001445 readUid(parser, pkgName);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001446 } else {
1447 Slog.w(TAG, "Unknown element under <pkg>: "
1448 + parser.getName());
1449 XmlUtils.skipCurrentTag(parser);
1450 }
1451 }
1452 }
1453
Dave Burke0997c5bd2013-08-02 20:25:02 +00001454 void readUid(XmlPullParser parser, String pkgName) throws NumberFormatException,
Dianne Hackborn35654b62013-01-14 17:38:02 -08001455 XmlPullParserException, IOException {
1456 int uid = Integer.parseInt(parser.getAttributeValue(null, "n"));
Jason Monk1c7c3192014-06-26 12:52:18 -04001457 String isPrivilegedString = parser.getAttributeValue(null, "p");
1458 boolean isPrivileged = false;
1459 if (isPrivilegedString == null) {
1460 try {
1461 IPackageManager packageManager = ActivityThread.getPackageManager();
1462 if (packageManager != null) {
1463 ApplicationInfo appInfo = ActivityThread.getPackageManager()
1464 .getApplicationInfo(pkgName, 0, UserHandle.getUserId(uid));
1465 if (appInfo != null) {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001466 isPrivileged = (appInfo.privateFlags
1467 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0;
Jason Monk1c7c3192014-06-26 12:52:18 -04001468 }
1469 } else {
1470 // Could not load data, don't add to cache so it will be loaded later.
1471 return;
1472 }
1473 } catch (RemoteException e) {
1474 Slog.w(TAG, "Could not contact PackageManager", e);
1475 }
1476 } else {
1477 isPrivileged = Boolean.parseBoolean(isPrivilegedString);
1478 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001479 int outerDepth = parser.getDepth();
1480 int type;
1481 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1482 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1483 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1484 continue;
1485 }
1486
1487 String tagName = parser.getName();
1488 if (tagName.equals("op")) {
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001489 Op op = new Op(uid, pkgName, Integer.parseInt(parser.getAttributeValue(null, "n")));
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001490 String mode = parser.getAttributeValue(null, "m");
1491 if (mode != null) {
Dave Burke0997c5bd2013-08-02 20:25:02 +00001492 op.mode = Integer.parseInt(mode);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001493 }
1494 String time = parser.getAttributeValue(null, "t");
1495 if (time != null) {
1496 op.time = Long.parseLong(time);
1497 }
1498 time = parser.getAttributeValue(null, "r");
1499 if (time != null) {
1500 op.rejectTime = Long.parseLong(time);
1501 }
1502 String dur = parser.getAttributeValue(null, "d");
1503 if (dur != null) {
1504 op.duration = Integer.parseInt(dur);
1505 }
Svet Ganov99b60432015-06-27 13:15:22 -07001506 String proxyUid = parser.getAttributeValue(null, "pu");
1507 if (proxyUid != null) {
1508 op.proxyUid = Integer.parseInt(proxyUid);
1509 }
1510 String proxyPackageName = parser.getAttributeValue(null, "pp");
1511 if (proxyPackageName != null) {
1512 op.proxyPackageName = proxyPackageName;
1513 }
Svet Ganov2af57082015-07-30 08:44:20 -07001514
1515 UidState uidState = getUidStateLocked(uid, true);
1516 if (uidState.pkgOps == null) {
1517 uidState.pkgOps = new ArrayMap<>();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001518 }
Svet Ganov2af57082015-07-30 08:44:20 -07001519
1520 Ops ops = uidState.pkgOps.get(pkgName);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001521 if (ops == null) {
Svet Ganov2af57082015-07-30 08:44:20 -07001522 ops = new Ops(pkgName, uidState, isPrivileged);
1523 uidState.pkgOps.put(pkgName, ops);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001524 }
1525 ops.put(op.op, op);
1526 } else {
1527 Slog.w(TAG, "Unknown element under <pkg>: "
1528 + parser.getName());
1529 XmlUtils.skipCurrentTag(parser);
1530 }
1531 }
1532 }
1533
1534 void writeState() {
1535 synchronized (mFile) {
1536 List<AppOpsManager.PackageOps> allOps = getPackagesForOps(null);
1537
1538 FileOutputStream stream;
1539 try {
1540 stream = mFile.startWrite();
1541 } catch (IOException e) {
1542 Slog.w(TAG, "Failed to write state: " + e);
1543 return;
1544 }
1545
1546 try {
1547 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001548 out.setOutput(stream, StandardCharsets.UTF_8.name());
Dianne Hackborn35654b62013-01-14 17:38:02 -08001549 out.startDocument(null, true);
Dianne Hackborn4d34bb82015-08-07 18:26:38 -07001550 out.startTag(null, "app-ops");
Svet Ganov2af57082015-07-30 08:44:20 -07001551
1552 final int uidStateCount = mUidStates.size();
1553 for (int i = 0; i < uidStateCount; i++) {
1554 UidState uidState = mUidStates.valueAt(i);
1555 if (uidState.opModes != null && uidState.opModes.size() > 0) {
1556 out.startTag(null, "uid");
1557 out.attribute(null, "n", Integer.toString(uidState.uid));
1558 SparseIntArray uidOpModes = uidState.opModes;
1559 final int opCount = uidOpModes.size();
1560 for (int j = 0; j < opCount; j++) {
1561 final int op = uidOpModes.keyAt(j);
1562 final int mode = uidOpModes.valueAt(j);
1563 out.startTag(null, "op");
1564 out.attribute(null, "n", Integer.toString(op));
1565 out.attribute(null, "m", Integer.toString(mode));
1566 out.endTag(null, "op");
1567 }
1568 out.endTag(null, "uid");
1569 }
1570 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001571
1572 if (allOps != null) {
1573 String lastPkg = null;
1574 for (int i=0; i<allOps.size(); i++) {
1575 AppOpsManager.PackageOps pkg = allOps.get(i);
1576 if (!pkg.getPackageName().equals(lastPkg)) {
1577 if (lastPkg != null) {
1578 out.endTag(null, "pkg");
1579 }
1580 lastPkg = pkg.getPackageName();
1581 out.startTag(null, "pkg");
1582 out.attribute(null, "n", lastPkg);
1583 }
1584 out.startTag(null, "uid");
1585 out.attribute(null, "n", Integer.toString(pkg.getUid()));
Jason Monk1c7c3192014-06-26 12:52:18 -04001586 synchronized (this) {
Svetoslav Ganovf73adb62016-03-29 01:07:06 +00001587 Ops ops = getOpsRawLocked(pkg.getUid(), pkg.getPackageName(), false);
Jason Monk1c7c3192014-06-26 12:52:18 -04001588 // Should always be present as the list of PackageOps is generated
1589 // from Ops.
1590 if (ops != null) {
1591 out.attribute(null, "p", Boolean.toString(ops.isPrivileged));
1592 } else {
1593 out.attribute(null, "p", Boolean.toString(false));
1594 }
1595 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001596 List<AppOpsManager.OpEntry> ops = pkg.getOps();
1597 for (int j=0; j<ops.size(); j++) {
1598 AppOpsManager.OpEntry op = ops.get(j);
1599 out.startTag(null, "op");
1600 out.attribute(null, "n", Integer.toString(op.getOp()));
David Braunf5d83192013-09-16 13:43:51 -07001601 if (op.getMode() != AppOpsManager.opToDefaultMode(op.getOp())) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001602 out.attribute(null, "m", Integer.toString(op.getMode()));
1603 }
1604 long time = op.getTime();
1605 if (time != 0) {
1606 out.attribute(null, "t", Long.toString(time));
1607 }
1608 time = op.getRejectTime();
1609 if (time != 0) {
1610 out.attribute(null, "r", Long.toString(time));
1611 }
1612 int dur = op.getDuration();
1613 if (dur != 0) {
1614 out.attribute(null, "d", Integer.toString(dur));
1615 }
Svet Ganov99b60432015-06-27 13:15:22 -07001616 int proxyUid = op.getProxyUid();
1617 if (proxyUid != -1) {
1618 out.attribute(null, "pu", Integer.toString(proxyUid));
1619 }
1620 String proxyPackageName = op.getProxyPackageName();
1621 if (proxyPackageName != null) {
1622 out.attribute(null, "pp", proxyPackageName);
1623 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001624 out.endTag(null, "op");
1625 }
1626 out.endTag(null, "uid");
1627 }
1628 if (lastPkg != null) {
1629 out.endTag(null, "pkg");
1630 }
1631 }
1632
1633 out.endTag(null, "app-ops");
1634 out.endDocument();
1635 mFile.finishWrite(stream);
1636 } catch (IOException e) {
1637 Slog.w(TAG, "Failed to write state, restoring backup.", e);
1638 mFile.failWrite(stream);
1639 }
1640 }
1641 }
1642
Dianne Hackborn268e4e32015-11-18 16:29:56 -08001643 static class Shell extends ShellCommand {
1644 final IAppOpsService mInterface;
1645 final AppOpsService mInternal;
1646
1647 int userId = UserHandle.USER_SYSTEM;
1648 String packageName;
1649 String opStr;
Dianne Hackborne91f3e72016-03-25 18:48:15 -07001650 String modeStr;
Dianne Hackborn268e4e32015-11-18 16:29:56 -08001651 int op;
Dianne Hackborne91f3e72016-03-25 18:48:15 -07001652 int mode;
Dianne Hackborn268e4e32015-11-18 16:29:56 -08001653 int packageUid;
1654
1655 Shell(IAppOpsService iface, AppOpsService internal) {
1656 mInterface = iface;
1657 mInternal = internal;
1658 }
1659
1660 @Override
1661 public int onCommand(String cmd) {
1662 return onShellCommand(this, cmd);
1663 }
1664
1665 @Override
1666 public void onHelp() {
1667 PrintWriter pw = getOutPrintWriter();
1668 dumpCommandHelp(pw);
1669 }
1670
1671 private int strOpToOp(String op, PrintWriter err) {
1672 try {
1673 return AppOpsManager.strOpToOp(op);
1674 } catch (IllegalArgumentException e) {
1675 }
1676 try {
1677 return Integer.parseInt(op);
1678 } catch (NumberFormatException e) {
1679 }
1680 try {
1681 return AppOpsManager.strDebugOpToOp(op);
1682 } catch (IllegalArgumentException e) {
1683 err.println("Error: " + e.getMessage());
1684 return -1;
1685 }
1686 }
1687
Dianne Hackborne91f3e72016-03-25 18:48:15 -07001688 int strModeToMode(String modeStr, PrintWriter err) {
1689 switch (modeStr) {
1690 case "allow":
1691 return AppOpsManager.MODE_ALLOWED;
1692 case "deny":
1693 return AppOpsManager.MODE_ERRORED;
1694 case "ignore":
1695 return AppOpsManager.MODE_IGNORED;
1696 case "default":
1697 return AppOpsManager.MODE_DEFAULT;
1698 }
1699 try {
1700 return Integer.parseInt(modeStr);
1701 } catch (NumberFormatException e) {
1702 }
1703 err.println("Error: Mode " + modeStr + " is not valid");
1704 return -1;
1705 }
1706
1707 int parseUserOpMode(int defMode, PrintWriter err) throws RemoteException {
1708 userId = UserHandle.USER_CURRENT;
1709 opStr = null;
1710 modeStr = null;
1711 for (String argument; (argument = getNextArg()) != null;) {
1712 if ("--user".equals(argument)) {
1713 userId = UserHandle.parseUserArg(getNextArgRequired());
1714 } else {
1715 if (opStr == null) {
1716 opStr = argument;
1717 } else if (modeStr == null) {
1718 modeStr = argument;
1719 break;
1720 }
1721 }
1722 }
1723 if (opStr == null) {
1724 err.println("Error: Operation not specified.");
1725 return -1;
1726 }
1727 op = strOpToOp(opStr, err);
1728 if (op < 0) {
1729 return -1;
1730 }
1731 if (modeStr != null) {
1732 if ((mode=strModeToMode(modeStr, err)) < 0) {
1733 return -1;
1734 }
1735 } else {
1736 mode = defMode;
1737 }
1738 return 0;
1739 }
1740
Dianne Hackborn268e4e32015-11-18 16:29:56 -08001741 int parseUserPackageOp(boolean reqOp, PrintWriter err) throws RemoteException {
1742 userId = UserHandle.USER_CURRENT;
1743 packageName = null;
1744 opStr = null;
1745 for (String argument; (argument = getNextArg()) != null;) {
1746 if ("--user".equals(argument)) {
1747 userId = UserHandle.parseUserArg(getNextArgRequired());
1748 } else {
1749 if (packageName == null) {
1750 packageName = argument;
1751 } else if (opStr == null) {
1752 opStr = argument;
1753 break;
1754 }
1755 }
1756 }
1757 if (packageName == null) {
1758 err.println("Error: Package name not specified.");
1759 return -1;
1760 } else if (opStr == null && reqOp) {
1761 err.println("Error: Operation not specified.");
1762 return -1;
1763 }
1764 if (opStr != null) {
1765 op = strOpToOp(opStr, err);
1766 if (op < 0) {
1767 return -1;
1768 }
1769 } else {
1770 op = AppOpsManager.OP_NONE;
1771 }
1772 if (userId == UserHandle.USER_CURRENT) {
1773 userId = ActivityManager.getCurrentUser();
1774 }
1775 if ("root".equals(packageName)) {
1776 packageUid = 0;
1777 } else {
Jeff Sharkeycd654482016-01-08 17:42:11 -07001778 packageUid = AppGlobals.getPackageManager().getPackageUid(packageName,
1779 PackageManager.MATCH_UNINSTALLED_PACKAGES, userId);
Dianne Hackborn268e4e32015-11-18 16:29:56 -08001780 }
1781 if (packageUid < 0) {
1782 err.println("Error: No UID for " + packageName + " in user " + userId);
1783 return -1;
1784 }
1785 return 0;
1786 }
1787 }
1788
1789 @Override public void onShellCommand(FileDescriptor in, FileDescriptor out,
1790 FileDescriptor err, String[] args, ResultReceiver resultReceiver) {
1791 (new Shell(this, this)).exec(this, in, out, err, args, resultReceiver);
1792 }
1793
1794 static void dumpCommandHelp(PrintWriter pw) {
1795 pw.println("AppOps service (appops) commands:");
1796 pw.println(" help");
1797 pw.println(" Print this help text.");
1798 pw.println(" set [--user <USER_ID>] <PACKAGE> <OP> <MODE>");
1799 pw.println(" Set the mode for a particular application and operation.");
1800 pw.println(" get [--user <USER_ID>] <PACKAGE> [<OP>]");
1801 pw.println(" Return the mode for a particular application and optional operation.");
Dianne Hackborne91f3e72016-03-25 18:48:15 -07001802 pw.println(" query-op [--user <USER_ID>] <OP> [<MODE>]");
1803 pw.println(" Print all packages that currently have the given op in the given mode.");
Dianne Hackborn268e4e32015-11-18 16:29:56 -08001804 pw.println(" reset [--user <USER_ID>] [<PACKAGE>]");
1805 pw.println(" Reset the given application or all applications to default modes.");
Dianne Hackborn4d34bb82015-08-07 18:26:38 -07001806 pw.println(" write-settings");
1807 pw.println(" Immediately write pending changes to storage.");
1808 pw.println(" read-settings");
1809 pw.println(" Read the last written settings, replacing current state in RAM.");
Dianne Hackborn268e4e32015-11-18 16:29:56 -08001810 pw.println(" options:");
1811 pw.println(" <PACKAGE> an Android package name.");
1812 pw.println(" <OP> an AppOps operation.");
1813 pw.println(" <MODE> one of allow, ignore, deny, or default");
1814 pw.println(" <USER_ID> the user id under which the package is installed. If --user is not");
1815 pw.println(" specified, the current user is assumed.");
1816 }
1817
1818 static int onShellCommand(Shell shell, String cmd) {
1819 if (cmd == null) {
1820 return shell.handleDefaultCommands(cmd);
1821 }
1822 PrintWriter pw = shell.getOutPrintWriter();
1823 PrintWriter err = shell.getErrPrintWriter();
1824 try {
1825 switch (cmd) {
1826 case "set": {
1827 int res = shell.parseUserPackageOp(true, err);
1828 if (res < 0) {
1829 return res;
1830 }
1831 String modeStr = shell.getNextArg();
1832 if (modeStr == null) {
1833 err.println("Error: Mode not specified.");
1834 return -1;
1835 }
1836
Dianne Hackborne91f3e72016-03-25 18:48:15 -07001837 final int mode = shell.strModeToMode(modeStr, err);
1838 if (mode < 0) {
1839 return -1;
Dianne Hackborn268e4e32015-11-18 16:29:56 -08001840 }
1841
1842 shell.mInterface.setMode(shell.op, shell.packageUid, shell.packageName, mode);
1843 return 0;
1844 }
1845 case "get": {
1846 int res = shell.parseUserPackageOp(false, err);
1847 if (res < 0) {
1848 return res;
1849 }
1850
1851 List<AppOpsManager.PackageOps> ops = shell.mInterface.getOpsForPackage(
1852 shell.packageUid, shell.packageName,
1853 shell.op != AppOpsManager.OP_NONE ? new int[] {shell.op} : null);
1854 if (ops == null || ops.size() <= 0) {
1855 pw.println("No operations.");
1856 return 0;
1857 }
1858 final long now = System.currentTimeMillis();
1859 for (int i=0; i<ops.size(); i++) {
1860 List<AppOpsManager.OpEntry> entries = ops.get(i).getOps();
1861 for (int j=0; j<entries.size(); j++) {
1862 AppOpsManager.OpEntry ent = entries.get(j);
1863 pw.print(AppOpsManager.opToName(ent.getOp()));
1864 pw.print(": ");
1865 switch (ent.getMode()) {
1866 case AppOpsManager.MODE_ALLOWED:
1867 pw.print("allow");
1868 break;
1869 case AppOpsManager.MODE_IGNORED:
1870 pw.print("ignore");
1871 break;
1872 case AppOpsManager.MODE_ERRORED:
1873 pw.print("deny");
1874 break;
1875 case AppOpsManager.MODE_DEFAULT:
1876 pw.print("default");
1877 break;
1878 default:
1879 pw.print("mode=");
1880 pw.print(ent.getMode());
1881 break;
1882 }
1883 if (ent.getTime() != 0) {
1884 pw.print("; time=");
1885 TimeUtils.formatDuration(now - ent.getTime(), pw);
1886 pw.print(" ago");
1887 }
1888 if (ent.getRejectTime() != 0) {
1889 pw.print("; rejectTime=");
1890 TimeUtils.formatDuration(now - ent.getRejectTime(), pw);
1891 pw.print(" ago");
1892 }
1893 if (ent.getDuration() == -1) {
1894 pw.print(" (running)");
1895 } else if (ent.getDuration() != 0) {
1896 pw.print("; duration=");
1897 TimeUtils.formatDuration(ent.getDuration(), pw);
1898 }
1899 pw.println();
1900 }
1901 }
1902 return 0;
1903 }
Dianne Hackborne91f3e72016-03-25 18:48:15 -07001904 case "query-op": {
1905 int res = shell.parseUserOpMode(AppOpsManager.MODE_IGNORED, err);
1906 if (res < 0) {
1907 return res;
1908 }
1909 List<AppOpsManager.PackageOps> ops = shell.mInterface.getPackagesForOps(
1910 new int[] {shell.op});
1911 if (ops == null || ops.size() <= 0) {
1912 pw.println("No operations.");
1913 return 0;
1914 }
1915 for (int i=0; i<ops.size(); i++) {
1916 final AppOpsManager.PackageOps pkg = ops.get(i);
1917 boolean hasMatch = false;
1918 final List<AppOpsManager.OpEntry> entries = ops.get(i).getOps();
1919 for (int j=0; j<entries.size(); j++) {
1920 AppOpsManager.OpEntry ent = entries.get(j);
1921 if (ent.getOp() == shell.op && ent.getMode() == shell.mode) {
1922 hasMatch = true;
1923 break;
1924 }
1925 }
1926 if (hasMatch) {
1927 pw.println(pkg.getPackageName());
1928 }
1929 }
1930 return 0;
1931 }
Dianne Hackborn268e4e32015-11-18 16:29:56 -08001932 case "reset": {
1933 String packageName = null;
1934 int userId = UserHandle.USER_CURRENT;
1935 for (String argument; (argument = shell.getNextArg()) != null;) {
1936 if ("--user".equals(argument)) {
1937 String userStr = shell.getNextArgRequired();
1938 userId = UserHandle.parseUserArg(userStr);
1939 } else {
1940 if (packageName == null) {
1941 packageName = argument;
1942 } else {
1943 err.println("Error: Unsupported argument: " + argument);
1944 return -1;
1945 }
1946 }
1947 }
1948
1949 if (userId == UserHandle.USER_CURRENT) {
1950 userId = ActivityManager.getCurrentUser();
1951 }
1952
1953 shell.mInterface.resetAllModes(userId, packageName);
1954 pw.print("Reset all modes for: ");
1955 if (userId == UserHandle.USER_ALL) {
1956 pw.print("all users");
1957 } else {
1958 pw.print("user "); pw.print(userId);
1959 }
1960 pw.print(", ");
1961 if (packageName == null) {
1962 pw.println("all packages");
1963 } else {
1964 pw.print("package "); pw.println(packageName);
1965 }
1966 return 0;
1967 }
1968 case "write-settings": {
1969 shell.mInternal.mContext.enforcePermission(
1970 android.Manifest.permission.UPDATE_APP_OPS_STATS,
1971 Binder.getCallingPid(), Binder.getCallingUid(), null);
1972 long token = Binder.clearCallingIdentity();
1973 try {
1974 synchronized (shell.mInternal) {
1975 shell.mInternal.mHandler.removeCallbacks(shell.mInternal.mWriteRunner);
1976 }
1977 shell.mInternal.writeState();
1978 pw.println("Current settings written.");
1979 } finally {
1980 Binder.restoreCallingIdentity(token);
1981 }
1982 return 0;
1983 }
1984 case "read-settings": {
1985 shell.mInternal.mContext.enforcePermission(
1986 android.Manifest.permission.UPDATE_APP_OPS_STATS,
1987 Binder.getCallingPid(), Binder.getCallingUid(), null);
1988 long token = Binder.clearCallingIdentity();
1989 try {
1990 shell.mInternal.readState();
1991 pw.println("Last settings read.");
1992 } finally {
1993 Binder.restoreCallingIdentity(token);
1994 }
1995 return 0;
1996 }
1997 default:
1998 return shell.handleDefaultCommands(cmd);
1999 }
2000 } catch (RemoteException e) {
2001 pw.println("Remote exception: " + e);
2002 }
2003 return -1;
2004 }
2005
2006 private void dumpHelp(PrintWriter pw) {
2007 pw.println("AppOps service (appops) dump options:");
2008 pw.println(" none");
Dianne Hackborn4d34bb82015-08-07 18:26:38 -07002009 }
2010
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002011 @Override
2012 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2013 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2014 != PackageManager.PERMISSION_GRANTED) {
2015 pw.println("Permission Denial: can't dump ApOps service from from pid="
2016 + Binder.getCallingPid()
2017 + ", uid=" + Binder.getCallingUid());
2018 return;
2019 }
2020
Dianne Hackborn4d34bb82015-08-07 18:26:38 -07002021 if (args != null) {
2022 for (int i=0; i<args.length; i++) {
2023 String arg = args[i];
2024 if ("-h".equals(arg)) {
2025 dumpHelp(pw);
2026 return;
Tim Kilbourn8f1ea832015-08-26 15:07:37 -07002027 } else if ("-a".equals(arg)) {
2028 // dump all data
Dianne Hackborn4d34bb82015-08-07 18:26:38 -07002029 } else if (arg.length() > 0 && arg.charAt(0) == '-'){
2030 pw.println("Unknown option: " + arg);
2031 return;
2032 } else {
2033 pw.println("Unknown command: " + arg);
2034 return;
2035 }
2036 }
2037 }
2038
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002039 synchronized (this) {
2040 pw.println("Current AppOps Service state:");
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08002041 final long now = System.currentTimeMillis();
Dianne Hackborne98f5db2013-07-17 17:23:25 -07002042 boolean needSep = false;
2043 if (mOpModeWatchers.size() > 0) {
2044 needSep = true;
2045 pw.println(" Op mode watchers:");
2046 for (int i=0; i<mOpModeWatchers.size(); i++) {
2047 pw.print(" Op "); pw.print(AppOpsManager.opToName(mOpModeWatchers.keyAt(i)));
2048 pw.println(":");
2049 ArrayList<Callback> callbacks = mOpModeWatchers.valueAt(i);
2050 for (int j=0; j<callbacks.size(); j++) {
2051 pw.print(" #"); pw.print(j); pw.print(": ");
2052 pw.println(callbacks.get(j));
2053 }
2054 }
2055 }
2056 if (mPackageModeWatchers.size() > 0) {
2057 needSep = true;
2058 pw.println(" Package mode watchers:");
2059 for (int i=0; i<mPackageModeWatchers.size(); i++) {
2060 pw.print(" Pkg "); pw.print(mPackageModeWatchers.keyAt(i));
2061 pw.println(":");
2062 ArrayList<Callback> callbacks = mPackageModeWatchers.valueAt(i);
2063 for (int j=0; j<callbacks.size(); j++) {
2064 pw.print(" #"); pw.print(j); pw.print(": ");
2065 pw.println(callbacks.get(j));
2066 }
2067 }
2068 }
2069 if (mModeWatchers.size() > 0) {
2070 needSep = true;
2071 pw.println(" All mode watchers:");
2072 for (int i=0; i<mModeWatchers.size(); i++) {
2073 pw.print(" "); pw.print(mModeWatchers.keyAt(i));
2074 pw.print(" -> "); pw.println(mModeWatchers.valueAt(i));
2075 }
2076 }
2077 if (mClients.size() > 0) {
2078 needSep = true;
2079 pw.println(" Clients:");
2080 for (int i=0; i<mClients.size(); i++) {
2081 pw.print(" "); pw.print(mClients.keyAt(i)); pw.println(":");
2082 ClientState cs = mClients.valueAt(i);
2083 pw.print(" "); pw.println(cs);
2084 if (cs.mStartedOps != null && cs.mStartedOps.size() > 0) {
2085 pw.println(" Started ops:");
2086 for (int j=0; j<cs.mStartedOps.size(); j++) {
2087 Op op = cs.mStartedOps.get(j);
2088 pw.print(" "); pw.print("uid="); pw.print(op.uid);
2089 pw.print(" pkg="); pw.print(op.packageName);
2090 pw.print(" op="); pw.println(AppOpsManager.opToName(op.op));
2091 }
2092 }
2093 }
2094 }
John Spurlock1af30c72014-03-10 08:33:35 -04002095 if (mAudioRestrictions.size() > 0) {
2096 boolean printedHeader = false;
2097 for (int o=0; o<mAudioRestrictions.size(); o++) {
2098 final String op = AppOpsManager.opToName(mAudioRestrictions.keyAt(o));
2099 final SparseArray<Restriction> restrictions = mAudioRestrictions.valueAt(o);
2100 for (int i=0; i<restrictions.size(); i++) {
2101 if (!printedHeader){
2102 pw.println(" Audio Restrictions:");
2103 printedHeader = true;
2104 needSep = true;
2105 }
John Spurlock7b414672014-07-18 13:02:39 -04002106 final int usage = restrictions.keyAt(i);
John Spurlock1af30c72014-03-10 08:33:35 -04002107 pw.print(" "); pw.print(op);
John Spurlock7b414672014-07-18 13:02:39 -04002108 pw.print(" usage="); pw.print(AudioAttributes.usageToString(usage));
John Spurlock1af30c72014-03-10 08:33:35 -04002109 Restriction r = restrictions.valueAt(i);
2110 pw.print(": mode="); pw.println(r.mode);
2111 if (!r.exceptionPackages.isEmpty()) {
2112 pw.println(" Exceptions:");
2113 for (int j=0; j<r.exceptionPackages.size(); j++) {
2114 pw.print(" "); pw.println(r.exceptionPackages.valueAt(j));
2115 }
2116 }
2117 }
2118 }
2119 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -07002120 if (needSep) {
2121 pw.println();
2122 }
Svet Ganov2af57082015-07-30 08:44:20 -07002123 for (int i=0; i<mUidStates.size(); i++) {
2124 UidState uidState = mUidStates.valueAt(i);
2125
2126 pw.print(" Uid "); UserHandle.formatUid(pw, uidState.uid); pw.println(":");
2127
2128 SparseIntArray opModes = uidState.opModes;
2129 if (opModes != null) {
2130 final int opModeCount = opModes.size();
2131 for (int j = 0; j < opModeCount; j++) {
2132 final int code = opModes.keyAt(j);
2133 final int mode = opModes.valueAt(j);
2134 pw.print(" "); pw.print(AppOpsManager.opToName(code));
2135 pw.print(": mode="); pw.println(mode);
2136 }
2137 }
2138
2139 ArrayMap<String, Ops> pkgOps = uidState.pkgOps;
2140 if (pkgOps == null) {
2141 continue;
2142 }
2143
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002144 for (Ops ops : pkgOps.values()) {
2145 pw.print(" Package "); pw.print(ops.packageName); pw.println(":");
2146 for (int j=0; j<ops.size(); j++) {
2147 Op op = ops.valueAt(j);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08002148 pw.print(" "); pw.print(AppOpsManager.opToName(op.op));
2149 pw.print(": mode="); pw.print(op.mode);
2150 if (op.time != 0) {
2151 pw.print("; time="); TimeUtils.formatDuration(now-op.time, pw);
2152 pw.print(" ago");
2153 }
2154 if (op.rejectTime != 0) {
2155 pw.print("; rejectTime="); TimeUtils.formatDuration(now-op.rejectTime, pw);
2156 pw.print(" ago");
2157 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002158 if (op.duration == -1) {
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -08002159 pw.print(" (running)");
2160 } else if (op.duration != 0) {
2161 pw.print("; duration="); TimeUtils.formatDuration(op.duration, pw);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002162 }
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -08002163 pw.println();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002164 }
2165 }
2166 }
2167 }
2168 }
John Spurlock1af30c72014-03-10 08:33:35 -04002169
2170 private static final class Restriction {
2171 private static final ArraySet<String> NO_EXCEPTIONS = new ArraySet<String>();
2172 int mode;
2173 ArraySet<String> exceptionPackages = NO_EXCEPTIONS;
2174 }
Jason Monk62062992014-05-06 09:55:28 -04002175
2176 @Override
Svet Ganov9cea80cd2016-02-16 11:47:00 -08002177 public void setUserRestrictions(Bundle restrictions, IBinder token, int userHandle) {
Jason Monk62062992014-05-06 09:55:28 -04002178 checkSystemUid("setUserRestrictions");
Svetoslav Ganovf73adb62016-03-29 01:07:06 +00002179 Preconditions.checkNotNull(restrictions);
Svet Ganov9cea80cd2016-02-16 11:47:00 -08002180 Preconditions.checkNotNull(token);
Svetoslav Ganova8bbd762016-05-13 17:08:16 -07002181 for (int i = 0; i < AppOpsManager._NUM_OP; i++) {
Jason Monk62062992014-05-06 09:55:28 -04002182 String restriction = AppOpsManager.opToRestriction(i);
Suprabh Shukla64e0dcb2016-05-24 16:23:11 -07002183 if (restriction != null) {
2184 setUserRestrictionNoCheck(i, restrictions.getBoolean(restriction, false), token,
2185 userHandle, null);
Svetoslav Ganova8bbd762016-05-13 17:08:16 -07002186 }
Svet Ganov9cea80cd2016-02-16 11:47:00 -08002187 }
2188 }
2189
2190 @Override
Ruben Brunk29931bc2016-03-11 00:24:26 -08002191 public void setUserRestriction(int code, boolean restricted, IBinder token, int userHandle,
2192 String[] exceptionPackages) {
Svet Ganov9cea80cd2016-02-16 11:47:00 -08002193 if (Binder.getCallingPid() != Process.myPid()) {
2194 mContext.enforcePermission(Manifest.permission.MANAGE_APP_OPS_RESTRICTIONS,
2195 Binder.getCallingPid(), Binder.getCallingUid(), null);
2196 }
2197 if (userHandle != UserHandle.getCallingUserId()) {
2198 if (mContext.checkCallingOrSelfPermission(Manifest.permission
2199 .INTERACT_ACROSS_USERS_FULL) != PackageManager.PERMISSION_GRANTED
2200 && mContext.checkCallingOrSelfPermission(Manifest.permission
2201 .INTERACT_ACROSS_USERS) != PackageManager.PERMISSION_GRANTED) {
2202 throw new SecurityException("Need INTERACT_ACROSS_USERS_FULL or"
2203 + " INTERACT_ACROSS_USERS to interact cross user ");
Jason Monk62062992014-05-06 09:55:28 -04002204 }
2205 }
Svet Ganov9cea80cd2016-02-16 11:47:00 -08002206 verifyIncomingOp(code);
2207 Preconditions.checkNotNull(token);
Ruben Brunk29931bc2016-03-11 00:24:26 -08002208 setUserRestrictionNoCheck(code, restricted, token, userHandle, exceptionPackages);
Svet Ganov9cea80cd2016-02-16 11:47:00 -08002209 }
2210
2211 private void setUserRestrictionNoCheck(int code, boolean restricted, IBinder token,
Ruben Brunk29931bc2016-03-11 00:24:26 -08002212 int userHandle, String[] exceptionPackages) {
Svet Ganov442ed572016-08-17 17:29:43 -07002213 boolean notifyChange = false;
Ruben Brunk29931bc2016-03-11 00:24:26 -08002214
Svet Ganov442ed572016-08-17 17:29:43 -07002215 synchronized (AppOpsService.this) {
2216 ClientRestrictionState restrictionState = mOpUserRestrictions.get(token);
2217
2218 if (restrictionState == null) {
2219 try {
2220 restrictionState = new ClientRestrictionState(token);
2221 } catch (RemoteException e) {
2222 return;
2223 }
2224 mOpUserRestrictions.put(token, restrictionState);
Ruben Brunk29931bc2016-03-11 00:24:26 -08002225 }
Svet Ganov442ed572016-08-17 17:29:43 -07002226
2227 if (restrictionState.setRestriction(code, restricted, exceptionPackages, userHandle)) {
2228 notifyChange = true;
2229 }
2230
2231 if (restrictionState.isDefault()) {
2232 mOpUserRestrictions.remove(token);
2233 restrictionState.destroy();
2234 }
Ruben Brunk29931bc2016-03-11 00:24:26 -08002235 }
2236
Svet Ganov442ed572016-08-17 17:29:43 -07002237 if (notifyChange) {
Svetoslav Ganova8bbd762016-05-13 17:08:16 -07002238 notifyWatchersOfChange(code);
Svet Ganov9cea80cd2016-02-16 11:47:00 -08002239 }
Julia Reynoldsbb21c252016-04-05 16:01:49 -04002240 }
2241
2242 private void notifyWatchersOfChange(int code) {
Svet Ganov9cea80cd2016-02-16 11:47:00 -08002243 final ArrayList<Callback> clonedCallbacks;
2244 synchronized (this) {
2245 ArrayList<Callback> callbacks = mOpModeWatchers.get(code);
2246 if (callbacks == null) {
2247 return;
2248 }
2249 clonedCallbacks = new ArrayList<>(callbacks);
2250 }
2251
2252 // There are components watching for mode changes such as window manager
2253 // and location manager which are in our process. The callbacks in these
Svetoslav Ganova8bbd762016-05-13 17:08:16 -07002254 // components may require permissions our remote caller does not have.s
Svet Ganov9cea80cd2016-02-16 11:47:00 -08002255 final long identity = Binder.clearCallingIdentity();
2256 try {
2257 final int callbackCount = clonedCallbacks.size();
2258 for (int i = 0; i < callbackCount; i++) {
2259 Callback callback = clonedCallbacks.get(i);
2260 try {
2261 callback.mCallback.opChanged(code, -1, null);
2262 } catch (RemoteException e) {
2263 Log.w(TAG, "Error dispatching op op change", e);
2264 }
2265 }
2266 } finally {
2267 Binder.restoreCallingIdentity(identity);
2268 }
Jason Monk62062992014-05-06 09:55:28 -04002269 }
2270
2271 @Override
2272 public void removeUser(int userHandle) throws RemoteException {
2273 checkSystemUid("removeUser");
Svet Ganov442ed572016-08-17 17:29:43 -07002274 synchronized (AppOpsService.this) {
2275 final int tokenCount = mOpUserRestrictions.size();
2276 for (int i = tokenCount - 1; i >= 0; i--) {
2277 ClientRestrictionState opRestrictions = mOpUserRestrictions.valueAt(i);
2278 opRestrictions.removeUser(userHandle);
2279 }
Sudheer Shankabc2fadd2016-09-27 17:36:39 -07002280 removeUidsForUserLocked(userHandle);
2281 }
2282 }
2283
2284 private void removeUidsForUserLocked(int userHandle) {
2285 for (int i = mUidStates.size() - 1; i >= 0; --i) {
2286 final int uid = mUidStates.keyAt(i);
2287 if (UserHandle.getUserId(uid) == userHandle) {
2288 mUidStates.removeAt(i);
2289 }
Svet Ganov9cea80cd2016-02-16 11:47:00 -08002290 }
2291 }
2292
Jason Monk62062992014-05-06 09:55:28 -04002293 private void checkSystemUid(String function) {
2294 int uid = Binder.getCallingUid();
2295 if (uid != Process.SYSTEM_UID) {
2296 throw new SecurityException(function + " must by called by the system");
2297 }
2298 }
2299
Svetoslav Ganovf73adb62016-03-29 01:07:06 +00002300 private static String resolvePackageName(int uid, String packageName) {
2301 if (uid == 0) {
2302 return "root";
2303 } else if (uid == Process.SHELL_UID) {
2304 return "com.android.shell";
2305 } else if (uid == Process.SYSTEM_UID && packageName == null) {
2306 return "android";
2307 }
2308 return packageName;
2309 }
2310
Svet Ganov2af57082015-07-30 08:44:20 -07002311 private static String[] getPackagesForUid(int uid) {
Svet Ganovf3807aa2015-08-02 10:09:56 -07002312 String[] packageNames = null;
Svet Ganov2af57082015-07-30 08:44:20 -07002313 try {
riddle_hsu40b300f2015-11-23 13:22:03 +08002314 packageNames = AppGlobals.getPackageManager().getPackagesForUid(uid);
Svet Ganov2af57082015-07-30 08:44:20 -07002315 } catch (RemoteException e) {
2316 /* ignore - local call */
2317 }
Svet Ganovf3807aa2015-08-02 10:09:56 -07002318 if (packageNames == null) {
2319 return EmptyArray.STRING;
2320 }
2321 return packageNames;
Svet Ganov2af57082015-07-30 08:44:20 -07002322 }
Svetoslav Ganova8bbd762016-05-13 17:08:16 -07002323
2324 private final class ClientRestrictionState implements DeathRecipient {
2325 private final IBinder token;
2326 SparseArray<boolean[]> perUserRestrictions;
2327 SparseArray<String[]> perUserExcludedPackages;
2328
2329 public ClientRestrictionState(IBinder token)
2330 throws RemoteException {
2331 token.linkToDeath(this, 0);
2332 this.token = token;
2333 }
2334
2335 public boolean setRestriction(int code, boolean restricted,
2336 String[] excludedPackages, int userId) {
2337 boolean changed = false;
2338
2339 if (perUserRestrictions == null && restricted) {
2340 perUserRestrictions = new SparseArray<>();
2341 }
2342
2343 if (perUserRestrictions != null) {
2344 boolean[] userRestrictions = perUserRestrictions.get(userId);
2345 if (userRestrictions == null && restricted) {
2346 userRestrictions = new boolean[AppOpsManager._NUM_OP];
2347 perUserRestrictions.put(userId, userRestrictions);
2348 }
2349 if (userRestrictions != null && userRestrictions[code] != restricted) {
2350 userRestrictions[code] = restricted;
2351 if (!restricted && isDefault(userRestrictions)) {
2352 perUserRestrictions.remove(userId);
2353 userRestrictions = null;
2354 }
2355 changed = true;
2356 }
2357
2358 if (userRestrictions != null) {
2359 final boolean noExcludedPackages = ArrayUtils.isEmpty(excludedPackages);
2360 if (perUserExcludedPackages == null && !noExcludedPackages) {
2361 perUserExcludedPackages = new SparseArray<>();
2362 }
2363 if (perUserExcludedPackages != null && !Arrays.equals(excludedPackages,
2364 perUserExcludedPackages.get(userId))) {
2365 if (noExcludedPackages) {
2366 perUserExcludedPackages.remove(userId);
2367 if (perUserExcludedPackages.size() <= 0) {
2368 perUserExcludedPackages = null;
2369 }
2370 } else {
2371 perUserExcludedPackages.put(userId, excludedPackages);
2372 }
2373 changed = true;
2374 }
2375 }
2376 }
2377
2378 return changed;
2379 }
2380
2381 public boolean hasRestriction(int restriction, String packageName, int userId) {
2382 if (perUserRestrictions == null) {
2383 return false;
2384 }
2385 boolean[] restrictions = perUserRestrictions.get(userId);
2386 if (restrictions == null) {
2387 return false;
2388 }
2389 if (!restrictions[restriction]) {
2390 return false;
2391 }
2392 if (perUserExcludedPackages == null) {
2393 return true;
2394 }
2395 String[] perUserExclusions = perUserExcludedPackages.get(userId);
2396 if (perUserExclusions == null) {
2397 return true;
2398 }
2399 return !ArrayUtils.contains(perUserExclusions, packageName);
2400 }
2401
2402 public void removeUser(int userId) {
2403 if (perUserExcludedPackages != null) {
2404 perUserExcludedPackages.remove(userId);
2405 if (perUserExcludedPackages.size() <= 0) {
2406 perUserExcludedPackages = null;
2407 }
2408 }
Sudheer Shankabc2fadd2016-09-27 17:36:39 -07002409 if (perUserRestrictions != null) {
2410 perUserRestrictions.remove(userId);
2411 if (perUserRestrictions.size() <= 0) {
2412 perUserRestrictions = null;
2413 }
2414 }
Svetoslav Ganova8bbd762016-05-13 17:08:16 -07002415 }
2416
2417 public boolean isDefault() {
2418 return perUserRestrictions == null || perUserRestrictions.size() <= 0;
2419 }
2420
2421 @Override
2422 public void binderDied() {
2423 synchronized (AppOpsService.this) {
2424 mOpUserRestrictions.remove(token);
2425 if (perUserRestrictions == null) {
2426 return;
2427 }
2428 final int userCount = perUserRestrictions.size();
2429 for (int i = 0; i < userCount; i++) {
2430 final boolean[] restrictions = perUserRestrictions.valueAt(i);
2431 final int restrictionCount = restrictions.length;
2432 for (int j = 0; j < restrictionCount; j++) {
2433 if (restrictions[j]) {
2434 final int changedCode = j;
2435 mHandler.post(() -> notifyWatchersOfChange(changedCode));
2436 }
2437 }
2438 }
2439 destroy();
2440 }
2441 }
2442
2443 public void destroy() {
2444 token.unlinkToDeath(this, 0);
2445 }
2446
2447 private boolean isDefault(boolean[] array) {
2448 if (ArrayUtils.isEmpty(array)) {
2449 return true;
2450 }
2451 for (boolean value : array) {
2452 if (value) {
2453 return false;
2454 }
2455 }
2456 return true;
2457 }
2458 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002459}