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 | f6ef879 | 2022-07-26 13:54:31 -0700 | [diff] [blame] | 40 | import com.android.launcher3.testing.shared.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 | |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +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; |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +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); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +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); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +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 | } |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +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(); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +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 | |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +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); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +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); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 147 | int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 148 | |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 149 | int visibleCount = getVisibleButtons(mTempTargets); |
| 150 | if (visibleCount == 1) { |
| 151 | int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST); |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame] | 152 | |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 153 | ButtonDropTarget firstButton = mTempTargets[0]; |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame^] | 154 | firstButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, |
| 155 | mLauncher.getDeviceProfile().dropTargetTextSizePx); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 156 | firstButton.setTextVisible(true); |
| 157 | firstButton.setIconVisible(true); |
| 158 | firstButton.measure(widthSpec, heightSpec); |
| 159 | } else if (visibleCount == 2) { |
| 160 | DeviceProfile dp = mLauncher.getDeviceProfile(); |
| 161 | int verticalPadding = dp.dropTargetVerticalPaddingPx; |
| 162 | int horizontalPadding = dp.dropTargetHorizontalPaddingPx; |
| 163 | |
| 164 | ButtonDropTarget firstButton = mTempTargets[0]; |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame^] | 165 | firstButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, dp.dropTargetTextSizePx); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 166 | firstButton.setTextVisible(true); |
| 167 | firstButton.setIconVisible(true); |
| 168 | firstButton.setTextMultiLine(false); |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame^] | 169 | // 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] | 170 | firstButton.setPadding(horizontalPadding, verticalPadding, horizontalPadding, |
| 171 | verticalPadding); |
| 172 | |
| 173 | ButtonDropTarget secondButton = mTempTargets[1]; |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame^] | 174 | secondButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, dp.dropTargetTextSizePx); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 175 | secondButton.setTextVisible(true); |
| 176 | secondButton.setIconVisible(true); |
| 177 | secondButton.setTextMultiLine(false); |
| 178 | // Reset second button padding in case it was previously changed to multi-line text. |
| 179 | secondButton.setPadding(horizontalPadding, verticalPadding, horizontalPadding, |
| 180 | verticalPadding); |
| 181 | |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 182 | int availableWidth; |
| 183 | if (dp.isTwoPanels) { |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame^] | 184 | // Each button for two panel fits to half the width of the screen excluding the |
| 185 | // center gap between the buttons. |
| 186 | availableWidth = (dp.availableWidthPx - dp.dropTargetGapPx) / 2; |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 187 | } else { |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame^] | 188 | // Both buttons plus the button gap do not display past the edge of the screen. |
| 189 | availableWidth = dp.availableWidthPx - dp.dropTargetGapPx; |
Pat Manning | b29dabc | 2022-05-20 15:21:48 +0000 | [diff] [blame] | 190 | } |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame] | 191 | |
Pat Manning | b29dabc | 2022-05-20 15:21:48 +0000 | [diff] [blame] | 192 | int widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 193 | firstButton.measure(widthSpec, heightSpec); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 194 | if (!mIsVertical) { |
Pat Manning | e3d74b4 | 2022-06-08 13:15:13 +0100 | [diff] [blame] | 195 | // 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] | 196 | if (firstButton.isTextTruncated(availableWidth)) { |
| 197 | firstButton.setIconVisible(false); |
Pat Manning | e3d74b4 | 2022-06-08 13:15:13 +0100 | [diff] [blame] | 198 | secondButton.setIconVisible(false); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 199 | firstButton.setTextMultiLine(true); |
| 200 | firstButton.setPadding(horizontalPadding, verticalPadding / 2, |
| 201 | horizontalPadding, verticalPadding / 2); |
| 202 | } |
Alex Chau | 5b01930 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | if (!dp.isTwoPanels) { |
| 206 | availableWidth -= firstButton.getMeasuredWidth() + dp.dropTargetGapPx; |
| 207 | widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST); |
| 208 | } |
| 209 | secondButton.measure(widthSpec, heightSpec); |
| 210 | if (!mIsVertical) { |
Pat Manning | e3d74b4 | 2022-06-08 13:15:13 +0100 | [diff] [blame] | 211 | // 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] | 212 | if (secondButton.isTextTruncated(availableWidth)) { |
| 213 | secondButton.setIconVisible(false); |
Pat Manning | e3d74b4 | 2022-06-08 13:15:13 +0100 | [diff] [blame] | 214 | firstButton.setIconVisible(false); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 215 | secondButton.setTextMultiLine(true); |
| 216 | secondButton.setPadding(horizontalPadding, verticalPadding / 2, |
| 217 | horizontalPadding, verticalPadding / 2); |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 218 | } |
| 219 | } |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame^] | 220 | |
| 221 | // If text is still truncated, shrink to fit in measured width and resize both targets. |
| 222 | float minTextSize = |
| 223 | Math.min(firstButton.resizeTextToFit(), secondButton.resizeTextToFit()); |
| 224 | if (firstButton.getTextSize() != minTextSize |
| 225 | || secondButton.getTextSize() != minTextSize) { |
| 226 | firstButton.setTextSize(minTextSize); |
| 227 | secondButton.setTextSize(minTextSize); |
| 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 | setMeasuredDimension(width, height); |
| 231 | } |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 232 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 233 | @Override |
| 234 | 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] | 235 | int visibleCount = getVisibleButtons(mTempTargets); |
Sunny Goyal | a4647b6 | 2021-02-02 13:45:34 -0800 | [diff] [blame] | 236 | if (visibleCount == 0) { |
Pat Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 237 | return; |
| 238 | } |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 239 | |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 240 | DeviceProfile dp = mLauncher.getDeviceProfile(); |
| 241 | // Center vertical bar over scaled workspace, accounting for hotseat offset. |
Pat Manning | 5f74bfd | 2022-07-20 12:08:54 +0100 | [diff] [blame] | 242 | float scale = dp.getWorkspaceSpringLoadScale(mLauncher); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 243 | Workspace<?> ws = mLauncher.getWorkspace(); |
| 244 | int barCenter; |
| 245 | if (dp.isTwoPanels) { |
| 246 | barCenter = (right - left) / 2; |
| 247 | } else { |
| 248 | int workspaceCenter = (ws.getLeft() + ws.getRight()) / 2; |
| 249 | int cellLayoutCenter = ((dp.getInsets().left + dp.workspacePadding.left) + (dp.widthPx |
| 250 | - dp.getInsets().right - dp.workspacePadding.right)) / 2; |
| 251 | int cellLayoutCenterOffset = (int) ((cellLayoutCenter - workspaceCenter) * scale); |
| 252 | barCenter = workspaceCenter + cellLayoutCenterOffset - left; |
| 253 | } |
Pat Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 254 | |
| 255 | if (visibleCount == 1) { |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 256 | ButtonDropTarget button = mTempTargets[0]; |
Pat Manning | de25c0d | 2022-04-05 19:11:26 +0100 | [diff] [blame] | 257 | button.layout(barCenter - (button.getMeasuredWidth() / 2), 0, |
| 258 | barCenter + (button.getMeasuredWidth() / 2), button.getMeasuredHeight()); |
| 259 | } else if (visibleCount == 2) { |
| 260 | int buttonGap = dp.dropTargetGapPx; |
| 261 | |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 262 | ButtonDropTarget leftButton = mTempTargets[0]; |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 263 | ButtonDropTarget rightButton = mTempTargets[1]; |
Alex Chau | 5b01930 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 264 | if (dp.isTwoPanels) { |
| 265 | leftButton.layout(barCenter - leftButton.getMeasuredWidth() - (buttonGap / 2), 0, |
| 266 | barCenter - (buttonGap / 2), leftButton.getMeasuredHeight()); |
| 267 | rightButton.layout(barCenter + (buttonGap / 2), 0, |
| 268 | barCenter + (buttonGap / 2) + rightButton.getMeasuredWidth(), |
| 269 | rightButton.getMeasuredHeight()); |
| 270 | } else { |
| 271 | int scaledPanelWidth = (int) (dp.getCellLayoutWidth() * scale); |
| 272 | |
| 273 | int leftButtonWidth = leftButton.getMeasuredWidth(); |
| 274 | int rightButtonWidth = rightButton.getMeasuredWidth(); |
| 275 | int extraSpace = scaledPanelWidth - leftButtonWidth - rightButtonWidth - buttonGap; |
| 276 | |
| 277 | int leftButtonStart = barCenter - (scaledPanelWidth / 2) + extraSpace / 2; |
| 278 | int leftButtonEnd = leftButtonStart + leftButtonWidth; |
| 279 | int rightButtonStart = leftButtonEnd + buttonGap; |
| 280 | int rightButtonEnd = rightButtonStart + rightButtonWidth; |
| 281 | |
| 282 | leftButton.layout(leftButtonStart, 0, leftButtonEnd, |
| 283 | leftButton.getMeasuredHeight()); |
| 284 | rightButton.layout(rightButtonStart, 0, rightButtonEnd, |
| 285 | rightButton.getMeasuredHeight()); |
| 286 | } |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 290 | private int getVisibleButtons(ButtonDropTarget[] outVisibleButtons) { |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 291 | int visibleCount = 0; |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 292 | for (ButtonDropTarget button : mDropTargets) { |
| 293 | if (button.getVisibility() != GONE) { |
| 294 | outVisibleButtons[visibleCount] = button; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 295 | visibleCount++; |
| 296 | } |
| 297 | } |
| 298 | return visibleCount; |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Hyunyoung Song | 497708c | 2019-05-01 16:16:53 -0700 | [diff] [blame] | 301 | public void animateToVisibility(boolean isVisible) { |
vadimt | 89d9423 | 2021-09-01 12:33:00 -0700 | [diff] [blame] | 302 | if (TestProtocol.sDebugTracing) { |
| 303 | Log.d(TestProtocol.NO_DROP_TARGET, "8"); |
| 304 | } |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 305 | if (mVisible != isVisible) { |
| 306 | mVisible = isVisible; |
| 307 | |
| 308 | // Cancel any existing animation |
| 309 | if (mCurrentAnimation != null) { |
| 310 | mCurrentAnimation.cancel(); |
| 311 | mCurrentAnimation = null; |
| 312 | } |
| 313 | |
| 314 | float finalAlpha = mVisible ? 1 : 0; |
| 315 | if (Float.compare(getAlpha(), finalAlpha) != 0) { |
| 316 | setVisibility(View.VISIBLE); |
| 317 | mCurrentAnimation = animate().alpha(finalAlpha) |
| 318 | .setInterpolator(DEFAULT_INTERPOLATOR) |
| 319 | .setDuration(DEFAULT_DRAG_FADE_DURATION) |
| 320 | .withEndAction(mFadeAnimationEndRunnable); |
| 321 | } |
| 322 | |
| 323 | } |
| 324 | } |
| 325 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 326 | /* |
| 327 | * DragController.DragListener implementation |
| 328 | */ |
| 329 | @Override |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 330 | public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) { |
vadimt | 89d9423 | 2021-09-01 12:33:00 -0700 | [diff] [blame] | 331 | if (TestProtocol.sDebugTracing) { |
| 332 | Log.d(TestProtocol.NO_DROP_TARGET, "7"); |
| 333 | } |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 334 | animateToVisibility(true); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * This is called to defer hiding the delete drop target until the drop animation has completed, |
| 339 | * instead of hiding immediately when the drag has ended. |
| 340 | */ |
| 341 | protected void deferOnDragEnd() { |
| 342 | mDeferOnDragEnd = true; |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public void onDragEnd() { |
| 347 | if (!mDeferOnDragEnd) { |
| 348 | animateToVisibility(false); |
| 349 | } else { |
| 350 | mDeferOnDragEnd = false; |
| 351 | } |
| 352 | } |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 353 | |
| 354 | public ButtonDropTarget[] getDropTargets() { |
Alex Chau | 5b01930 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 355 | return getVisibility() == View.VISIBLE ? mDropTargets : new ButtonDropTarget[0]; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 356 | } |
vadimt | 89d9423 | 2021-09-01 12:33:00 -0700 | [diff] [blame] | 357 | |
| 358 | @Override |
| 359 | protected void onVisibilityChanged(@NonNull View changedView, int visibility) { |
| 360 | super.onVisibilityChanged(changedView, visibility); |
Vadim Tryshev | 8629be7 | 2021-10-26 19:57:49 +0000 | [diff] [blame] | 361 | if (TestProtocol.sDebugTracing) { |
| 362 | if (visibility == VISIBLE) { |
| 363 | Log.d(TestProtocol.NO_DROP_TARGET, "9"); |
| 364 | } else { |
| 365 | Log.d(TestProtocol.NO_DROP_TARGET, "Hiding drop target", new Exception()); |
| 366 | } |
vadimt | 89d9423 | 2021-09-01 12:33:00 -0700 | [diff] [blame] | 367 | } |
| 368 | } |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 369 | } |