blob: f3c5191bbef7ed3d68db9b9314d20b0abf829748 [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;
Winson Chunga0f09f92018-05-11 21:55:21 +000020import static com.android.launcher3.util.SystemUiController.UI_STATE_OVERVIEW;
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 Goyal87b5eb62018-07-03 15:53:39 -070034import android.view.ContextThemeWrapper;
Sunny Goyal27835952017-01-13 12:15:53 -080035
Winson Chungef528762019-09-06 12:05:52 -070036import androidx.annotation.IntDef;
37
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;
40import com.android.launcher3.logging.StatsLogUtils;
41import com.android.launcher3.logging.StatsLogUtils.LogStateProvider;
Sunny Goyala535ae42017-02-27 10:07:13 -080042import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070043import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
vadimt6098a8c2020-01-23 19:12:19 -080044import com.android.launcher3.testing.TestLogging;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070045import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal8392c822017-06-20 10:03:56 -070046import com.android.launcher3.util.SystemUiController;
Sunny Goyal56863332019-05-22 14:13:53 -070047import com.android.launcher3.util.ViewCache;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080048import com.android.launcher3.views.ActivityContext;
Sunny Goyala535ae42017-02-27 10:07:13 -080049
Sunny Goyale43d00d2018-05-14 14:23:18 -070050import java.io.PrintWriter;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070051import java.lang.annotation.Retention;
Sunny Goyalfde55052018-02-01 14:46:13 -080052import java.util.ArrayList;
53
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080054public abstract class BaseActivity extends Activity
55 implements UserEventDelegate, LogStateProvider, ActivityContext {
Sunny Goyal27835952017-01-13 12:15:53 -080056
Sunny Goyalfa395362019-12-11 10:00:47 -080057 private static final String TAG = "BaseActivity";
58
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})
Sunny Goyal7eff40f2018-04-11 15:30:46 -070080 public @interface InvisibilityFlags{}
81
Sunny Goyalfde55052018-02-01 14:46:13 -080082 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070083 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
84 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080085
Sunny Goyal27835952017-01-13 12:15:53 -080086 protected DeviceProfile mDeviceProfile;
Sunny Goyala535ae42017-02-27 10:07:13 -080087 protected UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfc007472018-10-25 14:09:50 -070088 protected StatsLogManager mStatsLogManager;
Sunny Goyal8392c822017-06-20 10:03:56 -070089 protected SystemUiController mSystemUiController;
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.
106 * @see #isUserActive()
107 */
Sunny Goyal210e1742019-10-17 12:05:38 -0700108 public static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 4;
109
110 /**
111 * State flag indicating that a state transition is in progress
112 */
113 public static final int ACTIVITY_STATE_TRANSITION_ACTIVE = 1 << 5;
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
135 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
Hyunyoung Songfc007472018-10-25 14:09:50 -0700144 public int getCurrentState() { return StatsLogUtils.LAUNCHER_STATE_BACKGROUND; }
145
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700146 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {}
147
Hyunyoung Songfc007472018-10-25 14:09:50 -0700148 public final StatsLogManager getStatsLogManager() {
149 if (mStatsLogManager == null) {
150 mStatsLogManager = StatsLogManager.newInstance(this, this);
151 }
152 return mStatsLogManager;
153 }
154
Sunny Goyala535ae42017-02-27 10:07:13 -0800155 public final UserEventDispatcher getUserEventDispatcher() {
156 if (mUserEventDispatcher == null) {
Hyunyoung Song956ec4b2018-07-02 13:17:32 -0700157 mUserEventDispatcher = UserEventDispatcher.newInstance(this, this);
Sunny Goyala535ae42017-02-27 10:07:13 -0800158 }
159 return mUserEventDispatcher;
160 }
161
Sunny Goyal8392c822017-06-20 10:03:56 -0700162 public SystemUiController getSystemUiController() {
163 if (mSystemUiController == null) {
164 mSystemUiController = new SystemUiController(getWindow());
165 }
166 return mSystemUiController;
167 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700168
169 @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);
Tracy Zhoua706f002018-03-28 13:55:19 -0700183 super.onResume();
184 }
185
186 @Override
187 protected void onUserLeaveHint() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700188 removeActivityFlags(ACTIVITY_STATE_USER_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700189 super.onUserLeaveHint();
190 }
191
192 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700193 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
194 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
195 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
196 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
197 }
198 }
199
200 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800201 protected void onStop() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700202 removeActivityFlags(ACTIVITY_STATE_STARTED | ACTIVITY_STATE_USER_ACTIVE);
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700203 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800204 super.onStop();
Winson Chunga36c8002019-06-11 16:15:54 -0700205
206 // Reset the overridden sysui flags used for the task-swipe launch animation, this is a
207 // catch all for if we do not get resumed (and therefore not paused below)
208 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800209 }
210
Sunny Goyal3483c522018-04-12 11:23:33 -0700211 @Override
212 protected void onPause() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700213 removeActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_DEFERRED_RESUMED);
Sunny Goyal3483c522018-04-12 11:23:33 -0700214 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000215
216 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
217 // here instead of at the end of the animation because the start of the new activity does
218 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
219 // back to the new app (causing a flash)
220 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700221 }
222
Sunny Goyal210e1742019-10-17 12:05:38 -0700223 @Override
224 public void onWindowFocusChanged(boolean hasFocus) {
225 super.onWindowFocusChanged(hasFocus);
226 if (hasFocus) {
227 addActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
228 } else {
229 removeActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
230 }
231
232 }
233
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800234 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700235 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
236 }
237
238 /**
239 * isResumed in already defined as a hidden final method in Activity.java
240 */
241 public boolean hasBeenResumed() {
242 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800243 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800244
Tracy Zhoua706f002018-03-28 13:55:19 -0700245 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700246 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700247 }
248
Sunny Goyal210e1742019-10-17 12:05:38 -0700249 public int getActivityFlags() {
250 return mActivityFlags;
251 }
252
253 protected void addActivityFlags(int flags) {
254 mActivityFlags |= flags;
255 onActivityFlagsChanged(flags);
256 }
257
258 protected void removeActivityFlags(int flags) {
259 mActivityFlags &= ~flags;
260 onActivityFlagsChanged(flags);
261 }
262
263 protected void onActivityFlagsChanged(int changeBits) { }
264
Sunny Goyalfde55052018-02-01 14:46:13 -0800265 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
266 mDPChangeListeners.add(listener);
267 }
268
Winson Chung8a968fa2018-03-15 17:59:04 -0700269 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
270 mDPChangeListeners.remove(listener);
271 }
272
Sunny Goyalfde55052018-02-01 14:46:13 -0800273 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700274 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800275 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
276 }
277 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700278
Winson Chung1a77c3d2018-04-11 12:47:47 -0700279 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
280 mMultiWindowModeChangedListeners.add(listener);
281 }
282
283 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
284 mMultiWindowModeChangedListeners.remove(listener);
285 }
286
Sunny Goyalf633ef52018-03-13 09:57:05 -0700287 /**
Winson Chung9800e732018-04-04 13:33:23 -0700288 * Used to set the override visibility state, used only to handle the transition home with the
289 * recents animation.
Winson Chung24ab40c2019-10-30 22:35:09 -0700290 * @see QuickstepAppTransitionManagerImpl#createWallpaperOpenRunner
Winson Chung9800e732018-04-04 13:33:23 -0700291 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700292 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
293 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700294 }
295
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700296 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
297 mForceInvisible &= ~flag;
298 }
299
Winson Chung9800e732018-04-04 13:33:23 -0700300 /**
301 * @return Wether this activity should be considered invisible regardless of actual visibility.
302 */
303 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700304 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
305 }
306
307 public boolean hasSomeInvisibleFlag(int mask) {
308 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700309 }
310
Winson Chung1a77c3d2018-04-11 12:47:47 -0700311 public interface MultiWindowModeChangedListener {
312 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
313 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700314
Winson Chungef528762019-09-06 12:05:52 -0700315 protected void dumpMisc(String prefix, PrintWriter writer) {
316 writer.println(prefix + "deviceProfile isTransposed="
317 + getDeviceProfile().isVerticalBarLayout());
318 writer.println(prefix + "orientation=" + getResources().getConfiguration().orientation);
319 writer.println(prefix + "mSystemUiController: " + mSystemUiController);
320 writer.println(prefix + "mActivityFlags: " + mActivityFlags);
321 writer.println(prefix + "mForceInvisible: " + mForceInvisible);
Sunny Goyale43d00d2018-05-14 14:23:18 -0700322 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700323
Sunny Goyalfa395362019-12-11 10:00:47 -0800324 /**
325 * A wrapper around the platform method with Launcher specific checks
326 */
327 public void startShortcut(String packageName, String id, Rect sourceBounds,
328 Bundle startActivityOptions, UserHandle user) {
329 if (GO_DISABLE_WIDGETS) {
330 return;
331 }
332 try {
vadimt6098a8c2020-01-23 19:12:19 -0800333 if (Utilities.IS_RUNNING_IN_TEST_HARNESS) {
334 TestLogging.recordEvent("start: shortcut: " + packageName);
335 }
Sunny Goyalfa395362019-12-11 10:00:47 -0800336 getSystemService(LauncherApps.class).startShortcut(packageName, id, sourceBounds,
337 startActivityOptions, user);
338 } catch (SecurityException | IllegalStateException e) {
339 Log.e(TAG, "Failed to start shortcut", e);
340 }
341 }
342
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700343 public static <T extends BaseActivity> T fromContext(Context context) {
344 if (context instanceof BaseActivity) {
345 return (T) context;
346 } else if (context instanceof ContextThemeWrapper) {
347 return fromContext(((ContextWrapper) context).getBaseContext());
348 } else {
349 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
350 }
351 }
Sunny Goyal27835952017-01-13 12:15:53 -0800352}