blob: e71391f9072fc767cc68ba638b347baeec9b8e0e [file] [log] [blame]
Sunny Goyal27835952017-01-13 12:15:53 -08001/*
2 * Copyright (C) 2017 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.launcher3;
18
Tony Wickhamb4821882021-04-23 14:26:45 -070019import static com.android.launcher3.util.SystemUiController.UI_STATE_FULLSCREEN_TASK;
Sunny Goyal8c48d8b2019-01-25 15:10:18 -080020
Sunny Goyal7eff40f2018-04-11 15:30:46 -070021import static java.lang.annotation.RetentionPolicy.SOURCE;
22
Sunny Goyal27835952017-01-13 12:15:53 -080023import android.app.Activity;
24import android.content.Context;
25import android.content.ContextWrapper;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070026import android.content.Intent;
Winson Chung1a77c3d2018-04-11 12:47:47 -070027import android.content.res.Configuration;
Yein Jo18446d02022-09-19 22:18:09 +000028import android.os.Bundle;
29import android.window.OnBackInvokedDispatcher;
Sunny Goyal27835952017-01-13 12:15:53 -080030
Winson Chungef528762019-09-06 12:05:52 -070031import androidx.annotation.IntDef;
32
Sunny Goyalfde55052018-02-01 14:46:13 -080033import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener;
Hyunyoung Songfc007472018-10-25 14:09:50 -070034import com.android.launcher3.logging.StatsLogManager;
Yein Jo18446d02022-09-19 22:18:09 +000035import com.android.launcher3.testing.TestLogging;
36import com.android.launcher3.testing.shared.TestProtocol;
Sunny Goyal8392c822017-06-20 10:03:56 -070037import com.android.launcher3.util.SystemUiController;
Sunny Goyal56863332019-05-22 14:13:53 -070038import com.android.launcher3.util.ViewCache;
Sunny Goyal54fa1102022-12-07 22:48:37 -080039import com.android.launcher3.views.ActivityContext;
Tony Wickhamb4821882021-04-23 14:26:45 -070040import com.android.launcher3.views.ScrimView;
Sunny Goyala535ae42017-02-27 10:07:13 -080041
Sunny Goyale43d00d2018-05-14 14:23:18 -070042import java.io.PrintWriter;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070043import java.lang.annotation.Retention;
Sunny Goyalfde55052018-02-01 14:46:13 -080044import java.util.ArrayList;
Brian Isganitis099945b2022-01-31 18:15:00 -050045import java.util.List;
Sunny Goyalfde55052018-02-01 14:46:13 -080046
Samuel Fufaa579ddc2020-02-27 16:59:19 -080047/**
48 * Launcher BaseActivity
49 */
Sunny Goyal54fa1102022-12-07 22:48:37 -080050public abstract class BaseActivity extends Activity implements ActivityContext {
Sunny Goyal27835952017-01-13 12:15:53 -080051
Sunny Goyalfa395362019-12-11 10:00:47 -080052 private static final String TAG = "BaseActivity";
53
Sunny Goyal7eff40f2018-04-11 15:30:46 -070054 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
55 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
Sunny Goyal1c63c722018-06-05 16:00:34 -070056 public static final int INVISIBLE_BY_PENDING_FLAGS = 1 << 2;
57
58 // This is not treated as invisibility flag, but adds as a hint for an incomplete transition.
59 // When the wallpaper animation runs, it replaces this flag with a proper invisibility
60 // flag, INVISIBLE_BY_PENDING_FLAGS only for the duration of that animation.
61 public static final int PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION = 1 << 3;
62
63 private static final int INVISIBLE_FLAGS =
64 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS | INVISIBLE_BY_PENDING_FLAGS;
65 public static final int STATE_HANDLER_INVISIBILITY_FLAGS =
66 INVISIBLE_BY_STATE_HANDLER | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070067 public static final int INVISIBLE_ALL =
Sunny Goyal1c63c722018-06-05 16:00:34 -070068 INVISIBLE_FLAGS | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070069
70 @Retention(SOURCE)
71 @IntDef(
72 flag = true,
Sunny Goyal1c63c722018-06-05 16:00:34 -070073 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS,
74 INVISIBLE_BY_PENDING_FLAGS, PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION})
Sunny Goyal7eff40f2018-04-11 15:30:46 -070075 public @interface InvisibilityFlags{}
76
Sunny Goyalfde55052018-02-01 14:46:13 -080077 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070078 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
79 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080080
Sunny Goyal27835952017-01-13 12:15:53 -080081 protected DeviceProfile mDeviceProfile;
Sunny Goyal8392c822017-06-20 10:03:56 -070082 protected SystemUiController mSystemUiController;
Sunny Goyal977838b2022-06-27 13:15:41 -070083 private StatsLogManager mStatsLogManager;
Sunny Goyal27835952017-01-13 12:15:53 -080084
Sunny Goyal210e1742019-10-17 12:05:38 -070085
86 public static final int ACTIVITY_STATE_STARTED = 1 << 0;
87 public static final int ACTIVITY_STATE_RESUMED = 1 << 1;
88
Sunny Goyal3483c522018-04-12 11:23:33 -070089 /**
Sunny Goyal210e1742019-10-17 12:05:38 -070090 * State flags indicating that the activity has received one frame after resume, and was
91 * not immediately paused.
92 */
93 public static final int ACTIVITY_STATE_DEFERRED_RESUMED = 1 << 2;
94
95 public static final int ACTIVITY_STATE_WINDOW_FOCUSED = 1 << 3;
96
97 /**
98 * State flag indicating if the user is active or the activity when to background as a result
Sunny Goyal3483c522018-04-12 11:23:33 -070099 * of user action.
100 * @see #isUserActive()
101 */
Sunny Goyal210e1742019-10-17 12:05:38 -0700102 public static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 4;
103
104 /**
Winson Chung034ce6f2020-05-14 10:49:30 -0700105 * State flag indicating if the user will be active shortly.
106 */
107 public static final int ACTIVITY_STATE_USER_WILL_BE_ACTIVE = 1 << 5;
108
109 /**
Sunny Goyal210e1742019-10-17 12:05:38 -0700110 * State flag indicating that a state transition is in progress
111 */
Winson Chung034ce6f2020-05-14 10:49:30 -0700112 public static final int ACTIVITY_STATE_TRANSITION_ACTIVE = 1 << 6;
Sunny Goyal3483c522018-04-12 11:23:33 -0700113
114 @Retention(SOURCE)
115 @IntDef(
116 flag = true,
Sunny Goyal210e1742019-10-17 12:05:38 -0700117 value = {ACTIVITY_STATE_STARTED,
118 ACTIVITY_STATE_RESUMED,
119 ACTIVITY_STATE_DEFERRED_RESUMED,
120 ACTIVITY_STATE_WINDOW_FOCUSED,
121 ACTIVITY_STATE_USER_ACTIVE,
122 ACTIVITY_STATE_TRANSITION_ACTIVE})
Sunny Goyal3483c522018-04-12 11:23:33 -0700123 public @interface ActivityFlags{}
124
125 @ActivityFlags
126 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700127
Winson Chung9800e732018-04-04 13:33:23 -0700128 // When the recents animation is running, the visibility of the Launcher is managed by the
129 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700130 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800131
Sunny Goyal56863332019-05-22 14:13:53 -0700132 private final ViewCache mViewCache = new ViewCache();
133
Sunny Goyal59969372021-05-06 12:11:44 -0700134 @Override
Sunny Goyal56863332019-05-22 14:13:53 -0700135 public ViewCache getViewCache() {
136 return mViewCache;
137 }
138
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800139 @Override
Sunny Goyal27835952017-01-13 12:15:53 -0800140 public DeviceProfile getDeviceProfile() {
141 return mDeviceProfile;
142 }
143
Brian Isganitis099945b2022-01-31 18:15:00 -0500144 @Override
145 public List<OnDeviceProfileChangeListener> getOnDeviceProfileChangeListeners() {
146 return mDPChangeListeners;
147 }
148
thiruramc96873c2021-02-11 15:13:47 -0800149 /**
150 * Returns {@link StatsLogManager} for user event logging.
151 */
Sunny Goyal177785e2021-07-29 15:48:24 -0700152 @Override
thiruramc96873c2021-02-11 15:13:47 -0800153 public StatsLogManager getStatsLogManager() {
Hyunyoung Songfc007472018-10-25 14:09:50 -0700154 if (mStatsLogManager == null) {
Hyunyoung Song801f81f2020-06-19 02:58:53 -0700155 mStatsLogManager = StatsLogManager.newInstance(this);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700156 }
157 return mStatsLogManager;
158 }
159
Sunny Goyal8392c822017-06-20 10:03:56 -0700160 public SystemUiController getSystemUiController() {
161 if (mSystemUiController == null) {
162 mSystemUiController = new SystemUiController(getWindow());
163 }
164 return mSystemUiController;
165 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700166
Tony Wickhamb4821882021-04-23 14:26:45 -0700167 public ScrimView getScrimView() {
168 return null;
169 }
170
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700171 @Override
172 public void onActivityResult(int requestCode, int resultCode, Intent data) {
173 super.onActivityResult(requestCode, resultCode, data);
174 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800175
176 @Override
Yein Jo18446d02022-09-19 22:18:09 +0000177 protected void onCreate(Bundle savedInstanceState) {
178 super.onCreate(savedInstanceState);
Fengjiang Lie884c2c2022-12-19 14:42:14 -0800179 registerBackDispatcher();
Yein Jo18446d02022-09-19 22:18:09 +0000180 }
181
182 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800183 protected void onStart() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700184 addActivityFlags(ACTIVITY_STATE_STARTED);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800185 super.onStart();
186 }
187
188 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700189 protected void onResume() {
Mady Mellor9a90c2d2022-09-14 15:17:21 -0700190 setResumed();
Tracy Zhoua706f002018-03-28 13:55:19 -0700191 super.onResume();
192 }
193
194 @Override
195 protected void onUserLeaveHint() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700196 removeActivityFlags(ACTIVITY_STATE_USER_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700197 super.onUserLeaveHint();
198 }
199
200 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700201 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
202 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
203 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
204 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
205 }
206 }
207
208 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800209 protected void onStop() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700210 removeActivityFlags(ACTIVITY_STATE_STARTED | ACTIVITY_STATE_USER_ACTIVE);
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700211 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800212 super.onStop();
Winson Chunga36c8002019-06-11 16:15:54 -0700213
214 // Reset the overridden sysui flags used for the task-swipe launch animation, this is a
215 // catch all for if we do not get resumed (and therefore not paused below)
Tony Wickhamb4821882021-04-23 14:26:45 -0700216 getSystemUiController().updateUiState(UI_STATE_FULLSCREEN_TASK, 0);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800217 }
218
Sunny Goyal3483c522018-04-12 11:23:33 -0700219 @Override
220 protected void onPause() {
Mady Mellor9a90c2d2022-09-14 15:17:21 -0700221 setPaused();
Sunny Goyal3483c522018-04-12 11:23:33 -0700222 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000223
224 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
225 // here instead of at the end of the animation because the start of the new activity does
226 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
227 // back to the new app (causing a flash)
Tony Wickhamb4821882021-04-23 14:26:45 -0700228 getSystemUiController().updateUiState(UI_STATE_FULLSCREEN_TASK, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700229 }
230
Sunny Goyal210e1742019-10-17 12:05:38 -0700231 @Override
232 public void onWindowFocusChanged(boolean hasFocus) {
233 super.onWindowFocusChanged(hasFocus);
234 if (hasFocus) {
235 addActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
236 } else {
237 removeActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
238 }
239
240 }
241
Fengjiang Lie884c2c2022-12-19 14:42:14 -0800242 protected void registerBackDispatcher() {
243 if (Utilities.ATLEAST_T) {
244 getOnBackInvokedDispatcher().registerOnBackInvokedCallback(
245 OnBackInvokedDispatcher.PRIORITY_DEFAULT,
246 () -> {
247 onBackPressed();
248 TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "onBackInvoked");
249 });
250 }
251 }
252
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800253 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700254 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
255 }
256
257 /**
258 * isResumed in already defined as a hidden final method in Activity.java
259 */
260 public boolean hasBeenResumed() {
261 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800262 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800263
Mady Mellor9a90c2d2022-09-14 15:17:21 -0700264 /**
265 * Sets the activity to appear as paused.
266 */
267 public void setPaused() {
268 removeActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_DEFERRED_RESUMED);
269 }
270
271 /**
272 * Sets the activity to appear as resumed.
273 */
274 public void setResumed() {
275 addActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE);
276 removeActivityFlags(ACTIVITY_STATE_USER_WILL_BE_ACTIVE);
277 }
278
Tracy Zhoua706f002018-03-28 13:55:19 -0700279 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700280 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700281 }
282
Sunny Goyal210e1742019-10-17 12:05:38 -0700283 public int getActivityFlags() {
284 return mActivityFlags;
285 }
286
287 protected void addActivityFlags(int flags) {
288 mActivityFlags |= flags;
289 onActivityFlagsChanged(flags);
290 }
291
292 protected void removeActivityFlags(int flags) {
293 mActivityFlags &= ~flags;
294 onActivityFlagsChanged(flags);
295 }
296
297 protected void onActivityFlagsChanged(int changeBits) { }
298
Winson Chung1a77c3d2018-04-11 12:47:47 -0700299 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
300 mMultiWindowModeChangedListeners.add(listener);
301 }
302
303 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
304 mMultiWindowModeChangedListeners.remove(listener);
305 }
306
Sunny Goyalf633ef52018-03-13 09:57:05 -0700307 /**
Winson Chung9800e732018-04-04 13:33:23 -0700308 * Used to set the override visibility state, used only to handle the transition home with the
309 * recents animation.
Sunny Goyalb65d7662021-03-07 15:09:11 -0800310 * @see QuickstepTransitionManager#createWallpaperOpenRunner
Winson Chung9800e732018-04-04 13:33:23 -0700311 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700312 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
313 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700314 }
315
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700316 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
317 mForceInvisible &= ~flag;
318 }
319
Winson Chung9800e732018-04-04 13:33:23 -0700320 /**
321 * @return Wether this activity should be considered invisible regardless of actual visibility.
322 */
323 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700324 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
325 }
326
327 public boolean hasSomeInvisibleFlag(int mask) {
328 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700329 }
330
Winson Chung1a77c3d2018-04-11 12:47:47 -0700331 public interface MultiWindowModeChangedListener {
332 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
333 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700334
Winson Chungef528762019-09-06 12:05:52 -0700335 protected void dumpMisc(String prefix, PrintWriter writer) {
336 writer.println(prefix + "deviceProfile isTransposed="
337 + getDeviceProfile().isVerticalBarLayout());
338 writer.println(prefix + "orientation=" + getResources().getConfiguration().orientation);
339 writer.println(prefix + "mSystemUiController: " + mSystemUiController);
340 writer.println(prefix + "mActivityFlags: " + mActivityFlags);
341 writer.println(prefix + "mForceInvisible: " + mForceInvisible);
Sunny Goyale43d00d2018-05-14 14:23:18 -0700342 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700343
344 public static <T extends BaseActivity> T fromContext(Context context) {
345 if (context instanceof BaseActivity) {
346 return (T) context;
Tony Wickham1906cc32021-02-11 11:55:24 -0800347 } else if (context instanceof ContextWrapper) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700348 return fromContext(((ContextWrapper) context).getBaseContext());
349 } else {
350 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
351 }
352 }
Sunny Goyal27835952017-01-13 12:15:53 -0800353}