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