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