blob: 8ddfc652d333c7d34bcfbe712560cd62c61207ae [file] [log] [blame]
Soonil Nagarkarb6375a42020-01-29 15:23:06 -08001/*
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
17package com.android.server.location;
18
19import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE;
20import static android.app.ActivityManager.RunningAppProcessInfo.Importance;
21
22import android.annotation.Nullable;
23import android.app.ActivityManager;
24import android.content.Context;
25import android.os.Binder;
26
27import com.android.internal.annotations.GuardedBy;
28import com.android.internal.util.Preconditions;
29import com.android.server.FgThread;
30
31import java.util.Objects;
32import java.util.concurrent.CopyOnWriteArrayList;
33
34/**
35 * Provides accessors and listeners for all application foreground status. An application is
36 * considered foreground if it's uid's importance level is at or more important than
37 * {@link android.app.ActivityManager.RunningAppProcessInfo#IMPORTANCE_FOREGROUND_SERVICE}.
38 */
39public class AppForegroundHelper {
40
41 /**
42 * Listener for application foreground state changes.
43 */
44 public interface AppForegroundListener {
45 /**
46 * Called when an application's foreground state changes.
47 */
48 void onAppForegroundChanged(int uid, boolean foreground);
49 }
50
51 // importance constants decrement with increasing importance - this is our limit for an
52 // importance level we consider foreground.
53 private static final int FOREGROUND_IMPORTANCE_CUTOFF = IMPORTANCE_FOREGROUND_SERVICE;
54
55 private static boolean isForeground(@Importance int importance) {
56 return importance <= FOREGROUND_IMPORTANCE_CUTOFF;
57 }
58
59 private final Context mContext;
60 private final CopyOnWriteArrayList<AppForegroundListener> mListeners;
61
62 @GuardedBy("this")
63 @Nullable private ActivityManager mActivityManager;
64
65 public AppForegroundHelper(Context context) {
66 mContext = context;
67 mListeners = new CopyOnWriteArrayList<>();
68 }
69
70 /** Called when system is ready. */
71 public synchronized void onSystemReady() {
72 if (mActivityManager != null) {
73 return;
74 }
75
76 mActivityManager = Objects.requireNonNull(mContext.getSystemService(ActivityManager.class));
77 mActivityManager.addOnUidImportanceListener(this::onAppForegroundChanged,
78 FOREGROUND_IMPORTANCE_CUTOFF);
79 }
80
81 /**
82 * Adds a listener for app foreground changed events. Callbacks occur on an unspecified thread.
83 */
84 public void addListener(AppForegroundListener listener) {
85 mListeners.add(listener);
86 }
87
88 /**
89 * Removes a listener for app foreground changed events.
90 */
91 public void removeListener(AppForegroundListener listener) {
92 mListeners.remove(listener);
93 }
94
95 private void onAppForegroundChanged(int uid, @Importance int importance) {
96 // invoked on ui thread, move to fg thread so we don't block the ui thread
97 boolean foreground = isForeground(importance);
98 FgThread.getHandler().post(() -> {
99 for (AppForegroundListener listener : mListeners) {
100 listener.onAppForegroundChanged(uid, foreground);
101 }
102 });
103 }
104
105 /**
106 * Whether the given uid is currently foreground.
107 */
108 public boolean isAppForeground(int uid) {
109 return isForeground(getImportance(uid));
110 }
111
112 /**
113 * Retrieves the current importance of the given uid.
114 *
115 * @deprecated Prefer {@link #isAppForeground(int)}.
116 */
117 @Deprecated
118 @Importance
119 public int getImportance(int uid) {
120 synchronized (this) {
121 Preconditions.checkState(mActivityManager != null);
122 }
123
124 long identity = Binder.clearCallingIdentity();
125 try {
126 return mActivityManager.getUidImportance(uid);
127 } finally {
128 Binder.restoreCallingIdentity(identity);
129 }
130 }
131}