Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 18 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 19 | import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
| 20 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Tony Wickham | 855b1b5 | 2016-03-28 16:56:30 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 23 | import android.graphics.Rect; |
Sunny Goyal | 3a644ed | 2015-05-21 10:28:02 -0700 | [diff] [blame] | 24 | import android.graphics.drawable.Drawable; |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 25 | import android.text.InputType; |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 26 | import android.text.TextUtils; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 27 | import android.util.AttributeSet; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 28 | import android.view.LayoutInflater; |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 29 | import android.view.View; |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 30 | import android.view.View.OnClickListener; |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 31 | import android.view.accessibility.AccessibilityEvent; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 32 | import android.widget.PopupWindow; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 33 | import android.widget.TextView; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 34 | |
Andrew Cole | 44898c3 | 2023-04-05 13:49:32 -0700 | [diff] [blame] | 35 | import androidx.annotation.VisibleForTesting; |
| 36 | |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 37 | import com.android.launcher3.anim.Interpolators; |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 38 | import com.android.launcher3.dragndrop.DragController; |
| 39 | import com.android.launcher3.dragndrop.DragLayer; |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 40 | import com.android.launcher3.dragndrop.DragOptions; |
Pierre Barbier de Reuille | 2881801 | 2021-06-29 16:47:41 +0100 | [diff] [blame] | 41 | import com.android.launcher3.dragndrop.DragView; |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame] | 42 | import com.android.launcher3.model.data.ItemInfo; |
Andrew Cole | 44898c3 | 2023-04-05 13:49:32 -0700 | [diff] [blame] | 43 | import com.android.launcher3.views.ActivityContext; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Implements a DropTarget. |
| 47 | */ |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 48 | public abstract class ButtonDropTarget extends TextView |
| 49 | implements DropTarget, DragController.DragListener, OnClickListener { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 50 | |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 51 | private static final int[] sTempCords = new int[2]; |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 52 | private static final int DRAG_VIEW_DROP_DURATION = 285; |
Steven Ng | 6a2acfa | 2021-03-19 13:23:53 +0000 | [diff] [blame] | 53 | private static final float DRAG_VIEW_HOVER_OVER_OPACITY = 0.65f; |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 54 | private static final int MAX_LINES_TEXT_MULTI_LINE = 2; |
| 55 | private static final int MAX_LINES_TEXT_SINGLE_LINE = 1; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 56 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 57 | public static final int TOOLTIP_DEFAULT = 0; |
| 58 | public static final int TOOLTIP_LEFT = 1; |
| 59 | public static final int TOOLTIP_RIGHT = 2; |
| 60 | |
Helen Cheuk | 1a55cde | 2023-03-02 16:45:41 +0000 | [diff] [blame] | 61 | private final Rect mTempRect = new Rect(); |
| 62 | |
Andrew Cole | 44898c3 | 2023-04-05 13:49:32 -0700 | [diff] [blame] | 63 | protected final ActivityContext mActivityContext; |
| 64 | protected final DropTargetHandler mDropTargetHandler; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 65 | protected DropTargetBar mDropTargetBar; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 66 | |
| 67 | /** Whether this drop target is active for the current drag */ |
| 68 | protected boolean mActive; |
Sunny Goyal | 4583d09 | 2016-08-17 11:11:48 -0700 | [diff] [blame] | 69 | /** Whether an accessible drag is in progress */ |
| 70 | private boolean mAccessibleDrag; |
Tony Wickham | 855b1b5 | 2016-03-28 16:56:30 -0700 | [diff] [blame] | 71 | /** An item must be dragged at least this many pixels before this drop target is enabled. */ |
| 72 | private final int mDragDistanceThreshold; |
Yogisha Dixit | ed052ab | 2021-06-08 21:19:15 +0100 | [diff] [blame] | 73 | /** The size of the drawable shown in the drop target. */ |
| 74 | private final int mDrawableSize; |
Steven Ng | a1dcbce | 2021-07-23 17:06:10 +0100 | [diff] [blame] | 75 | /** The padding, in pixels, between the text and drawable. */ |
| 76 | private final int mDrawablePadding; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 77 | |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 78 | protected CharSequence mText; |
Sunny Goyal | 3a644ed | 2015-05-21 10:28:02 -0700 | [diff] [blame] | 79 | protected Drawable mDrawable; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 80 | private boolean mTextVisible = true; |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 81 | private boolean mIconVisible = true; |
| 82 | private boolean mTextMultiLine = true; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 83 | |
| 84 | private PopupWindow mToolTip; |
| 85 | private int mToolTipLocation; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 86 | |
Andrew Cole | 44898c3 | 2023-04-05 13:49:32 -0700 | [diff] [blame] | 87 | public ButtonDropTarget(Context context) { |
| 88 | this(context, null, 0); |
| 89 | } |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 90 | public ButtonDropTarget(Context context, AttributeSet attrs) { |
| 91 | this(context, attrs, 0); |
| 92 | } |
| 93 | |
| 94 | public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 95 | super(context, attrs, defStyle); |
Andrew Cole | 44898c3 | 2023-04-05 13:49:32 -0700 | [diff] [blame] | 96 | mActivityContext = ActivityContext.lookupContext(context); |
| 97 | mDropTargetHandler = mActivityContext.getDropTargetHandler(); |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 98 | |
Tony Wickham | 855b1b5 | 2016-03-28 16:56:30 -0700 | [diff] [blame] | 99 | Resources resources = getResources(); |
Tony Wickham | 855b1b5 | 2016-03-28 16:56:30 -0700 | [diff] [blame] | 100 | mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold); |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame] | 101 | mDrawableSize = resources.getDimensionPixelSize(R.dimen.drop_target_button_drawable_size); |
Steven Ng | a1dcbce | 2021-07-23 17:06:10 +0100 | [diff] [blame] | 102 | mDrawablePadding = resources.getDimensionPixelSize( |
| 103 | R.dimen.drop_target_button_drawable_padding); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 106 | @Override |
| 107 | protected void onFinishInflate() { |
| 108 | super.onFinishInflate(); |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 109 | mText = getText(); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 110 | setContentDescription(mText); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 113 | protected void updateText(int resId) { |
| 114 | setText(resId); |
| 115 | mText = getText(); |
| 116 | setContentDescription(mText); |
| 117 | } |
| 118 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 119 | protected void setDrawable(int resId) { |
Sunny Goyal | e0cab30 | 2015-05-20 16:40:30 -0700 | [diff] [blame] | 120 | // We do not set the drawable in the xml as that inflates two drawables corresponding to |
| 121 | // drawableLeft and drawableStart. |
Yogisha Dixit | 58c5e65 | 2021-06-16 14:18:22 +0100 | [diff] [blame] | 122 | mDrawable = getContext().getDrawable(resId).mutate(); |
Yogisha Dixit | 3cbd7e3 | 2021-05-29 00:26:25 +0100 | [diff] [blame] | 123 | mDrawable.setTintList(getTextColors()); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 124 | updateIconVisibility(); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 127 | public void setDropTargetBar(DropTargetBar dropTargetBar) { |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 128 | mDropTargetBar = dropTargetBar; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 131 | private void hideTooltip() { |
| 132 | if (mToolTip != null) { |
| 133 | mToolTip.dismiss(); |
| 134 | mToolTip = null; |
| 135 | } |
| 136 | } |
| 137 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 138 | @Override |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 139 | public final void onDragEnter(DragObject d) { |
Sunny Goyal | 9b18010 | 2020-03-11 10:02:29 -0700 | [diff] [blame] | 140 | if (!mAccessibleDrag && !mTextVisible) { |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 141 | // Show tooltip |
| 142 | hideTooltip(); |
| 143 | |
| 144 | TextView message = (TextView) LayoutInflater.from(getContext()).inflate( |
| 145 | R.layout.drop_target_tool_tip, null); |
| 146 | message.setText(mText); |
| 147 | |
| 148 | mToolTip = new PopupWindow(message, WRAP_CONTENT, WRAP_CONTENT); |
| 149 | int x = 0, y = 0; |
| 150 | if (mToolTipLocation != TOOLTIP_DEFAULT) { |
| 151 | y = -getMeasuredHeight(); |
| 152 | message.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); |
| 153 | if (mToolTipLocation == TOOLTIP_LEFT) { |
Sebastian Franco | d2d8e97 | 2022-04-04 14:51:53 -0700 | [diff] [blame] | 154 | x = -getMeasuredWidth() - message.getMeasuredWidth() / 2; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 155 | } else { |
| 156 | x = getMeasuredWidth() / 2 + message.getMeasuredWidth() / 2; |
| 157 | } |
| 158 | } |
| 159 | mToolTip.showAsDropDown(this, x, y); |
| 160 | } |
| 161 | |
Steven Ng | 6a2acfa | 2021-03-19 13:23:53 +0000 | [diff] [blame] | 162 | d.dragView.setAlpha(DRAG_VIEW_HOVER_OVER_OPACITY); |
Yogisha Dixit | 2ff963b | 2021-06-03 10:41:44 +0100 | [diff] [blame] | 163 | setSelected(true); |
Sunny Goyal | d21301e | 2015-09-25 12:17:08 -0700 | [diff] [blame] | 164 | if (d.stateAnnouncer != null) { |
| 165 | d.stateAnnouncer.cancel(); |
| 166 | } |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 167 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 170 | @Override |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 171 | public void onDragOver(DragObject d) { |
| 172 | // Do nothing |
| 173 | } |
| 174 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 175 | @Override |
| 176 | public final void onDragExit(DragObject d) { |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 177 | hideTooltip(); |
| 178 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 179 | if (!d.dragComplete) { |
Steven Ng | 6a2acfa | 2021-03-19 13:23:53 +0000 | [diff] [blame] | 180 | d.dragView.setAlpha(1f); |
Yogisha Dixit | 2ff963b | 2021-06-03 10:41:44 +0100 | [diff] [blame] | 181 | setSelected(false); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 182 | } else { |
Steven Ng | 6a2acfa | 2021-03-19 13:23:53 +0000 | [diff] [blame] | 183 | d.dragView.setAlpha(DRAG_VIEW_HOVER_OVER_OPACITY); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 184 | } |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 187 | @Override |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 188 | public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) { |
Alex Chau | 158caf4 | 2022-05-24 17:09:32 +0100 | [diff] [blame] | 189 | if (options.isKeyboardDrag) { |
| 190 | mActive = false; |
| 191 | } else { |
| 192 | setupItemInfo(dragObject.dragInfo); |
| 193 | mActive = supportsDrop(dragObject.dragInfo); |
| 194 | } |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 195 | setVisibility(mActive ? View.VISIBLE : View.GONE); |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 196 | |
| 197 | mAccessibleDrag = options.isAccessibleDrag; |
| 198 | setOnClickListener(mAccessibleDrag ? this : null); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public final boolean acceptDrop(DragObject dragObject) { |
Sunny Goyal | 1ce9c47 | 2017-10-03 16:17:32 -0700 | [diff] [blame] | 203 | return supportsDrop(dragObject.dragInfo); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Alex Chau | 158caf4 | 2022-05-24 17:09:32 +0100 | [diff] [blame] | 206 | /** |
| 207 | * Setups button for the specified ItemInfo. |
| 208 | */ |
| 209 | protected abstract void setupItemInfo(ItemInfo info); |
| 210 | |
Sunny Goyal | 1ce9c47 | 2017-10-03 16:17:32 -0700 | [diff] [blame] | 211 | protected abstract boolean supportsDrop(ItemInfo info); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 212 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 213 | public abstract boolean supportsAccessibilityDrop(ItemInfo info, View view); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 214 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 215 | @Override |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 216 | public boolean isDropEnabled() { |
Sunny Goyal | 4583d09 | 2016-08-17 11:11:48 -0700 | [diff] [blame] | 217 | return mActive && (mAccessibleDrag || |
Andrew Cole | 44898c3 | 2023-04-05 13:49:32 -0700 | [diff] [blame] | 218 | mActivityContext.getDragController().getDistanceDragged() |
| 219 | >= mDragDistanceThreshold); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 222 | @Override |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 223 | public void onDragEnd() { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 224 | mActive = false; |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 225 | setOnClickListener(null); |
Yogisha Dixit | 2ff963b | 2021-06-03 10:41:44 +0100 | [diff] [blame] | 226 | setSelected(false); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 229 | /** |
| 230 | * On drop animate the dropView to the icon. |
| 231 | */ |
| 232 | @Override |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 233 | public void onDrop(final DragObject d, final DragOptions options) { |
Tony Wickham | 6a71a5b | 2018-08-21 11:40:23 -0700 | [diff] [blame] | 234 | if (options.isFlingToDelete) { |
| 235 | // FlingAnimation handles the animation and then calls completeDrop(). |
| 236 | return; |
| 237 | } |
Andrew Cole | 44898c3 | 2023-04-05 13:49:32 -0700 | [diff] [blame] | 238 | |
| 239 | final DragLayer dragLayer = mDropTargetHandler.getDragLayer(); |
Pierre Barbier de Reuille | 2881801 | 2021-06-29 16:47:41 +0100 | [diff] [blame] | 240 | final DragView dragView = d.dragView; |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 241 | final Rect to = getIconRect(d); |
Sunny Goyal | 031418c | 2021-07-22 10:02:50 -0700 | [diff] [blame] | 242 | final float scale = (float) to.width() / dragView.getMeasuredWidth(); |
Pierre Barbier de Reuille | 2881801 | 2021-06-29 16:47:41 +0100 | [diff] [blame] | 243 | dragView.detachContentView(/* reattachToPreviousParent= */ true); |
| 244 | |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 245 | mDropTargetBar.deferOnDragEnd(); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 246 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 247 | Runnable onAnimationEndRunnable = () -> { |
| 248 | completeDrop(d); |
| 249 | mDropTargetBar.onDragEnd(); |
Andrew Cole | 44898c3 | 2023-04-05 13:49:32 -0700 | [diff] [blame] | 250 | mDropTargetHandler.onDropAnimationComplete(); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 251 | }; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 252 | |
Andrew Cole | 44898c3 | 2023-04-05 13:49:32 -0700 | [diff] [blame] | 253 | |
Sunny Goyal | 1721ccf | 2021-06-08 12:57:40 -0700 | [diff] [blame] | 254 | dragLayer.animateView(d.dragView, to, scale, 0.1f, 0.1f, |
Sunny Goyal | dec3a90 | 2017-01-25 18:23:36 -0800 | [diff] [blame] | 255 | DRAG_VIEW_DROP_DURATION, |
Sunny Goyal | 1721ccf | 2021-06-08 12:57:40 -0700 | [diff] [blame] | 256 | Interpolators.DEACCEL_2, onAnimationEndRunnable, |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 257 | DragLayer.ANIMATION_END_DISAPPEAR, null); |
| 258 | } |
| 259 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 260 | public abstract int getAccessibilityAction(); |
| 261 | |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 262 | @Override |
| 263 | public void prepareAccessibilityDrop() { } |
| 264 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 265 | public abstract void onAccessibilityDrop(View view, ItemInfo item); |
| 266 | |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 267 | public abstract void completeDrop(DragObject d); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 268 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 269 | @Override |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 270 | public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) { |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 271 | super.getHitRect(outRect); |
Andrew Cole | 44898c3 | 2023-04-05 13:49:32 -0700 | [diff] [blame] | 272 | outRect.bottom += mActivityContext.getDeviceProfile().dropTargetDragPaddingPx; |
Winson Chung | 156ab5b | 2013-07-12 14:14:16 -0700 | [diff] [blame] | 273 | |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 274 | sTempCords[0] = sTempCords[1] = 0; |
Andrew Cole | 44898c3 | 2023-04-05 13:49:32 -0700 | [diff] [blame] | 275 | mActivityContext.getDragLayer().getDescendantCoordRelativeToSelf(this, sTempCords); |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 276 | outRect.offsetTo(sTempCords[0], sTempCords[1]); |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 279 | public Rect getIconRect(DragObject dragObject) { |
| 280 | int viewWidth = dragObject.dragView.getMeasuredWidth(); |
| 281 | int viewHeight = dragObject.dragView.getMeasuredHeight(); |
| 282 | int drawableWidth = mDrawable.getIntrinsicWidth(); |
| 283 | int drawableHeight = mDrawable.getIntrinsicHeight(); |
Andrew Cole | 44898c3 | 2023-04-05 13:49:32 -0700 | [diff] [blame] | 284 | DragLayer dragLayer = mDropTargetHandler.getDragLayer(); |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 285 | |
| 286 | // Find the rect to animate to (the view is center aligned) |
| 287 | Rect to = new Rect(); |
| 288 | dragLayer.getViewRectRelativeToSelf(this, to); |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 289 | |
| 290 | final int width = drawableWidth; |
| 291 | final int height = drawableHeight; |
| 292 | |
| 293 | final int left; |
| 294 | final int right; |
| 295 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 296 | if (Utilities.isRtl(getResources())) { |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 297 | right = to.right - getPaddingRight(); |
| 298 | left = right - width; |
| 299 | } else { |
| 300 | left = to.left + getPaddingLeft(); |
| 301 | right = left + width; |
| 302 | } |
| 303 | |
| 304 | final int top = to.top + (getMeasuredHeight() - height) / 2; |
Sebastian Franco | 63d1a18 | 2021-10-18 09:52:04 -0500 | [diff] [blame] | 305 | final int bottom = top + height; |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 306 | |
| 307 | to.set(left, top, right, bottom); |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 308 | |
| 309 | // Center the destination rect about the trash icon |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 310 | final int xOffset = -(viewWidth - width) / 2; |
| 311 | final int yOffset = -(viewHeight - height) / 2; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 312 | to.offset(xOffset, yOffset); |
| 313 | |
| 314 | return to; |
| 315 | } |
| 316 | |
Sebastian Franco | 63d1a18 | 2021-10-18 09:52:04 -0500 | [diff] [blame] | 317 | private void centerIcon() { |
| 318 | int x = mTextVisible ? 0 |
| 319 | : (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 - mDrawableSize / 2; |
| 320 | mDrawable.setBounds(x, 0, x + mDrawableSize, mDrawableSize); |
| 321 | } |
| 322 | |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 323 | @Override |
| 324 | public void onClick(View v) { |
Andrew Cole | 44898c3 | 2023-04-05 13:49:32 -0700 | [diff] [blame] | 325 | mDropTargetHandler.onClick(this); |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 326 | } |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 327 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 328 | public void setTextVisible(boolean isVisible) { |
Jon Miranda | df1ffb9 | 2018-02-06 10:31:38 -0800 | [diff] [blame] | 329 | CharSequence newText = isVisible ? mText : ""; |
| 330 | if (mTextVisible != isVisible || !TextUtils.equals(newText, getText())) { |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 331 | mTextVisible = isVisible; |
Jon Miranda | df1ffb9 | 2018-02-06 10:31:38 -0800 | [diff] [blame] | 332 | setText(newText); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 333 | updateIconVisibility(); |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame] | 334 | } |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame] | 335 | } |
| 336 | |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 337 | /** |
| 338 | * Display button text over multiple lines when isMultiLine is true, single line otherwise. |
| 339 | */ |
| 340 | public void setTextMultiLine(boolean isMultiLine) { |
| 341 | if (mTextMultiLine != isMultiLine) { |
| 342 | mTextMultiLine = isMultiLine; |
| 343 | setSingleLine(!isMultiLine); |
| 344 | setMaxLines(isMultiLine ? MAX_LINES_TEXT_MULTI_LINE : MAX_LINES_TEXT_SINGLE_LINE); |
| 345 | int inputType = InputType.TYPE_CLASS_TEXT; |
| 346 | if (isMultiLine) { |
| 347 | inputType |= InputType.TYPE_TEXT_FLAG_MULTI_LINE; |
| 348 | |
| 349 | } |
| 350 | setInputType(inputType); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | protected boolean isTextMultiLine() { |
| 355 | return mTextMultiLine; |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Sets the button icon visible when isVisible is true, hides it otherwise. |
| 360 | */ |
| 361 | public void setIconVisible(boolean isVisible) { |
| 362 | if (mIconVisible != isVisible) { |
| 363 | mIconVisible = isVisible; |
| 364 | updateIconVisibility(); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | private void updateIconVisibility() { |
| 369 | if (mIconVisible) { |
| 370 | centerIcon(); |
| 371 | } |
| 372 | setCompoundDrawablesRelative(mIconVisible ? mDrawable : null, null, null, null); |
| 373 | setCompoundDrawablePadding(mIconVisible && mTextVisible ? mDrawablePadding : 0); |
| 374 | } |
| 375 | |
Sebastian Franco | 63d1a18 | 2021-10-18 09:52:04 -0500 | [diff] [blame] | 376 | @Override |
| 377 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 378 | super.onSizeChanged(w, h, oldw, oldh); |
| 379 | centerIcon(); |
| 380 | } |
| 381 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 382 | public void setToolTipLocation(int location) { |
| 383 | mToolTipLocation = location; |
| 384 | hideTooltip(); |
| 385 | } |
| 386 | |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame] | 387 | /** |
| 388 | * Returns if the text will be truncated within the provided availableWidth. |
| 389 | */ |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 390 | public boolean isTextTruncated(int availableWidth) { |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame] | 391 | availableWidth -= getPaddingLeft() + getPaddingRight(); |
| 392 | if (mIconVisible) { |
| 393 | availableWidth -= mDrawable.getIntrinsicWidth() + getCompoundDrawablePadding(); |
| 394 | } |
| 395 | if (availableWidth <= 0) { |
| 396 | return true; |
| 397 | } |
| 398 | CharSequence firstLine = TextUtils.ellipsize(mText, getPaint(), availableWidth, |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 399 | TextUtils.TruncateAt.END); |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame] | 400 | if (!mTextMultiLine) { |
| 401 | return !TextUtils.equals(mText, firstLine); |
| 402 | } |
| 403 | if (TextUtils.equals(mText, firstLine)) { |
| 404 | // When multi-line is active, if it can display as one line, then text is not truncated. |
| 405 | return false; |
| 406 | } |
| 407 | CharSequence secondLine = |
| 408 | TextUtils.ellipsize(mText.subSequence(firstLine.length(), mText.length()), |
| 409 | getPaint(), availableWidth, TextUtils.TruncateAt.END); |
| 410 | return !(TextUtils.equals(mText.subSequence(0, firstLine.length()), firstLine) |
| 411 | && TextUtils.equals(mText.subSequence(firstLine.length(), secondLine.length()), |
| 412 | secondLine)); |
| 413 | } |
| 414 | |
| 415 | /** |
Helen Cheuk | 1a55cde | 2023-03-02 16:45:41 +0000 | [diff] [blame] | 416 | * Returns if the text will be clipped vertically within the provided availableHeight. |
| 417 | */ |
Andrew Cole | 44898c3 | 2023-04-05 13:49:32 -0700 | [diff] [blame] | 418 | @VisibleForTesting |
| 419 | protected boolean isTextClippedVertically(int availableHeight) { |
Helen Cheuk | 1a55cde | 2023-03-02 16:45:41 +0000 | [diff] [blame] | 420 | availableHeight -= getPaddingTop() + getPaddingBottom(); |
| 421 | if (availableHeight <= 0) { |
| 422 | return true; |
| 423 | } |
| 424 | |
| 425 | getPaint().getTextBounds(mText.toString(), 0, mText.length(), mTempRect); |
| 426 | // Add bounds bottom to height, as text bounds height measures from the text baseline and |
| 427 | // above, which characters can descend below |
Andrew Cole | 3ae7be6 | 2023-03-28 16:26:33 -0700 | [diff] [blame] | 428 | return mTempRect.bottom + mTempRect.height() >= availableHeight; |
Helen Cheuk | 1a55cde | 2023-03-02 16:45:41 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /** |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame] | 432 | * Reduce the size of the text until it fits the measured width or reaches a minimum. |
| 433 | * |
| 434 | * The minimum size is defined by {@code R.dimen.button_drop_target_min_text_size} and |
| 435 | * it diminishes by intervals defined by |
| 436 | * {@code R.dimen.button_drop_target_resize_text_increment} |
| 437 | * This functionality is very similar to the option |
| 438 | * {@link TextView#setAutoSizeTextTypeWithDefaults(int)} but can't be used in this view because |
| 439 | * the layout width is {@code WRAP_CONTENT}. |
| 440 | * |
| 441 | * @return The biggest text size in SP that makes the text fit or if the text can't fit returns |
| 442 | * the min available value |
| 443 | */ |
| 444 | public float resizeTextToFit() { |
| 445 | float minSize = Utilities.pxToSp(getResources() |
| 446 | .getDimensionPixelSize(R.dimen.button_drop_target_min_text_size)); |
| 447 | float step = Utilities.pxToSp(getResources() |
| 448 | .getDimensionPixelSize(R.dimen.button_drop_target_resize_text_increment)); |
| 449 | float textSize = Utilities.pxToSp(getTextSize()); |
| 450 | |
| 451 | int availableWidth = getMeasuredWidth(); |
Helen Cheuk | 1a55cde | 2023-03-02 16:45:41 +0000 | [diff] [blame] | 452 | int availableHeight = getMeasuredHeight(); |
| 453 | |
| 454 | while (isTextTruncated(availableWidth) || isTextClippedVertically(availableHeight)) { |
Sebastian Franco | 5bf936a | 2022-12-01 11:51:12 -0800 | [diff] [blame] | 455 | textSize -= step; |
| 456 | if (textSize < minSize) { |
| 457 | textSize = minSize; |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame] | 458 | setTextSize(textSize); |
Sebastian Franco | 5bf936a | 2022-12-01 11:51:12 -0800 | [diff] [blame] | 459 | break; |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame] | 460 | } |
Sebastian Franco | 5bf936a | 2022-12-01 11:51:12 -0800 | [diff] [blame] | 461 | setTextSize(textSize); |
Pat Manning | e63dd25 | 2022-08-02 16:15:29 +0100 | [diff] [blame] | 462 | } |
Sebastian Franco | 5bf936a | 2022-12-01 11:51:12 -0800 | [diff] [blame] | 463 | return textSize; |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 464 | } |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 465 | } |