blob: 8bdf61a633d42ed2ae69087bb41b57e7093f219e [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;
Alex Chaua02eddc2021-04-29 00:36:06 +010026import android.util.TypedValue;
Sunny Goyal07b69292018-01-08 14:19:34 -080027import android.view.Gravity;
Sunny Goyal47328fd2016-05-25 18:56:41 -070028import android.view.View;
29import android.view.ViewDebug;
30import android.view.ViewPropertyAnimator;
Sunny Goyal07b69292018-01-08 14:19:34 -080031import android.widget.FrameLayout;
Sunny Goyal47328fd2016-05-25 18:56:41 -070032
Kateryna Ivanova71203732023-05-24 15:09:00 +000033import com.android.app.animation.Interpolators;
Sunny Goyal47328fd2016-05-25 18:56:41 -070034import com.android.launcher3.dragndrop.DragController;
Sunny Goyal07b69292018-01-08 14:19:34 -080035import com.android.launcher3.dragndrop.DragController.DragListener;
Sunny Goyal94b510c2016-08-16 15:36:48 -070036import com.android.launcher3.dragndrop.DragOptions;
Sunny Goyal47328fd2016-05-25 18:56:41 -070037
38/*
39 * The top bar containing various drop targets: Delete/App Info/Uninstall.
40 */
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080041public class DropTargetBar extends FrameLayout
Sunny Goyal07b69292018-01-08 14:19:34 -080042 implements DragListener, Insettable {
Sunny Goyal47328fd2016-05-25 18:56:41 -070043
44 protected static final int DEFAULT_DRAG_FADE_DURATION = 175;
Kateryna Ivanova71203732023-05-24 15:09:00 +000045 protected static final TimeInterpolator DEFAULT_INTERPOLATOR = Interpolators.ACCELERATE;
Sunny Goyal47328fd2016-05-25 18:56:41 -070046
Sunny Goyal07b69292018-01-08 14:19:34 -080047 private final Runnable mFadeAnimationEndRunnable =
Sunny Goyal18d71842018-03-27 13:44:00 -070048 () -> updateVisibility(DropTargetBar.this);
Sunny Goyal47328fd2016-05-25 18:56:41 -070049
Alex Chau906d8822022-05-23 10:42:25 +010050 private final Launcher mLauncher;
51
Sunny Goyal47328fd2016-05-25 18:56:41 -070052 @ViewDebug.ExportedProperty(category = "launcher")
53 protected boolean mDeferOnDragEnd;
54
55 @ViewDebug.ExportedProperty(category = "launcher")
56 protected boolean mVisible = false;
57
Sunny Goyal0236d0b2017-10-24 14:54:30 -070058 private ButtonDropTarget[] mDropTargets;
Alex Chau906d8822022-05-23 10:42:25 +010059 private ButtonDropTarget[] mTempTargets;
Sunny Goyal47328fd2016-05-25 18:56:41 -070060 private ViewPropertyAnimator mCurrentAnimation;
61
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080062 private boolean mIsVertical = true;
63
Sunny Goyal47328fd2016-05-25 18:56:41 -070064 public DropTargetBar(Context context, AttributeSet attrs) {
65 super(context, attrs);
Alex Chau906d8822022-05-23 10:42:25 +010066 mLauncher = Launcher.getLauncher(context);
Sunny Goyal47328fd2016-05-25 18:56:41 -070067 }
68
69 public DropTargetBar(Context context, AttributeSet attrs, int defStyle) {
70 super(context, attrs, defStyle);
Alex Chau906d8822022-05-23 10:42:25 +010071 mLauncher = Launcher.getLauncher(context);
Sunny Goyal47328fd2016-05-25 18:56:41 -070072 }
73
74 @Override
75 protected void onFinishInflate() {
76 super.onFinishInflate();
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080077 mDropTargets = new ButtonDropTarget[getChildCount()];
78 for (int i = 0; i < mDropTargets.length; i++) {
79 mDropTargets[i] = (ButtonDropTarget) getChildAt(i);
80 mDropTargets[i].setDropTargetBar(this);
81 }
Alex Chau906d8822022-05-23 10:42:25 +010082 mTempTargets = new ButtonDropTarget[getChildCount()];
Sunny Goyal47328fd2016-05-25 18:56:41 -070083 }
84
Sunny Goyal07b69292018-01-08 14:19:34 -080085 @Override
86 public void setInsets(Rect insets) {
87 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
Alex Chau906d8822022-05-23 10:42:25 +010088 DeviceProfile grid = mLauncher.getDeviceProfile();
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080089 mIsVertical = grid.isVerticalBarLayout();
Sunny Goyal07b69292018-01-08 14:19:34 -080090
91 lp.leftMargin = insets.left;
92 lp.topMargin = insets.top;
93 lp.bottomMargin = insets.bottom;
94 lp.rightMargin = insets.right;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080095 int tooltipLocation = TOOLTIP_DEFAULT;
Sunny Goyal07b69292018-01-08 14:19:34 -080096
Pat Manningde25c0d2022-04-05 19:11:26 +010097 int horizontalMargin;
98 if (grid.isTablet) {
99 // XXX: If the icon size changes across orientations, we will have to take
100 // that into account here too.
101 horizontalMargin = ((grid.widthPx - 2 * grid.edgeMarginPx
102 - (grid.inv.numColumns * grid.cellWidthPx))
103 / (2 * (grid.inv.numColumns + 1)))
104 + grid.edgeMarginPx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800105 } else {
Pat Manningde25c0d2022-04-05 19:11:26 +0100106 horizontalMargin = getContext().getResources()
107 .getDimensionPixelSize(R.dimen.drop_target_bar_margin_horizontal);
Pat Manninge3723662022-03-25 16:07:46 +0000108 }
Pat Manningde25c0d2022-04-05 19:11:26 +0100109 lp.topMargin += grid.dropTargetBarTopMarginPx;
110 lp.bottomMargin += grid.dropTargetBarBottomMarginPx;
111 lp.width = grid.availableWidthPx - 2 * horizontalMargin;
112 if (mIsVertical) {
113 lp.leftMargin = (grid.widthPx - lp.width) / 2;
114 lp.rightMargin = (grid.widthPx - lp.width) / 2;
115 }
116 lp.height = grid.dropTargetBarSizePx;
fbarone74256b2023-04-10 14:50:31 -0700117 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
Pat Manningde25c0d2022-04-05 19:11:26 +0100118
Alex Chau906d8822022-05-23 10:42:25 +0100119 DeviceProfile dp = mLauncher.getDeviceProfile();
120 int horizontalPadding = dp.dropTargetHorizontalPaddingPx;
121 int verticalPadding = dp.dropTargetVerticalPaddingPx;
Sunny Goyal07b69292018-01-08 14:19:34 -0800122 setLayoutParams(lp);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800123 for (ButtonDropTarget button : mDropTargets) {
Alex Chaua02eddc2021-04-29 00:36:06 +0100124 button.setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.dropTargetTextSizePx);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800125 button.setToolTipLocation(tooltipLocation);
Alex Chau906d8822022-05-23 10:42:25 +0100126 button.setPadding(horizontalPadding, verticalPadding, horizontalPadding,
127 verticalPadding);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800128 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800129 }
130
Sunny Goyal47328fd2016-05-25 18:56:41 -0700131 public void setup(DragController dragController) {
132 dragController.addDragListener(this);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700133 for (int i = 0; i < mDropTargets.length; i++) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700134 dragController.addDragListener(mDropTargets[i]);
135 dragController.addDropTarget(mDropTargets[i]);
136 }
137 }
138
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700139 @Override
140 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800141 int width = MeasureSpec.getSize(widthMeasureSpec);
142 int height = MeasureSpec.getSize(heightMeasureSpec);
Alex Chau906d8822022-05-23 10:42:25 +0100143 int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700144
Alex Chau906d8822022-05-23 10:42:25 +0100145 int visibleCount = getVisibleButtons(mTempTargets);
146 if (visibleCount == 1) {
147 int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST);
Pat Manning27bfcaa2022-04-25 13:50:28 +0100148
Alex Chau906d8822022-05-23 10:42:25 +0100149 ButtonDropTarget firstButton = mTempTargets[0];
Pat Manninge63dd252022-08-02 16:15:29 +0100150 firstButton.setTextSize(TypedValue.COMPLEX_UNIT_PX,
151 mLauncher.getDeviceProfile().dropTargetTextSizePx);
Alex Chau906d8822022-05-23 10:42:25 +0100152 firstButton.setTextVisible(true);
153 firstButton.setIconVisible(true);
154 firstButton.measure(widthSpec, heightSpec);
Jordan Silva567372f2023-03-28 00:33:08 +0100155 firstButton.resizeTextToFit();
Alex Chau906d8822022-05-23 10:42:25 +0100156 } else if (visibleCount == 2) {
157 DeviceProfile dp = mLauncher.getDeviceProfile();
158 int verticalPadding = dp.dropTargetVerticalPaddingPx;
159 int horizontalPadding = dp.dropTargetHorizontalPaddingPx;
160
161 ButtonDropTarget firstButton = mTempTargets[0];
Pat Manninge63dd252022-08-02 16:15:29 +0100162 firstButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, dp.dropTargetTextSizePx);
Alex Chau906d8822022-05-23 10:42:25 +0100163 firstButton.setTextVisible(true);
164 firstButton.setIconVisible(true);
165 firstButton.setTextMultiLine(false);
Pat Manninge63dd252022-08-02 16:15:29 +0100166 // Reset first button padding in case it was previously changed to multi-line text.
Alex Chau906d8822022-05-23 10:42:25 +0100167 firstButton.setPadding(horizontalPadding, verticalPadding, horizontalPadding,
168 verticalPadding);
169
170 ButtonDropTarget secondButton = mTempTargets[1];
Pat Manninge63dd252022-08-02 16:15:29 +0100171 secondButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, dp.dropTargetTextSizePx);
Alex Chau906d8822022-05-23 10:42:25 +0100172 secondButton.setTextVisible(true);
173 secondButton.setIconVisible(true);
174 secondButton.setTextMultiLine(false);
175 // Reset second button padding in case it was previously changed to multi-line text.
176 secondButton.setPadding(horizontalPadding, verticalPadding, horizontalPadding,
177 verticalPadding);
178
Alex Chau906d8822022-05-23 10:42:25 +0100179 int availableWidth;
180 if (dp.isTwoPanels) {
Pat Manninge63dd252022-08-02 16:15:29 +0100181 // Each button for two panel fits to half the width of the screen excluding the
182 // center gap between the buttons.
183 availableWidth = (dp.availableWidthPx - dp.dropTargetGapPx) / 2;
Alex Chau906d8822022-05-23 10:42:25 +0100184 } else {
Pat Manninge63dd252022-08-02 16:15:29 +0100185 // Both buttons plus the button gap do not display past the edge of the screen.
186 availableWidth = dp.availableWidthPx - dp.dropTargetGapPx;
Pat Manningb29dabc2022-05-20 15:21:48 +0000187 }
Pat Manning27bfcaa2022-04-25 13:50:28 +0100188
Pat Manningb29dabc2022-05-20 15:21:48 +0000189 int widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST);
Alex Chau906d8822022-05-23 10:42:25 +0100190 firstButton.measure(widthSpec, heightSpec);
Alex Chau906d8822022-05-23 10:42:25 +0100191 if (!mIsVertical) {
Pat Manninge3d74b42022-06-08 13:15:13 +0100192 // Remove both icons and put the button's text on two lines if text is truncated.
Alex Chau906d8822022-05-23 10:42:25 +0100193 if (firstButton.isTextTruncated(availableWidth)) {
194 firstButton.setIconVisible(false);
Pat Manninge3d74b42022-06-08 13:15:13 +0100195 secondButton.setIconVisible(false);
Alex Chau906d8822022-05-23 10:42:25 +0100196 firstButton.setTextMultiLine(true);
197 firstButton.setPadding(horizontalPadding, verticalPadding / 2,
198 horizontalPadding, verticalPadding / 2);
199 }
Alex Chau5b019302022-05-23 10:42:25 +0100200 }
201
202 if (!dp.isTwoPanels) {
203 availableWidth -= firstButton.getMeasuredWidth() + dp.dropTargetGapPx;
204 widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST);
205 }
206 secondButton.measure(widthSpec, heightSpec);
207 if (!mIsVertical) {
Pat Manninge3d74b42022-06-08 13:15:13 +0100208 // Remove both icons and put the button's text on two lines if text is truncated.
Alex Chau906d8822022-05-23 10:42:25 +0100209 if (secondButton.isTextTruncated(availableWidth)) {
210 secondButton.setIconVisible(false);
Pat Manninge3d74b42022-06-08 13:15:13 +0100211 firstButton.setIconVisible(false);
Alex Chau906d8822022-05-23 10:42:25 +0100212 secondButton.setTextMultiLine(true);
213 secondButton.setPadding(horizontalPadding, verticalPadding / 2,
214 horizontalPadding, verticalPadding / 2);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700215 }
216 }
Pat Manninge63dd252022-08-02 16:15:29 +0100217
218 // If text is still truncated, shrink to fit in measured width and resize both targets.
219 float minTextSize =
220 Math.min(firstButton.resizeTextToFit(), secondButton.resizeTextToFit());
221 if (firstButton.getTextSize() != minTextSize
222 || secondButton.getTextSize() != minTextSize) {
223 firstButton.setTextSize(minTextSize);
224 secondButton.setTextSize(minTextSize);
225 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700226 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800227 setMeasuredDimension(width, height);
228 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700229
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800230 @Override
231 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Alex Chau906d8822022-05-23 10:42:25 +0100232 int visibleCount = getVisibleButtons(mTempTargets);
Sunny Goyala4647b62021-02-02 13:45:34 -0800233 if (visibleCount == 0) {
Pat Manningde25c0d2022-04-05 19:11:26 +0100234 return;
235 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800236
Alex Chau906d8822022-05-23 10:42:25 +0100237 DeviceProfile dp = mLauncher.getDeviceProfile();
238 // Center vertical bar over scaled workspace, accounting for hotseat offset.
Pat Manning5f74bfd2022-07-20 12:08:54 +0100239 float scale = dp.getWorkspaceSpringLoadScale(mLauncher);
Alex Chau906d8822022-05-23 10:42:25 +0100240 Workspace<?> ws = mLauncher.getWorkspace();
241 int barCenter;
242 if (dp.isTwoPanels) {
243 barCenter = (right - left) / 2;
244 } else {
245 int workspaceCenter = (ws.getLeft() + ws.getRight()) / 2;
246 int cellLayoutCenter = ((dp.getInsets().left + dp.workspacePadding.left) + (dp.widthPx
247 - dp.getInsets().right - dp.workspacePadding.right)) / 2;
248 int cellLayoutCenterOffset = (int) ((cellLayoutCenter - workspaceCenter) * scale);
249 barCenter = workspaceCenter + cellLayoutCenterOffset - left;
250 }
Pat Manningde25c0d2022-04-05 19:11:26 +0100251
252 if (visibleCount == 1) {
Alex Chau906d8822022-05-23 10:42:25 +0100253 ButtonDropTarget button = mTempTargets[0];
Pat Manningde25c0d2022-04-05 19:11:26 +0100254 button.layout(barCenter - (button.getMeasuredWidth() / 2), 0,
255 barCenter + (button.getMeasuredWidth() / 2), button.getMeasuredHeight());
256 } else if (visibleCount == 2) {
257 int buttonGap = dp.dropTargetGapPx;
258
Alex Chau906d8822022-05-23 10:42:25 +0100259 ButtonDropTarget leftButton = mTempTargets[0];
Alex Chau906d8822022-05-23 10:42:25 +0100260 ButtonDropTarget rightButton = mTempTargets[1];
Alex Chau5b019302022-05-23 10:42:25 +0100261 if (dp.isTwoPanels) {
262 leftButton.layout(barCenter - leftButton.getMeasuredWidth() - (buttonGap / 2), 0,
263 barCenter - (buttonGap / 2), leftButton.getMeasuredHeight());
264 rightButton.layout(barCenter + (buttonGap / 2), 0,
265 barCenter + (buttonGap / 2) + rightButton.getMeasuredWidth(),
266 rightButton.getMeasuredHeight());
267 } else {
268 int scaledPanelWidth = (int) (dp.getCellLayoutWidth() * scale);
269
270 int leftButtonWidth = leftButton.getMeasuredWidth();
271 int rightButtonWidth = rightButton.getMeasuredWidth();
272 int extraSpace = scaledPanelWidth - leftButtonWidth - rightButtonWidth - buttonGap;
273
274 int leftButtonStart = barCenter - (scaledPanelWidth / 2) + extraSpace / 2;
275 int leftButtonEnd = leftButtonStart + leftButtonWidth;
276 int rightButtonStart = leftButtonEnd + buttonGap;
277 int rightButtonEnd = rightButtonStart + rightButtonWidth;
278
279 leftButton.layout(leftButtonStart, 0, leftButtonEnd,
280 leftButton.getMeasuredHeight());
281 rightButton.layout(rightButtonStart, 0, rightButtonEnd,
282 rightButton.getMeasuredHeight());
283 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800284 }
285 }
286
Alex Chau906d8822022-05-23 10:42:25 +0100287 private int getVisibleButtons(ButtonDropTarget[] outVisibleButtons) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800288 int visibleCount = 0;
Alex Chau906d8822022-05-23 10:42:25 +0100289 for (ButtonDropTarget button : mDropTargets) {
290 if (button.getVisibility() != GONE) {
291 outVisibleButtons[visibleCount] = button;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800292 visibleCount++;
293 }
294 }
295 return visibleCount;
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700296 }
297
Hyunyoung Song497708c2019-05-01 16:16:53 -0700298 public void animateToVisibility(boolean isVisible) {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700299 if (mVisible != isVisible) {
300 mVisible = isVisible;
301
302 // Cancel any existing animation
303 if (mCurrentAnimation != null) {
304 mCurrentAnimation.cancel();
305 mCurrentAnimation = null;
306 }
307
308 float finalAlpha = mVisible ? 1 : 0;
309 if (Float.compare(getAlpha(), finalAlpha) != 0) {
310 setVisibility(View.VISIBLE);
311 mCurrentAnimation = animate().alpha(finalAlpha)
312 .setInterpolator(DEFAULT_INTERPOLATOR)
313 .setDuration(DEFAULT_DRAG_FADE_DURATION)
314 .withEndAction(mFadeAnimationEndRunnable);
315 }
316
317 }
318 }
319
Sunny Goyal47328fd2016-05-25 18:56:41 -0700320 /*
321 * DragController.DragListener implementation
322 */
323 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700324 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
Sunny Goyal47328fd2016-05-25 18:56:41 -0700325 animateToVisibility(true);
326 }
327
328 /**
329 * This is called to defer hiding the delete drop target until the drop animation has completed,
330 * instead of hiding immediately when the drag has ended.
331 */
332 protected void deferOnDragEnd() {
333 mDeferOnDragEnd = true;
334 }
335
336 @Override
337 public void onDragEnd() {
338 if (!mDeferOnDragEnd) {
339 animateToVisibility(false);
340 } else {
341 mDeferOnDragEnd = false;
342 }
343 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700344
345 public ButtonDropTarget[] getDropTargets() {
Alex Chau5b019302022-05-23 10:42:25 +0100346 return getVisibility() == View.VISIBLE ? mDropTargets : new ButtonDropTarget[0];
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700347 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700348}