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; |
Alex Chau | a02eddc | 2021-04-29 00:36:06 +0100 | [diff] [blame] | 26 | import android.util.TypedValue; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 27 | import android.view.Gravity; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 28 | import android.view.View; |
| 29 | import android.view.ViewDebug; |
| 30 | import android.view.ViewPropertyAnimator; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 31 | import android.widget.FrameLayout; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 32 | |
Kateryna Ivanova | 7120373 | 2023-05-24 15:09:00 +0000 | [diff] [blame] | 33 | import com.android.app.animation.Interpolators; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 34 | import com.android.launcher3.dragndrop.DragController; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 35 | import com.android.launcher3.dragndrop.DragController.DragListener; |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 36 | import com.android.launcher3.dragndrop.DragOptions; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * The top bar containing various drop targets: Delete/App Info/Uninstall. |
| 40 | */ |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 41 | public class DropTargetBar extends FrameLayout |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 42 | implements DragListener, Insettable { |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 43 | |
| 44 | protected static final int DEFAULT_DRAG_FADE_DURATION = 175; |
Kateryna Ivanova | 7120373 | 2023-05-24 15:09:00 +0000 | [diff] [blame] | 45 | protected static final TimeInterpolator DEFAULT_INTERPOLATOR = Interpolators.ACCELERATE; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 46 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 47 | private final Runnable mFadeAnimationEndRunnable = |
Sunny Goyal | 18d7184 | 2018-03-27 13:44:00 -0700 | [diff] [blame] | 48 | () -> updateVisibility(DropTargetBar.this); |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 49 | |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 50 | private final Launcher mLauncher; |
| 51 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 52 | @ViewDebug.ExportedProperty(category = "launcher") |
| 53 | protected boolean mDeferOnDragEnd; |
| 54 | |
| 55 | @ViewDebug.ExportedProperty(category = "launcher") |
| 56 | protected boolean mVisible = false; |
| 57 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 58 | private ButtonDropTarget[] mDropTargets; |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 59 | private ButtonDropTarget[] mTempTargets; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 60 | private ViewPropertyAnimator mCurrentAnimation; |
| 61 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 62 | private boolean mIsVertical = true; |
| 63 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 64 | public DropTargetBar(Context context, AttributeSet attrs) { |
| 65 | super(context, attrs); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 66 | mLauncher = Launcher.getLauncher(context); |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | public DropTargetBar(Context context, AttributeSet attrs, int defStyle) { |
| 70 | super(context, attrs, defStyle); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 71 | mLauncher = Launcher.getLauncher(context); |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | protected void onFinishInflate() { |
| 76 | super.onFinishInflate(); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 77 | 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 Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 82 | mTempTargets = new ButtonDropTarget[getChildCount()]; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 85 | @Override |
| 86 | public void setInsets(Rect insets) { |
| 87 | FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams(); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 88 | DeviceProfile grid = mLauncher.getDeviceProfile(); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 89 | mIsVertical = grid.isVerticalBarLayout(); |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 90 | |
| 91 | lp.leftMargin = insets.left; |
| 92 | lp.topMargin = insets.top; |
| 93 | lp.bottomMargin = insets.bottom; |
| 94 | lp.rightMargin = insets.right; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 95 | int tooltipLocation = TOOLTIP_DEFAULT; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 96 | |
Pat Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 97 | 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 Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 105 | } else { |
Pat Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 106 | horizontalMargin = getContext().getResources() |
| 107 | .getDimensionPixelSize(R.dimen.drop_target_bar_margin_horizontal); |
Pat Manning | e372366 | 2022-03-25 16:07:46 +0000 | [diff] [blame] | 108 | } |
Pat Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 109 | 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; |
fbaron | e74256b | 2023-04-10 14:50:31 -0700 | [diff] [blame] | 117 | lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP; |
Pat Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 118 | |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 119 | DeviceProfile dp = mLauncher.getDeviceProfile(); |
| 120 | int horizontalPadding = dp.dropTargetHorizontalPaddingPx; |
| 121 | int verticalPadding = dp.dropTargetVerticalPaddingPx; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 122 | setLayoutParams(lp); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 123 | for (ButtonDropTarget button : mDropTargets) { |
Alex Chau | a02eddc | 2021-04-29 00:36:06 +0100 | [diff] [blame] | 124 | button.setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.dropTargetTextSizePx); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 125 | button.setToolTipLocation(tooltipLocation); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 126 | button.setPadding(horizontalPadding, verticalPadding, horizontalPadding, |
| 127 | verticalPadding); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 128 | } |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 131 | public void setup(DragController dragController) { |
| 132 | dragController.addDragListener(this); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 133 | for (int i = 0; i < mDropTargets.length; i++) { |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 134 | dragController.addDragListener(mDropTargets[i]); |
| 135 | dragController.addDropTarget(mDropTargets[i]); |
| 136 | } |
| 137 | } |
| 138 | |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 139 | @Override |
| 140 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 141 | int width = MeasureSpec.getSize(widthMeasureSpec); |
| 142 | int height = MeasureSpec.getSize(heightMeasureSpec); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 143 | int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 144 | |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 145 | int visibleCount = getVisibleButtons(mTempTargets); |
| 146 | if (visibleCount == 1) { |
| 147 | int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST); |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame] | 148 | |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 149 | ButtonDropTarget firstButton = mTempTargets[0]; |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame] | 150 | firstButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, |
| 151 | mLauncher.getDeviceProfile().dropTargetTextSizePx); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 152 | firstButton.setTextVisible(true); |
| 153 | firstButton.setIconVisible(true); |
| 154 | firstButton.measure(widthSpec, heightSpec); |
Jordan Silva | 567372f | 2023-03-28 00:33:08 +0100 | [diff] [blame] | 155 | firstButton.resizeTextToFit(); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 156 | } else if (visibleCount == 2) { |
| 157 | DeviceProfile dp = mLauncher.getDeviceProfile(); |
| 158 | int verticalPadding = dp.dropTargetVerticalPaddingPx; |
| 159 | int horizontalPadding = dp.dropTargetHorizontalPaddingPx; |
| 160 | |
| 161 | ButtonDropTarget firstButton = mTempTargets[0]; |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame] | 162 | firstButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, dp.dropTargetTextSizePx); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 163 | firstButton.setTextVisible(true); |
| 164 | firstButton.setIconVisible(true); |
| 165 | firstButton.setTextMultiLine(false); |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame] | 166 | // Reset first button padding in case it was previously changed to multi-line text. |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 167 | firstButton.setPadding(horizontalPadding, verticalPadding, horizontalPadding, |
| 168 | verticalPadding); |
| 169 | |
| 170 | ButtonDropTarget secondButton = mTempTargets[1]; |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame] | 171 | secondButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, dp.dropTargetTextSizePx); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 172 | secondButton.setTextVisible(true); |
| 173 | secondButton.setIconVisible(true); |
| 174 | secondButton.setTextMultiLine(false); |
| 175 | // Reset second button padding in case it was previously changed to multi-line text. |
| 176 | secondButton.setPadding(horizontalPadding, verticalPadding, horizontalPadding, |
| 177 | verticalPadding); |
| 178 | |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 179 | int availableWidth; |
| 180 | if (dp.isTwoPanels) { |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame] | 181 | // Each button for two panel fits to half the width of the screen excluding the |
| 182 | // center gap between the buttons. |
| 183 | availableWidth = (dp.availableWidthPx - dp.dropTargetGapPx) / 2; |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 184 | } else { |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame] | 185 | // Both buttons plus the button gap do not display past the edge of the screen. |
| 186 | availableWidth = dp.availableWidthPx - dp.dropTargetGapPx; |
Pat Manning | b29dabc | 2022-05-20 15:21:48 +0000 | [diff] [blame] | 187 | } |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame] | 188 | |
Pat Manning | b29dabc | 2022-05-20 15:21:48 +0000 | [diff] [blame] | 189 | int widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 190 | firstButton.measure(widthSpec, heightSpec); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 191 | if (!mIsVertical) { |
Pat Manning | e3d74b4 | 2022-06-08 13:15:13 +0100 | [diff] [blame] | 192 | // Remove both icons and put the button's text on two lines if text is truncated. |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 193 | if (firstButton.isTextTruncated(availableWidth)) { |
| 194 | firstButton.setIconVisible(false); |
Pat Manning | e3d74b4 | 2022-06-08 13:15:13 +0100 | [diff] [blame] | 195 | secondButton.setIconVisible(false); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 196 | firstButton.setTextMultiLine(true); |
| 197 | firstButton.setPadding(horizontalPadding, verticalPadding / 2, |
| 198 | horizontalPadding, verticalPadding / 2); |
| 199 | } |
Alex Chau | 5b01930 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | if (!dp.isTwoPanels) { |
| 203 | availableWidth -= firstButton.getMeasuredWidth() + dp.dropTargetGapPx; |
| 204 | widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST); |
| 205 | } |
| 206 | secondButton.measure(widthSpec, heightSpec); |
| 207 | if (!mIsVertical) { |
Pat Manning | e3d74b4 | 2022-06-08 13:15:13 +0100 | [diff] [blame] | 208 | // Remove both icons and put the button's text on two lines if text is truncated. |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 209 | if (secondButton.isTextTruncated(availableWidth)) { |
| 210 | secondButton.setIconVisible(false); |
Pat Manning | e3d74b4 | 2022-06-08 13:15:13 +0100 | [diff] [blame] | 211 | firstButton.setIconVisible(false); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 212 | secondButton.setTextMultiLine(true); |
| 213 | secondButton.setPadding(horizontalPadding, verticalPadding / 2, |
| 214 | horizontalPadding, verticalPadding / 2); |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame] | 217 | |
| 218 | // If text is still truncated, shrink to fit in measured width and resize both targets. |
| 219 | float minTextSize = |
| 220 | Math.min(firstButton.resizeTextToFit(), secondButton.resizeTextToFit()); |
| 221 | if (firstButton.getTextSize() != minTextSize |
| 222 | || secondButton.getTextSize() != minTextSize) { |
| 223 | firstButton.setTextSize(minTextSize); |
| 224 | secondButton.setTextSize(minTextSize); |
| 225 | } |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 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) { |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +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 | |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 237 | DeviceProfile dp = mLauncher.getDeviceProfile(); |
| 238 | // Center vertical bar over scaled workspace, accounting for hotseat offset. |
Pat Manning | 5f74bfd | 2022-07-20 12:08:54 +0100 | [diff] [blame] | 239 | float scale = dp.getWorkspaceSpringLoadScale(mLauncher); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 240 | Workspace<?> ws = mLauncher.getWorkspace(); |
| 241 | int barCenter; |
| 242 | if (dp.isTwoPanels) { |
| 243 | barCenter = (right - left) / 2; |
| 244 | } else { |
| 245 | int workspaceCenter = (ws.getLeft() + ws.getRight()) / 2; |
| 246 | int cellLayoutCenter = ((dp.getInsets().left + dp.workspacePadding.left) + (dp.widthPx |
| 247 | - dp.getInsets().right - dp.workspacePadding.right)) / 2; |
| 248 | int cellLayoutCenterOffset = (int) ((cellLayoutCenter - workspaceCenter) * scale); |
| 249 | barCenter = workspaceCenter + cellLayoutCenterOffset - left; |
| 250 | } |
Pat Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 251 | |
| 252 | if (visibleCount == 1) { |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 253 | ButtonDropTarget button = mTempTargets[0]; |
Pat Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 254 | button.layout(barCenter - (button.getMeasuredWidth() / 2), 0, |
| 255 | barCenter + (button.getMeasuredWidth() / 2), button.getMeasuredHeight()); |
| 256 | } else if (visibleCount == 2) { |
| 257 | int buttonGap = dp.dropTargetGapPx; |
| 258 | |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 259 | ButtonDropTarget leftButton = mTempTargets[0]; |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 260 | ButtonDropTarget rightButton = mTempTargets[1]; |
Alex Chau | 5b01930 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 261 | if (dp.isTwoPanels) { |
| 262 | leftButton.layout(barCenter - leftButton.getMeasuredWidth() - (buttonGap / 2), 0, |
| 263 | barCenter - (buttonGap / 2), leftButton.getMeasuredHeight()); |
| 264 | rightButton.layout(barCenter + (buttonGap / 2), 0, |
| 265 | barCenter + (buttonGap / 2) + rightButton.getMeasuredWidth(), |
| 266 | rightButton.getMeasuredHeight()); |
| 267 | } else { |
| 268 | int scaledPanelWidth = (int) (dp.getCellLayoutWidth() * scale); |
| 269 | |
| 270 | int leftButtonWidth = leftButton.getMeasuredWidth(); |
| 271 | int rightButtonWidth = rightButton.getMeasuredWidth(); |
| 272 | int extraSpace = scaledPanelWidth - leftButtonWidth - rightButtonWidth - buttonGap; |
| 273 | |
| 274 | int leftButtonStart = barCenter - (scaledPanelWidth / 2) + extraSpace / 2; |
| 275 | int leftButtonEnd = leftButtonStart + leftButtonWidth; |
| 276 | int rightButtonStart = leftButtonEnd + buttonGap; |
| 277 | int rightButtonEnd = rightButtonStart + rightButtonWidth; |
| 278 | |
| 279 | leftButton.layout(leftButtonStart, 0, leftButtonEnd, |
| 280 | leftButton.getMeasuredHeight()); |
| 281 | rightButton.layout(rightButtonStart, 0, rightButtonEnd, |
| 282 | rightButton.getMeasuredHeight()); |
| 283 | } |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 287 | private int getVisibleButtons(ButtonDropTarget[] outVisibleButtons) { |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 288 | int visibleCount = 0; |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 289 | for (ButtonDropTarget button : mDropTargets) { |
| 290 | if (button.getVisibility() != GONE) { |
| 291 | outVisibleButtons[visibleCount] = button; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 292 | visibleCount++; |
| 293 | } |
| 294 | } |
| 295 | return visibleCount; |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Hyunyoung Song | 497708c | 2019-05-01 16:16:53 -0700 | [diff] [blame] | 298 | public void animateToVisibility(boolean isVisible) { |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 299 | if (mVisible != isVisible) { |
| 300 | mVisible = isVisible; |
| 301 | |
| 302 | // Cancel any existing animation |
| 303 | if (mCurrentAnimation != null) { |
| 304 | mCurrentAnimation.cancel(); |
| 305 | mCurrentAnimation = null; |
| 306 | } |
| 307 | |
| 308 | float finalAlpha = mVisible ? 1 : 0; |
| 309 | if (Float.compare(getAlpha(), finalAlpha) != 0) { |
| 310 | setVisibility(View.VISIBLE); |
| 311 | mCurrentAnimation = animate().alpha(finalAlpha) |
| 312 | .setInterpolator(DEFAULT_INTERPOLATOR) |
| 313 | .setDuration(DEFAULT_DRAG_FADE_DURATION) |
| 314 | .withEndAction(mFadeAnimationEndRunnable); |
| 315 | } |
| 316 | |
| 317 | } |
| 318 | } |
| 319 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 320 | /* |
| 321 | * DragController.DragListener implementation |
| 322 | */ |
| 323 | @Override |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 324 | public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) { |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 325 | animateToVisibility(true); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * This is called to defer hiding the delete drop target until the drop animation has completed, |
| 330 | * instead of hiding immediately when the drag has ended. |
| 331 | */ |
| 332 | protected void deferOnDragEnd() { |
| 333 | mDeferOnDragEnd = true; |
| 334 | } |
| 335 | |
| 336 | @Override |
| 337 | public void onDragEnd() { |
| 338 | if (!mDeferOnDragEnd) { |
| 339 | animateToVisibility(false); |
| 340 | } else { |
| 341 | mDeferOnDragEnd = false; |
| 342 | } |
| 343 | } |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 344 | |
| 345 | public ButtonDropTarget[] getDropTargets() { |
Alex Chau | 5b01930 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 346 | return getVisibility() == View.VISIBLE ? mDropTargets : new ButtonDropTarget[0]; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 347 | } |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 348 | } |