blob: 43afbe59e25e6d1cb519c867569607c7886a1892 [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;
58
Winson Chung61fa4192011-06-12 15:15:29 -070059 protected Launcher mLauncher;
Winson Chunga62e9fd2011-07-11 15:20:48 -070060 private int mBottomDragPadding;
Tony Wickhamb54c4a32015-09-11 08:40:20 -070061 protected BaseDropTargetBar 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);
Tony Wickham855b1b52016-03-28 16:56:30 -070083 Resources resources = getResources();
84 mBottomDragPadding = resources.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Sunny Goyalf37a2142016-03-24 17:28:25 -070085
86 TypedArray a = context.obtainStyledAttributes(attrs,
87 R.styleable.ButtonDropTarget, defStyle, 0);
88 mHideParentOnDisable = a.getBoolean(R.styleable.ButtonDropTarget_hideParentOnDisable, false);
89 a.recycle();
Tony Wickham855b1b52016-03-28 16:56:30 -070090 mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold);
Winson Chung61fa4192011-06-12 15:15:29 -070091 }
92
Sunny Goyalfa401a12015-04-10 13:45:42 -070093 @Override
94 protected void onFinishInflate() {
95 super.onFinishInflate();
96 mOriginalTextColor = getTextColors();
Winson Chung61fa4192011-06-12 15:15:29 -070097 }
98
Sunny Goyal70660032015-05-14 00:07:08 -070099 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyalfa401a12015-04-10 13:45:42 -0700100 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700101 // We do not set the drawable in the xml as that inflates two drawables corresponding to
102 // drawableLeft and drawableStart.
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700103 mDrawable = getResources().getDrawable(resId);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700104
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700105 if (Utilities.ATLEAST_JB_MR1) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700106 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
107 } else {
108 setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null, null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700109 }
110 }
111
112 public void setLauncher(Launcher launcher) {
113 mLauncher = launcher;
Winson Chung61fa4192011-06-12 15:15:29 -0700114 }
115
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700116 public void setDropTargetBar(BaseDropTargetBar dropTargetBar) {
117 mDropTargetBar = dropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -0700118 }
119
Sunny Goyalfa401a12015-04-10 13:45:42 -0700120 @Override
Sunny Goyalddec7342015-04-29 18:12:37 -0700121 public void onFlingToDelete(DragObject d, PointF vec) { }
Winson Chung61fa4192011-06-12 15:15:29 -0700122
Sunny Goyalfa401a12015-04-10 13:45:42 -0700123 @Override
124 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800125 d.dragView.setColor(mHoverColor);
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700126 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700127 animateTextColor(mHoverColor);
128 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700129 if (mCurrentFilter == null) {
130 mCurrentFilter = new ColorMatrix();
131 }
132 DragView.setColorScale(mHoverColor, mCurrentFilter);
133 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700134 setTextColor(mHoverColor);
135 }
Sunny Goyald21301e2015-09-25 12:17:08 -0700136 if (d.stateAnnouncer != null) {
137 d.stateAnnouncer.cancel();
138 }
Sunny Goyale78e3d72015-09-24 11:23:31 -0700139 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Winson Chung61fa4192011-06-12 15:15:29 -0700140 }
141
Sunny Goyalfa401a12015-04-10 13:45:42 -0700142 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700143 public void onDragOver(DragObject d) {
144 // Do nothing
145 }
146
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700147 protected void resetHoverColor() {
148 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700149 animateTextColor(mOriginalTextColor.getDefaultColor());
150 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700151 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700152 setTextColor(mOriginalTextColor);
153 }
154 }
155
156 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
157 private void animateTextColor(int targetColor) {
158 if (mCurrentColorAnim != null) {
159 mCurrentColorAnim.cancel();
160 }
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700161
162 mCurrentColorAnim = new AnimatorSet();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700163 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700164
165 if (mSrcFilter == null) {
166 mSrcFilter = new ColorMatrix();
167 mDstFilter = new ColorMatrix();
168 mCurrentFilter = new ColorMatrix();
169 }
170
171 DragView.setColorScale(getTextColor(), mSrcFilter);
172 DragView.setColorScale(targetColor, mDstFilter);
173 ValueAnimator anim1 = ValueAnimator.ofObject(
174 new FloatArrayEvaluator(mCurrentFilter.getArray()),
175 mSrcFilter.getArray(), mDstFilter.getArray());
176 anim1.addUpdateListener(new AnimatorUpdateListener() {
177
178 @Override
179 public void onAnimationUpdate(ValueAnimator animation) {
180 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
181 invalidate();
182 }
183 });
184
185 mCurrentColorAnim.play(anim1);
186 mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700187 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700188 }
189
Sunny Goyalfa401a12015-04-10 13:45:42 -0700190 @Override
191 public final void onDragExit(DragObject d) {
192 if (!d.dragComplete) {
193 d.dragView.setColor(0);
194 resetHoverColor();
195 } else {
196 // Restore the hover color
197 d.dragView.setColor(mHoverColor);
198 }
Winson Chung61fa4192011-06-12 15:15:29 -0700199 }
200
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700201 @Override
Tony Wickham9aae47f2015-10-01 13:04:22 -0700202 public void onDragStart(DragSource source, ItemInfo info, int dragAction) {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700203 mActive = supportsDrop(source, info);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700204 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700205 if (mCurrentColorAnim != null) {
206 mCurrentColorAnim.cancel();
207 mCurrentColorAnim = null;
208 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700209 setTextColor(mOriginalTextColor);
Sunny Goyalf37a2142016-03-24 17:28:25 -0700210 (mHideParentOnDisable ? ((ViewGroup) getParent()) : this)
211 .setVisibility(mActive ? View.VISIBLE : View.GONE);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700212 }
213
214 @Override
215 public final boolean acceptDrop(DragObject dragObject) {
216 return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
217 }
218
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700219 protected abstract boolean supportsDrop(DragSource source, ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700220
221 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700222 public boolean isDropEnabled() {
Tony Wickham855b1b52016-03-28 16:56:30 -0700223 return mActive
224 && mLauncher.getDragController().getDistanceDragged() >= mDragDistanceThreshold;
Winson Chung61fa4192011-06-12 15:15:29 -0700225 }
226
Sunny Goyalfa401a12015-04-10 13:45:42 -0700227 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700228 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700229 mActive = false;
Winson Chung61fa4192011-06-12 15:15:29 -0700230 }
231
Sunny Goyalfa401a12015-04-10 13:45:42 -0700232 /**
233 * On drop animate the dropView to the icon.
234 */
235 @Override
236 public void onDrop(final DragObject d) {
237 final DragLayer dragLayer = mLauncher.getDragLayer();
238 final Rect from = new Rect();
239 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
240
241 int width = mDrawable.getIntrinsicWidth();
242 int height = mDrawable.getIntrinsicHeight();
243 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
244 width, height);
245 final float scale = (float) to.width() / from.width();
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700246 mDropTargetBar.deferOnDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700247
248 Runnable onAnimationEndRunnable = new Runnable() {
249 @Override
250 public void run() {
251 completeDrop(d);
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700252 mDropTargetBar.onDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700253 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
254 }
255 };
256 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
257 DRAG_VIEW_DROP_DURATION, new DecelerateInterpolator(2),
258 new LinearInterpolator(), onAnimationEndRunnable,
259 DragLayer.ANIMATION_END_DISAPPEAR, null);
260 }
261
Sunny Goyale9b651e2015-04-24 11:44:51 -0700262 @Override
263 public void prepareAccessibilityDrop() { }
264
Sunny Goyalfa401a12015-04-10 13:45:42 -0700265 @Thunk abstract void completeDrop(DragObject d);
266
Winson Chung61fa4192011-06-12 15:15:29 -0700267 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700268 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700269 super.getHitRect(outRect);
270 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700271
272 int[] coords = new int[2];
273 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
274 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700275 }
276
Sunny Goyalfa401a12015-04-10 13:45:42 -0700277 protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
Winson Chung61967cb2012-02-28 18:11:33 -0800278 DragLayer dragLayer = mLauncher.getDragLayer();
279
280 // Find the rect to animate to (the view is center aligned)
281 Rect to = new Rect();
282 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800283
284 final int width = drawableWidth;
285 final int height = drawableHeight;
286
287 final int left;
288 final int right;
289
Sunny Goyal70660032015-05-14 00:07:08 -0700290 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800291 right = to.right - getPaddingRight();
292 left = right - width;
293 } else {
294 left = to.left + getPaddingLeft();
295 right = left + width;
296 }
297
298 final int top = to.top + (getMeasuredHeight() - height) / 2;
299 final int bottom = top + height;
300
301 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800302
303 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800304 final int xOffset = (int) -(viewWidth - width) / 2;
305 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800306 to.offset(xOffset, yOffset);
307
308 return to;
309 }
310
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700311 public void enableAccessibleDrag(boolean enable) {
312 setOnClickListener(enable ? this : null);
313 }
314
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700315 @Override
316 public void onClick(View v) {
317 LauncherAppState.getInstance().getAccessibilityDelegate()
Sunny Goyal7aaa8412015-09-25 12:06:13 -0700318 .handleAccessibleDrop(this, null, null);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700319 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700320
321 public int getTextColor() {
322 return getTextColors().getDefaultColor();
323 }
Winson Chung61fa4192011-06-12 15:15:29 -0700324}