Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import java.io.File; |
| 20 | import java.io.FileDescriptor; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 21 | import java.io.FileInputStream; |
| 22 | import java.io.FileNotFoundException; |
| 23 | import java.io.FileOutputStream; |
| 24 | import java.io.IOException; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 25 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 26 | import java.nio.charset.StandardCharsets; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 27 | import java.util.ArrayList; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 28 | import java.util.HashMap; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 29 | import java.util.Iterator; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 30 | import java.util.List; |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 31 | import java.util.Map; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 32 | |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 33 | import android.app.ActivityManager; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 34 | import android.app.ActivityThread; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 35 | import android.app.AppOpsManager; |
| 36 | import android.content.Context; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 37 | import android.content.pm.ApplicationInfo; |
| 38 | import android.content.pm.IPackageManager; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 39 | import android.content.pm.PackageManager; |
| 40 | import android.content.pm.PackageManager.NameNotFoundException; |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 41 | import android.media.AudioAttributes; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 42 | import android.os.AsyncTask; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 43 | import android.os.Binder; |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 44 | import android.os.Bundle; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 45 | import android.os.Handler; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 46 | import android.os.IBinder; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 47 | import android.os.Process; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 48 | import android.os.RemoteException; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 49 | import android.os.ServiceManager; |
| 50 | import android.os.UserHandle; |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 51 | import android.os.storage.MountServiceInternal; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 52 | import android.util.ArrayMap; |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 53 | import android.util.ArraySet; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 54 | import android.util.AtomicFile; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 55 | import android.util.Log; |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 56 | import android.util.Pair; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 57 | import android.util.Slog; |
| 58 | import android.util.SparseArray; |
| 59 | import android.util.TimeUtils; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 60 | import android.util.Xml; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 61 | |
| 62 | import com.android.internal.app.IAppOpsService; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 63 | import com.android.internal.app.IAppOpsCallback; |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 64 | import com.android.internal.os.Zygote; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 65 | import com.android.internal.util.FastXmlSerializer; |
| 66 | import com.android.internal.util.XmlUtils; |
| 67 | |
| 68 | import org.xmlpull.v1.XmlPullParser; |
| 69 | import org.xmlpull.v1.XmlPullParserException; |
| 70 | import org.xmlpull.v1.XmlSerializer; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 71 | |
| 72 | public class AppOpsService extends IAppOpsService.Stub { |
| 73 | static final String TAG = "AppOps"; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 74 | static final boolean DEBUG = false; |
| 75 | |
| 76 | // Write at most every 30 minutes. |
| 77 | static final long WRITE_DELAY = DEBUG ? 1000 : 30*60*1000; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 78 | |
| 79 | Context mContext; |
| 80 | final AtomicFile mFile; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 81 | final Handler mHandler; |
| 82 | |
| 83 | boolean mWriteScheduled; |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 84 | boolean mFastWriteScheduled; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 85 | final Runnable mWriteRunner = new Runnable() { |
| 86 | public void run() { |
| 87 | synchronized (AppOpsService.this) { |
| 88 | mWriteScheduled = false; |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 89 | mFastWriteScheduled = false; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 90 | AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() { |
| 91 | @Override protected Void doInBackground(Void... params) { |
| 92 | writeState(); |
| 93 | return null; |
| 94 | } |
| 95 | }; |
| 96 | task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null); |
| 97 | } |
| 98 | } |
| 99 | }; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 100 | |
| 101 | final SparseArray<HashMap<String, Ops>> mUidOps |
| 102 | = new SparseArray<HashMap<String, Ops>>(); |
| 103 | |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 104 | private final SparseArray<boolean[]> mOpRestrictions = new SparseArray<boolean[]>(); |
| 105 | |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 106 | public final static class Ops extends SparseArray<Op> { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 107 | public final String packageName; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 108 | public final int uid; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 109 | public final boolean isPrivileged; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 110 | |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 111 | public Ops(String _packageName, int _uid, boolean _isPrivileged) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 112 | packageName = _packageName; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 113 | uid = _uid; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 114 | isPrivileged = _isPrivileged; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 118 | public final static class Op { |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 119 | public final int uid; |
| 120 | public final String packageName; |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 121 | public int proxyUid = -1; |
| 122 | public String proxyPackageName; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 123 | public final int op; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 124 | public int mode; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 125 | public int duration; |
| 126 | public long time; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 127 | public long rejectTime; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 128 | public int nesting; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 129 | |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 130 | public Op(int _uid, String _packageName, int _op) { |
| 131 | uid = _uid; |
| 132 | packageName = _packageName; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 133 | op = _op; |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 134 | mode = AppOpsManager.opToDefaultMode(op); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 138 | final SparseArray<ArrayList<Callback>> mOpModeWatchers |
| 139 | = new SparseArray<ArrayList<Callback>>(); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 140 | final ArrayMap<String, ArrayList<Callback>> mPackageModeWatchers |
| 141 | = new ArrayMap<String, ArrayList<Callback>>(); |
| 142 | final ArrayMap<IBinder, Callback> mModeWatchers |
| 143 | = new ArrayMap<IBinder, Callback>(); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 144 | final SparseArray<SparseArray<Restriction>> mAudioRestrictions |
| 145 | = new SparseArray<SparseArray<Restriction>>(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 146 | |
| 147 | public final class Callback implements DeathRecipient { |
| 148 | final IAppOpsCallback mCallback; |
| 149 | |
| 150 | public Callback(IAppOpsCallback callback) { |
| 151 | mCallback = callback; |
| 152 | try { |
| 153 | mCallback.asBinder().linkToDeath(this, 0); |
| 154 | } catch (RemoteException e) { |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | public void unlinkToDeath() { |
| 159 | mCallback.asBinder().unlinkToDeath(this, 0); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void binderDied() { |
| 164 | stopWatchingMode(mCallback); |
| 165 | } |
| 166 | } |
| 167 | |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 168 | final ArrayMap<IBinder, ClientState> mClients = new ArrayMap<IBinder, ClientState>(); |
| 169 | |
| 170 | public final class ClientState extends Binder implements DeathRecipient { |
| 171 | final IBinder mAppToken; |
| 172 | final int mPid; |
| 173 | final ArrayList<Op> mStartedOps; |
| 174 | |
| 175 | public ClientState(IBinder appToken) { |
| 176 | mAppToken = appToken; |
| 177 | mPid = Binder.getCallingPid(); |
| 178 | if (appToken instanceof Binder) { |
| 179 | // For local clients, there is no reason to track them. |
| 180 | mStartedOps = null; |
| 181 | } else { |
| 182 | mStartedOps = new ArrayList<Op>(); |
| 183 | try { |
| 184 | mAppToken.linkToDeath(this, 0); |
| 185 | } catch (RemoteException e) { |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public String toString() { |
| 192 | return "ClientState{" + |
| 193 | "mAppToken=" + mAppToken + |
| 194 | ", " + (mStartedOps != null ? ("pid=" + mPid) : "local") + |
| 195 | '}'; |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public void binderDied() { |
| 200 | synchronized (AppOpsService.this) { |
| 201 | for (int i=mStartedOps.size()-1; i>=0; i--) { |
| 202 | finishOperationLocked(mStartedOps.get(i)); |
| 203 | } |
| 204 | mClients.remove(mAppToken); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 209 | public AppOpsService(File storagePath, Handler handler) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 210 | mFile = new AtomicFile(storagePath); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 211 | mHandler = handler; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 212 | readState(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 213 | } |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 214 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 215 | public void publish(Context context) { |
| 216 | mContext = context; |
| 217 | ServiceManager.addService(Context.APP_OPS_SERVICE, asBinder()); |
| 218 | } |
| 219 | |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 220 | public void systemReady() { |
| 221 | synchronized (this) { |
| 222 | boolean changed = false; |
| 223 | for (int i=0; i<mUidOps.size(); i++) { |
| 224 | HashMap<String, Ops> pkgs = mUidOps.valueAt(i); |
| 225 | Iterator<Ops> it = pkgs.values().iterator(); |
| 226 | while (it.hasNext()) { |
| 227 | Ops ops = it.next(); |
| 228 | int curUid; |
| 229 | try { |
| 230 | curUid = mContext.getPackageManager().getPackageUid(ops.packageName, |
| 231 | UserHandle.getUserId(ops.uid)); |
| 232 | } catch (NameNotFoundException e) { |
| 233 | curUid = -1; |
| 234 | } |
| 235 | if (curUid != ops.uid) { |
| 236 | Slog.i(TAG, "Pruning old package " + ops.packageName |
| 237 | + "/" + ops.uid + ": new uid=" + curUid); |
| 238 | it.remove(); |
| 239 | changed = true; |
| 240 | } |
| 241 | } |
| 242 | if (pkgs.size() <= 0) { |
| 243 | mUidOps.removeAt(i); |
| 244 | } |
| 245 | } |
| 246 | if (changed) { |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 247 | scheduleFastWriteLocked(); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 250 | |
| 251 | MountServiceInternal mountServiceInternal = LocalServices.getService( |
| 252 | MountServiceInternal.class); |
| 253 | mountServiceInternal.addExternalStoragePolicy( |
| 254 | new MountServiceInternal.ExternalStorageMountPolicy() { |
| 255 | @Override |
| 256 | public int getMountMode(int uid, String packageName) { |
| 257 | if (Process.isIsolated(uid)) { |
| 258 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 259 | } |
| 260 | if (noteOperation(AppOpsManager.OP_READ_EXTERNAL_STORAGE, uid, |
| 261 | packageName) != AppOpsManager.MODE_ALLOWED) { |
| 262 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 263 | } |
| 264 | if (noteOperation(AppOpsManager.OP_WRITE_EXTERNAL_STORAGE, uid, |
| 265 | packageName) != AppOpsManager.MODE_ALLOWED) { |
| 266 | return Zygote.MOUNT_EXTERNAL_READ; |
| 267 | } |
| 268 | return Zygote.MOUNT_EXTERNAL_WRITE; |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public boolean hasExternalStorage(int uid, String packageName) { |
| 273 | final int mountMode = getMountMode(uid, packageName); |
| 274 | return mountMode == Zygote.MOUNT_EXTERNAL_READ |
| 275 | || mountMode == Zygote.MOUNT_EXTERNAL_WRITE; |
| 276 | } |
| 277 | }); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | public void packageRemoved(int uid, String packageName) { |
| 281 | synchronized (this) { |
| 282 | HashMap<String, Ops> pkgs = mUidOps.get(uid); |
| 283 | if (pkgs != null) { |
| 284 | if (pkgs.remove(packageName) != null) { |
| 285 | if (pkgs.size() <= 0) { |
| 286 | mUidOps.remove(uid); |
| 287 | } |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 288 | scheduleFastWriteLocked(); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | public void uidRemoved(int uid) { |
| 295 | synchronized (this) { |
| 296 | if (mUidOps.indexOfKey(uid) >= 0) { |
| 297 | mUidOps.remove(uid); |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 298 | scheduleFastWriteLocked(); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 303 | public void shutdown() { |
| 304 | Slog.w(TAG, "Writing app ops before shutdown..."); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 305 | boolean doWrite = false; |
| 306 | synchronized (this) { |
| 307 | if (mWriteScheduled) { |
| 308 | mWriteScheduled = false; |
| 309 | doWrite = true; |
| 310 | } |
| 311 | } |
| 312 | if (doWrite) { |
| 313 | writeState(); |
| 314 | } |
| 315 | } |
| 316 | |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 317 | private ArrayList<AppOpsManager.OpEntry> collectOps(Ops pkgOps, int[] ops) { |
| 318 | ArrayList<AppOpsManager.OpEntry> resOps = null; |
| 319 | if (ops == null) { |
| 320 | resOps = new ArrayList<AppOpsManager.OpEntry>(); |
| 321 | for (int j=0; j<pkgOps.size(); j++) { |
| 322 | Op curOp = pkgOps.valueAt(j); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 323 | resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time, |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 324 | curOp.rejectTime, curOp.duration, curOp.proxyUid, |
| 325 | curOp.proxyPackageName)); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 326 | } |
| 327 | } else { |
| 328 | for (int j=0; j<ops.length; j++) { |
| 329 | Op curOp = pkgOps.get(ops[j]); |
| 330 | if (curOp != null) { |
| 331 | if (resOps == null) { |
| 332 | resOps = new ArrayList<AppOpsManager.OpEntry>(); |
| 333 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 334 | resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time, |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 335 | curOp.rejectTime, curOp.duration, curOp.proxyUid, |
| 336 | curOp.proxyPackageName)); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | } |
| 340 | return resOps; |
| 341 | } |
| 342 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 343 | @Override |
| 344 | public List<AppOpsManager.PackageOps> getPackagesForOps(int[] ops) { |
| 345 | mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS, |
| 346 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 347 | ArrayList<AppOpsManager.PackageOps> res = null; |
| 348 | synchronized (this) { |
| 349 | for (int i=0; i<mUidOps.size(); i++) { |
| 350 | HashMap<String, Ops> packages = mUidOps.valueAt(i); |
| 351 | for (Ops pkgOps : packages.values()) { |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 352 | ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 353 | if (resOps != null) { |
| 354 | if (res == null) { |
| 355 | res = new ArrayList<AppOpsManager.PackageOps>(); |
| 356 | } |
| 357 | AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps( |
| 358 | pkgOps.packageName, pkgOps.uid, resOps); |
| 359 | res.add(resPackage); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | return res; |
| 365 | } |
| 366 | |
| 367 | @Override |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 368 | public List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName, |
| 369 | int[] ops) { |
| 370 | mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS, |
| 371 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 372 | synchronized (this) { |
| 373 | Ops pkgOps = getOpsLocked(uid, packageName, false); |
| 374 | if (pkgOps == null) { |
| 375 | return null; |
| 376 | } |
| 377 | ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops); |
| 378 | if (resOps == null) { |
| 379 | return null; |
| 380 | } |
| 381 | ArrayList<AppOpsManager.PackageOps> res = new ArrayList<AppOpsManager.PackageOps>(); |
| 382 | AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps( |
| 383 | pkgOps.packageName, pkgOps.uid, resOps); |
| 384 | res.add(resPackage); |
| 385 | return res; |
| 386 | } |
| 387 | } |
| 388 | |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 389 | private void pruneOp(Op op, int uid, String packageName) { |
| 390 | if (op.time == 0 && op.rejectTime == 0) { |
| 391 | Ops ops = getOpsLocked(uid, packageName, false); |
| 392 | if (ops != null) { |
| 393 | ops.remove(op.op); |
| 394 | if (ops.size() <= 0) { |
| 395 | HashMap<String, Ops> pkgOps = mUidOps.get(uid); |
| 396 | if (pkgOps != null) { |
| 397 | pkgOps.remove(ops.packageName); |
| 398 | if (pkgOps.size() <= 0) { |
| 399 | mUidOps.remove(uid); |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 407 | @Override |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 408 | public void setMode(int code, int uid, String packageName, int mode) { |
Dianne Hackborn | b64afe1 | 2014-07-22 16:29:04 -0700 | [diff] [blame] | 409 | if (Binder.getCallingPid() != Process.myPid()) { |
| 410 | mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS, |
| 411 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
Dianne Hackborn | 133b9df | 2014-07-01 13:06:10 -0700 | [diff] [blame] | 412 | } |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 413 | verifyIncomingOp(code); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 414 | ArrayList<Callback> repCbs = null; |
| 415 | code = AppOpsManager.opToSwitch(code); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 416 | synchronized (this) { |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 417 | Op op = getOpLocked(code, uid, packageName, true); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 418 | if (op != null) { |
| 419 | if (op.mode != mode) { |
| 420 | op.mode = mode; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 421 | ArrayList<Callback> cbs = mOpModeWatchers.get(code); |
| 422 | if (cbs != null) { |
| 423 | if (repCbs == null) { |
| 424 | repCbs = new ArrayList<Callback>(); |
| 425 | } |
| 426 | repCbs.addAll(cbs); |
| 427 | } |
| 428 | cbs = mPackageModeWatchers.get(packageName); |
| 429 | if (cbs != null) { |
| 430 | if (repCbs == null) { |
| 431 | repCbs = new ArrayList<Callback>(); |
| 432 | } |
| 433 | repCbs.addAll(cbs); |
| 434 | } |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 435 | if (mode == AppOpsManager.opToDefaultMode(op.op)) { |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 436 | // If going into the default mode, prune this op |
| 437 | // if there is nothing else interesting in it. |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 438 | pruneOp(op, uid, packageName); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 439 | } |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 440 | scheduleFastWriteLocked(); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | } |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 444 | if (repCbs != null) { |
Svet Ganov | 3853611 | 2015-05-19 12:45:52 -0700 | [diff] [blame] | 445 | // There are components watching for mode changes such as window manager |
| 446 | // and location manager which are in our process. The callbacks in these |
| 447 | // components may require permissions our remote caller does not have. |
| 448 | final long identity = Binder.clearCallingIdentity(); |
| 449 | try { |
| 450 | for (int i = 0; i < repCbs.size(); i++) { |
| 451 | try { |
| 452 | repCbs.get(i).mCallback.opChanged(code, packageName); |
| 453 | } catch (RemoteException e) { |
| 454 | } |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 455 | } |
Svet Ganov | 3853611 | 2015-05-19 12:45:52 -0700 | [diff] [blame] | 456 | } finally { |
| 457 | Binder.restoreCallingIdentity(identity); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 462 | private static HashMap<Callback, ArrayList<Pair<String, Integer>>> addCallbacks( |
| 463 | HashMap<Callback, ArrayList<Pair<String, Integer>>> callbacks, |
| 464 | String packageName, int op, ArrayList<Callback> cbs) { |
| 465 | if (cbs == null) { |
| 466 | return callbacks; |
| 467 | } |
| 468 | if (callbacks == null) { |
| 469 | callbacks = new HashMap<Callback, ArrayList<Pair<String, Integer>>>(); |
| 470 | } |
| 471 | for (int i=0; i<cbs.size(); i++) { |
| 472 | Callback cb = cbs.get(i); |
| 473 | ArrayList<Pair<String, Integer>> reports = callbacks.get(cb); |
| 474 | if (reports == null) { |
| 475 | reports = new ArrayList<Pair<String, Integer>>(); |
| 476 | callbacks.put(cb, reports); |
| 477 | } |
| 478 | reports.add(new Pair<String, Integer>(packageName, op)); |
| 479 | } |
| 480 | return callbacks; |
| 481 | } |
| 482 | |
| 483 | @Override |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 484 | public void resetAllModes(int reqUserId, String reqPackageName) { |
| 485 | final int callingPid = Binder.getCallingPid(); |
| 486 | final int callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 487 | mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS, |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 488 | callingPid, callingUid, null); |
| 489 | reqUserId = ActivityManager.handleIncomingUser(callingPid, callingUid, reqUserId, |
| 490 | true, true, "resetAllModes", null); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 491 | HashMap<Callback, ArrayList<Pair<String, Integer>>> callbacks = null; |
| 492 | synchronized (this) { |
| 493 | boolean changed = false; |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 494 | for (int i=mUidOps.size()-1; i>=0; i--) { |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 495 | HashMap<String, Ops> packages = mUidOps.valueAt(i); |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 496 | if (reqUserId != UserHandle.USER_ALL |
| 497 | && reqUserId != UserHandle.getUserId(mUidOps.keyAt(i))) { |
Alexandra Gherghina | d6a9897 | 2014-08-04 17:05:34 +0100 | [diff] [blame] | 498 | // Skip any ops for a different user |
| 499 | continue; |
| 500 | } |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 501 | Iterator<Map.Entry<String, Ops>> it = packages.entrySet().iterator(); |
| 502 | while (it.hasNext()) { |
| 503 | Map.Entry<String, Ops> ent = it.next(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 504 | String packageName = ent.getKey(); |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 505 | if (reqPackageName != null && !reqPackageName.equals(packageName)) { |
| 506 | // Skip any ops for a different package |
| 507 | continue; |
| 508 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 509 | Ops pkgOps = ent.getValue(); |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 510 | for (int j=pkgOps.size()-1; j>=0; j--) { |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 511 | Op curOp = pkgOps.valueAt(j); |
Dianne Hackborn | 8828d3a | 2013-09-25 16:47:10 -0700 | [diff] [blame] | 512 | if (AppOpsManager.opAllowsReset(curOp.op) |
| 513 | && curOp.mode != AppOpsManager.opToDefaultMode(curOp.op)) { |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 514 | curOp.mode = AppOpsManager.opToDefaultMode(curOp.op); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 515 | changed = true; |
| 516 | callbacks = addCallbacks(callbacks, packageName, curOp.op, |
| 517 | mOpModeWatchers.get(curOp.op)); |
| 518 | callbacks = addCallbacks(callbacks, packageName, curOp.op, |
| 519 | mPackageModeWatchers.get(packageName)); |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 520 | if (curOp.time == 0 && curOp.rejectTime == 0) { |
| 521 | pkgOps.removeAt(j); |
| 522 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 523 | } |
| 524 | } |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 525 | if (pkgOps.size() == 0) { |
| 526 | it.remove(); |
| 527 | } |
| 528 | } |
| 529 | if (packages.size() == 0) { |
| 530 | mUidOps.removeAt(i); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | if (changed) { |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 534 | scheduleFastWriteLocked(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | if (callbacks != null) { |
| 538 | for (Map.Entry<Callback, ArrayList<Pair<String, Integer>>> ent : callbacks.entrySet()) { |
| 539 | Callback cb = ent.getKey(); |
| 540 | ArrayList<Pair<String, Integer>> reports = ent.getValue(); |
| 541 | for (int i=0; i<reports.size(); i++) { |
| 542 | Pair<String, Integer> rep = reports.get(i); |
| 543 | try { |
| 544 | cb.mCallback.opChanged(rep.second, rep.first); |
| 545 | } catch (RemoteException e) { |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 552 | @Override |
| 553 | public void startWatchingMode(int op, String packageName, IAppOpsCallback callback) { |
| 554 | synchronized (this) { |
| 555 | op = AppOpsManager.opToSwitch(op); |
| 556 | Callback cb = mModeWatchers.get(callback.asBinder()); |
| 557 | if (cb == null) { |
| 558 | cb = new Callback(callback); |
| 559 | mModeWatchers.put(callback.asBinder(), cb); |
| 560 | } |
| 561 | if (op != AppOpsManager.OP_NONE) { |
| 562 | ArrayList<Callback> cbs = mOpModeWatchers.get(op); |
| 563 | if (cbs == null) { |
| 564 | cbs = new ArrayList<Callback>(); |
| 565 | mOpModeWatchers.put(op, cbs); |
| 566 | } |
| 567 | cbs.add(cb); |
| 568 | } |
| 569 | if (packageName != null) { |
| 570 | ArrayList<Callback> cbs = mPackageModeWatchers.get(packageName); |
| 571 | if (cbs == null) { |
| 572 | cbs = new ArrayList<Callback>(); |
| 573 | mPackageModeWatchers.put(packageName, cbs); |
| 574 | } |
| 575 | cbs.add(cb); |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | @Override |
| 581 | public void stopWatchingMode(IAppOpsCallback callback) { |
| 582 | synchronized (this) { |
| 583 | Callback cb = mModeWatchers.remove(callback.asBinder()); |
| 584 | if (cb != null) { |
| 585 | cb.unlinkToDeath(); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 586 | for (int i=mOpModeWatchers.size()-1; i>=0; i--) { |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 587 | ArrayList<Callback> cbs = mOpModeWatchers.valueAt(i); |
| 588 | cbs.remove(cb); |
| 589 | if (cbs.size() <= 0) { |
| 590 | mOpModeWatchers.removeAt(i); |
| 591 | } |
| 592 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 593 | for (int i=mPackageModeWatchers.size()-1; i>=0; i--) { |
| 594 | ArrayList<Callback> cbs = mPackageModeWatchers.valueAt(i); |
| 595 | cbs.remove(cb); |
| 596 | if (cbs.size() <= 0) { |
| 597 | mPackageModeWatchers.removeAt(i); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | } |
| 601 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | @Override |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 605 | public IBinder getToken(IBinder clientToken) { |
| 606 | synchronized (this) { |
| 607 | ClientState cs = mClients.get(clientToken); |
| 608 | if (cs == null) { |
| 609 | cs = new ClientState(clientToken); |
| 610 | mClients.put(clientToken, cs); |
| 611 | } |
| 612 | return cs; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 617 | public int checkOperation(int code, int uid, String packageName) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 618 | verifyIncomingUid(uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 619 | verifyIncomingOp(code); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 620 | synchronized (this) { |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 621 | if (isOpRestricted(uid, code, packageName)) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 622 | return AppOpsManager.MODE_IGNORED; |
| 623 | } |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 624 | Op op = getOpLocked(AppOpsManager.opToSwitch(code), uid, packageName, false); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 625 | if (op == null) { |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 626 | return AppOpsManager.opToDefaultMode(code); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 627 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 628 | return op.mode; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 629 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | @Override |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 633 | public int checkAudioOperation(int code, int usage, int uid, String packageName) { |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 634 | synchronized (this) { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 635 | final int mode = checkRestrictionLocked(code, usage, uid, packageName); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 636 | if (mode != AppOpsManager.MODE_ALLOWED) { |
| 637 | return mode; |
| 638 | } |
| 639 | } |
| 640 | return checkOperation(code, uid, packageName); |
| 641 | } |
| 642 | |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 643 | private int checkRestrictionLocked(int code, int usage, int uid, String packageName) { |
| 644 | final SparseArray<Restriction> usageRestrictions = mAudioRestrictions.get(code); |
| 645 | if (usageRestrictions != null) { |
| 646 | final Restriction r = usageRestrictions.get(usage); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 647 | if (r != null && !r.exceptionPackages.contains(packageName)) { |
| 648 | return r.mode; |
| 649 | } |
| 650 | } |
| 651 | return AppOpsManager.MODE_ALLOWED; |
| 652 | } |
| 653 | |
| 654 | @Override |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 655 | public void setAudioRestriction(int code, int usage, int uid, int mode, |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 656 | String[] exceptionPackages) { |
| 657 | verifyIncomingUid(uid); |
| 658 | verifyIncomingOp(code); |
| 659 | synchronized (this) { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 660 | SparseArray<Restriction> usageRestrictions = mAudioRestrictions.get(code); |
| 661 | if (usageRestrictions == null) { |
| 662 | usageRestrictions = new SparseArray<Restriction>(); |
| 663 | mAudioRestrictions.put(code, usageRestrictions); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 664 | } |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 665 | usageRestrictions.remove(usage); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 666 | if (mode != AppOpsManager.MODE_ALLOWED) { |
| 667 | final Restriction r = new Restriction(); |
| 668 | r.mode = mode; |
| 669 | if (exceptionPackages != null) { |
| 670 | final int N = exceptionPackages.length; |
| 671 | r.exceptionPackages = new ArraySet<String>(N); |
| 672 | for (int i = 0; i < N; i++) { |
| 673 | final String pkg = exceptionPackages[i]; |
| 674 | if (pkg != null) { |
| 675 | r.exceptionPackages.add(pkg.trim()); |
| 676 | } |
| 677 | } |
| 678 | } |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 679 | usageRestrictions.put(usage, r); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | @Override |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 685 | public int checkPackage(int uid, String packageName) { |
| 686 | synchronized (this) { |
Dianne Hackborn | 0fcef84 | 2014-09-12 15:38:33 -0700 | [diff] [blame] | 687 | if (getOpsRawLocked(uid, packageName, true) != null) { |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 688 | return AppOpsManager.MODE_ALLOWED; |
| 689 | } else { |
| 690 | return AppOpsManager.MODE_ERRORED; |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | @Override |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 696 | public int noteProxyOperation(int code, String proxyPackageName, |
| 697 | int proxiedUid, String proxiedPackageName) { |
| 698 | verifyIncomingOp(code); |
| 699 | final int proxyMode = noteOperationUnchecked(code, Binder.getCallingUid(), |
| 700 | proxyPackageName, -1, null); |
| 701 | if (proxyMode != AppOpsManager.MODE_ALLOWED || Binder.getCallingUid() == proxiedUid) { |
| 702 | return proxyMode; |
| 703 | } |
| 704 | return noteOperationUnchecked(code, proxiedUid, proxiedPackageName, |
| 705 | Binder.getCallingUid(), proxyPackageName); |
| 706 | |
| 707 | } |
| 708 | |
| 709 | @Override |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 710 | public int noteOperation(int code, int uid, String packageName) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 711 | verifyIncomingUid(uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 712 | verifyIncomingOp(code); |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 713 | return noteOperationUnchecked(code, uid, packageName, 0, null); |
| 714 | } |
| 715 | |
| 716 | private int noteOperationUnchecked(int code, int uid, String packageName, |
| 717 | int proxyUid, String proxyPackageName) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 718 | synchronized (this) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 719 | Ops ops = getOpsLocked(uid, packageName, true); |
| 720 | if (ops == null) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 721 | if (DEBUG) Log.d(TAG, "noteOperation: no op for code " + code + " uid " + uid |
| 722 | + " package " + packageName); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 723 | return AppOpsManager.MODE_ERRORED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 724 | } |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 725 | Op op = getOpLocked(ops, code, true); |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 726 | if (isOpRestricted(uid, code, packageName)) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 727 | return AppOpsManager.MODE_IGNORED; |
| 728 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 729 | if (op.duration == -1) { |
| 730 | Slog.w(TAG, "Noting op not finished: uid " + uid + " pkg " + packageName |
| 731 | + " code " + code + " time=" + op.time + " duration=" + op.duration); |
| 732 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 733 | op.duration = 0; |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 734 | final int switchCode = AppOpsManager.opToSwitch(code); |
| 735 | final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op; |
| 736 | if (switchOp.mode != AppOpsManager.MODE_ALLOWED) { |
| 737 | if (DEBUG) Log.d(TAG, "noteOperation: reject #" + op.mode + " for code " |
| 738 | + switchCode + " (" + code + ") uid " + uid + " package " + packageName); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 739 | op.rejectTime = System.currentTimeMillis(); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 740 | return switchOp.mode; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 741 | } |
| 742 | if (DEBUG) Log.d(TAG, "noteOperation: allowing code " + code + " uid " + uid |
| 743 | + " package " + packageName); |
| 744 | op.time = System.currentTimeMillis(); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 745 | op.rejectTime = 0; |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 746 | op.proxyUid = proxyUid; |
| 747 | op.proxyPackageName = proxyPackageName; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 748 | return AppOpsManager.MODE_ALLOWED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 749 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | @Override |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 753 | public int startOperation(IBinder token, int code, int uid, String packageName) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 754 | verifyIncomingUid(uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 755 | verifyIncomingOp(code); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 756 | ClientState client = (ClientState)token; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 757 | synchronized (this) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 758 | Ops ops = getOpsLocked(uid, packageName, true); |
| 759 | if (ops == null) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 760 | if (DEBUG) Log.d(TAG, "startOperation: no op for code " + code + " uid " + uid |
| 761 | + " package " + packageName); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 762 | return AppOpsManager.MODE_ERRORED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 763 | } |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 764 | Op op = getOpLocked(ops, code, true); |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 765 | if (isOpRestricted(uid, code, packageName)) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 766 | return AppOpsManager.MODE_IGNORED; |
| 767 | } |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 768 | final int switchCode = AppOpsManager.opToSwitch(code); |
| 769 | final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op; |
| 770 | if (switchOp.mode != AppOpsManager.MODE_ALLOWED) { |
| 771 | if (DEBUG) Log.d(TAG, "startOperation: reject #" + op.mode + " for code " |
| 772 | + switchCode + " (" + code + ") uid " + uid + " package " + packageName); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 773 | op.rejectTime = System.currentTimeMillis(); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 774 | return switchOp.mode; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 775 | } |
| 776 | if (DEBUG) Log.d(TAG, "startOperation: allowing code " + code + " uid " + uid |
| 777 | + " package " + packageName); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 778 | if (op.nesting == 0) { |
| 779 | op.time = System.currentTimeMillis(); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 780 | op.rejectTime = 0; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 781 | op.duration = -1; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 782 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 783 | op.nesting++; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 784 | if (client.mStartedOps != null) { |
| 785 | client.mStartedOps.add(op); |
| 786 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 787 | return AppOpsManager.MODE_ALLOWED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 788 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | @Override |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 792 | public void finishOperation(IBinder token, int code, int uid, String packageName) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 793 | verifyIncomingUid(uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 794 | verifyIncomingOp(code); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 795 | ClientState client = (ClientState)token; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 796 | synchronized (this) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 797 | Op op = getOpLocked(code, uid, packageName, true); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 798 | if (op == null) { |
| 799 | return; |
| 800 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 801 | if (client.mStartedOps != null) { |
| 802 | if (!client.mStartedOps.remove(op)) { |
| 803 | throw new IllegalStateException("Operation not started: uid" + op.uid |
| 804 | + " pkg=" + op.packageName + " op=" + op.op); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 805 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 806 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 807 | finishOperationLocked(op); |
| 808 | } |
| 809 | } |
| 810 | |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 811 | @Override |
| 812 | public int permissionToOpCode(String permission) { |
| 813 | return AppOpsManager.permissionToOpCode(permission); |
| 814 | } |
| 815 | |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 816 | void finishOperationLocked(Op op) { |
| 817 | if (op.nesting <= 1) { |
| 818 | if (op.nesting == 1) { |
| 819 | op.duration = (int)(System.currentTimeMillis() - op.time); |
| 820 | op.time += op.duration; |
| 821 | } else { |
| 822 | Slog.w(TAG, "Finishing op nesting under-run: uid " + op.uid + " pkg " |
| 823 | + op.packageName + " code " + op.op + " time=" + op.time |
| 824 | + " duration=" + op.duration + " nesting=" + op.nesting); |
| 825 | } |
| 826 | op.nesting = 0; |
| 827 | } else { |
| 828 | op.nesting--; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 832 | private void verifyIncomingUid(int uid) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 833 | if (uid == Binder.getCallingUid()) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 834 | return; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 835 | } |
| 836 | if (Binder.getCallingPid() == Process.myPid()) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 837 | return; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 838 | } |
| 839 | mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS, |
| 840 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 841 | } |
| 842 | |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 843 | private void verifyIncomingOp(int op) { |
| 844 | if (op >= 0 && op < AppOpsManager._NUM_OP) { |
| 845 | return; |
| 846 | } |
| 847 | throw new IllegalArgumentException("Bad operation #" + op); |
| 848 | } |
| 849 | |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 850 | private Ops getOpsLocked(int uid, String packageName, boolean edit) { |
Dianne Hackborn | 0fcef84 | 2014-09-12 15:38:33 -0700 | [diff] [blame] | 851 | if (uid == 0) { |
| 852 | packageName = "root"; |
| 853 | } else if (uid == Process.SHELL_UID) { |
| 854 | packageName = "com.android.shell"; |
| 855 | } |
| 856 | return getOpsRawLocked(uid, packageName, edit); |
| 857 | } |
| 858 | |
| 859 | private Ops getOpsRawLocked(int uid, String packageName, boolean edit) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 860 | HashMap<String, Ops> pkgOps = mUidOps.get(uid); |
| 861 | if (pkgOps == null) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 862 | if (!edit) { |
| 863 | return null; |
| 864 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 865 | pkgOps = new HashMap<String, Ops>(); |
| 866 | mUidOps.put(uid, pkgOps); |
| 867 | } |
| 868 | Ops ops = pkgOps.get(packageName); |
| 869 | if (ops == null) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 870 | if (!edit) { |
| 871 | return null; |
| 872 | } |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 873 | boolean isPrivileged = false; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 874 | // This is the first time we have seen this package name under this uid, |
| 875 | // so let's make sure it is valid. |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 876 | if (uid != 0) { |
| 877 | final long ident = Binder.clearCallingIdentity(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 878 | try { |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 879 | int pkgUid = -1; |
| 880 | try { |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 881 | ApplicationInfo appInfo = ActivityThread.getPackageManager() |
| 882 | .getApplicationInfo(packageName, 0, UserHandle.getUserId(uid)); |
| 883 | if (appInfo != null) { |
| 884 | pkgUid = appInfo.uid; |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 885 | isPrivileged = (appInfo.privateFlags |
| 886 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 887 | } else { |
| 888 | if ("media".equals(packageName)) { |
| 889 | pkgUid = Process.MEDIA_UID; |
| 890 | isPrivileged = false; |
| 891 | } |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 892 | } |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 893 | } catch (RemoteException e) { |
| 894 | Slog.w(TAG, "Could not contact PackageManager", e); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 895 | } |
| 896 | if (pkgUid != uid) { |
| 897 | // Oops! The package name is not valid for the uid they are calling |
| 898 | // under. Abort. |
| 899 | Slog.w(TAG, "Bad call: specified package " + packageName |
| 900 | + " under uid " + uid + " but it is really " + pkgUid); |
| 901 | return null; |
| 902 | } |
| 903 | } finally { |
| 904 | Binder.restoreCallingIdentity(ident); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 905 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 906 | } |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 907 | ops = new Ops(packageName, uid, isPrivileged); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 908 | pkgOps.put(packageName, ops); |
| 909 | } |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 910 | return ops; |
| 911 | } |
| 912 | |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 913 | private void scheduleWriteLocked() { |
| 914 | if (!mWriteScheduled) { |
| 915 | mWriteScheduled = true; |
| 916 | mHandler.postDelayed(mWriteRunner, WRITE_DELAY); |
| 917 | } |
| 918 | } |
| 919 | |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 920 | private void scheduleFastWriteLocked() { |
| 921 | if (!mFastWriteScheduled) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 922 | mWriteScheduled = true; |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 923 | mFastWriteScheduled = true; |
| 924 | mHandler.removeCallbacks(mWriteRunner); |
| 925 | mHandler.postDelayed(mWriteRunner, 10*1000); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 926 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 927 | } |
| 928 | |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 929 | private Op getOpLocked(int code, int uid, String packageName, boolean edit) { |
| 930 | Ops ops = getOpsLocked(uid, packageName, edit); |
| 931 | if (ops == null) { |
| 932 | return null; |
| 933 | } |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 934 | return getOpLocked(ops, code, edit); |
| 935 | } |
| 936 | |
| 937 | private Op getOpLocked(Ops ops, int code, boolean edit) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 938 | Op op = ops.get(code); |
| 939 | if (op == null) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 940 | if (!edit) { |
| 941 | return null; |
| 942 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 943 | op = new Op(ops.uid, ops.packageName, code); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 944 | ops.put(code, op); |
| 945 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 946 | if (edit) { |
| 947 | scheduleWriteLocked(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 948 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 949 | return op; |
| 950 | } |
| 951 | |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 952 | private boolean isOpRestricted(int uid, int code, String packageName) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 953 | int userHandle = UserHandle.getUserId(uid); |
| 954 | boolean[] opRestrictions = mOpRestrictions.get(userHandle); |
| 955 | if ((opRestrictions != null) && opRestrictions[code]) { |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 956 | if (AppOpsManager.opAllowSystemBypassRestriction(code)) { |
| 957 | synchronized (this) { |
| 958 | Ops ops = getOpsLocked(uid, packageName, true); |
| 959 | if ((ops != null) && ops.isPrivileged) { |
| 960 | return false; |
| 961 | } |
| 962 | } |
| 963 | } |
Julia Reynolds | 401de17 | 2014-07-24 18:21:29 -0400 | [diff] [blame] | 964 | return true; |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 965 | } |
| 966 | return false; |
| 967 | } |
| 968 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 969 | void readState() { |
| 970 | synchronized (mFile) { |
| 971 | synchronized (this) { |
| 972 | FileInputStream stream; |
| 973 | try { |
| 974 | stream = mFile.openRead(); |
| 975 | } catch (FileNotFoundException e) { |
| 976 | Slog.i(TAG, "No existing app ops " + mFile.getBaseFile() + "; starting empty"); |
| 977 | return; |
| 978 | } |
| 979 | boolean success = false; |
| 980 | try { |
| 981 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 982 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 983 | int type; |
| 984 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 985 | && type != XmlPullParser.END_DOCUMENT) { |
| 986 | ; |
| 987 | } |
| 988 | |
| 989 | if (type != XmlPullParser.START_TAG) { |
| 990 | throw new IllegalStateException("no start tag found"); |
| 991 | } |
| 992 | |
| 993 | int outerDepth = parser.getDepth(); |
| 994 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 995 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 996 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 997 | continue; |
| 998 | } |
| 999 | |
| 1000 | String tagName = parser.getName(); |
| 1001 | if (tagName.equals("pkg")) { |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 1002 | readPackage(parser); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1003 | } else { |
| 1004 | Slog.w(TAG, "Unknown element under <app-ops>: " |
| 1005 | + parser.getName()); |
| 1006 | XmlUtils.skipCurrentTag(parser); |
| 1007 | } |
| 1008 | } |
| 1009 | success = true; |
| 1010 | } catch (IllegalStateException e) { |
| 1011 | Slog.w(TAG, "Failed parsing " + e); |
| 1012 | } catch (NullPointerException e) { |
| 1013 | Slog.w(TAG, "Failed parsing " + e); |
| 1014 | } catch (NumberFormatException e) { |
| 1015 | Slog.w(TAG, "Failed parsing " + e); |
| 1016 | } catch (XmlPullParserException e) { |
| 1017 | Slog.w(TAG, "Failed parsing " + e); |
| 1018 | } catch (IOException e) { |
| 1019 | Slog.w(TAG, "Failed parsing " + e); |
| 1020 | } catch (IndexOutOfBoundsException e) { |
| 1021 | Slog.w(TAG, "Failed parsing " + e); |
| 1022 | } finally { |
| 1023 | if (!success) { |
| 1024 | mUidOps.clear(); |
| 1025 | } |
| 1026 | try { |
| 1027 | stream.close(); |
| 1028 | } catch (IOException e) { |
| 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 1035 | void readPackage(XmlPullParser parser) throws NumberFormatException, |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1036 | XmlPullParserException, IOException { |
| 1037 | String pkgName = parser.getAttributeValue(null, "n"); |
| 1038 | int outerDepth = parser.getDepth(); |
| 1039 | int type; |
| 1040 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1041 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1042 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1043 | continue; |
| 1044 | } |
| 1045 | |
| 1046 | String tagName = parser.getName(); |
| 1047 | if (tagName.equals("uid")) { |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 1048 | readUid(parser, pkgName); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1049 | } else { |
| 1050 | Slog.w(TAG, "Unknown element under <pkg>: " |
| 1051 | + parser.getName()); |
| 1052 | XmlUtils.skipCurrentTag(parser); |
| 1053 | } |
| 1054 | } |
| 1055 | } |
| 1056 | |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 1057 | void readUid(XmlPullParser parser, String pkgName) throws NumberFormatException, |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1058 | XmlPullParserException, IOException { |
| 1059 | int uid = Integer.parseInt(parser.getAttributeValue(null, "n")); |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1060 | String isPrivilegedString = parser.getAttributeValue(null, "p"); |
| 1061 | boolean isPrivileged = false; |
| 1062 | if (isPrivilegedString == null) { |
| 1063 | try { |
| 1064 | IPackageManager packageManager = ActivityThread.getPackageManager(); |
| 1065 | if (packageManager != null) { |
| 1066 | ApplicationInfo appInfo = ActivityThread.getPackageManager() |
| 1067 | .getApplicationInfo(pkgName, 0, UserHandle.getUserId(uid)); |
| 1068 | if (appInfo != null) { |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 1069 | isPrivileged = (appInfo.privateFlags |
| 1070 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1071 | } |
| 1072 | } else { |
| 1073 | // Could not load data, don't add to cache so it will be loaded later. |
| 1074 | return; |
| 1075 | } |
| 1076 | } catch (RemoteException e) { |
| 1077 | Slog.w(TAG, "Could not contact PackageManager", e); |
| 1078 | } |
| 1079 | } else { |
| 1080 | isPrivileged = Boolean.parseBoolean(isPrivilegedString); |
| 1081 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1082 | int outerDepth = parser.getDepth(); |
| 1083 | int type; |
| 1084 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1085 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1086 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1087 | continue; |
| 1088 | } |
| 1089 | |
| 1090 | String tagName = parser.getName(); |
| 1091 | if (tagName.equals("op")) { |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1092 | Op op = new Op(uid, pkgName, Integer.parseInt(parser.getAttributeValue(null, "n"))); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1093 | String mode = parser.getAttributeValue(null, "m"); |
| 1094 | if (mode != null) { |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 1095 | op.mode = Integer.parseInt(mode); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1096 | } |
| 1097 | String time = parser.getAttributeValue(null, "t"); |
| 1098 | if (time != null) { |
| 1099 | op.time = Long.parseLong(time); |
| 1100 | } |
| 1101 | time = parser.getAttributeValue(null, "r"); |
| 1102 | if (time != null) { |
| 1103 | op.rejectTime = Long.parseLong(time); |
| 1104 | } |
| 1105 | String dur = parser.getAttributeValue(null, "d"); |
| 1106 | if (dur != null) { |
| 1107 | op.duration = Integer.parseInt(dur); |
| 1108 | } |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1109 | String proxyUid = parser.getAttributeValue(null, "pu"); |
| 1110 | if (proxyUid != null) { |
| 1111 | op.proxyUid = Integer.parseInt(proxyUid); |
| 1112 | } |
| 1113 | String proxyPackageName = parser.getAttributeValue(null, "pp"); |
| 1114 | if (proxyPackageName != null) { |
| 1115 | op.proxyPackageName = proxyPackageName; |
| 1116 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1117 | HashMap<String, Ops> pkgOps = mUidOps.get(uid); |
| 1118 | if (pkgOps == null) { |
| 1119 | pkgOps = new HashMap<String, Ops>(); |
| 1120 | mUidOps.put(uid, pkgOps); |
| 1121 | } |
| 1122 | Ops ops = pkgOps.get(pkgName); |
| 1123 | if (ops == null) { |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1124 | ops = new Ops(pkgName, uid, isPrivileged); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1125 | pkgOps.put(pkgName, ops); |
| 1126 | } |
| 1127 | ops.put(op.op, op); |
| 1128 | } else { |
| 1129 | Slog.w(TAG, "Unknown element under <pkg>: " |
| 1130 | + parser.getName()); |
| 1131 | XmlUtils.skipCurrentTag(parser); |
| 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | void writeState() { |
| 1137 | synchronized (mFile) { |
| 1138 | List<AppOpsManager.PackageOps> allOps = getPackagesForOps(null); |
| 1139 | |
| 1140 | FileOutputStream stream; |
| 1141 | try { |
| 1142 | stream = mFile.startWrite(); |
| 1143 | } catch (IOException e) { |
| 1144 | Slog.w(TAG, "Failed to write state: " + e); |
| 1145 | return; |
| 1146 | } |
| 1147 | |
| 1148 | try { |
| 1149 | XmlSerializer out = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1150 | out.setOutput(stream, StandardCharsets.UTF_8.name()); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1151 | out.startDocument(null, true); |
| 1152 | out.startTag(null, "app-ops"); |
| 1153 | |
| 1154 | if (allOps != null) { |
| 1155 | String lastPkg = null; |
| 1156 | for (int i=0; i<allOps.size(); i++) { |
| 1157 | AppOpsManager.PackageOps pkg = allOps.get(i); |
| 1158 | if (!pkg.getPackageName().equals(lastPkg)) { |
| 1159 | if (lastPkg != null) { |
| 1160 | out.endTag(null, "pkg"); |
| 1161 | } |
| 1162 | lastPkg = pkg.getPackageName(); |
| 1163 | out.startTag(null, "pkg"); |
| 1164 | out.attribute(null, "n", lastPkg); |
| 1165 | } |
| 1166 | out.startTag(null, "uid"); |
| 1167 | out.attribute(null, "n", Integer.toString(pkg.getUid())); |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1168 | synchronized (this) { |
| 1169 | Ops ops = getOpsLocked(pkg.getUid(), pkg.getPackageName(), false); |
| 1170 | // Should always be present as the list of PackageOps is generated |
| 1171 | // from Ops. |
| 1172 | if (ops != null) { |
| 1173 | out.attribute(null, "p", Boolean.toString(ops.isPrivileged)); |
| 1174 | } else { |
| 1175 | out.attribute(null, "p", Boolean.toString(false)); |
| 1176 | } |
| 1177 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1178 | List<AppOpsManager.OpEntry> ops = pkg.getOps(); |
| 1179 | for (int j=0; j<ops.size(); j++) { |
| 1180 | AppOpsManager.OpEntry op = ops.get(j); |
| 1181 | out.startTag(null, "op"); |
| 1182 | out.attribute(null, "n", Integer.toString(op.getOp())); |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 1183 | if (op.getMode() != AppOpsManager.opToDefaultMode(op.getOp())) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1184 | out.attribute(null, "m", Integer.toString(op.getMode())); |
| 1185 | } |
| 1186 | long time = op.getTime(); |
| 1187 | if (time != 0) { |
| 1188 | out.attribute(null, "t", Long.toString(time)); |
| 1189 | } |
| 1190 | time = op.getRejectTime(); |
| 1191 | if (time != 0) { |
| 1192 | out.attribute(null, "r", Long.toString(time)); |
| 1193 | } |
| 1194 | int dur = op.getDuration(); |
| 1195 | if (dur != 0) { |
| 1196 | out.attribute(null, "d", Integer.toString(dur)); |
| 1197 | } |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1198 | int proxyUid = op.getProxyUid(); |
| 1199 | if (proxyUid != -1) { |
| 1200 | out.attribute(null, "pu", Integer.toString(proxyUid)); |
| 1201 | } |
| 1202 | String proxyPackageName = op.getProxyPackageName(); |
| 1203 | if (proxyPackageName != null) { |
| 1204 | out.attribute(null, "pp", proxyPackageName); |
| 1205 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1206 | out.endTag(null, "op"); |
| 1207 | } |
| 1208 | out.endTag(null, "uid"); |
| 1209 | } |
| 1210 | if (lastPkg != null) { |
| 1211 | out.endTag(null, "pkg"); |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | out.endTag(null, "app-ops"); |
| 1216 | out.endDocument(); |
| 1217 | mFile.finishWrite(stream); |
| 1218 | } catch (IOException e) { |
| 1219 | Slog.w(TAG, "Failed to write state, restoring backup.", e); |
| 1220 | mFile.failWrite(stream); |
| 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1225 | @Override |
| 1226 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1227 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1228 | != PackageManager.PERMISSION_GRANTED) { |
| 1229 | pw.println("Permission Denial: can't dump ApOps service from from pid=" |
| 1230 | + Binder.getCallingPid() |
| 1231 | + ", uid=" + Binder.getCallingUid()); |
| 1232 | return; |
| 1233 | } |
| 1234 | |
| 1235 | synchronized (this) { |
| 1236 | pw.println("Current AppOps Service state:"); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1237 | final long now = System.currentTimeMillis(); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1238 | boolean needSep = false; |
| 1239 | if (mOpModeWatchers.size() > 0) { |
| 1240 | needSep = true; |
| 1241 | pw.println(" Op mode watchers:"); |
| 1242 | for (int i=0; i<mOpModeWatchers.size(); i++) { |
| 1243 | pw.print(" Op "); pw.print(AppOpsManager.opToName(mOpModeWatchers.keyAt(i))); |
| 1244 | pw.println(":"); |
| 1245 | ArrayList<Callback> callbacks = mOpModeWatchers.valueAt(i); |
| 1246 | for (int j=0; j<callbacks.size(); j++) { |
| 1247 | pw.print(" #"); pw.print(j); pw.print(": "); |
| 1248 | pw.println(callbacks.get(j)); |
| 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | if (mPackageModeWatchers.size() > 0) { |
| 1253 | needSep = true; |
| 1254 | pw.println(" Package mode watchers:"); |
| 1255 | for (int i=0; i<mPackageModeWatchers.size(); i++) { |
| 1256 | pw.print(" Pkg "); pw.print(mPackageModeWatchers.keyAt(i)); |
| 1257 | pw.println(":"); |
| 1258 | ArrayList<Callback> callbacks = mPackageModeWatchers.valueAt(i); |
| 1259 | for (int j=0; j<callbacks.size(); j++) { |
| 1260 | pw.print(" #"); pw.print(j); pw.print(": "); |
| 1261 | pw.println(callbacks.get(j)); |
| 1262 | } |
| 1263 | } |
| 1264 | } |
| 1265 | if (mModeWatchers.size() > 0) { |
| 1266 | needSep = true; |
| 1267 | pw.println(" All mode watchers:"); |
| 1268 | for (int i=0; i<mModeWatchers.size(); i++) { |
| 1269 | pw.print(" "); pw.print(mModeWatchers.keyAt(i)); |
| 1270 | pw.print(" -> "); pw.println(mModeWatchers.valueAt(i)); |
| 1271 | } |
| 1272 | } |
| 1273 | if (mClients.size() > 0) { |
| 1274 | needSep = true; |
| 1275 | pw.println(" Clients:"); |
| 1276 | for (int i=0; i<mClients.size(); i++) { |
| 1277 | pw.print(" "); pw.print(mClients.keyAt(i)); pw.println(":"); |
| 1278 | ClientState cs = mClients.valueAt(i); |
| 1279 | pw.print(" "); pw.println(cs); |
| 1280 | if (cs.mStartedOps != null && cs.mStartedOps.size() > 0) { |
| 1281 | pw.println(" Started ops:"); |
| 1282 | for (int j=0; j<cs.mStartedOps.size(); j++) { |
| 1283 | Op op = cs.mStartedOps.get(j); |
| 1284 | pw.print(" "); pw.print("uid="); pw.print(op.uid); |
| 1285 | pw.print(" pkg="); pw.print(op.packageName); |
| 1286 | pw.print(" op="); pw.println(AppOpsManager.opToName(op.op)); |
| 1287 | } |
| 1288 | } |
| 1289 | } |
| 1290 | } |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1291 | if (mAudioRestrictions.size() > 0) { |
| 1292 | boolean printedHeader = false; |
| 1293 | for (int o=0; o<mAudioRestrictions.size(); o++) { |
| 1294 | final String op = AppOpsManager.opToName(mAudioRestrictions.keyAt(o)); |
| 1295 | final SparseArray<Restriction> restrictions = mAudioRestrictions.valueAt(o); |
| 1296 | for (int i=0; i<restrictions.size(); i++) { |
| 1297 | if (!printedHeader){ |
| 1298 | pw.println(" Audio Restrictions:"); |
| 1299 | printedHeader = true; |
| 1300 | needSep = true; |
| 1301 | } |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 1302 | final int usage = restrictions.keyAt(i); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1303 | pw.print(" "); pw.print(op); |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 1304 | pw.print(" usage="); pw.print(AudioAttributes.usageToString(usage)); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1305 | Restriction r = restrictions.valueAt(i); |
| 1306 | pw.print(": mode="); pw.println(r.mode); |
| 1307 | if (!r.exceptionPackages.isEmpty()) { |
| 1308 | pw.println(" Exceptions:"); |
| 1309 | for (int j=0; j<r.exceptionPackages.size(); j++) { |
| 1310 | pw.print(" "); pw.println(r.exceptionPackages.valueAt(j)); |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | } |
| 1315 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1316 | if (needSep) { |
| 1317 | pw.println(); |
| 1318 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1319 | for (int i=0; i<mUidOps.size(); i++) { |
| 1320 | pw.print(" Uid "); UserHandle.formatUid(pw, mUidOps.keyAt(i)); pw.println(":"); |
| 1321 | HashMap<String, Ops> pkgOps = mUidOps.valueAt(i); |
| 1322 | for (Ops ops : pkgOps.values()) { |
| 1323 | pw.print(" Package "); pw.print(ops.packageName); pw.println(":"); |
| 1324 | for (int j=0; j<ops.size(); j++) { |
| 1325 | Op op = ops.valueAt(j); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1326 | pw.print(" "); pw.print(AppOpsManager.opToName(op.op)); |
| 1327 | pw.print(": mode="); pw.print(op.mode); |
| 1328 | if (op.time != 0) { |
| 1329 | pw.print("; time="); TimeUtils.formatDuration(now-op.time, pw); |
| 1330 | pw.print(" ago"); |
| 1331 | } |
| 1332 | if (op.rejectTime != 0) { |
| 1333 | pw.print("; rejectTime="); TimeUtils.formatDuration(now-op.rejectTime, pw); |
| 1334 | pw.print(" ago"); |
| 1335 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1336 | if (op.duration == -1) { |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 1337 | pw.print(" (running)"); |
| 1338 | } else if (op.duration != 0) { |
| 1339 | pw.print("; duration="); TimeUtils.formatDuration(op.duration, pw); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1340 | } |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 1341 | pw.println(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1342 | } |
| 1343 | } |
| 1344 | } |
| 1345 | } |
| 1346 | } |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 1347 | |
| 1348 | private static final class Restriction { |
| 1349 | private static final ArraySet<String> NO_EXCEPTIONS = new ArraySet<String>(); |
| 1350 | int mode; |
| 1351 | ArraySet<String> exceptionPackages = NO_EXCEPTIONS; |
| 1352 | } |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 1353 | |
| 1354 | @Override |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 1355 | public void setUserRestrictions(Bundle restrictions, int userHandle) throws RemoteException { |
| 1356 | checkSystemUid("setUserRestrictions"); |
| 1357 | boolean[] opRestrictions = mOpRestrictions.get(userHandle); |
| 1358 | if (opRestrictions == null) { |
| 1359 | opRestrictions = new boolean[AppOpsManager._NUM_OP]; |
| 1360 | mOpRestrictions.put(userHandle, opRestrictions); |
| 1361 | } |
| 1362 | for (int i = 0; i < opRestrictions.length; ++i) { |
| 1363 | String restriction = AppOpsManager.opToRestriction(i); |
| 1364 | if (restriction != null) { |
| 1365 | opRestrictions[i] = restrictions.getBoolean(restriction, false); |
| 1366 | } else { |
| 1367 | opRestrictions[i] = false; |
| 1368 | } |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | @Override |
| 1373 | public void removeUser(int userHandle) throws RemoteException { |
| 1374 | checkSystemUid("removeUser"); |
| 1375 | mOpRestrictions.remove(userHandle); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | private void checkSystemUid(String function) { |
| 1379 | int uid = Binder.getCallingUid(); |
| 1380 | if (uid != Process.SYSTEM_UID) { |
| 1381 | throw new SecurityException(function + " must by called by the system"); |
| 1382 | } |
| 1383 | } |
| 1384 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1385 | } |