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 | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 29 | import android.view.View.OnClickListener; |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 30 | import android.view.ViewGroup; |
| 31 | import android.view.animation.DecelerateInterpolator; |
| 32 | import android.view.animation.LinearInterpolator; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 33 | import android.widget.TextView; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 34 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 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 | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 40 | public abstract class ButtonDropTarget extends TextView |
| 41 | implements DropTarget, DragController.DragListener, OnClickListener { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 42 | |
| 43 | private static int DRAG_VIEW_DROP_DURATION = 285; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 44 | |
| 45 | protected final int mTransitionDuration; |
| 46 | |
| 47 | protected Launcher mLauncher; |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 48 | private int mBottomDragPadding; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 49 | protected TextView mText; |
| 50 | protected SearchDropTargetBar mSearchDropTargetBar; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 51 | |
| 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 Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 56 | protected int mHoverColor = 0; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 57 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 58 | protected ColorStateList mOriginalTextColor; |
| 59 | protected TransitionDrawable mDrawable; |
| 60 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 61 | public ButtonDropTarget(Context context, AttributeSet attrs) { |
| 62 | this(context, attrs, 0); |
| 63 | } |
| 64 | |
| 65 | public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 66 | super(context, attrs, defStyle); |
| 67 | |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 68 | Resources r = getResources(); |
| 69 | mTransitionDuration = r.getInteger(R.integer.config_dropTargetBgTransitionDuration); |
| 70 | mBottomDragPadding = r.getDimensionPixelSize(R.dimen.drop_target_drag_padding); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 73 | @Override |
| 74 | protected void onFinishInflate() { |
| 75 | super.onFinishInflate(); |
| 76 | mOriginalTextColor = getTextColors(); |
| 77 | |
| 78 | // Remove the text in the Phone UI in landscape |
| 79 | int orientation = getResources().getConfiguration().orientation; |
| 80 | if (orientation == Configuration.ORIENTATION_LANDSCAPE) { |
| 81 | if (!LauncherAppState.getInstance().isScreenLarge()) { |
| 82 | setText(""); |
| 83 | } |
| 84 | } |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 87 | protected void setDrawable(int resId) { |
| 88 | // Get the hover color |
| 89 | mDrawable = (TransitionDrawable) getCurrentDrawable(); |
| 90 | |
| 91 | if (mDrawable == null) { |
| 92 | // TODO: investigate why this is ever happening. Presently only on one known device. |
| 93 | mDrawable = (TransitionDrawable) getResources().getDrawable(resId); |
| 94 | setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null); |
| 95 | } |
| 96 | |
| 97 | if (null != mDrawable) { |
| 98 | mDrawable.setCrossFadeEnabled(true); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | public void setLauncher(Launcher launcher) { |
| 103 | mLauncher = launcher; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 106 | public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) { |
| 107 | mSearchDropTargetBar = searchDropTargetBar; |
| 108 | } |
| 109 | |
Winson Chung | 947245b | 2012-05-15 16:34:19 -0700 | [diff] [blame] | 110 | protected Drawable getCurrentDrawable() { |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 111 | Drawable[] drawables = getCompoundDrawablesRelative(); |
Winson Chung | 947245b | 2012-05-15 16:34:19 -0700 | [diff] [blame] | 112 | for (int i = 0; i < drawables.length; ++i) { |
| 113 | if (drawables[i] != null) { |
| 114 | return drawables[i]; |
| 115 | } |
| 116 | } |
| 117 | return null; |
| 118 | } |
| 119 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 120 | @Override |
| 121 | public void onFlingToDelete(DragObject d, int x, int y, PointF vec) { } |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 122 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 123 | @Override |
| 124 | public final void onDragEnter(DragObject d) { |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 125 | d.dragView.setColor(mHoverColor); |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 126 | mDrawable.startTransition(mTransitionDuration); |
| 127 | setTextColor(mHoverColor); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 130 | @Override |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 131 | public void onDragOver(DragObject d) { |
| 132 | // Do nothing |
| 133 | } |
| 134 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 135 | protected void resetHoverColor() { |
| 136 | mDrawable.resetTransition(); |
| 137 | setTextColor(mOriginalTextColor); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 140 | @Override |
| 141 | public final void onDragExit(DragObject d) { |
| 142 | if (!d.dragComplete) { |
| 143 | d.dragView.setColor(0); |
| 144 | resetHoverColor(); |
| 145 | } else { |
| 146 | // Restore the hover color |
| 147 | d.dragView.setColor(mHoverColor); |
| 148 | } |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 151 | @Override |
| 152 | public final void onDragStart(DragSource source, Object info, int dragAction) { |
| 153 | mActive = supportsDrop(source, info); |
| 154 | mDrawable.resetTransition(); |
| 155 | setTextColor(mOriginalTextColor); |
| 156 | ((ViewGroup) getParent()).setVisibility(mActive ? View.VISIBLE : View.GONE); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public final boolean acceptDrop(DragObject dragObject) { |
| 161 | return supportsDrop(dragObject.dragSource, dragObject.dragInfo); |
| 162 | } |
| 163 | |
| 164 | protected abstract boolean supportsDrop(DragSource source, Object info); |
| 165 | |
| 166 | @Override |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 167 | public boolean isDropEnabled() { |
| 168 | return mActive; |
| 169 | } |
| 170 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 171 | @Override |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 172 | public void onDragEnd() { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 173 | mActive = false; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 176 | /** |
| 177 | * On drop animate the dropView to the icon. |
| 178 | */ |
| 179 | @Override |
| 180 | public void onDrop(final DragObject d) { |
| 181 | final DragLayer dragLayer = mLauncher.getDragLayer(); |
| 182 | final Rect from = new Rect(); |
| 183 | dragLayer.getViewRectRelativeToSelf(d.dragView, from); |
| 184 | |
| 185 | int width = mDrawable.getIntrinsicWidth(); |
| 186 | int height = mDrawable.getIntrinsicHeight(); |
| 187 | final Rect to = getIconRect(d.dragView.getMeasuredWidth(), d.dragView.getMeasuredHeight(), |
| 188 | width, height); |
| 189 | final float scale = (float) to.width() / from.width(); |
| 190 | mSearchDropTargetBar.deferOnDragEnd(); |
| 191 | |
| 192 | Runnable onAnimationEndRunnable = new Runnable() { |
| 193 | @Override |
| 194 | public void run() { |
| 195 | completeDrop(d); |
| 196 | mSearchDropTargetBar.onDragEnd(); |
| 197 | mLauncher.exitSpringLoadedDragModeDelayed(true, 0, null); |
| 198 | } |
| 199 | }; |
| 200 | dragLayer.animateView(d.dragView, from, to, scale, 1f, 1f, 0.1f, 0.1f, |
| 201 | DRAG_VIEW_DROP_DURATION, new DecelerateInterpolator(2), |
| 202 | new LinearInterpolator(), onAnimationEndRunnable, |
| 203 | DragLayer.ANIMATION_END_DISAPPEAR, null); |
| 204 | } |
| 205 | |
Sunny Goyal | e9b651e | 2015-04-24 11:44:51 -0700 | [diff] [blame^] | 206 | @Override |
| 207 | public void prepareAccessibilityDrop() { } |
| 208 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 209 | @Thunk abstract void completeDrop(DragObject d); |
| 210 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 211 | @Override |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 212 | public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) { |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 213 | super.getHitRect(outRect); |
| 214 | outRect.bottom += mBottomDragPadding; |
Winson Chung | 156ab5b | 2013-07-12 14:14:16 -0700 | [diff] [blame] | 215 | |
| 216 | int[] coords = new int[2]; |
| 217 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords); |
| 218 | outRect.offsetTo(coords[0], coords[1]); |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 221 | private boolean isRtl() { |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 222 | return (getLayoutDirection() == LAYOUT_DIRECTION_RTL); |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 225 | protected Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) { |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 226 | DragLayer dragLayer = mLauncher.getDragLayer(); |
| 227 | |
| 228 | // Find the rect to animate to (the view is center aligned) |
| 229 | Rect to = new Rect(); |
| 230 | dragLayer.getViewRectRelativeToSelf(this, to); |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 231 | |
| 232 | final int width = drawableWidth; |
| 233 | final int height = drawableHeight; |
| 234 | |
| 235 | final int left; |
| 236 | final int right; |
| 237 | |
| 238 | if (isRtl()) { |
| 239 | right = to.right - getPaddingRight(); |
| 240 | left = right - width; |
| 241 | } else { |
| 242 | left = to.left + getPaddingLeft(); |
| 243 | right = left + width; |
| 244 | } |
| 245 | |
| 246 | final int top = to.top + (getMeasuredHeight() - height) / 2; |
| 247 | final int bottom = top + height; |
| 248 | |
| 249 | to.set(left, top, right, bottom); |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 250 | |
| 251 | // Center the destination rect about the trash icon |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 252 | final int xOffset = (int) -(viewWidth - width) / 2; |
| 253 | final int yOffset = (int) -(viewHeight - height) / 2; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 254 | to.offset(xOffset, yOffset); |
| 255 | |
| 256 | return to; |
| 257 | } |
| 258 | |
Sunny Goyal | fa401a1 | 2015-04-10 13:45:42 -0700 | [diff] [blame] | 259 | @Override |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 260 | public void getLocationInDragLayer(int[] loc) { |
| 261 | mLauncher.getDragLayer().getLocationInDragLayer(this, loc); |
| 262 | } |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 263 | |
| 264 | public void enableAccessibleDrag(boolean enable) { |
| 265 | setOnClickListener(enable ? this : null); |
| 266 | } |
| 267 | |
| 268 | protected String getAccessibilityDropConfirmation() { |
| 269 | return null; |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public void onClick(View v) { |
| 274 | LauncherAppState.getInstance().getAccessibilityDelegate() |
| 275 | .handleAccessibleDrop(this, null, getAccessibilityDropConfirmation()); |
| 276 | } |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 277 | } |