blob: 3af43c021b74348a7a2197103c489528898d766b [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;
Sunny Goyal9f56a2f2020-02-03 14:22:09 -080021
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070022import android.app.WallpaperColors;
23import android.app.WallpaperManager;
24import android.app.WallpaperManager.OnColorsChangedListener;
Alex Chaufd6d9422021-04-22 19:10:21 +010025import android.content.Context;
Lucas Dupineca08a12018-08-11 15:53:40 -070026import android.content.res.Configuration;
Sunny Goyal0addbf02020-04-28 14:17:35 -070027import android.graphics.Point;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070028import android.graphics.Rect;
29import android.os.Bundle;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070030import android.view.ActionMode;
Sunny Goyal0addbf02020-04-28 14:17:35 -070031import android.view.Display;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070032import android.view.View;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070033
Sunny Goyalb65d7662021-03-07 15:09:11 -080034import androidx.annotation.NonNull;
Winson Chung13c1c2c2019-09-06 11:46:19 -070035import androidx.annotation.Nullable;
36
Brian Isganitis1664c9f2022-01-14 23:15:47 -050037import com.android.launcher3.allapps.ActivityAllAppsContainerView;
Samuel Fufafd58d232021-01-12 12:59:39 -060038import com.android.launcher3.allapps.search.DefaultSearchAdapterProvider;
39import com.android.launcher3.allapps.search.SearchAdapterProvider;
Sunny Goyale396abf2020-04-06 15:11:17 -070040import com.android.launcher3.model.data.ItemInfo;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080041import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyalb65d7662021-03-07 15:09:11 -080042import com.android.launcher3.util.ActivityOptionsWrapper;
Sunny Goyalfd58da62020-08-11 12:06:49 -070043import com.android.launcher3.util.DisplayController;
44import com.android.launcher3.util.DisplayController.DisplayInfoChangeListener;
45import com.android.launcher3.util.DisplayController.Info;
Sunny Goyalb65d7662021-03-07 15:09:11 -080046import com.android.launcher3.util.RunnableList;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070047import com.android.launcher3.util.Themes;
Sunny Goyal17c72fb2019-10-14 14:06:38 -070048import com.android.launcher3.util.TraceHelper;
Sunny Goyalb46703d2020-05-27 17:52:03 -070049import com.android.launcher3.util.WindowBounds;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070050
51/**
52 * Extension of BaseActivity allowing support for drag-n-drop
53 */
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070054@SuppressWarnings("NewApi")
Sunny Goyalab837732018-03-27 17:35:54 -070055public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070056 implements OnColorsChangedListener, DisplayInfoChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070057
58 private static final String TAG = "BaseDraggingActivity";
59
Sunny Goyal0b0847b2018-03-14 12:30:11 -070060 // When starting an action mode, setting this tag will cause the action mode to be cancelled
61 // automatically when user interacts with the launcher.
62 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
63
64 private ActionMode mCurrentActionMode;
65 protected boolean mIsSafeModeEnabled;
66
Winson Chunga19a2b72019-10-14 16:30:38 -070067 private Runnable mOnStartCallback;
Sunny Goyalb65d7662021-03-07 15:09:11 -080068 private RunnableList mOnResumeCallbacks = new RunnableList();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070069
Sunny Goyalbd88f392018-06-07 15:42:57 -070070 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070071
Sunny Goyal0b0847b2018-03-14 12:30:11 -070072 @Override
73 protected void onCreate(Bundle savedInstanceState) {
74 super.onCreate(savedInstanceState);
Sunny Goyal17c72fb2019-10-14 14:06:38 -070075
Sunny Goyal5d09b2e2020-07-09 12:21:40 -070076 mIsSafeModeEnabled = TraceHelper.allowIpcs("isSafeMode",
Sunny Goyal17c72fb2019-10-14 14:06:38 -070077 () -> getPackageManager().isSafeMode());
Sunny Goyal35c7b192021-04-20 16:51:10 -070078 DisplayController.INSTANCE.get(this).addChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -070079
80 // Update theme
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070081 if (Utilities.ATLEAST_P) {
82 getSystemService(WallpaperManager.class)
83 .addOnColorsChangedListener(this, MAIN_EXECUTOR.getHandler());
84 }
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070085 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070086 if (themeRes != mThemeRes) {
87 mThemeRes = themeRes;
88 setTheme(themeRes);
89 }
90 }
91
92 @Override
Sunny Goyalb65d7662021-03-07 15:09:11 -080093 protected void onResume() {
94 super.onResume();
95 mOnResumeCallbacks.executeAllAndClear();
96 }
97
98 public void addOnResumeCallback(Runnable callback) {
99 mOnResumeCallbacks.add(callback);
100 }
101
102 @Override
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700103 public void onColorsChanged(WallpaperColors wallpaperColors, int which) {
Lucas Dupineca08a12018-08-11 15:53:40 -0700104 updateTheme();
105 }
106
107 @Override
108 public void onConfigurationChanged(Configuration newConfig) {
109 super.onConfigurationChanged(newConfig);
110 updateTheme();
111 }
112
113 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -0700114 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Winson Chungc4bef352020-10-28 00:13:03 -0700115 recreate();
Sunny Goyalab837732018-03-27 17:35:54 -0700116 }
117 }
118
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700119 @Override
120 public void onActionModeStarted(ActionMode mode) {
121 super.onActionModeStarted(mode);
122 mCurrentActionMode = mode;
123 }
124
125 @Override
126 public void onActionModeFinished(ActionMode mode) {
127 super.onActionModeFinished(mode);
128 mCurrentActionMode = null;
129 }
130
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700131 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700132 public boolean finishAutoCancelActionMode() {
133 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
134 mCurrentActionMode.finish();
135 return true;
136 }
137 return false;
138 }
139
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700140 public abstract <T extends View> T getOverviewPanel();
141
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700142 public abstract View getRootView();
143
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700144 public void returnToHomescreen() {
145 // no-op
146 }
147
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700148 @Override
Sunny Goyalb65d7662021-03-07 15:09:11 -0800149 @NonNull
Winson Chung2b093942021-04-09 14:00:25 -0700150 public ActivityOptionsWrapper getActivityLaunchOptions(View v, @Nullable ItemInfo item) {
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700151 ActivityOptionsWrapper wrapper = super.getActivityLaunchOptions(v, item);
152 addOnResumeCallback(wrapper.onEndCallback::executeAllAndDestroy);
153 return wrapper;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700154 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700155
156 @Override
157 protected void onStart() {
158 super.onStart();
159
160 if (mOnStartCallback != null) {
Winson Chunga19a2b72019-10-14 16:30:38 -0700161 mOnStartCallback.run();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700162 mOnStartCallback = null;
163 }
164 }
165
Sunny Goyalab837732018-03-27 17:35:54 -0700166 @Override
167 protected void onDestroy() {
168 super.onDestroy();
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700169 if (Utilities.ATLEAST_P) {
170 getSystemService(WallpaperManager.class).removeOnColorsChangedListener(this);
171 }
Sunny Goyal35c7b192021-04-20 16:51:10 -0700172 DisplayController.INSTANCE.get(this).removeChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700173 }
174
Winson Chunga19a2b72019-10-14 16:30:38 -0700175 public void runOnceOnStart(Runnable action) {
176 mOnStartCallback = action;
177 }
178
179 public void clearRunOnceOnStartCallback() {
180 mOnStartCallback = null;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700181 }
182
Sunny Goyal59d086c2018-05-07 17:31:40 -0700183 protected void onDeviceProfileInitiated() {
184 if (mDeviceProfile.isVerticalBarLayout()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700185 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700186 }
187 }
188
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800189 @Override
Alex Chaufd6d9422021-04-22 19:10:21 +0100190 public void onDisplayInfoChanged(Context context, Info info, int flags) {
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800191 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700192 reapplyUi();
193 }
194 }
195
Schneider Victor-tulias16e04e22021-10-15 14:43:54 -0700196 @Override
197 public View.OnClickListener getItemOnClickListener() {
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800198 return ItemClickHandler.INSTANCE;
199 }
200
Sunny Goyal59d086c2018-05-07 17:31:40 -0700201 protected abstract void reapplyUi();
Sunny Goyal0addbf02020-04-28 14:17:35 -0700202
Sunny Goyalb46703d2020-05-27 17:52:03 -0700203 protected WindowBounds getMultiWindowDisplaySize() {
Sunny Goyal0addbf02020-04-28 14:17:35 -0700204 if (Utilities.ATLEAST_R) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800205 return WindowBounds.fromWindowMetrics(getWindowManager().getCurrentWindowMetrics());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700206 }
207 // Note: Calls to getSize() can't rely on our cached DefaultDisplay since it can return
208 // the app window size
209 Display display = getWindowManager().getDefaultDisplay();
210 Point mwSize = new Point();
211 display.getSize(mwSize);
Sunny Goyalb46703d2020-05-27 17:52:03 -0700212 return new WindowBounds(new Rect(0, 0, mwSize.x, mwSize.y), new Rect());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700213 }
Samuel Fufafd58d232021-01-12 12:59:39 -0600214
215 /**
216 * Creates and returns {@link SearchAdapterProvider} for build variant specific search result
217 * views
218 */
Brian Isganitis1664c9f2022-01-14 23:15:47 -0500219 public SearchAdapterProvider<?> createSearchAdapterProvider(
220 ActivityAllAppsContainerView<?> allApps) {
221 return new DefaultSearchAdapterProvider(this);
Samuel Fufafd58d232021-01-12 12:59:39 -0600222 }
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700223
224 @Override
225 public boolean isAppBlockedForSafeMode() {
226 return mIsSafeModeEnabled;
227 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700228}