blob: f0fc399613e65766cf3d446fcf9069f777bdf885 [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;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010026import java.nio.charset.StandardCharsets;
Dianne Hackborn35654b62013-01-14 17:38:02 -080027import java.util.ArrayList;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080028import java.util.HashMap;
Dianne Hackbornc2293022013-02-06 23:14:49 -080029import java.util.Iterator;
Dianne Hackborn35654b62013-01-14 17:38:02 -080030import java.util.List;
Dianne Hackborn607b4142013-08-02 18:10:10 -070031import java.util.Map;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080032
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -080033import android.app.ActivityManager;
Jason Monk1c7c3192014-06-26 12:52:18 -040034import android.app.ActivityThread;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080035import android.app.AppOpsManager;
36import android.content.Context;
Jason Monk1c7c3192014-06-26 12:52:18 -040037import android.content.pm.ApplicationInfo;
38import android.content.pm.IPackageManager;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080039import android.content.pm.PackageManager;
40import android.content.pm.PackageManager.NameNotFoundException;
John Spurlock7b414672014-07-18 13:02:39 -040041import android.media.AudioAttributes;
Dianne Hackborn35654b62013-01-14 17:38:02 -080042import android.os.AsyncTask;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080043import android.os.Binder;
Jason Monk62062992014-05-06 09:55:28 -040044import android.os.Bundle;
Dianne Hackborn35654b62013-01-14 17:38:02 -080045import android.os.Handler;
Dianne Hackbornc2293022013-02-06 23:14:49 -080046import android.os.IBinder;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080047import android.os.Process;
Dianne Hackbornc2293022013-02-06 23:14:49 -080048import android.os.RemoteException;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080049import android.os.ServiceManager;
50import android.os.UserHandle;
Dianne Hackborne98f5db2013-07-17 17:23:25 -070051import android.util.ArrayMap;
John Spurlock1af30c72014-03-10 08:33:35 -040052import android.util.ArraySet;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080053import android.util.AtomicFile;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -080054import android.util.Log;
Dianne Hackborn607b4142013-08-02 18:10:10 -070055import android.util.Pair;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080056import android.util.Slog;
57import android.util.SparseArray;
58import android.util.TimeUtils;
Dianne Hackborn35654b62013-01-14 17:38:02 -080059import android.util.Xml;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080060
61import com.android.internal.app.IAppOpsService;
Dianne Hackbornc2293022013-02-06 23:14:49 -080062import com.android.internal.app.IAppOpsCallback;
Dianne Hackborn35654b62013-01-14 17:38:02 -080063import com.android.internal.util.FastXmlSerializer;
64import com.android.internal.util.XmlUtils;
65
66import org.xmlpull.v1.XmlPullParser;
67import org.xmlpull.v1.XmlPullParserException;
68import org.xmlpull.v1.XmlSerializer;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080069
70public class AppOpsService extends IAppOpsService.Stub {
71 static final String TAG = "AppOps";
Dianne Hackborn35654b62013-01-14 17:38:02 -080072 static final boolean DEBUG = false;
73
74 // Write at most every 30 minutes.
75 static final long WRITE_DELAY = DEBUG ? 1000 : 30*60*1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080076
77 Context mContext;
78 final AtomicFile mFile;
Dianne Hackborn35654b62013-01-14 17:38:02 -080079 final Handler mHandler;
80
81 boolean mWriteScheduled;
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -080082 boolean mFastWriteScheduled;
Dianne Hackborn35654b62013-01-14 17:38:02 -080083 final Runnable mWriteRunner = new Runnable() {
84 public void run() {
85 synchronized (AppOpsService.this) {
86 mWriteScheduled = false;
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -080087 mFastWriteScheduled = false;
Dianne Hackborn35654b62013-01-14 17:38:02 -080088 AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
89 @Override protected Void doInBackground(Void... params) {
90 writeState();
91 return null;
92 }
93 };
94 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
95 }
96 }
97 };
Dianne Hackborna06de0f2012-12-11 16:34:47 -080098
99 final SparseArray<HashMap<String, Ops>> mUidOps
100 = new SparseArray<HashMap<String, Ops>>();
101
Jason Monk62062992014-05-06 09:55:28 -0400102 private final SparseArray<boolean[]> mOpRestrictions = new SparseArray<boolean[]>();
103
Dianne Hackbornc2293022013-02-06 23:14:49 -0800104 public final static class Ops extends SparseArray<Op> {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800105 public final String packageName;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800106 public final int uid;
Jason Monk1c7c3192014-06-26 12:52:18 -0400107 public final boolean isPrivileged;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800108
Jason Monk1c7c3192014-06-26 12:52:18 -0400109 public Ops(String _packageName, int _uid, boolean _isPrivileged) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800110 packageName = _packageName;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800111 uid = _uid;
Jason Monk1c7c3192014-06-26 12:52:18 -0400112 isPrivileged = _isPrivileged;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800113 }
114 }
115
Dianne Hackbornc2293022013-02-06 23:14:49 -0800116 public final static class Op {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700117 public final int uid;
118 public final String packageName;
Svet Ganov99b60432015-06-27 13:15:22 -0700119 public int proxyUid = -1;
120 public String proxyPackageName;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800121 public final int op;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800122 public int mode;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800123 public int duration;
124 public long time;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800125 public long rejectTime;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800126 public int nesting;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800127
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700128 public Op(int _uid, String _packageName, int _op) {
129 uid = _uid;
130 packageName = _packageName;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800131 op = _op;
David Braunf5d83192013-09-16 13:43:51 -0700132 mode = AppOpsManager.opToDefaultMode(op);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800133 }
134 }
135
Dianne Hackbornc2293022013-02-06 23:14:49 -0800136 final SparseArray<ArrayList<Callback>> mOpModeWatchers
137 = new SparseArray<ArrayList<Callback>>();
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700138 final ArrayMap<String, ArrayList<Callback>> mPackageModeWatchers
139 = new ArrayMap<String, ArrayList<Callback>>();
140 final ArrayMap<IBinder, Callback> mModeWatchers
141 = new ArrayMap<IBinder, Callback>();
John Spurlock1af30c72014-03-10 08:33:35 -0400142 final SparseArray<SparseArray<Restriction>> mAudioRestrictions
143 = new SparseArray<SparseArray<Restriction>>();
Dianne Hackbornc2293022013-02-06 23:14:49 -0800144
145 public final class Callback implements DeathRecipient {
146 final IAppOpsCallback mCallback;
147
148 public Callback(IAppOpsCallback callback) {
149 mCallback = callback;
150 try {
151 mCallback.asBinder().linkToDeath(this, 0);
152 } catch (RemoteException e) {
153 }
154 }
155
156 public void unlinkToDeath() {
157 mCallback.asBinder().unlinkToDeath(this, 0);
158 }
159
160 @Override
161 public void binderDied() {
162 stopWatchingMode(mCallback);
163 }
164 }
165
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700166 final ArrayMap<IBinder, ClientState> mClients = new ArrayMap<IBinder, ClientState>();
167
168 public final class ClientState extends Binder implements DeathRecipient {
169 final IBinder mAppToken;
170 final int mPid;
171 final ArrayList<Op> mStartedOps;
172
173 public ClientState(IBinder appToken) {
174 mAppToken = appToken;
175 mPid = Binder.getCallingPid();
176 if (appToken instanceof Binder) {
177 // For local clients, there is no reason to track them.
178 mStartedOps = null;
179 } else {
180 mStartedOps = new ArrayList<Op>();
181 try {
182 mAppToken.linkToDeath(this, 0);
183 } catch (RemoteException e) {
184 }
185 }
186 }
187
188 @Override
189 public String toString() {
190 return "ClientState{" +
191 "mAppToken=" + mAppToken +
192 ", " + (mStartedOps != null ? ("pid=" + mPid) : "local") +
193 '}';
194 }
195
196 @Override
197 public void binderDied() {
198 synchronized (AppOpsService.this) {
199 for (int i=mStartedOps.size()-1; i>=0; i--) {
200 finishOperationLocked(mStartedOps.get(i));
201 }
202 mClients.remove(mAppToken);
203 }
204 }
205 }
206
Jeff Brown6f357d32014-01-15 20:40:55 -0800207 public AppOpsService(File storagePath, Handler handler) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800208 mFile = new AtomicFile(storagePath);
Jeff Brown6f357d32014-01-15 20:40:55 -0800209 mHandler = handler;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800210 readState();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800211 }
David Braunf5d83192013-09-16 13:43:51 -0700212
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800213 public void publish(Context context) {
214 mContext = context;
215 ServiceManager.addService(Context.APP_OPS_SERVICE, asBinder());
216 }
217
Dianne Hackborn514074f2013-02-11 10:52:46 -0800218 public void systemReady() {
219 synchronized (this) {
220 boolean changed = false;
221 for (int i=0; i<mUidOps.size(); i++) {
222 HashMap<String, Ops> pkgs = mUidOps.valueAt(i);
223 Iterator<Ops> it = pkgs.values().iterator();
224 while (it.hasNext()) {
225 Ops ops = it.next();
226 int curUid;
227 try {
228 curUid = mContext.getPackageManager().getPackageUid(ops.packageName,
229 UserHandle.getUserId(ops.uid));
230 } catch (NameNotFoundException e) {
231 curUid = -1;
232 }
233 if (curUid != ops.uid) {
234 Slog.i(TAG, "Pruning old package " + ops.packageName
235 + "/" + ops.uid + ": new uid=" + curUid);
236 it.remove();
237 changed = true;
238 }
239 }
240 if (pkgs.size() <= 0) {
241 mUidOps.removeAt(i);
242 }
243 }
244 if (changed) {
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800245 scheduleFastWriteLocked();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800246 }
247 }
248 }
249
250 public void packageRemoved(int uid, String packageName) {
251 synchronized (this) {
252 HashMap<String, Ops> pkgs = mUidOps.get(uid);
253 if (pkgs != null) {
254 if (pkgs.remove(packageName) != null) {
255 if (pkgs.size() <= 0) {
256 mUidOps.remove(uid);
257 }
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800258 scheduleFastWriteLocked();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800259 }
260 }
261 }
262 }
263
264 public void uidRemoved(int uid) {
265 synchronized (this) {
266 if (mUidOps.indexOfKey(uid) >= 0) {
267 mUidOps.remove(uid);
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800268 scheduleFastWriteLocked();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800269 }
270 }
271 }
272
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800273 public void shutdown() {
274 Slog.w(TAG, "Writing app ops before shutdown...");
Dianne Hackborn35654b62013-01-14 17:38:02 -0800275 boolean doWrite = false;
276 synchronized (this) {
277 if (mWriteScheduled) {
278 mWriteScheduled = false;
279 doWrite = true;
280 }
281 }
282 if (doWrite) {
283 writeState();
284 }
285 }
286
Dianne Hackborn72e39832013-01-18 18:36:09 -0800287 private ArrayList<AppOpsManager.OpEntry> collectOps(Ops pkgOps, int[] ops) {
288 ArrayList<AppOpsManager.OpEntry> resOps = null;
289 if (ops == null) {
290 resOps = new ArrayList<AppOpsManager.OpEntry>();
291 for (int j=0; j<pkgOps.size(); j++) {
292 Op curOp = pkgOps.valueAt(j);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800293 resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time,
Svet Ganov99b60432015-06-27 13:15:22 -0700294 curOp.rejectTime, curOp.duration, curOp.proxyUid,
295 curOp.proxyPackageName));
Dianne Hackborn72e39832013-01-18 18:36:09 -0800296 }
297 } else {
298 for (int j=0; j<ops.length; j++) {
299 Op curOp = pkgOps.get(ops[j]);
300 if (curOp != null) {
301 if (resOps == null) {
302 resOps = new ArrayList<AppOpsManager.OpEntry>();
303 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800304 resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time,
Svet Ganov99b60432015-06-27 13:15:22 -0700305 curOp.rejectTime, curOp.duration, curOp.proxyUid,
306 curOp.proxyPackageName));
Dianne Hackborn72e39832013-01-18 18:36:09 -0800307 }
308 }
309 }
310 return resOps;
311 }
312
Dianne Hackborn35654b62013-01-14 17:38:02 -0800313 @Override
314 public List<AppOpsManager.PackageOps> getPackagesForOps(int[] ops) {
315 mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS,
316 Binder.getCallingPid(), Binder.getCallingUid(), null);
317 ArrayList<AppOpsManager.PackageOps> res = null;
318 synchronized (this) {
319 for (int i=0; i<mUidOps.size(); i++) {
320 HashMap<String, Ops> packages = mUidOps.valueAt(i);
321 for (Ops pkgOps : packages.values()) {
Dianne Hackborn72e39832013-01-18 18:36:09 -0800322 ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800323 if (resOps != null) {
324 if (res == null) {
325 res = new ArrayList<AppOpsManager.PackageOps>();
326 }
327 AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps(
328 pkgOps.packageName, pkgOps.uid, resOps);
329 res.add(resPackage);
330 }
331 }
332 }
333 }
334 return res;
335 }
336
337 @Override
Dianne Hackborn72e39832013-01-18 18:36:09 -0800338 public List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName,
339 int[] ops) {
340 mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS,
341 Binder.getCallingPid(), Binder.getCallingUid(), null);
342 synchronized (this) {
343 Ops pkgOps = getOpsLocked(uid, packageName, false);
344 if (pkgOps == null) {
345 return null;
346 }
347 ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops);
348 if (resOps == null) {
349 return null;
350 }
351 ArrayList<AppOpsManager.PackageOps> res = new ArrayList<AppOpsManager.PackageOps>();
352 AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps(
353 pkgOps.packageName, pkgOps.uid, resOps);
354 res.add(resPackage);
355 return res;
356 }
357 }
358
Dianne Hackborn607b4142013-08-02 18:10:10 -0700359 private void pruneOp(Op op, int uid, String packageName) {
360 if (op.time == 0 && op.rejectTime == 0) {
361 Ops ops = getOpsLocked(uid, packageName, false);
362 if (ops != null) {
363 ops.remove(op.op);
364 if (ops.size() <= 0) {
365 HashMap<String, Ops> pkgOps = mUidOps.get(uid);
366 if (pkgOps != null) {
367 pkgOps.remove(ops.packageName);
368 if (pkgOps.size() <= 0) {
369 mUidOps.remove(uid);
370 }
371 }
372 }
373 }
374 }
375 }
376
Dianne Hackborn72e39832013-01-18 18:36:09 -0800377 @Override
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800378 public void setMode(int code, int uid, String packageName, int mode) {
Dianne Hackbornb64afe12014-07-22 16:29:04 -0700379 if (Binder.getCallingPid() != Process.myPid()) {
380 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
381 Binder.getCallingPid(), Binder.getCallingUid(), null);
Dianne Hackborn133b9df2014-07-01 13:06:10 -0700382 }
Dianne Hackborn961321f2013-02-05 17:22:41 -0800383 verifyIncomingOp(code);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800384 ArrayList<Callback> repCbs = null;
385 code = AppOpsManager.opToSwitch(code);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800386 synchronized (this) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800387 Op op = getOpLocked(code, uid, packageName, true);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800388 if (op != null) {
389 if (op.mode != mode) {
390 op.mode = mode;
Dianne Hackbornc2293022013-02-06 23:14:49 -0800391 ArrayList<Callback> cbs = mOpModeWatchers.get(code);
392 if (cbs != null) {
393 if (repCbs == null) {
394 repCbs = new ArrayList<Callback>();
395 }
396 repCbs.addAll(cbs);
397 }
398 cbs = mPackageModeWatchers.get(packageName);
399 if (cbs != null) {
400 if (repCbs == null) {
401 repCbs = new ArrayList<Callback>();
402 }
403 repCbs.addAll(cbs);
404 }
David Braunf5d83192013-09-16 13:43:51 -0700405 if (mode == AppOpsManager.opToDefaultMode(op.op)) {
Dianne Hackborn514074f2013-02-11 10:52:46 -0800406 // If going into the default mode, prune this op
407 // if there is nothing else interesting in it.
Dianne Hackborn607b4142013-08-02 18:10:10 -0700408 pruneOp(op, uid, packageName);
Dianne Hackborn514074f2013-02-11 10:52:46 -0800409 }
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800410 scheduleFastWriteLocked();
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800411 }
412 }
413 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800414 if (repCbs != null) {
Svet Ganov38536112015-05-19 12:45:52 -0700415 // There are components watching for mode changes such as window manager
416 // and location manager which are in our process. The callbacks in these
417 // components may require permissions our remote caller does not have.
418 final long identity = Binder.clearCallingIdentity();
419 try {
420 for (int i = 0; i < repCbs.size(); i++) {
421 try {
422 repCbs.get(i).mCallback.opChanged(code, packageName);
423 } catch (RemoteException e) {
424 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800425 }
Svet Ganov38536112015-05-19 12:45:52 -0700426 } finally {
427 Binder.restoreCallingIdentity(identity);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800428 }
429 }
430 }
431
Dianne Hackborn607b4142013-08-02 18:10:10 -0700432 private static HashMap<Callback, ArrayList<Pair<String, Integer>>> addCallbacks(
433 HashMap<Callback, ArrayList<Pair<String, Integer>>> callbacks,
434 String packageName, int op, ArrayList<Callback> cbs) {
435 if (cbs == null) {
436 return callbacks;
437 }
438 if (callbacks == null) {
439 callbacks = new HashMap<Callback, ArrayList<Pair<String, Integer>>>();
440 }
441 for (int i=0; i<cbs.size(); i++) {
442 Callback cb = cbs.get(i);
443 ArrayList<Pair<String, Integer>> reports = callbacks.get(cb);
444 if (reports == null) {
445 reports = new ArrayList<Pair<String, Integer>>();
446 callbacks.put(cb, reports);
447 }
448 reports.add(new Pair<String, Integer>(packageName, op));
449 }
450 return callbacks;
451 }
452
453 @Override
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800454 public void resetAllModes(int reqUserId, String reqPackageName) {
455 final int callingPid = Binder.getCallingPid();
456 final int callingUid = Binder.getCallingUid();
Dianne Hackborn607b4142013-08-02 18:10:10 -0700457 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800458 callingPid, callingUid, null);
459 reqUserId = ActivityManager.handleIncomingUser(callingPid, callingUid, reqUserId,
460 true, true, "resetAllModes", null);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700461 HashMap<Callback, ArrayList<Pair<String, Integer>>> callbacks = null;
462 synchronized (this) {
463 boolean changed = false;
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700464 for (int i=mUidOps.size()-1; i>=0; i--) {
Dianne Hackborn607b4142013-08-02 18:10:10 -0700465 HashMap<String, Ops> packages = mUidOps.valueAt(i);
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800466 if (reqUserId != UserHandle.USER_ALL
467 && reqUserId != UserHandle.getUserId(mUidOps.keyAt(i))) {
Alexandra Gherghinad6a98972014-08-04 17:05:34 +0100468 // Skip any ops for a different user
469 continue;
470 }
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700471 Iterator<Map.Entry<String, Ops>> it = packages.entrySet().iterator();
472 while (it.hasNext()) {
473 Map.Entry<String, Ops> ent = it.next();
Dianne Hackborn607b4142013-08-02 18:10:10 -0700474 String packageName = ent.getKey();
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800475 if (reqPackageName != null && !reqPackageName.equals(packageName)) {
476 // Skip any ops for a different package
477 continue;
478 }
Dianne Hackborn607b4142013-08-02 18:10:10 -0700479 Ops pkgOps = ent.getValue();
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700480 for (int j=pkgOps.size()-1; j>=0; j--) {
Dianne Hackborn607b4142013-08-02 18:10:10 -0700481 Op curOp = pkgOps.valueAt(j);
Dianne Hackborn8828d3a2013-09-25 16:47:10 -0700482 if (AppOpsManager.opAllowsReset(curOp.op)
483 && curOp.mode != AppOpsManager.opToDefaultMode(curOp.op)) {
David Braunf5d83192013-09-16 13:43:51 -0700484 curOp.mode = AppOpsManager.opToDefaultMode(curOp.op);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700485 changed = true;
486 callbacks = addCallbacks(callbacks, packageName, curOp.op,
487 mOpModeWatchers.get(curOp.op));
488 callbacks = addCallbacks(callbacks, packageName, curOp.op,
489 mPackageModeWatchers.get(packageName));
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700490 if (curOp.time == 0 && curOp.rejectTime == 0) {
491 pkgOps.removeAt(j);
492 }
Dianne Hackborn607b4142013-08-02 18:10:10 -0700493 }
494 }
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700495 if (pkgOps.size() == 0) {
496 it.remove();
497 }
498 }
499 if (packages.size() == 0) {
500 mUidOps.removeAt(i);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700501 }
502 }
503 if (changed) {
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800504 scheduleFastWriteLocked();
Dianne Hackborn607b4142013-08-02 18:10:10 -0700505 }
506 }
507 if (callbacks != null) {
508 for (Map.Entry<Callback, ArrayList<Pair<String, Integer>>> ent : callbacks.entrySet()) {
509 Callback cb = ent.getKey();
510 ArrayList<Pair<String, Integer>> reports = ent.getValue();
511 for (int i=0; i<reports.size(); i++) {
512 Pair<String, Integer> rep = reports.get(i);
513 try {
514 cb.mCallback.opChanged(rep.second, rep.first);
515 } catch (RemoteException e) {
516 }
517 }
518 }
519 }
520 }
521
Dianne Hackbornc2293022013-02-06 23:14:49 -0800522 @Override
523 public void startWatchingMode(int op, String packageName, IAppOpsCallback callback) {
524 synchronized (this) {
525 op = AppOpsManager.opToSwitch(op);
526 Callback cb = mModeWatchers.get(callback.asBinder());
527 if (cb == null) {
528 cb = new Callback(callback);
529 mModeWatchers.put(callback.asBinder(), cb);
530 }
531 if (op != AppOpsManager.OP_NONE) {
532 ArrayList<Callback> cbs = mOpModeWatchers.get(op);
533 if (cbs == null) {
534 cbs = new ArrayList<Callback>();
535 mOpModeWatchers.put(op, cbs);
536 }
537 cbs.add(cb);
538 }
539 if (packageName != null) {
540 ArrayList<Callback> cbs = mPackageModeWatchers.get(packageName);
541 if (cbs == null) {
542 cbs = new ArrayList<Callback>();
543 mPackageModeWatchers.put(packageName, cbs);
544 }
545 cbs.add(cb);
546 }
547 }
548 }
549
550 @Override
551 public void stopWatchingMode(IAppOpsCallback callback) {
552 synchronized (this) {
553 Callback cb = mModeWatchers.remove(callback.asBinder());
554 if (cb != null) {
555 cb.unlinkToDeath();
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700556 for (int i=mOpModeWatchers.size()-1; i>=0; i--) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800557 ArrayList<Callback> cbs = mOpModeWatchers.valueAt(i);
558 cbs.remove(cb);
559 if (cbs.size() <= 0) {
560 mOpModeWatchers.removeAt(i);
561 }
562 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700563 for (int i=mPackageModeWatchers.size()-1; i>=0; i--) {
564 ArrayList<Callback> cbs = mPackageModeWatchers.valueAt(i);
565 cbs.remove(cb);
566 if (cbs.size() <= 0) {
567 mPackageModeWatchers.removeAt(i);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800568 }
569 }
570 }
571 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800572 }
573
574 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700575 public IBinder getToken(IBinder clientToken) {
576 synchronized (this) {
577 ClientState cs = mClients.get(clientToken);
578 if (cs == null) {
579 cs = new ClientState(clientToken);
580 mClients.put(clientToken, cs);
581 }
582 return cs;
583 }
584 }
585
586 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -0800587 public int checkOperation(int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800588 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800589 verifyIncomingOp(code);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800590 synchronized (this) {
Jason Monk1c7c3192014-06-26 12:52:18 -0400591 if (isOpRestricted(uid, code, packageName)) {
Jason Monk62062992014-05-06 09:55:28 -0400592 return AppOpsManager.MODE_IGNORED;
593 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800594 Op op = getOpLocked(AppOpsManager.opToSwitch(code), uid, packageName, false);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800595 if (op == null) {
David Braunf5d83192013-09-16 13:43:51 -0700596 return AppOpsManager.opToDefaultMode(code);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800597 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800598 return op.mode;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800599 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800600 }
601
602 @Override
John Spurlock7b414672014-07-18 13:02:39 -0400603 public int checkAudioOperation(int code, int usage, int uid, String packageName) {
John Spurlock1af30c72014-03-10 08:33:35 -0400604 synchronized (this) {
John Spurlock7b414672014-07-18 13:02:39 -0400605 final int mode = checkRestrictionLocked(code, usage, uid, packageName);
John Spurlock1af30c72014-03-10 08:33:35 -0400606 if (mode != AppOpsManager.MODE_ALLOWED) {
607 return mode;
608 }
609 }
610 return checkOperation(code, uid, packageName);
611 }
612
John Spurlock7b414672014-07-18 13:02:39 -0400613 private int checkRestrictionLocked(int code, int usage, int uid, String packageName) {
614 final SparseArray<Restriction> usageRestrictions = mAudioRestrictions.get(code);
615 if (usageRestrictions != null) {
616 final Restriction r = usageRestrictions.get(usage);
John Spurlock1af30c72014-03-10 08:33:35 -0400617 if (r != null && !r.exceptionPackages.contains(packageName)) {
618 return r.mode;
619 }
620 }
621 return AppOpsManager.MODE_ALLOWED;
622 }
623
624 @Override
John Spurlock7b414672014-07-18 13:02:39 -0400625 public void setAudioRestriction(int code, int usage, int uid, int mode,
John Spurlock1af30c72014-03-10 08:33:35 -0400626 String[] exceptionPackages) {
627 verifyIncomingUid(uid);
628 verifyIncomingOp(code);
629 synchronized (this) {
John Spurlock7b414672014-07-18 13:02:39 -0400630 SparseArray<Restriction> usageRestrictions = mAudioRestrictions.get(code);
631 if (usageRestrictions == null) {
632 usageRestrictions = new SparseArray<Restriction>();
633 mAudioRestrictions.put(code, usageRestrictions);
John Spurlock1af30c72014-03-10 08:33:35 -0400634 }
John Spurlock7b414672014-07-18 13:02:39 -0400635 usageRestrictions.remove(usage);
John Spurlock1af30c72014-03-10 08:33:35 -0400636 if (mode != AppOpsManager.MODE_ALLOWED) {
637 final Restriction r = new Restriction();
638 r.mode = mode;
639 if (exceptionPackages != null) {
640 final int N = exceptionPackages.length;
641 r.exceptionPackages = new ArraySet<String>(N);
642 for (int i = 0; i < N; i++) {
643 final String pkg = exceptionPackages[i];
644 if (pkg != null) {
645 r.exceptionPackages.add(pkg.trim());
646 }
647 }
648 }
John Spurlock7b414672014-07-18 13:02:39 -0400649 usageRestrictions.put(usage, r);
John Spurlock1af30c72014-03-10 08:33:35 -0400650 }
651 }
652 }
653
654 @Override
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700655 public int checkPackage(int uid, String packageName) {
656 synchronized (this) {
Dianne Hackborn0fcef842014-09-12 15:38:33 -0700657 if (getOpsRawLocked(uid, packageName, true) != null) {
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700658 return AppOpsManager.MODE_ALLOWED;
659 } else {
660 return AppOpsManager.MODE_ERRORED;
661 }
662 }
663 }
664
665 @Override
Svet Ganov99b60432015-06-27 13:15:22 -0700666 public int noteProxyOperation(int code, String proxyPackageName,
667 int proxiedUid, String proxiedPackageName) {
668 verifyIncomingOp(code);
669 final int proxyMode = noteOperationUnchecked(code, Binder.getCallingUid(),
670 proxyPackageName, -1, null);
671 if (proxyMode != AppOpsManager.MODE_ALLOWED || Binder.getCallingUid() == proxiedUid) {
672 return proxyMode;
673 }
674 return noteOperationUnchecked(code, proxiedUid, proxiedPackageName,
675 Binder.getCallingUid(), proxyPackageName);
676
677 }
678
679 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800680 public int noteOperation(int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800681 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800682 verifyIncomingOp(code);
Svet Ganov99b60432015-06-27 13:15:22 -0700683 return noteOperationUnchecked(code, uid, packageName, 0, null);
684 }
685
686 private int noteOperationUnchecked(int code, int uid, String packageName,
687 int proxyUid, String proxyPackageName) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800688 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800689 Ops ops = getOpsLocked(uid, packageName, true);
690 if (ops == null) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800691 if (DEBUG) Log.d(TAG, "noteOperation: no op for code " + code + " uid " + uid
692 + " package " + packageName);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700693 return AppOpsManager.MODE_ERRORED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800694 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800695 Op op = getOpLocked(ops, code, true);
Jason Monk1c7c3192014-06-26 12:52:18 -0400696 if (isOpRestricted(uid, code, packageName)) {
Jason Monk62062992014-05-06 09:55:28 -0400697 return AppOpsManager.MODE_IGNORED;
698 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800699 if (op.duration == -1) {
700 Slog.w(TAG, "Noting op not finished: uid " + uid + " pkg " + packageName
701 + " code " + code + " time=" + op.time + " duration=" + op.duration);
702 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800703 op.duration = 0;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800704 final int switchCode = AppOpsManager.opToSwitch(code);
705 final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
706 if (switchOp.mode != AppOpsManager.MODE_ALLOWED) {
707 if (DEBUG) Log.d(TAG, "noteOperation: reject #" + op.mode + " for code "
708 + switchCode + " (" + code + ") uid " + uid + " package " + packageName);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800709 op.rejectTime = System.currentTimeMillis();
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800710 return switchOp.mode;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800711 }
712 if (DEBUG) Log.d(TAG, "noteOperation: allowing code " + code + " uid " + uid
713 + " package " + packageName);
714 op.time = System.currentTimeMillis();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800715 op.rejectTime = 0;
Svet Ganov99b60432015-06-27 13:15:22 -0700716 op.proxyUid = proxyUid;
717 op.proxyPackageName = proxyPackageName;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800718 return AppOpsManager.MODE_ALLOWED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800719 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800720 }
721
722 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700723 public int startOperation(IBinder token, int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800724 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800725 verifyIncomingOp(code);
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700726 ClientState client = (ClientState)token;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800727 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800728 Ops ops = getOpsLocked(uid, packageName, true);
729 if (ops == null) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800730 if (DEBUG) Log.d(TAG, "startOperation: no op for code " + code + " uid " + uid
731 + " package " + packageName);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700732 return AppOpsManager.MODE_ERRORED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800733 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800734 Op op = getOpLocked(ops, code, true);
Jason Monk1c7c3192014-06-26 12:52:18 -0400735 if (isOpRestricted(uid, code, packageName)) {
Jason Monk62062992014-05-06 09:55:28 -0400736 return AppOpsManager.MODE_IGNORED;
737 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800738 final int switchCode = AppOpsManager.opToSwitch(code);
739 final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
740 if (switchOp.mode != AppOpsManager.MODE_ALLOWED) {
741 if (DEBUG) Log.d(TAG, "startOperation: reject #" + op.mode + " for code "
742 + switchCode + " (" + code + ") uid " + uid + " package " + packageName);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800743 op.rejectTime = System.currentTimeMillis();
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800744 return switchOp.mode;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800745 }
746 if (DEBUG) Log.d(TAG, "startOperation: allowing code " + code + " uid " + uid
747 + " package " + packageName);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800748 if (op.nesting == 0) {
749 op.time = System.currentTimeMillis();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800750 op.rejectTime = 0;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800751 op.duration = -1;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800752 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800753 op.nesting++;
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700754 if (client.mStartedOps != null) {
755 client.mStartedOps.add(op);
756 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800757 return AppOpsManager.MODE_ALLOWED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800758 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800759 }
760
761 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700762 public void finishOperation(IBinder token, int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800763 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800764 verifyIncomingOp(code);
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700765 ClientState client = (ClientState)token;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800766 synchronized (this) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800767 Op op = getOpLocked(code, uid, packageName, true);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800768 if (op == null) {
769 return;
770 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700771 if (client.mStartedOps != null) {
772 if (!client.mStartedOps.remove(op)) {
773 throw new IllegalStateException("Operation not started: uid" + op.uid
774 + " pkg=" + op.packageName + " op=" + op.op);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800775 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800776 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700777 finishOperationLocked(op);
778 }
779 }
780
Svet Ganovb9d71a62015-04-30 10:38:13 -0700781 @Override
782 public int permissionToOpCode(String permission) {
783 return AppOpsManager.permissionToOpCode(permission);
784 }
785
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700786 void finishOperationLocked(Op op) {
787 if (op.nesting <= 1) {
788 if (op.nesting == 1) {
789 op.duration = (int)(System.currentTimeMillis() - op.time);
790 op.time += op.duration;
791 } else {
792 Slog.w(TAG, "Finishing op nesting under-run: uid " + op.uid + " pkg "
793 + op.packageName + " code " + op.op + " time=" + op.time
794 + " duration=" + op.duration + " nesting=" + op.nesting);
795 }
796 op.nesting = 0;
797 } else {
798 op.nesting--;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800799 }
800 }
801
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800802 private void verifyIncomingUid(int uid) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800803 if (uid == Binder.getCallingUid()) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800804 return;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800805 }
806 if (Binder.getCallingPid() == Process.myPid()) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800807 return;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800808 }
809 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
810 Binder.getCallingPid(), Binder.getCallingUid(), null);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800811 }
812
Dianne Hackborn961321f2013-02-05 17:22:41 -0800813 private void verifyIncomingOp(int op) {
814 if (op >= 0 && op < AppOpsManager._NUM_OP) {
815 return;
816 }
817 throw new IllegalArgumentException("Bad operation #" + op);
818 }
819
Dianne Hackborn72e39832013-01-18 18:36:09 -0800820 private Ops getOpsLocked(int uid, String packageName, boolean edit) {
Dianne Hackborn0fcef842014-09-12 15:38:33 -0700821 if (uid == 0) {
822 packageName = "root";
823 } else if (uid == Process.SHELL_UID) {
824 packageName = "com.android.shell";
825 }
826 return getOpsRawLocked(uid, packageName, edit);
827 }
828
829 private Ops getOpsRawLocked(int uid, String packageName, boolean edit) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800830 HashMap<String, Ops> pkgOps = mUidOps.get(uid);
831 if (pkgOps == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800832 if (!edit) {
833 return null;
834 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800835 pkgOps = new HashMap<String, Ops>();
836 mUidOps.put(uid, pkgOps);
837 }
838 Ops ops = pkgOps.get(packageName);
839 if (ops == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800840 if (!edit) {
841 return null;
842 }
Jason Monk1c7c3192014-06-26 12:52:18 -0400843 boolean isPrivileged = false;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800844 // This is the first time we have seen this package name under this uid,
845 // so let's make sure it is valid.
Dianne Hackborn514074f2013-02-11 10:52:46 -0800846 if (uid != 0) {
847 final long ident = Binder.clearCallingIdentity();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800848 try {
Dianne Hackborn514074f2013-02-11 10:52:46 -0800849 int pkgUid = -1;
850 try {
Jason Monk1c7c3192014-06-26 12:52:18 -0400851 ApplicationInfo appInfo = ActivityThread.getPackageManager()
852 .getApplicationInfo(packageName, 0, UserHandle.getUserId(uid));
853 if (appInfo != null) {
854 pkgUid = appInfo.uid;
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800855 isPrivileged = (appInfo.privateFlags
856 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0;
Jason Monk1c7c3192014-06-26 12:52:18 -0400857 } else {
858 if ("media".equals(packageName)) {
859 pkgUid = Process.MEDIA_UID;
860 isPrivileged = false;
861 }
Dianne Hackborn713df152013-05-17 11:27:57 -0700862 }
Jason Monk1c7c3192014-06-26 12:52:18 -0400863 } catch (RemoteException e) {
864 Slog.w(TAG, "Could not contact PackageManager", e);
Dianne Hackborn514074f2013-02-11 10:52:46 -0800865 }
866 if (pkgUid != uid) {
867 // Oops! The package name is not valid for the uid they are calling
868 // under. Abort.
869 Slog.w(TAG, "Bad call: specified package " + packageName
870 + " under uid " + uid + " but it is really " + pkgUid);
871 return null;
872 }
873 } finally {
874 Binder.restoreCallingIdentity(ident);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800875 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800876 }
Jason Monk1c7c3192014-06-26 12:52:18 -0400877 ops = new Ops(packageName, uid, isPrivileged);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800878 pkgOps.put(packageName, ops);
879 }
Dianne Hackborn72e39832013-01-18 18:36:09 -0800880 return ops;
881 }
882
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800883 private void scheduleWriteLocked() {
884 if (!mWriteScheduled) {
885 mWriteScheduled = true;
886 mHandler.postDelayed(mWriteRunner, WRITE_DELAY);
887 }
888 }
889
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800890 private void scheduleFastWriteLocked() {
891 if (!mFastWriteScheduled) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800892 mWriteScheduled = true;
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800893 mFastWriteScheduled = true;
894 mHandler.removeCallbacks(mWriteRunner);
895 mHandler.postDelayed(mWriteRunner, 10*1000);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800896 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800897 }
898
Dianne Hackborn72e39832013-01-18 18:36:09 -0800899 private Op getOpLocked(int code, int uid, String packageName, boolean edit) {
900 Ops ops = getOpsLocked(uid, packageName, edit);
901 if (ops == null) {
902 return null;
903 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800904 return getOpLocked(ops, code, edit);
905 }
906
907 private Op getOpLocked(Ops ops, int code, boolean edit) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800908 Op op = ops.get(code);
909 if (op == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800910 if (!edit) {
911 return null;
912 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700913 op = new Op(ops.uid, ops.packageName, code);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800914 ops.put(code, op);
915 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800916 if (edit) {
917 scheduleWriteLocked();
Dianne Hackborn35654b62013-01-14 17:38:02 -0800918 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800919 return op;
920 }
921
Jason Monk1c7c3192014-06-26 12:52:18 -0400922 private boolean isOpRestricted(int uid, int code, String packageName) {
Jason Monk62062992014-05-06 09:55:28 -0400923 int userHandle = UserHandle.getUserId(uid);
924 boolean[] opRestrictions = mOpRestrictions.get(userHandle);
925 if ((opRestrictions != null) && opRestrictions[code]) {
Jason Monk1c7c3192014-06-26 12:52:18 -0400926 if (AppOpsManager.opAllowSystemBypassRestriction(code)) {
927 synchronized (this) {
928 Ops ops = getOpsLocked(uid, packageName, true);
929 if ((ops != null) && ops.isPrivileged) {
930 return false;
931 }
932 }
933 }
Julia Reynolds401de172014-07-24 18:21:29 -0400934 return true;
Jason Monk62062992014-05-06 09:55:28 -0400935 }
936 return false;
937 }
938
Dianne Hackborn35654b62013-01-14 17:38:02 -0800939 void readState() {
940 synchronized (mFile) {
941 synchronized (this) {
942 FileInputStream stream;
943 try {
944 stream = mFile.openRead();
945 } catch (FileNotFoundException e) {
946 Slog.i(TAG, "No existing app ops " + mFile.getBaseFile() + "; starting empty");
947 return;
948 }
949 boolean success = false;
950 try {
951 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100952 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackborn35654b62013-01-14 17:38:02 -0800953 int type;
954 while ((type = parser.next()) != XmlPullParser.START_TAG
955 && type != XmlPullParser.END_DOCUMENT) {
956 ;
957 }
958
959 if (type != XmlPullParser.START_TAG) {
960 throw new IllegalStateException("no start tag found");
961 }
962
963 int outerDepth = parser.getDepth();
964 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
965 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
966 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
967 continue;
968 }
969
970 String tagName = parser.getName();
971 if (tagName.equals("pkg")) {
Dave Burke0997c5bd2013-08-02 20:25:02 +0000972 readPackage(parser);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800973 } else {
974 Slog.w(TAG, "Unknown element under <app-ops>: "
975 + parser.getName());
976 XmlUtils.skipCurrentTag(parser);
977 }
978 }
979 success = true;
980 } catch (IllegalStateException e) {
981 Slog.w(TAG, "Failed parsing " + e);
982 } catch (NullPointerException e) {
983 Slog.w(TAG, "Failed parsing " + e);
984 } catch (NumberFormatException e) {
985 Slog.w(TAG, "Failed parsing " + e);
986 } catch (XmlPullParserException e) {
987 Slog.w(TAG, "Failed parsing " + e);
988 } catch (IOException e) {
989 Slog.w(TAG, "Failed parsing " + e);
990 } catch (IndexOutOfBoundsException e) {
991 Slog.w(TAG, "Failed parsing " + e);
992 } finally {
993 if (!success) {
994 mUidOps.clear();
995 }
996 try {
997 stream.close();
998 } catch (IOException e) {
999 }
1000 }
1001 }
1002 }
1003 }
1004
Dave Burke0997c5bd2013-08-02 20:25:02 +00001005 void readPackage(XmlPullParser parser) throws NumberFormatException,
Dianne Hackborn35654b62013-01-14 17:38:02 -08001006 XmlPullParserException, IOException {
1007 String pkgName = parser.getAttributeValue(null, "n");
1008 int outerDepth = parser.getDepth();
1009 int type;
1010 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1011 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1012 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1013 continue;
1014 }
1015
1016 String tagName = parser.getName();
1017 if (tagName.equals("uid")) {
Dave Burke0997c5bd2013-08-02 20:25:02 +00001018 readUid(parser, pkgName);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001019 } else {
1020 Slog.w(TAG, "Unknown element under <pkg>: "
1021 + parser.getName());
1022 XmlUtils.skipCurrentTag(parser);
1023 }
1024 }
1025 }
1026
Dave Burke0997c5bd2013-08-02 20:25:02 +00001027 void readUid(XmlPullParser parser, String pkgName) throws NumberFormatException,
Dianne Hackborn35654b62013-01-14 17:38:02 -08001028 XmlPullParserException, IOException {
1029 int uid = Integer.parseInt(parser.getAttributeValue(null, "n"));
Jason Monk1c7c3192014-06-26 12:52:18 -04001030 String isPrivilegedString = parser.getAttributeValue(null, "p");
1031 boolean isPrivileged = false;
1032 if (isPrivilegedString == null) {
1033 try {
1034 IPackageManager packageManager = ActivityThread.getPackageManager();
1035 if (packageManager != null) {
1036 ApplicationInfo appInfo = ActivityThread.getPackageManager()
1037 .getApplicationInfo(pkgName, 0, UserHandle.getUserId(uid));
1038 if (appInfo != null) {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001039 isPrivileged = (appInfo.privateFlags
1040 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0;
Jason Monk1c7c3192014-06-26 12:52:18 -04001041 }
1042 } else {
1043 // Could not load data, don't add to cache so it will be loaded later.
1044 return;
1045 }
1046 } catch (RemoteException e) {
1047 Slog.w(TAG, "Could not contact PackageManager", e);
1048 }
1049 } else {
1050 isPrivileged = Boolean.parseBoolean(isPrivilegedString);
1051 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001052 int outerDepth = parser.getDepth();
1053 int type;
1054 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1055 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1056 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1057 continue;
1058 }
1059
1060 String tagName = parser.getName();
1061 if (tagName.equals("op")) {
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001062 Op op = new Op(uid, pkgName, Integer.parseInt(parser.getAttributeValue(null, "n")));
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001063 String mode = parser.getAttributeValue(null, "m");
1064 if (mode != null) {
Dave Burke0997c5bd2013-08-02 20:25:02 +00001065 op.mode = Integer.parseInt(mode);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001066 }
1067 String time = parser.getAttributeValue(null, "t");
1068 if (time != null) {
1069 op.time = Long.parseLong(time);
1070 }
1071 time = parser.getAttributeValue(null, "r");
1072 if (time != null) {
1073 op.rejectTime = Long.parseLong(time);
1074 }
1075 String dur = parser.getAttributeValue(null, "d");
1076 if (dur != null) {
1077 op.duration = Integer.parseInt(dur);
1078 }
Svet Ganov99b60432015-06-27 13:15:22 -07001079 String proxyUid = parser.getAttributeValue(null, "pu");
1080 if (proxyUid != null) {
1081 op.proxyUid = Integer.parseInt(proxyUid);
1082 }
1083 String proxyPackageName = parser.getAttributeValue(null, "pp");
1084 if (proxyPackageName != null) {
1085 op.proxyPackageName = proxyPackageName;
1086 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001087 HashMap<String, Ops> pkgOps = mUidOps.get(uid);
1088 if (pkgOps == null) {
1089 pkgOps = new HashMap<String, Ops>();
1090 mUidOps.put(uid, pkgOps);
1091 }
1092 Ops ops = pkgOps.get(pkgName);
1093 if (ops == null) {
Jason Monk1c7c3192014-06-26 12:52:18 -04001094 ops = new Ops(pkgName, uid, isPrivileged);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001095 pkgOps.put(pkgName, ops);
1096 }
1097 ops.put(op.op, op);
1098 } else {
1099 Slog.w(TAG, "Unknown element under <pkg>: "
1100 + parser.getName());
1101 XmlUtils.skipCurrentTag(parser);
1102 }
1103 }
1104 }
1105
1106 void writeState() {
1107 synchronized (mFile) {
1108 List<AppOpsManager.PackageOps> allOps = getPackagesForOps(null);
1109
1110 FileOutputStream stream;
1111 try {
1112 stream = mFile.startWrite();
1113 } catch (IOException e) {
1114 Slog.w(TAG, "Failed to write state: " + e);
1115 return;
1116 }
1117
1118 try {
1119 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001120 out.setOutput(stream, StandardCharsets.UTF_8.name());
Dianne Hackborn35654b62013-01-14 17:38:02 -08001121 out.startDocument(null, true);
1122 out.startTag(null, "app-ops");
1123
1124 if (allOps != null) {
1125 String lastPkg = null;
1126 for (int i=0; i<allOps.size(); i++) {
1127 AppOpsManager.PackageOps pkg = allOps.get(i);
1128 if (!pkg.getPackageName().equals(lastPkg)) {
1129 if (lastPkg != null) {
1130 out.endTag(null, "pkg");
1131 }
1132 lastPkg = pkg.getPackageName();
1133 out.startTag(null, "pkg");
1134 out.attribute(null, "n", lastPkg);
1135 }
1136 out.startTag(null, "uid");
1137 out.attribute(null, "n", Integer.toString(pkg.getUid()));
Jason Monk1c7c3192014-06-26 12:52:18 -04001138 synchronized (this) {
1139 Ops ops = getOpsLocked(pkg.getUid(), pkg.getPackageName(), false);
1140 // Should always be present as the list of PackageOps is generated
1141 // from Ops.
1142 if (ops != null) {
1143 out.attribute(null, "p", Boolean.toString(ops.isPrivileged));
1144 } else {
1145 out.attribute(null, "p", Boolean.toString(false));
1146 }
1147 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001148 List<AppOpsManager.OpEntry> ops = pkg.getOps();
1149 for (int j=0; j<ops.size(); j++) {
1150 AppOpsManager.OpEntry op = ops.get(j);
1151 out.startTag(null, "op");
1152 out.attribute(null, "n", Integer.toString(op.getOp()));
David Braunf5d83192013-09-16 13:43:51 -07001153 if (op.getMode() != AppOpsManager.opToDefaultMode(op.getOp())) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001154 out.attribute(null, "m", Integer.toString(op.getMode()));
1155 }
1156 long time = op.getTime();
1157 if (time != 0) {
1158 out.attribute(null, "t", Long.toString(time));
1159 }
1160 time = op.getRejectTime();
1161 if (time != 0) {
1162 out.attribute(null, "r", Long.toString(time));
1163 }
1164 int dur = op.getDuration();
1165 if (dur != 0) {
1166 out.attribute(null, "d", Integer.toString(dur));
1167 }
Svet Ganov99b60432015-06-27 13:15:22 -07001168 int proxyUid = op.getProxyUid();
1169 if (proxyUid != -1) {
1170 out.attribute(null, "pu", Integer.toString(proxyUid));
1171 }
1172 String proxyPackageName = op.getProxyPackageName();
1173 if (proxyPackageName != null) {
1174 out.attribute(null, "pp", proxyPackageName);
1175 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001176 out.endTag(null, "op");
1177 }
1178 out.endTag(null, "uid");
1179 }
1180 if (lastPkg != null) {
1181 out.endTag(null, "pkg");
1182 }
1183 }
1184
1185 out.endTag(null, "app-ops");
1186 out.endDocument();
1187 mFile.finishWrite(stream);
1188 } catch (IOException e) {
1189 Slog.w(TAG, "Failed to write state, restoring backup.", e);
1190 mFile.failWrite(stream);
1191 }
1192 }
1193 }
1194
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001195 @Override
1196 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1197 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1198 != PackageManager.PERMISSION_GRANTED) {
1199 pw.println("Permission Denial: can't dump ApOps service from from pid="
1200 + Binder.getCallingPid()
1201 + ", uid=" + Binder.getCallingUid());
1202 return;
1203 }
1204
1205 synchronized (this) {
1206 pw.println("Current AppOps Service state:");
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001207 final long now = System.currentTimeMillis();
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001208 boolean needSep = false;
1209 if (mOpModeWatchers.size() > 0) {
1210 needSep = true;
1211 pw.println(" Op mode watchers:");
1212 for (int i=0; i<mOpModeWatchers.size(); i++) {
1213 pw.print(" Op "); pw.print(AppOpsManager.opToName(mOpModeWatchers.keyAt(i)));
1214 pw.println(":");
1215 ArrayList<Callback> callbacks = mOpModeWatchers.valueAt(i);
1216 for (int j=0; j<callbacks.size(); j++) {
1217 pw.print(" #"); pw.print(j); pw.print(": ");
1218 pw.println(callbacks.get(j));
1219 }
1220 }
1221 }
1222 if (mPackageModeWatchers.size() > 0) {
1223 needSep = true;
1224 pw.println(" Package mode watchers:");
1225 for (int i=0; i<mPackageModeWatchers.size(); i++) {
1226 pw.print(" Pkg "); pw.print(mPackageModeWatchers.keyAt(i));
1227 pw.println(":");
1228 ArrayList<Callback> callbacks = mPackageModeWatchers.valueAt(i);
1229 for (int j=0; j<callbacks.size(); j++) {
1230 pw.print(" #"); pw.print(j); pw.print(": ");
1231 pw.println(callbacks.get(j));
1232 }
1233 }
1234 }
1235 if (mModeWatchers.size() > 0) {
1236 needSep = true;
1237 pw.println(" All mode watchers:");
1238 for (int i=0; i<mModeWatchers.size(); i++) {
1239 pw.print(" "); pw.print(mModeWatchers.keyAt(i));
1240 pw.print(" -> "); pw.println(mModeWatchers.valueAt(i));
1241 }
1242 }
1243 if (mClients.size() > 0) {
1244 needSep = true;
1245 pw.println(" Clients:");
1246 for (int i=0; i<mClients.size(); i++) {
1247 pw.print(" "); pw.print(mClients.keyAt(i)); pw.println(":");
1248 ClientState cs = mClients.valueAt(i);
1249 pw.print(" "); pw.println(cs);
1250 if (cs.mStartedOps != null && cs.mStartedOps.size() > 0) {
1251 pw.println(" Started ops:");
1252 for (int j=0; j<cs.mStartedOps.size(); j++) {
1253 Op op = cs.mStartedOps.get(j);
1254 pw.print(" "); pw.print("uid="); pw.print(op.uid);
1255 pw.print(" pkg="); pw.print(op.packageName);
1256 pw.print(" op="); pw.println(AppOpsManager.opToName(op.op));
1257 }
1258 }
1259 }
1260 }
John Spurlock1af30c72014-03-10 08:33:35 -04001261 if (mAudioRestrictions.size() > 0) {
1262 boolean printedHeader = false;
1263 for (int o=0; o<mAudioRestrictions.size(); o++) {
1264 final String op = AppOpsManager.opToName(mAudioRestrictions.keyAt(o));
1265 final SparseArray<Restriction> restrictions = mAudioRestrictions.valueAt(o);
1266 for (int i=0; i<restrictions.size(); i++) {
1267 if (!printedHeader){
1268 pw.println(" Audio Restrictions:");
1269 printedHeader = true;
1270 needSep = true;
1271 }
John Spurlock7b414672014-07-18 13:02:39 -04001272 final int usage = restrictions.keyAt(i);
John Spurlock1af30c72014-03-10 08:33:35 -04001273 pw.print(" "); pw.print(op);
John Spurlock7b414672014-07-18 13:02:39 -04001274 pw.print(" usage="); pw.print(AudioAttributes.usageToString(usage));
John Spurlock1af30c72014-03-10 08:33:35 -04001275 Restriction r = restrictions.valueAt(i);
1276 pw.print(": mode="); pw.println(r.mode);
1277 if (!r.exceptionPackages.isEmpty()) {
1278 pw.println(" Exceptions:");
1279 for (int j=0; j<r.exceptionPackages.size(); j++) {
1280 pw.print(" "); pw.println(r.exceptionPackages.valueAt(j));
1281 }
1282 }
1283 }
1284 }
1285 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001286 if (needSep) {
1287 pw.println();
1288 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001289 for (int i=0; i<mUidOps.size(); i++) {
1290 pw.print(" Uid "); UserHandle.formatUid(pw, mUidOps.keyAt(i)); pw.println(":");
1291 HashMap<String, Ops> pkgOps = mUidOps.valueAt(i);
1292 for (Ops ops : pkgOps.values()) {
1293 pw.print(" Package "); pw.print(ops.packageName); pw.println(":");
1294 for (int j=0; j<ops.size(); j++) {
1295 Op op = ops.valueAt(j);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001296 pw.print(" "); pw.print(AppOpsManager.opToName(op.op));
1297 pw.print(": mode="); pw.print(op.mode);
1298 if (op.time != 0) {
1299 pw.print("; time="); TimeUtils.formatDuration(now-op.time, pw);
1300 pw.print(" ago");
1301 }
1302 if (op.rejectTime != 0) {
1303 pw.print("; rejectTime="); TimeUtils.formatDuration(now-op.rejectTime, pw);
1304 pw.print(" ago");
1305 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001306 if (op.duration == -1) {
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -08001307 pw.print(" (running)");
1308 } else if (op.duration != 0) {
1309 pw.print("; duration="); TimeUtils.formatDuration(op.duration, pw);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001310 }
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -08001311 pw.println();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001312 }
1313 }
1314 }
1315 }
1316 }
John Spurlock1af30c72014-03-10 08:33:35 -04001317
1318 private static final class Restriction {
1319 private static final ArraySet<String> NO_EXCEPTIONS = new ArraySet<String>();
1320 int mode;
1321 ArraySet<String> exceptionPackages = NO_EXCEPTIONS;
1322 }
Jason Monk62062992014-05-06 09:55:28 -04001323
1324 @Override
Jason Monk62062992014-05-06 09:55:28 -04001325 public void setUserRestrictions(Bundle restrictions, int userHandle) throws RemoteException {
1326 checkSystemUid("setUserRestrictions");
1327 boolean[] opRestrictions = mOpRestrictions.get(userHandle);
1328 if (opRestrictions == null) {
1329 opRestrictions = new boolean[AppOpsManager._NUM_OP];
1330 mOpRestrictions.put(userHandle, opRestrictions);
1331 }
1332 for (int i = 0; i < opRestrictions.length; ++i) {
1333 String restriction = AppOpsManager.opToRestriction(i);
1334 if (restriction != null) {
1335 opRestrictions[i] = restrictions.getBoolean(restriction, false);
1336 } else {
1337 opRestrictions[i] = false;
1338 }
1339 }
1340 }
1341
1342 @Override
1343 public void removeUser(int userHandle) throws RemoteException {
1344 checkSystemUid("removeUser");
1345 mOpRestrictions.remove(userHandle);
Jason Monk62062992014-05-06 09:55:28 -04001346 }
1347
1348 private void checkSystemUid(String function) {
1349 int uid = Binder.getCallingUid();
1350 if (uid != Process.SYSTEM_UID) {
1351 throw new SecurityException(function + " must by called by the system");
1352 }
1353 }
1354
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001355}