1 /*
2  * Copyright (C) 2022 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 package com.android.server.appop;
18 
19 import static android.app.ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE;
20 import static android.app.ActivityManager.PROCESS_STATE_BOUND_TOP;
21 import static android.app.ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE;
22 import static android.app.ActivityManager.PROCESS_STATE_PERSISTENT_UI;
23 import static android.app.ActivityManager.PROCESS_STATE_RECEIVER;
24 import static android.app.ActivityManager.PROCESS_STATE_TOP;
25 import static android.app.ActivityManager.PROCESS_STATE_UNKNOWN;
26 import static android.app.AppOpsManager.UID_STATE_BACKGROUND;
27 import static android.app.AppOpsManager.UID_STATE_CACHED;
28 import static android.app.AppOpsManager.UID_STATE_FOREGROUND;
29 import static android.app.AppOpsManager.UID_STATE_FOREGROUND_SERVICE;
30 import static android.app.AppOpsManager.UID_STATE_PERSISTENT;
31 import static android.app.AppOpsManager.UID_STATE_TOP;
32 
33 import android.annotation.CallbackExecutor;
34 import android.util.SparseArray;
35 
36 import java.io.PrintWriter;
37 import java.util.concurrent.Executor;
38 
39 interface AppOpsUidStateTracker {
40 
41     // Map from process states to the uid states we track.
processStateToUidState(int procState)42     static int processStateToUidState(int procState) {
43         if (procState == PROCESS_STATE_UNKNOWN) {
44             return UID_STATE_CACHED;
45         }
46 
47         if (procState <= PROCESS_STATE_PERSISTENT_UI) {
48             return UID_STATE_PERSISTENT;
49         }
50 
51         if (procState <= PROCESS_STATE_TOP) {
52             return UID_STATE_TOP;
53         }
54 
55         if (procState <= PROCESS_STATE_BOUND_TOP) {
56             return UID_STATE_FOREGROUND;
57         }
58 
59         if (procState <= PROCESS_STATE_FOREGROUND_SERVICE) {
60             return UID_STATE_FOREGROUND_SERVICE;
61         }
62 
63         if (procState <= PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
64             return UID_STATE_FOREGROUND;
65         }
66 
67         if (procState <= PROCESS_STATE_RECEIVER) {
68             return UID_STATE_BACKGROUND;
69         }
70 
71         return UID_STATE_CACHED;
72     }
73 
74     /*
75      * begin data pushed from appopsservice
76      */
77 
updateUidProcState(int uid, int procState, int capability)78     void updateUidProcState(int uid, int procState, int capability);
79 
updateAppWidgetVisibility(SparseArray<String> uidPackageNames, boolean visible)80     void updateAppWidgetVisibility(SparseArray<String> uidPackageNames, boolean visible);
81 
82     /*
83      * end data pushed from appopsservice
84      */
85 
86     /**
87      * Gets the {@link android.app.AppOpsManager.UidState} that the uid current is in.
88      */
getUidState(int uid)89     int getUidState(int uid);
90 
91     /**
92      * Determines if the uid is in foreground.
93      */
isUidInForeground(int uid)94     boolean isUidInForeground(int uid);
95 
96     /**
97      * Given a uid, code, and mode, resolve any foregroundness to MODE_IGNORED or MODE_ALLOWED
98      */
evalMode(int uid, int code, int mode)99     int evalMode(int uid, int code, int mode);
100 
101     /**
102      * Listen to changes in {@link android.app.AppOpsManager.UidState}
103      */
addUidStateChangedCallback(@allbackExecutor Executor executor, UidStateChangedCallback callback)104     void addUidStateChangedCallback(@CallbackExecutor Executor executor,
105             UidStateChangedCallback callback);
106 
107     /**
108      * Remove a {@link UidStateChangedCallback}
109      */
removeUidStateChangedCallback(UidStateChangedCallback callback)110     void removeUidStateChangedCallback(UidStateChangedCallback callback);
111 
112     interface UidStateChangedCallback {
113         /**
114          * Invoked when a UID's {@link android.app.AppOpsManager.UidState} changes.
115          * @param uid The uid that changed.
116          * @param uidState The state that was changed to.
117          * @param foregroundModeMayChange True if there may be a op in MODE_FOREGROUND whose
118          *                               evaluated result may have changed.
119          */
onUidStateChanged(int uid, int uidState, boolean foregroundModeMayChange)120         void onUidStateChanged(int uid, int uidState, boolean foregroundModeMayChange);
121     }
122 
dumpUidState(PrintWriter pw, int uid, long nowElapsed)123     void dumpUidState(PrintWriter pw, int uid, long nowElapsed);
124 
dumpEvents(PrintWriter pw)125     void dumpEvents(PrintWriter pw);
126 }
127