IAppOpsService.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #define LOG_TAG "AppOpsService"
  17. #include <binder/IAppOpsService.h>
  18. #include <utils/Log.h>
  19. #include <binder/Parcel.h>
  20. #include <utils/String8.h>
  21. #include <private/binder/Static.h>
  22. namespace android {
  23. // ----------------------------------------------------------------------
  24. class BpAppOpsService : public BpInterface<IAppOpsService>
  25. {
  26. public:
  27. explicit BpAppOpsService(const sp<IBinder>& impl)
  28. : BpInterface<IAppOpsService>(impl)
  29. {
  30. }
  31. virtual int32_t checkOperation(int32_t code, int32_t uid, const String16& packageName) {
  32. Parcel data, reply;
  33. data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
  34. data.writeInt32(code);
  35. data.writeInt32(uid);
  36. data.writeString16(packageName);
  37. remote()->transact(CHECK_OPERATION_TRANSACTION, data, &reply);
  38. // fail on exception
  39. if (reply.readExceptionCode() != 0) return MODE_ERRORED;
  40. return reply.readInt32();
  41. }
  42. virtual int32_t noteOperation(int32_t code, int32_t uid, const String16& packageName) {
  43. Parcel data, reply;
  44. data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
  45. data.writeInt32(code);
  46. data.writeInt32(uid);
  47. data.writeString16(packageName);
  48. remote()->transact(NOTE_OPERATION_TRANSACTION, data, &reply);
  49. // fail on exception
  50. if (reply.readExceptionCode() != 0) return MODE_ERRORED;
  51. return reply.readInt32();
  52. }
  53. virtual int32_t startOperation(const sp<IBinder>& token, int32_t code, int32_t uid,
  54. const String16& packageName, bool startIfModeDefault) {
  55. Parcel data, reply;
  56. data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
  57. data.writeStrongBinder(token);
  58. data.writeInt32(code);
  59. data.writeInt32(uid);
  60. data.writeString16(packageName);
  61. data.writeInt32(startIfModeDefault ? 1 : 0);
  62. remote()->transact(START_OPERATION_TRANSACTION, data, &reply);
  63. // fail on exception
  64. if (reply.readExceptionCode() != 0) return MODE_ERRORED;
  65. return reply.readInt32();
  66. }
  67. virtual void finishOperation(const sp<IBinder>& token, int32_t code, int32_t uid,
  68. const String16& packageName) {
  69. Parcel data, reply;
  70. data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
  71. data.writeStrongBinder(token);
  72. data.writeInt32(code);
  73. data.writeInt32(uid);
  74. data.writeString16(packageName);
  75. remote()->transact(FINISH_OPERATION_TRANSACTION, data, &reply);
  76. }
  77. virtual void startWatchingMode(int32_t op, const String16& packageName,
  78. const sp<IAppOpsCallback>& callback) {
  79. Parcel data, reply;
  80. data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
  81. data.writeInt32(op);
  82. data.writeString16(packageName);
  83. data.writeStrongBinder(IInterface::asBinder(callback));
  84. remote()->transact(START_WATCHING_MODE_TRANSACTION, data, &reply);
  85. }
  86. virtual void stopWatchingMode(const sp<IAppOpsCallback>& callback) {
  87. Parcel data, reply;
  88. data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
  89. data.writeStrongBinder(IInterface::asBinder(callback));
  90. remote()->transact(STOP_WATCHING_MODE_TRANSACTION, data, &reply);
  91. }
  92. virtual sp<IBinder> getToken(const sp<IBinder>& clientToken) {
  93. Parcel data, reply;
  94. data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
  95. data.writeStrongBinder(clientToken);
  96. remote()->transact(GET_TOKEN_TRANSACTION, data, &reply);
  97. // fail on exception
  98. if (reply.readExceptionCode() != 0) return nullptr;
  99. return reply.readStrongBinder();
  100. }
  101. virtual int32_t permissionToOpCode(const String16& permission) {
  102. Parcel data, reply;
  103. data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
  104. data.writeString16(permission);
  105. remote()->transact(PERMISSION_TO_OP_CODE_TRANSACTION, data, &reply);
  106. // fail on exception
  107. if (reply.readExceptionCode() != 0) return -1;
  108. return reply.readInt32();
  109. }
  110. virtual int32_t checkAudioOperation(int32_t code, int32_t usage,
  111. int32_t uid, const String16& packageName) {
  112. Parcel data, reply;
  113. data.writeInterfaceToken(IAppOpsService::getInterfaceDescriptor());
  114. data.writeInt32(code);
  115. data.writeInt32(usage);
  116. data.writeInt32(uid);
  117. data.writeString16(packageName);
  118. remote()->transact(CHECK_AUDIO_OPERATION_TRANSACTION, data, &reply);
  119. // fail on exception
  120. if (reply.readExceptionCode() != 0) {
  121. return MODE_ERRORED;
  122. }
  123. return reply.readInt32();
  124. }
  125. };
  126. IMPLEMENT_META_INTERFACE(AppOpsService, "com.android.internal.app.IAppOpsService");
  127. // ----------------------------------------------------------------------
  128. // NOLINTNEXTLINE(google-default-arguments)
  129. status_t BnAppOpsService::onTransact(
  130. uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  131. {
  132. //printf("AppOpsService received: "); data.print();
  133. switch(code) {
  134. case CHECK_OPERATION_TRANSACTION: {
  135. CHECK_INTERFACE(IAppOpsService, data, reply);
  136. int32_t code = data.readInt32();
  137. int32_t uid = data.readInt32();
  138. String16 packageName = data.readString16();
  139. int32_t res = checkOperation(code, uid, packageName);
  140. reply->writeNoException();
  141. reply->writeInt32(res);
  142. return NO_ERROR;
  143. } break;
  144. case NOTE_OPERATION_TRANSACTION: {
  145. CHECK_INTERFACE(IAppOpsService, data, reply);
  146. int32_t code = data.readInt32();
  147. int32_t uid = data.readInt32();
  148. String16 packageName = data.readString16();
  149. int32_t res = noteOperation(code, uid, packageName);
  150. reply->writeNoException();
  151. reply->writeInt32(res);
  152. return NO_ERROR;
  153. } break;
  154. case START_OPERATION_TRANSACTION: {
  155. CHECK_INTERFACE(IAppOpsService, data, reply);
  156. sp<IBinder> token = data.readStrongBinder();
  157. int32_t code = data.readInt32();
  158. int32_t uid = data.readInt32();
  159. String16 packageName = data.readString16();
  160. bool startIfModeDefault = data.readInt32() == 1;
  161. int32_t res = startOperation(token, code, uid, packageName, startIfModeDefault);
  162. reply->writeNoException();
  163. reply->writeInt32(res);
  164. return NO_ERROR;
  165. } break;
  166. case FINISH_OPERATION_TRANSACTION: {
  167. CHECK_INTERFACE(IAppOpsService, data, reply);
  168. sp<IBinder> token = data.readStrongBinder();
  169. int32_t code = data.readInt32();
  170. int32_t uid = data.readInt32();
  171. String16 packageName = data.readString16();
  172. finishOperation(token, code, uid, packageName);
  173. reply->writeNoException();
  174. return NO_ERROR;
  175. } break;
  176. case START_WATCHING_MODE_TRANSACTION: {
  177. CHECK_INTERFACE(IAppOpsService, data, reply);
  178. int32_t op = data.readInt32();
  179. String16 packageName = data.readString16();
  180. sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder());
  181. startWatchingMode(op, packageName, callback);
  182. reply->writeNoException();
  183. return NO_ERROR;
  184. } break;
  185. case STOP_WATCHING_MODE_TRANSACTION: {
  186. CHECK_INTERFACE(IAppOpsService, data, reply);
  187. sp<IAppOpsCallback> callback = interface_cast<IAppOpsCallback>(data.readStrongBinder());
  188. stopWatchingMode(callback);
  189. reply->writeNoException();
  190. return NO_ERROR;
  191. } break;
  192. case GET_TOKEN_TRANSACTION: {
  193. CHECK_INTERFACE(IAppOpsService, data, reply);
  194. sp<IBinder> clientToken = data.readStrongBinder();
  195. sp<IBinder> token = getToken(clientToken);
  196. reply->writeNoException();
  197. reply->writeStrongBinder(token);
  198. return NO_ERROR;
  199. } break;
  200. case PERMISSION_TO_OP_CODE_TRANSACTION: {
  201. CHECK_INTERFACE(IAppOpsService, data, reply);
  202. String16 permission = data.readString16();
  203. const int32_t opCode = permissionToOpCode(permission);
  204. reply->writeNoException();
  205. reply->writeInt32(opCode);
  206. return NO_ERROR;
  207. } break;
  208. case CHECK_AUDIO_OPERATION_TRANSACTION: {
  209. CHECK_INTERFACE(IAppOpsService, data, reply);
  210. const int32_t code = data.readInt32();
  211. const int32_t usage = data.readInt32();
  212. const int32_t uid = data.readInt32();
  213. const String16 packageName = data.readString16();
  214. const int32_t res = checkAudioOperation(code, usage, uid, packageName);
  215. reply->writeNoException();
  216. reply->writeInt32(res);
  217. return NO_ERROR;
  218. } break;
  219. default:
  220. return BBinder::onTransact(code, data, reply, flags);
  221. }
  222. }
  223. }; // namespace android