blob: fd69377b61764a80e96e4a8808cad4f665e0182a [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 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
Hyunyoung Song46d07f72018-05-22 15:41:25 -070043public abstract class BaseActivity extends Activity implements UserEventDelegate{
Sunny Goyal27835952017-01-13 12:15:53 -080044
Sunny Goyal7eff40f2018-04-11 15:30:46 -070045 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
46 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
Sunny Goyal1c63c722018-06-05 16:00:34 -070047 public static final int INVISIBLE_BY_PENDING_FLAGS = 1 << 2;
48
49 // This is not treated as invisibility flag, but adds as a hint for an incomplete transition.
50 // When the wallpaper animation runs, it replaces this flag with a proper invisibility
51 // flag, INVISIBLE_BY_PENDING_FLAGS only for the duration of that animation.
52 public static final int PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION = 1 << 3;
53
54 private static final int INVISIBLE_FLAGS =
55 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS | INVISIBLE_BY_PENDING_FLAGS;
56 public static final int STATE_HANDLER_INVISIBILITY_FLAGS =
57 INVISIBLE_BY_STATE_HANDLER | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070058 public static final int INVISIBLE_ALL =
Sunny Goyal1c63c722018-06-05 16:00:34 -070059 INVISIBLE_FLAGS | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070060
61 @Retention(SOURCE)
62 @IntDef(
63 flag = true,
Sunny Goyal1c63c722018-06-05 16:00:34 -070064 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS,
65 INVISIBLE_BY_PENDING_FLAGS, PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION})
Sunny Goyal7eff40f2018-04-11 15:30:46 -070066 public @interface InvisibilityFlags{}
67
Sunny Goyalfde55052018-02-01 14:46:13 -080068 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070069 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
70 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080071
Sunny Goyal27835952017-01-13 12:15:53 -080072 protected DeviceProfile mDeviceProfile;
Sunny Goyala535ae42017-02-27 10:07:13 -080073 protected UserEventDispatcher mUserEventDispatcher;
Sunny Goyal8392c822017-06-20 10:03:56 -070074 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080075
Sunny Goyal3483c522018-04-12 11:23:33 -070076 private static final int ACTIVITY_STATE_STARTED = 1 << 0;
77 private static final int ACTIVITY_STATE_RESUMED = 1 << 1;
78 /**
79 * State flag indicating if the user is active or the actitvity when to background as a result
80 * of user action.
81 * @see #isUserActive()
82 */
83 private static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 2;
84
85 @Retention(SOURCE)
86 @IntDef(
87 flag = true,
88 value = {ACTIVITY_STATE_STARTED, ACTIVITY_STATE_RESUMED, ACTIVITY_STATE_USER_ACTIVE})
89 public @interface ActivityFlags{}
90
91 @ActivityFlags
92 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070093
Winson Chung9800e732018-04-04 13:33:23 -070094 // When the recents animation is running, the visibility of the Launcher is managed by the
95 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -070096 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -080097
Sunny Goyal27835952017-01-13 12:15:53 -080098 public DeviceProfile getDeviceProfile() {
99 return mDeviceProfile;
100 }
101
102 public AccessibilityDelegate getAccessibilityDelegate() {
103 return null;
104 }
105
Hyunyoung Song46d07f72018-05-22 15:41:25 -0700106 public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {}
107
Sunny Goyala535ae42017-02-27 10:07:13 -0800108 public final UserEventDispatcher getUserEventDispatcher() {
109 if (mUserEventDispatcher == null) {
Hyunyoung Song956ec4b2018-07-02 13:17:32 -0700110 mUserEventDispatcher = UserEventDispatcher.newInstance(this, this);
Sunny Goyala535ae42017-02-27 10:07:13 -0800111 }
112 return mUserEventDispatcher;
113 }
114
Jon Mirandafe964322017-03-22 10:25:17 -0700115 public boolean isInMultiWindowModeCompat() {
116 return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode();
117 }
118
Sunny Goyal27835952017-01-13 12:15:53 -0800119 public static BaseActivity fromContext(Context context) {
120 if (context instanceof BaseActivity) {
121 return (BaseActivity) context;
122 }
123 return ((BaseActivity) ((ContextWrapper) context).getBaseContext());
124 }
Sunny Goyal8392c822017-06-20 10:03:56 -0700125
126 public SystemUiController getSystemUiController() {
127 if (mSystemUiController == null) {
128 mSystemUiController = new SystemUiController(getWindow());
129 }
130 return mSystemUiController;
131 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700132
133 @Override
134 public void onActivityResult(int requestCode, int resultCode, Intent data) {
135 super.onActivityResult(requestCode, resultCode, data);
136 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800137
138 @Override
139 protected void onStart() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700140 mActivityFlags |= ACTIVITY_STATE_STARTED;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800141 super.onStart();
142 }
143
144 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700145 protected void onResume() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700146 mActivityFlags |= ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700147 super.onResume();
148 }
149
150 @Override
151 protected void onUserLeaveHint() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700152 mActivityFlags &= ~ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700153 super.onUserLeaveHint();
154 }
155
156 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700157 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
158 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
159 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
160 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
161 }
162 }
163
164 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800165 protected void onStop() {
Tracy Zhoub67e3532018-04-17 23:45:47 -0700166 mActivityFlags &= ~ACTIVITY_STATE_STARTED & ~ACTIVITY_STATE_USER_ACTIVE;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700167 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800168 super.onStop();
169 }
170
Sunny Goyal3483c522018-04-12 11:23:33 -0700171 @Override
172 protected void onPause() {
173 mActivityFlags &= ~ACTIVITY_STATE_RESUMED;
174 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000175
176 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
177 // here instead of at the end of the animation because the start of the new activity does
178 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
179 // back to the new app (causing a flash)
180 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700181 }
182
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800183 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700184 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
185 }
186
187 /**
188 * isResumed in already defined as a hidden final method in Activity.java
189 */
190 public boolean hasBeenResumed() {
191 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800192 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800193
Tracy Zhoua706f002018-03-28 13:55:19 -0700194 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700195 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700196 }
197
Sunny Goyalfde55052018-02-01 14:46:13 -0800198 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
199 mDPChangeListeners.add(listener);
200 }
201
Winson Chung8a968fa2018-03-15 17:59:04 -0700202 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
203 mDPChangeListeners.remove(listener);
204 }
205
Sunny Goyalfde55052018-02-01 14:46:13 -0800206 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700207 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800208 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
209 }
210 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700211
Winson Chung1a77c3d2018-04-11 12:47:47 -0700212 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
213 mMultiWindowModeChangedListeners.add(listener);
214 }
215
216 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
217 mMultiWindowModeChangedListeners.remove(listener);
218 }
219
Sunny Goyalf633ef52018-03-13 09:57:05 -0700220 /**
Winson Chung9800e732018-04-04 13:33:23 -0700221 * Used to set the override visibility state, used only to handle the transition home with the
222 * recents animation.
Sunny Goyal1c63c722018-06-05 16:00:34 -0700223 * @see LauncherAppTransitionManagerImpl#getWallpaperOpenRunner()
Winson Chung9800e732018-04-04 13:33:23 -0700224 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700225 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
226 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700227 }
228
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700229 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
230 mForceInvisible &= ~flag;
231 }
232
Winson Chung9800e732018-04-04 13:33:23 -0700233 /**
234 * @return Wether this activity should be considered invisible regardless of actual visibility.
235 */
236 public boolean isForceInvisible() {
Sunny Goyal1c63c722018-06-05 16:00:34 -0700237 return hasSomeInvisibleFlag(INVISIBLE_FLAGS);
238 }
239
240 public boolean hasSomeInvisibleFlag(int mask) {
241 return (mForceInvisible & mask) != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700242 }
243
Winson Chung1a77c3d2018-04-11 12:47:47 -0700244 public interface MultiWindowModeChangedListener {
245 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
246 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700247
Sunny Goyal55eb5562018-05-15 14:53:30 -0700248 @Override
249 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
250 if (!UiFactory.dumpActivity(this, writer)) {
251 super.dump(prefix, fd, writer, args);
252 }
253 }
254
Sunny Goyale43d00d2018-05-14 14:23:18 -0700255 protected void dumpMisc(PrintWriter writer) {
256 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
257 writer.println(" orientation=" + getResources().getConfiguration().orientation);
258 writer.println(" mSystemUiController: " + mSystemUiController);
259 writer.println(" mActivityFlags: " + mActivityFlags);
260 writer.println(" mForceInvisible: " + mForceInvisible);
261 }
Sunny Goyal27835952017-01-13 12:15:53 -0800262}