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 | |
| 29 | /** |
| 30 | * Implements a DropTarget which allows applications to be dropped on it, |
| 31 | * in order to launch the application info for that app. |
| 32 | */ |
| 33 | public class ApplicationInfoDropTarget extends ImageView implements DropTarget, DragController.DragListener { |
| 34 | private Launcher mLauncher; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 35 | private boolean mActive = false; |
| 36 | |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 37 | /** |
| 38 | * If true, this View responsible for managing its own visibility, and that of its handle. |
| 39 | * This is generally the case, but it will be set to false when this is part of the |
| 40 | * Contextual Action Bar. |
| 41 | */ |
| 42 | private boolean mManageVisibility = true; |
| 43 | |
| 44 | /** The view that this view should appear in the place of. */ |
| 45 | private View mHandle = null; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 46 | |
| 47 | private final Paint mPaint = new Paint(); |
| 48 | |
| 49 | public ApplicationInfoDropTarget(Context context, AttributeSet attrs) { |
| 50 | this(context, attrs, 0); |
| 51 | } |
| 52 | |
| 53 | public ApplicationInfoDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 54 | super(context, attrs, defStyle); |
| 55 | } |
| 56 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Set the color that will be used as a filter over objects dragged over this object. |
| 59 | */ |
| 60 | public void setDragColor(int color) { |
| 61 | mPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP)); |
| 62 | } |
| 63 | |
| 64 | public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 65 | DragView dragView, Object dragInfo) { |
| 66 | |
| 67 | // acceptDrop is called just before onDrop. We do the work here, rather than |
| 68 | // in onDrop, because it allows us to reject the drop (by returning false) |
| 69 | // so that the object being dragged isn't removed from the home screen. |
| 70 | |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 71 | ComponentName componentName = null; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 72 | if (dragInfo instanceof ApplicationInfo) { |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 73 | componentName = ((ApplicationInfo)dragInfo).componentName; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 74 | } else if (dragInfo instanceof ShortcutInfo) { |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 75 | componentName = ((ShortcutInfo)dragInfo).intent.getComponent(); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 76 | } |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 77 | mLauncher.startApplicationDetailsActivity(componentName); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 78 | return false; |
| 79 | } |
| 80 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 81 | public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 82 | DragView dragView, Object dragInfo) { |
| 83 | |
| 84 | } |
| 85 | |
| 86 | public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, |
| 87 | DragView dragView, Object dragInfo) { |
| 88 | dragView.setPaint(mPaint); |
| 89 | } |
| 90 | |
| 91 | public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, |
| 92 | DragView dragView, Object dragInfo) { |
| 93 | } |
| 94 | |
| 95 | public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, |
| 96 | DragView dragView, Object dragInfo) { |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 97 | dragView.setPaint(null); |
| 98 | } |
| 99 | |
| 100 | public void onDragStart(DragSource source, Object info, int dragAction) { |
| 101 | if (info != null) { |
Michael Jurka | 774bd37 | 2010-10-22 13:40:50 -0700 | [diff] [blame^] | 102 | final int itemType = ((ItemInfo)info).itemType; |
| 103 | mActive = (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION); |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 104 | if (mManageVisibility) { |
| 105 | // Only show the info icon when an application is selected |
Michael Jurka | 774bd37 | 2010-10-22 13:40:50 -0700 | [diff] [blame^] | 106 | if (mActive) { |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 107 | setVisibility(VISIBLE); |
| 108 | } |
| 109 | mHandle.setVisibility(INVISIBLE); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 110 | } |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 114 | public boolean isDropEnabled() { |
Michael Jurka | 3adcb0c | 2010-10-15 18:07:21 -0700 | [diff] [blame] | 115 | return mActive; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 118 | public void onDragEnd() { |
| 119 | if (mActive) { |
| 120 | mActive = false; |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 121 | if (mManageVisibility) { |
| 122 | setVisibility(GONE); |
| 123 | mHandle.setVisibility(VISIBLE); |
| 124 | } |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Michael Jurka | 3adcb0c | 2010-10-15 18:07:21 -0700 | [diff] [blame] | 128 | @Override |
| 129 | public void getHitRect(Rect outRect) { |
| 130 | super.getHitRect(outRect); |
| 131 | if (LauncherApplication.isScreenXLarge()) { |
Michael Jurka | 774bd37 | 2010-10-22 13:40:50 -0700 | [diff] [blame^] | 132 | // TODO: This is a temporary hack. mManageVisiblity = false when you're in CAB mode. |
| 133 | // In that case, this icon is more tightly spaced next to the delete icon so we want |
| 134 | // it to have a smaller drag region. When the new drag&drop system comes in, we'll |
| 135 | // dispatch the drag/drop by calculating what target you're overlapping |
| 136 | final int dragPadding = mManageVisibility ? 50 : 10; |
| 137 | outRect.top -= dragPadding; |
| 138 | outRect.left -= dragPadding; |
| 139 | outRect.bottom += dragPadding; |
| 140 | outRect.right += dragPadding; |
Michael Jurka | 3adcb0c | 2010-10-15 18:07:21 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 144 | void setLauncher(Launcher launcher) { |
| 145 | mLauncher = launcher; |
| 146 | } |
| 147 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 148 | void setHandle(View view) { |
| 149 | mHandle = view; |
| 150 | } |
| 151 | |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 152 | void setManageVisibility(boolean value) { |
| 153 | mManageVisibility = value; |
| 154 | } |
| 155 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 156 | @Override |
| 157 | public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset, |
| 158 | DragView dragView, Object dragInfo) { |
| 159 | return null; |
| 160 | } |
| 161 | } |