blob: af1ebde8f7ba575997732c308d6d696744a7f484 [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;
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 Chung043f2af2012-03-01 16:09:54 -080030import android.graphics.PointF;
Winson Chung61967cb2012-02-28 18:11:33 -080031import android.graphics.Rect;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070032import android.graphics.drawable.Drawable;
Sunny Goyalf37a2142016-03-24 17:28:25 -070033import android.graphics.drawable.InsetDrawable;
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;
65
66 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080067 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070068
Sunny Goyalfa401a12015-04-10 13:45:42 -070069 protected ColorStateList mOriginalTextColor;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070070 protected Drawable mDrawable;
Sunny Goyalfa401a12015-04-10 13:45:42 -070071
Sunny Goyal3a644ed2015-05-21 10:28:02 -070072 private AnimatorSet mCurrentColorAnim;
Sunny Goyal316490e2015-06-02 09:38:28 -070073 @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070074
Winson Chung61fa4192011-06-12 15:15:29 -070075 public ButtonDropTarget(Context context, AttributeSet attrs) {
76 this(context, attrs, 0);
77 }
78
79 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
80 super(context, attrs, defStyle);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070081 mBottomDragPadding = getResources().getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Sunny Goyalf37a2142016-03-24 17:28:25 -070082
83 TypedArray a = context.obtainStyledAttributes(attrs,
84 R.styleable.ButtonDropTarget, defStyle, 0);
85 mHideParentOnDisable = a.getBoolean(R.styleable.ButtonDropTarget_hideParentOnDisable, false);
86 a.recycle();
Winson Chung61fa4192011-06-12 15:15:29 -070087 }
88
Sunny Goyalfa401a12015-04-10 13:45:42 -070089 @Override
90 protected void onFinishInflate() {
91 super.onFinishInflate();
92 mOriginalTextColor = getTextColors();
Winson Chung61fa4192011-06-12 15:15:29 -070093 }
94
Sunny Goyal70660032015-05-14 00:07:08 -070095 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyalfa401a12015-04-10 13:45:42 -070096 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -070097 // We do not set the drawable in the xml as that inflates two drawables corresponding to
98 // drawableLeft and drawableStart.
Sunny Goyal3a644ed2015-05-21 10:28:02 -070099 mDrawable = getResources().getDrawable(resId);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700100
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700101 if (Utilities.ATLEAST_JB_MR1) {
Sunny Goyale0cab302015-05-20 16:40:30 -0700102 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
103 } else {
104 setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null, null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700105 }
106 }
107
108 public void setLauncher(Launcher launcher) {
109 mLauncher = launcher;
Winson Chung61fa4192011-06-12 15:15:29 -0700110 }
111
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700112 public void setDropTargetBar(BaseDropTargetBar dropTargetBar) {
113 mDropTargetBar = dropTargetBar;
Adam Cohend4d7aa52011-07-19 21:47:37 -0700114 }
115
Sunny Goyalfa401a12015-04-10 13:45:42 -0700116 @Override
Sunny Goyalddec7342015-04-29 18:12:37 -0700117 public void onFlingToDelete(DragObject d, PointF vec) { }
Winson Chung61fa4192011-06-12 15:15:29 -0700118
Sunny Goyalfa401a12015-04-10 13:45:42 -0700119 @Override
120 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800121 d.dragView.setColor(mHoverColor);
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700122 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700123 animateTextColor(mHoverColor);
124 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700125 if (mCurrentFilter == null) {
126 mCurrentFilter = new ColorMatrix();
127 }
128 DragView.setColorScale(mHoverColor, mCurrentFilter);
129 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700130 setTextColor(mHoverColor);
131 }
Sunny Goyald21301e2015-09-25 12:17:08 -0700132 if (d.stateAnnouncer != null) {
133 d.stateAnnouncer.cancel();
134 }
Sunny Goyale78e3d72015-09-24 11:23:31 -0700135 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Winson Chung61fa4192011-06-12 15:15:29 -0700136 }
137
Sunny Goyalfa401a12015-04-10 13:45:42 -0700138 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700139 public void onDragOver(DragObject d) {
140 // Do nothing
141 }
142
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700143 protected void resetHoverColor() {
144 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700145 animateTextColor(mOriginalTextColor.getDefaultColor());
146 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700147 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700148 setTextColor(mOriginalTextColor);
149 }
150 }
151
152 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
153 private void animateTextColor(int targetColor) {
154 if (mCurrentColorAnim != null) {
155 mCurrentColorAnim.cancel();
156 }
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700157
158 mCurrentColorAnim = new AnimatorSet();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700159 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700160
161 if (mSrcFilter == null) {
162 mSrcFilter = new ColorMatrix();
163 mDstFilter = new ColorMatrix();
164 mCurrentFilter = new ColorMatrix();
165 }
166
167 DragView.setColorScale(getTextColor(), mSrcFilter);
168 DragView.setColorScale(targetColor, mDstFilter);
169 ValueAnimator anim1 = ValueAnimator.ofObject(
170 new FloatArrayEvaluator(mCurrentFilter.getArray()),
171 mSrcFilter.getArray(), mDstFilter.getArray());
172 anim1.addUpdateListener(new AnimatorUpdateListener() {
173
174 @Override
175 public void onAnimationUpdate(ValueAnimator animation) {
176 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
177 invalidate();
178 }
179 });
180
181 mCurrentColorAnim.play(anim1);
182 mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700183 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700184 }
185
Sunny Goyalfa401a12015-04-10 13:45:42 -0700186 @Override
187 public final void onDragExit(DragObject d) {
188 if (!d.dragComplete) {
189 d.dragView.setColor(0);
190 resetHoverColor();
191 } else {
192 // Restore the hover color
193 d.dragView.setColor(mHoverColor);
194 }
Winson Chung61fa4192011-06-12 15:15:29 -0700195 }
196
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700197 @Override
Tony Wickham9aae47f2015-10-01 13:04:22 -0700198 public void onDragStart(DragSource source, ItemInfo info, int dragAction) {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700199 mActive = supportsDrop(source, info);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700200 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700201 if (mCurrentColorAnim != null) {
202 mCurrentColorAnim.cancel();
203 mCurrentColorAnim = null;
204 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700205 setTextColor(mOriginalTextColor);
Sunny Goyalf37a2142016-03-24 17:28:25 -0700206 (mHideParentOnDisable ? ((ViewGroup) getParent()) : this)
207 .setVisibility(mActive ? View.VISIBLE : View.GONE);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700208 }
209
210 @Override
211 public final boolean acceptDrop(DragObject dragObject) {
212 return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
213 }
214
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700215 protected abstract boolean supportsDrop(DragSource source, ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700216
217 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700218 public boolean isDropEnabled() {
219 return mActive;
220 }
221
Sunny Goyalfa401a12015-04-10 13:45:42 -0700222 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700223 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700224 mActive = false;
Winson Chung61fa4192011-06-12 15:15:29 -0700225 }
226
Sunny Goyalfa401a12015-04-10 13:45:42 -0700227 /**
228 * On drop animate the dropView to the icon.
229 */
230 @Override
231 public void onDrop(final DragObject d) {
232 final DragLayer dragLayer = mLauncher.getDragLayer();
233 final Rect from = new Rect();
234 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
235
236 int width = mDrawable.getIntrinsicWidth();
237 int height = mDrawable.getIntrinsicHeight();
238 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
239 width, height);
240 final float scale = (float) to.width() / from.width();
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700241 mDropTargetBar.deferOnDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700242
243 Runnable onAnimationEndRunnable = new Runnable() {
244 @Override
245 public void run() {
246 completeDrop(d);
Tony Wickhamb54c4a32015-09-11 08:40:20 -0700247 mDropTargetBar.onDragEnd();
Sunny Goyalfa401a12015-04-10 13:45:42 -0700248 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
249 }
250 };
251 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
252 DRAG_VIEW_DROP_DURATION, new DecelerateInterpolator(2),
253 new LinearInterpolator(), onAnimationEndRunnable,
254 DragLayer.ANIMATION_END_DISAPPEAR, null);
255 }
256
Sunny Goyale9b651e2015-04-24 11:44:51 -0700257 @Override
258 public void prepareAccessibilityDrop() { }
259
Sunny Goyalfa401a12015-04-10 13:45:42 -0700260 @Thunk abstract void completeDrop(DragObject d);
261
Winson Chung61fa4192011-06-12 15:15:29 -0700262 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700263 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700264 super.getHitRect(outRect);
265 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700266
267 int[] coords = new int[2];
268 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
269 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700270 }
271
Sunny Goyalfa401a12015-04-10 13:45:42 -0700272 protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
Winson Chung61967cb2012-02-28 18:11:33 -0800273 DragLayer dragLayer = mLauncher.getDragLayer();
274
275 // Find the rect to animate to (the view is center aligned)
276 Rect to = new Rect();
277 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800278
279 final int width = drawableWidth;
280 final int height = drawableHeight;
281
282 final int left;
283 final int right;
284
Sunny Goyal70660032015-05-14 00:07:08 -0700285 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800286 right = to.right - getPaddingRight();
287 left = right - width;
288 } else {
289 left = to.left + getPaddingLeft();
290 right = left + width;
291 }
292
293 final int top = to.top + (getMeasuredHeight() - height) / 2;
294 final int bottom = top + height;
295
296 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800297
298 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800299 final int xOffset = (int) -(viewWidth - width) / 2;
300 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800301 to.offset(xOffset, yOffset);
302
303 return to;
304 }
305
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700306 public void enableAccessibleDrag(boolean enable) {
307 setOnClickListener(enable ? this : null);
308 }
309
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700310 @Override
311 public void onClick(View v) {
312 LauncherAppState.getInstance().getAccessibilityDelegate()
Sunny Goyal7aaa8412015-09-25 12:06:13 -0700313 .handleAccessibleDrop(this, null, null);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700314 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700315
316 public int getTextColor() {
317 return getTextColors().getDefaultColor();
318 }
Winson Chung61fa4192011-06-12 15:15:29 -0700319}