blob: 2ec7e012c41fcd8b96913fdc3a7afce54a8ec818 [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 Goyale43d00d2018-05-14 14:23:18 -070020
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 -080029import android.view.View.AccessibilityDelegate;
30
Sunny Goyalfde55052018-02-01 14:46:13 -080031import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener;
Sunny Goyala535ae42017-02-27 10:07:13 -080032import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070033import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyal55eb5562018-05-15 14:53:30 -070034import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070035import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal8392c822017-06-20 10:03:56 -070036import com.android.launcher3.util.SystemUiController;
Sunny Goyala535ae42017-02-27 10:07:13 -080037
Sunny Goyal55eb5562018-05-15 14:53:30 -070038import java.io.FileDescriptor;
Sunny Goyale43d00d2018-05-14 14:23:18 -070039import java.io.PrintWriter;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070040import java.lang.annotation.Retention;
Sunny Goyalfde55052018-02-01 14:46:13 -080041import java.util.ArrayList;
42
Sunny Goyald2303072018-08-14 15:21:45 -070043import androidx.annotation.IntDef;
44
Hyunyoung Song46d07f72018-05-22 15:41:25 -070045public abstract class BaseActivity extends Activity implements UserEventDelegate{
Sunny Goyal27835952017-01-13 12:15:53 -080046
Sunny Goyal7eff40f2018-04-11 15:30:46 -070047 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
48 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
Sunny Goyal1c63c722018-06-05 16:00:34 -070049 public static final int INVISIBLE_BY_PENDING_FLAGS = 1 << 2;
50
51 // This is not treated as invisibility flag, but adds as a hint for an incomplete transition.
52 // When the wallpaper animation runs, it replaces this flag with a proper invisibility
53 // flag, INVISIBLE_BY_PENDING_FLAGS only for the duration of that animation.
54 public static final int PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION = 1 << 3;
55
56 private static final int INVISIBLE_FLAGS =
57 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS | INVISIBLE_BY_PENDING_FLAGS;
58 public static final int STATE_HANDLER_INVISIBILITY_FLAGS =
59 INVISIBLE_BY_STATE_HANDLER | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070060 public static final int INVISIBLE_ALL =
Sunny Goyal1c63c722018-06-05 16:00:34 -070061 INVISIBLE_FLAGS | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070062
63 @Retention(SOURCE)
64 @IntDef(
65 flag = true,
Sunny Goyal1c63c722018-06-05 16:00:34 -070066 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS,
67 INVISIBLE_BY_PENDING_FLAGS, PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION})
Sunny Goyal7eff40f2018-04-11 15:30:46 -070068 public @interface InvisibilityFlags{}
69
Sunny Goyalfde55052018-02-01 14:46:13 -080070 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070071 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
72 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080073
Sunny Goyal27835952017-01-13 12:15:53 -080074 protected DeviceProfile mDeviceProfile;
Sunny Goyala535ae42017-02-27 10:07:13 -080075 protected UserEventDispatcher mUserEventDispatcher;
Sunny Goyal8392c822017-06-20 10:03:56 -070076 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080077
Sunny Goyal3483c522018-04-12 11:23:33 -070078 private static final int ACTIVITY_STATE_STARTED = 1 << 0;
79 private static final int ACTIVITY_STATE_RESUMED = 1 << 1;
80 /**
81 * State flag indicating if the user is active or the actitvity when to background as a result
82 * of user action.
83 * @see #isUserActive()
84 */
85 private static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 2;
86
87 @Retention(SOURCE)
88 @IntDef(
89 flag = true,
90 value = {ACTIVITY_STATE_STARTED, ACTIVITY_STATE_RESUMED, ACTIVITY_STATE_USER_ACTIVE})
91 public @interface ActivityFlags{}
92
93 @ActivityFlags
94 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070095
Winson Chung9800e732018-04-04 13:33:23 -070096 // When the recents animation is running, the visibility of the Launcher is managed by the
97 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -070098 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -080099
Sunny Goyal27835952017-01-13 12:15:53 -0800100 public DeviceProfile getDeviceProfile() {
101 return mDeviceProfile;
102 }
103
104 public AccessibilityDelegate getAccessibilityDelegate() {
105 return null;
106 }
107
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700108 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {}
109
Sunny Goyala535ae42017-02-27 10:07:13 -0800110 public final UserEventDispatcher getUserEventDispatcher() {
111 if (mUserEventDispatcher == null) {
Hyunyoung Song956ec4b2018-07-02 13:17:32 -0700112 mUserEventDispatcher = UserEventDispatcher.newInstance(this, this);
Sunny Goyala535ae42017-02-27 10:07:13 -0800113 }
114 return mUserEventDispatcher;
115 }
116
Jon Mirandafe964322017-03-22 10:25:17 -0700117 public boolean isInMultiWindowModeCompat() {
118 return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode();
119 }
120
Sunny Goyal8392c822017-06-20 10:03:56 -0700121 public SystemUiController getSystemUiController() {
122 if (mSystemUiController == null) {
123 mSystemUiController = new SystemUiController(getWindow());
124 }
125 return mSystemUiController;
126 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700127
128 @Override
129 public void onActivityResult(int requestCode, int resultCode, Intent data) {
130 super.onActivityResult(requestCode, resultCode, data);
131 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800132
133 @Override
134 protected void onStart() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700135 mActivityFlags |= ACTIVITY_STATE_STARTED;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800136 super.onStart();
137 }
138
139 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700140 protected void onResume() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700141 mActivityFlags |= ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700142 super.onResume();
143 }
144
145 @Override
146 protected void onUserLeaveHint() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700147 mActivityFlags &= ~ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700148 super.onUserLeaveHint();
149 }
150
151 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700152 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
153 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
154 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
155 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
156 }
157 }
158
159 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800160 protected void onStop() {
Tracy Zhoub67e3532018-04-17 23:45:47 -0700161 mActivityFlags &= ~ACTIVITY_STATE_STARTED & ~ACTIVITY_STATE_USER_ACTIVE;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700162 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800163 super.onStop();
164 }
165
Sunny Goyal3483c522018-04-12 11:23:33 -0700166 @Override
167 protected void onPause() {
168 mActivityFlags &= ~ACTIVITY_STATE_RESUMED;
169 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000170
171 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
172 // here instead of at the end of the animation because the start of the new activity does
173 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
174 // back to the new app (causing a flash)
175 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700176 }
177
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800178 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700179 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
180 }
181
182 /**
183 * isResumed in already defined as a hidden final method in Activity.java
184 */
185 public boolean hasBeenResumed() {
186 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800187 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800188
Tracy Zhoua706f002018-03-28 13:55:19 -0700189 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700190 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700191 }
192
Sunny Goyalfde55052018-02-01 14:46:13 -0800193 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
194 mDPChangeListeners.add(listener);
195 }
196
Winson Chung8a968fa2018-03-15 17:59:04 -0700197 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
198 mDPChangeListeners.remove(listener);
199 }
200
Sunny Goyalfde55052018-02-01 14:46:13 -0800201 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700202 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800203 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
204 }
205 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700206
Winson Chung1a77c3d2018-04-11 12:47:47 -0700207 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
208 mMultiWindowModeChangedListeners.add(listener);
209 }
210
211 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
212 mMultiWindowModeChangedListeners.remove(listener);
213 }
214
Sunny Goyalf633ef52018-03-13 09:57:05 -0700215 /**
Winson Chung9800e732018-04-04 13:33:23 -0700216 * Used to set the override visibility state, used only to handle the transition home with the
217 * recents animation.
Sunny Goyal1c63c722018-06-05 16:00:34 -0700218 * @see LauncherAppTransitionManagerImpl#getWallpaperOpenRunner()
Winson Chung9800e732018-04-04 13:33:23 -0700219 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700220 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
221 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700222 }
223
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700224 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
225 mForceInvisible &= ~flag;
226 }
227
Winson Chung9800e732018-04-04 13:33:23 -0700228 /**
229 * @return Wether this activity should be considered invisible regardless of actual visibility.
230 */
231 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700232 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
233 }
234
235 public boolean hasSomeInvisibleFlag(int mask) {
236 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700237 }
238
Winson Chung1a77c3d2018-04-11 12:47:47 -0700239 public interface MultiWindowModeChangedListener {
240 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
241 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700242
Sunny Goyal55eb5562018-05-15 14:53:30 -0700243 @Override
244 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
245 if (!UiFactory.dumpActivity(this, writer)) {
246 super.dump(prefix, fd, writer, args);
247 }
248 }
249
Sunny Goyale43d00d2018-05-14 14:23:18 -0700250 protected void dumpMisc(PrintWriter writer) {
251 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
252 writer.println(" orientation=" + getResources().getConfiguration().orientation);
253 writer.println(" mSystemUiController: " + mSystemUiController);
254 writer.println(" mActivityFlags: " + mActivityFlags);
255 writer.println(" mForceInvisible: " + mForceInvisible);
256 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700257
258 public static <T extends BaseActivity> T fromContext(Context context) {
259 if (context instanceof BaseActivity) {
260 return (T) context;
261 } else if (context instanceof ContextThemeWrapper) {
262 return fromContext(((ContextWrapper) context).getBaseContext());
263 } else {
264 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
265 }
266 }
Sunny Goyal27835952017-01-13 12:15:53 -0800267}