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 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
| 21 | import android.graphics.Paint; |
| 22 | import android.graphics.PorterDuff; |
| 23 | import android.graphics.PorterDuffColorFilter; |
| 24 | import android.graphics.Rect; |
| 25 | import android.util.AttributeSet; |
| 26 | import android.view.View; |
| 27 | import android.widget.ImageView; |
| 28 | |
| 29 | import com.android.launcher.R; |
| 30 | |
| 31 | /** |
| 32 | * Implements a DropTarget which allows applications to be dropped on it, |
| 33 | * in order to launch the application info for that app. |
| 34 | */ |
| 35 | public class IconDropTarget extends ImageView implements DropTarget, DragController.DragListener { |
| 36 | protected Launcher mLauncher; |
| 37 | |
| 38 | /** |
| 39 | * If true, this View responsible for managing its own visibility, and that of its handle. |
| 40 | * This is generally the case, but it will be set to false when this is part of the |
| 41 | * Contextual Action Bar. |
| 42 | */ |
| 43 | protected boolean mDragAndDropEnabled; |
| 44 | |
| 45 | /** Whether this drop target is active for the current drag */ |
| 46 | protected boolean mActive; |
| 47 | |
| 48 | /** The view that this view should appear in the place of. */ |
| 49 | protected View mHandle = null; |
| 50 | |
| 51 | /** The paint applied to the drag view on hover */ |
| 52 | protected final Paint mHoverPaint = new Paint(); |
| 53 | |
| 54 | /** Drag zone padding */ |
| 55 | protected int mInnerDragPadding; |
| 56 | protected int mOuterDragPadding; |
| 57 | |
| 58 | public IconDropTarget(Context context, AttributeSet attrs) { |
| 59 | this(context, attrs, 0); |
| 60 | } |
| 61 | |
| 62 | public IconDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 63 | super(context, attrs, defStyle); |
| 64 | mDragAndDropEnabled = true; |
| 65 | } |
| 66 | |
| 67 | void setLauncher(Launcher launcher) { |
| 68 | mLauncher = launcher; |
| 69 | } |
| 70 | |
| 71 | void setHandle(View view) { |
| 72 | mHandle = view; |
| 73 | } |
| 74 | |
| 75 | void setDragAndDropEnabled(boolean enabled) { |
| 76 | mDragAndDropEnabled = enabled; |
| 77 | } |
| 78 | |
| 79 | public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 80 | DragView dragView, Object dragInfo) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 85 | DragView dragView, Object dragInfo) { |
| 86 | // Do nothing |
| 87 | } |
| 88 | |
| 89 | public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, |
| 90 | DragView dragView, Object dragInfo) { |
| 91 | if (mDragAndDropEnabled) { |
| 92 | dragView.setPaint(mHoverPaint); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, |
| 97 | DragView dragView, Object dragInfo) { |
| 98 | // Do nothing |
| 99 | } |
| 100 | |
| 101 | public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, |
| 102 | DragView dragView, Object dragInfo) { |
| 103 | if (mDragAndDropEnabled) { |
| 104 | dragView.setPaint(null); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | public void onDragStart(DragSource source, Object info, int dragAction) { |
| 109 | // Do nothing |
| 110 | } |
| 111 | |
| 112 | public boolean isDropEnabled() { |
| 113 | return mDragAndDropEnabled && mActive; |
| 114 | } |
| 115 | |
| 116 | public void onDragEnd() { |
| 117 | // Do nothing |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void getHitRect(Rect outRect) { |
| 122 | super.getHitRect(outRect); |
| 123 | if (LauncherApplication.isScreenXLarge()) { |
| 124 | outRect.top -= mOuterDragPadding; |
| 125 | outRect.left -= mInnerDragPadding; |
| 126 | outRect.bottom += mOuterDragPadding; |
| 127 | outRect.right += mOuterDragPadding; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, |
| 133 | int yOffset, DragView dragView, Object dragInfo) { |
| 134 | return null; |
| 135 | } |
| 136 | } |