blob: 5f5fb728a06aefff76c0f9e77244adcc82d25b5f [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
Fengjiang Lidb897c42024-10-23 15:50:38 +080053 private boolean mIsThemeUpdatedBeforeRecreate;
54
Sunny Goyal0b0847b2018-03-14 12:30:11 -070055 private ActionMode mCurrentActionMode;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070056
Sunny Goyalbd88f392018-06-07 15:42:57 -070057 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070058
Sunny Goyal0b0847b2018-03-14 12:30:11 -070059 @Override
60 protected void onCreate(Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
Sunny Goyal35c7b192021-04-20 16:51:10 -070062 DisplayController.INSTANCE.get(this).addChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -070063
64 // Update theme
Stefan Andonian24a963b2023-05-05 20:25:34 +000065 WallpaperColorHints.get(this).registerOnColorHintsChangedListener(this);
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070066 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070067 if (themeRes != mThemeRes) {
68 mThemeRes = themeRes;
69 setTheme(themeRes);
70 }
71 }
72
Stefan Andonian24a963b2023-05-05 20:25:34 +000073 @MainThread
Sunny Goyalb65d7662021-03-07 15:09:11 -080074 @Override
Stefan Andonian24a963b2023-05-05 20:25:34 +000075 public void onColorHintsChanged(int colorHints) {
Lucas Dupineca08a12018-08-11 15:53:40 -070076 updateTheme();
77 }
78
79 @Override
80 public void onConfigurationChanged(Configuration newConfig) {
81 super.onConfigurationChanged(newConfig);
82 updateTheme();
83 }
84
Fengjiang Lidb897c42024-10-23 15:50:38 +080085 public boolean isThemeUpdatedBeforeRecreate() {
86 return mIsThemeUpdatedBeforeRecreate;
87 }
88
89 protected void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070090 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Fengjiang Lidb897c42024-10-23 15:50:38 +080091 mIsThemeUpdatedBeforeRecreate = true;
Winson Chungc4bef352020-10-28 00:13:03 -070092 recreate();
Sunny Goyalab837732018-03-27 17:35:54 -070093 }
94 }
95
Sunny Goyal0b0847b2018-03-14 12:30:11 -070096 @Override
97 public void onActionModeStarted(ActionMode mode) {
98 super.onActionModeStarted(mode);
99 mCurrentActionMode = mode;
100 }
101
102 @Override
103 public void onActionModeFinished(ActionMode mode) {
104 super.onActionModeFinished(mode);
105 mCurrentActionMode = null;
106 }
107
Fengjiang Li6bb8d792023-01-12 16:20:58 -0800108 protected boolean isInAutoCancelActionMode() {
109 return mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag();
110 }
111
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700112 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700113 public boolean finishAutoCancelActionMode() {
Fengjiang Li6bb8d792023-01-12 16:20:58 -0800114 if (isInAutoCancelActionMode()) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700115 mCurrentActionMode.finish();
116 return true;
117 }
118 return false;
119 }
120
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700121 public abstract View getRootView();
122
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700123 public void returnToHomescreen() {
124 // no-op
125 }
126
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700127 @Override
Sunny Goyalb65d7662021-03-07 15:09:11 -0800128 @NonNull
Winson Chung2b093942021-04-09 14:00:25 -0700129 public ActivityOptionsWrapper getActivityLaunchOptions(View v, @Nullable ItemInfo item) {
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700130 ActivityOptionsWrapper wrapper = super.getActivityLaunchOptions(v, item);
Sunny Goyale82a20a2023-10-19 13:52:49 -0700131 addEventCallback(EVENT_RESUMED, wrapper.onEndCallback::executeAllAndDestroy);
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700132 return wrapper;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700133 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700134
135 @Override
Sunny Goyal4fdc9182023-05-12 12:08:53 -0700136 public ActivityOptionsWrapper makeDefaultActivityOptions(int splashScreenStyle) {
137 ActivityOptionsWrapper wrapper = super.makeDefaultActivityOptions(splashScreenStyle);
Sunny Goyale82a20a2023-10-19 13:52:49 -0700138 addEventCallback(EVENT_RESUMED, wrapper.onEndCallback::executeAllAndDestroy);
Sunny Goyal4fdc9182023-05-12 12:08:53 -0700139 return wrapper;
140 }
141
142 @Override
Sunny Goyalab837732018-03-27 17:35:54 -0700143 protected void onDestroy() {
144 super.onDestroy();
Sunny Goyal35c7b192021-04-20 16:51:10 -0700145 DisplayController.INSTANCE.get(this).removeChangeListener(this);
Stefan Andonian24a963b2023-05-05 20:25:34 +0000146 WallpaperColorHints.get(this).unregisterOnColorsChangedListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700147 }
148
Sunny Goyal59d086c2018-05-07 17:31:40 -0700149 protected void onDeviceProfileInitiated() {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700150 }
151
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800152 @Override
Alex Chaufd6d9422021-04-22 19:10:21 +0100153 public void onDisplayInfoChanged(Context context, Info info, int flags) {
Sihua Maa44f4ac2024-06-03 18:35:39 +0000154 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.isVerticalBarLayout()) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700155 reapplyUi();
156 }
157 }
158
Schneider Victor-tulias16e04e22021-10-15 14:43:54 -0700159 @Override
160 public View.OnClickListener getItemOnClickListener() {
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800161 return ItemClickHandler.INSTANCE;
162 }
163
Sunny Goyal59d086c2018-05-07 17:31:40 -0700164 protected abstract void reapplyUi();
Sunny Goyal0addbf02020-04-28 14:17:35 -0700165
Sunny Goyalb46703d2020-05-27 17:52:03 -0700166 protected WindowBounds getMultiWindowDisplaySize() {
Andy Wickhamb922dcc2024-01-24 14:38:48 -0800167 return WindowBounds.fromWindowMetrics(getWindowManager().getCurrentWindowMetrics());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700168 }
Samuel Fufafd58d232021-01-12 12:59:39 -0600169
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700170 @Override
171 public boolean isAppBlockedForSafeMode() {
Sunny Goyal31e27ed2024-01-09 15:45:14 -0800172 return LauncherAppState.getInstance(this).isSafeModeEnabled();
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700173 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700174}