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; |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 28 | import java.util.Arrays; |
Svetoslav | 215b44a | 2015-08-04 19:03:40 -0700 | [diff] [blame] | 29 | import java.util.Collections; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 30 | import java.util.HashMap; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 31 | import java.util.Iterator; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 32 | import java.util.List; |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 33 | import java.util.Map; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 34 | |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 35 | import android.Manifest; |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 36 | import android.app.ActivityManager; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 37 | import android.app.ActivityThread; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 38 | import android.app.AppGlobals; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 39 | import android.app.AppOpsManager; |
| 40 | import android.content.Context; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 41 | import android.content.pm.ApplicationInfo; |
| 42 | import android.content.pm.IPackageManager; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 43 | import android.content.pm.PackageManager; |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 44 | import android.media.AudioAttributes; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 45 | import android.os.AsyncTask; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 46 | import android.os.Binder; |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 47 | import android.os.Bundle; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 48 | import android.os.Handler; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 49 | import android.os.IBinder; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 50 | import android.os.Process; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 51 | import android.os.RemoteException; |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 52 | import android.os.ResultReceiver; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 53 | import android.os.ServiceManager; |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 54 | import android.os.ShellCommand; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 55 | import android.os.UserHandle; |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 56 | import android.os.storage.MountServiceInternal; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 57 | import android.util.ArrayMap; |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 58 | import android.util.ArraySet; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 59 | import android.util.AtomicFile; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 60 | import android.util.Log; |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 61 | import android.util.Pair; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 62 | import android.util.Slog; |
| 63 | import android.util.SparseArray; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 64 | import android.util.SparseIntArray; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 65 | import android.util.TimeUtils; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 66 | import android.util.Xml; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 67 | |
| 68 | import com.android.internal.app.IAppOpsService; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 69 | import com.android.internal.app.IAppOpsCallback; |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 70 | import com.android.internal.os.Zygote; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 71 | import com.android.internal.util.ArrayUtils; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 72 | import com.android.internal.util.FastXmlSerializer; |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 73 | import com.android.internal.util.Preconditions; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 74 | import com.android.internal.util.XmlUtils; |
| 75 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 76 | import libcore.util.EmptyArray; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 77 | import org.xmlpull.v1.XmlPullParser; |
| 78 | import org.xmlpull.v1.XmlPullParserException; |
| 79 | import org.xmlpull.v1.XmlSerializer; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 80 | |
| 81 | public class AppOpsService extends IAppOpsService.Stub { |
| 82 | static final String TAG = "AppOps"; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 83 | static final boolean DEBUG = false; |
| 84 | |
| 85 | // Write at most every 30 minutes. |
| 86 | static final long WRITE_DELAY = DEBUG ? 1000 : 30*60*1000; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 87 | |
| 88 | Context mContext; |
| 89 | final AtomicFile mFile; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 90 | final Handler mHandler; |
| 91 | |
| 92 | boolean mWriteScheduled; |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 93 | boolean mFastWriteScheduled; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 94 | final Runnable mWriteRunner = new Runnable() { |
| 95 | public void run() { |
| 96 | synchronized (AppOpsService.this) { |
| 97 | mWriteScheduled = false; |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 98 | mFastWriteScheduled = false; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 99 | AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() { |
| 100 | @Override protected Void doInBackground(Void... params) { |
| 101 | writeState(); |
| 102 | return null; |
| 103 | } |
| 104 | }; |
| 105 | task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null); |
| 106 | } |
| 107 | } |
| 108 | }; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 109 | |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 110 | private final SparseArray<UidState> mUidStates = new SparseArray<>(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 111 | |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 112 | /* |
| 113 | * These are app op restrictions imposed per user from various parties. |
| 114 | * |
| 115 | * This is organized as follows: |
| 116 | * |
| 117 | * ArrayMap w/ mapping: |
| 118 | * IBinder (for client imposing restriction) --> SparseArray w/ mapping: |
| 119 | * User handle --> Pair containing: |
| 120 | * - Array w/ index = AppOp code, value = restricted status boolean |
| 121 | * - SparseArray w/ mapping: |
| 122 | * AppOp code --> Set of packages that are not restricted for this code |
| 123 | * |
Ruben Brunk | 32f0fa4 | 2016-03-11 19:07:07 -0800 | [diff] [blame^] | 124 | * For efficiency, a core assumption here is that the number of per-package exemptions stored |
| 125 | * here will be relatively small. If this changes, this data structure should be revisited. |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 126 | */ |
| 127 | private final ArrayMap<IBinder, SparseArray<Pair<boolean[], SparseArray<ArraySet<String>>>>> |
| 128 | mOpUserRestrictions = new ArrayMap<>(); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 129 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 130 | private static final class UidState { |
| 131 | public final int uid; |
| 132 | public ArrayMap<String, Ops> pkgOps; |
| 133 | public SparseIntArray opModes; |
| 134 | |
| 135 | public UidState(int uid) { |
| 136 | this.uid = uid; |
| 137 | } |
| 138 | |
| 139 | public void clear() { |
| 140 | pkgOps = null; |
| 141 | opModes = null; |
| 142 | } |
| 143 | |
| 144 | public boolean isDefault() { |
| 145 | return (pkgOps == null || pkgOps.isEmpty()) |
| 146 | && (opModes == null || opModes.size() <= 0); |
| 147 | } |
| 148 | } |
| 149 | |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 150 | public final static class Ops extends SparseArray<Op> { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 151 | public final String packageName; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 152 | public final UidState uidState; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 153 | public final boolean isPrivileged; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 154 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 155 | public Ops(String _packageName, UidState _uidState, boolean _isPrivileged) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 156 | packageName = _packageName; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 157 | uidState = _uidState; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 158 | isPrivileged = _isPrivileged; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 162 | public final static class Op { |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 163 | public final int uid; |
| 164 | public final String packageName; |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 165 | public int proxyUid = -1; |
| 166 | public String proxyPackageName; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 167 | public final int op; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 168 | public int mode; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 169 | public int duration; |
| 170 | public long time; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 171 | public long rejectTime; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 172 | public int nesting; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 173 | |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 174 | public Op(int _uid, String _packageName, int _op) { |
| 175 | uid = _uid; |
| 176 | packageName = _packageName; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 177 | op = _op; |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 178 | mode = AppOpsManager.opToDefaultMode(op); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 182 | final SparseArray<ArrayList<Callback>> mOpModeWatchers |
| 183 | = new SparseArray<ArrayList<Callback>>(); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 184 | final ArrayMap<String, ArrayList<Callback>> mPackageModeWatchers |
| 185 | = new ArrayMap<String, ArrayList<Callback>>(); |
| 186 | final ArrayMap<IBinder, Callback> mModeWatchers |
| 187 | = new ArrayMap<IBinder, Callback>(); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 188 | final SparseArray<SparseArray<Restriction>> mAudioRestrictions |
| 189 | = new SparseArray<SparseArray<Restriction>>(); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 190 | |
| 191 | public final class Callback implements DeathRecipient { |
| 192 | final IAppOpsCallback mCallback; |
| 193 | |
| 194 | public Callback(IAppOpsCallback callback) { |
| 195 | mCallback = callback; |
| 196 | try { |
| 197 | mCallback.asBinder().linkToDeath(this, 0); |
| 198 | } catch (RemoteException e) { |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | public void unlinkToDeath() { |
| 203 | mCallback.asBinder().unlinkToDeath(this, 0); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void binderDied() { |
| 208 | stopWatchingMode(mCallback); |
| 209 | } |
| 210 | } |
| 211 | |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 212 | final ArrayMap<IBinder, ClientState> mClients = new ArrayMap<IBinder, ClientState>(); |
| 213 | |
| 214 | public final class ClientState extends Binder implements DeathRecipient { |
| 215 | final IBinder mAppToken; |
| 216 | final int mPid; |
| 217 | final ArrayList<Op> mStartedOps; |
| 218 | |
| 219 | public ClientState(IBinder appToken) { |
| 220 | mAppToken = appToken; |
| 221 | mPid = Binder.getCallingPid(); |
| 222 | if (appToken instanceof Binder) { |
| 223 | // For local clients, there is no reason to track them. |
| 224 | mStartedOps = null; |
| 225 | } else { |
| 226 | mStartedOps = new ArrayList<Op>(); |
| 227 | try { |
| 228 | mAppToken.linkToDeath(this, 0); |
| 229 | } catch (RemoteException e) { |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public String toString() { |
| 236 | return "ClientState{" + |
| 237 | "mAppToken=" + mAppToken + |
| 238 | ", " + (mStartedOps != null ? ("pid=" + mPid) : "local") + |
| 239 | '}'; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public void binderDied() { |
| 244 | synchronized (AppOpsService.this) { |
| 245 | for (int i=mStartedOps.size()-1; i>=0; i--) { |
| 246 | finishOperationLocked(mStartedOps.get(i)); |
| 247 | } |
| 248 | mClients.remove(mAppToken); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 253 | public AppOpsService(File storagePath, Handler handler) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 254 | mFile = new AtomicFile(storagePath); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 255 | mHandler = handler; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 256 | readState(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 257 | } |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 258 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 259 | public void publish(Context context) { |
| 260 | mContext = context; |
| 261 | ServiceManager.addService(Context.APP_OPS_SERVICE, asBinder()); |
| 262 | } |
| 263 | |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 264 | public void systemReady() { |
| 265 | synchronized (this) { |
| 266 | boolean changed = false; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 267 | for (int i = mUidStates.size() - 1; i >= 0; i--) { |
| 268 | UidState uidState = mUidStates.valueAt(i); |
| 269 | |
| 270 | String[] packageNames = getPackagesForUid(uidState.uid); |
| 271 | if (ArrayUtils.isEmpty(packageNames)) { |
| 272 | uidState.clear(); |
| 273 | mUidStates.removeAt(i); |
| 274 | changed = true; |
| 275 | continue; |
| 276 | } |
| 277 | |
| 278 | ArrayMap<String, Ops> pkgs = uidState.pkgOps; |
| 279 | if (pkgs == null) { |
| 280 | continue; |
| 281 | } |
| 282 | |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 283 | Iterator<Ops> it = pkgs.values().iterator(); |
| 284 | while (it.hasNext()) { |
| 285 | Ops ops = it.next(); |
Jeff Sharkey | e2ed23e | 2015-10-29 19:00:44 -0700 | [diff] [blame] | 286 | int curUid = -1; |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 287 | try { |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 288 | curUid = AppGlobals.getPackageManager().getPackageUid(ops.packageName, |
| 289 | PackageManager.MATCH_UNINSTALLED_PACKAGES, |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 290 | UserHandle.getUserId(ops.uidState.uid)); |
Jeff Sharkey | e2ed23e | 2015-10-29 19:00:44 -0700 | [diff] [blame] | 291 | } catch (RemoteException ignored) { |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 292 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 293 | if (curUid != ops.uidState.uid) { |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 294 | Slog.i(TAG, "Pruning old package " + ops.packageName |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 295 | + "/" + ops.uidState + ": new uid=" + curUid); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 296 | it.remove(); |
| 297 | changed = true; |
| 298 | } |
| 299 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 300 | |
| 301 | if (uidState.isDefault()) { |
| 302 | mUidStates.removeAt(i); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | if (changed) { |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 306 | scheduleFastWriteLocked(); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 307 | } |
| 308 | } |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 309 | |
| 310 | MountServiceInternal mountServiceInternal = LocalServices.getService( |
| 311 | MountServiceInternal.class); |
| 312 | mountServiceInternal.addExternalStoragePolicy( |
| 313 | new MountServiceInternal.ExternalStorageMountPolicy() { |
| 314 | @Override |
| 315 | public int getMountMode(int uid, String packageName) { |
| 316 | if (Process.isIsolated(uid)) { |
| 317 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 318 | } |
| 319 | if (noteOperation(AppOpsManager.OP_READ_EXTERNAL_STORAGE, uid, |
| 320 | packageName) != AppOpsManager.MODE_ALLOWED) { |
| 321 | return Zygote.MOUNT_EXTERNAL_NONE; |
| 322 | } |
| 323 | if (noteOperation(AppOpsManager.OP_WRITE_EXTERNAL_STORAGE, uid, |
| 324 | packageName) != AppOpsManager.MODE_ALLOWED) { |
| 325 | return Zygote.MOUNT_EXTERNAL_READ; |
| 326 | } |
| 327 | return Zygote.MOUNT_EXTERNAL_WRITE; |
| 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public boolean hasExternalStorage(int uid, String packageName) { |
| 332 | final int mountMode = getMountMode(uid, packageName); |
| 333 | return mountMode == Zygote.MOUNT_EXTERNAL_READ |
| 334 | || mountMode == Zygote.MOUNT_EXTERNAL_WRITE; |
| 335 | } |
| 336 | }); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | public void packageRemoved(int uid, String packageName) { |
| 340 | synchronized (this) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 341 | UidState uidState = mUidStates.get(uid); |
| 342 | if (uidState == null) { |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | boolean changed = false; |
| 347 | |
| 348 | // Remove any package state if such. |
| 349 | if (uidState.pkgOps != null && uidState.pkgOps.remove(packageName) != null) { |
| 350 | changed = true; |
| 351 | } |
| 352 | |
| 353 | // If we just nuked the last package state check if the UID is valid. |
| 354 | if (changed && uidState.pkgOps.isEmpty() |
| 355 | && getPackagesForUid(uid).length <= 0) { |
| 356 | mUidStates.remove(uid); |
| 357 | } |
| 358 | |
| 359 | if (changed) { |
| 360 | scheduleFastWriteLocked(); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | public void uidRemoved(int uid) { |
| 366 | synchronized (this) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 367 | if (mUidStates.indexOfKey(uid) >= 0) { |
| 368 | mUidStates.remove(uid); |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 369 | scheduleFastWriteLocked(); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 374 | public void shutdown() { |
| 375 | Slog.w(TAG, "Writing app ops before shutdown..."); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 376 | boolean doWrite = false; |
| 377 | synchronized (this) { |
| 378 | if (mWriteScheduled) { |
| 379 | mWriteScheduled = false; |
| 380 | doWrite = true; |
| 381 | } |
| 382 | } |
| 383 | if (doWrite) { |
| 384 | writeState(); |
| 385 | } |
| 386 | } |
| 387 | |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 388 | private ArrayList<AppOpsManager.OpEntry> collectOps(Ops pkgOps, int[] ops) { |
| 389 | ArrayList<AppOpsManager.OpEntry> resOps = null; |
| 390 | if (ops == null) { |
| 391 | resOps = new ArrayList<AppOpsManager.OpEntry>(); |
| 392 | for (int j=0; j<pkgOps.size(); j++) { |
| 393 | Op curOp = pkgOps.valueAt(j); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 394 | resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time, |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 395 | curOp.rejectTime, curOp.duration, curOp.proxyUid, |
| 396 | curOp.proxyPackageName)); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 397 | } |
| 398 | } else { |
| 399 | for (int j=0; j<ops.length; j++) { |
| 400 | Op curOp = pkgOps.get(ops[j]); |
| 401 | if (curOp != null) { |
| 402 | if (resOps == null) { |
| 403 | resOps = new ArrayList<AppOpsManager.OpEntry>(); |
| 404 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 405 | resOps.add(new AppOpsManager.OpEntry(curOp.op, curOp.mode, curOp.time, |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 406 | curOp.rejectTime, curOp.duration, curOp.proxyUid, |
| 407 | curOp.proxyPackageName)); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | } |
| 411 | return resOps; |
| 412 | } |
| 413 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 414 | @Override |
| 415 | public List<AppOpsManager.PackageOps> getPackagesForOps(int[] ops) { |
| 416 | mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS, |
| 417 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 418 | ArrayList<AppOpsManager.PackageOps> res = null; |
| 419 | synchronized (this) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 420 | final int uidStateCount = mUidStates.size(); |
| 421 | for (int i = 0; i < uidStateCount; i++) { |
| 422 | UidState uidState = mUidStates.valueAt(i); |
| 423 | if (uidState.pkgOps == null || uidState.pkgOps.isEmpty()) { |
| 424 | continue; |
| 425 | } |
| 426 | ArrayMap<String, Ops> packages = uidState.pkgOps; |
| 427 | final int packageCount = packages.size(); |
| 428 | for (int j = 0; j < packageCount; j++) { |
| 429 | Ops pkgOps = packages.valueAt(j); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 430 | ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 431 | if (resOps != null) { |
| 432 | if (res == null) { |
| 433 | res = new ArrayList<AppOpsManager.PackageOps>(); |
| 434 | } |
| 435 | AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps( |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 436 | pkgOps.packageName, pkgOps.uidState.uid, resOps); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 437 | res.add(resPackage); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | return res; |
| 443 | } |
| 444 | |
| 445 | @Override |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 446 | public List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName, |
| 447 | int[] ops) { |
| 448 | mContext.enforcePermission(android.Manifest.permission.GET_APP_OPS_STATS, |
| 449 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 450 | synchronized (this) { |
| 451 | Ops pkgOps = getOpsLocked(uid, packageName, false); |
| 452 | if (pkgOps == null) { |
| 453 | return null; |
| 454 | } |
| 455 | ArrayList<AppOpsManager.OpEntry> resOps = collectOps(pkgOps, ops); |
| 456 | if (resOps == null) { |
| 457 | return null; |
| 458 | } |
| 459 | ArrayList<AppOpsManager.PackageOps> res = new ArrayList<AppOpsManager.PackageOps>(); |
| 460 | AppOpsManager.PackageOps resPackage = new AppOpsManager.PackageOps( |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 461 | pkgOps.packageName, pkgOps.uidState.uid, resOps); |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 462 | res.add(resPackage); |
| 463 | return res; |
| 464 | } |
| 465 | } |
| 466 | |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 467 | private void pruneOp(Op op, int uid, String packageName) { |
| 468 | if (op.time == 0 && op.rejectTime == 0) { |
| 469 | Ops ops = getOpsLocked(uid, packageName, false); |
| 470 | if (ops != null) { |
| 471 | ops.remove(op.op); |
| 472 | if (ops.size() <= 0) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 473 | UidState uidState = ops.uidState; |
| 474 | ArrayMap<String, Ops> pkgOps = uidState.pkgOps; |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 475 | if (pkgOps != null) { |
| 476 | pkgOps.remove(ops.packageName); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 477 | if (pkgOps.isEmpty()) { |
| 478 | uidState.pkgOps = null; |
| 479 | } |
| 480 | if (uidState.isDefault()) { |
| 481 | mUidStates.remove(uid); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 489 | @Override |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 490 | public void setUidMode(int code, int uid, int mode) { |
| 491 | if (Binder.getCallingPid() != Process.myPid()) { |
| 492 | mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS, |
| 493 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 494 | } |
| 495 | verifyIncomingOp(code); |
| 496 | code = AppOpsManager.opToSwitch(code); |
| 497 | |
| 498 | synchronized (this) { |
| 499 | final int defaultMode = AppOpsManager.opToDefaultMode(code); |
| 500 | |
| 501 | UidState uidState = getUidStateLocked(uid, false); |
| 502 | if (uidState == null) { |
| 503 | if (mode == defaultMode) { |
| 504 | return; |
| 505 | } |
| 506 | uidState = new UidState(uid); |
| 507 | uidState.opModes = new SparseIntArray(); |
| 508 | uidState.opModes.put(code, mode); |
| 509 | mUidStates.put(uid, uidState); |
| 510 | scheduleWriteLocked(); |
| 511 | } else if (uidState.opModes == null) { |
| 512 | if (mode != defaultMode) { |
| 513 | uidState.opModes = new SparseIntArray(); |
| 514 | uidState.opModes.put(code, mode); |
| 515 | scheduleWriteLocked(); |
| 516 | } |
| 517 | } else { |
| 518 | if (uidState.opModes.get(code) == mode) { |
| 519 | return; |
| 520 | } |
| 521 | if (mode == defaultMode) { |
| 522 | uidState.opModes.delete(code); |
| 523 | if (uidState.opModes.size() <= 0) { |
| 524 | uidState.opModes = null; |
| 525 | } |
| 526 | } else { |
| 527 | uidState.opModes.put(code, mode); |
| 528 | } |
| 529 | scheduleWriteLocked(); |
| 530 | } |
| 531 | } |
| 532 | |
Svetoslav | 215b44a | 2015-08-04 19:03:40 -0700 | [diff] [blame] | 533 | String[] uidPackageNames = getPackagesForUid(uid); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 534 | ArrayMap<Callback, ArraySet<String>> callbackSpecs = null; |
| 535 | |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 536 | synchronized (this) { |
| 537 | ArrayList<Callback> callbacks = mOpModeWatchers.get(code); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 538 | if (callbacks != null) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 539 | final int callbackCount = callbacks.size(); |
| 540 | for (int i = 0; i < callbackCount; i++) { |
| 541 | Callback callback = callbacks.get(i); |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 542 | ArraySet<String> changedPackages = new ArraySet<>(); |
| 543 | Collections.addAll(changedPackages, uidPackageNames); |
| 544 | callbackSpecs = new ArrayMap<>(); |
| 545 | callbackSpecs.put(callback, changedPackages); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | for (String uidPackageName : uidPackageNames) { |
| 550 | callbacks = mPackageModeWatchers.get(uidPackageName); |
| 551 | if (callbacks != null) { |
| 552 | if (callbackSpecs == null) { |
| 553 | callbackSpecs = new ArrayMap<>(); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 554 | } |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 555 | final int callbackCount = callbacks.size(); |
| 556 | for (int i = 0; i < callbackCount; i++) { |
| 557 | Callback callback = callbacks.get(i); |
| 558 | ArraySet<String> changedPackages = callbackSpecs.get(callback); |
| 559 | if (changedPackages == null) { |
| 560 | changedPackages = new ArraySet<>(); |
| 561 | callbackSpecs.put(callback, changedPackages); |
| 562 | } |
| 563 | changedPackages.add(uidPackageName); |
| 564 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | if (callbackSpecs == null) { |
| 570 | return; |
| 571 | } |
| 572 | |
| 573 | // There are components watching for mode changes such as window manager |
| 574 | // and location manager which are in our process. The callbacks in these |
| 575 | // components may require permissions our remote caller does not have. |
| 576 | final long identity = Binder.clearCallingIdentity(); |
| 577 | try { |
| 578 | for (int i = 0; i < callbackSpecs.size(); i++) { |
| 579 | Callback callback = callbackSpecs.keyAt(i); |
| 580 | ArraySet<String> reportedPackageNames = callbackSpecs.valueAt(i); |
| 581 | try { |
| 582 | if (reportedPackageNames == null) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 583 | callback.mCallback.opChanged(code, uid, null); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 584 | } else { |
| 585 | final int reportedPackageCount = reportedPackageNames.size(); |
| 586 | for (int j = 0; j < reportedPackageCount; j++) { |
| 587 | String reportedPackageName = reportedPackageNames.valueAt(j); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 588 | callback.mCallback.opChanged(code, uid, reportedPackageName); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | } catch (RemoteException e) { |
| 592 | Log.w(TAG, "Error dispatching op op change", e); |
| 593 | } |
| 594 | } |
| 595 | } finally { |
| 596 | Binder.restoreCallingIdentity(identity); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | @Override |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 601 | public void setMode(int code, int uid, String packageName, int mode) { |
Dianne Hackborn | b64afe1 | 2014-07-22 16:29:04 -0700 | [diff] [blame] | 602 | if (Binder.getCallingPid() != Process.myPid()) { |
| 603 | mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS, |
| 604 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
Dianne Hackborn | 133b9df | 2014-07-01 13:06:10 -0700 | [diff] [blame] | 605 | } |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 606 | verifyIncomingOp(code); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 607 | ArrayList<Callback> repCbs = null; |
| 608 | code = AppOpsManager.opToSwitch(code); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 609 | synchronized (this) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 610 | UidState uidState = getUidStateLocked(uid, false); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 611 | Op op = getOpLocked(code, uid, packageName, true); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 612 | if (op != null) { |
| 613 | if (op.mode != mode) { |
| 614 | op.mode = mode; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 615 | ArrayList<Callback> cbs = mOpModeWatchers.get(code); |
| 616 | if (cbs != null) { |
| 617 | if (repCbs == null) { |
| 618 | repCbs = new ArrayList<Callback>(); |
| 619 | } |
| 620 | repCbs.addAll(cbs); |
| 621 | } |
| 622 | cbs = mPackageModeWatchers.get(packageName); |
| 623 | if (cbs != null) { |
| 624 | if (repCbs == null) { |
| 625 | repCbs = new ArrayList<Callback>(); |
| 626 | } |
| 627 | repCbs.addAll(cbs); |
| 628 | } |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 629 | if (mode == AppOpsManager.opToDefaultMode(op.op)) { |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 630 | // If going into the default mode, prune this op |
| 631 | // if there is nothing else interesting in it. |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 632 | pruneOp(op, uid, packageName); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 633 | } |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 634 | scheduleFastWriteLocked(); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | } |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 638 | if (repCbs != null) { |
Svet Ganov | 3853611 | 2015-05-19 12:45:52 -0700 | [diff] [blame] | 639 | // There are components watching for mode changes such as window manager |
| 640 | // and location manager which are in our process. The callbacks in these |
| 641 | // components may require permissions our remote caller does not have. |
| 642 | final long identity = Binder.clearCallingIdentity(); |
| 643 | try { |
| 644 | for (int i = 0; i < repCbs.size(); i++) { |
| 645 | try { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 646 | repCbs.get(i).mCallback.opChanged(code, uid, packageName); |
Svet Ganov | 3853611 | 2015-05-19 12:45:52 -0700 | [diff] [blame] | 647 | } catch (RemoteException e) { |
| 648 | } |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 649 | } |
Svet Ganov | 3853611 | 2015-05-19 12:45:52 -0700 | [diff] [blame] | 650 | } finally { |
| 651 | Binder.restoreCallingIdentity(identity); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 656 | private static HashMap<Callback, ArrayList<ChangeRec>> addCallbacks( |
| 657 | HashMap<Callback, ArrayList<ChangeRec>> callbacks, |
| 658 | int op, int uid, String packageName, ArrayList<Callback> cbs) { |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 659 | if (cbs == null) { |
| 660 | return callbacks; |
| 661 | } |
| 662 | if (callbacks == null) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 663 | callbacks = new HashMap<>(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 664 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 665 | boolean duplicate = false; |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 666 | for (int i=0; i<cbs.size(); i++) { |
| 667 | Callback cb = cbs.get(i); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 668 | ArrayList<ChangeRec> reports = callbacks.get(cb); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 669 | if (reports == null) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 670 | reports = new ArrayList<>(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 671 | callbacks.put(cb, reports); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 672 | } else { |
| 673 | final int reportCount = reports.size(); |
| 674 | for (int j = 0; j < reportCount; j++) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 675 | ChangeRec report = reports.get(j); |
| 676 | if (report.op == op && report.pkg.equals(packageName)) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 677 | duplicate = true; |
| 678 | break; |
| 679 | } |
| 680 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 681 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 682 | if (!duplicate) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 683 | reports.add(new ChangeRec(op, uid, packageName)); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 684 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 685 | } |
| 686 | return callbacks; |
| 687 | } |
| 688 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 689 | static final class ChangeRec { |
| 690 | final int op; |
| 691 | final int uid; |
| 692 | final String pkg; |
| 693 | |
| 694 | ChangeRec(int _op, int _uid, String _pkg) { |
| 695 | op = _op; |
| 696 | uid = _uid; |
| 697 | pkg = _pkg; |
| 698 | } |
| 699 | } |
| 700 | |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 701 | @Override |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 702 | public void resetAllModes(int reqUserId, String reqPackageName) { |
| 703 | final int callingPid = Binder.getCallingPid(); |
| 704 | final int callingUid = Binder.getCallingUid(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 705 | mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS, |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 706 | callingPid, callingUid, null); |
| 707 | reqUserId = ActivityManager.handleIncomingUser(callingPid, callingUid, reqUserId, |
| 708 | true, true, "resetAllModes", null); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 709 | |
| 710 | int reqUid = -1; |
| 711 | if (reqPackageName != null) { |
| 712 | try { |
| 713 | reqUid = AppGlobals.getPackageManager().getPackageUid( |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 714 | reqPackageName, PackageManager.MATCH_UNINSTALLED_PACKAGES, reqUserId); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 715 | } catch (RemoteException e) { |
| 716 | /* ignore - local call */ |
| 717 | } |
| 718 | } |
| 719 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 720 | HashMap<Callback, ArrayList<ChangeRec>> callbacks = null; |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 721 | synchronized (this) { |
| 722 | boolean changed = false; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 723 | for (int i = mUidStates.size() - 1; i >= 0; i--) { |
| 724 | UidState uidState = mUidStates.valueAt(i); |
| 725 | |
| 726 | SparseIntArray opModes = uidState.opModes; |
| 727 | if (opModes != null && (uidState.uid == reqUid || reqUid == -1)) { |
| 728 | final int uidOpCount = opModes.size(); |
| 729 | for (int j = uidOpCount - 1; j >= 0; j--) { |
| 730 | final int code = opModes.keyAt(j); |
| 731 | if (AppOpsManager.opAllowsReset(code)) { |
| 732 | opModes.removeAt(j); |
| 733 | if (opModes.size() <= 0) { |
| 734 | uidState.opModes = null; |
| 735 | } |
| 736 | for (String packageName : getPackagesForUid(uidState.uid)) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 737 | callbacks = addCallbacks(callbacks, code, uidState.uid, packageName, |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 738 | mOpModeWatchers.get(code)); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 739 | callbacks = addCallbacks(callbacks, code, uidState.uid, packageName, |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 740 | mPackageModeWatchers.get(packageName)); |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | if (uidState.pkgOps == null) { |
| 747 | continue; |
| 748 | } |
| 749 | |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 750 | if (reqUserId != UserHandle.USER_ALL |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 751 | && reqUserId != UserHandle.getUserId(uidState.uid)) { |
Alexandra Gherghina | d6a9897 | 2014-08-04 17:05:34 +0100 | [diff] [blame] | 752 | // Skip any ops for a different user |
| 753 | continue; |
| 754 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 755 | |
| 756 | Map<String, Ops> packages = uidState.pkgOps; |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 757 | Iterator<Map.Entry<String, Ops>> it = packages.entrySet().iterator(); |
| 758 | while (it.hasNext()) { |
| 759 | Map.Entry<String, Ops> ent = it.next(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 760 | String packageName = ent.getKey(); |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 761 | if (reqPackageName != null && !reqPackageName.equals(packageName)) { |
| 762 | // Skip any ops for a different package |
| 763 | continue; |
| 764 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 765 | Ops pkgOps = ent.getValue(); |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 766 | for (int j=pkgOps.size()-1; j>=0; j--) { |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 767 | Op curOp = pkgOps.valueAt(j); |
Dianne Hackborn | 8828d3a | 2013-09-25 16:47:10 -0700 | [diff] [blame] | 768 | if (AppOpsManager.opAllowsReset(curOp.op) |
| 769 | && curOp.mode != AppOpsManager.opToDefaultMode(curOp.op)) { |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 770 | curOp.mode = AppOpsManager.opToDefaultMode(curOp.op); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 771 | changed = true; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 772 | callbacks = addCallbacks(callbacks, curOp.op, curOp.uid, packageName, |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 773 | mOpModeWatchers.get(curOp.op)); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 774 | callbacks = addCallbacks(callbacks, curOp.op, curOp.uid, packageName, |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 775 | mPackageModeWatchers.get(packageName)); |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 776 | if (curOp.time == 0 && curOp.rejectTime == 0) { |
| 777 | pkgOps.removeAt(j); |
| 778 | } |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 779 | } |
| 780 | } |
Dianne Hackborn | 7f09ec3 | 2013-08-07 15:36:08 -0700 | [diff] [blame] | 781 | if (pkgOps.size() == 0) { |
| 782 | it.remove(); |
| 783 | } |
| 784 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 785 | if (uidState.isDefault()) { |
| 786 | mUidStates.remove(uidState.uid); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 787 | } |
| 788 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 789 | |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 790 | if (changed) { |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 791 | scheduleFastWriteLocked(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | if (callbacks != null) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 795 | for (Map.Entry<Callback, ArrayList<ChangeRec>> ent : callbacks.entrySet()) { |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 796 | Callback cb = ent.getKey(); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 797 | ArrayList<ChangeRec> reports = ent.getValue(); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 798 | for (int i=0; i<reports.size(); i++) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 799 | ChangeRec rep = reports.get(i); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 800 | try { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 801 | cb.mCallback.opChanged(rep.op, rep.uid, rep.pkg); |
Dianne Hackborn | 607b414 | 2013-08-02 18:10:10 -0700 | [diff] [blame] | 802 | } catch (RemoteException e) { |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 809 | @Override |
| 810 | public void startWatchingMode(int op, String packageName, IAppOpsCallback callback) { |
Svetoslav Ganov | 8de5971 | 2015-12-09 18:25:13 -0800 | [diff] [blame] | 811 | if (callback == null) { |
| 812 | return; |
| 813 | } |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 814 | synchronized (this) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 815 | op = (op != AppOpsManager.OP_NONE) ? AppOpsManager.opToSwitch(op) : op; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 816 | Callback cb = mModeWatchers.get(callback.asBinder()); |
| 817 | if (cb == null) { |
| 818 | cb = new Callback(callback); |
| 819 | mModeWatchers.put(callback.asBinder(), cb); |
| 820 | } |
| 821 | if (op != AppOpsManager.OP_NONE) { |
| 822 | ArrayList<Callback> cbs = mOpModeWatchers.get(op); |
| 823 | if (cbs == null) { |
| 824 | cbs = new ArrayList<Callback>(); |
| 825 | mOpModeWatchers.put(op, cbs); |
| 826 | } |
| 827 | cbs.add(cb); |
| 828 | } |
| 829 | if (packageName != null) { |
| 830 | ArrayList<Callback> cbs = mPackageModeWatchers.get(packageName); |
| 831 | if (cbs == null) { |
| 832 | cbs = new ArrayList<Callback>(); |
| 833 | mPackageModeWatchers.put(packageName, cbs); |
| 834 | } |
| 835 | cbs.add(cb); |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | @Override |
| 841 | public void stopWatchingMode(IAppOpsCallback callback) { |
Svetoslav Ganov | 8de5971 | 2015-12-09 18:25:13 -0800 | [diff] [blame] | 842 | if (callback == null) { |
| 843 | return; |
| 844 | } |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 845 | synchronized (this) { |
| 846 | Callback cb = mModeWatchers.remove(callback.asBinder()); |
| 847 | if (cb != null) { |
| 848 | cb.unlinkToDeath(); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 849 | for (int i=mOpModeWatchers.size()-1; i>=0; i--) { |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 850 | ArrayList<Callback> cbs = mOpModeWatchers.valueAt(i); |
| 851 | cbs.remove(cb); |
| 852 | if (cbs.size() <= 0) { |
| 853 | mOpModeWatchers.removeAt(i); |
| 854 | } |
| 855 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 856 | for (int i=mPackageModeWatchers.size()-1; i>=0; i--) { |
| 857 | ArrayList<Callback> cbs = mPackageModeWatchers.valueAt(i); |
| 858 | cbs.remove(cb); |
| 859 | if (cbs.size() <= 0) { |
| 860 | mPackageModeWatchers.removeAt(i); |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | } |
| 864 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | @Override |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 868 | public IBinder getToken(IBinder clientToken) { |
| 869 | synchronized (this) { |
| 870 | ClientState cs = mClients.get(clientToken); |
| 871 | if (cs == null) { |
| 872 | cs = new ClientState(clientToken); |
| 873 | mClients.put(clientToken, cs); |
| 874 | } |
| 875 | return cs; |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | @Override |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 880 | public int checkOperation(int code, int uid, String packageName) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 881 | verifyIncomingUid(uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 882 | verifyIncomingOp(code); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 883 | synchronized (this) { |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 884 | if (isOpRestricted(uid, code, packageName)) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 885 | return AppOpsManager.MODE_IGNORED; |
| 886 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 887 | code = AppOpsManager.opToSwitch(code); |
| 888 | UidState uidState = getUidStateLocked(uid, false); |
| 889 | if (uidState != null && uidState.opModes != null) { |
| 890 | final int uidMode = uidState.opModes.get(code); |
| 891 | if (uidMode != AppOpsManager.MODE_ALLOWED) { |
| 892 | return uidMode; |
| 893 | } |
| 894 | } |
| 895 | Op op = getOpLocked(code, uid, packageName, false); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 896 | if (op == null) { |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 897 | return AppOpsManager.opToDefaultMode(code); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 898 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 899 | return op.mode; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 900 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | @Override |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 904 | public int checkAudioOperation(int code, int usage, int uid, String packageName) { |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 905 | if (isPackageSuspendedForUser(packageName, uid)) { |
Andrei Stingaceanu | 2bc2feb | 2016-02-11 16:23:49 +0000 | [diff] [blame] | 906 | Log.i(TAG, "Audio disabled for suspended package=" + packageName + " for uid=" + uid); |
| 907 | return AppOpsManager.MODE_IGNORED; |
| 908 | } |
| 909 | |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 910 | synchronized (this) { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 911 | final int mode = checkRestrictionLocked(code, usage, uid, packageName); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 912 | if (mode != AppOpsManager.MODE_ALLOWED) { |
| 913 | return mode; |
| 914 | } |
| 915 | } |
| 916 | return checkOperation(code, uid, packageName); |
| 917 | } |
| 918 | |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 919 | private boolean isPackageSuspendedForUser(String pkg, int uid) { |
Andrei Stingaceanu | 2bc2feb | 2016-02-11 16:23:49 +0000 | [diff] [blame] | 920 | try { |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 921 | return AppGlobals.getPackageManager().isPackageSuspendedForUser( |
| 922 | pkg, UserHandle.getUserId(uid)); |
Andrei Stingaceanu | 2bc2feb | 2016-02-11 16:23:49 +0000 | [diff] [blame] | 923 | } catch (RemoteException re) { |
| 924 | throw new SecurityException("Could not talk to package manager service"); |
| 925 | } |
Andrei Stingaceanu | 2bc2feb | 2016-02-11 16:23:49 +0000 | [diff] [blame] | 926 | } |
| 927 | |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 928 | private int checkRestrictionLocked(int code, int usage, int uid, String packageName) { |
| 929 | final SparseArray<Restriction> usageRestrictions = mAudioRestrictions.get(code); |
| 930 | if (usageRestrictions != null) { |
| 931 | final Restriction r = usageRestrictions.get(usage); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 932 | if (r != null && !r.exceptionPackages.contains(packageName)) { |
| 933 | return r.mode; |
| 934 | } |
| 935 | } |
| 936 | return AppOpsManager.MODE_ALLOWED; |
| 937 | } |
| 938 | |
| 939 | @Override |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 940 | public void setAudioRestriction(int code, int usage, int uid, int mode, |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 941 | String[] exceptionPackages) { |
| 942 | verifyIncomingUid(uid); |
| 943 | verifyIncomingOp(code); |
| 944 | synchronized (this) { |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 945 | SparseArray<Restriction> usageRestrictions = mAudioRestrictions.get(code); |
| 946 | if (usageRestrictions == null) { |
| 947 | usageRestrictions = new SparseArray<Restriction>(); |
| 948 | mAudioRestrictions.put(code, usageRestrictions); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 949 | } |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 950 | usageRestrictions.remove(usage); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 951 | if (mode != AppOpsManager.MODE_ALLOWED) { |
| 952 | final Restriction r = new Restriction(); |
| 953 | r.mode = mode; |
| 954 | if (exceptionPackages != null) { |
| 955 | final int N = exceptionPackages.length; |
| 956 | r.exceptionPackages = new ArraySet<String>(N); |
| 957 | for (int i = 0; i < N; i++) { |
| 958 | final String pkg = exceptionPackages[i]; |
| 959 | if (pkg != null) { |
| 960 | r.exceptionPackages.add(pkg.trim()); |
| 961 | } |
| 962 | } |
| 963 | } |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 964 | usageRestrictions.put(usage, r); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 965 | } |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | @Override |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 970 | public int checkPackage(int uid, String packageName) { |
| 971 | synchronized (this) { |
Dianne Hackborn | 0fcef84 | 2014-09-12 15:38:33 -0700 | [diff] [blame] | 972 | if (getOpsRawLocked(uid, packageName, true) != null) { |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 973 | return AppOpsManager.MODE_ALLOWED; |
| 974 | } else { |
| 975 | return AppOpsManager.MODE_ERRORED; |
| 976 | } |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | @Override |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 981 | public int noteProxyOperation(int code, String proxyPackageName, |
| 982 | int proxiedUid, String proxiedPackageName) { |
| 983 | verifyIncomingOp(code); |
| 984 | final int proxyMode = noteOperationUnchecked(code, Binder.getCallingUid(), |
| 985 | proxyPackageName, -1, null); |
| 986 | if (proxyMode != AppOpsManager.MODE_ALLOWED || Binder.getCallingUid() == proxiedUid) { |
| 987 | return proxyMode; |
| 988 | } |
| 989 | return noteOperationUnchecked(code, proxiedUid, proxiedPackageName, |
| 990 | Binder.getCallingUid(), proxyPackageName); |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | @Override |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 994 | public int noteOperation(int code, int uid, String packageName) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 995 | verifyIncomingUid(uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 996 | verifyIncomingOp(code); |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 997 | return noteOperationUnchecked(code, uid, packageName, 0, null); |
| 998 | } |
| 999 | |
| 1000 | private int noteOperationUnchecked(int code, int uid, String packageName, |
| 1001 | int proxyUid, String proxyPackageName) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1002 | synchronized (this) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1003 | Ops ops = getOpsLocked(uid, packageName, true); |
| 1004 | if (ops == null) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1005 | if (DEBUG) Log.d(TAG, "noteOperation: no op for code " + code + " uid " + uid |
| 1006 | + " package " + packageName); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 1007 | return AppOpsManager.MODE_ERRORED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1008 | } |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1009 | Op op = getOpLocked(ops, code, true); |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1010 | if (isOpRestricted(uid, code, packageName)) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 1011 | return AppOpsManager.MODE_IGNORED; |
| 1012 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1013 | if (op.duration == -1) { |
| 1014 | Slog.w(TAG, "Noting op not finished: uid " + uid + " pkg " + packageName |
| 1015 | + " code " + code + " time=" + op.time + " duration=" + op.duration); |
| 1016 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1017 | op.duration = 0; |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1018 | final int switchCode = AppOpsManager.opToSwitch(code); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1019 | UidState uidState = ops.uidState; |
| 1020 | if (uidState.opModes != null) { |
| 1021 | final int uidMode = uidState.opModes.get(switchCode); |
| 1022 | if (uidMode != AppOpsManager.MODE_ALLOWED) { |
| 1023 | if (DEBUG) Log.d(TAG, "noteOperation: reject #" + op.mode + " for code " |
| 1024 | + switchCode + " (" + code + ") uid " + uid + " package " |
| 1025 | + packageName); |
| 1026 | op.rejectTime = System.currentTimeMillis(); |
| 1027 | return uidMode; |
| 1028 | } |
| 1029 | } |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1030 | final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op; |
| 1031 | if (switchOp.mode != AppOpsManager.MODE_ALLOWED) { |
| 1032 | if (DEBUG) Log.d(TAG, "noteOperation: reject #" + op.mode + " for code " |
| 1033 | + switchCode + " (" + code + ") uid " + uid + " package " + packageName); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1034 | op.rejectTime = System.currentTimeMillis(); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1035 | return switchOp.mode; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1036 | } |
| 1037 | if (DEBUG) Log.d(TAG, "noteOperation: allowing code " + code + " uid " + uid |
| 1038 | + " package " + packageName); |
| 1039 | op.time = System.currentTimeMillis(); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1040 | op.rejectTime = 0; |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1041 | op.proxyUid = proxyUid; |
| 1042 | op.proxyPackageName = proxyPackageName; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1043 | return AppOpsManager.MODE_ALLOWED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1044 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | @Override |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1048 | public int startOperation(IBinder token, int code, int uid, String packageName) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1049 | verifyIncomingUid(uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 1050 | verifyIncomingOp(code); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1051 | ClientState client = (ClientState)token; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1052 | synchronized (this) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1053 | Ops ops = getOpsLocked(uid, packageName, true); |
| 1054 | if (ops == null) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1055 | if (DEBUG) Log.d(TAG, "startOperation: no op for code " + code + " uid " + uid |
| 1056 | + " package " + packageName); |
Jeff Sharkey | 911d7f4 | 2013-09-05 18:11:45 -0700 | [diff] [blame] | 1057 | return AppOpsManager.MODE_ERRORED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1058 | } |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1059 | Op op = getOpLocked(ops, code, true); |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1060 | if (isOpRestricted(uid, code, packageName)) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 1061 | return AppOpsManager.MODE_IGNORED; |
| 1062 | } |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1063 | final int switchCode = AppOpsManager.opToSwitch(code); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1064 | UidState uidState = ops.uidState; |
| 1065 | if (uidState.opModes != null) { |
| 1066 | final int uidMode = uidState.opModes.get(switchCode); |
| 1067 | if (uidMode != AppOpsManager.MODE_ALLOWED) { |
| 1068 | if (DEBUG) Log.d(TAG, "noteOperation: reject #" + op.mode + " for code " |
| 1069 | + switchCode + " (" + code + ") uid " + uid + " package " |
| 1070 | + packageName); |
| 1071 | op.rejectTime = System.currentTimeMillis(); |
| 1072 | return uidMode; |
| 1073 | } |
| 1074 | } |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1075 | final Op switchOp = switchCode != code ? getOpLocked(ops, switchCode, true) : op; |
| 1076 | if (switchOp.mode != AppOpsManager.MODE_ALLOWED) { |
| 1077 | if (DEBUG) Log.d(TAG, "startOperation: reject #" + op.mode + " for code " |
| 1078 | + switchCode + " (" + code + ") uid " + uid + " package " + packageName); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1079 | op.rejectTime = System.currentTimeMillis(); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1080 | return switchOp.mode; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1081 | } |
| 1082 | if (DEBUG) Log.d(TAG, "startOperation: allowing code " + code + " uid " + uid |
| 1083 | + " package " + packageName); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1084 | if (op.nesting == 0) { |
| 1085 | op.time = System.currentTimeMillis(); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1086 | op.rejectTime = 0; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1087 | op.duration = -1; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1088 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1089 | op.nesting++; |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1090 | if (client.mStartedOps != null) { |
| 1091 | client.mStartedOps.add(op); |
| 1092 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1093 | return AppOpsManager.MODE_ALLOWED; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1094 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | @Override |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1098 | public void finishOperation(IBinder token, int code, int uid, String packageName) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1099 | verifyIncomingUid(uid); |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 1100 | verifyIncomingOp(code); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1101 | ClientState client = (ClientState)token; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1102 | synchronized (this) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1103 | Op op = getOpLocked(code, uid, packageName, true); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1104 | if (op == null) { |
| 1105 | return; |
| 1106 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1107 | if (client.mStartedOps != null) { |
| 1108 | if (!client.mStartedOps.remove(op)) { |
| 1109 | throw new IllegalStateException("Operation not started: uid" + op.uid |
| 1110 | + " pkg=" + op.packageName + " op=" + op.op); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1111 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1112 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1113 | finishOperationLocked(op); |
| 1114 | } |
| 1115 | } |
| 1116 | |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 1117 | @Override |
| 1118 | public int permissionToOpCode(String permission) { |
| 1119 | return AppOpsManager.permissionToOpCode(permission); |
| 1120 | } |
| 1121 | |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1122 | void finishOperationLocked(Op op) { |
| 1123 | if (op.nesting <= 1) { |
| 1124 | if (op.nesting == 1) { |
| 1125 | op.duration = (int)(System.currentTimeMillis() - op.time); |
| 1126 | op.time += op.duration; |
| 1127 | } else { |
| 1128 | Slog.w(TAG, "Finishing op nesting under-run: uid " + op.uid + " pkg " |
| 1129 | + op.packageName + " code " + op.op + " time=" + op.time |
| 1130 | + " duration=" + op.duration + " nesting=" + op.nesting); |
| 1131 | } |
| 1132 | op.nesting = 0; |
| 1133 | } else { |
| 1134 | op.nesting--; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1135 | } |
| 1136 | } |
| 1137 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1138 | private void verifyIncomingUid(int uid) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1139 | if (uid == Binder.getCallingUid()) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1140 | return; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1141 | } |
| 1142 | if (Binder.getCallingPid() == Process.myPid()) { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1143 | return; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1144 | } |
| 1145 | mContext.enforcePermission(android.Manifest.permission.UPDATE_APP_OPS_STATS, |
| 1146 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1147 | } |
| 1148 | |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 1149 | private void verifyIncomingOp(int op) { |
| 1150 | if (op >= 0 && op < AppOpsManager._NUM_OP) { |
| 1151 | return; |
| 1152 | } |
| 1153 | throw new IllegalArgumentException("Bad operation #" + op); |
| 1154 | } |
| 1155 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1156 | private UidState getUidStateLocked(int uid, boolean edit) { |
| 1157 | UidState uidState = mUidStates.get(uid); |
| 1158 | if (uidState == null) { |
| 1159 | if (!edit) { |
| 1160 | return null; |
| 1161 | } |
| 1162 | uidState = new UidState(uid); |
| 1163 | mUidStates.put(uid, uidState); |
| 1164 | } |
| 1165 | return uidState; |
| 1166 | } |
| 1167 | |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 1168 | private Ops getOpsLocked(int uid, String packageName, boolean edit) { |
Dianne Hackborn | 0fcef84 | 2014-09-12 15:38:33 -0700 | [diff] [blame] | 1169 | if (uid == 0) { |
| 1170 | packageName = "root"; |
| 1171 | } else if (uid == Process.SHELL_UID) { |
| 1172 | packageName = "com.android.shell"; |
| 1173 | } |
| 1174 | return getOpsRawLocked(uid, packageName, edit); |
| 1175 | } |
| 1176 | |
| 1177 | private Ops getOpsRawLocked(int uid, String packageName, boolean edit) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1178 | UidState uidState = getUidStateLocked(uid, edit); |
| 1179 | if (uidState == null) { |
| 1180 | return null; |
| 1181 | } |
| 1182 | |
| 1183 | if (uidState.pkgOps == null) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1184 | if (!edit) { |
| 1185 | return null; |
| 1186 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1187 | uidState.pkgOps = new ArrayMap<>(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1188 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1189 | |
| 1190 | Ops ops = uidState.pkgOps.get(packageName); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1191 | if (ops == null) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1192 | if (!edit) { |
| 1193 | return null; |
| 1194 | } |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1195 | boolean isPrivileged = false; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1196 | // This is the first time we have seen this package name under this uid, |
| 1197 | // so let's make sure it is valid. |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1198 | if (uid != 0) { |
| 1199 | final long ident = Binder.clearCallingIdentity(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1200 | try { |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1201 | int pkgUid = -1; |
| 1202 | try { |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1203 | ApplicationInfo appInfo = ActivityThread.getPackageManager() |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 1204 | .getApplicationInfo(packageName, |
| 1205 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 1206 | UserHandle.getUserId(uid)); |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1207 | if (appInfo != null) { |
| 1208 | pkgUid = appInfo.uid; |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 1209 | isPrivileged = (appInfo.privateFlags |
| 1210 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1211 | } else { |
| 1212 | if ("media".equals(packageName)) { |
| 1213 | pkgUid = Process.MEDIA_UID; |
| 1214 | isPrivileged = false; |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 1215 | } else if ("audioserver".equals(packageName)) { |
| 1216 | pkgUid = Process.AUDIOSERVER_UID; |
| 1217 | isPrivileged = false; |
Chien-Yu Chen | 75cade0 | 2016-01-11 10:56:21 -0800 | [diff] [blame] | 1218 | } else if ("cameraserver".equals(packageName)) { |
| 1219 | pkgUid = Process.CAMERASERVER_UID; |
| 1220 | isPrivileged = false; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1221 | } |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 1222 | } |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1223 | } catch (RemoteException e) { |
| 1224 | Slog.w(TAG, "Could not contact PackageManager", e); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1225 | } |
| 1226 | if (pkgUid != uid) { |
| 1227 | // Oops! The package name is not valid for the uid they are calling |
| 1228 | // under. Abort. |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1229 | RuntimeException ex = new RuntimeException("here"); |
| 1230 | ex.fillInStackTrace(); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1231 | Slog.w(TAG, "Bad call: specified package " + packageName |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1232 | + " under uid " + uid + " but it is really " + pkgUid, ex); |
Dianne Hackborn | 514074f | 2013-02-11 10:52:46 -0800 | [diff] [blame] | 1233 | return null; |
| 1234 | } |
| 1235 | } finally { |
| 1236 | Binder.restoreCallingIdentity(ident); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1237 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1238 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1239 | ops = new Ops(packageName, uidState, isPrivileged); |
| 1240 | uidState.pkgOps.put(packageName, ops); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1241 | } |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 1242 | return ops; |
| 1243 | } |
| 1244 | |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1245 | private void scheduleWriteLocked() { |
| 1246 | if (!mWriteScheduled) { |
| 1247 | mWriteScheduled = true; |
| 1248 | mHandler.postDelayed(mWriteRunner, WRITE_DELAY); |
| 1249 | } |
| 1250 | } |
| 1251 | |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 1252 | private void scheduleFastWriteLocked() { |
| 1253 | if (!mFastWriteScheduled) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1254 | mWriteScheduled = true; |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 1255 | mFastWriteScheduled = true; |
| 1256 | mHandler.removeCallbacks(mWriteRunner); |
| 1257 | mHandler.postDelayed(mWriteRunner, 10*1000); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1258 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1259 | } |
| 1260 | |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 1261 | private Op getOpLocked(int code, int uid, String packageName, boolean edit) { |
| 1262 | Ops ops = getOpsLocked(uid, packageName, edit); |
| 1263 | if (ops == null) { |
| 1264 | return null; |
| 1265 | } |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1266 | return getOpLocked(ops, code, edit); |
| 1267 | } |
| 1268 | |
| 1269 | private Op getOpLocked(Ops ops, int code, boolean edit) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1270 | Op op = ops.get(code); |
| 1271 | if (op == null) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1272 | if (!edit) { |
| 1273 | return null; |
| 1274 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1275 | op = new Op(ops.uidState.uid, ops.packageName, code); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1276 | ops.put(code, op); |
| 1277 | } |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1278 | if (edit) { |
| 1279 | scheduleWriteLocked(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1280 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1281 | return op; |
| 1282 | } |
| 1283 | |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1284 | private boolean isOpRestricted(int uid, int code, String packageName) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 1285 | int userHandle = UserHandle.getUserId(uid); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 1286 | final int restrictionSetCount = mOpUserRestrictions.size(); |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 1287 | |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 1288 | for (int i = 0; i < restrictionSetCount; i++) { |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 1289 | // For each client, check that the given op is not restricted, or that the given |
| 1290 | // package is exempt from the restriction. |
| 1291 | |
| 1292 | SparseArray<Pair<boolean[],SparseArray<ArraySet<String>>>> perUserRestrictions = |
| 1293 | mOpUserRestrictions.valueAt(i); |
| 1294 | |
| 1295 | Pair<boolean[],SparseArray<ArraySet<String>>> restrictions = |
| 1296 | perUserRestrictions.get(userHandle); |
| 1297 | if (restrictions == null) { |
| 1298 | continue; // No restrictions set by this client |
| 1299 | } |
| 1300 | |
| 1301 | boolean[] opRestrictions = restrictions.first; |
| 1302 | SparseArray<ArraySet<String>> opExceptions = restrictions.second; |
| 1303 | |
| 1304 | if (opRestrictions == null) { |
| 1305 | continue; // No restrictions set by this client |
| 1306 | } |
| 1307 | |
| 1308 | if (opRestrictions[code]) { |
Ruben Brunk | 32f0fa4 | 2016-03-11 19:07:07 -0800 | [diff] [blame^] | 1309 | |
| 1310 | if (opExceptions != null) { |
| 1311 | ArraySet<String> ex = opExceptions.get(code); |
| 1312 | if (ex != null && ex.contains(packageName)) { |
| 1313 | continue; // AppOps code is restricted, but this package is exempt |
| 1314 | } |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 1315 | } |
| 1316 | |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 1317 | if (AppOpsManager.opAllowSystemBypassRestriction(code)) { |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 1318 | // If we are the system, bypass user restrictions for certain codes |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 1319 | synchronized (this) { |
| 1320 | Ops ops = getOpsLocked(uid, packageName, true); |
| 1321 | if ((ops != null) && ops.isPrivileged) { |
| 1322 | return false; |
| 1323 | } |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1324 | } |
| 1325 | } |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 1326 | |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 1327 | return true; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1328 | } |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 1329 | } |
| 1330 | return false; |
| 1331 | } |
| 1332 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1333 | void readState() { |
| 1334 | synchronized (mFile) { |
| 1335 | synchronized (this) { |
| 1336 | FileInputStream stream; |
| 1337 | try { |
| 1338 | stream = mFile.openRead(); |
| 1339 | } catch (FileNotFoundException e) { |
| 1340 | Slog.i(TAG, "No existing app ops " + mFile.getBaseFile() + "; starting empty"); |
| 1341 | return; |
| 1342 | } |
| 1343 | boolean success = false; |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 1344 | mUidStates.clear(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1345 | try { |
| 1346 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1347 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1348 | int type; |
| 1349 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 1350 | && type != XmlPullParser.END_DOCUMENT) { |
| 1351 | ; |
| 1352 | } |
| 1353 | |
| 1354 | if (type != XmlPullParser.START_TAG) { |
| 1355 | throw new IllegalStateException("no start tag found"); |
| 1356 | } |
| 1357 | |
| 1358 | int outerDepth = parser.getDepth(); |
| 1359 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1360 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1361 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1362 | continue; |
| 1363 | } |
| 1364 | |
| 1365 | String tagName = parser.getName(); |
| 1366 | if (tagName.equals("pkg")) { |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 1367 | readPackage(parser); |
Svetoslav | 215b44a | 2015-08-04 19:03:40 -0700 | [diff] [blame] | 1368 | } else if (tagName.equals("uid")) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1369 | readUidOps(parser); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1370 | } else { |
| 1371 | Slog.w(TAG, "Unknown element under <app-ops>: " |
| 1372 | + parser.getName()); |
| 1373 | XmlUtils.skipCurrentTag(parser); |
| 1374 | } |
| 1375 | } |
| 1376 | success = true; |
| 1377 | } catch (IllegalStateException e) { |
| 1378 | Slog.w(TAG, "Failed parsing " + e); |
| 1379 | } catch (NullPointerException e) { |
| 1380 | Slog.w(TAG, "Failed parsing " + e); |
| 1381 | } catch (NumberFormatException e) { |
| 1382 | Slog.w(TAG, "Failed parsing " + e); |
| 1383 | } catch (XmlPullParserException e) { |
| 1384 | Slog.w(TAG, "Failed parsing " + e); |
| 1385 | } catch (IOException e) { |
| 1386 | Slog.w(TAG, "Failed parsing " + e); |
| 1387 | } catch (IndexOutOfBoundsException e) { |
| 1388 | Slog.w(TAG, "Failed parsing " + e); |
| 1389 | } finally { |
| 1390 | if (!success) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1391 | mUidStates.clear(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1392 | } |
| 1393 | try { |
| 1394 | stream.close(); |
| 1395 | } catch (IOException e) { |
| 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1402 | void readUidOps(XmlPullParser parser) throws NumberFormatException, |
| 1403 | XmlPullParserException, IOException { |
| 1404 | final int uid = Integer.parseInt(parser.getAttributeValue(null, "n")); |
| 1405 | int outerDepth = parser.getDepth(); |
| 1406 | int type; |
| 1407 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1408 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1409 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1410 | continue; |
| 1411 | } |
| 1412 | |
| 1413 | String tagName = parser.getName(); |
| 1414 | if (tagName.equals("op")) { |
| 1415 | final int code = Integer.parseInt(parser.getAttributeValue(null, "n")); |
| 1416 | final int mode = Integer.parseInt(parser.getAttributeValue(null, "m")); |
| 1417 | UidState uidState = getUidStateLocked(uid, true); |
| 1418 | if (uidState.opModes == null) { |
| 1419 | uidState.opModes = new SparseIntArray(); |
| 1420 | } |
| 1421 | uidState.opModes.put(code, mode); |
| 1422 | } else { |
| 1423 | Slog.w(TAG, "Unknown element under <uid-ops>: " |
| 1424 | + parser.getName()); |
| 1425 | XmlUtils.skipCurrentTag(parser); |
| 1426 | } |
| 1427 | } |
| 1428 | } |
| 1429 | |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 1430 | void readPackage(XmlPullParser parser) throws NumberFormatException, |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1431 | XmlPullParserException, IOException { |
| 1432 | String pkgName = parser.getAttributeValue(null, "n"); |
| 1433 | int outerDepth = parser.getDepth(); |
| 1434 | int type; |
| 1435 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1436 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1437 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1438 | continue; |
| 1439 | } |
| 1440 | |
| 1441 | String tagName = parser.getName(); |
| 1442 | if (tagName.equals("uid")) { |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 1443 | readUid(parser, pkgName); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1444 | } else { |
| 1445 | Slog.w(TAG, "Unknown element under <pkg>: " |
| 1446 | + parser.getName()); |
| 1447 | XmlUtils.skipCurrentTag(parser); |
| 1448 | } |
| 1449 | } |
| 1450 | } |
| 1451 | |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 1452 | void readUid(XmlPullParser parser, String pkgName) throws NumberFormatException, |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1453 | XmlPullParserException, IOException { |
| 1454 | int uid = Integer.parseInt(parser.getAttributeValue(null, "n")); |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1455 | String isPrivilegedString = parser.getAttributeValue(null, "p"); |
| 1456 | boolean isPrivileged = false; |
| 1457 | if (isPrivilegedString == null) { |
| 1458 | try { |
| 1459 | IPackageManager packageManager = ActivityThread.getPackageManager(); |
| 1460 | if (packageManager != null) { |
| 1461 | ApplicationInfo appInfo = ActivityThread.getPackageManager() |
| 1462 | .getApplicationInfo(pkgName, 0, UserHandle.getUserId(uid)); |
| 1463 | if (appInfo != null) { |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 1464 | isPrivileged = (appInfo.privateFlags |
| 1465 | & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0; |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1466 | } |
| 1467 | } else { |
| 1468 | // Could not load data, don't add to cache so it will be loaded later. |
| 1469 | return; |
| 1470 | } |
| 1471 | } catch (RemoteException e) { |
| 1472 | Slog.w(TAG, "Could not contact PackageManager", e); |
| 1473 | } |
| 1474 | } else { |
| 1475 | isPrivileged = Boolean.parseBoolean(isPrivilegedString); |
| 1476 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1477 | int outerDepth = parser.getDepth(); |
| 1478 | int type; |
| 1479 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1480 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1481 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1482 | continue; |
| 1483 | } |
| 1484 | |
| 1485 | String tagName = parser.getName(); |
| 1486 | if (tagName.equals("op")) { |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1487 | Op op = new Op(uid, pkgName, Integer.parseInt(parser.getAttributeValue(null, "n"))); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1488 | String mode = parser.getAttributeValue(null, "m"); |
| 1489 | if (mode != null) { |
Dave Burke | 0997c5bd | 2013-08-02 20:25:02 +0000 | [diff] [blame] | 1490 | op.mode = Integer.parseInt(mode); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1491 | } |
| 1492 | String time = parser.getAttributeValue(null, "t"); |
| 1493 | if (time != null) { |
| 1494 | op.time = Long.parseLong(time); |
| 1495 | } |
| 1496 | time = parser.getAttributeValue(null, "r"); |
| 1497 | if (time != null) { |
| 1498 | op.rejectTime = Long.parseLong(time); |
| 1499 | } |
| 1500 | String dur = parser.getAttributeValue(null, "d"); |
| 1501 | if (dur != null) { |
| 1502 | op.duration = Integer.parseInt(dur); |
| 1503 | } |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1504 | String proxyUid = parser.getAttributeValue(null, "pu"); |
| 1505 | if (proxyUid != null) { |
| 1506 | op.proxyUid = Integer.parseInt(proxyUid); |
| 1507 | } |
| 1508 | String proxyPackageName = parser.getAttributeValue(null, "pp"); |
| 1509 | if (proxyPackageName != null) { |
| 1510 | op.proxyPackageName = proxyPackageName; |
| 1511 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1512 | |
| 1513 | UidState uidState = getUidStateLocked(uid, true); |
| 1514 | if (uidState.pkgOps == null) { |
| 1515 | uidState.pkgOps = new ArrayMap<>(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1516 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1517 | |
| 1518 | Ops ops = uidState.pkgOps.get(pkgName); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1519 | if (ops == null) { |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1520 | ops = new Ops(pkgName, uidState, isPrivileged); |
| 1521 | uidState.pkgOps.put(pkgName, ops); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1522 | } |
| 1523 | ops.put(op.op, op); |
| 1524 | } else { |
| 1525 | Slog.w(TAG, "Unknown element under <pkg>: " |
| 1526 | + parser.getName()); |
| 1527 | XmlUtils.skipCurrentTag(parser); |
| 1528 | } |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | void writeState() { |
| 1533 | synchronized (mFile) { |
| 1534 | List<AppOpsManager.PackageOps> allOps = getPackagesForOps(null); |
| 1535 | |
| 1536 | FileOutputStream stream; |
| 1537 | try { |
| 1538 | stream = mFile.startWrite(); |
| 1539 | } catch (IOException e) { |
| 1540 | Slog.w(TAG, "Failed to write state: " + e); |
| 1541 | return; |
| 1542 | } |
| 1543 | |
| 1544 | try { |
| 1545 | XmlSerializer out = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1546 | out.setOutput(stream, StandardCharsets.UTF_8.name()); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1547 | out.startDocument(null, true); |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 1548 | out.startTag(null, "app-ops"); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 1549 | |
| 1550 | final int uidStateCount = mUidStates.size(); |
| 1551 | for (int i = 0; i < uidStateCount; i++) { |
| 1552 | UidState uidState = mUidStates.valueAt(i); |
| 1553 | if (uidState.opModes != null && uidState.opModes.size() > 0) { |
| 1554 | out.startTag(null, "uid"); |
| 1555 | out.attribute(null, "n", Integer.toString(uidState.uid)); |
| 1556 | SparseIntArray uidOpModes = uidState.opModes; |
| 1557 | final int opCount = uidOpModes.size(); |
| 1558 | for (int j = 0; j < opCount; j++) { |
| 1559 | final int op = uidOpModes.keyAt(j); |
| 1560 | final int mode = uidOpModes.valueAt(j); |
| 1561 | out.startTag(null, "op"); |
| 1562 | out.attribute(null, "n", Integer.toString(op)); |
| 1563 | out.attribute(null, "m", Integer.toString(mode)); |
| 1564 | out.endTag(null, "op"); |
| 1565 | } |
| 1566 | out.endTag(null, "uid"); |
| 1567 | } |
| 1568 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1569 | |
| 1570 | if (allOps != null) { |
| 1571 | String lastPkg = null; |
| 1572 | for (int i=0; i<allOps.size(); i++) { |
| 1573 | AppOpsManager.PackageOps pkg = allOps.get(i); |
| 1574 | if (!pkg.getPackageName().equals(lastPkg)) { |
| 1575 | if (lastPkg != null) { |
| 1576 | out.endTag(null, "pkg"); |
| 1577 | } |
| 1578 | lastPkg = pkg.getPackageName(); |
| 1579 | out.startTag(null, "pkg"); |
| 1580 | out.attribute(null, "n", lastPkg); |
| 1581 | } |
| 1582 | out.startTag(null, "uid"); |
| 1583 | out.attribute(null, "n", Integer.toString(pkg.getUid())); |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1584 | synchronized (this) { |
| 1585 | Ops ops = getOpsLocked(pkg.getUid(), pkg.getPackageName(), false); |
| 1586 | // Should always be present as the list of PackageOps is generated |
| 1587 | // from Ops. |
| 1588 | if (ops != null) { |
| 1589 | out.attribute(null, "p", Boolean.toString(ops.isPrivileged)); |
| 1590 | } else { |
| 1591 | out.attribute(null, "p", Boolean.toString(false)); |
| 1592 | } |
| 1593 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1594 | List<AppOpsManager.OpEntry> ops = pkg.getOps(); |
| 1595 | for (int j=0; j<ops.size(); j++) { |
| 1596 | AppOpsManager.OpEntry op = ops.get(j); |
| 1597 | out.startTag(null, "op"); |
| 1598 | out.attribute(null, "n", Integer.toString(op.getOp())); |
David Braun | f5d8319 | 2013-09-16 13:43:51 -0700 | [diff] [blame] | 1599 | if (op.getMode() != AppOpsManager.opToDefaultMode(op.getOp())) { |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1600 | out.attribute(null, "m", Integer.toString(op.getMode())); |
| 1601 | } |
| 1602 | long time = op.getTime(); |
| 1603 | if (time != 0) { |
| 1604 | out.attribute(null, "t", Long.toString(time)); |
| 1605 | } |
| 1606 | time = op.getRejectTime(); |
| 1607 | if (time != 0) { |
| 1608 | out.attribute(null, "r", Long.toString(time)); |
| 1609 | } |
| 1610 | int dur = op.getDuration(); |
| 1611 | if (dur != 0) { |
| 1612 | out.attribute(null, "d", Integer.toString(dur)); |
| 1613 | } |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1614 | int proxyUid = op.getProxyUid(); |
| 1615 | if (proxyUid != -1) { |
| 1616 | out.attribute(null, "pu", Integer.toString(proxyUid)); |
| 1617 | } |
| 1618 | String proxyPackageName = op.getProxyPackageName(); |
| 1619 | if (proxyPackageName != null) { |
| 1620 | out.attribute(null, "pp", proxyPackageName); |
| 1621 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 1622 | out.endTag(null, "op"); |
| 1623 | } |
| 1624 | out.endTag(null, "uid"); |
| 1625 | } |
| 1626 | if (lastPkg != null) { |
| 1627 | out.endTag(null, "pkg"); |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | out.endTag(null, "app-ops"); |
| 1632 | out.endDocument(); |
| 1633 | mFile.finishWrite(stream); |
| 1634 | } catch (IOException e) { |
| 1635 | Slog.w(TAG, "Failed to write state, restoring backup.", e); |
| 1636 | mFile.failWrite(stream); |
| 1637 | } |
| 1638 | } |
| 1639 | } |
| 1640 | |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 1641 | static class Shell extends ShellCommand { |
| 1642 | final IAppOpsService mInterface; |
| 1643 | final AppOpsService mInternal; |
| 1644 | |
| 1645 | int userId = UserHandle.USER_SYSTEM; |
| 1646 | String packageName; |
| 1647 | String opStr; |
| 1648 | int op; |
| 1649 | int packageUid; |
| 1650 | |
| 1651 | Shell(IAppOpsService iface, AppOpsService internal) { |
| 1652 | mInterface = iface; |
| 1653 | mInternal = internal; |
| 1654 | } |
| 1655 | |
| 1656 | @Override |
| 1657 | public int onCommand(String cmd) { |
| 1658 | return onShellCommand(this, cmd); |
| 1659 | } |
| 1660 | |
| 1661 | @Override |
| 1662 | public void onHelp() { |
| 1663 | PrintWriter pw = getOutPrintWriter(); |
| 1664 | dumpCommandHelp(pw); |
| 1665 | } |
| 1666 | |
| 1667 | private int strOpToOp(String op, PrintWriter err) { |
| 1668 | try { |
| 1669 | return AppOpsManager.strOpToOp(op); |
| 1670 | } catch (IllegalArgumentException e) { |
| 1671 | } |
| 1672 | try { |
| 1673 | return Integer.parseInt(op); |
| 1674 | } catch (NumberFormatException e) { |
| 1675 | } |
| 1676 | try { |
| 1677 | return AppOpsManager.strDebugOpToOp(op); |
| 1678 | } catch (IllegalArgumentException e) { |
| 1679 | err.println("Error: " + e.getMessage()); |
| 1680 | return -1; |
| 1681 | } |
| 1682 | } |
| 1683 | |
| 1684 | int parseUserPackageOp(boolean reqOp, PrintWriter err) throws RemoteException { |
| 1685 | userId = UserHandle.USER_CURRENT; |
| 1686 | packageName = null; |
| 1687 | opStr = null; |
| 1688 | for (String argument; (argument = getNextArg()) != null;) { |
| 1689 | if ("--user".equals(argument)) { |
| 1690 | userId = UserHandle.parseUserArg(getNextArgRequired()); |
| 1691 | } else { |
| 1692 | if (packageName == null) { |
| 1693 | packageName = argument; |
| 1694 | } else if (opStr == null) { |
| 1695 | opStr = argument; |
| 1696 | break; |
| 1697 | } |
| 1698 | } |
| 1699 | } |
| 1700 | if (packageName == null) { |
| 1701 | err.println("Error: Package name not specified."); |
| 1702 | return -1; |
| 1703 | } else if (opStr == null && reqOp) { |
| 1704 | err.println("Error: Operation not specified."); |
| 1705 | return -1; |
| 1706 | } |
| 1707 | if (opStr != null) { |
| 1708 | op = strOpToOp(opStr, err); |
| 1709 | if (op < 0) { |
| 1710 | return -1; |
| 1711 | } |
| 1712 | } else { |
| 1713 | op = AppOpsManager.OP_NONE; |
| 1714 | } |
| 1715 | if (userId == UserHandle.USER_CURRENT) { |
| 1716 | userId = ActivityManager.getCurrentUser(); |
| 1717 | } |
| 1718 | if ("root".equals(packageName)) { |
| 1719 | packageUid = 0; |
| 1720 | } else { |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 1721 | packageUid = AppGlobals.getPackageManager().getPackageUid(packageName, |
| 1722 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 1723 | } |
| 1724 | if (packageUid < 0) { |
| 1725 | err.println("Error: No UID for " + packageName + " in user " + userId); |
| 1726 | return -1; |
| 1727 | } |
| 1728 | return 0; |
| 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | @Override public void onShellCommand(FileDescriptor in, FileDescriptor out, |
| 1733 | FileDescriptor err, String[] args, ResultReceiver resultReceiver) { |
| 1734 | (new Shell(this, this)).exec(this, in, out, err, args, resultReceiver); |
| 1735 | } |
| 1736 | |
| 1737 | static void dumpCommandHelp(PrintWriter pw) { |
| 1738 | pw.println("AppOps service (appops) commands:"); |
| 1739 | pw.println(" help"); |
| 1740 | pw.println(" Print this help text."); |
| 1741 | pw.println(" set [--user <USER_ID>] <PACKAGE> <OP> <MODE>"); |
| 1742 | pw.println(" Set the mode for a particular application and operation."); |
| 1743 | pw.println(" get [--user <USER_ID>] <PACKAGE> [<OP>]"); |
| 1744 | pw.println(" Return the mode for a particular application and optional operation."); |
| 1745 | pw.println(" reset [--user <USER_ID>] [<PACKAGE>]"); |
| 1746 | pw.println(" Reset the given application or all applications to default modes."); |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 1747 | pw.println(" write-settings"); |
| 1748 | pw.println(" Immediately write pending changes to storage."); |
| 1749 | pw.println(" read-settings"); |
| 1750 | pw.println(" Read the last written settings, replacing current state in RAM."); |
Dianne Hackborn | 268e4e3 | 2015-11-18 16:29:56 -0800 | [diff] [blame] | 1751 | pw.println(" options:"); |
| 1752 | pw.println(" <PACKAGE> an Android package name."); |
| 1753 | pw.println(" <OP> an AppOps operation."); |
| 1754 | pw.println(" <MODE> one of allow, ignore, deny, or default"); |
| 1755 | pw.println(" <USER_ID> the user id under which the package is installed. If --user is not"); |
| 1756 | pw.println(" specified, the current user is assumed."); |
| 1757 | } |
| 1758 | |
| 1759 | static int onShellCommand(Shell shell, String cmd) { |
| 1760 | if (cmd == null) { |
| 1761 | return shell.handleDefaultCommands(cmd); |
| 1762 | } |
| 1763 | PrintWriter pw = shell.getOutPrintWriter(); |
| 1764 | PrintWriter err = shell.getErrPrintWriter(); |
| 1765 | try { |
| 1766 | switch (cmd) { |
| 1767 | case "set": { |
| 1768 | int res = shell.parseUserPackageOp(true, err); |
| 1769 | if (res < 0) { |
| 1770 | return res; |
| 1771 | } |
| 1772 | String modeStr = shell.getNextArg(); |
| 1773 | if (modeStr == null) { |
| 1774 | err.println("Error: Mode not specified."); |
| 1775 | return -1; |
| 1776 | } |
| 1777 | |
| 1778 | final int mode; |
| 1779 | switch (modeStr) { |
| 1780 | case "allow": |
| 1781 | mode = AppOpsManager.MODE_ALLOWED; |
| 1782 | break; |
| 1783 | case "deny": |
| 1784 | mode = AppOpsManager.MODE_ERRORED; |
| 1785 | break; |
| 1786 | case "ignore": |
| 1787 | mode = AppOpsManager.MODE_IGNORED; |
| 1788 | break; |
| 1789 | case "default": |
| 1790 | mode = AppOpsManager.MODE_DEFAULT; |
| 1791 | break; |
| 1792 | default: |
| 1793 | err.println("Error: Mode " + modeStr + " is not valid,"); |
| 1794 | return -1; |
| 1795 | } |
| 1796 | |
| 1797 | shell.mInterface.setMode(shell.op, shell.packageUid, shell.packageName, mode); |
| 1798 | return 0; |
| 1799 | } |
| 1800 | case "get": { |
| 1801 | int res = shell.parseUserPackageOp(false, err); |
| 1802 | if (res < 0) { |
| 1803 | return res; |
| 1804 | } |
| 1805 | |
| 1806 | List<AppOpsManager.PackageOps> ops = shell.mInterface.getOpsForPackage( |
| 1807 | shell.packageUid, shell.packageName, |
| 1808 | shell.op != AppOpsManager.OP_NONE ? new int[] {shell.op} : null); |
| 1809 | if (ops == null || ops.size() <= 0) { |
| 1810 | pw.println("No operations."); |
| 1811 | return 0; |
| 1812 | } |
| 1813 | final long now = System.currentTimeMillis(); |
| 1814 | for (int i=0; i<ops.size(); i++) { |
| 1815 | List<AppOpsManager.OpEntry> entries = ops.get(i).getOps(); |
| 1816 | for (int j=0; j<entries.size(); j++) { |
| 1817 | AppOpsManager.OpEntry ent = entries.get(j); |
| 1818 | pw.print(AppOpsManager.opToName(ent.getOp())); |
| 1819 | pw.print(": "); |
| 1820 | switch (ent.getMode()) { |
| 1821 | case AppOpsManager.MODE_ALLOWED: |
| 1822 | pw.print("allow"); |
| 1823 | break; |
| 1824 | case AppOpsManager.MODE_IGNORED: |
| 1825 | pw.print("ignore"); |
| 1826 | break; |
| 1827 | case AppOpsManager.MODE_ERRORED: |
| 1828 | pw.print("deny"); |
| 1829 | break; |
| 1830 | case AppOpsManager.MODE_DEFAULT: |
| 1831 | pw.print("default"); |
| 1832 | break; |
| 1833 | default: |
| 1834 | pw.print("mode="); |
| 1835 | pw.print(ent.getMode()); |
| 1836 | break; |
| 1837 | } |
| 1838 | if (ent.getTime() != 0) { |
| 1839 | pw.print("; time="); |
| 1840 | TimeUtils.formatDuration(now - ent.getTime(), pw); |
| 1841 | pw.print(" ago"); |
| 1842 | } |
| 1843 | if (ent.getRejectTime() != 0) { |
| 1844 | pw.print("; rejectTime="); |
| 1845 | TimeUtils.formatDuration(now - ent.getRejectTime(), pw); |
| 1846 | pw.print(" ago"); |
| 1847 | } |
| 1848 | if (ent.getDuration() == -1) { |
| 1849 | pw.print(" (running)"); |
| 1850 | } else if (ent.getDuration() != 0) { |
| 1851 | pw.print("; duration="); |
| 1852 | TimeUtils.formatDuration(ent.getDuration(), pw); |
| 1853 | } |
| 1854 | pw.println(); |
| 1855 | } |
| 1856 | } |
| 1857 | return 0; |
| 1858 | } |
| 1859 | case "reset": { |
| 1860 | String packageName = null; |
| 1861 | int userId = UserHandle.USER_CURRENT; |
| 1862 | for (String argument; (argument = shell.getNextArg()) != null;) { |
| 1863 | if ("--user".equals(argument)) { |
| 1864 | String userStr = shell.getNextArgRequired(); |
| 1865 | userId = UserHandle.parseUserArg(userStr); |
| 1866 | } else { |
| 1867 | if (packageName == null) { |
| 1868 | packageName = argument; |
| 1869 | } else { |
| 1870 | err.println("Error: Unsupported argument: " + argument); |
| 1871 | return -1; |
| 1872 | } |
| 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | if (userId == UserHandle.USER_CURRENT) { |
| 1877 | userId = ActivityManager.getCurrentUser(); |
| 1878 | } |
| 1879 | |
| 1880 | shell.mInterface.resetAllModes(userId, packageName); |
| 1881 | pw.print("Reset all modes for: "); |
| 1882 | if (userId == UserHandle.USER_ALL) { |
| 1883 | pw.print("all users"); |
| 1884 | } else { |
| 1885 | pw.print("user "); pw.print(userId); |
| 1886 | } |
| 1887 | pw.print(", "); |
| 1888 | if (packageName == null) { |
| 1889 | pw.println("all packages"); |
| 1890 | } else { |
| 1891 | pw.print("package "); pw.println(packageName); |
| 1892 | } |
| 1893 | return 0; |
| 1894 | } |
| 1895 | case "write-settings": { |
| 1896 | shell.mInternal.mContext.enforcePermission( |
| 1897 | android.Manifest.permission.UPDATE_APP_OPS_STATS, |
| 1898 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 1899 | long token = Binder.clearCallingIdentity(); |
| 1900 | try { |
| 1901 | synchronized (shell.mInternal) { |
| 1902 | shell.mInternal.mHandler.removeCallbacks(shell.mInternal.mWriteRunner); |
| 1903 | } |
| 1904 | shell.mInternal.writeState(); |
| 1905 | pw.println("Current settings written."); |
| 1906 | } finally { |
| 1907 | Binder.restoreCallingIdentity(token); |
| 1908 | } |
| 1909 | return 0; |
| 1910 | } |
| 1911 | case "read-settings": { |
| 1912 | shell.mInternal.mContext.enforcePermission( |
| 1913 | android.Manifest.permission.UPDATE_APP_OPS_STATS, |
| 1914 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 1915 | long token = Binder.clearCallingIdentity(); |
| 1916 | try { |
| 1917 | shell.mInternal.readState(); |
| 1918 | pw.println("Last settings read."); |
| 1919 | } finally { |
| 1920 | Binder.restoreCallingIdentity(token); |
| 1921 | } |
| 1922 | return 0; |
| 1923 | } |
| 1924 | default: |
| 1925 | return shell.handleDefaultCommands(cmd); |
| 1926 | } |
| 1927 | } catch (RemoteException e) { |
| 1928 | pw.println("Remote exception: " + e); |
| 1929 | } |
| 1930 | return -1; |
| 1931 | } |
| 1932 | |
| 1933 | private void dumpHelp(PrintWriter pw) { |
| 1934 | pw.println("AppOps service (appops) dump options:"); |
| 1935 | pw.println(" none"); |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 1936 | } |
| 1937 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1938 | @Override |
| 1939 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1940 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1941 | != PackageManager.PERMISSION_GRANTED) { |
| 1942 | pw.println("Permission Denial: can't dump ApOps service from from pid=" |
| 1943 | + Binder.getCallingPid() |
| 1944 | + ", uid=" + Binder.getCallingUid()); |
| 1945 | return; |
| 1946 | } |
| 1947 | |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 1948 | if (args != null) { |
| 1949 | for (int i=0; i<args.length; i++) { |
| 1950 | String arg = args[i]; |
| 1951 | if ("-h".equals(arg)) { |
| 1952 | dumpHelp(pw); |
| 1953 | return; |
Tim Kilbourn | 8f1ea83 | 2015-08-26 15:07:37 -0700 | [diff] [blame] | 1954 | } else if ("-a".equals(arg)) { |
| 1955 | // dump all data |
Dianne Hackborn | 4d34bb8 | 2015-08-07 18:26:38 -0700 | [diff] [blame] | 1956 | } else if (arg.length() > 0 && arg.charAt(0) == '-'){ |
| 1957 | pw.println("Unknown option: " + arg); |
| 1958 | return; |
| 1959 | } else { |
| 1960 | pw.println("Unknown command: " + arg); |
| 1961 | return; |
| 1962 | } |
| 1963 | } |
| 1964 | } |
| 1965 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 1966 | synchronized (this) { |
| 1967 | pw.println("Current AppOps Service state:"); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 1968 | final long now = System.currentTimeMillis(); |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 1969 | boolean needSep = false; |
| 1970 | if (mOpModeWatchers.size() > 0) { |
| 1971 | needSep = true; |
| 1972 | pw.println(" Op mode watchers:"); |
| 1973 | for (int i=0; i<mOpModeWatchers.size(); i++) { |
| 1974 | pw.print(" Op "); pw.print(AppOpsManager.opToName(mOpModeWatchers.keyAt(i))); |
| 1975 | pw.println(":"); |
| 1976 | ArrayList<Callback> callbacks = mOpModeWatchers.valueAt(i); |
| 1977 | for (int j=0; j<callbacks.size(); j++) { |
| 1978 | pw.print(" #"); pw.print(j); pw.print(": "); |
| 1979 | pw.println(callbacks.get(j)); |
| 1980 | } |
| 1981 | } |
| 1982 | } |
| 1983 | if (mPackageModeWatchers.size() > 0) { |
| 1984 | needSep = true; |
| 1985 | pw.println(" Package mode watchers:"); |
| 1986 | for (int i=0; i<mPackageModeWatchers.size(); i++) { |
| 1987 | pw.print(" Pkg "); pw.print(mPackageModeWatchers.keyAt(i)); |
| 1988 | pw.println(":"); |
| 1989 | ArrayList<Callback> callbacks = mPackageModeWatchers.valueAt(i); |
| 1990 | for (int j=0; j<callbacks.size(); j++) { |
| 1991 | pw.print(" #"); pw.print(j); pw.print(": "); |
| 1992 | pw.println(callbacks.get(j)); |
| 1993 | } |
| 1994 | } |
| 1995 | } |
| 1996 | if (mModeWatchers.size() > 0) { |
| 1997 | needSep = true; |
| 1998 | pw.println(" All mode watchers:"); |
| 1999 | for (int i=0; i<mModeWatchers.size(); i++) { |
| 2000 | pw.print(" "); pw.print(mModeWatchers.keyAt(i)); |
| 2001 | pw.print(" -> "); pw.println(mModeWatchers.valueAt(i)); |
| 2002 | } |
| 2003 | } |
| 2004 | if (mClients.size() > 0) { |
| 2005 | needSep = true; |
| 2006 | pw.println(" Clients:"); |
| 2007 | for (int i=0; i<mClients.size(); i++) { |
| 2008 | pw.print(" "); pw.print(mClients.keyAt(i)); pw.println(":"); |
| 2009 | ClientState cs = mClients.valueAt(i); |
| 2010 | pw.print(" "); pw.println(cs); |
| 2011 | if (cs.mStartedOps != null && cs.mStartedOps.size() > 0) { |
| 2012 | pw.println(" Started ops:"); |
| 2013 | for (int j=0; j<cs.mStartedOps.size(); j++) { |
| 2014 | Op op = cs.mStartedOps.get(j); |
| 2015 | pw.print(" "); pw.print("uid="); pw.print(op.uid); |
| 2016 | pw.print(" pkg="); pw.print(op.packageName); |
| 2017 | pw.print(" op="); pw.println(AppOpsManager.opToName(op.op)); |
| 2018 | } |
| 2019 | } |
| 2020 | } |
| 2021 | } |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 2022 | if (mAudioRestrictions.size() > 0) { |
| 2023 | boolean printedHeader = false; |
| 2024 | for (int o=0; o<mAudioRestrictions.size(); o++) { |
| 2025 | final String op = AppOpsManager.opToName(mAudioRestrictions.keyAt(o)); |
| 2026 | final SparseArray<Restriction> restrictions = mAudioRestrictions.valueAt(o); |
| 2027 | for (int i=0; i<restrictions.size(); i++) { |
| 2028 | if (!printedHeader){ |
| 2029 | pw.println(" Audio Restrictions:"); |
| 2030 | printedHeader = true; |
| 2031 | needSep = true; |
| 2032 | } |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 2033 | final int usage = restrictions.keyAt(i); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 2034 | pw.print(" "); pw.print(op); |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 2035 | pw.print(" usage="); pw.print(AudioAttributes.usageToString(usage)); |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 2036 | Restriction r = restrictions.valueAt(i); |
| 2037 | pw.print(": mode="); pw.println(r.mode); |
| 2038 | if (!r.exceptionPackages.isEmpty()) { |
| 2039 | pw.println(" Exceptions:"); |
| 2040 | for (int j=0; j<r.exceptionPackages.size(); j++) { |
| 2041 | pw.print(" "); pw.println(r.exceptionPackages.valueAt(j)); |
| 2042 | } |
| 2043 | } |
| 2044 | } |
| 2045 | } |
| 2046 | } |
Dianne Hackborn | e98f5db | 2013-07-17 17:23:25 -0700 | [diff] [blame] | 2047 | if (needSep) { |
| 2048 | pw.println(); |
| 2049 | } |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2050 | for (int i=0; i<mUidStates.size(); i++) { |
| 2051 | UidState uidState = mUidStates.valueAt(i); |
| 2052 | |
| 2053 | pw.print(" Uid "); UserHandle.formatUid(pw, uidState.uid); pw.println(":"); |
| 2054 | |
| 2055 | SparseIntArray opModes = uidState.opModes; |
| 2056 | if (opModes != null) { |
| 2057 | final int opModeCount = opModes.size(); |
| 2058 | for (int j = 0; j < opModeCount; j++) { |
| 2059 | final int code = opModes.keyAt(j); |
| 2060 | final int mode = opModes.valueAt(j); |
| 2061 | pw.print(" "); pw.print(AppOpsManager.opToName(code)); |
| 2062 | pw.print(": mode="); pw.println(mode); |
| 2063 | } |
| 2064 | } |
| 2065 | |
| 2066 | ArrayMap<String, Ops> pkgOps = uidState.pkgOps; |
| 2067 | if (pkgOps == null) { |
| 2068 | continue; |
| 2069 | } |
| 2070 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2071 | for (Ops ops : pkgOps.values()) { |
| 2072 | pw.print(" Package "); pw.print(ops.packageName); pw.println(":"); |
| 2073 | for (int j=0; j<ops.size(); j++) { |
| 2074 | Op op = ops.valueAt(j); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 2075 | pw.print(" "); pw.print(AppOpsManager.opToName(op.op)); |
| 2076 | pw.print(": mode="); pw.print(op.mode); |
| 2077 | if (op.time != 0) { |
| 2078 | pw.print("; time="); TimeUtils.formatDuration(now-op.time, pw); |
| 2079 | pw.print(" ago"); |
| 2080 | } |
| 2081 | if (op.rejectTime != 0) { |
| 2082 | pw.print("; rejectTime="); TimeUtils.formatDuration(now-op.rejectTime, pw); |
| 2083 | pw.print(" ago"); |
| 2084 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2085 | if (op.duration == -1) { |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 2086 | pw.print(" (running)"); |
| 2087 | } else if (op.duration != 0) { |
| 2088 | pw.print("; duration="); TimeUtils.formatDuration(op.duration, pw); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2089 | } |
Dianne Hackborn | 7b7c58b | 2014-12-02 18:32:20 -0800 | [diff] [blame] | 2090 | pw.println(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2091 | } |
| 2092 | } |
| 2093 | } |
| 2094 | } |
| 2095 | } |
John Spurlock | 1af30c7 | 2014-03-10 08:33:35 -0400 | [diff] [blame] | 2096 | |
| 2097 | private static final class Restriction { |
| 2098 | private static final ArraySet<String> NO_EXCEPTIONS = new ArraySet<String>(); |
| 2099 | int mode; |
| 2100 | ArraySet<String> exceptionPackages = NO_EXCEPTIONS; |
| 2101 | } |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 2102 | |
| 2103 | @Override |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2104 | public void setUserRestrictions(Bundle restrictions, IBinder token, int userHandle) { |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 2105 | checkSystemUid("setUserRestrictions"); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2106 | Preconditions.checkNotNull(token); |
| 2107 | final boolean[] opRestrictions = getOrCreateUserRestrictionsForToken(token, userHandle); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 2108 | for (int i = 0; i < opRestrictions.length; ++i) { |
| 2109 | String restriction = AppOpsManager.opToRestriction(i); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2110 | final boolean restricted = restriction != null |
| 2111 | && restrictions.getBoolean(restriction, false); |
| 2112 | setUserRestrictionNoCheck(i, restricted, token, userHandle); |
| 2113 | } |
| 2114 | } |
| 2115 | |
| 2116 | @Override |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2117 | public void setUserRestriction(int code, boolean restricted, IBinder token, int userHandle, |
| 2118 | String[] exceptionPackages) { |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2119 | if (Binder.getCallingPid() != Process.myPid()) { |
| 2120 | mContext.enforcePermission(Manifest.permission.MANAGE_APP_OPS_RESTRICTIONS, |
| 2121 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 2122 | } |
| 2123 | if (userHandle != UserHandle.getCallingUserId()) { |
| 2124 | if (mContext.checkCallingOrSelfPermission(Manifest.permission |
| 2125 | .INTERACT_ACROSS_USERS_FULL) != PackageManager.PERMISSION_GRANTED |
| 2126 | && mContext.checkCallingOrSelfPermission(Manifest.permission |
| 2127 | .INTERACT_ACROSS_USERS) != PackageManager.PERMISSION_GRANTED) { |
| 2128 | throw new SecurityException("Need INTERACT_ACROSS_USERS_FULL or" |
| 2129 | + " INTERACT_ACROSS_USERS to interact cross user "); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 2130 | } |
| 2131 | } |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2132 | verifyIncomingOp(code); |
| 2133 | Preconditions.checkNotNull(token); |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2134 | setUserRestrictionNoCheck(code, restricted, token, userHandle, exceptionPackages); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | private void setUserRestrictionNoCheck(int code, boolean restricted, IBinder token, |
| 2138 | int userHandle) { |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2139 | setUserRestrictionNoCheck(code, restricted, token, userHandle, /*exceptionPackages*/null); |
| 2140 | } |
| 2141 | |
| 2142 | private void setUserRestrictionNoCheck(int code, boolean restricted, IBinder token, |
| 2143 | int userHandle, String[] exceptionPackages) { |
| 2144 | |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2145 | final boolean[] opRestrictions = getOrCreateUserRestrictionsForToken(token, userHandle); |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2146 | |
| 2147 | if (restricted) { |
| 2148 | final SparseArray<ArraySet<String>> opExceptions = |
| 2149 | getUserPackageExemptionsForToken(token, userHandle); |
| 2150 | |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2151 | ArraySet<String> exceptions = opExceptions.get(code); |
Ruben Brunk | 32f0fa4 | 2016-03-11 19:07:07 -0800 | [diff] [blame^] | 2152 | if (exceptionPackages != null && exceptionPackages.length > 0) { |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2153 | if (exceptions == null) { |
| 2154 | exceptions = new ArraySet<>(exceptionPackages.length); |
| 2155 | opExceptions.put(code, exceptions); |
| 2156 | } else { |
| 2157 | exceptions.clear(); |
| 2158 | } |
| 2159 | |
Ruben Brunk | 32f0fa4 | 2016-03-11 19:07:07 -0800 | [diff] [blame^] | 2160 | for (String p : exceptionPackages) { |
| 2161 | exceptions.add(p); |
| 2162 | } |
| 2163 | } else { |
| 2164 | opExceptions.remove(code); |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2165 | } |
| 2166 | } |
| 2167 | |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2168 | if (opRestrictions[code] == restricted) { |
| 2169 | return; |
| 2170 | } |
| 2171 | opRestrictions[code] = restricted; |
| 2172 | if (!restricted) { |
| 2173 | pruneUserRestrictionsForToken(token, userHandle); |
| 2174 | } |
| 2175 | |
| 2176 | final ArrayList<Callback> clonedCallbacks; |
| 2177 | synchronized (this) { |
| 2178 | ArrayList<Callback> callbacks = mOpModeWatchers.get(code); |
| 2179 | if (callbacks == null) { |
| 2180 | return; |
| 2181 | } |
| 2182 | clonedCallbacks = new ArrayList<>(callbacks); |
| 2183 | } |
| 2184 | |
| 2185 | // There are components watching for mode changes such as window manager |
| 2186 | // and location manager which are in our process. The callbacks in these |
| 2187 | // components may require permissions our remote caller does not have. |
| 2188 | final long identity = Binder.clearCallingIdentity(); |
| 2189 | try { |
| 2190 | final int callbackCount = clonedCallbacks.size(); |
| 2191 | for (int i = 0; i < callbackCount; i++) { |
| 2192 | Callback callback = clonedCallbacks.get(i); |
| 2193 | try { |
| 2194 | callback.mCallback.opChanged(code, -1, null); |
| 2195 | } catch (RemoteException e) { |
| 2196 | Log.w(TAG, "Error dispatching op op change", e); |
| 2197 | } |
| 2198 | } |
| 2199 | } finally { |
| 2200 | Binder.restoreCallingIdentity(identity); |
| 2201 | } |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | @Override |
| 2205 | public void removeUser(int userHandle) throws RemoteException { |
| 2206 | checkSystemUid("removeUser"); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2207 | final int tokenCount = mOpUserRestrictions.size(); |
| 2208 | for (int i = tokenCount - 1; i >= 0; i--) { |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2209 | SparseArray<Pair<boolean[], SparseArray<ArraySet<String>>>> opRestrictions = |
| 2210 | mOpUserRestrictions.valueAt(i); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2211 | if (opRestrictions != null) { |
| 2212 | opRestrictions.remove(userHandle); |
| 2213 | if (opRestrictions.size() <= 0) { |
| 2214 | mOpUserRestrictions.removeAt(i); |
| 2215 | } |
| 2216 | } |
| 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | |
| 2221 | private void pruneUserRestrictionsForToken(IBinder token, int userHandle) { |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2222 | SparseArray<Pair<boolean[], SparseArray<ArraySet<String>>>> perTokenRestrictions = |
| 2223 | mOpUserRestrictions.get(token); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2224 | if (perTokenRestrictions != null) { |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2225 | final Pair<boolean[], SparseArray<ArraySet<String>>> restrictions = |
| 2226 | perTokenRestrictions.get(userHandle); |
| 2227 | |
| 2228 | if (restrictions != null) { |
| 2229 | final boolean[] opRestrictions = restrictions.first; |
Ruben Brunk | 32f0fa4 | 2016-03-11 19:07:07 -0800 | [diff] [blame^] | 2230 | final SparseArray<ArraySet<String>> opExceptions = restrictions.second; |
| 2231 | boolean stillHasRestrictions = false; |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2232 | if (opRestrictions != null) { |
Ruben Brunk | 32f0fa4 | 2016-03-11 19:07:07 -0800 | [diff] [blame^] | 2233 | for (int i = 0; i < opRestrictions.length; i++) { |
| 2234 | boolean restriction = opRestrictions[i]; |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2235 | if (restriction) { |
Ruben Brunk | 32f0fa4 | 2016-03-11 19:07:07 -0800 | [diff] [blame^] | 2236 | stillHasRestrictions = true; |
| 2237 | } else { |
| 2238 | opExceptions.remove(i); |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2239 | } |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2240 | } |
| 2241 | } |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2242 | |
Ruben Brunk | 32f0fa4 | 2016-03-11 19:07:07 -0800 | [diff] [blame^] | 2243 | if (stillHasRestrictions) { |
| 2244 | return; |
| 2245 | } |
| 2246 | |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2247 | // No restrictions set for this client |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2248 | perTokenRestrictions.remove(userHandle); |
| 2249 | if (perTokenRestrictions.size() <= 0) { |
| 2250 | mOpUserRestrictions.remove(token); |
| 2251 | } |
| 2252 | } |
| 2253 | } |
| 2254 | } |
| 2255 | |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2256 | /** |
| 2257 | * Get or create the user restrictions array for a given client if it doesn't already exist. |
| 2258 | * |
| 2259 | * @param token the binder client creating the restriction. |
| 2260 | * @param userHandle the user handle to create a restriction for. |
| 2261 | * |
| 2262 | * @return the array of restriction states for each AppOps code. |
| 2263 | */ |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2264 | private boolean[] getOrCreateUserRestrictionsForToken(IBinder token, int userHandle) { |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2265 | SparseArray<Pair<boolean[], SparseArray<ArraySet<String>>>> perTokenRestrictions = |
| 2266 | mOpUserRestrictions.get(token); |
| 2267 | |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2268 | if (perTokenRestrictions == null) { |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2269 | perTokenRestrictions = |
| 2270 | new SparseArray<Pair<boolean[], SparseArray<ArraySet<String>>>>(); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2271 | mOpUserRestrictions.put(token, perTokenRestrictions); |
| 2272 | } |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2273 | |
| 2274 | Pair<boolean[], SparseArray<ArraySet<String>>> restrictions = |
| 2275 | perTokenRestrictions.get(userHandle); |
| 2276 | |
| 2277 | if (restrictions == null) { |
| 2278 | restrictions = new Pair<boolean[], SparseArray<ArraySet<String>>>( |
| 2279 | new boolean[AppOpsManager._NUM_OP], new SparseArray<ArraySet<String>>()); |
| 2280 | perTokenRestrictions.put(userHandle, restrictions); |
Svet Ganov | 9cea80cd | 2016-02-16 11:47:00 -0800 | [diff] [blame] | 2281 | } |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 2282 | |
| 2283 | return restrictions.first; |
| 2284 | } |
| 2285 | |
| 2286 | /** |
| 2287 | * Get the per-package exemptions for each AppOps code for a given client and userHandle. |
| 2288 | * |
| 2289 | * @param token the binder client to get the exemptions for. |
| 2290 | * @param userHandle the user handle to get the exemptions for. |
| 2291 | * |
| 2292 | * @return a mapping from the AppOps code to a set of packages exempt for that code. |
| 2293 | */ |
| 2294 | private SparseArray<ArraySet<String>> getUserPackageExemptionsForToken(IBinder token, |
| 2295 | int userHandle) { |
| 2296 | SparseArray<Pair<boolean[], SparseArray<ArraySet<String>>>> perTokenRestrictions = |
| 2297 | mOpUserRestrictions.get(token); |
| 2298 | |
| 2299 | if (perTokenRestrictions == null) { |
| 2300 | return null; // Don't create user restrictions accidentally |
| 2301 | } |
| 2302 | |
| 2303 | Pair<boolean[], SparseArray<ArraySet<String>>> restrictions = |
| 2304 | perTokenRestrictions.get(userHandle); |
| 2305 | |
| 2306 | if (restrictions == null) { |
| 2307 | return null; // Don't create user restrictions accidentally |
| 2308 | } |
| 2309 | |
| 2310 | return restrictions.second; |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | private void checkSystemUid(String function) { |
| 2314 | int uid = Binder.getCallingUid(); |
| 2315 | if (uid != Process.SYSTEM_UID) { |
| 2316 | throw new SecurityException(function + " must by called by the system"); |
| 2317 | } |
| 2318 | } |
| 2319 | |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2320 | private static String[] getPackagesForUid(int uid) { |
Svet Ganov | f3807aa | 2015-08-02 10:09:56 -0700 | [diff] [blame] | 2321 | String[] packageNames = null; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2322 | try { |
riddle_hsu | 40b300f | 2015-11-23 13:22:03 +0800 | [diff] [blame] | 2323 | packageNames = AppGlobals.getPackageManager().getPackagesForUid(uid); |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2324 | } catch (RemoteException e) { |
| 2325 | /* ignore - local call */ |
| 2326 | } |
Svet Ganov | f3807aa | 2015-08-02 10:09:56 -0700 | [diff] [blame] | 2327 | if (packageNames == null) { |
| 2328 | return EmptyArray.STRING; |
| 2329 | } |
| 2330 | return packageNames; |
Svet Ganov | 2af5708 | 2015-07-30 08:44:20 -0700 | [diff] [blame] | 2331 | } |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 2332 | } |