blob: 5137774b8f91a86a4be05ae860cd47b1100c2b57 [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 Goyal7eff40f2018-04-11 15:30:46 -070020import static java.lang.annotation.RetentionPolicy.SOURCE;
21
Sunny Goyal27835952017-01-13 12:15:53 -080022import android.app.Activity;
23import android.content.Context;
24import android.content.ContextWrapper;
Sunny Goyal64a75aa2017-07-03 13:50:52 -070025import android.content.Intent;
Winson Chung1a77c3d2018-04-11 12:47:47 -070026import android.content.res.Configuration;
Sunny Goyal87b5eb62018-07-03 15:53:39 -070027import android.view.ContextThemeWrapper;
Sunny Goyal27835952017-01-13 12:15:53 -080028import android.view.View.AccessibilityDelegate;
29
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
Jon Mirandafe964322017-03-22 10:25:17 -0700128 public boolean isInMultiWindowModeCompat() {
129 return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode();
130 }
131
Sunny Goyal8392c822017-06-20 10:03:56 -0700132 public SystemUiController getSystemUiController() {
133 if (mSystemUiController == null) {
134 mSystemUiController = new SystemUiController(getWindow());
135 }
136 return mSystemUiController;
137 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700138
139 @Override
140 public void onActivityResult(int requestCode, int resultCode, Intent data) {
141 super.onActivityResult(requestCode, resultCode, data);
142 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800143
144 @Override
145 protected void onStart() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700146 mActivityFlags |= ACTIVITY_STATE_STARTED;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800147 super.onStart();
148 }
149
150 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700151 protected void onResume() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700152 mActivityFlags |= ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700153 super.onResume();
154 }
155
156 @Override
157 protected void onUserLeaveHint() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700158 mActivityFlags &= ~ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700159 super.onUserLeaveHint();
160 }
161
162 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700163 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
164 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
165 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
166 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
167 }
168 }
169
170 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800171 protected void onStop() {
Tracy Zhoub67e3532018-04-17 23:45:47 -0700172 mActivityFlags &= ~ACTIVITY_STATE_STARTED & ~ACTIVITY_STATE_USER_ACTIVE;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700173 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800174 super.onStop();
175 }
176
Sunny Goyal3483c522018-04-12 11:23:33 -0700177 @Override
178 protected void onPause() {
179 mActivityFlags &= ~ACTIVITY_STATE_RESUMED;
180 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000181
182 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
183 // here instead of at the end of the animation because the start of the new activity does
184 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
185 // back to the new app (causing a flash)
186 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700187 }
188
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800189 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700190 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
191 }
192
193 /**
194 * isResumed in already defined as a hidden final method in Activity.java
195 */
196 public boolean hasBeenResumed() {
197 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800198 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800199
Tracy Zhoua706f002018-03-28 13:55:19 -0700200 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700201 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700202 }
203
Sunny Goyalfde55052018-02-01 14:46:13 -0800204 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
205 mDPChangeListeners.add(listener);
206 }
207
Winson Chung8a968fa2018-03-15 17:59:04 -0700208 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
209 mDPChangeListeners.remove(listener);
210 }
211
Sunny Goyalfde55052018-02-01 14:46:13 -0800212 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700213 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800214 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
215 }
216 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700217
Winson Chung1a77c3d2018-04-11 12:47:47 -0700218 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
219 mMultiWindowModeChangedListeners.add(listener);
220 }
221
222 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
223 mMultiWindowModeChangedListeners.remove(listener);
224 }
225
Sunny Goyalf633ef52018-03-13 09:57:05 -0700226 /**
Winson Chung9800e732018-04-04 13:33:23 -0700227 * Used to set the override visibility state, used only to handle the transition home with the
228 * recents animation.
Kevinc4ad03b2019-01-16 18:51:53 -0800229 * @see QuickstepAppTransitionManagerImpl#getWallpaperOpenRunner()
Winson Chung9800e732018-04-04 13:33:23 -0700230 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700231 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
232 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700233 }
234
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700235 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
236 mForceInvisible &= ~flag;
237 }
238
Winson Chung9800e732018-04-04 13:33:23 -0700239 /**
240 * @return Wether this activity should be considered invisible regardless of actual visibility.
241 */
242 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700243 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
244 }
245
246 public boolean hasSomeInvisibleFlag(int mask) {
247 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700248 }
249
Winson Chung1a77c3d2018-04-11 12:47:47 -0700250 public interface MultiWindowModeChangedListener {
251 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
252 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700253
Sunny Goyal55eb5562018-05-15 14:53:30 -0700254 @Override
255 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
256 if (!UiFactory.dumpActivity(this, writer)) {
257 super.dump(prefix, fd, writer, args);
258 }
259 }
260
Sunny Goyale43d00d2018-05-14 14:23:18 -0700261 protected void dumpMisc(PrintWriter writer) {
262 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
263 writer.println(" orientation=" + getResources().getConfiguration().orientation);
264 writer.println(" mSystemUiController: " + mSystemUiController);
265 writer.println(" mActivityFlags: " + mActivityFlags);
266 writer.println(" mForceInvisible: " + mForceInvisible);
267 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700268
269 public static <T extends BaseActivity> T fromContext(Context context) {
270 if (context instanceof BaseActivity) {
271 return (T) context;
272 } else if (context instanceof ContextThemeWrapper) {
273 return fromContext(((ContextWrapper) context).getBaseContext());
274 } else {
275 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
276 }
277 }
Sunny Goyal27835952017-01-13 12:15:53 -0800278}