• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 org.drrickorang.loopback;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.widget.EditText;
22 import android.widget.LinearLayout;
23 import android.widget.SeekBar;
24 import android.widget.TextView;
25 
26 public class SettingsPicker extends LinearLayout implements SeekBar.OnSeekBarChangeListener,
27         CatchEventsEditText.EditTextEventListener {
28 
29     protected TextView mTitleTextView;
30     protected CatchEventsEditText mValueEditText;
31     protected SeekBar mValueSeekBar;
32     protected SettingChangeListener mSettingsChangeListener;
33 
34     protected int mMinimumValue;
35     protected int mMaximumValue;
36 
37     public interface SettingChangeListener {
settingChanged(int value)38         public void settingChanged(int value);
39     }
40 
SettingsPicker(Context context, AttributeSet attrs)41     public SettingsPicker(Context context, AttributeSet attrs) {
42         super(context, attrs);
43 
44         inflate(context, R.layout.settings_picker, this);
45 
46         mTitleTextView = (TextView) findViewById(R.id.settings_title);
47         mValueEditText = (CatchEventsEditText) findViewById(R.id.settings_valueText);
48         mValueSeekBar = (SeekBar) findViewById(R.id.settings_seekbar);
49 
50         mValueEditText.setEditTextEvenListener(this);
51         mValueSeekBar.setOnSeekBarChangeListener(this);
52     }
53 
setMinMaxDefault(int min, int max, int def)54     public void setMinMaxDefault(int min, int max, int def) {
55         mMinimumValue = min;
56         mMaximumValue = max;
57         mValueSeekBar.setMax(max - min);
58         setValue(def);
59     }
60 
setTitle(String title)61     public void setTitle(String title) {
62         mTitleTextView.setText(title);
63     }
64 
setValue(int value)65     public void setValue(int value) {
66         mValueSeekBar.setProgress(value - mMinimumValue);
67         mValueEditText.setText(Integer.toString(value));
68     }
69 
setSettingsChangeListener(SettingChangeListener settingsChangeListener)70     public void setSettingsChangeListener(SettingChangeListener settingsChangeListener) {
71         mSettingsChangeListener = settingsChangeListener;
72     }
73 
textChanged(int value)74     protected void textChanged(int value) {
75         mValueSeekBar.setProgress(value - mMinimumValue);
76         if (mSettingsChangeListener != null) {
77             mSettingsChangeListener.settingChanged(value);
78         }
79     }
80 
sliderChanged(int value, boolean userInteractionFinished)81     protected void sliderChanged(int value, boolean userInteractionFinished) {
82         mValueEditText.setText(Integer.toString(value));
83         if (userInteractionFinished && mSettingsChangeListener != null) {
84             mSettingsChangeListener.settingChanged(value);
85         }
86     }
87 
88     @Override
textEdited(EditText v)89     public void textEdited(EditText v) {
90         if (!v.getText().toString().isEmpty()) {
91             int value;
92             try {
93                 value = Integer.parseInt(v.getText().toString());
94             } catch (NumberFormatException e) {
95                 value = mMinimumValue;
96                 v.setText(Integer.toString(value));
97             }
98             if (value < mMinimumValue) {
99                 value = mMinimumValue;
100                 v.setText(Integer.toString(value));
101             } else if (value > mMaximumValue) {
102                 value = mMaximumValue;
103                 v.setText(Integer.toString(value));
104             }
105             textChanged(value);
106         } else {
107             sliderChanged(mMinimumValue + mValueSeekBar.getProgress(), false);
108         }
109     }
110 
111     @Override
onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)112     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
113         if (fromUser) {
114             sliderChanged(mMinimumValue + progress, false);
115         }
116     }
117 
118     @Override
onStartTrackingTouch(SeekBar seekBar)119     public void onStartTrackingTouch(SeekBar seekBar) {
120 
121     }
122 
123     @Override
onStopTrackingTouch(SeekBar seekBar)124     public void onStopTrackingTouch(SeekBar seekBar) {
125         sliderChanged(mMinimumValue + seekBar.getProgress(), true);
126     }
127 
128     @Override
setEnabled(boolean enabled)129     public void setEnabled(boolean enabled) {
130         mValueEditText.setEnabled(enabled);
131         mValueSeekBar.setEnabled(enabled);
132     }
133 }
134