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 <android/permission_manager.h>
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
ActivityManager()29 ActivityManager::ActivityManager()
30 {
31 }
32
getService()33 sp<IActivityManager> ActivityManager::getService()
34 {
35 std::lock_guard<Mutex> scoped_lock(mLock);
36 int64_t startTime = 0;
37 sp<IActivityManager> service = mService;
38 while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
39 sp<IBinder> binder = defaultServiceManager()->checkService(String16("activity"));
40 if (binder == nullptr) {
41 // Wait for the activity service to come back...
42 if (startTime == 0) {
43 startTime = uptimeMillis();
44 ALOGI("Waiting for activity service");
45 } else if ((uptimeMillis() - startTime) > 1000000) {
46 ALOGW("Waiting too long for activity service, giving up");
47 service = nullptr;
48 break;
49 }
50 usleep(25000);
51 } else {
52 service = interface_cast<IActivityManager>(binder);
53 mService = service;
54 }
55 }
56 return service;
57 }
58
openContentUri(const String16 & stringUri)59 int ActivityManager::openContentUri(const String16& stringUri)
60 {
61 sp<IActivityManager> service = getService();
62 return service != nullptr ? service->openContentUri(stringUri) : -1;
63 }
64
registerUidObserver(const sp<IUidObserver> & observer,const int32_t event,const int32_t cutpoint,const String16 & callingPackage)65 status_t ActivityManager::registerUidObserver(const sp<IUidObserver>& observer,
66 const int32_t event,
67 const int32_t cutpoint,
68 const String16& callingPackage)
69 {
70 sp<IActivityManager> service = getService();
71 if (service != nullptr) {
72 return service->registerUidObserver(observer, event, cutpoint, callingPackage);
73 }
74 // ActivityManagerService appears dead. Return usual error code for dead service.
75 return DEAD_OBJECT;
76 }
77
unregisterUidObserver(const sp<IUidObserver> & observer)78 status_t ActivityManager::unregisterUidObserver(const sp<IUidObserver>& observer)
79 {
80 sp<IActivityManager> service = getService();
81 if (service != nullptr) {
82 return service->unregisterUidObserver(observer);
83 }
84 // ActivityManagerService appears dead. Return usual error code for dead service.
85 return DEAD_OBJECT;
86 }
87
isUidActive(const uid_t uid,const String16 & callingPackage)88 bool ActivityManager::isUidActive(const uid_t uid, const String16& callingPackage)
89 {
90 sp<IActivityManager> service = getService();
91 if (service != nullptr) {
92 return service->isUidActive(uid, callingPackage);
93 }
94 return false;
95 }
96
getUidProcessState(const uid_t uid,const String16 & callingPackage)97 int32_t ActivityManager::getUidProcessState(const uid_t uid, const String16& callingPackage)
98 {
99 sp<IActivityManager> service = getService();
100 if (service != nullptr) {
101 return service->getUidProcessState(uid, callingPackage);
102 }
103 return PROCESS_STATE_UNKNOWN;
104 }
105
checkPermission(const String16 & permission,const pid_t pid,const uid_t uid,int32_t * outResult)106 status_t ActivityManager::checkPermission(const String16& permission,
107 const pid_t pid,
108 const uid_t uid,
109 int32_t* outResult) {
110 sp<IActivityManager> service = getService();
111 if (service != nullptr) {
112 return service->checkPermission(permission, pid, uid, outResult);
113 }
114 // ActivityManagerService appears dead. Return usual error code for dead service.
115 return DEAD_OBJECT;
116 }
117
linkToDeath(const sp<IBinder::DeathRecipient> & recipient)118 status_t ActivityManager::linkToDeath(const sp<IBinder::DeathRecipient>& recipient) {
119 sp<IActivityManager> service = getService();
120 if (service != nullptr) {
121 return IInterface::asBinder(service)->linkToDeath(recipient);
122 }
123 return INVALID_OPERATION;
124 }
125
unlinkToDeath(const sp<IBinder::DeathRecipient> & recipient)126 status_t ActivityManager::unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient) {
127 sp<IActivityManager> service = getService();
128 if (service != nullptr) {
129 return IInterface::asBinder(service)->unlinkToDeath(recipient);
130 }
131 return INVALID_OPERATION;
132 }
133
134 } // namespace android
135