blob: bb6d8b09a2da151bcd23c9e4aecc1e7e9e459e4b [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
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070033import com.android.launcher3.anim.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;
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070045 protected static final TimeInterpolator DEFAULT_INTERPOLATOR = Interpolators.ACCEL;
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);
155 } else if (visibleCount == 2) {
156 DeviceProfile dp = mLauncher.getDeviceProfile();
157 int verticalPadding = dp.dropTargetVerticalPaddingPx;
158 int horizontalPadding = dp.dropTargetHorizontalPaddingPx;
159
160 ButtonDropTarget firstButton = mTempTargets[0];
Pat Manninge63dd252022-08-02 16:15:29 +0100161 firstButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, dp.dropTargetTextSizePx);
Alex Chau906d8822022-05-23 10:42:25 +0100162 firstButton.setTextVisible(true);
163 firstButton.setIconVisible(true);
164 firstButton.setTextMultiLine(false);
Pat Manninge63dd252022-08-02 16:15:29 +0100165 // Reset first button padding in case it was previously changed to multi-line text.
Alex Chau906d8822022-05-23 10:42:25 +0100166 firstButton.setPadding(horizontalPadding, verticalPadding, horizontalPadding,
167 verticalPadding);
168
169 ButtonDropTarget secondButton = mTempTargets[1];
Pat Manninge63dd252022-08-02 16:15:29 +0100170 secondButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, dp.dropTargetTextSizePx);
Alex Chau906d8822022-05-23 10:42:25 +0100171 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
Alex Chau906d8822022-05-23 10:42:25 +0100178 int availableWidth;
179 if (dp.isTwoPanels) {
Pat Manninge63dd252022-08-02 16:15:29 +0100180 // Each button for two panel fits to half the width of the screen excluding the
181 // center gap between the buttons.
182 availableWidth = (dp.availableWidthPx - dp.dropTargetGapPx) / 2;
Alex Chau906d8822022-05-23 10:42:25 +0100183 } else {
Pat Manninge63dd252022-08-02 16:15:29 +0100184 // Both buttons plus the button gap do not display past the edge of the screen.
185 availableWidth = dp.availableWidthPx - dp.dropTargetGapPx;
Pat Manningb29dabc2022-05-20 15:21:48 +0000186 }
Pat Manning27bfcaa2022-04-25 13:50:28 +0100187
Pat Manningb29dabc2022-05-20 15:21:48 +0000188 int widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST);
Alex Chau906d8822022-05-23 10:42:25 +0100189 firstButton.measure(widthSpec, heightSpec);
Alex Chau906d8822022-05-23 10:42:25 +0100190 if (!mIsVertical) {
Pat Manninge3d74b42022-06-08 13:15:13 +0100191 // Remove both icons and put the button's text on two lines if text is truncated.
Alex Chau906d8822022-05-23 10:42:25 +0100192 if (firstButton.isTextTruncated(availableWidth)) {
193 firstButton.setIconVisible(false);
Pat Manninge3d74b42022-06-08 13:15:13 +0100194 secondButton.setIconVisible(false);
Alex Chau906d8822022-05-23 10:42:25 +0100195 firstButton.setTextMultiLine(true);
196 firstButton.setPadding(horizontalPadding, verticalPadding / 2,
197 horizontalPadding, verticalPadding / 2);
198 }
Alex Chau5b019302022-05-23 10:42:25 +0100199 }
200
201 if (!dp.isTwoPanels) {
202 availableWidth -= firstButton.getMeasuredWidth() + dp.dropTargetGapPx;
203 widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST);
204 }
205 secondButton.measure(widthSpec, heightSpec);
206 if (!mIsVertical) {
Pat Manninge3d74b42022-06-08 13:15:13 +0100207 // Remove both icons and put the button's text on two lines if text is truncated.
Alex Chau906d8822022-05-23 10:42:25 +0100208 if (secondButton.isTextTruncated(availableWidth)) {
209 secondButton.setIconVisible(false);
Pat Manninge3d74b42022-06-08 13:15:13 +0100210 firstButton.setIconVisible(false);
Alex Chau906d8822022-05-23 10:42:25 +0100211 secondButton.setTextMultiLine(true);
212 secondButton.setPadding(horizontalPadding, verticalPadding / 2,
213 horizontalPadding, verticalPadding / 2);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700214 }
215 }
Pat Manninge63dd252022-08-02 16:15:29 +0100216
217 // If text is still truncated, shrink to fit in measured width and resize both targets.
218 float minTextSize =
219 Math.min(firstButton.resizeTextToFit(), secondButton.resizeTextToFit());
220 if (firstButton.getTextSize() != minTextSize
221 || secondButton.getTextSize() != minTextSize) {
222 firstButton.setTextSize(minTextSize);
223 secondButton.setTextSize(minTextSize);
224 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700225 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800226 setMeasuredDimension(width, height);
227 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700228
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800229 @Override
230 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Alex Chau906d8822022-05-23 10:42:25 +0100231 int visibleCount = getVisibleButtons(mTempTargets);
Sunny Goyala4647b62021-02-02 13:45:34 -0800232 if (visibleCount == 0) {
Pat Manningde25c0d2022-04-05 19:11:26 +0100233 return;
234 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800235
Alex Chau906d8822022-05-23 10:42:25 +0100236 DeviceProfile dp = mLauncher.getDeviceProfile();
237 // Center vertical bar over scaled workspace, accounting for hotseat offset.
Pat Manning5f74bfd2022-07-20 12:08:54 +0100238 float scale = dp.getWorkspaceSpringLoadScale(mLauncher);
Alex Chau906d8822022-05-23 10:42:25 +0100239 Workspace<?> ws = mLauncher.getWorkspace();
240 int barCenter;
241 if (dp.isTwoPanels) {
242 barCenter = (right - left) / 2;
243 } else {
244 int workspaceCenter = (ws.getLeft() + ws.getRight()) / 2;
245 int cellLayoutCenter = ((dp.getInsets().left + dp.workspacePadding.left) + (dp.widthPx
246 - dp.getInsets().right - dp.workspacePadding.right)) / 2;
247 int cellLayoutCenterOffset = (int) ((cellLayoutCenter - workspaceCenter) * scale);
248 barCenter = workspaceCenter + cellLayoutCenterOffset - left;
249 }
Pat Manningde25c0d2022-04-05 19:11:26 +0100250
251 if (visibleCount == 1) {
Alex Chau906d8822022-05-23 10:42:25 +0100252 ButtonDropTarget button = mTempTargets[0];
Pat Manningde25c0d2022-04-05 19:11:26 +0100253 button.layout(barCenter - (button.getMeasuredWidth() / 2), 0,
254 barCenter + (button.getMeasuredWidth() / 2), button.getMeasuredHeight());
255 } else if (visibleCount == 2) {
256 int buttonGap = dp.dropTargetGapPx;
257
Alex Chau906d8822022-05-23 10:42:25 +0100258 ButtonDropTarget leftButton = mTempTargets[0];
Alex Chau906d8822022-05-23 10:42:25 +0100259 ButtonDropTarget rightButton = mTempTargets[1];
Alex Chau5b019302022-05-23 10:42:25 +0100260 if (dp.isTwoPanels) {
261 leftButton.layout(barCenter - leftButton.getMeasuredWidth() - (buttonGap / 2), 0,
262 barCenter - (buttonGap / 2), leftButton.getMeasuredHeight());
263 rightButton.layout(barCenter + (buttonGap / 2), 0,
264 barCenter + (buttonGap / 2) + rightButton.getMeasuredWidth(),
265 rightButton.getMeasuredHeight());
266 } else {
267 int scaledPanelWidth = (int) (dp.getCellLayoutWidth() * scale);
268
269 int leftButtonWidth = leftButton.getMeasuredWidth();
270 int rightButtonWidth = rightButton.getMeasuredWidth();
271 int extraSpace = scaledPanelWidth - leftButtonWidth - rightButtonWidth - buttonGap;
272
273 int leftButtonStart = barCenter - (scaledPanelWidth / 2) + extraSpace / 2;
274 int leftButtonEnd = leftButtonStart + leftButtonWidth;
275 int rightButtonStart = leftButtonEnd + buttonGap;
276 int rightButtonEnd = rightButtonStart + rightButtonWidth;
277
278 leftButton.layout(leftButtonStart, 0, leftButtonEnd,
279 leftButton.getMeasuredHeight());
280 rightButton.layout(rightButtonStart, 0, rightButtonEnd,
281 rightButton.getMeasuredHeight());
282 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800283 }
284 }
285
Alex Chau906d8822022-05-23 10:42:25 +0100286 private int getVisibleButtons(ButtonDropTarget[] outVisibleButtons) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800287 int visibleCount = 0;
Alex Chau906d8822022-05-23 10:42:25 +0100288 for (ButtonDropTarget button : mDropTargets) {
289 if (button.getVisibility() != GONE) {
290 outVisibleButtons[visibleCount] = button;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800291 visibleCount++;
292 }
293 }
294 return visibleCount;
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700295 }
296
Hyunyoung Song497708c2019-05-01 16:16:53 -0700297 public void animateToVisibility(boolean isVisible) {
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) {
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 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700347}