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 | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 23 | import android.os.Bundle; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 24 | import android.view.ActionMode; |
| 25 | import android.view.View; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 26 | |
Stefan Andonian | 24a963b | 2023-05-05 20:25:34 +0000 | [diff] [blame] | 27 | import androidx.annotation.MainThread; |
Sunny Goyal | b65d766 | 2021-03-07 15:09:11 -0800 | [diff] [blame] | 28 | import androidx.annotation.NonNull; |
Winson Chung | 13c1c2c | 2019-09-06 11:46:19 -0700 | [diff] [blame] | 29 | import androidx.annotation.Nullable; |
| 30 | |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame] | 31 | import com.android.launcher3.model.data.ItemInfo; |
Sunny Goyal | 9c2b960 | 2020-01-07 13:07:55 -0800 | [diff] [blame] | 32 | import com.android.launcher3.touch.ItemClickHandler; |
Sunny Goyal | b65d766 | 2021-03-07 15:09:11 -0800 | [diff] [blame] | 33 | import com.android.launcher3.util.ActivityOptionsWrapper; |
Sunny Goyal | fd58da6 | 2020-08-11 12:06:49 -0700 | [diff] [blame] | 34 | import com.android.launcher3.util.DisplayController; |
| 35 | import com.android.launcher3.util.DisplayController.DisplayInfoChangeListener; |
| 36 | import com.android.launcher3.util.DisplayController.Info; |
Stefan Andonian | 24a963b | 2023-05-05 20:25:34 +0000 | [diff] [blame] | 37 | import com.android.launcher3.util.OnColorHintListener; |
Hyunyoung Song | fcd090d | 2019-05-01 13:15:29 -0700 | [diff] [blame] | 38 | import com.android.launcher3.util.Themes; |
Stefan Andonian | 24a963b | 2023-05-05 20:25:34 +0000 | [diff] [blame] | 39 | import com.android.launcher3.util.WallpaperColorHints; |
Sunny Goyal | b46703d | 2020-05-27 17:52:03 -0700 | [diff] [blame] | 40 | import com.android.launcher3.util.WindowBounds; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Extension of BaseActivity allowing support for drag-n-drop |
| 44 | */ |
Sunny Goyal | 4ed0fb5 | 2021-04-26 09:52:47 -0700 | [diff] [blame] | 45 | @SuppressWarnings("NewApi") |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 46 | public abstract class BaseDraggingActivity extends BaseActivity |
Stefan Andonian | 24a963b | 2023-05-05 20:25:34 +0000 | [diff] [blame] | 47 | implements OnColorHintListener, DisplayInfoChangeListener { |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 48 | |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 49 | // When starting an action mode, setting this tag will cause the action mode to be cancelled |
| 50 | // automatically when user interacts with the launcher. |
| 51 | public static final Object AUTO_CANCEL_ACTION_MODE = new Object(); |
| 52 | |
| 53 | private ActionMode mCurrentActionMode; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 54 | |
Sunny Goyal | bd88f39 | 2018-06-07 15:42:57 -0700 | [diff] [blame] | 55 | private int mThemeRes = R.style.AppTheme; |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 56 | |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 57 | @Override |
| 58 | protected void onCreate(Bundle savedInstanceState) { |
| 59 | super.onCreate(savedInstanceState); |
Sunny Goyal | 35c7b19 | 2021-04-20 16:51:10 -0700 | [diff] [blame] | 60 | DisplayController.INSTANCE.get(this).addChangeListener(this); |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 61 | |
| 62 | // Update theme |
Stefan Andonian | 24a963b | 2023-05-05 20:25:34 +0000 | [diff] [blame] | 63 | WallpaperColorHints.get(this).registerOnColorHintsChangedListener(this); |
Hyunyoung Song | fcd090d | 2019-05-01 13:15:29 -0700 | [diff] [blame] | 64 | int themeRes = Themes.getActivityThemeRes(this); |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 65 | if (themeRes != mThemeRes) { |
| 66 | mThemeRes = themeRes; |
| 67 | setTheme(themeRes); |
| 68 | } |
| 69 | } |
| 70 | |
Stefan Andonian | 24a963b | 2023-05-05 20:25:34 +0000 | [diff] [blame] | 71 | @MainThread |
Sunny Goyal | b65d766 | 2021-03-07 15:09:11 -0800 | [diff] [blame] | 72 | @Override |
Stefan Andonian | 24a963b | 2023-05-05 20:25:34 +0000 | [diff] [blame] | 73 | public void onColorHintsChanged(int colorHints) { |
Lucas Dupin | eca08a1 | 2018-08-11 15:53:40 -0700 | [diff] [blame] | 74 | updateTheme(); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void onConfigurationChanged(Configuration newConfig) { |
| 79 | super.onConfigurationChanged(newConfig); |
| 80 | updateTheme(); |
| 81 | } |
| 82 | |
| 83 | private void updateTheme() { |
Hyunyoung Song | fcd090d | 2019-05-01 13:15:29 -0700 | [diff] [blame] | 84 | if (mThemeRes != Themes.getActivityThemeRes(this)) { |
Winson Chung | c4bef35 | 2020-10-28 00:13:03 -0700 | [diff] [blame] | 85 | recreate(); |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 89 | @Override |
| 90 | public void onActionModeStarted(ActionMode mode) { |
| 91 | super.onActionModeStarted(mode); |
| 92 | mCurrentActionMode = mode; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void onActionModeFinished(ActionMode mode) { |
| 97 | super.onActionModeFinished(mode); |
| 98 | mCurrentActionMode = null; |
| 99 | } |
| 100 | |
Fengjiang Li | 6bb8d79 | 2023-01-12 16:20:58 -0800 | [diff] [blame] | 101 | protected boolean isInAutoCancelActionMode() { |
| 102 | return mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag(); |
| 103 | } |
| 104 | |
Sunny Goyal | 87b5eb6 | 2018-07-03 15:53:39 -0700 | [diff] [blame] | 105 | @Override |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 106 | public boolean finishAutoCancelActionMode() { |
Fengjiang Li | 6bb8d79 | 2023-01-12 16:20:58 -0800 | [diff] [blame] | 107 | if (isInAutoCancelActionMode()) { |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 108 | mCurrentActionMode.finish(); |
| 109 | return true; |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | |
Sunny Goyal | 9d69c8d | 2018-03-19 13:41:31 -0700 | [diff] [blame] | 114 | public abstract View getRootView(); |
| 115 | |
Vinit Nayak | f9b585b | 2019-07-10 14:25:32 -0700 | [diff] [blame] | 116 | public void returnToHomescreen() { |
| 117 | // no-op |
| 118 | } |
| 119 | |
Brian Isganitis | bde3c8b | 2022-03-15 13:35:06 -0700 | [diff] [blame] | 120 | @Override |
Sunny Goyal | b65d766 | 2021-03-07 15:09:11 -0800 | [diff] [blame] | 121 | @NonNull |
Winson Chung | 2b09394 | 2021-04-09 14:00:25 -0700 | [diff] [blame] | 122 | public ActivityOptionsWrapper getActivityLaunchOptions(View v, @Nullable ItemInfo item) { |
Brian Isganitis | bde3c8b | 2022-03-15 13:35:06 -0700 | [diff] [blame] | 123 | ActivityOptionsWrapper wrapper = super.getActivityLaunchOptions(v, item); |
Sunny Goyal | e82a20a | 2023-10-19 13:52:49 -0700 | [diff] [blame] | 124 | addEventCallback(EVENT_RESUMED, wrapper.onEndCallback::executeAllAndDestroy); |
Brian Isganitis | bde3c8b | 2022-03-15 13:35:06 -0700 | [diff] [blame] | 125 | return wrapper; |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 126 | } |
Sunny Goyal | 9d69c8d | 2018-03-19 13:41:31 -0700 | [diff] [blame] | 127 | |
| 128 | @Override |
Sunny Goyal | 4fdc918 | 2023-05-12 12:08:53 -0700 | [diff] [blame] | 129 | public ActivityOptionsWrapper makeDefaultActivityOptions(int splashScreenStyle) { |
| 130 | ActivityOptionsWrapper wrapper = super.makeDefaultActivityOptions(splashScreenStyle); |
Sunny Goyal | e82a20a | 2023-10-19 13:52:49 -0700 | [diff] [blame] | 131 | addEventCallback(EVENT_RESUMED, wrapper.onEndCallback::executeAllAndDestroy); |
Sunny Goyal | 4fdc918 | 2023-05-12 12:08:53 -0700 | [diff] [blame] | 132 | return wrapper; |
| 133 | } |
| 134 | |
| 135 | @Override |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 136 | protected void onDestroy() { |
| 137 | super.onDestroy(); |
Sunny Goyal | 35c7b19 | 2021-04-20 16:51:10 -0700 | [diff] [blame] | 138 | DisplayController.INSTANCE.get(this).removeChangeListener(this); |
Stefan Andonian | 24a963b | 2023-05-05 20:25:34 +0000 | [diff] [blame] | 139 | WallpaperColorHints.get(this).unregisterOnColorsChangedListener(this); |
Sunny Goyal | ab83773 | 2018-03-27 17:35:54 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Sunny Goyal | 59d086c | 2018-05-07 17:31:40 -0700 | [diff] [blame] | 142 | protected void onDeviceProfileInitiated() { |
Sunny Goyal | 59d086c | 2018-05-07 17:31:40 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Sunny Goyal | 9f56a2f | 2020-02-03 14:22:09 -0800 | [diff] [blame] | 145 | @Override |
Alex Chau | fd6d942 | 2021-04-22 19:10:21 +0100 | [diff] [blame] | 146 | public void onDisplayInfoChanged(Context context, Info info, int flags) { |
Sihua Ma | a44f4ac | 2024-06-03 18:35:39 +0000 | [diff] [blame] | 147 | if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.isVerticalBarLayout()) { |
Sunny Goyal | 59d086c | 2018-05-07 17:31:40 -0700 | [diff] [blame] | 148 | reapplyUi(); |
| 149 | } |
| 150 | } |
| 151 | |
Schneider Victor-tulias | 16e04e2 | 2021-10-15 14:43:54 -0700 | [diff] [blame] | 152 | @Override |
| 153 | public View.OnClickListener getItemOnClickListener() { |
Sunny Goyal | 9c2b960 | 2020-01-07 13:07:55 -0800 | [diff] [blame] | 154 | return ItemClickHandler.INSTANCE; |
| 155 | } |
| 156 | |
Sunny Goyal | 59d086c | 2018-05-07 17:31:40 -0700 | [diff] [blame] | 157 | protected abstract void reapplyUi(); |
Sunny Goyal | 0addbf0 | 2020-04-28 14:17:35 -0700 | [diff] [blame] | 158 | |
Sunny Goyal | b46703d | 2020-05-27 17:52:03 -0700 | [diff] [blame] | 159 | protected WindowBounds getMultiWindowDisplaySize() { |
Andy Wickham | b922dcc | 2024-01-24 14:38:48 -0800 | [diff] [blame] | 160 | return WindowBounds.fromWindowMetrics(getWindowManager().getCurrentWindowMetrics()); |
Sunny Goyal | 0addbf0 | 2020-04-28 14:17:35 -0700 | [diff] [blame] | 161 | } |
Samuel Fufa | fd58d23 | 2021-01-12 12:59:39 -0600 | [diff] [blame] | 162 | |
Brian Isganitis | bde3c8b | 2022-03-15 13:35:06 -0700 | [diff] [blame] | 163 | @Override |
| 164 | public boolean isAppBlockedForSafeMode() { |
Sunny Goyal | 31e27ed | 2024-01-09 15:45:14 -0800 | [diff] [blame] | 165 | return LauncherAppState.getInstance(this).isSafeModeEnabled(); |
Brian Isganitis | bde3c8b | 2022-03-15 13:35:06 -0700 | [diff] [blame] | 166 | } |
Sunny Goyal | 0b0847b | 2018-03-14 12:30:11 -0700 | [diff] [blame] | 167 | } |