Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | 35c7b19 | 2021-04-20 16:51:10 -0700 | [diff] [blame] | 19 | import static com.android.launcher3.util.DisplayController.CHANGE_ROTATION; |
Sunny Goyal | 9f56a2f | 2020-02-03 14:22:09 -0800 | [diff] [blame] | 20 | |
Alex Chau | fd6d942 | 2021-04-22 19:10:21 +0100 | [diff] [blame] | 21 | import android.content.Context; |
Lucas Dupin | eca08a1 | 2018-08-11 15:53:40 -0700 | [diff] [blame] | 22 | import android.content.res.Configuration; |
Sunny Goyal | 0addbf0 | 2020-04-28 14:17:35 -0700 | [diff] [blame] | 23 | import android.graphics.Point; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 24 | import android.graphics.Rect; |
| 25 | import android.os.Bundle; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 26 | import android.view.ActionMode; |
Sunny Goyal | 0addbf0 | 2020-04-28 14:17:35 -0700 | [diff] [blame] | 27 | import android.view.Display; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 28 | import android.view.View; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 29 | |
Stefan Andonian | 24a963b | 2023-05-05 20:25:34 +0000 | [diff] [blame^] | 30 | import androidx.annotation.MainThread; |
Sunny Goyal | b65d766 | 2021-03-07 15:09:11 -0800 | [diff] [blame] | 31 | import androidx.annotation.NonNull; |
Winson Chung | 13c1c2c | 2019-09-06 11:46:19 -0700 | [diff] [blame] | 32 | import androidx.annotation.Nullable; |
| 33 | |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame] | 34 | import com.android.launcher3.model.data.ItemInfo; |
Sunny Goyal | 9c2b960 | 2020-01-07 13:07:55 -0800 | [diff] [blame] | 35 | import com.android.launcher3.touch.ItemClickHandler; |
Sunny Goyal | b65d766 | 2021-03-07 15:09:11 -0800 | [diff] [blame] | 36 | import com.android.launcher3.util.ActivityOptionsWrapper; |
Sunny Goyal | fd58da6 | 2020-08-11 12:06:49 -0700 | [diff] [blame] | 37 | import com.android.launcher3.util.DisplayController; |
| 38 | import com.android.launcher3.util.DisplayController.DisplayInfoChangeListener; |
| 39 | import com.android.launcher3.util.DisplayController.Info; |
Stefan Andonian | 24a963b | 2023-05-05 20:25:34 +0000 | [diff] [blame^] | 40 | import com.android.launcher3.util.OnColorHintListener; |
Sunny Goyal | b65d766 | 2021-03-07 15:09:11 -0800 | [diff] [blame] | 41 | import com.android.launcher3.util.RunnableList; |
Hyunyoung Song | fcd090d | 2019-05-01 13:15:29 -0700 | [diff] [blame] | 42 | import com.android.launcher3.util.Themes; |
Sunny Goyal | 17c72fb | 2019-10-14 14:06:38 -0700 | [diff] [blame] | 43 | import com.android.launcher3.util.TraceHelper; |
Stefan Andonian | 24a963b | 2023-05-05 20:25:34 +0000 | [diff] [blame^] | 44 | import com.android.launcher3.util.WallpaperColorHints; |
Sunny Goyal | b46703d | 2020-05-27 17:52:03 -0700 | [diff] [blame] | 45 | import com.android.launcher3.util.WindowBounds; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Extension of BaseActivity allowing support for drag-n-drop |
| 49 | */ |
Sunny Goyal | 4ed0fb5 | 2021-04-26 09:52:47 -0700 | [diff] [blame] | 50 | @SuppressWarnings("NewApi") |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 51 | public abstract class BaseDraggingActivity extends BaseActivity |
Stefan Andonian | 24a963b | 2023-05-05 20:25:34 +0000 | [diff] [blame^] | 52 | implements OnColorHintListener, DisplayInfoChangeListener { |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 53 | |
| 54 | private static final String TAG = "BaseDraggingActivity"; |
| 55 | |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 56 | // When starting an action mode, setting this tag will cause the action mode to be cancelled |
| 57 | // automatically when user interacts with the launcher. |
| 58 | public static final Object AUTO_CANCEL_ACTION_MODE = new Object(); |
| 59 | |
| 60 | private ActionMode mCurrentActionMode; |
| 61 | protected boolean mIsSafeModeEnabled; |
| 62 | |
Winson Chung | a19a2b7 | 2019-10-14 16:30:38 -0700 | [diff] [blame] | 63 | private Runnable mOnStartCallback; |
Stefan Andonian | 24a963b | 2023-05-05 20:25:34 +0000 | [diff] [blame^] | 64 | private final RunnableList mOnResumeCallbacks = new RunnableList(); |
Sunny Goyal | bd88f39 | 2018-06-07 15:42:57 -0700 | [diff] [blame] | 65 | private int mThemeRes = R.style.AppTheme; |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 66 | |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 67 | @Override |
| 68 | protected void onCreate(Bundle savedInstanceState) { |
| 69 | super.onCreate(savedInstanceState); |
Sunny Goyal | 17c72fb | 2019-10-14 14:06:38 -0700 | [diff] [blame] | 70 | |
Sunny Goyal | 5d09b2e | 2020-07-09 12:21:40 -0700 | [diff] [blame] | 71 | mIsSafeModeEnabled = TraceHelper.allowIpcs("isSafeMode", |
Sunny Goyal | 17c72fb | 2019-10-14 14:06:38 -0700 | [diff] [blame] | 72 | () -> getPackageManager().isSafeMode()); |
Sunny Goyal | 35c7b19 | 2021-04-20 16:51:10 -0700 | [diff] [blame] | 73 | DisplayController.INSTANCE.get(this).addChangeListener(this); |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 74 | |
| 75 | // Update theme |
Stefan Andonian | 24a963b | 2023-05-05 20:25:34 +0000 | [diff] [blame^] | 76 | WallpaperColorHints.get(this).registerOnColorHintsChangedListener(this); |
Hyunyoung Song | fcd090d | 2019-05-01 13:15:29 -0700 | [diff] [blame] | 77 | int themeRes = Themes.getActivityThemeRes(this); |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 78 | if (themeRes != mThemeRes) { |
| 79 | mThemeRes = themeRes; |
| 80 | setTheme(themeRes); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | @Override |
Sunny Goyal | b65d766 | 2021-03-07 15:09:11 -0800 | [diff] [blame] | 85 | protected void onResume() { |
| 86 | super.onResume(); |
| 87 | mOnResumeCallbacks.executeAllAndClear(); |
| 88 | } |
| 89 | |
| 90 | public void addOnResumeCallback(Runnable callback) { |
| 91 | mOnResumeCallbacks.add(callback); |
| 92 | } |
| 93 | |
Stefan Andonian | 24a963b | 2023-05-05 20:25:34 +0000 | [diff] [blame^] | 94 | @MainThread |
Sunny Goyal | b65d766 | 2021-03-07 15:09:11 -0800 | [diff] [blame] | 95 | @Override |
Stefan Andonian | 24a963b | 2023-05-05 20:25:34 +0000 | [diff] [blame^] | 96 | public void onColorHintsChanged(int colorHints) { |
Lucas Dupin | eca08a1 | 2018-08-11 15:53:40 -0700 | [diff] [blame] | 97 | updateTheme(); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void onConfigurationChanged(Configuration newConfig) { |
| 102 | super.onConfigurationChanged(newConfig); |
| 103 | updateTheme(); |
| 104 | } |
| 105 | |
| 106 | private void updateTheme() { |
Hyunyoung Song | fcd090d | 2019-05-01 13:15:29 -0700 | [diff] [blame] | 107 | if (mThemeRes != Themes.getActivityThemeRes(this)) { |
Winson Chung | c4bef35 | 2020-10-28 00:13:03 -0700 | [diff] [blame] | 108 | recreate(); |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 112 | @Override |
| 113 | public void onActionModeStarted(ActionMode mode) { |
| 114 | super.onActionModeStarted(mode); |
| 115 | mCurrentActionMode = mode; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void onActionModeFinished(ActionMode mode) { |
| 120 | super.onActionModeFinished(mode); |
| 121 | mCurrentActionMode = null; |
| 122 | } |
| 123 | |
Fengjiang Li | 6bb8d79 | 2023-01-12 16:20:58 -0800 | [diff] [blame] | 124 | protected boolean isInAutoCancelActionMode() { |
| 125 | return mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag(); |
| 126 | } |
| 127 | |
Sunny Goyal | 87b5eb6 | 2018-07-03 15:53:39 -0700 | [diff] [blame] | 128 | @Override |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 129 | public boolean finishAutoCancelActionMode() { |
Fengjiang Li | 6bb8d79 | 2023-01-12 16:20:58 -0800 | [diff] [blame] | 130 | if (isInAutoCancelActionMode()) { |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 131 | mCurrentActionMode.finish(); |
| 132 | return true; |
| 133 | } |
| 134 | return false; |
| 135 | } |
| 136 | |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 137 | public abstract <T extends View> T getOverviewPanel(); |
| 138 | |
Sunny Goyal | 9d69c8d | 2018-03-19 13:41:31 -0700 | [diff] [blame] | 139 | public abstract View getRootView(); |
| 140 | |
Vinit Nayak | f9b585b | 2019-07-10 14:25:32 -0700 | [diff] [blame] | 141 | public void returnToHomescreen() { |
| 142 | // no-op |
| 143 | } |
| 144 | |
Brian Isganitis | bde3c8b | 2022-03-15 13:35:06 -0700 | [diff] [blame] | 145 | @Override |
Sunny Goyal | b65d766 | 2021-03-07 15:09:11 -0800 | [diff] [blame] | 146 | @NonNull |
Winson Chung | 2b09394 | 2021-04-09 14:00:25 -0700 | [diff] [blame] | 147 | public ActivityOptionsWrapper getActivityLaunchOptions(View v, @Nullable ItemInfo item) { |
Brian Isganitis | bde3c8b | 2022-03-15 13:35:06 -0700 | [diff] [blame] | 148 | ActivityOptionsWrapper wrapper = super.getActivityLaunchOptions(v, item); |
| 149 | addOnResumeCallback(wrapper.onEndCallback::executeAllAndDestroy); |
| 150 | return wrapper; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 151 | } |
Sunny Goyal | 9d69c8d | 2018-03-19 13:41:31 -0700 | [diff] [blame] | 152 | |
| 153 | @Override |
Sunny Goyal | 4fdc918 | 2023-05-12 12:08:53 -0700 | [diff] [blame] | 154 | public ActivityOptionsWrapper makeDefaultActivityOptions(int splashScreenStyle) { |
| 155 | ActivityOptionsWrapper wrapper = super.makeDefaultActivityOptions(splashScreenStyle); |
| 156 | addOnResumeCallback(wrapper.onEndCallback::executeAllAndDestroy); |
| 157 | return wrapper; |
| 158 | } |
| 159 | |
| 160 | @Override |
Sunny Goyal | 9d69c8d | 2018-03-19 13:41:31 -0700 | [diff] [blame] | 161 | protected void onStart() { |
| 162 | super.onStart(); |
| 163 | |
| 164 | if (mOnStartCallback != null) { |
Winson Chung | a19a2b7 | 2019-10-14 16:30:38 -0700 | [diff] [blame] | 165 | mOnStartCallback.run(); |
Sunny Goyal | 9d69c8d | 2018-03-19 13:41:31 -0700 | [diff] [blame] | 166 | mOnStartCallback = null; |
| 167 | } |
| 168 | } |
| 169 | |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 170 | @Override |
| 171 | protected void onDestroy() { |
| 172 | super.onDestroy(); |
Sunny Goyal | 35c7b19 | 2021-04-20 16:51:10 -0700 | [diff] [blame] | 173 | DisplayController.INSTANCE.get(this).removeChangeListener(this); |
Stefan Andonian | 24a963b | 2023-05-05 20:25:34 +0000 | [diff] [blame^] | 174 | WallpaperColorHints.get(this).unregisterOnColorsChangedListener(this); |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Winson Chung | a19a2b7 | 2019-10-14 16:30:38 -0700 | [diff] [blame] | 177 | public void runOnceOnStart(Runnable action) { |
| 178 | mOnStartCallback = action; |
| 179 | } |
| 180 | |
| 181 | public void clearRunOnceOnStartCallback() { |
| 182 | mOnStartCallback = null; |
Sunny Goyal | 9d69c8d | 2018-03-19 13:41:31 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Sunny Goyal | 59d086c | 2018-05-07 17:31:40 -0700 | [diff] [blame] | 185 | protected void onDeviceProfileInitiated() { |
| 186 | if (mDeviceProfile.isVerticalBarLayout()) { |
Winson Chung | 13c1c2c | 2019-09-06 11:46:19 -0700 | [diff] [blame] | 187 | mDeviceProfile.updateIsSeascape(this); |
Sunny Goyal | 59d086c | 2018-05-07 17:31:40 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Sunny Goyal | 9f56a2f | 2020-02-03 14:22:09 -0800 | [diff] [blame] | 191 | @Override |
Alex Chau | fd6d942 | 2021-04-22 19:10:21 +0100 | [diff] [blame] | 192 | public void onDisplayInfoChanged(Context context, Info info, int flags) { |
Sunny Goyal | 9f56a2f | 2020-02-03 14:22:09 -0800 | [diff] [blame] | 193 | if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) { |
Sunny Goyal | 59d086c | 2018-05-07 17:31:40 -0700 | [diff] [blame] | 194 | reapplyUi(); |
| 195 | } |
| 196 | } |
| 197 | |
Schneider Victor-tulias | 16e04e2 | 2021-10-15 14:43:54 -0700 | [diff] [blame] | 198 | @Override |
| 199 | public View.OnClickListener getItemOnClickListener() { |
Sunny Goyal | 9c2b960 | 2020-01-07 13:07:55 -0800 | [diff] [blame] | 200 | return ItemClickHandler.INSTANCE; |
| 201 | } |
| 202 | |
Sunny Goyal | 59d086c | 2018-05-07 17:31:40 -0700 | [diff] [blame] | 203 | protected abstract void reapplyUi(); |
Sunny Goyal | 0addbf0 | 2020-04-28 14:17:35 -0700 | [diff] [blame] | 204 | |
Sunny Goyal | b46703d | 2020-05-27 17:52:03 -0700 | [diff] [blame] | 205 | protected WindowBounds getMultiWindowDisplaySize() { |
Sunny Goyal | 0addbf0 | 2020-04-28 14:17:35 -0700 | [diff] [blame] | 206 | if (Utilities.ATLEAST_R) { |
Sunny Goyal | 187b16c | 2022-03-01 16:53:23 -0800 | [diff] [blame] | 207 | return WindowBounds.fromWindowMetrics(getWindowManager().getCurrentWindowMetrics()); |
Sunny Goyal | 0addbf0 | 2020-04-28 14:17:35 -0700 | [diff] [blame] | 208 | } |
| 209 | // Note: Calls to getSize() can't rely on our cached DefaultDisplay since it can return |
| 210 | // the app window size |
| 211 | Display display = getWindowManager().getDefaultDisplay(); |
| 212 | Point mwSize = new Point(); |
| 213 | display.getSize(mwSize); |
Sunny Goyal | b46703d | 2020-05-27 17:52:03 -0700 | [diff] [blame] | 214 | return new WindowBounds(new Rect(0, 0, mwSize.x, mwSize.y), new Rect()); |
Sunny Goyal | 0addbf0 | 2020-04-28 14:17:35 -0700 | [diff] [blame] | 215 | } |
Samuel Fufa | fd58d23 | 2021-01-12 12:59:39 -0600 | [diff] [blame] | 216 | |
Brian Isganitis | bde3c8b | 2022-03-15 13:35:06 -0700 | [diff] [blame] | 217 | @Override |
| 218 | public boolean isAppBlockedForSafeMode() { |
| 219 | return mIsSafeModeEnabled; |
| 220 | } |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 221 | } |