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 | |
Mike Schneider | a79d460 | 2023-03-03 15:58:06 +0100 | [diff] [blame] | 19 | import static com.android.launcher3.util.FlagDebugUtils.appendFlag; |
| 20 | import static com.android.launcher3.util.FlagDebugUtils.formatFlagChange; |
Tony Wickham | b482188 | 2021-04-23 14:26:45 -0700 | [diff] [blame] | 21 | import static com.android.launcher3.util.SystemUiController.UI_STATE_FULLSCREEN_TASK; |
Sunny Goyal | 8c48d8b | 2019-01-25 15:10:18 -0800 | [diff] [blame] | 22 | |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 23 | import static java.lang.annotation.RetentionPolicy.SOURCE; |
| 24 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 25 | import android.app.Activity; |
| 26 | import android.content.Context; |
| 27 | import android.content.ContextWrapper; |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 28 | import android.content.Intent; |
Winson Chung | 1a77c3d | 2018-04-11 12:47:47 -0700 | [diff] [blame] | 29 | import android.content.res.Configuration; |
Yein Jo | 18446d0 | 2022-09-19 22:18:09 +0000 | [diff] [blame] | 30 | import android.os.Bundle; |
Mike Schneider | a79d460 | 2023-03-03 15:58:06 +0100 | [diff] [blame] | 31 | import android.util.Log; |
Yein Jo | 18446d0 | 2022-09-19 22:18:09 +0000 | [diff] [blame] | 32 | import android.window.OnBackInvokedDispatcher; |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 33 | |
Winson Chung | ef52876 | 2019-09-06 12:05:52 -0700 | [diff] [blame] | 34 | import androidx.annotation.IntDef; |
| 35 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 36 | import com.android.launcher3.DeviceProfile.OnDeviceProfileChangeListener; |
Hyunyoung Song | fc00747 | 2018-10-25 14:09:50 -0700 | [diff] [blame] | 37 | import com.android.launcher3.logging.StatsLogManager; |
Yein Jo | 18446d0 | 2022-09-19 22:18:09 +0000 | [diff] [blame] | 38 | import com.android.launcher3.testing.TestLogging; |
| 39 | import com.android.launcher3.testing.shared.TestProtocol; |
Sunny Goyal | e82a20a | 2023-10-19 13:52:49 -0700 | [diff] [blame] | 40 | import com.android.launcher3.util.RunnableList; |
Sunny Goyal | 8392c82 | 2017-06-20 10:03:56 -0700 | [diff] [blame] | 41 | import com.android.launcher3.util.SystemUiController; |
Sunny Goyal | 5686333 | 2019-05-22 14:13:53 -0700 | [diff] [blame] | 42 | import com.android.launcher3.util.ViewCache; |
Sunny Goyal | 54fa110 | 2022-12-07 22:48:37 -0800 | [diff] [blame] | 43 | import com.android.launcher3.views.ActivityContext; |
Tony Wickham | b482188 | 2021-04-23 14:26:45 -0700 | [diff] [blame] | 44 | import com.android.launcher3.views.ScrimView; |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 45 | |
Sunny Goyal | e43d00d | 2018-05-14 14:23:18 -0700 | [diff] [blame] | 46 | import java.io.PrintWriter; |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 47 | import java.lang.annotation.Retention; |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 48 | import java.util.ArrayList; |
Brian Isganitis | 099945b | 2022-01-31 18:15:00 -0500 | [diff] [blame] | 49 | import java.util.List; |
Mike Schneider | a79d460 | 2023-03-03 15:58:06 +0100 | [diff] [blame] | 50 | import java.util.StringJoiner; |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 51 | |
Samuel Fufa | a579ddc | 2020-02-27 16:59:19 -0800 | [diff] [blame] | 52 | /** |
| 53 | * Launcher BaseActivity |
| 54 | */ |
Sunny Goyal | 54fa110 | 2022-12-07 22:48:37 -0800 | [diff] [blame] | 55 | public abstract class BaseActivity extends Activity implements ActivityContext { |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 56 | |
Sunny Goyal | fa39536 | 2019-12-11 10:00:47 -0800 | [diff] [blame] | 57 | private static final String TAG = "BaseActivity"; |
Mike Schneider | a79d460 | 2023-03-03 15:58:06 +0100 | [diff] [blame] | 58 | static final boolean DEBUG = false; |
Sunny Goyal | fa39536 | 2019-12-11 10:00:47 -0800 | [diff] [blame] | 59 | |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 60 | public static final int INVISIBLE_BY_STATE_HANDLER = 1 << 0; |
| 61 | public static final int INVISIBLE_BY_APP_TRANSITIONS = 1 << 1; |
Sunny Goyal | 1c63c72 | 2018-06-05 16:00:34 -0700 | [diff] [blame] | 62 | public static final int INVISIBLE_BY_PENDING_FLAGS = 1 << 2; |
| 63 | |
| 64 | // This is not treated as invisibility flag, but adds as a hint for an incomplete transition. |
| 65 | // When the wallpaper animation runs, it replaces this flag with a proper invisibility |
| 66 | // flag, INVISIBLE_BY_PENDING_FLAGS only for the duration of that animation. |
| 67 | public static final int PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION = 1 << 3; |
| 68 | |
| 69 | private static final int INVISIBLE_FLAGS = |
| 70 | INVISIBLE_BY_STATE_HANDLER | INVISIBLE_BY_APP_TRANSITIONS | INVISIBLE_BY_PENDING_FLAGS; |
| 71 | public static final int STATE_HANDLER_INVISIBILITY_FLAGS = |
| 72 | INVISIBLE_BY_STATE_HANDLER | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION; |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 73 | public static final int INVISIBLE_ALL = |
Sunny Goyal | 1c63c72 | 2018-06-05 16:00:34 -0700 | [diff] [blame] | 74 | INVISIBLE_FLAGS | PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION; |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 75 | |
| 76 | @Retention(SOURCE) |
| 77 | @IntDef( |
| 78 | flag = true, |
Sunny Goyal | 1c63c72 | 2018-06-05 16:00:34 -0700 | [diff] [blame] | 79 | value = {INVISIBLE_BY_STATE_HANDLER, INVISIBLE_BY_APP_TRANSITIONS, |
| 80 | INVISIBLE_BY_PENDING_FLAGS, PENDING_INVISIBLE_BY_WALLPAPER_ANIMATION}) |
Mike Schneider | a79d460 | 2023-03-03 15:58:06 +0100 | [diff] [blame] | 81 | public @interface InvisibilityFlags { |
| 82 | } |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 83 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 84 | private final ArrayList<OnDeviceProfileChangeListener> mDPChangeListeners = new ArrayList<>(); |
Winson Chung | 1a77c3d | 2018-04-11 12:47:47 -0700 | [diff] [blame] | 85 | private final ArrayList<MultiWindowModeChangedListener> mMultiWindowModeChangedListeners = |
| 86 | new ArrayList<>(); |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 87 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 88 | protected DeviceProfile mDeviceProfile; |
Sunny Goyal | 8392c82 | 2017-06-20 10:03:56 -0700 | [diff] [blame] | 89 | protected SystemUiController mSystemUiController; |
Sunny Goyal | 977838b | 2022-06-27 13:15:41 -0700 | [diff] [blame] | 90 | private StatsLogManager mStatsLogManager; |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 91 | |
Sunny Goyal | 210e174 | 2019-10-17 12:05:38 -0700 | [diff] [blame] | 92 | |
| 93 | public static final int ACTIVITY_STATE_STARTED = 1 << 0; |
| 94 | public static final int ACTIVITY_STATE_RESUMED = 1 << 1; |
| 95 | |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 96 | /** |
Sunny Goyal | 210e174 | 2019-10-17 12:05:38 -0700 | [diff] [blame] | 97 | * State flags indicating that the activity has received one frame after resume, and was |
| 98 | * not immediately paused. |
| 99 | */ |
| 100 | public static final int ACTIVITY_STATE_DEFERRED_RESUMED = 1 << 2; |
| 101 | |
| 102 | public static final int ACTIVITY_STATE_WINDOW_FOCUSED = 1 << 3; |
| 103 | |
| 104 | /** |
| 105 | * State flag indicating if the user is active or the activity when to background as a result |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 106 | * of user action. |
Mike Schneider | a79d460 | 2023-03-03 15:58:06 +0100 | [diff] [blame] | 107 | * |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 108 | * @see #isUserActive() |
| 109 | */ |
Sunny Goyal | 210e174 | 2019-10-17 12:05:38 -0700 | [diff] [blame] | 110 | public static final int ACTIVITY_STATE_USER_ACTIVE = 1 << 4; |
| 111 | |
| 112 | /** |
| 113 | * State flag indicating that a state transition is in progress |
| 114 | */ |
Winson Chung | 034ce6f | 2020-05-14 10:49:30 -0700 | [diff] [blame] | 115 | public static final int ACTIVITY_STATE_TRANSITION_ACTIVE = 1 << 6; |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 116 | |
| 117 | @Retention(SOURCE) |
| 118 | @IntDef( |
| 119 | flag = true, |
Sunny Goyal | 210e174 | 2019-10-17 12:05:38 -0700 | [diff] [blame] | 120 | value = {ACTIVITY_STATE_STARTED, |
| 121 | ACTIVITY_STATE_RESUMED, |
| 122 | ACTIVITY_STATE_DEFERRED_RESUMED, |
| 123 | ACTIVITY_STATE_WINDOW_FOCUSED, |
| 124 | ACTIVITY_STATE_USER_ACTIVE, |
| 125 | ACTIVITY_STATE_TRANSITION_ACTIVE}) |
Mike Schneider | a79d460 | 2023-03-03 15:58:06 +0100 | [diff] [blame] | 126 | public @interface ActivityFlags { |
| 127 | } |
| 128 | |
| 129 | /** Returns a human-readable string for the specified {@link ActivityFlags}. */ |
| 130 | public static String getActivityStateString(@ActivityFlags int flags) { |
| 131 | StringJoiner result = new StringJoiner("|"); |
| 132 | appendFlag(result, flags, ACTIVITY_STATE_STARTED, "state_started"); |
| 133 | appendFlag(result, flags, ACTIVITY_STATE_RESUMED, "state_resumed"); |
| 134 | appendFlag(result, flags, ACTIVITY_STATE_DEFERRED_RESUMED, "state_deferred_resumed"); |
| 135 | appendFlag(result, flags, ACTIVITY_STATE_WINDOW_FOCUSED, "state_window_focused"); |
| 136 | appendFlag(result, flags, ACTIVITY_STATE_USER_ACTIVE, "state_user_active"); |
| 137 | appendFlag(result, flags, ACTIVITY_STATE_TRANSITION_ACTIVE, "state_transition_active"); |
| 138 | return result.toString(); |
| 139 | } |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 140 | |
| 141 | @ActivityFlags |
| 142 | private int mActivityFlags; |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 143 | |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 144 | // When the recents animation is running, the visibility of the Launcher is managed by the |
| 145 | // animation |
Mike Schneider | a79d460 | 2023-03-03 15:58:06 +0100 | [diff] [blame] | 146 | @InvisibilityFlags |
| 147 | private int mForceInvisible; |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 148 | |
Sunny Goyal | 5686333 | 2019-05-22 14:13:53 -0700 | [diff] [blame] | 149 | private final ViewCache mViewCache = new ViewCache(); |
| 150 | |
Sunny Goyal | e82a20a | 2023-10-19 13:52:49 -0700 | [diff] [blame] | 151 | @Retention(SOURCE) |
| 152 | @IntDef({EVENT_STARTED, EVENT_RESUMED, EVENT_STOPPED, EVENT_DESTROYED}) |
| 153 | public @interface ActivityEvent { } |
| 154 | public static final int EVENT_STARTED = 0; |
| 155 | public static final int EVENT_RESUMED = 1; |
| 156 | public static final int EVENT_STOPPED = 2; |
| 157 | public static final int EVENT_DESTROYED = 3; |
| 158 | |
| 159 | // Callback array that corresponds to events defined in @ActivityEvent |
| 160 | private final RunnableList[] mEventCallbacks = |
| 161 | {new RunnableList(), new RunnableList(), new RunnableList(), new RunnableList()}; |
| 162 | |
Sunny Goyal | 5996937 | 2021-05-06 12:11:44 -0700 | [diff] [blame] | 163 | @Override |
Sunny Goyal | 5686333 | 2019-05-22 14:13:53 -0700 | [diff] [blame] | 164 | public ViewCache getViewCache() { |
| 165 | return mViewCache; |
| 166 | } |
| 167 | |
Sunny Goyal | fe8e4a9 | 2018-11-13 19:43:57 -0800 | [diff] [blame] | 168 | @Override |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 169 | public DeviceProfile getDeviceProfile() { |
| 170 | return mDeviceProfile; |
| 171 | } |
| 172 | |
Brian Isganitis | 099945b | 2022-01-31 18:15:00 -0500 | [diff] [blame] | 173 | @Override |
| 174 | public List<OnDeviceProfileChangeListener> getOnDeviceProfileChangeListeners() { |
| 175 | return mDPChangeListeners; |
| 176 | } |
| 177 | |
thiruram | c96873c | 2021-02-11 15:13:47 -0800 | [diff] [blame] | 178 | /** |
| 179 | * Returns {@link StatsLogManager} for user event logging. |
| 180 | */ |
Sunny Goyal | 177785e | 2021-07-29 15:48:24 -0700 | [diff] [blame] | 181 | @Override |
thiruram | c96873c | 2021-02-11 15:13:47 -0800 | [diff] [blame] | 182 | public StatsLogManager getStatsLogManager() { |
Hyunyoung Song | fc00747 | 2018-10-25 14:09:50 -0700 | [diff] [blame] | 183 | if (mStatsLogManager == null) { |
Hyunyoung Song | 801f81f | 2020-06-19 02:58:53 -0700 | [diff] [blame] | 184 | mStatsLogManager = StatsLogManager.newInstance(this); |
Hyunyoung Song | fc00747 | 2018-10-25 14:09:50 -0700 | [diff] [blame] | 185 | } |
| 186 | return mStatsLogManager; |
| 187 | } |
| 188 | |
Sunny Goyal | 8392c82 | 2017-06-20 10:03:56 -0700 | [diff] [blame] | 189 | public SystemUiController getSystemUiController() { |
| 190 | if (mSystemUiController == null) { |
| 191 | mSystemUiController = new SystemUiController(getWindow()); |
| 192 | } |
| 193 | return mSystemUiController; |
| 194 | } |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 195 | |
Tony Wickham | b482188 | 2021-04-23 14:26:45 -0700 | [diff] [blame] | 196 | public ScrimView getScrimView() { |
| 197 | return null; |
| 198 | } |
| 199 | |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 200 | @Override |
| 201 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 202 | super.onActivityResult(requestCode, resultCode, data); |
| 203 | } |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 204 | |
| 205 | @Override |
Yein Jo | 18446d0 | 2022-09-19 22:18:09 +0000 | [diff] [blame] | 206 | protected void onCreate(Bundle savedInstanceState) { |
| 207 | super.onCreate(savedInstanceState); |
Fengjiang Li | e884c2c | 2022-12-19 14:42:14 -0800 | [diff] [blame] | 208 | registerBackDispatcher(); |
Yein Jo | 18446d0 | 2022-09-19 22:18:09 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | @Override |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 212 | protected void onStart() { |
Sunny Goyal | 210e174 | 2019-10-17 12:05:38 -0700 | [diff] [blame] | 213 | addActivityFlags(ACTIVITY_STATE_STARTED); |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 214 | super.onStart(); |
Sunny Goyal | e82a20a | 2023-10-19 13:52:49 -0700 | [diff] [blame] | 215 | mEventCallbacks[EVENT_STARTED].executeAllAndClear(); |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | @Override |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 219 | protected void onResume() { |
Mady Mellor | 9a90c2d | 2022-09-14 15:17:21 -0700 | [diff] [blame] | 220 | setResumed(); |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 221 | super.onResume(); |
Sunny Goyal | e82a20a | 2023-10-19 13:52:49 -0700 | [diff] [blame] | 222 | mEventCallbacks[EVENT_RESUMED].executeAllAndClear(); |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | @Override |
| 226 | protected void onUserLeaveHint() { |
Sunny Goyal | 210e174 | 2019-10-17 12:05:38 -0700 | [diff] [blame] | 227 | removeActivityFlags(ACTIVITY_STATE_USER_ACTIVE); |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 228 | super.onUserLeaveHint(); |
| 229 | } |
| 230 | |
| 231 | @Override |
Winson Chung | 1a77c3d | 2018-04-11 12:47:47 -0700 | [diff] [blame] | 232 | public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) { |
| 233 | super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig); |
| 234 | for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) { |
| 235 | mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | @Override |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 240 | protected void onStop() { |
Sunny Goyal | 210e174 | 2019-10-17 12:05:38 -0700 | [diff] [blame] | 241 | removeActivityFlags(ACTIVITY_STATE_STARTED | ACTIVITY_STATE_USER_ACTIVE); |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 242 | mForceInvisible = 0; |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 243 | super.onStop(); |
Sunny Goyal | e82a20a | 2023-10-19 13:52:49 -0700 | [diff] [blame] | 244 | mEventCallbacks[EVENT_STOPPED].executeAllAndClear(); |
| 245 | |
Winson Chung | a36c800 | 2019-06-11 16:15:54 -0700 | [diff] [blame] | 246 | |
| 247 | // Reset the overridden sysui flags used for the task-swipe launch animation, this is a |
| 248 | // catch all for if we do not get resumed (and therefore not paused below) |
Tony Wickham | b482188 | 2021-04-23 14:26:45 -0700 | [diff] [blame] | 249 | getSystemUiController().updateUiState(UI_STATE_FULLSCREEN_TASK, 0); |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 252 | @Override |
Sunny Goyal | e82a20a | 2023-10-19 13:52:49 -0700 | [diff] [blame] | 253 | protected void onDestroy() { |
| 254 | super.onDestroy(); |
| 255 | mEventCallbacks[EVENT_DESTROYED].executeAllAndClear(); |
| 256 | } |
| 257 | |
| 258 | @Override |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 259 | protected void onPause() { |
Mady Mellor | 9a90c2d | 2022-09-14 15:17:21 -0700 | [diff] [blame] | 260 | setPaused(); |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 261 | super.onPause(); |
Winson Chung | a0f09f9 | 2018-05-11 21:55:21 +0000 | [diff] [blame] | 262 | |
| 263 | // Reset the overridden sysui flags used for the task-swipe launch animation, we do this |
| 264 | // here instead of at the end of the animation because the start of the new activity does |
| 265 | // not happen immediately, which would cause us to reset to launcher's sysui flags and then |
| 266 | // back to the new app (causing a flash) |
Tony Wickham | b482188 | 2021-04-23 14:26:45 -0700 | [diff] [blame] | 267 | getSystemUiController().updateUiState(UI_STATE_FULLSCREEN_TASK, 0); |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Sunny Goyal | 210e174 | 2019-10-17 12:05:38 -0700 | [diff] [blame] | 270 | @Override |
| 271 | public void onWindowFocusChanged(boolean hasFocus) { |
| 272 | super.onWindowFocusChanged(hasFocus); |
| 273 | if (hasFocus) { |
| 274 | addActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED); |
| 275 | } else { |
| 276 | removeActivityFlags(ACTIVITY_STATE_WINDOW_FOCUSED); |
| 277 | } |
Sunny Goyal | 210e174 | 2019-10-17 12:05:38 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Fengjiang Li | e884c2c | 2022-12-19 14:42:14 -0800 | [diff] [blame] | 280 | protected void registerBackDispatcher() { |
| 281 | if (Utilities.ATLEAST_T) { |
| 282 | getOnBackInvokedDispatcher().registerOnBackInvokedCallback( |
| 283 | OnBackInvokedDispatcher.PRIORITY_DEFAULT, |
| 284 | () -> { |
| 285 | onBackPressed(); |
| 286 | TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "onBackInvoked"); |
| 287 | }); |
| 288 | } |
| 289 | } |
| 290 | |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 291 | public boolean isStarted() { |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 292 | return (mActivityFlags & ACTIVITY_STATE_STARTED) != 0; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * isResumed in already defined as a hidden final method in Activity.java |
| 297 | */ |
| 298 | public boolean hasBeenResumed() { |
| 299 | return (mActivityFlags & ACTIVITY_STATE_RESUMED) != 0; |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 300 | } |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 301 | |
Mady Mellor | 9a90c2d | 2022-09-14 15:17:21 -0700 | [diff] [blame] | 302 | /** |
| 303 | * Sets the activity to appear as paused. |
| 304 | */ |
| 305 | public void setPaused() { |
| 306 | removeActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_DEFERRED_RESUMED); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Sets the activity to appear as resumed. |
| 311 | */ |
| 312 | public void setResumed() { |
| 313 | addActivityFlags(ACTIVITY_STATE_RESUMED | ACTIVITY_STATE_USER_ACTIVE); |
Mady Mellor | 9a90c2d | 2022-09-14 15:17:21 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 316 | public boolean isUserActive() { |
Sunny Goyal | 3483c52 | 2018-04-12 11:23:33 -0700 | [diff] [blame] | 317 | return (mActivityFlags & ACTIVITY_STATE_USER_ACTIVE) != 0; |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Sunny Goyal | 210e174 | 2019-10-17 12:05:38 -0700 | [diff] [blame] | 320 | public int getActivityFlags() { |
| 321 | return mActivityFlags; |
| 322 | } |
| 323 | |
Mike Schneider | a79d460 | 2023-03-03 15:58:06 +0100 | [diff] [blame] | 324 | protected void addActivityFlags(int toAdd) { |
| 325 | final int oldFlags = mActivityFlags; |
| 326 | mActivityFlags |= toAdd; |
| 327 | if (DEBUG) { |
| 328 | Log.d(TAG, "Launcher flags updated: " + formatFlagChange(mActivityFlags, oldFlags, |
| 329 | BaseActivity::getActivityStateString)); |
| 330 | } |
| 331 | onActivityFlagsChanged(toAdd); |
Sunny Goyal | 210e174 | 2019-10-17 12:05:38 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Mike Schneider | a79d460 | 2023-03-03 15:58:06 +0100 | [diff] [blame] | 334 | protected void removeActivityFlags(int toRemove) { |
| 335 | final int oldFlags = mActivityFlags; |
| 336 | mActivityFlags &= ~toRemove; |
| 337 | if (DEBUG) { |
| 338 | Log.d(TAG, "Launcher flags updated: " + formatFlagChange(mActivityFlags, oldFlags, |
| 339 | BaseActivity::getActivityStateString)); |
| 340 | } |
| 341 | |
| 342 | onActivityFlagsChanged(toRemove); |
Sunny Goyal | 210e174 | 2019-10-17 12:05:38 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Mike Schneider | a79d460 | 2023-03-03 15:58:06 +0100 | [diff] [blame] | 345 | protected void onActivityFlagsChanged(int changeBits) { |
| 346 | } |
Sunny Goyal | 210e174 | 2019-10-17 12:05:38 -0700 | [diff] [blame] | 347 | |
Winson Chung | 1a77c3d | 2018-04-11 12:47:47 -0700 | [diff] [blame] | 348 | public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) { |
| 349 | mMultiWindowModeChangedListeners.add(listener); |
| 350 | } |
| 351 | |
| 352 | public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) { |
| 353 | mMultiWindowModeChangedListeners.remove(listener); |
| 354 | } |
| 355 | |
Sunny Goyal | f633ef5 | 2018-03-13 09:57:05 -0700 | [diff] [blame] | 356 | /** |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 357 | * Used to set the override visibility state, used only to handle the transition home with the |
| 358 | * recents animation. |
Mike Schneider | a79d460 | 2023-03-03 15:58:06 +0100 | [diff] [blame] | 359 | * |
Sunny Goyal | b65d766 | 2021-03-07 15:09:11 -0800 | [diff] [blame] | 360 | * @see QuickstepTransitionManager#createWallpaperOpenRunner |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 361 | */ |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 362 | public void addForceInvisibleFlag(@InvisibilityFlags int flag) { |
| 363 | mForceInvisible |= flag; |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame] | 366 | public void clearForceInvisibleFlag(@InvisibilityFlags int flag) { |
| 367 | mForceInvisible &= ~flag; |
| 368 | } |
| 369 | |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 370 | /** |
| 371 | * @return Wether this activity should be considered invisible regardless of actual visibility. |
| 372 | */ |
| 373 | public boolean isForceInvisible() { |
Sunny Goyal | 1c63c72 | 2018-06-05 16:00:34 -0700 | [diff] [blame] | 374 | return hasSomeInvisibleFlag(INVISIBLE_FLAGS); |
| 375 | } |
| 376 | |
| 377 | public boolean hasSomeInvisibleFlag(int mask) { |
| 378 | return (mForceInvisible & mask) != 0; |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Jon Miranda | cb58259 | 2023-04-19 15:40:04 -0700 | [diff] [blame] | 381 | /** |
Sunny Goyal | e82a20a | 2023-10-19 13:52:49 -0700 | [diff] [blame] | 382 | * Adds a callback for the provided activity event |
Jon Miranda | cb58259 | 2023-04-19 15:40:04 -0700 | [diff] [blame] | 383 | */ |
Sunny Goyal | e82a20a | 2023-10-19 13:52:49 -0700 | [diff] [blame] | 384 | public void addEventCallback(@ActivityEvent int event, Runnable callback) { |
| 385 | mEventCallbacks[event].add(callback); |
| 386 | } |
| 387 | |
| 388 | /** Removes a previously added callback */ |
| 389 | public void removeEventCallback(@ActivityEvent int event, Runnable callback) { |
| 390 | mEventCallbacks[event].remove(callback); |
Jon Miranda | cb58259 | 2023-04-19 15:40:04 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Winson Chung | 1a77c3d | 2018-04-11 12:47:47 -0700 | [diff] [blame] | 393 | public interface MultiWindowModeChangedListener { |
| 394 | void onMultiWindowModeChanged(boolean isInMultiWindowMode); |
| 395 | } |
Sunny Goyal | e43d00d | 2018-05-14 14:23:18 -0700 | [diff] [blame] | 396 | |
Winson Chung | ef52876 | 2019-09-06 12:05:52 -0700 | [diff] [blame] | 397 | protected void dumpMisc(String prefix, PrintWriter writer) { |
| 398 | writer.println(prefix + "deviceProfile isTransposed=" |
| 399 | + getDeviceProfile().isVerticalBarLayout()); |
| 400 | writer.println(prefix + "orientation=" + getResources().getConfiguration().orientation); |
| 401 | writer.println(prefix + "mSystemUiController: " + mSystemUiController); |
Mike Schneider | a79d460 | 2023-03-03 15:58:06 +0100 | [diff] [blame] | 402 | writer.println(prefix + "mActivityFlags: " + getActivityStateString(mActivityFlags)); |
Winson Chung | ef52876 | 2019-09-06 12:05:52 -0700 | [diff] [blame] | 403 | writer.println(prefix + "mForceInvisible: " + mForceInvisible); |
Sunny Goyal | e43d00d | 2018-05-14 14:23:18 -0700 | [diff] [blame] | 404 | } |
Sunny Goyal | 87b5eb6 | 2018-07-03 15:53:39 -0700 | [diff] [blame] | 405 | |
| 406 | public static <T extends BaseActivity> T fromContext(Context context) { |
| 407 | if (context instanceof BaseActivity) { |
| 408 | return (T) context; |
Fengjiang Li | b2d0845 | 2024-04-25 15:51:09 -0700 | [diff] [blame] | 409 | } else if (context instanceof ActivityContextDelegate) { |
| 410 | return (T) ((ActivityContextDelegate) context).mDelegate; |
Tony Wickham | 1906cc3 | 2021-02-11 11:55:24 -0800 | [diff] [blame] | 411 | } else if (context instanceof ContextWrapper) { |
Sunny Goyal | 87b5eb6 | 2018-07-03 15:53:39 -0700 | [diff] [blame] | 412 | return fromContext(((ContextWrapper) context).getBaseContext()); |
| 413 | } else { |
| 414 | throw new IllegalArgumentException("Cannot find BaseActivity in parent tree"); |
| 415 | } |
| 416 | } |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 417 | } |