blob: 6f3e94829cd3ca240848a7ad2fd60f5a64d242a4 [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
Sunny Goyale396abf2020-04-06 15:11:17 -070037import com.android.launcher3.model.data.ItemInfo;
Sunny Goyal9c2b9602020-01-07 13:07:55 -080038import com.android.launcher3.touch.ItemClickHandler;
Sunny Goyalb65d7662021-03-07 15:09:11 -080039import com.android.launcher3.util.ActivityOptionsWrapper;
Sunny Goyalfd58da62020-08-11 12:06:49 -070040import com.android.launcher3.util.DisplayController;
41import com.android.launcher3.util.DisplayController.DisplayInfoChangeListener;
42import com.android.launcher3.util.DisplayController.Info;
Sunny Goyalb65d7662021-03-07 15:09:11 -080043import com.android.launcher3.util.RunnableList;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070044import com.android.launcher3.util.Themes;
Sunny Goyal17c72fb2019-10-14 14:06:38 -070045import com.android.launcher3.util.TraceHelper;
Sunny Goyalb46703d2020-05-27 17:52:03 -070046import com.android.launcher3.util.WindowBounds;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070047
48/**
49 * Extension of BaseActivity allowing support for drag-n-drop
50 */
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070051@SuppressWarnings("NewApi")
Sunny Goyalab837732018-03-27 17:35:54 -070052public abstract class BaseDraggingActivity extends BaseActivity
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070053 implements OnColorsChangedListener, DisplayInfoChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070054
55 private static final String TAG = "BaseDraggingActivity";
56
Sunny Goyal0b0847b2018-03-14 12:30:11 -070057 // When starting an action mode, setting this tag will cause the action mode to be cancelled
58 // automatically when user interacts with the launcher.
59 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
60
61 private ActionMode mCurrentActionMode;
62 protected boolean mIsSafeModeEnabled;
63
Winson Chunga19a2b72019-10-14 16:30:38 -070064 private Runnable mOnStartCallback;
Sunny Goyalb65d7662021-03-07 15:09:11 -080065 private RunnableList mOnResumeCallbacks = new RunnableList();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -070066
Sunny Goyalbd88f392018-06-07 15:42:57 -070067 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070068
Sunny Goyal0b0847b2018-03-14 12:30:11 -070069 @Override
70 protected void onCreate(Bundle savedInstanceState) {
71 super.onCreate(savedInstanceState);
Sunny Goyal17c72fb2019-10-14 14:06:38 -070072
Sunny Goyal5d09b2e2020-07-09 12:21:40 -070073 mIsSafeModeEnabled = TraceHelper.allowIpcs("isSafeMode",
Sunny Goyal17c72fb2019-10-14 14:06:38 -070074 () -> getPackageManager().isSafeMode());
Sunny Goyal35c7b192021-04-20 16:51:10 -070075 DisplayController.INSTANCE.get(this).addChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -070076
77 // Update theme
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070078 if (Utilities.ATLEAST_P) {
79 getSystemService(WallpaperManager.class)
80 .addOnColorsChangedListener(this, MAIN_EXECUTOR.getHandler());
81 }
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070082 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070083 if (themeRes != mThemeRes) {
84 mThemeRes = themeRes;
85 setTheme(themeRes);
86 }
87 }
88
89 @Override
Sunny Goyalb65d7662021-03-07 15:09:11 -080090 protected void onResume() {
91 super.onResume();
92 mOnResumeCallbacks.executeAllAndClear();
93 }
94
95 public void addOnResumeCallback(Runnable callback) {
96 mOnResumeCallbacks.add(callback);
97 }
98
99 @Override
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700100 public void onColorsChanged(WallpaperColors wallpaperColors, int which) {
Lucas Dupineca08a12018-08-11 15:53:40 -0700101 updateTheme();
102 }
103
104 @Override
105 public void onConfigurationChanged(Configuration newConfig) {
106 super.onConfigurationChanged(newConfig);
107 updateTheme();
108 }
109
110 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -0700111 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Winson Chungc4bef352020-10-28 00:13:03 -0700112 recreate();
Sunny Goyalab837732018-03-27 17:35:54 -0700113 }
114 }
115
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700116 @Override
117 public void onActionModeStarted(ActionMode mode) {
118 super.onActionModeStarted(mode);
119 mCurrentActionMode = mode;
120 }
121
122 @Override
123 public void onActionModeFinished(ActionMode mode) {
124 super.onActionModeFinished(mode);
125 mCurrentActionMode = null;
126 }
127
Fengjiang Li6bb8d792023-01-12 16:20:58 -0800128 protected boolean isInAutoCancelActionMode() {
129 return mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag();
130 }
131
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700132 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700133 public boolean finishAutoCancelActionMode() {
Fengjiang Li6bb8d792023-01-12 16:20:58 -0800134 if (isInAutoCancelActionMode()) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700135 mCurrentActionMode.finish();
136 return true;
137 }
138 return false;
139 }
140
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700141 public abstract <T extends View> T getOverviewPanel();
142
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700143 public abstract View getRootView();
144
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700145 public void returnToHomescreen() {
146 // no-op
147 }
148
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700149 @Override
Sunny Goyalb65d7662021-03-07 15:09:11 -0800150 @NonNull
Winson Chung2b093942021-04-09 14:00:25 -0700151 public ActivityOptionsWrapper getActivityLaunchOptions(View v, @Nullable ItemInfo item) {
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700152 ActivityOptionsWrapper wrapper = super.getActivityLaunchOptions(v, item);
153 addOnResumeCallback(wrapper.onEndCallback::executeAllAndDestroy);
154 return wrapper;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700155 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700156
157 @Override
158 protected void onStart() {
159 super.onStart();
160
161 if (mOnStartCallback != null) {
Winson Chunga19a2b72019-10-14 16:30:38 -0700162 mOnStartCallback.run();
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700163 mOnStartCallback = null;
164 }
165 }
166
Sunny Goyalab837732018-03-27 17:35:54 -0700167 @Override
168 protected void onDestroy() {
169 super.onDestroy();
Sunny Goyal4ed0fb52021-04-26 09:52:47 -0700170 if (Utilities.ATLEAST_P) {
171 getSystemService(WallpaperManager.class).removeOnColorsChangedListener(this);
172 }
Sunny Goyal35c7b192021-04-20 16:51:10 -0700173 DisplayController.INSTANCE.get(this).removeChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700174 }
175
Winson Chunga19a2b72019-10-14 16:30:38 -0700176 public void runOnceOnStart(Runnable action) {
177 mOnStartCallback = action;
178 }
179
180 public void clearRunOnceOnStartCallback() {
181 mOnStartCallback = null;
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700182 }
183
Sunny Goyal59d086c2018-05-07 17:31:40 -0700184 protected void onDeviceProfileInitiated() {
185 if (mDeviceProfile.isVerticalBarLayout()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700186 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700187 }
188 }
189
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800190 @Override
Alex Chaufd6d9422021-04-22 19:10:21 +0100191 public void onDisplayInfoChanged(Context context, Info info, int flags) {
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800192 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700193 reapplyUi();
194 }
195 }
196
Schneider Victor-tulias16e04e22021-10-15 14:43:54 -0700197 @Override
198 public View.OnClickListener getItemOnClickListener() {
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800199 return ItemClickHandler.INSTANCE;
200 }
201
Sunny Goyal59d086c2018-05-07 17:31:40 -0700202 protected abstract void reapplyUi();
Sunny Goyal0addbf02020-04-28 14:17:35 -0700203
Sunny Goyalb46703d2020-05-27 17:52:03 -0700204 protected WindowBounds getMultiWindowDisplaySize() {
Sunny Goyal0addbf02020-04-28 14:17:35 -0700205 if (Utilities.ATLEAST_R) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800206 return WindowBounds.fromWindowMetrics(getWindowManager().getCurrentWindowMetrics());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700207 }
208 // Note: Calls to getSize() can't rely on our cached DefaultDisplay since it can return
209 // the app window size
210 Display display = getWindowManager().getDefaultDisplay();
211 Point mwSize = new Point();
212 display.getSize(mwSize);
Sunny Goyalb46703d2020-05-27 17:52:03 -0700213 return new WindowBounds(new Rect(0, 0, mwSize.x, mwSize.y), new Rect());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700214 }
Samuel Fufafd58d232021-01-12 12:59:39 -0600215
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700216 @Override
217 public boolean isAppBlockedForSafeMode() {
218 return mIsSafeModeEnabled;
219 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700220}