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