1 /*
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 #pragma once
18 
19 #ifndef __ANDROID_VNDK__
20 
21 #include <android/app/ProcessStateEnum.h>
22 #include <binder/IActivityManager.h>
23 #include <utils/Mutex.h>
24 
25 // ---------------------------------------------------------------------------
26 namespace android {
27 
28 #define DECLARE_PROCESS_STATE(name) \
29     PROCESS_STATE_##name = (int32_t) app::ProcessStateEnum::name
30 
31 class ActivityManager
32 {
33 public:
34     enum {
35         // Flag for registerUidObserver: report uid state changed
36         UID_OBSERVER_PROCSTATE = 1 << 0,
37         // Flag for registerUidObserver: report uid gone
38         UID_OBSERVER_GONE = 1 << 1,
39         // Flag for registerUidObserver: report uid has become idle
40         UID_OBSERVER_IDLE = 1 << 2,
41         // Flag for registerUidObserver: report uid has become active
42         UID_OBSERVER_ACTIVE = 1 << 3,
43         // Flag for registerUidObserver: report uid cached state has changed
44         UID_OBSERVER_CACHED = 1 << 4,
45         // Flag for registerUidObserver: report uid capability has changed
46         UID_OBSERVER_CAPABILITY = 1 << 5,
47         // Flag for registerUidObserver: report pid oom adj has changed
48         UID_OBSERVER_PROC_OOM_ADJ = 1 << 6,
49     };
50 
51     // PROCESS_STATE_* must come from frameworks/base/core/java/android/app/ProcessStateEnum.aidl.
52     // This is to make sure that Java side uses the same values as native.
53     enum {
54         DECLARE_PROCESS_STATE(UNKNOWN),
55         DECLARE_PROCESS_STATE(PERSISTENT),
56         DECLARE_PROCESS_STATE(PERSISTENT_UI),
57         DECLARE_PROCESS_STATE(TOP),
58         DECLARE_PROCESS_STATE(BOUND_TOP),
59         DECLARE_PROCESS_STATE(FOREGROUND_SERVICE),
60         DECLARE_PROCESS_STATE(BOUND_FOREGROUND_SERVICE),
61         DECLARE_PROCESS_STATE(IMPORTANT_FOREGROUND),
62         DECLARE_PROCESS_STATE(IMPORTANT_BACKGROUND),
63         DECLARE_PROCESS_STATE(TRANSIENT_BACKGROUND),
64         DECLARE_PROCESS_STATE(BACKUP),
65         DECLARE_PROCESS_STATE(SERVICE),
66         DECLARE_PROCESS_STATE(RECEIVER),
67         DECLARE_PROCESS_STATE(TOP_SLEEPING),
68         DECLARE_PROCESS_STATE(HEAVY_WEIGHT),
69         DECLARE_PROCESS_STATE(HOME),
70         DECLARE_PROCESS_STATE(LAST_ACTIVITY),
71         DECLARE_PROCESS_STATE(CACHED_ACTIVITY),
72         DECLARE_PROCESS_STATE(CACHED_ACTIVITY_CLIENT),
73         DECLARE_PROCESS_STATE(CACHED_RECENT),
74         DECLARE_PROCESS_STATE(CACHED_EMPTY),
75         DECLARE_PROCESS_STATE(NONEXISTENT),
76     };
77 
78     ActivityManager();
79 
80     int openContentUri(const String16& stringUri);
81     status_t registerUidObserver(const sp<IUidObserver>& observer,
82                              const int32_t event,
83                              const int32_t cutpoint,
84                              const String16& callingPackage);
85     status_t registerUidObserverForUids(const sp<IUidObserver>& observer, const int32_t event,
86                                         const int32_t cutpoint, const String16& callingPackage,
87                                         const int32_t uids[], size_t nUids,
88                                         /*out*/ sp<IBinder>& observerToken);
89     status_t unregisterUidObserver(const sp<IUidObserver>& observer);
90     status_t addUidToObserver(const sp<IBinder>& observerToken, const String16& callingPackage,
91                               int32_t uid);
92     status_t removeUidFromObserver(const sp<IBinder>& observerToken, const String16& callingPackage,
93                                    int32_t uid);
94     bool isUidActive(const uid_t uid, const String16& callingPackage);
95     int getUidProcessState(const uid_t uid, const String16& callingPackage);
96     status_t checkPermission(const String16& permission, const pid_t pid, const uid_t uid, int32_t* outResult);
97 
98     status_t linkToDeath(const sp<IBinder::DeathRecipient>& recipient);
99     status_t unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient);
100 
101 private:
102     Mutex mLock;
103     sp<IActivityManager> mService;
104     sp<IActivityManager> getService();
105 };
106 
107 
108 } // namespace android
109 // ---------------------------------------------------------------------------
110 #else // __ANDROID_VNDK__
111 #error "This header is not visible to vendors"
112 #endif // __ANDROID_VNDK__
113