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         // UID_STATE_NONEXISTENT is deliberately excluded here
72         return UID_STATE_CACHED;
73     }
74 
75     /*
76      * begin data pushed from appopsservice
77      */
78 
updateUidProcState(int uid, int procState, int capability)79     void updateUidProcState(int uid, int procState, int capability);
80 
updateAppWidgetVisibility(SparseArray<String> uidPackageNames, boolean visible)81     void updateAppWidgetVisibility(SparseArray<String> uidPackageNames, boolean visible);
82 
83     /*
84      * end data pushed from appopsservice
85      */
86 
87     /**
88      * Gets the {@link android.app.AppOpsManager.UidState} that the uid current is in.
89      */
getUidState(int uid)90     int getUidState(int uid);
91 
92     /**
93      * Determines if the uid is in foreground.
94      */
isUidInForeground(int uid)95     boolean isUidInForeground(int uid);
96 
97     /**
98      * Given a uid, code, and mode, resolve any foregroundness to MODE_IGNORED or MODE_ALLOWED
99      */
evalMode(int uid, int code, int mode)100     int evalMode(int uid, int code, int mode);
101 
102     /**
103      * Listen to changes in {@link android.app.AppOpsManager.UidState}
104      */
addUidStateChangedCallback(@allbackExecutor Executor executor, UidStateChangedCallback callback)105     void addUidStateChangedCallback(@CallbackExecutor Executor executor,
106             UidStateChangedCallback callback);
107 
108     /**
109      * Remove a {@link UidStateChangedCallback}
110      */
removeUidStateChangedCallback(UidStateChangedCallback callback)111     void removeUidStateChangedCallback(UidStateChangedCallback callback);
112 
113     interface UidStateChangedCallback {
114         /**
115          * Invoked when a UID's {@link android.app.AppOpsManager.UidState} changes.
116          * @param uid The uid that changed.
117          * @param uidState The state that was changed to.
118          * @param foregroundModeMayChange True if there may be a op in MODE_FOREGROUND whose
119          *                               evaluated result may have changed.
120          */
onUidStateChanged(int uid, int uidState, boolean foregroundModeMayChange)121         void onUidStateChanged(int uid, int uidState, boolean foregroundModeMayChange);
122     }
123 
dumpUidState(PrintWriter pw, int uid, long nowElapsed)124     void dumpUidState(PrintWriter pw, int uid, long nowElapsed);
125 
dumpEvents(PrintWriter pw)126     void dumpEvents(PrintWriter pw);
127 }
128