blob: 8876a1ba34ca0e774062a0446cb7cbf3bcc6afad [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 Goyal4ed0fb52021-04-26 09:52:47 -070020import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
Stefan Andonianefcff6c2023-04-27 21:26:59 +000021import static com.android.launcher3.util.Executors.THREAD_POOL_EXECUTOR;
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080022
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070023import android.app.WallpaperColors;
24import android.app.WallpaperManager;
25import android.app.WallpaperManager.OnColorsChangedListener;
Alex Chaufd6d9422021-04-22 19:10:21 +010026import android.content.Context;
Lucas Dupineca08a12018-08-11 15:53:40 -070027import android.content.res.Configuration;
Sunny Goyal0addbf02020-04-28 14:17:35 -070028import android.graphics.Point;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070029import android.graphics.Rect;
30import android.os.Bundle;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070031import android.view.ActionMode;
Sunny Goyal0addbf02020-04-28 14:17:35 -070032import android.view.Display;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070033import android.view.View;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070034
Sunny Goyalb65d7662021-03-07 15:09:11 -080035import androidx.annotation.NonNull;
Winson Chung13c1c2c2019-09-06 11:46:19 -070036import androidx.annotation.Nullable;
37
Sunny Goyale396abf2020-04-06 15:11:17 -070038import com.android.launcher3.model.data.ItemInfo;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080039import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyalb65d7662021-03-07 15:09:11 -080040import com.android.launcher3.util.ActivityOptionsWrapper;
Sunny Goyalfd58da62020-08-11 12:06:49 -070041import com.android.launcher3.util.DisplayController;
42import com.android.launcher3.util.DisplayController.DisplayInfoChangeListener;
43import com.android.launcher3.util.DisplayController.Info;
Sunny Goyalb65d7662021-03-07 15:09:11 -080044import com.android.launcher3.util.RunnableList;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070045import com.android.launcher3.util.Themes;
Sunny Goyal17c72fb2019-10-14 14:06:38 -070046import com.android.launcher3.util.TraceHelper;
Sunny Goyalb46703d2020-05-27 17:52:03 -070047import com.android.launcher3.util.WindowBounds;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070048
49/**
50 * Extension of BaseActivity allowing support for drag-n-drop
51 */
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070052@SuppressWarnings("NewApi")
Sunny Goyalab837732018-03-27 17:35:54 -070053public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070054 implements OnColorsChangedListener, DisplayInfoChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070055
56 private static final String TAG = "BaseDraggingActivity";
57
Sunny Goyal0b0847b2018-03-14 12:30:11 -070058 // When starting an action mode, setting this tag will cause the action mode to be cancelled
59 // automatically when user interacts with the launcher.
60 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
61
62 private ActionMode mCurrentActionMode;
63 protected boolean mIsSafeModeEnabled;
64
Winson Chunga19a2b72019-10-14 16:30:38 -070065 private Runnable mOnStartCallback;
Sunny Goyalb65d7662021-03-07 15:09:11 -080066 private RunnableList mOnResumeCallbacks = new RunnableList();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070067
Sunny Goyalbd88f392018-06-07 15:42:57 -070068 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070069
Sunny Goyal0b0847b2018-03-14 12:30:11 -070070 @Override
71 protected void onCreate(Bundle savedInstanceState) {
72 super.onCreate(savedInstanceState);
Sunny Goyal17c72fb2019-10-14 14:06:38 -070073
Sunny Goyal5d09b2e2020-07-09 12:21:40 -070074 mIsSafeModeEnabled = TraceHelper.allowIpcs("isSafeMode",
Sunny Goyal17c72fb2019-10-14 14:06:38 -070075 () -> getPackageManager().isSafeMode());
Sunny Goyal35c7b192021-04-20 16:51:10 -070076 DisplayController.INSTANCE.get(this).addChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -070077
78 // Update theme
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070079 if (Utilities.ATLEAST_P) {
Stefan Andonianefcff6c2023-04-27 21:26:59 +000080 THREAD_POOL_EXECUTOR.execute(() -> getSystemService(WallpaperManager.class)
81 .addOnColorsChangedListener(this, MAIN_EXECUTOR.getHandler()));
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070082 }
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070083 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070084 if (themeRes != mThemeRes) {
85 mThemeRes = themeRes;
86 setTheme(themeRes);
87 }
88 }
89
90 @Override
Sunny Goyalb65d7662021-03-07 15:09:11 -080091 protected void onResume() {
92 super.onResume();
93 mOnResumeCallbacks.executeAllAndClear();
94 }
95
96 public void addOnResumeCallback(Runnable callback) {
97 mOnResumeCallbacks.add(callback);
98 }
99
100 @Override
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700101 public void onColorsChanged(WallpaperColors wallpaperColors, int which) {
Lucas Dupineca08a12018-08-11 15:53:40 -0700102 updateTheme();
103 }
104
105 @Override
106 public void onConfigurationChanged(Configuration newConfig) {
107 super.onConfigurationChanged(newConfig);
108 updateTheme();
109 }
110
111 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -0700112 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Winson Chungc4bef352020-10-28 00:13:03 -0700113 recreate();
Sunny Goyalab837732018-03-27 17:35:54 -0700114 }
115 }
116
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700117 @Override
118 public void onActionModeStarted(ActionMode mode) {
119 super.onActionModeStarted(mode);
120 mCurrentActionMode = mode;
121 }
122
123 @Override
124 public void onActionModeFinished(ActionMode mode) {
125 super.onActionModeFinished(mode);
126 mCurrentActionMode = null;
127 }
128
Fengjiang Li6bb8d792023-01-12 16:20:58 -0800129 protected boolean isInAutoCancelActionMode() {
130 return mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag();
131 }
132
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700133 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700134 public boolean finishAutoCancelActionMode() {
Fengjiang Li6bb8d792023-01-12 16:20:58 -0800135 if (isInAutoCancelActionMode()) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700136 mCurrentActionMode.finish();
137 return true;
138 }
139 return false;
140 }
141
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700142 public abstract <T extends View> T getOverviewPanel();
143
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700144 public abstract View getRootView();
145
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700146 public void returnToHomescreen() {
147 // no-op
148 }
149
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700150 @Override
Sunny Goyalb65d7662021-03-07 15:09:11 -0800151 @NonNull
Winson Chung2b093942021-04-09 14:00:25 -0700152 public ActivityOptionsWrapper getActivityLaunchOptions(View v, @Nullable ItemInfo item) {
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700153 ActivityOptionsWrapper wrapper = super.getActivityLaunchOptions(v, item);
154 addOnResumeCallback(wrapper.onEndCallback::executeAllAndDestroy);
155 return wrapper;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700156 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700157
158 @Override
Sunny Goyal4fdc9182023-05-12 12:08:53 -0700159 public ActivityOptionsWrapper makeDefaultActivityOptions(int splashScreenStyle) {
160 ActivityOptionsWrapper wrapper = super.makeDefaultActivityOptions(splashScreenStyle);
161 addOnResumeCallback(wrapper.onEndCallback::executeAllAndDestroy);
162 return wrapper;
163 }
164
165 @Override
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700166 protected void onStart() {
167 super.onStart();
168
169 if (mOnStartCallback != null) {
Winson Chunga19a2b72019-10-14 16:30:38 -0700170 mOnStartCallback.run();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700171 mOnStartCallback = null;
172 }
173 }
174
Sunny Goyalab837732018-03-27 17:35:54 -0700175 @Override
176 protected void onDestroy() {
177 super.onDestroy();
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700178 if (Utilities.ATLEAST_P) {
179 getSystemService(WallpaperManager.class).removeOnColorsChangedListener(this);
180 }
Sunny Goyal35c7b192021-04-20 16:51:10 -0700181 DisplayController.INSTANCE.get(this).removeChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700182 }
183
Winson Chunga19a2b72019-10-14 16:30:38 -0700184 public void runOnceOnStart(Runnable action) {
185 mOnStartCallback = action;
186 }
187
188 public void clearRunOnceOnStartCallback() {
189 mOnStartCallback = null;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700190 }
191
Sunny Goyal59d086c2018-05-07 17:31:40 -0700192 protected void onDeviceProfileInitiated() {
193 if (mDeviceProfile.isVerticalBarLayout()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700194 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700195 }
196 }
197
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800198 @Override
Alex Chaufd6d9422021-04-22 19:10:21 +0100199 public void onDisplayInfoChanged(Context context, Info info, int flags) {
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800200 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700201 reapplyUi();
202 }
203 }
204
Schneider Victor-tulias16e04e22021-10-15 14:43:54 -0700205 @Override
206 public View.OnClickListener getItemOnClickListener() {
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800207 return ItemClickHandler.INSTANCE;
208 }
209
Sunny Goyal59d086c2018-05-07 17:31:40 -0700210 protected abstract void reapplyUi();
Sunny Goyal0addbf02020-04-28 14:17:35 -0700211
Sunny Goyalb46703d2020-05-27 17:52:03 -0700212 protected WindowBounds getMultiWindowDisplaySize() {
Sunny Goyal0addbf02020-04-28 14:17:35 -0700213 if (Utilities.ATLEAST_R) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800214 return WindowBounds.fromWindowMetrics(getWindowManager().getCurrentWindowMetrics());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700215 }
216 // Note: Calls to getSize() can't rely on our cached DefaultDisplay since it can return
217 // the app window size
218 Display display = getWindowManager().getDefaultDisplay();
219 Point mwSize = new Point();
220 display.getSize(mwSize);
Sunny Goyalb46703d2020-05-27 17:52:03 -0700221 return new WindowBounds(new Rect(0, 0, mwSize.x, mwSize.y), new Rect());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700222 }
Samuel Fufafd58d232021-01-12 12:59:39 -0600223
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700224 @Override
225 public boolean isAppBlockedForSafeMode() {
226 return mIsSafeModeEnabled;
227 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700228}