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