Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher3; |
| 18 | |
Winson Chung | a0f09f9 | 2018-05-11 21:55:21 +0000 | [diff] [blame] | 19 | import static com.android.launcher3.util.SystemUiController.UI_STATE_OVERVIEW; |
Sunny Goyal | e43d00d | 2018-05-14 14:23:18 -0700 | [diff] [blame] | 20 | |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 21 | import static java.lang.annotation.RetentionPolicy.SOURCE; |
| 22 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 23 | import android.app.Activity; |
| 24 | import android.content.Context; |
| 25 | import android.content.ContextWrapper; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 26 | import android.content.Intent; |
Winson Chung | 1a77c3d | 2018-04-11 12:47:47 -0700 | [diff] [blame] | 27 | import android.content.res.Configuration; |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 28 | import android.support.annotation.IntDef; |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 29 | import android.view.View.AccessibilityDelegate; |
| 30 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 31 | import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener; |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 32 | import com.android.launcher3.logging.UserEventDispatcher; |
Sunny Goyal | 8392c82 | 2017-06-20 10:03:56 -0700 | [diff] [blame] | 33 | import com.android.launcher3.util.SystemUiController; |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 34 | |
Sunny Goyal | e43d00d | 2018-05-14 14:23:18 -0700 | [diff] [blame] | 35 | import java.io.PrintWriter; |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 36 | import java.lang.annotation.Retention; |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 37 | import java.util.ArrayList; |
| 38 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 39 | public abstract class BaseActivity extends Activity { |
| 40 | |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 41 | 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 Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 52 | private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>(); |
Winson Chung | 1a77c3d | 2018-04-11 12:47:47 -0700 | [diff] [blame] | 53 | private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners = |
| 54 | new ArrayList<>(); |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 55 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 56 | protected DeviceProfile mDeviceProfile; |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 57 | protected UserEventDispatcher mUserEventDispatcher; |
Sunny Goyal | 8392c82 | 2017-06-20 10:03:56 -0700 | [diff] [blame] | 58 | protected SystemUiController mSystemUiController; |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 59 | |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 60 | 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 Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 77 | |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 78 | // When the recents animation is running, the visibility of the Launcher is managed by the |
| 79 | // animation |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 80 | @InvisibilityFlags private int mForceInvisible; |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 81 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 82 | public DeviceProfile getDeviceProfile() { |
| 83 | return mDeviceProfile; |
| 84 | } |
| 85 | |
| 86 | public AccessibilityDelegate getAccessibilityDelegate() { |
| 87 | return null; |
| 88 | } |
| 89 | |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 90 | public final UserEventDispatcher getUserEventDispatcher() { |
| 91 | if (mUserEventDispatcher == null) { |
Sunny Goyal | d70e75a | 2018-02-22 10:07:32 -0800 | [diff] [blame] | 92 | mUserEventDispatcher = UserEventDispatcher.newInstance(this, mDeviceProfile); |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 93 | } |
| 94 | return mUserEventDispatcher; |
| 95 | } |
| 96 | |
Jon Miranda | fe96432 | 2017-03-22 10:25:17 -0700 | [diff] [blame] | 97 | public boolean isInMultiWindowModeCompat() { |
| 98 | return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode(); |
| 99 | } |
| 100 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 101 | public static BaseActivity fromContext(Context context) { |
| 102 | if (context instanceof BaseActivity) { |
| 103 | return (BaseActivity) context; |
| 104 | } |
| 105 | return ((BaseActivity) ((ContextWrapper) context).getBaseContext()); |
| 106 | } |
Sunny Goyal | 8392c82 | 2017-06-20 10:03:56 -0700 | [diff] [blame] | 107 | |
| 108 | public SystemUiController getSystemUiController() { |
| 109 | if (mSystemUiController == null) { |
| 110 | mSystemUiController = new SystemUiController(getWindow()); |
| 111 | } |
| 112 | return mSystemUiController; |
| 113 | } |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 114 | |
| 115 | @Override |
| 116 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 117 | super.onActivityResult(requestCode, resultCode, data); |
| 118 | } |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 119 | |
| 120 | @Override |
| 121 | protected void onStart() { |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 122 | mActivityFlags |= ACTIVITY_STATE_STARTED; |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 123 | super.onStart(); |
| 124 | } |
| 125 | |
| 126 | @Override |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 127 | protected void onResume() { |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 128 | mActivityFlags |= ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE; |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 129 | super.onResume(); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | protected void onUserLeaveHint() { |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 134 | mActivityFlags &= ~ACTIVITY_STATE_USER_ACTIVE; |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 135 | super.onUserLeaveHint(); |
| 136 | } |
| 137 | |
| 138 | @Override |
Winson Chung | 1a77c3d | 2018-04-11 12:47:47 -0700 | [diff] [blame] | 139 | 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 Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 147 | protected void onStop() { |
Tracy Zhou | b67e353 | 2018-04-17 23:45:47 -0700 | [diff] [blame] | 148 | mActivityFlags &= ~ACTIVITY_STATE_STARTED & ~ACTIVITY_STATE_USER_ACTIVE; |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 149 | mForceInvisible = 0; |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 150 | super.onStop(); |
| 151 | } |
| 152 | |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 153 | @Override |
| 154 | protected void onPause() { |
| 155 | mActivityFlags &= ~ACTIVITY_STATE_RESUMED; |
| 156 | super.onPause(); |
Winson Chung | a0f09f9 | 2018-05-11 21:55:21 +0000 | [diff] [blame] | 157 | |
| 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 Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 165 | public boolean isStarted() { |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 166 | 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 Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 174 | } |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 175 | |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 176 | public boolean isUserActive() { |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 177 | return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0; |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 180 | public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) { |
| 181 | mDPChangeListeners.add(listener); |
| 182 | } |
| 183 | |
Winson Chung | 8a968fa | 2018-03-15 17:59:04 -0700 | [diff] [blame] | 184 | public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) { |
| 185 | mDPChangeListeners.remove(listener); |
| 186 | } |
| 187 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 188 | protected void dispatchDeviceProfileChanged() { |
Winson Chung | 8a968fa | 2018-03-15 17:59:04 -0700 | [diff] [blame] | 189 | for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) { |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 190 | mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile); |
| 191 | } |
| 192 | } |
Sunny Goyal | f633ef5 | 2018-03-13 09:57:05 -0700 | [diff] [blame] | 193 | |
Winson Chung | 1a77c3d | 2018-04-11 12:47:47 -0700 | [diff] [blame] | 194 | public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) { |
| 195 | mMultiWindowModeChangedListeners.add(listener); |
| 196 | } |
| 197 | |
| 198 | public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) { |
| 199 | mMultiWindowModeChangedListeners.remove(listener); |
| 200 | } |
| 201 | |
Sunny Goyal | f633ef5 | 2018-03-13 09:57:05 -0700 | [diff] [blame] | 202 | /** |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 203 | * 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 Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 207 | public void addForceInvisibleFlag(@InvisibilityFlags int flag) { |
| 208 | mForceInvisible |= flag; |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 211 | public void clearForceInvisibleFlag(@InvisibilityFlags int flag) { |
| 212 | mForceInvisible &= ~flag; |
| 213 | } |
| 214 | |
| 215 | |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 216 | /** |
| 217 | * @return Wether this activity should be considered invisible regardless of actual visibility. |
| 218 | */ |
| 219 | public boolean isForceInvisible() { |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 220 | return mForceInvisible != 0; |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Winson Chung | 1a77c3d | 2018-04-11 12:47:47 -0700 | [diff] [blame] | 223 | public interface MultiWindowModeChangedListener { |
| 224 | void onMultiWindowModeChanged(boolean isInMultiWindowMode); |
| 225 | } |
Sunny Goyal | e43d00d | 2018-05-14 14:23:18 -0700 | [diff] [blame] | 226 | |
| 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 Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 234 | } |