blob: 09a71b0cc7ef865c04aed5be576b386431871e19 [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;
27import android.content.res.Configuration;
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 Goyalfe0d1f22015-04-28 22:01:31 -070033import android.os.Build;
Winson Chung61fa4192011-06-12 15:15:29 -070034import android.util.AttributeSet;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080035import android.view.View;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070036import android.view.View.OnClickListener;
Sunny Goyalfa401a12015-04-10 13:45:42 -070037import android.view.ViewGroup;
38import 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
Sunny Goyalfa401a12015-04-10 13:45:42 -070042import com.android.launcher3.util.Thunk;
Winson Chung61fa4192011-06-12 15:15:29 -070043
44/**
45 * Implements a DropTarget.
46 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070047public abstract class ButtonDropTarget extends TextView
48 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070049
50 private static int DRAG_VIEW_DROP_DURATION = 285;
Winson Chung61fa4192011-06-12 15:15:29 -070051
Winson Chung61fa4192011-06-12 15:15:29 -070052 protected Launcher mLauncher;
Winson Chunga62e9fd2011-07-11 15:20:48 -070053 private int mBottomDragPadding;
Adam Cohend4d7aa52011-07-19 21:47:37 -070054 protected TextView mText;
55 protected SearchDropTargetBar mSearchDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070056
57 /** Whether this drop target is active for the current drag */
58 protected boolean mActive;
59
60 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080061 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070062
Sunny Goyalfa401a12015-04-10 13:45:42 -070063 protected ColorStateList mOriginalTextColor;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070064 protected Drawable mDrawable;
Sunny Goyalfa401a12015-04-10 13:45:42 -070065
Sunny Goyal3a644ed2015-05-21 10:28:02 -070066 private AnimatorSet mCurrentColorAnim;
Sunny Goyal316490e2015-06-02 09:38:28 -070067 @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070068
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070069
Winson Chung61fa4192011-06-12 15:15:29 -070070 public ButtonDropTarget(Context context, AttributeSet attrs) {
71 this(context, attrs, 0);
72 }
73
74 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
75 super(context, attrs, defStyle);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070076 mBottomDragPadding = getResources().getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Winson Chung61fa4192011-06-12 15:15:29 -070077 }
78
Sunny Goyalfa401a12015-04-10 13:45:42 -070079 @Override
80 protected void onFinishInflate() {
81 super.onFinishInflate();
82 mOriginalTextColor = getTextColors();
83
84 // Remove the text in the Phone UI in landscape
85 int orientation = getResources().getConfiguration().orientation;
86 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
87 if (!LauncherAppState.getInstance().isScreenLarge()) {
88 setText("");
89 }
90 }
Winson Chung61fa4192011-06-12 15:15:29 -070091 }
92
Sunny Goyal70660032015-05-14 00:07:08 -070093 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyalfa401a12015-04-10 13:45:42 -070094 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -070095 // We do not set the drawable in the xml as that inflates two drawables corresponding to
96 // drawableLeft and drawableStart.
Sunny Goyal3a644ed2015-05-21 10:28:02 -070097 mDrawable = getResources().getDrawable(resId);
Sunny Goyalfa401a12015-04-10 13:45:42 -070098
Sunny Goyale0cab302015-05-20 16:40:30 -070099 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
100 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
101 } else {
102 setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null, null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700103 }
104 }
105
106 public void setLauncher(Launcher launcher) {
107 mLauncher = launcher;
Winson Chung61fa4192011-06-12 15:15:29 -0700108 }
109
Adam Cohend4d7aa52011-07-19 21:47:37 -0700110 public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) {
111 mSearchDropTargetBar = searchDropTargetBar;
112 }
113
Sunny Goyalfa401a12015-04-10 13:45:42 -0700114 @Override
Sunny Goyalddec7342015-04-29 18:12:37 -0700115 public void onFlingToDelete(DragObject d, PointF vec) { }
Winson Chung61fa4192011-06-12 15:15:29 -0700116
Sunny Goyalfa401a12015-04-10 13:45:42 -0700117 @Override
118 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800119 d.dragView.setColor(mHoverColor);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700120 if (Utilities.isLmpOrAbove()) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700121 animateTextColor(mHoverColor);
122 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700123 if (mCurrentFilter == null) {
124 mCurrentFilter = new ColorMatrix();
125 }
126 DragView.setColorScale(mHoverColor, mCurrentFilter);
127 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700128 setTextColor(mHoverColor);
129 }
Winson Chung61fa4192011-06-12 15:15:29 -0700130 }
131
Sunny Goyalfa401a12015-04-10 13:45:42 -0700132 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700133 public void onDragOver(DragObject d) {
134 // Do nothing
135 }
136
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700137 protected void resetHoverColor() {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700138 if (Utilities.isLmpOrAbove()) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700139 animateTextColor(mOriginalTextColor.getDefaultColor());
140 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700141 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700142 setTextColor(mOriginalTextColor);
143 }
144 }
145
146 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
147 private void animateTextColor(int targetColor) {
148 if (mCurrentColorAnim != null) {
149 mCurrentColorAnim.cancel();
150 }
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700151
152 mCurrentColorAnim = new AnimatorSet();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700153 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700154
155 if (mSrcFilter == null) {
156 mSrcFilter = new ColorMatrix();
157 mDstFilter = new ColorMatrix();
158 mCurrentFilter = new ColorMatrix();
159 }
160
161 DragView.setColorScale(getTextColor(), mSrcFilter);
162 DragView.setColorScale(targetColor, mDstFilter);
163 ValueAnimator anim1 = ValueAnimator.ofObject(
164 new FloatArrayEvaluator(mCurrentFilter.getArray()),
165 mSrcFilter.getArray(), mDstFilter.getArray());
166 anim1.addUpdateListener(new AnimatorUpdateListener() {
167
168 @Override
169 public void onAnimationUpdate(ValueAnimator animation) {
170 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
171 invalidate();
172 }
173 });
174
175 mCurrentColorAnim.play(anim1);
176 mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700177 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700178 }
179
Sunny Goyalfa401a12015-04-10 13:45:42 -0700180 @Override
181 public final void onDragExit(DragObject d) {
182 if (!d.dragComplete) {
183 d.dragView.setColor(0);
184 resetHoverColor();
185 } else {
186 // Restore the hover color
187 d.dragView.setColor(mHoverColor);
188 }
Winson Chung61fa4192011-06-12 15:15:29 -0700189 }
190
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700191 @Override
Sunny Goyalfa401a12015-04-10 13:45:42 -0700192 public final void onDragStart(DragSource source, Object info, int dragAction) {
193 mActive = supportsDrop(source, info);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700194 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700195 if (mCurrentColorAnim != null) {
196 mCurrentColorAnim.cancel();
197 mCurrentColorAnim = null;
198 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700199 setTextColor(mOriginalTextColor);
200 ((ViewGroup) getParent()).setVisibility(mActive ? View.VISIBLE : View.GONE);
201 }
202
203 @Override
204 public final boolean acceptDrop(DragObject dragObject) {
205 return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
206 }
207
208 protected abstract boolean supportsDrop(DragSource source, Object info);
209
210 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700211 public boolean isDropEnabled() {
212 return mActive;
213 }
214
Sunny Goyalfa401a12015-04-10 13:45:42 -0700215 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700216 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700217 mActive = false;
Winson Chung61fa4192011-06-12 15:15:29 -0700218 }
219
Sunny Goyalfa401a12015-04-10 13:45:42 -0700220 /**
221 * On drop animate the dropView to the icon.
222 */
223 @Override
224 public void onDrop(final DragObject d) {
225 final DragLayer dragLayer = mLauncher.getDragLayer();
226 final Rect from = new Rect();
227 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
228
229 int width = mDrawable.getIntrinsicWidth();
230 int height = mDrawable.getIntrinsicHeight();
231 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
232 width, height);
233 final float scale = (float) to.width() / from.width();
234 mSearchDropTargetBar.deferOnDragEnd();
235
236 Runnable onAnimationEndRunnable = new Runnable() {
237 @Override
238 public void run() {
239 completeDrop(d);
240 mSearchDropTargetBar.onDragEnd();
241 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
242 }
243 };
244 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
245 DRAG_VIEW_DROP_DURATION, new DecelerateInterpolator(2),
246 new LinearInterpolator(), onAnimationEndRunnable,
247 DragLayer.ANIMATION_END_DISAPPEAR, null);
248 }
249
Sunny Goyale9b651e2015-04-24 11:44:51 -0700250 @Override
251 public void prepareAccessibilityDrop() { }
252
Sunny Goyalfa401a12015-04-10 13:45:42 -0700253 @Thunk abstract void completeDrop(DragObject d);
254
Winson Chung61fa4192011-06-12 15:15:29 -0700255 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700256 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700257 super.getHitRect(outRect);
258 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700259
260 int[] coords = new int[2];
261 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
262 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700263 }
264
Sunny Goyalfa401a12015-04-10 13:45:42 -0700265 protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
Winson Chung61967cb2012-02-28 18:11:33 -0800266 DragLayer dragLayer = mLauncher.getDragLayer();
267
268 // Find the rect to animate to (the view is center aligned)
269 Rect to = new Rect();
270 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800271
272 final int width = drawableWidth;
273 final int height = drawableHeight;
274
275 final int left;
276 final int right;
277
Sunny Goyal70660032015-05-14 00:07:08 -0700278 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800279 right = to.right - getPaddingRight();
280 left = right - width;
281 } else {
282 left = to.left + getPaddingLeft();
283 right = left + width;
284 }
285
286 final int top = to.top + (getMeasuredHeight() - height) / 2;
287 final int bottom = top + height;
288
289 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800290
291 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800292 final int xOffset = (int) -(viewWidth - width) / 2;
293 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800294 to.offset(xOffset, yOffset);
295
296 return to;
297 }
298
Sunny Goyalfa401a12015-04-10 13:45:42 -0700299 @Override
Adam Cohen8dfcba42011-07-07 16:38:18 -0700300 public void getLocationInDragLayer(int[] loc) {
301 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
302 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700303
304 public void enableAccessibleDrag(boolean enable) {
305 setOnClickListener(enable ? this : null);
306 }
307
308 protected String getAccessibilityDropConfirmation() {
309 return null;
310 }
311
312 @Override
313 public void onClick(View v) {
314 LauncherAppState.getInstance().getAccessibilityDelegate()
315 .handleAccessibleDrop(this, null, getAccessibilityDropConfirmation());
316 }
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}