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) { |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 102 | if (mManageVisibility) { |
Michael Jurka | 3adcb0c | 2010-10-15 18:07:21 -0700 | [diff] [blame] | 103 | mActive = true; |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 104 | // Only show the info icon when an application is selected |
| 105 | if (((ItemInfo)info).itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { |
| 106 | setVisibility(VISIBLE); |
| 107 | } |
| 108 | mHandle.setVisibility(INVISIBLE); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 109 | } |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 113 | public boolean isDropEnabled() { |
Michael Jurka | 3adcb0c | 2010-10-15 18:07:21 -0700 | [diff] [blame] | 114 | return mActive; |
Michael Jurka | 0280c3b | 2010-09-17 15:00:07 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 117 | public void onDragEnd() { |
| 118 | if (mActive) { |
| 119 | mActive = false; |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 120 | if (mManageVisibility) { |
| 121 | setVisibility(GONE); |
| 122 | mHandle.setVisibility(VISIBLE); |
| 123 | } |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Michael Jurka | 3adcb0c | 2010-10-15 18:07:21 -0700 | [diff] [blame] | 127 | @Override |
| 128 | public void getHitRect(Rect outRect) { |
| 129 | super.getHitRect(outRect); |
| 130 | if (LauncherApplication.isScreenXLarge()) { |
| 131 | outRect.top -= 50; |
| 132 | outRect.left -= 50; |
| 133 | outRect.bottom += 50; |
| 134 | outRect.right += 50; |
| 135 | } |
| 136 | } |
| 137 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 138 | void setLauncher(Launcher launcher) { |
| 139 | mLauncher = launcher; |
| 140 | } |
| 141 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 142 | void setHandle(View view) { |
| 143 | mHandle = view; |
| 144 | } |
| 145 | |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 146 | void setManageVisibility(boolean value) { |
| 147 | mManageVisibility = value; |
| 148 | } |
| 149 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 150 | @Override |
| 151 | public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset, |
| 152 | DragView dragView, Object dragInfo) { |
| 153 | return null; |
| 154 | } |
| 155 | } |