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 | |
Sunny Goyal | 3e3f44c | 2017-10-23 17:14:52 -0700 | [diff] [blame] | 21 | import static com.android.launcher3.LauncherState.NORMAL; |
Sunny Goyal | aeb1643 | 2017-10-16 11:46:41 -0700 | [diff] [blame] | 22 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Tony Wickham | 855b1b5 | 2016-03-28 16:56:30 -0700 | [diff] [blame] | 24 | import android.content.res.Resources; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 25 | import android.graphics.Rect; |
Sunny Goyal | 3a644ed | 2015-05-21 10:28:02 -0700 | [diff] [blame] | 26 | import android.graphics.drawable.Drawable; |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 27 | import android.text.InputType; |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 28 | import android.text.TextUtils; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 29 | import android.util.AttributeSet; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 30 | import android.view.LayoutInflater; |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 31 | import android.view.View; |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 32 | import android.view.View.OnClickListener; |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 33 | import android.view.accessibility.AccessibilityEvent; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 34 | import android.widget.PopupWindow; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 35 | import android.widget.TextView; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 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; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Implements a DropTarget. |
| 46 | */ |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 47 | public abstract class ButtonDropTarget extends TextView |
| 48 | implements DropTarget, DragController.DragListener, OnClickListener { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 49 | |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 50 | private static final int[] sTempCords = new int[2]; |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 51 | private static final int DRAG_VIEW_DROP_DURATION = 285; |
Steven Ng | 6a2acfa | 2021-03-19 13:23:53 +0000 | [diff] [blame] | 52 | private static final float DRAG_VIEW_HOVER_OVER_OPACITY = 0.65f; |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 53 | private static final int MAX_LINES_TEXT_MULTI_LINE = 2; |
| 54 | private static final int MAX_LINES_TEXT_SINGLE_LINE = 1; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 55 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 56 | public static final int TOOLTIP_DEFAULT = 0; |
| 57 | public static final int TOOLTIP_LEFT = 1; |
| 58 | public static final int TOOLTIP_RIGHT = 2; |
| 59 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 60 | protected final Launcher mLauncher; |
Sunny Goyal | f37a214 | 2016-03-24 17:28:25 -0700 | [diff] [blame] | 61 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 62 | protected DropTargetBar mDropTargetBar; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 63 | |
| 64 | /** Whether this drop target is active for the current drag */ |
| 65 | protected boolean mActive; |
Sunny Goyal | 4583d09 | 2016-08-17 11:11:48 -0700 | [diff] [blame] | 66 | /** Whether an accessible drag is in progress */ |
| 67 | private boolean mAccessibleDrag; |
Tony Wickham | 855b1b5 | 2016-03-28 16:56:30 -0700 | [diff] [blame] | 68 | /** An item must be dragged at least this many pixels before this drop target is enabled. */ |
| 69 | private final int mDragDistanceThreshold; |
Yogisha Dixit | ed052ab | 2021-06-08 21:19:15 +0100 | [diff] [blame] | 70 | /** The size of the drawable shown in the drop target. */ |
| 71 | private final int mDrawableSize; |
Steven Ng | a1dcbce | 2021-07-23 17:06:10 +0100 | [diff] [blame] | 72 | /** The padding, in pixels, between the text and drawable. */ |
| 73 | private final int mDrawablePadding; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 74 | |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 75 | protected CharSequence mText; |
Sunny Goyal | 3a644ed | 2015-05-21 10:28:02 -0700 | [diff] [blame] | 76 | protected Drawable mDrawable; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 77 | private boolean mTextVisible = true; |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 78 | private boolean mIconVisible = true; |
| 79 | private boolean mTextMultiLine = true; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 80 | |
| 81 | private PopupWindow mToolTip; |
| 82 | private int mToolTipLocation; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 83 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 84 | public ButtonDropTarget(Context context, AttributeSet attrs) { |
| 85 | this(context, attrs, 0); |
| 86 | } |
| 87 | |
| 88 | public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 89 | super(context, attrs, defStyle); |
Tony | 2fd0208 | 2016-10-07 12:50:01 -0700 | [diff] [blame] | 90 | mLauncher = Launcher.getLauncher(context); |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 91 | |
Tony Wickham | 855b1b5 | 2016-03-28 16:56:30 -0700 | [diff] [blame] | 92 | Resources resources = getResources(); |
Tony Wickham | 855b1b5 | 2016-03-28 16:56:30 -0700 | [diff] [blame] | 93 | mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold); |
Yogisha Dixit | ed052ab | 2021-06-08 21:19:15 +0100 | [diff] [blame] | 94 | mDrawableSize = resources.getDimensionPixelSize(R.dimen.drop_target_text_size); |
Steven Ng | a1dcbce | 2021-07-23 17:06:10 +0100 | [diff] [blame] | 95 | mDrawablePadding = resources.getDimensionPixelSize( |
| 96 | R.dimen.drop_target_button_drawable_padding); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 99 | @Override |
| 100 | protected void onFinishInflate() { |
| 101 | super.onFinishInflate(); |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 102 | mText = getText(); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 103 | setContentDescription(mText); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 106 | protected void updateText(int resId) { |
| 107 | setText(resId); |
| 108 | mText = getText(); |
| 109 | setContentDescription(mText); |
| 110 | } |
| 111 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 112 | protected void setDrawable(int resId) { |
Sunny Goyal | e0cab30 | 2015-05-20 16:40:30 -0700 | [diff] [blame] | 113 | // We do not set the drawable in the xml as that inflates two drawables corresponding to |
| 114 | // drawableLeft and drawableStart. |
Yogisha Dixit | 58c5e65 | 2021-06-16 14:18:22 +0100 | [diff] [blame] | 115 | mDrawable = getContext().getDrawable(resId).mutate(); |
Yogisha Dixit | 3cbd7e3 | 2021-05-29 00:26:25 +0100 | [diff] [blame] | 116 | mDrawable.setTintList(getTextColors()); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 117 | updateIconVisibility(); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 120 | public void setDropTargetBar(DropTargetBar dropTargetBar) { |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 121 | mDropTargetBar = dropTargetBar; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 124 | private void hideTooltip() { |
| 125 | if (mToolTip != null) { |
| 126 | mToolTip.dismiss(); |
| 127 | mToolTip = null; |
| 128 | } |
| 129 | } |
| 130 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 131 | @Override |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 132 | public final void onDragEnter(DragObject d) { |
Sunny Goyal | 9b18010 | 2020-03-11 10:02:29 -0700 | [diff] [blame] | 133 | if (!mAccessibleDrag && !mTextVisible) { |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 134 | // Show tooltip |
| 135 | hideTooltip(); |
| 136 | |
| 137 | TextView message = (TextView) LayoutInflater.from(getContext()).inflate( |
| 138 | R.layout.drop_target_tool_tip, null); |
| 139 | message.setText(mText); |
| 140 | |
| 141 | mToolTip = new PopupWindow(message, WRAP_CONTENT, WRAP_CONTENT); |
| 142 | int x = 0, y = 0; |
| 143 | if (mToolTipLocation != TOOLTIP_DEFAULT) { |
| 144 | y = -getMeasuredHeight(); |
| 145 | message.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); |
| 146 | if (mToolTipLocation == TOOLTIP_LEFT) { |
Sebastian Franco | d2d8e97 | 2022-04-04 14:51:53 -0700 | [diff] [blame] | 147 | x = -getMeasuredWidth() - message.getMeasuredWidth() / 2; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 148 | } else { |
| 149 | x = getMeasuredWidth() / 2 + message.getMeasuredWidth() / 2; |
| 150 | } |
| 151 | } |
| 152 | mToolTip.showAsDropDown(this, x, y); |
| 153 | } |
| 154 | |
Steven Ng | 6a2acfa | 2021-03-19 13:23:53 +0000 | [diff] [blame] | 155 | d.dragView.setAlpha(DRAG_VIEW_HOVER_OVER_OPACITY); |
Yogisha Dixit | 2ff963b | 2021-06-03 10:41:44 +0100 | [diff] [blame] | 156 | setSelected(true); |
Sunny Goyal | d21301e | 2015-09-25 12:17:08 -0700 | [diff] [blame] | 157 | if (d.stateAnnouncer != null) { |
| 158 | d.stateAnnouncer.cancel(); |
| 159 | } |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 160 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 163 | @Override |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 164 | public void onDragOver(DragObject d) { |
| 165 | // Do nothing |
| 166 | } |
| 167 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 168 | @Override |
| 169 | public final void onDragExit(DragObject d) { |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 170 | hideTooltip(); |
| 171 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 172 | if (!d.dragComplete) { |
Steven Ng | 6a2acfa | 2021-03-19 13:23:53 +0000 | [diff] [blame] | 173 | d.dragView.setAlpha(1f); |
Yogisha Dixit | 2ff963b | 2021-06-03 10:41:44 +0100 | [diff] [blame] | 174 | setSelected(false); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 175 | } else { |
Steven Ng | 6a2acfa | 2021-03-19 13:23:53 +0000 | [diff] [blame] | 176 | d.dragView.setAlpha(DRAG_VIEW_HOVER_OVER_OPACITY); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 177 | } |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 180 | @Override |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 181 | public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) { |
Sunny Goyal | a4647b6 | 2021-02-02 13:45:34 -0800 | [diff] [blame] | 182 | mActive = !options.isKeyboardDrag && supportsDrop(dragObject.dragInfo); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 183 | setVisibility(mActive ? View.VISIBLE : View.GONE); |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 184 | |
| 185 | mAccessibleDrag = options.isAccessibleDrag; |
| 186 | setOnClickListener(mAccessibleDrag ? this : null); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public final boolean acceptDrop(DragObject dragObject) { |
Sunny Goyal | 1ce9c47 | 2017-10-03 16:17:32 -0700 | [diff] [blame] | 191 | return supportsDrop(dragObject.dragInfo); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Sunny Goyal | 1ce9c47 | 2017-10-03 16:17:32 -0700 | [diff] [blame] | 194 | protected abstract boolean supportsDrop(ItemInfo info); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 195 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 196 | public abstract boolean supportsAccessibilityDrop(ItemInfo info, View view); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 197 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 198 | @Override |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 199 | public boolean isDropEnabled() { |
Sunny Goyal | 4583d09 | 2016-08-17 11:11:48 -0700 | [diff] [blame] | 200 | return mActive && (mAccessibleDrag || |
| 201 | mLauncher.getDragController().getDistanceDragged() >= mDragDistanceThreshold); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 204 | @Override |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 205 | public void onDragEnd() { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 206 | mActive = false; |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 207 | setOnClickListener(null); |
Yogisha Dixit | 2ff963b | 2021-06-03 10:41:44 +0100 | [diff] [blame] | 208 | setSelected(false); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 211 | /** |
| 212 | * On drop animate the dropView to the icon. |
| 213 | */ |
| 214 | @Override |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 215 | public void onDrop(final DragObject d, final DragOptions options) { |
Tony Wickham | 6a71a5b | 2018-08-21 11:40:23 -0700 | [diff] [blame] | 216 | if (options.isFlingToDelete) { |
| 217 | // FlingAnimation handles the animation and then calls completeDrop(). |
| 218 | return; |
| 219 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 220 | final DragLayer dragLayer = mLauncher.getDragLayer(); |
Pierre Barbier de Reuille | 2881801 | 2021-06-29 16:47:41 +0100 | [diff] [blame] | 221 | final DragView dragView = d.dragView; |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 222 | final Rect to = getIconRect(d); |
Sunny Goyal | 031418c | 2021-07-22 10:02:50 -0700 | [diff] [blame] | 223 | final float scale = (float) to.width() / dragView.getMeasuredWidth(); |
Pierre Barbier de Reuille | 2881801 | 2021-06-29 16:47:41 +0100 | [diff] [blame] | 224 | dragView.detachContentView(/* reattachToPreviousParent= */ true); |
| 225 | |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 226 | mDropTargetBar.deferOnDragEnd(); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 227 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 228 | Runnable onAnimationEndRunnable = () -> { |
| 229 | completeDrop(d); |
| 230 | mDropTargetBar.onDragEnd(); |
| 231 | mLauncher.getStateManager().goToState(NORMAL); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 232 | }; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 233 | |
Sunny Goyal | 1721ccf | 2021-06-08 12:57:40 -0700 | [diff] [blame] | 234 | dragLayer.animateView(d.dragView, to, scale, 0.1f, 0.1f, |
Sunny Goyal | dec3a90 | 2017-01-25 18:23:36 -0800 | [diff] [blame] | 235 | DRAG_VIEW_DROP_DURATION, |
Sunny Goyal | 1721ccf | 2021-06-08 12:57:40 -0700 | [diff] [blame] | 236 | Interpolators.DEACCEL_2, onAnimationEndRunnable, |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 237 | DragLayer.ANIMATION_END_DISAPPEAR, null); |
| 238 | } |
| 239 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 240 | public abstract int getAccessibilityAction(); |
| 241 | |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 242 | @Override |
| 243 | public void prepareAccessibilityDrop() { } |
| 244 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 245 | public abstract void onAccessibilityDrop(View view, ItemInfo item); |
| 246 | |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 247 | public abstract void completeDrop(DragObject d); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 248 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 249 | @Override |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 250 | public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) { |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 251 | super.getHitRect(outRect); |
Alex Chau | a02eddc | 2021-04-29 00:36:06 +0100 | [diff] [blame] | 252 | outRect.bottom += mLauncher.getDeviceProfile().dropTargetDragPaddingPx; |
Winson Chung | 156ab5b | 2013-07-12 14:14:16 -0700 | [diff] [blame] | 253 | |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 254 | sTempCords[0] = sTempCords[1] = 0; |
| 255 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, sTempCords); |
| 256 | outRect.offsetTo(sTempCords[0], sTempCords[1]); |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 259 | public Rect getIconRect(DragObject dragObject) { |
| 260 | int viewWidth = dragObject.dragView.getMeasuredWidth(); |
| 261 | int viewHeight = dragObject.dragView.getMeasuredHeight(); |
| 262 | int drawableWidth = mDrawable.getIntrinsicWidth(); |
| 263 | int drawableHeight = mDrawable.getIntrinsicHeight(); |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 264 | DragLayer dragLayer = mLauncher.getDragLayer(); |
| 265 | |
| 266 | // Find the rect to animate to (the view is center aligned) |
| 267 | Rect to = new Rect(); |
| 268 | dragLayer.getViewRectRelativeToSelf(this, to); |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 269 | |
| 270 | final int width = drawableWidth; |
| 271 | final int height = drawableHeight; |
| 272 | |
| 273 | final int left; |
| 274 | final int right; |
| 275 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 276 | if (Utilities.isRtl(getResources())) { |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 277 | right = to.right - getPaddingRight(); |
| 278 | left = right - width; |
| 279 | } else { |
| 280 | left = to.left + getPaddingLeft(); |
| 281 | right = left + width; |
| 282 | } |
| 283 | |
| 284 | final int top = to.top + (getMeasuredHeight() - height) / 2; |
Sebastian Franco | 63d1a18 | 2021-10-18 09:52:04 -0500 | [diff] [blame] | 285 | final int bottom = top + height; |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 286 | |
| 287 | to.set(left, top, right, bottom); |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 288 | |
| 289 | // Center the destination rect about the trash icon |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 290 | final int xOffset = -(viewWidth - width) / 2; |
| 291 | final int yOffset = -(viewHeight - height) / 2; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 292 | to.offset(xOffset, yOffset); |
| 293 | |
| 294 | return to; |
| 295 | } |
| 296 | |
Sebastian Franco | 63d1a18 | 2021-10-18 09:52:04 -0500 | [diff] [blame] | 297 | private void centerIcon() { |
| 298 | int x = mTextVisible ? 0 |
| 299 | : (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 - mDrawableSize / 2; |
| 300 | mDrawable.setBounds(x, 0, x + mDrawableSize, mDrawableSize); |
| 301 | } |
| 302 | |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 303 | @Override |
| 304 | public void onClick(View v) { |
Sunny Goyal | ae50284 | 2016-06-17 08:43:56 -0700 | [diff] [blame] | 305 | mLauncher.getAccessibilityDelegate().handleAccessibleDrop(this, null, null); |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 306 | } |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 307 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 308 | public void setTextVisible(boolean isVisible) { |
Jon Miranda | df1ffb9 | 2018-02-06 10:31:38 -0800 | [diff] [blame] | 309 | CharSequence newText = isVisible ? mText : ""; |
| 310 | if (mTextVisible != isVisible || !TextUtils.equals(newText, getText())) { |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 311 | mTextVisible = isVisible; |
Jon Miranda | df1ffb9 | 2018-02-06 10:31:38 -0800 | [diff] [blame] | 312 | setText(newText); |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 313 | updateIconVisibility(); |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame] | 314 | } |
Pat Manning | 27bfcaa | 2022-04-25 13:50:28 +0100 | [diff] [blame] | 315 | } |
| 316 | |
Alex Chau | 906d882 | 2022-05-23 10:42:25 +0100 | [diff] [blame] | 317 | /** |
| 318 | * Display button text over multiple lines when isMultiLine is true, single line otherwise. |
| 319 | */ |
| 320 | public void setTextMultiLine(boolean isMultiLine) { |
| 321 | if (mTextMultiLine != isMultiLine) { |
| 322 | mTextMultiLine = isMultiLine; |
| 323 | setSingleLine(!isMultiLine); |
| 324 | setMaxLines(isMultiLine ? MAX_LINES_TEXT_MULTI_LINE : MAX_LINES_TEXT_SINGLE_LINE); |
| 325 | int inputType = InputType.TYPE_CLASS_TEXT; |
| 326 | if (isMultiLine) { |
| 327 | inputType |= InputType.TYPE_TEXT_FLAG_MULTI_LINE; |
| 328 | |
| 329 | } |
| 330 | setInputType(inputType); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | protected boolean isTextMultiLine() { |
| 335 | return mTextMultiLine; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Sets the button icon visible when isVisible is true, hides it otherwise. |
| 340 | */ |
| 341 | public void setIconVisible(boolean isVisible) { |
| 342 | if (mIconVisible != isVisible) { |
| 343 | mIconVisible = isVisible; |
| 344 | updateIconVisibility(); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | private void updateIconVisibility() { |
| 349 | if (mIconVisible) { |
| 350 | centerIcon(); |
| 351 | } |
| 352 | setCompoundDrawablesRelative(mIconVisible ? mDrawable : null, null, null, null); |
| 353 | setCompoundDrawablePadding(mIconVisible && mTextVisible ? mDrawablePadding : 0); |
| 354 | } |
| 355 | |
Sebastian Franco | 63d1a18 | 2021-10-18 09:52:04 -0500 | [diff] [blame] | 356 | @Override |
| 357 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 358 | super.onSizeChanged(w, h, oldw, oldh); |
| 359 | centerIcon(); |
| 360 | } |
| 361 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 362 | public void setToolTipLocation(int location) { |
| 363 | mToolTipLocation = location; |
| 364 | hideTooltip(); |
| 365 | } |
| 366 | |
| 367 | public boolean isTextTruncated(int availableWidth) { |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 368 | availableWidth -= (getPaddingLeft() + getPaddingRight() + mDrawable.getIntrinsicWidth() |
| 369 | + getCompoundDrawablePadding()); |
| 370 | CharSequence displayedText = TextUtils.ellipsize(mText, getPaint(), availableWidth, |
| 371 | TextUtils.TruncateAt.END); |
| 372 | return !mText.equals(displayedText); |
| 373 | } |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 374 | } |