blob: 3c36361aa8772212cc5bf3ba04edcec154abea38 [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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung4c98d922011-05-31 16:50:48 -070018
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;
Winson Chung967289b2011-06-30 18:09:30 -070024import android.graphics.drawable.TransitionDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070025import android.util.AttributeSet;
26import android.view.View;
Winson Chunga6427b12011-07-27 10:53:39 -070027import android.view.ViewGroup;
Winson Chung4c98d922011-05-31 16:50:48 -070028
Kenny Guyf07af7b2014-07-31 11:39:16 +010029import com.android.launcher3.compat.UserHandleCompat;
30
Winson Chung61fa4192011-06-12 15:15:29 -070031public class InfoDropTarget extends ButtonDropTarget {
Winson Chung4c98d922011-05-31 16:50:48 -070032
Winson Chunga62e9fd2011-07-11 15:20:48 -070033 private ColorStateList mOriginalTextColor;
Winson Chung967289b2011-06-30 18:09:30 -070034 private TransitionDrawable mDrawable;
Winson Chung4c98d922011-05-31 16:50:48 -070035
36 public InfoDropTarget(Context context, AttributeSet attrs) {
37 this(context, attrs, 0);
38 }
39
40 public InfoDropTarget(Context context, AttributeSet attrs, int defStyle) {
41 super(context, attrs, defStyle);
42 }
43
44 @Override
45 protected void onFinishInflate() {
46 super.onFinishInflate();
47
Winson Chunga6427b12011-07-27 10:53:39 -070048 mOriginalTextColor = getTextColors();
Winson Chung201bc822011-06-20 15:41:53 -070049
Winson Chung4c98d922011-05-31 16:50:48 -070050 // Get the hover color
51 Resources r = getResources();
Winson Chung4c98d922011-05-31 16:50:48 -070052 mHoverColor = r.getColor(R.color.info_target_hover_tint);
Winson Chung947245b2012-05-15 16:34:19 -070053 mDrawable = (TransitionDrawable) getCurrentDrawable();
Adam Cohen18bbc6a2014-06-03 21:43:24 -070054
55 if (mDrawable == null) {
56 // TODO: investigate why this is ever happening. Presently only on one known device.
Adam Cohen17937012014-06-03 22:23:28 -070057 mDrawable = (TransitionDrawable) r.getDrawable(R.drawable.info_target_selector);
Adam Cohen18bbc6a2014-06-03 21:43:24 -070058 setCompoundDrawablesRelativeWithIntrinsicBounds(mDrawable, null, null, null);
59 }
60
Fabrice Di Megliocc11f742012-12-18 16:25:49 -080061 if (null != mDrawable) {
62 mDrawable.setCrossFadeEnabled(true);
63 }
Winson Chung201bc822011-06-20 15:41:53 -070064
65 // Remove the text in the Phone UI in landscape
66 int orientation = getResources().getConfiguration().orientation;
67 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
Daniel Sandlere4f98912013-06-25 15:13:26 -040068 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chunga6427b12011-07-27 10:53:39 -070069 setText("");
Winson Chung201bc822011-06-20 15:41:53 -070070 }
71 }
Winson Chung4c98d922011-05-31 16:50:48 -070072 }
73
Winson Chung4c98d922011-05-31 16:50:48 -070074 @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.
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080079
80 startDetailsActivityForInfo(d.dragInfo, mLauncher);
81 // There is no post-drop animation, so clean up the DragView now
82 d.deferDragViewCleanupPostAnimation = false;
83 return false;
84 }
85
86 public static void startDetailsActivityForInfo(Object info, Launcher launcher) {
Winson Chung4c98d922011-05-31 16:50:48 -070087 ComponentName componentName = null;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080088 if (info instanceof AppInfo) {
89 componentName = ((AppInfo) info).componentName;
90 } else if (info instanceof ShortcutInfo) {
91 componentName = ((ShortcutInfo) info).intent.getComponent();
92 } else if (info instanceof PendingAddItemInfo) {
93 componentName = ((PendingAddItemInfo) info).componentName;
Winson Chung4c98d922011-05-31 16:50:48 -070094 }
Kenny Guyf07af7b2014-07-31 11:39:16 +010095 final UserHandleCompat user;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080096 if (info instanceof ItemInfo) {
97 user = ((ItemInfo) info).user;
Kenny Guyf07af7b2014-07-31 11:39:16 +010098 } else {
99 user = UserHandleCompat.myUserHandle();
100 }
101
Winson Chung4c98d922011-05-31 16:50:48 -0700102 if (componentName != null) {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800103 launcher.startApplicationDetailsActivity(componentName, user);
Winson Chung4c98d922011-05-31 16:50:48 -0700104 }
Winson Chung4c98d922011-05-31 16:50:48 -0700105 }
106
107 @Override
108 public void onDragStart(DragSource source, Object info, int dragAction) {
Winson Chung4c98d922011-05-31 16:50:48 -0700109 boolean isVisible = true;
110
Winson Chung11a49372012-04-27 15:12:38 -0700111 // Hide this button unless we are dragging something from AllApps
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000112 if (!source.supportsAppInfoDropTarget()) {
Winson Chung4c98d922011-05-31 16:50:48 -0700113 isVisible = false;
114 }
115
116 mActive = isVisible;
Adam Cohene9a51192014-06-04 01:24:03 +0000117 mDrawable.resetTransition();
Winson Chunga6427b12011-07-27 10:53:39 -0700118 setTextColor(mOriginalTextColor);
119 ((ViewGroup) getParent()).setVisibility(isVisible ? View.VISIBLE : View.GONE);
Winson Chung4c98d922011-05-31 16:50:48 -0700120 }
121
122 @Override
123 public void onDragEnd() {
124 super.onDragEnd();
125 mActive = false;
126 }
127
128 public void onDragEnter(DragObject d) {
129 super.onDragEnter(d);
130
Adam Cohene9a51192014-06-04 01:24:03 +0000131 mDrawable.startTransition(mTransitionDuration);
Winson Chunga6427b12011-07-27 10:53:39 -0700132 setTextColor(mHoverColor);
Winson Chung4c98d922011-05-31 16:50:48 -0700133 }
134
135 public void onDragExit(DragObject d) {
136 super.onDragExit(d);
137
Winson Chungaaa530a2011-07-11 21:06:30 -0700138 if (!d.dragComplete) {
139 mDrawable.resetTransition();
Winson Chunga6427b12011-07-27 10:53:39 -0700140 setTextColor(mOriginalTextColor);
Winson Chungaaa530a2011-07-11 21:06:30 -0700141 }
Winson Chung4c98d922011-05-31 16:50:48 -0700142 }
143}