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