Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher3; |
| 18 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 19 | import static com.android.launcher3.ButtonDropTarget.TOOLTIP_DEFAULT; |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 20 | import static com.android.launcher3.anim.AlphaUpdateListener.updateVisibility; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 21 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 22 | import android.animation.TimeInterpolator; |
| 23 | import android.content.Context; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 24 | import android.graphics.Rect; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 25 | import android.util.AttributeSet; |
vadimt | 89d9423 | 2021-09-01 12:33:00 -0700 | [diff] [blame] | 26 | import android.util.Log; |
Alex Chau | a02eddc | 2021-04-29 00:36:06 +0100 | [diff] [blame] | 27 | import android.util.TypedValue; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 28 | import android.view.Gravity; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 29 | import android.view.View; |
| 30 | import android.view.ViewDebug; |
| 31 | import android.view.ViewPropertyAnimator; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 32 | import android.widget.FrameLayout; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 33 | |
vadimt | 89d9423 | 2021-09-01 12:33:00 -0700 | [diff] [blame] | 34 | import androidx.annotation.NonNull; |
| 35 | |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 36 | import com.android.launcher3.anim.Interpolators; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 37 | import com.android.launcher3.dragndrop.DragController; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 38 | import com.android.launcher3.dragndrop.DragController.DragListener; |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 39 | import com.android.launcher3.dragndrop.DragOptions; |
vadimt | 89d9423 | 2021-09-01 12:33:00 -0700 | [diff] [blame] | 40 | import com.android.launcher3.testing.TestProtocol; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * The top bar containing various drop targets: Delete/App Info/Uninstall. |
| 44 | */ |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 45 | public class DropTargetBar extends FrameLayout |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 46 | implements DragListener, Insettable { |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 47 | |
| 48 | protected static final int DEFAULT_DRAG_FADE_DURATION = 175; |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 49 | protected static final TimeInterpolator DEFAULT_INTERPOLATOR = Interpolators.ACCEL; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 50 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 51 | private final Runnable mFadeAnimationEndRunnable = |
Sunny Goyal | 18d7184 | 2018-03-27 13:44:00 -0700 | [diff] [blame] | 52 | () -> updateVisibility(DropTargetBar.this); |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 53 | |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 54 | private final Launcher mLauncher; |
| 55 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 56 | @ViewDebug.ExportedProperty(category = "launcher") |
| 57 | protected boolean mDeferOnDragEnd; |
| 58 | |
| 59 | @ViewDebug.ExportedProperty(category = "launcher") |
| 60 | protected boolean mVisible = false; |
| 61 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 62 | private ButtonDropTarget[] mDropTargets; |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 63 | private ButtonDropTarget[] mTempTargets; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 64 | private ViewPropertyAnimator mCurrentAnimation; |
| 65 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 66 | private boolean mIsVertical = true; |
| 67 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 68 | public DropTargetBar(Context context, AttributeSet attrs) { |
| 69 | super(context, attrs); |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 70 | mLauncher = Launcher.getLauncher(context); |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | public DropTargetBar(Context context, AttributeSet attrs, int defStyle) { |
| 74 | super(context, attrs, defStyle); |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 75 | mLauncher = Launcher.getLauncher(context); |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | protected void onFinishInflate() { |
| 80 | super.onFinishInflate(); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 81 | 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 Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 86 | mTempTargets = new ButtonDropTarget[getChildCount()]; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 89 | @Override |
| 90 | public void setInsets(Rect insets) { |
| 91 | FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams(); |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 92 | DeviceProfile grid = mLauncher.getDeviceProfile(); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 93 | mIsVertical = grid.isVerticalBarLayout(); |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 94 | |
| 95 | lp.leftMargin = insets.left; |
| 96 | lp.topMargin = insets.top; |
| 97 | lp.bottomMargin = insets.bottom; |
| 98 | lp.rightMargin = insets.right; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 99 | int tooltipLocation = TOOLTIP_DEFAULT; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 100 | |
Pat Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 101 | 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 Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 109 | } else { |
Pat Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 110 | horizontalMargin = getContext().getResources() |
| 111 | .getDimensionPixelSize(R.dimen.drop_target_bar_margin_horizontal); |
Pat Manning | e372366 | 2022-03-25 16:07:46 +0000 | [diff] [blame] | 112 | } |
Pat Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 113 | 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 Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 123 | DeviceProfile dp = mLauncher.getDeviceProfile(); |
| 124 | int horizontalPadding = dp.dropTargetHorizontalPaddingPx; |
| 125 | int verticalPadding = dp.dropTargetVerticalPaddingPx; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 126 | setLayoutParams(lp); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 127 | for (ButtonDropTarget button : mDropTargets) { |
Alex Chau | a02eddc | 2021-04-29 00:36:06 +0100 | [diff] [blame] | 128 | button.setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.dropTargetTextSizePx); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 129 | button.setToolTipLocation(tooltipLocation); |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 130 | button.setPadding(horizontalPadding, verticalPadding, horizontalPadding, |
| 131 | verticalPadding); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 132 | } |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 135 | public void setup(DragController dragController) { |
| 136 | dragController.addDragListener(this); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 137 | for (int i = 0; i < mDropTargets.length; i++) { |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 138 | dragController.addDragListener(mDropTargets[i]); |
| 139 | dragController.addDropTarget(mDropTargets[i]); |
| 140 | } |
| 141 | } |
| 142 | |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 143 | @Override |
| 144 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 145 | int width = MeasureSpec.getSize(widthMeasureSpec); |
| 146 | int height = MeasureSpec.getSize(heightMeasureSpec); |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 147 | int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 148 | |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 149 | int visibleCount = getVisibleButtons(mTempTargets); |
| 150 | if (visibleCount == 1) { |
| 151 | int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST); |
Sebastian Franco | d2d8e97 | 2022-04-04 14:51:53 -0700 | [diff] [blame] | 152 | |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 153 | 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 Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 161 | |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 162 | 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 Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | } |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 227 | setMeasuredDimension(width, height); |
| 228 | } |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 229 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 230 | @Override |
| 231 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 232 | int visibleCount = getVisibleButtons(mTempTargets); |
Sunny Goyal | a4647b6 | 2021-02-02 13:45:34 -0800 | [diff] [blame] | 233 | if (visibleCount == 0) { |
Pat Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 234 | return; |
| 235 | } |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 236 | |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 237 | DeviceProfile dp = mLauncher.getDeviceProfile(); |
Pat Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 238 | int barCenter = (right - left) / 2; |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 239 | 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 Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 249 | |
| 250 | if (visibleCount == 1) { |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 251 | ButtonDropTarget button = mTempTargets[0]; |
Pat Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 252 | 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 Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 258 | ButtonDropTarget leftButton = mTempTargets[0]; |
Pat Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 259 | leftButton.layout(barCenter - leftButton.getMeasuredWidth() - (buttonGap / 2), 0, |
| 260 | barCenter - (buttonGap / 2), leftButton.getMeasuredHeight()); |
| 261 | |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 262 | ButtonDropTarget rightButton = mTempTargets[1]; |
Pat Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 263 | rightButton.layout(barCenter + (buttonGap / 2), 0, |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 264 | barCenter + (buttonGap / 2) + rightButton.getMeasuredWidth(), |
Pat Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 265 | rightButton.getMeasuredHeight()); |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 266 | } 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 Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 280 | 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 Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 291 | leftButton.getMeasuredHeight()); |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 292 | |
| 293 | int rightButtonStart = start + leftButtonWidth + buttonGap; |
| 294 | rightButton.layout(rightButtonStart, 0, rightButtonStart + rightButtonWidth, |
Pat Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 295 | rightButton.getMeasuredHeight()); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 300 | private int getVisibleButtons(ButtonDropTarget[] outVisibleButtons) { |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 301 | int visibleCount = 0; |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame^] | 302 | for (ButtonDropTarget button : mDropTargets) { |
| 303 | if (button.getVisibility() != GONE) { |
| 304 | outVisibleButtons[visibleCount] = button; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 305 | visibleCount++; |
| 306 | } |
| 307 | } |
| 308 | return visibleCount; |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Hyunyoung Song | 497708c | 2019-05-01 16:16:53 -0700 | [diff] [blame] | 311 | public void animateToVisibility(boolean isVisible) { |
vadimt | 89d9423 | 2021-09-01 12:33:00 -0700 | [diff] [blame] | 312 | if (TestProtocol.sDebugTracing) { |
| 313 | Log.d(TestProtocol.NO_DROP_TARGET, "8"); |
| 314 | } |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 315 | 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 Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 336 | /* |
| 337 | * DragController.DragListener implementation |
| 338 | */ |
| 339 | @Override |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 340 | public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) { |
vadimt | 89d9423 | 2021-09-01 12:33:00 -0700 | [diff] [blame] | 341 | if (TestProtocol.sDebugTracing) { |
| 342 | Log.d(TestProtocol.NO_DROP_TARGET, "7"); |
| 343 | } |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 344 | 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 Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 363 | |
| 364 | public ButtonDropTarget[] getDropTargets() { |
| 365 | return mDropTargets; |
| 366 | } |
vadimt | 89d9423 | 2021-09-01 12:33:00 -0700 | [diff] [blame] | 367 | |
| 368 | @Override |
| 369 | protected void onVisibilityChanged(@NonNull View changedView, int visibility) { |
| 370 | super.onVisibilityChanged(changedView, visibility); |
Vadim Tryshev | 8629be7 | 2021-10-26 19:57:49 +0000 | [diff] [blame] | 371 | 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 | } |
vadimt | 89d9423 | 2021-09-01 12:33:00 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 379 | } |