Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.graphics.Paint; |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 21 | import android.graphics.Rect; |
| 22 | import android.util.AttributeSet; |
| 23 | import android.view.View; |
Michael Jurka | 577d017 | 2010-12-17 20:04:50 -0800 | [diff] [blame] | 24 | import android.widget.TextView; |
| 25 | |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 26 | |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 27 | /** |
| 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 Jurka | 577d017 | 2010-12-17 20:04:50 -0800 | [diff] [blame] | 31 | public class IconDropTarget extends TextView implements DropTarget, DragController.DragListener { |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 32 | protected Launcher mLauncher; |
| 33 | |
| 34 | /** |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 35 | * 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 Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 37 | * 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 Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 44 | /** The views that this view should appear in the place of. */ |
| 45 | protected View[] mOverlappingViews = null; |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 46 | |
| 47 | /** The paint applied to the drag view on hover */ |
| 48 | protected final Paint mHoverPaint = new Paint(); |
| 49 | |
Winson Chung | be5212b | 2010-12-20 11:36:33 -0800 | [diff] [blame] | 50 | /** Drag zone padding [T, R, B, L] */ |
| 51 | protected final int mDragPadding[] = new int[4]; |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 52 | |
| 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 Chung | be5212b | 2010-12-20 11:36:33 -0800 | [diff] [blame] | 62 | 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 Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 69 | void setLauncher(Launcher launcher) { |
| 70 | mLauncher = launcher; |
| 71 | } |
| 72 | |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 73 | void setOverlappingView(View view) { |
| 74 | mOverlappingViews = new View[] { view }; |
| 75 | } |
| 76 | |
| 77 | void setOverlappingViews(View[] views) { |
| 78 | mOverlappingViews = views; |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 79 | } |
| 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 Chung | be5212b | 2010-12-20 11:36:33 -0800 | [diff] [blame] | 130 | outRect.top -= mDragPadding[0]; |
| 131 | outRect.right += mDragPadding[1]; |
| 132 | outRect.bottom += mDragPadding[2]; |
| 133 | outRect.left -= mDragPadding[3]; |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 134 | } |
| 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 | } |