blob: dc34d8828574ca3ee1945b77a3ea082c37655de7 [file] [log] [blame]
Winson Chung61fa4192011-06-12 15:15:29 -07001/*
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;
Winson Chunga62e9fd2011-07-11 15:20:48 -070020import android.content.res.Resources;
Winson Chung61fa4192011-06-12 15:15:29 -070021import android.graphics.Paint;
Winson Chung61967cb2012-02-28 18:11:33 -080022import android.graphics.Rect;
Winson Chung61fa4192011-06-12 15:15:29 -070023import android.util.AttributeSet;
Adam Cohend4d7aa52011-07-19 21:47:37 -070024import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070025
26import com.android.launcher.R;
27
28
29/**
30 * Implements a DropTarget.
31 */
Winson Chunga6427b12011-07-27 10:53:39 -070032public class ButtonDropTarget extends TextView implements DropTarget, DragController.DragListener {
Winson Chung61fa4192011-06-12 15:15:29 -070033
34 protected final int mTransitionDuration;
35
36 protected Launcher mLauncher;
Winson Chunga62e9fd2011-07-11 15:20:48 -070037 private int mBottomDragPadding;
Adam Cohend4d7aa52011-07-19 21:47:37 -070038 protected TextView mText;
39 protected SearchDropTargetBar mSearchDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070040
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 Chung61967cb2012-02-28 18:11:33 -080045 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070046
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 Chunga62e9fd2011-07-11 15:20:48 -070054 Resources r = getResources();
55 mTransitionDuration = r.getInteger(R.integer.config_dropTargetBgTransitionDuration);
56 mBottomDragPadding = r.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Winson Chung61fa4192011-06-12 15:15:29 -070057 }
58
59 void setLauncher(Launcher launcher) {
60 mLauncher = launcher;
61 }
62
63 public boolean acceptDrop(DragObject d) {
64 return false;
65 }
66
Adam Cohend4d7aa52011-07-19 21:47:37 -070067 public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) {
68 mSearchDropTargetBar = searchDropTargetBar;
69 }
70
Winson Chung61fa4192011-06-12 15:15:29 -070071 public void onDrop(DragObject d) {
Winson Chung61fa4192011-06-12 15:15:29 -070072 }
73
74 public void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -080075 d.dragView.setColor(mHoverColor);
Winson Chung61fa4192011-06-12 15:15:29 -070076 }
77
78 public void onDragOver(DragObject d) {
79 // Do nothing
80 }
81
82 public void onDragExit(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -080083 d.dragView.setColor(0);
Winson Chung61fa4192011-06-12 15:15:29 -070084 }
85
86 public void onDragStart(DragSource source, Object info, int dragAction) {
87 // Do nothing
88 }
89
90 public boolean isDropEnabled() {
91 return mActive;
92 }
93
94 public void onDragEnd() {
95 // Do nothing
96 }
97
98 @Override
Winson Chunga62e9fd2011-07-11 15:20:48 -070099 public void getHitRect(android.graphics.Rect outRect) {
100 super.getHitRect(outRect);
101 outRect.bottom += mBottomDragPadding;
102 }
103
Winson Chung61967cb2012-02-28 18:11:33 -0800104 Rect getIconRect(int itemWidth, int itemHeight, int drawableWidth, int drawableHeight) {
105 DragLayer dragLayer = mLauncher.getDragLayer();
106
107 // Find the rect to animate to (the view is center aligned)
108 Rect to = new Rect();
109 dragLayer.getViewRectRelativeToSelf(this, to);
110 int width = drawableWidth;
111 int height = drawableHeight;
112 int left = to.left + getPaddingLeft();
113 int top = to.top + (getMeasuredHeight() - height) / 2;
114 to.set(left, top, left + width, top + height);
115
116 // Center the destination rect about the trash icon
117 int xOffset = (int) -(itemWidth - width) / 2;
118 int yOffset = (int) -(itemHeight - height) / 2;
119 to.offset(xOffset, yOffset);
120
121 return to;
122 }
123
Winson Chunga62e9fd2011-07-11 15:20:48 -0700124 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700125 public DropTarget getDropTargetDelegate(DragObject d) {
126 return null;
127 }
Adam Cohen8dfcba42011-07-07 16:38:18 -0700128
129 public void getLocationInDragLayer(int[] loc) {
130 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
131 }
Winson Chung61fa4192011-06-12 15:15:29 -0700132}