blob: 808cf70a50ed96d9f0ca9795e6892c94558e90d3 [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;
Sunny Goyalb65d7662021-03-07 15:09:11 -080041import com.android.launcher3.util.RunnableList;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070042import com.android.launcher3.util.Themes;
Sunny Goyal17c72fb2019-10-14 14:06:38 -070043import com.android.launcher3.util.TraceHelper;
Stefan Andonian24a963b2023-05-05 20:25:34 +000044import com.android.launcher3.util.WallpaperColorHints;
Sunny Goyalb46703d2020-05-27 17:52:03 -070045import com.android.launcher3.util.WindowBounds;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070046
47/**
48 * Extension of BaseActivity allowing support for drag-n-drop
49 */
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070050@SuppressWarnings("NewApi")
Sunny Goyalab837732018-03-27 17:35:54 -070051public abstract class BaseDraggingActivity extends BaseActivity
Stefan Andonian24a963b2023-05-05 20:25:34 +000052 implements OnColorHintListener, DisplayInfoChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070053
54 private static final String TAG = "BaseDraggingActivity";
55
Sunny Goyal0b0847b2018-03-14 12:30:11 -070056 // When starting an action mode, setting this tag will cause the action mode to be cancelled
57 // automatically when user interacts with the launcher.
58 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
59
60 private ActionMode mCurrentActionMode;
61 protected boolean mIsSafeModeEnabled;
62
Winson Chunga19a2b72019-10-14 16:30:38 -070063 private Runnable mOnStartCallback;
Stefan Andonian24a963b2023-05-05 20:25:34 +000064 private final RunnableList mOnResumeCallbacks = new RunnableList();
Sunny Goyalbd88f392018-06-07 15:42:57 -070065 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070066
Sunny Goyal0b0847b2018-03-14 12:30:11 -070067 @Override
68 protected void onCreate(Bundle savedInstanceState) {
69 super.onCreate(savedInstanceState);
Sunny Goyal17c72fb2019-10-14 14:06:38 -070070
Sunny Goyal5d09b2e2020-07-09 12:21:40 -070071 mIsSafeModeEnabled = TraceHelper.allowIpcs("isSafeMode",
Sunny Goyal17c72fb2019-10-14 14:06:38 -070072 () -> getPackageManager().isSafeMode());
Sunny Goyal35c7b192021-04-20 16:51:10 -070073 DisplayController.INSTANCE.get(this).addChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -070074
75 // Update theme
Stefan Andonian24a963b2023-05-05 20:25:34 +000076 WallpaperColorHints.get(this).registerOnColorHintsChangedListener(this);
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070077 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070078 if (themeRes != mThemeRes) {
79 mThemeRes = themeRes;
80 setTheme(themeRes);
81 }
82 }
83
84 @Override
Sunny Goyalb65d7662021-03-07 15:09:11 -080085 protected void onResume() {
86 super.onResume();
87 mOnResumeCallbacks.executeAllAndClear();
88 }
89
90 public void addOnResumeCallback(Runnable callback) {
91 mOnResumeCallbacks.add(callback);
92 }
93
Stefan Andonian24a963b2023-05-05 20:25:34 +000094 @MainThread
Sunny Goyalb65d7662021-03-07 15:09:11 -080095 @Override
Stefan Andonian24a963b2023-05-05 20:25:34 +000096 public void onColorHintsChanged(int colorHints) {
Lucas Dupineca08a12018-08-11 15:53:40 -070097 updateTheme();
98 }
99
100 @Override
101 public void onConfigurationChanged(Configuration newConfig) {
102 super.onConfigurationChanged(newConfig);
103 updateTheme();
104 }
105
106 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -0700107 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Winson Chungc4bef352020-10-28 00:13:03 -0700108 recreate();
Sunny Goyalab837732018-03-27 17:35:54 -0700109 }
110 }
111
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700112 @Override
113 public void onActionModeStarted(ActionMode mode) {
114 super.onActionModeStarted(mode);
115 mCurrentActionMode = mode;
116 }
117
118 @Override
119 public void onActionModeFinished(ActionMode mode) {
120 super.onActionModeFinished(mode);
121 mCurrentActionMode = null;
122 }
123
Fengjiang Li6bb8d792023-01-12 16:20:58 -0800124 protected boolean isInAutoCancelActionMode() {
125 return mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag();
126 }
127
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700128 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700129 public boolean finishAutoCancelActionMode() {
Fengjiang Li6bb8d792023-01-12 16:20:58 -0800130 if (isInAutoCancelActionMode()) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700131 mCurrentActionMode.finish();
132 return true;
133 }
134 return false;
135 }
136
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700137 public abstract <T extends View> T getOverviewPanel();
138
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700139 public abstract View getRootView();
140
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700141 public void returnToHomescreen() {
142 // no-op
143 }
144
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700145 @Override
Sunny Goyalb65d7662021-03-07 15:09:11 -0800146 @NonNull
Winson Chung2b093942021-04-09 14:00:25 -0700147 public ActivityOptionsWrapper getActivityLaunchOptions(View v, @Nullable ItemInfo item) {
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700148 ActivityOptionsWrapper wrapper = super.getActivityLaunchOptions(v, item);
149 addOnResumeCallback(wrapper.onEndCallback::executeAllAndDestroy);
150 return wrapper;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700151 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700152
153 @Override
Sunny Goyal4fdc9182023-05-12 12:08:53 -0700154 public ActivityOptionsWrapper makeDefaultActivityOptions(int splashScreenStyle) {
155 ActivityOptionsWrapper wrapper = super.makeDefaultActivityOptions(splashScreenStyle);
156 addOnResumeCallback(wrapper.onEndCallback::executeAllAndDestroy);
157 return wrapper;
158 }
159
160 @Override
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700161 protected void onStart() {
162 super.onStart();
163
164 if (mOnStartCallback != null) {
Winson Chunga19a2b72019-10-14 16:30:38 -0700165 mOnStartCallback.run();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700166 mOnStartCallback = null;
167 }
168 }
169
Sunny Goyalab837732018-03-27 17:35:54 -0700170 @Override
171 protected void onDestroy() {
172 super.onDestroy();
Sunny Goyal35c7b192021-04-20 16:51:10 -0700173 DisplayController.INSTANCE.get(this).removeChangeListener(this);
Stefan Andonian24a963b2023-05-05 20:25:34 +0000174 WallpaperColorHints.get(this).unregisterOnColorsChangedListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700175 }
176
Winson Chunga19a2b72019-10-14 16:30:38 -0700177 public void runOnceOnStart(Runnable action) {
178 mOnStartCallback = action;
179 }
180
181 public void clearRunOnceOnStartCallback() {
182 mOnStartCallback = null;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700183 }
184
Sunny Goyal59d086c2018-05-07 17:31:40 -0700185 protected void onDeviceProfileInitiated() {
186 if (mDeviceProfile.isVerticalBarLayout()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700187 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700188 }
189 }
190
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800191 @Override
Alex Chaufd6d9422021-04-22 19:10:21 +0100192 public void onDisplayInfoChanged(Context context, Info info, int flags) {
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800193 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700194 reapplyUi();
195 }
196 }
197
Schneider Victor-tulias16e04e22021-10-15 14:43:54 -0700198 @Override
199 public View.OnClickListener getItemOnClickListener() {
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800200 return ItemClickHandler.INSTANCE;
201 }
202
Sunny Goyal59d086c2018-05-07 17:31:40 -0700203 protected abstract void reapplyUi();
Sunny Goyal0addbf02020-04-28 14:17:35 -0700204
Sunny Goyalb46703d2020-05-27 17:52:03 -0700205 protected WindowBounds getMultiWindowDisplaySize() {
Sunny Goyal0addbf02020-04-28 14:17:35 -0700206 if (Utilities.ATLEAST_R) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800207 return WindowBounds.fromWindowMetrics(getWindowManager().getCurrentWindowMetrics());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700208 }
209 // Note: Calls to getSize() can't rely on our cached DefaultDisplay since it can return
210 // the app window size
211 Display display = getWindowManager().getDefaultDisplay();
212 Point mwSize = new Point();
213 display.getSize(mwSize);
Sunny Goyalb46703d2020-05-27 17:52:03 -0700214 return new WindowBounds(new Rect(0, 0, mwSize.x, mwSize.y), new Rect());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700215 }
Samuel Fufafd58d232021-01-12 12:59:39 -0600216
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700217 @Override
218 public boolean isAppBlockedForSafeMode() {
219 return mIsSafeModeEnabled;
220 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700221}