blob: 73289fb87853c7e8ae971fd1e35e25b6ba007e3d [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;
143 for (ButtonDropTarget buttons : mDropTargets) {
144 if (buttons.getVisibility() != GONE) {
145 textVisible = textVisible && !buttons.isTextTruncated(availableWidth);
146 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700147 }
148
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800149 int widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST);
150 int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
151 for (ButtonDropTarget button : mDropTargets) {
152 if (button.getVisibility() != GONE) {
153 button.setTextVisible(textVisible);
154 button.measure(widthSpec, heightSpec);
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700155 }
156 }
157 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800158 setMeasuredDimension(width, height);
159 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700160
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800161 @Override
162 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyala4647b62021-02-02 13:45:34 -0800163 int visibleCount = getVisibleButtonsCount();
164 if (visibleCount == 0) {
Pat Manningde25c0d2022-04-05 19:11:26 +0100165 return;
166 }
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800167
Pat Manningde25c0d2022-04-05 19:11:26 +0100168 Launcher launcher = Launcher.getLauncher(getContext());
169 Workspace workspace = launcher.getWorkspace();
170 DeviceProfile dp = launcher.getDeviceProfile();
171 int buttonHorizontalPadding = dp.dropTargetHorizontalPaddingPx;
172 int buttonVerticalPadding = dp.dropTargetVerticalPaddingPx;
173 int barCenter = (right - left) / 2;
Pat Manning1acf2b12022-02-25 15:41:55 +0000174
Pat Manningde25c0d2022-04-05 19:11:26 +0100175 ButtonDropTarget[] visibleButtons = Arrays.stream(mDropTargets)
176 .filter(b -> b.getVisibility() != GONE)
177 .toArray(ButtonDropTarget[]::new);
178 Arrays.stream(visibleButtons).forEach(
179 b -> b.setPadding(buttonHorizontalPadding, buttonVerticalPadding,
180 buttonHorizontalPadding, buttonVerticalPadding));
181
182 if (visibleCount == 1) {
183 ButtonDropTarget button = visibleButtons[0];
184 button.layout(barCenter - (button.getMeasuredWidth() / 2), 0,
185 barCenter + (button.getMeasuredWidth() / 2), button.getMeasuredHeight());
186 } else if (visibleCount == 2) {
187 int buttonGap = dp.dropTargetGapPx;
188
189 if (dp.isTwoPanels) {
190 ButtonDropTarget leftButton = visibleButtons[0];
191 leftButton.layout(barCenter - leftButton.getMeasuredWidth() - (buttonGap / 2), 0,
192 barCenter - (buttonGap / 2), leftButton.getMeasuredHeight());
193
194 ButtonDropTarget rightButton = visibleButtons[1];
195 rightButton.layout(barCenter + (buttonGap / 2), 0,
196 barCenter + rightButton.getMeasuredWidth() + (buttonGap / 2),
197 rightButton.getMeasuredHeight());
198 } else if (dp.isTablet) {
199 int numberOfMargins = visibleCount - 1;
200 int buttonWidths = Arrays.stream(mDropTargets)
201 .filter(b -> b.getVisibility() != GONE)
202 .mapToInt(ButtonDropTarget::getMeasuredWidth)
203 .sum();
204 int totalWidth = buttonWidths + (numberOfMargins * buttonGap);
205 int buttonsStartMargin = barCenter - (totalWidth / 2);
206
207 int start = buttonsStartMargin;
208 for (ButtonDropTarget button : visibleButtons) {
209 int margin = (start != buttonsStartMargin) ? buttonGap : 0;
210 button.layout(start + margin, 0, start + margin + button.getMeasuredWidth(),
211 button.getMeasuredHeight());
212 start += button.getMeasuredWidth() + margin;
Pat Manning1acf2b12022-02-25 15:41:55 +0000213 }
Pat Manningde25c0d2022-04-05 19:11:26 +0100214 } else if (mIsVertical) {
215 // Center buttons over workspace, not screen.
216 int verticalCenter = (workspace.getRight() - workspace.getLeft()) / 2;
217 ButtonDropTarget leftButton = visibleButtons[0];
218 leftButton.layout(verticalCenter - leftButton.getMeasuredWidth() - (buttonGap / 2),
219 0, verticalCenter - (buttonGap / 2), leftButton.getMeasuredHeight());
220
221 ButtonDropTarget rightButton = visibleButtons[1];
222 rightButton.layout(verticalCenter + (buttonGap / 2), 0,
223 verticalCenter + rightButton.getMeasuredWidth() + (buttonGap / 2),
224 rightButton.getMeasuredHeight());
225 } else if (dp.isPhone) {
226 // Buttons aligned to outer edges of scaled workspace.
227 float shrunkTop = dp.getWorkspaceSpringLoadShrunkTop();
228 float shrunkBottom = dp.getWorkspaceSpringLoadShrunkBottom();
229 float scale =
230 (shrunkBottom - shrunkTop) / launcher.getWorkspace().getNormalChildHeight();
231 int workspaceWidth = (int) (launcher.getWorkspace().getNormalChildWidth() * scale);
232 int start = barCenter - (workspaceWidth / 2);
233 int end = barCenter + (workspaceWidth / 2);
234
235 ButtonDropTarget leftButton = visibleButtons[0];
236 ButtonDropTarget rightButton = visibleButtons[1];
237
238 // If the text within the buttons is too long, the buttons can overlap
239 int overlap = start + leftButton.getMeasuredWidth() + rightButton.getMeasuredWidth()
240 - end;
241 if (overlap > 0) {
242 start -= overlap / 2;
243 end += overlap / 2;
244 }
245
246 leftButton.layout(start, 0, start + leftButton.getMeasuredWidth(),
247 leftButton.getMeasuredHeight());
248 rightButton.layout(end - rightButton.getMeasuredWidth(), 0, end,
249 rightButton.getMeasuredHeight());
Sunny Goyald1b3f5c2018-01-18 17:14:05 -0800250 }
251 }
252 }
253
254 private int getVisibleButtonsCount() {
255 int visibleCount = 0;
256 for (ButtonDropTarget buttons : mDropTargets) {
257 if (buttons.getVisibility() != GONE) {
258 visibleCount++;
259 }
260 }
261 return visibleCount;
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700262 }
263
Hyunyoung Song497708c2019-05-01 16:16:53 -0700264 public void animateToVisibility(boolean isVisible) {
vadimt89d94232021-09-01 12:33:00 -0700265 if (TestProtocol.sDebugTracing) {
266 Log.d(TestProtocol.NO_DROP_TARGET, "8");
267 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700268 if (mVisible != isVisible) {
269 mVisible = isVisible;
270
271 // Cancel any existing animation
272 if (mCurrentAnimation != null) {
273 mCurrentAnimation.cancel();
274 mCurrentAnimation = null;
275 }
276
277 float finalAlpha = mVisible ? 1 : 0;
278 if (Float.compare(getAlpha(), finalAlpha) != 0) {
279 setVisibility(View.VISIBLE);
280 mCurrentAnimation = animate().alpha(finalAlpha)
281 .setInterpolator(DEFAULT_INTERPOLATOR)
282 .setDuration(DEFAULT_DRAG_FADE_DURATION)
283 .withEndAction(mFadeAnimationEndRunnable);
284 }
285
286 }
287 }
288
Sunny Goyal47328fd2016-05-25 18:56:41 -0700289 /*
290 * DragController.DragListener implementation
291 */
292 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700293 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
vadimt89d94232021-09-01 12:33:00 -0700294 if (TestProtocol.sDebugTracing) {
295 Log.d(TestProtocol.NO_DROP_TARGET, "7");
296 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700297 animateToVisibility(true);
298 }
299
300 /**
301 * This is called to defer hiding the delete drop target until the drop animation has completed,
302 * instead of hiding immediately when the drag has ended.
303 */
304 protected void deferOnDragEnd() {
305 mDeferOnDragEnd = true;
306 }
307
308 @Override
309 public void onDragEnd() {
310 if (!mDeferOnDragEnd) {
311 animateToVisibility(false);
312 } else {
313 mDeferOnDragEnd = false;
314 }
315 }
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700316
317 public ButtonDropTarget[] getDropTargets() {
318 return mDropTargets;
319 }
vadimt89d94232021-09-01 12:33:00 -0700320
321 @Override
322 protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
323 super.onVisibilityChanged(changedView, visibility);
Vadim Tryshev8629be72021-10-26 19:57:49 +0000324 if (TestProtocol.sDebugTracing) {
325 if (visibility == VISIBLE) {
326 Log.d(TestProtocol.NO_DROP_TARGET, "9");
327 } else {
328 Log.d(TestProtocol.NO_DROP_TARGET, "Hiding drop target", new Exception());
329 }
vadimt89d94232021-09-01 12:33:00 -0700330 }
331 }
Sunny Goyal47328fd2016-05-25 18:56:41 -0700332}