blob: a1a0d47cb0199a8b8629a42f8f1dc4eab0e54979 [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;
Dianne Hackborn35654b62013-01-14 17:38:02 -080036import android.os.AsyncTask;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080037import android.os.Binder;
Dianne Hackborn35654b62013-01-14 17:38:02 -080038import android.os.Handler;
Dianne Hackbornc2293022013-02-06 23:14:49 -080039import android.os.IBinder;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080040import android.os.Process;
Dianne Hackbornc2293022013-02-06 23:14:49 -080041import android.os.RemoteException;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080042import android.os.ServiceManager;
43import android.os.UserHandle;
Dianne Hackborne98f5db2013-07-17 17:23:25 -070044import android.util.ArrayMap;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080045import android.util.AtomicFile;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -080046import android.util.Log;
Dianne Hackborn607b4142013-08-02 18:10:10 -070047import android.util.Pair;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080048import android.util.Slog;
49import android.util.SparseArray;
50import android.util.TimeUtils;
Dianne Hackborn35654b62013-01-14 17:38:02 -080051import android.util.Xml;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080052
53import com.android.internal.app.IAppOpsService;
Dianne Hackbornc2293022013-02-06 23:14:49 -080054import com.android.internal.app.IAppOpsCallback;
Dianne Hackborn35654b62013-01-14 17:38:02 -080055import com.android.internal.util.FastXmlSerializer;
56import com.android.internal.util.XmlUtils;
57
58import org.xmlpull.v1.XmlPullParser;
59import org.xmlpull.v1.XmlPullParserException;
60import org.xmlpull.v1.XmlSerializer;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080061
62public class AppOpsService extends IAppOpsService.Stub {
63 static final String TAG = "AppOps";
Dianne Hackborn35654b62013-01-14 17:38:02 -080064 static final boolean DEBUG = false;
65
66 // Write at most every 30 minutes.
67 static final long WRITE_DELAY = DEBUG ? 1000 : 30*60*1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080068
69 Context mContext;
70 final AtomicFile mFile;
Dianne Hackborn35654b62013-01-14 17:38:02 -080071 final Handler mHandler;
72
73 boolean mWriteScheduled;
74 final Runnable mWriteRunner = new Runnable() {
75 public void run() {
76 synchronized (AppOpsService.this) {
77 mWriteScheduled = false;
78 AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
79 @Override protected Void doInBackground(Void... params) {
80 writeState();
81 return null;
82 }
83 };
84 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
85 }
86 }
87 };
Dianne Hackborna06de0f2012-12-11 16:34:47 -080088
89 final SparseArray<HashMap<String, Ops>> mUidOps
90 = new SparseArray<HashMap<String, Ops>>();
91
Dianne Hackbornc2293022013-02-06 23:14:49 -080092 public final static class Ops extends SparseArray<Op> {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080093 public final String packageName;
Dianne Hackborn35654b62013-01-14 17:38:02 -080094 public final int uid;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080095
Dianne Hackborn35654b62013-01-14 17:38:02 -080096 public Ops(String _packageName, int _uid) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080097 packageName = _packageName;
Dianne Hackborn35654b62013-01-14 17:38:02 -080098 uid = _uid;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080099 }
100 }
101
Dianne Hackbornc2293022013-02-06 23:14:49 -0800102 public final static class Op {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700103 public final int uid;
104 public final String packageName;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800105 public final int op;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800106 public int mode;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800107 public int duration;
108 public long time;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800109 public long rejectTime;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800110 public int nesting;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800111
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700112 public Op(int _uid, String _packageName, int _op) {
113 uid = _uid;
114 packageName = _packageName;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800115 op = _op;
David Braunf5d83192013-09-16 13:43:51 -0700116 mode = AppOpsManager.opToDefaultMode(op);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800117 }
118 }
119
Dianne Hackbornc2293022013-02-06 23:14:49 -0800120 final SparseArray<ArrayList<Callback>> mOpModeWatchers
121 = new SparseArray<ArrayList<Callback>>();
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700122 final ArrayMap<String, ArrayList<Callback>> mPackageModeWatchers
123 = new ArrayMap<String, ArrayList<Callback>>();
124 final ArrayMap<IBinder, Callback> mModeWatchers
125 = new ArrayMap<IBinder, Callback>();
Dianne Hackbornc2293022013-02-06 23:14:49 -0800126
127 public final class Callback implements DeathRecipient {
128 final IAppOpsCallback mCallback;
129
130 public Callback(IAppOpsCallback callback) {
131 mCallback = callback;
132 try {
133 mCallback.asBinder().linkToDeath(this, 0);
134 } catch (RemoteException e) {
135 }
136 }
137
138 public void unlinkToDeath() {
139 mCallback.asBinder().unlinkToDeath(this, 0);
140 }
141
142 @Override
143 public void binderDied() {
144 stopWatchingMode(mCallback);
145 }
146 }
147
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700148 final ArrayMap<IBinder, ClientState> mClients = new ArrayMap<IBinder, ClientState>();
149
150 public final class ClientState extends Binder implements DeathRecipient {
151 final IBinder mAppToken;
152 final int mPid;
153 final ArrayList<Op> mStartedOps;
154
155 public ClientState(IBinder appToken) {
156 mAppToken = appToken;
157 mPid = Binder.getCallingPid();
158 if (appToken instanceof Binder) {
159 // For local clients, there is no reason to track them.
160 mStartedOps = null;
161 } else {
162 mStartedOps = new ArrayList<Op>();
163 try {
164 mAppToken.linkToDeath(this, 0);
165 } catch (RemoteException e) {
166 }
167 }
168 }
169
170 @Override
171 public String toString() {
172 return "ClientState{" +
173 "mAppToken=" + mAppToken +
174 ", " + (mStartedOps != null ? ("pid=" + mPid) : "local") +
175 '}';
176 }
177
178 @Override
179 public void binderDied() {
180 synchronized (AppOpsService.this) {
181 for (int i=mStartedOps.size()-1; i>=0; i--) {
182 finishOperationLocked(mStartedOps.get(i));
183 }
184 mClients.remove(mAppToken);
185 }
186 }
187 }
188
Dianne Hackborn35654b62013-01-14 17:38:02 -0800189 public AppOpsService(File storagePath) {
190 mFile = new AtomicFile(storagePath);
191 mHandler = new Handler();
192 readState();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800193 }
David Braunf5d83192013-09-16 13:43:51 -0700194
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800195 public void publish(Context context) {
196 mContext = context;
197 ServiceManager.addService(Context.APP_OPS_SERVICE, asBinder());
198 }
199
Dianne Hackborn514074f2013-02-11 10:52:46 -0800200 public void systemReady() {
201 synchronized (this) {
202 boolean changed = false;
203 for (int i=0; i<mUidOps.size(); i++) {
204 HashMap<String, Ops> pkgs = mUidOps.valueAt(i);
205 Iterator<Ops> it = pkgs.values().iterator();
206 while (it.hasNext()) {
207 Ops ops = it.next();
208 int curUid;
209 try {
210 curUid = mContext.getPackageManager().getPackageUid(ops.packageName,
211 UserHandle.getUserId(ops.uid));
212 } catch (NameNotFoundException e) {
213 curUid = -1;
214 }
215 if (curUid != ops.uid) {
216 Slog.i(TAG, "Pruning old package " + ops.packageName
217 + "/" + ops.uid + ": new uid=" + curUid);
218 it.remove();
219 changed = true;
220 }
221 }
222 if (pkgs.size() <= 0) {
223 mUidOps.removeAt(i);
224 }
225 }
226 if (changed) {
227 scheduleWriteLocked();
228 }
229 }
230 }
231
232 public void packageRemoved(int uid, String packageName) {
233 synchronized (this) {
234 HashMap<String, Ops> pkgs = mUidOps.get(uid);
235 if (pkgs != null) {
236 if (pkgs.remove(packageName) != null) {
237 if (pkgs.size() <= 0) {
238 mUidOps.remove(uid);
239 }
240 scheduleWriteLocked();
241 }
242 }
243 }
244 }
245
246 public void uidRemoved(int uid) {
247 synchronized (this) {
248 if (mUidOps.indexOfKey(uid) >= 0) {
249 mUidOps.remove(uid);
250 scheduleWriteLocked();
251 }
252 }
253 }
254
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800255 public void shutdown() {
256 Slog.w(TAG, "Writing app ops before shutdown...");
Dianne Hackborn35654b62013-01-14 17:38:02 -0800257 boolean doWrite = false;
258 synchronized (this) {
259 if (mWriteScheduled) {
260 mWriteScheduled = false;
261 doWrite = true;
262 }
263 }
264 if (doWrite) {
265 writeState();
266 }
267 }
268
Dianne Hackborn72e39832013-01-18 18:36:09 -0800269 private ArrayList<AppOpsManager.OpEntry> collectOps(Ops pkgOps, int[] ops) {
270 ArrayList<AppOpsManager.OpEntry> resOps = null;
271 if (ops == null) {
272 resOps = new ArrayList<AppOpsManager.OpEntry>();
273 for (int j=0; j<pkgOps.size(); j++) {
274 Op curOp = pkgOps.valueAt(j);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800275 resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time,
276 curOp.rejectTime, curOp.duration));
Dianne Hackborn72e39832013-01-18 18:36:09 -0800277 }
278 } else {
279 for (int j=0; j<ops.length; j++) {
280 Op curOp = pkgOps.get(ops[j]);
281 if (curOp != null) {
282 if (resOps == null) {
283 resOps = new ArrayList<AppOpsManager.OpEntry>();
284 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800285 resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time,
286 curOp.rejectTime, curOp.duration));
Dianne Hackborn72e39832013-01-18 18:36:09 -0800287 }
288 }
289 }
290 return resOps;
291 }
292
Dianne Hackborn35654b62013-01-14 17:38:02 -0800293 @Override
294 public List<AppOpsManager.PackageOps> getPackagesForOps(int[] ops) {
295 mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS,
296 Binder.getCallingPid(), Binder.getCallingUid(), null);
297 ArrayList<AppOpsManager.PackageOps> res = null;
298 synchronized (this) {
299 for (int i=0; i<mUidOps.size(); i++) {
300 HashMap<String, Ops> packages = mUidOps.valueAt(i);
301 for (Ops pkgOps : packages.values()) {
Dianne Hackborn72e39832013-01-18 18:36:09 -0800302 ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800303 if (resOps != null) {
304 if (res == null) {
305 res = new ArrayList<AppOpsManager.PackageOps>();
306 }
307 AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps(
308 pkgOps.packageName, pkgOps.uid, resOps);
309 res.add(resPackage);
310 }
311 }
312 }
313 }
314 return res;
315 }
316
317 @Override
Dianne Hackborn72e39832013-01-18 18:36:09 -0800318 public List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName,
319 int[] ops) {
320 mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS,
321 Binder.getCallingPid(), Binder.getCallingUid(), null);
322 synchronized (this) {
323 Ops pkgOps = getOpsLocked(uid, packageName, false);
324 if (pkgOps == null) {
325 return null;
326 }
327 ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops);
328 if (resOps == null) {
329 return null;
330 }
331 ArrayList<AppOpsManager.PackageOps> res = new ArrayList<AppOpsManager.PackageOps>();
332 AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps(
333 pkgOps.packageName, pkgOps.uid, resOps);
334 res.add(resPackage);
335 return res;
336 }
337 }
338
Dianne Hackborn607b4142013-08-02 18:10:10 -0700339 private void pruneOp(Op op, int uid, String packageName) {
340 if (op.time == 0 && op.rejectTime == 0) {
341 Ops ops = getOpsLocked(uid, packageName, false);
342 if (ops != null) {
343 ops.remove(op.op);
344 if (ops.size() <= 0) {
345 HashMap<String, Ops> pkgOps = mUidOps.get(uid);
346 if (pkgOps != null) {
347 pkgOps.remove(ops.packageName);
348 if (pkgOps.size() <= 0) {
349 mUidOps.remove(uid);
350 }
351 }
352 }
353 }
354 }
355 }
356
Dianne Hackborn72e39832013-01-18 18:36:09 -0800357 @Override
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800358 public void setMode(int code, int uid, String packageName, int mode) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800359 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800360 verifyIncomingOp(code);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800361 ArrayList<Callback> repCbs = null;
362 code = AppOpsManager.opToSwitch(code);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800363 synchronized (this) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800364 Op op = getOpLocked(code, uid, packageName, true);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800365 if (op != null) {
366 if (op.mode != mode) {
367 op.mode = mode;
Dianne Hackbornc2293022013-02-06 23:14:49 -0800368 ArrayList<Callback> cbs = mOpModeWatchers.get(code);
369 if (cbs != null) {
370 if (repCbs == null) {
371 repCbs = new ArrayList<Callback>();
372 }
373 repCbs.addAll(cbs);
374 }
375 cbs = mPackageModeWatchers.get(packageName);
376 if (cbs != null) {
377 if (repCbs == null) {
378 repCbs = new ArrayList<Callback>();
379 }
380 repCbs.addAll(cbs);
381 }
David Braunf5d83192013-09-16 13:43:51 -0700382 if (mode == AppOpsManager.opToDefaultMode(op.op)) {
Dianne Hackborn514074f2013-02-11 10:52:46 -0800383 // If going into the default mode, prune this op
384 // if there is nothing else interesting in it.
Dianne Hackborn607b4142013-08-02 18:10:10 -0700385 pruneOp(op, uid, packageName);
Dianne Hackborn514074f2013-02-11 10:52:46 -0800386 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800387 scheduleWriteNowLocked();
388 }
389 }
390 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800391 if (repCbs != null) {
392 for (int i=0; i<repCbs.size(); i++) {
393 try {
394 repCbs.get(i).mCallback.opChanged(code, packageName);
395 } catch (RemoteException e) {
396 }
397 }
398 }
399 }
400
Dianne Hackborn607b4142013-08-02 18:10:10 -0700401 private static HashMap<Callback, ArrayList<Pair<String, Integer>>> addCallbacks(
402 HashMap<Callback, ArrayList<Pair<String, Integer>>> callbacks,
403 String packageName, int op, ArrayList<Callback> cbs) {
404 if (cbs == null) {
405 return callbacks;
406 }
407 if (callbacks == null) {
408 callbacks = new HashMap<Callback, ArrayList<Pair<String, Integer>>>();
409 }
410 for (int i=0; i<cbs.size(); i++) {
411 Callback cb = cbs.get(i);
412 ArrayList<Pair<String, Integer>> reports = callbacks.get(cb);
413 if (reports == null) {
414 reports = new ArrayList<Pair<String, Integer>>();
415 callbacks.put(cb, reports);
416 }
417 reports.add(new Pair<String, Integer>(packageName, op));
418 }
419 return callbacks;
420 }
421
422 @Override
423 public void resetAllModes() {
424 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
425 Binder.getCallingPid(), Binder.getCallingUid(), null);
426 HashMap<Callback, ArrayList<Pair<String, Integer>>> callbacks = null;
427 synchronized (this) {
428 boolean changed = false;
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700429 for (int i=mUidOps.size()-1; i>=0; i--) {
Dianne Hackborn607b4142013-08-02 18:10:10 -0700430 HashMap<String, Ops> packages = mUidOps.valueAt(i);
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700431 Iterator<Map.Entry<String, Ops>> it = packages.entrySet().iterator();
432 while (it.hasNext()) {
433 Map.Entry<String, Ops> ent = it.next();
Dianne Hackborn607b4142013-08-02 18:10:10 -0700434 String packageName = ent.getKey();
435 Ops pkgOps = ent.getValue();
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700436 for (int j=pkgOps.size()-1; j>=0; j--) {
Dianne Hackborn607b4142013-08-02 18:10:10 -0700437 Op curOp = pkgOps.valueAt(j);
Dianne Hackborn8828d3a2013-09-25 16:47:10 -0700438 if (AppOpsManager.opAllowsReset(curOp.op)
439 && curOp.mode != AppOpsManager.opToDefaultMode(curOp.op)) {
David Braunf5d83192013-09-16 13:43:51 -0700440 curOp.mode = AppOpsManager.opToDefaultMode(curOp.op);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700441 changed = true;
442 callbacks = addCallbacks(callbacks, packageName, curOp.op,
443 mOpModeWatchers.get(curOp.op));
444 callbacks = addCallbacks(callbacks, packageName, curOp.op,
445 mPackageModeWatchers.get(packageName));
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700446 if (curOp.time == 0 && curOp.rejectTime == 0) {
447 pkgOps.removeAt(j);
448 }
Dianne Hackborn607b4142013-08-02 18:10:10 -0700449 }
450 }
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700451 if (pkgOps.size() == 0) {
452 it.remove();
453 }
454 }
455 if (packages.size() == 0) {
456 mUidOps.removeAt(i);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700457 }
458 }
459 if (changed) {
460 scheduleWriteNowLocked();
461 }
462 }
463 if (callbacks != null) {
464 for (Map.Entry<Callback, ArrayList<Pair<String, Integer>>> ent : callbacks.entrySet()) {
465 Callback cb = ent.getKey();
466 ArrayList<Pair<String, Integer>> reports = ent.getValue();
467 for (int i=0; i<reports.size(); i++) {
468 Pair<String, Integer> rep = reports.get(i);
469 try {
470 cb.mCallback.opChanged(rep.second, rep.first);
471 } catch (RemoteException e) {
472 }
473 }
474 }
475 }
476 }
477
Dianne Hackbornc2293022013-02-06 23:14:49 -0800478 @Override
479 public void startWatchingMode(int op, String packageName, IAppOpsCallback callback) {
480 synchronized (this) {
481 op = AppOpsManager.opToSwitch(op);
482 Callback cb = mModeWatchers.get(callback.asBinder());
483 if (cb == null) {
484 cb = new Callback(callback);
485 mModeWatchers.put(callback.asBinder(), cb);
486 }
487 if (op != AppOpsManager.OP_NONE) {
488 ArrayList<Callback> cbs = mOpModeWatchers.get(op);
489 if (cbs == null) {
490 cbs = new ArrayList<Callback>();
491 mOpModeWatchers.put(op, cbs);
492 }
493 cbs.add(cb);
494 }
495 if (packageName != null) {
496 ArrayList<Callback> cbs = mPackageModeWatchers.get(packageName);
497 if (cbs == null) {
498 cbs = new ArrayList<Callback>();
499 mPackageModeWatchers.put(packageName, cbs);
500 }
501 cbs.add(cb);
502 }
503 }
504 }
505
506 @Override
507 public void stopWatchingMode(IAppOpsCallback callback) {
508 synchronized (this) {
509 Callback cb = mModeWatchers.remove(callback.asBinder());
510 if (cb != null) {
511 cb.unlinkToDeath();
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700512 for (int i=mOpModeWatchers.size()-1; i>=0; i--) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800513 ArrayList<Callback> cbs = mOpModeWatchers.valueAt(i);
514 cbs.remove(cb);
515 if (cbs.size() <= 0) {
516 mOpModeWatchers.removeAt(i);
517 }
518 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700519 for (int i=mPackageModeWatchers.size()-1; i>=0; i--) {
520 ArrayList<Callback> cbs = mPackageModeWatchers.valueAt(i);
521 cbs.remove(cb);
522 if (cbs.size() <= 0) {
523 mPackageModeWatchers.removeAt(i);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800524 }
525 }
526 }
527 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800528 }
529
530 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700531 public IBinder getToken(IBinder clientToken) {
532 synchronized (this) {
533 ClientState cs = mClients.get(clientToken);
534 if (cs == null) {
535 cs = new ClientState(clientToken);
536 mClients.put(clientToken, cs);
537 }
538 return cs;
539 }
540 }
541
542 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -0800543 public int checkOperation(int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800544 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800545 verifyIncomingOp(code);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800546 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800547 Op op = getOpLocked(AppOpsManager.opToSwitch(code), uid, packageName, false);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800548 if (op == null) {
David Braunf5d83192013-09-16 13:43:51 -0700549 return AppOpsManager.opToDefaultMode(code);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800550 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800551 return op.mode;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800552 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800553 }
554
555 @Override
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700556 public int checkPackage(int uid, String packageName) {
557 synchronized (this) {
558 if (getOpsLocked(uid, packageName, true) != null) {
559 return AppOpsManager.MODE_ALLOWED;
560 } else {
561 return AppOpsManager.MODE_ERRORED;
562 }
563 }
564 }
565
566 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800567 public int noteOperation(int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800568 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800569 verifyIncomingOp(code);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800570 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800571 Ops ops = getOpsLocked(uid, packageName, true);
572 if (ops == null) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800573 if (DEBUG) Log.d(TAG, "noteOperation: no op for code " + code + " uid " + uid
574 + " package " + packageName);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700575 return AppOpsManager.MODE_ERRORED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800576 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800577 Op op = getOpLocked(ops, code, true);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800578 if (op.duration == -1) {
579 Slog.w(TAG, "Noting op not finished: uid " + uid + " pkg " + packageName
580 + " code " + code + " time=" + op.time + " duration=" + op.duration);
581 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800582 op.duration = 0;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800583 final int switchCode = AppOpsManager.opToSwitch(code);
584 final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
585 if (switchOp.mode != AppOpsManager.MODE_ALLOWED) {
586 if (DEBUG) Log.d(TAG, "noteOperation: reject #" + op.mode + " for code "
587 + switchCode + " (" + code + ") uid " + uid + " package " + packageName);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800588 op.rejectTime = System.currentTimeMillis();
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800589 return switchOp.mode;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800590 }
591 if (DEBUG) Log.d(TAG, "noteOperation: allowing code " + code + " uid " + uid
592 + " package " + packageName);
593 op.time = System.currentTimeMillis();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800594 op.rejectTime = 0;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800595 return AppOpsManager.MODE_ALLOWED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800596 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800597 }
598
599 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700600 public int startOperation(IBinder token, int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800601 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800602 verifyIncomingOp(code);
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700603 ClientState client = (ClientState)token;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800604 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800605 Ops ops = getOpsLocked(uid, packageName, true);
606 if (ops == null) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800607 if (DEBUG) Log.d(TAG, "startOperation: no op for code " + code + " uid " + uid
608 + " package " + packageName);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700609 return AppOpsManager.MODE_ERRORED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800610 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800611 Op op = getOpLocked(ops, code, true);
612 final int switchCode = AppOpsManager.opToSwitch(code);
613 final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
614 if (switchOp.mode != AppOpsManager.MODE_ALLOWED) {
615 if (DEBUG) Log.d(TAG, "startOperation: reject #" + op.mode + " for code "
616 + switchCode + " (" + code + ") uid " + uid + " package " + packageName);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800617 op.rejectTime = System.currentTimeMillis();
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800618 return switchOp.mode;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800619 }
620 if (DEBUG) Log.d(TAG, "startOperation: allowing code " + code + " uid " + uid
621 + " package " + packageName);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800622 if (op.nesting == 0) {
623 op.time = System.currentTimeMillis();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800624 op.rejectTime = 0;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800625 op.duration = -1;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800626 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800627 op.nesting++;
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700628 if (client.mStartedOps != null) {
629 client.mStartedOps.add(op);
630 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800631 return AppOpsManager.MODE_ALLOWED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800632 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800633 }
634
635 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700636 public void finishOperation(IBinder token, int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800637 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800638 verifyIncomingOp(code);
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700639 ClientState client = (ClientState)token;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800640 synchronized (this) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800641 Op op = getOpLocked(code, uid, packageName, true);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800642 if (op == null) {
643 return;
644 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700645 if (client.mStartedOps != null) {
646 if (!client.mStartedOps.remove(op)) {
647 throw new IllegalStateException("Operation not started: uid" + op.uid
648 + " pkg=" + op.packageName + " op=" + op.op);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800649 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800650 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700651 finishOperationLocked(op);
652 }
653 }
654
655 void finishOperationLocked(Op op) {
656 if (op.nesting <= 1) {
657 if (op.nesting == 1) {
658 op.duration = (int)(System.currentTimeMillis() - op.time);
659 op.time += op.duration;
660 } else {
661 Slog.w(TAG, "Finishing op nesting under-run: uid " + op.uid + " pkg "
662 + op.packageName + " code " + op.op + " time=" + op.time
663 + " duration=" + op.duration + " nesting=" + op.nesting);
664 }
665 op.nesting = 0;
666 } else {
667 op.nesting--;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800668 }
669 }
670
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800671 private void verifyIncomingUid(int uid) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800672 if (uid == Binder.getCallingUid()) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800673 return;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800674 }
675 if (Binder.getCallingPid() == Process.myPid()) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800676 return;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800677 }
678 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
679 Binder.getCallingPid(), Binder.getCallingUid(), null);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800680 }
681
Dianne Hackborn961321f2013-02-05 17:22:41 -0800682 private void verifyIncomingOp(int op) {
683 if (op >= 0 && op < AppOpsManager._NUM_OP) {
684 return;
685 }
686 throw new IllegalArgumentException("Bad operation #" + op);
687 }
688
Dianne Hackborn72e39832013-01-18 18:36:09 -0800689 private Ops getOpsLocked(int uid, String packageName, boolean edit) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800690 HashMap<String, Ops> pkgOps = mUidOps.get(uid);
691 if (pkgOps == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800692 if (!edit) {
693 return null;
694 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800695 pkgOps = new HashMap<String, Ops>();
696 mUidOps.put(uid, pkgOps);
697 }
Dianne Hackborn514074f2013-02-11 10:52:46 -0800698 if (uid == 0) {
699 packageName = "root";
700 } else if (uid == Process.SHELL_UID) {
701 packageName = "com.android.shell";
702 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800703 Ops ops = pkgOps.get(packageName);
704 if (ops == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800705 if (!edit) {
706 return null;
707 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800708 // This is the first time we have seen this package name under this uid,
709 // so let's make sure it is valid.
Dianne Hackborn514074f2013-02-11 10:52:46 -0800710 if (uid != 0) {
711 final long ident = Binder.clearCallingIdentity();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800712 try {
Dianne Hackborn514074f2013-02-11 10:52:46 -0800713 int pkgUid = -1;
714 try {
715 pkgUid = mContext.getPackageManager().getPackageUid(packageName,
716 UserHandle.getUserId(uid));
717 } catch (NameNotFoundException e) {
Dianne Hackborn713df152013-05-17 11:27:57 -0700718 if ("media".equals(packageName)) {
719 pkgUid = Process.MEDIA_UID;
720 }
Dianne Hackborn514074f2013-02-11 10:52:46 -0800721 }
722 if (pkgUid != uid) {
723 // Oops! The package name is not valid for the uid they are calling
724 // under. Abort.
725 Slog.w(TAG, "Bad call: specified package " + packageName
726 + " under uid " + uid + " but it is really " + pkgUid);
727 return null;
728 }
729 } finally {
730 Binder.restoreCallingIdentity(ident);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800731 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800732 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800733 ops = new Ops(packageName, uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800734 pkgOps.put(packageName, ops);
735 }
Dianne Hackborn72e39832013-01-18 18:36:09 -0800736 return ops;
737 }
738
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800739 private void scheduleWriteLocked() {
740 if (!mWriteScheduled) {
741 mWriteScheduled = true;
742 mHandler.postDelayed(mWriteRunner, WRITE_DELAY);
743 }
744 }
745
746 private void scheduleWriteNowLocked() {
747 if (!mWriteScheduled) {
748 mWriteScheduled = true;
749 }
750 mHandler.removeCallbacks(mWriteRunner);
751 mHandler.post(mWriteRunner);
752 }
753
Dianne Hackborn72e39832013-01-18 18:36:09 -0800754 private Op getOpLocked(int code, int uid, String packageName, boolean edit) {
755 Ops ops = getOpsLocked(uid, packageName, edit);
756 if (ops == null) {
757 return null;
758 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800759 return getOpLocked(ops, code, edit);
760 }
761
762 private Op getOpLocked(Ops ops, int code, boolean edit) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800763 Op op = ops.get(code);
764 if (op == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800765 if (!edit) {
766 return null;
767 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700768 op = new Op(ops.uid, ops.packageName, code);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800769 ops.put(code, op);
770 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800771 if (edit) {
772 scheduleWriteLocked();
Dianne Hackborn35654b62013-01-14 17:38:02 -0800773 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800774 return op;
775 }
776
Dianne Hackborn35654b62013-01-14 17:38:02 -0800777 void readState() {
778 synchronized (mFile) {
779 synchronized (this) {
780 FileInputStream stream;
781 try {
782 stream = mFile.openRead();
783 } catch (FileNotFoundException e) {
784 Slog.i(TAG, "No existing app ops " + mFile.getBaseFile() + "; starting empty");
785 return;
786 }
787 boolean success = false;
788 try {
789 XmlPullParser parser = Xml.newPullParser();
790 parser.setInput(stream, null);
791 int type;
792 while ((type = parser.next()) != XmlPullParser.START_TAG
793 && type != XmlPullParser.END_DOCUMENT) {
794 ;
795 }
796
797 if (type != XmlPullParser.START_TAG) {
798 throw new IllegalStateException("no start tag found");
799 }
800
801 int outerDepth = parser.getDepth();
802 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
803 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
804 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
805 continue;
806 }
807
808 String tagName = parser.getName();
809 if (tagName.equals("pkg")) {
Dave Burke0997c5bd2013-08-02 20:25:02 +0000810 readPackage(parser);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800811 } else {
812 Slog.w(TAG, "Unknown element under <app-ops>: "
813 + parser.getName());
814 XmlUtils.skipCurrentTag(parser);
815 }
816 }
817 success = true;
818 } catch (IllegalStateException e) {
819 Slog.w(TAG, "Failed parsing " + e);
820 } catch (NullPointerException e) {
821 Slog.w(TAG, "Failed parsing " + e);
822 } catch (NumberFormatException e) {
823 Slog.w(TAG, "Failed parsing " + e);
824 } catch (XmlPullParserException e) {
825 Slog.w(TAG, "Failed parsing " + e);
826 } catch (IOException e) {
827 Slog.w(TAG, "Failed parsing " + e);
828 } catch (IndexOutOfBoundsException e) {
829 Slog.w(TAG, "Failed parsing " + e);
830 } finally {
831 if (!success) {
832 mUidOps.clear();
833 }
834 try {
835 stream.close();
836 } catch (IOException e) {
837 }
838 }
839 }
840 }
841 }
842
Dave Burke0997c5bd2013-08-02 20:25:02 +0000843 void readPackage(XmlPullParser parser) throws NumberFormatException,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800844 XmlPullParserException, IOException {
845 String pkgName = parser.getAttributeValue(null, "n");
846 int outerDepth = parser.getDepth();
847 int type;
848 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
849 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
850 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
851 continue;
852 }
853
854 String tagName = parser.getName();
855 if (tagName.equals("uid")) {
Dave Burke0997c5bd2013-08-02 20:25:02 +0000856 readUid(parser, pkgName);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800857 } else {
858 Slog.w(TAG, "Unknown element under <pkg>: "
859 + parser.getName());
860 XmlUtils.skipCurrentTag(parser);
861 }
862 }
863 }
864
Dave Burke0997c5bd2013-08-02 20:25:02 +0000865 void readUid(XmlPullParser parser, String pkgName) throws NumberFormatException,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800866 XmlPullParserException, IOException {
867 int uid = Integer.parseInt(parser.getAttributeValue(null, "n"));
868 int outerDepth = parser.getDepth();
869 int type;
870 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
871 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
872 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
873 continue;
874 }
875
876 String tagName = parser.getName();
877 if (tagName.equals("op")) {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700878 Op op = new Op(uid, pkgName, Integer.parseInt(parser.getAttributeValue(null, "n")));
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800879 String mode = parser.getAttributeValue(null, "m");
880 if (mode != null) {
Dave Burke0997c5bd2013-08-02 20:25:02 +0000881 op.mode = Integer.parseInt(mode);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800882 }
883 String time = parser.getAttributeValue(null, "t");
884 if (time != null) {
885 op.time = Long.parseLong(time);
886 }
887 time = parser.getAttributeValue(null, "r");
888 if (time != null) {
889 op.rejectTime = Long.parseLong(time);
890 }
891 String dur = parser.getAttributeValue(null, "d");
892 if (dur != null) {
893 op.duration = Integer.parseInt(dur);
894 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800895 HashMap<String, Ops> pkgOps = mUidOps.get(uid);
896 if (pkgOps == null) {
897 pkgOps = new HashMap<String, Ops>();
898 mUidOps.put(uid, pkgOps);
899 }
900 Ops ops = pkgOps.get(pkgName);
901 if (ops == null) {
902 ops = new Ops(pkgName, uid);
903 pkgOps.put(pkgName, ops);
904 }
905 ops.put(op.op, op);
906 } else {
907 Slog.w(TAG, "Unknown element under <pkg>: "
908 + parser.getName());
909 XmlUtils.skipCurrentTag(parser);
910 }
911 }
912 }
913
914 void writeState() {
915 synchronized (mFile) {
916 List<AppOpsManager.PackageOps> allOps = getPackagesForOps(null);
917
918 FileOutputStream stream;
919 try {
920 stream = mFile.startWrite();
921 } catch (IOException e) {
922 Slog.w(TAG, "Failed to write state: " + e);
923 return;
924 }
925
926 try {
927 XmlSerializer out = new FastXmlSerializer();
928 out.setOutput(stream, "utf-8");
929 out.startDocument(null, true);
930 out.startTag(null, "app-ops");
931
932 if (allOps != null) {
933 String lastPkg = null;
934 for (int i=0; i<allOps.size(); i++) {
935 AppOpsManager.PackageOps pkg = allOps.get(i);
936 if (!pkg.getPackageName().equals(lastPkg)) {
937 if (lastPkg != null) {
938 out.endTag(null, "pkg");
939 }
940 lastPkg = pkg.getPackageName();
941 out.startTag(null, "pkg");
942 out.attribute(null, "n", lastPkg);
943 }
944 out.startTag(null, "uid");
945 out.attribute(null, "n", Integer.toString(pkg.getUid()));
946 List<AppOpsManager.OpEntry> ops = pkg.getOps();
947 for (int j=0; j<ops.size(); j++) {
948 AppOpsManager.OpEntry op = ops.get(j);
949 out.startTag(null, "op");
950 out.attribute(null, "n", Integer.toString(op.getOp()));
David Braunf5d83192013-09-16 13:43:51 -0700951 if (op.getMode() != AppOpsManager.opToDefaultMode(op.getOp())) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800952 out.attribute(null, "m", Integer.toString(op.getMode()));
953 }
954 long time = op.getTime();
955 if (time != 0) {
956 out.attribute(null, "t", Long.toString(time));
957 }
958 time = op.getRejectTime();
959 if (time != 0) {
960 out.attribute(null, "r", Long.toString(time));
961 }
962 int dur = op.getDuration();
963 if (dur != 0) {
964 out.attribute(null, "d", Integer.toString(dur));
965 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800966 out.endTag(null, "op");
967 }
968 out.endTag(null, "uid");
969 }
970 if (lastPkg != null) {
971 out.endTag(null, "pkg");
972 }
973 }
974
975 out.endTag(null, "app-ops");
976 out.endDocument();
977 mFile.finishWrite(stream);
978 } catch (IOException e) {
979 Slog.w(TAG, "Failed to write state, restoring backup.", e);
980 mFile.failWrite(stream);
981 }
982 }
983 }
984
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800985 @Override
986 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
987 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
988 != PackageManager.PERMISSION_GRANTED) {
989 pw.println("Permission Denial: can't dump ApOps service from from pid="
990 + Binder.getCallingPid()
991 + ", uid=" + Binder.getCallingUid());
992 return;
993 }
994
995 synchronized (this) {
996 pw.println("Current AppOps Service state:");
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800997 final long now = System.currentTimeMillis();
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700998 boolean needSep = false;
999 if (mOpModeWatchers.size() > 0) {
1000 needSep = true;
1001 pw.println(" Op mode watchers:");
1002 for (int i=0; i<mOpModeWatchers.size(); i++) {
1003 pw.print(" Op "); pw.print(AppOpsManager.opToName(mOpModeWatchers.keyAt(i)));
1004 pw.println(":");
1005 ArrayList<Callback> callbacks = mOpModeWatchers.valueAt(i);
1006 for (int j=0; j<callbacks.size(); j++) {
1007 pw.print(" #"); pw.print(j); pw.print(": ");
1008 pw.println(callbacks.get(j));
1009 }
1010 }
1011 }
1012 if (mPackageModeWatchers.size() > 0) {
1013 needSep = true;
1014 pw.println(" Package mode watchers:");
1015 for (int i=0; i<mPackageModeWatchers.size(); i++) {
1016 pw.print(" Pkg "); pw.print(mPackageModeWatchers.keyAt(i));
1017 pw.println(":");
1018 ArrayList<Callback> callbacks = mPackageModeWatchers.valueAt(i);
1019 for (int j=0; j<callbacks.size(); j++) {
1020 pw.print(" #"); pw.print(j); pw.print(": ");
1021 pw.println(callbacks.get(j));
1022 }
1023 }
1024 }
1025 if (mModeWatchers.size() > 0) {
1026 needSep = true;
1027 pw.println(" All mode watchers:");
1028 for (int i=0; i<mModeWatchers.size(); i++) {
1029 pw.print(" "); pw.print(mModeWatchers.keyAt(i));
1030 pw.print(" -> "); pw.println(mModeWatchers.valueAt(i));
1031 }
1032 }
1033 if (mClients.size() > 0) {
1034 needSep = true;
1035 pw.println(" Clients:");
1036 for (int i=0; i<mClients.size(); i++) {
1037 pw.print(" "); pw.print(mClients.keyAt(i)); pw.println(":");
1038 ClientState cs = mClients.valueAt(i);
1039 pw.print(" "); pw.println(cs);
1040 if (cs.mStartedOps != null && cs.mStartedOps.size() > 0) {
1041 pw.println(" Started ops:");
1042 for (int j=0; j<cs.mStartedOps.size(); j++) {
1043 Op op = cs.mStartedOps.get(j);
1044 pw.print(" "); pw.print("uid="); pw.print(op.uid);
1045 pw.print(" pkg="); pw.print(op.packageName);
1046 pw.print(" op="); pw.println(AppOpsManager.opToName(op.op));
1047 }
1048 }
1049 }
1050 }
1051 if (needSep) {
1052 pw.println();
1053 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001054 for (int i=0; i<mUidOps.size(); i++) {
1055 pw.print(" Uid "); UserHandle.formatUid(pw, mUidOps.keyAt(i)); pw.println(":");
1056 HashMap<String, Ops> pkgOps = mUidOps.valueAt(i);
1057 for (Ops ops : pkgOps.values()) {
1058 pw.print(" Package "); pw.print(ops.packageName); pw.println(":");
1059 for (int j=0; j<ops.size(); j++) {
1060 Op op = ops.valueAt(j);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001061 pw.print(" "); pw.print(AppOpsManager.opToName(op.op));
1062 pw.print(": mode="); pw.print(op.mode);
1063 if (op.time != 0) {
1064 pw.print("; time="); TimeUtils.formatDuration(now-op.time, pw);
1065 pw.print(" ago");
1066 }
1067 if (op.rejectTime != 0) {
1068 pw.print("; rejectTime="); TimeUtils.formatDuration(now-op.rejectTime, pw);
1069 pw.print(" ago");
1070 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001071 if (op.duration == -1) {
1072 pw.println(" (running)");
1073 } else {
1074 pw.print("; duration=");
1075 TimeUtils.formatDuration(op.duration, pw);
1076 pw.println();
1077 }
1078 }
1079 }
1080 }
1081 }
1082 }
1083}