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