blob: 73d3e3301c19de3f7f261126103e5fc542a46a76 [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;
Sunny Goyal27835952017-01-13 12:15:53 -080028
Winson Chungef528762019-09-06 12:05:52 -070029import androidx.annotation.IntDef;
30
Brian Isganitis099945b2022-01-31 18:15:00 -050031import com.android.launcher3.DeviceProfile.DeviceProfileListenable;
Sunny Goyalfde55052018-02-01 14:46:13 -080032import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener;
Hyunyoung Songfc007472018-10-25 14:09:50 -070033import com.android.launcher3.logging.StatsLogManager;
Sunny Goyal8392c822017-06-20 10:03:56 -070034import com.android.launcher3.util.SystemUiController;
Sunny Goyal56863332019-05-22 14:13:53 -070035import com.android.launcher3.util.ViewCache;
Brian Isganitisbde3c8b2022-03-15 13:35:06 -070036import com.android.launcher3.views.AppLauncher;
Tony Wickhamb4821882021-04-23 14:26:45 -070037import com.android.launcher3.views.ScrimView;
Sunny Goyala535ae42017-02-27 10:07:13 -080038
Sunny Goyale43d00d2018-05-14 14:23:18 -070039import java.io.PrintWriter;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070040import java.lang.annotation.Retention;
Sunny Goyalfde55052018-02-01 14:46:13 -080041import java.util.ArrayList;
Brian Isganitis099945b2022-01-31 18:15:00 -050042import java.util.List;
Sunny Goyalfde55052018-02-01 14:46:13 -080043
Samuel Fufaa579ddc2020-02-27 16:59:19 -080044/**
45 * Launcher BaseActivity
46 */
Brian Isganitisbde3c8b2022-03-15 13:35:06 -070047public abstract class BaseActivity extends Activity implements AppLauncher,
Brian Isganitis099945b2022-01-31 18:15:00 -050048 DeviceProfileListenable {
Sunny Goyal27835952017-01-13 12:15:53 -080049
Sunny Goyalfa395362019-12-11 10:00:47 -080050 private static final String TAG = "BaseActivity";
51
Sunny Goyal7eff40f2018-04-11 15:30:46 -070052 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
53 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
Sunny Goyal1c63c722018-06-05 16:00:34 -070054 public static final int INVISIBLE_BY_PENDING_FLAGS = 1 << 2;
55
56 // This is not treated as invisibility flag, but adds as a hint for an incomplete transition.
57 // When the wallpaper animation runs, it replaces this flag with a proper invisibility
58 // flag, INVISIBLE_BY_PENDING_FLAGS only for the duration of that animation.
59 public static final int PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION = 1 << 3;
60
61 private static final int INVISIBLE_FLAGS =
62 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS | INVISIBLE_BY_PENDING_FLAGS;
63 public static final int STATE_HANDLER_INVISIBILITY_FLAGS =
64 INVISIBLE_BY_STATE_HANDLER | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070065 public static final int INVISIBLE_ALL =
Sunny Goyal1c63c722018-06-05 16:00:34 -070066 INVISIBLE_FLAGS | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070067
68 @Retention(SOURCE)
69 @IntDef(
70 flag = true,
Sunny Goyal1c63c722018-06-05 16:00:34 -070071 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS,
72 INVISIBLE_BY_PENDING_FLAGS, PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION})
Sunny Goyal7eff40f2018-04-11 15:30:46 -070073 public @interface InvisibilityFlags{}
74
Sunny Goyalfde55052018-02-01 14:46:13 -080075 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070076 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
77 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080078
Sunny Goyal27835952017-01-13 12:15:53 -080079 protected DeviceProfile mDeviceProfile;
Hyunyoung Songfc007472018-10-25 14:09:50 -070080 protected StatsLogManager mStatsLogManager;
Sunny Goyal8392c822017-06-20 10:03:56 -070081 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080082
Sunny Goyal210e1742019-10-17 12:05:38 -070083
84 public static final int ACTIVITY_STATE_STARTED = 1 << 0;
85 public static final int ACTIVITY_STATE_RESUMED = 1 << 1;
86
Sunny Goyal3483c522018-04-12 11:23:33 -070087 /**
Sunny Goyal210e1742019-10-17 12:05:38 -070088 * State flags indicating that the activity has received one frame after resume, and was
89 * not immediately paused.
90 */
91 public static final int ACTIVITY_STATE_DEFERRED_RESUMED = 1 << 2;
92
93 public static final int ACTIVITY_STATE_WINDOW_FOCUSED = 1 << 3;
94
95 /**
96 * State flag indicating if the user is active or the activity when to background as a result
Sunny Goyal3483c522018-04-12 11:23:33 -070097 * of user action.
98 * @see #isUserActive()
99 */
Sunny Goyal210e1742019-10-17 12:05:38 -0700100 public static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 4;
101
102 /**
Winson Chung034ce6f2020-05-14 10:49:30 -0700103 * State flag indicating if the user will be active shortly.
104 */
105 public static final int ACTIVITY_STATE_USER_WILL_BE_ACTIVE = 1 << 5;
106
107 /**
Sunny Goyal210e1742019-10-17 12:05:38 -0700108 * State flag indicating that a state transition is in progress
109 */
Winson Chung034ce6f2020-05-14 10:49:30 -0700110 public static final int ACTIVITY_STATE_TRANSITION_ACTIVE = 1 << 6;
Sunny Goyal3483c522018-04-12 11:23:33 -0700111
112 @Retention(SOURCE)
113 @IntDef(
114 flag = true,
Sunny Goyal210e1742019-10-17 12:05:38 -0700115 value = {ACTIVITY_STATE_STARTED,
116 ACTIVITY_STATE_RESUMED,
117 ACTIVITY_STATE_DEFERRED_RESUMED,
118 ACTIVITY_STATE_WINDOW_FOCUSED,
119 ACTIVITY_STATE_USER_ACTIVE,
120 ACTIVITY_STATE_TRANSITION_ACTIVE})
Sunny Goyal3483c522018-04-12 11:23:33 -0700121 public @interface ActivityFlags{}
122
123 @ActivityFlags
124 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700125
Winson Chung9800e732018-04-04 13:33:23 -0700126 // When the recents animation is running, the visibility of the Launcher is managed by the
127 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700128 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800129
Sunny Goyal56863332019-05-22 14:13:53 -0700130 private final ViewCache mViewCache = new ViewCache();
131
Sunny Goyal59969372021-05-06 12:11:44 -0700132 @Override
Sunny Goyal56863332019-05-22 14:13:53 -0700133 public ViewCache getViewCache() {
134 return mViewCache;
135 }
136
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800137 @Override
Sunny Goyal27835952017-01-13 12:15:53 -0800138 public DeviceProfile getDeviceProfile() {
139 return mDeviceProfile;
140 }
141
Brian Isganitis099945b2022-01-31 18:15:00 -0500142 @Override
143 public List<OnDeviceProfileChangeListener> getOnDeviceProfileChangeListeners() {
144 return mDPChangeListeners;
145 }
146
thiruramc96873c2021-02-11 15:13:47 -0800147 /**
148 * Returns {@link StatsLogManager} for user event logging.
149 */
Sunny Goyal177785e2021-07-29 15:48:24 -0700150 @Override
thiruramc96873c2021-02-11 15:13:47 -0800151 public StatsLogManager getStatsLogManager() {
Hyunyoung Songfc007472018-10-25 14:09:50 -0700152 if (mStatsLogManager == null) {
Hyunyoung Song801f81f2020-06-19 02:58:53 -0700153 mStatsLogManager = StatsLogManager.newInstance(this);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700154 }
155 return mStatsLogManager;
156 }
157
Sunny Goyal8392c822017-06-20 10:03:56 -0700158 public SystemUiController getSystemUiController() {
159 if (mSystemUiController == null) {
160 mSystemUiController = new SystemUiController(getWindow());
161 }
162 return mSystemUiController;
163 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700164
Tony Wickhamb4821882021-04-23 14:26:45 -0700165 public ScrimView getScrimView() {
166 return null;
167 }
168
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700169 @Override
170 public void onActivityResult(int requestCode, int resultCode, Intent data) {
171 super.onActivityResult(requestCode, resultCode, data);
172 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800173
174 @Override
175 protected void onStart() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700176 addActivityFlags(ACTIVITY_STATE_STARTED);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800177 super.onStart();
178 }
179
180 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700181 protected void onResume() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700182 addActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE);
Winson Chung034ce6f2020-05-14 10:49:30 -0700183 removeActivityFlags(ACTIVITY_STATE_USER_WILL_BE_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700184 super.onResume();
185 }
186
187 @Override
188 protected void onUserLeaveHint() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700189 removeActivityFlags(ACTIVITY_STATE_USER_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700190 super.onUserLeaveHint();
191 }
192
193 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700194 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
195 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
196 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
197 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
198 }
199 }
200
201 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800202 protected void onStop() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700203 removeActivityFlags(ACTIVITY_STATE_STARTED | ACTIVITY_STATE_USER_ACTIVE);
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700204 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800205 super.onStop();
Winson Chunga36c8002019-06-11 16:15:54 -0700206
207 // Reset the overridden sysui flags used for the task-swipe launch animation, this is a
208 // catch all for if we do not get resumed (and therefore not paused below)
Tony Wickhamb4821882021-04-23 14:26:45 -0700209 getSystemUiController().updateUiState(UI_STATE_FULLSCREEN_TASK, 0);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800210 }
211
Sunny Goyal3483c522018-04-12 11:23:33 -0700212 @Override
213 protected void onPause() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700214 removeActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_DEFERRED_RESUMED);
Sunny Goyal3483c522018-04-12 11:23:33 -0700215 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000216
217 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
218 // here instead of at the end of the animation because the start of the new activity does
219 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
220 // back to the new app (causing a flash)
Tony Wickhamb4821882021-04-23 14:26:45 -0700221 getSystemUiController().updateUiState(UI_STATE_FULLSCREEN_TASK, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700222 }
223
Sunny Goyal210e1742019-10-17 12:05:38 -0700224 @Override
225 public void onWindowFocusChanged(boolean hasFocus) {
226 super.onWindowFocusChanged(hasFocus);
227 if (hasFocus) {
228 addActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
229 } else {
230 removeActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
231 }
232
233 }
234
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800235 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700236 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
237 }
238
239 /**
240 * isResumed in already defined as a hidden final method in Activity.java
241 */
242 public boolean hasBeenResumed() {
243 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800244 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800245
Tracy Zhoua706f002018-03-28 13:55:19 -0700246 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700247 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700248 }
249
Sunny Goyal210e1742019-10-17 12:05:38 -0700250 public int getActivityFlags() {
251 return mActivityFlags;
252 }
253
254 protected void addActivityFlags(int flags) {
255 mActivityFlags |= flags;
256 onActivityFlagsChanged(flags);
257 }
258
259 protected void removeActivityFlags(int flags) {
260 mActivityFlags &= ~flags;
261 onActivityFlagsChanged(flags);
262 }
263
264 protected void onActivityFlagsChanged(int changeBits) { }
265
Winson Chung1a77c3d2018-04-11 12:47:47 -0700266 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
267 mMultiWindowModeChangedListeners.add(listener);
268 }
269
270 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
271 mMultiWindowModeChangedListeners.remove(listener);
272 }
273
Sunny Goyalf633ef52018-03-13 09:57:05 -0700274 /**
Winson Chung9800e732018-04-04 13:33:23 -0700275 * Used to set the override visibility state, used only to handle the transition home with the
276 * recents animation.
Sunny Goyalb65d7662021-03-07 15:09:11 -0800277 * @see QuickstepTransitionManager#createWallpaperOpenRunner
Winson Chung9800e732018-04-04 13:33:23 -0700278 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700279 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
280 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700281 }
282
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700283 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
284 mForceInvisible &= ~flag;
285 }
286
Winson Chung9800e732018-04-04 13:33:23 -0700287 /**
288 * @return Wether this activity should be considered invisible regardless of actual visibility.
289 */
290 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700291 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
292 }
293
294 public boolean hasSomeInvisibleFlag(int mask) {
295 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700296 }
297
Winson Chung1a77c3d2018-04-11 12:47:47 -0700298 public interface MultiWindowModeChangedListener {
299 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
300 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700301
Winson Chungef528762019-09-06 12:05:52 -0700302 protected void dumpMisc(String prefix, PrintWriter writer) {
303 writer.println(prefix + "deviceProfile isTransposed="
304 + getDeviceProfile().isVerticalBarLayout());
305 writer.println(prefix + "orientation=" + getResources().getConfiguration().orientation);
306 writer.println(prefix + "mSystemUiController: " + mSystemUiController);
307 writer.println(prefix + "mActivityFlags: " + mActivityFlags);
308 writer.println(prefix + "mForceInvisible: " + mForceInvisible);
Sunny Goyale43d00d2018-05-14 14:23:18 -0700309 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700310
311 public static <T extends BaseActivity> T fromContext(Context context) {
312 if (context instanceof BaseActivity) {
313 return (T) context;
Tony Wickham1906cc32021-02-11 11:55:24 -0800314 } else if (context instanceof ContextWrapper) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700315 return fromContext(((ContextWrapper) context).getBaseContext());
316 } else {
317 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
318 }
319 }
Sunny Goyal27835952017-01-13 12:15:53 -0800320}