blob: e1b5e2c7544e8bd2b62ca573af24cdcf03f2431e [file] [log] [blame]
Winson Chung61fa4192011-06-12 15:15:29 -07001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung61fa4192011-06-12 15:15:29 -070018
Sunny Goyal3a644ed2015-05-21 10:28:02 -070019import android.animation.AnimatorSet;
20import android.animation.FloatArrayEvaluator;
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070021import android.animation.ObjectAnimator;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070022import android.animation.ValueAnimator;
23import android.animation.ValueAnimator.AnimatorUpdateListener;
Winson Chung61fa4192011-06-12 15:15:29 -070024import android.content.Context;
Sunny Goyalfa401a12015-04-10 13:45:42 -070025import android.content.res.ColorStateList;
Tony Wickham855b1b52016-03-28 16:56:30 -070026import android.content.res.Resources;
Sunny Goyalf37a2142016-03-24 17:28:25 -070027import android.content.res.TypedArray;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070028import android.graphics.ColorMatrix;
29import android.graphics.ColorMatrixColorFilter;
Winson Chung61967cb2012-02-28 18:11:33 -080030import android.graphics.Rect;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070031import android.graphics.drawable.Drawable;
Jon Mirandabfaa4a42017-08-21 15:31:51 -070032import android.text.TextUtils;
Winson Chung61fa4192011-06-12 15:15:29 -070033import android.util.AttributeSet;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080034import android.view.View;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070035import android.view.View.OnClickListener;
Sunny Goyalfa401a12015-04-10 13:45:42 -070036import android.view.ViewGroup;
Sunny Goyale78e3d72015-09-24 11:23:31 -070037import android.view.accessibility.AccessibilityEvent;
Sunny Goyalfa401a12015-04-10 13:45:42 -070038import android.view.animation.DecelerateInterpolator;
39import android.view.animation.LinearInterpolator;
Adam Cohend4d7aa52011-07-19 21:47:37 -070040import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070041
Vadim Tryshevfedca432015-08-19 17:55:02 -070042import com.android.launcher3.dragndrop.DragController;
43import com.android.launcher3.dragndrop.DragLayer;
Sunny Goyal94b510c2016-08-16 15:36:48 -070044import com.android.launcher3.dragndrop.DragOptions;
Vadim Tryshevfedca432015-08-19 17:55:02 -070045import com.android.launcher3.dragndrop.DragView;
Tony Wickham6c828672017-02-27 13:30:20 -080046import com.android.launcher3.util.Themes;
Sunny Goyalfa401a12015-04-10 13:45:42 -070047import com.android.launcher3.util.Thunk;
Winson Chung61fa4192011-06-12 15:15:29 -070048
49/**
50 * Implements a DropTarget.
51 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070052public abstract class ButtonDropTarget extends TextView
53 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070054
Tony Wickhamb54c4a32015-09-11 08:40:20 -070055 private static final int DRAG_VIEW_DROP_DURATION = 285;
Winson Chung61fa4192011-06-12 15:15:29 -070056
Sunny Goyalf37a2142016-03-24 17:28:25 -070057 private final boolean mHideParentOnDisable;
Sunny Goyal47328fd2016-05-25 18:56:41 -070058 protected final Launcher mLauncher;
Sunny Goyalf37a2142016-03-24 17:28:25 -070059
Winson Chunga62e9fd2011-07-11 15:20:48 -070060 private int mBottomDragPadding;
Sunny Goyal47328fd2016-05-25 18:56:41 -070061 protected DropTargetBar mDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070062
63 /** Whether this drop target is active for the current drag */
64 protected boolean mActive;
Sunny Goyal4583d092016-08-17 11:11:48 -070065 /** Whether an accessible drag is in progress */
66 private boolean mAccessibleDrag;
Tony Wickham855b1b52016-03-28 16:56:30 -070067 /** An item must be dragged at least this many pixels before this drop target is enabled. */
68 private final int mDragDistanceThreshold;
Winson Chung61fa4192011-06-12 15:15:29 -070069
70 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080071 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070072
Jon Mirandabfaa4a42017-08-21 15:31:51 -070073 protected CharSequence mText;
Sunny Goyalfa401a12015-04-10 13:45:42 -070074 protected ColorStateList mOriginalTextColor;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070075 protected Drawable mDrawable;
Sunny Goyalfa401a12015-04-10 13:45:42 -070076
Sunny Goyal3a644ed2015-05-21 10:28:02 -070077 private AnimatorSet mCurrentColorAnim;
Sunny Goyal316490e2015-06-02 09:38:28 -070078 @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070079
Winson Chung61fa4192011-06-12 15:15:29 -070080 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);
Tony2fd02082016-10-07 12:50:01 -070086 mLauncher = Launcher.getLauncher(context);
Sunny Goyal47328fd2016-05-25 18:56:41 -070087
Tony Wickham855b1b52016-03-28 16:56:30 -070088 Resources resources = getResources();
89 mBottomDragPadding = resources.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Sunny Goyalf37a2142016-03-24 17:28:25 -070090
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 Wickham855b1b52016-03-28 16:56:30 -070095 mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -070096 }
97
Sunny Goyalfa401a12015-04-10 13:45:42 -070098 @Override
99 protected void onFinishInflate() {
100 super.onFinishInflate();
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700101 mText = getText();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700102 mOriginalTextColor = getTextColors();
Winson Chung61fa4192011-06-12 15:15:29 -0700103 }
104
Sunny Goyalfa401a12015-04-10 13:45:42 -0700105 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700106 // We do not set the drawable in the xml as that inflates two drawables corresponding to
107 // drawableLeft and drawableStart.
Sunny Goyal5931ca02017-06-19 17:29:48 -0700108 setCompoundDrawablesRelativeWithIntrinsicBounds(resId, 0, 0, 0);
109 mDrawable = getCompoundDrawablesRelative()[0];
Sunny Goyalfa401a12015-04-10 13:45:42 -0700110 }
111
Sunny Goyal47328fd2016-05-25 18:56:41 -0700112 public void setDropTargetBar(DropTargetBar dropTargetBar) {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700113 mDropTargetBar = dropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -0700114 }
115
Sunny Goyalfa401a12015-04-10 13:45:42 -0700116 @Override
Sunny Goyalfa401a12015-04-10 13:45:42 -0700117 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800118 d.dragView.setColor(mHoverColor);
Sunny Goyala52ecb02016-12-16 15:04:51 -0800119 animateTextColor(mHoverColor);
Sunny Goyald21301e2015-09-25 12:17:08 -0700120 if (d.stateAnnouncer != null) {
121 d.stateAnnouncer.cancel();
122 }
Sunny Goyale78e3d72015-09-24 11:23:31 -0700123 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Winson Chung61fa4192011-06-12 15:15:29 -0700124 }
125
Sunny Goyalfa401a12015-04-10 13:45:42 -0700126 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700127 public void onDragOver(DragObject d) {
128 // Do nothing
129 }
130
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700131 protected void resetHoverColor() {
Sunny Goyala52ecb02016-12-16 15:04:51 -0800132 animateTextColor(mOriginalTextColor.getDefaultColor());
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700133 }
134
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700135 private void animateTextColor(int targetColor) {
136 if (mCurrentColorAnim != null) {
137 mCurrentColorAnim.cancel();
138 }
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700139
140 mCurrentColorAnim = new AnimatorSet();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700141 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700142
143 if (mSrcFilter == null) {
144 mSrcFilter = new ColorMatrix();
145 mDstFilter = new ColorMatrix();
146 mCurrentFilter = new ColorMatrix();
147 }
148
Tony Wickham6c828672017-02-27 13:30:20 -0800149 Themes.setColorScaleOnMatrix(getTextColor(), mSrcFilter);
150 Themes.setColorScaleOnMatrix(targetColor, mDstFilter);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700151 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 Goyalfe0d1f22015-04-28 22:01:31 -0700165 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700166 }
167
Sunny Goyalfa401a12015-04-10 13:45:42 -0700168 @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 Chung61fa4192011-06-12 15:15:29 -0700177 }
178
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700179 @Override
Sunny Goyal94b510c2016-08-16 15:36:48 -0700180 public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700181 mActive = supportsDrop(dragObject.dragInfo);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700182 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700183 if (mCurrentColorAnim != null) {
184 mCurrentColorAnim.cancel();
185 mCurrentColorAnim = null;
186 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700187 setTextColor(mOriginalTextColor);
Sunny Goyalf37a2142016-03-24 17:28:25 -0700188 (mHideParentOnDisable ? ((ViewGroup) getParent()) : this)
189 .setVisibility(mActive ? View.VISIBLE : View.GONE);
Sunny Goyal94b510c2016-08-16 15:36:48 -0700190
191 mAccessibleDrag = options.isAccessibleDrag;
192 setOnClickListener(mAccessibleDrag ? this : null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700193 }
194
195 @Override
196 public final boolean acceptDrop(DragObject dragObject) {
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700197 return supportsDrop(dragObject.dragInfo);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700198 }
199
Sunny Goyal1ce9c472017-10-03 16:17:32 -0700200 protected abstract boolean supportsDrop(ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700201
202 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700203 public boolean isDropEnabled() {
Sunny Goyal4583d092016-08-17 11:11:48 -0700204 return mActive && (mAccessibleDrag ||
205 mLauncher.getDragController().getDistanceDragged() >= mDragDistanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -0700206 }
207
Sunny Goyalfa401a12015-04-10 13:45:42 -0700208 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700209 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700210 mActive = false;
Sunny Goyal94b510c2016-08-16 15:36:48 -0700211 setOnClickListener(null);
Winson Chung61fa4192011-06-12 15:15:29 -0700212 }
213
Sunny Goyalfa401a12015-04-10 13:45:42 -0700214 /**
215 * On drop animate the dropView to the icon.
216 */
217 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -0700218 public void onDrop(final DragObject d, final DragOptions options) {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700219 final DragLayer dragLayer = mLauncher.getDragLayer();
220 final Rect from = new Rect();
221 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
222
Sunny Goyal0f76b562016-12-13 19:37:10 -0800223 final Rect to = getIconRect(d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700224 final float scale = (float) to.width() / from.width();
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700225 mDropTargetBar.deferOnDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700226
227 Runnable onAnimationEndRunnable = new Runnable() {
228 @Override
229 public void run() {
230 completeDrop(d);
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700231 mDropTargetBar.onDragEnd();
Sunny Goyal1797af42017-10-06 13:29:57 -0700232 mLauncher.exitSpringLoadedDragMode(true, 0);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700233 }
234 };
235 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
Sunny Goyaldec3a902017-01-25 18:23:36 -0800236 DRAG_VIEW_DROP_DURATION,
Sunny Goyald139b0a2016-09-12 14:36:30 -0700237 new DecelerateInterpolator(2),
Sunny Goyalfa401a12015-04-10 13:45:42 -0700238 new LinearInterpolator(), onAnimationEndRunnable,
239 DragLayer.ANIMATION_END_DISAPPEAR, null);
240 }
241
Sunny Goyale9b651e2015-04-24 11:44:51 -0700242 @Override
243 public void prepareAccessibilityDrop() { }
244
Sunny Goyal0f76b562016-12-13 19:37:10 -0800245 public abstract void completeDrop(DragObject d);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700246
Winson Chung61fa4192011-06-12 15:15:29 -0700247 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700248 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700249 super.getHitRect(outRect);
250 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700251
252 int[] coords = new int[2];
253 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
254 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700255 }
256
Sunny Goyal0f76b562016-12-13 19:37:10 -0800257 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 Chung61967cb2012-02-28 18:11:33 -0800262 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 Megliod6a33c62013-02-06 15:40:46 -0800267
268 final int width = drawableWidth;
269 final int height = drawableHeight;
270
271 final int left;
272 final int right;
273
Sunny Goyal70660032015-05-14 00:07:08 -0700274 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800275 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 Chung61967cb2012-02-28 18:11:33 -0800286
287 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800288 final int xOffset = (int) -(viewWidth - width) / 2;
289 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800290 to.offset(xOffset, yOffset);
291
292 return to;
293 }
294
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700295 @Override
296 public void onClick(View v) {
Sunny Goyalae502842016-06-17 08:43:56 -0700297 mLauncher.getAccessibilityDelegate().handleAccessibleDrop(this, null, null);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700298 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700299
300 public int getTextColor() {
301 return getTextColors().getDefaultColor();
302 }
Jon Mirandabfaa4a42017-08-21 15:31:51 -0700303
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 Chung61fa4192011-06-12 15:15:29 -0700329}