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> |
| 18 | #include <binder/ActivityManager.h> |
| 19 | #include <binder/Binder.h> |
| 20 | #include <binder/IServiceManager.h> |
| 21 | |
| 22 | #include <utils/SystemClock.h> |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | ActivityManager::ActivityManager() |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | sp<IActivityManager> ActivityManager::getService() |
| 31 | { |
| 32 | std::lock_guard<Mutex> scoped_lock(mLock); |
| 33 | int64_t startTime = 0; |
| 34 | sp<IActivityManager> service = mService; |
| 35 | while (service == NULL || !IInterface::asBinder(service)->isBinderAlive()) { |
| 36 | sp<IBinder> binder = defaultServiceManager()->checkService(String16("activity")); |
| 37 | if (binder == NULL) { |
| 38 | // Wait for the activity service to come back... |
| 39 | if (startTime == 0) { |
| 40 | startTime = uptimeMillis(); |
| 41 | ALOGI("Waiting for activity service"); |
Svet Ganov | 25d78a1 | 2018-01-21 02:49:00 -0800 | [diff] [blame^] | 42 | } else if ((uptimeMillis() - startTime) > 1000000) { |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 43 | ALOGW("Waiting too long for activity service, giving up"); |
| 44 | service = NULL; |
| 45 | break; |
| 46 | } |
| 47 | sleep(1); |
| 48 | } else { |
| 49 | service = interface_cast<IActivityManager>(binder); |
| 50 | mService = service; |
| 51 | } |
| 52 | } |
| 53 | return service; |
| 54 | } |
| 55 | |
| 56 | int ActivityManager::openContentUri(const String16& stringUri) |
| 57 | { |
| 58 | sp<IActivityManager> service = getService(); |
| 59 | return service != NULL ? service->openContentUri(stringUri) : -1; |
| 60 | } |
| 61 | |
| 62 | void ActivityManager::registerUidObserver(const sp<IUidObserver>& observer, |
| 63 | const int32_t event, |
| 64 | const int32_t cutpoint, |
| 65 | const String16& callingPackage) |
| 66 | { |
| 67 | sp<IActivityManager> service = getService(); |
| 68 | if (service != NULL) { |
| 69 | service->registerUidObserver(observer, event, cutpoint, callingPackage); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void ActivityManager::unregisterUidObserver(const sp<IUidObserver>& observer) |
| 74 | { |
| 75 | sp<IActivityManager> service = getService(); |
| 76 | if (service != NULL) { |
| 77 | service->unregisterUidObserver(observer); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | }; // namespace android |