• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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.inputmethod.latin;
18 
19 import android.app.AlertDialog;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.content.res.TypedArray;
23 import android.preference.DialogPreference;
24 import android.util.AttributeSet;
25 import android.view.View;
26 import android.widget.SeekBar;
27 import android.widget.TextView;
28 
29 public final class SeekBarDialogPreference extends DialogPreference
30         implements SeekBar.OnSeekBarChangeListener {
31     public interface ValueProxy {
readValue(final String key)32         public int readValue(final String key);
readDefaultValue(final String key)33         public int readDefaultValue(final String key);
writeValue(final int value, final String key)34         public void writeValue(final int value, final String key);
feedbackValue(final int value)35         public void feedbackValue(final int value);
36     }
37 
38     private final int mValueFormatResId;
39     private final int mMaxValue;
40     private final int mMinValue;
41     private final int mStepValue;
42 
43     private TextView mValueView;
44     private SeekBar mSeekBar;
45 
46     private ValueProxy mValueProxy;
47 
SeekBarDialogPreference(final Context context, final AttributeSet attrs)48     public SeekBarDialogPreference(final Context context, final AttributeSet attrs) {
49         super(context, attrs);
50         final TypedArray a = context.obtainStyledAttributes(
51                 attrs, R.styleable.SeekBarDialogPreference, 0, 0);
52         mValueFormatResId = a.getResourceId(R.styleable.SeekBarDialogPreference_valueFormatText, 0);
53         mMaxValue = a.getInt(R.styleable.SeekBarDialogPreference_maxValue, 0);
54         mMinValue = a.getInt(R.styleable.SeekBarDialogPreference_minValue, 0);
55         mStepValue = a.getInt(R.styleable.SeekBarDialogPreference_stepValue, 0);
56         a.recycle();
57         setDialogLayoutResource(R.layout.seek_bar_dialog);
58     }
59 
setInterface(final ValueProxy proxy)60     public void setInterface(final ValueProxy proxy) {
61         mValueProxy = proxy;
62         setSummary(getValueText(clipValue(proxy.readValue(getKey()))));
63     }
64 
getValueText(final int value)65     private String getValueText(final int value) {
66         if (mValueFormatResId == 0) {
67             return Integer.toString(value);
68         } else {
69             return getContext().getString(mValueFormatResId, value);
70         }
71     }
72 
73     @Override
onCreateDialogView()74     protected View onCreateDialogView() {
75         final View view = super.onCreateDialogView();
76         mSeekBar = (SeekBar)view.findViewById(R.id.seek_bar_dialog_bar);
77         mSeekBar.setMax(mMaxValue - mMinValue);
78         mSeekBar.setOnSeekBarChangeListener(this);
79         mValueView = (TextView)view.findViewById(R.id.seek_bar_dialog_value);
80         return view;
81     }
82 
getProgressFromValue(final int value)83     private int getProgressFromValue(final int value) {
84         return value - mMinValue;
85     }
86 
getValueFromProgress(final int progress)87     private int getValueFromProgress(final int progress) {
88         return progress + mMinValue;
89     }
90 
clipValue(final int value)91     private int clipValue(final int value) {
92         final int clippedValue = Math.min(mMaxValue, Math.max(mMinValue, value));
93         if (mStepValue <= 1) {
94             return clippedValue;
95         }
96         return clippedValue - (clippedValue % mStepValue);
97     }
98 
getClippedValueFromProgress(final int progress)99     private int getClippedValueFromProgress(final int progress) {
100         return clipValue(getValueFromProgress(progress));
101     }
102 
setValue(final int value, final boolean fromUser)103     private void setValue(final int value, final boolean fromUser) {
104         mValueView.setText(getValueText(value));
105         if (!fromUser) {
106             mSeekBar.setProgress(getProgressFromValue(value));
107         }
108     }
109 
110     @Override
onBindDialogView(final View view)111     protected void onBindDialogView(final View view) {
112         setValue(clipValue(mValueProxy.readValue(getKey())), false /* fromUser */);
113     }
114 
115     @Override
onPrepareDialogBuilder(final AlertDialog.Builder builder)116     protected void onPrepareDialogBuilder(final AlertDialog.Builder builder) {
117         builder.setPositiveButton(android.R.string.ok, this)
118             .setNegativeButton(android.R.string.cancel, this)
119             .setNeutralButton(R.string.button_default, this);
120     }
121 
122     @Override
onClick(final DialogInterface dialog, final int which)123     public void onClick(final DialogInterface dialog, final int which) {
124         super.onClick(dialog, which);
125         if (which == DialogInterface.BUTTON_NEUTRAL) {
126             setValue(clipValue(mValueProxy.readDefaultValue(getKey())), false /* fromUser */);
127         }
128         if (which != DialogInterface.BUTTON_NEGATIVE) {
129             setSummary(mValueView.getText());
130             mValueProxy.writeValue(getClippedValueFromProgress(mSeekBar.getProgress()), getKey());
131         }
132     }
133 
134     @Override
onProgressChanged(final SeekBar seekBar, final int progress, final boolean fromUser)135     public void onProgressChanged(final SeekBar seekBar, final int progress,
136             final boolean fromUser) {
137         setValue(getClippedValueFromProgress(progress), fromUser);
138     }
139 
140     @Override
onStartTrackingTouch(final SeekBar seekBar)141     public void onStartTrackingTouch(final SeekBar seekBar) {}
142 
143     @Override
onStopTrackingTouch(final SeekBar seekBar)144     public void onStopTrackingTouch(final SeekBar seekBar) {
145         mValueProxy.feedbackValue(getClippedValueFromProgress(seekBar.getProgress()));
146     }
147 }
148