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