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