blob: 3e4589d1f57ed8fa26f8c6932488f654ce777b81 [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 Goyal8392c822017-06-20 10:03:56 -070033import com.android.launcher3.util.SystemUiController;
Sunny Goyala535ae42017-02-27 10:07:13 -080034
Sunny Goyale43d00d2018-05-14 14:23:18 -070035import java.io.PrintWriter;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070036import java.lang.annotation.Retention;
Sunny Goyalfde55052018-02-01 14:46:13 -080037import java.util.ArrayList;
38
Sunny Goyal27835952017-01-13 12:15:53 -080039public abstract class BaseActivity extends Activity {
40
Sunny Goyal7eff40f2018-04-11 15:30:46 -070041 public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0;
42 public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1;
43 public static final int INVISIBLE_ALL =
44 INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS;
45
46 @Retention(SOURCE)
47 @IntDef(
48 flag = true,
49 value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS})
50 public @interface InvisibilityFlags{}
51
Sunny Goyalfde55052018-02-01 14:46:13 -080052 private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>();
Winson Chung1a77c3d2018-04-11 12:47:47 -070053 private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners =
54 new ArrayList<>();
Sunny Goyalfde55052018-02-01 14:46:13 -080055
Sunny Goyal27835952017-01-13 12:15:53 -080056 protected DeviceProfile mDeviceProfile;
Sunny Goyala535ae42017-02-27 10:07:13 -080057 protected UserEventDispatcher mUserEventDispatcher;
Sunny Goyal8392c822017-06-20 10:03:56 -070058 protected SystemUiController mSystemUiController;
Sunny Goyal27835952017-01-13 12:15:53 -080059
Sunny Goyal3483c522018-04-12 11:23:33 -070060 private static final int ACTIVITY_STATE_STARTED = 1 << 0;
61 private static final int ACTIVITY_STATE_RESUMED = 1 << 1;
62 /**
63 * State flag indicating if the user is active or the actitvity when to background as a result
64 * of user action.
65 * @see #isUserActive()
66 */
67 private static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 2;
68
69 @Retention(SOURCE)
70 @IntDef(
71 flag = true,
72 value = {ACTIVITY_STATE_STARTED, ACTIVITY_STATE_RESUMED, ACTIVITY_STATE_USER_ACTIVE})
73 public @interface ActivityFlags{}
74
75 @ActivityFlags
76 private int mActivityFlags;
Sunny Goyal7eff40f2018-04-11 15:30:46 -070077
Winson Chung9800e732018-04-04 13:33:23 -070078 // When the recents animation is running, the visibility of the Launcher is managed by the
79 // animation
Sunny Goyal7eff40f2018-04-11 15:30:46 -070080 @InvisibilityFlags private int mForceInvisible;
Sunny Goyalcc96aa12018-01-11 09:56:07 -080081
Sunny Goyal27835952017-01-13 12:15:53 -080082 public DeviceProfile getDeviceProfile() {
83 return mDeviceProfile;
84 }
85
86 public AccessibilityDelegate getAccessibilityDelegate() {
87 return null;
88 }
89
Sunny Goyala535ae42017-02-27 10:07:13 -080090 public final UserEventDispatcher getUserEventDispatcher() {
91 if (mUserEventDispatcher == null) {
Sunny Goyald70e75a2018-02-22 10:07:32 -080092 mUserEventDispatcher = UserEventDispatcher.newInstance(this, mDeviceProfile);
Sunny Goyala535ae42017-02-27 10:07:13 -080093 }
94 return mUserEventDispatcher;
95 }
96
Jon Mirandafe964322017-03-22 10:25:17 -070097 public boolean isInMultiWindowModeCompat() {
98 return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode();
99 }
100
Sunny Goyal27835952017-01-13 12:15:53 -0800101 public static BaseActivity fromContext(Context context) {
102 if (context instanceof BaseActivity) {
103 return (BaseActivity) context;
104 }
105 return ((BaseActivity) ((ContextWrapper) context).getBaseContext());
106 }
Sunny Goyal8392c822017-06-20 10:03:56 -0700107
108 public SystemUiController getSystemUiController() {
109 if (mSystemUiController == null) {
110 mSystemUiController = new SystemUiController(getWindow());
111 }
112 return mSystemUiController;
113 }
Sunny Goyal64a75aa2017-07-03 13:50:52 -0700114
115 @Override
116 public void onActivityResult(int requestCode, int resultCode, Intent data) {
117 super.onActivityResult(requestCode, resultCode, data);
118 }
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800119
120 @Override
121 protected void onStart() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700122 mActivityFlags |= ACTIVITY_STATE_STARTED;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800123 super.onStart();
124 }
125
126 @Override
Tracy Zhoua706f002018-03-28 13:55:19 -0700127 protected void onResume() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700128 mActivityFlags |= ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700129 super.onResume();
130 }
131
132 @Override
133 protected void onUserLeaveHint() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700134 mActivityFlags &= ~ACTIVITY_STATE_USER_ACTIVE;
Tracy Zhoua706f002018-03-28 13:55:19 -0700135 super.onUserLeaveHint();
136 }
137
138 @Override
Winson Chung1a77c3d2018-04-11 12:47:47 -0700139 public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) {
140 super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig);
141 for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) {
142 mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode);
143 }
144 }
145
146 @Override
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800147 protected void onStop() {
Tracy Zhoub67e3532018-04-17 23:45:47 -0700148 mActivityFlags &= ~ACTIVITY_STATE_STARTED & ~ACTIVITY_STATE_USER_ACTIVE;
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700149 mForceInvisible = 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800150 super.onStop();
151 }
152
Sunny Goyal3483c522018-04-12 11:23:33 -0700153 @Override
154 protected void onPause() {
155 mActivityFlags &= ~ACTIVITY_STATE_RESUMED;
156 super.onPause();
Winson Chunga0f09f92018-05-11 21:55:21 +0000157
158 // Reset the overridden sysui flags used for the task-swipe launch animation, we do this
159 // here instead of at the end of the animation because the start of the new activity does
160 // not happen immediately, which would cause us to reset to launcher's sysui flags and then
161 // back to the new app (causing a flash)
162 getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0);
Sunny Goyal3483c522018-04-12 11:23:33 -0700163 }
164
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800165 public boolean isStarted() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700166 return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0;
167 }
168
169 /**
170 * isResumed in already defined as a hidden final method in Activity.java
171 */
172 public boolean hasBeenResumed() {
173 return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0;
Sunny Goyalcc96aa12018-01-11 09:56:07 -0800174 }
Sunny Goyalfde55052018-02-01 14:46:13 -0800175
Tracy Zhoua706f002018-03-28 13:55:19 -0700176 public boolean isUserActive() {
Sunny Goyal3483c522018-04-12 11:23:33 -0700177 return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0;
Tracy Zhoua706f002018-03-28 13:55:19 -0700178 }
179
Sunny Goyalfde55052018-02-01 14:46:13 -0800180 public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
181 mDPChangeListeners.add(listener);
182 }
183
Winson Chung8a968fa2018-03-15 17:59:04 -0700184 public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) {
185 mDPChangeListeners.remove(listener);
186 }
187
Sunny Goyalfde55052018-02-01 14:46:13 -0800188 protected void dispatchDeviceProfileChanged() {
Winson Chung8a968fa2018-03-15 17:59:04 -0700189 for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) {
Sunny Goyalfde55052018-02-01 14:46:13 -0800190 mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile);
191 }
192 }
Sunny Goyalf633ef52018-03-13 09:57:05 -0700193
Winson Chung1a77c3d2018-04-11 12:47:47 -0700194 public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
195 mMultiWindowModeChangedListeners.add(listener);
196 }
197
198 public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) {
199 mMultiWindowModeChangedListeners.remove(listener);
200 }
201
Sunny Goyalf633ef52018-03-13 09:57:05 -0700202 /**
Winson Chung9800e732018-04-04 13:33:23 -0700203 * Used to set the override visibility state, used only to handle the transition home with the
204 * recents animation.
205 * @see LauncherAppTransitionManagerImpl.getWallpaperOpenRunner()
206 */
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700207 public void addForceInvisibleFlag(@InvisibilityFlags int flag) {
208 mForceInvisible |= flag;
Winson Chung9800e732018-04-04 13:33:23 -0700209 }
210
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700211 public void clearForceInvisibleFlag(@InvisibilityFlags int flag) {
212 mForceInvisible &= ~flag;
213 }
214
215
Winson Chung9800e732018-04-04 13:33:23 -0700216 /**
217 * @return Wether this activity should be considered invisible regardless of actual visibility.
218 */
219 public boolean isForceInvisible() {
Sunny Goyal7eff40f2018-04-11 15:30:46 -0700220 return mForceInvisible != 0;
Winson Chung9800e732018-04-04 13:33:23 -0700221 }
222
Winson Chung1a77c3d2018-04-11 12:47:47 -0700223 public interface MultiWindowModeChangedListener {
224 void onMultiWindowModeChanged(boolean isInMultiWindowMode);
225 }
Sunny Goyale43d00d2018-05-14 14:23:18 -0700226
227 protected void dumpMisc(PrintWriter writer) {
228 writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout());
229 writer.println(" orientation=" + getResources().getConfiguration().orientation);
230 writer.println(" mSystemUiController: " + mSystemUiController);
231 writer.println(" mActivityFlags: " + mActivityFlags);
232 writer.println(" mForceInvisible: " + mForceInvisible);
233 }
Sunny Goyal27835952017-01-13 12:15:53 -0800234}