blob: b6f66152dc8a06a083dbf3e09dd9750e76f646f0 [file] [log] [blame]
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001/*
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
17package com.android.launcher3;
18
Sunny Goyal35c7b192021-04-20 16:51:10 -070019import static com.android.launcher3.util.DisplayController.CHANGE_ROTATION;
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080020
Alex Chaufd6d9422021-04-22 19:10:21 +010021import android.content.Context;
Lucas Dupineca08a12018-08-11 15:53:40 -070022import android.content.res.Configuration;
Sunny Goyal0addbf02020-04-28 14:17:35 -070023import android.graphics.Point;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070024import android.graphics.Rect;
25import android.os.Bundle;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070026import android.view.ActionMode;
Sunny Goyal0addbf02020-04-28 14:17:35 -070027import android.view.Display;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070028import android.view.View;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070029
Stefan Andonian24a963b2023-05-05 20:25:34 +000030import androidx.annotation.MainThread;
Sunny Goyalb65d7662021-03-07 15:09:11 -080031import androidx.annotation.NonNull;
Winson Chung13c1c2c2019-09-06 11:46:19 -070032import androidx.annotation.Nullable;
33
Sunny Goyale396abf2020-04-06 15:11:17 -070034import com.android.launcher3.model.data.ItemInfo;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080035import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyalb65d7662021-03-07 15:09:11 -080036import com.android.launcher3.util.ActivityOptionsWrapper;
Sunny Goyalfd58da62020-08-11 12:06:49 -070037import com.android.launcher3.util.DisplayController;
38import com.android.launcher3.util.DisplayController.DisplayInfoChangeListener;
39import com.android.launcher3.util.DisplayController.Info;
Stefan Andonian24a963b2023-05-05 20:25:34 +000040import com.android.launcher3.util.OnColorHintListener;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070041import com.android.launcher3.util.Themes;
Stefan Andonian24a963b2023-05-05 20:25:34 +000042import com.android.launcher3.util.WallpaperColorHints;
Sunny Goyalb46703d2020-05-27 17:52:03 -070043import com.android.launcher3.util.WindowBounds;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070044
45/**
46 * Extension of BaseActivity allowing support for drag-n-drop
47 */
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070048@SuppressWarnings("NewApi")
Sunny Goyalab837732018-03-27 17:35:54 -070049public abstract class BaseDraggingActivity extends BaseActivity
Stefan Andonian24a963b2023-05-05 20:25:34 +000050 implements OnColorHintListener, DisplayInfoChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070051
Sunny Goyal0b0847b2018-03-14 12:30:11 -070052 // When starting an action mode, setting this tag will cause the action mode to be cancelled
53 // automatically when user interacts with the launcher.
54 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
55
56 private ActionMode mCurrentActionMode;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070057
Sunny Goyalbd88f392018-06-07 15:42:57 -070058 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070059
Sunny Goyal0b0847b2018-03-14 12:30:11 -070060 @Override
61 protected void onCreate(Bundle savedInstanceState) {
62 super.onCreate(savedInstanceState);
Sunny Goyal35c7b192021-04-20 16:51:10 -070063 DisplayController.INSTANCE.get(this).addChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -070064
65 // Update theme
Stefan Andonian24a963b2023-05-05 20:25:34 +000066 WallpaperColorHints.get(this).registerOnColorHintsChangedListener(this);
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070067 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070068 if (themeRes != mThemeRes) {
69 mThemeRes = themeRes;
70 setTheme(themeRes);
71 }
72 }
73
Stefan Andonian24a963b2023-05-05 20:25:34 +000074 @MainThread
Sunny Goyalb65d7662021-03-07 15:09:11 -080075 @Override
Stefan Andonian24a963b2023-05-05 20:25:34 +000076 public void onColorHintsChanged(int colorHints) {
Lucas Dupineca08a12018-08-11 15:53:40 -070077 updateTheme();
78 }
79
80 @Override
81 public void onConfigurationChanged(Configuration newConfig) {
82 super.onConfigurationChanged(newConfig);
83 updateTheme();
84 }
85
86 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070087 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Winson Chungc4bef352020-10-28 00:13:03 -070088 recreate();
Sunny Goyalab837732018-03-27 17:35:54 -070089 }
90 }
91
Sunny Goyal0b0847b2018-03-14 12:30:11 -070092 @Override
93 public void onActionModeStarted(ActionMode mode) {
94 super.onActionModeStarted(mode);
95 mCurrentActionMode = mode;
96 }
97
98 @Override
99 public void onActionModeFinished(ActionMode mode) {
100 super.onActionModeFinished(mode);
101 mCurrentActionMode = null;
102 }
103
Fengjiang Li6bb8d792023-01-12 16:20:58 -0800104 protected boolean isInAutoCancelActionMode() {
105 return mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag();
106 }
107
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700108 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700109 public boolean finishAutoCancelActionMode() {
Fengjiang Li6bb8d792023-01-12 16:20:58 -0800110 if (isInAutoCancelActionMode()) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700111 mCurrentActionMode.finish();
112 return true;
113 }
114 return false;
115 }
116
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700117 public abstract <T extends View> T getOverviewPanel();
118
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700119 public abstract View getRootView();
120
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700121 public void returnToHomescreen() {
122 // no-op
123 }
124
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700125 @Override
Sunny Goyalb65d7662021-03-07 15:09:11 -0800126 @NonNull
Winson Chung2b093942021-04-09 14:00:25 -0700127 public ActivityOptionsWrapper getActivityLaunchOptions(View v, @Nullable ItemInfo item) {
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700128 ActivityOptionsWrapper wrapper = super.getActivityLaunchOptions(v, item);
Sunny Goyale82a20a2023-10-19 13:52:49 -0700129 addEventCallback(EVENT_RESUMED, wrapper.onEndCallback::executeAllAndDestroy);
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700130 return wrapper;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700131 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700132
133 @Override
Sunny Goyal4fdc9182023-05-12 12:08:53 -0700134 public ActivityOptionsWrapper makeDefaultActivityOptions(int splashScreenStyle) {
135 ActivityOptionsWrapper wrapper = super.makeDefaultActivityOptions(splashScreenStyle);
Sunny Goyale82a20a2023-10-19 13:52:49 -0700136 addEventCallback(EVENT_RESUMED, wrapper.onEndCallback::executeAllAndDestroy);
Sunny Goyal4fdc9182023-05-12 12:08:53 -0700137 return wrapper;
138 }
139
140 @Override
Sunny Goyalab837732018-03-27 17:35:54 -0700141 protected void onDestroy() {
142 super.onDestroy();
Sunny Goyal35c7b192021-04-20 16:51:10 -0700143 DisplayController.INSTANCE.get(this).removeChangeListener(this);
Stefan Andonian24a963b2023-05-05 20:25:34 +0000144 WallpaperColorHints.get(this).unregisterOnColorsChangedListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700145 }
146
Sunny Goyal59d086c2018-05-07 17:31:40 -0700147 protected void onDeviceProfileInitiated() {
148 if (mDeviceProfile.isVerticalBarLayout()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700149 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700150 }
151 }
152
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800153 @Override
Alex Chaufd6d9422021-04-22 19:10:21 +0100154 public void onDisplayInfoChanged(Context context, Info info, int flags) {
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800155 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700156 reapplyUi();
157 }
158 }
159
Schneider Victor-tulias16e04e22021-10-15 14:43:54 -0700160 @Override
161 public View.OnClickListener getItemOnClickListener() {
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800162 return ItemClickHandler.INSTANCE;
163 }
164
Sunny Goyal59d086c2018-05-07 17:31:40 -0700165 protected abstract void reapplyUi();
Sunny Goyal0addbf02020-04-28 14:17:35 -0700166
Sunny Goyalb46703d2020-05-27 17:52:03 -0700167 protected WindowBounds getMultiWindowDisplaySize() {
Sunny Goyal0addbf02020-04-28 14:17:35 -0700168 if (Utilities.ATLEAST_R) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800169 return WindowBounds.fromWindowMetrics(getWindowManager().getCurrentWindowMetrics());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700170 }
171 // Note: Calls to getSize() can't rely on our cached DefaultDisplay since it can return
172 // the app window size
173 Display display = getWindowManager().getDefaultDisplay();
174 Point mwSize = new Point();
175 display.getSize(mwSize);
Sunny Goyalb46703d2020-05-27 17:52:03 -0700176 return new WindowBounds(new Rect(0, 0, mwSize.x, mwSize.y), new Rect());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700177 }
Samuel Fufafd58d232021-01-12 12:59:39 -0600178
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700179 @Override
180 public boolean isAppBlockedForSafeMode() {
Sunny Goyal31e27ed2024-01-09 15:45:14 -0800181 return LauncherAppState.getInstance(this).isSafeModeEnabled();
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700182 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700183}