blob: 81572c51d22d56df6a6ba7f634dd0e6d31906b1e [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;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800116 mode = AppOpsManager.MODE_ALLOWED;
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 }
194
195 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 }
Dianne Hackborn514074f2013-02-11 10:52:46 -0800382 if (mode == AppOpsManager.MODE_ALLOWED) {
383 // 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;
429 for (int i=0; i<mUidOps.size(); i++) {
430 HashMap<String, Ops> packages = mUidOps.valueAt(i);
431 for (Map.Entry<String, Ops> ent : packages.entrySet()) {
432 String packageName = ent.getKey();
433 Ops pkgOps = ent.getValue();
434 for (int j=0; j<pkgOps.size(); j++) {
435 Op curOp = pkgOps.valueAt(j);
436 if (curOp.mode != AppOpsManager.MODE_ALLOWED) {
437 curOp.mode = AppOpsManager.MODE_ALLOWED;
438 changed = true;
439 callbacks = addCallbacks(callbacks, packageName, curOp.op,
440 mOpModeWatchers.get(curOp.op));
441 callbacks = addCallbacks(callbacks, packageName, curOp.op,
442 mPackageModeWatchers.get(packageName));
443 pruneOp(curOp, mUidOps.keyAt(i), packageName);
444 }
445 }
446 }
447 }
448 if (changed) {
449 scheduleWriteNowLocked();
450 }
451 }
452 if (callbacks != null) {
453 for (Map.Entry<Callback, ArrayList<Pair<String, Integer>>> ent : callbacks.entrySet()) {
454 Callback cb = ent.getKey();
455 ArrayList<Pair<String, Integer>> reports = ent.getValue();
456 for (int i=0; i<reports.size(); i++) {
457 Pair<String, Integer> rep = reports.get(i);
458 try {
459 cb.mCallback.opChanged(rep.second, rep.first);
460 } catch (RemoteException e) {
461 }
462 }
463 }
464 }
465 }
466
Dianne Hackbornc2293022013-02-06 23:14:49 -0800467 @Override
468 public void startWatchingMode(int op, String packageName, IAppOpsCallback callback) {
469 synchronized (this) {
470 op = AppOpsManager.opToSwitch(op);
471 Callback cb = mModeWatchers.get(callback.asBinder());
472 if (cb == null) {
473 cb = new Callback(callback);
474 mModeWatchers.put(callback.asBinder(), cb);
475 }
476 if (op != AppOpsManager.OP_NONE) {
477 ArrayList<Callback> cbs = mOpModeWatchers.get(op);
478 if (cbs == null) {
479 cbs = new ArrayList<Callback>();
480 mOpModeWatchers.put(op, cbs);
481 }
482 cbs.add(cb);
483 }
484 if (packageName != null) {
485 ArrayList<Callback> cbs = mPackageModeWatchers.get(packageName);
486 if (cbs == null) {
487 cbs = new ArrayList<Callback>();
488 mPackageModeWatchers.put(packageName, cbs);
489 }
490 cbs.add(cb);
491 }
492 }
493 }
494
495 @Override
496 public void stopWatchingMode(IAppOpsCallback callback) {
497 synchronized (this) {
498 Callback cb = mModeWatchers.remove(callback.asBinder());
499 if (cb != null) {
500 cb.unlinkToDeath();
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700501 for (int i=mOpModeWatchers.size()-1; i>=0; i--) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800502 ArrayList<Callback> cbs = mOpModeWatchers.valueAt(i);
503 cbs.remove(cb);
504 if (cbs.size() <= 0) {
505 mOpModeWatchers.removeAt(i);
506 }
507 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700508 for (int i=mPackageModeWatchers.size()-1; i>=0; i--) {
509 ArrayList<Callback> cbs = mPackageModeWatchers.valueAt(i);
510 cbs.remove(cb);
511 if (cbs.size() <= 0) {
512 mPackageModeWatchers.removeAt(i);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800513 }
514 }
515 }
516 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800517 }
518
519 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700520 public IBinder getToken(IBinder clientToken) {
521 synchronized (this) {
522 ClientState cs = mClients.get(clientToken);
523 if (cs == null) {
524 cs = new ClientState(clientToken);
525 mClients.put(clientToken, cs);
526 }
527 return cs;
528 }
529 }
530
531 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -0800532 public int checkOperation(int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800533 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800534 verifyIncomingOp(code);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800535 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800536 Op op = getOpLocked(AppOpsManager.opToSwitch(code), uid, packageName, false);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800537 if (op == null) {
538 return AppOpsManager.MODE_ALLOWED;
539 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800540 return op.mode;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800541 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800542 }
543
544 @Override
545 public int noteOperation(int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800546 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800547 verifyIncomingOp(code);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800548 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800549 Ops ops = getOpsLocked(uid, packageName, true);
550 if (ops == null) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800551 if (DEBUG) Log.d(TAG, "noteOperation: no op for code " + code + " uid " + uid
552 + " package " + packageName);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800553 return AppOpsManager.MODE_IGNORED;
554 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800555 Op op = getOpLocked(ops, code, true);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800556 if (op.duration == -1) {
557 Slog.w(TAG, "Noting op not finished: uid " + uid + " pkg " + packageName
558 + " code " + code + " time=" + op.time + " duration=" + op.duration);
559 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800560 op.duration = 0;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800561 final int switchCode = AppOpsManager.opToSwitch(code);
562 final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
563 if (switchOp.mode != AppOpsManager.MODE_ALLOWED) {
564 if (DEBUG) Log.d(TAG, "noteOperation: reject #" + op.mode + " for code "
565 + switchCode + " (" + code + ") uid " + uid + " package " + packageName);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800566 op.rejectTime = System.currentTimeMillis();
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800567 return switchOp.mode;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800568 }
569 if (DEBUG) Log.d(TAG, "noteOperation: allowing code " + code + " uid " + uid
570 + " package " + packageName);
571 op.time = System.currentTimeMillis();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800572 op.rejectTime = 0;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800573 return AppOpsManager.MODE_ALLOWED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800574 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800575 }
576
577 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700578 public int startOperation(IBinder token, int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800579 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800580 verifyIncomingOp(code);
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700581 ClientState client = (ClientState)token;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800582 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800583 Ops ops = getOpsLocked(uid, packageName, true);
584 if (ops == null) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800585 if (DEBUG) Log.d(TAG, "startOperation: no op for code " + code + " uid " + uid
586 + " package " + packageName);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800587 return AppOpsManager.MODE_IGNORED;
588 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800589 Op op = getOpLocked(ops, code, true);
590 final int switchCode = AppOpsManager.opToSwitch(code);
591 final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
592 if (switchOp.mode != AppOpsManager.MODE_ALLOWED) {
593 if (DEBUG) Log.d(TAG, "startOperation: reject #" + op.mode + " for code "
594 + switchCode + " (" + code + ") uid " + uid + " package " + packageName);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800595 op.rejectTime = System.currentTimeMillis();
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800596 return switchOp.mode;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800597 }
598 if (DEBUG) Log.d(TAG, "startOperation: allowing code " + code + " uid " + uid
599 + " package " + packageName);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800600 if (op.nesting == 0) {
601 op.time = System.currentTimeMillis();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800602 op.rejectTime = 0;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800603 op.duration = -1;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800604 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800605 op.nesting++;
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700606 if (client.mStartedOps != null) {
607 client.mStartedOps.add(op);
608 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800609 return AppOpsManager.MODE_ALLOWED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800610 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800611 }
612
613 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700614 public void finishOperation(IBinder token, int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800615 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800616 verifyIncomingOp(code);
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700617 ClientState client = (ClientState)token;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800618 synchronized (this) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800619 Op op = getOpLocked(code, uid, packageName, true);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800620 if (op == null) {
621 return;
622 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700623 if (client.mStartedOps != null) {
624 if (!client.mStartedOps.remove(op)) {
625 throw new IllegalStateException("Operation not started: uid" + op.uid
626 + " pkg=" + op.packageName + " op=" + op.op);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800627 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800628 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700629 finishOperationLocked(op);
630 }
631 }
632
633 void finishOperationLocked(Op op) {
634 if (op.nesting <= 1) {
635 if (op.nesting == 1) {
636 op.duration = (int)(System.currentTimeMillis() - op.time);
637 op.time += op.duration;
638 } else {
639 Slog.w(TAG, "Finishing op nesting under-run: uid " + op.uid + " pkg "
640 + op.packageName + " code " + op.op + " time=" + op.time
641 + " duration=" + op.duration + " nesting=" + op.nesting);
642 }
643 op.nesting = 0;
644 } else {
645 op.nesting--;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800646 }
647 }
648
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800649 private void verifyIncomingUid(int uid) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800650 if (uid == Binder.getCallingUid()) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800651 return;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800652 }
653 if (Binder.getCallingPid() == Process.myPid()) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800654 return;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800655 }
656 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
657 Binder.getCallingPid(), Binder.getCallingUid(), null);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800658 }
659
Dianne Hackborn961321f2013-02-05 17:22:41 -0800660 private void verifyIncomingOp(int op) {
661 if (op >= 0 && op < AppOpsManager._NUM_OP) {
662 return;
663 }
664 throw new IllegalArgumentException("Bad operation #" + op);
665 }
666
Dianne Hackborn72e39832013-01-18 18:36:09 -0800667 private Ops getOpsLocked(int uid, String packageName, boolean edit) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800668 HashMap<String, Ops> pkgOps = mUidOps.get(uid);
669 if (pkgOps == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800670 if (!edit) {
671 return null;
672 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800673 pkgOps = new HashMap<String, Ops>();
674 mUidOps.put(uid, pkgOps);
675 }
Dianne Hackborn514074f2013-02-11 10:52:46 -0800676 if (uid == 0) {
677 packageName = "root";
678 } else if (uid == Process.SHELL_UID) {
679 packageName = "com.android.shell";
680 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800681 Ops ops = pkgOps.get(packageName);
682 if (ops == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800683 if (!edit) {
684 return null;
685 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800686 // This is the first time we have seen this package name under this uid,
687 // so let's make sure it is valid.
Dianne Hackborn514074f2013-02-11 10:52:46 -0800688 if (uid != 0) {
689 final long ident = Binder.clearCallingIdentity();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800690 try {
Dianne Hackborn514074f2013-02-11 10:52:46 -0800691 int pkgUid = -1;
692 try {
693 pkgUid = mContext.getPackageManager().getPackageUid(packageName,
694 UserHandle.getUserId(uid));
695 } catch (NameNotFoundException e) {
Dianne Hackborn713df152013-05-17 11:27:57 -0700696 if ("media".equals(packageName)) {
697 pkgUid = Process.MEDIA_UID;
698 }
Dianne Hackborn514074f2013-02-11 10:52:46 -0800699 }
700 if (pkgUid != uid) {
701 // Oops! The package name is not valid for the uid they are calling
702 // under. Abort.
703 Slog.w(TAG, "Bad call: specified package " + packageName
704 + " under uid " + uid + " but it is really " + pkgUid);
705 return null;
706 }
707 } finally {
708 Binder.restoreCallingIdentity(ident);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800709 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800710 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800711 ops = new Ops(packageName, uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800712 pkgOps.put(packageName, ops);
713 }
Dianne Hackborn72e39832013-01-18 18:36:09 -0800714 return ops;
715 }
716
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800717 private void scheduleWriteLocked() {
718 if (!mWriteScheduled) {
719 mWriteScheduled = true;
720 mHandler.postDelayed(mWriteRunner, WRITE_DELAY);
721 }
722 }
723
724 private void scheduleWriteNowLocked() {
725 if (!mWriteScheduled) {
726 mWriteScheduled = true;
727 }
728 mHandler.removeCallbacks(mWriteRunner);
729 mHandler.post(mWriteRunner);
730 }
731
Dianne Hackborn72e39832013-01-18 18:36:09 -0800732 private Op getOpLocked(int code, int uid, String packageName, boolean edit) {
733 Ops ops = getOpsLocked(uid, packageName, edit);
734 if (ops == null) {
735 return null;
736 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800737 return getOpLocked(ops, code, edit);
738 }
739
740 private Op getOpLocked(Ops ops, int code, boolean edit) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800741 Op op = ops.get(code);
742 if (op == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800743 if (!edit) {
744 return null;
745 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700746 op = new Op(ops.uid, ops.packageName, code);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800747 ops.put(code, op);
748 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800749 if (edit) {
750 scheduleWriteLocked();
Dianne Hackborn35654b62013-01-14 17:38:02 -0800751 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800752 return op;
753 }
754
Dianne Hackborn35654b62013-01-14 17:38:02 -0800755 void readState() {
756 synchronized (mFile) {
757 synchronized (this) {
758 FileInputStream stream;
759 try {
760 stream = mFile.openRead();
761 } catch (FileNotFoundException e) {
762 Slog.i(TAG, "No existing app ops " + mFile.getBaseFile() + "; starting empty");
763 return;
764 }
765 boolean success = false;
766 try {
767 XmlPullParser parser = Xml.newPullParser();
768 parser.setInput(stream, null);
769 int type;
770 while ((type = parser.next()) != XmlPullParser.START_TAG
771 && type != XmlPullParser.END_DOCUMENT) {
772 ;
773 }
774
775 if (type != XmlPullParser.START_TAG) {
776 throw new IllegalStateException("no start tag found");
777 }
778
779 int outerDepth = parser.getDepth();
780 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
781 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
782 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
783 continue;
784 }
785
786 String tagName = parser.getName();
787 if (tagName.equals("pkg")) {
Dave Burke0997c5bd2013-08-02 20:25:02 +0000788 readPackage(parser);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800789 } else {
790 Slog.w(TAG, "Unknown element under <app-ops>: "
791 + parser.getName());
792 XmlUtils.skipCurrentTag(parser);
793 }
794 }
795 success = true;
796 } catch (IllegalStateException e) {
797 Slog.w(TAG, "Failed parsing " + e);
798 } catch (NullPointerException e) {
799 Slog.w(TAG, "Failed parsing " + e);
800 } catch (NumberFormatException e) {
801 Slog.w(TAG, "Failed parsing " + e);
802 } catch (XmlPullParserException e) {
803 Slog.w(TAG, "Failed parsing " + e);
804 } catch (IOException e) {
805 Slog.w(TAG, "Failed parsing " + e);
806 } catch (IndexOutOfBoundsException e) {
807 Slog.w(TAG, "Failed parsing " + e);
808 } finally {
809 if (!success) {
810 mUidOps.clear();
811 }
812 try {
813 stream.close();
814 } catch (IOException e) {
815 }
816 }
817 }
818 }
819 }
820
Dave Burke0997c5bd2013-08-02 20:25:02 +0000821 void readPackage(XmlPullParser parser) throws NumberFormatException,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800822 XmlPullParserException, IOException {
823 String pkgName = parser.getAttributeValue(null, "n");
824 int outerDepth = parser.getDepth();
825 int type;
826 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
827 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
828 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
829 continue;
830 }
831
832 String tagName = parser.getName();
833 if (tagName.equals("uid")) {
Dave Burke0997c5bd2013-08-02 20:25:02 +0000834 readUid(parser, pkgName);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800835 } else {
836 Slog.w(TAG, "Unknown element under <pkg>: "
837 + parser.getName());
838 XmlUtils.skipCurrentTag(parser);
839 }
840 }
841 }
842
Dave Burke0997c5bd2013-08-02 20:25:02 +0000843 void readUid(XmlPullParser parser, String pkgName) throws NumberFormatException,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800844 XmlPullParserException, IOException {
845 int uid = Integer.parseInt(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("op")) {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700856 Op op = new Op(uid, pkgName, Integer.parseInt(parser.getAttributeValue(null, "n")));
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800857 String mode = parser.getAttributeValue(null, "m");
858 if (mode != null) {
Dave Burke0997c5bd2013-08-02 20:25:02 +0000859 op.mode = Integer.parseInt(mode);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800860 }
861 String time = parser.getAttributeValue(null, "t");
862 if (time != null) {
863 op.time = Long.parseLong(time);
864 }
865 time = parser.getAttributeValue(null, "r");
866 if (time != null) {
867 op.rejectTime = Long.parseLong(time);
868 }
869 String dur = parser.getAttributeValue(null, "d");
870 if (dur != null) {
871 op.duration = Integer.parseInt(dur);
872 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800873 HashMap<String, Ops> pkgOps = mUidOps.get(uid);
874 if (pkgOps == null) {
875 pkgOps = new HashMap<String, Ops>();
876 mUidOps.put(uid, pkgOps);
877 }
878 Ops ops = pkgOps.get(pkgName);
879 if (ops == null) {
880 ops = new Ops(pkgName, uid);
881 pkgOps.put(pkgName, ops);
882 }
883 ops.put(op.op, op);
884 } else {
885 Slog.w(TAG, "Unknown element under <pkg>: "
886 + parser.getName());
887 XmlUtils.skipCurrentTag(parser);
888 }
889 }
890 }
891
892 void writeState() {
893 synchronized (mFile) {
894 List<AppOpsManager.PackageOps> allOps = getPackagesForOps(null);
895
896 FileOutputStream stream;
897 try {
898 stream = mFile.startWrite();
899 } catch (IOException e) {
900 Slog.w(TAG, "Failed to write state: " + e);
901 return;
902 }
903
904 try {
905 XmlSerializer out = new FastXmlSerializer();
906 out.setOutput(stream, "utf-8");
907 out.startDocument(null, true);
908 out.startTag(null, "app-ops");
909
910 if (allOps != null) {
911 String lastPkg = null;
912 for (int i=0; i<allOps.size(); i++) {
913 AppOpsManager.PackageOps pkg = allOps.get(i);
914 if (!pkg.getPackageName().equals(lastPkg)) {
915 if (lastPkg != null) {
916 out.endTag(null, "pkg");
917 }
918 lastPkg = pkg.getPackageName();
919 out.startTag(null, "pkg");
920 out.attribute(null, "n", lastPkg);
921 }
922 out.startTag(null, "uid");
923 out.attribute(null, "n", Integer.toString(pkg.getUid()));
924 List<AppOpsManager.OpEntry> ops = pkg.getOps();
925 for (int j=0; j<ops.size(); j++) {
926 AppOpsManager.OpEntry op = ops.get(j);
927 out.startTag(null, "op");
928 out.attribute(null, "n", Integer.toString(op.getOp()));
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800929 if (op.getMode() != AppOpsManager.MODE_ALLOWED) {
930 out.attribute(null, "m", Integer.toString(op.getMode()));
931 }
932 long time = op.getTime();
933 if (time != 0) {
934 out.attribute(null, "t", Long.toString(time));
935 }
936 time = op.getRejectTime();
937 if (time != 0) {
938 out.attribute(null, "r", Long.toString(time));
939 }
940 int dur = op.getDuration();
941 if (dur != 0) {
942 out.attribute(null, "d", Integer.toString(dur));
943 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800944 out.endTag(null, "op");
945 }
946 out.endTag(null, "uid");
947 }
948 if (lastPkg != null) {
949 out.endTag(null, "pkg");
950 }
951 }
952
953 out.endTag(null, "app-ops");
954 out.endDocument();
955 mFile.finishWrite(stream);
956 } catch (IOException e) {
957 Slog.w(TAG, "Failed to write state, restoring backup.", e);
958 mFile.failWrite(stream);
959 }
960 }
961 }
962
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800963 @Override
964 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
965 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
966 != PackageManager.PERMISSION_GRANTED) {
967 pw.println("Permission Denial: can't dump ApOps service from from pid="
968 + Binder.getCallingPid()
969 + ", uid=" + Binder.getCallingUid());
970 return;
971 }
972
973 synchronized (this) {
974 pw.println("Current AppOps Service state:");
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800975 final long now = System.currentTimeMillis();
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700976 boolean needSep = false;
977 if (mOpModeWatchers.size() > 0) {
978 needSep = true;
979 pw.println(" Op mode watchers:");
980 for (int i=0; i<mOpModeWatchers.size(); i++) {
981 pw.print(" Op "); pw.print(AppOpsManager.opToName(mOpModeWatchers.keyAt(i)));
982 pw.println(":");
983 ArrayList<Callback> callbacks = mOpModeWatchers.valueAt(i);
984 for (int j=0; j<callbacks.size(); j++) {
985 pw.print(" #"); pw.print(j); pw.print(": ");
986 pw.println(callbacks.get(j));
987 }
988 }
989 }
990 if (mPackageModeWatchers.size() > 0) {
991 needSep = true;
992 pw.println(" Package mode watchers:");
993 for (int i=0; i<mPackageModeWatchers.size(); i++) {
994 pw.print(" Pkg "); pw.print(mPackageModeWatchers.keyAt(i));
995 pw.println(":");
996 ArrayList<Callback> callbacks = mPackageModeWatchers.valueAt(i);
997 for (int j=0; j<callbacks.size(); j++) {
998 pw.print(" #"); pw.print(j); pw.print(": ");
999 pw.println(callbacks.get(j));
1000 }
1001 }
1002 }
1003 if (mModeWatchers.size() > 0) {
1004 needSep = true;
1005 pw.println(" All mode watchers:");
1006 for (int i=0; i<mModeWatchers.size(); i++) {
1007 pw.print(" "); pw.print(mModeWatchers.keyAt(i));
1008 pw.print(" -> "); pw.println(mModeWatchers.valueAt(i));
1009 }
1010 }
1011 if (mClients.size() > 0) {
1012 needSep = true;
1013 pw.println(" Clients:");
1014 for (int i=0; i<mClients.size(); i++) {
1015 pw.print(" "); pw.print(mClients.keyAt(i)); pw.println(":");
1016 ClientState cs = mClients.valueAt(i);
1017 pw.print(" "); pw.println(cs);
1018 if (cs.mStartedOps != null && cs.mStartedOps.size() > 0) {
1019 pw.println(" Started ops:");
1020 for (int j=0; j<cs.mStartedOps.size(); j++) {
1021 Op op = cs.mStartedOps.get(j);
1022 pw.print(" "); pw.print("uid="); pw.print(op.uid);
1023 pw.print(" pkg="); pw.print(op.packageName);
1024 pw.print(" op="); pw.println(AppOpsManager.opToName(op.op));
1025 }
1026 }
1027 }
1028 }
1029 if (needSep) {
1030 pw.println();
1031 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001032 for (int i=0; i<mUidOps.size(); i++) {
1033 pw.print(" Uid "); UserHandle.formatUid(pw, mUidOps.keyAt(i)); pw.println(":");
1034 HashMap<String, Ops> pkgOps = mUidOps.valueAt(i);
1035 for (Ops ops : pkgOps.values()) {
1036 pw.print(" Package "); pw.print(ops.packageName); pw.println(":");
1037 for (int j=0; j<ops.size(); j++) {
1038 Op op = ops.valueAt(j);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001039 pw.print(" "); pw.print(AppOpsManager.opToName(op.op));
1040 pw.print(": mode="); pw.print(op.mode);
1041 if (op.time != 0) {
1042 pw.print("; time="); TimeUtils.formatDuration(now-op.time, pw);
1043 pw.print(" ago");
1044 }
1045 if (op.rejectTime != 0) {
1046 pw.print("; rejectTime="); TimeUtils.formatDuration(now-op.rejectTime, pw);
1047 pw.print(" ago");
1048 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001049 if (op.duration == -1) {
1050 pw.println(" (running)");
1051 } else {
1052 pw.print("; duration=");
1053 TimeUtils.formatDuration(op.duration, pw);
1054 pw.println();
1055 }
1056 }
1057 }
1058 }
1059 }
1060 }
1061}