blob: 417f18db9d76c0c0665fab362e89aa493b765178 [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;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080028import java.util.HashMap;
Dianne Hackbornc2293022013-02-06 23:14:49 -080029import java.util.Iterator;
Dianne Hackborn35654b62013-01-14 17:38:02 -080030import java.util.List;
Dianne Hackborn607b4142013-08-02 18:10:10 -070031import java.util.Map;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080032
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -080033import android.app.ActivityManager;
Jason Monk1c7c3192014-06-26 12:52:18 -040034import android.app.ActivityThread;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080035import android.app.AppOpsManager;
36import android.content.Context;
Jason Monk1c7c3192014-06-26 12:52:18 -040037import android.content.pm.ApplicationInfo;
38import android.content.pm.IPackageManager;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080039import android.content.pm.PackageManager;
40import android.content.pm.PackageManager.NameNotFoundException;
John Spurlock7b414672014-07-18 13:02:39 -040041import android.media.AudioAttributes;
Dianne Hackborn35654b62013-01-14 17:38:02 -080042import android.os.AsyncTask;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080043import android.os.Binder;
Jason Monk62062992014-05-06 09:55:28 -040044import android.os.Bundle;
Dianne Hackborn35654b62013-01-14 17:38:02 -080045import android.os.Handler;
Dianne Hackbornc2293022013-02-06 23:14:49 -080046import android.os.IBinder;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080047import android.os.Process;
Dianne Hackbornc2293022013-02-06 23:14:49 -080048import android.os.RemoteException;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080049import android.os.ServiceManager;
50import android.os.UserHandle;
Svet Ganov6ee871e2015-07-10 14:29:33 -070051import android.os.storage.MountServiceInternal;
Dianne Hackborne98f5db2013-07-17 17:23:25 -070052import android.util.ArrayMap;
John Spurlock1af30c72014-03-10 08:33:35 -040053import android.util.ArraySet;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080054import android.util.AtomicFile;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -080055import android.util.Log;
Dianne Hackborn607b4142013-08-02 18:10:10 -070056import android.util.Pair;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080057import android.util.Slog;
58import android.util.SparseArray;
59import android.util.TimeUtils;
Dianne Hackborn35654b62013-01-14 17:38:02 -080060import android.util.Xml;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080061
62import com.android.internal.app.IAppOpsService;
Dianne Hackbornc2293022013-02-06 23:14:49 -080063import com.android.internal.app.IAppOpsCallback;
Svet Ganov6ee871e2015-07-10 14:29:33 -070064import com.android.internal.os.Zygote;
Dianne Hackborn35654b62013-01-14 17:38:02 -080065import com.android.internal.util.FastXmlSerializer;
66import com.android.internal.util.XmlUtils;
67
68import org.xmlpull.v1.XmlPullParser;
69import org.xmlpull.v1.XmlPullParserException;
70import org.xmlpull.v1.XmlSerializer;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080071
72public class AppOpsService extends IAppOpsService.Stub {
73 static final String TAG = "AppOps";
Dianne Hackborn35654b62013-01-14 17:38:02 -080074 static final boolean DEBUG = false;
75
76 // Write at most every 30 minutes.
77 static final long WRITE_DELAY = DEBUG ? 1000 : 30*60*1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080078
79 Context mContext;
80 final AtomicFile mFile;
Dianne Hackborn35654b62013-01-14 17:38:02 -080081 final Handler mHandler;
82
83 boolean mWriteScheduled;
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -080084 boolean mFastWriteScheduled;
Dianne Hackborn35654b62013-01-14 17:38:02 -080085 final Runnable mWriteRunner = new Runnable() {
86 public void run() {
87 synchronized (AppOpsService.this) {
88 mWriteScheduled = false;
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -080089 mFastWriteScheduled = false;
Dianne Hackborn35654b62013-01-14 17:38:02 -080090 AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
91 @Override protected Void doInBackground(Void... params) {
92 writeState();
93 return null;
94 }
95 };
96 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
97 }
98 }
99 };
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800100
101 final SparseArray<HashMap<String, Ops>> mUidOps
102 = new SparseArray<HashMap<String, Ops>>();
103
Jason Monk62062992014-05-06 09:55:28 -0400104 private final SparseArray<boolean[]> mOpRestrictions = new SparseArray<boolean[]>();
105
Dianne Hackbornc2293022013-02-06 23:14:49 -0800106 public final static class Ops extends SparseArray<Op> {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800107 public final String packageName;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800108 public final int uid;
Jason Monk1c7c3192014-06-26 12:52:18 -0400109 public final boolean isPrivileged;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800110
Jason Monk1c7c3192014-06-26 12:52:18 -0400111 public Ops(String _packageName, int _uid, boolean _isPrivileged) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800112 packageName = _packageName;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800113 uid = _uid;
Jason Monk1c7c3192014-06-26 12:52:18 -0400114 isPrivileged = _isPrivileged;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800115 }
116 }
117
Dianne Hackbornc2293022013-02-06 23:14:49 -0800118 public final static class Op {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700119 public final int uid;
120 public final String packageName;
Svet Ganov99b60432015-06-27 13:15:22 -0700121 public int proxyUid = -1;
122 public String proxyPackageName;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800123 public final int op;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800124 public int mode;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800125 public int duration;
126 public long time;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800127 public long rejectTime;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800128 public int nesting;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800129
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700130 public Op(int _uid, String _packageName, int _op) {
131 uid = _uid;
132 packageName = _packageName;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800133 op = _op;
David Braunf5d83192013-09-16 13:43:51 -0700134 mode = AppOpsManager.opToDefaultMode(op);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800135 }
136 }
137
Dianne Hackbornc2293022013-02-06 23:14:49 -0800138 final SparseArray<ArrayList<Callback>> mOpModeWatchers
139 = new SparseArray<ArrayList<Callback>>();
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700140 final ArrayMap<String, ArrayList<Callback>> mPackageModeWatchers
141 = new ArrayMap<String, ArrayList<Callback>>();
142 final ArrayMap<IBinder, Callback> mModeWatchers
143 = new ArrayMap<IBinder, Callback>();
John Spurlock1af30c72014-03-10 08:33:35 -0400144 final SparseArray<SparseArray<Restriction>> mAudioRestrictions
145 = new SparseArray<SparseArray<Restriction>>();
Dianne Hackbornc2293022013-02-06 23:14:49 -0800146
147 public final class Callback implements DeathRecipient {
148 final IAppOpsCallback mCallback;
149
150 public Callback(IAppOpsCallback callback) {
151 mCallback = callback;
152 try {
153 mCallback.asBinder().linkToDeath(this, 0);
154 } catch (RemoteException e) {
155 }
156 }
157
158 public void unlinkToDeath() {
159 mCallback.asBinder().unlinkToDeath(this, 0);
160 }
161
162 @Override
163 public void binderDied() {
164 stopWatchingMode(mCallback);
165 }
166 }
167
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700168 final ArrayMap<IBinder, ClientState> mClients = new ArrayMap<IBinder, ClientState>();
169
170 public final class ClientState extends Binder implements DeathRecipient {
171 final IBinder mAppToken;
172 final int mPid;
173 final ArrayList<Op> mStartedOps;
174
175 public ClientState(IBinder appToken) {
176 mAppToken = appToken;
177 mPid = Binder.getCallingPid();
178 if (appToken instanceof Binder) {
179 // For local clients, there is no reason to track them.
180 mStartedOps = null;
181 } else {
182 mStartedOps = new ArrayList<Op>();
183 try {
184 mAppToken.linkToDeath(this, 0);
185 } catch (RemoteException e) {
186 }
187 }
188 }
189
190 @Override
191 public String toString() {
192 return "ClientState{" +
193 "mAppToken=" + mAppToken +
194 ", " + (mStartedOps != null ? ("pid=" + mPid) : "local") +
195 '}';
196 }
197
198 @Override
199 public void binderDied() {
200 synchronized (AppOpsService.this) {
201 for (int i=mStartedOps.size()-1; i>=0; i--) {
202 finishOperationLocked(mStartedOps.get(i));
203 }
204 mClients.remove(mAppToken);
205 }
206 }
207 }
208
Jeff Brown6f357d32014-01-15 20:40:55 -0800209 public AppOpsService(File storagePath, Handler handler) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800210 mFile = new AtomicFile(storagePath);
Jeff Brown6f357d32014-01-15 20:40:55 -0800211 mHandler = handler;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800212 readState();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800213 }
David Braunf5d83192013-09-16 13:43:51 -0700214
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800215 public void publish(Context context) {
216 mContext = context;
217 ServiceManager.addService(Context.APP_OPS_SERVICE, asBinder());
218 }
219
Dianne Hackborn514074f2013-02-11 10:52:46 -0800220 public void systemReady() {
221 synchronized (this) {
222 boolean changed = false;
223 for (int i=0; i<mUidOps.size(); i++) {
224 HashMap<String, Ops> pkgs = mUidOps.valueAt(i);
225 Iterator<Ops> it = pkgs.values().iterator();
226 while (it.hasNext()) {
227 Ops ops = it.next();
228 int curUid;
229 try {
230 curUid = mContext.getPackageManager().getPackageUid(ops.packageName,
231 UserHandle.getUserId(ops.uid));
232 } catch (NameNotFoundException e) {
233 curUid = -1;
234 }
235 if (curUid != ops.uid) {
236 Slog.i(TAG, "Pruning old package " + ops.packageName
237 + "/" + ops.uid + ": new uid=" + curUid);
238 it.remove();
239 changed = true;
240 }
241 }
242 if (pkgs.size() <= 0) {
243 mUidOps.removeAt(i);
244 }
245 }
246 if (changed) {
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800247 scheduleFastWriteLocked();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800248 }
249 }
Svet Ganov6ee871e2015-07-10 14:29:33 -0700250
251 MountServiceInternal mountServiceInternal = LocalServices.getService(
252 MountServiceInternal.class);
253 mountServiceInternal.addExternalStoragePolicy(
254 new MountServiceInternal.ExternalStorageMountPolicy() {
255 @Override
256 public int getMountMode(int uid, String packageName) {
257 if (Process.isIsolated(uid)) {
258 return Zygote.MOUNT_EXTERNAL_NONE;
259 }
260 if (noteOperation(AppOpsManager.OP_READ_EXTERNAL_STORAGE, uid,
261 packageName) != AppOpsManager.MODE_ALLOWED) {
262 return Zygote.MOUNT_EXTERNAL_NONE;
263 }
264 if (noteOperation(AppOpsManager.OP_WRITE_EXTERNAL_STORAGE, uid,
265 packageName) != AppOpsManager.MODE_ALLOWED) {
266 return Zygote.MOUNT_EXTERNAL_READ;
267 }
268 return Zygote.MOUNT_EXTERNAL_WRITE;
269 }
270
271 @Override
272 public boolean hasExternalStorage(int uid, String packageName) {
273 final int mountMode = getMountMode(uid, packageName);
274 return mountMode == Zygote.MOUNT_EXTERNAL_READ
275 || mountMode == Zygote.MOUNT_EXTERNAL_WRITE;
276 }
277 });
Dianne Hackborn514074f2013-02-11 10:52:46 -0800278 }
279
280 public void packageRemoved(int uid, String packageName) {
281 synchronized (this) {
282 HashMap<String, Ops> pkgs = mUidOps.get(uid);
283 if (pkgs != null) {
284 if (pkgs.remove(packageName) != null) {
285 if (pkgs.size() <= 0) {
286 mUidOps.remove(uid);
287 }
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800288 scheduleFastWriteLocked();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800289 }
290 }
291 }
292 }
293
294 public void uidRemoved(int uid) {
295 synchronized (this) {
296 if (mUidOps.indexOfKey(uid) >= 0) {
297 mUidOps.remove(uid);
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800298 scheduleFastWriteLocked();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800299 }
300 }
301 }
302
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800303 public void shutdown() {
304 Slog.w(TAG, "Writing app ops before shutdown...");
Dianne Hackborn35654b62013-01-14 17:38:02 -0800305 boolean doWrite = false;
306 synchronized (this) {
307 if (mWriteScheduled) {
308 mWriteScheduled = false;
309 doWrite = true;
310 }
311 }
312 if (doWrite) {
313 writeState();
314 }
315 }
316
Dianne Hackborn72e39832013-01-18 18:36:09 -0800317 private ArrayList<AppOpsManager.OpEntry> collectOps(Ops pkgOps, int[] ops) {
318 ArrayList<AppOpsManager.OpEntry> resOps = null;
319 if (ops == null) {
320 resOps = new ArrayList<AppOpsManager.OpEntry>();
321 for (int j=0; j<pkgOps.size(); j++) {
322 Op curOp = pkgOps.valueAt(j);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800323 resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time,
Svet Ganov99b60432015-06-27 13:15:22 -0700324 curOp.rejectTime, curOp.duration, curOp.proxyUid,
325 curOp.proxyPackageName));
Dianne Hackborn72e39832013-01-18 18:36:09 -0800326 }
327 } else {
328 for (int j=0; j<ops.length; j++) {
329 Op curOp = pkgOps.get(ops[j]);
330 if (curOp != null) {
331 if (resOps == null) {
332 resOps = new ArrayList<AppOpsManager.OpEntry>();
333 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800334 resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time,
Svet Ganov99b60432015-06-27 13:15:22 -0700335 curOp.rejectTime, curOp.duration, curOp.proxyUid,
336 curOp.proxyPackageName));
Dianne Hackborn72e39832013-01-18 18:36:09 -0800337 }
338 }
339 }
340 return resOps;
341 }
342
Dianne Hackborn35654b62013-01-14 17:38:02 -0800343 @Override
344 public List<AppOpsManager.PackageOps> getPackagesForOps(int[] ops) {
345 mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS,
346 Binder.getCallingPid(), Binder.getCallingUid(), null);
347 ArrayList<AppOpsManager.PackageOps> res = null;
348 synchronized (this) {
349 for (int i=0; i<mUidOps.size(); i++) {
350 HashMap<String, Ops> packages = mUidOps.valueAt(i);
351 for (Ops pkgOps : packages.values()) {
Dianne Hackborn72e39832013-01-18 18:36:09 -0800352 ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800353 if (resOps != null) {
354 if (res == null) {
355 res = new ArrayList<AppOpsManager.PackageOps>();
356 }
357 AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps(
358 pkgOps.packageName, pkgOps.uid, resOps);
359 res.add(resPackage);
360 }
361 }
362 }
363 }
364 return res;
365 }
366
367 @Override
Dianne Hackborn72e39832013-01-18 18:36:09 -0800368 public List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName,
369 int[] ops) {
370 mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS,
371 Binder.getCallingPid(), Binder.getCallingUid(), null);
372 synchronized (this) {
373 Ops pkgOps = getOpsLocked(uid, packageName, false);
374 if (pkgOps == null) {
375 return null;
376 }
377 ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops);
378 if (resOps == null) {
379 return null;
380 }
381 ArrayList<AppOpsManager.PackageOps> res = new ArrayList<AppOpsManager.PackageOps>();
382 AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps(
383 pkgOps.packageName, pkgOps.uid, resOps);
384 res.add(resPackage);
385 return res;
386 }
387 }
388
Dianne Hackborn607b4142013-08-02 18:10:10 -0700389 private void pruneOp(Op op, int uid, String packageName) {
390 if (op.time == 0 && op.rejectTime == 0) {
391 Ops ops = getOpsLocked(uid, packageName, false);
392 if (ops != null) {
393 ops.remove(op.op);
394 if (ops.size() <= 0) {
395 HashMap<String, Ops> pkgOps = mUidOps.get(uid);
396 if (pkgOps != null) {
397 pkgOps.remove(ops.packageName);
398 if (pkgOps.size() <= 0) {
399 mUidOps.remove(uid);
400 }
401 }
402 }
403 }
404 }
405 }
406
Dianne Hackborn72e39832013-01-18 18:36:09 -0800407 @Override
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800408 public void setMode(int code, int uid, String packageName, int mode) {
Dianne Hackbornb64afe12014-07-22 16:29:04 -0700409 if (Binder.getCallingPid() != Process.myPid()) {
410 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
411 Binder.getCallingPid(), Binder.getCallingUid(), null);
Dianne Hackborn133b9df2014-07-01 13:06:10 -0700412 }
Dianne Hackborn961321f2013-02-05 17:22:41 -0800413 verifyIncomingOp(code);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800414 ArrayList<Callback> repCbs = null;
415 code = AppOpsManager.opToSwitch(code);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800416 synchronized (this) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800417 Op op = getOpLocked(code, uid, packageName, true);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800418 if (op != null) {
419 if (op.mode != mode) {
420 op.mode = mode;
Dianne Hackbornc2293022013-02-06 23:14:49 -0800421 ArrayList<Callback> cbs = mOpModeWatchers.get(code);
422 if (cbs != null) {
423 if (repCbs == null) {
424 repCbs = new ArrayList<Callback>();
425 }
426 repCbs.addAll(cbs);
427 }
428 cbs = mPackageModeWatchers.get(packageName);
429 if (cbs != null) {
430 if (repCbs == null) {
431 repCbs = new ArrayList<Callback>();
432 }
433 repCbs.addAll(cbs);
434 }
David Braunf5d83192013-09-16 13:43:51 -0700435 if (mode == AppOpsManager.opToDefaultMode(op.op)) {
Dianne Hackborn514074f2013-02-11 10:52:46 -0800436 // If going into the default mode, prune this op
437 // if there is nothing else interesting in it.
Dianne Hackborn607b4142013-08-02 18:10:10 -0700438 pruneOp(op, uid, packageName);
Dianne Hackborn514074f2013-02-11 10:52:46 -0800439 }
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800440 scheduleFastWriteLocked();
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800441 }
442 }
443 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800444 if (repCbs != null) {
Svet Ganov38536112015-05-19 12:45:52 -0700445 // There are components watching for mode changes such as window manager
446 // and location manager which are in our process. The callbacks in these
447 // components may require permissions our remote caller does not have.
448 final long identity = Binder.clearCallingIdentity();
449 try {
450 for (int i = 0; i < repCbs.size(); i++) {
451 try {
452 repCbs.get(i).mCallback.opChanged(code, packageName);
453 } catch (RemoteException e) {
454 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800455 }
Svet Ganov38536112015-05-19 12:45:52 -0700456 } finally {
457 Binder.restoreCallingIdentity(identity);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800458 }
459 }
460 }
461
Dianne Hackborn607b4142013-08-02 18:10:10 -0700462 private static HashMap<Callback, ArrayList<Pair<String, Integer>>> addCallbacks(
463 HashMap<Callback, ArrayList<Pair<String, Integer>>> callbacks,
464 String packageName, int op, ArrayList<Callback> cbs) {
465 if (cbs == null) {
466 return callbacks;
467 }
468 if (callbacks == null) {
469 callbacks = new HashMap<Callback, ArrayList<Pair<String, Integer>>>();
470 }
471 for (int i=0; i<cbs.size(); i++) {
472 Callback cb = cbs.get(i);
473 ArrayList<Pair<String, Integer>> reports = callbacks.get(cb);
474 if (reports == null) {
475 reports = new ArrayList<Pair<String, Integer>>();
476 callbacks.put(cb, reports);
477 }
478 reports.add(new Pair<String, Integer>(packageName, op));
479 }
480 return callbacks;
481 }
482
483 @Override
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800484 public void resetAllModes(int reqUserId, String reqPackageName) {
485 final int callingPid = Binder.getCallingPid();
486 final int callingUid = Binder.getCallingUid();
Dianne Hackborn607b4142013-08-02 18:10:10 -0700487 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800488 callingPid, callingUid, null);
489 reqUserId = ActivityManager.handleIncomingUser(callingPid, callingUid, reqUserId,
490 true, true, "resetAllModes", null);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700491 HashMap<Callback, ArrayList<Pair<String, Integer>>> callbacks = null;
492 synchronized (this) {
493 boolean changed = false;
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700494 for (int i=mUidOps.size()-1; i>=0; i--) {
Dianne Hackborn607b4142013-08-02 18:10:10 -0700495 HashMap<String, Ops> packages = mUidOps.valueAt(i);
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800496 if (reqUserId != UserHandle.USER_ALL
497 && reqUserId != UserHandle.getUserId(mUidOps.keyAt(i))) {
Alexandra Gherghinad6a98972014-08-04 17:05:34 +0100498 // Skip any ops for a different user
499 continue;
500 }
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700501 Iterator<Map.Entry<String, Ops>> it = packages.entrySet().iterator();
502 while (it.hasNext()) {
503 Map.Entry<String, Ops> ent = it.next();
Dianne Hackborn607b4142013-08-02 18:10:10 -0700504 String packageName = ent.getKey();
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800505 if (reqPackageName != null && !reqPackageName.equals(packageName)) {
506 // Skip any ops for a different package
507 continue;
508 }
Dianne Hackborn607b4142013-08-02 18:10:10 -0700509 Ops pkgOps = ent.getValue();
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700510 for (int j=pkgOps.size()-1; j>=0; j--) {
Dianne Hackborn607b4142013-08-02 18:10:10 -0700511 Op curOp = pkgOps.valueAt(j);
Dianne Hackborn8828d3a2013-09-25 16:47:10 -0700512 if (AppOpsManager.opAllowsReset(curOp.op)
513 && curOp.mode != AppOpsManager.opToDefaultMode(curOp.op)) {
David Braunf5d83192013-09-16 13:43:51 -0700514 curOp.mode = AppOpsManager.opToDefaultMode(curOp.op);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700515 changed = true;
516 callbacks = addCallbacks(callbacks, packageName, curOp.op,
517 mOpModeWatchers.get(curOp.op));
518 callbacks = addCallbacks(callbacks, packageName, curOp.op,
519 mPackageModeWatchers.get(packageName));
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700520 if (curOp.time == 0 && curOp.rejectTime == 0) {
521 pkgOps.removeAt(j);
522 }
Dianne Hackborn607b4142013-08-02 18:10:10 -0700523 }
524 }
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700525 if (pkgOps.size() == 0) {
526 it.remove();
527 }
528 }
529 if (packages.size() == 0) {
530 mUidOps.removeAt(i);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700531 }
532 }
533 if (changed) {
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800534 scheduleFastWriteLocked();
Dianne Hackborn607b4142013-08-02 18:10:10 -0700535 }
536 }
537 if (callbacks != null) {
538 for (Map.Entry<Callback, ArrayList<Pair<String, Integer>>> ent : callbacks.entrySet()) {
539 Callback cb = ent.getKey();
540 ArrayList<Pair<String, Integer>> reports = ent.getValue();
541 for (int i=0; i<reports.size(); i++) {
542 Pair<String, Integer> rep = reports.get(i);
543 try {
544 cb.mCallback.opChanged(rep.second, rep.first);
545 } catch (RemoteException e) {
546 }
547 }
548 }
549 }
550 }
551
Dianne Hackbornc2293022013-02-06 23:14:49 -0800552 @Override
553 public void startWatchingMode(int op, String packageName, IAppOpsCallback callback) {
554 synchronized (this) {
555 op = AppOpsManager.opToSwitch(op);
556 Callback cb = mModeWatchers.get(callback.asBinder());
557 if (cb == null) {
558 cb = new Callback(callback);
559 mModeWatchers.put(callback.asBinder(), cb);
560 }
561 if (op != AppOpsManager.OP_NONE) {
562 ArrayList<Callback> cbs = mOpModeWatchers.get(op);
563 if (cbs == null) {
564 cbs = new ArrayList<Callback>();
565 mOpModeWatchers.put(op, cbs);
566 }
567 cbs.add(cb);
568 }
569 if (packageName != null) {
570 ArrayList<Callback> cbs = mPackageModeWatchers.get(packageName);
571 if (cbs == null) {
572 cbs = new ArrayList<Callback>();
573 mPackageModeWatchers.put(packageName, cbs);
574 }
575 cbs.add(cb);
576 }
577 }
578 }
579
580 @Override
581 public void stopWatchingMode(IAppOpsCallback callback) {
582 synchronized (this) {
583 Callback cb = mModeWatchers.remove(callback.asBinder());
584 if (cb != null) {
585 cb.unlinkToDeath();
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700586 for (int i=mOpModeWatchers.size()-1; i>=0; i--) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800587 ArrayList<Callback> cbs = mOpModeWatchers.valueAt(i);
588 cbs.remove(cb);
589 if (cbs.size() <= 0) {
590 mOpModeWatchers.removeAt(i);
591 }
592 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700593 for (int i=mPackageModeWatchers.size()-1; i>=0; i--) {
594 ArrayList<Callback> cbs = mPackageModeWatchers.valueAt(i);
595 cbs.remove(cb);
596 if (cbs.size() <= 0) {
597 mPackageModeWatchers.removeAt(i);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800598 }
599 }
600 }
601 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800602 }
603
604 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700605 public IBinder getToken(IBinder clientToken) {
606 synchronized (this) {
607 ClientState cs = mClients.get(clientToken);
608 if (cs == null) {
609 cs = new ClientState(clientToken);
610 mClients.put(clientToken, cs);
611 }
612 return cs;
613 }
614 }
615
616 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -0800617 public int checkOperation(int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800618 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800619 verifyIncomingOp(code);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800620 synchronized (this) {
Jason Monk1c7c3192014-06-26 12:52:18 -0400621 if (isOpRestricted(uid, code, packageName)) {
Jason Monk62062992014-05-06 09:55:28 -0400622 return AppOpsManager.MODE_IGNORED;
623 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800624 Op op = getOpLocked(AppOpsManager.opToSwitch(code), uid, packageName, false);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800625 if (op == null) {
David Braunf5d83192013-09-16 13:43:51 -0700626 return AppOpsManager.opToDefaultMode(code);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800627 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800628 return op.mode;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800629 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800630 }
631
632 @Override
John Spurlock7b414672014-07-18 13:02:39 -0400633 public int checkAudioOperation(int code, int usage, int uid, String packageName) {
John Spurlock1af30c72014-03-10 08:33:35 -0400634 synchronized (this) {
John Spurlock7b414672014-07-18 13:02:39 -0400635 final int mode = checkRestrictionLocked(code, usage, uid, packageName);
John Spurlock1af30c72014-03-10 08:33:35 -0400636 if (mode != AppOpsManager.MODE_ALLOWED) {
637 return mode;
638 }
639 }
640 return checkOperation(code, uid, packageName);
641 }
642
John Spurlock7b414672014-07-18 13:02:39 -0400643 private int checkRestrictionLocked(int code, int usage, int uid, String packageName) {
644 final SparseArray<Restriction> usageRestrictions = mAudioRestrictions.get(code);
645 if (usageRestrictions != null) {
646 final Restriction r = usageRestrictions.get(usage);
John Spurlock1af30c72014-03-10 08:33:35 -0400647 if (r != null && !r.exceptionPackages.contains(packageName)) {
648 return r.mode;
649 }
650 }
651 return AppOpsManager.MODE_ALLOWED;
652 }
653
654 @Override
John Spurlock7b414672014-07-18 13:02:39 -0400655 public void setAudioRestriction(int code, int usage, int uid, int mode,
John Spurlock1af30c72014-03-10 08:33:35 -0400656 String[] exceptionPackages) {
657 verifyIncomingUid(uid);
658 verifyIncomingOp(code);
659 synchronized (this) {
John Spurlock7b414672014-07-18 13:02:39 -0400660 SparseArray<Restriction> usageRestrictions = mAudioRestrictions.get(code);
661 if (usageRestrictions == null) {
662 usageRestrictions = new SparseArray<Restriction>();
663 mAudioRestrictions.put(code, usageRestrictions);
John Spurlock1af30c72014-03-10 08:33:35 -0400664 }
John Spurlock7b414672014-07-18 13:02:39 -0400665 usageRestrictions.remove(usage);
John Spurlock1af30c72014-03-10 08:33:35 -0400666 if (mode != AppOpsManager.MODE_ALLOWED) {
667 final Restriction r = new Restriction();
668 r.mode = mode;
669 if (exceptionPackages != null) {
670 final int N = exceptionPackages.length;
671 r.exceptionPackages = new ArraySet<String>(N);
672 for (int i = 0; i < N; i++) {
673 final String pkg = exceptionPackages[i];
674 if (pkg != null) {
675 r.exceptionPackages.add(pkg.trim());
676 }
677 }
678 }
John Spurlock7b414672014-07-18 13:02:39 -0400679 usageRestrictions.put(usage, r);
John Spurlock1af30c72014-03-10 08:33:35 -0400680 }
681 }
682 }
683
684 @Override
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700685 public int checkPackage(int uid, String packageName) {
686 synchronized (this) {
Dianne Hackborn0fcef842014-09-12 15:38:33 -0700687 if (getOpsRawLocked(uid, packageName, true) != null) {
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700688 return AppOpsManager.MODE_ALLOWED;
689 } else {
690 return AppOpsManager.MODE_ERRORED;
691 }
692 }
693 }
694
695 @Override
Svet Ganov99b60432015-06-27 13:15:22 -0700696 public int noteProxyOperation(int code, String proxyPackageName,
697 int proxiedUid, String proxiedPackageName) {
698 verifyIncomingOp(code);
699 final int proxyMode = noteOperationUnchecked(code, Binder.getCallingUid(),
700 proxyPackageName, -1, null);
701 if (proxyMode != AppOpsManager.MODE_ALLOWED || Binder.getCallingUid() == proxiedUid) {
702 return proxyMode;
703 }
704 return noteOperationUnchecked(code, proxiedUid, proxiedPackageName,
705 Binder.getCallingUid(), proxyPackageName);
706
707 }
708
709 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800710 public int noteOperation(int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800711 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800712 verifyIncomingOp(code);
Svet Ganov99b60432015-06-27 13:15:22 -0700713 return noteOperationUnchecked(code, uid, packageName, 0, null);
714 }
715
716 private int noteOperationUnchecked(int code, int uid, String packageName,
717 int proxyUid, String proxyPackageName) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800718 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800719 Ops ops = getOpsLocked(uid, packageName, true);
720 if (ops == null) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800721 if (DEBUG) Log.d(TAG, "noteOperation: no op for code " + code + " uid " + uid
722 + " package " + packageName);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700723 return AppOpsManager.MODE_ERRORED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800724 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800725 Op op = getOpLocked(ops, code, true);
Jason Monk1c7c3192014-06-26 12:52:18 -0400726 if (isOpRestricted(uid, code, packageName)) {
Jason Monk62062992014-05-06 09:55:28 -0400727 return AppOpsManager.MODE_IGNORED;
728 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800729 if (op.duration == -1) {
730 Slog.w(TAG, "Noting op not finished: uid " + uid + " pkg " + packageName
731 + " code " + code + " time=" + op.time + " duration=" + op.duration);
732 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800733 op.duration = 0;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800734 final int switchCode = AppOpsManager.opToSwitch(code);
735 final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
736 if (switchOp.mode != AppOpsManager.MODE_ALLOWED) {
737 if (DEBUG) Log.d(TAG, "noteOperation: reject #" + op.mode + " for code "
738 + switchCode + " (" + code + ") uid " + uid + " package " + packageName);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800739 op.rejectTime = System.currentTimeMillis();
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800740 return switchOp.mode;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800741 }
742 if (DEBUG) Log.d(TAG, "noteOperation: allowing code " + code + " uid " + uid
743 + " package " + packageName);
744 op.time = System.currentTimeMillis();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800745 op.rejectTime = 0;
Svet Ganov99b60432015-06-27 13:15:22 -0700746 op.proxyUid = proxyUid;
747 op.proxyPackageName = proxyPackageName;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800748 return AppOpsManager.MODE_ALLOWED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800749 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800750 }
751
752 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700753 public int startOperation(IBinder token, int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800754 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800755 verifyIncomingOp(code);
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700756 ClientState client = (ClientState)token;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800757 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800758 Ops ops = getOpsLocked(uid, packageName, true);
759 if (ops == null) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800760 if (DEBUG) Log.d(TAG, "startOperation: no op for code " + code + " uid " + uid
761 + " package " + packageName);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700762 return AppOpsManager.MODE_ERRORED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800763 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800764 Op op = getOpLocked(ops, code, true);
Jason Monk1c7c3192014-06-26 12:52:18 -0400765 if (isOpRestricted(uid, code, packageName)) {
Jason Monk62062992014-05-06 09:55:28 -0400766 return AppOpsManager.MODE_IGNORED;
767 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800768 final int switchCode = AppOpsManager.opToSwitch(code);
769 final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
770 if (switchOp.mode != AppOpsManager.MODE_ALLOWED) {
771 if (DEBUG) Log.d(TAG, "startOperation: reject #" + op.mode + " for code "
772 + switchCode + " (" + code + ") uid " + uid + " package " + packageName);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800773 op.rejectTime = System.currentTimeMillis();
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800774 return switchOp.mode;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800775 }
776 if (DEBUG) Log.d(TAG, "startOperation: allowing code " + code + " uid " + uid
777 + " package " + packageName);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800778 if (op.nesting == 0) {
779 op.time = System.currentTimeMillis();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800780 op.rejectTime = 0;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800781 op.duration = -1;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800782 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800783 op.nesting++;
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700784 if (client.mStartedOps != null) {
785 client.mStartedOps.add(op);
786 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800787 return AppOpsManager.MODE_ALLOWED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800788 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800789 }
790
791 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700792 public void finishOperation(IBinder token, int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800793 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800794 verifyIncomingOp(code);
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700795 ClientState client = (ClientState)token;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800796 synchronized (this) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800797 Op op = getOpLocked(code, uid, packageName, true);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800798 if (op == null) {
799 return;
800 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700801 if (client.mStartedOps != null) {
802 if (!client.mStartedOps.remove(op)) {
803 throw new IllegalStateException("Operation not started: uid" + op.uid
804 + " pkg=" + op.packageName + " op=" + op.op);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800805 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800806 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700807 finishOperationLocked(op);
808 }
809 }
810
Svet Ganovb9d71a62015-04-30 10:38:13 -0700811 @Override
812 public int permissionToOpCode(String permission) {
813 return AppOpsManager.permissionToOpCode(permission);
814 }
815
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700816 void finishOperationLocked(Op op) {
817 if (op.nesting <= 1) {
818 if (op.nesting == 1) {
819 op.duration = (int)(System.currentTimeMillis() - op.time);
820 op.time += op.duration;
821 } else {
822 Slog.w(TAG, "Finishing op nesting under-run: uid " + op.uid + " pkg "
823 + op.packageName + " code " + op.op + " time=" + op.time
824 + " duration=" + op.duration + " nesting=" + op.nesting);
825 }
826 op.nesting = 0;
827 } else {
828 op.nesting--;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800829 }
830 }
831
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800832 private void verifyIncomingUid(int uid) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800833 if (uid == Binder.getCallingUid()) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800834 return;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800835 }
836 if (Binder.getCallingPid() == Process.myPid()) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800837 return;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800838 }
839 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
840 Binder.getCallingPid(), Binder.getCallingUid(), null);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800841 }
842
Dianne Hackborn961321f2013-02-05 17:22:41 -0800843 private void verifyIncomingOp(int op) {
844 if (op >= 0 && op < AppOpsManager._NUM_OP) {
845 return;
846 }
847 throw new IllegalArgumentException("Bad operation #" + op);
848 }
849
Dianne Hackborn72e39832013-01-18 18:36:09 -0800850 private Ops getOpsLocked(int uid, String packageName, boolean edit) {
Dianne Hackborn0fcef842014-09-12 15:38:33 -0700851 if (uid == 0) {
852 packageName = "root";
853 } else if (uid == Process.SHELL_UID) {
854 packageName = "com.android.shell";
855 }
856 return getOpsRawLocked(uid, packageName, edit);
857 }
858
859 private Ops getOpsRawLocked(int uid, String packageName, boolean edit) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800860 HashMap<String, Ops> pkgOps = mUidOps.get(uid);
861 if (pkgOps == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800862 if (!edit) {
863 return null;
864 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800865 pkgOps = new HashMap<String, Ops>();
866 mUidOps.put(uid, pkgOps);
867 }
868 Ops ops = pkgOps.get(packageName);
869 if (ops == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800870 if (!edit) {
871 return null;
872 }
Jason Monk1c7c3192014-06-26 12:52:18 -0400873 boolean isPrivileged = false;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800874 // This is the first time we have seen this package name under this uid,
875 // so let's make sure it is valid.
Dianne Hackborn514074f2013-02-11 10:52:46 -0800876 if (uid != 0) {
877 final long ident = Binder.clearCallingIdentity();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800878 try {
Dianne Hackborn514074f2013-02-11 10:52:46 -0800879 int pkgUid = -1;
880 try {
Jason Monk1c7c3192014-06-26 12:52:18 -0400881 ApplicationInfo appInfo = ActivityThread.getPackageManager()
882 .getApplicationInfo(packageName, 0, UserHandle.getUserId(uid));
883 if (appInfo != null) {
884 pkgUid = appInfo.uid;
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800885 isPrivileged = (appInfo.privateFlags
886 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0;
Jason Monk1c7c3192014-06-26 12:52:18 -0400887 } else {
888 if ("media".equals(packageName)) {
889 pkgUid = Process.MEDIA_UID;
890 isPrivileged = false;
891 }
Dianne Hackborn713df152013-05-17 11:27:57 -0700892 }
Jason Monk1c7c3192014-06-26 12:52:18 -0400893 } catch (RemoteException e) {
894 Slog.w(TAG, "Could not contact PackageManager", e);
Dianne Hackborn514074f2013-02-11 10:52:46 -0800895 }
896 if (pkgUid != uid) {
897 // Oops! The package name is not valid for the uid they are calling
898 // under. Abort.
899 Slog.w(TAG, "Bad call: specified package " + packageName
900 + " under uid " + uid + " but it is really " + pkgUid);
901 return null;
902 }
903 } finally {
904 Binder.restoreCallingIdentity(ident);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800905 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800906 }
Jason Monk1c7c3192014-06-26 12:52:18 -0400907 ops = new Ops(packageName, uid, isPrivileged);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800908 pkgOps.put(packageName, ops);
909 }
Dianne Hackborn72e39832013-01-18 18:36:09 -0800910 return ops;
911 }
912
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800913 private void scheduleWriteLocked() {
914 if (!mWriteScheduled) {
915 mWriteScheduled = true;
916 mHandler.postDelayed(mWriteRunner, WRITE_DELAY);
917 }
918 }
919
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800920 private void scheduleFastWriteLocked() {
921 if (!mFastWriteScheduled) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800922 mWriteScheduled = true;
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800923 mFastWriteScheduled = true;
924 mHandler.removeCallbacks(mWriteRunner);
925 mHandler.postDelayed(mWriteRunner, 10*1000);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800926 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800927 }
928
Dianne Hackborn72e39832013-01-18 18:36:09 -0800929 private Op getOpLocked(int code, int uid, String packageName, boolean edit) {
930 Ops ops = getOpsLocked(uid, packageName, edit);
931 if (ops == null) {
932 return null;
933 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800934 return getOpLocked(ops, code, edit);
935 }
936
937 private Op getOpLocked(Ops ops, int code, boolean edit) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800938 Op op = ops.get(code);
939 if (op == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800940 if (!edit) {
941 return null;
942 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700943 op = new Op(ops.uid, ops.packageName, code);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800944 ops.put(code, op);
945 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800946 if (edit) {
947 scheduleWriteLocked();
Dianne Hackborn35654b62013-01-14 17:38:02 -0800948 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800949 return op;
950 }
951
Jason Monk1c7c3192014-06-26 12:52:18 -0400952 private boolean isOpRestricted(int uid, int code, String packageName) {
Jason Monk62062992014-05-06 09:55:28 -0400953 int userHandle = UserHandle.getUserId(uid);
954 boolean[] opRestrictions = mOpRestrictions.get(userHandle);
955 if ((opRestrictions != null) && opRestrictions[code]) {
Jason Monk1c7c3192014-06-26 12:52:18 -0400956 if (AppOpsManager.opAllowSystemBypassRestriction(code)) {
957 synchronized (this) {
958 Ops ops = getOpsLocked(uid, packageName, true);
959 if ((ops != null) && ops.isPrivileged) {
960 return false;
961 }
962 }
963 }
Julia Reynolds401de172014-07-24 18:21:29 -0400964 return true;
Jason Monk62062992014-05-06 09:55:28 -0400965 }
966 return false;
967 }
968
Dianne Hackborn35654b62013-01-14 17:38:02 -0800969 void readState() {
970 synchronized (mFile) {
971 synchronized (this) {
972 FileInputStream stream;
973 try {
974 stream = mFile.openRead();
975 } catch (FileNotFoundException e) {
976 Slog.i(TAG, "No existing app ops " + mFile.getBaseFile() + "; starting empty");
977 return;
978 }
979 boolean success = false;
980 try {
981 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100982 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackborn35654b62013-01-14 17:38:02 -0800983 int type;
984 while ((type = parser.next()) != XmlPullParser.START_TAG
985 && type != XmlPullParser.END_DOCUMENT) {
986 ;
987 }
988
989 if (type != XmlPullParser.START_TAG) {
990 throw new IllegalStateException("no start tag found");
991 }
992
993 int outerDepth = parser.getDepth();
994 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
995 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
996 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
997 continue;
998 }
999
1000 String tagName = parser.getName();
1001 if (tagName.equals("pkg")) {
Dave Burke0997c5bd2013-08-02 20:25:02 +00001002 readPackage(parser);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001003 } else {
1004 Slog.w(TAG, "Unknown element under <app-ops>: "
1005 + parser.getName());
1006 XmlUtils.skipCurrentTag(parser);
1007 }
1008 }
1009 success = true;
1010 } catch (IllegalStateException e) {
1011 Slog.w(TAG, "Failed parsing " + e);
1012 } catch (NullPointerException e) {
1013 Slog.w(TAG, "Failed parsing " + e);
1014 } catch (NumberFormatException e) {
1015 Slog.w(TAG, "Failed parsing " + e);
1016 } catch (XmlPullParserException e) {
1017 Slog.w(TAG, "Failed parsing " + e);
1018 } catch (IOException e) {
1019 Slog.w(TAG, "Failed parsing " + e);
1020 } catch (IndexOutOfBoundsException e) {
1021 Slog.w(TAG, "Failed parsing " + e);
1022 } finally {
1023 if (!success) {
1024 mUidOps.clear();
1025 }
1026 try {
1027 stream.close();
1028 } catch (IOException e) {
1029 }
1030 }
1031 }
1032 }
1033 }
1034
Dave Burke0997c5bd2013-08-02 20:25:02 +00001035 void readPackage(XmlPullParser parser) throws NumberFormatException,
Dianne Hackborn35654b62013-01-14 17:38:02 -08001036 XmlPullParserException, IOException {
1037 String pkgName = parser.getAttributeValue(null, "n");
1038 int outerDepth = parser.getDepth();
1039 int type;
1040 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1041 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1042 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1043 continue;
1044 }
1045
1046 String tagName = parser.getName();
1047 if (tagName.equals("uid")) {
Dave Burke0997c5bd2013-08-02 20:25:02 +00001048 readUid(parser, pkgName);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001049 } else {
1050 Slog.w(TAG, "Unknown element under <pkg>: "
1051 + parser.getName());
1052 XmlUtils.skipCurrentTag(parser);
1053 }
1054 }
1055 }
1056
Dave Burke0997c5bd2013-08-02 20:25:02 +00001057 void readUid(XmlPullParser parser, String pkgName) throws NumberFormatException,
Dianne Hackborn35654b62013-01-14 17:38:02 -08001058 XmlPullParserException, IOException {
1059 int uid = Integer.parseInt(parser.getAttributeValue(null, "n"));
Jason Monk1c7c3192014-06-26 12:52:18 -04001060 String isPrivilegedString = parser.getAttributeValue(null, "p");
1061 boolean isPrivileged = false;
1062 if (isPrivilegedString == null) {
1063 try {
1064 IPackageManager packageManager = ActivityThread.getPackageManager();
1065 if (packageManager != null) {
1066 ApplicationInfo appInfo = ActivityThread.getPackageManager()
1067 .getApplicationInfo(pkgName, 0, UserHandle.getUserId(uid));
1068 if (appInfo != null) {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001069 isPrivileged = (appInfo.privateFlags
1070 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0;
Jason Monk1c7c3192014-06-26 12:52:18 -04001071 }
1072 } else {
1073 // Could not load data, don't add to cache so it will be loaded later.
1074 return;
1075 }
1076 } catch (RemoteException e) {
1077 Slog.w(TAG, "Could not contact PackageManager", e);
1078 }
1079 } else {
1080 isPrivileged = Boolean.parseBoolean(isPrivilegedString);
1081 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001082 int outerDepth = parser.getDepth();
1083 int type;
1084 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1085 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1086 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1087 continue;
1088 }
1089
1090 String tagName = parser.getName();
1091 if (tagName.equals("op")) {
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001092 Op op = new Op(uid, pkgName, Integer.parseInt(parser.getAttributeValue(null, "n")));
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001093 String mode = parser.getAttributeValue(null, "m");
1094 if (mode != null) {
Dave Burke0997c5bd2013-08-02 20:25:02 +00001095 op.mode = Integer.parseInt(mode);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001096 }
1097 String time = parser.getAttributeValue(null, "t");
1098 if (time != null) {
1099 op.time = Long.parseLong(time);
1100 }
1101 time = parser.getAttributeValue(null, "r");
1102 if (time != null) {
1103 op.rejectTime = Long.parseLong(time);
1104 }
1105 String dur = parser.getAttributeValue(null, "d");
1106 if (dur != null) {
1107 op.duration = Integer.parseInt(dur);
1108 }
Svet Ganov99b60432015-06-27 13:15:22 -07001109 String proxyUid = parser.getAttributeValue(null, "pu");
1110 if (proxyUid != null) {
1111 op.proxyUid = Integer.parseInt(proxyUid);
1112 }
1113 String proxyPackageName = parser.getAttributeValue(null, "pp");
1114 if (proxyPackageName != null) {
1115 op.proxyPackageName = proxyPackageName;
1116 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001117 HashMap<String, Ops> pkgOps = mUidOps.get(uid);
1118 if (pkgOps == null) {
1119 pkgOps = new HashMap<String, Ops>();
1120 mUidOps.put(uid, pkgOps);
1121 }
1122 Ops ops = pkgOps.get(pkgName);
1123 if (ops == null) {
Jason Monk1c7c3192014-06-26 12:52:18 -04001124 ops = new Ops(pkgName, uid, isPrivileged);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001125 pkgOps.put(pkgName, ops);
1126 }
1127 ops.put(op.op, op);
1128 } else {
1129 Slog.w(TAG, "Unknown element under <pkg>: "
1130 + parser.getName());
1131 XmlUtils.skipCurrentTag(parser);
1132 }
1133 }
1134 }
1135
1136 void writeState() {
1137 synchronized (mFile) {
1138 List<AppOpsManager.PackageOps> allOps = getPackagesForOps(null);
1139
1140 FileOutputStream stream;
1141 try {
1142 stream = mFile.startWrite();
1143 } catch (IOException e) {
1144 Slog.w(TAG, "Failed to write state: " + e);
1145 return;
1146 }
1147
1148 try {
1149 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001150 out.setOutput(stream, StandardCharsets.UTF_8.name());
Dianne Hackborn35654b62013-01-14 17:38:02 -08001151 out.startDocument(null, true);
1152 out.startTag(null, "app-ops");
1153
1154 if (allOps != null) {
1155 String lastPkg = null;
1156 for (int i=0; i<allOps.size(); i++) {
1157 AppOpsManager.PackageOps pkg = allOps.get(i);
1158 if (!pkg.getPackageName().equals(lastPkg)) {
1159 if (lastPkg != null) {
1160 out.endTag(null, "pkg");
1161 }
1162 lastPkg = pkg.getPackageName();
1163 out.startTag(null, "pkg");
1164 out.attribute(null, "n", lastPkg);
1165 }
1166 out.startTag(null, "uid");
1167 out.attribute(null, "n", Integer.toString(pkg.getUid()));
Jason Monk1c7c3192014-06-26 12:52:18 -04001168 synchronized (this) {
1169 Ops ops = getOpsLocked(pkg.getUid(), pkg.getPackageName(), false);
1170 // Should always be present as the list of PackageOps is generated
1171 // from Ops.
1172 if (ops != null) {
1173 out.attribute(null, "p", Boolean.toString(ops.isPrivileged));
1174 } else {
1175 out.attribute(null, "p", Boolean.toString(false));
1176 }
1177 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001178 List<AppOpsManager.OpEntry> ops = pkg.getOps();
1179 for (int j=0; j<ops.size(); j++) {
1180 AppOpsManager.OpEntry op = ops.get(j);
1181 out.startTag(null, "op");
1182 out.attribute(null, "n", Integer.toString(op.getOp()));
David Braunf5d83192013-09-16 13:43:51 -07001183 if (op.getMode() != AppOpsManager.opToDefaultMode(op.getOp())) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001184 out.attribute(null, "m", Integer.toString(op.getMode()));
1185 }
1186 long time = op.getTime();
1187 if (time != 0) {
1188 out.attribute(null, "t", Long.toString(time));
1189 }
1190 time = op.getRejectTime();
1191 if (time != 0) {
1192 out.attribute(null, "r", Long.toString(time));
1193 }
1194 int dur = op.getDuration();
1195 if (dur != 0) {
1196 out.attribute(null, "d", Integer.toString(dur));
1197 }
Svet Ganov99b60432015-06-27 13:15:22 -07001198 int proxyUid = op.getProxyUid();
1199 if (proxyUid != -1) {
1200 out.attribute(null, "pu", Integer.toString(proxyUid));
1201 }
1202 String proxyPackageName = op.getProxyPackageName();
1203 if (proxyPackageName != null) {
1204 out.attribute(null, "pp", proxyPackageName);
1205 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001206 out.endTag(null, "op");
1207 }
1208 out.endTag(null, "uid");
1209 }
1210 if (lastPkg != null) {
1211 out.endTag(null, "pkg");
1212 }
1213 }
1214
1215 out.endTag(null, "app-ops");
1216 out.endDocument();
1217 mFile.finishWrite(stream);
1218 } catch (IOException e) {
1219 Slog.w(TAG, "Failed to write state, restoring backup.", e);
1220 mFile.failWrite(stream);
1221 }
1222 }
1223 }
1224
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001225 @Override
1226 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1227 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1228 != PackageManager.PERMISSION_GRANTED) {
1229 pw.println("Permission Denial: can't dump ApOps service from from pid="
1230 + Binder.getCallingPid()
1231 + ", uid=" + Binder.getCallingUid());
1232 return;
1233 }
1234
1235 synchronized (this) {
1236 pw.println("Current AppOps Service state:");
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001237 final long now = System.currentTimeMillis();
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001238 boolean needSep = false;
1239 if (mOpModeWatchers.size() > 0) {
1240 needSep = true;
1241 pw.println(" Op mode watchers:");
1242 for (int i=0; i<mOpModeWatchers.size(); i++) {
1243 pw.print(" Op "); pw.print(AppOpsManager.opToName(mOpModeWatchers.keyAt(i)));
1244 pw.println(":");
1245 ArrayList<Callback> callbacks = mOpModeWatchers.valueAt(i);
1246 for (int j=0; j<callbacks.size(); j++) {
1247 pw.print(" #"); pw.print(j); pw.print(": ");
1248 pw.println(callbacks.get(j));
1249 }
1250 }
1251 }
1252 if (mPackageModeWatchers.size() > 0) {
1253 needSep = true;
1254 pw.println(" Package mode watchers:");
1255 for (int i=0; i<mPackageModeWatchers.size(); i++) {
1256 pw.print(" Pkg "); pw.print(mPackageModeWatchers.keyAt(i));
1257 pw.println(":");
1258 ArrayList<Callback> callbacks = mPackageModeWatchers.valueAt(i);
1259 for (int j=0; j<callbacks.size(); j++) {
1260 pw.print(" #"); pw.print(j); pw.print(": ");
1261 pw.println(callbacks.get(j));
1262 }
1263 }
1264 }
1265 if (mModeWatchers.size() > 0) {
1266 needSep = true;
1267 pw.println(" All mode watchers:");
1268 for (int i=0; i<mModeWatchers.size(); i++) {
1269 pw.print(" "); pw.print(mModeWatchers.keyAt(i));
1270 pw.print(" -> "); pw.println(mModeWatchers.valueAt(i));
1271 }
1272 }
1273 if (mClients.size() > 0) {
1274 needSep = true;
1275 pw.println(" Clients:");
1276 for (int i=0; i<mClients.size(); i++) {
1277 pw.print(" "); pw.print(mClients.keyAt(i)); pw.println(":");
1278 ClientState cs = mClients.valueAt(i);
1279 pw.print(" "); pw.println(cs);
1280 if (cs.mStartedOps != null && cs.mStartedOps.size() > 0) {
1281 pw.println(" Started ops:");
1282 for (int j=0; j<cs.mStartedOps.size(); j++) {
1283 Op op = cs.mStartedOps.get(j);
1284 pw.print(" "); pw.print("uid="); pw.print(op.uid);
1285 pw.print(" pkg="); pw.print(op.packageName);
1286 pw.print(" op="); pw.println(AppOpsManager.opToName(op.op));
1287 }
1288 }
1289 }
1290 }
John Spurlock1af30c72014-03-10 08:33:35 -04001291 if (mAudioRestrictions.size() > 0) {
1292 boolean printedHeader = false;
1293 for (int o=0; o<mAudioRestrictions.size(); o++) {
1294 final String op = AppOpsManager.opToName(mAudioRestrictions.keyAt(o));
1295 final SparseArray<Restriction> restrictions = mAudioRestrictions.valueAt(o);
1296 for (int i=0; i<restrictions.size(); i++) {
1297 if (!printedHeader){
1298 pw.println(" Audio Restrictions:");
1299 printedHeader = true;
1300 needSep = true;
1301 }
John Spurlock7b414672014-07-18 13:02:39 -04001302 final int usage = restrictions.keyAt(i);
John Spurlock1af30c72014-03-10 08:33:35 -04001303 pw.print(" "); pw.print(op);
John Spurlock7b414672014-07-18 13:02:39 -04001304 pw.print(" usage="); pw.print(AudioAttributes.usageToString(usage));
John Spurlock1af30c72014-03-10 08:33:35 -04001305 Restriction r = restrictions.valueAt(i);
1306 pw.print(": mode="); pw.println(r.mode);
1307 if (!r.exceptionPackages.isEmpty()) {
1308 pw.println(" Exceptions:");
1309 for (int j=0; j<r.exceptionPackages.size(); j++) {
1310 pw.print(" "); pw.println(r.exceptionPackages.valueAt(j));
1311 }
1312 }
1313 }
1314 }
1315 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001316 if (needSep) {
1317 pw.println();
1318 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001319 for (int i=0; i<mUidOps.size(); i++) {
1320 pw.print(" Uid "); UserHandle.formatUid(pw, mUidOps.keyAt(i)); pw.println(":");
1321 HashMap<String, Ops> pkgOps = mUidOps.valueAt(i);
1322 for (Ops ops : pkgOps.values()) {
1323 pw.print(" Package "); pw.print(ops.packageName); pw.println(":");
1324 for (int j=0; j<ops.size(); j++) {
1325 Op op = ops.valueAt(j);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001326 pw.print(" "); pw.print(AppOpsManager.opToName(op.op));
1327 pw.print(": mode="); pw.print(op.mode);
1328 if (op.time != 0) {
1329 pw.print("; time="); TimeUtils.formatDuration(now-op.time, pw);
1330 pw.print(" ago");
1331 }
1332 if (op.rejectTime != 0) {
1333 pw.print("; rejectTime="); TimeUtils.formatDuration(now-op.rejectTime, pw);
1334 pw.print(" ago");
1335 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001336 if (op.duration == -1) {
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -08001337 pw.print(" (running)");
1338 } else if (op.duration != 0) {
1339 pw.print("; duration="); TimeUtils.formatDuration(op.duration, pw);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001340 }
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -08001341 pw.println();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001342 }
1343 }
1344 }
1345 }
1346 }
John Spurlock1af30c72014-03-10 08:33:35 -04001347
1348 private static final class Restriction {
1349 private static final ArraySet<String> NO_EXCEPTIONS = new ArraySet<String>();
1350 int mode;
1351 ArraySet<String> exceptionPackages = NO_EXCEPTIONS;
1352 }
Jason Monk62062992014-05-06 09:55:28 -04001353
1354 @Override
Jason Monk62062992014-05-06 09:55:28 -04001355 public void setUserRestrictions(Bundle restrictions, int userHandle) throws RemoteException {
1356 checkSystemUid("setUserRestrictions");
1357 boolean[] opRestrictions = mOpRestrictions.get(userHandle);
1358 if (opRestrictions == null) {
1359 opRestrictions = new boolean[AppOpsManager._NUM_OP];
1360 mOpRestrictions.put(userHandle, opRestrictions);
1361 }
1362 for (int i = 0; i < opRestrictions.length; ++i) {
1363 String restriction = AppOpsManager.opToRestriction(i);
1364 if (restriction != null) {
1365 opRestrictions[i] = restrictions.getBoolean(restriction, false);
1366 } else {
1367 opRestrictions[i] = false;
1368 }
1369 }
1370 }
1371
1372 @Override
1373 public void removeUser(int userHandle) throws RemoteException {
1374 checkSystemUid("removeUser");
1375 mOpRestrictions.remove(userHandle);
Jason Monk62062992014-05-06 09:55:28 -04001376 }
1377
1378 private void checkSystemUid(String function) {
1379 int uid = Binder.getCallingUid();
1380 if (uid != Process.SYSTEM_UID) {
1381 throw new SecurityException(function + " must by called by the system");
1382 }
1383 }
1384
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001385}