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