blob: 424ffde1ff7a3e6d6333cb55199e7155a18931c4 [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
Sunny Goyalfde55052018-02-01 14:46:13 -080030import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener;
Hyunyoung Songfc007472018-10-25 14:09:50 -070031import com.android.launcher3.logging.StatsLogManager;
32import com.android.launcher3.logging.StatsLogUtils;
33import com.android.launcher3.logging.StatsLogUtils.LogStateProvider;
Sunny Goyala535ae42017-02-27 10:07:13 -080034import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070035import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyal55eb5562018-05-15 14:53:30 -070036import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070037import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal8392c822017-06-20 10:03:56 -070038import com.android.launcher3.util.SystemUiController;
Sunny Goyal56863332019-05-22 14:13:53 -070039import com.android.launcher3.util.ViewCache;
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080040import com.android.launcher3.views.ActivityContext;
Sunny Goyala535ae42017-02-27 10:07:13 -080041
Sunny Goyal55eb5562018-05-15 14:53:30 -070042import java.io.FileDescriptor;
Sunny Goyale43d00d2018-05-14 14:23:18 -070043import java.io.PrintWriter;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070044import java.lang.annotation.Retention;
Sunny Goyalfde55052018-02-01 14:46:13 -080045import java.util.ArrayList;
46
Sunny Goyald2303072018-08-14 15:21:45 -070047import androidx.annotation.IntDef;
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 Goyal3483c522018-04-12 11:23:33 -070084 private static final int ACTIVITY_STATE_STARTED = 1 << 0;
85 private static final int ACTIVITY_STATE_RESUMED = 1 << 1;
86 /**
87 * State flag indicating if the user is active or the actitvity when to background as a result
88 * of user action.
89 * @see #isUserActive()
90 */
91 private static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 2;
92
93 @Retention(SOURCE)
94 @IntDef(
95 flag = true,
96 value = {ACTIVITY_STATE_STARTED, ACTIVITY_STATE_RESUMED, ACTIVITY_STATE_USER_ACTIVE})
97 public @interface ActivityFlags{}
98
99 @ActivityFlags
100 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700101
Winson Chung9800e732018-04-04 13:33:23 -0700102 // When the recents animation is running, the visibility of the Launcher is managed by the
103 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700104 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800105
Sunny Goyal56863332019-05-22 14:13:53 -0700106 private final ViewCache mViewCache = new ViewCache();
107
108 public ViewCache getViewCache() {
109 return mViewCache;
110 }
111
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800112 @Override
Sunny Goyal27835952017-01-13 12:15:53 -0800113 public DeviceProfile getDeviceProfile() {
114 return mDeviceProfile;
115 }
116
Hyunyoung Songfc007472018-10-25 14:09:50 -0700117 public int getCurrentState() { return StatsLogUtils.LAUNCHER_STATE_BACKGROUND; }
118
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700119 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {}
120
Hyunyoung Songfc007472018-10-25 14:09:50 -0700121 public final StatsLogManager getStatsLogManager() {
122 if (mStatsLogManager == null) {
123 mStatsLogManager = StatsLogManager.newInstance(this, this);
124 }
125 return mStatsLogManager;
126 }
127
Sunny Goyala535ae42017-02-27 10:07:13 -0800128 public final UserEventDispatcher getUserEventDispatcher() {
129 if (mUserEventDispatcher == null) {
Hyunyoung Song956ec4b2018-07-02 13:17:32 -0700130 mUserEventDispatcher = UserEventDispatcher.newInstance(this, this);
Sunny Goyala535ae42017-02-27 10:07:13 -0800131 }
132 return mUserEventDispatcher;
133 }
134
Sunny Goyal8392c822017-06-20 10:03:56 -0700135 public SystemUiController getSystemUiController() {
136 if (mSystemUiController == null) {
137 mSystemUiController = new SystemUiController(getWindow());
138 }
139 return mSystemUiController;
140 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700141
142 @Override
143 public void onActivityResult(int requestCode, int resultCode, Intent data) {
144 super.onActivityResult(requestCode, resultCode, data);
145 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800146
147 @Override
148 protected void onStart() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700149 mActivityFlags |= ACTIVITY_STATE_STARTED;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800150 super.onStart();
151 }
152
153 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700154 protected void onResume() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700155 mActivityFlags |= ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700156 super.onResume();
157 }
158
159 @Override
160 protected void onUserLeaveHint() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700161 mActivityFlags &= ~ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700162 super.onUserLeaveHint();
163 }
164
165 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700166 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
167 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
168 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
169 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
170 }
171 }
172
173 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800174 protected void onStop() {
Tracy Zhoub67e3532018-04-17 23:45:47 -0700175 mActivityFlags &= ~ACTIVITY_STATE_STARTED & ~ACTIVITY_STATE_USER_ACTIVE;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700176 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800177 super.onStop();
178 }
179
Sunny Goyal3483c522018-04-12 11:23:33 -0700180 @Override
181 protected void onPause() {
182 mActivityFlags &= ~ACTIVITY_STATE_RESUMED;
183 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000184
185 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
186 // here instead of at the end of the animation because the start of the new activity does
187 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
188 // back to the new app (causing a flash)
189 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700190 }
191
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800192 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700193 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
194 }
195
196 /**
197 * isResumed in already defined as a hidden final method in Activity.java
198 */
199 public boolean hasBeenResumed() {
200 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800201 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800202
Tracy Zhoua706f002018-03-28 13:55:19 -0700203 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700204 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700205 }
206
Sunny Goyalfde55052018-02-01 14:46:13 -0800207 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
208 mDPChangeListeners.add(listener);
209 }
210
Winson Chung8a968fa2018-03-15 17:59:04 -0700211 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
212 mDPChangeListeners.remove(listener);
213 }
214
Sunny Goyalfde55052018-02-01 14:46:13 -0800215 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700216 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800217 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
218 }
219 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700220
Winson Chung1a77c3d2018-04-11 12:47:47 -0700221 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
222 mMultiWindowModeChangedListeners.add(listener);
223 }
224
225 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
226 mMultiWindowModeChangedListeners.remove(listener);
227 }
228
Sunny Goyalf633ef52018-03-13 09:57:05 -0700229 /**
Winson Chung9800e732018-04-04 13:33:23 -0700230 * Used to set the override visibility state, used only to handle the transition home with the
231 * recents animation.
Kevinc4ad03b2019-01-16 18:51:53 -0800232 * @see QuickstepAppTransitionManagerImpl#getWallpaperOpenRunner()
Winson Chung9800e732018-04-04 13:33:23 -0700233 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700234 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
235 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700236 }
237
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700238 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
239 mForceInvisible &= ~flag;
240 }
241
Winson Chung9800e732018-04-04 13:33:23 -0700242 /**
243 * @return Wether this activity should be considered invisible regardless of actual visibility.
244 */
245 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700246 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
247 }
248
249 public boolean hasSomeInvisibleFlag(int mask) {
250 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700251 }
252
Winson Chung1a77c3d2018-04-11 12:47:47 -0700253 public interface MultiWindowModeChangedListener {
254 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
255 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700256
Sunny Goyal55eb5562018-05-15 14:53:30 -0700257 @Override
258 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
259 if (!UiFactory.dumpActivity(this, writer)) {
260 super.dump(prefix, fd, writer, args);
261 }
262 }
263
Sunny Goyale43d00d2018-05-14 14:23:18 -0700264 protected void dumpMisc(PrintWriter writer) {
265 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
266 writer.println(" orientation=" + getResources().getConfiguration().orientation);
267 writer.println(" mSystemUiController: " + mSystemUiController);
268 writer.println(" mActivityFlags: " + mActivityFlags);
269 writer.println(" mForceInvisible: " + mForceInvisible);
270 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700271
272 public static <T extends BaseActivity> T fromContext(Context context) {
273 if (context instanceof BaseActivity) {
274 return (T) context;
275 } else if (context instanceof ContextThemeWrapper) {
276 return fromContext(((ContextWrapper) context).getBaseContext());
277 } else {
278 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
279 }
280 }
Sunny Goyal27835952017-01-13 12:15:53 -0800281}