blob: fb5d0f081adc1a20dbe2efd2aab3e694622922f2 [file] [log] [blame]
Winson Chung760e5372010-12-15 13:14:23 -08001/*
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
Winson Chung760e5372010-12-15 13:14:23 -080019import android.content.Context;
20import android.graphics.Paint;
Winson Chung760e5372010-12-15 13:14:23 -080021import android.graphics.Rect;
22import android.util.AttributeSet;
23import android.view.View;
Michael Jurka577d0172010-12-17 20:04:50 -080024import android.widget.TextView;
25
Winson Chung760e5372010-12-15 13:14:23 -080026
Winson Chung760e5372010-12-15 13:14:23 -080027/**
28 * Implements a DropTarget which allows applications to be dropped on it,
29 * in order to launch the application info for that app.
30 */
Michael Jurka577d0172010-12-17 20:04:50 -080031public class IconDropTarget extends TextView implements DropTarget, DragController.DragListener {
Winson Chung760e5372010-12-15 13:14:23 -080032 protected Launcher mLauncher;
33
34 /**
Michael Jurkab8e14472010-12-20 16:06:10 -080035 * If true, this View responsible for managing its own visibility, and that of its overlapping
36 * views. This is generally the case, but it will be set to false when this is part of the
Winson Chung760e5372010-12-15 13:14:23 -080037 * Contextual Action Bar.
38 */
39 protected boolean mDragAndDropEnabled;
40
41 /** Whether this drop target is active for the current drag */
42 protected boolean mActive;
43
Michael Jurkab8e14472010-12-20 16:06:10 -080044 /** The views that this view should appear in the place of. */
45 protected View[] mOverlappingViews = null;
Winson Chung760e5372010-12-15 13:14:23 -080046
47 /** The paint applied to the drag view on hover */
48 protected final Paint mHoverPaint = new Paint();
49
Winson Chungbe5212b2010-12-20 11:36:33 -080050 /** Drag zone padding [T, R, B, L] */
51 protected final int mDragPadding[] = new int[4];
Winson Chung760e5372010-12-15 13:14:23 -080052
53 public IconDropTarget(Context context, AttributeSet attrs) {
54 this(context, attrs, 0);
55 }
56
57 public IconDropTarget(Context context, AttributeSet attrs, int defStyle) {
58 super(context, attrs, defStyle);
59 mDragAndDropEnabled = true;
60 }
61
Winson Chungbe5212b2010-12-20 11:36:33 -080062 protected void setDragPadding(int t, int r, int b, int l) {
63 mDragPadding[0] = t;
64 mDragPadding[1] = r;
65 mDragPadding[2] = b;
66 mDragPadding[3] = l;
67 }
68
Winson Chung760e5372010-12-15 13:14:23 -080069 void setLauncher(Launcher launcher) {
70 mLauncher = launcher;
71 }
72
Michael Jurkab8e14472010-12-20 16:06:10 -080073 void setOverlappingView(View view) {
74 mOverlappingViews = new View[] { view };
75 }
76
77 void setOverlappingViews(View[] views) {
78 mOverlappingViews = views;
Winson Chung760e5372010-12-15 13:14:23 -080079 }
80
81 void setDragAndDropEnabled(boolean enabled) {
82 mDragAndDropEnabled = enabled;
83 }
84
85 public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
86 DragView dragView, Object dragInfo) {
87 return false;
88 }
89
90 public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,
91 DragView dragView, Object dragInfo) {
92 // Do nothing
93 }
94
95 public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
96 DragView dragView, Object dragInfo) {
97 if (mDragAndDropEnabled) {
98 dragView.setPaint(mHoverPaint);
99 }
100 }
101
102 public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset,
103 DragView dragView, Object dragInfo) {
104 // Do nothing
105 }
106
107 public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
108 DragView dragView, Object dragInfo) {
109 if (mDragAndDropEnabled) {
110 dragView.setPaint(null);
111 }
112 }
113
114 public void onDragStart(DragSource source, Object info, int dragAction) {
115 // Do nothing
116 }
117
118 public boolean isDropEnabled() {
119 return mDragAndDropEnabled && mActive;
120 }
121
122 public void onDragEnd() {
123 // Do nothing
124 }
125
126 @Override
127 public void getHitRect(Rect outRect) {
128 super.getHitRect(outRect);
129 if (LauncherApplication.isScreenXLarge()) {
Winson Chungbe5212b2010-12-20 11:36:33 -0800130 outRect.top -= mDragPadding[0];
131 outRect.right += mDragPadding[1];
132 outRect.bottom += mDragPadding[2];
133 outRect.left -= mDragPadding[3];
Winson Chung760e5372010-12-15 13:14:23 -0800134 }
135 }
136
137 @Override
138 public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset,
139 int yOffset, DragView dragView, Object dragInfo) {
140 return null;
141 }
142}