blob: 83ff0849a6ca22864121e3389f0cdbac8a3e92c7 [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
Brian Isganitis099945b2022-01-31 18:15:00 -050033import com.android.launcher3.DeviceProfile.DeviceProfileListenable;
Sunny Goyalfde55052018-02-01 14:46:13 -080034import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener;
Hyunyoung Songfc007472018-10-25 14:09:50 -070035import com.android.launcher3.logging.StatsLogManager;
Yein Jo18446d02022-09-19 22:18:09 +000036import com.android.launcher3.testing.TestLogging;
37import com.android.launcher3.testing.shared.TestProtocol;
Sunny Goyal8392c822017-06-20 10:03:56 -070038import com.android.launcher3.util.SystemUiController;
Sunny Goyal56863332019-05-22 14:13:53 -070039import com.android.launcher3.util.ViewCache;
Brian Isganitisbde3c8b2022-03-15 13:35:06 -070040import com.android.launcher3.views.AppLauncher;
Tony Wickhamb4821882021-04-23 14:26:45 -070041import com.android.launcher3.views.ScrimView;
Sunny Goyala535ae42017-02-27 10:07:13 -080042
Sunny Goyale43d00d2018-05-14 14:23:18 -070043import java.io.PrintWriter;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070044import java.lang.annotation.Retention;
Sunny Goyalfde55052018-02-01 14:46:13 -080045import java.util.ArrayList;
Brian Isganitis099945b2022-01-31 18:15:00 -050046import java.util.List;
Sunny Goyalfde55052018-02-01 14:46:13 -080047
Samuel Fufaa579ddc2020-02-27 16:59:19 -080048/**
49 * Launcher BaseActivity
50 */
Brian Isganitisbde3c8b2022-03-15 13:35:06 -070051public abstract class BaseActivity extends Activity implements AppLauncher,
Brian Isganitis099945b2022-01-31 18:15:00 -050052 DeviceProfileListenable {
Sunny Goyal27835952017-01-13 12:15:53 -080053
Sunny Goyalfa395362019-12-11 10:00:47 -080054 private static final String TAG = "BaseActivity";
55
Sunny Goyal7eff40f2018-04-11 15:30:46 -070056 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
57 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
Sunny Goyal1c63c722018-06-05 16:00:34 -070058 public static final int INVISIBLE_BY_PENDING_FLAGS = 1 << 2;
59
60 // This is not treated as invisibility flag, but adds as a hint for an incomplete transition.
61 // When the wallpaper animation runs, it replaces this flag with a proper invisibility
62 // flag, INVISIBLE_BY_PENDING_FLAGS only for the duration of that animation.
63 public static final int PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION = 1 << 3;
64
65 private static final int INVISIBLE_FLAGS =
66 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS | INVISIBLE_BY_PENDING_FLAGS;
67 public static final int STATE_HANDLER_INVISIBILITY_FLAGS =
68 INVISIBLE_BY_STATE_HANDLER | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070069 public static final int INVISIBLE_ALL =
Sunny Goyal1c63c722018-06-05 16:00:34 -070070 INVISIBLE_FLAGS | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070071
72 @Retention(SOURCE)
73 @IntDef(
74 flag = true,
Sunny Goyal1c63c722018-06-05 16:00:34 -070075 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS,
76 INVISIBLE_BY_PENDING_FLAGS, PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION})
Sunny Goyal7eff40f2018-04-11 15:30:46 -070077 public @interface InvisibilityFlags{}
78
Sunny Goyalfde55052018-02-01 14:46:13 -080079 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070080 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
81 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080082
Sunny Goyal27835952017-01-13 12:15:53 -080083 protected DeviceProfile mDeviceProfile;
Sunny Goyal8392c822017-06-20 10:03:56 -070084 protected SystemUiController mSystemUiController;
Sunny Goyal977838b2022-06-27 13:15:41 -070085 private StatsLogManager mStatsLogManager;
Sunny Goyal27835952017-01-13 12:15:53 -080086
Sunny Goyal210e1742019-10-17 12:05:38 -070087
88 public static final int ACTIVITY_STATE_STARTED = 1 << 0;
89 public static final int ACTIVITY_STATE_RESUMED = 1 << 1;
90
Sunny Goyal3483c522018-04-12 11:23:33 -070091 /**
Sunny Goyal210e1742019-10-17 12:05:38 -070092 * State flags indicating that the activity has received one frame after resume, and was
93 * not immediately paused.
94 */
95 public static final int ACTIVITY_STATE_DEFERRED_RESUMED = 1 << 2;
96
97 public static final int ACTIVITY_STATE_WINDOW_FOCUSED = 1 << 3;
98
99 /**
100 * State flag indicating if the user is active or the activity when to background as a result
Sunny Goyal3483c522018-04-12 11:23:33 -0700101 * of user action.
102 * @see #isUserActive()
103 */
Sunny Goyal210e1742019-10-17 12:05:38 -0700104 public static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 4;
105
106 /**
Winson Chung034ce6f2020-05-14 10:49:30 -0700107 * State flag indicating if the user will be active shortly.
108 */
109 public static final int ACTIVITY_STATE_USER_WILL_BE_ACTIVE = 1 << 5;
110
111 /**
Sunny Goyal210e1742019-10-17 12:05:38 -0700112 * State flag indicating that a state transition is in progress
113 */
Winson Chung034ce6f2020-05-14 10:49:30 -0700114 public static final int ACTIVITY_STATE_TRANSITION_ACTIVE = 1 << 6;
Sunny Goyal3483c522018-04-12 11:23:33 -0700115
116 @Retention(SOURCE)
117 @IntDef(
118 flag = true,
Sunny Goyal210e1742019-10-17 12:05:38 -0700119 value = {ACTIVITY_STATE_STARTED,
120 ACTIVITY_STATE_RESUMED,
121 ACTIVITY_STATE_DEFERRED_RESUMED,
122 ACTIVITY_STATE_WINDOW_FOCUSED,
123 ACTIVITY_STATE_USER_ACTIVE,
124 ACTIVITY_STATE_TRANSITION_ACTIVE})
Sunny Goyal3483c522018-04-12 11:23:33 -0700125 public @interface ActivityFlags{}
126
127 @ActivityFlags
128 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700129
Winson Chung9800e732018-04-04 13:33:23 -0700130 // When the recents animation is running, the visibility of the Launcher is managed by the
131 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700132 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800133
Sunny Goyal56863332019-05-22 14:13:53 -0700134 private final ViewCache mViewCache = new ViewCache();
135
Sunny Goyal59969372021-05-06 12:11:44 -0700136 @Override
Sunny Goyal56863332019-05-22 14:13:53 -0700137 public ViewCache getViewCache() {
138 return mViewCache;
139 }
140
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800141 @Override
Sunny Goyal27835952017-01-13 12:15:53 -0800142 public DeviceProfile getDeviceProfile() {
143 return mDeviceProfile;
144 }
145
Brian Isganitis099945b2022-01-31 18:15:00 -0500146 @Override
147 public List<OnDeviceProfileChangeListener> getOnDeviceProfileChangeListeners() {
148 return mDPChangeListeners;
149 }
150
thiruramc96873c2021-02-11 15:13:47 -0800151 /**
152 * Returns {@link StatsLogManager} for user event logging.
153 */
Sunny Goyal177785e2021-07-29 15:48:24 -0700154 @Override
thiruramc96873c2021-02-11 15:13:47 -0800155 public StatsLogManager getStatsLogManager() {
Hyunyoung Songfc007472018-10-25 14:09:50 -0700156 if (mStatsLogManager == null) {
Hyunyoung Song801f81f2020-06-19 02:58:53 -0700157 mStatsLogManager = StatsLogManager.newInstance(this);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700158 }
159 return mStatsLogManager;
160 }
161
Sunny Goyal8392c822017-06-20 10:03:56 -0700162 public SystemUiController getSystemUiController() {
163 if (mSystemUiController == null) {
164 mSystemUiController = new SystemUiController(getWindow());
165 }
166 return mSystemUiController;
167 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700168
Tony Wickhamb4821882021-04-23 14:26:45 -0700169 public ScrimView getScrimView() {
170 return null;
171 }
172
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700173 @Override
174 public void onActivityResult(int requestCode, int resultCode, Intent data) {
175 super.onActivityResult(requestCode, resultCode, data);
176 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800177
178 @Override
Yein Jo18446d02022-09-19 22:18:09 +0000179 protected void onCreate(Bundle savedInstanceState) {
180 super.onCreate(savedInstanceState);
181 if (Utilities.ATLEAST_T) {
182 getOnBackInvokedDispatcher().registerOnBackInvokedCallback(
183 OnBackInvokedDispatcher.PRIORITY_DEFAULT,
184 () -> {
185 onBackPressed();
186 TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "onBackInvoked");
187 });
188 }
189 }
190
191 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800192 protected void onStart() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700193 addActivityFlags(ACTIVITY_STATE_STARTED);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800194 super.onStart();
195 }
196
197 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700198 protected void onResume() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700199 addActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE);
Winson Chung034ce6f2020-05-14 10:49:30 -0700200 removeActivityFlags(ACTIVITY_STATE_USER_WILL_BE_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700201 super.onResume();
202 }
203
204 @Override
205 protected void onUserLeaveHint() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700206 removeActivityFlags(ACTIVITY_STATE_USER_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700207 super.onUserLeaveHint();
208 }
209
210 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700211 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
212 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
213 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
214 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
215 }
216 }
217
218 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800219 protected void onStop() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700220 removeActivityFlags(ACTIVITY_STATE_STARTED | ACTIVITY_STATE_USER_ACTIVE);
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700221 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800222 super.onStop();
Winson Chunga36c8002019-06-11 16:15:54 -0700223
224 // Reset the overridden sysui flags used for the task-swipe launch animation, this is a
225 // catch all for if we do not get resumed (and therefore not paused below)
Tony Wickhamb4821882021-04-23 14:26:45 -0700226 getSystemUiController().updateUiState(UI_STATE_FULLSCREEN_TASK, 0);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800227 }
228
Sunny Goyal3483c522018-04-12 11:23:33 -0700229 @Override
230 protected void onPause() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700231 removeActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_DEFERRED_RESUMED);
Sunny Goyal3483c522018-04-12 11:23:33 -0700232 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000233
234 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
235 // here instead of at the end of the animation because the start of the new activity does
236 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
237 // back to the new app (causing a flash)
Tony Wickhamb4821882021-04-23 14:26:45 -0700238 getSystemUiController().updateUiState(UI_STATE_FULLSCREEN_TASK, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700239 }
240
Sunny Goyal210e1742019-10-17 12:05:38 -0700241 @Override
242 public void onWindowFocusChanged(boolean hasFocus) {
243 super.onWindowFocusChanged(hasFocus);
244 if (hasFocus) {
245 addActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
246 } else {
247 removeActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
248 }
249
250 }
251
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800252 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700253 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
254 }
255
256 /**
257 * isResumed in already defined as a hidden final method in Activity.java
258 */
259 public boolean hasBeenResumed() {
260 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800261 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800262
Tracy Zhoua706f002018-03-28 13:55:19 -0700263 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700264 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700265 }
266
Sunny Goyal210e1742019-10-17 12:05:38 -0700267 public int getActivityFlags() {
268 return mActivityFlags;
269 }
270
271 protected void addActivityFlags(int flags) {
272 mActivityFlags |= flags;
273 onActivityFlagsChanged(flags);
274 }
275
276 protected void removeActivityFlags(int flags) {
277 mActivityFlags &= ~flags;
278 onActivityFlagsChanged(flags);
279 }
280
281 protected void onActivityFlagsChanged(int changeBits) { }
282
Winson Chung1a77c3d2018-04-11 12:47:47 -0700283 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
284 mMultiWindowModeChangedListeners.add(listener);
285 }
286
287 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
288 mMultiWindowModeChangedListeners.remove(listener);
289 }
290
Sunny Goyalf633ef52018-03-13 09:57:05 -0700291 /**
Winson Chung9800e732018-04-04 13:33:23 -0700292 * Used to set the override visibility state, used only to handle the transition home with the
293 * recents animation.
Sunny Goyalb65d7662021-03-07 15:09:11 -0800294 * @see QuickstepTransitionManager#createWallpaperOpenRunner
Winson Chung9800e732018-04-04 13:33:23 -0700295 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700296 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
297 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700298 }
299
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700300 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
301 mForceInvisible &= ~flag;
302 }
303
Winson Chung9800e732018-04-04 13:33:23 -0700304 /**
305 * @return Wether this activity should be considered invisible regardless of actual visibility.
306 */
307 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700308 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
309 }
310
311 public boolean hasSomeInvisibleFlag(int mask) {
312 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700313 }
314
Winson Chung1a77c3d2018-04-11 12:47:47 -0700315 public interface MultiWindowModeChangedListener {
316 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
317 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700318
Winson Chungef528762019-09-06 12:05:52 -0700319 protected void dumpMisc(String prefix, PrintWriter writer) {
320 writer.println(prefix + "deviceProfile isTransposed="
321 + getDeviceProfile().isVerticalBarLayout());
322 writer.println(prefix + "orientation=" + getResources().getConfiguration().orientation);
323 writer.println(prefix + "mSystemUiController: " + mSystemUiController);
324 writer.println(prefix + "mActivityFlags: " + mActivityFlags);
325 writer.println(prefix + "mForceInvisible: " + mForceInvisible);
Sunny Goyale43d00d2018-05-14 14:23:18 -0700326 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700327
328 public static <T extends BaseActivity> T fromContext(Context context) {
329 if (context instanceof BaseActivity) {
330 return (T) context;
Tony Wickham1906cc32021-02-11 11:55:24 -0800331 } else if (context instanceof ContextWrapper) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700332 return fromContext(((ContextWrapper) context).getBaseContext());
333 } else {
334 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
335 }
336 }
Sunny Goyal27835952017-01-13 12:15:53 -0800337}