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 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame^] | 85 | public boolean acceptDrop(DragObject d) { |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 86 | return false; |
| 87 | } |
| 88 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame^] | 89 | public void onDrop(DragObject d) { |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 90 | // Do nothing |
| 91 | } |
| 92 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame^] | 93 | public void onDragEnter(DragObject d) { |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 94 | if (mDragAndDropEnabled) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame^] | 95 | d.dragView.setPaint(mHoverPaint); |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame^] | 99 | public void onDragOver(DragObject d) { |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 100 | // Do nothing |
| 101 | } |
| 102 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame^] | 103 | public void onDragExit(DragObject d) { |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 104 | if (mDragAndDropEnabled) { |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame^] | 105 | d.dragView.setPaint(null); |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 106 | } |
| 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 Jurka | a2eb170 | 2011-05-12 14:57:05 -0700 | [diff] [blame] | 124 | if (LauncherApplication.isScreenLarge()) { |
Winson Chung | be5212b | 2010-12-20 11:36:33 -0800 | [diff] [blame] | 125 | outRect.top -= mDragPadding[0]; |
| 126 | outRect.right += mDragPadding[1]; |
| 127 | outRect.bottom += mDragPadding[2]; |
| 128 | outRect.left -= mDragPadding[3]; |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | @Override |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame^] | 133 | public DropTarget getDropTargetDelegate(DragObject d) { |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 134 | return null; |
| 135 | } |
| 136 | } |