blob: 526427663b0017439f60fac47294965f7ff9b02e [file] [log] [blame]
Ganesh Mahendran4d85b8c2017-11-02 14:43:38 +00001/*
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 Ganov86fc4f32018-02-22 00:29:30 -080018#include <unistd.h>
19
Chong Zhangb632bd52020-11-02 11:01:48 -080020#include <android/permission_manager.h>
Ganesh Mahendran4d85b8c2017-11-02 14:43:38 +000021#include <binder/ActivityManager.h>
22#include <binder/Binder.h>
23#include <binder/IServiceManager.h>
Yifei Zhang7086bcb2023-06-02 15:18:11 -070024#include <binder/ProcessState.h>
Ganesh Mahendran4d85b8c2017-11-02 14:43:38 +000025
26#include <utils/SystemClock.h>
27
28namespace android {
29
30ActivityManager::ActivityManager()
31{
32}
33
34sp<IActivityManager> ActivityManager::getService()
35{
36 std::lock_guard<Mutex> scoped_lock(mLock);
Ganesh Mahendran4d85b8c2017-11-02 14:43:38 +000037 sp<IActivityManager> service = mService;
Yifei Zhang7086bcb2023-06-02 15:18:11 -070038 if (ProcessState::self()->isThreadPoolStarted()) {
39 if (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
40 sp<IBinder> binder = defaultServiceManager()->waitForService(String16("activity"));
Ganesh Mahendran4d85b8c2017-11-02 14:43:38 +000041 service = interface_cast<IActivityManager>(binder);
42 mService = service;
43 }
Yifei Zhang7086bcb2023-06-02 15:18:11 -070044 } else {
45 ALOGI("Thread pool not started. Polling for activity service.");
46 int64_t startTime = 0;
47 while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
48 sp<IBinder> binder = defaultServiceManager()->checkService(String16("activity"));
49 if (binder == nullptr) {
50 // Wait for the activity service to come back...
51 if (startTime == 0) {
52 startTime = uptimeMillis();
53 ALOGI("Waiting for activity service");
54 } else if ((uptimeMillis() - startTime) > 1000000) {
55 ALOGW("Waiting too long for activity service, giving up");
56 service = nullptr;
57 break;
58 }
59 usleep(25000);
60 } else {
61 service = interface_cast<IActivityManager>(binder);
62 mService = service;
63 }
64 }
Ganesh Mahendran4d85b8c2017-11-02 14:43:38 +000065 }
Yifei Zhang7086bcb2023-06-02 15:18:11 -070066 return mService;
Ganesh Mahendran4d85b8c2017-11-02 14:43:38 +000067}
68
69int ActivityManager::openContentUri(const String16& stringUri)
70{
71 sp<IActivityManager> service = getService();
Yi Kong91635562018-06-07 14:38:36 -070072 return service != nullptr ? service->openContentUri(stringUri) : -1;
Ganesh Mahendran4d85b8c2017-11-02 14:43:38 +000073}
74
Chong Zhang15765342020-11-17 14:44:20 -080075status_t ActivityManager::registerUidObserver(const sp<IUidObserver>& observer,
Ganesh Mahendran4d85b8c2017-11-02 14:43:38 +000076 const int32_t event,
77 const int32_t cutpoint,
78 const String16& callingPackage)
79{
80 sp<IActivityManager> service = getService();
Yi Kong91635562018-06-07 14:38:36 -070081 if (service != nullptr) {
Chong Zhang15765342020-11-17 14:44:20 -080082 return service->registerUidObserver(observer, event, cutpoint, callingPackage);
Ganesh Mahendran4d85b8c2017-11-02 14:43:38 +000083 }
Chong Zhang15765342020-11-17 14:44:20 -080084 // ActivityManagerService appears dead. Return usual error code for dead service.
85 return DEAD_OBJECT;
Ganesh Mahendran4d85b8c2017-11-02 14:43:38 +000086}
87
Austin Borger805d8042023-04-21 20:07:49 -070088status_t ActivityManager::registerUidObserverForUids(const sp<IUidObserver>& observer,
89 const int32_t event, const int32_t cutpoint,
90 const String16& callingPackage,
91 const int32_t uids[], size_t nUids,
92 /*out*/ sp<IBinder>& observerToken) {
93 sp<IActivityManager> service = getService();
94 if (service != nullptr) {
95 return service->registerUidObserverForUids(observer, event, cutpoint, callingPackage, uids,
96 nUids, observerToken);
97 }
98 // ActivityManagerService appears dead. Return usual error code for dead service.
99 return DEAD_OBJECT;
100}
101
Chong Zhang15765342020-11-17 14:44:20 -0800102status_t ActivityManager::unregisterUidObserver(const sp<IUidObserver>& observer)
Ganesh Mahendran4d85b8c2017-11-02 14:43:38 +0000103{
104 sp<IActivityManager> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700105 if (service != nullptr) {
Chong Zhang15765342020-11-17 14:44:20 -0800106 return service->unregisterUidObserver(observer);
Ganesh Mahendran4d85b8c2017-11-02 14:43:38 +0000107 }
Chong Zhang15765342020-11-17 14:44:20 -0800108 // ActivityManagerService appears dead. Return usual error code for dead service.
109 return DEAD_OBJECT;
Ganesh Mahendran4d85b8c2017-11-02 14:43:38 +0000110}
111
Austin Borger805d8042023-04-21 20:07:49 -0700112status_t ActivityManager::addUidToObserver(const sp<IBinder>& observerToken,
113 const String16& callingPackage, int32_t uid) {
114 sp<IActivityManager> service = getService();
115 if (service != nullptr) {
116 return service->addUidToObserver(observerToken, callingPackage, uid);
117 }
118 // ActivityManagerService appears dead. Return usual error code for dead service.
119 return DEAD_OBJECT;
120}
121
122status_t ActivityManager::removeUidFromObserver(const sp<IBinder>& observerToken,
123 const String16& callingPackage, int32_t uid) {
124 sp<IActivityManager> service = getService();
125 if (service != nullptr) {
126 return service->removeUidFromObserver(observerToken, callingPackage, uid);
127 }
128 // ActivityManagerService appears dead. Return usual error code for dead service.
129 return DEAD_OBJECT;
130}
131
Svet Ganovfa851802018-03-27 17:17:46 -0700132bool ActivityManager::isUidActive(const uid_t uid, const String16& callingPackage)
133{
134 sp<IActivityManager> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700135 if (service != nullptr) {
Svet Ganovfa851802018-03-27 17:17:46 -0700136 return service->isUidActive(uid, callingPackage);
137 }
138 return false;
139}
140
Eric Laurent05595892018-10-18 14:56:24 -0700141int32_t ActivityManager::getUidProcessState(const uid_t uid, const String16& callingPackage)
142{
143 sp<IActivityManager> service = getService();
144 if (service != nullptr) {
145 return service->getUidProcessState(uid, callingPackage);
146 }
147 return PROCESS_STATE_UNKNOWN;
148}
149
Chong Zhangb632bd52020-11-02 11:01:48 -0800150status_t ActivityManager::checkPermission(const String16& permission,
151 const pid_t pid,
152 const uid_t uid,
153 int32_t* outResult) {
154 sp<IActivityManager> service = getService();
155 if (service != nullptr) {
156 return service->checkPermission(permission, pid, uid, outResult);
157 }
158 // ActivityManagerService appears dead. Return usual error code for dead service.
159 return DEAD_OBJECT;
160}
161
Eino-Ville Talvalaae8b20d2018-03-20 11:05:23 -0700162status_t ActivityManager::linkToDeath(const sp<IBinder::DeathRecipient>& recipient) {
163 sp<IActivityManager> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700164 if (service != nullptr) {
Eino-Ville Talvalaae8b20d2018-03-20 11:05:23 -0700165 return IInterface::asBinder(service)->linkToDeath(recipient);
166 }
167 return INVALID_OPERATION;
168}
169
170status_t ActivityManager::unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient) {
171 sp<IActivityManager> service = getService();
Yi Kong91635562018-06-07 14:38:36 -0700172 if (service != nullptr) {
Eino-Ville Talvalaae8b20d2018-03-20 11:05:23 -0700173 return IInterface::asBinder(service)->unlinkToDeath(recipient);
174 }
175 return INVALID_OPERATION;
176}
177
Steven Moreland6511af52019-09-26 16:05:45 -0700178} // namespace android