blob: f8ed4df453f3c658479535ee0b995a769387f238 [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;
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070041import com.android.launcher3.util.Themes;
Sunny Goyal17c72fb2019-10-14 14:06:38 -070042import com.android.launcher3.util.TraceHelper;
Stefan Andonian24a963b2023-05-05 20:25:34 +000043import com.android.launcher3.util.WallpaperColorHints;
Sunny Goyalb46703d2020-05-27 17:52:03 -070044import com.android.launcher3.util.WindowBounds;
Sunny Goyal0b0847b2018-03-14 12:30:11 -070045
46/**
47 * Extension of BaseActivity allowing support for drag-n-drop
48 */
Sunny Goyal4ed0fb52021-04-26 09:52:47 -070049@SuppressWarnings("NewApi")
Sunny Goyalab837732018-03-27 17:35:54 -070050public abstract class BaseDraggingActivity extends BaseActivity
Stefan Andonian24a963b2023-05-05 20:25:34 +000051 implements OnColorHintListener, DisplayInfoChangeListener {
Sunny Goyal0b0847b2018-03-14 12:30:11 -070052
Sunny Goyal0b0847b2018-03-14 12:30:11 -070053 // When starting an action mode, setting this tag will cause the action mode to be cancelled
54 // automatically when user interacts with the launcher.
55 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
56
57 private ActionMode mCurrentActionMode;
58 protected boolean mIsSafeModeEnabled;
59
Sunny Goyalbd88f392018-06-07 15:42:57 -070060 private int mThemeRes = R.style.AppTheme;
Sunny Goyalab837732018-03-27 17:35:54 -070061
Sunny Goyal0b0847b2018-03-14 12:30:11 -070062 @Override
63 protected void onCreate(Bundle savedInstanceState) {
64 super.onCreate(savedInstanceState);
Sunny Goyal17c72fb2019-10-14 14:06:38 -070065
Sunny Goyal5d09b2e2020-07-09 12:21:40 -070066 mIsSafeModeEnabled = TraceHelper.allowIpcs("isSafeMode",
Sunny Goyal17c72fb2019-10-14 14:06:38 -070067 () -> getPackageManager().isSafeMode());
Sunny Goyal35c7b192021-04-20 16:51:10 -070068 DisplayController.INSTANCE.get(this).addChangeListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -070069
70 // Update theme
Stefan Andonian24a963b2023-05-05 20:25:34 +000071 WallpaperColorHints.get(this).registerOnColorHintsChangedListener(this);
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070072 int themeRes = Themes.getActivityThemeRes(this);
Sunny Goyalab837732018-03-27 17:35:54 -070073 if (themeRes != mThemeRes) {
74 mThemeRes = themeRes;
75 setTheme(themeRes);
76 }
77 }
78
Stefan Andonian24a963b2023-05-05 20:25:34 +000079 @MainThread
Sunny Goyalb65d7662021-03-07 15:09:11 -080080 @Override
Stefan Andonian24a963b2023-05-05 20:25:34 +000081 public void onColorHintsChanged(int colorHints) {
Lucas Dupineca08a12018-08-11 15:53:40 -070082 updateTheme();
83 }
84
85 @Override
86 public void onConfigurationChanged(Configuration newConfig) {
87 super.onConfigurationChanged(newConfig);
88 updateTheme();
89 }
90
91 private void updateTheme() {
Hyunyoung Songfcd090d2019-05-01 13:15:29 -070092 if (mThemeRes != Themes.getActivityThemeRes(this)) {
Winson Chungc4bef352020-10-28 00:13:03 -070093 recreate();
Sunny Goyalab837732018-03-27 17:35:54 -070094 }
95 }
96
Sunny Goyal0b0847b2018-03-14 12:30:11 -070097 @Override
98 public void onActionModeStarted(ActionMode mode) {
99 super.onActionModeStarted(mode);
100 mCurrentActionMode = mode;
101 }
102
103 @Override
104 public void onActionModeFinished(ActionMode mode) {
105 super.onActionModeFinished(mode);
106 mCurrentActionMode = null;
107 }
108
Fengjiang Li6bb8d792023-01-12 16:20:58 -0800109 protected boolean isInAutoCancelActionMode() {
110 return mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag();
111 }
112
Sunny Goyal87b5eb62018-07-03 15:53:39 -0700113 @Override
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700114 public boolean finishAutoCancelActionMode() {
Fengjiang Li6bb8d792023-01-12 16:20:58 -0800115 if (isInAutoCancelActionMode()) {
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700116 mCurrentActionMode.finish();
117 return true;
118 }
119 return false;
120 }
121
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700122 public abstract <T extends View> T getOverviewPanel();
123
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700124 public abstract View getRootView();
125
Vinit Nayakf9b585b2019-07-10 14:25:32 -0700126 public void returnToHomescreen() {
127 // no-op
128 }
129
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700130 @Override
Sunny Goyalb65d7662021-03-07 15:09:11 -0800131 @NonNull
Winson Chung2b093942021-04-09 14:00:25 -0700132 public ActivityOptionsWrapper getActivityLaunchOptions(View v, @Nullable ItemInfo item) {
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700133 ActivityOptionsWrapper wrapper = super.getActivityLaunchOptions(v, item);
Sunny Goyale82a20a2023-10-19 13:52:49 -0700134 addEventCallback(EVENT_RESUMED, wrapper.onEndCallback::executeAllAndDestroy);
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700135 return wrapper;
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700136 }
Sunny Goyal9d69c8d2018-03-19 13:41:31 -0700137
138 @Override
Sunny Goyal4fdc9182023-05-12 12:08:53 -0700139 public ActivityOptionsWrapper makeDefaultActivityOptions(int splashScreenStyle) {
140 ActivityOptionsWrapper wrapper = super.makeDefaultActivityOptions(splashScreenStyle);
Sunny Goyale82a20a2023-10-19 13:52:49 -0700141 addEventCallback(EVENT_RESUMED, wrapper.onEndCallback::executeAllAndDestroy);
Sunny Goyal4fdc9182023-05-12 12:08:53 -0700142 return wrapper;
143 }
144
145 @Override
Sunny Goyalab837732018-03-27 17:35:54 -0700146 protected void onDestroy() {
147 super.onDestroy();
Sunny Goyal35c7b192021-04-20 16:51:10 -0700148 DisplayController.INSTANCE.get(this).removeChangeListener(this);
Stefan Andonian24a963b2023-05-05 20:25:34 +0000149 WallpaperColorHints.get(this).unregisterOnColorsChangedListener(this);
Sunny Goyalab837732018-03-27 17:35:54 -0700150 }
151
Sunny Goyal59d086c2018-05-07 17:31:40 -0700152 protected void onDeviceProfileInitiated() {
153 if (mDeviceProfile.isVerticalBarLayout()) {
Winson Chung13c1c2c2019-09-06 11:46:19 -0700154 mDeviceProfile.updateIsSeascape(this);
Sunny Goyal59d086c2018-05-07 17:31:40 -0700155 }
156 }
157
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800158 @Override
Alex Chaufd6d9422021-04-22 19:10:21 +0100159 public void onDisplayInfoChanged(Context context, Info info, int flags) {
Sunny Goyal9f56a2f2020-02-03 14:22:09 -0800160 if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) {
Sunny Goyal59d086c2018-05-07 17:31:40 -0700161 reapplyUi();
162 }
163 }
164
Schneider Victor-tulias16e04e22021-10-15 14:43:54 -0700165 @Override
166 public View.OnClickListener getItemOnClickListener() {
Sunny Goyal9c2b9602020-01-07 13:07:55 -0800167 return ItemClickHandler.INSTANCE;
168 }
169
Sunny Goyal59d086c2018-05-07 17:31:40 -0700170 protected abstract void reapplyUi();
Sunny Goyal0addbf02020-04-28 14:17:35 -0700171
Sunny Goyalb46703d2020-05-27 17:52:03 -0700172 protected WindowBounds getMultiWindowDisplaySize() {
Sunny Goyal0addbf02020-04-28 14:17:35 -0700173 if (Utilities.ATLEAST_R) {
Sunny Goyal187b16c2022-03-01 16:53:23 -0800174 return WindowBounds.fromWindowMetrics(getWindowManager().getCurrentWindowMetrics());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700175 }
176 // Note: Calls to getSize() can't rely on our cached DefaultDisplay since it can return
177 // the app window size
178 Display display = getWindowManager().getDefaultDisplay();
179 Point mwSize = new Point();
180 display.getSize(mwSize);
Sunny Goyalb46703d2020-05-27 17:52:03 -0700181 return new WindowBounds(new Rect(0, 0, mwSize.x, mwSize.y), new Rect());
Sunny Goyal0addbf02020-04-28 14:17:35 -0700182 }
Samuel Fufafd58d232021-01-12 12:59:39 -0600183
Brian Isganitisbde3c8b2022-03-15 13:35:06 -0700184 @Override
185 public boolean isAppBlockedForSafeMode() {
186 return mIsSafeModeEnabled;
187 }
Sunny Goyal0b0847b2018-03-14 12:30:11 -0700188}