blob: 217a41c0834f3a2d013c9efb5ea3ab10f0f7e5a7 [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;
vadimtd633c9c2020-01-22 18:00:37 -080045import com.android.launcher3.testing.TestProtocol;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070046import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal8392c822017-06-20 10:03:56 -070047import com.android.launcher3.util.SystemUiController;
Sunny Goyal56863332019-05-22 14:13:53 -070048import com.android.launcher3.util.ViewCache;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080049import com.android.launcher3.views.ActivityContext;
Sunny Goyala535ae42017-02-27 10:07:13 -080050
Sunny Goyale43d00d2018-05-14 14:23:18 -070051import java.io.PrintWriter;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070052import java.lang.annotation.Retention;
Sunny Goyalfde55052018-02-01 14:46:13 -080053import java.util.ArrayList;
54
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080055public abstract class BaseActivity extends Activity
56 implements UserEventDelegate, LogStateProvider, ActivityContext {
Sunny Goyal27835952017-01-13 12:15:53 -080057
Sunny Goyalfa395362019-12-11 10:00:47 -080058 private static final String TAG = "BaseActivity";
59
Sunny Goyal7eff40f2018-04-11 15:30:46 -070060 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
61 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
Sunny Goyal1c63c722018-06-05 16:00:34 -070062 public static final int INVISIBLE_BY_PENDING_FLAGS = 1 << 2;
63
64 // This is not treated as invisibility flag, but adds as a hint for an incomplete transition.
65 // When the wallpaper animation runs, it replaces this flag with a proper invisibility
66 // flag, INVISIBLE_BY_PENDING_FLAGS only for the duration of that animation.
67 public static final int PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION = 1 << 3;
68
69 private static final int INVISIBLE_FLAGS =
70 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS | INVISIBLE_BY_PENDING_FLAGS;
71 public static final int STATE_HANDLER_INVISIBILITY_FLAGS =
72 INVISIBLE_BY_STATE_HANDLER | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070073 public static final int INVISIBLE_ALL =
Sunny Goyal1c63c722018-06-05 16:00:34 -070074 INVISIBLE_FLAGS | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070075
76 @Retention(SOURCE)
77 @IntDef(
78 flag = true,
Sunny Goyal1c63c722018-06-05 16:00:34 -070079 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS,
80 INVISIBLE_BY_PENDING_FLAGS, PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION})
Sunny Goyal7eff40f2018-04-11 15:30:46 -070081 public @interface InvisibilityFlags{}
82
Sunny Goyalfde55052018-02-01 14:46:13 -080083 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070084 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
85 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080086
Sunny Goyal27835952017-01-13 12:15:53 -080087 protected DeviceProfile mDeviceProfile;
Sunny Goyala535ae42017-02-27 10:07:13 -080088 protected UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfc007472018-10-25 14:09:50 -070089 protected StatsLogManager mStatsLogManager;
Sunny Goyal8392c822017-06-20 10:03:56 -070090 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080091
Sunny Goyal210e1742019-10-17 12:05:38 -070092
93 public static final int ACTIVITY_STATE_STARTED = 1 << 0;
94 public static final int ACTIVITY_STATE_RESUMED = 1 << 1;
95
Sunny Goyal3483c522018-04-12 11:23:33 -070096 /**
Sunny Goyal210e1742019-10-17 12:05:38 -070097 * State flags indicating that the activity has received one frame after resume, and was
98 * not immediately paused.
99 */
100 public static final int ACTIVITY_STATE_DEFERRED_RESUMED = 1 << 2;
101
102 public static final int ACTIVITY_STATE_WINDOW_FOCUSED = 1 << 3;
103
104 /**
105 * State flag indicating if the user is active or the activity when to background as a result
Sunny Goyal3483c522018-04-12 11:23:33 -0700106 * of user action.
107 * @see #isUserActive()
108 */
Sunny Goyal210e1742019-10-17 12:05:38 -0700109 public static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 4;
110
111 /**
112 * State flag indicating that a state transition is in progress
113 */
114 public static final int ACTIVITY_STATE_TRANSITION_ACTIVE = 1 << 5;
Sunny Goyal3483c522018-04-12 11:23:33 -0700115
116 @Retention(SOURCE)
117 @IntDef(
118 flag = true,
Sunny Goyal210e1742019-10-17 12:05:38 -0700119 value = {ACTIVITY_STATE_STARTED,
120 ACTIVITY_STATE_RESUMED,
121 ACTIVITY_STATE_DEFERRED_RESUMED,
122 ACTIVITY_STATE_WINDOW_FOCUSED,
123 ACTIVITY_STATE_USER_ACTIVE,
124 ACTIVITY_STATE_TRANSITION_ACTIVE})
Sunny Goyal3483c522018-04-12 11:23:33 -0700125 public @interface ActivityFlags{}
126
127 @ActivityFlags
128 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700129
Winson Chung9800e732018-04-04 13:33:23 -0700130 // When the recents animation is running, the visibility of the Launcher is managed by the
131 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700132 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800133
Sunny Goyal56863332019-05-22 14:13:53 -0700134 private final ViewCache mViewCache = new ViewCache();
135
136 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
Hyunyoung Songfc007472018-10-25 14:09:50 -0700145 public int getCurrentState() { return StatsLogUtils.LAUNCHER_STATE_BACKGROUND; }
146
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700147 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {}
148
Hyunyoung Songfc007472018-10-25 14:09:50 -0700149 public final StatsLogManager getStatsLogManager() {
150 if (mStatsLogManager == null) {
151 mStatsLogManager = StatsLogManager.newInstance(this, this);
152 }
153 return mStatsLogManager;
154 }
155
Sunny Goyala535ae42017-02-27 10:07:13 -0800156 public final UserEventDispatcher getUserEventDispatcher() {
157 if (mUserEventDispatcher == null) {
Hyunyoung Song956ec4b2018-07-02 13:17:32 -0700158 mUserEventDispatcher = UserEventDispatcher.newInstance(this, this);
Sunny Goyala535ae42017-02-27 10:07:13 -0800159 }
160 return mUserEventDispatcher;
161 }
162
Sunny Goyal8392c822017-06-20 10:03:56 -0700163 public SystemUiController getSystemUiController() {
164 if (mSystemUiController == null) {
165 mSystemUiController = new SystemUiController(getWindow());
166 }
167 return mSystemUiController;
168 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700169
170 @Override
171 public void onActivityResult(int requestCode, int resultCode, Intent data) {
172 super.onActivityResult(requestCode, resultCode, data);
173 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800174
175 @Override
176 protected void onStart() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700177 addActivityFlags(ACTIVITY_STATE_STARTED);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800178 super.onStart();
179 }
180
181 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700182 protected void onResume() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700183 addActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_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)
209 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 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)
221 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 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
Sunny Goyalfde55052018-02-01 14:46:13 -0800266 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
267 mDPChangeListeners.add(listener);
268 }
269
Winson Chung8a968fa2018-03-15 17:59:04 -0700270 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
271 mDPChangeListeners.remove(listener);
272 }
273
Sunny Goyalfde55052018-02-01 14:46:13 -0800274 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700275 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800276 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
277 }
278 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700279
Winson Chung1a77c3d2018-04-11 12:47:47 -0700280 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
281 mMultiWindowModeChangedListeners.add(listener);
282 }
283
284 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
285 mMultiWindowModeChangedListeners.remove(listener);
286 }
287
Sunny Goyalf633ef52018-03-13 09:57:05 -0700288 /**
Winson Chung9800e732018-04-04 13:33:23 -0700289 * Used to set the override visibility state, used only to handle the transition home with the
290 * recents animation.
Winson Chung24ab40c2019-10-30 22:35:09 -0700291 * @see QuickstepAppTransitionManagerImpl#createWallpaperOpenRunner
Winson Chung9800e732018-04-04 13:33:23 -0700292 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700293 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
294 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700295 }
296
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700297 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
298 mForceInvisible &= ~flag;
299 }
300
Winson Chung9800e732018-04-04 13:33:23 -0700301 /**
302 * @return Wether this activity should be considered invisible regardless of actual visibility.
303 */
304 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700305 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
306 }
307
308 public boolean hasSomeInvisibleFlag(int mask) {
309 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700310 }
311
Winson Chung1a77c3d2018-04-11 12:47:47 -0700312 public interface MultiWindowModeChangedListener {
313 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
314 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700315
Winson Chungef528762019-09-06 12:05:52 -0700316 protected void dumpMisc(String prefix, PrintWriter writer) {
317 writer.println(prefix + "deviceProfile isTransposed="
318 + getDeviceProfile().isVerticalBarLayout());
319 writer.println(prefix + "orientation=" + getResources().getConfiguration().orientation);
320 writer.println(prefix + "mSystemUiController: " + mSystemUiController);
321 writer.println(prefix + "mActivityFlags: " + mActivityFlags);
322 writer.println(prefix + "mForceInvisible: " + mForceInvisible);
Sunny Goyale43d00d2018-05-14 14:23:18 -0700323 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700324
Sunny Goyalfa395362019-12-11 10:00:47 -0800325 /**
326 * A wrapper around the platform method with Launcher specific checks
327 */
328 public void startShortcut(String packageName, String id, Rect sourceBounds,
329 Bundle startActivityOptions, UserHandle user) {
330 if (GO_DISABLE_WIDGETS) {
331 return;
332 }
333 try {
vadimtd633c9c2020-01-22 18:00:37 -0800334 TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "start: shortcut", packageName);
Sunny Goyalfa395362019-12-11 10:00:47 -0800335 getSystemService(LauncherApps.class).startShortcut(packageName, id, sourceBounds,
336 startActivityOptions, user);
337 } catch (SecurityException | IllegalStateException e) {
338 Log.e(TAG, "Failed to start shortcut", e);
339 }
340 }
341
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700342 public static <T extends BaseActivity> T fromContext(Context context) {
343 if (context instanceof BaseActivity) {
344 return (T) context;
345 } else if (context instanceof ContextThemeWrapper) {
346 return fromContext(((ContextWrapper) context).getBaseContext());
347 } else {
348 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
349 }
350 }
Sunny Goyal27835952017-01-13 12:15:53 -0800351}