blob: 683c511da7e9a3aaa003b4024b1589e9d4955416 [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 Goyalfe0d1f22015-04-28 22:01:31 -070019import android.animation.ObjectAnimator;
20import android.annotation.TargetApi;
Winson Chung61fa4192011-06-12 15:15:29 -070021import android.content.Context;
Sunny Goyalfa401a12015-04-10 13:45:42 -070022import android.content.res.ColorStateList;
23import android.content.res.Configuration;
Winson Chung043f2af2012-03-01 16:09:54 -080024import android.graphics.PointF;
Winson Chung61967cb2012-02-28 18:11:33 -080025import android.graphics.Rect;
Winson Chung947245b2012-05-15 16:34:19 -070026import android.graphics.drawable.Drawable;
Sunny Goyalfa401a12015-04-10 13:45:42 -070027import android.graphics.drawable.TransitionDrawable;
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070028import android.os.Build;
Winson Chung61fa4192011-06-12 15:15:29 -070029import android.util.AttributeSet;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080030import android.view.View;
Sunny Goyal1a70cef2015-04-22 11:29:51 -070031import android.view.View.OnClickListener;
Sunny Goyalfa401a12015-04-10 13:45:42 -070032import android.view.ViewGroup;
33import android.view.animation.DecelerateInterpolator;
34import android.view.animation.LinearInterpolator;
Adam Cohend4d7aa52011-07-19 21:47:37 -070035import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070036
Sunny Goyalfa401a12015-04-10 13:45:42 -070037import com.android.launcher3.util.Thunk;
Winson Chung61fa4192011-06-12 15:15:29 -070038
39/**
40 * Implements a DropTarget.
41 */
Sunny Goyal1a70cef2015-04-22 11:29:51 -070042public abstract class ButtonDropTarget extends TextView
43 implements DropTarget, DragController.DragListener, OnClickListener {
Sunny Goyalfa401a12015-04-10 13:45:42 -070044
45 private static int DRAG_VIEW_DROP_DURATION = 285;
Winson Chung61fa4192011-06-12 15:15:29 -070046
Winson Chung61fa4192011-06-12 15:15:29 -070047 protected Launcher mLauncher;
Winson Chunga62e9fd2011-07-11 15:20:48 -070048 private int mBottomDragPadding;
Adam Cohend4d7aa52011-07-19 21:47:37 -070049 protected TextView mText;
50 protected SearchDropTargetBar mSearchDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070051
52 /** Whether this drop target is active for the current drag */
53 protected boolean mActive;
54
55 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080056 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070057
Sunny Goyalfa401a12015-04-10 13:45:42 -070058 protected ColorStateList mOriginalTextColor;
59 protected TransitionDrawable mDrawable;
60
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070061 private ObjectAnimator mCurrentColorAnim;
62
Winson Chung61fa4192011-06-12 15:15:29 -070063 public ButtonDropTarget(Context context, AttributeSet attrs) {
64 this(context, attrs, 0);
65 }
66
67 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
68 super(context, attrs, defStyle);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -070069 mBottomDragPadding = getResources().getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Winson Chung61fa4192011-06-12 15:15:29 -070070 }
71
Sunny Goyalfa401a12015-04-10 13:45:42 -070072 @Override
73 protected void onFinishInflate() {
74 super.onFinishInflate();
75 mOriginalTextColor = getTextColors();
76
77 // Remove the text in the Phone UI in landscape
78 int orientation = getResources().getConfiguration().orientation;
79 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
80 if (!LauncherAppState.getInstance().isScreenLarge()) {
81 setText("");
82 }
83 }
Winson Chung61fa4192011-06-12 15:15:29 -070084 }
85
Sunny Goyalfa401a12015-04-10 13:45:42 -070086 protected void setDrawable(int resId) {
87 // Get the hover color
88 mDrawable = (TransitionDrawable) getCurrentDrawable();
89
90 if (mDrawable == null) {
91 // TODO: investigate why this is ever happening. Presently only on one known device.
92 mDrawable = (TransitionDrawable) getResources().getDrawable(resId);
93 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
94 }
95
96 if (null != mDrawable) {
97 mDrawable.setCrossFadeEnabled(true);
98 }
99 }
100
101 public void setLauncher(Launcher launcher) {
102 mLauncher = launcher;
Winson Chung61fa4192011-06-12 15:15:29 -0700103 }
104
Adam Cohend4d7aa52011-07-19 21:47:37 -0700105 public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) {
106 mSearchDropTargetBar = searchDropTargetBar;
107 }
108
Winson Chung947245b2012-05-15 16:34:19 -0700109 protected Drawable getCurrentDrawable() {
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800110 Drawable[] drawables = getCompoundDrawablesRelative();
Winson Chung947245b2012-05-15 16:34:19 -0700111 for (int i = 0; i < drawables.length; ++i) {
112 if (drawables[i] != null) {
113 return drawables[i];
114 }
115 }
116 return null;
117 }
118
Sunny Goyalfa401a12015-04-10 13:45:42 -0700119 @Override
Sunny Goyalddec7342015-04-29 18:12:37 -0700120 public void onFlingToDelete(DragObject d, PointF vec) { }
Winson Chung61fa4192011-06-12 15:15:29 -0700121
Sunny Goyalfa401a12015-04-10 13:45:42 -0700122 @Override
123 public final void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -0800124 d.dragView.setColor(mHoverColor);
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700125 if (Utilities.isLmpOrAbove()) {
126 mDrawable.startTransition(DragView.COLOR_CHANGE_DURATION);
127 animateTextColor(mHoverColor);
128 } else {
129 mDrawable.startTransition(0);
130 setTextColor(mHoverColor);
131 }
Winson Chung61fa4192011-06-12 15:15:29 -0700132 }
133
Sunny Goyalfa401a12015-04-10 13:45:42 -0700134 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700135 public void onDragOver(DragObject d) {
136 // Do nothing
137 }
138
Sunny Goyalfa401a12015-04-10 13:45:42 -0700139 protected void resetHoverColor() {
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700140 if (Utilities.isLmpOrAbove()) {
141 mDrawable.reverseTransition(DragView.COLOR_CHANGE_DURATION);
142 animateTextColor(mOriginalTextColor.getDefaultColor());
143 } else {
144 mDrawable.resetTransition();
145 setTextColor(mOriginalTextColor);
146 }
147 }
148
149 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
150 private void animateTextColor(int targetColor) {
151 if (mCurrentColorAnim != null) {
152 mCurrentColorAnim.cancel();
153 }
154 mCurrentColorAnim = ObjectAnimator.ofArgb(this, "textColor", targetColor);
155 mCurrentColorAnim.setDuration(DragView.COLOR_CHANGE_DURATION);
156 mCurrentColorAnim.start();
Winson Chung61fa4192011-06-12 15:15:29 -0700157 }
158
Sunny Goyalfa401a12015-04-10 13:45:42 -0700159 @Override
160 public final void onDragExit(DragObject d) {
161 if (!d.dragComplete) {
162 d.dragView.setColor(0);
163 resetHoverColor();
164 } else {
165 // Restore the hover color
166 d.dragView.setColor(mHoverColor);
167 }
Winson Chung61fa4192011-06-12 15:15:29 -0700168 }
169
Sunny Goyalfa401a12015-04-10 13:45:42 -0700170 @Override
171 public final void onDragStart(DragSource source, Object info, int dragAction) {
172 mActive = supportsDrop(source, info);
173 mDrawable.resetTransition();
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700174 if (mCurrentColorAnim != null) {
175 mCurrentColorAnim.cancel();
176 mCurrentColorAnim = null;
177 }
Sunny Goyalfa401a12015-04-10 13:45:42 -0700178 setTextColor(mOriginalTextColor);
179 ((ViewGroup) getParent()).setVisibility(mActive ? View.VISIBLE : View.GONE);
180 }
181
182 @Override
183 public final boolean acceptDrop(DragObject dragObject) {
184 return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
185 }
186
187 protected abstract boolean supportsDrop(DragSource source, Object info);
188
189 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700190 public boolean isDropEnabled() {
191 return mActive;
192 }
193
Sunny Goyalfa401a12015-04-10 13:45:42 -0700194 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700195 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700196 mActive = false;
Winson Chung61fa4192011-06-12 15:15:29 -0700197 }
198
Sunny Goyalfa401a12015-04-10 13:45:42 -0700199 /**
200 * On drop animate the dropView to the icon.
201 */
202 @Override
203 public void onDrop(final DragObject d) {
204 final DragLayer dragLayer = mLauncher.getDragLayer();
205 final Rect from = new Rect();
206 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
207
208 int width = mDrawable.getIntrinsicWidth();
209 int height = mDrawable.getIntrinsicHeight();
210 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
211 width, height);
212 final float scale = (float) to.width() / from.width();
213 mSearchDropTargetBar.deferOnDragEnd();
214
215 Runnable onAnimationEndRunnable = new Runnable() {
216 @Override
217 public void run() {
218 completeDrop(d);
219 mSearchDropTargetBar.onDragEnd();
220 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
221 }
222 };
223 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
224 DRAG_VIEW_DROP_DURATION, new DecelerateInterpolator(2),
225 new LinearInterpolator(), onAnimationEndRunnable,
226 DragLayer.ANIMATION_END_DISAPPEAR, null);
227 }
228
Sunny Goyale9b651e2015-04-24 11:44:51 -0700229 @Override
230 public void prepareAccessibilityDrop() { }
231
Sunny Goyalfa401a12015-04-10 13:45:42 -0700232 @Thunk abstract void completeDrop(DragObject d);
233
Winson Chung61fa4192011-06-12 15:15:29 -0700234 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700235 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700236 super.getHitRect(outRect);
237 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700238
239 int[] coords = new int[2];
240 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
241 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700242 }
243
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800244 private boolean isRtl() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700245 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800246 }
247
Sunny Goyalfa401a12015-04-10 13:45:42 -0700248 protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
Winson Chung61967cb2012-02-28 18:11:33 -0800249 DragLayer dragLayer = mLauncher.getDragLayer();
250
251 // Find the rect to animate to (the view is center aligned)
252 Rect to = new Rect();
253 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800254
255 final int width = drawableWidth;
256 final int height = drawableHeight;
257
258 final int left;
259 final int right;
260
261 if (isRtl()) {
262 right = to.right - getPaddingRight();
263 left = right - width;
264 } else {
265 left = to.left + getPaddingLeft();
266 right = left + width;
267 }
268
269 final int top = to.top + (getMeasuredHeight() - height) / 2;
270 final int bottom = top + height;
271
272 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800273
274 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800275 final int xOffset = (int) -(viewWidth - width) / 2;
276 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800277 to.offset(xOffset, yOffset);
278
279 return to;
280 }
281
Sunny Goyalfa401a12015-04-10 13:45:42 -0700282 @Override
Adam Cohen8dfcba42011-07-07 16:38:18 -0700283 public void getLocationInDragLayer(int[] loc) {
284 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
285 }
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700286
287 public void enableAccessibleDrag(boolean enable) {
288 setOnClickListener(enable ? this : null);
289 }
290
291 protected String getAccessibilityDropConfirmation() {
292 return null;
293 }
294
295 @Override
296 public void onClick(View v) {
297 LauncherAppState.getInstance().getAccessibilityDelegate()
298 .handleAccessibleDrop(this, null, getAccessibilityDropConfirmation());
299 }
Sunny Goyalfe0d1f22015-04-28 22:01:31 -0700300
301 public int getTextColor() {
302 return getTextColors().getDefaultColor();
303 }
Winson Chung61fa4192011-06-12 15:15:29 -0700304}