blob: 9ae695d26ed57e00214844023573183af0d937e8 [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
Pat Manningde25c0d2022-04-05 19:11:26 +010042import java.util.Arrays;
43
Sunny Goyal47328fd2016-05-25 18:56:41 -070044/*
45 * The top bar containing various drop targets: Delete/App Info/Uninstall.
46 */
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080047public class DropTargetBar extends FrameLayout
Sunny Goyal07b69292018-01-08 14:19:34 -080048 implements DragListener, Insettable {
Sunny Goyal47328fd2016-05-25 18:56:41 -070049
50 protected static final int DEFAULT_DRAG_FADE_DURATION = 175;
Sunny Goyal5bc6b6f2017-10-26 15:36:10 -070051 protected static final TimeInterpolator DEFAULT_INTERPOLATOR = Interpolators.ACCEL;
Sunny Goyal47328fd2016-05-25 18:56:41 -070052
Sunny Goyal07b69292018-01-08 14:19:34 -080053 private final Runnable mFadeAnimationEndRunnable =
Sunny Goyal18d71842018-03-27 13:44:00 -070054 () -> updateVisibility(DropTargetBar.this);
Sunny Goyal47328fd2016-05-25 18:56:41 -070055
56 @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;
Sunny Goyal47328fd2016-05-25 18:56:41 -070063 private ViewPropertyAnimator mCurrentAnimation;
64
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080065 private boolean mIsVertical = true;
66
Sunny Goyal47328fd2016-05-25 18:56:41 -070067 public DropTargetBar(Context context, AttributeSet attrs) {
68 super(context, attrs);
69 }
70
71 public DropTargetBar(Context context, AttributeSet attrs, int defStyle) {
72 super(context, attrs, defStyle);
73 }
74
75 @Override
76 protected void onFinishInflate() {
77 super.onFinishInflate();
Sunny Goyald1b3f5c2018-01-18 17:14:05 -080078 mDropTargets = new ButtonDropTarget[getChildCount()];
79 for (int i = 0; i < mDropTargets.length; i++) {
80 mDropTargets[i] = (ButtonDropTarget) getChildAt(i);
81 mDropTargets[i].setDropTargetBar(this);
82 }
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();
88 DeviceProfile grid = Launcher.getLauncher(getContext()).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;
117 lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
118
Sunny Goyal07b69292018-01-08 14:19:34 -0800119 setLayoutParams(lp);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800120 for (ButtonDropTarget button : mDropTargets) {
Alex Chaua02eddc2021-04-29 00:36:06 +0100121 button.setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.dropTargetTextSizePx);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800122 button.setToolTipLocation(tooltipLocation);
123 }
Sunny Goyal07b69292018-01-08 14:19:34 -0800124 }
125
Sunny Goyal47328fd2016-05-25 18:56:41 -0700126 public void setup(DragController dragController) {
127 dragController.addDragListener(this);
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700128 for (int i = 0; i < mDropTargets.length; i++) {
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700129 dragController.addDragListener(mDropTargets[i]);
130 dragController.addDropTarget(mDropTargets[i]);
131 }
132 }
133
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700134 @Override
135 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800136 int width = MeasureSpec.getSize(widthMeasureSpec);
137 int height = MeasureSpec.getSize(heightMeasureSpec);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700138
Sunny Goyala4647b62021-02-02 13:45:34 -0800139 int visibleCount = getVisibleButtonsCount();
Pat Manningde25c0d2022-04-05 19:11:26 +0100140 if (visibleCount > 0) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800141 int availableWidth = width / visibleCount;
142 boolean textVisible = true;
Sebastian Francod2d8e972022-04-04 14:51:53 -0700143 boolean textResized = false;
144 float textSize = mDropTargets[0].getTextSize();
145 for (ButtonDropTarget button : mDropTargets) {
146 if (button.getVisibility() == GONE) {
147 continue;
148 }
149 if (button.isTextTruncated(availableWidth)) {
150 textSize = Math.min(textSize, button.resizeTextToFit(availableWidth));
151 textResized = true;
152 }
153 textVisible = textVisible && !button.isTextTruncated(availableWidth);
154 }
155
156 if (textResized) {
157 for (ButtonDropTarget button : mDropTargets) {
158 button.setTextSize(textSize);
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800159 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700160 }
161
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800162 int widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST);
163 int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
164 for (ButtonDropTarget button : mDropTargets) {
165 if (button.getVisibility() != GONE) {
166 button.setTextVisible(textVisible);
167 button.measure(widthSpec, heightSpec);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700168 }
169 }
170 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800171 setMeasuredDimension(width, height);
172 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700173
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800174 @Override
175 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyala4647b62021-02-02 13:45:34 -0800176 int visibleCount = getVisibleButtonsCount();
177 if (visibleCount == 0) {
Pat Manningde25c0d2022-04-05 19:11:26 +0100178 return;
179 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800180
Pat Manningde25c0d2022-04-05 19:11:26 +0100181 Launcher launcher = Launcher.getLauncher(getContext());
Shikha Malhotraf78da1b2022-04-11 10:23:18 +0000182 Workspace<?> workspace = launcher.getWorkspace();
Pat Manningde25c0d2022-04-05 19:11:26 +0100183 DeviceProfile dp = launcher.getDeviceProfile();
184 int buttonHorizontalPadding = dp.dropTargetHorizontalPaddingPx;
185 int buttonVerticalPadding = dp.dropTargetVerticalPaddingPx;
186 int barCenter = (right - left) / 2;
Pat Manning1acf2b12022-02-25 15:41:55 +0000187
Pat Manningde25c0d2022-04-05 19:11:26 +0100188 ButtonDropTarget[] visibleButtons = Arrays.stream(mDropTargets)
189 .filter(b -> b.getVisibility() != GONE)
190 .toArray(ButtonDropTarget[]::new);
191 Arrays.stream(visibleButtons).forEach(
192 b -> b.setPadding(buttonHorizontalPadding, buttonVerticalPadding,
193 buttonHorizontalPadding, buttonVerticalPadding));
194
195 if (visibleCount == 1) {
196 ButtonDropTarget button = visibleButtons[0];
197 button.layout(barCenter - (button.getMeasuredWidth() / 2), 0,
198 barCenter + (button.getMeasuredWidth() / 2), button.getMeasuredHeight());
199 } else if (visibleCount == 2) {
200 int buttonGap = dp.dropTargetGapPx;
201
202 if (dp.isTwoPanels) {
203 ButtonDropTarget leftButton = visibleButtons[0];
204 leftButton.layout(barCenter - leftButton.getMeasuredWidth() - (buttonGap / 2), 0,
205 barCenter - (buttonGap / 2), leftButton.getMeasuredHeight());
206
207 ButtonDropTarget rightButton = visibleButtons[1];
208 rightButton.layout(barCenter + (buttonGap / 2), 0,
209 barCenter + rightButton.getMeasuredWidth() + (buttonGap / 2),
210 rightButton.getMeasuredHeight());
211 } else if (dp.isTablet) {
212 int numberOfMargins = visibleCount - 1;
213 int buttonWidths = Arrays.stream(mDropTargets)
214 .filter(b -> b.getVisibility() != GONE)
215 .mapToInt(ButtonDropTarget::getMeasuredWidth)
216 .sum();
217 int totalWidth = buttonWidths + (numberOfMargins * buttonGap);
218 int buttonsStartMargin = barCenter - (totalWidth / 2);
219
220 int start = buttonsStartMargin;
221 for (ButtonDropTarget button : visibleButtons) {
222 int margin = (start != buttonsStartMargin) ? buttonGap : 0;
223 button.layout(start + margin, 0, start + margin + button.getMeasuredWidth(),
224 button.getMeasuredHeight());
225 start += button.getMeasuredWidth() + margin;
Pat Manning1acf2b12022-02-25 15:41:55 +0000226 }
Pat Manningde25c0d2022-04-05 19:11:26 +0100227 } else if (mIsVertical) {
228 // Center buttons over workspace, not screen.
229 int verticalCenter = (workspace.getRight() - workspace.getLeft()) / 2;
230 ButtonDropTarget leftButton = visibleButtons[0];
231 leftButton.layout(verticalCenter - leftButton.getMeasuredWidth() - (buttonGap / 2),
232 0, verticalCenter - (buttonGap / 2), leftButton.getMeasuredHeight());
233
234 ButtonDropTarget rightButton = visibleButtons[1];
235 rightButton.layout(verticalCenter + (buttonGap / 2), 0,
236 verticalCenter + rightButton.getMeasuredWidth() + (buttonGap / 2),
237 rightButton.getMeasuredHeight());
238 } else if (dp.isPhone) {
239 // Buttons aligned to outer edges of scaled workspace.
240 float shrunkTop = dp.getWorkspaceSpringLoadShrunkTop();
241 float shrunkBottom = dp.getWorkspaceSpringLoadShrunkBottom();
242 float scale =
243 (shrunkBottom - shrunkTop) / launcher.getWorkspace().getNormalChildHeight();
244 int workspaceWidth = (int) (launcher.getWorkspace().getNormalChildWidth() * scale);
245 int start = barCenter - (workspaceWidth / 2);
246 int end = barCenter + (workspaceWidth / 2);
247
248 ButtonDropTarget leftButton = visibleButtons[0];
249 ButtonDropTarget rightButton = visibleButtons[1];
250
251 // If the text within the buttons is too long, the buttons can overlap
252 int overlap = start + leftButton.getMeasuredWidth() + rightButton.getMeasuredWidth()
253 - end;
254 if (overlap > 0) {
255 start -= overlap / 2;
256 end += overlap / 2;
257 }
258
259 leftButton.layout(start, 0, start + leftButton.getMeasuredWidth(),
260 leftButton.getMeasuredHeight());
261 rightButton.layout(end - rightButton.getMeasuredWidth(), 0, end,
262 rightButton.getMeasuredHeight());
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800263 }
264 }
265 }
266
267 private int getVisibleButtonsCount() {
268 int visibleCount = 0;
269 for (ButtonDropTarget buttons : mDropTargets) {
270 if (buttons.getVisibility() != GONE) {
271 visibleCount++;
272 }
273 }
274 return visibleCount;
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700275 }
276
Hyunyoung Song497708c2019-05-01 16:16:53 -0700277 public void animateToVisibility(boolean isVisible) {
vadimt89d94232021-09-01 12:33:00 -0700278 if (TestProtocol.sDebugTracing) {
279 Log.d(TestProtocol.NO_DROP_TARGET, "8");
280 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700281 if (mVisible != isVisible) {
282 mVisible = isVisible;
283
284 // Cancel any existing animation
285 if (mCurrentAnimation != null) {
286 mCurrentAnimation.cancel();
287 mCurrentAnimation = null;
288 }
289
290 float finalAlpha = mVisible ? 1 : 0;
291 if (Float.compare(getAlpha(), finalAlpha) != 0) {
292 setVisibility(View.VISIBLE);
293 mCurrentAnimation = animate().alpha(finalAlpha)
294 .setInterpolator(DEFAULT_INTERPOLATOR)
295 .setDuration(DEFAULT_DRAG_FADE_DURATION)
296 .withEndAction(mFadeAnimationEndRunnable);
297 }
298
299 }
300 }
301
Sunny Goyal47328fd2016-05-25 18:56:41 -0700302 /*
303 * DragController.DragListener implementation
304 */
305 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700306 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
vadimt89d94232021-09-01 12:33:00 -0700307 if (TestProtocol.sDebugTracing) {
308 Log.d(TestProtocol.NO_DROP_TARGET, "7");
309 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700310 animateToVisibility(true);
311 }
312
313 /**
314 * This is called to defer hiding the delete drop target until the drop animation has completed,
315 * instead of hiding immediately when the drag has ended.
316 */
317 protected void deferOnDragEnd() {
318 mDeferOnDragEnd = true;
319 }
320
321 @Override
322 public void onDragEnd() {
323 if (!mDeferOnDragEnd) {
324 animateToVisibility(false);
325 } else {
326 mDeferOnDragEnd = false;
327 }
328 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700329
330 public ButtonDropTarget[] getDropTargets() {
331 return mDropTargets;
332 }
vadimt89d94232021-09-01 12:33:00 -0700333
334 @Override
335 protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
336 super.onVisibilityChanged(changedView, visibility);
Vadim Tryshev8629be72021-10-26 19:57:49 +0000337 if (TestProtocol.sDebugTracing) {
338 if (visibility == VISIBLE) {
339 Log.d(TestProtocol.NO_DROP_TARGET, "9");
340 } else {
341 Log.d(TestProtocol.NO_DROP_TARGET, "Hiding drop target", new Exception());
342 }
vadimt89d94232021-09-01 12:33:00 -0700343 }
344 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700345}