blob: 42f70536d4afe5cf2f6f2ab9190c9b2a400a2a9f [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
Sunny Goyalfa395362019-12-11 10:00:47 -080019import static com.android.launcher3.model.WidgetsModel.GO_DISABLE_WIDGETS;
Tony Wickhamb4821882021-04-23 14:26:45 -070020import static com.android.launcher3.util.SystemUiController.UI_STATE_FULLSCREEN_TASK;
Sunny Goyal8c48d8b2019-01-25 15:10:18 -080021
Sunny Goyal7eff40f2018-04-11 15:30:46 -070022import static java.lang.annotation.RetentionPolicy.SOURCE;
23
Sunny Goyal27835952017-01-13 12:15:53 -080024import android.app.Activity;
25import android.content.Context;
26import android.content.ContextWrapper;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070027import android.content.Intent;
Sunny Goyalfa395362019-12-11 10:00:47 -080028import android.content.pm.LauncherApps;
Winson Chung1a77c3d2018-04-11 12:47:47 -070029import android.content.res.Configuration;
Sunny Goyalfa395362019-12-11 10:00:47 -080030import android.graphics.Rect;
31import android.os.Bundle;
32import android.os.UserHandle;
33import android.util.Log;
Sunny Goyal27835952017-01-13 12:15:53 -080034
Winson Chungef528762019-09-06 12:05:52 -070035import androidx.annotation.IntDef;
36
Brian Isganitis099945b2022-01-31 18:15:00 -050037import com.android.launcher3.DeviceProfile.DeviceProfileListenable;
Sunny Goyalfde55052018-02-01 14:46:13 -080038import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener;
Hyunyoung Songfc007472018-10-25 14:09:50 -070039import com.android.launcher3.logging.StatsLogManager;
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 Goyalfe8e4a92018-11-13 19:43:57 -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;
Sunny Goyalfde55052018-02-01 14:46:13 -080049
Samuel Fufaa579ddc2020-02-27 16:59:19 -080050/**
51 * Launcher BaseActivity
52 */
Brian Isganitis099945b2022-01-31 18:15:00 -050053public abstract class BaseActivity extends Activity implements ActivityContext,
54 DeviceProfileListenable {
Sunny Goyal27835952017-01-13 12:15:53 -080055
Sunny Goyalfa395362019-12-11 10:00:47 -080056 private static final String TAG = "BaseActivity";
57
Sunny Goyal7eff40f2018-04-11 15:30:46 -070058 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
59 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
Sunny Goyal1c63c722018-06-05 16:00:34 -070060 public static final int INVISIBLE_BY_PENDING_FLAGS = 1 << 2;
61
62 // This is not treated as invisibility flag, but adds as a hint for an incomplete transition.
63 // When the wallpaper animation runs, it replaces this flag with a proper invisibility
64 // flag, INVISIBLE_BY_PENDING_FLAGS only for the duration of that animation.
65 public static final int PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION = 1 << 3;
66
67 private static final int INVISIBLE_FLAGS =
68 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS | INVISIBLE_BY_PENDING_FLAGS;
69 public static final int STATE_HANDLER_INVISIBILITY_FLAGS =
70 INVISIBLE_BY_STATE_HANDLER | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070071 public static final int INVISIBLE_ALL =
Sunny Goyal1c63c722018-06-05 16:00:34 -070072 INVISIBLE_FLAGS | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070073
74 @Retention(SOURCE)
75 @IntDef(
76 flag = true,
Sunny Goyal1c63c722018-06-05 16:00:34 -070077 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS,
78 INVISIBLE_BY_PENDING_FLAGS, PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION})
Sunny Goyal7eff40f2018-04-11 15:30:46 -070079 public @interface InvisibilityFlags{}
80
Sunny Goyalfde55052018-02-01 14:46:13 -080081 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070082 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
83 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080084
Sunny Goyal27835952017-01-13 12:15:53 -080085 protected DeviceProfile mDeviceProfile;
Hyunyoung Songfc007472018-10-25 14:09:50 -070086 protected StatsLogManager mStatsLogManager;
Sunny Goyal8392c822017-06-20 10:03:56 -070087 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080088
Sunny Goyal210e1742019-10-17 12:05:38 -070089
90 public static final int ACTIVITY_STATE_STARTED = 1 << 0;
91 public static final int ACTIVITY_STATE_RESUMED = 1 << 1;
92
Sunny Goyal3483c522018-04-12 11:23:33 -070093 /**
Sunny Goyal210e1742019-10-17 12:05:38 -070094 * State flags indicating that the activity has received one frame after resume, and was
95 * not immediately paused.
96 */
97 public static final int ACTIVITY_STATE_DEFERRED_RESUMED = 1 << 2;
98
99 public static final int ACTIVITY_STATE_WINDOW_FOCUSED = 1 << 3;
100
101 /**
102 * State flag indicating if the user is active or the activity when to background as a result
Sunny Goyal3483c522018-04-12 11:23:33 -0700103 * of user action.
104 * @see #isUserActive()
105 */
Sunny Goyal210e1742019-10-17 12:05:38 -0700106 public static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 4;
107
108 /**
Winson Chung034ce6f2020-05-14 10:49:30 -0700109 * State flag indicating if the user will be active shortly.
110 */
111 public static final int ACTIVITY_STATE_USER_WILL_BE_ACTIVE = 1 << 5;
112
113 /**
Sunny Goyal210e1742019-10-17 12:05:38 -0700114 * State flag indicating that a state transition is in progress
115 */
Winson Chung034ce6f2020-05-14 10:49:30 -0700116 public static final int ACTIVITY_STATE_TRANSITION_ACTIVE = 1 << 6;
Sunny Goyal3483c522018-04-12 11:23:33 -0700117
118 @Retention(SOURCE)
119 @IntDef(
120 flag = true,
Sunny Goyal210e1742019-10-17 12:05:38 -0700121 value = {ACTIVITY_STATE_STARTED,
122 ACTIVITY_STATE_RESUMED,
123 ACTIVITY_STATE_DEFERRED_RESUMED,
124 ACTIVITY_STATE_WINDOW_FOCUSED,
125 ACTIVITY_STATE_USER_ACTIVE,
126 ACTIVITY_STATE_TRANSITION_ACTIVE})
Sunny Goyal3483c522018-04-12 11:23:33 -0700127 public @interface ActivityFlags{}
128
129 @ActivityFlags
130 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700131
Winson Chung9800e732018-04-04 13:33:23 -0700132 // When the recents animation is running, the visibility of the Launcher is managed by the
133 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700134 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800135
Sunny Goyal56863332019-05-22 14:13:53 -0700136 private final ViewCache mViewCache = new ViewCache();
137
Sunny Goyal59969372021-05-06 12:11:44 -0700138 @Override
Sunny Goyal56863332019-05-22 14:13:53 -0700139 public ViewCache getViewCache() {
140 return mViewCache;
141 }
142
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800143 @Override
Sunny Goyal27835952017-01-13 12:15:53 -0800144 public DeviceProfile getDeviceProfile() {
145 return mDeviceProfile;
146 }
147
Brian Isganitis099945b2022-01-31 18:15:00 -0500148 @Override
149 public List<OnDeviceProfileChangeListener> getOnDeviceProfileChangeListeners() {
150 return mDPChangeListeners;
151 }
152
thiruramc96873c2021-02-11 15:13:47 -0800153 /**
154 * Returns {@link StatsLogManager} for user event logging.
155 */
Sunny Goyal177785e2021-07-29 15:48:24 -0700156 @Override
thiruramc96873c2021-02-11 15:13:47 -0800157 public StatsLogManager getStatsLogManager() {
Hyunyoung Songfc007472018-10-25 14:09:50 -0700158 if (mStatsLogManager == null) {
Hyunyoung Song801f81f2020-06-19 02:58:53 -0700159 mStatsLogManager = StatsLogManager.newInstance(this);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700160 }
161 return mStatsLogManager;
162 }
163
Sunny Goyal8392c822017-06-20 10:03:56 -0700164 public SystemUiController getSystemUiController() {
165 if (mSystemUiController == null) {
166 mSystemUiController = new SystemUiController(getWindow());
167 }
168 return mSystemUiController;
169 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700170
Tony Wickhamb4821882021-04-23 14:26:45 -0700171 public ScrimView getScrimView() {
172 return null;
173 }
174
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700175 @Override
176 public void onActivityResult(int requestCode, int resultCode, Intent data) {
177 super.onActivityResult(requestCode, resultCode, data);
178 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800179
180 @Override
181 protected void onStart() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700182 addActivityFlags(ACTIVITY_STATE_STARTED);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800183 super.onStart();
184 }
185
186 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700187 protected void onResume() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700188 addActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE);
Winson Chung034ce6f2020-05-14 10:49:30 -0700189 removeActivityFlags(ACTIVITY_STATE_USER_WILL_BE_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700190 super.onResume();
191 }
192
193 @Override
194 protected void onUserLeaveHint() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700195 removeActivityFlags(ACTIVITY_STATE_USER_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700196 super.onUserLeaveHint();
197 }
198
199 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700200 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
201 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
202 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
203 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
204 }
205 }
206
207 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800208 protected void onStop() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700209 removeActivityFlags(ACTIVITY_STATE_STARTED | ACTIVITY_STATE_USER_ACTIVE);
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700210 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800211 super.onStop();
Winson Chunga36c8002019-06-11 16:15:54 -0700212
213 // Reset the overridden sysui flags used for the task-swipe launch animation, this is a
214 // catch all for if we do not get resumed (and therefore not paused below)
Tony Wickhamb4821882021-04-23 14:26:45 -0700215 getSystemUiController().updateUiState(UI_STATE_FULLSCREEN_TASK, 0);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800216 }
217
Sunny Goyal3483c522018-04-12 11:23:33 -0700218 @Override
219 protected void onPause() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700220 removeActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_DEFERRED_RESUMED);
Sunny Goyal3483c522018-04-12 11:23:33 -0700221 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000222
223 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
224 // here instead of at the end of the animation because the start of the new activity does
225 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
226 // back to the new app (causing a flash)
Tony Wickhamb4821882021-04-23 14:26:45 -0700227 getSystemUiController().updateUiState(UI_STATE_FULLSCREEN_TASK, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700228 }
229
Sunny Goyal210e1742019-10-17 12:05:38 -0700230 @Override
231 public void onWindowFocusChanged(boolean hasFocus) {
232 super.onWindowFocusChanged(hasFocus);
233 if (hasFocus) {
234 addActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
235 } else {
236 removeActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
237 }
238
239 }
240
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800241 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700242 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
243 }
244
245 /**
246 * isResumed in already defined as a hidden final method in Activity.java
247 */
248 public boolean hasBeenResumed() {
249 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800250 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800251
Tracy Zhoua706f002018-03-28 13:55:19 -0700252 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700253 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700254 }
255
Sunny Goyal210e1742019-10-17 12:05:38 -0700256 public int getActivityFlags() {
257 return mActivityFlags;
258 }
259
260 protected void addActivityFlags(int flags) {
261 mActivityFlags |= flags;
262 onActivityFlagsChanged(flags);
263 }
264
265 protected void removeActivityFlags(int flags) {
266 mActivityFlags &= ~flags;
267 onActivityFlagsChanged(flags);
268 }
269
270 protected void onActivityFlagsChanged(int changeBits) { }
271
Winson Chung1a77c3d2018-04-11 12:47:47 -0700272 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
273 mMultiWindowModeChangedListeners.add(listener);
274 }
275
276 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
277 mMultiWindowModeChangedListeners.remove(listener);
278 }
279
Sunny Goyalf633ef52018-03-13 09:57:05 -0700280 /**
Winson Chung9800e732018-04-04 13:33:23 -0700281 * Used to set the override visibility state, used only to handle the transition home with the
282 * recents animation.
Sunny Goyalb65d7662021-03-07 15:09:11 -0800283 * @see QuickstepTransitionManager#createWallpaperOpenRunner
Winson Chung9800e732018-04-04 13:33:23 -0700284 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700285 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
286 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700287 }
288
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700289 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
290 mForceInvisible &= ~flag;
291 }
292
Winson Chung9800e732018-04-04 13:33:23 -0700293 /**
294 * @return Wether this activity should be considered invisible regardless of actual visibility.
295 */
296 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700297 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
298 }
299
300 public boolean hasSomeInvisibleFlag(int mask) {
301 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700302 }
303
Winson Chung1a77c3d2018-04-11 12:47:47 -0700304 public interface MultiWindowModeChangedListener {
305 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
306 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700307
Winson Chungef528762019-09-06 12:05:52 -0700308 protected void dumpMisc(String prefix, PrintWriter writer) {
309 writer.println(prefix + "deviceProfile isTransposed="
310 + getDeviceProfile().isVerticalBarLayout());
311 writer.println(prefix + "orientation=" + getResources().getConfiguration().orientation);
312 writer.println(prefix + "mSystemUiController: " + mSystemUiController);
313 writer.println(prefix + "mActivityFlags: " + mActivityFlags);
314 writer.println(prefix + "mForceInvisible: " + mForceInvisible);
Sunny Goyale43d00d2018-05-14 14:23:18 -0700315 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700316
Sunny Goyalfa395362019-12-11 10:00:47 -0800317 /**
318 * A wrapper around the platform method with Launcher specific checks
319 */
320 public void startShortcut(String packageName, String id, Rect sourceBounds,
321 Bundle startActivityOptions, UserHandle user) {
322 if (GO_DISABLE_WIDGETS) {
323 return;
324 }
325 try {
326 getSystemService(LauncherApps.class).startShortcut(packageName, id, sourceBounds,
327 startActivityOptions, user);
328 } catch (SecurityException | IllegalStateException e) {
329 Log.e(TAG, "Failed to start shortcut", e);
330 }
331 }
332
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700333 public static <T extends BaseActivity> T fromContext(Context context) {
334 if (context instanceof BaseActivity) {
335 return (T) context;
Tony Wickham1906cc32021-02-11 11:55:24 -0800336 } else if (context instanceof ContextWrapper) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700337 return fromContext(((ContextWrapper) context).getBaseContext());
338 } else {
339 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
340 }
341 }
Sunny Goyal27835952017-01-13 12:15:53 -0800342}