blob: 3b93cf4abf119522a4f12bd101907ab7a330239a [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
Alex Chau007abe82024-12-02 14:29:38 +000083 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070084 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Alex Chau007abe82024-12-02 14:29:38 +000085 recreateToUpdateTheme();
Sunny Goyalab837732018-03-27 17:35:54 -070086 }
87 }
88
Alex Chau007abe82024-12-02 14:29:38 +000089 protected void recreateToUpdateTheme() {
90 recreate();
91 }
92
Sunny Goyal0b0847b2018-03-14 12:30:11 -070093 @Override
94 public void onActionModeStarted(ActionMode mode) {
95 super.onActionModeStarted(mode);
96 mCurrentActionMode = mode;
97 }
98
99 @Override
100 public void onActionModeFinished(ActionMode mode) {
101 super.onActionModeFinished(mode);
102 mCurrentActionMode = null;
103 }
104
Fengjiang Li6bb8d792023-01-12 16:20:58 -0800105 protected boolean isInAutoCancelActionMode() {
106 return mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag();
107 }
108
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700109 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700110 public boolean finishAutoCancelActionMode() {
Fengjiang Li6bb8d792023-01-12 16:20:58 -0800111 if (isInAutoCancelActionMode()) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700112 mCurrentActionMode.finish();
113 return true;
114 }
115 return false;
116 }
117
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700118 public abstract View getRootView();
119
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700120 public void returnToHomescreen() {
121 // no-op
122 }
123
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700124 @Override
Sunny Goyalb65d7662021-03-07 15:09:11 -0800125 @NonNull
Winson Chung2b093942021-04-09 14:00:25 -0700126 public ActivityOptionsWrapper getActivityLaunchOptions(View v, @Nullable ItemInfo item) {
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700127 ActivityOptionsWrapper wrapper = super.getActivityLaunchOptions(v, item);
Sunny Goyale82a20a2023-10-19 13:52:49 -0700128 addEventCallback(EVENT_RESUMED, wrapper.onEndCallback::executeAllAndDestroy);
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700129 return wrapper;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700130 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700131
132 @Override
Sunny Goyal4fdc9182023-05-12 12:08:53 -0700133 public ActivityOptionsWrapper makeDefaultActivityOptions(int splashScreenStyle) {
134 ActivityOptionsWrapper wrapper = super.makeDefaultActivityOptions(splashScreenStyle);
Sunny Goyale82a20a2023-10-19 13:52:49 -0700135 addEventCallback(EVENT_RESUMED, wrapper.onEndCallback::executeAllAndDestroy);
Sunny Goyal4fdc9182023-05-12 12:08:53 -0700136 return wrapper;
137 }
138
139 @Override
Sunny Goyalab837732018-03-27 17:35:54 -0700140 protected void onDestroy() {
141 super.onDestroy();
Sunny Goyal35c7b192021-04-20 16:51:10 -0700142 DisplayController.INSTANCE.get(this).removeChangeListener(this);
Stefan Andonian24a963b2023-05-05 20:25:34 +0000143 WallpaperColorHints.get(this).unregisterOnColorsChangedListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700144 }
145
Sunny Goyal59d086c2018-05-07 17:31:40 -0700146 protected void onDeviceProfileInitiated() {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700147 }
148
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800149 @Override
Alex Chaufd6d9422021-04-22 19:10:21 +0100150 public void onDisplayInfoChanged(Context context, Info info, int flags) {
Sihua Maa44f4ac2024-06-03 18:35:39 +0000151 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.isVerticalBarLayout()) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700152 reapplyUi();
153 }
154 }
155
Schneider Victor-tulias16e04e22021-10-15 14:43:54 -0700156 @Override
157 public View.OnClickListener getItemOnClickListener() {
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800158 return ItemClickHandler.INSTANCE;
159 }
160
Sunny Goyal59d086c2018-05-07 17:31:40 -0700161 protected abstract void reapplyUi();
Sunny Goyal0addbf02020-04-28 14:17:35 -0700162
Sunny Goyalb46703d2020-05-27 17:52:03 -0700163 protected WindowBounds getMultiWindowDisplaySize() {
Andy Wickhamb922dcc2024-01-24 14:38:48 -0800164 return WindowBounds.fromWindowMetrics(getWindowManager().getCurrentWindowMetrics());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700165 }
Samuel Fufafd58d232021-01-12 12:59:39 -0600166
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700167 @Override
168 public boolean isAppBlockedForSafeMode() {
Sunny Goyal31e27ed2024-01-09 15:45:14 -0800169 return LauncherAppState.getInstance(this).isSafeModeEnabled();
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700170 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700171}