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 | |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 20 | #include <binder/ActivityManager.h> |
| 21 | #include <binder/Binder.h> |
| 22 | #include <binder/IServiceManager.h> |
| 23 | |
| 24 | #include <utils/SystemClock.h> |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | ActivityManager::ActivityManager() |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | sp<IActivityManager> ActivityManager::getService() |
| 33 | { |
| 34 | std::lock_guard<Mutex> scoped_lock(mLock); |
| 35 | int64_t startTime = 0; |
| 36 | sp<IActivityManager> service = mService; |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 37 | while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) { |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 38 | sp<IBinder> binder = defaultServiceManager()->checkService(String16("activity")); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 39 | if (binder == nullptr) { |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 40 | // Wait for the activity service to come back... |
| 41 | if (startTime == 0) { |
| 42 | startTime = uptimeMillis(); |
| 43 | ALOGI("Waiting for activity service"); |
Svet Ganov | 25d78a1 | 2018-01-21 02:49:00 -0800 | [diff] [blame] | 44 | } else if ((uptimeMillis() - startTime) > 1000000) { |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 45 | ALOGW("Waiting too long for activity service, giving up"); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 46 | service = nullptr; |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 47 | break; |
| 48 | } |
Svetoslav Ganov | 86fc4f3 | 2018-02-22 00:29:30 -0800 | [diff] [blame] | 49 | usleep(25000); |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 50 | } else { |
| 51 | service = interface_cast<IActivityManager>(binder); |
| 52 | mService = service; |
| 53 | } |
| 54 | } |
| 55 | return service; |
| 56 | } |
| 57 | |
| 58 | int ActivityManager::openContentUri(const String16& stringUri) |
| 59 | { |
| 60 | sp<IActivityManager> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 61 | return service != nullptr ? service->openContentUri(stringUri) : -1; |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void ActivityManager::registerUidObserver(const sp<IUidObserver>& observer, |
| 65 | const int32_t event, |
| 66 | const int32_t cutpoint, |
| 67 | const String16& callingPackage) |
| 68 | { |
| 69 | sp<IActivityManager> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 70 | if (service != nullptr) { |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 71 | service->registerUidObserver(observer, event, cutpoint, callingPackage); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void ActivityManager::unregisterUidObserver(const sp<IUidObserver>& observer) |
| 76 | { |
| 77 | sp<IActivityManager> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 78 | if (service != nullptr) { |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 79 | service->unregisterUidObserver(observer); |
| 80 | } |
| 81 | } |
| 82 | |
Svet Ganov | fa85180 | 2018-03-27 17:17:46 -0700 | [diff] [blame] | 83 | bool ActivityManager::isUidActive(const uid_t uid, const String16& callingPackage) |
| 84 | { |
| 85 | sp<IActivityManager> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 86 | if (service != nullptr) { |
Svet Ganov | fa85180 | 2018-03-27 17:17:46 -0700 | [diff] [blame] | 87 | return service->isUidActive(uid, callingPackage); |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 | |
Eric Laurent | 0559589 | 2018-10-18 14:56:24 -0700 | [diff] [blame^] | 92 | int32_t ActivityManager::getUidProcessState(const uid_t uid, const String16& callingPackage) |
| 93 | { |
| 94 | sp<IActivityManager> service = getService(); |
| 95 | if (service != nullptr) { |
| 96 | return service->getUidProcessState(uid, callingPackage); |
| 97 | } |
| 98 | return PROCESS_STATE_UNKNOWN; |
| 99 | } |
| 100 | |
Eino-Ville Talvala | ae8b20d | 2018-03-20 11:05:23 -0700 | [diff] [blame] | 101 | status_t ActivityManager::linkToDeath(const sp<IBinder::DeathRecipient>& recipient) { |
| 102 | sp<IActivityManager> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 103 | if (service != nullptr) { |
Eino-Ville Talvala | ae8b20d | 2018-03-20 11:05:23 -0700 | [diff] [blame] | 104 | return IInterface::asBinder(service)->linkToDeath(recipient); |
| 105 | } |
| 106 | return INVALID_OPERATION; |
| 107 | } |
| 108 | |
| 109 | status_t ActivityManager::unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient) { |
| 110 | sp<IActivityManager> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 111 | if (service != nullptr) { |
Eino-Ville Talvala | ae8b20d | 2018-03-20 11:05:23 -0700 | [diff] [blame] | 112 | return IInterface::asBinder(service)->unlinkToDeath(recipient); |
| 113 | } |
| 114 | return INVALID_OPERATION; |
| 115 | } |
| 116 | |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 117 | }; // namespace android |