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 | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 20 | import static java.lang.annotation.RetentionPolicy.SOURCE; |
| 21 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 22 | import android.app.Activity; |
| 23 | import android.content.Context; |
| 24 | import android.content.ContextWrapper; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 25 | import android.content.Intent; |
Winson Chung | 1a77c3d | 2018-04-11 12:47:47 -0700 | [diff] [blame] | 26 | import android.content.res.Configuration; |
Sunny Goyal | 87b5eb6 | 2018-07-03 15:53:39 -0700 | [diff] [blame] | 27 | import android.view.ContextThemeWrapper; |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 28 | import android.view.View.AccessibilityDelegate; |
| 29 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 30 | import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener; |
Hyunyoung Song | fc00747 | 2018-10-25 14:09:50 -0700 | [diff] [blame] | 31 | import com.android.launcher3.logging.StatsLogManager; |
| 32 | import com.android.launcher3.logging.StatsLogUtils; |
| 33 | import com.android.launcher3.logging.StatsLogUtils.LogStateProvider; |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 34 | import com.android.launcher3.logging.UserEventDispatcher; |
Hyunyoung Song | 46d07f7 | 2018-05-22 15:41:25 -0700 | [diff] [blame] | 35 | import com.android.launcher3.logging.UserEventDispatcher.UserEventDelegate; |
Sunny Goyal | 55eb556 | 2018-05-15 14:53:30 -0700 | [diff] [blame] | 36 | import com.android.launcher3.uioverrides.UiFactory; |
Hyunyoung Song | 46d07f7 | 2018-05-22 15:41:25 -0700 | [diff] [blame] | 37 | import com.android.launcher3.userevent.nano.LauncherLogProto; |
Sunny Goyal | 8392c82 | 2017-06-20 10:03:56 -0700 | [diff] [blame] | 38 | import com.android.launcher3.util.SystemUiController; |
Sunny Goyal | fe8e4a9 | 2018-11-13 19:43:57 -0800 | [diff] [blame] | 39 | import com.android.launcher3.views.ActivityContext; |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 40 | |
Sunny Goyal | 55eb556 | 2018-05-15 14:53:30 -0700 | [diff] [blame] | 41 | import java.io.FileDescriptor; |
Sunny Goyal | e43d00d | 2018-05-14 14:23:18 -0700 | [diff] [blame] | 42 | import java.io.PrintWriter; |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 43 | import java.lang.annotation.Retention; |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 44 | import java.util.ArrayList; |
| 45 | |
Sunny Goyal | d230307 | 2018-08-14 15:21:45 -0700 | [diff] [blame] | 46 | import androidx.annotation.IntDef; |
| 47 | |
Sunny Goyal | fe8e4a9 | 2018-11-13 19:43:57 -0800 | [diff] [blame] | 48 | public abstract class BaseActivity extends Activity |
| 49 | implements UserEventDelegate, LogStateProvider, ActivityContext { |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 50 | |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 51 | public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0; |
| 52 | public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1; |
Sunny Goyal | 1c63c72 | 2018-06-05 16:00:34 -0700 | [diff] [blame] | 53 | public static final int INVISIBLE_BY_PENDING_FLAGS = 1 << 2; |
| 54 | |
| 55 | // This is not treated as invisibility flag, but adds as a hint for an incomplete transition. |
| 56 | // When the wallpaper animation runs, it replaces this flag with a proper invisibility |
| 57 | // flag, INVISIBLE_BY_PENDING_FLAGS only for the duration of that animation. |
| 58 | public static final int PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION = 1 << 3; |
| 59 | |
| 60 | private static final int INVISIBLE_FLAGS = |
| 61 | INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS | INVISIBLE_BY_PENDING_FLAGS; |
| 62 | public static final int STATE_HANDLER_INVISIBILITY_FLAGS = |
| 63 | INVISIBLE_BY_STATE_HANDLER | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION; |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 64 | public static final int INVISIBLE_ALL = |
Sunny Goyal | 1c63c72 | 2018-06-05 16:00:34 -0700 | [diff] [blame] | 65 | INVISIBLE_FLAGS | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION; |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 66 | |
| 67 | @Retention(SOURCE) |
| 68 | @IntDef( |
| 69 | flag = true, |
Sunny Goyal | 1c63c72 | 2018-06-05 16:00:34 -0700 | [diff] [blame] | 70 | value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS, |
| 71 | INVISIBLE_BY_PENDING_FLAGS, PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION}) |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 72 | public @interface InvisibilityFlags{} |
| 73 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 74 | private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>(); |
Winson Chung | 1a77c3d | 2018-04-11 12:47:47 -0700 | [diff] [blame] | 75 | private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners = |
| 76 | new ArrayList<>(); |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 77 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 78 | protected DeviceProfile mDeviceProfile; |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 79 | protected UserEventDispatcher mUserEventDispatcher; |
Hyunyoung Song | fc00747 | 2018-10-25 14:09:50 -0700 | [diff] [blame] | 80 | protected StatsLogManager mStatsLogManager; |
Sunny Goyal | 8392c82 | 2017-06-20 10:03:56 -0700 | [diff] [blame] | 81 | protected SystemUiController mSystemUiController; |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 82 | |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 83 | private static final int ACTIVITY_STATE_STARTED = 1 << 0; |
| 84 | private static final int ACTIVITY_STATE_RESUMED = 1 << 1; |
| 85 | /** |
| 86 | * State flag indicating if the user is active or the actitvity when to background as a result |
| 87 | * of user action. |
| 88 | * @see #isUserActive() |
| 89 | */ |
| 90 | private static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 2; |
| 91 | |
| 92 | @Retention(SOURCE) |
| 93 | @IntDef( |
| 94 | flag = true, |
| 95 | value = {ACTIVITY_STATE_STARTED, ACTIVITY_STATE_RESUMED, ACTIVITY_STATE_USER_ACTIVE}) |
| 96 | public @interface ActivityFlags{} |
| 97 | |
| 98 | @ActivityFlags |
| 99 | private int mActivityFlags; |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 100 | |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 101 | // When the recents animation is running, the visibility of the Launcher is managed by the |
| 102 | // animation |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 103 | @InvisibilityFlags private int mForceInvisible; |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 104 | |
Sunny Goyal | fe8e4a9 | 2018-11-13 19:43:57 -0800 | [diff] [blame] | 105 | @Override |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 106 | public DeviceProfile getDeviceProfile() { |
| 107 | return mDeviceProfile; |
| 108 | } |
| 109 | |
Hyunyoung Song | fc00747 | 2018-10-25 14:09:50 -0700 | [diff] [blame] | 110 | public int getCurrentState() { return StatsLogUtils.LAUNCHER_STATE_BACKGROUND; } |
| 111 | |
Hyunyoung Song | 46d07f7 | 2018-05-22 15:41:25 -0700 | [diff] [blame] | 112 | public void modifyUserEvent(LauncherLogProto.LauncherEvent event) {} |
| 113 | |
Hyunyoung Song | fc00747 | 2018-10-25 14:09:50 -0700 | [diff] [blame] | 114 | public final StatsLogManager getStatsLogManager() { |
| 115 | if (mStatsLogManager == null) { |
| 116 | mStatsLogManager = StatsLogManager.newInstance(this, this); |
| 117 | } |
| 118 | return mStatsLogManager; |
| 119 | } |
| 120 | |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 121 | public final UserEventDispatcher getUserEventDispatcher() { |
| 122 | if (mUserEventDispatcher == null) { |
Hyunyoung Song | 956ec4b | 2018-07-02 13:17:32 -0700 | [diff] [blame] | 123 | mUserEventDispatcher = UserEventDispatcher.newInstance(this, this); |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 124 | } |
| 125 | return mUserEventDispatcher; |
| 126 | } |
| 127 | |
Jon Miranda | fe96432 | 2017-03-22 10:25:17 -0700 | [diff] [blame] | 128 | public boolean isInMultiWindowModeCompat() { |
| 129 | return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode(); |
| 130 | } |
| 131 | |
Sunny Goyal | 8392c82 | 2017-06-20 10:03:56 -0700 | [diff] [blame] | 132 | public SystemUiController getSystemUiController() { |
| 133 | if (mSystemUiController == null) { |
| 134 | mSystemUiController = new SystemUiController(getWindow()); |
| 135 | } |
| 136 | return mSystemUiController; |
| 137 | } |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 138 | |
| 139 | @Override |
| 140 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 141 | super.onActivityResult(requestCode, resultCode, data); |
| 142 | } |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 143 | |
| 144 | @Override |
| 145 | protected void onStart() { |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 146 | mActivityFlags |= ACTIVITY_STATE_STARTED; |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 147 | super.onStart(); |
| 148 | } |
| 149 | |
| 150 | @Override |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 151 | protected void onResume() { |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 152 | mActivityFlags |= ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE; |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 153 | super.onResume(); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | protected void onUserLeaveHint() { |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 158 | mActivityFlags &= ~ACTIVITY_STATE_USER_ACTIVE; |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 159 | super.onUserLeaveHint(); |
| 160 | } |
| 161 | |
| 162 | @Override |
Winson Chung | 1a77c3d | 2018-04-11 12:47:47 -0700 | [diff] [blame] | 163 | public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) { |
| 164 | super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig); |
| 165 | for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) { |
| 166 | mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | @Override |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 171 | protected void onStop() { |
Tracy Zhou | b67e353 | 2018-04-17 23:45:47 -0700 | [diff] [blame] | 172 | mActivityFlags &= ~ACTIVITY_STATE_STARTED & ~ACTIVITY_STATE_USER_ACTIVE; |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 173 | mForceInvisible = 0; |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 174 | super.onStop(); |
| 175 | } |
| 176 | |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 177 | @Override |
| 178 | protected void onPause() { |
| 179 | mActivityFlags &= ~ACTIVITY_STATE_RESUMED; |
| 180 | super.onPause(); |
Winson Chung | a0f09f9 | 2018-05-11 21:55:21 +0000 | [diff] [blame] | 181 | |
| 182 | // Reset the overridden sysui flags used for the task-swipe launch animation, we do this |
| 183 | // here instead of at the end of the animation because the start of the new activity does |
| 184 | // not happen immediately, which would cause us to reset to launcher's sysui flags and then |
| 185 | // back to the new app (causing a flash) |
| 186 | getSystemUiController().updateUiState(UI_STATE_OVERVIEW, 0); |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 189 | public boolean isStarted() { |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 190 | return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * isResumed in already defined as a hidden final method in Activity.java |
| 195 | */ |
| 196 | public boolean hasBeenResumed() { |
| 197 | return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0; |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 198 | } |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 199 | |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 200 | public boolean isUserActive() { |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 201 | return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0; |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 204 | public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) { |
| 205 | mDPChangeListeners.add(listener); |
| 206 | } |
| 207 | |
Winson Chung | 8a968fa | 2018-03-15 17:59:04 -0700 | [diff] [blame] | 208 | public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) { |
| 209 | mDPChangeListeners.remove(listener); |
| 210 | } |
| 211 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 212 | protected void dispatchDeviceProfileChanged() { |
Winson Chung | 8a968fa | 2018-03-15 17:59:04 -0700 | [diff] [blame] | 213 | for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) { |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 214 | mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile); |
| 215 | } |
| 216 | } |
Sunny Goyal | f633ef5 | 2018-03-13 09:57:05 -0700 | [diff] [blame] | 217 | |
Winson Chung | 1a77c3d | 2018-04-11 12:47:47 -0700 | [diff] [blame] | 218 | public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) { |
| 219 | mMultiWindowModeChangedListeners.add(listener); |
| 220 | } |
| 221 | |
| 222 | public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) { |
| 223 | mMultiWindowModeChangedListeners.remove(listener); |
| 224 | } |
| 225 | |
Sunny Goyal | f633ef5 | 2018-03-13 09:57:05 -0700 | [diff] [blame] | 226 | /** |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 227 | * Used to set the override visibility state, used only to handle the transition home with the |
| 228 | * recents animation. |
Kevin | c4ad03b | 2019-01-16 18:51:53 -0800 | [diff] [blame^] | 229 | * @see QuickstepAppTransitionManagerImpl#getWallpaperOpenRunner() |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 230 | */ |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 231 | public void addForceInvisibleFlag(@InvisibilityFlags int flag) { |
| 232 | mForceInvisible |= flag; |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 235 | public void clearForceInvisibleFlag(@InvisibilityFlags int flag) { |
| 236 | mForceInvisible &= ~flag; |
| 237 | } |
| 238 | |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 239 | /** |
| 240 | * @return Wether this activity should be considered invisible regardless of actual visibility. |
| 241 | */ |
| 242 | public boolean isForceInvisible() { |
Sunny Goyal | 1c63c72 | 2018-06-05 16:00:34 -0700 | [diff] [blame] | 243 | return hasSomeInvisibleFlag(INVISIBLE_FLAGS); |
| 244 | } |
| 245 | |
| 246 | public boolean hasSomeInvisibleFlag(int mask) { |
| 247 | return (mForceInvisible & mask) != 0; |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Winson Chung | 1a77c3d | 2018-04-11 12:47:47 -0700 | [diff] [blame] | 250 | public interface MultiWindowModeChangedListener { |
| 251 | void onMultiWindowModeChanged(boolean isInMultiWindowMode); |
| 252 | } |
Sunny Goyal | e43d00d | 2018-05-14 14:23:18 -0700 | [diff] [blame] | 253 | |
Sunny Goyal | 55eb556 | 2018-05-15 14:53:30 -0700 | [diff] [blame] | 254 | @Override |
| 255 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 256 | if (!UiFactory.dumpActivity(this, writer)) { |
| 257 | super.dump(prefix, fd, writer, args); |
| 258 | } |
| 259 | } |
| 260 | |
Sunny Goyal | e43d00d | 2018-05-14 14:23:18 -0700 | [diff] [blame] | 261 | protected void dumpMisc(PrintWriter writer) { |
| 262 | writer.println(" deviceProfile isTransposed=" + getDeviceProfile().isVerticalBarLayout()); |
| 263 | writer.println(" orientation=" + getResources().getConfiguration().orientation); |
| 264 | writer.println(" mSystemUiController: " + mSystemUiController); |
| 265 | writer.println(" mActivityFlags: " + mActivityFlags); |
| 266 | writer.println(" mForceInvisible: " + mForceInvisible); |
| 267 | } |
Sunny Goyal | 87b5eb6 | 2018-07-03 15:53:39 -0700 | [diff] [blame] | 268 | |
| 269 | public static <T extends BaseActivity> T fromContext(Context context) { |
| 270 | if (context instanceof BaseActivity) { |
| 271 | return (T) context; |
| 272 | } else if (context instanceof ContextThemeWrapper) { |
| 273 | return fromContext(((ContextWrapper) context).getBaseContext()); |
| 274 | } else { |
| 275 | throw new IllegalArgumentException("Cannot find BaseActivity in parent tree"); |
| 276 | } |
| 277 | } |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 278 | } |