blob: 574f229eb8ce974993babc49a2a94733a9333ed9 [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 Goyal3a644ed2015-05-21 10:28:02 -070027import android.graphics.ColorMatrix;
28import android.graphics.ColorMatrixColorFilter;
Winson Chung043f2af2012-03-01 16:09:54 -080029import android.graphics.PointF;
Winson Chung61967cb2012-02-28 18:11:33 -080030import android.graphics.Rect;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070031import android.graphics.drawable.Drawable;
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070032import android.os.Build;
Winson Chung61fa4192011-06-12 15:15:29 -070033import android.util.AttributeSet;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080034import android.view.View;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070035import android.view.View.OnClickListener;
Sunny Goyalfa401a12015-04-10 13:45:42 -070036import android.view.ViewGroup;
37import android.view.animation.DecelerateInterpolator;
38import android.view.animation.LinearInterpolator;
Adam Cohend4d7aa52011-07-19 21:47:37 -070039import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070040
Sunny Goyalfa401a12015-04-10 13:45:42 -070041import com.android.launcher3.util.Thunk;
Winson Chung61fa4192011-06-12 15:15:29 -070042
43/**
44 * Implements a DropTarget.
45 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070046public abstract class ButtonDropTarget extends TextView
47 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070048
49 private static int DRAG_VIEW_DROP_DURATION = 285;
Winson Chung61fa4192011-06-12 15:15:29 -070050
Winson Chung61fa4192011-06-12 15:15:29 -070051 protected Launcher mLauncher;
Winson Chunga62e9fd2011-07-11 15:20:48 -070052 private int mBottomDragPadding;
Adam Cohend4d7aa52011-07-19 21:47:37 -070053 protected SearchDropTargetBar mSearchDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070054
55 /** Whether this drop target is active for the current drag */
56 protected boolean mActive;
57
58 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080059 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070060
Sunny Goyalfa401a12015-04-10 13:45:42 -070061 protected ColorStateList mOriginalTextColor;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070062 protected Drawable mDrawable;
Sunny Goyalfa401a12015-04-10 13:45:42 -070063
Sunny Goyal3a644ed2015-05-21 10:28:02 -070064 private AnimatorSet mCurrentColorAnim;
Sunny Goyal316490e2015-06-02 09:38:28 -070065 @Thunk ColorMatrix mSrcFilter, mDstFilter, mCurrentFilter;
Sunny Goyal3a644ed2015-05-21 10:28:02 -070066
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070067
Winson Chung61fa4192011-06-12 15:15:29 -070068 public ButtonDropTarget(Context context, AttributeSet attrs) {
69 this(context, attrs, 0);
70 }
71
72 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
73 super(context, attrs, defStyle);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070074 mBottomDragPadding = getResources().getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Winson Chung61fa4192011-06-12 15:15:29 -070075 }
76
Sunny Goyalfa401a12015-04-10 13:45:42 -070077 @Override
78 protected void onFinishInflate() {
79 super.onFinishInflate();
80 mOriginalTextColor = getTextColors();
81
82 // Remove the text in the Phone UI in landscape
Winson Chung7501adf2015-06-02 11:24:28 -070083 DeviceProfile grid = ((Launcher) getContext()).getDeviceProfile();
84 if (grid.isVerticalBarLayout()) {
85 setText("");
Sunny Goyalfa401a12015-04-10 13:45:42 -070086 }
Winson Chung61fa4192011-06-12 15:15:29 -070087 }
88
Sunny Goyal70660032015-05-14 00:07:08 -070089 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyalfa401a12015-04-10 13:45:42 -070090 protected void setDrawable(int resId) {
Sunny Goyale0cab302015-05-20 16:40:30 -070091 // We do not set the drawable in the xml as that inflates two drawables corresponding to
92 // drawableLeft and drawableStart.
Sunny Goyal3a644ed2015-05-21 10:28:02 -070093 mDrawable = getResources().getDrawable(resId);
Sunny Goyalfa401a12015-04-10 13:45:42 -070094
Sunny Goyale0cab302015-05-20 16:40:30 -070095 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
96 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
97 } else {
98 setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null, null);
Sunny Goyalfa401a12015-04-10 13:45:42 -070099 }
100 }
101
102 public void setLauncher(Launcher launcher) {
103 mLauncher = launcher;
Winson Chung61fa4192011-06-12 15:15:29 -0700104 }
105
Adam Cohend4d7aa52011-07-19 21:47:37 -0700106 public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) {
107 mSearchDropTargetBar = searchDropTargetBar;
108 }
109
Sunny Goyalfa401a12015-04-10 13:45:42 -0700110 @Override
Sunny Goyalddec7342015-04-29 18:12:37 -0700111 public void onFlingToDelete(DragObject d, PointF vec) { }
Winson Chung61fa4192011-06-12 15:15:29 -0700112
Sunny Goyalfa401a12015-04-10 13:45:42 -0700113 @Override
114 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800115 d.dragView.setColor(mHoverColor);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700116 if (Utilities.isLmpOrAbove()) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700117 animateTextColor(mHoverColor);
118 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700119 if (mCurrentFilter == null) {
120 mCurrentFilter = new ColorMatrix();
121 }
122 DragView.setColorScale(mHoverColor, mCurrentFilter);
123 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700124 setTextColor(mHoverColor);
125 }
Winson Chung61fa4192011-06-12 15:15:29 -0700126 }
127
Sunny Goyalfa401a12015-04-10 13:45:42 -0700128 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700129 public void onDragOver(DragObject d) {
130 // Do nothing
131 }
132
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700133 protected void resetHoverColor() {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700134 if (Utilities.isLmpOrAbove()) {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700135 animateTextColor(mOriginalTextColor.getDefaultColor());
136 } else {
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700137 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700138 setTextColor(mOriginalTextColor);
139 }
140 }
141
142 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
143 private void animateTextColor(int targetColor) {
144 if (mCurrentColorAnim != null) {
145 mCurrentColorAnim.cancel();
146 }
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700147
148 mCurrentColorAnim = new AnimatorSet();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700149 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700150
151 if (mSrcFilter == null) {
152 mSrcFilter = new ColorMatrix();
153 mDstFilter = new ColorMatrix();
154 mCurrentFilter = new ColorMatrix();
155 }
156
157 DragView.setColorScale(getTextColor(), mSrcFilter);
158 DragView.setColorScale(targetColor, mDstFilter);
159 ValueAnimator anim1 = ValueAnimator.ofObject(
160 new FloatArrayEvaluator(mCurrentFilter.getArray()),
161 mSrcFilter.getArray(), mDstFilter.getArray());
162 anim1.addUpdateListener(new AnimatorUpdateListener() {
163
164 @Override
165 public void onAnimationUpdate(ValueAnimator animation) {
166 mDrawable.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter));
167 invalidate();
168 }
169 });
170
171 mCurrentColorAnim.play(anim1);
172 mCurrentColorAnim.play(ObjectAnimator.ofArgb(this, "textColor", targetColor));
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700173 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700174 }
175
Sunny Goyalfa401a12015-04-10 13:45:42 -0700176 @Override
177 public final void onDragExit(DragObject d) {
178 if (!d.dragComplete) {
179 d.dragView.setColor(0);
180 resetHoverColor();
181 } else {
182 // Restore the hover color
183 d.dragView.setColor(mHoverColor);
184 }
Winson Chung61fa4192011-06-12 15:15:29 -0700185 }
186
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700187 @Override
188 public final void onDragStart(DragSource source, ItemInfo info, int dragAction) {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700189 mActive = supportsDrop(source, info);
Sunny Goyal3a644ed2015-05-21 10:28:02 -0700190 mDrawable.setColorFilter(null);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700191 if (mCurrentColorAnim != null) {
192 mCurrentColorAnim.cancel();
193 mCurrentColorAnim = null;
194 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700195 setTextColor(mOriginalTextColor);
196 ((ViewGroup) getParent()).setVisibility(mActive ? View.VISIBLE : View.GONE);
197 }
198
199 @Override
200 public final boolean acceptDrop(DragObject dragObject) {
201 return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
202 }
203
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700204 protected abstract boolean supportsDrop(DragSource source, ItemInfo info);
Sunny Goyalfa401a12015-04-10 13:45:42 -0700205
206 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700207 public boolean isDropEnabled() {
208 return mActive;
209 }
210
Sunny Goyalfa401a12015-04-10 13:45:42 -0700211 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700212 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700213 mActive = false;
Winson Chung61fa4192011-06-12 15:15:29 -0700214 }
215
Sunny Goyalfa401a12015-04-10 13:45:42 -0700216 /**
217 * On drop animate the dropView to the icon.
218 */
219 @Override
220 public void onDrop(final DragObject d) {
221 final DragLayer dragLayer = mLauncher.getDragLayer();
222 final Rect from = new Rect();
223 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
224
225 int width = mDrawable.getIntrinsicWidth();
226 int height = mDrawable.getIntrinsicHeight();
227 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
228 width, height);
229 final float scale = (float) to.width() / from.width();
230 mSearchDropTargetBar.deferOnDragEnd();
231
232 Runnable onAnimationEndRunnable = new Runnable() {
233 @Override
234 public void run() {
235 completeDrop(d);
236 mSearchDropTargetBar.onDragEnd();
237 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
238 }
239 };
240 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
241 DRAG_VIEW_DROP_DURATION, new DecelerateInterpolator(2),
242 new LinearInterpolator(), onAnimationEndRunnable,
243 DragLayer.ANIMATION_END_DISAPPEAR, null);
244 }
245
Sunny Goyale9b651e2015-04-24 11:44:51 -0700246 @Override
247 public void prepareAccessibilityDrop() { }
248
Sunny Goyalfa401a12015-04-10 13:45:42 -0700249 @Thunk abstract void completeDrop(DragObject d);
250
Winson Chung61fa4192011-06-12 15:15:29 -0700251 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700252 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700253 super.getHitRect(outRect);
254 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700255
256 int[] coords = new int[2];
257 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
258 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700259 }
260
Sunny Goyalfa401a12015-04-10 13:45:42 -0700261 protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
Winson Chung61967cb2012-02-28 18:11:33 -0800262 DragLayer dragLayer = mLauncher.getDragLayer();
263
264 // Find the rect to animate to (the view is center aligned)
265 Rect to = new Rect();
266 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800267
268 final int width = drawableWidth;
269 final int height = drawableHeight;
270
271 final int left;
272 final int right;
273
Sunny Goyal70660032015-05-14 00:07:08 -0700274 if (Utilities.isRtl(getResources())) {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800275 right = to.right - getPaddingRight();
276 left = right - width;
277 } else {
278 left = to.left + getPaddingLeft();
279 right = left + width;
280 }
281
282 final int top = to.top + (getMeasuredHeight() - height) / 2;
283 final int bottom = top + height;
284
285 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800286
287 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800288 final int xOffset = (int) -(viewWidth - width) / 2;
289 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800290 to.offset(xOffset, yOffset);
291
292 return to;
293 }
294
Sunny Goyalfa401a12015-04-10 13:45:42 -0700295 @Override
Adam Cohen8dfcba42011-07-07 16:38:18 -0700296 public void getLocationInDragLayer(int[] loc) {
297 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
298 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700299
300 public void enableAccessibleDrag(boolean enable) {
301 setOnClickListener(enable ? this : null);
302 }
303
304 protected String getAccessibilityDropConfirmation() {
305 return null;
306 }
307
308 @Override
309 public void onClick(View v) {
310 LauncherAppState.getInstance().getAccessibilityDelegate()
311 .handleAccessibleDrop(this, null, getAccessibilityDropConfirmation());
312 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700313
314 public int getTextColor() {
315 return getTextColors().getDefaultColor();
316 }
Winson Chung61fa4192011-06-12 15:15:29 -0700317}