ButtonDropTarget.java revision 043f2af567178b82b0b41f12d379e7dd12da2936
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 17package com.android.launcher2; 18 19import android.content.Context; 20import android.content.res.Resources; 21import android.graphics.PointF; 22import android.graphics.Rect; 23import android.util.AttributeSet; 24import android.widget.TextView; 25 26import com.android.launcher.R; 27 28 29/** 30 * Implements a DropTarget. 31 */ 32public class ButtonDropTarget extends TextView implements DropTarget, DragController.DragListener { 33 34 protected final int mTransitionDuration; 35 36 protected Launcher mLauncher; 37 private int mBottomDragPadding; 38 protected TextView mText; 39 protected SearchDropTargetBar mSearchDropTargetBar; 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 */ 45 protected int mHoverColor = 0; 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 54 Resources r = getResources(); 55 mTransitionDuration = r.getInteger(R.integer.config_dropTargetBgTransitionDuration); 56 mBottomDragPadding = r.getDimensionPixelSize(R.dimen.drop_target_drag_padding); 57 } 58 59 void setLauncher(Launcher launcher) { 60 mLauncher = launcher; 61 } 62 63 public boolean acceptDrop(DragObject d) { 64 return false; 65 } 66 67 public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) { 68 mSearchDropTargetBar = searchDropTargetBar; 69 } 70 71 public void onDrop(DragObject d) { 72 } 73 74 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) { 75 // Do nothing 76 } 77 78 public void onDragEnter(DragObject d) { 79 d.dragView.setColor(mHoverColor); 80 } 81 82 public void onDragOver(DragObject d) { 83 // Do nothing 84 } 85 86 public void onDragExit(DragObject d) { 87 d.dragView.setColor(0); 88 } 89 90 public void onDragStart(DragSource source, Object info, int dragAction) { 91 // Do nothing 92 } 93 94 public boolean isDropEnabled() { 95 return mActive; 96 } 97 98 public void onDragEnd() { 99 // Do nothing 100 } 101 102 @Override 103 public void getHitRect(android.graphics.Rect outRect) { 104 super.getHitRect(outRect); 105 outRect.bottom += mBottomDragPadding; 106 } 107 108 Rect getIconRect(int itemWidth, int itemHeight, int drawableWidth, int drawableHeight) { 109 DragLayer dragLayer = mLauncher.getDragLayer(); 110 111 // Find the rect to animate to (the view is center aligned) 112 Rect to = new Rect(); 113 dragLayer.getViewRectRelativeToSelf(this, to); 114 int width = drawableWidth; 115 int height = drawableHeight; 116 int left = to.left + getPaddingLeft(); 117 int top = to.top + (getMeasuredHeight() - height) / 2; 118 to.set(left, top, left + width, top + height); 119 120 // Center the destination rect about the trash icon 121 int xOffset = (int) -(itemWidth - width) / 2; 122 int yOffset = (int) -(itemHeight - height) / 2; 123 to.offset(xOffset, yOffset); 124 125 return to; 126 } 127 128 @Override 129 public DropTarget getDropTargetDelegate(DragObject d) { 130 return null; 131 } 132 133 public void getLocationInDragLayer(int[] loc) { 134 mLauncher.getDragLayer().getLocationInDragLayer(this, loc); 135 } 136} 137