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