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