blob: e26747cfc582b31d95bdc596c2ac669c606c1aac [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;
Dianne Hackborn35654b62013-01-14 17:38:02 -080026import java.util.ArrayList;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080027import java.util.HashMap;
Dianne Hackbornc2293022013-02-06 23:14:49 -080028import java.util.Iterator;
Dianne Hackborn35654b62013-01-14 17:38:02 -080029import java.util.List;
Dianne Hackborn607b4142013-08-02 18:10:10 -070030import java.util.Map;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080031
32import android.app.AppOpsManager;
33import android.content.Context;
34import android.content.pm.PackageManager;
35import android.content.pm.PackageManager.NameNotFoundException;
John Spurlock1af30c72014-03-10 08:33:35 -040036import android.media.AudioService;
Dianne Hackborn35654b62013-01-14 17:38:02 -080037import android.os.AsyncTask;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080038import android.os.Binder;
Dianne Hackborn35654b62013-01-14 17:38:02 -080039import android.os.Handler;
Dianne Hackbornc2293022013-02-06 23:14:49 -080040import android.os.IBinder;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080041import android.os.Process;
Dianne Hackbornc2293022013-02-06 23:14:49 -080042import android.os.RemoteException;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080043import android.os.ServiceManager;
44import android.os.UserHandle;
Dianne Hackborne98f5db2013-07-17 17:23:25 -070045import android.util.ArrayMap;
John Spurlock1af30c72014-03-10 08:33:35 -040046import android.util.ArraySet;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080047import android.util.AtomicFile;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -080048import android.util.Log;
Dianne Hackborn607b4142013-08-02 18:10:10 -070049import android.util.Pair;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080050import android.util.Slog;
51import android.util.SparseArray;
52import android.util.TimeUtils;
Dianne Hackborn35654b62013-01-14 17:38:02 -080053import android.util.Xml;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080054
55import com.android.internal.app.IAppOpsService;
Dianne Hackbornc2293022013-02-06 23:14:49 -080056import com.android.internal.app.IAppOpsCallback;
Dianne Hackborn35654b62013-01-14 17:38:02 -080057import com.android.internal.util.FastXmlSerializer;
58import com.android.internal.util.XmlUtils;
59
60import org.xmlpull.v1.XmlPullParser;
61import org.xmlpull.v1.XmlPullParserException;
62import org.xmlpull.v1.XmlSerializer;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080063
64public class AppOpsService extends IAppOpsService.Stub {
65 static final String TAG = "AppOps";
Dianne Hackborn35654b62013-01-14 17:38:02 -080066 static final boolean DEBUG = false;
67
68 // Write at most every 30 minutes.
69 static final long WRITE_DELAY = DEBUG ? 1000 : 30*60*1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080070
71 Context mContext;
72 final AtomicFile mFile;
Dianne Hackborn35654b62013-01-14 17:38:02 -080073 final Handler mHandler;
74
75 boolean mWriteScheduled;
76 final Runnable mWriteRunner = new Runnable() {
77 public void run() {
78 synchronized (AppOpsService.this) {
79 mWriteScheduled = false;
80 AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
81 @Override protected Void doInBackground(Void... params) {
82 writeState();
83 return null;
84 }
85 };
86 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
87 }
88 }
89 };
Dianne Hackborna06de0f2012-12-11 16:34:47 -080090
91 final SparseArray<HashMap<String, Ops>> mUidOps
92 = new SparseArray<HashMap<String, Ops>>();
93
Dianne Hackbornc2293022013-02-06 23:14:49 -080094 public final static class Ops extends SparseArray<Op> {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080095 public final String packageName;
Dianne Hackborn35654b62013-01-14 17:38:02 -080096 public final int uid;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080097
Dianne Hackborn35654b62013-01-14 17:38:02 -080098 public Ops(String _packageName, int _uid) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080099 packageName = _packageName;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800100 uid = _uid;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800101 }
102 }
103
Dianne Hackbornc2293022013-02-06 23:14:49 -0800104 public final static class Op {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700105 public final int uid;
106 public final String packageName;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800107 public final int op;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800108 public int mode;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800109 public int duration;
110 public long time;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800111 public long rejectTime;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800112 public int nesting;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800113
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700114 public Op(int _uid, String _packageName, int _op) {
115 uid = _uid;
116 packageName = _packageName;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800117 op = _op;
David Braunf5d83192013-09-16 13:43:51 -0700118 mode = AppOpsManager.opToDefaultMode(op);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800119 }
120 }
121
Dianne Hackbornc2293022013-02-06 23:14:49 -0800122 final SparseArray<ArrayList<Callback>> mOpModeWatchers
123 = new SparseArray<ArrayList<Callback>>();
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700124 final ArrayMap<String, ArrayList<Callback>> mPackageModeWatchers
125 = new ArrayMap<String, ArrayList<Callback>>();
126 final ArrayMap<IBinder, Callback> mModeWatchers
127 = new ArrayMap<IBinder, Callback>();
John Spurlock1af30c72014-03-10 08:33:35 -0400128 final SparseArray<SparseArray<Restriction>> mAudioRestrictions
129 = new SparseArray<SparseArray<Restriction>>();
Dianne Hackbornc2293022013-02-06 23:14:49 -0800130
131 public final class Callback implements DeathRecipient {
132 final IAppOpsCallback mCallback;
133
134 public Callback(IAppOpsCallback callback) {
135 mCallback = callback;
136 try {
137 mCallback.asBinder().linkToDeath(this, 0);
138 } catch (RemoteException e) {
139 }
140 }
141
142 public void unlinkToDeath() {
143 mCallback.asBinder().unlinkToDeath(this, 0);
144 }
145
146 @Override
147 public void binderDied() {
148 stopWatchingMode(mCallback);
149 }
150 }
151
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700152 final ArrayMap<IBinder, ClientState> mClients = new ArrayMap<IBinder, ClientState>();
153
154 public final class ClientState extends Binder implements DeathRecipient {
155 final IBinder mAppToken;
156 final int mPid;
157 final ArrayList<Op> mStartedOps;
158
159 public ClientState(IBinder appToken) {
160 mAppToken = appToken;
161 mPid = Binder.getCallingPid();
162 if (appToken instanceof Binder) {
163 // For local clients, there is no reason to track them.
164 mStartedOps = null;
165 } else {
166 mStartedOps = new ArrayList<Op>();
167 try {
168 mAppToken.linkToDeath(this, 0);
169 } catch (RemoteException e) {
170 }
171 }
172 }
173
174 @Override
175 public String toString() {
176 return "ClientState{" +
177 "mAppToken=" + mAppToken +
178 ", " + (mStartedOps != null ? ("pid=" + mPid) : "local") +
179 '}';
180 }
181
182 @Override
183 public void binderDied() {
184 synchronized (AppOpsService.this) {
185 for (int i=mStartedOps.size()-1; i>=0; i--) {
186 finishOperationLocked(mStartedOps.get(i));
187 }
188 mClients.remove(mAppToken);
189 }
190 }
191 }
192
Jeff Brown6f357d32014-01-15 20:40:55 -0800193 public AppOpsService(File storagePath, Handler handler) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800194 mFile = new AtomicFile(storagePath);
Jeff Brown6f357d32014-01-15 20:40:55 -0800195 mHandler = handler;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800196 readState();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800197 }
David Braunf5d83192013-09-16 13:43:51 -0700198
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800199 public void publish(Context context) {
200 mContext = context;
201 ServiceManager.addService(Context.APP_OPS_SERVICE, asBinder());
202 }
203
Dianne Hackborn514074f2013-02-11 10:52:46 -0800204 public void systemReady() {
205 synchronized (this) {
206 boolean changed = false;
207 for (int i=0; i<mUidOps.size(); i++) {
208 HashMap<String, Ops> pkgs = mUidOps.valueAt(i);
209 Iterator<Ops> it = pkgs.values().iterator();
210 while (it.hasNext()) {
211 Ops ops = it.next();
212 int curUid;
213 try {
214 curUid = mContext.getPackageManager().getPackageUid(ops.packageName,
215 UserHandle.getUserId(ops.uid));
216 } catch (NameNotFoundException e) {
217 curUid = -1;
218 }
219 if (curUid != ops.uid) {
220 Slog.i(TAG, "Pruning old package " + ops.packageName
221 + "/" + ops.uid + ": new uid=" + curUid);
222 it.remove();
223 changed = true;
224 }
225 }
226 if (pkgs.size() <= 0) {
227 mUidOps.removeAt(i);
228 }
229 }
230 if (changed) {
231 scheduleWriteLocked();
232 }
233 }
234 }
235
236 public void packageRemoved(int uid, String packageName) {
237 synchronized (this) {
238 HashMap<String, Ops> pkgs = mUidOps.get(uid);
239 if (pkgs != null) {
240 if (pkgs.remove(packageName) != null) {
241 if (pkgs.size() <= 0) {
242 mUidOps.remove(uid);
243 }
244 scheduleWriteLocked();
245 }
246 }
247 }
248 }
249
250 public void uidRemoved(int uid) {
251 synchronized (this) {
252 if (mUidOps.indexOfKey(uid) >= 0) {
253 mUidOps.remove(uid);
254 scheduleWriteLocked();
255 }
256 }
257 }
258
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800259 public void shutdown() {
260 Slog.w(TAG, "Writing app ops before shutdown...");
Dianne Hackborn35654b62013-01-14 17:38:02 -0800261 boolean doWrite = false;
262 synchronized (this) {
263 if (mWriteScheduled) {
264 mWriteScheduled = false;
265 doWrite = true;
266 }
267 }
268 if (doWrite) {
269 writeState();
270 }
271 }
272
Dianne Hackborn72e39832013-01-18 18:36:09 -0800273 private ArrayList<AppOpsManager.OpEntry> collectOps(Ops pkgOps, int[] ops) {
274 ArrayList<AppOpsManager.OpEntry> resOps = null;
275 if (ops == null) {
276 resOps = new ArrayList<AppOpsManager.OpEntry>();
277 for (int j=0; j<pkgOps.size(); j++) {
278 Op curOp = pkgOps.valueAt(j);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800279 resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time,
280 curOp.rejectTime, curOp.duration));
Dianne Hackborn72e39832013-01-18 18:36:09 -0800281 }
282 } else {
283 for (int j=0; j<ops.length; j++) {
284 Op curOp = pkgOps.get(ops[j]);
285 if (curOp != null) {
286 if (resOps == null) {
287 resOps = new ArrayList<AppOpsManager.OpEntry>();
288 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800289 resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time,
290 curOp.rejectTime, curOp.duration));
Dianne Hackborn72e39832013-01-18 18:36:09 -0800291 }
292 }
293 }
294 return resOps;
295 }
296
Dianne Hackborn35654b62013-01-14 17:38:02 -0800297 @Override
298 public List<AppOpsManager.PackageOps> getPackagesForOps(int[] ops) {
299 mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS,
300 Binder.getCallingPid(), Binder.getCallingUid(), null);
301 ArrayList<AppOpsManager.PackageOps> res = null;
302 synchronized (this) {
303 for (int i=0; i<mUidOps.size(); i++) {
304 HashMap<String, Ops> packages = mUidOps.valueAt(i);
305 for (Ops pkgOps : packages.values()) {
Dianne Hackborn72e39832013-01-18 18:36:09 -0800306 ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800307 if (resOps != null) {
308 if (res == null) {
309 res = new ArrayList<AppOpsManager.PackageOps>();
310 }
311 AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps(
312 pkgOps.packageName, pkgOps.uid, resOps);
313 res.add(resPackage);
314 }
315 }
316 }
317 }
318 return res;
319 }
320
321 @Override
Dianne Hackborn72e39832013-01-18 18:36:09 -0800322 public List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName,
323 int[] ops) {
324 mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS,
325 Binder.getCallingPid(), Binder.getCallingUid(), null);
326 synchronized (this) {
327 Ops pkgOps = getOpsLocked(uid, packageName, false);
328 if (pkgOps == null) {
329 return null;
330 }
331 ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops);
332 if (resOps == null) {
333 return null;
334 }
335 ArrayList<AppOpsManager.PackageOps> res = new ArrayList<AppOpsManager.PackageOps>();
336 AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps(
337 pkgOps.packageName, pkgOps.uid, resOps);
338 res.add(resPackage);
339 return res;
340 }
341 }
342
Dianne Hackborn607b4142013-08-02 18:10:10 -0700343 private void pruneOp(Op op, int uid, String packageName) {
344 if (op.time == 0 && op.rejectTime == 0) {
345 Ops ops = getOpsLocked(uid, packageName, false);
346 if (ops != null) {
347 ops.remove(op.op);
348 if (ops.size() <= 0) {
349 HashMap<String, Ops> pkgOps = mUidOps.get(uid);
350 if (pkgOps != null) {
351 pkgOps.remove(ops.packageName);
352 if (pkgOps.size() <= 0) {
353 mUidOps.remove(uid);
354 }
355 }
356 }
357 }
358 }
359 }
360
Dianne Hackborn72e39832013-01-18 18:36:09 -0800361 @Override
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800362 public void setMode(int code, int uid, String packageName, int mode) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800363 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800364 verifyIncomingOp(code);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800365 ArrayList<Callback> repCbs = null;
366 code = AppOpsManager.opToSwitch(code);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800367 synchronized (this) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800368 Op op = getOpLocked(code, uid, packageName, true);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800369 if (op != null) {
370 if (op.mode != mode) {
371 op.mode = mode;
Dianne Hackbornc2293022013-02-06 23:14:49 -0800372 ArrayList<Callback> cbs = mOpModeWatchers.get(code);
373 if (cbs != null) {
374 if (repCbs == null) {
375 repCbs = new ArrayList<Callback>();
376 }
377 repCbs.addAll(cbs);
378 }
379 cbs = mPackageModeWatchers.get(packageName);
380 if (cbs != null) {
381 if (repCbs == null) {
382 repCbs = new ArrayList<Callback>();
383 }
384 repCbs.addAll(cbs);
385 }
David Braunf5d83192013-09-16 13:43:51 -0700386 if (mode == AppOpsManager.opToDefaultMode(op.op)) {
Dianne Hackborn514074f2013-02-11 10:52:46 -0800387 // If going into the default mode, prune this op
388 // if there is nothing else interesting in it.
Dianne Hackborn607b4142013-08-02 18:10:10 -0700389 pruneOp(op, uid, packageName);
Dianne Hackborn514074f2013-02-11 10:52:46 -0800390 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800391 scheduleWriteNowLocked();
392 }
393 }
394 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800395 if (repCbs != null) {
396 for (int i=0; i<repCbs.size(); i++) {
397 try {
398 repCbs.get(i).mCallback.opChanged(code, packageName);
399 } catch (RemoteException e) {
400 }
401 }
402 }
403 }
404
Dianne Hackborn607b4142013-08-02 18:10:10 -0700405 private static HashMap<Callback, ArrayList<Pair<String, Integer>>> addCallbacks(
406 HashMap<Callback, ArrayList<Pair<String, Integer>>> callbacks,
407 String packageName, int op, ArrayList<Callback> cbs) {
408 if (cbs == null) {
409 return callbacks;
410 }
411 if (callbacks == null) {
412 callbacks = new HashMap<Callback, ArrayList<Pair<String, Integer>>>();
413 }
414 for (int i=0; i<cbs.size(); i++) {
415 Callback cb = cbs.get(i);
416 ArrayList<Pair<String, Integer>> reports = callbacks.get(cb);
417 if (reports == null) {
418 reports = new ArrayList<Pair<String, Integer>>();
419 callbacks.put(cb, reports);
420 }
421 reports.add(new Pair<String, Integer>(packageName, op));
422 }
423 return callbacks;
424 }
425
426 @Override
427 public void resetAllModes() {
428 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
429 Binder.getCallingPid(), Binder.getCallingUid(), null);
430 HashMap<Callback, ArrayList<Pair<String, Integer>>> callbacks = null;
431 synchronized (this) {
432 boolean changed = false;
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700433 for (int i=mUidOps.size()-1; i>=0; i--) {
Dianne Hackborn607b4142013-08-02 18:10:10 -0700434 HashMap<String, Ops> packages = mUidOps.valueAt(i);
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700435 Iterator<Map.Entry<String, Ops>> it = packages.entrySet().iterator();
436 while (it.hasNext()) {
437 Map.Entry<String, Ops> ent = it.next();
Dianne Hackborn607b4142013-08-02 18:10:10 -0700438 String packageName = ent.getKey();
439 Ops pkgOps = ent.getValue();
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700440 for (int j=pkgOps.size()-1; j>=0; j--) {
Dianne Hackborn607b4142013-08-02 18:10:10 -0700441 Op curOp = pkgOps.valueAt(j);
Dianne Hackborn8828d3a2013-09-25 16:47:10 -0700442 if (AppOpsManager.opAllowsReset(curOp.op)
443 && curOp.mode != AppOpsManager.opToDefaultMode(curOp.op)) {
David Braunf5d83192013-09-16 13:43:51 -0700444 curOp.mode = AppOpsManager.opToDefaultMode(curOp.op);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700445 changed = true;
446 callbacks = addCallbacks(callbacks, packageName, curOp.op,
447 mOpModeWatchers.get(curOp.op));
448 callbacks = addCallbacks(callbacks, packageName, curOp.op,
449 mPackageModeWatchers.get(packageName));
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700450 if (curOp.time == 0 && curOp.rejectTime == 0) {
451 pkgOps.removeAt(j);
452 }
Dianne Hackborn607b4142013-08-02 18:10:10 -0700453 }
454 }
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700455 if (pkgOps.size() == 0) {
456 it.remove();
457 }
458 }
459 if (packages.size() == 0) {
460 mUidOps.removeAt(i);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700461 }
462 }
463 if (changed) {
464 scheduleWriteNowLocked();
465 }
466 }
467 if (callbacks != null) {
468 for (Map.Entry<Callback, ArrayList<Pair<String, Integer>>> ent : callbacks.entrySet()) {
469 Callback cb = ent.getKey();
470 ArrayList<Pair<String, Integer>> reports = ent.getValue();
471 for (int i=0; i<reports.size(); i++) {
472 Pair<String, Integer> rep = reports.get(i);
473 try {
474 cb.mCallback.opChanged(rep.second, rep.first);
475 } catch (RemoteException e) {
476 }
477 }
478 }
479 }
480 }
481
Dianne Hackbornc2293022013-02-06 23:14:49 -0800482 @Override
483 public void startWatchingMode(int op, String packageName, IAppOpsCallback callback) {
484 synchronized (this) {
485 op = AppOpsManager.opToSwitch(op);
486 Callback cb = mModeWatchers.get(callback.asBinder());
487 if (cb == null) {
488 cb = new Callback(callback);
489 mModeWatchers.put(callback.asBinder(), cb);
490 }
491 if (op != AppOpsManager.OP_NONE) {
492 ArrayList<Callback> cbs = mOpModeWatchers.get(op);
493 if (cbs == null) {
494 cbs = new ArrayList<Callback>();
495 mOpModeWatchers.put(op, cbs);
496 }
497 cbs.add(cb);
498 }
499 if (packageName != null) {
500 ArrayList<Callback> cbs = mPackageModeWatchers.get(packageName);
501 if (cbs == null) {
502 cbs = new ArrayList<Callback>();
503 mPackageModeWatchers.put(packageName, cbs);
504 }
505 cbs.add(cb);
506 }
507 }
508 }
509
510 @Override
511 public void stopWatchingMode(IAppOpsCallback callback) {
512 synchronized (this) {
513 Callback cb = mModeWatchers.remove(callback.asBinder());
514 if (cb != null) {
515 cb.unlinkToDeath();
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700516 for (int i=mOpModeWatchers.size()-1; i>=0; i--) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800517 ArrayList<Callback> cbs = mOpModeWatchers.valueAt(i);
518 cbs.remove(cb);
519 if (cbs.size() <= 0) {
520 mOpModeWatchers.removeAt(i);
521 }
522 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700523 for (int i=mPackageModeWatchers.size()-1; i>=0; i--) {
524 ArrayList<Callback> cbs = mPackageModeWatchers.valueAt(i);
525 cbs.remove(cb);
526 if (cbs.size() <= 0) {
527 mPackageModeWatchers.removeAt(i);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800528 }
529 }
530 }
531 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800532 }
533
534 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700535 public IBinder getToken(IBinder clientToken) {
536 synchronized (this) {
537 ClientState cs = mClients.get(clientToken);
538 if (cs == null) {
539 cs = new ClientState(clientToken);
540 mClients.put(clientToken, cs);
541 }
542 return cs;
543 }
544 }
545
546 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -0800547 public int checkOperation(int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800548 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800549 verifyIncomingOp(code);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800550 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800551 Op op = getOpLocked(AppOpsManager.opToSwitch(code), uid, packageName, false);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800552 if (op == null) {
David Braunf5d83192013-09-16 13:43:51 -0700553 return AppOpsManager.opToDefaultMode(code);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800554 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800555 return op.mode;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800556 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800557 }
558
559 @Override
John Spurlock1af30c72014-03-10 08:33:35 -0400560 public int checkAudioOperation(int code, int stream, int uid, String packageName) {
561 synchronized (this) {
562 final int mode = checkRestrictionLocked(code, stream, uid, packageName);
563 if (mode != AppOpsManager.MODE_ALLOWED) {
564 return mode;
565 }
566 }
567 return checkOperation(code, uid, packageName);
568 }
569
570 private int checkRestrictionLocked(int code, int stream, int uid, String packageName) {
571 final SparseArray<Restriction> streamRestrictions = mAudioRestrictions.get(code);
572 if (streamRestrictions != null) {
573 final Restriction r = streamRestrictions.get(stream);
574 if (r != null && !r.exceptionPackages.contains(packageName)) {
575 return r.mode;
576 }
577 }
578 return AppOpsManager.MODE_ALLOWED;
579 }
580
581 @Override
582 public void setAudioRestriction(int code, int stream, int uid, int mode,
583 String[] exceptionPackages) {
584 verifyIncomingUid(uid);
585 verifyIncomingOp(code);
586 synchronized (this) {
587 SparseArray<Restriction> streamRestrictions = mAudioRestrictions.get(code);
588 if (streamRestrictions == null) {
589 streamRestrictions = new SparseArray<Restriction>();
590 mAudioRestrictions.put(code, streamRestrictions);
591 }
592 streamRestrictions.remove(stream);
593 if (mode != AppOpsManager.MODE_ALLOWED) {
594 final Restriction r = new Restriction();
595 r.mode = mode;
596 if (exceptionPackages != null) {
597 final int N = exceptionPackages.length;
598 r.exceptionPackages = new ArraySet<String>(N);
599 for (int i = 0; i < N; i++) {
600 final String pkg = exceptionPackages[i];
601 if (pkg != null) {
602 r.exceptionPackages.add(pkg.trim());
603 }
604 }
605 }
606 streamRestrictions.put(stream, r);
607 }
608 }
609 }
610
611 @Override
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700612 public int checkPackage(int uid, String packageName) {
613 synchronized (this) {
614 if (getOpsLocked(uid, packageName, true) != null) {
615 return AppOpsManager.MODE_ALLOWED;
616 } else {
617 return AppOpsManager.MODE_ERRORED;
618 }
619 }
620 }
621
622 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800623 public int noteOperation(int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800624 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800625 verifyIncomingOp(code);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800626 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800627 Ops ops = getOpsLocked(uid, packageName, true);
628 if (ops == null) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800629 if (DEBUG) Log.d(TAG, "noteOperation: no op for code " + code + " uid " + uid
630 + " package " + packageName);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700631 return AppOpsManager.MODE_ERRORED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800632 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800633 Op op = getOpLocked(ops, code, true);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800634 if (op.duration == -1) {
635 Slog.w(TAG, "Noting op not finished: uid " + uid + " pkg " + packageName
636 + " code " + code + " time=" + op.time + " duration=" + op.duration);
637 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800638 op.duration = 0;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800639 final int switchCode = AppOpsManager.opToSwitch(code);
640 final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
641 if (switchOp.mode != AppOpsManager.MODE_ALLOWED) {
642 if (DEBUG) Log.d(TAG, "noteOperation: reject #" + op.mode + " for code "
643 + switchCode + " (" + code + ") uid " + uid + " package " + packageName);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800644 op.rejectTime = System.currentTimeMillis();
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800645 return switchOp.mode;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800646 }
647 if (DEBUG) Log.d(TAG, "noteOperation: allowing code " + code + " uid " + uid
648 + " package " + packageName);
649 op.time = System.currentTimeMillis();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800650 op.rejectTime = 0;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800651 return AppOpsManager.MODE_ALLOWED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800652 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800653 }
654
655 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700656 public int startOperation(IBinder token, int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800657 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800658 verifyIncomingOp(code);
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700659 ClientState client = (ClientState)token;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800660 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800661 Ops ops = getOpsLocked(uid, packageName, true);
662 if (ops == null) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800663 if (DEBUG) Log.d(TAG, "startOperation: no op for code " + code + " uid " + uid
664 + " package " + packageName);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700665 return AppOpsManager.MODE_ERRORED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800666 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800667 Op op = getOpLocked(ops, code, true);
668 final int switchCode = AppOpsManager.opToSwitch(code);
669 final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
670 if (switchOp.mode != AppOpsManager.MODE_ALLOWED) {
671 if (DEBUG) Log.d(TAG, "startOperation: reject #" + op.mode + " for code "
672 + switchCode + " (" + code + ") uid " + uid + " package " + packageName);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800673 op.rejectTime = System.currentTimeMillis();
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800674 return switchOp.mode;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800675 }
676 if (DEBUG) Log.d(TAG, "startOperation: allowing code " + code + " uid " + uid
677 + " package " + packageName);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800678 if (op.nesting == 0) {
679 op.time = System.currentTimeMillis();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800680 op.rejectTime = 0;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800681 op.duration = -1;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800682 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800683 op.nesting++;
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700684 if (client.mStartedOps != null) {
685 client.mStartedOps.add(op);
686 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800687 return AppOpsManager.MODE_ALLOWED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800688 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800689 }
690
691 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700692 public void finishOperation(IBinder token, int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800693 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800694 verifyIncomingOp(code);
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700695 ClientState client = (ClientState)token;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800696 synchronized (this) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800697 Op op = getOpLocked(code, uid, packageName, true);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800698 if (op == null) {
699 return;
700 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700701 if (client.mStartedOps != null) {
702 if (!client.mStartedOps.remove(op)) {
703 throw new IllegalStateException("Operation not started: uid" + op.uid
704 + " pkg=" + op.packageName + " op=" + op.op);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800705 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800706 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700707 finishOperationLocked(op);
708 }
709 }
710
711 void finishOperationLocked(Op op) {
712 if (op.nesting <= 1) {
713 if (op.nesting == 1) {
714 op.duration = (int)(System.currentTimeMillis() - op.time);
715 op.time += op.duration;
716 } else {
717 Slog.w(TAG, "Finishing op nesting under-run: uid " + op.uid + " pkg "
718 + op.packageName + " code " + op.op + " time=" + op.time
719 + " duration=" + op.duration + " nesting=" + op.nesting);
720 }
721 op.nesting = 0;
722 } else {
723 op.nesting--;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800724 }
725 }
726
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800727 private void verifyIncomingUid(int uid) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800728 if (uid == Binder.getCallingUid()) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800729 return;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800730 }
731 if (Binder.getCallingPid() == Process.myPid()) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800732 return;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800733 }
734 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
735 Binder.getCallingPid(), Binder.getCallingUid(), null);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800736 }
737
Dianne Hackborn961321f2013-02-05 17:22:41 -0800738 private void verifyIncomingOp(int op) {
739 if (op >= 0 && op < AppOpsManager._NUM_OP) {
740 return;
741 }
742 throw new IllegalArgumentException("Bad operation #" + op);
743 }
744
Dianne Hackborn72e39832013-01-18 18:36:09 -0800745 private Ops getOpsLocked(int uid, String packageName, boolean edit) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800746 HashMap<String, Ops> pkgOps = mUidOps.get(uid);
747 if (pkgOps == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800748 if (!edit) {
749 return null;
750 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800751 pkgOps = new HashMap<String, Ops>();
752 mUidOps.put(uid, pkgOps);
753 }
Dianne Hackborn514074f2013-02-11 10:52:46 -0800754 if (uid == 0) {
755 packageName = "root";
756 } else if (uid == Process.SHELL_UID) {
757 packageName = "com.android.shell";
758 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800759 Ops ops = pkgOps.get(packageName);
760 if (ops == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800761 if (!edit) {
762 return null;
763 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800764 // This is the first time we have seen this package name under this uid,
765 // so let's make sure it is valid.
Dianne Hackborn514074f2013-02-11 10:52:46 -0800766 if (uid != 0) {
767 final long ident = Binder.clearCallingIdentity();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800768 try {
Dianne Hackborn514074f2013-02-11 10:52:46 -0800769 int pkgUid = -1;
770 try {
771 pkgUid = mContext.getPackageManager().getPackageUid(packageName,
772 UserHandle.getUserId(uid));
773 } catch (NameNotFoundException e) {
Dianne Hackborn713df152013-05-17 11:27:57 -0700774 if ("media".equals(packageName)) {
775 pkgUid = Process.MEDIA_UID;
776 }
Dianne Hackborn514074f2013-02-11 10:52:46 -0800777 }
778 if (pkgUid != uid) {
779 // Oops! The package name is not valid for the uid they are calling
780 // under. Abort.
781 Slog.w(TAG, "Bad call: specified package " + packageName
782 + " under uid " + uid + " but it is really " + pkgUid);
783 return null;
784 }
785 } finally {
786 Binder.restoreCallingIdentity(ident);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800787 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800788 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800789 ops = new Ops(packageName, uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800790 pkgOps.put(packageName, ops);
791 }
Dianne Hackborn72e39832013-01-18 18:36:09 -0800792 return ops;
793 }
794
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800795 private void scheduleWriteLocked() {
796 if (!mWriteScheduled) {
797 mWriteScheduled = true;
798 mHandler.postDelayed(mWriteRunner, WRITE_DELAY);
799 }
800 }
801
802 private void scheduleWriteNowLocked() {
803 if (!mWriteScheduled) {
804 mWriteScheduled = true;
805 }
806 mHandler.removeCallbacks(mWriteRunner);
807 mHandler.post(mWriteRunner);
808 }
809
Dianne Hackborn72e39832013-01-18 18:36:09 -0800810 private Op getOpLocked(int code, int uid, String packageName, boolean edit) {
811 Ops ops = getOpsLocked(uid, packageName, edit);
812 if (ops == null) {
813 return null;
814 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800815 return getOpLocked(ops, code, edit);
816 }
817
818 private Op getOpLocked(Ops ops, int code, boolean edit) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800819 Op op = ops.get(code);
820 if (op == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800821 if (!edit) {
822 return null;
823 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700824 op = new Op(ops.uid, ops.packageName, code);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800825 ops.put(code, op);
826 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800827 if (edit) {
828 scheduleWriteLocked();
Dianne Hackborn35654b62013-01-14 17:38:02 -0800829 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800830 return op;
831 }
832
Dianne Hackborn35654b62013-01-14 17:38:02 -0800833 void readState() {
834 synchronized (mFile) {
835 synchronized (this) {
836 FileInputStream stream;
837 try {
838 stream = mFile.openRead();
839 } catch (FileNotFoundException e) {
840 Slog.i(TAG, "No existing app ops " + mFile.getBaseFile() + "; starting empty");
841 return;
842 }
843 boolean success = false;
844 try {
845 XmlPullParser parser = Xml.newPullParser();
846 parser.setInput(stream, null);
847 int type;
848 while ((type = parser.next()) != XmlPullParser.START_TAG
849 && type != XmlPullParser.END_DOCUMENT) {
850 ;
851 }
852
853 if (type != XmlPullParser.START_TAG) {
854 throw new IllegalStateException("no start tag found");
855 }
856
857 int outerDepth = parser.getDepth();
858 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
859 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
860 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
861 continue;
862 }
863
864 String tagName = parser.getName();
865 if (tagName.equals("pkg")) {
Dave Burke0997c5bd2013-08-02 20:25:02 +0000866 readPackage(parser);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800867 } else {
868 Slog.w(TAG, "Unknown element under <app-ops>: "
869 + parser.getName());
870 XmlUtils.skipCurrentTag(parser);
871 }
872 }
873 success = true;
874 } catch (IllegalStateException e) {
875 Slog.w(TAG, "Failed parsing " + e);
876 } catch (NullPointerException e) {
877 Slog.w(TAG, "Failed parsing " + e);
878 } catch (NumberFormatException e) {
879 Slog.w(TAG, "Failed parsing " + e);
880 } catch (XmlPullParserException e) {
881 Slog.w(TAG, "Failed parsing " + e);
882 } catch (IOException e) {
883 Slog.w(TAG, "Failed parsing " + e);
884 } catch (IndexOutOfBoundsException e) {
885 Slog.w(TAG, "Failed parsing " + e);
886 } finally {
887 if (!success) {
888 mUidOps.clear();
889 }
890 try {
891 stream.close();
892 } catch (IOException e) {
893 }
894 }
895 }
896 }
897 }
898
Dave Burke0997c5bd2013-08-02 20:25:02 +0000899 void readPackage(XmlPullParser parser) throws NumberFormatException,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800900 XmlPullParserException, IOException {
901 String pkgName = parser.getAttributeValue(null, "n");
902 int outerDepth = parser.getDepth();
903 int type;
904 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
905 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
906 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
907 continue;
908 }
909
910 String tagName = parser.getName();
911 if (tagName.equals("uid")) {
Dave Burke0997c5bd2013-08-02 20:25:02 +0000912 readUid(parser, pkgName);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800913 } else {
914 Slog.w(TAG, "Unknown element under <pkg>: "
915 + parser.getName());
916 XmlUtils.skipCurrentTag(parser);
917 }
918 }
919 }
920
Dave Burke0997c5bd2013-08-02 20:25:02 +0000921 void readUid(XmlPullParser parser, String pkgName) throws NumberFormatException,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800922 XmlPullParserException, IOException {
923 int uid = Integer.parseInt(parser.getAttributeValue(null, "n"));
924 int outerDepth = parser.getDepth();
925 int type;
926 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
927 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
928 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
929 continue;
930 }
931
932 String tagName = parser.getName();
933 if (tagName.equals("op")) {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700934 Op op = new Op(uid, pkgName, Integer.parseInt(parser.getAttributeValue(null, "n")));
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800935 String mode = parser.getAttributeValue(null, "m");
936 if (mode != null) {
Dave Burke0997c5bd2013-08-02 20:25:02 +0000937 op.mode = Integer.parseInt(mode);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800938 }
939 String time = parser.getAttributeValue(null, "t");
940 if (time != null) {
941 op.time = Long.parseLong(time);
942 }
943 time = parser.getAttributeValue(null, "r");
944 if (time != null) {
945 op.rejectTime = Long.parseLong(time);
946 }
947 String dur = parser.getAttributeValue(null, "d");
948 if (dur != null) {
949 op.duration = Integer.parseInt(dur);
950 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800951 HashMap<String, Ops> pkgOps = mUidOps.get(uid);
952 if (pkgOps == null) {
953 pkgOps = new HashMap<String, Ops>();
954 mUidOps.put(uid, pkgOps);
955 }
956 Ops ops = pkgOps.get(pkgName);
957 if (ops == null) {
958 ops = new Ops(pkgName, uid);
959 pkgOps.put(pkgName, ops);
960 }
961 ops.put(op.op, op);
962 } else {
963 Slog.w(TAG, "Unknown element under <pkg>: "
964 + parser.getName());
965 XmlUtils.skipCurrentTag(parser);
966 }
967 }
968 }
969
970 void writeState() {
971 synchronized (mFile) {
972 List<AppOpsManager.PackageOps> allOps = getPackagesForOps(null);
973
974 FileOutputStream stream;
975 try {
976 stream = mFile.startWrite();
977 } catch (IOException e) {
978 Slog.w(TAG, "Failed to write state: " + e);
979 return;
980 }
981
982 try {
983 XmlSerializer out = new FastXmlSerializer();
984 out.setOutput(stream, "utf-8");
985 out.startDocument(null, true);
986 out.startTag(null, "app-ops");
987
988 if (allOps != null) {
989 String lastPkg = null;
990 for (int i=0; i<allOps.size(); i++) {
991 AppOpsManager.PackageOps pkg = allOps.get(i);
992 if (!pkg.getPackageName().equals(lastPkg)) {
993 if (lastPkg != null) {
994 out.endTag(null, "pkg");
995 }
996 lastPkg = pkg.getPackageName();
997 out.startTag(null, "pkg");
998 out.attribute(null, "n", lastPkg);
999 }
1000 out.startTag(null, "uid");
1001 out.attribute(null, "n", Integer.toString(pkg.getUid()));
1002 List<AppOpsManager.OpEntry> ops = pkg.getOps();
1003 for (int j=0; j<ops.size(); j++) {
1004 AppOpsManager.OpEntry op = ops.get(j);
1005 out.startTag(null, "op");
1006 out.attribute(null, "n", Integer.toString(op.getOp()));
David Braunf5d83192013-09-16 13:43:51 -07001007 if (op.getMode() != AppOpsManager.opToDefaultMode(op.getOp())) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001008 out.attribute(null, "m", Integer.toString(op.getMode()));
1009 }
1010 long time = op.getTime();
1011 if (time != 0) {
1012 out.attribute(null, "t", Long.toString(time));
1013 }
1014 time = op.getRejectTime();
1015 if (time != 0) {
1016 out.attribute(null, "r", Long.toString(time));
1017 }
1018 int dur = op.getDuration();
1019 if (dur != 0) {
1020 out.attribute(null, "d", Integer.toString(dur));
1021 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001022 out.endTag(null, "op");
1023 }
1024 out.endTag(null, "uid");
1025 }
1026 if (lastPkg != null) {
1027 out.endTag(null, "pkg");
1028 }
1029 }
1030
1031 out.endTag(null, "app-ops");
1032 out.endDocument();
1033 mFile.finishWrite(stream);
1034 } catch (IOException e) {
1035 Slog.w(TAG, "Failed to write state, restoring backup.", e);
1036 mFile.failWrite(stream);
1037 }
1038 }
1039 }
1040
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001041 @Override
1042 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1043 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1044 != PackageManager.PERMISSION_GRANTED) {
1045 pw.println("Permission Denial: can't dump ApOps service from from pid="
1046 + Binder.getCallingPid()
1047 + ", uid=" + Binder.getCallingUid());
1048 return;
1049 }
1050
1051 synchronized (this) {
1052 pw.println("Current AppOps Service state:");
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001053 final long now = System.currentTimeMillis();
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001054 boolean needSep = false;
1055 if (mOpModeWatchers.size() > 0) {
1056 needSep = true;
1057 pw.println(" Op mode watchers:");
1058 for (int i=0; i<mOpModeWatchers.size(); i++) {
1059 pw.print(" Op "); pw.print(AppOpsManager.opToName(mOpModeWatchers.keyAt(i)));
1060 pw.println(":");
1061 ArrayList<Callback> callbacks = mOpModeWatchers.valueAt(i);
1062 for (int j=0; j<callbacks.size(); j++) {
1063 pw.print(" #"); pw.print(j); pw.print(": ");
1064 pw.println(callbacks.get(j));
1065 }
1066 }
1067 }
1068 if (mPackageModeWatchers.size() > 0) {
1069 needSep = true;
1070 pw.println(" Package mode watchers:");
1071 for (int i=0; i<mPackageModeWatchers.size(); i++) {
1072 pw.print(" Pkg "); pw.print(mPackageModeWatchers.keyAt(i));
1073 pw.println(":");
1074 ArrayList<Callback> callbacks = mPackageModeWatchers.valueAt(i);
1075 for (int j=0; j<callbacks.size(); j++) {
1076 pw.print(" #"); pw.print(j); pw.print(": ");
1077 pw.println(callbacks.get(j));
1078 }
1079 }
1080 }
1081 if (mModeWatchers.size() > 0) {
1082 needSep = true;
1083 pw.println(" All mode watchers:");
1084 for (int i=0; i<mModeWatchers.size(); i++) {
1085 pw.print(" "); pw.print(mModeWatchers.keyAt(i));
1086 pw.print(" -> "); pw.println(mModeWatchers.valueAt(i));
1087 }
1088 }
1089 if (mClients.size() > 0) {
1090 needSep = true;
1091 pw.println(" Clients:");
1092 for (int i=0; i<mClients.size(); i++) {
1093 pw.print(" "); pw.print(mClients.keyAt(i)); pw.println(":");
1094 ClientState cs = mClients.valueAt(i);
1095 pw.print(" "); pw.println(cs);
1096 if (cs.mStartedOps != null && cs.mStartedOps.size() > 0) {
1097 pw.println(" Started ops:");
1098 for (int j=0; j<cs.mStartedOps.size(); j++) {
1099 Op op = cs.mStartedOps.get(j);
1100 pw.print(" "); pw.print("uid="); pw.print(op.uid);
1101 pw.print(" pkg="); pw.print(op.packageName);
1102 pw.print(" op="); pw.println(AppOpsManager.opToName(op.op));
1103 }
1104 }
1105 }
1106 }
John Spurlock1af30c72014-03-10 08:33:35 -04001107 if (mAudioRestrictions.size() > 0) {
1108 boolean printedHeader = false;
1109 for (int o=0; o<mAudioRestrictions.size(); o++) {
1110 final String op = AppOpsManager.opToName(mAudioRestrictions.keyAt(o));
1111 final SparseArray<Restriction> restrictions = mAudioRestrictions.valueAt(o);
1112 for (int i=0; i<restrictions.size(); i++) {
1113 if (!printedHeader){
1114 pw.println(" Audio Restrictions:");
1115 printedHeader = true;
1116 needSep = true;
1117 }
1118 final int stream = restrictions.keyAt(i);
1119 pw.print(" "); pw.print(op);
1120 pw.print(" stream="); pw.print(AudioService.streamToString(stream));
1121 Restriction r = restrictions.valueAt(i);
1122 pw.print(": mode="); pw.println(r.mode);
1123 if (!r.exceptionPackages.isEmpty()) {
1124 pw.println(" Exceptions:");
1125 for (int j=0; j<r.exceptionPackages.size(); j++) {
1126 pw.print(" "); pw.println(r.exceptionPackages.valueAt(j));
1127 }
1128 }
1129 }
1130 }
1131 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001132 if (needSep) {
1133 pw.println();
1134 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001135 for (int i=0; i<mUidOps.size(); i++) {
1136 pw.print(" Uid "); UserHandle.formatUid(pw, mUidOps.keyAt(i)); pw.println(":");
1137 HashMap<String, Ops> pkgOps = mUidOps.valueAt(i);
1138 for (Ops ops : pkgOps.values()) {
1139 pw.print(" Package "); pw.print(ops.packageName); pw.println(":");
1140 for (int j=0; j<ops.size(); j++) {
1141 Op op = ops.valueAt(j);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001142 pw.print(" "); pw.print(AppOpsManager.opToName(op.op));
1143 pw.print(": mode="); pw.print(op.mode);
1144 if (op.time != 0) {
1145 pw.print("; time="); TimeUtils.formatDuration(now-op.time, pw);
1146 pw.print(" ago");
1147 }
1148 if (op.rejectTime != 0) {
1149 pw.print("; rejectTime="); TimeUtils.formatDuration(now-op.rejectTime, pw);
1150 pw.print(" ago");
1151 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001152 if (op.duration == -1) {
1153 pw.println(" (running)");
1154 } else {
1155 pw.print("; duration=");
1156 TimeUtils.formatDuration(op.duration, pw);
1157 pw.println();
1158 }
1159 }
1160 }
1161 }
1162 }
1163 }
John Spurlock1af30c72014-03-10 08:33:35 -04001164
1165 private static final class Restriction {
1166 private static final ArraySet<String> NO_EXCEPTIONS = new ArraySet<String>();
1167 int mode;
1168 ArraySet<String> exceptionPackages = NO_EXCEPTIONS;
1169 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001170}