Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [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 | |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 19 | import android.content.ComponentName; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.graphics.Paint; |
| 22 | import android.graphics.PorterDuff; |
| 23 | import android.graphics.PorterDuffColorFilter; |
Michael Jurka | 3adcb0c | 2010-10-15 18:07:21 -0700 | [diff] [blame] | 24 | import android.graphics.Rect; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 25 | import android.util.AttributeSet; |
| 26 | import android.view.View; |
| 27 | import android.widget.ImageView; |
| 28 | |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame^] | 29 | import com.android.launcher.R; |
| 30 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 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 ApplicationInfoDropTarget extends ImageView implements DropTarget, DragController.DragListener { |
| 36 | private Launcher mLauncher; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 37 | private boolean mActive = false; |
| 38 | |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 39 | /** |
| 40 | * If true, this View responsible for managing its own visibility, and that of its handle. |
| 41 | * This is generally the case, but it will be set to false when this is part of the |
| 42 | * Contextual Action Bar. |
| 43 | */ |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame^] | 44 | private boolean mDragAndDropEnabled = true; |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 45 | |
| 46 | /** The view that this view should appear in the place of. */ |
| 47 | private View mHandle = null; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 48 | |
| 49 | private final Paint mPaint = new Paint(); |
| 50 | |
| 51 | public ApplicationInfoDropTarget(Context context, AttributeSet attrs) { |
| 52 | this(context, attrs, 0); |
| 53 | } |
| 54 | |
| 55 | public ApplicationInfoDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 56 | super(context, attrs, defStyle); |
| 57 | } |
| 58 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 59 | /** |
| 60 | * Set the color that will be used as a filter over objects dragged over this object. |
| 61 | */ |
| 62 | public void setDragColor(int color) { |
| 63 | mPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP)); |
| 64 | } |
| 65 | |
| 66 | public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 67 | DragView dragView, Object dragInfo) { |
| 68 | |
| 69 | // acceptDrop is called just before onDrop. We do the work here, rather than |
| 70 | // in onDrop, because it allows us to reject the drop (by returning false) |
| 71 | // so that the object being dragged isn't removed from the home screen. |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame^] | 72 | if (getVisibility() != VISIBLE) return false; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 73 | |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 74 | ComponentName componentName = null; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 75 | if (dragInfo instanceof ApplicationInfo) { |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 76 | componentName = ((ApplicationInfo)dragInfo).componentName; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 77 | } else if (dragInfo instanceof ShortcutInfo) { |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 78 | componentName = ((ShortcutInfo)dragInfo).intent.getComponent(); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 79 | } |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 80 | mLauncher.startApplicationDetailsActivity(componentName); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 81 | return false; |
| 82 | } |
| 83 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 84 | public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 85 | DragView dragView, Object dragInfo) { |
| 86 | |
| 87 | } |
| 88 | |
| 89 | public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, |
| 90 | DragView dragView, Object dragInfo) { |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame^] | 91 | if (!mDragAndDropEnabled) return; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 92 | dragView.setPaint(mPaint); |
| 93 | } |
| 94 | |
| 95 | public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, |
| 96 | DragView dragView, Object dragInfo) { |
| 97 | } |
| 98 | |
| 99 | public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, |
| 100 | DragView dragView, Object dragInfo) { |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame^] | 101 | if (!mDragAndDropEnabled) return; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 102 | dragView.setPaint(null); |
| 103 | } |
| 104 | |
| 105 | public void onDragStart(DragSource source, Object info, int dragAction) { |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame^] | 106 | if (info != null && mDragAndDropEnabled) { |
Michael Jurka | 774bd37 | 2010-10-22 13:40:50 -0700 | [diff] [blame] | 107 | final int itemType = ((ItemInfo)info).itemType; |
| 108 | mActive = (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION); |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame^] | 109 | |
| 110 | // Only show the info icon when an application is selected |
| 111 | if (mActive) { |
| 112 | setVisibility(VISIBLE); |
| 113 | } |
| 114 | if (mHandle != null) { |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 115 | mHandle.setVisibility(INVISIBLE); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 116 | } |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 120 | public boolean isDropEnabled() { |
Michael Jurka | 3adcb0c | 2010-10-15 18:07:21 -0700 | [diff] [blame] | 121 | return mActive; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 124 | public void onDragEnd() { |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame^] | 125 | if (!mDragAndDropEnabled) return; |
| 126 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 127 | if (mActive) { |
| 128 | mActive = false; |
Michael Jurka | 466810b | 2010-10-25 13:41:02 -0700 | [diff] [blame] | 129 | } |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame^] | 130 | setVisibility(GONE); |
| 131 | if (mHandle != null) { |
Michael Jurka | 466810b | 2010-10-25 13:41:02 -0700 | [diff] [blame] | 132 | mHandle.setVisibility(VISIBLE); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Michael Jurka | 3adcb0c | 2010-10-15 18:07:21 -0700 | [diff] [blame] | 136 | @Override |
| 137 | public void getHitRect(Rect outRect) { |
| 138 | super.getHitRect(outRect); |
| 139 | if (LauncherApplication.isScreenXLarge()) { |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame^] | 140 | final int padding = R.dimen.delete_zone_padding; |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 141 | final int outerDragPadding = |
| 142 | getResources().getDimensionPixelSize(R.dimen.delete_zone_size); |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame^] | 143 | final int innerDragPadding = getResources().getDimensionPixelSize(padding); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 144 | outRect.top -= outerDragPadding; |
| 145 | outRect.left -= innerDragPadding; |
| 146 | outRect.bottom += outerDragPadding; |
| 147 | outRect.right += outerDragPadding; |
Michael Jurka | 3adcb0c | 2010-10-15 18:07:21 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 151 | void setLauncher(Launcher launcher) { |
| 152 | mLauncher = launcher; |
| 153 | } |
| 154 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 155 | void setHandle(View view) { |
| 156 | mHandle = view; |
| 157 | } |
| 158 | |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame^] | 159 | void setDragAndDropEnabled(boolean enabled) { |
| 160 | mDragAndDropEnabled = enabled; |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 163 | @Override |
| 164 | public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset, |
| 165 | DragView dragView, Object dragInfo) { |
| 166 | return null; |
| 167 | } |
| 168 | } |