blob: 5a4ed2f548fca74e17275cd9fdf78d369de91fef [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;
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070024import android.annotation.TargetApi;
Winson Chung61fa4192011-06-12 15:15:29 -070025import android.content.Context;
Sunny Goyalfa401a12015-04-10 13:45:42 -070026import android.content.res.ColorStateList;
Tony Wickham855b1b52016-03-28 16:56:30 -070027import android.content.res.Resources;
Sunny Goyalf37a2142016-03-24 17:28:25 -070028import android.content.res.TypedArray;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070029import android.graphics.ColorMatrix;
30import android.graphics.ColorMatrixColorFilter;
Winson Chung043f2af2012-03-01 16:09:54 -080031import android.graphics.PointF;
Winson Chung61967cb2012-02-28 18:11:33 -080032import android.graphics.Rect;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070033import android.graphics.drawable.Drawable;
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070034import android.os.Build;
Winson Chung61fa4192011-06-12 15:15:29 -070035import android.util.AttributeSet;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080036import android.view.View;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070037import android.view.View.OnClickListener;
Sunny Goyalfa401a12015-04-10 13:45:42 -070038import android.view.ViewGroup;
Sunny Goyale78e3d72015-09-24 11:23:31 -070039import android.view.accessibility.AccessibilityEvent;
Sunny Goyalfa401a12015-04-10 13:45:42 -070040import android.view.animation.DecelerateInterpolator;
41import android.view.animation.LinearInterpolator;
Adam Cohend4d7aa52011-07-19 21:47:37 -070042import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070043
Vadim Tryshevfedca432015-08-19 17:55:02 -070044import com.android.launcher3.dragndrop.DragController;
45import com.android.launcher3.dragndrop.DragLayer;
46import com.android.launcher3.dragndrop.DragView;
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;
Tony Wickham855b1b52016-03-28 16:56:30 -070065 /** An item must be dragged at least this many pixels before this drop target is enabled. */
66 private final int mDragDistanceThreshold;
Winson Chung61fa4192011-06-12 15:15:29 -070067
68 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080069 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070070
Sunny Goyalfa401a12015-04-10 13:45:42 -070071 protected ColorStateList mOriginalTextColor;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070072 protected Drawable mDrawable;
Sunny Goyalfa401a12015-04-10 13:45:42 -070073
Sunny Goyal3a644ed2015-05-21 10:28:02 -070074 private AnimatorSet mCurrentColorAnim;
Sunny Goyal316490e2015-06-02 09:38:28 -070075 @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070076
Winson Chung61fa4192011-06-12 15:15:29 -070077 public ButtonDropTarget(Context context, AttributeSet attrs) {
78 this(context, attrs, 0);
79 }
80
81 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
82 super(context, attrs, defStyle);
Sunny Goyal47328fd2016-05-25 18:56:41 -070083 mLauncher = (Launcher) context;
84
Tony Wickham855b1b52016-03-28 16:56:30 -070085 Resources resources = getResources();
86 mBottomDragPadding = resources.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Sunny Goyalf37a2142016-03-24 17:28:25 -070087
88 TypedArray a = context.obtainStyledAttributes(attrs,
89 R.styleable.ButtonDropTarget, defStyle, 0);
90 mHideParentOnDisable = a.getBoolean(R.styleable.ButtonDropTarget_hideParentOnDisable, false);
91 a.recycle();
Tony Wickham855b1b52016-03-28 16:56:30 -070092 mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -070093 }
94
Sunny Goyalfa401a12015-04-10 13:45:42 -070095 @Override
96 protected void onFinishInflate() {
97 super.onFinishInflate();
98 mOriginalTextColor = getTextColors();
Winson Chung61fa4192011-06-12 15:15:29 -070099 }
100
Sunny Goyal70660032015-05-14 00:07:08 -0700101 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyalfa401a12015-04-10 13:45:42 -0700102 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700103 // We do not set the drawable in the xml as that inflates two drawables corresponding to
104 // drawableLeft and drawableStart.
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700105 mDrawable = getResources().getDrawable(resId);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700106
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700107 if (Utilities.ATLEAST_JB_MR1) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700108 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
109 } else {
110 setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null, null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700111 }
112 }
113
Sunny Goyal47328fd2016-05-25 18:56:41 -0700114 public void setDropTargetBar(DropTargetBar dropTargetBar) {
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700115 mDropTargetBar = dropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -0700116 }
117
Sunny Goyalfa401a12015-04-10 13:45:42 -0700118 @Override
Sunny Goyalddec7342015-04-29 18:12:37 -0700119 public void onFlingToDelete(DragObject d, PointF vec) { }
Winson Chung61fa4192011-06-12 15:15:29 -0700120
Sunny Goyalfa401a12015-04-10 13:45:42 -0700121 @Override
122 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800123 d.dragView.setColor(mHoverColor);
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700124 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700125 animateTextColor(mHoverColor);
126 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700127 if (mCurrentFilter == null) {
128 mCurrentFilter = new ColorMatrix();
129 }
130 DragView.setColorScale(mHoverColor, mCurrentFilter);
131 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700132 setTextColor(mHoverColor);
133 }
Sunny Goyald21301e2015-09-25 12:17:08 -0700134 if (d.stateAnnouncer != null) {
135 d.stateAnnouncer.cancel();
136 }
Sunny Goyale78e3d72015-09-24 11:23:31 -0700137 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Winson Chung61fa4192011-06-12 15:15:29 -0700138 }
139
Sunny Goyalfa401a12015-04-10 13:45:42 -0700140 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700141 public void onDragOver(DragObject d) {
142 // Do nothing
143 }
144
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700145 protected void resetHoverColor() {
146 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700147 animateTextColor(mOriginalTextColor.getDefaultColor());
148 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700149 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700150 setTextColor(mOriginalTextColor);
151 }
152 }
153
154 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
155 private void animateTextColor(int targetColor) {
156 if (mCurrentColorAnim != null) {
157 mCurrentColorAnim.cancel();
158 }
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700159
160 mCurrentColorAnim = new AnimatorSet();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700161 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700162
163 if (mSrcFilter == null) {
164 mSrcFilter = new ColorMatrix();
165 mDstFilter = new ColorMatrix();
166 mCurrentFilter = new ColorMatrix();
167 }
168
169 DragView.setColorScale(getTextColor(), mSrcFilter);
170 DragView.setColorScale(targetColor, mDstFilter);
171 ValueAnimator anim1 = ValueAnimator.ofObject(
172 new FloatArrayEvaluator(mCurrentFilter.getArray()),
173 mSrcFilter.getArray(), mDstFilter.getArray());
174 anim1.addUpdateListener(new AnimatorUpdateListener() {
175
176 @Override
177 public void onAnimationUpdate(ValueAnimator animation) {
178 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
179 invalidate();
180 }
181 });
182
183 mCurrentColorAnim.play(anim1);
184 mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700185 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700186 }
187
Sunny Goyalfa401a12015-04-10 13:45:42 -0700188 @Override
189 public final void onDragExit(DragObject d) {
190 if (!d.dragComplete) {
191 d.dragView.setColor(0);
192 resetHoverColor();
193 } else {
194 // Restore the hover color
195 d.dragView.setColor(mHoverColor);
196 }
Winson Chung61fa4192011-06-12 15:15:29 -0700197 }
198
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700199 @Override
Tony Wickham9aae47f2015-10-01 13:04:22 -0700200 public void onDragStart(DragSource source, ItemInfo info, int dragAction) {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700201 mActive = supportsDrop(source, info);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700202 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700203 if (mCurrentColorAnim != null) {
204 mCurrentColorAnim.cancel();
205 mCurrentColorAnim = null;
206 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700207 setTextColor(mOriginalTextColor);
Sunny Goyalf37a2142016-03-24 17:28:25 -0700208 (mHideParentOnDisable ? ((ViewGroup) getParent()) : this)
209 .setVisibility(mActive ? View.VISIBLE : View.GONE);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700210 }
211
212 @Override
213 public final boolean acceptDrop(DragObject dragObject) {
214 return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
215 }
216
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700217 protected abstract boolean supportsDrop(DragSource source, ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700218
219 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700220 public boolean isDropEnabled() {
Tony Wickham855b1b52016-03-28 16:56:30 -0700221 return mActive
222 && mLauncher.getDragController().getDistanceDragged() >= mDragDistanceThreshold;
Winson Chung61fa4192011-06-12 15:15:29 -0700223 }
224
Sunny Goyalfa401a12015-04-10 13:45:42 -0700225 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700226 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700227 mActive = false;
Winson Chung61fa4192011-06-12 15:15:29 -0700228 }
229
Sunny Goyalfa401a12015-04-10 13:45:42 -0700230 /**
231 * On drop animate the dropView to the icon.
232 */
233 @Override
234 public void onDrop(final DragObject d) {
235 final DragLayer dragLayer = mLauncher.getDragLayer();
236 final Rect from = new Rect();
237 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
238
239 int width = mDrawable.getIntrinsicWidth();
240 int height = mDrawable.getIntrinsicHeight();
241 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
242 width, height);
243 final float scale = (float) to.width() / from.width();
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700244 mDropTargetBar.deferOnDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700245
246 Runnable onAnimationEndRunnable = new Runnable() {
247 @Override
248 public void run() {
249 completeDrop(d);
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700250 mDropTargetBar.onDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700251 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
252 }
253 };
254 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
255 DRAG_VIEW_DROP_DURATION, new DecelerateInterpolator(2),
256 new LinearInterpolator(), onAnimationEndRunnable,
257 DragLayer.ANIMATION_END_DISAPPEAR, null);
258 }
259
Sunny Goyale9b651e2015-04-24 11:44:51 -0700260 @Override
261 public void prepareAccessibilityDrop() { }
262
Sunny Goyalfa401a12015-04-10 13:45:42 -0700263 @Thunk abstract void completeDrop(DragObject d);
264
Winson Chung61fa4192011-06-12 15:15:29 -0700265 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700266 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700267 super.getHitRect(outRect);
268 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700269
270 int[] coords = new int[2];
271 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
272 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700273 }
274
Sunny Goyalfa401a12015-04-10 13:45:42 -0700275 protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
Winson Chung61967cb2012-02-28 18:11:33 -0800276 DragLayer dragLayer = mLauncher.getDragLayer();
277
278 // Find the rect to animate to (the view is center aligned)
279 Rect to = new Rect();
280 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800281
282 final int width = drawableWidth;
283 final int height = drawableHeight;
284
285 final int left;
286 final int right;
287
Sunny Goyal70660032015-05-14 00:07:08 -0700288 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800289 right = to.right - getPaddingRight();
290 left = right - width;
291 } else {
292 left = to.left + getPaddingLeft();
293 right = left + width;
294 }
295
296 final int top = to.top + (getMeasuredHeight() - height) / 2;
297 final int bottom = top + height;
298
299 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800300
301 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800302 final int xOffset = (int) -(viewWidth - width) / 2;
303 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800304 to.offset(xOffset, yOffset);
305
306 return to;
307 }
308
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700309 public void enableAccessibleDrag(boolean enable) {
310 setOnClickListener(enable ? this : null);
311 }
312
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700313 @Override
314 public void onClick(View v) {
Sunny Goyalae502842016-06-17 08:43:56 -0700315 mLauncher.getAccessibilityDelegate().handleAccessibleDrop(this, null, null);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700316 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700317
318 public int getTextColor() {
319 return getTextColors().getDefaultColor();
320 }
Winson Chung61fa4192011-06-12 15:15:29 -0700321}