blob: 9778b6185428072940760a6da5a96b6019f9499e [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
Sunny Goyalfde55052018-02-01 14:46:13 -080037import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener;
Hyunyoung Songfc007472018-10-25 14:09:50 -070038import com.android.launcher3.logging.StatsLogManager;
Sunny Goyal8392c822017-06-20 10:03:56 -070039import com.android.launcher3.util.SystemUiController;
Sunny Goyal56863332019-05-22 14:13:53 -070040import com.android.launcher3.util.ViewCache;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080041import com.android.launcher3.views.ActivityContext;
Tony Wickhamb4821882021-04-23 14:26:45 -070042import com.android.launcher3.views.ScrimView;
Sunny Goyala535ae42017-02-27 10:07:13 -080043
Sunny Goyale43d00d2018-05-14 14:23:18 -070044import java.io.PrintWriter;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070045import java.lang.annotation.Retention;
Sunny Goyalfde55052018-02-01 14:46:13 -080046import java.util.ArrayList;
47
Samuel Fufaa579ddc2020-02-27 16:59:19 -080048/**
49 * Launcher BaseActivity
50 */
Hyunyoung Song801f81f2020-06-19 02:58:53 -070051public abstract class BaseActivity extends Activity implements ActivityContext {
Sunny Goyal27835952017-01-13 12:15:53 -080052
Sunny Goyalfa395362019-12-11 10:00:47 -080053 private static final String TAG = "BaseActivity";
54
Sunny Goyal7eff40f2018-04-11 15:30:46 -070055 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
56 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
Sunny Goyal1c63c722018-06-05 16:00:34 -070057 public static final int INVISIBLE_BY_PENDING_FLAGS = 1 << 2;
58
59 // This is not treated as invisibility flag, but adds as a hint for an incomplete transition.
60 // When the wallpaper animation runs, it replaces this flag with a proper invisibility
61 // flag, INVISIBLE_BY_PENDING_FLAGS only for the duration of that animation.
62 public static final int PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION = 1 << 3;
63
64 private static final int INVISIBLE_FLAGS =
65 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS | INVISIBLE_BY_PENDING_FLAGS;
66 public static final int STATE_HANDLER_INVISIBILITY_FLAGS =
67 INVISIBLE_BY_STATE_HANDLER | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070068 public static final int INVISIBLE_ALL =
Sunny Goyal1c63c722018-06-05 16:00:34 -070069 INVISIBLE_FLAGS | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070070
71 @Retention(SOURCE)
72 @IntDef(
73 flag = true,
Sunny Goyal1c63c722018-06-05 16:00:34 -070074 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS,
75 INVISIBLE_BY_PENDING_FLAGS, PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION})
Sunny Goyal7eff40f2018-04-11 15:30:46 -070076 public @interface InvisibilityFlags{}
77
Sunny Goyalfde55052018-02-01 14:46:13 -080078 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070079 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
80 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080081
Sunny Goyal27835952017-01-13 12:15:53 -080082 protected DeviceProfile mDeviceProfile;
Hyunyoung Songfc007472018-10-25 14:09:50 -070083 protected StatsLogManager mStatsLogManager;
Sunny Goyal8392c822017-06-20 10:03:56 -070084 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080085
Sunny Goyal210e1742019-10-17 12:05:38 -070086
87 public static final int ACTIVITY_STATE_STARTED = 1 << 0;
88 public static final int ACTIVITY_STATE_RESUMED = 1 << 1;
89
Sunny Goyal3483c522018-04-12 11:23:33 -070090 /**
Sunny Goyal210e1742019-10-17 12:05:38 -070091 * State flags indicating that the activity has received one frame after resume, and was
92 * not immediately paused.
93 */
94 public static final int ACTIVITY_STATE_DEFERRED_RESUMED = 1 << 2;
95
96 public static final int ACTIVITY_STATE_WINDOW_FOCUSED = 1 << 3;
97
98 /**
99 * State flag indicating if the user is active or the activity when to background as a result
Sunny Goyal3483c522018-04-12 11:23:33 -0700100 * of user action.
101 * @see #isUserActive()
102 */
Sunny Goyal210e1742019-10-17 12:05:38 -0700103 public static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 4;
104
105 /**
Winson Chung034ce6f2020-05-14 10:49:30 -0700106 * State flag indicating if the user will be active shortly.
107 */
108 public static final int ACTIVITY_STATE_USER_WILL_BE_ACTIVE = 1 << 5;
109
110 /**
Sunny Goyal210e1742019-10-17 12:05:38 -0700111 * State flag indicating that a state transition is in progress
112 */
Winson Chung034ce6f2020-05-14 10:49:30 -0700113 public static final int ACTIVITY_STATE_TRANSITION_ACTIVE = 1 << 6;
Sunny Goyal3483c522018-04-12 11:23:33 -0700114
115 @Retention(SOURCE)
116 @IntDef(
117 flag = true,
Sunny Goyal210e1742019-10-17 12:05:38 -0700118 value = {ACTIVITY_STATE_STARTED,
119 ACTIVITY_STATE_RESUMED,
120 ACTIVITY_STATE_DEFERRED_RESUMED,
121 ACTIVITY_STATE_WINDOW_FOCUSED,
122 ACTIVITY_STATE_USER_ACTIVE,
123 ACTIVITY_STATE_TRANSITION_ACTIVE})
Sunny Goyal3483c522018-04-12 11:23:33 -0700124 public @interface ActivityFlags{}
125
126 @ActivityFlags
127 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700128
Winson Chung9800e732018-04-04 13:33:23 -0700129 // When the recents animation is running, the visibility of the Launcher is managed by the
130 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700131 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800132
Sunny Goyal56863332019-05-22 14:13:53 -0700133 private final ViewCache mViewCache = new ViewCache();
134
Sunny Goyal59969372021-05-06 12:11:44 -0700135 @Override
Sunny Goyal56863332019-05-22 14:13:53 -0700136 public ViewCache getViewCache() {
137 return mViewCache;
138 }
139
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800140 @Override
Sunny Goyal27835952017-01-13 12:15:53 -0800141 public DeviceProfile getDeviceProfile() {
142 return mDeviceProfile;
143 }
144
thiruramc96873c2021-02-11 15:13:47 -0800145 /**
146 * Returns {@link StatsLogManager} for user event logging.
147 */
148 public StatsLogManager getStatsLogManager() {
Hyunyoung Songfc007472018-10-25 14:09:50 -0700149 if (mStatsLogManager == null) {
Hyunyoung Song801f81f2020-06-19 02:58:53 -0700150 mStatsLogManager = StatsLogManager.newInstance(this);
Hyunyoung Songfc007472018-10-25 14:09:50 -0700151 }
152 return mStatsLogManager;
153 }
154
Sunny Goyal8392c822017-06-20 10:03:56 -0700155 public SystemUiController getSystemUiController() {
156 if (mSystemUiController == null) {
157 mSystemUiController = new SystemUiController(getWindow());
158 }
159 return mSystemUiController;
160 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700161
Tony Wickhamb4821882021-04-23 14:26:45 -0700162 public ScrimView getScrimView() {
163 return null;
164 }
165
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700166 @Override
167 public void onActivityResult(int requestCode, int resultCode, Intent data) {
168 super.onActivityResult(requestCode, resultCode, data);
169 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800170
171 @Override
172 protected void onStart() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700173 addActivityFlags(ACTIVITY_STATE_STARTED);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800174 super.onStart();
175 }
176
177 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700178 protected void onResume() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700179 addActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE);
Winson Chung034ce6f2020-05-14 10:49:30 -0700180 removeActivityFlags(ACTIVITY_STATE_USER_WILL_BE_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700181 super.onResume();
182 }
183
184 @Override
185 protected void onUserLeaveHint() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700186 removeActivityFlags(ACTIVITY_STATE_USER_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700187 super.onUserLeaveHint();
188 }
189
190 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700191 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
192 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
193 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
194 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
195 }
196 }
197
198 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800199 protected void onStop() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700200 removeActivityFlags(ACTIVITY_STATE_STARTED | ACTIVITY_STATE_USER_ACTIVE);
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700201 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800202 super.onStop();
Winson Chunga36c8002019-06-11 16:15:54 -0700203
204 // Reset the overridden sysui flags used for the task-swipe launch animation, this is a
205 // catch all for if we do not get resumed (and therefore not paused below)
Tony Wickhamb4821882021-04-23 14:26:45 -0700206 getSystemUiController().updateUiState(UI_STATE_FULLSCREEN_TASK, 0);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800207 }
208
Sunny Goyal3483c522018-04-12 11:23:33 -0700209 @Override
210 protected void onPause() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700211 removeActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_DEFERRED_RESUMED);
Sunny Goyal3483c522018-04-12 11:23:33 -0700212 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000213
214 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
215 // here instead of at the end of the animation because the start of the new activity does
216 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
217 // back to the new app (causing a flash)
Tony Wickhamb4821882021-04-23 14:26:45 -0700218 getSystemUiController().updateUiState(UI_STATE_FULLSCREEN_TASK, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700219 }
220
Sunny Goyal210e1742019-10-17 12:05:38 -0700221 @Override
222 public void onWindowFocusChanged(boolean hasFocus) {
223 super.onWindowFocusChanged(hasFocus);
224 if (hasFocus) {
225 addActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
226 } else {
227 removeActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
228 }
229
230 }
231
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800232 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700233 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
234 }
235
236 /**
237 * isResumed in already defined as a hidden final method in Activity.java
238 */
239 public boolean hasBeenResumed() {
240 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800241 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800242
Tracy Zhoua706f002018-03-28 13:55:19 -0700243 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700244 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700245 }
246
Sunny Goyal210e1742019-10-17 12:05:38 -0700247 public int getActivityFlags() {
248 return mActivityFlags;
249 }
250
251 protected void addActivityFlags(int flags) {
252 mActivityFlags |= flags;
253 onActivityFlagsChanged(flags);
254 }
255
256 protected void removeActivityFlags(int flags) {
257 mActivityFlags &= ~flags;
258 onActivityFlagsChanged(flags);
259 }
260
261 protected void onActivityFlagsChanged(int changeBits) { }
262
Sunny Goyalfde55052018-02-01 14:46:13 -0800263 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
264 mDPChangeListeners.add(listener);
265 }
266
Winson Chung8a968fa2018-03-15 17:59:04 -0700267 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
268 mDPChangeListeners.remove(listener);
269 }
270
Sunny Goyalfde55052018-02-01 14:46:13 -0800271 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700272 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800273 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
274 }
275 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700276
Winson Chung1a77c3d2018-04-11 12:47:47 -0700277 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
278 mMultiWindowModeChangedListeners.add(listener);
279 }
280
281 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
282 mMultiWindowModeChangedListeners.remove(listener);
283 }
284
Sunny Goyalf633ef52018-03-13 09:57:05 -0700285 /**
Winson Chung9800e732018-04-04 13:33:23 -0700286 * Used to set the override visibility state, used only to handle the transition home with the
287 * recents animation.
Sunny Goyalb65d7662021-03-07 15:09:11 -0800288 * @see QuickstepTransitionManager#createWallpaperOpenRunner
Winson Chung9800e732018-04-04 13:33:23 -0700289 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700290 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
291 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700292 }
293
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700294 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
295 mForceInvisible &= ~flag;
296 }
297
Winson Chung9800e732018-04-04 13:33:23 -0700298 /**
299 * @return Wether this activity should be considered invisible regardless of actual visibility.
300 */
301 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700302 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
303 }
304
305 public boolean hasSomeInvisibleFlag(int mask) {
306 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700307 }
308
Winson Chung1a77c3d2018-04-11 12:47:47 -0700309 public interface MultiWindowModeChangedListener {
310 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
311 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700312
Winson Chungef528762019-09-06 12:05:52 -0700313 protected void dumpMisc(String prefix, PrintWriter writer) {
314 writer.println(prefix + "deviceProfile isTransposed="
315 + getDeviceProfile().isVerticalBarLayout());
316 writer.println(prefix + "orientation=" + getResources().getConfiguration().orientation);
317 writer.println(prefix + "mSystemUiController: " + mSystemUiController);
318 writer.println(prefix + "mActivityFlags: " + mActivityFlags);
319 writer.println(prefix + "mForceInvisible: " + mForceInvisible);
Sunny Goyale43d00d2018-05-14 14:23:18 -0700320 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700321
Sunny Goyalfa395362019-12-11 10:00:47 -0800322 /**
323 * A wrapper around the platform method with Launcher specific checks
324 */
325 public void startShortcut(String packageName, String id, Rect sourceBounds,
326 Bundle startActivityOptions, UserHandle user) {
327 if (GO_DISABLE_WIDGETS) {
328 return;
329 }
330 try {
331 getSystemService(LauncherApps.class).startShortcut(packageName, id, sourceBounds,
332 startActivityOptions, user);
333 } catch (SecurityException | IllegalStateException e) {
334 Log.e(TAG, "Failed to start shortcut", e);
335 }
336 }
337
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700338 public static <T extends BaseActivity> T fromContext(Context context) {
339 if (context instanceof BaseActivity) {
340 return (T) context;
Tony Wickham1906cc32021-02-11 11:55:24 -0800341 } else if (context instanceof ContextWrapper) {
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700342 return fromContext(((ContextWrapper) context).getBaseContext());
343 } else {
344 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
345 }
346 }
Sunny Goyal27835952017-01-13 12:15:53 -0800347}