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; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 41 | |
| 42 | public ApplicationInfoDropTarget(Context context, AttributeSet attrs) { |
| 43 | this(context, attrs, 0); |
| 44 | } |
| 45 | |
| 46 | public ApplicationInfoDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 47 | super(context, attrs, defStyle); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 48 | |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 49 | // Set the hover paint colour |
| 50 | int colour = getContext().getResources().getColor(R.color.app_info_filter); |
| 51 | mHoverPaint.setColorFilter(new PorterDuffColorFilter(colour, PorterDuff.Mode.SRC_ATOP)); |
Winson Chung | be5212b | 2010-12-20 11:36:33 -0800 | [diff] [blame] | 52 | |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 53 | if (LauncherApplication.isScreenXLarge()) { |
| 54 | // For the application info drop target, we just ignore the left padding since we don't want |
| 55 | // to overlap with the delete zone padding |
| 56 | int tb = getResources().getDimensionPixelSize( |
| 57 | R.dimen.delete_zone_vertical_drag_padding); |
| 58 | int lr = getResources().getDimensionPixelSize( |
| 59 | R.dimen.delete_zone_horizontal_drag_padding); |
| 60 | setDragPadding(tb, lr, tb, 0); |
| 61 | } |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, |
| 65 | DragView dragView, Object dragInfo) { |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 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. |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 69 | if (getVisibility() != VISIBLE) return false; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 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 onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, |
| 82 | DragView dragView, Object dragInfo) { |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 83 | if (!mDragAndDropEnabled) return; |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 84 | dragView.setPaint(mHoverPaint); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, |
| 88 | DragView dragView, Object dragInfo) { |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 89 | if (!mDragAndDropEnabled) return; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 90 | dragView.setPaint(null); |
| 91 | } |
| 92 | |
| 93 | public void onDragStart(DragSource source, Object info, int dragAction) { |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 94 | if (info != null && mDragAndDropEnabled) { |
Michael Jurka | 774bd37 | 2010-10-22 13:40:50 -0700 | [diff] [blame] | 95 | final int itemType = ((ItemInfo)info).itemType; |
| 96 | mActive = (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION); |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 97 | if (mActive) { |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 98 | // Fade in this icon |
| 99 | if (mFadeAnimator != null) mFadeAnimator.cancel(); |
Michael Jurka | 800242b | 2010-12-16 11:39:26 -0800 | [diff] [blame] | 100 | mFadeAnimator = new AnimatorSet(); |
| 101 | Animator infoButtonAnimator = ObjectAnimator.ofFloat(this, "alpha", 0.0f, 1.0f); |
| 102 | infoButtonAnimator.setDuration(sFadeInAnimationDuration); |
| 103 | |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 104 | mFadeAnimator.play(infoButtonAnimator); |
| 105 | |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 106 | setVisibility(VISIBLE); |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 107 | |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 108 | // Fade out the overlapping views |
| 109 | if (mOverlappingViews != null) { |
| 110 | for (View view : mOverlappingViews) { |
| 111 | ObjectAnimator oa = ObjectAnimator.ofFloat(view, "alpha", 0.0f); |
| 112 | oa.setDuration(sFadeOutAnimationDuration); |
| 113 | mFadeAnimator.play(oa); |
| 114 | } |
| 115 | mFadeAnimator.addListener(new AnimatorListener() { |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 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() { |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 125 | for (View view : mOverlappingViews) { |
| 126 | view.setVisibility(INVISIBLE); |
| 127 | } |
| 128 | mFadeAnimator = null; |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 129 | } |
| 130 | }); |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 131 | } |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 132 | mFadeAnimator.start(); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 133 | } |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | public void onDragEnd() { |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 138 | if (!mDragAndDropEnabled) return; |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 139 | if (mActive) mActive = false; |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 140 | |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 141 | // Fade out this icon |
| 142 | if (mFadeAnimator != null) mFadeAnimator.cancel(); |
Michael Jurka | 800242b | 2010-12-16 11:39:26 -0800 | [diff] [blame] | 143 | mFadeAnimator = new AnimatorSet(); |
| 144 | Animator infoButtonAnimator = ObjectAnimator.ofFloat(this, "alpha", 0.0f); |
| 145 | infoButtonAnimator.setDuration(sFadeOutAnimationDuration); |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 146 | mFadeAnimator.addListener(new AnimatorListener() { |
| 147 | public void onAnimationStart(Animator animation) {} |
| 148 | public void onAnimationRepeat(Animator animation) {} |
| 149 | public void onAnimationEnd(Animator animation) { |
| 150 | onEndOrCancel(); |
| 151 | } |
| 152 | public void onAnimationCancel(Animator animation) { |
| 153 | onEndOrCancel(); |
| 154 | } |
| 155 | private void onEndOrCancel() { |
| 156 | setVisibility(GONE); |
| 157 | mFadeAnimator = null; |
| 158 | } |
| 159 | }); |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 160 | mFadeAnimator.play(infoButtonAnimator); |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 161 | |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 162 | // Fade in the overlapping views |
| 163 | if (mOverlappingViews != null) { |
| 164 | for (View view : mOverlappingViews) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame^] | 165 | // Check whether the views are enabled first, before trying to fade them in |
| 166 | if (view.isEnabled()) { |
| 167 | ObjectAnimator oa = ObjectAnimator.ofFloat(view, "alpha", 1.0f); |
| 168 | oa.setDuration(sFadeInAnimationDuration); |
| 169 | mFadeAnimator.play(oa); |
| 170 | view.setVisibility(VISIBLE); |
| 171 | } |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 172 | } |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 173 | } |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 174 | mFadeAnimator.start(); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 175 | } |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 176 | } |