1 /* 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 17 #include <unistd.h> 18 #include <fcntl.h> 19 20 #include <android/permission_manager.h> 21 #include <binder/ActivityManager.h> 22 #include <binder/IActivityManager.h> 23 #include <binder/Parcel.h> 24 #include <utils/Errors.h> 25 26 namespace android { 27 28 // ------------------------------------------------------------------------------------ 29 30 class BpActivityManager : public BpInterface<IActivityManager> 31 { 32 public: BpActivityManager(const sp<IBinder> & impl)33 explicit BpActivityManager(const sp<IBinder>& impl) 34 : BpInterface<IActivityManager>(impl) 35 { 36 } 37 openContentUri(const String16 & stringUri)38 virtual int openContentUri(const String16& stringUri) 39 { 40 Parcel data, reply; 41 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); 42 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) { 51 fd = fcntl(reply.readParcelFileDescriptor(), F_DUPFD_CLOEXEC, 0); 52 } 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 } 61 registerUidObserver(const sp<IUidObserver> & observer,const int32_t event,const int32_t cutpoint,const String16 & callingPackage)62 virtual status_t 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 status_t err = remote()->transact(REGISTER_UID_OBSERVER_TRANSACTION, data, &reply); 74 if (err != NO_ERROR || ((err = reply.readExceptionCode()) != NO_ERROR)) { 75 return err; 76 } 77 return OK; 78 } 79 unregisterUidObserver(const sp<IUidObserver> & observer)80 virtual status_t unregisterUidObserver(const sp<IUidObserver>& observer) 81 { 82 Parcel data, reply; 83 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); 84 data.writeStrongBinder(IInterface::asBinder(observer)); 85 status_t err = remote()->transact(UNREGISTER_UID_OBSERVER_TRANSACTION, data, &reply); 86 if (err != NO_ERROR || ((err = reply.readExceptionCode()) != NO_ERROR)) { 87 return err; 88 } 89 return OK; 90 } 91 isUidActive(const uid_t uid,const String16 & callingPackage)92 virtual bool isUidActive(const uid_t uid, const String16& callingPackage) 93 { 94 Parcel data, reply; 95 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); 96 data.writeInt32(uid); 97 data.writeString16(callingPackage); 98 remote()->transact(IS_UID_ACTIVE_TRANSACTION, data, &reply); 99 // fail on exception 100 if (reply.readExceptionCode() != 0) return false; 101 return reply.readInt32() == 1; 102 } 103 getUidProcessState(const uid_t uid,const String16 & callingPackage)104 virtual int32_t getUidProcessState(const uid_t uid, const String16& callingPackage) 105 { 106 Parcel data, reply; 107 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); 108 data.writeInt32(uid); 109 data.writeString16(callingPackage); 110 remote()->transact(GET_UID_PROCESS_STATE_TRANSACTION, data, &reply); 111 // fail on exception 112 if (reply.readExceptionCode() != 0) { 113 return ActivityManager::PROCESS_STATE_UNKNOWN; 114 } 115 return reply.readInt32(); 116 } 117 checkPermission(const String16 & permission,const pid_t pid,const uid_t uid,int32_t * outResult)118 virtual status_t checkPermission(const String16& permission, 119 const pid_t pid, 120 const uid_t uid, 121 int32_t* outResult) { 122 Parcel data, reply; 123 data.writeInterfaceToken(IActivityManager::getInterfaceDescriptor()); 124 data.writeString16(permission); 125 data.writeInt32(pid); 126 data.writeInt32(uid); 127 status_t err = remote()->transact(CHECK_PERMISSION_TRANSACTION, data, &reply); 128 if (err != NO_ERROR || ((err = reply.readExceptionCode()) != NO_ERROR)) { 129 return err; 130 } 131 *outResult = reply.readInt32(); 132 return NO_ERROR; 133 } 134 }; 135 136 // ------------------------------------------------------------------------------------ 137 138 IMPLEMENT_META_INTERFACE(ActivityManager, "android.app.IActivityManager") 139 140 } // namespace android 141