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 <unistd.h>
19
20#include <binder/ActivityManager.h>
21#include <binder/Binder.h>
22#include <binder/IServiceManager.h>
23
24#include <utils/SystemClock.h>
25
26namespace android {
27
28ActivityManager::ActivityManager()
29{
30}
31
32sp<IActivityManager> ActivityManager::getService()
33{
34    std::lock_guard<Mutex> scoped_lock(mLock);
35    int64_t startTime = 0;
36    sp<IActivityManager> service = mService;
37    while (service == NULL || !IInterface::asBinder(service)->isBinderAlive()) {
38        sp<IBinder> binder = defaultServiceManager()->checkService(String16("activity"));
39        if (binder == NULL) {
40            // Wait for the activity service to come back...
41            if (startTime == 0) {
42                startTime = uptimeMillis();
43                ALOGI("Waiting for activity service");
44            } else if ((uptimeMillis() - startTime) > 1000000) {
45                ALOGW("Waiting too long for activity service, giving up");
46                service = NULL;
47                break;
48            }
49            usleep(25000);
50        } else {
51            service = interface_cast<IActivityManager>(binder);
52            mService = service;
53        }
54    }
55    return service;
56}
57
58int ActivityManager::openContentUri(const String16& stringUri)
59{
60    sp<IActivityManager> service = getService();
61    return service != NULL ? service->openContentUri(stringUri) : -1;
62}
63
64void 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();
70    if (service != NULL) {
71        service->registerUidObserver(observer, event, cutpoint, callingPackage);
72    }
73}
74
75void ActivityManager::unregisterUidObserver(const sp<IUidObserver>& observer)
76{
77    sp<IActivityManager> service = getService();
78    if (service != NULL) {
79        service->unregisterUidObserver(observer);
80    }
81}
82
83bool ActivityManager::isUidActive(const uid_t uid, const String16& callingPackage)
84{
85    sp<IActivityManager> service = getService();
86    if (service != NULL) {
87        return service->isUidActive(uid, callingPackage);
88    }
89    return false;
90}
91
92status_t ActivityManager::linkToDeath(const sp<IBinder::DeathRecipient>& recipient) {
93    sp<IActivityManager> service = getService();
94    if (service != NULL) {
95        return IInterface::asBinder(service)->linkToDeath(recipient);
96    }
97    return INVALID_OPERATION;
98}
99
100status_t ActivityManager::unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient) {
101    sp<IActivityManager> service = getService();
102    if (service != NULL) {
103        return IInterface::asBinder(service)->unlinkToDeath(recipient);
104    }
105    return INVALID_OPERATION;
106}
107
108}; // namespace android
109