| Chong Zhang | 8af8792 | 2020-11-11 14:46:30 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2020 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 | #define LOG_NDEBUG 0 | 
|  | 18 | #define LOG_TAG "AActivityManager" | 
|  | 19 | #include <utils/Log.h> | 
|  | 20 |  | 
|  | 21 | #include <android/activity_manager.h> | 
|  | 22 | #include <binder/ActivityManager.h> | 
|  | 23 |  | 
|  | 24 | namespace android { | 
|  | 25 | namespace activitymanager { | 
|  | 26 |  | 
|  | 27 | // Global instance of ActivityManager, service is obtained only on first use. | 
|  | 28 | static ActivityManager gAm; | 
|  | 29 | // String tag used with ActivityManager. | 
|  | 30 | static const String16& getTag() { | 
|  | 31 | static String16 tag("libandroid"); | 
|  | 32 | return tag; | 
|  | 33 | } | 
|  | 34 |  | 
|  | 35 | struct UidObserver : public BnUidObserver, public virtual IBinder::DeathRecipient { | 
|  | 36 | explicit UidObserver(const AActivityManager_onUidImportance& cb, | 
|  | 37 | int32_t cutpoint, void* cookie) | 
|  | 38 | : mCallback(cb), mImportanceCutpoint(cutpoint), mCookie(cookie), mRegistered(false) {} | 
|  | 39 | bool registerSelf(); | 
|  | 40 | void unregisterSelf(); | 
|  | 41 |  | 
|  | 42 | // IUidObserver | 
|  | 43 | void onUidGone(uid_t uid, bool disabled) override; | 
|  | 44 | void onUidActive(uid_t uid) override; | 
|  | 45 | void onUidIdle(uid_t uid, bool disabled) override; | 
|  | 46 | void onUidStateChanged(uid_t uid, int32_t procState, int64_t procStateSeq, | 
|  | 47 | int32_t capability) override; | 
| Austin Borger | 690c1ed | 2023-03-30 17:52:34 -0700 | [diff] [blame] | 48 | void onUidProcAdjChanged(uid_t uid, int32_t adj) override; | 
| Chong Zhang | 8af8792 | 2020-11-11 14:46:30 -0800 | [diff] [blame] | 49 |  | 
|  | 50 | // IBinder::DeathRecipient implementation | 
|  | 51 | void binderDied(const wp<IBinder>& who) override; | 
|  | 52 |  | 
|  | 53 | static int32_t procStateToImportance(int32_t procState); | 
|  | 54 | static int32_t importanceToProcState(int32_t importance); | 
|  | 55 |  | 
|  | 56 | AActivityManager_onUidImportance mCallback; | 
|  | 57 | int32_t mImportanceCutpoint; | 
|  | 58 | void* mCookie; | 
|  | 59 | std::mutex mRegisteredLock; | 
|  | 60 | bool mRegistered GUARDED_BY(mRegisteredLock); | 
|  | 61 | }; | 
|  | 62 |  | 
|  | 63 | //static | 
|  | 64 | int32_t UidObserver::procStateToImportance(int32_t procState) { | 
|  | 65 | // TODO: remove this after adding Importance to onUidStateChanged callback. | 
|  | 66 | if (procState == ActivityManager::PROCESS_STATE_NONEXISTENT) { | 
|  | 67 | return AACTIVITYMANAGER_IMPORTANCE_GONE; | 
|  | 68 | } else if (procState >= ActivityManager::PROCESS_STATE_HOME) { | 
|  | 69 | return AACTIVITYMANAGER_IMPORTANCE_CACHED; | 
|  | 70 | } else if (procState == ActivityManager::PROCESS_STATE_HEAVY_WEIGHT) { | 
|  | 71 | return AACTIVITYMANAGER_IMPORTANCE_CANT_SAVE_STATE; | 
|  | 72 | } else if (procState >= ActivityManager::PROCESS_STATE_TOP_SLEEPING) { | 
|  | 73 | return AACTIVITYMANAGER_IMPORTANCE_TOP_SLEEPING; | 
|  | 74 | } else if (procState >= ActivityManager::PROCESS_STATE_SERVICE) { | 
|  | 75 | return AACTIVITYMANAGER_IMPORTANCE_SERVICE; | 
|  | 76 | } else if (procState >= ActivityManager::PROCESS_STATE_TRANSIENT_BACKGROUND) { | 
|  | 77 | return AACTIVITYMANAGER_IMPORTANCE_PERCEPTIBLE; | 
|  | 78 | } else if (procState >= ActivityManager::PROCESS_STATE_IMPORTANT_FOREGROUND) { | 
|  | 79 | return AACTIVITYMANAGER_IMPORTANCE_VISIBLE; | 
|  | 80 | } else if (procState >= ActivityManager::PROCESS_STATE_FOREGROUND_SERVICE) { | 
|  | 81 | return AACTIVITYMANAGER_IMPORTANCE_FOREGROUND_SERVICE; | 
|  | 82 | } else { | 
|  | 83 | return AACTIVITYMANAGER_IMPORTANCE_FOREGROUND; | 
|  | 84 | } | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | //static | 
|  | 88 | int32_t UidObserver::importanceToProcState(int32_t importance) { | 
|  | 89 | // TODO: remove this after adding Importance to onUidStateChanged callback. | 
|  | 90 | if (importance == AACTIVITYMANAGER_IMPORTANCE_GONE) { | 
|  | 91 | return ActivityManager::PROCESS_STATE_NONEXISTENT; | 
|  | 92 | } else if (importance >= AACTIVITYMANAGER_IMPORTANCE_CACHED) { | 
|  | 93 | return ActivityManager::PROCESS_STATE_HOME; | 
|  | 94 | } else if (importance >= AACTIVITYMANAGER_IMPORTANCE_CANT_SAVE_STATE) { | 
|  | 95 | return ActivityManager::PROCESS_STATE_HEAVY_WEIGHT; | 
|  | 96 | } else if (importance >= AACTIVITYMANAGER_IMPORTANCE_TOP_SLEEPING) { | 
|  | 97 | return ActivityManager::PROCESS_STATE_TOP_SLEEPING; | 
|  | 98 | } else if (importance >= AACTIVITYMANAGER_IMPORTANCE_SERVICE) { | 
|  | 99 | return ActivityManager::PROCESS_STATE_SERVICE; | 
|  | 100 | } else if (importance >= AACTIVITYMANAGER_IMPORTANCE_PERCEPTIBLE) { | 
|  | 101 | return ActivityManager::PROCESS_STATE_TRANSIENT_BACKGROUND; | 
|  | 102 | } else if (importance >= AACTIVITYMANAGER_IMPORTANCE_VISIBLE) { | 
|  | 103 | return ActivityManager::PROCESS_STATE_IMPORTANT_FOREGROUND; | 
|  | 104 | } else if (importance >= AACTIVITYMANAGER_IMPORTANCE_FOREGROUND_SERVICE) { | 
|  | 105 | return ActivityManager::PROCESS_STATE_FOREGROUND_SERVICE; | 
|  | 106 | } else { | 
|  | 107 | return ActivityManager::PROCESS_STATE_TOP; | 
|  | 108 | } | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 |  | 
|  | 112 | void UidObserver::onUidGone(uid_t uid, bool disabled __unused) { | 
|  | 113 | std::scoped_lock lock{mRegisteredLock}; | 
|  | 114 |  | 
|  | 115 | if (mRegistered && mCallback) { | 
|  | 116 | mCallback(uid, AACTIVITYMANAGER_IMPORTANCE_GONE, mCookie); | 
|  | 117 | } | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | void UidObserver::onUidActive(uid_t uid __unused) {} | 
|  | 121 |  | 
|  | 122 | void UidObserver::onUidIdle(uid_t uid __unused, bool disabled __unused) {} | 
|  | 123 |  | 
| Austin Borger | 690c1ed | 2023-03-30 17:52:34 -0700 | [diff] [blame] | 124 | void UidObserver::onUidProcAdjChanged(uid_t uid __unused, int32_t adj __unused) {} | 
| Austin Borger | db9165b | 2022-02-17 00:22:33 +0000 | [diff] [blame] | 125 |  | 
| Chong Zhang | 8af8792 | 2020-11-11 14:46:30 -0800 | [diff] [blame] | 126 | void UidObserver::onUidStateChanged(uid_t uid, int32_t procState, | 
|  | 127 | int64_t procStateSeq __unused, | 
|  | 128 | int32_t capability __unused) { | 
|  | 129 | std::scoped_lock lock{mRegisteredLock}; | 
|  | 130 |  | 
|  | 131 | if (mRegistered && mCallback) { | 
|  | 132 | mCallback(uid, procStateToImportance(procState), mCookie); | 
|  | 133 | } | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | void UidObserver::binderDied(const wp<IBinder>& /*who*/) { | 
|  | 137 | // ActivityManager is dead, try to re-register. | 
|  | 138 | { | 
|  | 139 | std::scoped_lock lock{mRegisteredLock}; | 
|  | 140 | // If client already unregistered, don't try to re-register. | 
|  | 141 | if (!mRegistered) { | 
|  | 142 | return; | 
|  | 143 | } | 
|  | 144 | // Clear mRegistered to re-register. | 
|  | 145 | mRegistered = false; | 
|  | 146 | } | 
|  | 147 | registerSelf(); | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | bool UidObserver::registerSelf() { | 
|  | 151 | std::scoped_lock lock{mRegisteredLock}; | 
|  | 152 | if (mRegistered) { | 
|  | 153 | return true; | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | status_t res = gAm.linkToDeath(this); | 
|  | 157 | if (res != OK) { | 
|  | 158 | ALOGE("UidObserver: Failed to linkToDeath with ActivityManager (err %d)", res); | 
|  | 159 | return false; | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | // TODO: it seems only way to get all changes is to set cutoff to PROCESS_STATE_UNKNOWN. | 
|  | 163 | // But there is no equivalent of PROCESS_STATE_UNKNOWN in the UidImportance. | 
|  | 164 | // If mImportanceCutpoint is < 0, use PROCESS_STATE_UNKNOWN instead. | 
|  | 165 | res = gAm.registerUidObserver( | 
|  | 166 | this, | 
|  | 167 | ActivityManager::UID_OBSERVER_GONE | ActivityManager::UID_OBSERVER_PROCSTATE, | 
|  | 168 | (mImportanceCutpoint < 0) ? ActivityManager::PROCESS_STATE_UNKNOWN | 
|  | 169 | : importanceToProcState(mImportanceCutpoint), | 
|  | 170 | getTag()); | 
|  | 171 | if (res != OK) { | 
|  | 172 | ALOGE("UidObserver: Failed to register with ActivityManager (err %d)", res); | 
|  | 173 | gAm.unlinkToDeath(this); | 
|  | 174 | return false; | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | mRegistered = true; | 
|  | 178 | ALOGV("UidObserver: Registered with ActivityManager"); | 
|  | 179 | return true; | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | void UidObserver::unregisterSelf() { | 
|  | 183 | std::scoped_lock lock{mRegisteredLock}; | 
|  | 184 |  | 
|  | 185 | if (mRegistered) { | 
|  | 186 | gAm.unregisterUidObserver(this); | 
|  | 187 | gAm.unlinkToDeath(this); | 
|  | 188 | mRegistered = false; | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | ALOGV("UidObserver: Unregistered with ActivityManager"); | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | } // activitymanager | 
|  | 195 | } // android | 
|  | 196 |  | 
|  | 197 | using namespace android; | 
|  | 198 | using namespace activitymanager; | 
|  | 199 |  | 
|  | 200 | struct AActivityManager_UidImportanceListener : public UidObserver { | 
|  | 201 | }; | 
|  | 202 |  | 
|  | 203 | AActivityManager_UidImportanceListener* AActivityManager_addUidImportanceListener( | 
|  | 204 | AActivityManager_onUidImportance onUidImportance, int32_t importanceCutpoint, void* cookie) { | 
|  | 205 | sp<UidObserver> observer(new UidObserver(onUidImportance, importanceCutpoint, cookie)); | 
|  | 206 | if (observer == nullptr || !observer->registerSelf()) { | 
|  | 207 | return nullptr; | 
|  | 208 | } | 
|  | 209 | observer->incStrong((void *)AActivityManager_addUidImportanceListener); | 
|  | 210 | return static_cast<AActivityManager_UidImportanceListener*>(observer.get()); | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | void AActivityManager_removeUidImportanceListener( | 
|  | 214 | AActivityManager_UidImportanceListener* listener) { | 
|  | 215 | if (listener != nullptr) { | 
|  | 216 | UidObserver* observer = static_cast<UidObserver*>(listener); | 
|  | 217 | observer->unregisterSelf(); | 
|  | 218 | observer->decStrong((void *)AActivityManager_addUidImportanceListener); | 
|  | 219 | } | 
|  | 220 | } | 
|  | 221 |  | 
|  | 222 | bool AActivityManager_isUidActive(uid_t uid) { | 
|  | 223 | return gAm.isUidActive(uid, getTag()); | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | int32_t AActivityManager_getUidImportance(uid_t uid) { | 
|  | 227 | return UidObserver::procStateToImportance(gAm.getUidProcessState(uid, getTag())); | 
|  | 228 | } | 
|  | 229 |  |