blob: 7af95f3f84e648362f6d114873819a16afb99db6 [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;
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);
438 if (curOp.mode != AppOpsManager.MODE_ALLOWED) {
439 curOp.mode = AppOpsManager.MODE_ALLOWED;
440 changed = true;
441 callbacks = addCallbacks(callbacks, packageName, curOp.op,
442 mOpModeWatchers.get(curOp.op));
443 callbacks = addCallbacks(callbacks, packageName, curOp.op,
444 mPackageModeWatchers.get(packageName));
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700445 if (curOp.time == 0 && curOp.rejectTime == 0) {
446 pkgOps.removeAt(j);
447 }
Dianne Hackborn607b4142013-08-02 18:10:10 -0700448 }
449 }
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700450 if (pkgOps.size() == 0) {
451 it.remove();
452 }
453 }
454 if (packages.size() == 0) {
455 mUidOps.removeAt(i);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700456 }
457 }
458 if (changed) {
459 scheduleWriteNowLocked();
460 }
461 }
462 if (callbacks != null) {
463 for (Map.Entry<Callback, ArrayList<Pair<String, Integer>>> ent : callbacks.entrySet()) {
464 Callback cb = ent.getKey();
465 ArrayList<Pair<String, Integer>> reports = ent.getValue();
466 for (int i=0; i<reports.size(); i++) {
467 Pair<String, Integer> rep = reports.get(i);
468 try {
469 cb.mCallback.opChanged(rep.second, rep.first);
470 } catch (RemoteException e) {
471 }
472 }
473 }
474 }
475 }
476
Dianne Hackbornc2293022013-02-06 23:14:49 -0800477 @Override
478 public void startWatchingMode(int op, String packageName, IAppOpsCallback callback) {
479 synchronized (this) {
480 op = AppOpsManager.opToSwitch(op);
481 Callback cb = mModeWatchers.get(callback.asBinder());
482 if (cb == null) {
483 cb = new Callback(callback);
484 mModeWatchers.put(callback.asBinder(), cb);
485 }
486 if (op != AppOpsManager.OP_NONE) {
487 ArrayList<Callback> cbs = mOpModeWatchers.get(op);
488 if (cbs == null) {
489 cbs = new ArrayList<Callback>();
490 mOpModeWatchers.put(op, cbs);
491 }
492 cbs.add(cb);
493 }
494 if (packageName != null) {
495 ArrayList<Callback> cbs = mPackageModeWatchers.get(packageName);
496 if (cbs == null) {
497 cbs = new ArrayList<Callback>();
498 mPackageModeWatchers.put(packageName, cbs);
499 }
500 cbs.add(cb);
501 }
502 }
503 }
504
505 @Override
506 public void stopWatchingMode(IAppOpsCallback callback) {
507 synchronized (this) {
508 Callback cb = mModeWatchers.remove(callback.asBinder());
509 if (cb != null) {
510 cb.unlinkToDeath();
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700511 for (int i=mOpModeWatchers.size()-1; i>=0; i--) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800512 ArrayList<Callback> cbs = mOpModeWatchers.valueAt(i);
513 cbs.remove(cb);
514 if (cbs.size() <= 0) {
515 mOpModeWatchers.removeAt(i);
516 }
517 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700518 for (int i=mPackageModeWatchers.size()-1; i>=0; i--) {
519 ArrayList<Callback> cbs = mPackageModeWatchers.valueAt(i);
520 cbs.remove(cb);
521 if (cbs.size() <= 0) {
522 mPackageModeWatchers.removeAt(i);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800523 }
524 }
525 }
526 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800527 }
528
529 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700530 public IBinder getToken(IBinder clientToken) {
531 synchronized (this) {
532 ClientState cs = mClients.get(clientToken);
533 if (cs == null) {
534 cs = new ClientState(clientToken);
535 mClients.put(clientToken, cs);
536 }
537 return cs;
538 }
539 }
540
541 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -0800542 public int checkOperation(int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800543 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800544 verifyIncomingOp(code);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800545 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800546 Op op = getOpLocked(AppOpsManager.opToSwitch(code), uid, packageName, false);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800547 if (op == null) {
548 return AppOpsManager.MODE_ALLOWED;
549 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800550 return op.mode;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800551 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800552 }
553
554 @Override
555 public int noteOperation(int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800556 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800557 verifyIncomingOp(code);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800558 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800559 Ops ops = getOpsLocked(uid, packageName, true);
560 if (ops == null) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800561 if (DEBUG) Log.d(TAG, "noteOperation: no op for code " + code + " uid " + uid
562 + " package " + packageName);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800563 return AppOpsManager.MODE_IGNORED;
564 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800565 Op op = getOpLocked(ops, code, true);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800566 if (op.duration == -1) {
567 Slog.w(TAG, "Noting op not finished: uid " + uid + " pkg " + packageName
568 + " code " + code + " time=" + op.time + " duration=" + op.duration);
569 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800570 op.duration = 0;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800571 final int switchCode = AppOpsManager.opToSwitch(code);
572 final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
573 if (switchOp.mode != AppOpsManager.MODE_ALLOWED) {
574 if (DEBUG) Log.d(TAG, "noteOperation: reject #" + op.mode + " for code "
575 + switchCode + " (" + code + ") uid " + uid + " package " + packageName);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800576 op.rejectTime = System.currentTimeMillis();
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800577 return switchOp.mode;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800578 }
579 if (DEBUG) Log.d(TAG, "noteOperation: allowing code " + code + " uid " + uid
580 + " package " + packageName);
581 op.time = System.currentTimeMillis();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800582 op.rejectTime = 0;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800583 return AppOpsManager.MODE_ALLOWED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800584 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800585 }
586
587 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700588 public int startOperation(IBinder token, int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800589 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800590 verifyIncomingOp(code);
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700591 ClientState client = (ClientState)token;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800592 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800593 Ops ops = getOpsLocked(uid, packageName, true);
594 if (ops == null) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800595 if (DEBUG) Log.d(TAG, "startOperation: no op for code " + code + " uid " + uid
596 + " package " + packageName);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800597 return AppOpsManager.MODE_IGNORED;
598 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800599 Op op = getOpLocked(ops, code, true);
600 final int switchCode = AppOpsManager.opToSwitch(code);
601 final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
602 if (switchOp.mode != AppOpsManager.MODE_ALLOWED) {
603 if (DEBUG) Log.d(TAG, "startOperation: reject #" + op.mode + " for code "
604 + switchCode + " (" + code + ") uid " + uid + " package " + packageName);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800605 op.rejectTime = System.currentTimeMillis();
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800606 return switchOp.mode;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800607 }
608 if (DEBUG) Log.d(TAG, "startOperation: allowing code " + code + " uid " + uid
609 + " package " + packageName);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800610 if (op.nesting == 0) {
611 op.time = System.currentTimeMillis();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800612 op.rejectTime = 0;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800613 op.duration = -1;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800614 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800615 op.nesting++;
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700616 if (client.mStartedOps != null) {
617 client.mStartedOps.add(op);
618 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800619 return AppOpsManager.MODE_ALLOWED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800620 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800621 }
622
623 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700624 public void finishOperation(IBinder token, int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800625 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800626 verifyIncomingOp(code);
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700627 ClientState client = (ClientState)token;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800628 synchronized (this) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800629 Op op = getOpLocked(code, uid, packageName, true);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800630 if (op == null) {
631 return;
632 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700633 if (client.mStartedOps != null) {
634 if (!client.mStartedOps.remove(op)) {
635 throw new IllegalStateException("Operation not started: uid" + op.uid
636 + " pkg=" + op.packageName + " op=" + op.op);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800637 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800638 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700639 finishOperationLocked(op);
640 }
641 }
642
643 void finishOperationLocked(Op op) {
644 if (op.nesting <= 1) {
645 if (op.nesting == 1) {
646 op.duration = (int)(System.currentTimeMillis() - op.time);
647 op.time += op.duration;
648 } else {
649 Slog.w(TAG, "Finishing op nesting under-run: uid " + op.uid + " pkg "
650 + op.packageName + " code " + op.op + " time=" + op.time
651 + " duration=" + op.duration + " nesting=" + op.nesting);
652 }
653 op.nesting = 0;
654 } else {
655 op.nesting--;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800656 }
657 }
658
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800659 private void verifyIncomingUid(int uid) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800660 if (uid == Binder.getCallingUid()) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800661 return;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800662 }
663 if (Binder.getCallingPid() == Process.myPid()) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800664 return;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800665 }
666 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
667 Binder.getCallingPid(), Binder.getCallingUid(), null);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800668 }
669
Dianne Hackborn961321f2013-02-05 17:22:41 -0800670 private void verifyIncomingOp(int op) {
671 if (op >= 0 && op < AppOpsManager._NUM_OP) {
672 return;
673 }
674 throw new IllegalArgumentException("Bad operation #" + op);
675 }
676
Dianne Hackborn72e39832013-01-18 18:36:09 -0800677 private Ops getOpsLocked(int uid, String packageName, boolean edit) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800678 HashMap<String, Ops> pkgOps = mUidOps.get(uid);
679 if (pkgOps == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800680 if (!edit) {
681 return null;
682 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800683 pkgOps = new HashMap<String, Ops>();
684 mUidOps.put(uid, pkgOps);
685 }
Dianne Hackborn514074f2013-02-11 10:52:46 -0800686 if (uid == 0) {
687 packageName = "root";
688 } else if (uid == Process.SHELL_UID) {
689 packageName = "com.android.shell";
690 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800691 Ops ops = pkgOps.get(packageName);
692 if (ops == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800693 if (!edit) {
694 return null;
695 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800696 // This is the first time we have seen this package name under this uid,
697 // so let's make sure it is valid.
Dianne Hackborn514074f2013-02-11 10:52:46 -0800698 if (uid != 0) {
699 final long ident = Binder.clearCallingIdentity();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800700 try {
Dianne Hackborn514074f2013-02-11 10:52:46 -0800701 int pkgUid = -1;
702 try {
703 pkgUid = mContext.getPackageManager().getPackageUid(packageName,
704 UserHandle.getUserId(uid));
705 } catch (NameNotFoundException e) {
Dianne Hackborn713df152013-05-17 11:27:57 -0700706 if ("media".equals(packageName)) {
707 pkgUid = Process.MEDIA_UID;
708 }
Dianne Hackborn514074f2013-02-11 10:52:46 -0800709 }
710 if (pkgUid != uid) {
711 // Oops! The package name is not valid for the uid they are calling
712 // under. Abort.
713 Slog.w(TAG, "Bad call: specified package " + packageName
714 + " under uid " + uid + " but it is really " + pkgUid);
715 return null;
716 }
717 } finally {
718 Binder.restoreCallingIdentity(ident);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800719 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800720 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800721 ops = new Ops(packageName, uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800722 pkgOps.put(packageName, ops);
723 }
Dianne Hackborn72e39832013-01-18 18:36:09 -0800724 return ops;
725 }
726
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800727 private void scheduleWriteLocked() {
728 if (!mWriteScheduled) {
729 mWriteScheduled = true;
730 mHandler.postDelayed(mWriteRunner, WRITE_DELAY);
731 }
732 }
733
734 private void scheduleWriteNowLocked() {
735 if (!mWriteScheduled) {
736 mWriteScheduled = true;
737 }
738 mHandler.removeCallbacks(mWriteRunner);
739 mHandler.post(mWriteRunner);
740 }
741
Dianne Hackborn72e39832013-01-18 18:36:09 -0800742 private Op getOpLocked(int code, int uid, String packageName, boolean edit) {
743 Ops ops = getOpsLocked(uid, packageName, edit);
744 if (ops == null) {
745 return null;
746 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800747 return getOpLocked(ops, code, edit);
748 }
749
750 private Op getOpLocked(Ops ops, int code, boolean edit) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800751 Op op = ops.get(code);
752 if (op == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800753 if (!edit) {
754 return null;
755 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700756 op = new Op(ops.uid, ops.packageName, code);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800757 ops.put(code, op);
758 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800759 if (edit) {
760 scheduleWriteLocked();
Dianne Hackborn35654b62013-01-14 17:38:02 -0800761 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800762 return op;
763 }
764
Dianne Hackborn35654b62013-01-14 17:38:02 -0800765 void readState() {
766 synchronized (mFile) {
767 synchronized (this) {
768 FileInputStream stream;
769 try {
770 stream = mFile.openRead();
771 } catch (FileNotFoundException e) {
772 Slog.i(TAG, "No existing app ops " + mFile.getBaseFile() + "; starting empty");
773 return;
774 }
775 boolean success = false;
776 try {
777 XmlPullParser parser = Xml.newPullParser();
778 parser.setInput(stream, null);
779 int type;
780 while ((type = parser.next()) != XmlPullParser.START_TAG
781 && type != XmlPullParser.END_DOCUMENT) {
782 ;
783 }
784
785 if (type != XmlPullParser.START_TAG) {
786 throw new IllegalStateException("no start tag found");
787 }
788
789 int outerDepth = parser.getDepth();
790 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
791 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
792 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
793 continue;
794 }
795
796 String tagName = parser.getName();
797 if (tagName.equals("pkg")) {
Dave Burke0997c5bd2013-08-02 20:25:02 +0000798 readPackage(parser);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800799 } else {
800 Slog.w(TAG, "Unknown element under <app-ops>: "
801 + parser.getName());
802 XmlUtils.skipCurrentTag(parser);
803 }
804 }
805 success = true;
806 } catch (IllegalStateException e) {
807 Slog.w(TAG, "Failed parsing " + e);
808 } catch (NullPointerException e) {
809 Slog.w(TAG, "Failed parsing " + e);
810 } catch (NumberFormatException e) {
811 Slog.w(TAG, "Failed parsing " + e);
812 } catch (XmlPullParserException e) {
813 Slog.w(TAG, "Failed parsing " + e);
814 } catch (IOException e) {
815 Slog.w(TAG, "Failed parsing " + e);
816 } catch (IndexOutOfBoundsException e) {
817 Slog.w(TAG, "Failed parsing " + e);
818 } finally {
819 if (!success) {
820 mUidOps.clear();
821 }
822 try {
823 stream.close();
824 } catch (IOException e) {
825 }
826 }
827 }
828 }
829 }
830
Dave Burke0997c5bd2013-08-02 20:25:02 +0000831 void readPackage(XmlPullParser parser) throws NumberFormatException,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800832 XmlPullParserException, IOException {
833 String pkgName = parser.getAttributeValue(null, "n");
834 int outerDepth = parser.getDepth();
835 int type;
836 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
837 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
838 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
839 continue;
840 }
841
842 String tagName = parser.getName();
843 if (tagName.equals("uid")) {
Dave Burke0997c5bd2013-08-02 20:25:02 +0000844 readUid(parser, pkgName);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800845 } else {
846 Slog.w(TAG, "Unknown element under <pkg>: "
847 + parser.getName());
848 XmlUtils.skipCurrentTag(parser);
849 }
850 }
851 }
852
Dave Burke0997c5bd2013-08-02 20:25:02 +0000853 void readUid(XmlPullParser parser, String pkgName) throws NumberFormatException,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800854 XmlPullParserException, IOException {
855 int uid = Integer.parseInt(parser.getAttributeValue(null, "n"));
856 int outerDepth = parser.getDepth();
857 int type;
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("op")) {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700866 Op op = new Op(uid, pkgName, Integer.parseInt(parser.getAttributeValue(null, "n")));
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800867 String mode = parser.getAttributeValue(null, "m");
868 if (mode != null) {
Dave Burke0997c5bd2013-08-02 20:25:02 +0000869 op.mode = Integer.parseInt(mode);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800870 }
871 String time = parser.getAttributeValue(null, "t");
872 if (time != null) {
873 op.time = Long.parseLong(time);
874 }
875 time = parser.getAttributeValue(null, "r");
876 if (time != null) {
877 op.rejectTime = Long.parseLong(time);
878 }
879 String dur = parser.getAttributeValue(null, "d");
880 if (dur != null) {
881 op.duration = Integer.parseInt(dur);
882 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800883 HashMap<String, Ops> pkgOps = mUidOps.get(uid);
884 if (pkgOps == null) {
885 pkgOps = new HashMap<String, Ops>();
886 mUidOps.put(uid, pkgOps);
887 }
888 Ops ops = pkgOps.get(pkgName);
889 if (ops == null) {
890 ops = new Ops(pkgName, uid);
891 pkgOps.put(pkgName, ops);
892 }
893 ops.put(op.op, op);
894 } else {
895 Slog.w(TAG, "Unknown element under <pkg>: "
896 + parser.getName());
897 XmlUtils.skipCurrentTag(parser);
898 }
899 }
900 }
901
902 void writeState() {
903 synchronized (mFile) {
904 List<AppOpsManager.PackageOps> allOps = getPackagesForOps(null);
905
906 FileOutputStream stream;
907 try {
908 stream = mFile.startWrite();
909 } catch (IOException e) {
910 Slog.w(TAG, "Failed to write state: " + e);
911 return;
912 }
913
914 try {
915 XmlSerializer out = new FastXmlSerializer();
916 out.setOutput(stream, "utf-8");
917 out.startDocument(null, true);
918 out.startTag(null, "app-ops");
919
920 if (allOps != null) {
921 String lastPkg = null;
922 for (int i=0; i<allOps.size(); i++) {
923 AppOpsManager.PackageOps pkg = allOps.get(i);
924 if (!pkg.getPackageName().equals(lastPkg)) {
925 if (lastPkg != null) {
926 out.endTag(null, "pkg");
927 }
928 lastPkg = pkg.getPackageName();
929 out.startTag(null, "pkg");
930 out.attribute(null, "n", lastPkg);
931 }
932 out.startTag(null, "uid");
933 out.attribute(null, "n", Integer.toString(pkg.getUid()));
934 List<AppOpsManager.OpEntry> ops = pkg.getOps();
935 for (int j=0; j<ops.size(); j++) {
936 AppOpsManager.OpEntry op = ops.get(j);
937 out.startTag(null, "op");
938 out.attribute(null, "n", Integer.toString(op.getOp()));
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800939 if (op.getMode() != AppOpsManager.MODE_ALLOWED) {
940 out.attribute(null, "m", Integer.toString(op.getMode()));
941 }
942 long time = op.getTime();
943 if (time != 0) {
944 out.attribute(null, "t", Long.toString(time));
945 }
946 time = op.getRejectTime();
947 if (time != 0) {
948 out.attribute(null, "r", Long.toString(time));
949 }
950 int dur = op.getDuration();
951 if (dur != 0) {
952 out.attribute(null, "d", Integer.toString(dur));
953 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800954 out.endTag(null, "op");
955 }
956 out.endTag(null, "uid");
957 }
958 if (lastPkg != null) {
959 out.endTag(null, "pkg");
960 }
961 }
962
963 out.endTag(null, "app-ops");
964 out.endDocument();
965 mFile.finishWrite(stream);
966 } catch (IOException e) {
967 Slog.w(TAG, "Failed to write state, restoring backup.", e);
968 mFile.failWrite(stream);
969 }
970 }
971 }
972
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800973 @Override
974 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
975 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
976 != PackageManager.PERMISSION_GRANTED) {
977 pw.println("Permission Denial: can't dump ApOps service from from pid="
978 + Binder.getCallingPid()
979 + ", uid=" + Binder.getCallingUid());
980 return;
981 }
982
983 synchronized (this) {
984 pw.println("Current AppOps Service state:");
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800985 final long now = System.currentTimeMillis();
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700986 boolean needSep = false;
987 if (mOpModeWatchers.size() > 0) {
988 needSep = true;
989 pw.println(" Op mode watchers:");
990 for (int i=0; i<mOpModeWatchers.size(); i++) {
991 pw.print(" Op "); pw.print(AppOpsManager.opToName(mOpModeWatchers.keyAt(i)));
992 pw.println(":");
993 ArrayList<Callback> callbacks = mOpModeWatchers.valueAt(i);
994 for (int j=0; j<callbacks.size(); j++) {
995 pw.print(" #"); pw.print(j); pw.print(": ");
996 pw.println(callbacks.get(j));
997 }
998 }
999 }
1000 if (mPackageModeWatchers.size() > 0) {
1001 needSep = true;
1002 pw.println(" Package mode watchers:");
1003 for (int i=0; i<mPackageModeWatchers.size(); i++) {
1004 pw.print(" Pkg "); pw.print(mPackageModeWatchers.keyAt(i));
1005 pw.println(":");
1006 ArrayList<Callback> callbacks = mPackageModeWatchers.valueAt(i);
1007 for (int j=0; j<callbacks.size(); j++) {
1008 pw.print(" #"); pw.print(j); pw.print(": ");
1009 pw.println(callbacks.get(j));
1010 }
1011 }
1012 }
1013 if (mModeWatchers.size() > 0) {
1014 needSep = true;
1015 pw.println(" All mode watchers:");
1016 for (int i=0; i<mModeWatchers.size(); i++) {
1017 pw.print(" "); pw.print(mModeWatchers.keyAt(i));
1018 pw.print(" -> "); pw.println(mModeWatchers.valueAt(i));
1019 }
1020 }
1021 if (mClients.size() > 0) {
1022 needSep = true;
1023 pw.println(" Clients:");
1024 for (int i=0; i<mClients.size(); i++) {
1025 pw.print(" "); pw.print(mClients.keyAt(i)); pw.println(":");
1026 ClientState cs = mClients.valueAt(i);
1027 pw.print(" "); pw.println(cs);
1028 if (cs.mStartedOps != null && cs.mStartedOps.size() > 0) {
1029 pw.println(" Started ops:");
1030 for (int j=0; j<cs.mStartedOps.size(); j++) {
1031 Op op = cs.mStartedOps.get(j);
1032 pw.print(" "); pw.print("uid="); pw.print(op.uid);
1033 pw.print(" pkg="); pw.print(op.packageName);
1034 pw.print(" op="); pw.println(AppOpsManager.opToName(op.op));
1035 }
1036 }
1037 }
1038 }
1039 if (needSep) {
1040 pw.println();
1041 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001042 for (int i=0; i<mUidOps.size(); i++) {
1043 pw.print(" Uid "); UserHandle.formatUid(pw, mUidOps.keyAt(i)); pw.println(":");
1044 HashMap<String, Ops> pkgOps = mUidOps.valueAt(i);
1045 for (Ops ops : pkgOps.values()) {
1046 pw.print(" Package "); pw.print(ops.packageName); pw.println(":");
1047 for (int j=0; j<ops.size(); j++) {
1048 Op op = ops.valueAt(j);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001049 pw.print(" "); pw.print(AppOpsManager.opToName(op.op));
1050 pw.print(": mode="); pw.print(op.mode);
1051 if (op.time != 0) {
1052 pw.print("; time="); TimeUtils.formatDuration(now-op.time, pw);
1053 pw.print(" ago");
1054 }
1055 if (op.rejectTime != 0) {
1056 pw.print("; rejectTime="); TimeUtils.formatDuration(now-op.rejectTime, pw);
1057 pw.print(" ago");
1058 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001059 if (op.duration == -1) {
1060 pw.println(" (running)");
1061 } else {
1062 pw.print("; duration=");
1063 TimeUtils.formatDuration(op.duration, pw);
1064 pw.println();
1065 }
1066 }
1067 }
1068 }
1069 }
1070 }
1071}