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
17package com.android.launcher2;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.res.ColorStateList;
22import android.content.res.Configuration;
23import android.content.res.Resources;
24import android.graphics.drawable.TransitionDrawable;
25import android.os.UserHandle;
26import android.util.AttributeSet;
27import android.view.View;
28import android.view.ViewGroup;
29
30import com.android.launcher.R;
31
32public class InfoDropTarget extends ButtonDropTarget {
33
34    private ColorStateList mOriginalTextColor;
35    private TransitionDrawable mDrawable;
36
37    public InfoDropTarget(Context context, AttributeSet attrs) {
38        this(context, attrs, 0);
39    }
40
41    public InfoDropTarget(Context context, AttributeSet attrs, int defStyle) {
42        super(context, attrs, defStyle);
43    }
44
45    @Override
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
68    private boolean isFromAllApps(DragSource source) {
69        return (source instanceof AppsCustomizePagedView);
70    }
71
72    @Override
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
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
116    public void onDragEnd() {
117        super.onDragEnd();
118        mActive = false;
119    }
120
121    public void onDragEnter(DragObject d) {
122        super.onDragEnter(d);
123
124        mDrawable.startTransition(mTransitionDuration);
125        setTextColor(mHoverColor);
126    }
127
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