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