blob: eca280bfd7ff32db477fcd2b0c3b5137e27f20c1 [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 Goyal7eff40f2018-04-11 15:30:46 -070028import android.support.annotation.IntDef;
Sunny Goyal87b5eb62018-07-03 15:53:39 -070029import android.view.ContextThemeWrapper;
Sunny Goyal27835952017-01-13 12:15:53 -080030import android.view.View.AccessibilityDelegate;
31
Sunny Goyalfde55052018-02-01 14:46:13 -080032import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener;
Sunny Goyala535ae42017-02-27 10:07:13 -080033import com.android.launcher3.logging.UserEventDispatcher;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070034import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate;
Sunny Goyal55eb5562018-05-15 14:53:30 -070035import com.android.launcher3.uioverrides.UiFactory;
Hyunyoung Song46d07f72018-05-22 15:41:25 -070036import com.android.launcher3.userevent.nano.LauncherLogProto;
Sunny Goyal8392c822017-06-20 10:03:56 -070037import com.android.launcher3.util.SystemUiController;
Sunny Goyala535ae42017-02-27 10:07:13 -080038
Sunny Goyal55eb5562018-05-15 14:53:30 -070039import java.io.FileDescriptor;
Sunny Goyale43d00d2018-05-14 14:23:18 -070040import java.io.PrintWriter;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070041import java.lang.annotation.Retention;
Sunny Goyalfde55052018-02-01 14:46:13 -080042import java.util.ArrayList;
43
Hyunyoung Song46d07f72018-05-22 15:41:25 -070044public abstract class BaseActivity extends Activity implements UserEventDelegate{
Sunny Goyal27835952017-01-13 12:15:53 -080045
Sunny Goyal7eff40f2018-04-11 15:30:46 -070046 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
47 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
Sunny Goyal1c63c722018-06-05 16:00:34 -070048 public static final int INVISIBLE_BY_PENDING_FLAGS = 1 << 2;
49
50 // This is not treated as invisibility flag, but adds as a hint for an incomplete transition.
51 // When the wallpaper animation runs, it replaces this flag with a proper invisibility
52 // flag, INVISIBLE_BY_PENDING_FLAGS only for the duration of that animation.
53 public static final int PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION = 1 << 3;
54
55 private static final int INVISIBLE_FLAGS =
56 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS | INVISIBLE_BY_PENDING_FLAGS;
57 public static final int STATE_HANDLER_INVISIBILITY_FLAGS =
58 INVISIBLE_BY_STATE_HANDLER | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070059 public static final int INVISIBLE_ALL =
Sunny Goyal1c63c722018-06-05 16:00:34 -070060 INVISIBLE_FLAGS | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070061
62 @Retention(SOURCE)
63 @IntDef(
64 flag = true,
Sunny Goyal1c63c722018-06-05 16:00:34 -070065 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS,
66 INVISIBLE_BY_PENDING_FLAGS, PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION})
Sunny Goyal7eff40f2018-04-11 15:30:46 -070067 public @interface InvisibilityFlags{}
68
Sunny Goyalfde55052018-02-01 14:46:13 -080069 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070070 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
71 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080072
Sunny Goyal27835952017-01-13 12:15:53 -080073 protected DeviceProfile mDeviceProfile;
Sunny Goyala535ae42017-02-27 10:07:13 -080074 protected UserEventDispatcher mUserEventDispatcher;
Sunny Goyal8392c822017-06-20 10:03:56 -070075 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080076
Sunny Goyal3483c522018-04-12 11:23:33 -070077 private static final int ACTIVITY_STATE_STARTED = 1 << 0;
78 private static final int ACTIVITY_STATE_RESUMED = 1 << 1;
79 /**
80 * State flag indicating if the user is active or the actitvity when to background as a result
81 * of user action.
82 * @see #isUserActive()
83 */
84 private static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 2;
85
86 @Retention(SOURCE)
87 @IntDef(
88 flag = true,
89 value = {ACTIVITY_STATE_STARTED, ACTIVITY_STATE_RESUMED, ACTIVITY_STATE_USER_ACTIVE})
90 public @interface ActivityFlags{}
91
92 @ActivityFlags
93 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070094
Winson Chung9800e732018-04-04 13:33:23 -070095 // When the recents animation is running, the visibility of the Launcher is managed by the
96 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -070097 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -080098
Sunny Goyal27835952017-01-13 12:15:53 -080099 public DeviceProfile getDeviceProfile() {
100 return mDeviceProfile;
101 }
102
103 public AccessibilityDelegate getAccessibilityDelegate() {
104 return null;
105 }
106
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700107 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {}
108
Sunny Goyala535ae42017-02-27 10:07:13 -0800109 public final UserEventDispatcher getUserEventDispatcher() {
110 if (mUserEventDispatcher == null) {
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700111 mUserEventDispatcher = UserEventDispatcher.newInstance(this, mDeviceProfile, this);
Sunny Goyala535ae42017-02-27 10:07:13 -0800112 }
113 return mUserEventDispatcher;
114 }
115
Jon Mirandafe964322017-03-22 10:25:17 -0700116 public boolean isInMultiWindowModeCompat() {
117 return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode();
118 }
119
Sunny Goyal8392c822017-06-20 10:03:56 -0700120 public SystemUiController getSystemUiController() {
121 if (mSystemUiController == null) {
122 mSystemUiController = new SystemUiController(getWindow());
123 }
124 return mSystemUiController;
125 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700126
127 @Override
128 public void onActivityResult(int requestCode, int resultCode, Intent data) {
129 super.onActivityResult(requestCode, resultCode, data);
130 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800131
132 @Override
133 protected void onStart() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700134 mActivityFlags |= ACTIVITY_STATE_STARTED;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800135 super.onStart();
136 }
137
138 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700139 protected void onResume() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700140 mActivityFlags |= ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700141 super.onResume();
142 }
143
144 @Override
145 protected void onUserLeaveHint() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700146 mActivityFlags &= ~ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700147 super.onUserLeaveHint();
148 }
149
150 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700151 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
152 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
153 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
154 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
155 }
156 }
157
158 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800159 protected void onStop() {
Tracy Zhoub67e3532018-04-17 23:45:47 -0700160 mActivityFlags &= ~ACTIVITY_STATE_STARTED & ~ACTIVITY_STATE_USER_ACTIVE;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700161 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800162 super.onStop();
163 }
164
Sunny Goyal3483c522018-04-12 11:23:33 -0700165 @Override
166 protected void onPause() {
167 mActivityFlags &= ~ACTIVITY_STATE_RESUMED;
168 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000169
170 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
171 // here instead of at the end of the animation because the start of the new activity does
172 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
173 // back to the new app (causing a flash)
174 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700175 }
176
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800177 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700178 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
179 }
180
181 /**
182 * isResumed in already defined as a hidden final method in Activity.java
183 */
184 public boolean hasBeenResumed() {
185 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800186 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800187
Tracy Zhoua706f002018-03-28 13:55:19 -0700188 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700189 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700190 }
191
Sunny Goyalfde55052018-02-01 14:46:13 -0800192 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
193 mDPChangeListeners.add(listener);
194 }
195
Winson Chung8a968fa2018-03-15 17:59:04 -0700196 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
197 mDPChangeListeners.remove(listener);
198 }
199
Sunny Goyalfde55052018-02-01 14:46:13 -0800200 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700201 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800202 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
203 }
204 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700205
Winson Chung1a77c3d2018-04-11 12:47:47 -0700206 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
207 mMultiWindowModeChangedListeners.add(listener);
208 }
209
210 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
211 mMultiWindowModeChangedListeners.remove(listener);
212 }
213
Sunny Goyalf633ef52018-03-13 09:57:05 -0700214 /**
Winson Chung9800e732018-04-04 13:33:23 -0700215 * Used to set the override visibility state, used only to handle the transition home with the
216 * recents animation.
Sunny Goyal1c63c722018-06-05 16:00:34 -0700217 * @see LauncherAppTransitionManagerImpl#getWallpaperOpenRunner()
Winson Chung9800e732018-04-04 13:33:23 -0700218 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700219 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
220 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700221 }
222
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700223 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
224 mForceInvisible &= ~flag;
225 }
226
Winson Chung9800e732018-04-04 13:33:23 -0700227 /**
228 * @return Wether this activity should be considered invisible regardless of actual visibility.
229 */
230 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700231 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
232 }
233
234 public boolean hasSomeInvisibleFlag(int mask) {
235 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700236 }
237
Winson Chung1a77c3d2018-04-11 12:47:47 -0700238 public interface MultiWindowModeChangedListener {
239 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
240 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700241
Sunny Goyal55eb5562018-05-15 14:53:30 -0700242 @Override
243 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
244 if (!UiFactory.dumpActivity(this, writer)) {
245 super.dump(prefix, fd, writer, args);
246 }
247 }
248
Sunny Goyale43d00d2018-05-14 14:23:18 -0700249 protected void dumpMisc(PrintWriter writer) {
250 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
251 writer.println(" orientation=" + getResources().getConfiguration().orientation);
252 writer.println(" mSystemUiController: " + mSystemUiController);
253 writer.println(" mActivityFlags: " + mActivityFlags);
254 writer.println(" mForceInvisible: " + mForceInvisible);
255 }
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700256
257 public static <T extends BaseActivity> T fromContext(Context context) {
258 if (context instanceof BaseActivity) {
259 return (T) context;
260 } else if (context instanceof ContextThemeWrapper) {
261 return fromContext(((ContextWrapper) context).getBaseContext());
262 } else {
263 throw new IllegalArgumentException("Cannot find BaseActivity in parent tree");
264 }
265 }
Sunny Goyal27835952017-01-13 12:15:53 -0800266}