summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/shortcuts/DeepShortcutView.java
blob: 7997d1e2e971ea41bc4e8c8832fccd4d2f00d717 (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
/*
 * Copyright (C) 2016 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.launcher3.shortcuts;

import android.content.Context;
import android.support.annotation.IntDef;
import android.util.AttributeSet;

import com.android.launcher3.BubbleTextView;
import com.android.launcher3.R;

/**
 * A {@link BubbleTextView} that represents a deep shortcut within an app.
 */
public class DeepShortcutView extends BubbleTextView {

    private static final float HOVER_SCALE = 1.1f;
    // The direction this view should translate when animating the hover state.
    // This allows hovered shortcuts to "push" other shortcuts away.
    @IntDef({DIRECTION_UP, DIRECTION_NONE, DIRECTION_DOWN})
    public @interface TranslationDirection {}

    public static final int DIRECTION_UP = -1;
    public static final int DIRECTION_NONE = 0;
    public static final int DIRECTION_DOWN = 1;
    @TranslationDirection
    private int mTranslationDirection = DIRECTION_NONE;

    private int mSpacing;
    private int mTop;
    private boolean mIsHoveringOver = false;

    public DeepShortcutView(Context context) {
        this(context, null, 0);
    }

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

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

        mSpacing = getResources().getDimensionPixelSize(R.dimen.deep_shortcuts_spacing);
    }

    public int getSpacing() {
        return mSpacing;
    }

    /**
     * Updates the state of this view based on touches over the container before user lifts finger.
     *
     * @param containerContainsTouch whether the {@link DeepShortcutsContainer} this shortcut
     *                               is inside contains the current touch
     * @param isBelowHoveredShortcut whether a sibling shortcut before this one in the
     *                               view hierarchy is being hovered over
     * @param touchY the y coordinate of the touch, relative to the {@link DeepShortcutsContainer}
     *               this shortcut is inside
     * @return whether this shortcut is being hovered over
     */
    public boolean updateHoverState(boolean containerContainsTouch, boolean isBelowHoveredShortcut,
            float touchY) {
        if (!containerContainsTouch) {
            mIsHoveringOver = false;
            mTranslationDirection = DIRECTION_NONE;
        } else if (isBelowHoveredShortcut) {
            mIsHoveringOver = false;
            mTranslationDirection = DIRECTION_DOWN;
        } else {
            // Include space around the view when determining hover state to avoid gaps.
            mTop = (int) (getY() - getTranslationY());
            mIsHoveringOver = (touchY >= mTop - mSpacing / 2)
                    && (touchY < mTop + getHeight() + mSpacing / 2);
            mTranslationDirection = mIsHoveringOver ? DIRECTION_NONE : DIRECTION_UP;
        }
        animateHoverState();
        return mIsHoveringOver;
    }

    /**
     * If this shortcut is being hovered over, we scale it up. If another shortcut is being hovered
     * over, we translate this one away from it to account for its increased size.
     *
     * TODO: apply motion spec here
     */
    private void animateHoverState() {
        float scale = mIsHoveringOver ? HOVER_SCALE : 1f;
        setScaleX(scale);
        setScaleY(scale);

        float translation = (HOVER_SCALE - 1f) * getHeight();
        setTranslationY(translation * mTranslationDirection);
    }

    public boolean isHoveringOver() {
        return mIsHoveringOver;
    }
}