Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 19 | import android.animation.ObjectAnimator; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.content.res.Resources; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 23 | import android.graphics.Color; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 24 | import android.graphics.PorterDuff; |
| 25 | import android.graphics.PorterDuffColorFilter; |
| 26 | import android.util.AttributeSet; |
| 27 | import android.view.View; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 28 | |
| 29 | import com.android.launcher.R; |
| 30 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 31 | public class InfoDropTarget extends ButtonDropTarget { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 32 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 33 | private int mHoverColor = 0xFF0000FF; |
| 34 | |
| 35 | public InfoDropTarget(Context context, AttributeSet attrs) { |
| 36 | this(context, attrs, 0); |
| 37 | } |
| 38 | |
| 39 | public InfoDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 40 | super(context, attrs, defStyle); |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | protected void onFinishInflate() { |
| 45 | super.onFinishInflate(); |
| 46 | |
| 47 | // Get the hover color |
| 48 | Resources r = getResources(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 49 | mHoverColor = r.getColor(R.color.info_target_hover_tint); |
| 50 | mHoverPaint.setColorFilter(new PorterDuffColorFilter( |
| 51 | mHoverColor, PorterDuff.Mode.SRC_ATOP)); |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 52 | setBackgroundColor(mHoverColor); |
| 53 | getBackground().setAlpha(0); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | private boolean isApplication(Object info) { |
| 57 | if (info instanceof ApplicationInfo) return true; |
| 58 | return (((ItemInfo) info).itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public boolean acceptDrop(DragObject d) { |
| 63 | // acceptDrop is called just before onDrop. We do the work here, rather than |
| 64 | // in onDrop, because it allows us to reject the drop (by returning false) |
| 65 | // so that the object being dragged isn't removed from the drag source. |
| 66 | ComponentName componentName = null; |
| 67 | if (d.dragInfo instanceof ApplicationInfo) { |
| 68 | componentName = ((ApplicationInfo) d.dragInfo).componentName; |
| 69 | } else if (d.dragInfo instanceof ShortcutInfo) { |
| 70 | componentName = ((ShortcutInfo) d.dragInfo).intent.getComponent(); |
| 71 | } |
| 72 | if (componentName != null) { |
| 73 | mLauncher.startApplicationDetailsActivity(componentName); |
| 74 | } |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void onDragStart(DragSource source, Object info, int dragAction) { |
| 80 | ItemInfo item = (ItemInfo) info; |
| 81 | boolean isVisible = true; |
| 82 | |
| 83 | // If we are dragging a widget or shortcut, hide the info target |
| 84 | if (!isApplication(info)) { |
| 85 | isVisible = false; |
| 86 | } |
| 87 | |
| 88 | mActive = isVisible; |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 89 | setVisibility(isVisible ? View.VISIBLE : View.GONE); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void onDragEnd() { |
| 94 | super.onDragEnd(); |
| 95 | mActive = false; |
| 96 | } |
| 97 | |
| 98 | public void onDragEnter(DragObject d) { |
| 99 | super.onDragEnter(d); |
| 100 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 101 | ObjectAnimator anim = ObjectAnimator.ofInt(getBackground(), "alpha", |
| 102 | Color.alpha(mHoverColor)); |
| 103 | anim.setDuration(mTransitionDuration); |
| 104 | anim.start(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | public void onDragExit(DragObject d) { |
| 108 | super.onDragExit(d); |
| 109 | |
Winson Chung | 61fa419 | 2011-06-12 15:15:29 -0700 | [diff] [blame] | 110 | ObjectAnimator anim = ObjectAnimator.ofInt(getBackground(), "alpha", 0); |
| 111 | anim.setDuration(mTransitionDuration); |
| 112 | anim.start(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 113 | } |
| 114 | } |