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 | |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.Animator.AnimatorListener; |
| 21 | import android.animation.ObjectAnimator; |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 22 | import android.content.ComponentName; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 24 | import android.graphics.PorterDuff; |
| 25 | import android.graphics.PorterDuffColorFilter; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 26 | import android.util.AttributeSet; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 27 | |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 28 | import com.android.launcher.R; |
| 29 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Implements a DropTarget which allows applications to be dropped on it, |
| 32 | * in order to launch the application info for that app. |
| 33 | */ |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 34 | public class ApplicationInfoDropTarget extends IconDropTarget { |
| 35 | private static final int sFadeInAnimationDuration = 200; |
| 36 | private static final int sFadeOutAnimationDuration = 100; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 37 | |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 38 | private ObjectAnimator mFadeAnimator; |
| 39 | private ObjectAnimator mHandleFadeAnimator; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 40 | |
| 41 | public ApplicationInfoDropTarget(Context context, AttributeSet attrs) { |
| 42 | this(context, attrs, 0); |
| 43 | } |
| 44 | |
| 45 | public ApplicationInfoDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 46 | super(context, attrs, defStyle); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 47 | |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 48 | // Set the hover paint colour |
| 49 | int colour = getContext().getResources().getColor(R.color.app_info_filter); |
| 50 | mHoverPaint.setColorFilter(new PorterDuffColorFilter(colour, PorterDuff.Mode.SRC_ATOP)); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 54 | DragView dragView, Object dragInfo) { |
| 55 | |
| 56 | // acceptDrop is called just before onDrop. We do the work here, rather than |
| 57 | // in onDrop, because it allows us to reject the drop (by returning false) |
| 58 | // 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] | 59 | if (getVisibility() != VISIBLE) return false; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 60 | |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 61 | ComponentName componentName = null; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 62 | if (dragInfo instanceof ApplicationInfo) { |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 63 | componentName = ((ApplicationInfo)dragInfo).componentName; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 64 | } else if (dragInfo instanceof ShortcutInfo) { |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 65 | componentName = ((ShortcutInfo)dragInfo).intent.getComponent(); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 66 | } |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 67 | mLauncher.startApplicationDetailsActivity(componentName); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 71 | public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, |
| 72 | DragView dragView, Object dragInfo) { |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 73 | if (!mDragAndDropEnabled) return; |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 74 | dragView.setPaint(mHoverPaint); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, |
| 78 | DragView dragView, Object dragInfo) { |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 79 | if (!mDragAndDropEnabled) return; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 80 | dragView.setPaint(null); |
| 81 | } |
| 82 | |
| 83 | public void onDragStart(DragSource source, Object info, int dragAction) { |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 84 | if (info != null && mDragAndDropEnabled) { |
Michael Jurka | 774bd37 | 2010-10-22 13:40:50 -0700 | [diff] [blame] | 85 | final int itemType = ((ItemInfo)info).itemType; |
| 86 | mActive = (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION); |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 87 | if (mActive) { |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 88 | // Fade in this icon |
| 89 | if (mFadeAnimator != null) mFadeAnimator.cancel(); |
| 90 | mFadeAnimator = ObjectAnimator.ofFloat(this, "alpha", 0.0f, 1.0f); |
| 91 | mFadeAnimator.setDuration(sFadeInAnimationDuration); |
| 92 | mFadeAnimator.start(); |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 93 | setVisibility(VISIBLE); |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 94 | |
| 95 | // Fade out the handle |
| 96 | if (mHandle != null) { |
| 97 | if (mHandleFadeAnimator != null) mHandleFadeAnimator.cancel(); |
| 98 | mHandleFadeAnimator = ObjectAnimator.ofFloat(mHandle, "alpha", 0.0f); |
| 99 | mHandleFadeAnimator.setDuration(sFadeOutAnimationDuration); |
| 100 | mHandleFadeAnimator.addListener(new AnimatorListener() { |
| 101 | public void onAnimationStart(Animator animation) {} |
| 102 | public void onAnimationRepeat(Animator animation) {} |
| 103 | public void onAnimationEnd(Animator animation) { |
| 104 | onEndOrCancel(); |
| 105 | } |
| 106 | public void onAnimationCancel(Animator animation) { |
| 107 | onEndOrCancel(); |
| 108 | } |
| 109 | private void onEndOrCancel() { |
| 110 | mHandle.setVisibility(INVISIBLE); |
| 111 | mHandleFadeAnimator = null; |
| 112 | } |
| 113 | }); |
| 114 | mHandleFadeAnimator.start(); |
| 115 | } |
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 | |
| 120 | public void onDragEnd() { |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 121 | if (!mDragAndDropEnabled) return; |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 122 | if (mActive) mActive = false; |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 123 | |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 124 | // Fade out this icon |
| 125 | if (mFadeAnimator != null) mFadeAnimator.cancel(); |
| 126 | mFadeAnimator = ObjectAnimator.ofFloat(this, "alpha", 0.0f); |
| 127 | mFadeAnimator.setDuration(sFadeOutAnimationDuration); |
| 128 | mFadeAnimator.addListener(new AnimatorListener() { |
| 129 | public void onAnimationStart(Animator animation) {} |
| 130 | public void onAnimationRepeat(Animator animation) {} |
| 131 | public void onAnimationEnd(Animator animation) { |
| 132 | onEndOrCancel(); |
| 133 | } |
| 134 | public void onAnimationCancel(Animator animation) { |
| 135 | onEndOrCancel(); |
| 136 | } |
| 137 | private void onEndOrCancel() { |
| 138 | setVisibility(GONE); |
| 139 | mFadeAnimator = null; |
| 140 | } |
| 141 | }); |
| 142 | mFadeAnimator.start(); |
| 143 | |
| 144 | // Fade in the handle |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 145 | if (mHandle != null) { |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 146 | if (mHandleFadeAnimator != null) mHandleFadeAnimator.cancel(); |
| 147 | mHandleFadeAnimator = ObjectAnimator.ofFloat(mHandle, "alpha", 1.0f); |
| 148 | mHandleFadeAnimator.setDuration(sFadeInAnimationDuration); |
| 149 | mHandleFadeAnimator.start(); |
Michael Jurka | 466810b | 2010-10-25 13:41:02 -0700 | [diff] [blame] | 150 | mHandle.setVisibility(VISIBLE); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 153 | } |