blob: b7f89d02a8edc082c302aff6109dda64aee36ee9 [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 SearchDropTargetBar mSearchDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070055
56 /** Whether this drop target is active for the current drag */
57 protected boolean mActive;
58
59 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080060 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070061
Sunny Goyalfa401a12015-04-10 13:45:42 -070062 protected ColorStateList mOriginalTextColor;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070063 protected Drawable mDrawable;
Sunny Goyalfa401a12015-04-10 13:45:42 -070064
Sunny Goyal3a644ed2015-05-21 10:28:02 -070065 private AnimatorSet mCurrentColorAnim;
Sunny Goyal316490e2015-06-02 09:38:28 -070066 @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070067
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070068
Winson Chung61fa4192011-06-12 15:15:29 -070069 public ButtonDropTarget(Context context, AttributeSet attrs) {
70 this(context, attrs, 0);
71 }
72
73 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
74 super(context, attrs, defStyle);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070075 mBottomDragPadding = getResources().getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Winson Chung61fa4192011-06-12 15:15:29 -070076 }
77
Sunny Goyalfa401a12015-04-10 13:45:42 -070078 @Override
79 protected void onFinishInflate() {
80 super.onFinishInflate();
81 mOriginalTextColor = getTextColors();
82
83 // Remove the text in the Phone UI in landscape
Winson Chung7501adf2015-06-02 11:24:28 -070084 DeviceProfile grid = ((Launcher) getContext()).getDeviceProfile();
85 if (grid.isVerticalBarLayout()) {
86 setText("");
Sunny Goyalfa401a12015-04-10 13:45:42 -070087 }
Winson Chung61fa4192011-06-12 15:15:29 -070088 }
89
Sunny Goyal70660032015-05-14 00:07:08 -070090 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyalfa401a12015-04-10 13:45:42 -070091 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -070092 // We do not set the drawable in the xml as that inflates two drawables corresponding to
93 // drawableLeft and drawableStart.
Sunny Goyal3a644ed2015-05-21 10:28:02 -070094 mDrawable = getResources().getDrawable(resId);
Sunny Goyalfa401a12015-04-10 13:45:42 -070095
Sunny Goyale0cab302015-05-20 16:40:30 -070096 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
97 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
98 } else {
99 setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null, null);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700100 }
101 }
102
103 public void setLauncher(Launcher launcher) {
104 mLauncher = launcher;
Winson Chung61fa4192011-06-12 15:15:29 -0700105 }
106
Adam Cohend4d7aa52011-07-19 21:47:37 -0700107 public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) {
108 mSearchDropTargetBar = searchDropTargetBar;
109 }
110
Sunny Goyalfa401a12015-04-10 13:45:42 -0700111 @Override
Sunny Goyalddec7342015-04-29 18:12:37 -0700112 public void onFlingToDelete(DragObject d, PointF vec) { }
Winson Chung61fa4192011-06-12 15:15:29 -0700113
Sunny Goyalfa401a12015-04-10 13:45:42 -0700114 @Override
115 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800116 d.dragView.setColor(mHoverColor);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700117 if (Utilities.isLmpOrAbove()) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700118 animateTextColor(mHoverColor);
119 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700120 if (mCurrentFilter == null) {
121 mCurrentFilter = new ColorMatrix();
122 }
123 DragView.setColorScale(mHoverColor, mCurrentFilter);
124 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700125 setTextColor(mHoverColor);
126 }
Winson Chung61fa4192011-06-12 15:15:29 -0700127 }
128
Sunny Goyalfa401a12015-04-10 13:45:42 -0700129 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700130 public void onDragOver(DragObject d) {
131 // Do nothing
132 }
133
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700134 protected void resetHoverColor() {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700135 if (Utilities.isLmpOrAbove()) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700136 animateTextColor(mOriginalTextColor.getDefaultColor());
137 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700138 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700139 setTextColor(mOriginalTextColor);
140 }
141 }
142
143 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
144 private void animateTextColor(int targetColor) {
145 if (mCurrentColorAnim != null) {
146 mCurrentColorAnim.cancel();
147 }
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700148
149 mCurrentColorAnim = new AnimatorSet();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700150 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700151
152 if (mSrcFilter == null) {
153 mSrcFilter = new ColorMatrix();
154 mDstFilter = new ColorMatrix();
155 mCurrentFilter = new ColorMatrix();
156 }
157
158 DragView.setColorScale(getTextColor(), mSrcFilter);
159 DragView.setColorScale(targetColor, mDstFilter);
160 ValueAnimator anim1 = ValueAnimator.ofObject(
161 new FloatArrayEvaluator(mCurrentFilter.getArray()),
162 mSrcFilter.getArray(), mDstFilter.getArray());
163 anim1.addUpdateListener(new AnimatorUpdateListener() {
164
165 @Override
166 public void onAnimationUpdate(ValueAnimator animation) {
167 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
168 invalidate();
169 }
170 });
171
172 mCurrentColorAnim.play(anim1);
173 mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700174 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700175 }
176
Sunny Goyalfa401a12015-04-10 13:45:42 -0700177 @Override
178 public final void onDragExit(DragObject d) {
179 if (!d.dragComplete) {
180 d.dragView.setColor(0);
181 resetHoverColor();
182 } else {
183 // Restore the hover color
184 d.dragView.setColor(mHoverColor);
185 }
Winson Chung61fa4192011-06-12 15:15:29 -0700186 }
187
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700188 @Override
Sunny Goyalfa401a12015-04-10 13:45:42 -0700189 public final void onDragStart(DragSource source, Object info, int dragAction) {
190 mActive = supportsDrop(source, info);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700191 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700192 if (mCurrentColorAnim != null) {
193 mCurrentColorAnim.cancel();
194 mCurrentColorAnim = null;
195 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700196 setTextColor(mOriginalTextColor);
197 ((ViewGroup) getParent()).setVisibility(mActive ? View.VISIBLE : View.GONE);
198 }
199
200 @Override
201 public final boolean acceptDrop(DragObject dragObject) {
202 return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
203 }
204
205 protected abstract boolean supportsDrop(DragSource source, Object info);
206
207 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700208 public boolean isDropEnabled() {
209 return mActive;
210 }
211
Sunny Goyalfa401a12015-04-10 13:45:42 -0700212 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700213 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700214 mActive = false;
Winson Chung61fa4192011-06-12 15:15:29 -0700215 }
216
Sunny Goyalfa401a12015-04-10 13:45:42 -0700217 /**
218 * On drop animate the dropView to the icon.
219 */
220 @Override
221 public void onDrop(final DragObject d) {
222 final DragLayer dragLayer = mLauncher.getDragLayer();
223 final Rect from = new Rect();
224 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
225
226 int width = mDrawable.getIntrinsicWidth();
227 int height = mDrawable.getIntrinsicHeight();
228 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
229 width, height);
230 final float scale = (float) to.width() / from.width();
231 mSearchDropTargetBar.deferOnDragEnd();
232
233 Runnable onAnimationEndRunnable = new Runnable() {
234 @Override
235 public void run() {
236 completeDrop(d);
237 mSearchDropTargetBar.onDragEnd();
238 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
239 }
240 };
241 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
242 DRAG_VIEW_DROP_DURATION, new DecelerateInterpolator(2),
243 new LinearInterpolator(), onAnimationEndRunnable,
244 DragLayer.ANIMATION_END_DISAPPEAR, null);
245 }
246
Sunny Goyale9b651e2015-04-24 11:44:51 -0700247 @Override
248 public void prepareAccessibilityDrop() { }
249
Sunny Goyalfa401a12015-04-10 13:45:42 -0700250 @Thunk abstract void completeDrop(DragObject d);
251
Winson Chung61fa4192011-06-12 15:15:29 -0700252 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700253 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700254 super.getHitRect(outRect);
255 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700256
257 int[] coords = new int[2];
258 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
259 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700260 }
261
Sunny Goyalfa401a12015-04-10 13:45:42 -0700262 protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
Winson Chung61967cb2012-02-28 18:11:33 -0800263 DragLayer dragLayer = mLauncher.getDragLayer();
264
265 // Find the rect to animate to (the view is center aligned)
266 Rect to = new Rect();
267 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800268
269 final int width = drawableWidth;
270 final int height = drawableHeight;
271
272 final int left;
273 final int right;
274
Sunny Goyal70660032015-05-14 00:07:08 -0700275 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800276 right = to.right - getPaddingRight();
277 left = right - width;
278 } else {
279 left = to.left + getPaddingLeft();
280 right = left + width;
281 }
282
283 final int top = to.top + (getMeasuredHeight() - height) / 2;
284 final int bottom = top + height;
285
286 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800287
288 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800289 final int xOffset = (int) -(viewWidth - width) / 2;
290 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800291 to.offset(xOffset, yOffset);
292
293 return to;
294 }
295
Sunny Goyalfa401a12015-04-10 13:45:42 -0700296 @Override
Adam Cohen8dfcba42011-07-07 16:38:18 -0700297 public void getLocationInDragLayer(int[] loc) {
298 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
299 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700300
301 public void enableAccessibleDrag(boolean enable) {
302 setOnClickListener(enable ? this : null);
303 }
304
305 protected String getAccessibilityDropConfirmation() {
306 return null;
307 }
308
309 @Override
310 public void onClick(View v) {
311 LauncherAppState.getInstance().getAccessibilityDelegate()
312 .handleAccessibleDrop(this, null, getAccessibilityDropConfirmation());
313 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700314
315 public int getTextColor() {
316 return getTextColors().getDefaultColor();
317 }
Winson Chung61fa4192011-06-12 15:15:29 -0700318}