Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 20 | import android.content.res.ColorStateList; |
| 21 | import android.content.res.Configuration; |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 23 | import android.graphics.PointF; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 24 | import android.graphics.Rect; |
Winson Chung | 947245b | 2012-05-15 16:34:19 -0700 | [diff] [blame] | 25 | import android.graphics.drawable.Drawable; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 26 | import android.graphics.drawable.TransitionDrawable; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 27 | import android.util.AttributeSet; |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 28 | import android.view.View; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 29 | import android.view.ViewGroup; |
| 30 | import android.view.animation.DecelerateInterpolator; |
| 31 | import android.view.animation.LinearInterpolator; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 32 | import android.widget.TextView; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 33 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 34 | import com.android.launcher3.R; |
| 35 | import com.android.launcher3.util.Thunk; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Implements a DropTarget. |
| 39 | */ |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 40 | public abstract class ButtonDropTarget extends TextView implements DropTarget, DragController.DragListener { |
| 41 | |
| 42 | private static int DRAG_VIEW_DROP_DURATION = 285; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 43 | |
| 44 | protected final int mTransitionDuration; |
| 45 | |
| 46 | protected Launcher mLauncher; |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 47 | private int mBottomDragPadding; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 48 | protected TextView mText; |
| 49 | protected SearchDropTargetBar mSearchDropTargetBar; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 50 | |
| 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 Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 55 | protected int mHoverColor = 0; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 56 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 57 | protected ColorStateList mOriginalTextColor; |
| 58 | protected TransitionDrawable mDrawable; |
| 59 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 60 | 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 Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 67 | Resources r = getResources(); |
| 68 | mTransitionDuration = r.getInteger(R.integer.config_dropTargetBgTransitionDuration); |
| 69 | mBottomDragPadding = r.getDimensionPixelSize(R.dimen.drop_target_drag_padding); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 72 | @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 Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 86 | 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 Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 105 | public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) { |
| 106 | mSearchDropTargetBar = searchDropTargetBar; |
| 107 | } |
| 108 | |
Winson Chung | 947245b | 2012-05-15 16:34:19 -0700 | [diff] [blame] | 109 | protected Drawable getCurrentDrawable() { |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 110 | Drawable[] drawables = getCompoundDrawablesRelative(); |
Winson Chung | 947245b | 2012-05-15 16:34:19 -0700 | [diff] [blame] | 111 | 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 Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 119 | @Override |
| 120 | public void onFlingToDelete(DragObject d, int x, int y, PointF vec) { } |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 121 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 122 | @Override |
| 123 | public final void onDragEnter(DragObject d) { |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 124 | d.dragView.setColor(mHoverColor); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 125 | mDrawable.startTransition(mTransitionDuration); |
| 126 | setTextColor(mHoverColor); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 129 | @Override |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 130 | public void onDragOver(DragObject d) { |
| 131 | // Do nothing |
| 132 | } |
| 133 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 134 | protected void resetHoverColor() { |
| 135 | mDrawable.resetTransition(); |
| 136 | setTextColor(mOriginalTextColor); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 139 | @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 Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 150 | @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 Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 166 | public boolean isDropEnabled() { |
| 167 | return mActive; |
| 168 | } |
| 169 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 170 | @Override |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 171 | public void onDragEnd() { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 172 | mActive = false; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 175 | /** |
| 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 Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 207 | @Override |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 208 | public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) { |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 209 | super.getHitRect(outRect); |
| 210 | outRect.bottom += mBottomDragPadding; |
Winson Chung | 156ab5b | 2013-07-12 14:14:16 -0700 | [diff] [blame] | 211 | |
| 212 | int[] coords = new int[2]; |
| 213 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords); |
| 214 | outRect.offsetTo(coords[0], coords[1]); |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 217 | private boolean isRtl() { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 218 | return (getLayoutDirection() == LAYOUT_DIRECTION_RTL); |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 221 | protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) { |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 222 | 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 Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 227 | |
| 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 Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 246 | |
| 247 | // Center the destination rect about the trash icon |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 248 | final int xOffset = (int) -(viewWidth - width) / 2; |
| 249 | final int yOffset = (int) -(viewHeight - height) / 2; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 250 | to.offset(xOffset, yOffset); |
| 251 | |
| 252 | return to; |
| 253 | } |
| 254 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 255 | @Override |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 256 | public void getLocationInDragLayer(int[] loc) { |
| 257 | mLauncher.getDragLayer().getLocationInDragLayer(this, loc); |
| 258 | } |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 259 | } |