blob: dba845b04ae65bfc731945db247235f87ee0768d [file] [log] [blame]
Winson Chung4c98d922011-05-31 16:50:48 -07001/*
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
17package com.android.launcher2;
18
19import android.content.ComponentName;
20import android.content.Context;
Winson Chunga62e9fd2011-07-11 15:20:48 -070021import android.content.res.ColorStateList;
Winson Chung201bc822011-06-20 15:41:53 -070022import android.content.res.Configuration;
Winson Chung4c98d922011-05-31 16:50:48 -070023import android.content.res.Resources;
24import android.graphics.PorterDuff;
25import android.graphics.PorterDuffColorFilter;
Winson Chung967289b2011-06-30 18:09:30 -070026import android.graphics.drawable.TransitionDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070027import android.util.AttributeSet;
28import android.view.View;
Winson Chunga6427b12011-07-27 10:53:39 -070029import android.view.ViewGroup;
Winson Chung4c98d922011-05-31 16:50:48 -070030
31import com.android.launcher.R;
32
Winson Chung61fa4192011-06-12 15:15:29 -070033public class InfoDropTarget extends ButtonDropTarget {
Winson Chung4c98d922011-05-31 16:50:48 -070034
Winson Chunga62e9fd2011-07-11 15:20:48 -070035 private ColorStateList mOriginalTextColor;
Winson Chung967289b2011-06-30 18:09:30 -070036 private TransitionDrawable mDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070037 private int mHoverColor = 0xFF0000FF;
38
39 public InfoDropTarget(Context context, AttributeSet attrs) {
40 this(context, attrs, 0);
41 }
42
43 public InfoDropTarget(Context context, AttributeSet attrs, int defStyle) {
44 super(context, attrs, defStyle);
45 }
46
47 @Override
48 protected void onFinishInflate() {
49 super.onFinishInflate();
50
Winson Chunga6427b12011-07-27 10:53:39 -070051 mOriginalTextColor = getTextColors();
Winson Chung201bc822011-06-20 15:41:53 -070052
Winson Chung4c98d922011-05-31 16:50:48 -070053 // Get the hover color
54 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070055 mHoverColor = r.getColor(R.color.info_target_hover_tint);
56 mHoverPaint.setColorFilter(new PorterDuffColorFilter(
57 mHoverColor, PorterDuff.Mode.SRC_ATOP));
Winson Chunga6427b12011-07-27 10:53:39 -070058 mDrawable = (TransitionDrawable) getCompoundDrawables()[0];
Winson Chung967289b2011-06-30 18:09:30 -070059 mDrawable.setCrossFadeEnabled(true);
Winson Chung201bc822011-06-20 15:41:53 -070060
61 // Remove the text in the Phone UI in landscape
62 int orientation = getResources().getConfiguration().orientation;
63 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
64 if (!LauncherApplication.isScreenLarge()) {
Winson Chunga6427b12011-07-27 10:53:39 -070065 setText("");
Winson Chung201bc822011-06-20 15:41:53 -070066 }
67 }
Winson Chung4c98d922011-05-31 16:50:48 -070068 }
69
Winson Chung201bc822011-06-20 15:41:53 -070070 private boolean isAllAppsApplication(DragSource source, Object info) {
71 return (source instanceof AppsCustomizePagedView) && (info instanceof ApplicationInfo);
Winson Chung4c98d922011-05-31 16:50:48 -070072 }
73
74 @Override
75 public boolean acceptDrop(DragObject d) {
76 // acceptDrop is called just before onDrop. We do the work here, rather than
77 // in onDrop, because it allows us to reject the drop (by returning false)
78 // so that the object being dragged isn't removed from the drag source.
79 ComponentName componentName = null;
80 if (d.dragInfo instanceof ApplicationInfo) {
81 componentName = ((ApplicationInfo) d.dragInfo).componentName;
82 } else if (d.dragInfo instanceof ShortcutInfo) {
83 componentName = ((ShortcutInfo) d.dragInfo).intent.getComponent();
84 }
85 if (componentName != null) {
86 mLauncher.startApplicationDetailsActivity(componentName);
87 }
88 return false;
89 }
90
91 @Override
92 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -070093 boolean isVisible = true;
94
95 // If we are dragging a widget or shortcut, hide the info target
Winson Chung201bc822011-06-20 15:41:53 -070096 if (!isAllAppsApplication(source, info)) {
Winson Chung4c98d922011-05-31 16:50:48 -070097 isVisible = false;
98 }
99
100 mActive = isVisible;
Winson Chungaaa530a2011-07-11 21:06:30 -0700101 mDrawable.resetTransition();
Winson Chunga6427b12011-07-27 10:53:39 -0700102 setTextColor(mOriginalTextColor);
103 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
Winson Chung4c98d922011-05-31 16:50:48 -0700104 }
105
106 @Override
107 public void onDragEnd() {
108 super.onDragEnd();
109 mActive = false;
110 }
111
112 public void onDragEnter(DragObject d) {
113 super.onDragEnter(d);
114
Winson Chung967289b2011-06-30 18:09:30 -0700115 mDrawable.startTransition(mTransitionDuration);
Winson Chunga6427b12011-07-27 10:53:39 -0700116 setTextColor(mHoverColor);
Winson Chung4c98d922011-05-31 16:50:48 -0700117 }
118
119 public void onDragExit(DragObject d) {
120 super.onDragExit(d);
121
Winson Chungaaa530a2011-07-11 21:06:30 -0700122 if (!d.dragComplete) {
123 mDrawable.resetTransition();
Winson Chunga6427b12011-07-27 10:53:39 -0700124 setTextColor(mOriginalTextColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700125 }
Winson Chung4c98d922011-05-31 16:50:48 -0700126 }
127}