blob: 61707dff497afafff7a03b4e3b3b0dc29c8a0e2e [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);
179 if (Utilities.ATLEAST_T) {
180 getOnBackInvokedDispatcher().registerOnBackInvokedCallback(
181 OnBackInvokedDispatcher.PRIORITY_DEFAULT,
182 () -> {
183 onBackPressed();
184 TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "onBackInvoked");
185 });
186 }
187 }
188
189 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800190 protected void onStart() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700191 addActivityFlags(ACTIVITY_STATE_STARTED);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800192 super.onStart();
193 }
194
195 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700196 protected void onResume() {
Mady Mellor9a90c2d2022-09-14 15:17:21 -0700197 setResumed();
Tracy Zhoua706f002018-03-28 13:55:19 -0700198 super.onResume();
199 }
200
201 @Override
202 protected void onUserLeaveHint() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700203 removeActivityFlags(ACTIVITY_STATE_USER_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700204 super.onUserLeaveHint();
205 }
206
207 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700208 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
209 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
210 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
211 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
212 }
213 }
214
215 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800216 protected void onStop() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700217 removeActivityFlags(ACTIVITY_STATE_STARTED | ACTIVITY_STATE_USER_ACTIVE);
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700218 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800219 super.onStop();
Winson Chunga36c8002019-06-11 16:15:54 -0700220
221 // Reset the overridden sysui flags used for the task-swipe launch animation, this is a
222 // catch all for if we do not get resumed (and therefore not paused below)
Tony Wickhamb4821882021-04-23 14:26:45 -0700223 getSystemUiController().updateUiState(UI_STATE_FULLSCREEN_TASK, 0);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800224 }
225
Sunny Goyal3483c522018-04-12 11:23:33 -0700226 @Override
227 protected void onPause() {
Mady Mellor9a90c2d2022-09-14 15:17:21 -0700228 setPaused();
Sunny Goyal3483c522018-04-12 11:23:33 -0700229 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000230
231 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
232 // here instead of at the end of the animation because the start of the new activity does
233 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
234 // back to the new app (causing a flash)
Tony Wickhamb4821882021-04-23 14:26:45 -0700235 getSystemUiController().updateUiState(UI_STATE_FULLSCREEN_TASK, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700236 }
237
Sunny Goyal210e1742019-10-17 12:05:38 -0700238 @Override
239 public void onWindowFocusChanged(boolean hasFocus) {
240 super.onWindowFocusChanged(hasFocus);
241 if (hasFocus) {
242 addActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
243 } else {
244 removeActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
245 }
246
247 }
248
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800249 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700250 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
251 }
252
253 /**
254 * isResumed in already defined as a hidden final method in Activity.java
255 */
256 public boolean hasBeenResumed() {
257 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800258 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800259
Mady Mellor9a90c2d2022-09-14 15:17:21 -0700260 /**
261 * Sets the activity to appear as paused.
262 */
263 public void setPaused() {
264 removeActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_DEFERRED_RESUMED);
265 }
266
267 /**
268 * Sets the activity to appear as resumed.
269 */
270 public void setResumed() {
271 addActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE);
272 removeActivityFlags(ACTIVITY_STATE_USER_WILL_BE_ACTIVE);
273 }
274
Tracy Zhoua706f002018-03-28 13:55:19 -0700275 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700276 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700277 }
278
Sunny Goyal210e1742019-10-17 12:05:38 -0700279 public int getActivityFlags() {
280 return mActivityFlags;
281 }
282
283 protected void addActivityFlags(int flags) {
284 mActivityFlags |= flags;
285 onActivityFlagsChanged(flags);
286 }
287
288 protected void removeActivityFlags(int flags) {
289 mActivityFlags &= ~flags;
290 onActivityFlagsChanged(flags);
291 }
292
293 protected void onActivityFlagsChanged(int changeBits) { }
294
Winson Chung1a77c3d2018-04-11 12:47:47 -0700295 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
296 mMultiWindowModeChangedListeners.add(listener);
297 }
298
299 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
300 mMultiWindowModeChangedListeners.remove(listener);
301 }
302
Sunny Goyalf633ef52018-03-13 09:57:05 -0700303 /**
Winson Chung9800e732018-04-04 13:33:23 -0700304 * Used to set the override visibility state, used only to handle the transition home with the
305 * recents animation.
Sunny Goyalb65d7662021-03-07 15:09:11 -0800306 * @see QuickstepTransitionManager#createWallpaperOpenRunner
Winson Chung9800e732018-04-04 13:33:23 -0700307 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700308 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
309 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700310 }
311
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700312 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
313 mForceInvisible &= ~flag;
314 }
315
Winson Chung9800e732018-04-04 13:33:23 -0700316 /**
317 * @return Wether this activity should be considered invisible regardless of actual visibility.
318 */
319 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700320 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
321 }
322
323 public boolean hasSomeInvisibleFlag(int mask) {
324 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700325 }
326
Winson Chung1a77c3d2018-04-11 12:47:47 -0700327 public interface MultiWindowModeChangedListener {
328 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
329 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700330
Winson Chungef528762019-09-06 12:05:52 -0700331 protected void dumpMisc(String prefix, PrintWriter writer) {
332 writer.println(prefix + "deviceProfile isTransposed="
333 + getDeviceProfile().isVerticalBarLayout());
334 writer.println(prefix + "orientation=" + getResources().getConfiguration().orientation);
335 writer.println(prefix + "mSystemUiController: " + mSystemUiController);
336 writer.println(prefix + "mActivityFlags: " + mActivityFlags);
337 writer.println(prefix + "mForceInvisible: " + mForceInvisible);
Sunny Goyale43d00d2018-05-14 14:23:18 -0700338 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700339
340 public static <T extends BaseActivity> T fromContext(Context context) {
341 if (context instanceof BaseActivity) {
342 return (T) context;
Tony Wickham1906cc32021-02-11 11:55:24 -0800343 } else if (context instanceof ContextWrapper) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700344 return fromContext(((ContextWrapper) context).getBaseContext());
345 } else {
346 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
347 }
348 }
Sunny Goyal27835952017-01-13 12:15:53 -0800349}