blob: 5b399087a9770c3446593decd02886fbd77b717f [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
19import android.content.Context;
Sunny Goyalfa401a12015-04-10 13:45:42 -070020import android.content.res.ColorStateList;
21import android.content.res.Configuration;
Winson Chunga62e9fd2011-07-11 15:20:48 -070022import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080023import android.graphics.PointF;
Winson Chung61967cb2012-02-28 18:11:33 -080024import android.graphics.Rect;
Winson Chung947245b2012-05-15 16:34:19 -070025import android.graphics.drawable.Drawable;
Sunny Goyalfa401a12015-04-10 13:45:42 -070026import android.graphics.drawable.TransitionDrawable;
Winson Chung61fa4192011-06-12 15:15:29 -070027import android.util.AttributeSet;
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -080028import android.view.View;
Sunny Goyalfa401a12015-04-10 13:45:42 -070029import android.view.ViewGroup;
30import android.view.animation.DecelerateInterpolator;
31import android.view.animation.LinearInterpolator;
Adam Cohend4d7aa52011-07-19 21:47:37 -070032import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070033
Sunny Goyalfa401a12015-04-10 13:45:42 -070034import com.android.launcher3.R;
35import com.android.launcher3.util.Thunk;
Winson Chung61fa4192011-06-12 15:15:29 -070036
37/**
38 * Implements a DropTarget.
39 */
Sunny Goyalfa401a12015-04-10 13:45:42 -070040public abstract class ButtonDropTarget extends TextView implements DropTarget, DragController.DragListener {
41
42 private static int DRAG_VIEW_DROP_DURATION = 285;
Winson Chung61fa4192011-06-12 15:15:29 -070043
44 protected final int mTransitionDuration;
45
46 protected Launcher mLauncher;
Winson Chunga62e9fd2011-07-11 15:20:48 -070047 private int mBottomDragPadding;
Adam Cohend4d7aa52011-07-19 21:47:37 -070048 protected TextView mText;
49 protected SearchDropTargetBar mSearchDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070050
51 /** Whether this drop target is active for the current drag */
52 protected boolean mActive;
53
54 /** The paint applied to the drag view on hover */
Winson Chung61967cb2012-02-28 18:11:33 -080055 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070056
Sunny Goyalfa401a12015-04-10 13:45:42 -070057 protected ColorStateList mOriginalTextColor;
58 protected TransitionDrawable mDrawable;
59
Winson Chung61fa4192011-06-12 15:15:29 -070060 public ButtonDropTarget(Context context, AttributeSet attrs) {
61 this(context, attrs, 0);
62 }
63
64 public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) {
65 super(context, attrs, defStyle);
66
Winson Chunga62e9fd2011-07-11 15:20:48 -070067 Resources r = getResources();
68 mTransitionDuration = r.getInteger(R.integer.config_dropTargetBgTransitionDuration);
69 mBottomDragPadding = r.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
120 public void onFlingToDelete(DragObject d, int x, int y, 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 Goyalfa401a12015-04-10 13:45:42 -0700125 mDrawable.startTransition(mTransitionDuration);
126 setTextColor(mHoverColor);
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 Goyalfa401a12015-04-10 13:45:42 -0700134 protected void resetHoverColor() {
135 mDrawable.resetTransition();
136 setTextColor(mOriginalTextColor);
Winson Chung61fa4192011-06-12 15:15:29 -0700137 }
138
Sunny Goyalfa401a12015-04-10 13:45:42 -0700139 @Override
140 public final void onDragExit(DragObject d) {
141 if (!d.dragComplete) {
142 d.dragView.setColor(0);
143 resetHoverColor();
144 } else {
145 // Restore the hover color
146 d.dragView.setColor(mHoverColor);
147 }
Winson Chung61fa4192011-06-12 15:15:29 -0700148 }
149
Sunny Goyalfa401a12015-04-10 13:45:42 -0700150 @Override
151 public final void onDragStart(DragSource source, Object info, int dragAction) {
152 mActive = supportsDrop(source, info);
153 mDrawable.resetTransition();
154 setTextColor(mOriginalTextColor);
155 ((ViewGroup) getParent()).setVisibility(mActive ? View.VISIBLE : View.GONE);
156 }
157
158 @Override
159 public final boolean acceptDrop(DragObject dragObject) {
160 return supportsDrop(dragObject.dragSource, dragObject.dragInfo);
161 }
162
163 protected abstract boolean supportsDrop(DragSource source, Object info);
164
165 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700166 public boolean isDropEnabled() {
167 return mActive;
168 }
169
Sunny Goyalfa401a12015-04-10 13:45:42 -0700170 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700171 public void onDragEnd() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700172 mActive = false;
Winson Chung61fa4192011-06-12 15:15:29 -0700173 }
174
Sunny Goyalfa401a12015-04-10 13:45:42 -0700175 /**
176 * On drop animate the dropView to the icon.
177 */
178 @Override
179 public void onDrop(final DragObject d) {
180 final DragLayer dragLayer = mLauncher.getDragLayer();
181 final Rect from = new Rect();
182 dragLayer.getViewRectRelativeToSelf(d.dragView, from);
183
184 int width = mDrawable.getIntrinsicWidth();
185 int height = mDrawable.getIntrinsicHeight();
186 final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(),
187 width, height);
188 final float scale = (float) to.width() / from.width();
189 mSearchDropTargetBar.deferOnDragEnd();
190
191 Runnable onAnimationEndRunnable = new Runnable() {
192 @Override
193 public void run() {
194 completeDrop(d);
195 mSearchDropTargetBar.onDragEnd();
196 mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null);
197 }
198 };
199 dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f,
200 DRAG_VIEW_DROP_DURATION, new DecelerateInterpolator(2),
201 new LinearInterpolator(), onAnimationEndRunnable,
202 DragLayer.ANIMATION_END_DISAPPEAR, null);
203 }
204
205 @Thunk abstract void completeDrop(DragObject d);
206
Winson Chung61fa4192011-06-12 15:15:29 -0700207 @Override
Adam Cohen7d30a372013-07-01 17:03:59 -0700208 public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) {
Winson Chunga62e9fd2011-07-11 15:20:48 -0700209 super.getHitRect(outRect);
210 outRect.bottom += mBottomDragPadding;
Winson Chung156ab5b2013-07-12 14:14:16 -0700211
212 int[] coords = new int[2];
213 mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords);
214 outRect.offsetTo(coords[0], coords[1]);
Winson Chunga62e9fd2011-07-11 15:20:48 -0700215 }
216
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800217 private boolean isRtl() {
Sunny Goyalfa401a12015-04-10 13:45:42 -0700218 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800219 }
220
Sunny Goyalfa401a12015-04-10 13:45:42 -0700221 protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) {
Winson Chung61967cb2012-02-28 18:11:33 -0800222 DragLayer dragLayer = mLauncher.getDragLayer();
223
224 // Find the rect to animate to (the view is center aligned)
225 Rect to = new Rect();
226 dragLayer.getViewRectRelativeToSelf(this, to);
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800227
228 final int width = drawableWidth;
229 final int height = drawableHeight;
230
231 final int left;
232 final int right;
233
234 if (isRtl()) {
235 right = to.right - getPaddingRight();
236 left = right - width;
237 } else {
238 left = to.left + getPaddingLeft();
239 right = left + width;
240 }
241
242 final int top = to.top + (getMeasuredHeight() - height) / 2;
243 final int bottom = top + height;
244
245 to.set(left, top, right, bottom);
Winson Chung61967cb2012-02-28 18:11:33 -0800246
247 // Center the destination rect about the trash icon
Fabrice Di Megliod6a33c62013-02-06 15:40:46 -0800248 final int xOffset = (int) -(viewWidth - width) / 2;
249 final int yOffset = (int) -(viewHeight - height) / 2;
Winson Chung61967cb2012-02-28 18:11:33 -0800250 to.offset(xOffset, yOffset);
251
252 return to;
253 }
254
Sunny Goyalfa401a12015-04-10 13:45:42 -0700255 @Override
Adam Cohen8dfcba42011-07-07 16:38:18 -0700256 public void getLocationInDragLayer(int[] loc) {
257 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
258 }
Winson Chung61fa4192011-06-12 15:15:29 -0700259}