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; |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 20 | import android.content.res.Resources; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 21 | import android.graphics.PointF; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 22 | import android.graphics.Rect; |
Winson Chung | 947245b | 2012-05-15 16:34:19 -0700 | [diff] [blame] | 23 | import android.graphics.drawable.Drawable; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 24 | import android.util.AttributeSet; |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 25 | import android.view.View; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 26 | import android.widget.TextView; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 27 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Implements a DropTarget. |
| 31 | */ |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 32 | public class ButtonDropTarget extends TextView implements DropTarget, DragController.DragListener { |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 33 | |
| 34 | protected final int mTransitionDuration; |
| 35 | |
| 36 | protected Launcher mLauncher; |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 37 | private int mBottomDragPadding; |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 38 | protected TextView mText; |
| 39 | protected SearchDropTargetBar mSearchDropTargetBar; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 40 | |
| 41 | /** Whether this drop target is active for the current drag */ |
| 42 | protected boolean mActive; |
| 43 | |
| 44 | /** The paint applied to the drag view on hover */ |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 45 | protected int mHoverColor = 0; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 46 | |
| 47 | public ButtonDropTarget(Context context, AttributeSet attrs) { |
| 48 | this(context, attrs, 0); |
| 49 | } |
| 50 | |
| 51 | public ButtonDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 52 | super(context, attrs, defStyle); |
| 53 | |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 54 | Resources r = getResources(); |
| 55 | mTransitionDuration = r.getInteger(R.integer.config_dropTargetBgTransitionDuration); |
| 56 | mBottomDragPadding = r.getDimensionPixelSize(R.dimen.drop_target_drag_padding); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void setLauncher(Launcher launcher) { |
| 60 | mLauncher = launcher; |
| 61 | } |
| 62 | |
| 63 | public boolean acceptDrop(DragObject d) { |
| 64 | return false; |
| 65 | } |
| 66 | |
Adam Cohen | d4d7aa5 | 2011-07-19 21:47:37 -0700 | [diff] [blame] | 67 | public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) { |
| 68 | mSearchDropTargetBar = searchDropTargetBar; |
| 69 | } |
| 70 | |
Winson Chung | 947245b | 2012-05-15 16:34:19 -0700 | [diff] [blame] | 71 | protected Drawable getCurrentDrawable() { |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 72 | Drawable[] drawables = getCompoundDrawablesRelative(); |
Winson Chung | 947245b | 2012-05-15 16:34:19 -0700 | [diff] [blame] | 73 | for (int i = 0; i < drawables.length; ++i) { |
| 74 | if (drawables[i] != null) { |
| 75 | return drawables[i]; |
| 76 | } |
| 77 | } |
| 78 | return null; |
| 79 | } |
| 80 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 81 | public void onDrop(DragObject d) { |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 84 | public void onFlingToDelete(DragObject d, int x, int y, PointF vec) { |
| 85 | // Do nothing |
| 86 | } |
| 87 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 88 | public void onDragEnter(DragObject d) { |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 89 | d.dragView.setColor(mHoverColor); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | public void onDragOver(DragObject d) { |
| 93 | // Do nothing |
| 94 | } |
| 95 | |
| 96 | public void onDragExit(DragObject d) { |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 97 | d.dragView.setColor(0); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | public void onDragStart(DragSource source, Object info, int dragAction) { |
| 101 | // Do nothing |
| 102 | } |
| 103 | |
| 104 | public boolean isDropEnabled() { |
| 105 | return mActive; |
| 106 | } |
| 107 | |
| 108 | public void onDragEnd() { |
| 109 | // Do nothing |
| 110 | } |
| 111 | |
| 112 | @Override |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 113 | public void getHitRectRelativeToDragLayer(android.graphics.Rect outRect) { |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 114 | super.getHitRect(outRect); |
| 115 | outRect.bottom += mBottomDragPadding; |
Winson Chung | 156ab5b | 2013-07-12 14:14:16 -0700 | [diff] [blame] | 116 | |
| 117 | int[] coords = new int[2]; |
| 118 | mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, coords); |
| 119 | outRect.offsetTo(coords[0], coords[1]); |
Winson Chung | a62e9fd | 2011-07-11 15:20:48 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 122 | private boolean isRtl() { |
| 123 | return (getLayoutDirection() == View.LAYOUT_DIRECTION_RTL); |
| 124 | } |
| 125 | |
| 126 | Rect getIconRect(int viewWidth, int viewHeight, int drawableWidth, int drawableHeight) { |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 127 | DragLayer dragLayer = mLauncher.getDragLayer(); |
| 128 | |
| 129 | // Find the rect to animate to (the view is center aligned) |
| 130 | Rect to = new Rect(); |
| 131 | dragLayer.getViewRectRelativeToSelf(this, to); |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 132 | |
| 133 | final int width = drawableWidth; |
| 134 | final int height = drawableHeight; |
| 135 | |
| 136 | final int left; |
| 137 | final int right; |
| 138 | |
| 139 | if (isRtl()) { |
| 140 | right = to.right - getPaddingRight(); |
| 141 | left = right - width; |
| 142 | } else { |
| 143 | left = to.left + getPaddingLeft(); |
| 144 | right = left + width; |
| 145 | } |
| 146 | |
| 147 | final int top = to.top + (getMeasuredHeight() - height) / 2; |
| 148 | final int bottom = top + height; |
| 149 | |
| 150 | to.set(left, top, right, bottom); |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 151 | |
| 152 | // Center the destination rect about the trash icon |
Fabrice Di Meglio | d6a33c6 | 2013-02-06 15:40:46 -0800 | [diff] [blame] | 153 | final int xOffset = (int) -(viewWidth - width) / 2; |
| 154 | final int yOffset = (int) -(viewHeight - height) / 2; |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 155 | to.offset(xOffset, yOffset); |
| 156 | |
| 157 | return to; |
| 158 | } |
| 159 | |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 160 | public void getLocationInDragLayer(int[] loc) { |
| 161 | mLauncher.getDragLayer().getLocationInDragLayer(this, loc); |
| 162 | } |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 163 | } |