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; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 24 | import android.util.AttributeSet; |
| 25 | import android.view.View; |
| 26 | import android.widget.ImageView; |
| 27 | |
| 28 | /** |
| 29 | * Implements a DropTarget which allows applications to be dropped on it, |
| 30 | * in order to launch the application info for that app. |
| 31 | */ |
| 32 | public class ApplicationInfoDropTarget extends ImageView implements DropTarget, DragController.DragListener { |
| 33 | private Launcher mLauncher; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 34 | private boolean mActive = false; |
| 35 | |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 36 | /** |
| 37 | * If true, this View responsible for managing its own visibility, and that of its handle. |
| 38 | * This is generally the case, but it will be set to false when this is part of the |
| 39 | * Contextual Action Bar. |
| 40 | */ |
| 41 | private boolean mManageVisibility = true; |
| 42 | |
| 43 | /** The view that this view should appear in the place of. */ |
| 44 | private View mHandle = null; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 45 | |
| 46 | private final Paint mPaint = new Paint(); |
| 47 | |
| 48 | public ApplicationInfoDropTarget(Context context, AttributeSet attrs) { |
| 49 | this(context, attrs, 0); |
| 50 | } |
| 51 | |
| 52 | public ApplicationInfoDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 53 | super(context, attrs, defStyle); |
| 54 | } |
| 55 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Set the color that will be used as a filter over objects dragged over this object. |
| 58 | */ |
| 59 | public void setDragColor(int color) { |
| 60 | mPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP)); |
| 61 | } |
| 62 | |
| 63 | public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 64 | DragView dragView, Object dragInfo) { |
| 65 | |
| 66 | // acceptDrop is called just before onDrop. We do the work here, rather than |
| 67 | // in onDrop, because it allows us to reject the drop (by returning false) |
| 68 | // so that the object being dragged isn't removed from the home screen. |
| 69 | |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 70 | ComponentName componentName = null; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 71 | if (dragInfo instanceof ApplicationInfo) { |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 72 | componentName = ((ApplicationInfo)dragInfo).componentName; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 73 | } else if (dragInfo instanceof ShortcutInfo) { |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 74 | componentName = ((ShortcutInfo)dragInfo).intent.getComponent(); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 75 | } |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 76 | mLauncher.startApplicationDetailsActivity(componentName); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 77 | return false; |
| 78 | } |
| 79 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 80 | public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 81 | DragView dragView, Object dragInfo) { |
| 82 | |
| 83 | } |
| 84 | |
| 85 | public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, |
| 86 | DragView dragView, Object dragInfo) { |
| 87 | dragView.setPaint(mPaint); |
| 88 | } |
| 89 | |
| 90 | public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, |
| 91 | DragView dragView, Object dragInfo) { |
| 92 | } |
| 93 | |
| 94 | public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, |
| 95 | DragView dragView, Object dragInfo) { |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 96 | dragView.setPaint(null); |
| 97 | } |
| 98 | |
| 99 | public void onDragStart(DragSource source, Object info, int dragAction) { |
| 100 | if (info != null) { |
| 101 | mActive = true; |
| 102 | |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 103 | if (mManageVisibility) { |
| 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 | |
| 113 | public void onDragEnd() { |
| 114 | if (mActive) { |
| 115 | mActive = false; |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 116 | if (mManageVisibility) { |
| 117 | setVisibility(GONE); |
| 118 | mHandle.setVisibility(VISIBLE); |
| 119 | } |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | void setLauncher(Launcher launcher) { |
| 124 | mLauncher = launcher; |
| 125 | } |
| 126 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 127 | void setHandle(View view) { |
| 128 | mHandle = view; |
| 129 | } |
| 130 | |
Patrick Dubroy | dea9e93 | 2010-09-22 15:04:29 -0700 | [diff] [blame] | 131 | void setManageVisibility(boolean value) { |
| 132 | mManageVisibility = value; |
| 133 | } |
| 134 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 135 | @Override |
| 136 | public DropTarget getDropTargetDelegate(DragSource source, int x, int y, int xOffset, int yOffset, |
| 137 | DragView dragView, Object dragInfo) { |
| 138 | return null; |
| 139 | } |
| 140 | } |