• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 
17 package com.android.emergency.widgets.slider;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.widget.FrameLayout;
24 import android.widget.SeekBar;
25 import android.widget.TextView;
26 
27 import com.android.emergency.R;
28 
29 /**
30  * A slider button. User can slide the thumb/scrim from one end to the other end to activate the
31  * button.
32  */
33 public final class SliderView extends FrameLayout
34         implements SeekBar.OnSeekBarChangeListener {
35 
36     private SliderSeekBar mSliderSeekBar;
37     private View mSliderTextContainer;
38     private TextView mSliderTextView;
39 
SliderView(Context context)40     public SliderView(Context context) {
41         super(context);
42         init(null, 0);
43     }
44 
SliderView(Context context, AttributeSet attrs)45     public SliderView(Context context, AttributeSet attrs) {
46         super(context, attrs);
47         init(attrs, 0);
48     }
49 
SliderView(Context context, AttributeSet attrs, int defStyleAttr)50     public SliderView(Context context, AttributeSet attrs, int defStyleAttr) {
51         super(context, attrs, defStyleAttr);
52         init(attrs, defStyleAttr);
53     }
54 
SliderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)55     public SliderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
56         super(context, attrs, defStyleAttr, defStyleRes);
57         init(attrs, defStyleAttr);
58     }
59 
init(AttributeSet attrs, int defStyle)60     private void init(AttributeSet attrs, int defStyle) {
61         inflate(getContext(), R.layout.slider_view, this);
62         mSliderTextView = findViewById(android.R.id.title);
63         mSliderSeekBar = findViewById(R.id.slider_seek_bar);
64         mSliderTextContainer = findViewById(R.id.slider_text_container);
65         mSliderSeekBar.setOnSeekBarChangeListener(this);
66 
67         String slideText;
68 
69         TypedArray a =
70                 getContext()
71                         .getTheme()
72                         .obtainStyledAttributes(attrs, R.styleable.SliderView, defStyle, defStyle);
73 
74         slideText = a.getString(R.styleable.SliderView_android_text);
75 
76         if (a.hasValue(R.styleable.SliderView_android_thumb)) {
77             // Updating thumb drawable clears the offset. Cache it first.
78             int thumbOffSet = mSliderSeekBar.getThumbOffset();
79             mSliderSeekBar.setThumb(a.getDrawable(R.styleable.SliderView_android_thumb));
80             mSliderSeekBar.setThumbOffset(thumbOffSet);
81         }
82 
83         setText(slideText);
84         a.recycle();
85     }
86 
setText(CharSequence text)87     public void setText(CharSequence text) {
88         mSliderTextView.setText(text);
89     }
90 
setSlideCompleteListener(OnSlideCompleteListener listener)91     public void setSlideCompleteListener(OnSlideCompleteListener listener) {
92         mSliderSeekBar.setOnSlideCompleteListener(listener);
93     }
94 
95     @Override
onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)96     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
97         // Dim text as slider makes progress
98         mSliderTextContainer.setAlpha(1 - (progress / (float) SliderSeekBar.SLIDE_MAX_SLIDE_SCALE));
99     }
100 
101     @Override
onStartTrackingTouch(SeekBar seekBar)102     public void onStartTrackingTouch(SeekBar seekBar) {
103     }
104 
105     @Override
onStopTrackingTouch(SeekBar seekBar)106     public void onStopTrackingTouch(SeekBar seekBar) {
107     }
108 }