blob: 382bfdf51366910eee8e871a05de62ff4990de3d [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
Winson Chunga0f09f92018-05-11 21:55:21 +000019import static com.android.launcher3.util.SystemUiController.UI_STATE_OVERVIEW;
Sunny Goyal8c48d8b2019-01-25 15:10:18 -080020
Sunny Goyal7eff40f2018-04-11 15:30:46 -070021import static java.lang.annotation.RetentionPolicy.SOURCE;
22
Sunny Goyal27835952017-01-13 12:15:53 -080023import android.app.Activity;
24import android.content.Context;
25import android.content.ContextWrapper;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070026import android.content.Intent;
Winson Chung1a77c3d2018-04-11 12:47:47 -070027import android.content.res.Configuration;
Sunny Goyal87b5eb62018-07-03 15:53:39 -070028import android.view.ContextThemeWrapper;
Sunny Goyal27835952017-01-13 12:15:53 -080029
Winson Chungef528762019-09-06 12:05:52 -070030import androidx.annotation.IntDef;
31
Sunny Goyalfde55052018-02-01 14:46:13 -080032import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener;
Hyunyoung Songfc007472018-10-25 14:09:50 -070033import com.android.launcher3.logging.StatsLogManager;
34import com.android.launcher3.logging.StatsLogUtils;
35import com.android.launcher3.logging.StatsLogUtils.LogStateProvider;
Sunny Goyala535ae42017-02-27 10:07:13 -080036import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070037import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyal210e1742019-10-17 12:05:38 -070038import com.android.launcher3.uioverrides.ApiWrapper;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070039import com.android.launcher3.userevent.nano.LauncherLogProto;
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;
Sunny Goyala535ae42017-02-27 10:07:13 -080043
Sunny Goyal55eb5562018-05-15 14:53:30 -070044import java.io.FileDescriptor;
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;
48
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080049public abstract class BaseActivity extends Activity
50 implements UserEventDelegate, LogStateProvider, ActivityContext {
Sunny Goyal27835952017-01-13 12:15:53 -080051
Sunny Goyal7eff40f2018-04-11 15:30:46 -070052 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
53 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
Sunny Goyal1c63c722018-06-05 16:00:34 -070054 public static final int INVISIBLE_BY_PENDING_FLAGS = 1 << 2;
55
56 // This is not treated as invisibility flag, but adds as a hint for an incomplete transition.
57 // When the wallpaper animation runs, it replaces this flag with a proper invisibility
58 // flag, INVISIBLE_BY_PENDING_FLAGS only for the duration of that animation.
59 public static final int PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION = 1 << 3;
60
61 private static final int INVISIBLE_FLAGS =
62 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS | INVISIBLE_BY_PENDING_FLAGS;
63 public static final int STATE_HANDLER_INVISIBILITY_FLAGS =
64 INVISIBLE_BY_STATE_HANDLER | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070065 public static final int INVISIBLE_ALL =
Sunny Goyal1c63c722018-06-05 16:00:34 -070066 INVISIBLE_FLAGS | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070067
68 @Retention(SOURCE)
69 @IntDef(
70 flag = true,
Sunny Goyal1c63c722018-06-05 16:00:34 -070071 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS,
72 INVISIBLE_BY_PENDING_FLAGS, PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION})
Sunny Goyal7eff40f2018-04-11 15:30:46 -070073 public @interface InvisibilityFlags{}
74
Sunny Goyalfde55052018-02-01 14:46:13 -080075 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070076 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
77 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080078
Sunny Goyal27835952017-01-13 12:15:53 -080079 protected DeviceProfile mDeviceProfile;
Sunny Goyala535ae42017-02-27 10:07:13 -080080 protected UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfc007472018-10-25 14:09:50 -070081 protected StatsLogManager mStatsLogManager;
Sunny Goyal8392c822017-06-20 10:03:56 -070082 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080083
Sunny Goyal210e1742019-10-17 12:05:38 -070084
85 public static final int ACTIVITY_STATE_STARTED = 1 << 0;
86 public static final int ACTIVITY_STATE_RESUMED = 1 << 1;
87
Sunny Goyal3483c522018-04-12 11:23:33 -070088 /**
Sunny Goyal210e1742019-10-17 12:05:38 -070089 * State flags indicating that the activity has received one frame after resume, and was
90 * not immediately paused.
91 */
92 public static final int ACTIVITY_STATE_DEFERRED_RESUMED = 1 << 2;
93
94 public static final int ACTIVITY_STATE_WINDOW_FOCUSED = 1 << 3;
95
96 /**
97 * State flag indicating if the user is active or the activity when to background as a result
Sunny Goyal3483c522018-04-12 11:23:33 -070098 * of user action.
99 * @see #isUserActive()
100 */
Sunny Goyal210e1742019-10-17 12:05:38 -0700101 public static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 4;
102
103 /**
104 * State flag indicating that a state transition is in progress
105 */
106 public static final int ACTIVITY_STATE_TRANSITION_ACTIVE = 1 << 5;
Sunny Goyal3483c522018-04-12 11:23:33 -0700107
108 @Retention(SOURCE)
109 @IntDef(
110 flag = true,
Sunny Goyal210e1742019-10-17 12:05:38 -0700111 value = {ACTIVITY_STATE_STARTED,
112 ACTIVITY_STATE_RESUMED,
113 ACTIVITY_STATE_DEFERRED_RESUMED,
114 ACTIVITY_STATE_WINDOW_FOCUSED,
115 ACTIVITY_STATE_USER_ACTIVE,
116 ACTIVITY_STATE_TRANSITION_ACTIVE})
Sunny Goyal3483c522018-04-12 11:23:33 -0700117 public @interface ActivityFlags{}
118
119 @ActivityFlags
120 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700121
Winson Chung9800e732018-04-04 13:33:23 -0700122 // When the recents animation is running, the visibility of the Launcher is managed by the
123 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700124 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800125
Sunny Goyal56863332019-05-22 14:13:53 -0700126 private final ViewCache mViewCache = new ViewCache();
127
128 public ViewCache getViewCache() {
129 return mViewCache;
130 }
131
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800132 @Override
Sunny Goyal27835952017-01-13 12:15:53 -0800133 public DeviceProfile getDeviceProfile() {
134 return mDeviceProfile;
135 }
136
Hyunyoung Songfc007472018-10-25 14:09:50 -0700137 public int getCurrentState() { return StatsLogUtils.LAUNCHER_STATE_BACKGROUND; }
138
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700139 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {}
140
Hyunyoung Songfc007472018-10-25 14:09:50 -0700141 public final StatsLogManager getStatsLogManager() {
142 if (mStatsLogManager == null) {
143 mStatsLogManager = StatsLogManager.newInstance(this, this);
144 }
145 return mStatsLogManager;
146 }
147
Sunny Goyala535ae42017-02-27 10:07:13 -0800148 public final UserEventDispatcher getUserEventDispatcher() {
149 if (mUserEventDispatcher == null) {
Hyunyoung Song956ec4b2018-07-02 13:17:32 -0700150 mUserEventDispatcher = UserEventDispatcher.newInstance(this, this);
Sunny Goyala535ae42017-02-27 10:07:13 -0800151 }
152 return mUserEventDispatcher;
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
162 @Override
163 public void onActivityResult(int requestCode, int resultCode, Intent data) {
164 super.onActivityResult(requestCode, resultCode, data);
165 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800166
167 @Override
168 protected void onStart() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700169 addActivityFlags(ACTIVITY_STATE_STARTED);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800170 super.onStart();
171 }
172
173 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700174 protected void onResume() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700175 addActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700176 super.onResume();
177 }
178
179 @Override
180 protected void onUserLeaveHint() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700181 removeActivityFlags(ACTIVITY_STATE_USER_ACTIVE);
Tracy Zhoua706f002018-03-28 13:55:19 -0700182 super.onUserLeaveHint();
183 }
184
185 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700186 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
187 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
188 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
189 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
190 }
191 }
192
193 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800194 protected void onStop() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700195 removeActivityFlags(ACTIVITY_STATE_STARTED | ACTIVITY_STATE_USER_ACTIVE);
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700196 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800197 super.onStop();
Winson Chunga36c8002019-06-11 16:15:54 -0700198
199 // Reset the overridden sysui flags used for the task-swipe launch animation, this is a
200 // catch all for if we do not get resumed (and therefore not paused below)
201 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800202 }
203
Sunny Goyal3483c522018-04-12 11:23:33 -0700204 @Override
205 protected void onPause() {
Sunny Goyal210e1742019-10-17 12:05:38 -0700206 removeActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_DEFERRED_RESUMED);
Sunny Goyal3483c522018-04-12 11:23:33 -0700207 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000208
209 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
210 // here instead of at the end of the animation because the start of the new activity does
211 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
212 // back to the new app (causing a flash)
213 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700214 }
215
Sunny Goyal210e1742019-10-17 12:05:38 -0700216 @Override
217 public void onWindowFocusChanged(boolean hasFocus) {
218 super.onWindowFocusChanged(hasFocus);
219 if (hasFocus) {
220 addActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
221 } else {
222 removeActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED);
223 }
224
225 }
226
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800227 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700228 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
229 }
230
231 /**
232 * isResumed in already defined as a hidden final method in Activity.java
233 */
234 public boolean hasBeenResumed() {
235 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800236 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800237
Tracy Zhoua706f002018-03-28 13:55:19 -0700238 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700239 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700240 }
241
Sunny Goyal210e1742019-10-17 12:05:38 -0700242 public int getActivityFlags() {
243 return mActivityFlags;
244 }
245
246 protected void addActivityFlags(int flags) {
247 mActivityFlags |= flags;
248 onActivityFlagsChanged(flags);
249 }
250
251 protected void removeActivityFlags(int flags) {
252 mActivityFlags &= ~flags;
253 onActivityFlagsChanged(flags);
254 }
255
256 protected void onActivityFlagsChanged(int changeBits) { }
257
Sunny Goyalfde55052018-02-01 14:46:13 -0800258 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
259 mDPChangeListeners.add(listener);
260 }
261
Winson Chung8a968fa2018-03-15 17:59:04 -0700262 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
263 mDPChangeListeners.remove(listener);
264 }
265
Sunny Goyalfde55052018-02-01 14:46:13 -0800266 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700267 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800268 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
269 }
270 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700271
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 Goyal210e1742019-10-17 12:05:38 -0700283 * @see QuickstepAppTransitionManagerImpl#getWallpaperOpenRunner
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
Sunny Goyal55eb5562018-05-15 14:53:30 -0700308 @Override
309 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
Sunny Goyal210e1742019-10-17 12:05:38 -0700310 if (!ApiWrapper.dumpActivity(this, writer)) {
Sunny Goyal55eb5562018-05-15 14:53:30 -0700311 super.dump(prefix, fd, writer, args);
312 }
313 }
314
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
324 public static <T extends BaseActivity> T fromContext(Context context) {
325 if (context instanceof BaseActivity) {
326 return (T) context;
327 } else if (context instanceof ContextThemeWrapper) {
328 return fromContext(((ContextWrapper) context).getBaseContext());
329 } else {
330 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
331 }
332 }
Sunny Goyal27835952017-01-13 12:15:53 -0800333}