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