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 | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 59 | private boolean mStarted; |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame^] | 60 | private boolean mUserActive; |
| 61 | |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 62 | // When the recents animation is running, the visibility of the Launcher is managed by the |
| 63 | // animation |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame^] | 64 | @InvisibilityFlags private int mForceInvisible; |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 65 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 66 | public DeviceProfile getDeviceProfile() { |
| 67 | return mDeviceProfile; |
| 68 | } |
| 69 | |
| 70 | public AccessibilityDelegate getAccessibilityDelegate() { |
| 71 | return null; |
| 72 | } |
| 73 | |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 74 | public final UserEventDispatcher getUserEventDispatcher() { |
| 75 | if (mUserEventDispatcher == null) { |
Sunny Goyal | d70e75a | 2018-02-22 10:07:32 -0800 | [diff] [blame] | 76 | mUserEventDispatcher = UserEventDispatcher.newInstance(this, mDeviceProfile); |
Sunny Goyal | a535ae4 | 2017-02-27 10:07:13 -0800 | [diff] [blame] | 77 | } |
| 78 | return mUserEventDispatcher; |
| 79 | } |
| 80 | |
Jon Miranda | fe96432 | 2017-03-22 10:25:17 -0700 | [diff] [blame] | 81 | public boolean isInMultiWindowModeCompat() { |
| 82 | return Utilities.ATLEAST_NOUGAT && isInMultiWindowMode(); |
| 83 | } |
| 84 | |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 85 | public static BaseActivity fromContext(Context context) { |
| 86 | if (context instanceof BaseActivity) { |
| 87 | return (BaseActivity) context; |
| 88 | } |
| 89 | return ((BaseActivity) ((ContextWrapper) context).getBaseContext()); |
| 90 | } |
Sunny Goyal | 8392c82 | 2017-06-20 10:03:56 -0700 | [diff] [blame] | 91 | |
| 92 | public SystemUiController getSystemUiController() { |
| 93 | if (mSystemUiController == null) { |
| 94 | mSystemUiController = new SystemUiController(getWindow()); |
| 95 | } |
| 96 | return mSystemUiController; |
| 97 | } |
Sunny Goyal | 64a75aa | 2017-07-03 13:50:52 -0700 | [diff] [blame] | 98 | |
| 99 | @Override |
| 100 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 101 | super.onActivityResult(requestCode, resultCode, data); |
| 102 | } |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 103 | |
| 104 | @Override |
| 105 | protected void onStart() { |
| 106 | mStarted = true; |
| 107 | super.onStart(); |
| 108 | } |
| 109 | |
| 110 | @Override |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 111 | protected void onResume() { |
| 112 | mUserActive = true; |
| 113 | super.onResume(); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | protected void onUserLeaveHint() { |
| 118 | mUserActive = false; |
| 119 | super.onUserLeaveHint(); |
| 120 | } |
| 121 | |
| 122 | @Override |
Winson Chung | 1a77c3d | 2018-04-11 12:47:47 -0700 | [diff] [blame] | 123 | public void onMultiWindowModeChanged(boolean isInMultiWindowMode, Configuration newConfig) { |
| 124 | super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig); |
| 125 | for (int i = mMultiWindowModeChangedListeners.size() - 1; i >= 0; i--) { |
| 126 | mMultiWindowModeChangedListeners.get(i).onMultiWindowModeChanged(isInMultiWindowMode); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @Override |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 131 | protected void onStop() { |
| 132 | mStarted = false; |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame^] | 133 | mForceInvisible = 0; |
Sunny Goyal | cc96aa1 | 2018-01-11 09:56:07 -0800 | [diff] [blame] | 134 | super.onStop(); |
| 135 | } |
| 136 | |
| 137 | public boolean isStarted() { |
| 138 | return mStarted; |
| 139 | } |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 140 | |
Tracy Zhou | a706f00 | 2018-03-28 13:55:19 -0700 | [diff] [blame] | 141 | public boolean isUserActive() { |
| 142 | return mUserActive; |
| 143 | } |
| 144 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 145 | public void addOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) { |
| 146 | mDPChangeListeners.add(listener); |
| 147 | } |
| 148 | |
Winson Chung | 8a968fa | 2018-03-15 17:59:04 -0700 | [diff] [blame] | 149 | public void removeOnDeviceProfileChangeListener(OnDeviceProfileChangeListener listener) { |
| 150 | mDPChangeListeners.remove(listener); |
| 151 | } |
| 152 | |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 153 | protected void dispatchDeviceProfileChanged() { |
Winson Chung | 8a968fa | 2018-03-15 17:59:04 -0700 | [diff] [blame] | 154 | for (int i = mDPChangeListeners.size() - 1; i >= 0; i--) { |
Sunny Goyal | fde5505 | 2018-02-01 14:46:13 -0800 | [diff] [blame] | 155 | mDPChangeListeners.get(i).onDeviceProfileChanged(mDeviceProfile); |
| 156 | } |
| 157 | } |
Sunny Goyal | f633ef5 | 2018-03-13 09:57:05 -0700 | [diff] [blame] | 158 | |
Winson Chung | 1a77c3d | 2018-04-11 12:47:47 -0700 | [diff] [blame] | 159 | public void addMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) { |
| 160 | mMultiWindowModeChangedListeners.add(listener); |
| 161 | } |
| 162 | |
| 163 | public void removeMultiWindowModeChangedListener(MultiWindowModeChangedListener listener) { |
| 164 | mMultiWindowModeChangedListeners.remove(listener); |
| 165 | } |
| 166 | |
Sunny Goyal | f633ef5 | 2018-03-13 09:57:05 -0700 | [diff] [blame] | 167 | /** |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 168 | * Used to set the override visibility state, used only to handle the transition home with the |
| 169 | * recents animation. |
| 170 | * @see LauncherAppTransitionManagerImpl.getWallpaperOpenRunner() |
| 171 | */ |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame^] | 172 | public void addForceInvisibleFlag(@InvisibilityFlags int flag) { |
| 173 | mForceInvisible |= flag; |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame^] | 176 | public void clearForceInvisibleFlag(@InvisibilityFlags int flag) { |
| 177 | mForceInvisible &= ~flag; |
| 178 | } |
| 179 | |
| 180 | |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 181 | /** |
| 182 | * @return Wether this activity should be considered invisible regardless of actual visibility. |
| 183 | */ |
| 184 | public boolean isForceInvisible() { |
Sunny Goyal | 7eff40f | 2018-04-11 15:30:46 -0700 | [diff] [blame^] | 185 | return mForceInvisible != 0; |
Winson Chung | 9800e73 | 2018-04-04 13:33:23 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
Sunny Goyal | f633ef5 | 2018-03-13 09:57:05 -0700 | [diff] [blame] | 189 | * Sets the device profile, adjusting it accordingly in case of multi-window |
| 190 | */ |
| 191 | protected void setDeviceProfile(DeviceProfile dp) { |
| 192 | mDeviceProfile = dp; |
| 193 | if (isInMultiWindowModeCompat()) { |
| 194 | Display display = getWindowManager().getDefaultDisplay(); |
| 195 | Point mwSize = new Point(); |
| 196 | display.getSize(mwSize); |
| 197 | mDeviceProfile = mDeviceProfile.getMultiWindowProfile(this, mwSize); |
| 198 | } |
| 199 | } |
Winson Chung | 1a77c3d | 2018-04-11 12:47:47 -0700 | [diff] [blame] | 200 | |
| 201 | public interface MultiWindowModeChangedListener { |
| 202 | void onMultiWindowModeChanged(boolean isInMultiWindowMode); |
| 203 | } |
Sunny Goyal | 2783595 | 2017-01-13 12:15:53 -0800 | [diff] [blame] | 204 | } |