blob: 7f7224241be1961a8572b2ce2a003d9737fd2bcf [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 Goyalfe8e4a92018-11-13 19:43:57 -080039import com.android.launcher3.views.ActivityContext;
Sunny Goyala535ae42017-02-27 10:07:13 -080040
Sunny Goyal55eb5562018-05-15 14:53:30 -070041import java.io.FileDescriptor;
Sunny Goyale43d00d2018-05-14 14:23:18 -070042import java.io.PrintWriter;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070043import java.lang.annotation.Retention;
Sunny Goyalfde55052018-02-01 14:46:13 -080044import java.util.ArrayList;
45
Sunny Goyald2303072018-08-14 15:21:45 -070046import androidx.annotation.IntDef;
47
Sunny Goyalfe8e4a92018-11-13 19:43:57 -080048public abstract class BaseActivity extends Activity
49 implements UserEventDelegate, LogStateProvider, ActivityContext {
Sunny Goyal27835952017-01-13 12:15:53 -080050
Sunny Goyal7eff40f2018-04-11 15:30:46 -070051 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
52 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
Sunny Goyal1c63c722018-06-05 16:00:34 -070053 public static final int INVISIBLE_BY_PENDING_FLAGS = 1 << 2;
54
55 // This is not treated as invisibility flag, but adds as a hint for an incomplete transition.
56 // When the wallpaper animation runs, it replaces this flag with a proper invisibility
57 // flag, INVISIBLE_BY_PENDING_FLAGS only for the duration of that animation.
58 public static final int PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION = 1 << 3;
59
60 private static final int INVISIBLE_FLAGS =
61 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS | INVISIBLE_BY_PENDING_FLAGS;
62 public static final int STATE_HANDLER_INVISIBILITY_FLAGS =
63 INVISIBLE_BY_STATE_HANDLER | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070064 public static final int INVISIBLE_ALL =
Sunny Goyal1c63c722018-06-05 16:00:34 -070065 INVISIBLE_FLAGS | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070066
67 @Retention(SOURCE)
68 @IntDef(
69 flag = true,
Sunny Goyal1c63c722018-06-05 16:00:34 -070070 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS,
71 INVISIBLE_BY_PENDING_FLAGS, PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION})
Sunny Goyal7eff40f2018-04-11 15:30:46 -070072 public @interface InvisibilityFlags{}
73
Sunny Goyalfde55052018-02-01 14:46:13 -080074 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070075 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
76 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080077
Sunny Goyal27835952017-01-13 12:15:53 -080078 protected DeviceProfile mDeviceProfile;
Sunny Goyala535ae42017-02-27 10:07:13 -080079 protected UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfc007472018-10-25 14:09:50 -070080 protected StatsLogManager mStatsLogManager;
Sunny Goyal8392c822017-06-20 10:03:56 -070081 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080082
Sunny Goyal3483c522018-04-12 11:23:33 -070083 private static final int ACTIVITY_STATE_STARTED = 1 << 0;
84 private static final int ACTIVITY_STATE_RESUMED = 1 << 1;
85 /**
86 * State flag indicating if the user is active or the actitvity when to background as a result
87 * of user action.
88 * @see #isUserActive()
89 */
90 private static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 2;
91
92 @Retention(SOURCE)
93 @IntDef(
94 flag = true,
95 value = {ACTIVITY_STATE_STARTED, ACTIVITY_STATE_RESUMED, ACTIVITY_STATE_USER_ACTIVE})
96 public @interface ActivityFlags{}
97
98 @ActivityFlags
99 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700100
Winson Chung9800e732018-04-04 13:33:23 -0700101 // When the recents animation is running, the visibility of the Launcher is managed by the
102 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700103 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800104
Sunny Goyalfe8e4a92018-11-13 19:43:57 -0800105 @Override
Sunny Goyal27835952017-01-13 12:15:53 -0800106 public DeviceProfile getDeviceProfile() {
107 return mDeviceProfile;
108 }
109
Hyunyoung Songfc007472018-10-25 14:09:50 -0700110 public int getCurrentState() { return StatsLogUtils.LAUNCHER_STATE_BACKGROUND; }
111
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700112 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {}
113
Hyunyoung Songfc007472018-10-25 14:09:50 -0700114 public final StatsLogManager getStatsLogManager() {
115 if (mStatsLogManager == null) {
116 mStatsLogManager = StatsLogManager.newInstance(this, this);
117 }
118 return mStatsLogManager;
119 }
120
Sunny Goyala535ae42017-02-27 10:07:13 -0800121 public final UserEventDispatcher getUserEventDispatcher() {
122 if (mUserEventDispatcher == null) {
Hyunyoung Song956ec4b2018-07-02 13:17:32 -0700123 mUserEventDispatcher = UserEventDispatcher.newInstance(this, this);
Sunny Goyala535ae42017-02-27 10:07:13 -0800124 }
125 return mUserEventDispatcher;
126 }
127
Sunny Goyal8392c822017-06-20 10:03:56 -0700128 public SystemUiController getSystemUiController() {
129 if (mSystemUiController == null) {
130 mSystemUiController = new SystemUiController(getWindow());
131 }
132 return mSystemUiController;
133 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700134
135 @Override
136 public void onActivityResult(int requestCode, int resultCode, Intent data) {
137 super.onActivityResult(requestCode, resultCode, data);
138 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800139
140 @Override
141 protected void onStart() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700142 mActivityFlags |= ACTIVITY_STATE_STARTED;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800143 super.onStart();
144 }
145
146 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700147 protected void onResume() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700148 mActivityFlags |= ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700149 super.onResume();
150 }
151
152 @Override
153 protected void onUserLeaveHint() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700154 mActivityFlags &= ~ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700155 super.onUserLeaveHint();
156 }
157
158 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700159 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
160 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
161 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
162 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
163 }
164 }
165
166 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800167 protected void onStop() {
Tracy Zhoub67e3532018-04-17 23:45:47 -0700168 mActivityFlags &= ~ACTIVITY_STATE_STARTED & ~ACTIVITY_STATE_USER_ACTIVE;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700169 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800170 super.onStop();
171 }
172
Sunny Goyal3483c522018-04-12 11:23:33 -0700173 @Override
174 protected void onPause() {
175 mActivityFlags &= ~ACTIVITY_STATE_RESUMED;
176 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000177
178 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
179 // here instead of at the end of the animation because the start of the new activity does
180 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
181 // back to the new app (causing a flash)
182 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700183 }
184
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800185 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700186 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
187 }
188
189 /**
190 * isResumed in already defined as a hidden final method in Activity.java
191 */
192 public boolean hasBeenResumed() {
193 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800194 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800195
Tracy Zhoua706f002018-03-28 13:55:19 -0700196 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700197 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700198 }
199
Sunny Goyalfde55052018-02-01 14:46:13 -0800200 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
201 mDPChangeListeners.add(listener);
202 }
203
Winson Chung8a968fa2018-03-15 17:59:04 -0700204 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
205 mDPChangeListeners.remove(listener);
206 }
207
Sunny Goyalfde55052018-02-01 14:46:13 -0800208 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700209 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800210 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
211 }
212 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700213
Winson Chung1a77c3d2018-04-11 12:47:47 -0700214 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
215 mMultiWindowModeChangedListeners.add(listener);
216 }
217
218 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
219 mMultiWindowModeChangedListeners.remove(listener);
220 }
221
Sunny Goyalf633ef52018-03-13 09:57:05 -0700222 /**
Winson Chung9800e732018-04-04 13:33:23 -0700223 * Used to set the override visibility state, used only to handle the transition home with the
224 * recents animation.
Kevinc4ad03b2019-01-16 18:51:53 -0800225 * @see QuickstepAppTransitionManagerImpl#getWallpaperOpenRunner()
Winson Chung9800e732018-04-04 13:33:23 -0700226 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700227 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
228 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700229 }
230
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700231 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
232 mForceInvisible &= ~flag;
233 }
234
Winson Chung9800e732018-04-04 13:33:23 -0700235 /**
236 * @return Wether this activity should be considered invisible regardless of actual visibility.
237 */
238 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700239 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
240 }
241
242 public boolean hasSomeInvisibleFlag(int mask) {
243 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700244 }
245
Winson Chung1a77c3d2018-04-11 12:47:47 -0700246 public interface MultiWindowModeChangedListener {
247 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
248 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700249
Sunny Goyal55eb5562018-05-15 14:53:30 -0700250 @Override
251 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
252 if (!UiFactory.dumpActivity(this, writer)) {
253 super.dump(prefix, fd, writer, args);
254 }
255 }
256
Sunny Goyale43d00d2018-05-14 14:23:18 -0700257 protected void dumpMisc(PrintWriter writer) {
258 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
259 writer.println(" orientation=" + getResources().getConfiguration().orientation);
260 writer.println(" mSystemUiController: " + mSystemUiController);
261 writer.println(" mActivityFlags: " + mActivityFlags);
262 writer.println(" mForceInvisible: " + mForceInvisible);
263 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700264
265 public static <T extends BaseActivity> T fromContext(Context context) {
266 if (context instanceof BaseActivity) {
267 return (T) context;
268 } else if (context instanceof ContextThemeWrapper) {
269 return fromContext(((ContextWrapper) context).getBaseContext());
270 } else {
271 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
272 }
273 }
Sunny Goyal27835952017-01-13 12:15:53 -0800274}