Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | #define LOG_TAG "AppOpsService" |
| 18 | |
| 19 | #include <binder/IAppOpsService.h> |
| 20 | |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 21 | #include <utils/Log.h> |
| 22 | #include <binder/Parcel.h> |
| 23 | #include <utils/String8.h> |
| 24 | |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 25 | namespace android { |
| 26 | |
| 27 | // ---------------------------------------------------------------------- |
| 28 | |
| 29 | class BpAppOpsService : public BpInterface<IAppOpsService> |
| 30 | { |
| 31 | public: |
Chih-Hung Hsieh | e2347b7 | 2016-04-25 15:41:05 -0700 | [diff] [blame] | 32 | explicit BpAppOpsService(const sp<IBinder>& impl) |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 33 | : BpInterface<IAppOpsService>(impl) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | virtual int32_t checkOperation(int32_t code, int32_t uid, const String16& packageName) { |
| 38 | Parcel data, reply; |
| 39 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 40 | data.writeInt32(code); |
| 41 | data.writeInt32(uid); |
| 42 | data.writeString16(packageName); |
| 43 | remote()->transact(CHECK_OPERATION_TRANSACTION, data, &reply); |
| 44 | // fail on exception |
Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame] | 45 | if (reply.readExceptionCode() != 0) return MODE_ERRORED; |
| 46 | return reply.readInt32(); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | virtual int32_t noteOperation(int32_t code, int32_t uid, const String16& packageName) { |
| 50 | Parcel data, reply; |
| 51 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 52 | data.writeInt32(code); |
| 53 | data.writeInt32(uid); |
| 54 | data.writeString16(packageName); |
| 55 | remote()->transact(NOTE_OPERATION_TRANSACTION, data, &reply); |
| 56 | // fail on exception |
Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame] | 57 | if (reply.readExceptionCode() != 0) return MODE_ERRORED; |
| 58 | return reply.readInt32(); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 61 | virtual int32_t startOperation(const sp<IBinder>& token, int32_t code, int32_t uid, |
Svet Ganov | 616554c | 2018-02-26 13:27:26 -0800 | [diff] [blame] | 62 | const String16& packageName, bool startIfModeDefault) { |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 63 | Parcel data, reply; |
| 64 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 65 | data.writeStrongBinder(token); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 66 | data.writeInt32(code); |
| 67 | data.writeInt32(uid); |
| 68 | data.writeString16(packageName); |
Svet Ganov | 616554c | 2018-02-26 13:27:26 -0800 | [diff] [blame] | 69 | data.writeInt32(startIfModeDefault ? 1 : 0); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 70 | remote()->transact(START_OPERATION_TRANSACTION, data, &reply); |
| 71 | // fail on exception |
Eino-Ville Talvala | e88a85e | 2013-02-19 12:54:57 -0800 | [diff] [blame] | 72 | if (reply.readExceptionCode() != 0) return MODE_ERRORED; |
| 73 | return reply.readInt32(); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 76 | virtual void finishOperation(const sp<IBinder>& token, int32_t code, int32_t uid, |
| 77 | const String16& packageName) { |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 78 | Parcel data, reply; |
| 79 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 80 | data.writeStrongBinder(token); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 81 | data.writeInt32(code); |
| 82 | data.writeInt32(uid); |
| 83 | data.writeString16(packageName); |
| 84 | remote()->transact(FINISH_OPERATION_TRANSACTION, data, &reply); |
| 85 | } |
| 86 | |
| 87 | virtual void startWatchingMode(int32_t op, const String16& packageName, |
| 88 | const sp<IAppOpsCallback>& callback) { |
| 89 | Parcel data, reply; |
| 90 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 91 | data.writeInt32(op); |
| 92 | data.writeString16(packageName); |
Marco Nelissen | 2ea926b | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 93 | data.writeStrongBinder(IInterface::asBinder(callback)); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 94 | remote()->transact(START_WATCHING_MODE_TRANSACTION, data, &reply); |
| 95 | } |
| 96 | |
| 97 | virtual void stopWatchingMode(const sp<IAppOpsCallback>& callback) { |
| 98 | Parcel data, reply; |
| 99 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
Marco Nelissen | 2ea926b | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 100 | data.writeStrongBinder(IInterface::asBinder(callback)); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 101 | remote()->transact(STOP_WATCHING_MODE_TRANSACTION, data, &reply); |
| 102 | } |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 103 | |
| 104 | virtual sp<IBinder> getToken(const sp<IBinder>& clientToken) { |
| 105 | Parcel data, reply; |
| 106 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 107 | data.writeStrongBinder(clientToken); |
| 108 | remote()->transact(GET_TOKEN_TRANSACTION, data, &reply); |
| 109 | // fail on exception |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 110 | if (reply.readExceptionCode() != 0) return nullptr; |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 111 | return reply.readStrongBinder(); |
| 112 | } |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 113 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 114 | virtual int32_t permissionToOpCode(const String16& permission) { |
| 115 | Parcel data, reply; |
| 116 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 117 | data.writeString16(permission); |
| 118 | remote()->transact(PERMISSION_TO_OP_CODE_TRANSACTION, data, &reply); |
| 119 | // fail on exception |
| 120 | if (reply.readExceptionCode() != 0) return -1; |
| 121 | return reply.readInt32(); |
| 122 | } |
Jean-Michel Trivi | 94a566d | 2019-02-25 12:16:02 -0800 | [diff] [blame] | 123 | |
| 124 | virtual int32_t checkAudioOperation(int32_t code, int32_t usage, |
| 125 | int32_t uid, const String16& packageName) { |
| 126 | Parcel data, reply; |
| 127 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 128 | data.writeInt32(code); |
| 129 | data.writeInt32(usage); |
| 130 | data.writeInt32(uid); |
| 131 | data.writeString16(packageName); |
| 132 | remote()->transact(CHECK_AUDIO_OPERATION_TRANSACTION, data, &reply); |
| 133 | // fail on exception |
| 134 | if (reply.readExceptionCode() != 0) { |
| 135 | return MODE_ERRORED; |
| 136 | } |
| 137 | return reply.readInt32(); |
| 138 | } |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame] | 139 | |
Yin-Chia Yeh | 8e95ee8 | 2019-08-13 12:24:25 -0700 | [diff] [blame] | 140 | virtual void setCameraAudioRestriction(int32_t mode) { |
| 141 | Parcel data, reply; |
| 142 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 143 | data.writeInt32(mode); |
| 144 | remote()->transact(SET_CAMERA_AUDIO_RESTRICTION_TRANSACTION, data, &reply); |
| 145 | } |
| 146 | |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame] | 147 | virtual void noteAsyncOp(const String16& callingPackageName, int32_t uid, |
Philip P. Moltmann | b130188 | 2019-09-06 11:01:18 -0700 | [diff] [blame^] | 148 | const String16& packageName, int32_t opCode, const String16& featureId, |
| 149 | const String16& message) { |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame] | 150 | Parcel data, reply; |
| 151 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 152 | |
| 153 | // Convert empty callingPackage into null string |
| 154 | if (callingPackageName.size() != 0) { |
| 155 | data.writeString16(callingPackageName); |
| 156 | } else { |
| 157 | data.writeString16(nullptr, 0); |
| 158 | } |
| 159 | |
| 160 | data.writeInt32(uid); |
| 161 | |
| 162 | // Convert empty packageName into null string |
| 163 | if (packageName.size() != 0) { |
| 164 | data.writeString16(packageName); |
| 165 | } else { |
| 166 | data.writeString16(nullptr, 0); |
| 167 | } |
| 168 | |
| 169 | data.writeInt32(opCode); |
Philip P. Moltmann | b130188 | 2019-09-06 11:01:18 -0700 | [diff] [blame^] | 170 | |
| 171 | // Convert empty featureId into null string |
| 172 | if (featureId.size() != 0) { |
| 173 | data.writeString16(featureId); |
| 174 | } else { |
| 175 | data.writeString16(nullptr, 0); |
| 176 | } |
| 177 | |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame] | 178 | data.writeString16(message); |
| 179 | remote()->transact(NOTE_ASYNC_OP_TRANSACTION, data, &reply); |
| 180 | } |
| 181 | |
| 182 | virtual bool shouldCollectNotes(int32_t opCode) { |
| 183 | Parcel data, reply; |
| 184 | data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor()); |
| 185 | data.writeInt32(opCode); |
| 186 | remote()->transact(SHOULD_COLLECT_NOTES_TRANSACTION, data, &reply); |
| 187 | // fail on exception |
| 188 | if (reply.readExceptionCode() != 0) { |
| 189 | return false; |
| 190 | } |
| 191 | return reply.readBool(); |
| 192 | } |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | IMPLEMENT_META_INTERFACE(AppOpsService, "com.android.internal.app.IAppOpsService"); |
| 196 | |
| 197 | // ---------------------------------------------------------------------- |
| 198 | |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 199 | // NOLINTNEXTLINE(google-default-arguments) |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 200 | status_t BnAppOpsService::onTransact( |
| 201 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 202 | { |
| 203 | //printf("AppOpsService received: "); data.print(); |
| 204 | switch(code) { |
| 205 | case CHECK_OPERATION_TRANSACTION: { |
| 206 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 207 | int32_t code = data.readInt32(); |
| 208 | int32_t uid = data.readInt32(); |
| 209 | String16 packageName = data.readString16(); |
| 210 | int32_t res = checkOperation(code, uid, packageName); |
| 211 | reply->writeNoException(); |
| 212 | reply->writeInt32(res); |
| 213 | return NO_ERROR; |
| 214 | } break; |
| 215 | case NOTE_OPERATION_TRANSACTION: { |
| 216 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 217 | int32_t code = data.readInt32(); |
| 218 | int32_t uid = data.readInt32(); |
| 219 | String16 packageName = data.readString16(); |
| 220 | int32_t res = noteOperation(code, uid, packageName); |
| 221 | reply->writeNoException(); |
| 222 | reply->writeInt32(res); |
| 223 | return NO_ERROR; |
| 224 | } break; |
| 225 | case START_OPERATION_TRANSACTION: { |
| 226 | CHECK_INTERFACE(IAppOpsService, data, reply); |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 227 | sp<IBinder> token = data.readStrongBinder(); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 228 | int32_t code = data.readInt32(); |
| 229 | int32_t uid = data.readInt32(); |
| 230 | String16 packageName = data.readString16(); |
Svet Ganov | 616554c | 2018-02-26 13:27:26 -0800 | [diff] [blame] | 231 | bool startIfModeDefault = data.readInt32() == 1; |
| 232 | int32_t res = startOperation(token, code, uid, packageName, startIfModeDefault); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 233 | reply->writeNoException(); |
| 234 | reply->writeInt32(res); |
| 235 | return NO_ERROR; |
| 236 | } break; |
| 237 | case FINISH_OPERATION_TRANSACTION: { |
| 238 | CHECK_INTERFACE(IAppOpsService, data, reply); |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 239 | sp<IBinder> token = data.readStrongBinder(); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 240 | int32_t code = data.readInt32(); |
| 241 | int32_t uid = data.readInt32(); |
| 242 | String16 packageName = data.readString16(); |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 243 | finishOperation(token, code, uid, packageName); |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 244 | reply->writeNoException(); |
| 245 | return NO_ERROR; |
| 246 | } break; |
| 247 | case START_WATCHING_MODE_TRANSACTION: { |
| 248 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 249 | int32_t op = data.readInt32(); |
| 250 | String16 packageName = data.readString16(); |
| 251 | sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder()); |
| 252 | startWatchingMode(op, packageName, callback); |
| 253 | reply->writeNoException(); |
| 254 | return NO_ERROR; |
| 255 | } break; |
| 256 | case STOP_WATCHING_MODE_TRANSACTION: { |
| 257 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 258 | sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder()); |
| 259 | stopWatchingMode(callback); |
| 260 | reply->writeNoException(); |
| 261 | return NO_ERROR; |
| 262 | } break; |
Dianne Hackborn | 913b63d | 2013-07-17 17:26:15 -0700 | [diff] [blame] | 263 | case GET_TOKEN_TRANSACTION: { |
| 264 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 265 | sp<IBinder> clientToken = data.readStrongBinder(); |
| 266 | sp<IBinder> token = getToken(clientToken); |
| 267 | reply->writeNoException(); |
| 268 | reply->writeStrongBinder(token); |
| 269 | return NO_ERROR; |
| 270 | } break; |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 271 | case PERMISSION_TO_OP_CODE_TRANSACTION: { |
| 272 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 273 | String16 permission = data.readString16(); |
| 274 | const int32_t opCode = permissionToOpCode(permission); |
| 275 | reply->writeNoException(); |
| 276 | reply->writeInt32(opCode); |
| 277 | return NO_ERROR; |
| 278 | } break; |
Jean-Michel Trivi | 94a566d | 2019-02-25 12:16:02 -0800 | [diff] [blame] | 279 | case CHECK_AUDIO_OPERATION_TRANSACTION: { |
| 280 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 281 | const int32_t code = data.readInt32(); |
| 282 | const int32_t usage = data.readInt32(); |
| 283 | const int32_t uid = data.readInt32(); |
| 284 | const String16 packageName = data.readString16(); |
| 285 | const int32_t res = checkAudioOperation(code, usage, uid, packageName); |
| 286 | reply->writeNoException(); |
| 287 | reply->writeInt32(res); |
| 288 | return NO_ERROR; |
| 289 | } break; |
Yin-Chia Yeh | 8e95ee8 | 2019-08-13 12:24:25 -0700 | [diff] [blame] | 290 | case SET_CAMERA_AUDIO_RESTRICTION_TRANSACTION: { |
| 291 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 292 | const int32_t mode = data.readInt32(); |
| 293 | setCameraAudioRestriction(mode); |
| 294 | reply->writeNoException(); |
| 295 | return NO_ERROR; |
| 296 | } break; |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame] | 297 | case NOTE_ASYNC_OP_TRANSACTION: { |
| 298 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 299 | String16 callingPackageName = data.readString16(); |
| 300 | int32_t uid = data.readInt32(); |
| 301 | String16 packageName = data.readString16(); |
| 302 | int32_t opCode = data.readInt32(); |
Philip P. Moltmann | b130188 | 2019-09-06 11:01:18 -0700 | [diff] [blame^] | 303 | String16 featureId = data.readString16(); |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame] | 304 | String16 message = data.readString16(); |
Philip P. Moltmann | b130188 | 2019-09-06 11:01:18 -0700 | [diff] [blame^] | 305 | noteAsyncOp(callingPackageName, uid, packageName, opCode, featureId, message); |
Philip P. Moltmann | 66a8777 | 2019-06-24 16:30:00 -0700 | [diff] [blame] | 306 | reply->writeNoException(); |
| 307 | return NO_ERROR; |
| 308 | } break; |
| 309 | case SHOULD_COLLECT_NOTES_TRANSACTION: { |
| 310 | CHECK_INTERFACE(IAppOpsService, data, reply); |
| 311 | int32_t opCode = data.readInt32(); |
| 312 | bool shouldCollect = shouldCollectNotes(opCode); |
| 313 | reply->writeNoException(); |
| 314 | reply->writeBool(shouldCollect); |
| 315 | return NO_ERROR; |
| 316 | } break; |
Dianne Hackborn | 5da5ca5 | 2013-02-12 15:12:21 -0800 | [diff] [blame] | 317 | default: |
| 318 | return BBinder::onTransact(code, data, reply, flags); |
| 319 | } |
| 320 | } |
| 321 | |
Steven Moreland | 6511af5 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 322 | } // namespace android |