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; |
| 20 | import static com.android.launcher3.ButtonDropTarget.TOOLTIP_LEFT; |
| 21 | import static com.android.launcher3.ButtonDropTarget.TOOLTIP_RIGHT; |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 22 | import static com.android.launcher3.anim.AlphaUpdateListener.updateVisibility; |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 23 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 24 | import android.animation.TimeInterpolator; |
| 25 | import android.content.Context; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 26 | import android.graphics.Rect; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 27 | import android.util.AttributeSet; |
Alex Chau | a02eddc | 2021-04-29 00:36:06 +0100 | [diff] [blame^] | 28 | import android.util.TypedValue; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 29 | import android.view.Gravity; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 30 | import android.view.View; |
| 31 | import android.view.ViewDebug; |
| 32 | import android.view.ViewPropertyAnimator; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 33 | import android.widget.FrameLayout; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 34 | |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 35 | import com.android.launcher3.anim.Interpolators; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 36 | import com.android.launcher3.dragndrop.DragController; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 37 | import com.android.launcher3.dragndrop.DragController.DragListener; |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 38 | import com.android.launcher3.dragndrop.DragOptions; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * The top bar containing various drop targets: Delete/App Info/Uninstall. |
| 42 | */ |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 43 | public class DropTargetBar extends FrameLayout |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 44 | implements DragListener, Insettable { |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 45 | |
| 46 | protected static final int DEFAULT_DRAG_FADE_DURATION = 175; |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 47 | protected static final TimeInterpolator DEFAULT_INTERPOLATOR = Interpolators.ACCEL; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 48 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 49 | private final Runnable mFadeAnimationEndRunnable = |
Sunny Goyal | 18d7184 | 2018-03-27 13:44:00 -0700 | [diff] [blame] | 50 | () -> updateVisibility(DropTargetBar.this); |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 51 | |
| 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; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 59 | private ViewPropertyAnimator mCurrentAnimation; |
| 60 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 61 | private boolean mIsVertical = true; |
| 62 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 63 | public DropTargetBar(Context context, AttributeSet attrs) { |
| 64 | super(context, attrs); |
| 65 | } |
| 66 | |
| 67 | public DropTargetBar(Context context, AttributeSet attrs, int defStyle) { |
| 68 | super(context, attrs, defStyle); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | protected void onFinishInflate() { |
| 73 | super.onFinishInflate(); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 74 | mDropTargets = new ButtonDropTarget[getChildCount()]; |
| 75 | for (int i = 0; i < mDropTargets.length; i++) { |
| 76 | mDropTargets[i] = (ButtonDropTarget) getChildAt(i); |
| 77 | mDropTargets[i].setDropTargetBar(this); |
| 78 | } |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 81 | @Override |
| 82 | public void setInsets(Rect insets) { |
| 83 | FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams(); |
| 84 | DeviceProfile grid = Launcher.getLauncher(getContext()).getDeviceProfile(); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 85 | mIsVertical = grid.isVerticalBarLayout(); |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 86 | |
| 87 | lp.leftMargin = insets.left; |
| 88 | lp.topMargin = insets.top; |
| 89 | lp.bottomMargin = insets.bottom; |
| 90 | lp.rightMargin = insets.right; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 91 | int tooltipLocation = TOOLTIP_DEFAULT; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 92 | |
| 93 | if (grid.isVerticalBarLayout()) { |
| 94 | lp.width = grid.dropTargetBarSizePx; |
| 95 | lp.height = grid.availableHeightPx - 2 * grid.edgeMarginPx; |
Sunny Goyal | 7e2e67f | 2018-01-26 13:40:08 -0800 | [diff] [blame] | 96 | lp.gravity = grid.isSeascape() ? Gravity.RIGHT : Gravity.LEFT; |
| 97 | tooltipLocation = grid.isSeascape() ? TOOLTIP_LEFT : TOOLTIP_RIGHT; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 98 | } else { |
| 99 | int gap; |
| 100 | if (grid.isTablet) { |
| 101 | // XXX: If the icon size changes across orientations, we will have to take |
| 102 | // that into account here too. |
| 103 | gap = ((grid.widthPx - 2 * grid.edgeMarginPx |
| 104 | - (grid.inv.numColumns * grid.cellWidthPx)) |
| 105 | / (2 * (grid.inv.numColumns + 1))) |
| 106 | + grid.edgeMarginPx; |
| 107 | } else { |
Sunny Goyal | 58fa4b6 | 2019-03-22 16:23:25 -0700 | [diff] [blame] | 108 | gap = grid.desiredWorkspaceLeftRightMarginPx - grid.inv.defaultWidgetPadding.right; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 109 | } |
| 110 | lp.width = grid.availableWidthPx - 2 * gap; |
| 111 | |
| 112 | lp.topMargin += grid.edgeMarginPx; |
| 113 | lp.height = grid.dropTargetBarSizePx; |
Sunny Goyal | f8d56fc | 2018-01-31 15:18:11 -0800 | [diff] [blame] | 114 | lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP; |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 115 | } |
| 116 | setLayoutParams(lp); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 117 | for (ButtonDropTarget button : mDropTargets) { |
Alex Chau | a02eddc | 2021-04-29 00:36:06 +0100 | [diff] [blame^] | 118 | button.setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.dropTargetTextSizePx); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 119 | button.setToolTipLocation(tooltipLocation); |
| 120 | } |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 123 | public void setup(DragController dragController) { |
| 124 | dragController.addDragListener(this); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 125 | for (int i = 0; i < mDropTargets.length; i++) { |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 126 | dragController.addDragListener(mDropTargets[i]); |
| 127 | dragController.addDropTarget(mDropTargets[i]); |
| 128 | } |
| 129 | } |
| 130 | |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 131 | @Override |
| 132 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 133 | int width = MeasureSpec.getSize(widthMeasureSpec); |
| 134 | int height = MeasureSpec.getSize(heightMeasureSpec); |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 135 | |
Sunny Goyal | a4647b6 | 2021-02-02 13:45:34 -0800 | [diff] [blame] | 136 | int visibleCount = getVisibleButtonsCount(); |
| 137 | if (visibleCount == 0) { |
| 138 | // do nothing |
| 139 | } else if (mIsVertical) { |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 140 | int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY); |
| 141 | int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST); |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 142 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 143 | for (ButtonDropTarget button : mDropTargets) { |
| 144 | if (button.getVisibility() != GONE) { |
| 145 | button.setTextVisible(false); |
| 146 | button.measure(widthSpec, heightSpec); |
| 147 | } |
| 148 | } |
| 149 | } else { |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 150 | int availableWidth = width / visibleCount; |
| 151 | boolean textVisible = true; |
| 152 | for (ButtonDropTarget buttons : mDropTargets) { |
| 153 | if (buttons.getVisibility() != GONE) { |
| 154 | textVisible = textVisible && !buttons.isTextTruncated(availableWidth); |
| 155 | } |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 158 | int widthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST); |
| 159 | int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); |
| 160 | for (ButtonDropTarget button : mDropTargets) { |
| 161 | if (button.getVisibility() != GONE) { |
| 162 | button.setTextVisible(textVisible); |
| 163 | button.measure(widthSpec, heightSpec); |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | } |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 167 | setMeasuredDimension(width, height); |
| 168 | } |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 169 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 170 | @Override |
| 171 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Sunny Goyal | a4647b6 | 2021-02-02 13:45:34 -0800 | [diff] [blame] | 172 | int visibleCount = getVisibleButtonsCount(); |
| 173 | if (visibleCount == 0) { |
| 174 | // do nothing |
| 175 | } else if (mIsVertical) { |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 176 | int gap = getResources().getDimensionPixelSize(R.dimen.drop_target_vertical_gap); |
| 177 | int start = gap; |
| 178 | int end; |
| 179 | |
| 180 | for (ButtonDropTarget button : mDropTargets) { |
| 181 | if (button.getVisibility() != GONE) { |
| 182 | end = start + button.getMeasuredHeight(); |
| 183 | button.layout(0, start, button.getMeasuredWidth(), end); |
| 184 | start = end + gap; |
| 185 | } |
| 186 | } |
| 187 | } else { |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 188 | int frameSize = (right - left) / visibleCount; |
| 189 | |
| 190 | int start = frameSize / 2; |
| 191 | int halfWidth; |
| 192 | for (ButtonDropTarget button : mDropTargets) { |
| 193 | if (button.getVisibility() != GONE) { |
| 194 | halfWidth = button.getMeasuredWidth() / 2; |
| 195 | button.layout(start - halfWidth, 0, |
| 196 | start + halfWidth, button.getMeasuredHeight()); |
| 197 | start = start + frameSize; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | private int getVisibleButtonsCount() { |
| 204 | int visibleCount = 0; |
| 205 | for (ButtonDropTarget buttons : mDropTargets) { |
| 206 | if (buttons.getVisibility() != GONE) { |
| 207 | visibleCount++; |
| 208 | } |
| 209 | } |
| 210 | return visibleCount; |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Hyunyoung Song | 497708c | 2019-05-01 16:16:53 -0700 | [diff] [blame] | 213 | public void animateToVisibility(boolean isVisible) { |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 214 | if (mVisible != isVisible) { |
| 215 | mVisible = isVisible; |
| 216 | |
| 217 | // Cancel any existing animation |
| 218 | if (mCurrentAnimation != null) { |
| 219 | mCurrentAnimation.cancel(); |
| 220 | mCurrentAnimation = null; |
| 221 | } |
| 222 | |
| 223 | float finalAlpha = mVisible ? 1 : 0; |
| 224 | if (Float.compare(getAlpha(), finalAlpha) != 0) { |
| 225 | setVisibility(View.VISIBLE); |
| 226 | mCurrentAnimation = animate().alpha(finalAlpha) |
| 227 | .setInterpolator(DEFAULT_INTERPOLATOR) |
| 228 | .setDuration(DEFAULT_DRAG_FADE_DURATION) |
| 229 | .withEndAction(mFadeAnimationEndRunnable); |
| 230 | } |
| 231 | |
| 232 | } |
| 233 | } |
| 234 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 235 | /* |
| 236 | * DragController.DragListener implementation |
| 237 | */ |
| 238 | @Override |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 239 | public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) { |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 240 | animateToVisibility(true); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * This is called to defer hiding the delete drop target until the drop animation has completed, |
| 245 | * instead of hiding immediately when the drag has ended. |
| 246 | */ |
| 247 | protected void deferOnDragEnd() { |
| 248 | mDeferOnDragEnd = true; |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public void onDragEnd() { |
| 253 | if (!mDeferOnDragEnd) { |
| 254 | animateToVisibility(false); |
| 255 | } else { |
| 256 | mDeferOnDragEnd = false; |
| 257 | } |
| 258 | } |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 259 | |
| 260 | public ButtonDropTarget[] getDropTargets() { |
| 261 | return mDropTargets; |
| 262 | } |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 263 | } |