blob: 814b72882b14f5ed174f34e75865603cb02505fc [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;
vadimt6098a8c2020-01-23 19:12:19 -080043import com.android.launcher3.testing.TestLogging;
vadimtd633c9c2020-01-22 18:00:37 -080044import com.android.launcher3.testing.TestProtocol;
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
Samuel Fufaa579ddc2020-02-27 16:59:19 -080054/**
55 * Launcher BaseActivity
56 */
57public abstract class BaseActivity extends Activity implements LogStateProvider, ActivityContext {
Sunny Goyal27835952017-01-13 12:15:53 -080058
Sunny Goyalfa395362019-12-11 10:00:47 -080059 private static final String TAG = "BaseActivity";
60
Sunny Goyal7eff40f2018-04-11 15:30:46 -070061 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
62 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
Sunny Goyal1c63c722018-06-05 16:00:34 -070063 public static final int INVISIBLE_BY_PENDING_FLAGS = 1 << 2;
64
65 // This is not treated as invisibility flag, but adds as a hint for an incomplete transition.
66 // When the wallpaper animation runs, it replaces this flag with a proper invisibility
67 // flag, INVISIBLE_BY_PENDING_FLAGS only for the duration of that animation.
68 public static final int PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION = 1 << 3;
69
70 private static final int INVISIBLE_FLAGS =
71 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS | INVISIBLE_BY_PENDING_FLAGS;
72 public static final int STATE_HANDLER_INVISIBILITY_FLAGS =
73 INVISIBLE_BY_STATE_HANDLER | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070074 public static final int INVISIBLE_ALL =
Sunny Goyal1c63c722018-06-05 16:00:34 -070075 INVISIBLE_FLAGS | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070076
77 @Retention(SOURCE)
78 @IntDef(
79 flag = true,
Sunny Goyal1c63c722018-06-05 16:00:34 -070080 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS,
81 INVISIBLE_BY_PENDING_FLAGS, PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION})
Sunny Goyal7eff40f2018-04-11 15:30:46 -070082 public @interface InvisibilityFlags{}
83
Sunny Goyalfde55052018-02-01 14:46:13 -080084 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070085 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
86 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080087
Sunny Goyal27835952017-01-13 12:15:53 -080088 protected DeviceProfile mDeviceProfile;
Sunny Goyala535ae42017-02-27 10:07:13 -080089 protected UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfc007472018-10-25 14:09:50 -070090 protected StatsLogManager mStatsLogManager;
Sunny Goyal8392c822017-06-20 10:03:56 -070091 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080092
Sunny Goyal210e1742019-10-17 12:05:38 -070093
94 public static final int ACTIVITY_STATE_STARTED = 1 << 0;
95 public static final int ACTIVITY_STATE_RESUMED = 1 << 1;
96
Sunny Goyal3483c522018-04-12 11:23:33 -070097 /**
Sunny Goyal210e1742019-10-17 12:05:38 -070098 * State flags indicating that the activity has received one frame after resume, and was
99 * not immediately paused.
100 */
101 public static final int ACTIVITY_STATE_DEFERRED_RESUMED = 1 << 2;
102
103 public static final int ACTIVITY_STATE_WINDOW_FOCUSED = 1 << 3;
104
105 /**
106 * State flag indicating if the user is active or the activity when to background as a result
Sunny Goyal3483c522018-04-12 11:23:33 -0700107 * of user action.
108 * @see #isUserActive()
109 */
Sunny Goyal210e1742019-10-17 12:05:38 -0700110 public static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 4;
111
112 /**
113 * State flag indicating that a state transition is in progress
114 */
115 public static final int ACTIVITY_STATE_TRANSITION_ACTIVE = 1 << 5;
Sunny Goyal3483c522018-04-12 11:23:33 -0700116
117 @Retention(SOURCE)
118 @IntDef(
119 flag = true,
Sunny Goyal210e1742019-10-17 12:05:38 -0700120 value = {ACTIVITY_STATE_STARTED,
121 ACTIVITY_STATE_RESUMED,
122 ACTIVITY_STATE_DEFERRED_RESUMED,
123 ACTIVITY_STATE_WINDOW_FOCUSED,
124 ACTIVITY_STATE_USER_ACTIVE,
125 ACTIVITY_STATE_TRANSITION_ACTIVE})
Sunny Goyal3483c522018-04-12 11:23:33 -0700126 public @interface ActivityFlags{}
127
128 @ActivityFlags
129 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700130
Winson Chung9800e732018-04-04 13:33:23 -0700131 // When the recents animation is running, the visibility of the Launcher is managed by the
132 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700133 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800134
Sunny Goyal56863332019-05-22 14:13:53 -0700135 private final ViewCache mViewCache = new ViewCache();
136
137 public ViewCache getViewCache() {
138 return mViewCache;
139 }
140
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800141 @Override
Sunny Goyal27835952017-01-13 12:15:53 -0800142 public DeviceProfile getDeviceProfile() {
143 return mDeviceProfile;
144 }
145
Hyunyoung Songfc007472018-10-25 14:09:50 -0700146 public int getCurrentState() { return StatsLogUtils.LAUNCHER_STATE_BACKGROUND; }
147
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700148 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {}
149
Hyunyoung Songfc007472018-10-25 14:09:50 -0700150 public final StatsLogManager getStatsLogManager() {
151 if (mStatsLogManager == null) {
152 mStatsLogManager = StatsLogManager.newInstance(this, this);
153 }
154 return mStatsLogManager;
155 }
156
Sunny Goyala535ae42017-02-27 10:07:13 -0800157 public final UserEventDispatcher getUserEventDispatcher() {
158 if (mUserEventDispatcher == null) {
Samuel Fufaa579ddc2020-02-27 16:59:19 -0800159 mUserEventDispatcher = UserEventDispatcher.newInstance(this);
Sunny Goyala535ae42017-02-27 10:07:13 -0800160 }
161 return mUserEventDispatcher;
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
171 @Override
172 public void onActivityResult(int requestCode, int resultCode, Intent data) {
173 super.onActivityResult(requestCode, resultCode, data);
174 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800175
176 @Override
177 protected void onStart() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700178 addActivityFlags(ACTIVITY_STATE_STARTED);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800179 super.onStart();
180 }
181
182 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700183 protected void onResume() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700184 addActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700185 super.onResume();
186 }
187
188 @Override
189 protected void onUserLeaveHint() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700190 removeActivityFlags(ACTIVITY_STATE_USER_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700191 super.onUserLeaveHint();
192 }
193
194 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700195 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
196 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
197 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
198 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
199 }
200 }
201
202 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800203 protected void onStop() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700204 removeActivityFlags(ACTIVITY_STATE_STARTED | ACTIVITY_STATE_USER_ACTIVE);
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700205 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800206 super.onStop();
Winson Chunga36c8002019-06-11 16:15:54 -0700207
208 // Reset the overridden sysui flags used for the task-swipe launch animation, this is a
209 // catch all for if we do not get resumed (and therefore not paused below)
210 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800211 }
212
Sunny Goyal3483c522018-04-12 11:23:33 -0700213 @Override
214 protected void onPause() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700215 removeActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_DEFERRED_RESUMED);
Sunny Goyal3483c522018-04-12 11:23:33 -0700216 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000217
218 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
219 // here instead of at the end of the animation because the start of the new activity does
220 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
221 // back to the new app (causing a flash)
222 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700223 }
224
Sunny Goyal210e1742019-10-17 12:05:38 -0700225 @Override
226 public void onWindowFocusChanged(boolean hasFocus) {
227 super.onWindowFocusChanged(hasFocus);
228 if (hasFocus) {
229 addActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
230 } else {
231 removeActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
232 }
233
234 }
235
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800236 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700237 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
238 }
239
240 /**
241 * isResumed in already defined as a hidden final method in Activity.java
242 */
243 public boolean hasBeenResumed() {
244 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800245 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800246
Tracy Zhoua706f002018-03-28 13:55:19 -0700247 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700248 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700249 }
250
Sunny Goyal210e1742019-10-17 12:05:38 -0700251 public int getActivityFlags() {
252 return mActivityFlags;
253 }
254
255 protected void addActivityFlags(int flags) {
256 mActivityFlags |= flags;
257 onActivityFlagsChanged(flags);
258 }
259
260 protected void removeActivityFlags(int flags) {
261 mActivityFlags &= ~flags;
262 onActivityFlagsChanged(flags);
263 }
264
265 protected void onActivityFlagsChanged(int changeBits) { }
266
Sunny Goyalfde55052018-02-01 14:46:13 -0800267 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
268 mDPChangeListeners.add(listener);
269 }
270
Winson Chung8a968fa2018-03-15 17:59:04 -0700271 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
272 mDPChangeListeners.remove(listener);
273 }
274
Sunny Goyalfde55052018-02-01 14:46:13 -0800275 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700276 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800277 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
278 }
279 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700280
Winson Chung1a77c3d2018-04-11 12:47:47 -0700281 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
282 mMultiWindowModeChangedListeners.add(listener);
283 }
284
285 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
286 mMultiWindowModeChangedListeners.remove(listener);
287 }
288
Sunny Goyalf633ef52018-03-13 09:57:05 -0700289 /**
Winson Chung9800e732018-04-04 13:33:23 -0700290 * Used to set the override visibility state, used only to handle the transition home with the
291 * recents animation.
Winson Chung24ab40c2019-10-30 22:35:09 -0700292 * @see QuickstepAppTransitionManagerImpl#createWallpaperOpenRunner
Winson Chung9800e732018-04-04 13:33:23 -0700293 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700294 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
295 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700296 }
297
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700298 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
299 mForceInvisible &= ~flag;
300 }
301
Winson Chung9800e732018-04-04 13:33:23 -0700302 /**
303 * @return Wether this activity should be considered invisible regardless of actual visibility.
304 */
305 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700306 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
307 }
308
309 public boolean hasSomeInvisibleFlag(int mask) {
310 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700311 }
312
Winson Chung1a77c3d2018-04-11 12:47:47 -0700313 public interface MultiWindowModeChangedListener {
314 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
315 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700316
Winson Chungef528762019-09-06 12:05:52 -0700317 protected void dumpMisc(String prefix, PrintWriter writer) {
318 writer.println(prefix + "deviceProfile isTransposed="
319 + getDeviceProfile().isVerticalBarLayout());
320 writer.println(prefix + "orientation=" + getResources().getConfiguration().orientation);
321 writer.println(prefix + "mSystemUiController: " + mSystemUiController);
322 writer.println(prefix + "mActivityFlags: " + mActivityFlags);
323 writer.println(prefix + "mForceInvisible: " + mForceInvisible);
Sunny Goyale43d00d2018-05-14 14:23:18 -0700324 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700325
Sunny Goyalfa395362019-12-11 10:00:47 -0800326 /**
327 * A wrapper around the platform method with Launcher specific checks
328 */
329 public void startShortcut(String packageName, String id, Rect sourceBounds,
330 Bundle startActivityOptions, UserHandle user) {
331 if (GO_DISABLE_WIDGETS) {
332 return;
333 }
334 try {
vadimtd633c9c2020-01-22 18:00:37 -0800335 TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "start: shortcut", packageName);
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}