blob: 6a8ba1bec4cbc2d80cfd8a77b1543416221f87ae [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
Pat Manning27bfcaa2022-04-25 13:50:28 +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;
Pat Manning27bfcaa2022-04-25 13:50:28 +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);
Pat Manning27bfcaa2022-04-25 13:50:28 +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);
Pat Manning27bfcaa2022-04-25 13:50:28 +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 }
Pat Manning27bfcaa2022-04-25 13:50:28 +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();
Pat Manning27bfcaa2022-04-25 13:50:28 +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
Pat Manning27bfcaa2022-04-25 13:50:28 +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);
Pat Manning27bfcaa2022-04-25 13:50:28 +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);
Pat Manning27bfcaa2022-04-25 13:50:28 +0100147 int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700148
Pat Manning27bfcaa2022-04-25 13:50:28 +0100149 int visibleCount = getVisibleButtons(mTempTargets);
150 if (visibleCount == 1) {
151 int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST);
Sebastian Francod2d8e972022-04-04 14:51:53 -0700152
Pat Manning27bfcaa2022-04-25 13:50:28 +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;
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700161
Pat Manning27bfcaa2022-04-25 13:50:28 +0100162 ButtonDropTarget firstButton = mTempTargets[0];
163 firstButton.setTextVisible(true);
164 firstButton.setIconVisible(true);
165
166 ButtonDropTarget secondButton = mTempTargets[1];
167 secondButton.setTextVisible(true);
168 secondButton.setIconVisible(true);
169 secondButton.setTextMultiLine(false);
170 // Reset second button padding in case it was previously changed to multi-line text.
171 secondButton.setPadding(horizontalPadding, verticalPadding, horizontalPadding,
172 verticalPadding);
173
174 if (dp.isTwoPanels) {
175 // Both buttons for two panel fit to the width of one Cell Layout (less
176 // half of the center gap between the buttons).
177 float scale = dp.getWorkspaceSpringLoadScale();
178 int scaledPanelWidth = (int) (dp.getCellLayoutWidth() * scale);
179 int halfButtonGap = dp.dropTargetGapPx / 2;
180 scaledPanelWidth -= halfButtonGap / 2;
181
182 int widthSpec = MeasureSpec.makeMeasureSpec(scaledPanelWidth, MeasureSpec.AT_MOST);
183 firstButton.measure(widthSpec, heightSpec);
184 secondButton.measure(widthSpec, heightSpec);
185 } else {
186 int availableWidth;
187 int buttonGap = dp.dropTargetGapPx;
188 if (mIsVertical) {
189 // Both buttons plus the button gap do not display past the edge of the
190 // scaled workspace, less a pre-defined gap from the edge of the workspace.
191 float scale = dp.getWorkspaceSpringLoadScale();
192 int panelWidth = (int) (dp.getCellLayoutWidth() * scale);
193 availableWidth = Math.min(
194 panelWidth - (2 * dp.dropTargetButtonWorkspaceEdgeGapPx), width);
195 } else {
196 // Both buttons plus the button gap display up to a pre-defined margin of
197 // the unscaled workspace edge.
198 availableWidth = Math.min(
199 dp.availableWidthPx - (2 * dp.dropTargetButtonScreenEdgeGapPx),
200 width);
201 }
202 int widthSpec = MeasureSpec.makeMeasureSpec(availableWidth - buttonGap,
203 MeasureSpec.AT_MOST);
204
205 // First button's width is at most the drop target bar's total width less the button
206 // gap.
207 firstButton.measure(widthSpec, heightSpec);
208
209 int usedWidth = firstButton.getMeasuredWidth() + buttonGap;
210 int remainingWidth = availableWidth - usedWidth;
211 widthSpec = MeasureSpec.makeMeasureSpec(remainingWidth, MeasureSpec.AT_MOST);
212 secondButton.measure(widthSpec, heightSpec);
213
214 // Remove both icons and put the second button's text on two lines if text is
215 // truncated on phones. We assume first button's text is never truncated, so it
216 // remains single-line.
217 if (secondButton.isTextTruncated(remainingWidth) && !mIsVertical) {
218 firstButton.setIconVisible(false);
219 secondButton.setIconVisible(false);
220 secondButton.setTextMultiLine(true);
221 secondButton.setPadding(secondButton.getPaddingLeft(),
222 secondButton.getPaddingTop() / 2, secondButton.getPaddingRight(),
223 secondButton.getPaddingBottom() / 2);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700224 }
225 }
226 }
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) {
Pat Manning27bfcaa2022-04-25 13:50:28 +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
Pat Manning27bfcaa2022-04-25 13:50:28 +0100237 DeviceProfile dp = mLauncher.getDeviceProfile();
Pat Manningde25c0d2022-04-05 19:11:26 +0100238 int barCenter = (right - left) / 2;
Pat Manning27bfcaa2022-04-25 13:50:28 +0100239 if (mIsVertical) {
240 // Center vertical bar over scaled workspace, accounting for hotseat offset.
241 float scale = dp.getWorkspaceSpringLoadScale();
242 Workspace<?> ws = mLauncher.getWorkspace();
243 int workspaceCenter = (ws.getLeft() + ws.getRight()) / 2;
244 int cellLayoutCenter = ((dp.getInsets().left + dp.workspacePadding.left) + (dp.widthPx
245 - dp.getInsets().right - dp.workspacePadding.right)) / 2;
246 int cellLayoutCenterOffset = (int) ((cellLayoutCenter - workspaceCenter) * scale);
247 barCenter = workspaceCenter + cellLayoutCenterOffset;
248 }
Pat Manningde25c0d2022-04-05 19:11:26 +0100249
250 if (visibleCount == 1) {
Pat Manning27bfcaa2022-04-25 13:50:28 +0100251 ButtonDropTarget button = mTempTargets[0];
Pat Manningde25c0d2022-04-05 19:11:26 +0100252 button.layout(barCenter - (button.getMeasuredWidth() / 2), 0,
253 barCenter + (button.getMeasuredWidth() / 2), button.getMeasuredHeight());
254 } else if (visibleCount == 2) {
255 int buttonGap = dp.dropTargetGapPx;
256
257 if (dp.isTwoPanels) {
Pat Manning27bfcaa2022-04-25 13:50:28 +0100258 ButtonDropTarget leftButton = mTempTargets[0];
Pat Manningde25c0d2022-04-05 19:11:26 +0100259 leftButton.layout(barCenter - leftButton.getMeasuredWidth() - (buttonGap / 2), 0,
260 barCenter - (buttonGap / 2), leftButton.getMeasuredHeight());
261
Pat Manning27bfcaa2022-04-25 13:50:28 +0100262 ButtonDropTarget rightButton = mTempTargets[1];
Pat Manningde25c0d2022-04-05 19:11:26 +0100263 rightButton.layout(barCenter + (buttonGap / 2), 0,
Pat Manning27bfcaa2022-04-25 13:50:28 +0100264 barCenter + (buttonGap / 2) + rightButton.getMeasuredWidth(),
Pat Manningde25c0d2022-04-05 19:11:26 +0100265 rightButton.getMeasuredHeight());
Pat Manning27bfcaa2022-04-25 13:50:28 +0100266 } else {
267 int start;
268 int end;
269 if (mIsVertical) {
270 // Scaled CellLayout width is assumed to not exceed the bounds of left/right.
271 float scale = dp.getWorkspaceSpringLoadScale();
272 int panelWidth = (int) (dp.getCellLayoutWidth() * scale);
273 start = barCenter - (panelWidth / 2) + dp.dropTargetButtonWorkspaceEdgeGapPx;
274 end = barCenter + (panelWidth / 2) - dp.dropTargetButtonWorkspaceEdgeGapPx;
275 } else {
276 start = Math.max(dp.dropTargetButtonScreenEdgeGapPx, left);
277 end = Math.min(dp.availableWidthPx - dp.dropTargetButtonScreenEdgeGapPx, right);
Pat Manningde25c0d2022-04-05 19:11:26 +0100278 }
279
Pat Manning27bfcaa2022-04-25 13:50:28 +0100280 ButtonDropTarget leftButton = mTempTargets[0];
281 ButtonDropTarget rightButton = mTempTargets[1];
282
283 int leftButtonWidth = leftButton.getMeasuredWidth();
284 int rightButtonWidth = rightButton.getMeasuredWidth();
285 int buttonPlusGapWidth = leftButtonWidth + buttonGap + rightButtonWidth;
286
287 int extraSpace = end - start - buttonPlusGapWidth;
288 start = (start - left) + (extraSpace / 2);
289
290 leftButton.layout(start, 0, start + leftButtonWidth,
Pat Manningde25c0d2022-04-05 19:11:26 +0100291 leftButton.getMeasuredHeight());
Pat Manning27bfcaa2022-04-25 13:50:28 +0100292
293 int rightButtonStart = start + leftButtonWidth + buttonGap;
294 rightButton.layout(rightButtonStart, 0, rightButtonStart + rightButtonWidth,
Pat Manningde25c0d2022-04-05 19:11:26 +0100295 rightButton.getMeasuredHeight());
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800296 }
297 }
298 }
299
Pat Manning27bfcaa2022-04-25 13:50:28 +0100300 private int getVisibleButtons(ButtonDropTarget[] outVisibleButtons) {
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800301 int visibleCount = 0;
Pat Manning27bfcaa2022-04-25 13:50:28 +0100302 for (ButtonDropTarget button : mDropTargets) {
303 if (button.getVisibility() != GONE) {
304 outVisibleButtons[visibleCount] = button;
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800305 visibleCount++;
306 }
307 }
308 return visibleCount;
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700309 }
310
Hyunyoung Song497708c2019-05-01 16:16:53 -0700311 public void animateToVisibility(boolean isVisible) {
vadimt89d94232021-09-01 12:33:00 -0700312 if (TestProtocol.sDebugTracing) {
313 Log.d(TestProtocol.NO_DROP_TARGET, "8");
314 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700315 if (mVisible != isVisible) {
316 mVisible = isVisible;
317
318 // Cancel any existing animation
319 if (mCurrentAnimation != null) {
320 mCurrentAnimation.cancel();
321 mCurrentAnimation = null;
322 }
323
324 float finalAlpha = mVisible ? 1 : 0;
325 if (Float.compare(getAlpha(), finalAlpha) != 0) {
326 setVisibility(View.VISIBLE);
327 mCurrentAnimation = animate().alpha(finalAlpha)
328 .setInterpolator(DEFAULT_INTERPOLATOR)
329 .setDuration(DEFAULT_DRAG_FADE_DURATION)
330 .withEndAction(mFadeAnimationEndRunnable);
331 }
332
333 }
334 }
335
Sunny Goyal47328fd2016-05-25 18:56:41 -0700336 /*
337 * DragController.DragListener implementation
338 */
339 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700340 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
vadimt89d94232021-09-01 12:33:00 -0700341 if (TestProtocol.sDebugTracing) {
342 Log.d(TestProtocol.NO_DROP_TARGET, "7");
343 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700344 animateToVisibility(true);
345 }
346
347 /**
348 * This is called to defer hiding the delete drop target until the drop animation has completed,
349 * instead of hiding immediately when the drag has ended.
350 */
351 protected void deferOnDragEnd() {
352 mDeferOnDragEnd = true;
353 }
354
355 @Override
356 public void onDragEnd() {
357 if (!mDeferOnDragEnd) {
358 animateToVisibility(false);
359 } else {
360 mDeferOnDragEnd = false;
361 }
362 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700363
364 public ButtonDropTarget[] getDropTargets() {
365 return mDropTargets;
366 }
vadimt89d94232021-09-01 12:33:00 -0700367
368 @Override
369 protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
370 super.onVisibilityChanged(changedView, visibility);
Vadim Tryshev8629be72021-10-26 19:57:49 +0000371 if (TestProtocol.sDebugTracing) {
372 if (visibility == VISIBLE) {
373 Log.d(TestProtocol.NO_DROP_TARGET, "9");
374 } else {
375 Log.d(TestProtocol.NO_DROP_TARGET, "Hiding drop target", new Exception());
376 }
vadimt89d94232021-09-01 12:33:00 -0700377 }
378 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700379}