blob: 1c9fa5f9573d8634d5654fc54ec38444e4576369 [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 Chung043f2af2012-03-01 16:09:54 -080021import android.graphics.PointF;
Winson Chung61967cb2012-02-28 18:11:33 -080022import android.graphics.Rect;
Winson Chung947245b2012-05-15 16:34:19 -070023import android.graphics.drawable.Drawable;
Winson Chung61fa4192011-06-12 15:15:29 -070024import android.util.AttributeSet;
Adam Cohend4d7aa52011-07-19 21:47:37 -070025import android.widget.TextView;
Winson Chung61fa4192011-06-12 15:15:29 -070026
27import com.android.launcher.R;
28
29
30/**
31 * Implements a DropTarget.
32 */
Winson Chunga6427b12011-07-27 10:53:39 -070033public class ButtonDropTarget extends TextView implements DropTarget, DragController.DragListener {
Winson Chung61fa4192011-06-12 15:15:29 -070034
35 protected final int mTransitionDuration;
36
37 protected Launcher mLauncher;
Winson Chunga62e9fd2011-07-11 15:20:48 -070038 private int mBottomDragPadding;
Adam Cohend4d7aa52011-07-19 21:47:37 -070039 protected TextView mText;
40 protected SearchDropTargetBar mSearchDropTargetBar;
Winson Chung61fa4192011-06-12 15:15:29 -070041
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 Chung61967cb2012-02-28 18:11:33 -080046 protected int mHoverColor = 0;
Winson Chung61fa4192011-06-12 15:15:29 -070047
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 Chunga62e9fd2011-07-11 15:20:48 -070055 Resources r = getResources();
56 mTransitionDuration = r.getInteger(R.integer.config_dropTargetBgTransitionDuration);
57 mBottomDragPadding = r.getDimensionPixelSize(R.dimen.drop_target_drag_padding);
Winson Chung61fa4192011-06-12 15:15:29 -070058 }
59
60 void setLauncher(Launcher launcher) {
61 mLauncher = launcher;
62 }
63
64 public boolean acceptDrop(DragObject d) {
65 return false;
66 }
67
Adam Cohend4d7aa52011-07-19 21:47:37 -070068 public void setSearchDropTargetBar(SearchDropTargetBar searchDropTargetBar) {
69 mSearchDropTargetBar = searchDropTargetBar;
70 }
71
Winson Chung947245b2012-05-15 16:34:19 -070072 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 Chung61fa4192011-06-12 15:15:29 -070082 public void onDrop(DragObject d) {
Winson Chung61fa4192011-06-12 15:15:29 -070083 }
84
Winson Chung043f2af2012-03-01 16:09:54 -080085 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
86 // Do nothing
87 }
88
Winson Chung61fa4192011-06-12 15:15:29 -070089 public void onDragEnter(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -080090 d.dragView.setColor(mHoverColor);
Winson Chung61fa4192011-06-12 15:15:29 -070091 }
92
93 public void onDragOver(DragObject d) {
94 // Do nothing
95 }
96
97 public void onDragExit(DragObject d) {
Winson Chung61967cb2012-02-28 18:11:33 -080098 d.dragView.setColor(0);
Winson Chung61fa4192011-06-12 15:15:29 -070099 }
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 Chunga62e9fd2011-07-11 15:20:48 -0700114 public void getHitRect(android.graphics.Rect outRect) {
115 super.getHitRect(outRect);
116 outRect.bottom += mBottomDragPadding;
117 }
118
Winson Chung61967cb2012-02-28 18:11:33 -0800119 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 Chunga62e9fd2011-07-11 15:20:48 -0700139 @Override
Winson Chung61fa4192011-06-12 15:15:29 -0700140 public DropTarget getDropTargetDelegate(DragObject d) {
141 return null;
142 }
Adam Cohen8dfcba42011-07-07 16:38:18 -0700143
144 public void getLocationInDragLayer(int[] loc) {
145 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
146 }
Winson Chung61fa4192011-06-12 15:15:29 -0700147}