blob: 9bdc822570e5d0c80139abc985d424cccb5abb1a [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() {
Mady Mellor9a90c2d2022-09-14 15:17:21 -0700199 setResumed();
Tracy Zhoua706f002018-03-28 13:55:19 -0700200 super.onResume();
201 }
202
203 @Override
204 protected void onUserLeaveHint() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700205 removeActivityFlags(ACTIVITY_STATE_USER_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700206 super.onUserLeaveHint();
207 }
208
209 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700210 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
211 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
212 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
213 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
214 }
215 }
216
217 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800218 protected void onStop() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700219 removeActivityFlags(ACTIVITY_STATE_STARTED | ACTIVITY_STATE_USER_ACTIVE);
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700220 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800221 super.onStop();
Winson Chunga36c8002019-06-11 16:15:54 -0700222
223 // Reset the overridden sysui flags used for the task-swipe launch animation, this is a
224 // catch all for if we do not get resumed (and therefore not paused below)
Tony Wickhamb4821882021-04-23 14:26:45 -0700225 getSystemUiController().updateUiState(UI_STATE_FULLSCREEN_TASK, 0);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800226 }
227
Sunny Goyal3483c522018-04-12 11:23:33 -0700228 @Override
229 protected void onPause() {
Mady Mellor9a90c2d2022-09-14 15:17:21 -0700230 setPaused();
Sunny Goyal3483c522018-04-12 11:23:33 -0700231 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000232
233 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
234 // here instead of at the end of the animation because the start of the new activity does
235 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
236 // back to the new app (causing a flash)
Tony Wickhamb4821882021-04-23 14:26:45 -0700237 getSystemUiController().updateUiState(UI_STATE_FULLSCREEN_TASK, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700238 }
239
Sunny Goyal210e1742019-10-17 12:05:38 -0700240 @Override
241 public void onWindowFocusChanged(boolean hasFocus) {
242 super.onWindowFocusChanged(hasFocus);
243 if (hasFocus) {
244 addActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
245 } else {
246 removeActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
247 }
248
249 }
250
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800251 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700252 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
253 }
254
255 /**
256 * isResumed in already defined as a hidden final method in Activity.java
257 */
258 public boolean hasBeenResumed() {
259 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800260 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800261
Mady Mellor9a90c2d2022-09-14 15:17:21 -0700262 /**
263 * Sets the activity to appear as paused.
264 */
265 public void setPaused() {
266 removeActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_DEFERRED_RESUMED);
267 }
268
269 /**
270 * Sets the activity to appear as resumed.
271 */
272 public void setResumed() {
273 addActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE);
274 removeActivityFlags(ACTIVITY_STATE_USER_WILL_BE_ACTIVE);
275 }
276
Tracy Zhoua706f002018-03-28 13:55:19 -0700277 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700278 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700279 }
280
Sunny Goyal210e1742019-10-17 12:05:38 -0700281 public int getActivityFlags() {
282 return mActivityFlags;
283 }
284
285 protected void addActivityFlags(int flags) {
286 mActivityFlags |= flags;
287 onActivityFlagsChanged(flags);
288 }
289
290 protected void removeActivityFlags(int flags) {
291 mActivityFlags &= ~flags;
292 onActivityFlagsChanged(flags);
293 }
294
295 protected void onActivityFlagsChanged(int changeBits) { }
296
Winson Chung1a77c3d2018-04-11 12:47:47 -0700297 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
298 mMultiWindowModeChangedListeners.add(listener);
299 }
300
301 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
302 mMultiWindowModeChangedListeners.remove(listener);
303 }
304
Sunny Goyalf633ef52018-03-13 09:57:05 -0700305 /**
Winson Chung9800e732018-04-04 13:33:23 -0700306 * Used to set the override visibility state, used only to handle the transition home with the
307 * recents animation.
Sunny Goyalb65d7662021-03-07 15:09:11 -0800308 * @see QuickstepTransitionManager#createWallpaperOpenRunner
Winson Chung9800e732018-04-04 13:33:23 -0700309 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700310 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
311 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700312 }
313
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700314 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
315 mForceInvisible &= ~flag;
316 }
317
Winson Chung9800e732018-04-04 13:33:23 -0700318 /**
319 * @return Wether this activity should be considered invisible regardless of actual visibility.
320 */
321 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700322 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
323 }
324
325 public boolean hasSomeInvisibleFlag(int mask) {
326 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700327 }
328
Winson Chung1a77c3d2018-04-11 12:47:47 -0700329 public interface MultiWindowModeChangedListener {
330 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
331 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700332
Winson Chungef528762019-09-06 12:05:52 -0700333 protected void dumpMisc(String prefix, PrintWriter writer) {
334 writer.println(prefix + "deviceProfile isTransposed="
335 + getDeviceProfile().isVerticalBarLayout());
336 writer.println(prefix + "orientation=" + getResources().getConfiguration().orientation);
337 writer.println(prefix + "mSystemUiController: " + mSystemUiController);
338 writer.println(prefix + "mActivityFlags: " + mActivityFlags);
339 writer.println(prefix + "mForceInvisible: " + mForceInvisible);
Sunny Goyale43d00d2018-05-14 14:23:18 -0700340 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700341
342 public static <T extends BaseActivity> T fromContext(Context context) {
343 if (context instanceof BaseActivity) {
344 return (T) context;
Tony Wickham1906cc32021-02-11 11:55:24 -0800345 } else if (context instanceof ContextWrapper) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700346 return fromContext(((ContextWrapper) context).getBaseContext());
347 } else {
348 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
349 }
350 }
Sunny Goyal27835952017-01-13 12:15:53 -0800351}