Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #include <mutex> |
Svetoslav Ganov | 86fc4f3 | 2018-02-22 00:29:30 -0800 | [diff] [blame] | 18 | #include <unistd.h> |
| 19 | |
Chong Zhang | b632bd5 | 2020-11-02 11:01:48 -0800 | [diff] [blame] | 20 | #include <android/permission_manager.h> |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 21 | #include <binder/ActivityManager.h> |
| 22 | #include <binder/Binder.h> |
| 23 | #include <binder/IServiceManager.h> |
| 24 | |
| 25 | #include <utils/SystemClock.h> |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | ActivityManager::ActivityManager() |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | sp<IActivityManager> ActivityManager::getService() |
| 34 | { |
| 35 | std::lock_guard<Mutex> scoped_lock(mLock); |
| 36 | int64_t startTime = 0; |
| 37 | sp<IActivityManager> service = mService; |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 38 | while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) { |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 39 | sp<IBinder> binder = defaultServiceManager()->checkService(String16("activity")); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 40 | if (binder == nullptr) { |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 41 | // Wait for the activity service to come back... |
| 42 | if (startTime == 0) { |
| 43 | startTime = uptimeMillis(); |
| 44 | ALOGI("Waiting for activity service"); |
Svet Ganov | 25d78a1 | 2018-01-21 02:49:00 -0800 | [diff] [blame] | 45 | } else if ((uptimeMillis() - startTime) > 1000000) { |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 46 | ALOGW("Waiting too long for activity service, giving up"); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 47 | service = nullptr; |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 48 | break; |
| 49 | } |
Svetoslav Ganov | 86fc4f3 | 2018-02-22 00:29:30 -0800 | [diff] [blame] | 50 | usleep(25000); |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 51 | } else { |
| 52 | service = interface_cast<IActivityManager>(binder); |
| 53 | mService = service; |
| 54 | } |
| 55 | } |
| 56 | return service; |
| 57 | } |
| 58 | |
| 59 | int ActivityManager::openContentUri(const String16& stringUri) |
| 60 | { |
| 61 | sp<IActivityManager> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 62 | return service != nullptr ? service->openContentUri(stringUri) : -1; |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Chong Zhang | 1576534 | 2020-11-17 14:44:20 -0800 | [diff] [blame] | 65 | status_t ActivityManager::registerUidObserver(const sp<IUidObserver>& observer, |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 66 | const int32_t event, |
| 67 | const int32_t cutpoint, |
| 68 | const String16& callingPackage) |
| 69 | { |
| 70 | sp<IActivityManager> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 71 | if (service != nullptr) { |
Chong Zhang | 1576534 | 2020-11-17 14:44:20 -0800 | [diff] [blame] | 72 | return service->registerUidObserver(observer, event, cutpoint, callingPackage); |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 73 | } |
Chong Zhang | 1576534 | 2020-11-17 14:44:20 -0800 | [diff] [blame] | 74 | // ActivityManagerService appears dead. Return usual error code for dead service. |
| 75 | return DEAD_OBJECT; |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Austin Borger | 805d804 | 2023-04-21 20:07:49 -0700 | [diff] [blame] | 78 | status_t ActivityManager::registerUidObserverForUids(const sp<IUidObserver>& observer, |
| 79 | const int32_t event, const int32_t cutpoint, |
| 80 | const String16& callingPackage, |
| 81 | const int32_t uids[], size_t nUids, |
| 82 | /*out*/ sp<IBinder>& observerToken) { |
| 83 | sp<IActivityManager> service = getService(); |
| 84 | if (service != nullptr) { |
| 85 | return service->registerUidObserverForUids(observer, event, cutpoint, callingPackage, uids, |
| 86 | nUids, observerToken); |
| 87 | } |
| 88 | // ActivityManagerService appears dead. Return usual error code for dead service. |
| 89 | return DEAD_OBJECT; |
| 90 | } |
| 91 | |
Chong Zhang | 1576534 | 2020-11-17 14:44:20 -0800 | [diff] [blame] | 92 | status_t ActivityManager::unregisterUidObserver(const sp<IUidObserver>& observer) |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 93 | { |
| 94 | sp<IActivityManager> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 95 | if (service != nullptr) { |
Chong Zhang | 1576534 | 2020-11-17 14:44:20 -0800 | [diff] [blame] | 96 | return service->unregisterUidObserver(observer); |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 97 | } |
Chong Zhang | 1576534 | 2020-11-17 14:44:20 -0800 | [diff] [blame] | 98 | // ActivityManagerService appears dead. Return usual error code for dead service. |
| 99 | return DEAD_OBJECT; |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Austin Borger | 805d804 | 2023-04-21 20:07:49 -0700 | [diff] [blame] | 102 | status_t ActivityManager::addUidToObserver(const sp<IBinder>& observerToken, |
| 103 | const String16& callingPackage, int32_t uid) { |
| 104 | sp<IActivityManager> service = getService(); |
| 105 | if (service != nullptr) { |
| 106 | return service->addUidToObserver(observerToken, callingPackage, uid); |
| 107 | } |
| 108 | // ActivityManagerService appears dead. Return usual error code for dead service. |
| 109 | return DEAD_OBJECT; |
| 110 | } |
| 111 | |
| 112 | status_t ActivityManager::removeUidFromObserver(const sp<IBinder>& observerToken, |
| 113 | const String16& callingPackage, int32_t uid) { |
| 114 | sp<IActivityManager> service = getService(); |
| 115 | if (service != nullptr) { |
| 116 | return service->removeUidFromObserver(observerToken, callingPackage, uid); |
| 117 | } |
| 118 | // ActivityManagerService appears dead. Return usual error code for dead service. |
| 119 | return DEAD_OBJECT; |
| 120 | } |
| 121 | |
Svet Ganov | fa85180 | 2018-03-27 17:17:46 -0700 | [diff] [blame] | 122 | bool ActivityManager::isUidActive(const uid_t uid, const String16& callingPackage) |
| 123 | { |
| 124 | sp<IActivityManager> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 125 | if (service != nullptr) { |
Svet Ganov | fa85180 | 2018-03-27 17:17:46 -0700 | [diff] [blame] | 126 | return service->isUidActive(uid, callingPackage); |
| 127 | } |
| 128 | return false; |
| 129 | } |
| 130 | |
Eric Laurent | 0559589 | 2018-10-18 14:56:24 -0700 | [diff] [blame] | 131 | int32_t ActivityManager::getUidProcessState(const uid_t uid, const String16& callingPackage) |
| 132 | { |
| 133 | sp<IActivityManager> service = getService(); |
| 134 | if (service != nullptr) { |
| 135 | return service->getUidProcessState(uid, callingPackage); |
| 136 | } |
| 137 | return PROCESS_STATE_UNKNOWN; |
| 138 | } |
| 139 | |
Chong Zhang | b632bd5 | 2020-11-02 11:01:48 -0800 | [diff] [blame] | 140 | status_t ActivityManager::checkPermission(const String16& permission, |
| 141 | const pid_t pid, |
| 142 | const uid_t uid, |
| 143 | int32_t* outResult) { |
| 144 | sp<IActivityManager> service = getService(); |
| 145 | if (service != nullptr) { |
| 146 | return service->checkPermission(permission, pid, uid, outResult); |
| 147 | } |
| 148 | // ActivityManagerService appears dead. Return usual error code for dead service. |
| 149 | return DEAD_OBJECT; |
| 150 | } |
| 151 | |
Eino-Ville Talvala | ae8b20d | 2018-03-20 11:05:23 -0700 | [diff] [blame] | 152 | status_t ActivityManager::linkToDeath(const sp<IBinder::DeathRecipient>& recipient) { |
| 153 | sp<IActivityManager> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 154 | if (service != nullptr) { |
Eino-Ville Talvala | ae8b20d | 2018-03-20 11:05:23 -0700 | [diff] [blame] | 155 | return IInterface::asBinder(service)->linkToDeath(recipient); |
| 156 | } |
| 157 | return INVALID_OPERATION; |
| 158 | } |
| 159 | |
| 160 | status_t ActivityManager::unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient) { |
| 161 | sp<IActivityManager> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 162 | if (service != nullptr) { |
Eino-Ville Talvala | ae8b20d | 2018-03-20 11:05:23 -0700 | [diff] [blame] | 163 | return IInterface::asBinder(service)->unlinkToDeath(recipient); |
| 164 | } |
| 165 | return INVALID_OPERATION; |
| 166 | } |
| 167 | |
Steven Moreland | 6511af5 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 168 | } // namespace android |