blob: c1304d413c051c0db8877b30164c04b72a7aa1e7 [file] [log] [blame]
Sunny Goyal47328fd2016-05-25 18:56:41 -07001/*
2 * Copyright (C) 2011 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 Goyald1b3f5c2018-01-18 17:14:05 -080019import static com.android.launcher3.ButtonDropTarget.TOOLTIP_DEFAULT;
Sunny Goyal7185dd62018-03-14 17:51:49 -070020import static com.android.launcher3.anim.AlphaUpdateListener.updateVisibility;
Sunny Goyal0236d0b2017-10-24 14:54:30 -070021
Sunny Goyal47328fd2016-05-25 18:56:41 -070022import android.animation.TimeInterpolator;
23import android.content.Context;
Sunny Goyal07b69292018-01-08 14:19:34 -080024import android.graphics.Rect;
Sunny Goyal47328fd2016-05-25 18:56:41 -070025import android.util.AttributeSet;
vadimt89d94232021-09-01 12:33:00 -070026import android.util.Log;
Alex Chaua02eddc2021-04-29 00:36:06 +010027import android.util.TypedValue;
Sunny Goyal07b69292018-01-08 14:19:34 -080028import android.view.Gravity;
Sunny Goyal47328fd2016-05-25 18:56:41 -070029import android.view.View;
30import android.view.ViewDebug;
31import android.view.ViewPropertyAnimator;
Sunny Goyal07b69292018-01-08 14:19:34 -080032import android.widget.FrameLayout;
Sunny Goyal47328fd2016-05-25 18:56:41 -070033
vadimt89d94232021-09-01 12:33:00 -070034import androidx.annotation.NonNull;
35
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070036import com.android.launcher3.anim.Interpolators;
Sunny Goyal47328fd2016-05-25 18:56:41 -070037import com.android.launcher3.dragndrop.DragController;
Sunny Goyal07b69292018-01-08 14:19:34 -080038import com.android.launcher3.dragndrop.DragController.DragListener;
Sunny Goyal94b510c2016-08-16 15:36:48 -070039import com.android.launcher3.dragndrop.DragOptions;
vadimt89d94232021-09-01 12:33:00 -070040import com.android.launcher3.testing.TestProtocol;
Sunny Goyal47328fd2016-05-25 18:56:41 -070041
42/*
43 * The top bar containing various drop targets: Delete/App Info/Uninstall.
44 */
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080045public class DropTargetBar extends FrameLayout
Sunny Goyal07b69292018-01-08 14:19:34 -080046 implements DragListener, Insettable {
Sunny Goyal47328fd2016-05-25 18:56:41 -070047
48 protected static final int DEFAULT_DRAG_FADE_DURATION = 175;
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070049 protected static final TimeInterpolator DEFAULT_INTERPOLATOR = Interpolators.ACCEL;
Sunny Goyal47328fd2016-05-25 18:56:41 -070050
Sunny Goyal07b69292018-01-08 14:19:34 -080051 private final Runnable mFadeAnimationEndRunnable =
Sunny Goyal18d71842018-03-27 13:44:00 -070052 () -> updateVisibility(DropTargetBar.this);
Sunny Goyal47328fd2016-05-25 18:56:41 -070053
Alex Chau906d8822022-05-23 10:42:25 +010054 private final Launcher mLauncher;
55
Sunny Goyal47328fd2016-05-25 18:56:41 -070056 @ViewDebug.ExportedProperty(category = "launcher")
57 protected boolean mDeferOnDragEnd;
58
59 @ViewDebug.ExportedProperty(category = "launcher")
60 protected boolean mVisible = false;
61
Sunny Goyal0236d0b2017-10-24 14:54:30 -070062 private ButtonDropTarget[] mDropTargets;
Alex Chau906d8822022-05-23 10:42:25 +010063 private ButtonDropTarget[] mTempTargets;
Sunny Goyal47328fd2016-05-25 18:56:41 -070064 private ViewPropertyAnimator mCurrentAnimation;
65
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080066 private boolean mIsVertical = true;
67
Sunny Goyal47328fd2016-05-25 18:56:41 -070068 public DropTargetBar(Context context, AttributeSet attrs) {
69 super(context, attrs);
Alex Chau906d8822022-05-23 10:42:25 +010070 mLauncher = Launcher.getLauncher(context);
Sunny Goyal47328fd2016-05-25 18:56:41 -070071 }
72
73 public DropTargetBar(Context context, AttributeSet attrs, int defStyle) {
74 super(context, attrs, defStyle);
Alex Chau906d8822022-05-23 10:42:25 +010075 mLauncher = Launcher.getLauncher(context);
Sunny Goyal47328fd2016-05-25 18:56:41 -070076 }
77
78 @Override
79 protected void onFinishInflate() {
80 super.onFinishInflate();
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080081 mDropTargets = new ButtonDropTarget[getChildCount()];
82 for (int i = 0; i < mDropTargets.length; i++) {
83 mDropTargets[i] = (ButtonDropTarget) getChildAt(i);
84 mDropTargets[i].setDropTargetBar(this);
85 }
Alex Chau906d8822022-05-23 10:42:25 +010086 mTempTargets = new ButtonDropTarget[getChildCount()];
Sunny Goyal47328fd2016-05-25 18:56:41 -070087 }
88
Sunny Goyal07b69292018-01-08 14:19:34 -080089 @Override
90 public void setInsets(Rect insets) {
91 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
Alex Chau906d8822022-05-23 10:42:25 +010092 DeviceProfile grid = mLauncher.getDeviceProfile();
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080093 mIsVertical = grid.isVerticalBarLayout();
Sunny Goyal07b69292018-01-08 14:19:34 -080094
95 lp.leftMargin = insets.left;
96 lp.topMargin = insets.top;
97 lp.bottomMargin = insets.bottom;
98 lp.rightMargin = insets.right;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080099 int tooltipLocation = TOOLTIP_DEFAULT;
Sunny Goyal07b69292018-01-08 14:19:34 -0800100
Pat Manningde25c0d2022-04-05 19:11:26 +0100101 int horizontalMargin;
102 if (grid.isTablet) {
103 // XXX: If the icon size changes across orientations, we will have to take
104 // that into account here too.
105 horizontalMargin = ((grid.widthPx - 2 * grid.edgeMarginPx
106 - (grid.inv.numColumns * grid.cellWidthPx))
107 / (2 * (grid.inv.numColumns + 1)))
108 + grid.edgeMarginPx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800109 } else {
Pat Manningde25c0d2022-04-05 19:11:26 +0100110 horizontalMargin = getContext().getResources()
111 .getDimensionPixelSize(R.dimen.drop_target_bar_margin_horizontal);
Pat Manninge3723662022-03-25 16:07:46 +0000112 }
Pat Manningde25c0d2022-04-05 19:11:26 +0100113 lp.topMargin += grid.dropTargetBarTopMarginPx;
114 lp.bottomMargin += grid.dropTargetBarBottomMarginPx;
115 lp.width = grid.availableWidthPx - 2 * horizontalMargin;
116 if (mIsVertical) {
117 lp.leftMargin = (grid.widthPx - lp.width) / 2;
118 lp.rightMargin = (grid.widthPx - lp.width) / 2;
119 }
120 lp.height = grid.dropTargetBarSizePx;
121 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
122
Alex Chau906d8822022-05-23 10:42:25 +0100123 DeviceProfile dp = mLauncher.getDeviceProfile();
124 int horizontalPadding = dp.dropTargetHorizontalPaddingPx;
125 int verticalPadding = dp.dropTargetVerticalPaddingPx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800126 setLayoutParams(lp);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800127 for (ButtonDropTarget button : mDropTargets) {
Alex Chaua02eddc2021-04-29 00:36:06 +0100128 button.setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.dropTargetTextSizePx);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800129 button.setToolTipLocation(tooltipLocation);
Alex Chau906d8822022-05-23 10:42:25 +0100130 button.setPadding(horizontalPadding, verticalPadding, horizontalPadding,
131 verticalPadding);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800132 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800133 }
134
Sunny Goyal47328fd2016-05-25 18:56:41 -0700135 public void setup(DragController dragController) {
136 dragController.addDragListener(this);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700137 for (int i = 0; i < mDropTargets.length; i++) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700138 dragController.addDragListener(mDropTargets[i]);
139 dragController.addDropTarget(mDropTargets[i]);
140 }
141 }
142
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700143 @Override
144 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800145 int width = MeasureSpec.getSize(widthMeasureSpec);
146 int height = MeasureSpec.getSize(heightMeasureSpec);
Alex Chau906d8822022-05-23 10:42:25 +0100147 int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700148
Alex Chau906d8822022-05-23 10:42:25 +0100149 int visibleCount = getVisibleButtons(mTempTargets);
150 if (visibleCount == 1) {
151 int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST);
Pat Manning27bfcaa2022-04-25 13:50:28 +0100152
Alex Chau906d8822022-05-23 10:42:25 +0100153 ButtonDropTarget firstButton = mTempTargets[0];
154 firstButton.setTextVisible(true);
155 firstButton.setIconVisible(true);
156 firstButton.measure(widthSpec, heightSpec);
157 } else if (visibleCount == 2) {
158 DeviceProfile dp = mLauncher.getDeviceProfile();
159 int verticalPadding = dp.dropTargetVerticalPaddingPx;
160 int horizontalPadding = dp.dropTargetHorizontalPaddingPx;
161
162 ButtonDropTarget firstButton = mTempTargets[0];
163 firstButton.setTextVisible(true);
164 firstButton.setIconVisible(true);
165 firstButton.setTextMultiLine(false);
166 // Reset second button padding in case it was previously changed to multi-line text.
167 firstButton.setPadding(horizontalPadding, verticalPadding, horizontalPadding,
168 verticalPadding);
169
170 ButtonDropTarget secondButton = mTempTargets[1];
171 secondButton.setTextVisible(true);
172 secondButton.setIconVisible(true);
173 secondButton.setTextMultiLine(false);
174 // Reset second button padding in case it was previously changed to multi-line text.
175 secondButton.setPadding(horizontalPadding, verticalPadding, horizontalPadding,
176 verticalPadding);
177
178 float scale = dp.getWorkspaceSpringLoadScale();
179 int scaledPanelWidth = (int) (dp.getCellLayoutWidth() * scale);
180
181 int availableWidth;
182 if (dp.isTwoPanels) {
183 // Both buttons for two panel fit to the width of one Cell Layout (less
184 // half of the center gap between the buttons).
185 int halfButtonGap = dp.dropTargetGapPx / 2;
186 availableWidth = scaledPanelWidth - halfButtonGap / 2;
187 } else {
188 // Both buttons plus the button gap do not display past the edge of the scaled
Alex Chau5b019302022-05-23 10:42:25 +0100189 // workspace, less a pre-defined gap from the edge of the workspace.
190 availableWidth = scaledPanelWidth - dp.dropTargetGapPx
191 - 2 * dp.dropTargetButtonWorkspaceEdgeGapPx;
Pat Manningb29dabc2022-05-20 15:21:48 +0000192 }
Pat Manning27bfcaa2022-04-25 13:50:28 +0100193
Pat Manningb29dabc2022-05-20 15:21:48 +0000194 int widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST);
Alex Chau906d8822022-05-23 10:42:25 +0100195 firstButton.measure(widthSpec, heightSpec);
Alex Chau906d8822022-05-23 10:42:25 +0100196 if (!mIsVertical) {
Pat Manninge3d74b42022-06-08 13:15:13 +0100197 // Remove both icons and put the button's text on two lines if text is truncated.
Alex Chau906d8822022-05-23 10:42:25 +0100198 if (firstButton.isTextTruncated(availableWidth)) {
199 firstButton.setIconVisible(false);
Pat Manninge3d74b42022-06-08 13:15:13 +0100200 secondButton.setIconVisible(false);
Alex Chau906d8822022-05-23 10:42:25 +0100201 firstButton.setTextMultiLine(true);
202 firstButton.setPadding(horizontalPadding, verticalPadding / 2,
203 horizontalPadding, verticalPadding / 2);
204 }
Alex Chau5b019302022-05-23 10:42:25 +0100205 }
206
207 if (!dp.isTwoPanels) {
208 availableWidth -= firstButton.getMeasuredWidth() + dp.dropTargetGapPx;
209 widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST);
210 }
211 secondButton.measure(widthSpec, heightSpec);
212 if (!mIsVertical) {
Pat Manninge3d74b42022-06-08 13:15:13 +0100213 // Remove both icons and put the button's text on two lines if text is truncated.
Alex Chau906d8822022-05-23 10:42:25 +0100214 if (secondButton.isTextTruncated(availableWidth)) {
215 secondButton.setIconVisible(false);
Pat Manninge3d74b42022-06-08 13:15:13 +0100216 firstButton.setIconVisible(false);
Alex Chau906d8822022-05-23 10:42:25 +0100217 secondButton.setTextMultiLine(true);
218 secondButton.setPadding(horizontalPadding, verticalPadding / 2,
219 horizontalPadding, verticalPadding / 2);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700220 }
221 }
222 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800223 setMeasuredDimension(width, height);
224 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700225
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800226 @Override
227 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Alex Chau906d8822022-05-23 10:42:25 +0100228 int visibleCount = getVisibleButtons(mTempTargets);
Sunny Goyala4647b62021-02-02 13:45:34 -0800229 if (visibleCount == 0) {
Pat Manningde25c0d2022-04-05 19:11:26 +0100230 return;
231 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800232
Alex Chau906d8822022-05-23 10:42:25 +0100233 DeviceProfile dp = mLauncher.getDeviceProfile();
234 // Center vertical bar over scaled workspace, accounting for hotseat offset.
235 float scale = dp.getWorkspaceSpringLoadScale();
236 Workspace<?> ws = mLauncher.getWorkspace();
237 int barCenter;
238 if (dp.isTwoPanels) {
239 barCenter = (right - left) / 2;
240 } else {
241 int workspaceCenter = (ws.getLeft() + ws.getRight()) / 2;
242 int cellLayoutCenter = ((dp.getInsets().left + dp.workspacePadding.left) + (dp.widthPx
243 - dp.getInsets().right - dp.workspacePadding.right)) / 2;
244 int cellLayoutCenterOffset = (int) ((cellLayoutCenter - workspaceCenter) * scale);
245 barCenter = workspaceCenter + cellLayoutCenterOffset - left;
246 }
Pat Manningde25c0d2022-04-05 19:11:26 +0100247
248 if (visibleCount == 1) {
Alex Chau906d8822022-05-23 10:42:25 +0100249 ButtonDropTarget button = mTempTargets[0];
Pat Manningde25c0d2022-04-05 19:11:26 +0100250 button.layout(barCenter - (button.getMeasuredWidth() / 2), 0,
251 barCenter + (button.getMeasuredWidth() / 2), button.getMeasuredHeight());
252 } else if (visibleCount == 2) {
253 int buttonGap = dp.dropTargetGapPx;
254
Alex Chau906d8822022-05-23 10:42:25 +0100255 ButtonDropTarget leftButton = mTempTargets[0];
Alex Chau906d8822022-05-23 10:42:25 +0100256 ButtonDropTarget rightButton = mTempTargets[1];
Alex Chau5b019302022-05-23 10:42:25 +0100257 if (dp.isTwoPanels) {
258 leftButton.layout(barCenter - leftButton.getMeasuredWidth() - (buttonGap / 2), 0,
259 barCenter - (buttonGap / 2), leftButton.getMeasuredHeight());
260 rightButton.layout(barCenter + (buttonGap / 2), 0,
261 barCenter + (buttonGap / 2) + rightButton.getMeasuredWidth(),
262 rightButton.getMeasuredHeight());
263 } else {
264 int scaledPanelWidth = (int) (dp.getCellLayoutWidth() * scale);
265
266 int leftButtonWidth = leftButton.getMeasuredWidth();
267 int rightButtonWidth = rightButton.getMeasuredWidth();
268 int extraSpace = scaledPanelWidth - leftButtonWidth - rightButtonWidth - buttonGap;
269
270 int leftButtonStart = barCenter - (scaledPanelWidth / 2) + extraSpace / 2;
271 int leftButtonEnd = leftButtonStart + leftButtonWidth;
272 int rightButtonStart = leftButtonEnd + buttonGap;
273 int rightButtonEnd = rightButtonStart + rightButtonWidth;
274
275 leftButton.layout(leftButtonStart, 0, leftButtonEnd,
276 leftButton.getMeasuredHeight());
277 rightButton.layout(rightButtonStart, 0, rightButtonEnd,
278 rightButton.getMeasuredHeight());
279 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800280 }
281 }
282
Alex Chau906d8822022-05-23 10:42:25 +0100283 private int getVisibleButtons(ButtonDropTarget[] outVisibleButtons) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800284 int visibleCount = 0;
Alex Chau906d8822022-05-23 10:42:25 +0100285 for (ButtonDropTarget button : mDropTargets) {
286 if (button.getVisibility() != GONE) {
287 outVisibleButtons[visibleCount] = button;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800288 visibleCount++;
289 }
290 }
291 return visibleCount;
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700292 }
293
Hyunyoung Song497708c2019-05-01 16:16:53 -0700294 public void animateToVisibility(boolean isVisible) {
vadimt89d94232021-09-01 12:33:00 -0700295 if (TestProtocol.sDebugTracing) {
296 Log.d(TestProtocol.NO_DROP_TARGET, "8");
297 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700298 if (mVisible != isVisible) {
299 mVisible = isVisible;
300
301 // Cancel any existing animation
302 if (mCurrentAnimation != null) {
303 mCurrentAnimation.cancel();
304 mCurrentAnimation = null;
305 }
306
307 float finalAlpha = mVisible ? 1 : 0;
308 if (Float.compare(getAlpha(), finalAlpha) != 0) {
309 setVisibility(View.VISIBLE);
310 mCurrentAnimation = animate().alpha(finalAlpha)
311 .setInterpolator(DEFAULT_INTERPOLATOR)
312 .setDuration(DEFAULT_DRAG_FADE_DURATION)
313 .withEndAction(mFadeAnimationEndRunnable);
314 }
315
316 }
317 }
318
Sunny Goyal47328fd2016-05-25 18:56:41 -0700319 /*
320 * DragController.DragListener implementation
321 */
322 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700323 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
vadimt89d94232021-09-01 12:33:00 -0700324 if (TestProtocol.sDebugTracing) {
325 Log.d(TestProtocol.NO_DROP_TARGET, "7");
326 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700327 animateToVisibility(true);
328 }
329
330 /**
331 * This is called to defer hiding the delete drop target until the drop animation has completed,
332 * instead of hiding immediately when the drag has ended.
333 */
334 protected void deferOnDragEnd() {
335 mDeferOnDragEnd = true;
336 }
337
338 @Override
339 public void onDragEnd() {
340 if (!mDeferOnDragEnd) {
341 animateToVisibility(false);
342 } else {
343 mDeferOnDragEnd = false;
344 }
345 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700346
347 public ButtonDropTarget[] getDropTargets() {
Alex Chau5b019302022-05-23 10:42:25 +0100348 return getVisibility() == View.VISIBLE ? mDropTargets : new ButtonDropTarget[0];
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700349 }
vadimt89d94232021-09-01 12:33:00 -0700350
351 @Override
352 protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
353 super.onVisibilityChanged(changedView, visibility);
Vadim Tryshev8629be72021-10-26 19:57:49 +0000354 if (TestProtocol.sDebugTracing) {
355 if (visibility == VISIBLE) {
356 Log.d(TestProtocol.NO_DROP_TARGET, "9");
357 } else {
358 Log.d(TestProtocol.NO_DROP_TARGET, "Hiding drop target", new Exception());
359 }
vadimt89d94232021-09-01 12:33:00 -0700360 }
361 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700362}