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