blob: e9f5aae34727f309691d82af7bfe8a03fe6bba44 [file] [log] [blame]
Sudheer Shanka6ef26f12016-11-23 15:52:26 -08001/*
2 * Copyright (C) 2016 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
Nick Kralevichec9ec7d2016-12-17 19:47:27 -080017#include <unistd.h>
18#include <fcntl.h>
19
Chong Zhangb632bd52020-11-02 11:01:48 -080020#include <android/permission_manager.h>
Eric Laurent05595892018-10-18 14:56:24 -070021#include <binder/ActivityManager.h>
Sudheer Shanka6ef26f12016-11-23 15:52:26 -080022#include <binder/IActivityManager.h>
Sudheer Shanka6ef26f12016-11-23 15:52:26 -080023#include <binder/Parcel.h>
Chong Zhangb632bd52020-11-02 11:01:48 -080024#include <utils/Errors.h>
Sudheer Shanka6ef26f12016-11-23 15:52:26 -080025
26namespace android {
27
28// ------------------------------------------------------------------------------------
29
30class BpActivityManager : public BpInterface<IActivityManager>
31{
32public:
33 explicit BpActivityManager(const sp<IBinder>& impl)
34 : BpInterface<IActivityManager>(impl)
35 {
36 }
37
38 virtual int openContentUri(const String16& stringUri)
39 {
40 Parcel data, reply;
Sudheer Shankaff81a092017-03-02 12:27:58 -080041 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor());
Sudheer Shanka6ef26f12016-11-23 15:52:26 -080042 data.writeString16(stringUri);
43 status_t ret = remote()->transact(OPEN_CONTENT_URI_TRANSACTION, data, & reply);
44 int fd = -1;
45 if (ret == NO_ERROR) {
46 int32_t exceptionCode = reply.readExceptionCode();
47 if (!exceptionCode) {
48 // Success is indicated here by a nonzero int followed by the fd;
49 // failure by a zero int with no data following.
50 if (reply.readInt32() != 0) {
Nick Kralevichec9ec7d2016-12-17 19:47:27 -080051 fd = fcntl(reply.readParcelFileDescriptor(), F_DUPFD_CLOEXEC, 0);
Sudheer Shanka6ef26f12016-11-23 15:52:26 -080052 }
53 } else {
54 // An exception was thrown back; fall through to return failure
55 ALOGD("openContentUri(%s) caught exception %d\n",
56 String8(stringUri).string(), exceptionCode);
57 }
58 }
59 return fd;
60 }
Ganesh Mahendran4d85b8c2017-11-02 14:43:38 +000061
62 virtual void registerUidObserver(const sp<IUidObserver>& observer,
63 const int32_t event,
64 const int32_t cutpoint,
65 const String16& callingPackage)
66 {
67 Parcel data, reply;
68 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor());
69 data.writeStrongBinder(IInterface::asBinder(observer));
70 data.writeInt32(event);
71 data.writeInt32(cutpoint);
72 data.writeString16(callingPackage);
73 remote()->transact(REGISTER_UID_OBSERVER_TRANSACTION, data, &reply);
74 }
75
76 virtual void unregisterUidObserver(const sp<IUidObserver>& observer)
77 {
78 Parcel data, reply;
79 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor());
80 data.writeStrongBinder(IInterface::asBinder(observer));
81 remote()->transact(UNREGISTER_UID_OBSERVER_TRANSACTION, data, &reply);
82 }
Svet Ganovfa851802018-03-27 17:17:46 -070083
84 virtual bool isUidActive(const uid_t uid, const String16& callingPackage)
85 {
86 Parcel data, reply;
87 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor());
88 data.writeInt32(uid);
89 data.writeString16(callingPackage);
90 remote()->transact(IS_UID_ACTIVE_TRANSACTION, data, &reply);
91 // fail on exception
92 if (reply.readExceptionCode() != 0) return false;
93 return reply.readInt32() == 1;
94 }
Eric Laurent05595892018-10-18 14:56:24 -070095
96 virtual int32_t getUidProcessState(const uid_t uid, const String16& callingPackage)
97 {
98 Parcel data, reply;
99 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor());
100 data.writeInt32(uid);
101 data.writeString16(callingPackage);
102 remote()->transact(GET_UID_PROCESS_STATE_TRANSACTION, data, &reply);
103 // fail on exception
104 if (reply.readExceptionCode() != 0) {
105 return ActivityManager::PROCESS_STATE_UNKNOWN;
106 }
107 return reply.readInt32();
108 }
Chong Zhangb632bd52020-11-02 11:01:48 -0800109
110 virtual status_t checkPermission(const String16& permission,
111 const pid_t pid,
112 const uid_t uid,
113 int32_t* outResult) {
114 Parcel data, reply;
115 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor());
116 data.writeString16(permission);
117 data.writeInt32(pid);
118 data.writeInt32(uid);
119 status_t err = remote()->transact(CHECK_PERMISSION_TRANSACTION, data, &reply);
120 if (err != NO_ERROR || ((err = reply.readExceptionCode()) != NO_ERROR)) {
121 return err;
122 }
123 *outResult = reply.readInt32();
124 return NO_ERROR;
125 }
Sudheer Shanka6ef26f12016-11-23 15:52:26 -0800126};
127
128// ------------------------------------------------------------------------------------
129
130IMPLEMENT_META_INTERFACE(ActivityManager, "android.app.IActivityManager");
131
Steven Moreland6511af52019-09-26 16:05:45 -0700132} // namespace android