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; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame^] | 20 | import com.android.launcher2.DropTarget.DragObject; |
Michael Jurka | 800242b | 2010-12-16 11:39:26 -0800 | [diff] [blame] | 21 | |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 22 | import android.animation.Animator; |
Michael Jurka | 800242b | 2010-12-16 11:39:26 -0800 | [diff] [blame] | 23 | import android.animation.AnimatorSet; |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 24 | import android.animation.ObjectAnimator; |
Michael Jurka | 800242b | 2010-12-16 11:39:26 -0800 | [diff] [blame] | 25 | import android.animation.Animator.AnimatorListener; |
Patrick Dubroy | bc6840b | 2010-09-01 11:54:27 -0700 | [diff] [blame] | 26 | import android.content.ComponentName; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 27 | import android.content.Context; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 28 | import android.graphics.PorterDuff; |
| 29 | import android.graphics.PorterDuffColorFilter; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 30 | import android.util.AttributeSet; |
Michael Jurka | 800242b | 2010-12-16 11:39:26 -0800 | [diff] [blame] | 31 | import android.view.View; |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 32 | |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Implements a DropTarget which allows applications to be dropped on it, |
| 35 | * in order to launch the application info for that app. |
| 36 | */ |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 37 | public class ApplicationInfoDropTarget extends IconDropTarget { |
| 38 | private static final int sFadeInAnimationDuration = 200; |
| 39 | private static final int sFadeOutAnimationDuration = 100; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 40 | |
Michael Jurka | 800242b | 2010-12-16 11:39:26 -0800 | [diff] [blame] | 41 | private AnimatorSet mFadeAnimator; |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 42 | |
| 43 | public ApplicationInfoDropTarget(Context context, AttributeSet attrs) { |
| 44 | this(context, attrs, 0); |
| 45 | } |
| 46 | |
| 47 | public ApplicationInfoDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 48 | super(context, attrs, defStyle); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 49 | |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 50 | // Set the hover paint colour |
| 51 | int colour = getContext().getResources().getColor(R.color.app_info_filter); |
| 52 | mHoverPaint.setColorFilter(new PorterDuffColorFilter(colour, PorterDuff.Mode.SRC_ATOP)); |
Winson Chung | be5212b | 2010-12-20 11:36:33 -0800 | [diff] [blame] | 53 | |
Michael Jurka | a2eb170 | 2011-05-12 14:57:05 -0700 | [diff] [blame] | 54 | if (LauncherApplication.isScreenLarge()) { |
Winson Chung | 59e1f9a | 2010-12-21 11:31:54 -0800 | [diff] [blame] | 55 | // For the application info drop target, we just ignore the left padding since we don't want |
| 56 | // to overlap with the delete zone padding |
| 57 | int tb = getResources().getDimensionPixelSize( |
| 58 | R.dimen.delete_zone_vertical_drag_padding); |
| 59 | int lr = getResources().getDimensionPixelSize( |
| 60 | R.dimen.delete_zone_horizontal_drag_padding); |
| 61 | setDragPadding(tb, lr, tb, 0); |
| 62 | } |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame^] | 65 | public boolean acceptDrop(DragObject d) { |
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; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame^] | 72 | if (d.dragInfo instanceof ApplicationInfo) { |
| 73 | componentName = ((ApplicationInfo) d.dragInfo).componentName; |
| 74 | } else if (d.dragInfo instanceof ShortcutInfo) { |
| 75 | componentName = ((ShortcutInfo) d.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 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame^] | 81 | public void onDragEnter(DragObject d) { |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 82 | if (!mDragAndDropEnabled) return; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame^] | 83 | d.dragView.setPaint(mHoverPaint); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame^] | 86 | public void onDragExit(DragObject d) { |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 87 | if (!mDragAndDropEnabled) return; |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame^] | 88 | d.dragView.setPaint(null); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | public void onDragStart(DragSource source, Object info, int dragAction) { |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 92 | if (info != null && mDragAndDropEnabled) { |
Michael Jurka | 774bd37 | 2010-10-22 13:40:50 -0700 | [diff] [blame] | 93 | final int itemType = ((ItemInfo)info).itemType; |
| 94 | mActive = (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION); |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 95 | if (mActive) { |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 96 | // Fade in this icon |
| 97 | if (mFadeAnimator != null) mFadeAnimator.cancel(); |
Michael Jurka | 800242b | 2010-12-16 11:39:26 -0800 | [diff] [blame] | 98 | mFadeAnimator = new AnimatorSet(); |
| 99 | Animator infoButtonAnimator = ObjectAnimator.ofFloat(this, "alpha", 0.0f, 1.0f); |
| 100 | infoButtonAnimator.setDuration(sFadeInAnimationDuration); |
| 101 | |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 102 | mFadeAnimator.play(infoButtonAnimator); |
| 103 | |
Adam Cohen | cdc30d5 | 2010-12-01 15:09:47 -0800 | [diff] [blame] | 104 | setVisibility(VISIBLE); |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 105 | |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 106 | // Fade out the overlapping views |
| 107 | if (mOverlappingViews != null) { |
| 108 | for (View view : mOverlappingViews) { |
| 109 | ObjectAnimator oa = ObjectAnimator.ofFloat(view, "alpha", 0.0f); |
| 110 | oa.setDuration(sFadeOutAnimationDuration); |
| 111 | mFadeAnimator.play(oa); |
| 112 | } |
| 113 | mFadeAnimator.addListener(new AnimatorListener() { |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 114 | public void onAnimationStart(Animator animation) {} |
| 115 | public void onAnimationRepeat(Animator animation) {} |
| 116 | public void onAnimationEnd(Animator animation) { |
| 117 | onEndOrCancel(); |
| 118 | } |
| 119 | public void onAnimationCancel(Animator animation) { |
| 120 | onEndOrCancel(); |
| 121 | } |
| 122 | private void onEndOrCancel() { |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 123 | for (View view : mOverlappingViews) { |
| 124 | view.setVisibility(INVISIBLE); |
| 125 | } |
| 126 | mFadeAnimator = null; |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 127 | } |
| 128 | }); |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 129 | } |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 130 | mFadeAnimator.start(); |
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); |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 144 | mFadeAnimator.addListener(new AnimatorListener() { |
| 145 | public void onAnimationStart(Animator animation) {} |
| 146 | public void onAnimationRepeat(Animator animation) {} |
| 147 | public void onAnimationEnd(Animator animation) { |
| 148 | onEndOrCancel(); |
| 149 | } |
| 150 | public void onAnimationCancel(Animator animation) { |
| 151 | onEndOrCancel(); |
| 152 | } |
| 153 | private void onEndOrCancel() { |
| 154 | setVisibility(GONE); |
| 155 | mFadeAnimator = null; |
| 156 | } |
| 157 | }); |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 158 | mFadeAnimator.play(infoButtonAnimator); |
Winson Chung | 760e537 | 2010-12-15 13:14:23 -0800 | [diff] [blame] | 159 | |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 160 | // Fade in the overlapping views |
| 161 | if (mOverlappingViews != null) { |
| 162 | for (View view : mOverlappingViews) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 163 | // Check whether the views are enabled first, before trying to fade them in |
| 164 | if (view.isEnabled()) { |
| 165 | ObjectAnimator oa = ObjectAnimator.ofFloat(view, "alpha", 1.0f); |
| 166 | oa.setDuration(sFadeInAnimationDuration); |
| 167 | mFadeAnimator.play(oa); |
| 168 | view.setVisibility(VISIBLE); |
| 169 | } |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 170 | } |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 171 | } |
Michael Jurka | b8e1447 | 2010-12-20 16:06:10 -0800 | [diff] [blame] | 172 | mFadeAnimator.start(); |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 173 | } |
Patrick Dubroy | 4ed6278 | 2010-08-17 15:11:18 -0700 | [diff] [blame] | 174 | } |