• 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 package com.android.server.location.injector;
18 
19 import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE;
20 import static android.app.ActivityManager.RunningAppProcessInfo.Importance;
21 
22 import java.util.concurrent.CopyOnWriteArrayList;
23 
24 /**
25  * Provides accessors and listeners for all application foreground status. An application is
26  * considered foreground if it's uid's importance level is at or more important than
27  * {@link android.app.ActivityManager.RunningAppProcessInfo#IMPORTANCE_FOREGROUND_SERVICE}.
28  */
29 public abstract class AppForegroundHelper {
30 
31     /**
32      * Listener for application foreground state changes.
33      */
34     public interface AppForegroundListener {
35         /**
36          * Called when an application's foreground state changes.
37          */
onAppForegroundChanged(int uid, boolean foreground)38         void onAppForegroundChanged(int uid, boolean foreground);
39     }
40 
41     // importance constants decrement with increasing importance - this is our limit for an
42     // importance level we consider foreground.
43     protected static final int FOREGROUND_IMPORTANCE_CUTOFF = IMPORTANCE_FOREGROUND_SERVICE;
44 
isForeground(@mportance int importance)45     protected static boolean isForeground(@Importance int importance) {
46         return importance <= FOREGROUND_IMPORTANCE_CUTOFF;
47     }
48 
49     private final CopyOnWriteArrayList<AppForegroundListener> mListeners;
50 
AppForegroundHelper()51     public AppForegroundHelper() {
52         mListeners = new CopyOnWriteArrayList<>();
53     }
54 
55     /**
56      * Adds a listener for app foreground changed events. Callbacks occur on an unspecified thread.
57      */
addListener(AppForegroundListener listener)58     public final void addListener(AppForegroundListener listener) {
59         mListeners.add(listener);
60     }
61 
62     /**
63      * Removes a listener for app foreground changed events.
64      */
removeListener(AppForegroundListener listener)65     public final void removeListener(AppForegroundListener listener) {
66         mListeners.remove(listener);
67     }
68 
notifyAppForeground(int uid, boolean foreground)69     protected final void notifyAppForeground(int uid, boolean foreground) {
70         for (AppForegroundListener listener : mListeners) {
71             listener.onAppForegroundChanged(uid, foreground);
72         }
73     }
74 
75     /**
76      * Whether the given uid is currently foreground.
77      */
isAppForeground(int uid)78     public abstract boolean isAppForeground(int uid);
79 }
80