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 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.res.ColorStateList; 22 import android.content.res.Configuration; 23 import android.content.res.Resources; 24 import android.graphics.drawable.TransitionDrawable; 25 import android.os.UserHandle; 26 import android.util.AttributeSet; 27 import android.view.View; 28 import android.view.ViewGroup; 29 30 import com.android.launcher.R; 31 32 public class InfoDropTarget extends ButtonDropTarget { 33 34 private ColorStateList mOriginalTextColor; 35 private TransitionDrawable mDrawable; 36 InfoDropTarget(Context context, AttributeSet attrs)37 public InfoDropTarget(Context context, AttributeSet attrs) { 38 this(context, attrs, 0); 39 } 40 InfoDropTarget(Context context, AttributeSet attrs, int defStyle)41 public InfoDropTarget(Context context, AttributeSet attrs, int defStyle) { 42 super(context, attrs, defStyle); 43 } 44 45 @Override onFinishInflate()46 protected void onFinishInflate() { 47 super.onFinishInflate(); 48 49 mOriginalTextColor = getTextColors(); 50 51 // Get the hover color 52 Resources r = getResources(); 53 mHoverColor = r.getColor(R.color.info_target_hover_tint); 54 mDrawable = (TransitionDrawable) getCurrentDrawable(); 55 if (null != mDrawable) { 56 mDrawable.setCrossFadeEnabled(true); 57 } 58 59 // Remove the text in the Phone UI in landscape 60 int orientation = getResources().getConfiguration().orientation; 61 if (orientation == Configuration.ORIENTATION_LANDSCAPE) { 62 if (!LauncherApplication.isScreenLarge()) { 63 setText(""); 64 } 65 } 66 } 67 isFromAllApps(DragSource source)68 private boolean isFromAllApps(DragSource source) { 69 return (source instanceof AppsCustomizePagedView); 70 } 71 72 @Override acceptDrop(DragObject d)73 public boolean acceptDrop(DragObject d) { 74 // acceptDrop is called just before onDrop. We do the work here, rather than 75 // in onDrop, because it allows us to reject the drop (by returning false) 76 // so that the object being dragged isn't removed from the drag source. 77 ComponentName componentName = null; 78 if (d.dragInfo instanceof ApplicationInfo) { 79 componentName = ((ApplicationInfo) d.dragInfo).componentName; 80 } else if (d.dragInfo instanceof ShortcutInfo) { 81 componentName = ((ShortcutInfo) d.dragInfo).intent.getComponent(); 82 } else if (d.dragInfo instanceof PendingAddItemInfo) { 83 componentName = ((PendingAddItemInfo) d.dragInfo).componentName; 84 } 85 final UserHandle user; 86 if (d.dragInfo instanceof ItemInfo) { 87 user = ((ItemInfo) d.dragInfo).user; 88 } else { 89 user = android.os.Process.myUserHandle(); 90 } 91 if (componentName != null) { 92 mLauncher.startApplicationDetailsActivity(componentName, user); 93 } 94 95 // There is no post-drop animation, so clean up the DragView now 96 d.deferDragViewCleanupPostAnimation = false; 97 return false; 98 } 99 100 @Override onDragStart(DragSource source, Object info, int dragAction)101 public void onDragStart(DragSource source, Object info, int dragAction) { 102 boolean isVisible = true; 103 104 // Hide this button unless we are dragging something from AllApps 105 if (!isFromAllApps(source)) { 106 isVisible = false; 107 } 108 109 mActive = isVisible; 110 mDrawable.resetTransition(); 111 setTextColor(mOriginalTextColor); 112 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE); 113 } 114 115 @Override onDragEnd()116 public void onDragEnd() { 117 super.onDragEnd(); 118 mActive = false; 119 } 120 onDragEnter(DragObject d)121 public void onDragEnter(DragObject d) { 122 super.onDragEnter(d); 123 124 mDrawable.startTransition(mTransitionDuration); 125 setTextColor(mHoverColor); 126 } 127 onDragExit(DragObject d)128 public void onDragExit(DragObject d) { 129 super.onDragExit(d); 130 131 if (!d.dragComplete) { 132 mDrawable.resetTransition(); 133 setTextColor(mOriginalTextColor); 134 } 135 } 136 } 137