blob: d908440bc894c5633f9d7c6c681d91de76c18091 [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) {
197 // Remove icons and put the button's text on two lines if text is truncated.
198 if (firstButton.isTextTruncated(availableWidth)) {
199 firstButton.setIconVisible(false);
200 firstButton.setTextMultiLine(true);
201 firstButton.setPadding(horizontalPadding, verticalPadding / 2,
202 horizontalPadding, verticalPadding / 2);
203 }
Alex Chau5b019302022-05-23 10:42:25 +0100204 }
205
206 if (!dp.isTwoPanels) {
207 availableWidth -= firstButton.getMeasuredWidth() + dp.dropTargetGapPx;
208 widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST);
209 }
210 secondButton.measure(widthSpec, heightSpec);
211 if (!mIsVertical) {
Alex Chau906d8822022-05-23 10:42:25 +0100212 if (secondButton.isTextTruncated(availableWidth)) {
213 secondButton.setIconVisible(false);
214 secondButton.setTextMultiLine(true);
215 secondButton.setPadding(horizontalPadding, verticalPadding / 2,
216 horizontalPadding, verticalPadding / 2);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700217 }
218 }
219 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800220 setMeasuredDimension(width, height);
221 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700222
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800223 @Override
224 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Alex Chau906d8822022-05-23 10:42:25 +0100225 int visibleCount = getVisibleButtons(mTempTargets);
Sunny Goyala4647b62021-02-02 13:45:34 -0800226 if (visibleCount == 0) {
Pat Manningde25c0d2022-04-05 19:11:26 +0100227 return;
228 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800229
Alex Chau906d8822022-05-23 10:42:25 +0100230 DeviceProfile dp = mLauncher.getDeviceProfile();
231 // Center vertical bar over scaled workspace, accounting for hotseat offset.
232 float scale = dp.getWorkspaceSpringLoadScale();
233 Workspace<?> ws = mLauncher.getWorkspace();
234 int barCenter;
235 if (dp.isTwoPanels) {
236 barCenter = (right - left) / 2;
237 } else {
238 int workspaceCenter = (ws.getLeft() + ws.getRight()) / 2;
239 int cellLayoutCenter = ((dp.getInsets().left + dp.workspacePadding.left) + (dp.widthPx
240 - dp.getInsets().right - dp.workspacePadding.right)) / 2;
241 int cellLayoutCenterOffset = (int) ((cellLayoutCenter - workspaceCenter) * scale);
242 barCenter = workspaceCenter + cellLayoutCenterOffset - left;
243 }
Pat Manningde25c0d2022-04-05 19:11:26 +0100244
245 if (visibleCount == 1) {
Alex Chau906d8822022-05-23 10:42:25 +0100246 ButtonDropTarget button = mTempTargets[0];
Pat Manningde25c0d2022-04-05 19:11:26 +0100247 button.layout(barCenter - (button.getMeasuredWidth() / 2), 0,
248 barCenter + (button.getMeasuredWidth() / 2), button.getMeasuredHeight());
249 } else if (visibleCount == 2) {
250 int buttonGap = dp.dropTargetGapPx;
251
Alex Chau906d8822022-05-23 10:42:25 +0100252 ButtonDropTarget leftButton = mTempTargets[0];
Alex Chau906d8822022-05-23 10:42:25 +0100253 ButtonDropTarget rightButton = mTempTargets[1];
Alex Chau5b019302022-05-23 10:42:25 +0100254 if (dp.isTwoPanels) {
255 leftButton.layout(barCenter - leftButton.getMeasuredWidth() - (buttonGap / 2), 0,
256 barCenter - (buttonGap / 2), leftButton.getMeasuredHeight());
257 rightButton.layout(barCenter + (buttonGap / 2), 0,
258 barCenter + (buttonGap / 2) + rightButton.getMeasuredWidth(),
259 rightButton.getMeasuredHeight());
260 } else {
261 int scaledPanelWidth = (int) (dp.getCellLayoutWidth() * scale);
262
263 int leftButtonWidth = leftButton.getMeasuredWidth();
264 int rightButtonWidth = rightButton.getMeasuredWidth();
265 int extraSpace = scaledPanelWidth - leftButtonWidth - rightButtonWidth - buttonGap;
266
267 int leftButtonStart = barCenter - (scaledPanelWidth / 2) + extraSpace / 2;
268 int leftButtonEnd = leftButtonStart + leftButtonWidth;
269 int rightButtonStart = leftButtonEnd + buttonGap;
270 int rightButtonEnd = rightButtonStart + rightButtonWidth;
271
272 leftButton.layout(leftButtonStart, 0, leftButtonEnd,
273 leftButton.getMeasuredHeight());
274 rightButton.layout(rightButtonStart, 0, rightButtonEnd,
275 rightButton.getMeasuredHeight());
276 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800277 }
278 }
279
Alex Chau906d8822022-05-23 10:42:25 +0100280 private int getVisibleButtons(ButtonDropTarget[] outVisibleButtons) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800281 int visibleCount = 0;
Alex Chau906d8822022-05-23 10:42:25 +0100282 for (ButtonDropTarget button : mDropTargets) {
283 if (button.getVisibility() != GONE) {
284 outVisibleButtons[visibleCount] = button;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800285 visibleCount++;
286 }
287 }
288 return visibleCount;
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700289 }
290
Hyunyoung Song497708c2019-05-01 16:16:53 -0700291 public void animateToVisibility(boolean isVisible) {
vadimt89d94232021-09-01 12:33:00 -0700292 if (TestProtocol.sDebugTracing) {
293 Log.d(TestProtocol.NO_DROP_TARGET, "8");
294 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700295 if (mVisible != isVisible) {
296 mVisible = isVisible;
297
298 // Cancel any existing animation
299 if (mCurrentAnimation != null) {
300 mCurrentAnimation.cancel();
301 mCurrentAnimation = null;
302 }
303
304 float finalAlpha = mVisible ? 1 : 0;
305 if (Float.compare(getAlpha(), finalAlpha) != 0) {
306 setVisibility(View.VISIBLE);
307 mCurrentAnimation = animate().alpha(finalAlpha)
308 .setInterpolator(DEFAULT_INTERPOLATOR)
309 .setDuration(DEFAULT_DRAG_FADE_DURATION)
310 .withEndAction(mFadeAnimationEndRunnable);
311 }
312
313 }
314 }
315
Sunny Goyal47328fd2016-05-25 18:56:41 -0700316 /*
317 * DragController.DragListener implementation
318 */
319 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700320 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
vadimt89d94232021-09-01 12:33:00 -0700321 if (TestProtocol.sDebugTracing) {
322 Log.d(TestProtocol.NO_DROP_TARGET, "7");
323 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700324 animateToVisibility(true);
325 }
326
327 /**
328 * This is called to defer hiding the delete drop target until the drop animation has completed,
329 * instead of hiding immediately when the drag has ended.
330 */
331 protected void deferOnDragEnd() {
332 mDeferOnDragEnd = true;
333 }
334
335 @Override
336 public void onDragEnd() {
337 if (!mDeferOnDragEnd) {
338 animateToVisibility(false);
339 } else {
340 mDeferOnDragEnd = false;
341 }
342 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700343
344 public ButtonDropTarget[] getDropTargets() {
Alex Chau5b019302022-05-23 10:42:25 +0100345 return getVisibility() == View.VISIBLE ? mDropTargets : new ButtonDropTarget[0];
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700346 }
vadimt89d94232021-09-01 12:33:00 -0700347
348 @Override
349 protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
350 super.onVisibilityChanged(changedView, visibility);
Vadim Tryshev8629be72021-10-26 19:57:49 +0000351 if (TestProtocol.sDebugTracing) {
352 if (visibility == VISIBLE) {
353 Log.d(TestProtocol.NO_DROP_TARGET, "9");
354 } else {
355 Log.d(TestProtocol.NO_DROP_TARGET, "Hiding drop target", new Exception());
356 }
vadimt89d94232021-09-01 12:33:00 -0700357 }
358 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700359}