| Sudheer Shanka | 6ef26f1 | 2016-11-23 15:52:26 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2016 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 |  | 
| Nick Kralevich | ec9ec7d | 2016-12-17 19:47:27 -0800 | [diff] [blame] | 17 | #include <unistd.h> | 
 | 18 | #include <fcntl.h> | 
 | 19 |  | 
| Eric Laurent | 0559589 | 2018-10-18 14:56:24 -0700 | [diff] [blame] | 20 | #include <binder/ActivityManager.h> | 
| Sudheer Shanka | 6ef26f1 | 2016-11-23 15:52:26 -0800 | [diff] [blame] | 21 | #include <binder/IActivityManager.h> | 
| Sudheer Shanka | 6ef26f1 | 2016-11-23 15:52:26 -0800 | [diff] [blame] | 22 | #include <binder/Parcel.h> | 
 | 23 |  | 
 | 24 | namespace android { | 
 | 25 |  | 
 | 26 | // ------------------------------------------------------------------------------------ | 
 | 27 |  | 
 | 28 | class BpActivityManager : public BpInterface<IActivityManager> | 
 | 29 | { | 
 | 30 | public: | 
 | 31 |     explicit BpActivityManager(const sp<IBinder>& impl) | 
 | 32 |         : BpInterface<IActivityManager>(impl) | 
 | 33 |     { | 
 | 34 |     } | 
 | 35 |  | 
 | 36 |     virtual int openContentUri(const String16& stringUri) | 
 | 37 |     { | 
 | 38 |         Parcel data, reply; | 
| Sudheer Shanka | ff81a09 | 2017-03-02 12:27:58 -0800 | [diff] [blame] | 39 |         data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); | 
| Sudheer Shanka | 6ef26f1 | 2016-11-23 15:52:26 -0800 | [diff] [blame] | 40 |         data.writeString16(stringUri); | 
 | 41 |         status_t ret = remote()->transact(OPEN_CONTENT_URI_TRANSACTION, data, & reply); | 
 | 42 |         int fd = -1; | 
 | 43 |         if (ret == NO_ERROR) { | 
 | 44 |             int32_t exceptionCode = reply.readExceptionCode(); | 
 | 45 |             if (!exceptionCode) { | 
 | 46 |                 // Success is indicated here by a nonzero int followed by the fd; | 
 | 47 |                 // failure by a zero int with no data following. | 
 | 48 |                 if (reply.readInt32() != 0) { | 
| Nick Kralevich | ec9ec7d | 2016-12-17 19:47:27 -0800 | [diff] [blame] | 49 |                     fd = fcntl(reply.readParcelFileDescriptor(), F_DUPFD_CLOEXEC, 0); | 
| Sudheer Shanka | 6ef26f1 | 2016-11-23 15:52:26 -0800 | [diff] [blame] | 50 |                 } | 
 | 51 |             } else { | 
 | 52 |                 // An exception was thrown back; fall through to return failure | 
 | 53 |                 ALOGD("openContentUri(%s) caught exception %d\n", | 
 | 54 |                         String8(stringUri).string(), exceptionCode); | 
 | 55 |             } | 
 | 56 |         } | 
 | 57 |         return fd; | 
 | 58 |     } | 
| Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 59 |  | 
 | 60 |     virtual void registerUidObserver(const sp<IUidObserver>& observer, | 
 | 61 |                                      const int32_t event, | 
 | 62 |                                      const int32_t cutpoint, | 
 | 63 |                                      const String16& callingPackage) | 
 | 64 |     { | 
 | 65 |          Parcel data, reply; | 
 | 66 |          data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); | 
 | 67 |          data.writeStrongBinder(IInterface::asBinder(observer)); | 
 | 68 |          data.writeInt32(event); | 
 | 69 |          data.writeInt32(cutpoint); | 
 | 70 |          data.writeString16(callingPackage); | 
 | 71 |          remote()->transact(REGISTER_UID_OBSERVER_TRANSACTION, data, &reply); | 
 | 72 |     } | 
 | 73 |  | 
 | 74 |     virtual void unregisterUidObserver(const sp<IUidObserver>& observer) | 
 | 75 |     { | 
 | 76 |          Parcel data, reply; | 
 | 77 |          data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); | 
 | 78 |          data.writeStrongBinder(IInterface::asBinder(observer)); | 
 | 79 |          remote()->transact(UNREGISTER_UID_OBSERVER_TRANSACTION, data, &reply); | 
 | 80 |     } | 
| Svet Ganov | fa85180 | 2018-03-27 17:17:46 -0700 | [diff] [blame] | 81 |  | 
 | 82 |     virtual bool isUidActive(const uid_t uid, const String16& callingPackage) | 
 | 83 |     { | 
 | 84 |          Parcel data, reply; | 
 | 85 |          data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); | 
 | 86 |          data.writeInt32(uid); | 
 | 87 |          data.writeString16(callingPackage); | 
 | 88 |          remote()->transact(IS_UID_ACTIVE_TRANSACTION, data, &reply); | 
 | 89 |          // fail on exception | 
 | 90 |          if (reply.readExceptionCode() != 0) return false; | 
 | 91 |          return reply.readInt32() == 1; | 
 | 92 |     } | 
| Eric Laurent | 0559589 | 2018-10-18 14:56:24 -0700 | [diff] [blame] | 93 |  | 
 | 94 |     virtual int32_t getUidProcessState(const uid_t uid, const String16& callingPackage) | 
 | 95 |     { | 
 | 96 |         Parcel data, reply; | 
 | 97 |         data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); | 
 | 98 |         data.writeInt32(uid); | 
 | 99 |         data.writeString16(callingPackage); | 
 | 100 |         remote()->transact(GET_UID_PROCESS_STATE_TRANSACTION, data, &reply); | 
 | 101 |         // fail on exception | 
 | 102 |         if (reply.readExceptionCode() != 0) { | 
 | 103 |             return ActivityManager::PROCESS_STATE_UNKNOWN; | 
 | 104 |         } | 
 | 105 |         return reply.readInt32(); | 
 | 106 |     } | 
| Sudheer Shanka | 6ef26f1 | 2016-11-23 15:52:26 -0800 | [diff] [blame] | 107 | }; | 
 | 108 |  | 
 | 109 | // ------------------------------------------------------------------------------------ | 
 | 110 |  | 
 | 111 | IMPLEMENT_META_INTERFACE(ActivityManager, "android.app.IActivityManager"); | 
 | 112 |  | 
 | 113 | }; // namespace android |