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 | |
Sunny Goyal | 3a644ed | 2015-05-21 10:28:02 -0700 | [diff] [blame] | 23 | import android.animation.AnimatorSet; |
| 24 | import android.animation.FloatArrayEvaluator; |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 25 | import android.animation.ObjectAnimator; |
Sunny Goyal | 3a644ed | 2015-05-21 10:28:02 -0700 | [diff] [blame] | 26 | import android.animation.ValueAnimator; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 27 | import android.content.Context; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 28 | import android.content.res.ColorStateList; |
Tony Wickham | 855b1b5 | 2016-03-28 16:56:30 -0700 | [diff] [blame] | 29 | import android.content.res.Resources; |
Sunny Goyal | 3a644ed | 2015-05-21 10:28:02 -0700 | [diff] [blame] | 30 | import android.graphics.ColorMatrix; |
| 31 | import android.graphics.ColorMatrixColorFilter; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 32 | import android.graphics.Rect; |
Sunny Goyal | 3a644ed | 2015-05-21 10:28:02 -0700 | [diff] [blame] | 33 | import android.graphics.drawable.Drawable; |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 34 | import android.text.TextUtils; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 35 | import android.util.AttributeSet; |
Sunny Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 36 | import android.util.Property; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 37 | import android.view.LayoutInflater; |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 38 | import android.view.View; |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 39 | import android.view.View.OnClickListener; |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 40 | import android.view.accessibility.AccessibilityEvent; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 41 | import android.widget.PopupWindow; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 42 | import android.widget.TextView; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 43 | |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 44 | import com.android.launcher3.anim.Interpolators; |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 45 | import com.android.launcher3.dragndrop.DragController; |
| 46 | import com.android.launcher3.dragndrop.DragLayer; |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 47 | import com.android.launcher3.dragndrop.DragOptions; |
Vadim Tryshev | fedca43 | 2015-08-19 17:55:02 -0700 | [diff] [blame] | 48 | import com.android.launcher3.dragndrop.DragView; |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame] | 49 | import com.android.launcher3.userevent.nano.LauncherLogProto.Target; |
Tony Wickham | 6c82867 | 2017-02-27 13:30:20 -0800 | [diff] [blame] | 50 | import com.android.launcher3.util.Themes; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 51 | import com.android.launcher3.util.Thunk; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Implements a DropTarget. |
| 55 | */ |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 56 | public abstract class ButtonDropTarget extends TextView |
| 57 | implements DropTarget, DragController.DragListener, OnClickListener { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 58 | |
Sunny Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 59 | private static final Property<ButtonDropTarget, Integer> TEXT_COLOR = |
| 60 | new Property<ButtonDropTarget, Integer>(Integer.TYPE, "textColor") { |
| 61 | |
| 62 | @Override |
| 63 | public Integer get(ButtonDropTarget target) { |
| 64 | return target.getTextColor(); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void set(ButtonDropTarget target, Integer value) { |
| 69 | target.setTextColor(value); |
| 70 | } |
| 71 | }; |
| 72 | |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 73 | private static final int[] sTempCords = new int[2]; |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 74 | private static final int DRAG_VIEW_DROP_DURATION = 285; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 75 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 76 | public static final int TOOLTIP_DEFAULT = 0; |
| 77 | public static final int TOOLTIP_LEFT = 1; |
| 78 | public static final int TOOLTIP_RIGHT = 2; |
| 79 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 80 | protected final Launcher mLauncher; |
Sunny Goyal | f37a214 | 2016-03-24 17:28:25 -0700 | [diff] [blame] | 81 | |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 82 | private int mBottomDragPadding; |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 83 | protected DropTargetBar mDropTargetBar; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 84 | |
| 85 | /** Whether this drop target is active for the current drag */ |
| 86 | protected boolean mActive; |
Sunny Goyal | 4583d09 | 2016-08-17 11:11:48 -0700 | [diff] [blame] | 87 | /** Whether an accessible drag is in progress */ |
| 88 | private boolean mAccessibleDrag; |
Tony Wickham | 855b1b5 | 2016-03-28 16:56:30 -0700 | [diff] [blame] | 89 | /** An item must be dragged at least this many pixels before this drop target is enabled. */ |
| 90 | private final int mDragDistanceThreshold; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 91 | |
| 92 | /** The paint applied to the drag view on hover */ |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 93 | protected int mHoverColor = 0; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 94 | |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 95 | protected CharSequence mText; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 96 | protected ColorStateList mOriginalTextColor; |
Sunny Goyal | 3a644ed | 2015-05-21 10:28:02 -0700 | [diff] [blame] | 97 | protected Drawable mDrawable; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 98 | private boolean mTextVisible = true; |
| 99 | |
| 100 | private PopupWindow mToolTip; |
| 101 | private int mToolTipLocation; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 102 | |
Sunny Goyal | 3a644ed | 2015-05-21 10:28:02 -0700 | [diff] [blame] | 103 | private AnimatorSet mCurrentColorAnim; |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 104 | @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter; |
Sunny Goyal | 3a644ed | 2015-05-21 10:28:02 -0700 | [diff] [blame] | 105 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 106 | public ButtonDropTarget(Context context, AttributeSet attrs) { |
| 107 | this(context, attrs, 0); |
| 108 | } |
| 109 | |
| 110 | public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 111 | super(context, attrs, defStyle); |
Tony | 2fd0208 | 2016-10-07 12:50:01 -0700 | [diff] [blame] | 112 | mLauncher = Launcher.getLauncher(context); |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 113 | |
Tony Wickham | 855b1b5 | 2016-03-28 16:56:30 -0700 | [diff] [blame] | 114 | Resources resources = getResources(); |
| 115 | mBottomDragPadding = resources.getDimensionPixelSize(R.dimen.drop_target_drag_padding); |
Tony Wickham | 855b1b5 | 2016-03-28 16:56:30 -0700 | [diff] [blame] | 116 | mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 119 | @Override |
| 120 | protected void onFinishInflate() { |
| 121 | super.onFinishInflate(); |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 122 | mText = getText(); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 123 | mOriginalTextColor = getTextColors(); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 124 | setContentDescription(mText); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 127 | protected void updateText(int resId) { |
| 128 | setText(resId); |
| 129 | mText = getText(); |
| 130 | setContentDescription(mText); |
| 131 | } |
| 132 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 133 | protected void setDrawable(int resId) { |
Sunny Goyal | e0cab30 | 2015-05-20 16:40:30 -0700 | [diff] [blame] | 134 | // We do not set the drawable in the xml as that inflates two drawables corresponding to |
| 135 | // drawableLeft and drawableStart. |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 136 | if (mTextVisible) { |
| 137 | setCompoundDrawablesRelativeWithIntrinsicBounds(resId, 0, 0, 0); |
| 138 | mDrawable = getCompoundDrawablesRelative()[0]; |
| 139 | } else { |
| 140 | setCompoundDrawablesRelativeWithIntrinsicBounds(0, resId, 0, 0); |
| 141 | mDrawable = getCompoundDrawablesRelative()[1]; |
| 142 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Sunny Goyal | 47328fd | 2016-05-25 18:56:41 -0700 | [diff] [blame] | 145 | public void setDropTargetBar(DropTargetBar dropTargetBar) { |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 146 | mDropTargetBar = dropTargetBar; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 149 | private void hideTooltip() { |
| 150 | if (mToolTip != null) { |
| 151 | mToolTip.dismiss(); |
| 152 | mToolTip = null; |
| 153 | } |
| 154 | } |
| 155 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 156 | @Override |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 157 | public final void onDragEnter(DragObject d) { |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 158 | if (!d.accessibleDrag && !mTextVisible) { |
| 159 | // Show tooltip |
| 160 | hideTooltip(); |
| 161 | |
| 162 | TextView message = (TextView) LayoutInflater.from(getContext()).inflate( |
| 163 | R.layout.drop_target_tool_tip, null); |
| 164 | message.setText(mText); |
| 165 | |
| 166 | mToolTip = new PopupWindow(message, WRAP_CONTENT, WRAP_CONTENT); |
| 167 | int x = 0, y = 0; |
| 168 | if (mToolTipLocation != TOOLTIP_DEFAULT) { |
| 169 | y = -getMeasuredHeight(); |
| 170 | message.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); |
| 171 | if (mToolTipLocation == TOOLTIP_LEFT) { |
| 172 | x = - getMeasuredWidth() - message.getMeasuredWidth() / 2; |
| 173 | } else { |
| 174 | x = getMeasuredWidth() / 2 + message.getMeasuredWidth() / 2; |
| 175 | } |
| 176 | } |
| 177 | mToolTip.showAsDropDown(this, x, y); |
| 178 | } |
| 179 | |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 180 | d.dragView.setColor(mHoverColor); |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 181 | animateTextColor(mHoverColor); |
Sunny Goyal | d21301e | 2015-09-25 12:17:08 -0700 | [diff] [blame] | 182 | if (d.stateAnnouncer != null) { |
| 183 | d.stateAnnouncer.cancel(); |
| 184 | } |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 185 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 188 | @Override |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 189 | public void onDragOver(DragObject d) { |
| 190 | // Do nothing |
| 191 | } |
| 192 | |
Sunny Goyal | 9fc953b | 2015-08-17 12:24:25 -0700 | [diff] [blame] | 193 | protected void resetHoverColor() { |
Sunny Goyal | a52ecb0 | 2016-12-16 15:04:51 -0800 | [diff] [blame] | 194 | animateTextColor(mOriginalTextColor.getDefaultColor()); |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 197 | private void animateTextColor(int targetColor) { |
| 198 | if (mCurrentColorAnim != null) { |
| 199 | mCurrentColorAnim.cancel(); |
| 200 | } |
Sunny Goyal | 3a644ed | 2015-05-21 10:28:02 -0700 | [diff] [blame] | 201 | |
| 202 | mCurrentColorAnim = new AnimatorSet(); |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 203 | mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION); |
Sunny Goyal | 3a644ed | 2015-05-21 10:28:02 -0700 | [diff] [blame] | 204 | |
| 205 | if (mSrcFilter == null) { |
| 206 | mSrcFilter = new ColorMatrix(); |
| 207 | mDstFilter = new ColorMatrix(); |
| 208 | mCurrentFilter = new ColorMatrix(); |
| 209 | } |
| 210 | |
Mehdi Alizadeh | 3e024c0 | 2018-04-30 11:15:39 -0700 | [diff] [blame] | 211 | int defaultTextColor = mOriginalTextColor.getDefaultColor(); |
| 212 | Themes.setColorChangeOnMatrix(defaultTextColor, getTextColor(), mSrcFilter); |
| 213 | Themes.setColorChangeOnMatrix(defaultTextColor, targetColor, mDstFilter); |
| 214 | |
Sunny Goyal | 3a644ed | 2015-05-21 10:28:02 -0700 | [diff] [blame] | 215 | ValueAnimator anim1 = ValueAnimator.ofObject( |
| 216 | new FloatArrayEvaluator(mCurrentFilter.getArray()), |
| 217 | mSrcFilter.getArray(), mDstFilter.getArray()); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 218 | anim1.addUpdateListener((anim) -> { |
| 219 | mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter)); |
| 220 | invalidate(); |
Sunny Goyal | 3a644ed | 2015-05-21 10:28:02 -0700 | [diff] [blame] | 221 | }); |
| 222 | |
| 223 | mCurrentColorAnim.play(anim1); |
Sunny Goyal | 55bdeed | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 224 | mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, TEXT_COLOR, targetColor)); |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 225 | mCurrentColorAnim.start(); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 228 | @Override |
| 229 | public final void onDragExit(DragObject d) { |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 230 | hideTooltip(); |
| 231 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 232 | if (!d.dragComplete) { |
| 233 | d.dragView.setColor(0); |
| 234 | resetHoverColor(); |
| 235 | } else { |
| 236 | // Restore the hover color |
| 237 | d.dragView.setColor(mHoverColor); |
| 238 | } |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 241 | @Override |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 242 | public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) { |
Sunny Goyal | 1ce9c47 | 2017-10-03 16:17:32 -0700 | [diff] [blame] | 243 | mActive = supportsDrop(dragObject.dragInfo); |
Sunny Goyal | 3a644ed | 2015-05-21 10:28:02 -0700 | [diff] [blame] | 244 | mDrawable.setColorFilter(null); |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 245 | if (mCurrentColorAnim != null) { |
| 246 | mCurrentColorAnim.cancel(); |
| 247 | mCurrentColorAnim = null; |
| 248 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 249 | setTextColor(mOriginalTextColor); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 250 | setVisibility(mActive ? View.VISIBLE : View.GONE); |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 251 | |
| 252 | mAccessibleDrag = options.isAccessibleDrag; |
| 253 | setOnClickListener(mAccessibleDrag ? this : null); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public final boolean acceptDrop(DragObject dragObject) { |
Sunny Goyal | 1ce9c47 | 2017-10-03 16:17:32 -0700 | [diff] [blame] | 258 | return supportsDrop(dragObject.dragInfo); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Sunny Goyal | 1ce9c47 | 2017-10-03 16:17:32 -0700 | [diff] [blame] | 261 | protected abstract boolean supportsDrop(ItemInfo info); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 262 | |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 263 | public abstract boolean supportsAccessibilityDrop(ItemInfo info, View view); |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 264 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 265 | @Override |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 266 | public boolean isDropEnabled() { |
Sunny Goyal | 4583d09 | 2016-08-17 11:11:48 -0700 | [diff] [blame] | 267 | return mActive && (mAccessibleDrag || |
| 268 | mLauncher.getDragController().getDistanceDragged() >= mDragDistanceThreshold); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 271 | @Override |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 272 | public void onDragEnd() { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 273 | mActive = false; |
Sunny Goyal | 94b510c | 2016-08-16 15:36:48 -0700 | [diff] [blame] | 274 | setOnClickListener(null); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 277 | /** |
| 278 | * On drop animate the dropView to the icon. |
| 279 | */ |
| 280 | @Override |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 281 | public void onDrop(final DragObject d, final DragOptions options) { |
Tony Wickham | 6a71a5b | 2018-08-21 11:40:23 -0700 | [diff] [blame] | 282 | if (options.isFlingToDelete) { |
| 283 | // FlingAnimation handles the animation and then calls completeDrop(). |
| 284 | return; |
| 285 | } |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 286 | final DragLayer dragLayer = mLauncher.getDragLayer(); |
| 287 | final Rect from = new Rect(); |
| 288 | dragLayer.getViewRectRelativeToSelf(d.dragView, from); |
| 289 | |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 290 | final Rect to = getIconRect(d); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 291 | final float scale = (float) to.width() / from.width(); |
Tony Wickham | b54c4a3 | 2015-09-11 08:40:20 -0700 | [diff] [blame] | 292 | mDropTargetBar.deferOnDragEnd(); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 293 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 294 | Runnable onAnimationEndRunnable = () -> { |
| 295 | completeDrop(d); |
| 296 | mDropTargetBar.onDragEnd(); |
| 297 | mLauncher.getStateManager().goToState(NORMAL); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 298 | }; |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 299 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 300 | dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f, |
Sunny Goyal | dec3a90 | 2017-01-25 18:23:36 -0800 | [diff] [blame] | 301 | DRAG_VIEW_DROP_DURATION, |
Sunny Goyal | 5bc6b6f | 2017-10-26 15:36:10 -0700 | [diff] [blame] | 302 | Interpolators.DEACCEL_2, Interpolators.LINEAR, onAnimationEndRunnable, |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 303 | DragLayer.ANIMATION_END_DISAPPEAR, null); |
| 304 | } |
| 305 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 306 | public abstract int getAccessibilityAction(); |
| 307 | |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame] | 308 | @Override |
| 309 | public void prepareAccessibilityDrop() { } |
| 310 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 311 | public abstract void onAccessibilityDrop(View view, ItemInfo item); |
| 312 | |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 313 | public abstract void completeDrop(DragObject d); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 314 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 315 | @Override |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 316 | public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) { |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 317 | super.getHitRect(outRect); |
| 318 | outRect.bottom += mBottomDragPadding; |
Winson Chung | 156ab5b | 2013-07-12 14:14:16 -0700 | [diff] [blame] | 319 | |
Sunny Goyal | ac00cba | 2017-11-13 15:58:01 -0800 | [diff] [blame] | 320 | sTempCords[0] = sTempCords[1] = 0; |
| 321 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, sTempCords); |
| 322 | outRect.offsetTo(sTempCords[0], sTempCords[1]); |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 325 | public Rect getIconRect(DragObject dragObject) { |
| 326 | int viewWidth = dragObject.dragView.getMeasuredWidth(); |
| 327 | int viewHeight = dragObject.dragView.getMeasuredHeight(); |
| 328 | int drawableWidth = mDrawable.getIntrinsicWidth(); |
| 329 | int drawableHeight = mDrawable.getIntrinsicHeight(); |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 330 | DragLayer dragLayer = mLauncher.getDragLayer(); |
| 331 | |
| 332 | // Find the rect to animate to (the view is center aligned) |
| 333 | Rect to = new Rect(); |
| 334 | dragLayer.getViewRectRelativeToSelf(this, to); |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 335 | |
| 336 | final int width = drawableWidth; |
| 337 | final int height = drawableHeight; |
| 338 | |
| 339 | final int left; |
| 340 | final int right; |
| 341 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 342 | if (Utilities.isRtl(getResources())) { |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 343 | right = to.right - getPaddingRight(); |
| 344 | left = right - width; |
| 345 | } else { |
| 346 | left = to.left + getPaddingLeft(); |
| 347 | right = left + width; |
| 348 | } |
| 349 | |
| 350 | final int top = to.top + (getMeasuredHeight() - height) / 2; |
| 351 | final int bottom = top + height; |
| 352 | |
| 353 | to.set(left, top, right, bottom); |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 354 | |
| 355 | // Center the destination rect about the trash icon |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 356 | final int xOffset = -(viewWidth - width) / 2; |
| 357 | final int yOffset = -(viewHeight - height) / 2; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 358 | to.offset(xOffset, yOffset); |
| 359 | |
| 360 | return to; |
| 361 | } |
| 362 | |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 363 | @Override |
| 364 | public void onClick(View v) { |
Sunny Goyal | ae50284 | 2016-06-17 08:43:56 -0700 | [diff] [blame] | 365 | mLauncher.getAccessibilityDelegate().handleAccessibleDrop(this, null, null); |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 366 | } |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 367 | |
| 368 | public int getTextColor() { |
| 369 | return getTextColors().getDefaultColor(); |
| 370 | } |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 371 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 372 | public void setTextVisible(boolean isVisible) { |
Jon Miranda | df1ffb9 | 2018-02-06 10:31:38 -0800 | [diff] [blame] | 373 | CharSequence newText = isVisible ? mText : ""; |
| 374 | if (mTextVisible != isVisible || !TextUtils.equals(newText, getText())) { |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 375 | mTextVisible = isVisible; |
Jon Miranda | df1ffb9 | 2018-02-06 10:31:38 -0800 | [diff] [blame] | 376 | setText(newText); |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 377 | if (mTextVisible) { |
| 378 | setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null); |
| 379 | } else { |
| 380 | setCompoundDrawablesRelativeWithIntrinsicBounds(null, mDrawable, null, null); |
| 381 | } |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 382 | } |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Sunny Goyal | d1b3f5c | 2018-01-18 17:14:05 -0800 | [diff] [blame] | 385 | public void setToolTipLocation(int location) { |
| 386 | mToolTipLocation = location; |
| 387 | hideTooltip(); |
| 388 | } |
| 389 | |
| 390 | public boolean isTextTruncated(int availableWidth) { |
Jon Miranda | bfaa4a4 | 2017-08-21 15:31:51 -0700 | [diff] [blame] | 391 | availableWidth -= (getPaddingLeft() + getPaddingRight() + mDrawable.getIntrinsicWidth() |
| 392 | + getCompoundDrawablePadding()); |
| 393 | CharSequence displayedText = TextUtils.ellipsize(mText, getPaint(), availableWidth, |
| 394 | TextUtils.TruncateAt.END); |
| 395 | return !mText.equals(displayedText); |
| 396 | } |
Winson Chung | 1054d4e | 2018-03-05 19:39:21 +0000 | [diff] [blame] | 397 | |
Mehdi Alizadeh | bda47cf | 2018-05-01 19:26:05 -0700 | [diff] [blame] | 398 | public abstract Target getDropTargetForLogging(); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 399 | } |