summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher2/ApplicationInfoDropTarget.java
blob: f42aaf535fa272305d21b5dbfa25fcb00275493c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.launcher2;

import com.android.launcher.R;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.Animator.AnimatorListener;
import android.content.ComponentName;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.util.AttributeSet;
import android.view.View;

/**
 * Implements a DropTarget which allows applications to be dropped on it,
 * in order to launch the application info for that app.
 */
public class ApplicationInfoDropTarget extends IconDropTarget {
    private static final int sFadeInAnimationDuration = 200;
    private static final int sFadeOutAnimationDuration = 100;

    private AnimatorSet mFadeAnimator;

    public ApplicationInfoDropTarget(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ApplicationInfoDropTarget(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        // Set the hover paint colour
        int colour = getContext().getResources().getColor(R.color.app_info_filter);
        mHoverPaint.setColorFilter(new PorterDuffColorFilter(colour, PorterDuff.Mode.SRC_ATOP));

        if (LauncherApplication.isScreenLarge()) {
            // For the application info drop target, we just ignore the left padding since we don't want
            // to overlap with the delete zone padding
            int tb = getResources().getDimensionPixelSize(
                    R.dimen.delete_zone_vertical_drag_padding);
            int lr = getResources().getDimensionPixelSize(
                    R.dimen.delete_zone_horizontal_drag_padding);
            setDragPadding(tb, lr, tb, 0);
        }
    }

    public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset,
            DragView dragView, Object dragInfo) {
        // acceptDrop is called just before onDrop. We do the work here, rather than
        // in onDrop, because it allows us to reject the drop (by returning false)
        // so that the object being dragged isn't removed from the home screen.
        if (getVisibility() != VISIBLE) return false;

        ComponentName componentName = null;
        if (dragInfo instanceof ApplicationInfo) {
            componentName = ((ApplicationInfo)dragInfo).componentName;
        } else if (dragInfo instanceof ShortcutInfo) {
            componentName = ((ShortcutInfo)dragInfo).intent.getComponent();
        }
        mLauncher.startApplicationDetailsActivity(componentName);
        return false;
    }

    public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset,
            DragView dragView, Object dragInfo) {
        if (!mDragAndDropEnabled) return;
        dragView.setPaint(mHoverPaint);
    }

    public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset,
            DragView dragView, Object dragInfo) {
        if (!mDragAndDropEnabled) return;
        dragView.setPaint(null);
    }

    public void onDragStart(DragSource source, Object info, int dragAction) {
        if (info != null && mDragAndDropEnabled) {
            final int itemType = ((ItemInfo)info).itemType;
            mActive = (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION);
            if (mActive) {
                // Fade in this icon
                if (mFadeAnimator != null) mFadeAnimator.cancel();
                mFadeAnimator = new AnimatorSet();
                Animator infoButtonAnimator = ObjectAnimator.ofFloat(this, "alpha", 0.0f, 1.0f);
                infoButtonAnimator.setDuration(sFadeInAnimationDuration);

                mFadeAnimator.play(infoButtonAnimator);

                setVisibility(VISIBLE);

                // Fade out the overlapping views
                if (mOverlappingViews != null) {
                    for (View view : mOverlappingViews) {
                        ObjectAnimator oa = ObjectAnimator.ofFloat(view, "alpha", 0.0f);
                        oa.setDuration(sFadeOutAnimationDuration);
                        mFadeAnimator.play(oa);
                    }
                    mFadeAnimator.addListener(new AnimatorListener() {
                        public void onAnimationStart(Animator animation) {}
                        public void onAnimationRepeat(Animator animation) {}
                        public void onAnimationEnd(Animator animation) {
                            onEndOrCancel();
                        }
                        public void onAnimationCancel(Animator animation) {
                            onEndOrCancel();
                        }
                        private void onEndOrCancel() {
                            for (View view : mOverlappingViews) {
                                view.setVisibility(INVISIBLE);
                            }
                            mFadeAnimator = null;
                        }
                    });
                }
                mFadeAnimator.start();
            }
        }
    }

    public void onDragEnd() {
        if (!mDragAndDropEnabled) return;
        if (mActive) mActive = false;

        // Fade out this icon
        if (mFadeAnimator != null) mFadeAnimator.cancel();
        mFadeAnimator = new AnimatorSet();
        Animator infoButtonAnimator = ObjectAnimator.ofFloat(this, "alpha", 0.0f);
        infoButtonAnimator.setDuration(sFadeOutAnimationDuration);
        mFadeAnimator.addListener(new AnimatorListener() {
            public void onAnimationStart(Animator animation) {}
            public void onAnimationRepeat(Animator animation) {}
            public void onAnimationEnd(Animator animation) {
                onEndOrCancel();
            }
            public void onAnimationCancel(Animator animation) {
                onEndOrCancel();
            }
            private void onEndOrCancel() {
                setVisibility(GONE);
                mFadeAnimator = null;
            }
        });
        mFadeAnimator.play(infoButtonAnimator);

        // Fade in the overlapping views
        if (mOverlappingViews != null) {
            for (View view : mOverlappingViews) {
                // Check whether the views are enabled first, before trying to fade them in
                if (view.isEnabled()) {
                    ObjectAnimator oa = ObjectAnimator.ofFloat(view, "alpha", 1.0f);
                    oa.setDuration(sFadeInAnimationDuration);
                    mFadeAnimator.play(oa);
                    view.setVisibility(VISIBLE);
                }
            }
        }
        mFadeAnimator.start();
    }
}