blob: 727ea6024b5705b1a35a5cb2c52c39e3280e6d89 [file] [log] [blame]
Ganesh Mahendran4d85b8c2017-11-02 14:43:381/*
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 08:29:3018#include <unistd.h>
19
Chong Zhangb632bd52020-11-02 19:01:4820#include <android/permission_manager.h>
Ganesh Mahendran4d85b8c2017-11-02 14:43:3821#include <binder/ActivityManager.h>
22#include <binder/Binder.h>
23#include <binder/IServiceManager.h>
24
25#include <utils/SystemClock.h>
26
27namespace android {
28
29ActivityManager::ActivityManager()
30{
31}
32
33sp<IActivityManager> ActivityManager::getService()
34{
35 std::lock_guard<Mutex> scoped_lock(mLock);
36 int64_t startTime = 0;
37 sp<IActivityManager> service = mService;
Yi Kong91635562018-06-07 21:38:3638 while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
Ganesh Mahendran4d85b8c2017-11-02 14:43:3839 sp<IBinder> binder = defaultServiceManager()->checkService(String16("activity"));
Yi Kong91635562018-06-07 21:38:3640 if (binder == nullptr) {
Ganesh Mahendran4d85b8c2017-11-02 14:43:3841 // Wait for the activity service to come back...
42 if (startTime == 0) {
43 startTime = uptimeMillis();
44 ALOGI("Waiting for activity service");
Svet Ganov25d78a12018-01-21 10:49:0045 } else if ((uptimeMillis() - startTime) > 1000000) {
Ganesh Mahendran4d85b8c2017-11-02 14:43:3846 ALOGW("Waiting too long for activity service, giving up");
Yi Kong91635562018-06-07 21:38:3647 service = nullptr;
Ganesh Mahendran4d85b8c2017-11-02 14:43:3848 break;
49 }
Svetoslav Ganov86fc4f32018-02-22 08:29:3050 usleep(25000);
Ganesh Mahendran4d85b8c2017-11-02 14:43:3851 } else {
52 service = interface_cast<IActivityManager>(binder);
53 mService = service;
54 }
55 }
56 return service;
57}
58
59int ActivityManager::openContentUri(const String16& stringUri)
60{
61 sp<IActivityManager> service = getService();
Yi Kong91635562018-06-07 21:38:3662 return service != nullptr ? service->openContentUri(stringUri) : -1;
Ganesh Mahendran4d85b8c2017-11-02 14:43:3863}
64
65void 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();
Yi Kong91635562018-06-07 21:38:3671 if (service != nullptr) {
Ganesh Mahendran4d85b8c2017-11-02 14:43:3872 service->registerUidObserver(observer, event, cutpoint, callingPackage);
73 }
74}
75
76void ActivityManager::unregisterUidObserver(const sp<IUidObserver>& observer)
77{
78 sp<IActivityManager> service = getService();
Yi Kong91635562018-06-07 21:38:3679 if (service != nullptr) {
Ganesh Mahendran4d85b8c2017-11-02 14:43:3880 service->unregisterUidObserver(observer);
81 }
82}
83
Svet Ganovfa851802018-03-28 00:17:4684bool ActivityManager::isUidActive(const uid_t uid, const String16& callingPackage)
85{
86 sp<IActivityManager> service = getService();
Yi Kong91635562018-06-07 21:38:3687 if (service != nullptr) {
Svet Ganovfa851802018-03-28 00:17:4688 return service->isUidActive(uid, callingPackage);
89 }
90 return false;
91}
92
Eric Laurent05595892018-10-18 21:56:2493int32_t ActivityManager::getUidProcessState(const uid_t uid, const String16& callingPackage)
94{
95 sp<IActivityManager> service = getService();
96 if (service != nullptr) {
97 return service->getUidProcessState(uid, callingPackage);
98 }
99 return PROCESS_STATE_UNKNOWN;
100}
101
Chong Zhangb632bd52020-11-02 19:01:48102status_t ActivityManager::checkPermission(const String16& permission,
103 const pid_t pid,
104 const uid_t uid,
105 int32_t* outResult) {
106 sp<IActivityManager> service = getService();
107 if (service != nullptr) {
108 return service->checkPermission(permission, pid, uid, outResult);
109 }
110 // ActivityManagerService appears dead. Return usual error code for dead service.
111 return DEAD_OBJECT;
112}
113
Eino-Ville Talvalaae8b20d2018-03-20 18:05:23114status_t ActivityManager::linkToDeath(const sp<IBinder::DeathRecipient>& recipient) {
115 sp<IActivityManager> service = getService();
Yi Kong91635562018-06-07 21:38:36116 if (service != nullptr) {
Eino-Ville Talvalaae8b20d2018-03-20 18:05:23117 return IInterface::asBinder(service)->linkToDeath(recipient);
118 }
119 return INVALID_OPERATION;
120}
121
122status_t ActivityManager::unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient) {
123 sp<IActivityManager> service = getService();
Yi Kong91635562018-06-07 21:38:36124 if (service != nullptr) {
Eino-Ville Talvalaae8b20d2018-03-20 18:05:23125 return IInterface::asBinder(service)->unlinkToDeath(recipient);
126 }
127 return INVALID_OPERATION;
128}
129
Steven Moreland6511af52019-09-26 23:05:45130} // namespace android