summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/AppDrawerScrubber.java
blob: dff02661aa89739d325378cc4717fff4d8c19595 (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
/*
 * Copyright (C) 2013 The CyanogenMod 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;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

public class AppDrawerScrubber extends LinearLayout {

    private final int SCRUBBER_INDICATOR_DISPLAY_DURATION = 200;
    private final float SCRUBBER_INDICATOR_DISPLAY_TRANSLATIONY = 20f;

    private AppDrawerListAdapter mAdapter;
    private RecyclerView mListView;
    private TextView mScrubberIndicator;
    private SeekBar mSeekBar;
    private String[] mSections;
    private LinearLayoutManager mLayoutManager;

    public AppDrawerScrubber(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AppDrawerScrubber(Context context) {
        super(context);
        init(context);
    }

    public void updateSections() {
        mSections = (String[]) mAdapter.getSections();
        mSeekBar.setMax(mSections.length - 1);
    }

    public void setSource(RecyclerView listView) {
        mListView = listView;
        mAdapter = (AppDrawerListAdapter) listView.getAdapter();
        mLayoutManager = (LinearLayoutManager) listView.getLayoutManager();
    }

    public void setScrubberIndicator(TextView scrubberIndicator) {
        mScrubberIndicator = scrubberIndicator;
    }

    private boolean isReady() {
        return mListView != null &&
                mAdapter != null &&
                mSections != null;
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.scrub_layout, this);
        mSeekBar = (SeekBar) findViewById(R.id.scrubber);

        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, final int progress, boolean fromUser) {
                if (!isReady()) {
                    return;
                }
                resetScrubber();

                String section = String.valueOf(mSections[progress]);

                if (mScrubberIndicator != null) {
                    float translateX = (progress * seekBar.getWidth()) / mSections.length;
                    translateX -= (mScrubberIndicator.getWidth() / 6); // offset for alignment
                    mScrubberIndicator.setTranslationX(translateX);
                    mScrubberIndicator.setText(section);
                }

                mLayoutManager.scrollToPositionWithOffset(
                        mAdapter.getPositionForSection(progress), 0);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                resetScrubber();
                if (mScrubberIndicator != null) {
                    mScrubberIndicator.setAlpha(1f);
                    mScrubberIndicator.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                resetScrubber();
                if (mScrubberIndicator != null) {
                    mScrubberIndicator.animate().alpha(0f).translationYBy(20f)
                            .setDuration(200).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            mScrubberIndicator.setVisibility(View.INVISIBLE);
                        }
                    });
                }
            }

            private void resetScrubber() {
                if (mScrubberIndicator != null) {
                    mScrubberIndicator.animate().cancel();
                    mScrubberIndicator.setTranslationY(0f);
                }
            }
        });
    }
}