blob: 421966755770e88ebc0102716aee96b6ae6da401 [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;
Sunny Goyal55eb5562018-05-15 14:53:30 -070033import com.android.launcher3.uioverrides.UiFactory;
Sunny Goyal8392c822017-06-20 10:03:56 -070034import com.android.launcher3.util.SystemUiController;
Sunny Goyala535ae42017-02-27 10:07:13 -080035
Sunny Goyal55eb5562018-05-15 14:53:30 -070036import java.io.FileDescriptor;
Sunny Goyale43d00d2018-05-14 14:23:18 -070037import java.io.PrintWriter;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070038import java.lang.annotation.Retention;
Sunny Goyalfde55052018-02-01 14:46:13 -080039import java.util.ArrayList;
40
Sunny Goyal27835952017-01-13 12:15:53 -080041public abstract class BaseActivity extends Activity {
42
Sunny Goyal7eff40f2018-04-11 15:30:46 -070043 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
44 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
45 public static final int INVISIBLE_ALL =
46 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS;
47
48 @Retention(SOURCE)
49 @IntDef(
50 flag = true,
51 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS})
52 public @interface InvisibilityFlags{}
53
Sunny Goyalfde55052018-02-01 14:46:13 -080054 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070055 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
56 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080057
Sunny Goyal27835952017-01-13 12:15:53 -080058 protected DeviceProfile mDeviceProfile;
Sunny Goyala535ae42017-02-27 10:07:13 -080059 protected UserEventDispatcher mUserEventDispatcher;
Sunny Goyal8392c822017-06-20 10:03:56 -070060 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080061
Sunny Goyal3483c522018-04-12 11:23:33 -070062 private static final int ACTIVITY_STATE_STARTED = 1 << 0;
63 private static final int ACTIVITY_STATE_RESUMED = 1 << 1;
64 /**
65 * State flag indicating if the user is active or the actitvity when to background as a result
66 * of user action.
67 * @see #isUserActive()
68 */
69 private static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 2;
70
71 @Retention(SOURCE)
72 @IntDef(
73 flag = true,
74 value = {ACTIVITY_STATE_STARTED, ACTIVITY_STATE_RESUMED, ACTIVITY_STATE_USER_ACTIVE})
75 public @interface ActivityFlags{}
76
77 @ActivityFlags
78 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070079
Winson Chung9800e732018-04-04 13:33:23 -070080 // When the recents animation is running, the visibility of the Launcher is managed by the
81 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -070082 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -080083
Sunny Goyal27835952017-01-13 12:15:53 -080084 public DeviceProfile getDeviceProfile() {
85 return mDeviceProfile;
86 }
87
88 public AccessibilityDelegate getAccessibilityDelegate() {
89 return null;
90 }
91
Sunny Goyala535ae42017-02-27 10:07:13 -080092 public final UserEventDispatcher getUserEventDispatcher() {
93 if (mUserEventDispatcher == null) {
Sunny Goyald70e75a2018-02-22 10:07:32 -080094 mUserEventDispatcher = UserEventDispatcher.newInstance(this, mDeviceProfile);
Sunny Goyala535ae42017-02-27 10:07:13 -080095 }
96 return mUserEventDispatcher;
97 }
98
Jon Mirandafe964322017-03-22 10:25:17 -070099 public boolean isInMultiWindowModeCompat() {
100 return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode();
101 }
102
Sunny Goyal27835952017-01-13 12:15:53 -0800103 public static BaseActivity fromContext(Context context) {
104 if (context instanceof BaseActivity) {
105 return (BaseActivity) context;
106 }
107 return ((BaseActivity) ((ContextWrapper) context).getBaseContext());
108 }
Sunny Goyal8392c822017-06-20 10:03:56 -0700109
110 public SystemUiController getSystemUiController() {
111 if (mSystemUiController == null) {
112 mSystemUiController = new SystemUiController(getWindow());
113 }
114 return mSystemUiController;
115 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700116
117 @Override
118 public void onActivityResult(int requestCode, int resultCode, Intent data) {
119 super.onActivityResult(requestCode, resultCode, data);
120 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800121
122 @Override
123 protected void onStart() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700124 mActivityFlags |= ACTIVITY_STATE_STARTED;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800125 super.onStart();
126 }
127
128 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700129 protected void onResume() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700130 mActivityFlags |= ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700131 super.onResume();
132 }
133
134 @Override
135 protected void onUserLeaveHint() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700136 mActivityFlags &= ~ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700137 super.onUserLeaveHint();
138 }
139
140 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700141 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
142 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
143 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
144 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
145 }
146 }
147
148 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800149 protected void onStop() {
Tracy Zhoub67e3532018-04-17 23:45:47 -0700150 mActivityFlags &= ~ACTIVITY_STATE_STARTED & ~ACTIVITY_STATE_USER_ACTIVE;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700151 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800152 super.onStop();
153 }
154
Sunny Goyal3483c522018-04-12 11:23:33 -0700155 @Override
156 protected void onPause() {
157 mActivityFlags &= ~ACTIVITY_STATE_RESUMED;
158 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000159
160 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
161 // here instead of at the end of the animation because the start of the new activity does
162 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
163 // back to the new app (causing a flash)
164 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700165 }
166
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800167 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700168 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
169 }
170
171 /**
172 * isResumed in already defined as a hidden final method in Activity.java
173 */
174 public boolean hasBeenResumed() {
175 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800176 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800177
Tracy Zhoua706f002018-03-28 13:55:19 -0700178 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700179 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700180 }
181
Sunny Goyalfde55052018-02-01 14:46:13 -0800182 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
183 mDPChangeListeners.add(listener);
184 }
185
Winson Chung8a968fa2018-03-15 17:59:04 -0700186 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
187 mDPChangeListeners.remove(listener);
188 }
189
Sunny Goyalfde55052018-02-01 14:46:13 -0800190 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700191 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800192 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
193 }
194 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700195
Winson Chung1a77c3d2018-04-11 12:47:47 -0700196 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
197 mMultiWindowModeChangedListeners.add(listener);
198 }
199
200 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
201 mMultiWindowModeChangedListeners.remove(listener);
202 }
203
Sunny Goyalf633ef52018-03-13 09:57:05 -0700204 /**
Winson Chung9800e732018-04-04 13:33:23 -0700205 * Used to set the override visibility state, used only to handle the transition home with the
206 * recents animation.
207 * @see LauncherAppTransitionManagerImpl.getWallpaperOpenRunner()
208 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700209 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
210 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700211 }
212
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700213 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
214 mForceInvisible &= ~flag;
215 }
216
217
Winson Chung9800e732018-04-04 13:33:23 -0700218 /**
219 * @return Wether this activity should be considered invisible regardless of actual visibility.
220 */
221 public boolean isForceInvisible() {
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700222 return mForceInvisible != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700223 }
224
Winson Chung1a77c3d2018-04-11 12:47:47 -0700225 public interface MultiWindowModeChangedListener {
226 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
227 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700228
Sunny Goyal55eb5562018-05-15 14:53:30 -0700229 @Override
230 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
231 if (!UiFactory.dumpActivity(this, writer)) {
232 super.dump(prefix, fd, writer, args);
233 }
234 }
235
Sunny Goyale43d00d2018-05-14 14:23:18 -0700236 protected void dumpMisc(PrintWriter writer) {
237 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
238 writer.println(" orientation=" + getResources().getConfiguration().orientation);
239 writer.println(" mSystemUiController: " + mSystemUiController);
240 writer.println(" mActivityFlags: " + mActivityFlags);
241 writer.println(" mForceInvisible: " + mForceInvisible);
242 }
Sunny Goyal27835952017-01-13 12:15:53 -0800243}