blob: 1c2ed4343d6611e49dcd69a4b416c6a1157801ee [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 Goyal0b0847b2018-03-14 12:30:11 -070023import android.os.Bundle;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070024import android.view.ActionMode;
25import android.view.View;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070026
Stefan Andonian24a963b2023-05-05 20:25:34 +000027import androidx.annotation.MainThread;
Sunny Goyalb65d7662021-03-07 15:09:11 -080028import androidx.annotation.NonNull;
Winson Chung13c1c2c2019-09-06 11:46:19 -070029import androidx.annotation.Nullable;
30
Sunny Goyale396abf2020-04-06 15:11:17 -070031import com.android.launcher3.model.data.ItemInfo;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080032import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyalb65d7662021-03-07 15:09:11 -080033import com.android.launcher3.util.ActivityOptionsWrapper;
Sunny Goyalfd58da62020-08-11 12:06:49 -070034import com.android.launcher3.util.DisplayController;
35import com.android.launcher3.util.DisplayController.DisplayInfoChangeListener;
36import com.android.launcher3.util.DisplayController.Info;
Stefan Andonian24a963b2023-05-05 20:25:34 +000037import com.android.launcher3.util.OnColorHintListener;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070038import com.android.launcher3.util.Themes;
Stefan Andonian24a963b2023-05-05 20:25:34 +000039import com.android.launcher3.util.WallpaperColorHints;
Sunny Goyalb46703d2020-05-27 17:52:03 -070040import com.android.launcher3.util.WindowBounds;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070041
42/**
43 * Extension of BaseActivity allowing support for drag-n-drop
44 */
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070045@SuppressWarnings("NewApi")
Sunny Goyalab837732018-03-27 17:35:54 -070046public abstract class BaseDraggingActivity extends BaseActivity
Stefan Andonian24a963b2023-05-05 20:25:34 +000047 implements OnColorHintListener, DisplayInfoChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070048
Sunny Goyal0b0847b2018-03-14 12:30:11 -070049 // 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 Goyal0b0847b2018-03-14 12:30:11 -070054
Sunny Goyalbd88f392018-06-07 15:42:57 -070055 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070056
Sunny Goyal0b0847b2018-03-14 12:30:11 -070057 @Override
58 protected void onCreate(Bundle savedInstanceState) {
59 super.onCreate(savedInstanceState);
Sunny Goyal35c7b192021-04-20 16:51:10 -070060 DisplayController.INSTANCE.get(this).addChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -070061
62 // Update theme
Stefan Andonian24a963b2023-05-05 20:25:34 +000063 WallpaperColorHints.get(this).registerOnColorHintsChangedListener(this);
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070064 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070065 if (themeRes != mThemeRes) {
66 mThemeRes = themeRes;
67 setTheme(themeRes);
68 }
69 }
70
Stefan Andonian24a963b2023-05-05 20:25:34 +000071 @MainThread
Sunny Goyalb65d7662021-03-07 15:09:11 -080072 @Override
Stefan Andonian24a963b2023-05-05 20:25:34 +000073 public void onColorHintsChanged(int colorHints) {
Lucas Dupineca08a12018-08-11 15:53:40 -070074 updateTheme();
75 }
76
77 @Override
78 public void onConfigurationChanged(Configuration newConfig) {
79 super.onConfigurationChanged(newConfig);
80 updateTheme();
81 }
82
83 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070084 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Winson Chungc4bef352020-10-28 00:13:03 -070085 recreate();
Sunny Goyalab837732018-03-27 17:35:54 -070086 }
87 }
88
Sunny Goyal0b0847b2018-03-14 12:30:11 -070089 @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 Li6bb8d792023-01-12 16:20:58 -0800101 protected boolean isInAutoCancelActionMode() {
102 return mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag();
103 }
104
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700105 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700106 public boolean finishAutoCancelActionMode() {
Fengjiang Li6bb8d792023-01-12 16:20:58 -0800107 if (isInAutoCancelActionMode()) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700108 mCurrentActionMode.finish();
109 return true;
110 }
111 return false;
112 }
113
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700114 public abstract <T extends View> T getOverviewPanel();
115
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700116 public abstract View getRootView();
117
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700118 public void returnToHomescreen() {
119 // no-op
120 }
121
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700122 @Override
Sunny Goyalb65d7662021-03-07 15:09:11 -0800123 @NonNull
Winson Chung2b093942021-04-09 14:00:25 -0700124 public ActivityOptionsWrapper getActivityLaunchOptions(View v, @Nullable ItemInfo item) {
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700125 ActivityOptionsWrapper wrapper = super.getActivityLaunchOptions(v, item);
Sunny Goyale82a20a2023-10-19 13:52:49 -0700126 addEventCallback(EVENT_RESUMED, wrapper.onEndCallback::executeAllAndDestroy);
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700127 return wrapper;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700128 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700129
130 @Override
Sunny Goyal4fdc9182023-05-12 12:08:53 -0700131 public ActivityOptionsWrapper makeDefaultActivityOptions(int splashScreenStyle) {
132 ActivityOptionsWrapper wrapper = super.makeDefaultActivityOptions(splashScreenStyle);
Sunny Goyale82a20a2023-10-19 13:52:49 -0700133 addEventCallback(EVENT_RESUMED, wrapper.onEndCallback::executeAllAndDestroy);
Sunny Goyal4fdc9182023-05-12 12:08:53 -0700134 return wrapper;
135 }
136
137 @Override
Sunny Goyalab837732018-03-27 17:35:54 -0700138 protected void onDestroy() {
139 super.onDestroy();
Sunny Goyal35c7b192021-04-20 16:51:10 -0700140 DisplayController.INSTANCE.get(this).removeChangeListener(this);
Stefan Andonian24a963b2023-05-05 20:25:34 +0000141 WallpaperColorHints.get(this).unregisterOnColorsChangedListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700142 }
143
Sunny Goyal59d086c2018-05-07 17:31:40 -0700144 protected void onDeviceProfileInitiated() {
145 if (mDeviceProfile.isVerticalBarLayout()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700146 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700147 }
148 }
149
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800150 @Override
Alex Chaufd6d9422021-04-22 19:10:21 +0100151 public void onDisplayInfoChanged(Context context, Info info, int flags) {
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800152 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700153 reapplyUi();
154 }
155 }
156
Schneider Victor-tulias16e04e22021-10-15 14:43:54 -0700157 @Override
158 public View.OnClickListener getItemOnClickListener() {
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800159 return ItemClickHandler.INSTANCE;
160 }
161
Sunny Goyal59d086c2018-05-07 17:31:40 -0700162 protected abstract void reapplyUi();
Sunny Goyal0addbf02020-04-28 14:17:35 -0700163
Sunny Goyalb46703d2020-05-27 17:52:03 -0700164 protected WindowBounds getMultiWindowDisplaySize() {
Andy Wickhamb922dcc2024-01-24 14:38:48 -0800165 return WindowBounds.fromWindowMetrics(getWindowManager().getCurrentWindowMetrics());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700166 }
Samuel Fufafd58d232021-01-12 12:59:39 -0600167
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700168 @Override
169 public boolean isAppBlockedForSafeMode() {
Sunny Goyal31e27ed2024-01-09 15:45:14 -0800170 return LauncherAppState.getInstance(this).isSafeModeEnabled();
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700171 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700172}