blob: ec027895ce99f739fa3a37a8b484f4669fdcd5e8 [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;
Svet Ganov2af57082015-07-30 08:44:20 -070035import android.app.AppGlobals;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080036import android.app.AppOpsManager;
37import android.content.Context;
Jason Monk1c7c3192014-06-26 12:52:18 -040038import android.content.pm.ApplicationInfo;
39import android.content.pm.IPackageManager;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080040import android.content.pm.PackageManager;
41import android.content.pm.PackageManager.NameNotFoundException;
John Spurlock7b414672014-07-18 13:02:39 -040042import android.media.AudioAttributes;
Dianne Hackborn35654b62013-01-14 17:38:02 -080043import android.os.AsyncTask;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080044import android.os.Binder;
Jason Monk62062992014-05-06 09:55:28 -040045import android.os.Bundle;
Dianne Hackborn35654b62013-01-14 17:38:02 -080046import android.os.Handler;
Dianne Hackbornc2293022013-02-06 23:14:49 -080047import android.os.IBinder;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080048import android.os.Process;
Dianne Hackbornc2293022013-02-06 23:14:49 -080049import android.os.RemoteException;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080050import android.os.ServiceManager;
51import android.os.UserHandle;
Svet Ganov6ee871e2015-07-10 14:29:33 -070052import android.os.storage.MountServiceInternal;
Dianne Hackborne98f5db2013-07-17 17:23:25 -070053import android.util.ArrayMap;
John Spurlock1af30c72014-03-10 08:33:35 -040054import android.util.ArraySet;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080055import android.util.AtomicFile;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -080056import android.util.Log;
Dianne Hackborn607b4142013-08-02 18:10:10 -070057import android.util.Pair;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080058import android.util.Slog;
59import android.util.SparseArray;
Svet Ganov2af57082015-07-30 08:44:20 -070060import android.util.SparseIntArray;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080061import android.util.TimeUtils;
Dianne Hackborn35654b62013-01-14 17:38:02 -080062import android.util.Xml;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080063
64import com.android.internal.app.IAppOpsService;
Dianne Hackbornc2293022013-02-06 23:14:49 -080065import com.android.internal.app.IAppOpsCallback;
Svet Ganov6ee871e2015-07-10 14:29:33 -070066import com.android.internal.os.Zygote;
Svet Ganov2af57082015-07-30 08:44:20 -070067import com.android.internal.util.ArrayUtils;
Dianne Hackborn35654b62013-01-14 17:38:02 -080068import com.android.internal.util.FastXmlSerializer;
69import com.android.internal.util.XmlUtils;
70
Svet Ganov2af57082015-07-30 08:44:20 -070071import libcore.util.EmptyArray;
Dianne Hackborn35654b62013-01-14 17:38:02 -080072import org.xmlpull.v1.XmlPullParser;
73import org.xmlpull.v1.XmlPullParserException;
74import org.xmlpull.v1.XmlSerializer;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080075
76public class AppOpsService extends IAppOpsService.Stub {
77 static final String TAG = "AppOps";
Dianne Hackborn35654b62013-01-14 17:38:02 -080078 static final boolean DEBUG = false;
79
80 // Write at most every 30 minutes.
81 static final long WRITE_DELAY = DEBUG ? 1000 : 30*60*1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -080082
83 Context mContext;
84 final AtomicFile mFile;
Dianne Hackborn35654b62013-01-14 17:38:02 -080085 final Handler mHandler;
86
87 boolean mWriteScheduled;
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -080088 boolean mFastWriteScheduled;
Dianne Hackborn35654b62013-01-14 17:38:02 -080089 final Runnable mWriteRunner = new Runnable() {
90 public void run() {
91 synchronized (AppOpsService.this) {
92 mWriteScheduled = false;
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -080093 mFastWriteScheduled = false;
Dianne Hackborn35654b62013-01-14 17:38:02 -080094 AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
95 @Override protected Void doInBackground(Void... params) {
96 writeState();
97 return null;
98 }
99 };
100 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
101 }
102 }
103 };
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800104
Svet Ganov2af57082015-07-30 08:44:20 -0700105 final SparseArray<UidState> mUidStates = new SparseArray<>();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800106
Jason Monk62062992014-05-06 09:55:28 -0400107 private final SparseArray<boolean[]> mOpRestrictions = new SparseArray<boolean[]>();
108
Svet Ganov2af57082015-07-30 08:44:20 -0700109 private static final class UidState {
110 public final int uid;
111 public ArrayMap<String, Ops> pkgOps;
112 public SparseIntArray opModes;
113
114 public UidState(int uid) {
115 this.uid = uid;
116 }
117
118 public void clear() {
119 pkgOps = null;
120 opModes = null;
121 }
122
123 public boolean isDefault() {
124 return (pkgOps == null || pkgOps.isEmpty())
125 && (opModes == null || opModes.size() <= 0);
126 }
127 }
128
Dianne Hackbornc2293022013-02-06 23:14:49 -0800129 public final static class Ops extends SparseArray<Op> {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800130 public final String packageName;
Svet Ganov2af57082015-07-30 08:44:20 -0700131 public final UidState uidState;
Jason Monk1c7c3192014-06-26 12:52:18 -0400132 public final boolean isPrivileged;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800133
Svet Ganov2af57082015-07-30 08:44:20 -0700134 public Ops(String _packageName, UidState _uidState, boolean _isPrivileged) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800135 packageName = _packageName;
Svet Ganov2af57082015-07-30 08:44:20 -0700136 uidState = _uidState;
Jason Monk1c7c3192014-06-26 12:52:18 -0400137 isPrivileged = _isPrivileged;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800138 }
139 }
140
Dianne Hackbornc2293022013-02-06 23:14:49 -0800141 public final static class Op {
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700142 public final int uid;
143 public final String packageName;
Svet Ganov99b60432015-06-27 13:15:22 -0700144 public int proxyUid = -1;
145 public String proxyPackageName;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800146 public final int op;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800147 public int mode;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800148 public int duration;
149 public long time;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800150 public long rejectTime;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800151 public int nesting;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800152
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700153 public Op(int _uid, String _packageName, int _op) {
154 uid = _uid;
155 packageName = _packageName;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800156 op = _op;
David Braunf5d83192013-09-16 13:43:51 -0700157 mode = AppOpsManager.opToDefaultMode(op);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800158 }
159 }
160
Dianne Hackbornc2293022013-02-06 23:14:49 -0800161 final SparseArray<ArrayList<Callback>> mOpModeWatchers
162 = new SparseArray<ArrayList<Callback>>();
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700163 final ArrayMap<String, ArrayList<Callback>> mPackageModeWatchers
164 = new ArrayMap<String, ArrayList<Callback>>();
165 final ArrayMap<IBinder, Callback> mModeWatchers
166 = new ArrayMap<IBinder, Callback>();
John Spurlock1af30c72014-03-10 08:33:35 -0400167 final SparseArray<SparseArray<Restriction>> mAudioRestrictions
168 = new SparseArray<SparseArray<Restriction>>();
Dianne Hackbornc2293022013-02-06 23:14:49 -0800169
170 public final class Callback implements DeathRecipient {
171 final IAppOpsCallback mCallback;
172
173 public Callback(IAppOpsCallback callback) {
174 mCallback = callback;
175 try {
176 mCallback.asBinder().linkToDeath(this, 0);
177 } catch (RemoteException e) {
178 }
179 }
180
181 public void unlinkToDeath() {
182 mCallback.asBinder().unlinkToDeath(this, 0);
183 }
184
185 @Override
186 public void binderDied() {
187 stopWatchingMode(mCallback);
188 }
189 }
190
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700191 final ArrayMap<IBinder, ClientState> mClients = new ArrayMap<IBinder, ClientState>();
192
193 public final class ClientState extends Binder implements DeathRecipient {
194 final IBinder mAppToken;
195 final int mPid;
196 final ArrayList<Op> mStartedOps;
197
198 public ClientState(IBinder appToken) {
199 mAppToken = appToken;
200 mPid = Binder.getCallingPid();
201 if (appToken instanceof Binder) {
202 // For local clients, there is no reason to track them.
203 mStartedOps = null;
204 } else {
205 mStartedOps = new ArrayList<Op>();
206 try {
207 mAppToken.linkToDeath(this, 0);
208 } catch (RemoteException e) {
209 }
210 }
211 }
212
213 @Override
214 public String toString() {
215 return "ClientState{" +
216 "mAppToken=" + mAppToken +
217 ", " + (mStartedOps != null ? ("pid=" + mPid) : "local") +
218 '}';
219 }
220
221 @Override
222 public void binderDied() {
223 synchronized (AppOpsService.this) {
224 for (int i=mStartedOps.size()-1; i>=0; i--) {
225 finishOperationLocked(mStartedOps.get(i));
226 }
227 mClients.remove(mAppToken);
228 }
229 }
230 }
231
Jeff Brown6f357d32014-01-15 20:40:55 -0800232 public AppOpsService(File storagePath, Handler handler) {
Dianne Hackborn35654b62013-01-14 17:38:02 -0800233 mFile = new AtomicFile(storagePath);
Jeff Brown6f357d32014-01-15 20:40:55 -0800234 mHandler = handler;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800235 readState();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800236 }
David Braunf5d83192013-09-16 13:43:51 -0700237
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800238 public void publish(Context context) {
239 mContext = context;
240 ServiceManager.addService(Context.APP_OPS_SERVICE, asBinder());
241 }
242
Dianne Hackborn514074f2013-02-11 10:52:46 -0800243 public void systemReady() {
244 synchronized (this) {
245 boolean changed = false;
Svet Ganov2af57082015-07-30 08:44:20 -0700246 for (int i = mUidStates.size() - 1; i >= 0; i--) {
247 UidState uidState = mUidStates.valueAt(i);
248
249 String[] packageNames = getPackagesForUid(uidState.uid);
250 if (ArrayUtils.isEmpty(packageNames)) {
251 uidState.clear();
252 mUidStates.removeAt(i);
253 changed = true;
254 continue;
255 }
256
257 ArrayMap<String, Ops> pkgs = uidState.pkgOps;
258 if (pkgs == null) {
259 continue;
260 }
261
Dianne Hackborn514074f2013-02-11 10:52:46 -0800262 Iterator<Ops> it = pkgs.values().iterator();
263 while (it.hasNext()) {
264 Ops ops = it.next();
265 int curUid;
266 try {
267 curUid = mContext.getPackageManager().getPackageUid(ops.packageName,
Svet Ganov2af57082015-07-30 08:44:20 -0700268 UserHandle.getUserId(ops.uidState.uid));
Dianne Hackborn514074f2013-02-11 10:52:46 -0800269 } catch (NameNotFoundException e) {
270 curUid = -1;
271 }
Svet Ganov2af57082015-07-30 08:44:20 -0700272 if (curUid != ops.uidState.uid) {
Dianne Hackborn514074f2013-02-11 10:52:46 -0800273 Slog.i(TAG, "Pruning old package " + ops.packageName
Svet Ganov2af57082015-07-30 08:44:20 -0700274 + "/" + ops.uidState + ": new uid=" + curUid);
Dianne Hackborn514074f2013-02-11 10:52:46 -0800275 it.remove();
276 changed = true;
277 }
278 }
Svet Ganov2af57082015-07-30 08:44:20 -0700279
280 if (uidState.isDefault()) {
281 mUidStates.removeAt(i);
Dianne Hackborn514074f2013-02-11 10:52:46 -0800282 }
283 }
284 if (changed) {
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800285 scheduleFastWriteLocked();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800286 }
287 }
Svet Ganov6ee871e2015-07-10 14:29:33 -0700288
289 MountServiceInternal mountServiceInternal = LocalServices.getService(
290 MountServiceInternal.class);
291 mountServiceInternal.addExternalStoragePolicy(
292 new MountServiceInternal.ExternalStorageMountPolicy() {
293 @Override
294 public int getMountMode(int uid, String packageName) {
295 if (Process.isIsolated(uid)) {
296 return Zygote.MOUNT_EXTERNAL_NONE;
297 }
298 if (noteOperation(AppOpsManager.OP_READ_EXTERNAL_STORAGE, uid,
299 packageName) != AppOpsManager.MODE_ALLOWED) {
300 return Zygote.MOUNT_EXTERNAL_NONE;
301 }
302 if (noteOperation(AppOpsManager.OP_WRITE_EXTERNAL_STORAGE, uid,
303 packageName) != AppOpsManager.MODE_ALLOWED) {
304 return Zygote.MOUNT_EXTERNAL_READ;
305 }
306 return Zygote.MOUNT_EXTERNAL_WRITE;
307 }
308
309 @Override
310 public boolean hasExternalStorage(int uid, String packageName) {
311 final int mountMode = getMountMode(uid, packageName);
312 return mountMode == Zygote.MOUNT_EXTERNAL_READ
313 || mountMode == Zygote.MOUNT_EXTERNAL_WRITE;
314 }
315 });
Dianne Hackborn514074f2013-02-11 10:52:46 -0800316 }
317
318 public void packageRemoved(int uid, String packageName) {
319 synchronized (this) {
Svet Ganov2af57082015-07-30 08:44:20 -0700320 UidState uidState = mUidStates.get(uid);
321 if (uidState == null) {
322 return;
323 }
324
325 boolean changed = false;
326
327 // Remove any package state if such.
328 if (uidState.pkgOps != null && uidState.pkgOps.remove(packageName) != null) {
329 changed = true;
330 }
331
332 // If we just nuked the last package state check if the UID is valid.
333 if (changed && uidState.pkgOps.isEmpty()
334 && getPackagesForUid(uid).length <= 0) {
335 mUidStates.remove(uid);
336 }
337
338 if (changed) {
339 scheduleFastWriteLocked();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800340 }
341 }
342 }
343
344 public void uidRemoved(int uid) {
345 synchronized (this) {
Svet Ganov2af57082015-07-30 08:44:20 -0700346 if (mUidStates.indexOfKey(uid) >= 0) {
347 mUidStates.remove(uid);
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800348 scheduleFastWriteLocked();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800349 }
350 }
351 }
352
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800353 public void shutdown() {
354 Slog.w(TAG, "Writing app ops before shutdown...");
Dianne Hackborn35654b62013-01-14 17:38:02 -0800355 boolean doWrite = false;
356 synchronized (this) {
357 if (mWriteScheduled) {
358 mWriteScheduled = false;
359 doWrite = true;
360 }
361 }
362 if (doWrite) {
363 writeState();
364 }
365 }
366
Dianne Hackborn72e39832013-01-18 18:36:09 -0800367 private ArrayList<AppOpsManager.OpEntry> collectOps(Ops pkgOps, int[] ops) {
368 ArrayList<AppOpsManager.OpEntry> resOps = null;
369 if (ops == null) {
370 resOps = new ArrayList<AppOpsManager.OpEntry>();
371 for (int j=0; j<pkgOps.size(); j++) {
372 Op curOp = pkgOps.valueAt(j);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800373 resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time,
Svet Ganov99b60432015-06-27 13:15:22 -0700374 curOp.rejectTime, curOp.duration, curOp.proxyUid,
375 curOp.proxyPackageName));
Dianne Hackborn72e39832013-01-18 18:36:09 -0800376 }
377 } else {
378 for (int j=0; j<ops.length; j++) {
379 Op curOp = pkgOps.get(ops[j]);
380 if (curOp != null) {
381 if (resOps == null) {
382 resOps = new ArrayList<AppOpsManager.OpEntry>();
383 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800384 resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time,
Svet Ganov99b60432015-06-27 13:15:22 -0700385 curOp.rejectTime, curOp.duration, curOp.proxyUid,
386 curOp.proxyPackageName));
Dianne Hackborn72e39832013-01-18 18:36:09 -0800387 }
388 }
389 }
390 return resOps;
391 }
392
Dianne Hackborn35654b62013-01-14 17:38:02 -0800393 @Override
394 public List<AppOpsManager.PackageOps> getPackagesForOps(int[] ops) {
395 mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS,
396 Binder.getCallingPid(), Binder.getCallingUid(), null);
397 ArrayList<AppOpsManager.PackageOps> res = null;
398 synchronized (this) {
Svet Ganov2af57082015-07-30 08:44:20 -0700399 final int uidStateCount = mUidStates.size();
400 for (int i = 0; i < uidStateCount; i++) {
401 UidState uidState = mUidStates.valueAt(i);
402 if (uidState.pkgOps == null || uidState.pkgOps.isEmpty()) {
403 continue;
404 }
405 ArrayMap<String, Ops> packages = uidState.pkgOps;
406 final int packageCount = packages.size();
407 for (int j = 0; j < packageCount; j++) {
408 Ops pkgOps = packages.valueAt(j);
Dianne Hackborn72e39832013-01-18 18:36:09 -0800409 ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800410 if (resOps != null) {
411 if (res == null) {
412 res = new ArrayList<AppOpsManager.PackageOps>();
413 }
414 AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps(
Svet Ganov2af57082015-07-30 08:44:20 -0700415 pkgOps.packageName, pkgOps.uidState.uid, resOps);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800416 res.add(resPackage);
417 }
418 }
419 }
420 }
421 return res;
422 }
423
424 @Override
Dianne Hackborn72e39832013-01-18 18:36:09 -0800425 public List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName,
426 int[] ops) {
427 mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS,
428 Binder.getCallingPid(), Binder.getCallingUid(), null);
429 synchronized (this) {
430 Ops pkgOps = getOpsLocked(uid, packageName, false);
431 if (pkgOps == null) {
432 return null;
433 }
434 ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops);
435 if (resOps == null) {
436 return null;
437 }
438 ArrayList<AppOpsManager.PackageOps> res = new ArrayList<AppOpsManager.PackageOps>();
439 AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps(
Svet Ganov2af57082015-07-30 08:44:20 -0700440 pkgOps.packageName, pkgOps.uidState.uid, resOps);
Dianne Hackborn72e39832013-01-18 18:36:09 -0800441 res.add(resPackage);
442 return res;
443 }
444 }
445
Dianne Hackborn607b4142013-08-02 18:10:10 -0700446 private void pruneOp(Op op, int uid, String packageName) {
447 if (op.time == 0 && op.rejectTime == 0) {
448 Ops ops = getOpsLocked(uid, packageName, false);
449 if (ops != null) {
450 ops.remove(op.op);
451 if (ops.size() <= 0) {
Svet Ganov2af57082015-07-30 08:44:20 -0700452 UidState uidState = ops.uidState;
453 ArrayMap<String, Ops> pkgOps = uidState.pkgOps;
Dianne Hackborn607b4142013-08-02 18:10:10 -0700454 if (pkgOps != null) {
455 pkgOps.remove(ops.packageName);
Svet Ganov2af57082015-07-30 08:44:20 -0700456 if (pkgOps.isEmpty()) {
457 uidState.pkgOps = null;
458 }
459 if (uidState.isDefault()) {
460 mUidStates.remove(uid);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700461 }
462 }
463 }
464 }
465 }
466 }
467
Dianne Hackborn72e39832013-01-18 18:36:09 -0800468 @Override
Svet Ganov2af57082015-07-30 08:44:20 -0700469 public void setUidMode(int code, int uid, int mode) {
470 if (Binder.getCallingPid() != Process.myPid()) {
471 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
472 Binder.getCallingPid(), Binder.getCallingUid(), null);
473 }
474 verifyIncomingOp(code);
475 code = AppOpsManager.opToSwitch(code);
476
477 synchronized (this) {
478 final int defaultMode = AppOpsManager.opToDefaultMode(code);
479
480 UidState uidState = getUidStateLocked(uid, false);
481 if (uidState == null) {
482 if (mode == defaultMode) {
483 return;
484 }
485 uidState = new UidState(uid);
486 uidState.opModes = new SparseIntArray();
487 uidState.opModes.put(code, mode);
488 mUidStates.put(uid, uidState);
489 scheduleWriteLocked();
490 } else if (uidState.opModes == null) {
491 if (mode != defaultMode) {
492 uidState.opModes = new SparseIntArray();
493 uidState.opModes.put(code, mode);
494 scheduleWriteLocked();
495 }
496 } else {
497 if (uidState.opModes.get(code) == mode) {
498 return;
499 }
500 if (mode == defaultMode) {
501 uidState.opModes.delete(code);
502 if (uidState.opModes.size() <= 0) {
503 uidState.opModes = null;
504 }
505 } else {
506 uidState.opModes.put(code, mode);
507 }
508 scheduleWriteLocked();
509 }
510 }
511
512 ArrayMap<Callback, ArraySet<String>> callbackSpecs = null;
513
514 ArrayList<Callback> callbacks = mOpModeWatchers.get(code);
515 if (callbacks != null) {
516 final int callbackCount = callbacks.size();
517 for (int i = 0; i < callbackCount; i++) {
518 Callback callback = callbacks.get(i);
519 callbackSpecs = new ArrayMap<>();
520 callbackSpecs.put(callback, null);
521 }
522 }
523
524 String[] uidPackageNames = getPackagesForUid(uid);
525 for (String uidPackageName : uidPackageNames) {
526 callbacks = mPackageModeWatchers.get(uidPackageName);
527 if (callbacks != null) {
528 if (callbackSpecs == null) {
529 callbackSpecs = new ArrayMap<>();
530 }
531 final int callbackCount = callbacks.size();
532 for (int i = 0; i < callbackCount; i++) {
533 Callback callback = callbacks.get(i);
534 ArraySet<String> changedPackages = callbackSpecs.get(callback);
535 if (changedPackages == null) {
536 changedPackages = new ArraySet<>();
537 callbackSpecs.put(callback, changedPackages);
538 }
539 changedPackages.add(uidPackageName);
540 }
541 }
542 }
543
544 if (callbackSpecs == null) {
545 return;
546 }
547
548 // There are components watching for mode changes such as window manager
549 // and location manager which are in our process. The callbacks in these
550 // components may require permissions our remote caller does not have.
551 final long identity = Binder.clearCallingIdentity();
552 try {
553 for (int i = 0; i < callbackSpecs.size(); i++) {
554 Callback callback = callbackSpecs.keyAt(i);
555 ArraySet<String> reportedPackageNames = callbackSpecs.valueAt(i);
556 try {
557 if (reportedPackageNames == null) {
558 callback.mCallback.opChanged(code, null);
559 } else {
560 final int reportedPackageCount = reportedPackageNames.size();
561 for (int j = 0; j < reportedPackageCount; j++) {
562 String reportedPackageName = reportedPackageNames.valueAt(j);
563 callback.mCallback.opChanged(code, reportedPackageName);
564 }
565 }
566 } catch (RemoteException e) {
567 Log.w(TAG, "Error dispatching op op change", e);
568 }
569 }
570 } finally {
571 Binder.restoreCallingIdentity(identity);
572 }
573 }
574
575 @Override
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800576 public void setMode(int code, int uid, String packageName, int mode) {
Dianne Hackbornb64afe12014-07-22 16:29:04 -0700577 if (Binder.getCallingPid() != Process.myPid()) {
578 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
579 Binder.getCallingPid(), Binder.getCallingUid(), null);
Dianne Hackborn133b9df2014-07-01 13:06:10 -0700580 }
Dianne Hackborn961321f2013-02-05 17:22:41 -0800581 verifyIncomingOp(code);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800582 ArrayList<Callback> repCbs = null;
583 code = AppOpsManager.opToSwitch(code);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800584 synchronized (this) {
Svet Ganov2af57082015-07-30 08:44:20 -0700585 UidState uidState = getUidStateLocked(uid, false);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800586 Op op = getOpLocked(code, uid, packageName, true);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800587 if (op != null) {
588 if (op.mode != mode) {
589 op.mode = mode;
Dianne Hackbornc2293022013-02-06 23:14:49 -0800590 ArrayList<Callback> cbs = mOpModeWatchers.get(code);
591 if (cbs != null) {
592 if (repCbs == null) {
593 repCbs = new ArrayList<Callback>();
594 }
595 repCbs.addAll(cbs);
596 }
597 cbs = mPackageModeWatchers.get(packageName);
598 if (cbs != null) {
599 if (repCbs == null) {
600 repCbs = new ArrayList<Callback>();
601 }
602 repCbs.addAll(cbs);
603 }
David Braunf5d83192013-09-16 13:43:51 -0700604 if (mode == AppOpsManager.opToDefaultMode(op.op)) {
Dianne Hackborn514074f2013-02-11 10:52:46 -0800605 // If going into the default mode, prune this op
606 // if there is nothing else interesting in it.
Dianne Hackborn607b4142013-08-02 18:10:10 -0700607 pruneOp(op, uid, packageName);
Dianne Hackborn514074f2013-02-11 10:52:46 -0800608 }
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800609 scheduleFastWriteLocked();
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800610 }
611 }
612 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800613 if (repCbs != null) {
Svet Ganov38536112015-05-19 12:45:52 -0700614 // There are components watching for mode changes such as window manager
615 // and location manager which are in our process. The callbacks in these
616 // components may require permissions our remote caller does not have.
617 final long identity = Binder.clearCallingIdentity();
618 try {
619 for (int i = 0; i < repCbs.size(); i++) {
620 try {
621 repCbs.get(i).mCallback.opChanged(code, packageName);
622 } catch (RemoteException e) {
623 }
Dianne Hackbornc2293022013-02-06 23:14:49 -0800624 }
Svet Ganov38536112015-05-19 12:45:52 -0700625 } finally {
626 Binder.restoreCallingIdentity(identity);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800627 }
628 }
629 }
630
Dianne Hackborn607b4142013-08-02 18:10:10 -0700631 private static HashMap<Callback, ArrayList<Pair<String, Integer>>> addCallbacks(
632 HashMap<Callback, ArrayList<Pair<String, Integer>>> callbacks,
633 String packageName, int op, ArrayList<Callback> cbs) {
634 if (cbs == null) {
635 return callbacks;
636 }
637 if (callbacks == null) {
638 callbacks = new HashMap<Callback, ArrayList<Pair<String, Integer>>>();
639 }
Svet Ganov2af57082015-07-30 08:44:20 -0700640 boolean duplicate = false;
Dianne Hackborn607b4142013-08-02 18:10:10 -0700641 for (int i=0; i<cbs.size(); i++) {
642 Callback cb = cbs.get(i);
643 ArrayList<Pair<String, Integer>> reports = callbacks.get(cb);
644 if (reports == null) {
645 reports = new ArrayList<Pair<String, Integer>>();
646 callbacks.put(cb, reports);
Svet Ganov2af57082015-07-30 08:44:20 -0700647 } else {
648 final int reportCount = reports.size();
649 for (int j = 0; j < reportCount; j++) {
650 Pair<String, Integer> report = reports.get(j);
651 if (report.second == op && report.first.equals(packageName)) {
652 duplicate = true;
653 break;
654 }
655 }
Dianne Hackborn607b4142013-08-02 18:10:10 -0700656 }
Svet Ganov2af57082015-07-30 08:44:20 -0700657 if (!duplicate) {
658 reports.add(new Pair<>(packageName, op));
659 }
Dianne Hackborn607b4142013-08-02 18:10:10 -0700660 }
661 return callbacks;
662 }
663
664 @Override
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800665 public void resetAllModes(int reqUserId, String reqPackageName) {
666 final int callingPid = Binder.getCallingPid();
667 final int callingUid = Binder.getCallingUid();
Dianne Hackborn607b4142013-08-02 18:10:10 -0700668 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800669 callingPid, callingUid, null);
670 reqUserId = ActivityManager.handleIncomingUser(callingPid, callingUid, reqUserId,
671 true, true, "resetAllModes", null);
Svet Ganov2af57082015-07-30 08:44:20 -0700672
673 int reqUid = -1;
674 if (reqPackageName != null) {
675 try {
676 reqUid = AppGlobals.getPackageManager().getPackageUid(
677 reqPackageName, reqUserId);
678 } catch (RemoteException e) {
679 /* ignore - local call */
680 }
681 }
682
Dianne Hackborn607b4142013-08-02 18:10:10 -0700683 HashMap<Callback, ArrayList<Pair<String, Integer>>> callbacks = null;
684 synchronized (this) {
685 boolean changed = false;
Svet Ganov2af57082015-07-30 08:44:20 -0700686 for (int i = mUidStates.size() - 1; i >= 0; i--) {
687 UidState uidState = mUidStates.valueAt(i);
688
689 SparseIntArray opModes = uidState.opModes;
690 if (opModes != null && (uidState.uid == reqUid || reqUid == -1)) {
691 final int uidOpCount = opModes.size();
692 for (int j = uidOpCount - 1; j >= 0; j--) {
693 final int code = opModes.keyAt(j);
694 if (AppOpsManager.opAllowsReset(code)) {
695 opModes.removeAt(j);
696 if (opModes.size() <= 0) {
697 uidState.opModes = null;
698 }
699 for (String packageName : getPackagesForUid(uidState.uid)) {
700 callbacks = addCallbacks(callbacks, packageName, code,
701 mOpModeWatchers.get(code));
702 callbacks = addCallbacks(callbacks, packageName, code,
703 mPackageModeWatchers.get(packageName));
704 }
705 }
706 }
707 }
708
709 if (uidState.pkgOps == null) {
710 continue;
711 }
712
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800713 if (reqUserId != UserHandle.USER_ALL
Svet Ganov2af57082015-07-30 08:44:20 -0700714 && reqUserId != UserHandle.getUserId(uidState.uid)) {
Alexandra Gherghinad6a98972014-08-04 17:05:34 +0100715 // Skip any ops for a different user
716 continue;
717 }
Svet Ganov2af57082015-07-30 08:44:20 -0700718
719 Map<String, Ops> packages = uidState.pkgOps;
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700720 Iterator<Map.Entry<String, Ops>> it = packages.entrySet().iterator();
721 while (it.hasNext()) {
722 Map.Entry<String, Ops> ent = it.next();
Dianne Hackborn607b4142013-08-02 18:10:10 -0700723 String packageName = ent.getKey();
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800724 if (reqPackageName != null && !reqPackageName.equals(packageName)) {
725 // Skip any ops for a different package
726 continue;
727 }
Dianne Hackborn607b4142013-08-02 18:10:10 -0700728 Ops pkgOps = ent.getValue();
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700729 for (int j=pkgOps.size()-1; j>=0; j--) {
Dianne Hackborn607b4142013-08-02 18:10:10 -0700730 Op curOp = pkgOps.valueAt(j);
Dianne Hackborn8828d3a2013-09-25 16:47:10 -0700731 if (AppOpsManager.opAllowsReset(curOp.op)
732 && curOp.mode != AppOpsManager.opToDefaultMode(curOp.op)) {
David Braunf5d83192013-09-16 13:43:51 -0700733 curOp.mode = AppOpsManager.opToDefaultMode(curOp.op);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700734 changed = true;
735 callbacks = addCallbacks(callbacks, packageName, curOp.op,
736 mOpModeWatchers.get(curOp.op));
737 callbacks = addCallbacks(callbacks, packageName, curOp.op,
738 mPackageModeWatchers.get(packageName));
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700739 if (curOp.time == 0 && curOp.rejectTime == 0) {
740 pkgOps.removeAt(j);
741 }
Dianne Hackborn607b4142013-08-02 18:10:10 -0700742 }
743 }
Dianne Hackborn7f09ec32013-08-07 15:36:08 -0700744 if (pkgOps.size() == 0) {
745 it.remove();
746 }
747 }
Svet Ganov2af57082015-07-30 08:44:20 -0700748 if (uidState.isDefault()) {
749 mUidStates.remove(uidState.uid);
Dianne Hackborn607b4142013-08-02 18:10:10 -0700750 }
751 }
Svet Ganov2af57082015-07-30 08:44:20 -0700752
Dianne Hackborn607b4142013-08-02 18:10:10 -0700753 if (changed) {
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -0800754 scheduleFastWriteLocked();
Dianne Hackborn607b4142013-08-02 18:10:10 -0700755 }
756 }
757 if (callbacks != null) {
758 for (Map.Entry<Callback, ArrayList<Pair<String, Integer>>> ent : callbacks.entrySet()) {
759 Callback cb = ent.getKey();
760 ArrayList<Pair<String, Integer>> reports = ent.getValue();
761 for (int i=0; i<reports.size(); i++) {
762 Pair<String, Integer> rep = reports.get(i);
763 try {
764 cb.mCallback.opChanged(rep.second, rep.first);
765 } catch (RemoteException e) {
766 }
767 }
768 }
769 }
770 }
771
Dianne Hackbornc2293022013-02-06 23:14:49 -0800772 @Override
773 public void startWatchingMode(int op, String packageName, IAppOpsCallback callback) {
774 synchronized (this) {
Svet Ganov2af57082015-07-30 08:44:20 -0700775 op = (op != AppOpsManager.OP_NONE) ? AppOpsManager.opToSwitch(op) : op;
Dianne Hackbornc2293022013-02-06 23:14:49 -0800776 Callback cb = mModeWatchers.get(callback.asBinder());
777 if (cb == null) {
778 cb = new Callback(callback);
779 mModeWatchers.put(callback.asBinder(), cb);
780 }
781 if (op != AppOpsManager.OP_NONE) {
782 ArrayList<Callback> cbs = mOpModeWatchers.get(op);
783 if (cbs == null) {
784 cbs = new ArrayList<Callback>();
785 mOpModeWatchers.put(op, cbs);
786 }
787 cbs.add(cb);
788 }
789 if (packageName != null) {
790 ArrayList<Callback> cbs = mPackageModeWatchers.get(packageName);
791 if (cbs == null) {
792 cbs = new ArrayList<Callback>();
793 mPackageModeWatchers.put(packageName, cbs);
794 }
795 cbs.add(cb);
796 }
797 }
798 }
799
800 @Override
801 public void stopWatchingMode(IAppOpsCallback callback) {
802 synchronized (this) {
803 Callback cb = mModeWatchers.remove(callback.asBinder());
804 if (cb != null) {
805 cb.unlinkToDeath();
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700806 for (int i=mOpModeWatchers.size()-1; i>=0; i--) {
Dianne Hackbornc2293022013-02-06 23:14:49 -0800807 ArrayList<Callback> cbs = mOpModeWatchers.valueAt(i);
808 cbs.remove(cb);
809 if (cbs.size() <= 0) {
810 mOpModeWatchers.removeAt(i);
811 }
812 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700813 for (int i=mPackageModeWatchers.size()-1; i>=0; i--) {
814 ArrayList<Callback> cbs = mPackageModeWatchers.valueAt(i);
815 cbs.remove(cb);
816 if (cbs.size() <= 0) {
817 mPackageModeWatchers.removeAt(i);
Dianne Hackbornc2293022013-02-06 23:14:49 -0800818 }
819 }
820 }
821 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800822 }
823
824 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700825 public IBinder getToken(IBinder clientToken) {
826 synchronized (this) {
827 ClientState cs = mClients.get(clientToken);
828 if (cs == null) {
829 cs = new ClientState(clientToken);
830 mClients.put(clientToken, cs);
831 }
832 return cs;
833 }
834 }
835
836 @Override
Dianne Hackborn35654b62013-01-14 17:38:02 -0800837 public int checkOperation(int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800838 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800839 verifyIncomingOp(code);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800840 synchronized (this) {
Jason Monk1c7c3192014-06-26 12:52:18 -0400841 if (isOpRestricted(uid, code, packageName)) {
Jason Monk62062992014-05-06 09:55:28 -0400842 return AppOpsManager.MODE_IGNORED;
843 }
Svet Ganov2af57082015-07-30 08:44:20 -0700844 code = AppOpsManager.opToSwitch(code);
845 UidState uidState = getUidStateLocked(uid, false);
846 if (uidState != null && uidState.opModes != null) {
847 final int uidMode = uidState.opModes.get(code);
848 if (uidMode != AppOpsManager.MODE_ALLOWED) {
849 return uidMode;
850 }
851 }
852 Op op = getOpLocked(code, uid, packageName, false);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800853 if (op == null) {
David Braunf5d83192013-09-16 13:43:51 -0700854 return AppOpsManager.opToDefaultMode(code);
Dianne Hackborn35654b62013-01-14 17:38:02 -0800855 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800856 return op.mode;
Dianne Hackborn35654b62013-01-14 17:38:02 -0800857 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800858 }
859
860 @Override
John Spurlock7b414672014-07-18 13:02:39 -0400861 public int checkAudioOperation(int code, int usage, int uid, String packageName) {
John Spurlock1af30c72014-03-10 08:33:35 -0400862 synchronized (this) {
John Spurlock7b414672014-07-18 13:02:39 -0400863 final int mode = checkRestrictionLocked(code, usage, uid, packageName);
John Spurlock1af30c72014-03-10 08:33:35 -0400864 if (mode != AppOpsManager.MODE_ALLOWED) {
865 return mode;
866 }
867 }
868 return checkOperation(code, uid, packageName);
869 }
870
John Spurlock7b414672014-07-18 13:02:39 -0400871 private int checkRestrictionLocked(int code, int usage, int uid, String packageName) {
872 final SparseArray<Restriction> usageRestrictions = mAudioRestrictions.get(code);
873 if (usageRestrictions != null) {
874 final Restriction r = usageRestrictions.get(usage);
John Spurlock1af30c72014-03-10 08:33:35 -0400875 if (r != null && !r.exceptionPackages.contains(packageName)) {
876 return r.mode;
877 }
878 }
879 return AppOpsManager.MODE_ALLOWED;
880 }
881
882 @Override
John Spurlock7b414672014-07-18 13:02:39 -0400883 public void setAudioRestriction(int code, int usage, int uid, int mode,
John Spurlock1af30c72014-03-10 08:33:35 -0400884 String[] exceptionPackages) {
885 verifyIncomingUid(uid);
886 verifyIncomingOp(code);
887 synchronized (this) {
John Spurlock7b414672014-07-18 13:02:39 -0400888 SparseArray<Restriction> usageRestrictions = mAudioRestrictions.get(code);
889 if (usageRestrictions == null) {
890 usageRestrictions = new SparseArray<Restriction>();
891 mAudioRestrictions.put(code, usageRestrictions);
John Spurlock1af30c72014-03-10 08:33:35 -0400892 }
John Spurlock7b414672014-07-18 13:02:39 -0400893 usageRestrictions.remove(usage);
John Spurlock1af30c72014-03-10 08:33:35 -0400894 if (mode != AppOpsManager.MODE_ALLOWED) {
895 final Restriction r = new Restriction();
896 r.mode = mode;
897 if (exceptionPackages != null) {
898 final int N = exceptionPackages.length;
899 r.exceptionPackages = new ArraySet<String>(N);
900 for (int i = 0; i < N; i++) {
901 final String pkg = exceptionPackages[i];
902 if (pkg != null) {
903 r.exceptionPackages.add(pkg.trim());
904 }
905 }
906 }
John Spurlock7b414672014-07-18 13:02:39 -0400907 usageRestrictions.put(usage, r);
John Spurlock1af30c72014-03-10 08:33:35 -0400908 }
909 }
910 }
911
912 @Override
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700913 public int checkPackage(int uid, String packageName) {
914 synchronized (this) {
Dianne Hackborn0fcef842014-09-12 15:38:33 -0700915 if (getOpsRawLocked(uid, packageName, true) != null) {
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700916 return AppOpsManager.MODE_ALLOWED;
917 } else {
918 return AppOpsManager.MODE_ERRORED;
919 }
920 }
921 }
922
923 @Override
Svet Ganov99b60432015-06-27 13:15:22 -0700924 public int noteProxyOperation(int code, String proxyPackageName,
925 int proxiedUid, String proxiedPackageName) {
926 verifyIncomingOp(code);
927 final int proxyMode = noteOperationUnchecked(code, Binder.getCallingUid(),
928 proxyPackageName, -1, null);
929 if (proxyMode != AppOpsManager.MODE_ALLOWED || Binder.getCallingUid() == proxiedUid) {
930 return proxyMode;
931 }
932 return noteOperationUnchecked(code, proxiedUid, proxiedPackageName,
933 Binder.getCallingUid(), proxyPackageName);
934
935 }
936
937 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800938 public int noteOperation(int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800939 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800940 verifyIncomingOp(code);
Svet Ganov99b60432015-06-27 13:15:22 -0700941 return noteOperationUnchecked(code, uid, packageName, 0, null);
942 }
943
944 private int noteOperationUnchecked(int code, int uid, String packageName,
945 int proxyUid, String proxyPackageName) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800946 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800947 Ops ops = getOpsLocked(uid, packageName, true);
948 if (ops == null) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800949 if (DEBUG) Log.d(TAG, "noteOperation: no op for code " + code + " uid " + uid
950 + " package " + packageName);
Jeff Sharkey911d7f42013-09-05 18:11:45 -0700951 return AppOpsManager.MODE_ERRORED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800952 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800953 Op op = getOpLocked(ops, code, true);
Jason Monk1c7c3192014-06-26 12:52:18 -0400954 if (isOpRestricted(uid, code, packageName)) {
Jason Monk62062992014-05-06 09:55:28 -0400955 return AppOpsManager.MODE_IGNORED;
956 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800957 if (op.duration == -1) {
958 Slog.w(TAG, "Noting op not finished: uid " + uid + " pkg " + packageName
959 + " code " + code + " time=" + op.time + " duration=" + op.duration);
960 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800961 op.duration = 0;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800962 final int switchCode = AppOpsManager.opToSwitch(code);
Svet Ganov2af57082015-07-30 08:44:20 -0700963 UidState uidState = ops.uidState;
964 if (uidState.opModes != null) {
965 final int uidMode = uidState.opModes.get(switchCode);
966 if (uidMode != AppOpsManager.MODE_ALLOWED) {
967 if (DEBUG) Log.d(TAG, "noteOperation: reject #" + op.mode + " for code "
968 + switchCode + " (" + code + ") uid " + uid + " package "
969 + packageName);
970 op.rejectTime = System.currentTimeMillis();
971 return uidMode;
972 }
973 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800974 final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
975 if (switchOp.mode != AppOpsManager.MODE_ALLOWED) {
976 if (DEBUG) Log.d(TAG, "noteOperation: reject #" + op.mode + " for code "
977 + switchCode + " (" + code + ") uid " + uid + " package " + packageName);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800978 op.rejectTime = System.currentTimeMillis();
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800979 return switchOp.mode;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800980 }
981 if (DEBUG) Log.d(TAG, "noteOperation: allowing code " + code + " uid " + uid
982 + " package " + packageName);
983 op.time = System.currentTimeMillis();
Dianne Hackborn514074f2013-02-11 10:52:46 -0800984 op.rejectTime = 0;
Svet Ganov99b60432015-06-27 13:15:22 -0700985 op.proxyUid = proxyUid;
986 op.proxyPackageName = proxyPackageName;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800987 return AppOpsManager.MODE_ALLOWED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800988 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800989 }
990
991 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700992 public int startOperation(IBinder token, int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800993 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -0800994 verifyIncomingOp(code);
Dianne Hackborne98f5db2013-07-17 17:23:25 -0700995 ClientState client = (ClientState)token;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800996 synchronized (this) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800997 Ops ops = getOpsLocked(uid, packageName, true);
998 if (ops == null) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -0800999 if (DEBUG) Log.d(TAG, "startOperation: no op for code " + code + " uid " + uid
1000 + " package " + packageName);
Jeff Sharkey911d7f42013-09-05 18:11:45 -07001001 return AppOpsManager.MODE_ERRORED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001002 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001003 Op op = getOpLocked(ops, code, true);
Jason Monk1c7c3192014-06-26 12:52:18 -04001004 if (isOpRestricted(uid, code, packageName)) {
Jason Monk62062992014-05-06 09:55:28 -04001005 return AppOpsManager.MODE_IGNORED;
1006 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001007 final int switchCode = AppOpsManager.opToSwitch(code);
Svet Ganov2af57082015-07-30 08:44:20 -07001008 UidState uidState = ops.uidState;
1009 if (uidState.opModes != null) {
1010 final int uidMode = uidState.opModes.get(switchCode);
1011 if (uidMode != AppOpsManager.MODE_ALLOWED) {
1012 if (DEBUG) Log.d(TAG, "noteOperation: reject #" + op.mode + " for code "
1013 + switchCode + " (" + code + ") uid " + uid + " package "
1014 + packageName);
1015 op.rejectTime = System.currentTimeMillis();
1016 return uidMode;
1017 }
1018 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001019 final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op;
1020 if (switchOp.mode != AppOpsManager.MODE_ALLOWED) {
1021 if (DEBUG) Log.d(TAG, "startOperation: reject #" + op.mode + " for code "
1022 + switchCode + " (" + code + ") uid " + uid + " package " + packageName);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001023 op.rejectTime = System.currentTimeMillis();
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001024 return switchOp.mode;
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001025 }
1026 if (DEBUG) Log.d(TAG, "startOperation: allowing code " + code + " uid " + uid
1027 + " package " + packageName);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001028 if (op.nesting == 0) {
1029 op.time = System.currentTimeMillis();
Dianne Hackborn514074f2013-02-11 10:52:46 -08001030 op.rejectTime = 0;
Dianne Hackborn35654b62013-01-14 17:38:02 -08001031 op.duration = -1;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001032 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001033 op.nesting++;
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001034 if (client.mStartedOps != null) {
1035 client.mStartedOps.add(op);
1036 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001037 return AppOpsManager.MODE_ALLOWED;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001038 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001039 }
1040
1041 @Override
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001042 public void finishOperation(IBinder token, int code, int uid, String packageName) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001043 verifyIncomingUid(uid);
Dianne Hackborn961321f2013-02-05 17:22:41 -08001044 verifyIncomingOp(code);
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001045 ClientState client = (ClientState)token;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001046 synchronized (this) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001047 Op op = getOpLocked(code, uid, packageName, true);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001048 if (op == null) {
1049 return;
1050 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001051 if (client.mStartedOps != null) {
1052 if (!client.mStartedOps.remove(op)) {
1053 throw new IllegalStateException("Operation not started: uid" + op.uid
1054 + " pkg=" + op.packageName + " op=" + op.op);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001055 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001056 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001057 finishOperationLocked(op);
1058 }
1059 }
1060
Svet Ganovb9d71a62015-04-30 10:38:13 -07001061 @Override
1062 public int permissionToOpCode(String permission) {
1063 return AppOpsManager.permissionToOpCode(permission);
1064 }
1065
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001066 void finishOperationLocked(Op op) {
1067 if (op.nesting <= 1) {
1068 if (op.nesting == 1) {
1069 op.duration = (int)(System.currentTimeMillis() - op.time);
1070 op.time += op.duration;
1071 } else {
1072 Slog.w(TAG, "Finishing op nesting under-run: uid " + op.uid + " pkg "
1073 + op.packageName + " code " + op.op + " time=" + op.time
1074 + " duration=" + op.duration + " nesting=" + op.nesting);
1075 }
1076 op.nesting = 0;
1077 } else {
1078 op.nesting--;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001079 }
1080 }
1081
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001082 private void verifyIncomingUid(int uid) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001083 if (uid == Binder.getCallingUid()) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001084 return;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001085 }
1086 if (Binder.getCallingPid() == Process.myPid()) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001087 return;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001088 }
1089 mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS,
1090 Binder.getCallingPid(), Binder.getCallingUid(), null);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001091 }
1092
Dianne Hackborn961321f2013-02-05 17:22:41 -08001093 private void verifyIncomingOp(int op) {
1094 if (op >= 0 && op < AppOpsManager._NUM_OP) {
1095 return;
1096 }
1097 throw new IllegalArgumentException("Bad operation #" + op);
1098 }
1099
Svet Ganov2af57082015-07-30 08:44:20 -07001100 private UidState getUidStateLocked(int uid, boolean edit) {
1101 UidState uidState = mUidStates.get(uid);
1102 if (uidState == null) {
1103 if (!edit) {
1104 return null;
1105 }
1106 uidState = new UidState(uid);
1107 mUidStates.put(uid, uidState);
1108 }
1109 return uidState;
1110 }
1111
Dianne Hackborn72e39832013-01-18 18:36:09 -08001112 private Ops getOpsLocked(int uid, String packageName, boolean edit) {
Dianne Hackborn0fcef842014-09-12 15:38:33 -07001113 if (uid == 0) {
1114 packageName = "root";
1115 } else if (uid == Process.SHELL_UID) {
1116 packageName = "com.android.shell";
1117 }
1118 return getOpsRawLocked(uid, packageName, edit);
1119 }
1120
1121 private Ops getOpsRawLocked(int uid, String packageName, boolean edit) {
Svet Ganov2af57082015-07-30 08:44:20 -07001122 UidState uidState = getUidStateLocked(uid, edit);
1123 if (uidState == null) {
1124 return null;
1125 }
1126
1127 if (uidState.pkgOps == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001128 if (!edit) {
1129 return null;
1130 }
Svet Ganov2af57082015-07-30 08:44:20 -07001131 uidState.pkgOps = new ArrayMap<>();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001132 }
Svet Ganov2af57082015-07-30 08:44:20 -07001133
1134 Ops ops = uidState.pkgOps.get(packageName);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001135 if (ops == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001136 if (!edit) {
1137 return null;
1138 }
Jason Monk1c7c3192014-06-26 12:52:18 -04001139 boolean isPrivileged = false;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001140 // This is the first time we have seen this package name under this uid,
1141 // so let's make sure it is valid.
Dianne Hackborn514074f2013-02-11 10:52:46 -08001142 if (uid != 0) {
1143 final long ident = Binder.clearCallingIdentity();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001144 try {
Dianne Hackborn514074f2013-02-11 10:52:46 -08001145 int pkgUid = -1;
1146 try {
Jason Monk1c7c3192014-06-26 12:52:18 -04001147 ApplicationInfo appInfo = ActivityThread.getPackageManager()
1148 .getApplicationInfo(packageName, 0, UserHandle.getUserId(uid));
1149 if (appInfo != null) {
1150 pkgUid = appInfo.uid;
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001151 isPrivileged = (appInfo.privateFlags
1152 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0;
Jason Monk1c7c3192014-06-26 12:52:18 -04001153 } else {
1154 if ("media".equals(packageName)) {
1155 pkgUid = Process.MEDIA_UID;
1156 isPrivileged = false;
1157 }
Dianne Hackborn713df152013-05-17 11:27:57 -07001158 }
Jason Monk1c7c3192014-06-26 12:52:18 -04001159 } catch (RemoteException e) {
1160 Slog.w(TAG, "Could not contact PackageManager", e);
Dianne Hackborn514074f2013-02-11 10:52:46 -08001161 }
1162 if (pkgUid != uid) {
1163 // Oops! The package name is not valid for the uid they are calling
1164 // under. Abort.
1165 Slog.w(TAG, "Bad call: specified package " + packageName
1166 + " under uid " + uid + " but it is really " + pkgUid);
1167 return null;
1168 }
1169 } finally {
1170 Binder.restoreCallingIdentity(ident);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001171 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001172 }
Svet Ganov2af57082015-07-30 08:44:20 -07001173 ops = new Ops(packageName, uidState, isPrivileged);
1174 uidState.pkgOps.put(packageName, ops);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001175 }
Dianne Hackborn72e39832013-01-18 18:36:09 -08001176 return ops;
1177 }
1178
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001179 private void scheduleWriteLocked() {
1180 if (!mWriteScheduled) {
1181 mWriteScheduled = true;
1182 mHandler.postDelayed(mWriteRunner, WRITE_DELAY);
1183 }
1184 }
1185
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -08001186 private void scheduleFastWriteLocked() {
1187 if (!mFastWriteScheduled) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001188 mWriteScheduled = true;
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -08001189 mFastWriteScheduled = true;
1190 mHandler.removeCallbacks(mWriteRunner);
1191 mHandler.postDelayed(mWriteRunner, 10*1000);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001192 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001193 }
1194
Dianne Hackborn72e39832013-01-18 18:36:09 -08001195 private Op getOpLocked(int code, int uid, String packageName, boolean edit) {
1196 Ops ops = getOpsLocked(uid, packageName, edit);
1197 if (ops == null) {
1198 return null;
1199 }
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001200 return getOpLocked(ops, code, edit);
1201 }
1202
1203 private Op getOpLocked(Ops ops, int code, boolean edit) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001204 Op op = ops.get(code);
1205 if (op == null) {
Dianne Hackborn35654b62013-01-14 17:38:02 -08001206 if (!edit) {
1207 return null;
1208 }
Svet Ganov2af57082015-07-30 08:44:20 -07001209 op = new Op(ops.uidState.uid, ops.packageName, code);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001210 ops.put(code, op);
1211 }
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001212 if (edit) {
1213 scheduleWriteLocked();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001214 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001215 return op;
1216 }
1217
Jason Monk1c7c3192014-06-26 12:52:18 -04001218 private boolean isOpRestricted(int uid, int code, String packageName) {
Jason Monk62062992014-05-06 09:55:28 -04001219 int userHandle = UserHandle.getUserId(uid);
1220 boolean[] opRestrictions = mOpRestrictions.get(userHandle);
1221 if ((opRestrictions != null) && opRestrictions[code]) {
Jason Monk1c7c3192014-06-26 12:52:18 -04001222 if (AppOpsManager.opAllowSystemBypassRestriction(code)) {
1223 synchronized (this) {
1224 Ops ops = getOpsLocked(uid, packageName, true);
1225 if ((ops != null) && ops.isPrivileged) {
1226 return false;
1227 }
1228 }
1229 }
Julia Reynolds401de172014-07-24 18:21:29 -04001230 return true;
Jason Monk62062992014-05-06 09:55:28 -04001231 }
1232 return false;
1233 }
1234
Dianne Hackborn35654b62013-01-14 17:38:02 -08001235 void readState() {
1236 synchronized (mFile) {
1237 synchronized (this) {
1238 FileInputStream stream;
1239 try {
1240 stream = mFile.openRead();
1241 } catch (FileNotFoundException e) {
1242 Slog.i(TAG, "No existing app ops " + mFile.getBaseFile() + "; starting empty");
1243 return;
1244 }
1245 boolean success = false;
1246 try {
1247 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001248 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackborn35654b62013-01-14 17:38:02 -08001249 int type;
1250 while ((type = parser.next()) != XmlPullParser.START_TAG
1251 && type != XmlPullParser.END_DOCUMENT) {
1252 ;
1253 }
1254
1255 if (type != XmlPullParser.START_TAG) {
1256 throw new IllegalStateException("no start tag found");
1257 }
1258
1259 int outerDepth = parser.getDepth();
1260 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1261 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1262 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1263 continue;
1264 }
1265
1266 String tagName = parser.getName();
1267 if (tagName.equals("pkg")) {
Dave Burke0997c5bd2013-08-02 20:25:02 +00001268 readPackage(parser);
Svet Ganov2af57082015-07-30 08:44:20 -07001269 } if (tagName.equals("uid")) {
1270 readUidOps(parser);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001271 } else {
1272 Slog.w(TAG, "Unknown element under <app-ops>: "
1273 + parser.getName());
1274 XmlUtils.skipCurrentTag(parser);
1275 }
1276 }
1277 success = true;
1278 } catch (IllegalStateException e) {
1279 Slog.w(TAG, "Failed parsing " + e);
1280 } catch (NullPointerException e) {
1281 Slog.w(TAG, "Failed parsing " + e);
1282 } catch (NumberFormatException e) {
1283 Slog.w(TAG, "Failed parsing " + e);
1284 } catch (XmlPullParserException e) {
1285 Slog.w(TAG, "Failed parsing " + e);
1286 } catch (IOException e) {
1287 Slog.w(TAG, "Failed parsing " + e);
1288 } catch (IndexOutOfBoundsException e) {
1289 Slog.w(TAG, "Failed parsing " + e);
1290 } finally {
1291 if (!success) {
Svet Ganov2af57082015-07-30 08:44:20 -07001292 mUidStates.clear();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001293 }
1294 try {
1295 stream.close();
1296 } catch (IOException e) {
1297 }
1298 }
1299 }
1300 }
1301 }
1302
Svet Ganov2af57082015-07-30 08:44:20 -07001303 void readUidOps(XmlPullParser parser) throws NumberFormatException,
1304 XmlPullParserException, IOException {
1305 final int uid = Integer.parseInt(parser.getAttributeValue(null, "n"));
1306 int outerDepth = parser.getDepth();
1307 int type;
1308 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1309 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1310 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1311 continue;
1312 }
1313
1314 String tagName = parser.getName();
1315 if (tagName.equals("op")) {
1316 final int code = Integer.parseInt(parser.getAttributeValue(null, "n"));
1317 final int mode = Integer.parseInt(parser.getAttributeValue(null, "m"));
1318 UidState uidState = getUidStateLocked(uid, true);
1319 if (uidState.opModes == null) {
1320 uidState.opModes = new SparseIntArray();
1321 }
1322 uidState.opModes.put(code, mode);
1323 } else {
1324 Slog.w(TAG, "Unknown element under <uid-ops>: "
1325 + parser.getName());
1326 XmlUtils.skipCurrentTag(parser);
1327 }
1328 }
1329 }
1330
Dave Burke0997c5bd2013-08-02 20:25:02 +00001331 void readPackage(XmlPullParser parser) throws NumberFormatException,
Dianne Hackborn35654b62013-01-14 17:38:02 -08001332 XmlPullParserException, IOException {
1333 String pkgName = parser.getAttributeValue(null, "n");
1334 int outerDepth = parser.getDepth();
1335 int type;
1336 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1337 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1338 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1339 continue;
1340 }
1341
1342 String tagName = parser.getName();
1343 if (tagName.equals("uid")) {
Dave Burke0997c5bd2013-08-02 20:25:02 +00001344 readUid(parser, pkgName);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001345 } else {
1346 Slog.w(TAG, "Unknown element under <pkg>: "
1347 + parser.getName());
1348 XmlUtils.skipCurrentTag(parser);
1349 }
1350 }
1351 }
1352
Dave Burke0997c5bd2013-08-02 20:25:02 +00001353 void readUid(XmlPullParser parser, String pkgName) throws NumberFormatException,
Dianne Hackborn35654b62013-01-14 17:38:02 -08001354 XmlPullParserException, IOException {
1355 int uid = Integer.parseInt(parser.getAttributeValue(null, "n"));
Jason Monk1c7c3192014-06-26 12:52:18 -04001356 String isPrivilegedString = parser.getAttributeValue(null, "p");
1357 boolean isPrivileged = false;
1358 if (isPrivilegedString == null) {
1359 try {
1360 IPackageManager packageManager = ActivityThread.getPackageManager();
1361 if (packageManager != null) {
1362 ApplicationInfo appInfo = ActivityThread.getPackageManager()
1363 .getApplicationInfo(pkgName, 0, UserHandle.getUserId(uid));
1364 if (appInfo != null) {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001365 isPrivileged = (appInfo.privateFlags
1366 & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0;
Jason Monk1c7c3192014-06-26 12:52:18 -04001367 }
1368 } else {
1369 // Could not load data, don't add to cache so it will be loaded later.
1370 return;
1371 }
1372 } catch (RemoteException e) {
1373 Slog.w(TAG, "Could not contact PackageManager", e);
1374 }
1375 } else {
1376 isPrivileged = Boolean.parseBoolean(isPrivilegedString);
1377 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001378 int outerDepth = parser.getDepth();
1379 int type;
1380 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1381 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1382 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1383 continue;
1384 }
1385
1386 String tagName = parser.getName();
1387 if (tagName.equals("op")) {
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001388 Op op = new Op(uid, pkgName, Integer.parseInt(parser.getAttributeValue(null, "n")));
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001389 String mode = parser.getAttributeValue(null, "m");
1390 if (mode != null) {
Dave Burke0997c5bd2013-08-02 20:25:02 +00001391 op.mode = Integer.parseInt(mode);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001392 }
1393 String time = parser.getAttributeValue(null, "t");
1394 if (time != null) {
1395 op.time = Long.parseLong(time);
1396 }
1397 time = parser.getAttributeValue(null, "r");
1398 if (time != null) {
1399 op.rejectTime = Long.parseLong(time);
1400 }
1401 String dur = parser.getAttributeValue(null, "d");
1402 if (dur != null) {
1403 op.duration = Integer.parseInt(dur);
1404 }
Svet Ganov99b60432015-06-27 13:15:22 -07001405 String proxyUid = parser.getAttributeValue(null, "pu");
1406 if (proxyUid != null) {
1407 op.proxyUid = Integer.parseInt(proxyUid);
1408 }
1409 String proxyPackageName = parser.getAttributeValue(null, "pp");
1410 if (proxyPackageName != null) {
1411 op.proxyPackageName = proxyPackageName;
1412 }
Svet Ganov2af57082015-07-30 08:44:20 -07001413
1414 UidState uidState = getUidStateLocked(uid, true);
1415 if (uidState.pkgOps == null) {
1416 uidState.pkgOps = new ArrayMap<>();
Dianne Hackborn35654b62013-01-14 17:38:02 -08001417 }
Svet Ganov2af57082015-07-30 08:44:20 -07001418
1419 Ops ops = uidState.pkgOps.get(pkgName);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001420 if (ops == null) {
Svet Ganov2af57082015-07-30 08:44:20 -07001421 ops = new Ops(pkgName, uidState, isPrivileged);
1422 uidState.pkgOps.put(pkgName, ops);
Dianne Hackborn35654b62013-01-14 17:38:02 -08001423 }
1424 ops.put(op.op, op);
1425 } else {
1426 Slog.w(TAG, "Unknown element under <pkg>: "
1427 + parser.getName());
1428 XmlUtils.skipCurrentTag(parser);
1429 }
1430 }
1431 }
1432
1433 void writeState() {
1434 synchronized (mFile) {
1435 List<AppOpsManager.PackageOps> allOps = getPackagesForOps(null);
1436
1437 FileOutputStream stream;
1438 try {
1439 stream = mFile.startWrite();
1440 } catch (IOException e) {
1441 Slog.w(TAG, "Failed to write state: " + e);
1442 return;
1443 }
1444
1445 try {
1446 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001447 out.setOutput(stream, StandardCharsets.UTF_8.name());
Dianne Hackborn35654b62013-01-14 17:38:02 -08001448 out.startDocument(null, true);
Svet Ganov2af57082015-07-30 08:44:20 -07001449 out.startTag(null, "app");
1450
1451 final int uidStateCount = mUidStates.size();
1452 for (int i = 0; i < uidStateCount; i++) {
1453 UidState uidState = mUidStates.valueAt(i);
1454 if (uidState.opModes != null && uidState.opModes.size() > 0) {
1455 out.startTag(null, "uid");
1456 out.attribute(null, "n", Integer.toString(uidState.uid));
1457 SparseIntArray uidOpModes = uidState.opModes;
1458 final int opCount = uidOpModes.size();
1459 for (int j = 0; j < opCount; j++) {
1460 final int op = uidOpModes.keyAt(j);
1461 final int mode = uidOpModes.valueAt(j);
1462 out.startTag(null, "op");
1463 out.attribute(null, "n", Integer.toString(op));
1464 out.attribute(null, "m", Integer.toString(mode));
1465 out.endTag(null, "op");
1466 }
1467 out.endTag(null, "uid");
1468 }
1469 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001470
1471 if (allOps != null) {
1472 String lastPkg = null;
1473 for (int i=0; i<allOps.size(); i++) {
1474 AppOpsManager.PackageOps pkg = allOps.get(i);
1475 if (!pkg.getPackageName().equals(lastPkg)) {
1476 if (lastPkg != null) {
1477 out.endTag(null, "pkg");
1478 }
1479 lastPkg = pkg.getPackageName();
1480 out.startTag(null, "pkg");
1481 out.attribute(null, "n", lastPkg);
1482 }
1483 out.startTag(null, "uid");
1484 out.attribute(null, "n", Integer.toString(pkg.getUid()));
Jason Monk1c7c3192014-06-26 12:52:18 -04001485 synchronized (this) {
1486 Ops ops = getOpsLocked(pkg.getUid(), pkg.getPackageName(), false);
1487 // Should always be present as the list of PackageOps is generated
1488 // from Ops.
1489 if (ops != null) {
1490 out.attribute(null, "p", Boolean.toString(ops.isPrivileged));
1491 } else {
1492 out.attribute(null, "p", Boolean.toString(false));
1493 }
1494 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001495 List<AppOpsManager.OpEntry> ops = pkg.getOps();
1496 for (int j=0; j<ops.size(); j++) {
1497 AppOpsManager.OpEntry op = ops.get(j);
1498 out.startTag(null, "op");
1499 out.attribute(null, "n", Integer.toString(op.getOp()));
David Braunf5d83192013-09-16 13:43:51 -07001500 if (op.getMode() != AppOpsManager.opToDefaultMode(op.getOp())) {
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001501 out.attribute(null, "m", Integer.toString(op.getMode()));
1502 }
1503 long time = op.getTime();
1504 if (time != 0) {
1505 out.attribute(null, "t", Long.toString(time));
1506 }
1507 time = op.getRejectTime();
1508 if (time != 0) {
1509 out.attribute(null, "r", Long.toString(time));
1510 }
1511 int dur = op.getDuration();
1512 if (dur != 0) {
1513 out.attribute(null, "d", Integer.toString(dur));
1514 }
Svet Ganov99b60432015-06-27 13:15:22 -07001515 int proxyUid = op.getProxyUid();
1516 if (proxyUid != -1) {
1517 out.attribute(null, "pu", Integer.toString(proxyUid));
1518 }
1519 String proxyPackageName = op.getProxyPackageName();
1520 if (proxyPackageName != null) {
1521 out.attribute(null, "pp", proxyPackageName);
1522 }
Dianne Hackborn35654b62013-01-14 17:38:02 -08001523 out.endTag(null, "op");
1524 }
1525 out.endTag(null, "uid");
1526 }
1527 if (lastPkg != null) {
1528 out.endTag(null, "pkg");
1529 }
1530 }
1531
1532 out.endTag(null, "app-ops");
1533 out.endDocument();
1534 mFile.finishWrite(stream);
1535 } catch (IOException e) {
1536 Slog.w(TAG, "Failed to write state, restoring backup.", e);
1537 mFile.failWrite(stream);
1538 }
1539 }
1540 }
1541
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001542 @Override
1543 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1544 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1545 != PackageManager.PERMISSION_GRANTED) {
1546 pw.println("Permission Denial: can't dump ApOps service from from pid="
1547 + Binder.getCallingPid()
1548 + ", uid=" + Binder.getCallingUid());
1549 return;
1550 }
1551
1552 synchronized (this) {
1553 pw.println("Current AppOps Service state:");
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001554 final long now = System.currentTimeMillis();
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001555 boolean needSep = false;
1556 if (mOpModeWatchers.size() > 0) {
1557 needSep = true;
1558 pw.println(" Op mode watchers:");
1559 for (int i=0; i<mOpModeWatchers.size(); i++) {
1560 pw.print(" Op "); pw.print(AppOpsManager.opToName(mOpModeWatchers.keyAt(i)));
1561 pw.println(":");
1562 ArrayList<Callback> callbacks = mOpModeWatchers.valueAt(i);
1563 for (int j=0; j<callbacks.size(); j++) {
1564 pw.print(" #"); pw.print(j); pw.print(": ");
1565 pw.println(callbacks.get(j));
1566 }
1567 }
1568 }
1569 if (mPackageModeWatchers.size() > 0) {
1570 needSep = true;
1571 pw.println(" Package mode watchers:");
1572 for (int i=0; i<mPackageModeWatchers.size(); i++) {
1573 pw.print(" Pkg "); pw.print(mPackageModeWatchers.keyAt(i));
1574 pw.println(":");
1575 ArrayList<Callback> callbacks = mPackageModeWatchers.valueAt(i);
1576 for (int j=0; j<callbacks.size(); j++) {
1577 pw.print(" #"); pw.print(j); pw.print(": ");
1578 pw.println(callbacks.get(j));
1579 }
1580 }
1581 }
1582 if (mModeWatchers.size() > 0) {
1583 needSep = true;
1584 pw.println(" All mode watchers:");
1585 for (int i=0; i<mModeWatchers.size(); i++) {
1586 pw.print(" "); pw.print(mModeWatchers.keyAt(i));
1587 pw.print(" -> "); pw.println(mModeWatchers.valueAt(i));
1588 }
1589 }
1590 if (mClients.size() > 0) {
1591 needSep = true;
1592 pw.println(" Clients:");
1593 for (int i=0; i<mClients.size(); i++) {
1594 pw.print(" "); pw.print(mClients.keyAt(i)); pw.println(":");
1595 ClientState cs = mClients.valueAt(i);
1596 pw.print(" "); pw.println(cs);
1597 if (cs.mStartedOps != null && cs.mStartedOps.size() > 0) {
1598 pw.println(" Started ops:");
1599 for (int j=0; j<cs.mStartedOps.size(); j++) {
1600 Op op = cs.mStartedOps.get(j);
1601 pw.print(" "); pw.print("uid="); pw.print(op.uid);
1602 pw.print(" pkg="); pw.print(op.packageName);
1603 pw.print(" op="); pw.println(AppOpsManager.opToName(op.op));
1604 }
1605 }
1606 }
1607 }
John Spurlock1af30c72014-03-10 08:33:35 -04001608 if (mAudioRestrictions.size() > 0) {
1609 boolean printedHeader = false;
1610 for (int o=0; o<mAudioRestrictions.size(); o++) {
1611 final String op = AppOpsManager.opToName(mAudioRestrictions.keyAt(o));
1612 final SparseArray<Restriction> restrictions = mAudioRestrictions.valueAt(o);
1613 for (int i=0; i<restrictions.size(); i++) {
1614 if (!printedHeader){
1615 pw.println(" Audio Restrictions:");
1616 printedHeader = true;
1617 needSep = true;
1618 }
John Spurlock7b414672014-07-18 13:02:39 -04001619 final int usage = restrictions.keyAt(i);
John Spurlock1af30c72014-03-10 08:33:35 -04001620 pw.print(" "); pw.print(op);
John Spurlock7b414672014-07-18 13:02:39 -04001621 pw.print(" usage="); pw.print(AudioAttributes.usageToString(usage));
John Spurlock1af30c72014-03-10 08:33:35 -04001622 Restriction r = restrictions.valueAt(i);
1623 pw.print(": mode="); pw.println(r.mode);
1624 if (!r.exceptionPackages.isEmpty()) {
1625 pw.println(" Exceptions:");
1626 for (int j=0; j<r.exceptionPackages.size(); j++) {
1627 pw.print(" "); pw.println(r.exceptionPackages.valueAt(j));
1628 }
1629 }
1630 }
1631 }
1632 }
Dianne Hackborne98f5db2013-07-17 17:23:25 -07001633 if (needSep) {
1634 pw.println();
1635 }
Svet Ganov2af57082015-07-30 08:44:20 -07001636 for (int i=0; i<mUidStates.size(); i++) {
1637 UidState uidState = mUidStates.valueAt(i);
1638
1639 pw.print(" Uid "); UserHandle.formatUid(pw, uidState.uid); pw.println(":");
1640
1641 SparseIntArray opModes = uidState.opModes;
1642 if (opModes != null) {
1643 final int opModeCount = opModes.size();
1644 for (int j = 0; j < opModeCount; j++) {
1645 final int code = opModes.keyAt(j);
1646 final int mode = opModes.valueAt(j);
1647 pw.print(" "); pw.print(AppOpsManager.opToName(code));
1648 pw.print(": mode="); pw.println(mode);
1649 }
1650 }
1651
1652 ArrayMap<String, Ops> pkgOps = uidState.pkgOps;
1653 if (pkgOps == null) {
1654 continue;
1655 }
1656
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001657 for (Ops ops : pkgOps.values()) {
1658 pw.print(" Package "); pw.print(ops.packageName); pw.println(":");
1659 for (int j=0; j<ops.size(); j++) {
1660 Op op = ops.valueAt(j);
Dianne Hackborn5e45ee62013-01-24 19:13:44 -08001661 pw.print(" "); pw.print(AppOpsManager.opToName(op.op));
1662 pw.print(": mode="); pw.print(op.mode);
1663 if (op.time != 0) {
1664 pw.print("; time="); TimeUtils.formatDuration(now-op.time, pw);
1665 pw.print(" ago");
1666 }
1667 if (op.rejectTime != 0) {
1668 pw.print("; rejectTime="); TimeUtils.formatDuration(now-op.rejectTime, pw);
1669 pw.print(" ago");
1670 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001671 if (op.duration == -1) {
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -08001672 pw.print(" (running)");
1673 } else if (op.duration != 0) {
1674 pw.print("; duration="); TimeUtils.formatDuration(op.duration, pw);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001675 }
Dianne Hackborn7b7c58b2014-12-02 18:32:20 -08001676 pw.println();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001677 }
1678 }
1679 }
1680 }
1681 }
John Spurlock1af30c72014-03-10 08:33:35 -04001682
1683 private static final class Restriction {
1684 private static final ArraySet<String> NO_EXCEPTIONS = new ArraySet<String>();
1685 int mode;
1686 ArraySet<String> exceptionPackages = NO_EXCEPTIONS;
1687 }
Jason Monk62062992014-05-06 09:55:28 -04001688
1689 @Override
Jason Monk62062992014-05-06 09:55:28 -04001690 public void setUserRestrictions(Bundle restrictions, int userHandle) throws RemoteException {
1691 checkSystemUid("setUserRestrictions");
1692 boolean[] opRestrictions = mOpRestrictions.get(userHandle);
1693 if (opRestrictions == null) {
1694 opRestrictions = new boolean[AppOpsManager._NUM_OP];
1695 mOpRestrictions.put(userHandle, opRestrictions);
1696 }
1697 for (int i = 0; i < opRestrictions.length; ++i) {
1698 String restriction = AppOpsManager.opToRestriction(i);
1699 if (restriction != null) {
1700 opRestrictions[i] = restrictions.getBoolean(restriction, false);
1701 } else {
1702 opRestrictions[i] = false;
1703 }
1704 }
1705 }
1706
1707 @Override
1708 public void removeUser(int userHandle) throws RemoteException {
1709 checkSystemUid("removeUser");
1710 mOpRestrictions.remove(userHandle);
Jason Monk62062992014-05-06 09:55:28 -04001711 }
1712
1713 private void checkSystemUid(String function) {
1714 int uid = Binder.getCallingUid();
1715 if (uid != Process.SYSTEM_UID) {
1716 throw new SecurityException(function + " must by called by the system");
1717 }
1718 }
1719
Svet Ganov2af57082015-07-30 08:44:20 -07001720 private static String[] getPackagesForUid(int uid) {
1721 try {
1722 return AppGlobals.getPackageManager().getPackagesForUid(uid);
1723 } catch (RemoteException e) {
1724 /* ignore - local call */
1725 }
1726 return EmptyArray.STRING;
1727 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001728}