blob: 855983fdaddeb442f2d05292f217e16561598cc5 [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
Mike Schneidera79d4602023-03-03 15:58:06 +010019import static com.android.launcher3.util.FlagDebugUtils.appendFlag;
20import static com.android.launcher3.util.FlagDebugUtils.formatFlagChange;
Tony Wickhamb4821882021-04-23 14:26:45 -070021import static com.android.launcher3.util.SystemUiController.UI_STATE_FULLSCREEN_TASK;
Sunny Goyal8c48d8b2019-01-25 15:10:18 -080022
Sunny Goyal7eff40f2018-04-11 15:30:46 -070023import static java.lang.annotation.RetentionPolicy.SOURCE;
24
Sunny Goyal27835952017-01-13 12:15:53 -080025import android.app.Activity;
26import android.content.Context;
27import android.content.ContextWrapper;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070028import android.content.Intent;
Winson Chung1a77c3d2018-04-11 12:47:47 -070029import android.content.res.Configuration;
Yein Jo18446d02022-09-19 22:18:09 +000030import android.os.Bundle;
Mike Schneidera79d4602023-03-03 15:58:06 +010031import android.util.Log;
Yein Jo18446d02022-09-19 22:18:09 +000032import android.window.OnBackInvokedDispatcher;
Sunny Goyal27835952017-01-13 12:15:53 -080033
Winson Chungef528762019-09-06 12:05:52 -070034import androidx.annotation.IntDef;
35
Sunny Goyalfde55052018-02-01 14:46:13 -080036import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener;
Hyunyoung Songfc007472018-10-25 14:09:50 -070037import com.android.launcher3.logging.StatsLogManager;
Yein Jo18446d02022-09-19 22:18:09 +000038import com.android.launcher3.testing.TestLogging;
39import com.android.launcher3.testing.shared.TestProtocol;
Sunny Goyal8392c822017-06-20 10:03:56 -070040import com.android.launcher3.util.SystemUiController;
Sunny Goyal56863332019-05-22 14:13:53 -070041import com.android.launcher3.util.ViewCache;
Sunny Goyal54fa1102022-12-07 22:48:37 -080042import com.android.launcher3.views.ActivityContext;
Tony Wickhamb4821882021-04-23 14:26:45 -070043import com.android.launcher3.views.ScrimView;
Sunny Goyala535ae42017-02-27 10:07:13 -080044
Sunny Goyale43d00d2018-05-14 14:23:18 -070045import java.io.PrintWriter;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070046import java.lang.annotation.Retention;
Sunny Goyalfde55052018-02-01 14:46:13 -080047import java.util.ArrayList;
Brian Isganitis099945b2022-01-31 18:15:00 -050048import java.util.List;
Mike Schneidera79d4602023-03-03 15:58:06 +010049import java.util.StringJoiner;
Sunny Goyalfde55052018-02-01 14:46:13 -080050
Samuel Fufaa579ddc2020-02-27 16:59:19 -080051/**
52 * Launcher BaseActivity
53 */
Sunny Goyal54fa1102022-12-07 22:48:37 -080054public abstract class BaseActivity extends Activity implements ActivityContext {
Sunny Goyal27835952017-01-13 12:15:53 -080055
Sunny Goyalfa395362019-12-11 10:00:47 -080056 private static final String TAG = "BaseActivity";
Mike Schneidera79d4602023-03-03 15:58:06 +010057 static final boolean DEBUG = false;
Sunny Goyalfa395362019-12-11 10:00:47 -080058
Sunny Goyal7eff40f2018-04-11 15:30:46 -070059 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
60 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
Sunny Goyal1c63c722018-06-05 16:00:34 -070061 public static final int INVISIBLE_BY_PENDING_FLAGS = 1 << 2;
62
63 // This is not treated as invisibility flag, but adds as a hint for an incomplete transition.
64 // When the wallpaper animation runs, it replaces this flag with a proper invisibility
65 // flag, INVISIBLE_BY_PENDING_FLAGS only for the duration of that animation.
66 public static final int PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION = 1 << 3;
67
68 private static final int INVISIBLE_FLAGS =
69 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS | INVISIBLE_BY_PENDING_FLAGS;
70 public static final int STATE_HANDLER_INVISIBILITY_FLAGS =
71 INVISIBLE_BY_STATE_HANDLER | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070072 public static final int INVISIBLE_ALL =
Sunny Goyal1c63c722018-06-05 16:00:34 -070073 INVISIBLE_FLAGS | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070074
75 @Retention(SOURCE)
76 @IntDef(
77 flag = true,
Sunny Goyal1c63c722018-06-05 16:00:34 -070078 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS,
79 INVISIBLE_BY_PENDING_FLAGS, PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION})
Mike Schneidera79d4602023-03-03 15:58:06 +010080 public @interface InvisibilityFlags {
81 }
Sunny Goyal7eff40f2018-04-11 15:30:46 -070082
Sunny Goyalfde55052018-02-01 14:46:13 -080083 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070084 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
85 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080086
Sunny Goyal27835952017-01-13 12:15:53 -080087 protected DeviceProfile mDeviceProfile;
Sunny Goyal8392c822017-06-20 10:03:56 -070088 protected SystemUiController mSystemUiController;
Sunny Goyal977838b2022-06-27 13:15:41 -070089 private StatsLogManager mStatsLogManager;
Sunny Goyal27835952017-01-13 12:15:53 -080090
Sunny Goyal210e1742019-10-17 12:05:38 -070091
92 public static final int ACTIVITY_STATE_STARTED = 1 << 0;
93 public static final int ACTIVITY_STATE_RESUMED = 1 << 1;
94
Sunny Goyal3483c522018-04-12 11:23:33 -070095 /**
Sunny Goyal210e1742019-10-17 12:05:38 -070096 * State flags indicating that the activity has received one frame after resume, and was
97 * not immediately paused.
98 */
99 public static final int ACTIVITY_STATE_DEFERRED_RESUMED = 1 << 2;
100
101 public static final int ACTIVITY_STATE_WINDOW_FOCUSED = 1 << 3;
102
103 /**
104 * State flag indicating if the user is active or the activity when to background as a result
Sunny Goyal3483c522018-04-12 11:23:33 -0700105 * of user action.
Mike Schneidera79d4602023-03-03 15:58:06 +0100106 *
Sunny Goyal3483c522018-04-12 11:23:33 -0700107 * @see #isUserActive()
108 */
Sunny Goyal210e1742019-10-17 12:05:38 -0700109 public static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 4;
110
111 /**
Winson Chung034ce6f2020-05-14 10:49:30 -0700112 * State flag indicating if the user will be active shortly.
113 */
114 public static final int ACTIVITY_STATE_USER_WILL_BE_ACTIVE = 1 << 5;
115
116 /**
Sunny Goyal210e1742019-10-17 12:05:38 -0700117 * State flag indicating that a state transition is in progress
118 */
Winson Chung034ce6f2020-05-14 10:49:30 -0700119 public static final int ACTIVITY_STATE_TRANSITION_ACTIVE = 1 << 6;
Sunny Goyal3483c522018-04-12 11:23:33 -0700120
121 @Retention(SOURCE)
122 @IntDef(
123 flag = true,
Sunny Goyal210e1742019-10-17 12:05:38 -0700124 value = {ACTIVITY_STATE_STARTED,
125 ACTIVITY_STATE_RESUMED,
126 ACTIVITY_STATE_DEFERRED_RESUMED,
127 ACTIVITY_STATE_WINDOW_FOCUSED,
128 ACTIVITY_STATE_USER_ACTIVE,
129 ACTIVITY_STATE_TRANSITION_ACTIVE})
Mike Schneidera79d4602023-03-03 15:58:06 +0100130 public @interface ActivityFlags {
131 }
132
133 /** Returns a human-readable string for the specified {@link ActivityFlags}. */
134 public static String getActivityStateString(@ActivityFlags int flags) {
135 StringJoiner result = new StringJoiner("|");
136 appendFlag(result, flags, ACTIVITY_STATE_STARTED, "state_started");
137 appendFlag(result, flags, ACTIVITY_STATE_RESUMED, "state_resumed");
138 appendFlag(result, flags, ACTIVITY_STATE_DEFERRED_RESUMED, "state_deferred_resumed");
139 appendFlag(result, flags, ACTIVITY_STATE_WINDOW_FOCUSED, "state_window_focused");
140 appendFlag(result, flags, ACTIVITY_STATE_USER_ACTIVE, "state_user_active");
141 appendFlag(result, flags, ACTIVITY_STATE_TRANSITION_ACTIVE, "state_transition_active");
142 return result.toString();
143 }
Sunny Goyal3483c522018-04-12 11:23:33 -0700144
145 @ActivityFlags
146 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700147
Winson Chung9800e732018-04-04 13:33:23 -0700148 // When the recents animation is running, the visibility of the Launcher is managed by the
149 // animation
Mike Schneidera79d4602023-03-03 15:58:06 +0100150 @InvisibilityFlags
151 private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800152
Sunny Goyal56863332019-05-22 14:13:53 -0700153 private final ViewCache mViewCache = new ViewCache();
154
Sunny Goyal59969372021-05-06 12:11:44 -0700155 @Override
Sunny Goyal56863332019-05-22 14:13:53 -0700156 public ViewCache getViewCache() {
157 return mViewCache;
158 }
159
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800160 @Override
Sunny Goyal27835952017-01-13 12:15:53 -0800161 public DeviceProfile getDeviceProfile() {
162 return mDeviceProfile;
163 }
164
Brian Isganitis099945b2022-01-31 18:15:00 -0500165 @Override
166 public List<OnDeviceProfileChangeListener> getOnDeviceProfileChangeListeners() {
167 return mDPChangeListeners;
168 }
169
thiruramc96873c2021-02-11 15:13:47 -0800170 /**
171 * Returns {@link StatsLogManager} for user event logging.
172 */
Sunny Goyal177785e2021-07-29 15:48:24 -0700173 @Override
thiruramc96873c2021-02-11 15:13:47 -0800174 public StatsLogManager getStatsLogManager() {
Hyunyoung Songfc007472018-10-25 14:09:50 -0700175 if (mStatsLogManager == null) {
Hyunyoung Song801f81f2020-06-19 02:58:53 -0700176 mStatsLogManager = StatsLogManager.newInstance(this);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700177 }
178 return mStatsLogManager;
179 }
180
Sunny Goyal8392c822017-06-20 10:03:56 -0700181 public SystemUiController getSystemUiController() {
182 if (mSystemUiController == null) {
183 mSystemUiController = new SystemUiController(getWindow());
184 }
185 return mSystemUiController;
186 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700187
Tony Wickhamb4821882021-04-23 14:26:45 -0700188 public ScrimView getScrimView() {
189 return null;
190 }
191
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700192 @Override
193 public void onActivityResult(int requestCode, int resultCode, Intent data) {
194 super.onActivityResult(requestCode, resultCode, data);
195 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800196
197 @Override
Yein Jo18446d02022-09-19 22:18:09 +0000198 protected void onCreate(Bundle savedInstanceState) {
199 super.onCreate(savedInstanceState);
Fengjiang Lie884c2c2022-12-19 14:42:14 -0800200 registerBackDispatcher();
Yein Jo18446d02022-09-19 22:18:09 +0000201 }
202
203 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800204 protected void onStart() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700205 addActivityFlags(ACTIVITY_STATE_STARTED);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800206 super.onStart();
207 }
208
209 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700210 protected void onResume() {
Mady Mellor9a90c2d2022-09-14 15:17:21 -0700211 setResumed();
Tracy Zhoua706f002018-03-28 13:55:19 -0700212 super.onResume();
213 }
214
215 @Override
216 protected void onUserLeaveHint() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700217 removeActivityFlags(ACTIVITY_STATE_USER_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700218 super.onUserLeaveHint();
219 }
220
221 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700222 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
223 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
224 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
225 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
226 }
227 }
228
229 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800230 protected void onStop() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700231 removeActivityFlags(ACTIVITY_STATE_STARTED | ACTIVITY_STATE_USER_ACTIVE);
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700232 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800233 super.onStop();
Winson Chunga36c8002019-06-11 16:15:54 -0700234
235 // Reset the overridden sysui flags used for the task-swipe launch animation, this is a
236 // catch all for if we do not get resumed (and therefore not paused below)
Tony Wickhamb4821882021-04-23 14:26:45 -0700237 getSystemUiController().updateUiState(UI_STATE_FULLSCREEN_TASK, 0);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800238 }
239
Sunny Goyal3483c522018-04-12 11:23:33 -0700240 @Override
241 protected void onPause() {
Mady Mellor9a90c2d2022-09-14 15:17:21 -0700242 setPaused();
Sunny Goyal3483c522018-04-12 11:23:33 -0700243 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000244
245 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
246 // here instead of at the end of the animation because the start of the new activity does
247 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
248 // back to the new app (causing a flash)
Tony Wickhamb4821882021-04-23 14:26:45 -0700249 getSystemUiController().updateUiState(UI_STATE_FULLSCREEN_TASK, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700250 }
251
Sunny Goyal210e1742019-10-17 12:05:38 -0700252 @Override
253 public void onWindowFocusChanged(boolean hasFocus) {
254 super.onWindowFocusChanged(hasFocus);
255 if (hasFocus) {
256 addActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
257 } else {
258 removeActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
259 }
260
261 }
262
Fengjiang Lie884c2c2022-12-19 14:42:14 -0800263 protected void registerBackDispatcher() {
264 if (Utilities.ATLEAST_T) {
265 getOnBackInvokedDispatcher().registerOnBackInvokedCallback(
266 OnBackInvokedDispatcher.PRIORITY_DEFAULT,
267 () -> {
268 onBackPressed();
269 TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "onBackInvoked");
270 });
271 }
272 }
273
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800274 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700275 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
276 }
277
278 /**
279 * isResumed in already defined as a hidden final method in Activity.java
280 */
281 public boolean hasBeenResumed() {
282 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800283 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800284
Mady Mellor9a90c2d2022-09-14 15:17:21 -0700285 /**
286 * Sets the activity to appear as paused.
287 */
288 public void setPaused() {
289 removeActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_DEFERRED_RESUMED);
290 }
291
292 /**
293 * Sets the activity to appear as resumed.
294 */
295 public void setResumed() {
296 addActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE);
297 removeActivityFlags(ACTIVITY_STATE_USER_WILL_BE_ACTIVE);
298 }
299
Tracy Zhoua706f002018-03-28 13:55:19 -0700300 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700301 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700302 }
303
Sunny Goyal210e1742019-10-17 12:05:38 -0700304 public int getActivityFlags() {
305 return mActivityFlags;
306 }
307
Mike Schneidera79d4602023-03-03 15:58:06 +0100308 protected void addActivityFlags(int toAdd) {
309 final int oldFlags = mActivityFlags;
310 mActivityFlags |= toAdd;
311 if (DEBUG) {
312 Log.d(TAG, "Launcher flags updated: " + formatFlagChange(mActivityFlags, oldFlags,
313 BaseActivity::getActivityStateString));
314 }
315 onActivityFlagsChanged(toAdd);
Sunny Goyal210e1742019-10-17 12:05:38 -0700316 }
317
Mike Schneidera79d4602023-03-03 15:58:06 +0100318 protected void removeActivityFlags(int toRemove) {
319 final int oldFlags = mActivityFlags;
320 mActivityFlags &= ~toRemove;
321 if (DEBUG) {
322 Log.d(TAG, "Launcher flags updated: " + formatFlagChange(mActivityFlags, oldFlags,
323 BaseActivity::getActivityStateString));
324 }
325
326 onActivityFlagsChanged(toRemove);
Sunny Goyal210e1742019-10-17 12:05:38 -0700327 }
328
Mike Schneidera79d4602023-03-03 15:58:06 +0100329 protected void onActivityFlagsChanged(int changeBits) {
330 }
Sunny Goyal210e1742019-10-17 12:05:38 -0700331
Winson Chung1a77c3d2018-04-11 12:47:47 -0700332 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
333 mMultiWindowModeChangedListeners.add(listener);
334 }
335
336 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
337 mMultiWindowModeChangedListeners.remove(listener);
338 }
339
Sunny Goyalf633ef52018-03-13 09:57:05 -0700340 /**
Winson Chung9800e732018-04-04 13:33:23 -0700341 * Used to set the override visibility state, used only to handle the transition home with the
342 * recents animation.
Mike Schneidera79d4602023-03-03 15:58:06 +0100343 *
Sunny Goyalb65d7662021-03-07 15:09:11 -0800344 * @see QuickstepTransitionManager#createWallpaperOpenRunner
Winson Chung9800e732018-04-04 13:33:23 -0700345 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700346 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
347 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700348 }
349
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700350 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
351 mForceInvisible &= ~flag;
352 }
353
Winson Chung9800e732018-04-04 13:33:23 -0700354 /**
355 * @return Wether this activity should be considered invisible regardless of actual visibility.
356 */
357 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700358 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
359 }
360
361 public boolean hasSomeInvisibleFlag(int mask) {
362 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700363 }
364
Winson Chung1a77c3d2018-04-11 12:47:47 -0700365 public interface MultiWindowModeChangedListener {
366 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
367 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700368
Winson Chungef528762019-09-06 12:05:52 -0700369 protected void dumpMisc(String prefix, PrintWriter writer) {
370 writer.println(prefix + "deviceProfile isTransposed="
371 + getDeviceProfile().isVerticalBarLayout());
372 writer.println(prefix + "orientation=" + getResources().getConfiguration().orientation);
373 writer.println(prefix + "mSystemUiController: " + mSystemUiController);
Mike Schneidera79d4602023-03-03 15:58:06 +0100374 writer.println(prefix + "mActivityFlags: " + getActivityStateString(mActivityFlags));
Winson Chungef528762019-09-06 12:05:52 -0700375 writer.println(prefix + "mForceInvisible: " + mForceInvisible);
Sunny Goyale43d00d2018-05-14 14:23:18 -0700376 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700377
378 public static <T extends BaseActivity> T fromContext(Context context) {
379 if (context instanceof BaseActivity) {
380 return (T) context;
Tony Wickham1906cc32021-02-11 11:55:24 -0800381 } else if (context instanceof ContextWrapper) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700382 return fromContext(((ContextWrapper) context).getBaseContext());
383 } else {
384 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
385 }
386 }
Sunny Goyal27835952017-01-13 12:15:53 -0800387}