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 android.app; |
| 18 | |
Dianne Hackborn | d7d28e6 | 2013-02-12 14:59:53 -0800 | [diff] [blame^] | 19 | import android.Manifest; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 20 | import com.android.internal.app.IAppOpsService; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 21 | import com.android.internal.app.IAppOpsCallback; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 22 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 23 | import java.util.ArrayList; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 24 | import java.util.HashMap; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 25 | import java.util.List; |
| 26 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 27 | import android.content.Context; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 28 | import android.os.Parcel; |
| 29 | import android.os.Parcelable; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 30 | import android.os.Process; |
| 31 | import android.os.RemoteException; |
| 32 | |
Dianne Hackborn | d7d28e6 | 2013-02-12 14:59:53 -0800 | [diff] [blame^] | 33 | /** |
| 34 | * API for interacting with "application operation" tracking. Allows you to: |
| 35 | * |
| 36 | * - Note when operations are happening, and find out if they are allowed for the current caller. |
| 37 | * - Disallow specific apps from doing specific operations. |
| 38 | * - Collect all of the current information about operations that have been executed or are not |
| 39 | * being allowed. |
| 40 | * - Monitor for changes in whether an operation is allowed. |
| 41 | * |
| 42 | * Each operation is identified by a single integer; these integers are a fixed set of |
| 43 | * operations, enumerated by the OP_* constants. |
| 44 | * |
| 45 | * When checking operations, the result is a "mode" integer indicating the current setting |
| 46 | * for the operation under that caller: MODE_ALLOWED, MODE_IGNORED (don't execute the operation but |
| 47 | * fake its behavior enough so that the caller doesn't crash), MODE_ERRORED (through a |
| 48 | * SecurityException back to the caller; the normal operation calls will do this for you). |
| 49 | * |
| 50 | * @hide |
| 51 | */ |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 52 | public class AppOpsManager { |
| 53 | final Context mContext; |
| 54 | final IAppOpsService mService; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 55 | final HashMap<Callback, IAppOpsCallback> mModeWatchers |
| 56 | = new HashMap<Callback, IAppOpsCallback>(); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 57 | |
| 58 | public static final int MODE_ALLOWED = 0; |
| 59 | public static final int MODE_IGNORED = 1; |
| 60 | public static final int MODE_ERRORED = 2; |
| 61 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 62 | // when adding one of these: |
| 63 | // - increment _NUM_OP |
| 64 | // - add rows to sOpToSwitch, sOpNames, sOpPerms |
| 65 | // - add descriptive strings to Settings/res/values/arrays.xml |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 66 | public static final int OP_NONE = -1; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 67 | public static final int OP_COARSE_LOCATION = 0; |
| 68 | public static final int OP_FINE_LOCATION = 1; |
| 69 | public static final int OP_GPS = 2; |
| 70 | public static final int OP_VIBRATE = 3; |
| 71 | public static final int OP_READ_CONTACTS = 4; |
| 72 | public static final int OP_WRITE_CONTACTS = 5; |
| 73 | public static final int OP_READ_CALL_LOG = 6; |
| 74 | public static final int OP_WRITE_CALL_LOG = 7; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 75 | public static final int OP_READ_CALENDAR = 8; |
| 76 | public static final int OP_WRITE_CALENDAR = 9; |
| 77 | public static final int OP_WIFI_SCAN = 10; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 78 | public static final int OP_POST_NOTIFICATION = 11; |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 79 | public static final int OP_NEIGHBORING_CELLS = 12; |
| 80 | public static final int OP_CALL_PHONE = 13; |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 81 | public static final int OP_READ_SMS = 14; |
| 82 | public static final int OP_WRITE_SMS = 15; |
| 83 | public static final int OP_RECEIVE_SMS = 16; |
| 84 | public static final int OP_RECEIVE_EMERGECY_SMS = 17; |
| 85 | public static final int OP_RECEIVE_MMS = 18; |
| 86 | public static final int OP_RECEIVE_WAP_PUSH = 19; |
| 87 | public static final int OP_SEND_SMS = 20; |
| 88 | public static final int OP_READ_ICC_SMS = 21; |
| 89 | public static final int OP_WRITE_ICC_SMS = 22; |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 90 | public static final int OP_WRITE_SETTINGS = 23; |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 91 | public static final int OP_SYSTEM_ALERT_WINDOW = 24; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 92 | public static final int OP_ACCESS_NOTIFICATIONS = 25; |
Dianne Hackborn | d7d28e6 | 2013-02-12 14:59:53 -0800 | [diff] [blame^] | 93 | public static final int OP_CAMERA = 26; |
| 94 | public static final int OP_RECORD_AUDIO = 27; |
| 95 | public static final int OP_PLAY_AUDIO = 28; |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 96 | /** @hide */ |
Dianne Hackborn | d7d28e6 | 2013-02-12 14:59:53 -0800 | [diff] [blame^] | 97 | public static final int _NUM_OP = 29; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 98 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 99 | /** |
| 100 | * This maps each operation to the operation that serves as the |
| 101 | * switch to determine whether it is allowed. Generally this is |
| 102 | * a 1:1 mapping, but for some things (like location) that have |
| 103 | * multiple low-level operations being tracked that should be |
| 104 | * presented to hte user as one switch then this can be used to |
| 105 | * make them all controlled by the same single operation. |
| 106 | */ |
| 107 | private static int[] sOpToSwitch = new int[] { |
| 108 | OP_COARSE_LOCATION, |
| 109 | OP_COARSE_LOCATION, |
| 110 | OP_COARSE_LOCATION, |
| 111 | OP_VIBRATE, |
| 112 | OP_READ_CONTACTS, |
| 113 | OP_WRITE_CONTACTS, |
| 114 | OP_READ_CALL_LOG, |
| 115 | OP_WRITE_CALL_LOG, |
| 116 | OP_READ_CALENDAR, |
| 117 | OP_WRITE_CALENDAR, |
| 118 | OP_COARSE_LOCATION, |
| 119 | OP_POST_NOTIFICATION, |
| 120 | OP_COARSE_LOCATION, |
| 121 | OP_CALL_PHONE, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 122 | OP_READ_SMS, |
| 123 | OP_WRITE_SMS, |
| 124 | OP_READ_SMS, |
| 125 | OP_READ_SMS, |
| 126 | OP_READ_SMS, |
| 127 | OP_READ_SMS, |
| 128 | OP_WRITE_SMS, |
| 129 | OP_READ_SMS, |
| 130 | OP_WRITE_SMS, |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 131 | OP_WRITE_SETTINGS, |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 132 | OP_SYSTEM_ALERT_WINDOW, |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 133 | OP_ACCESS_NOTIFICATIONS, |
Dianne Hackborn | d7d28e6 | 2013-02-12 14:59:53 -0800 | [diff] [blame^] | 134 | OP_CAMERA, |
| 135 | OP_RECORD_AUDIO, |
| 136 | OP_PLAY_AUDIO, |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | /** |
| 140 | * This provides a simple name for each operation to be used |
| 141 | * in debug output. |
| 142 | */ |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 143 | private static String[] sOpNames = new String[] { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 144 | "COARSE_LOCATION", |
| 145 | "FINE_LOCATION", |
| 146 | "GPS", |
| 147 | "VIBRATE", |
| 148 | "READ_CONTACTS", |
| 149 | "WRITE_CONTACTS", |
| 150 | "READ_CALL_LOG", |
| 151 | "WRITE_CALL_LOG", |
| 152 | "READ_CALENDAR", |
| 153 | "WRITE_CALENDAR", |
| 154 | "WIFI_SCAN", |
| 155 | "POST_NOTIFICATION", |
| 156 | "NEIGHBORING_CELLS", |
| 157 | "CALL_PHONE", |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 158 | "READ_SMS", |
| 159 | "WRITE_SMS", |
| 160 | "RECEIVE_SMS", |
| 161 | "RECEIVE_EMERGECY_SMS", |
| 162 | "RECEIVE_MMS", |
| 163 | "RECEIVE_WAP_PUSH", |
| 164 | "SEND_SMS", |
| 165 | "READ_ICC_SMS", |
| 166 | "WRITE_ICC_SMS", |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 167 | "WRITE_SETTINGS", |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 168 | "SYSTEM_ALERT_WINDOW", |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 169 | "ACCESS_NOTIFICATIONS", |
Dianne Hackborn | d7d28e6 | 2013-02-12 14:59:53 -0800 | [diff] [blame^] | 170 | "CAMERA", |
| 171 | "RECORD_AUDIO", |
| 172 | "PLAY_AUDIO", |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 173 | }; |
| 174 | |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 175 | /** |
| 176 | * This optionally maps a permission to an operation. If there |
| 177 | * is no permission associated with an operation, it is null. |
| 178 | */ |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 179 | private static String[] sOpPerms = new String[] { |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 180 | android.Manifest.permission.ACCESS_COARSE_LOCATION, |
| 181 | android.Manifest.permission.ACCESS_FINE_LOCATION, |
| 182 | null, |
| 183 | android.Manifest.permission.VIBRATE, |
| 184 | android.Manifest.permission.READ_CONTACTS, |
| 185 | android.Manifest.permission.WRITE_CONTACTS, |
| 186 | android.Manifest.permission.READ_CALL_LOG, |
| 187 | android.Manifest.permission.WRITE_CALL_LOG, |
| 188 | android.Manifest.permission.READ_CALENDAR, |
| 189 | android.Manifest.permission.WRITE_CALENDAR, |
| 190 | null, // no permission required for notifications |
| 191 | android.Manifest.permission.ACCESS_WIFI_STATE, |
| 192 | null, // neighboring cells shares the coarse location perm |
| 193 | android.Manifest.permission.CALL_PHONE, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 194 | android.Manifest.permission.READ_SMS, |
| 195 | android.Manifest.permission.WRITE_SMS, |
| 196 | android.Manifest.permission.RECEIVE_SMS, |
| 197 | android.Manifest.permission.RECEIVE_EMERGENCY_BROADCAST, |
| 198 | android.Manifest.permission.RECEIVE_MMS, |
| 199 | android.Manifest.permission.RECEIVE_WAP_PUSH, |
| 200 | android.Manifest.permission.SEND_SMS, |
| 201 | android.Manifest.permission.READ_SMS, |
| 202 | android.Manifest.permission.WRITE_SMS, |
Dianne Hackborn | 961321f | 2013-02-05 17:22:41 -0800 | [diff] [blame] | 203 | android.Manifest.permission.WRITE_SETTINGS, |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 204 | android.Manifest.permission.SYSTEM_ALERT_WINDOW, |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 205 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
Dianne Hackborn | d7d28e6 | 2013-02-12 14:59:53 -0800 | [diff] [blame^] | 206 | android.Manifest.permission.CAMERA, |
| 207 | android.Manifest.permission.RECORD_AUDIO, |
| 208 | null, // no permission for playing audio |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 209 | }; |
| 210 | |
Dianne Hackborn | d7d28e6 | 2013-02-12 14:59:53 -0800 | [diff] [blame^] | 211 | /** |
| 212 | * Retrieve the op switch that controls the given operation. |
| 213 | */ |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 214 | public static int opToSwitch(int op) { |
| 215 | return sOpToSwitch[op]; |
| 216 | } |
| 217 | |
Dianne Hackborn | d7d28e6 | 2013-02-12 14:59:53 -0800 | [diff] [blame^] | 218 | /** |
| 219 | * Retrieve a non-localized name for the operation, for debugging output. |
| 220 | */ |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 221 | public static String opToName(int op) { |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 222 | if (op == OP_NONE) return "NONE"; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 223 | return op < sOpNames.length ? sOpNames[op] : ("Unknown(" + op + ")"); |
| 224 | } |
| 225 | |
Dianne Hackborn | d7d28e6 | 2013-02-12 14:59:53 -0800 | [diff] [blame^] | 226 | /** |
| 227 | * Retrieve the permission associated with an operation, or null if there is not one. |
| 228 | */ |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 229 | public static String opToPermission(int op) { |
| 230 | return sOpPerms[op]; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Dianne Hackborn | d7d28e6 | 2013-02-12 14:59:53 -0800 | [diff] [blame^] | 233 | /** |
| 234 | * Class holding all of the operation information associated with an app. |
| 235 | */ |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 236 | public static class PackageOps implements Parcelable { |
| 237 | private final String mPackageName; |
| 238 | private final int mUid; |
| 239 | private final List<OpEntry> mEntries; |
| 240 | |
| 241 | public PackageOps(String packageName, int uid, List<OpEntry> entries) { |
| 242 | mPackageName = packageName; |
| 243 | mUid = uid; |
| 244 | mEntries = entries; |
| 245 | } |
| 246 | |
| 247 | public String getPackageName() { |
| 248 | return mPackageName; |
| 249 | } |
| 250 | |
| 251 | public int getUid() { |
| 252 | return mUid; |
| 253 | } |
| 254 | |
| 255 | public List<OpEntry> getOps() { |
| 256 | return mEntries; |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public int describeContents() { |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public void writeToParcel(Parcel dest, int flags) { |
| 266 | dest.writeString(mPackageName); |
| 267 | dest.writeInt(mUid); |
| 268 | dest.writeInt(mEntries.size()); |
| 269 | for (int i=0; i<mEntries.size(); i++) { |
| 270 | mEntries.get(i).writeToParcel(dest, flags); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | PackageOps(Parcel source) { |
| 275 | mPackageName = source.readString(); |
| 276 | mUid = source.readInt(); |
| 277 | mEntries = new ArrayList<OpEntry>(); |
| 278 | final int N = source.readInt(); |
| 279 | for (int i=0; i<N; i++) { |
| 280 | mEntries.add(OpEntry.CREATOR.createFromParcel(source)); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | public static final Creator<PackageOps> CREATOR = new Creator<PackageOps>() { |
| 285 | @Override public PackageOps createFromParcel(Parcel source) { |
| 286 | return new PackageOps(source); |
| 287 | } |
| 288 | |
| 289 | @Override public PackageOps[] newArray(int size) { |
| 290 | return new PackageOps[size]; |
| 291 | } |
| 292 | }; |
| 293 | } |
| 294 | |
Dianne Hackborn | d7d28e6 | 2013-02-12 14:59:53 -0800 | [diff] [blame^] | 295 | /** |
| 296 | * Class holding the information about one unique operation of an application. |
| 297 | */ |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 298 | public static class OpEntry implements Parcelable { |
| 299 | private final int mOp; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 300 | private final int mMode; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 301 | private final long mTime; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 302 | private final long mRejectTime; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 303 | private final int mDuration; |
| 304 | |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 305 | public OpEntry(int op, int mode, long time, long rejectTime, int duration) { |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 306 | mOp = op; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 307 | mMode = mode; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 308 | mTime = time; |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 309 | mRejectTime = rejectTime; |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 310 | mDuration = duration; |
| 311 | } |
| 312 | |
| 313 | public int getOp() { |
| 314 | return mOp; |
| 315 | } |
| 316 | |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 317 | public int getMode() { |
| 318 | return mMode; |
| 319 | } |
| 320 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 321 | public long getTime() { |
| 322 | return mTime; |
| 323 | } |
| 324 | |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 325 | public long getRejectTime() { |
| 326 | return mRejectTime; |
| 327 | } |
| 328 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 329 | public boolean isRunning() { |
| 330 | return mDuration == -1; |
| 331 | } |
| 332 | |
| 333 | public int getDuration() { |
| 334 | return mDuration == -1 ? (int)(System.currentTimeMillis()-mTime) : mDuration; |
| 335 | } |
| 336 | |
| 337 | @Override |
| 338 | public int describeContents() { |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | @Override |
| 343 | public void writeToParcel(Parcel dest, int flags) { |
| 344 | dest.writeInt(mOp); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 345 | dest.writeInt(mMode); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 346 | dest.writeLong(mTime); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 347 | dest.writeLong(mRejectTime); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 348 | dest.writeInt(mDuration); |
| 349 | } |
| 350 | |
| 351 | OpEntry(Parcel source) { |
| 352 | mOp = source.readInt(); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 353 | mMode = source.readInt(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 354 | mTime = source.readLong(); |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 355 | mRejectTime = source.readLong(); |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 356 | mDuration = source.readInt(); |
| 357 | } |
| 358 | |
| 359 | public static final Creator<OpEntry> CREATOR = new Creator<OpEntry>() { |
| 360 | @Override public OpEntry createFromParcel(Parcel source) { |
| 361 | return new OpEntry(source); |
| 362 | } |
| 363 | |
| 364 | @Override public OpEntry[] newArray(int size) { |
| 365 | return new OpEntry[size]; |
| 366 | } |
| 367 | }; |
| 368 | } |
| 369 | |
Dianne Hackborn | d7d28e6 | 2013-02-12 14:59:53 -0800 | [diff] [blame^] | 370 | /** |
| 371 | * Callback for notification of changes to operation state. |
| 372 | */ |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 373 | public interface Callback { |
| 374 | public void opChanged(int op, String packageName); |
| 375 | } |
| 376 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 377 | public AppOpsManager(Context context, IAppOpsService service) { |
| 378 | mContext = context; |
| 379 | mService = service; |
| 380 | } |
| 381 | |
Dianne Hackborn | d7d28e6 | 2013-02-12 14:59:53 -0800 | [diff] [blame^] | 382 | /** |
| 383 | * Retrieve current operation state for all applications. |
| 384 | * |
| 385 | * @param ops The set of operations you are interested in, or null if you want all of them. |
| 386 | */ |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 387 | public List<AppOpsManager.PackageOps> getPackagesForOps(int[] ops) { |
| 388 | try { |
| 389 | return mService.getPackagesForOps(ops); |
| 390 | } catch (RemoteException e) { |
| 391 | } |
| 392 | return null; |
| 393 | } |
| 394 | |
Dianne Hackborn | d7d28e6 | 2013-02-12 14:59:53 -0800 | [diff] [blame^] | 395 | /** |
| 396 | * Retrieve current operation state for one application. |
| 397 | * |
| 398 | * @param uid The uid of the application of interest. |
| 399 | * @param packageName The name of the application of interest. |
| 400 | * @param ops The set of operations you are interested in, or null if you want all of them. |
| 401 | */ |
Dianne Hackborn | 72e3983 | 2013-01-18 18:36:09 -0800 | [diff] [blame] | 402 | public List<AppOpsManager.PackageOps> getOpsForPackage(int uid, String packageName, int[] ops) { |
| 403 | try { |
| 404 | return mService.getOpsForPackage(uid, packageName, ops); |
| 405 | } catch (RemoteException e) { |
| 406 | } |
| 407 | return null; |
| 408 | } |
| 409 | |
Dianne Hackborn | 5e45ee6 | 2013-01-24 19:13:44 -0800 | [diff] [blame] | 410 | public void setMode(int code, int uid, String packageName, int mode) { |
| 411 | try { |
| 412 | mService.setMode(code, uid, packageName, mode); |
| 413 | } catch (RemoteException e) { |
| 414 | } |
| 415 | } |
| 416 | |
Dianne Hackborn | c229302 | 2013-02-06 23:14:49 -0800 | [diff] [blame] | 417 | public void startWatchingMode(int op, String packageName, final Callback callback) { |
| 418 | synchronized (mModeWatchers) { |
| 419 | IAppOpsCallback cb = mModeWatchers.get(callback); |
| 420 | if (cb == null) { |
| 421 | cb = new IAppOpsCallback.Stub() { |
| 422 | public void opChanged(int op, String packageName) { |
| 423 | callback.opChanged(op, packageName); |
| 424 | } |
| 425 | }; |
| 426 | mModeWatchers.put(callback, cb); |
| 427 | } |
| 428 | try { |
| 429 | mService.startWatchingMode(op, packageName, cb); |
| 430 | } catch (RemoteException e) { |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | public void stopWatchingMode(Callback callback) { |
| 436 | synchronized (mModeWatchers) { |
| 437 | IAppOpsCallback cb = mModeWatchers.get(callback); |
| 438 | if (cb != null) { |
| 439 | try { |
| 440 | mService.stopWatchingMode(cb); |
| 441 | } catch (RemoteException e) { |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 447 | public int checkOp(int op, int uid, String packageName) { |
| 448 | try { |
| 449 | int mode = mService.checkOperation(op, uid, packageName); |
| 450 | if (mode == MODE_ERRORED) { |
| 451 | throw new SecurityException("Operation not allowed"); |
| 452 | } |
| 453 | return mode; |
| 454 | } catch (RemoteException e) { |
| 455 | } |
| 456 | return MODE_IGNORED; |
| 457 | } |
| 458 | |
| 459 | public int checkOpNoThrow(int op, int uid, String packageName) { |
| 460 | try { |
| 461 | return mService.checkOperation(op, uid, packageName); |
| 462 | } catch (RemoteException e) { |
| 463 | } |
| 464 | return MODE_IGNORED; |
| 465 | } |
| 466 | |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 467 | public int noteOp(int op, int uid, String packageName) { |
| 468 | try { |
| 469 | int mode = mService.noteOperation(op, uid, packageName); |
| 470 | if (mode == MODE_ERRORED) { |
| 471 | throw new SecurityException("Operation not allowed"); |
| 472 | } |
| 473 | return mode; |
| 474 | } catch (RemoteException e) { |
| 475 | } |
| 476 | return MODE_IGNORED; |
| 477 | } |
| 478 | |
| 479 | public int noteOpNoThrow(int op, int uid, String packageName) { |
| 480 | try { |
| 481 | return mService.noteOperation(op, uid, packageName); |
| 482 | } catch (RemoteException e) { |
| 483 | } |
| 484 | return MODE_IGNORED; |
| 485 | } |
| 486 | |
| 487 | public int noteOp(int op) { |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 488 | return noteOp(op, Process.myUid(), mContext.getBasePackageName()); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | public int startOp(int op, int uid, String packageName) { |
| 492 | try { |
| 493 | int mode = mService.startOperation(op, uid, packageName); |
| 494 | if (mode == MODE_ERRORED) { |
| 495 | throw new SecurityException("Operation not allowed"); |
| 496 | } |
| 497 | return mode; |
| 498 | } catch (RemoteException e) { |
| 499 | } |
| 500 | return MODE_IGNORED; |
| 501 | } |
| 502 | |
| 503 | public int startOpNoThrow(int op, int uid, String packageName) { |
| 504 | try { |
| 505 | return mService.startOperation(op, uid, packageName); |
| 506 | } catch (RemoteException e) { |
| 507 | } |
| 508 | return MODE_IGNORED; |
| 509 | } |
| 510 | |
| 511 | public int startOp(int op) { |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 512 | return startOp(op, Process.myUid(), mContext.getBasePackageName()); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | public void finishOp(int op, int uid, String packageName) { |
| 516 | try { |
| 517 | mService.finishOperation(op, uid, packageName); |
| 518 | } catch (RemoteException e) { |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | public void finishOp(int op) { |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 523 | finishOp(op, Process.myUid(), mContext.getBasePackageName()); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 524 | } |
| 525 | } |