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