blob: f8eac371e63372ee34f20ed3046a0ee4e8c0bd8b [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
Jason Monk1c7c3192014-06-26 12:52:18 -040032import android.app.ActivityThread;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080033import android.app.AppOpsManager;
34import android.content.Context;
Jason Monk1c7c3192014-06-26 12:52:18 -040035import android.content.pm.ApplicationInfo;
36import android.content.pm.IPackageManager;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080037import android.content.pm.PackageManager;
38import android.content.pm.PackageManager.NameNotFoundException;
John Spurlock7b414672014-07-18 13:02:39 -040039import android.media.AudioAttributes;
Dianne Hackborn35654b62013-01-14 17:38:02 -080040import android.os.AsyncTask;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080041import android.os.Binder;
Jason Monk62062992014-05-06 09:55:28 -040042import android.os.Bundle;
Dianne Hackborn35654b62013-01-14 17:38:02 -080043import android.os.Handler;
Dianne Hackbornc2293022013-02-06 23:14:49 -080044import android.os.IBinder;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080045import android.os.Process;
Dianne Hackbornc2293022013-02-06 23:14:49 -080046import android.os.RemoteException;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080047import android.os.ServiceManager;
48import android.os.UserHandle;
Dianne Hackborne98f5db2013-07-17 17:23:25 -070049import android.util.ArrayMap;
John Spurlock1af30c72014-03-10 08:33:35 -040050import android.util.ArraySet;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080051import android.util.AtomicFile;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -080052import android.util.Log;
Dianne Hackborn607b4142013-08-02 18:10:10 -070053import android.util.Pair;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080054import android.util.Slog;
55import android.util.SparseArray;
Jason Monk62062992014-05-06 09:55:28 -040056import android.util.SparseIntArray;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080057import android.util.TimeUtils;
Dianne Hackborn35654b62013-01-14 17:38:02 -080058import android.util.Xml;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080059
60import com.android.internal.app.IAppOpsService;
Dianne Hackbornc2293022013-02-06 23:14:49 -080061import com.android.internal.app.IAppOpsCallback;
Dianne Hackborn35654b62013-01-14 17:38:02 -080062import com.android.internal.util.FastXmlSerializer;
63import com.android.internal.util.XmlUtils;
64
65import org.xmlpull.v1.XmlPullParser;
66import org.xmlpull.v1.XmlPullParserException;
67import org.xmlpull.v1.XmlSerializer;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080068
69public class AppOpsService extends IAppOpsService.Stub {
70 static final String TAG = "AppOps";
Dianne Hackborn35654b62013-01-14 17:38:02 -080071 static final boolean DEBUG = false;
72
73 // Write at most every 30 minutes.
74 static final long WRITE_DELAY = DEBUG ? 1000 : 30*60*1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080075
76 Context mContext;
77 final AtomicFile mFile;
Dianne Hackborn35654b62013-01-14 17:38:02 -080078 final Handler mHandler;
79
80 boolean mWriteScheduled;
81 final Runnable mWriteRunner = new Runnable() {
82 public void run() {
83 synchronized (AppOpsService.this) {
84 mWriteScheduled = false;
85 AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
86 @Override protected Void doInBackground(Void... params) {
87 writeState();
88 return null;
89 }
90 };
91 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
92 }
93 }
94 };
Dianne Hackborna06de0f2012-12-11 16:34:47 -080095
96 final SparseArray<HashMap<String, Ops>> mUidOps
97 = new SparseArray<HashMap<String, Ops>>();
98
Jason Monk62062992014-05-06 09:55:28 -040099 private final SparseArray<boolean[]> mOpRestrictions = new SparseArray<boolean[]>();
100
Dianne Hackbornc2293022013-02-06 23:14:49 -0800101 public final static class Ops extends SparseArray<Op> {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800102 public final String packageName;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800103 public final int uid;
Jason Monk1c7c3192014-06-26 12:52:18 -0400104 public final boolean isPrivileged;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800105
Jason Monk1c7c3192014-06-26 12:52:18 -0400106 public Ops(String _packageName, int _uid, boolean _isPrivileged) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800107 packageName = _packageName;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800108 uid = _uid;
Jason Monk1c7c3192014-06-26 12:52:18 -0400109 isPrivileged = _isPrivileged;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800110 }
111 }
112
Dianne Hackbornc2293022013-02-06 23:14:49 -0800113 public final static class Op {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700114 public final int uid;
115 public final String packageName;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800116 public final int op;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800117 public int mode;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800118 public int duration;
119 public long time;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800120 public long rejectTime;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800121 public int nesting;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800122
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700123 public Op(int _uid, String _packageName, int _op) {
124 uid = _uid;
125 packageName = _packageName;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800126 op = _op;
David Braunf5d83192013-09-16 13:43:51 -0700127 mode = AppOpsManager.opToDefaultMode(op);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800128 }
129 }
130
Dianne Hackbornc2293022013-02-06 23:14:49 -0800131 final SparseArray<ArrayList<Callback>> mOpModeWatchers
132 = new SparseArray<ArrayList<Callback>>();
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700133 final ArrayMap<String, ArrayList<Callback>> mPackageModeWatchers
134 = new ArrayMap<String, ArrayList<Callback>>();
135 final ArrayMap<IBinder, Callback> mModeWatchers
136 = new ArrayMap<IBinder, Callback>();
John Spurlock1af30c72014-03-10 08:33:35 -0400137 final SparseArray<SparseArray<Restriction>> mAudioRestrictions
138 = new SparseArray<SparseArray<Restriction>>();
Dianne Hackbornc2293022013-02-06 23:14:49 -0800139
140 public final class Callback implements DeathRecipient {
141 final IAppOpsCallback mCallback;
142
143 public Callback(IAppOpsCallback callback) {
144 mCallback = callback;
145 try {
146 mCallback.asBinder().linkToDeath(this, 0);
147 } catch (RemoteException e) {
148 }
149 }
150
151 public void unlinkToDeath() {
152 mCallback.asBinder().unlinkToDeath(this, 0);
153 }
154
155 @Override
156 public void binderDied() {
157 stopWatchingMode(mCallback);
158 }
159 }
160
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700161 final ArrayMap<IBinder, ClientState> mClients = new ArrayMap<IBinder, ClientState>();
162
163 public final class ClientState extends Binder implements DeathRecipient {
164 final IBinder mAppToken;
165 final int mPid;
166 final ArrayList<Op> mStartedOps;
167
168 public ClientState(IBinder appToken) {
169 mAppToken = appToken;
170 mPid = Binder.getCallingPid();
171 if (appToken instanceof Binder) {
172 // For local clients, there is no reason to track them.
173 mStartedOps = null;
174 } else {
175 mStartedOps = new ArrayList<Op>();
176 try {
177 mAppToken.linkToDeath(this, 0);
178 } catch (RemoteException e) {
179 }
180 }
181 }
182
183 @Override
184 public String toString() {
185 return "ClientState{" +
186 "mAppToken=" + mAppToken +
187 ", " + (mStartedOps != null ? ("pid=" + mPid) : "local") +
188 '}';
189 }
190
191 @Override
192 public void binderDied() {
193 synchronized (AppOpsService.this) {
194 for (int i=mStartedOps.size()-1; i>=0; i--) {
195 finishOperationLocked(mStartedOps.get(i));
196 }
197 mClients.remove(mAppToken);
198 }
199 }
200 }
201
Jeff Brown6f357d32014-01-15 20:40:55 -0800202 public AppOpsService(File storagePath, Handler handler) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800203 mFile = new AtomicFile(storagePath);
Jeff Brown6f357d32014-01-15 20:40:55 -0800204 mHandler = handler;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800205 readState();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800206 }
David Braunf5d83192013-09-16 13:43:51 -0700207
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800208 public void publish(Context context) {
209 mContext = context;
210 ServiceManager.addService(Context.APP_OPS_SERVICE, asBinder());
211 }
212
Dianne Hackborn514074f2013-02-11 10:52:46 -0800213 public void systemReady() {
214 synchronized (this) {
215 boolean changed = false;
216 for (int i=0; i<mUidOps.size(); i++) {
217 HashMap<String, Ops> pkgs = mUidOps.valueAt(i);
218 Iterator<Ops> it = pkgs.values().iterator();
219 while (it.hasNext()) {
220 Ops ops = it.next();
221 int curUid;
222 try {
223 curUid = mContext.getPackageManager().getPackageUid(ops.packageName,
224 UserHandle.getUserId(ops.uid));
225 } catch (NameNotFoundException e) {
226 curUid = -1;
227 }
228 if (curUid != ops.uid) {
229 Slog.i(TAG, "Pruning old package " + ops.packageName
230 + "/" + ops.uid + ": new uid=" + curUid);
231 it.remove();
232 changed = true;
233 }
234 }
235 if (pkgs.size() <= 0) {
236 mUidOps.removeAt(i);
237 }
238 }
239 if (changed) {
240 scheduleWriteLocked();
241 }
242 }
243 }
244
245 public void packageRemoved(int uid, String packageName) {
246 synchronized (this) {
247 HashMap<String, Ops> pkgs = mUidOps.get(uid);
248 if (pkgs != null) {
249 if (pkgs.remove(packageName) != null) {
250 if (pkgs.size() <= 0) {
251 mUidOps.remove(uid);
252 }
253 scheduleWriteLocked();
254 }
255 }
256 }
257 }
258
259 public void uidRemoved(int uid) {
260 synchronized (this) {
261 if (mUidOps.indexOfKey(uid) >= 0) {
262 mUidOps.remove(uid);
263 scheduleWriteLocked();
264 }
265 }
266 }
267
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800268 public void shutdown() {
269 Slog.w(TAG, "Writing app ops before shutdown...");
Dianne Hackborn35654b62013-01-14 17:38:02 -0800270 boolean doWrite = false;
271 synchronized (this) {
272 if (mWriteScheduled) {
273 mWriteScheduled = false;
274 doWrite = true;
275 }
276 }
277 if (doWrite) {
278 writeState();
279 }
280 }
281
Dianne Hackborn72e39832013-01-18 18:36:09 -0800282 private ArrayList<AppOpsManager.OpEntry> collectOps(Ops pkgOps, int[] ops) {
283 ArrayList<AppOpsManager.OpEntry> resOps = null;
284 if (ops == null) {
285 resOps = new ArrayList<AppOpsManager.OpEntry>();
286 for (int j=0; j<pkgOps.size(); j++) {
287 Op curOp = pkgOps.valueAt(j);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800288 resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time,
289 curOp.rejectTime, curOp.duration));
Dianne Hackborn72e39832013-01-18 18:36:09 -0800290 }
291 } else {
292 for (int j=0; j<ops.length; j++) {
293 Op curOp = pkgOps.get(ops[j]);
294 if (curOp != null) {
295 if (resOps == null) {
296 resOps = new ArrayList<AppOpsManager.OpEntry>();
297 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800298 resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time,
299 curOp.rejectTime, curOp.duration));
Dianne Hackborn72e39832013-01-18 18:36:09 -0800300 }
301 }
302 }
303 return resOps;
304 }
305
Dianne Hackborn35654b62013-01-14 17:38:02 -0800306 @Override
307 public List<AppOpsManager.PackageOps> getPackagesForOps(int[] ops) {
308 mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS,
309 Binder.getCallingPid(), Binder.getCallingUid(), null);
310 ArrayList<AppOpsManager.PackageOps> res = null;
311 synchronized (this) {
312 for (int i=0; i<mUidOps.size(); i++) {
313 HashMap<String, Ops> packages = mUidOps.valueAt(i);
314 for (Ops pkgOps : packages.values()) {
Dianne Hackborn72e39832013-01-18 18:36:09 -0800315 ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800316 if (resOps != null) {
317 if (res == null) {
318 res = new ArrayList<AppOpsManager.PackageOps>();
319 }
320 AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps(
321 pkgOps.packageName, pkgOps.uid, resOps);
322 res.add(resPackage);
323 }
324 }
325 }
326 }
327 return res;
328 }
329
330 @Override
Dianne Hackborn72e39832013-01-18 18:36:09 -0800331 public List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName,
332 int[] ops) {
333 mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS,
334 Binder.getCallingPid(), Binder.getCallingUid(), null);
335 synchronized (this) {
336 Ops pkgOps = getOpsLocked(uid, packageName, false);
337 if (pkgOps == null) {
338 return null;
339 }
340 ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops);
341 if (resOps == null) {
342 return null;
343 }
344 ArrayList<AppOpsManager.PackageOps> res = new ArrayList<AppOpsManager.PackageOps>();
345 AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps(
346 pkgOps.packageName, pkgOps.uid, resOps);
347 res.add(resPackage);
348 return res;
349 }
350 }
351
Dianne Hackborn607b4142013-08-02 18:10:10 -0700352 private void pruneOp(Op op, int uid, String packageName) {
353 if (op.time == 0 && op.rejectTime == 0) {
354 Ops ops = getOpsLocked(uid, packageName, false);
355 if (ops != null) {
356 ops.remove(op.op);
357 if (ops.size() <= 0) {
358 HashMap<String, Ops> pkgOps = mUidOps.get(uid);
359 if (pkgOps != null) {
360 pkgOps.remove(ops.packageName);
361 if (pkgOps.size() <= 0) {
362 mUidOps.remove(uid);
363 }
364 }
365 }
366 }
367 }
368 }
369
Dianne Hackborn72e39832013-01-18 18:36:09 -0800370 @Override
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800371 public void setMode(int code, int uid, String packageName, int mode) {
Dianne Hackbornb64afe12014-07-22 16:29:04 -0700372 if (Binder.getCallingPid() != Process.myPid()) {
373 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
374 Binder.getCallingPid(), Binder.getCallingUid(), null);
Dianne Hackborn133b9df2014-07-01 13:06:10 -0700375 }
Dianne Hackborn961321f2013-02-05 17:22:41 -0800376 verifyIncomingOp(code);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800377 ArrayList<Callback> repCbs = null;
378 code = AppOpsManager.opToSwitch(code);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800379 synchronized (this) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800380 Op op = getOpLocked(code, uid, packageName, true);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800381 if (op != null) {
382 if (op.mode != mode) {
383 op.mode = mode;
Dianne Hackbornc2293022013-02-06 23:14:49 -0800384 ArrayList<Callback> cbs = mOpModeWatchers.get(code);
385 if (cbs != null) {
386 if (repCbs == null) {
387 repCbs = new ArrayList<Callback>();
388 }
389 repCbs.addAll(cbs);
390 }
391 cbs = mPackageModeWatchers.get(packageName);
392 if (cbs != null) {
393 if (repCbs == null) {
394 repCbs = new ArrayList<Callback>();
395 }
396 repCbs.addAll(cbs);
397 }
David Braunf5d83192013-09-16 13:43:51 -0700398 if (mode == AppOpsManager.opToDefaultMode(op.op)) {
Dianne Hackborn514074f2013-02-11 10:52:46 -0800399 // If going into the default mode, prune this op
400 // if there is nothing else interesting in it.
Dianne Hackborn607b4142013-08-02 18:10:10 -0700401 pruneOp(op, uid, packageName);
Dianne Hackborn514074f2013-02-11 10:52:46 -0800402 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800403 scheduleWriteNowLocked();
404 }
405 }
406 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800407 if (repCbs != null) {
408 for (int i=0; i<repCbs.size(); i++) {
409 try {
410 repCbs.get(i).mCallback.opChanged(code, packageName);
411 } catch (RemoteException e) {
412 }
413 }
414 }
415 }
416
Dianne Hackborn607b4142013-08-02 18:10:10 -0700417 private static HashMap<Callback, ArrayList<Pair<String, Integer>>> addCallbacks(
418 HashMap<Callback, ArrayList<Pair<String, Integer>>> callbacks,
419 String packageName, int op, ArrayList<Callback> cbs) {
420 if (cbs == null) {
421 return callbacks;
422 }
423 if (callbacks == null) {
424 callbacks = new HashMap<Callback, ArrayList<Pair<String, Integer>>>();
425 }
426 for (int i=0; i<cbs.size(); i++) {
427 Callback cb = cbs.get(i);
428 ArrayList<Pair<String, Integer>> reports = callbacks.get(cb);
429 if (reports == null) {
430 reports = new ArrayList<Pair<String, Integer>>();
431 callbacks.put(cb, reports);
432 }
433 reports.add(new Pair<String, Integer>(packageName, op));
434 }
435 return callbacks;
436 }
437
438 @Override
439 public void resetAllModes() {
Alexandra Gherghinad6a98972014-08-04 17:05:34 +0100440 int callingUid = Binder.getCallingUid();
Dianne Hackborn607b4142013-08-02 18:10:10 -0700441 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
Alexandra Gherghinad6a98972014-08-04 17:05:34 +0100442 Binder.getCallingPid(), callingUid, null);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700443 HashMap<Callback, ArrayList<Pair<String, Integer>>> callbacks = null;
444 synchronized (this) {
445 boolean changed = false;
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700446 for (int i=mUidOps.size()-1; i>=0; i--) {
Dianne Hackborn607b4142013-08-02 18:10:10 -0700447 HashMap<String, Ops> packages = mUidOps.valueAt(i);
Alexandra Gherghinad6a98972014-08-04 17:05:34 +0100448 if (UserHandle.getUserId(callingUid) != UserHandle.getUserId(mUidOps.keyAt(i))) {
449 // Skip any ops for a different user
450 continue;
451 }
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700452 Iterator<Map.Entry<String, Ops>> it = packages.entrySet().iterator();
453 while (it.hasNext()) {
454 Map.Entry<String, Ops> ent = it.next();
Dianne Hackborn607b4142013-08-02 18:10:10 -0700455 String packageName = ent.getKey();
456 Ops pkgOps = ent.getValue();
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700457 for (int j=pkgOps.size()-1; j>=0; j--) {
Dianne Hackborn607b4142013-08-02 18:10:10 -0700458 Op curOp = pkgOps.valueAt(j);
Dianne Hackborn8828d3a2013-09-25 16:47:10 -0700459 if (AppOpsManager.opAllowsReset(curOp.op)
460 && curOp.mode != AppOpsManager.opToDefaultMode(curOp.op)) {
David Braunf5d83192013-09-16 13:43:51 -0700461 curOp.mode = AppOpsManager.opToDefaultMode(curOp.op);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700462 changed = true;
463 callbacks = addCallbacks(callbacks, packageName, curOp.op,
464 mOpModeWatchers.get(curOp.op));
465 callbacks = addCallbacks(callbacks, packageName, curOp.op,
466 mPackageModeWatchers.get(packageName));
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700467 if (curOp.time == 0 && curOp.rejectTime == 0) {
468 pkgOps.removeAt(j);
469 }
Dianne Hackborn607b4142013-08-02 18:10:10 -0700470 }
471 }
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700472 if (pkgOps.size() == 0) {
473 it.remove();
474 }
475 }
476 if (packages.size() == 0) {
477 mUidOps.removeAt(i);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700478 }
479 }
480 if (changed) {
481 scheduleWriteNowLocked();
482 }
483 }
484 if (callbacks != null) {
485 for (Map.Entry<Callback, ArrayList<Pair<String, Integer>>> ent : callbacks.entrySet()) {
486 Callback cb = ent.getKey();
487 ArrayList<Pair<String, Integer>> reports = ent.getValue();
488 for (int i=0; i<reports.size(); i++) {
489 Pair<String, Integer> rep = reports.get(i);
490 try {
491 cb.mCallback.opChanged(rep.second, rep.first);
492 } catch (RemoteException e) {
493 }
494 }
495 }
496 }
497 }
498
Dianne Hackbornc2293022013-02-06 23:14:49 -0800499 @Override
500 public void startWatchingMode(int op, String packageName, IAppOpsCallback callback) {
501 synchronized (this) {
502 op = AppOpsManager.opToSwitch(op);
503 Callback cb = mModeWatchers.get(callback.asBinder());
504 if (cb == null) {
505 cb = new Callback(callback);
506 mModeWatchers.put(callback.asBinder(), cb);
507 }
508 if (op != AppOpsManager.OP_NONE) {
509 ArrayList<Callback> cbs = mOpModeWatchers.get(op);
510 if (cbs == null) {
511 cbs = new ArrayList<Callback>();
512 mOpModeWatchers.put(op, cbs);
513 }
514 cbs.add(cb);
515 }
516 if (packageName != null) {
517 ArrayList<Callback> cbs = mPackageModeWatchers.get(packageName);
518 if (cbs == null) {
519 cbs = new ArrayList<Callback>();
520 mPackageModeWatchers.put(packageName, cbs);
521 }
522 cbs.add(cb);
523 }
524 }
525 }
526
527 @Override
528 public void stopWatchingMode(IAppOpsCallback callback) {
529 synchronized (this) {
530 Callback cb = mModeWatchers.remove(callback.asBinder());
531 if (cb != null) {
532 cb.unlinkToDeath();
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700533 for (int i=mOpModeWatchers.size()-1; i>=0; i--) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800534 ArrayList<Callback> cbs = mOpModeWatchers.valueAt(i);
535 cbs.remove(cb);
536 if (cbs.size() <= 0) {
537 mOpModeWatchers.removeAt(i);
538 }
539 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700540 for (int i=mPackageModeWatchers.size()-1; i>=0; i--) {
541 ArrayList<Callback> cbs = mPackageModeWatchers.valueAt(i);
542 cbs.remove(cb);
543 if (cbs.size() <= 0) {
544 mPackageModeWatchers.removeAt(i);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800545 }
546 }
547 }
548 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800549 }
550
551 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700552 public IBinder getToken(IBinder clientToken) {
553 synchronized (this) {
554 ClientState cs = mClients.get(clientToken);
555 if (cs == null) {
556 cs = new ClientState(clientToken);
557 mClients.put(clientToken, cs);
558 }
559 return cs;
560 }
561 }
562
563 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -0800564 public int checkOperation(int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800565 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800566 verifyIncomingOp(code);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800567 synchronized (this) {
Jason Monk1c7c3192014-06-26 12:52:18 -0400568 if (isOpRestricted(uid, code, packageName)) {
Jason Monk62062992014-05-06 09:55:28 -0400569 return AppOpsManager.MODE_IGNORED;
570 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800571 Op op = getOpLocked(AppOpsManager.opToSwitch(code), uid, packageName, false);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800572 if (op == null) {
David Braunf5d83192013-09-16 13:43:51 -0700573 return AppOpsManager.opToDefaultMode(code);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800574 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800575 return op.mode;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800576 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800577 }
578
579 @Override
John Spurlock7b414672014-07-18 13:02:39 -0400580 public int checkAudioOperation(int code, int usage, int uid, String packageName) {
John Spurlock1af30c72014-03-10 08:33:35 -0400581 synchronized (this) {
John Spurlock7b414672014-07-18 13:02:39 -0400582 final int mode = checkRestrictionLocked(code, usage, uid, packageName);
John Spurlock1af30c72014-03-10 08:33:35 -0400583 if (mode != AppOpsManager.MODE_ALLOWED) {
584 return mode;
585 }
586 }
587 return checkOperation(code, uid, packageName);
588 }
589
John Spurlock7b414672014-07-18 13:02:39 -0400590 private int checkRestrictionLocked(int code, int usage, int uid, String packageName) {
591 final SparseArray<Restriction> usageRestrictions = mAudioRestrictions.get(code);
592 if (usageRestrictions != null) {
593 final Restriction r = usageRestrictions.get(usage);
John Spurlock1af30c72014-03-10 08:33:35 -0400594 if (r != null && !r.exceptionPackages.contains(packageName)) {
595 return r.mode;
596 }
597 }
598 return AppOpsManager.MODE_ALLOWED;
599 }
600
601 @Override
John Spurlock7b414672014-07-18 13:02:39 -0400602 public void setAudioRestriction(int code, int usage, int uid, int mode,
John Spurlock1af30c72014-03-10 08:33:35 -0400603 String[] exceptionPackages) {
604 verifyIncomingUid(uid);
605 verifyIncomingOp(code);
606 synchronized (this) {
John Spurlock7b414672014-07-18 13:02:39 -0400607 SparseArray<Restriction> usageRestrictions = mAudioRestrictions.get(code);
608 if (usageRestrictions == null) {
609 usageRestrictions = new SparseArray<Restriction>();
610 mAudioRestrictions.put(code, usageRestrictions);
John Spurlock1af30c72014-03-10 08:33:35 -0400611 }
John Spurlock7b414672014-07-18 13:02:39 -0400612 usageRestrictions.remove(usage);
John Spurlock1af30c72014-03-10 08:33:35 -0400613 if (mode != AppOpsManager.MODE_ALLOWED) {
614 final Restriction r = new Restriction();
615 r.mode = mode;
616 if (exceptionPackages != null) {
617 final int N = exceptionPackages.length;
618 r.exceptionPackages = new ArraySet<String>(N);
619 for (int i = 0; i < N; i++) {
620 final String pkg = exceptionPackages[i];
621 if (pkg != null) {
622 r.exceptionPackages.add(pkg.trim());
623 }
624 }
625 }
John Spurlock7b414672014-07-18 13:02:39 -0400626 usageRestrictions.put(usage, r);
John Spurlock1af30c72014-03-10 08:33:35 -0400627 }
628 }
629 }
630
631 @Override
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700632 public int checkPackage(int uid, String packageName) {
633 synchronized (this) {
Dianne Hackborn0fcef842014-09-12 15:38:33 -0700634 if (getOpsRawLocked(uid, packageName, true) != null) {
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700635 return AppOpsManager.MODE_ALLOWED;
636 } else {
637 return AppOpsManager.MODE_ERRORED;
638 }
639 }
640 }
641
642 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800643 public int noteOperation(int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800644 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800645 verifyIncomingOp(code);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800646 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800647 Ops ops = getOpsLocked(uid, packageName, true);
648 if (ops == null) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800649 if (DEBUG) Log.d(TAG, "noteOperation: no op for code " + code + " uid " + uid
650 + " package " + packageName);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700651 return AppOpsManager.MODE_ERRORED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800652 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800653 Op op = getOpLocked(ops, code, true);
Jason Monk1c7c3192014-06-26 12:52:18 -0400654 if (isOpRestricted(uid, code, packageName)) {
Jason Monk62062992014-05-06 09:55:28 -0400655 return AppOpsManager.MODE_IGNORED;
656 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800657 if (op.duration == -1) {
658 Slog.w(TAG, "Noting op not finished: uid " + uid + " pkg " + packageName
659 + " code " + code + " time=" + op.time + " duration=" + op.duration);
660 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800661 op.duration = 0;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800662 final int switchCode = AppOpsManager.opToSwitch(code);
663 final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
664 if (switchOp.mode != AppOpsManager.MODE_ALLOWED) {
665 if (DEBUG) Log.d(TAG, "noteOperation: reject #" + op.mode + " for code "
666 + switchCode + " (" + code + ") uid " + uid + " package " + packageName);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800667 op.rejectTime = System.currentTimeMillis();
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800668 return switchOp.mode;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800669 }
670 if (DEBUG) Log.d(TAG, "noteOperation: allowing code " + code + " uid " + uid
671 + " package " + packageName);
672 op.time = System.currentTimeMillis();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800673 op.rejectTime = 0;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800674 return AppOpsManager.MODE_ALLOWED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800675 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800676 }
677
678 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700679 public int startOperation(IBinder token, int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800680 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800681 verifyIncomingOp(code);
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700682 ClientState client = (ClientState)token;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800683 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800684 Ops ops = getOpsLocked(uid, packageName, true);
685 if (ops == null) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800686 if (DEBUG) Log.d(TAG, "startOperation: no op for code " + code + " uid " + uid
687 + " package " + packageName);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700688 return AppOpsManager.MODE_ERRORED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800689 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800690 Op op = getOpLocked(ops, code, true);
Jason Monk1c7c3192014-06-26 12:52:18 -0400691 if (isOpRestricted(uid, code, packageName)) {
Jason Monk62062992014-05-06 09:55:28 -0400692 return AppOpsManager.MODE_IGNORED;
693 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800694 final int switchCode = AppOpsManager.opToSwitch(code);
695 final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
696 if (switchOp.mode != AppOpsManager.MODE_ALLOWED) {
697 if (DEBUG) Log.d(TAG, "startOperation: reject #" + op.mode + " for code "
698 + switchCode + " (" + code + ") uid " + uid + " package " + packageName);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800699 op.rejectTime = System.currentTimeMillis();
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800700 return switchOp.mode;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800701 }
702 if (DEBUG) Log.d(TAG, "startOperation: allowing code " + code + " uid " + uid
703 + " package " + packageName);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800704 if (op.nesting == 0) {
705 op.time = System.currentTimeMillis();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800706 op.rejectTime = 0;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800707 op.duration = -1;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800708 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800709 op.nesting++;
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700710 if (client.mStartedOps != null) {
711 client.mStartedOps.add(op);
712 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800713 return AppOpsManager.MODE_ALLOWED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800714 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800715 }
716
717 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700718 public void finishOperation(IBinder token, int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800719 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800720 verifyIncomingOp(code);
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700721 ClientState client = (ClientState)token;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800722 synchronized (this) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800723 Op op = getOpLocked(code, uid, packageName, true);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800724 if (op == null) {
725 return;
726 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700727 if (client.mStartedOps != null) {
728 if (!client.mStartedOps.remove(op)) {
729 throw new IllegalStateException("Operation not started: uid" + op.uid
730 + " pkg=" + op.packageName + " op=" + op.op);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800731 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800732 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700733 finishOperationLocked(op);
734 }
735 }
736
737 void finishOperationLocked(Op op) {
738 if (op.nesting <= 1) {
739 if (op.nesting == 1) {
740 op.duration = (int)(System.currentTimeMillis() - op.time);
741 op.time += op.duration;
742 } else {
743 Slog.w(TAG, "Finishing op nesting under-run: uid " + op.uid + " pkg "
744 + op.packageName + " code " + op.op + " time=" + op.time
745 + " duration=" + op.duration + " nesting=" + op.nesting);
746 }
747 op.nesting = 0;
748 } else {
749 op.nesting--;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800750 }
751 }
752
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800753 private void verifyIncomingUid(int uid) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800754 if (uid == Binder.getCallingUid()) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800755 return;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800756 }
757 if (Binder.getCallingPid() == Process.myPid()) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800758 return;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800759 }
760 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
761 Binder.getCallingPid(), Binder.getCallingUid(), null);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800762 }
763
Dianne Hackborn961321f2013-02-05 17:22:41 -0800764 private void verifyIncomingOp(int op) {
765 if (op >= 0 && op < AppOpsManager._NUM_OP) {
766 return;
767 }
768 throw new IllegalArgumentException("Bad operation #" + op);
769 }
770
Dianne Hackborn72e39832013-01-18 18:36:09 -0800771 private Ops getOpsLocked(int uid, String packageName, boolean edit) {
Dianne Hackborn0fcef842014-09-12 15:38:33 -0700772 if (uid == 0) {
773 packageName = "root";
774 } else if (uid == Process.SHELL_UID) {
775 packageName = "com.android.shell";
776 }
777 return getOpsRawLocked(uid, packageName, edit);
778 }
779
780 private Ops getOpsRawLocked(int uid, String packageName, boolean edit) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800781 HashMap<String, Ops> pkgOps = mUidOps.get(uid);
782 if (pkgOps == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800783 if (!edit) {
784 return null;
785 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800786 pkgOps = new HashMap<String, Ops>();
787 mUidOps.put(uid, pkgOps);
788 }
789 Ops ops = pkgOps.get(packageName);
790 if (ops == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800791 if (!edit) {
792 return null;
793 }
Jason Monk1c7c3192014-06-26 12:52:18 -0400794 boolean isPrivileged = false;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800795 // This is the first time we have seen this package name under this uid,
796 // so let's make sure it is valid.
Dianne Hackborn514074f2013-02-11 10:52:46 -0800797 if (uid != 0) {
798 final long ident = Binder.clearCallingIdentity();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800799 try {
Dianne Hackborn514074f2013-02-11 10:52:46 -0800800 int pkgUid = -1;
801 try {
Jason Monk1c7c3192014-06-26 12:52:18 -0400802 ApplicationInfo appInfo = ActivityThread.getPackageManager()
803 .getApplicationInfo(packageName, 0, UserHandle.getUserId(uid));
804 if (appInfo != null) {
805 pkgUid = appInfo.uid;
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800806 isPrivileged = (appInfo.privateFlags
807 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0;
Jason Monk1c7c3192014-06-26 12:52:18 -0400808 } else {
809 if ("media".equals(packageName)) {
810 pkgUid = Process.MEDIA_UID;
811 isPrivileged = false;
812 }
Dianne Hackborn713df152013-05-17 11:27:57 -0700813 }
Jason Monk1c7c3192014-06-26 12:52:18 -0400814 } catch (RemoteException e) {
815 Slog.w(TAG, "Could not contact PackageManager", e);
Dianne Hackborn514074f2013-02-11 10:52:46 -0800816 }
817 if (pkgUid != uid) {
818 // Oops! The package name is not valid for the uid they are calling
819 // under. Abort.
820 Slog.w(TAG, "Bad call: specified package " + packageName
821 + " under uid " + uid + " but it is really " + pkgUid);
822 return null;
823 }
824 } finally {
825 Binder.restoreCallingIdentity(ident);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800826 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800827 }
Jason Monk1c7c3192014-06-26 12:52:18 -0400828 ops = new Ops(packageName, uid, isPrivileged);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800829 pkgOps.put(packageName, ops);
830 }
Dianne Hackborn72e39832013-01-18 18:36:09 -0800831 return ops;
832 }
833
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800834 private void scheduleWriteLocked() {
835 if (!mWriteScheduled) {
836 mWriteScheduled = true;
837 mHandler.postDelayed(mWriteRunner, WRITE_DELAY);
838 }
839 }
840
841 private void scheduleWriteNowLocked() {
842 if (!mWriteScheduled) {
843 mWriteScheduled = true;
844 }
845 mHandler.removeCallbacks(mWriteRunner);
846 mHandler.post(mWriteRunner);
847 }
848
Dianne Hackborn72e39832013-01-18 18:36:09 -0800849 private Op getOpLocked(int code, int uid, String packageName, boolean edit) {
850 Ops ops = getOpsLocked(uid, packageName, edit);
851 if (ops == null) {
852 return null;
853 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800854 return getOpLocked(ops, code, edit);
855 }
856
857 private Op getOpLocked(Ops ops, int code, boolean edit) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800858 Op op = ops.get(code);
859 if (op == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800860 if (!edit) {
861 return null;
862 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700863 op = new Op(ops.uid, ops.packageName, code);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800864 ops.put(code, op);
865 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800866 if (edit) {
867 scheduleWriteLocked();
Dianne Hackborn35654b62013-01-14 17:38:02 -0800868 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800869 return op;
870 }
871
Jason Monk1c7c3192014-06-26 12:52:18 -0400872 private boolean isOpRestricted(int uid, int code, String packageName) {
Jason Monk62062992014-05-06 09:55:28 -0400873 int userHandle = UserHandle.getUserId(uid);
874 boolean[] opRestrictions = mOpRestrictions.get(userHandle);
875 if ((opRestrictions != null) && opRestrictions[code]) {
Jason Monk1c7c3192014-06-26 12:52:18 -0400876 if (AppOpsManager.opAllowSystemBypassRestriction(code)) {
877 synchronized (this) {
878 Ops ops = getOpsLocked(uid, packageName, true);
879 if ((ops != null) && ops.isPrivileged) {
880 return false;
881 }
882 }
883 }
Julia Reynolds401de172014-07-24 18:21:29 -0400884 return true;
Jason Monk62062992014-05-06 09:55:28 -0400885 }
886 return false;
887 }
888
Dianne Hackborn35654b62013-01-14 17:38:02 -0800889 void readState() {
890 synchronized (mFile) {
891 synchronized (this) {
892 FileInputStream stream;
893 try {
894 stream = mFile.openRead();
895 } catch (FileNotFoundException e) {
896 Slog.i(TAG, "No existing app ops " + mFile.getBaseFile() + "; starting empty");
897 return;
898 }
899 boolean success = false;
900 try {
901 XmlPullParser parser = Xml.newPullParser();
902 parser.setInput(stream, null);
903 int type;
904 while ((type = parser.next()) != XmlPullParser.START_TAG
905 && type != XmlPullParser.END_DOCUMENT) {
906 ;
907 }
908
909 if (type != XmlPullParser.START_TAG) {
910 throw new IllegalStateException("no start tag found");
911 }
912
913 int outerDepth = parser.getDepth();
914 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
915 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
916 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
917 continue;
918 }
919
920 String tagName = parser.getName();
921 if (tagName.equals("pkg")) {
Dave Burke0997c5bd2013-08-02 20:25:02 +0000922 readPackage(parser);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800923 } else {
924 Slog.w(TAG, "Unknown element under <app-ops>: "
925 + parser.getName());
926 XmlUtils.skipCurrentTag(parser);
927 }
928 }
929 success = true;
930 } catch (IllegalStateException e) {
931 Slog.w(TAG, "Failed parsing " + e);
932 } catch (NullPointerException e) {
933 Slog.w(TAG, "Failed parsing " + e);
934 } catch (NumberFormatException e) {
935 Slog.w(TAG, "Failed parsing " + e);
936 } catch (XmlPullParserException e) {
937 Slog.w(TAG, "Failed parsing " + e);
938 } catch (IOException e) {
939 Slog.w(TAG, "Failed parsing " + e);
940 } catch (IndexOutOfBoundsException e) {
941 Slog.w(TAG, "Failed parsing " + e);
942 } finally {
943 if (!success) {
944 mUidOps.clear();
945 }
946 try {
947 stream.close();
948 } catch (IOException e) {
949 }
950 }
951 }
952 }
953 }
954
Dave Burke0997c5bd2013-08-02 20:25:02 +0000955 void readPackage(XmlPullParser parser) throws NumberFormatException,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800956 XmlPullParserException, IOException {
957 String pkgName = parser.getAttributeValue(null, "n");
958 int outerDepth = parser.getDepth();
959 int type;
960 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
961 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
962 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
963 continue;
964 }
965
966 String tagName = parser.getName();
967 if (tagName.equals("uid")) {
Dave Burke0997c5bd2013-08-02 20:25:02 +0000968 readUid(parser, pkgName);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800969 } else {
970 Slog.w(TAG, "Unknown element under <pkg>: "
971 + parser.getName());
972 XmlUtils.skipCurrentTag(parser);
973 }
974 }
975 }
976
Dave Burke0997c5bd2013-08-02 20:25:02 +0000977 void readUid(XmlPullParser parser, String pkgName) throws NumberFormatException,
Dianne Hackborn35654b62013-01-14 17:38:02 -0800978 XmlPullParserException, IOException {
979 int uid = Integer.parseInt(parser.getAttributeValue(null, "n"));
Jason Monk1c7c3192014-06-26 12:52:18 -0400980 String isPrivilegedString = parser.getAttributeValue(null, "p");
981 boolean isPrivileged = false;
982 if (isPrivilegedString == null) {
983 try {
984 IPackageManager packageManager = ActivityThread.getPackageManager();
985 if (packageManager != null) {
986 ApplicationInfo appInfo = ActivityThread.getPackageManager()
987 .getApplicationInfo(pkgName, 0, UserHandle.getUserId(uid));
988 if (appInfo != null) {
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800989 isPrivileged = (appInfo.privateFlags
990 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0;
Jason Monk1c7c3192014-06-26 12:52:18 -0400991 }
992 } else {
993 // Could not load data, don't add to cache so it will be loaded later.
994 return;
995 }
996 } catch (RemoteException e) {
997 Slog.w(TAG, "Could not contact PackageManager", e);
998 }
999 } else {
1000 isPrivileged = Boolean.parseBoolean(isPrivilegedString);
1001 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001002 int outerDepth = parser.getDepth();
1003 int type;
1004 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1005 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1006 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1007 continue;
1008 }
1009
1010 String tagName = parser.getName();
1011 if (tagName.equals("op")) {
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001012 Op op = new Op(uid, pkgName, Integer.parseInt(parser.getAttributeValue(null, "n")));
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001013 String mode = parser.getAttributeValue(null, "m");
1014 if (mode != null) {
Dave Burke0997c5bd2013-08-02 20:25:02 +00001015 op.mode = Integer.parseInt(mode);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001016 }
1017 String time = parser.getAttributeValue(null, "t");
1018 if (time != null) {
1019 op.time = Long.parseLong(time);
1020 }
1021 time = parser.getAttributeValue(null, "r");
1022 if (time != null) {
1023 op.rejectTime = Long.parseLong(time);
1024 }
1025 String dur = parser.getAttributeValue(null, "d");
1026 if (dur != null) {
1027 op.duration = Integer.parseInt(dur);
1028 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001029 HashMap<String, Ops> pkgOps = mUidOps.get(uid);
1030 if (pkgOps == null) {
1031 pkgOps = new HashMap<String, Ops>();
1032 mUidOps.put(uid, pkgOps);
1033 }
1034 Ops ops = pkgOps.get(pkgName);
1035 if (ops == null) {
Jason Monk1c7c3192014-06-26 12:52:18 -04001036 ops = new Ops(pkgName, uid, isPrivileged);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001037 pkgOps.put(pkgName, ops);
1038 }
1039 ops.put(op.op, op);
1040 } else {
1041 Slog.w(TAG, "Unknown element under <pkg>: "
1042 + parser.getName());
1043 XmlUtils.skipCurrentTag(parser);
1044 }
1045 }
1046 }
1047
1048 void writeState() {
1049 synchronized (mFile) {
1050 List<AppOpsManager.PackageOps> allOps = getPackagesForOps(null);
1051
1052 FileOutputStream stream;
1053 try {
1054 stream = mFile.startWrite();
1055 } catch (IOException e) {
1056 Slog.w(TAG, "Failed to write state: " + e);
1057 return;
1058 }
1059
1060 try {
1061 XmlSerializer out = new FastXmlSerializer();
1062 out.setOutput(stream, "utf-8");
1063 out.startDocument(null, true);
1064 out.startTag(null, "app-ops");
1065
1066 if (allOps != null) {
1067 String lastPkg = null;
1068 for (int i=0; i<allOps.size(); i++) {
1069 AppOpsManager.PackageOps pkg = allOps.get(i);
1070 if (!pkg.getPackageName().equals(lastPkg)) {
1071 if (lastPkg != null) {
1072 out.endTag(null, "pkg");
1073 }
1074 lastPkg = pkg.getPackageName();
1075 out.startTag(null, "pkg");
1076 out.attribute(null, "n", lastPkg);
1077 }
1078 out.startTag(null, "uid");
1079 out.attribute(null, "n", Integer.toString(pkg.getUid()));
Jason Monk1c7c3192014-06-26 12:52:18 -04001080 synchronized (this) {
1081 Ops ops = getOpsLocked(pkg.getUid(), pkg.getPackageName(), false);
1082 // Should always be present as the list of PackageOps is generated
1083 // from Ops.
1084 if (ops != null) {
1085 out.attribute(null, "p", Boolean.toString(ops.isPrivileged));
1086 } else {
1087 out.attribute(null, "p", Boolean.toString(false));
1088 }
1089 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001090 List<AppOpsManager.OpEntry> ops = pkg.getOps();
1091 for (int j=0; j<ops.size(); j++) {
1092 AppOpsManager.OpEntry op = ops.get(j);
1093 out.startTag(null, "op");
1094 out.attribute(null, "n", Integer.toString(op.getOp()));
David Braunf5d83192013-09-16 13:43:51 -07001095 if (op.getMode() != AppOpsManager.opToDefaultMode(op.getOp())) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001096 out.attribute(null, "m", Integer.toString(op.getMode()));
1097 }
1098 long time = op.getTime();
1099 if (time != 0) {
1100 out.attribute(null, "t", Long.toString(time));
1101 }
1102 time = op.getRejectTime();
1103 if (time != 0) {
1104 out.attribute(null, "r", Long.toString(time));
1105 }
1106 int dur = op.getDuration();
1107 if (dur != 0) {
1108 out.attribute(null, "d", Integer.toString(dur));
1109 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001110 out.endTag(null, "op");
1111 }
1112 out.endTag(null, "uid");
1113 }
1114 if (lastPkg != null) {
1115 out.endTag(null, "pkg");
1116 }
1117 }
1118
1119 out.endTag(null, "app-ops");
1120 out.endDocument();
1121 mFile.finishWrite(stream);
1122 } catch (IOException e) {
1123 Slog.w(TAG, "Failed to write state, restoring backup.", e);
1124 mFile.failWrite(stream);
1125 }
1126 }
1127 }
1128
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001129 @Override
1130 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1131 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1132 != PackageManager.PERMISSION_GRANTED) {
1133 pw.println("Permission Denial: can't dump ApOps service from from pid="
1134 + Binder.getCallingPid()
1135 + ", uid=" + Binder.getCallingUid());
1136 return;
1137 }
1138
1139 synchronized (this) {
1140 pw.println("Current AppOps Service state:");
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001141 final long now = System.currentTimeMillis();
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001142 boolean needSep = false;
1143 if (mOpModeWatchers.size() > 0) {
1144 needSep = true;
1145 pw.println(" Op mode watchers:");
1146 for (int i=0; i<mOpModeWatchers.size(); i++) {
1147 pw.print(" Op "); pw.print(AppOpsManager.opToName(mOpModeWatchers.keyAt(i)));
1148 pw.println(":");
1149 ArrayList<Callback> callbacks = mOpModeWatchers.valueAt(i);
1150 for (int j=0; j<callbacks.size(); j++) {
1151 pw.print(" #"); pw.print(j); pw.print(": ");
1152 pw.println(callbacks.get(j));
1153 }
1154 }
1155 }
1156 if (mPackageModeWatchers.size() > 0) {
1157 needSep = true;
1158 pw.println(" Package mode watchers:");
1159 for (int i=0; i<mPackageModeWatchers.size(); i++) {
1160 pw.print(" Pkg "); pw.print(mPackageModeWatchers.keyAt(i));
1161 pw.println(":");
1162 ArrayList<Callback> callbacks = mPackageModeWatchers.valueAt(i);
1163 for (int j=0; j<callbacks.size(); j++) {
1164 pw.print(" #"); pw.print(j); pw.print(": ");
1165 pw.println(callbacks.get(j));
1166 }
1167 }
1168 }
1169 if (mModeWatchers.size() > 0) {
1170 needSep = true;
1171 pw.println(" All mode watchers:");
1172 for (int i=0; i<mModeWatchers.size(); i++) {
1173 pw.print(" "); pw.print(mModeWatchers.keyAt(i));
1174 pw.print(" -> "); pw.println(mModeWatchers.valueAt(i));
1175 }
1176 }
1177 if (mClients.size() > 0) {
1178 needSep = true;
1179 pw.println(" Clients:");
1180 for (int i=0; i<mClients.size(); i++) {
1181 pw.print(" "); pw.print(mClients.keyAt(i)); pw.println(":");
1182 ClientState cs = mClients.valueAt(i);
1183 pw.print(" "); pw.println(cs);
1184 if (cs.mStartedOps != null && cs.mStartedOps.size() > 0) {
1185 pw.println(" Started ops:");
1186 for (int j=0; j<cs.mStartedOps.size(); j++) {
1187 Op op = cs.mStartedOps.get(j);
1188 pw.print(" "); pw.print("uid="); pw.print(op.uid);
1189 pw.print(" pkg="); pw.print(op.packageName);
1190 pw.print(" op="); pw.println(AppOpsManager.opToName(op.op));
1191 }
1192 }
1193 }
1194 }
John Spurlock1af30c72014-03-10 08:33:35 -04001195 if (mAudioRestrictions.size() > 0) {
1196 boolean printedHeader = false;
1197 for (int o=0; o<mAudioRestrictions.size(); o++) {
1198 final String op = AppOpsManager.opToName(mAudioRestrictions.keyAt(o));
1199 final SparseArray<Restriction> restrictions = mAudioRestrictions.valueAt(o);
1200 for (int i=0; i<restrictions.size(); i++) {
1201 if (!printedHeader){
1202 pw.println(" Audio Restrictions:");
1203 printedHeader = true;
1204 needSep = true;
1205 }
John Spurlock7b414672014-07-18 13:02:39 -04001206 final int usage = restrictions.keyAt(i);
John Spurlock1af30c72014-03-10 08:33:35 -04001207 pw.print(" "); pw.print(op);
John Spurlock7b414672014-07-18 13:02:39 -04001208 pw.print(" usage="); pw.print(AudioAttributes.usageToString(usage));
John Spurlock1af30c72014-03-10 08:33:35 -04001209 Restriction r = restrictions.valueAt(i);
1210 pw.print(": mode="); pw.println(r.mode);
1211 if (!r.exceptionPackages.isEmpty()) {
1212 pw.println(" Exceptions:");
1213 for (int j=0; j<r.exceptionPackages.size(); j++) {
1214 pw.print(" "); pw.println(r.exceptionPackages.valueAt(j));
1215 }
1216 }
1217 }
1218 }
1219 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001220 if (needSep) {
1221 pw.println();
1222 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001223 for (int i=0; i<mUidOps.size(); i++) {
1224 pw.print(" Uid "); UserHandle.formatUid(pw, mUidOps.keyAt(i)); pw.println(":");
1225 HashMap<String, Ops> pkgOps = mUidOps.valueAt(i);
1226 for (Ops ops : pkgOps.values()) {
1227 pw.print(" Package "); pw.print(ops.packageName); pw.println(":");
1228 for (int j=0; j<ops.size(); j++) {
1229 Op op = ops.valueAt(j);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001230 pw.print(" "); pw.print(AppOpsManager.opToName(op.op));
1231 pw.print(": mode="); pw.print(op.mode);
1232 if (op.time != 0) {
1233 pw.print("; time="); TimeUtils.formatDuration(now-op.time, pw);
1234 pw.print(" ago");
1235 }
1236 if (op.rejectTime != 0) {
1237 pw.print("; rejectTime="); TimeUtils.formatDuration(now-op.rejectTime, pw);
1238 pw.print(" ago");
1239 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001240 if (op.duration == -1) {
1241 pw.println(" (running)");
1242 } else {
1243 pw.print("; duration=");
1244 TimeUtils.formatDuration(op.duration, pw);
1245 pw.println();
1246 }
1247 }
1248 }
1249 }
1250 }
1251 }
John Spurlock1af30c72014-03-10 08:33:35 -04001252
1253 private static final class Restriction {
1254 private static final ArraySet<String> NO_EXCEPTIONS = new ArraySet<String>();
1255 int mode;
1256 ArraySet<String> exceptionPackages = NO_EXCEPTIONS;
1257 }
Jason Monk62062992014-05-06 09:55:28 -04001258
1259 @Override
Jason Monk62062992014-05-06 09:55:28 -04001260 public void setUserRestrictions(Bundle restrictions, int userHandle) throws RemoteException {
1261 checkSystemUid("setUserRestrictions");
1262 boolean[] opRestrictions = mOpRestrictions.get(userHandle);
1263 if (opRestrictions == null) {
1264 opRestrictions = new boolean[AppOpsManager._NUM_OP];
1265 mOpRestrictions.put(userHandle, opRestrictions);
1266 }
1267 for (int i = 0; i < opRestrictions.length; ++i) {
1268 String restriction = AppOpsManager.opToRestriction(i);
1269 if (restriction != null) {
1270 opRestrictions[i] = restrictions.getBoolean(restriction, false);
1271 } else {
1272 opRestrictions[i] = false;
1273 }
1274 }
1275 }
1276
1277 @Override
1278 public void removeUser(int userHandle) throws RemoteException {
1279 checkSystemUid("removeUser");
1280 mOpRestrictions.remove(userHandle);
Jason Monk62062992014-05-06 09:55:28 -04001281 }
1282
1283 private void checkSystemUid(String function) {
1284 int uid = Binder.getCallingUid();
1285 if (uid != Process.SYSTEM_UID) {
1286 throw new SecurityException(function + " must by called by the system");
1287 }
1288 }
1289
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001290}