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