summaryrefslogtreecommitdiffstats
path: root/packages/SystemUI/src/com/android/systemui/statusbar/SpeedBumpDotsLayout.java
blob: ddf5215c4ca60ff8e38c244e3b08083e9ec484d1 (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
/*
 * Copyright (C) 2014 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.systemui.statusbar;

import android.animation.TimeAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import com.android.systemui.R;

/**
 * A layout with a certain number of dots which are integrated in the
 * {@link com.android.systemui.statusbar.SpeedBumpView}
 */
public class SpeedBumpDotsLayout extends ViewGroup {

    private static final float DOT_CLICK_ANIMATION_LENGTH = 300;
    private final int mDotSize;
    private final SpeedBumpDotsAlgorithm mAlgorithm = new SpeedBumpDotsAlgorithm(getContext());
    private final SpeedBumpDotsState mCurrentState = new SpeedBumpDotsState(this);
    private boolean mIsCurrentlyVisible = true;
    private final ValueAnimator mClickAnimator;
    private float mAnimationProgress;
    private ValueAnimator.AnimatorUpdateListener mClickUpdateListener
            = new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            mAnimationProgress = animation.getAnimatedFraction();
            updateChildren();
        }
    };

    public SpeedBumpDotsLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mDotSize = getResources().getDimensionPixelSize(R.dimen.speed_bump_dots_height);
        createDots(context, attrs);
        mClickAnimator = TimeAnimator.ofFloat(0, DOT_CLICK_ANIMATION_LENGTH);
        mClickAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        mClickAnimator.addUpdateListener(mClickUpdateListener);
    }

    private void createDots(Context context, AttributeSet attrs) {
        SpeedBumpDotView blueDot = new SpeedBumpDotView(context, attrs);
        blueDot.setColor(getResources().getColor(R.color.speed_bump_dot_blue));
        addView(blueDot);

        SpeedBumpDotView redDot = new SpeedBumpDotView(context, attrs);
        redDot.setColor(getResources().getColor(R.color.speed_bump_dot_red));
        addView(redDot);

        SpeedBumpDotView yellowDot = new SpeedBumpDotView(context, attrs);
        yellowDot.setColor(getResources().getColor(R.color.speed_bump_dot_yellow));
        addView(yellowDot);

        SpeedBumpDotView greenDot = new SpeedBumpDotView(context, attrs);
        greenDot.setColor(getResources().getColor(R.color.speed_bump_dot_green));
        addView(greenDot);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int childWidthSpec = MeasureSpec.makeMeasureSpec(mDotSize,
                MeasureSpec.getMode(widthMeasureSpec));
        int childHeightSpec = MeasureSpec.makeMeasureSpec(mDotSize,
                MeasureSpec.getMode(heightMeasureSpec));
        measureChildren(childWidthSpec, childHeightSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            child.layout(0, 0, mDotSize, mDotSize);
        }
        if (changed) {
            updateChildren();
        }
    }

    private void updateChildren() {
        mAlgorithm.getState(mCurrentState);
        mCurrentState.apply();
    }

    public void performVisibilityAnimation(boolean visible) {
        if (mClickAnimator.isRunning()) {
            mClickAnimator.cancel();
        }
        mIsCurrentlyVisible = visible;
        mAlgorithm.getState(mCurrentState);
        mCurrentState.animateToState();
    }

    public void setInvisible() {
        mIsCurrentlyVisible = false;
        mAlgorithm.getState(mCurrentState);
        mCurrentState.apply();
    }

    public boolean isCurrentlyVisible() {
        return mIsCurrentlyVisible;
    }

    public void performDotClickAnimation() {
        if (mClickAnimator.isRunning()) {
            // don't perform an animation if it's running already
            return;
        }
        mClickAnimator.start();
    }


    public float getAnimationProgress() {
        return mAnimationProgress;
    }
}