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