blob: a091f6c5d6d3c20e494e4fa03ac2781c8ad3645b [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
Adam Cohencb3382b2011-05-24 14:07:08 -070085 public boolean acceptDrop(DragObject d) {
Winson Chung760e5372010-12-15 13:14:23 -080086 return false;
87 }
88
Adam Cohencb3382b2011-05-24 14:07:08 -070089 public void onDrop(DragObject d) {
Winson Chung760e5372010-12-15 13:14:23 -080090 // Do nothing
91 }
92
Adam Cohencb3382b2011-05-24 14:07:08 -070093 public void onDragEnter(DragObject d) {
Winson Chung760e5372010-12-15 13:14:23 -080094 if (mDragAndDropEnabled) {
Adam Cohencb3382b2011-05-24 14:07:08 -070095 d.dragView.setPaint(mHoverPaint);
Winson Chung760e5372010-12-15 13:14:23 -080096 }
97 }
98
Adam Cohencb3382b2011-05-24 14:07:08 -070099 public void onDragOver(DragObject d) {
Winson Chung760e5372010-12-15 13:14:23 -0800100 // Do nothing
101 }
102
Adam Cohencb3382b2011-05-24 14:07:08 -0700103 public void onDragExit(DragObject d) {
Winson Chung760e5372010-12-15 13:14:23 -0800104 if (mDragAndDropEnabled) {
Adam Cohencb3382b2011-05-24 14:07:08 -0700105 d.dragView.setPaint(null);
Winson Chung760e5372010-12-15 13:14:23 -0800106 }
107 }
108
109 public void onDragStart(DragSource source, Object info, int dragAction) {
110 // Do nothing
111 }
112
113 public boolean isDropEnabled() {
114 return mDragAndDropEnabled && mActive;
115 }
116
117 public void onDragEnd() {
118 // Do nothing
119 }
120
121 @Override
122 public void getHitRect(Rect outRect) {
123 super.getHitRect(outRect);
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700124 if (LauncherApplication.isScreenLarge()) {
Winson Chungbe5212b2010-12-20 11:36:33 -0800125 outRect.top -= mDragPadding[0];
126 outRect.right += mDragPadding[1];
127 outRect.bottom += mDragPadding[2];
128 outRect.left -= mDragPadding[3];
Winson Chung760e5372010-12-15 13:14:23 -0800129 }
130 }
131
132 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700133 public DropTarget getDropTargetDelegate(DragObject d) {
Winson Chung760e5372010-12-15 13:14:23 -0800134 return null;
135 }
136}