blob: 77b6010c267c2aa7bd3efa5bd26b749928796774 [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 Goyala535ae42017-02-27 10:07:13 -080039
Sunny Goyal55eb5562018-05-15 14:53:30 -070040import java.io.FileDescriptor;
Sunny Goyale43d00d2018-05-14 14:23:18 -070041import java.io.PrintWriter;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070042import java.lang.annotation.Retention;
Sunny Goyalfde55052018-02-01 14:46:13 -080043import java.util.ArrayList;
44
Sunny Goyald2303072018-08-14 15:21:45 -070045import androidx.annotation.IntDef;
46
Hyunyoung Songfc007472018-10-25 14:09:50 -070047public abstract class BaseActivity extends Activity implements UserEventDelegate, LogStateProvider{
Sunny Goyal27835952017-01-13 12:15:53 -080048
Sunny Goyal7eff40f2018-04-11 15:30:46 -070049 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
50 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
Sunny Goyal1c63c722018-06-05 16:00:34 -070051 public static final int INVISIBLE_BY_PENDING_FLAGS = 1 << 2;
52
53 // This is not treated as invisibility flag, but adds as a hint for an incomplete transition.
54 // When the wallpaper animation runs, it replaces this flag with a proper invisibility
55 // flag, INVISIBLE_BY_PENDING_FLAGS only for the duration of that animation.
56 public static final int PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION = 1 << 3;
57
58 private static final int INVISIBLE_FLAGS =
59 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS | INVISIBLE_BY_PENDING_FLAGS;
60 public static final int STATE_HANDLER_INVISIBILITY_FLAGS =
61 INVISIBLE_BY_STATE_HANDLER | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070062 public static final int INVISIBLE_ALL =
Sunny Goyal1c63c722018-06-05 16:00:34 -070063 INVISIBLE_FLAGS | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070064
65 @Retention(SOURCE)
66 @IntDef(
67 flag = true,
Sunny Goyal1c63c722018-06-05 16:00:34 -070068 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS,
69 INVISIBLE_BY_PENDING_FLAGS, PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION})
Sunny Goyal7eff40f2018-04-11 15:30:46 -070070 public @interface InvisibilityFlags{}
71
Sunny Goyalfde55052018-02-01 14:46:13 -080072 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070073 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
74 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080075
Sunny Goyal27835952017-01-13 12:15:53 -080076 protected DeviceProfile mDeviceProfile;
Sunny Goyala535ae42017-02-27 10:07:13 -080077 protected UserEventDispatcher mUserEventDispatcher;
Hyunyoung Songfc007472018-10-25 14:09:50 -070078 protected StatsLogManager mStatsLogManager;
Sunny Goyal8392c822017-06-20 10:03:56 -070079 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080080
Sunny Goyal3483c522018-04-12 11:23:33 -070081 private static final int ACTIVITY_STATE_STARTED = 1 << 0;
82 private static final int ACTIVITY_STATE_RESUMED = 1 << 1;
83 /**
84 * State flag indicating if the user is active or the actitvity when to background as a result
85 * of user action.
86 * @see #isUserActive()
87 */
88 private static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 2;
89
90 @Retention(SOURCE)
91 @IntDef(
92 flag = true,
93 value = {ACTIVITY_STATE_STARTED, ACTIVITY_STATE_RESUMED, ACTIVITY_STATE_USER_ACTIVE})
94 public @interface ActivityFlags{}
95
96 @ActivityFlags
97 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070098
Winson Chung9800e732018-04-04 13:33:23 -070099 // When the recents animation is running, the visibility of the Launcher is managed by the
100 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700101 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800102
Sunny Goyal27835952017-01-13 12:15:53 -0800103 public DeviceProfile getDeviceProfile() {
104 return mDeviceProfile;
105 }
106
107 public AccessibilityDelegate getAccessibilityDelegate() {
108 return null;
109 }
110
Hyunyoung Songfc007472018-10-25 14:09:50 -0700111 public int getCurrentState() { return StatsLogUtils.LAUNCHER_STATE_BACKGROUND; }
112
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700113 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {}
114
Hyunyoung Songfc007472018-10-25 14:09:50 -0700115 public final StatsLogManager getStatsLogManager() {
116 if (mStatsLogManager == null) {
117 mStatsLogManager = StatsLogManager.newInstance(this, this);
118 }
119 return mStatsLogManager;
120 }
121
Sunny Goyala535ae42017-02-27 10:07:13 -0800122 public final UserEventDispatcher getUserEventDispatcher() {
123 if (mUserEventDispatcher == null) {
Hyunyoung Song956ec4b2018-07-02 13:17:32 -0700124 mUserEventDispatcher = UserEventDispatcher.newInstance(this, this);
Sunny Goyala535ae42017-02-27 10:07:13 -0800125 }
126 return mUserEventDispatcher;
127 }
128
Jon Mirandafe964322017-03-22 10:25:17 -0700129 public boolean isInMultiWindowModeCompat() {
130 return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode();
131 }
132
Sunny Goyal8392c822017-06-20 10:03:56 -0700133 public SystemUiController getSystemUiController() {
134 if (mSystemUiController == null) {
135 mSystemUiController = new SystemUiController(getWindow());
136 }
137 return mSystemUiController;
138 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700139
140 @Override
141 public void onActivityResult(int requestCode, int resultCode, Intent data) {
142 super.onActivityResult(requestCode, resultCode, data);
143 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800144
145 @Override
146 protected void onStart() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700147 mActivityFlags |= ACTIVITY_STATE_STARTED;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800148 super.onStart();
149 }
150
151 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700152 protected void onResume() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700153 mActivityFlags |= ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700154 super.onResume();
155 }
156
157 @Override
158 protected void onUserLeaveHint() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700159 mActivityFlags &= ~ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700160 super.onUserLeaveHint();
161 }
162
163 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700164 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
165 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
166 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
167 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
168 }
169 }
170
171 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800172 protected void onStop() {
Tracy Zhoub67e3532018-04-17 23:45:47 -0700173 mActivityFlags &= ~ACTIVITY_STATE_STARTED & ~ACTIVITY_STATE_USER_ACTIVE;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700174 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800175 super.onStop();
176 }
177
Sunny Goyal3483c522018-04-12 11:23:33 -0700178 @Override
179 protected void onPause() {
180 mActivityFlags &= ~ACTIVITY_STATE_RESUMED;
181 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000182
183 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
184 // here instead of at the end of the animation because the start of the new activity does
185 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
186 // back to the new app (causing a flash)
187 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700188 }
189
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800190 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700191 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
192 }
193
194 /**
195 * isResumed in already defined as a hidden final method in Activity.java
196 */
197 public boolean hasBeenResumed() {
198 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800199 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800200
Tracy Zhoua706f002018-03-28 13:55:19 -0700201 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700202 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700203 }
204
Sunny Goyalfde55052018-02-01 14:46:13 -0800205 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
206 mDPChangeListeners.add(listener);
207 }
208
Winson Chung8a968fa2018-03-15 17:59:04 -0700209 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
210 mDPChangeListeners.remove(listener);
211 }
212
Sunny Goyalfde55052018-02-01 14:46:13 -0800213 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700214 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800215 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
216 }
217 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700218
Winson Chung1a77c3d2018-04-11 12:47:47 -0700219 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
220 mMultiWindowModeChangedListeners.add(listener);
221 }
222
223 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
224 mMultiWindowModeChangedListeners.remove(listener);
225 }
226
Sunny Goyalf633ef52018-03-13 09:57:05 -0700227 /**
Winson Chung9800e732018-04-04 13:33:23 -0700228 * Used to set the override visibility state, used only to handle the transition home with the
229 * recents animation.
Sunny Goyal1c63c722018-06-05 16:00:34 -0700230 * @see LauncherAppTransitionManagerImpl#getWallpaperOpenRunner()
Winson Chung9800e732018-04-04 13:33:23 -0700231 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700232 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
233 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700234 }
235
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700236 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
237 mForceInvisible &= ~flag;
238 }
239
Winson Chung9800e732018-04-04 13:33:23 -0700240 /**
241 * @return Wether this activity should be considered invisible regardless of actual visibility.
242 */
243 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700244 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
245 }
246
247 public boolean hasSomeInvisibleFlag(int mask) {
248 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700249 }
250
Winson Chung1a77c3d2018-04-11 12:47:47 -0700251 public interface MultiWindowModeChangedListener {
252 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
253 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700254
Sunny Goyal55eb5562018-05-15 14:53:30 -0700255 @Override
256 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
257 if (!UiFactory.dumpActivity(this, writer)) {
258 super.dump(prefix, fd, writer, args);
259 }
260 }
261
Sunny Goyale43d00d2018-05-14 14:23:18 -0700262 protected void dumpMisc(PrintWriter writer) {
263 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
264 writer.println(" orientation=" + getResources().getConfiguration().orientation);
265 writer.println(" mSystemUiController: " + mSystemUiController);
266 writer.println(" mActivityFlags: " + mActivityFlags);
267 writer.println(" mForceInvisible: " + mForceInvisible);
268 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700269
270 public static <T extends BaseActivity> T fromContext(Context context) {
271 if (context instanceof BaseActivity) {
272 return (T) context;
273 } else if (context instanceof ContextThemeWrapper) {
274 return fromContext(((ContextWrapper) context).getBaseContext());
275 } else {
276 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
277 }
278 }
Sunny Goyal27835952017-01-13 12:15:53 -0800279}