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