Merge "Add SeekBarDialogPreference"

This commit is contained in:
Tadashi G. Takaoka 2013-01-18 10:25:13 +00:00 committed by Android (Google) Code Review
commit f420e506a0
8 changed files with 197 additions and 339 deletions

View File

@ -434,4 +434,9 @@
<!-- Enable proximity characters correction. Disabled by default. -->
<attr name="enableProximityCharsCorrection" format="boolean" />
</declare-styleable>
<declare-styleable name="SeekBarDialogPreference">
<attr name="valueFormatText" format="reference" />
<attr name="maxValue" format="integer" />
</declare-styleable>
</resources>

View File

@ -33,6 +33,7 @@
<bool name="config_default_next_word_prediction">true</bool>
<bool name="config_default_sound_enabled">false</bool>
<bool name="config_default_vibration_enabled">true</bool>
<integer name="config_max_vibration_duration">250</integer> <!-- milliseconds -->
<integer name="config_delay_update_suggestions">100</integer>
<integer name="config_delay_update_old_suggestions">300</integer>
<integer name="config_delay_update_shift_state">100</integer>

View File

@ -76,8 +76,8 @@
<!-- Description for delay for dismissing a popup on screen: default value of the delay [CHAR LIMIT=15] -->
<string name="key_preview_popup_dismiss_default_delay">Default</string>
<!-- Units abbreviation for the keypress vibration duration (milliseconds) [CHAR LIMIT=10] -->
<string name="settings_keypress_vibration_duration"><xliff:g id="milliseconds">%s</xliff:g>ms</string>
<!-- Units abbreviation for the duration (milliseconds) [CHAR LIMIT=10] -->
<string name="abbreviation_unit_milliseconds"><xliff:g id="milliseconds">%s</xliff:g>ms</string>
<!-- Option name for enabling or disabling the use of names of people in Contacts for suggestion and correction [CHAR LIMIT=25] -->
<string name="use_contacts_dict">Suggest Contact names</string>

View File

@ -16,6 +16,7 @@
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:latin="http://schemas.android.com/apk/res/com.android.inputmethod.latin"
android:title="@string/english_ime_settings"
android:key="english_ime_settings">
<PreferenceCategory
@ -144,12 +145,15 @@
<ListPreference
android:key="pref_key_preview_popup_dismiss_delay"
android:title="@string/key_preview_popup_dismiss_delay" />
<PreferenceScreen
<com.android.inputmethod.latin.SeekBarDialogPreference
android:key="pref_vibration_duration_settings"
android:title="@string/prefs_keypress_vibration_duration_settings"/>
<PreferenceScreen
android:title="@string/prefs_keypress_vibration_duration_settings"
latin:valueFormatText="@string/abbreviation_unit_milliseconds"
latin:maxValue="@integer/config_max_vibration_duration" />
<com.android.inputmethod.latin.SeekBarDialogPreference
android:key="pref_keypress_sound_volume"
android:title="@string/prefs_keypress_sound_volume_settings" />
android:title="@string/prefs_keypress_sound_volume_settings"
latin:maxValue="100" /> <!-- percent -->
</PreferenceScreen>
<PreferenceScreen
android:key="debug_settings"

View File

@ -29,8 +29,6 @@ import android.view.View;
* complexity of settings and the like.
*/
public final class AudioAndHapticFeedbackManager {
public static final int MAX_KEYPRESS_VIBRATION_DURATION = 250; // millisecond
private AudioManager mAudioManager;
private Vibrator mVibrator;

View File

@ -1,201 +0,0 @@
/*
* Copyright (C) 2012 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.inputmethod.latin;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public final class SeekBarDialog implements DialogInterface.OnClickListener,
OnSeekBarChangeListener {
public interface Listener {
public void onPositiveButtonClick(final SeekBarDialog dialog);
public void onNegativeButtonClick(final SeekBarDialog dialog);
public void onNeutralButtonClick(final SeekBarDialog dialog);
public void onDismiss(final SeekBarDialog dialog);
public void onProgressChanged(final SeekBarDialog dialog);
public void onStartTrackingTouch(final SeekBarDialog dialog);
public void onStopTrackingTouch(final SeekBarDialog dialog);
}
public static class Adapter implements Listener {
@Override
public void onPositiveButtonClick(final SeekBarDialog dialog) {}
@Override
public void onNegativeButtonClick(final SeekBarDialog dialog) {}
@Override
public void onNeutralButtonClick(final SeekBarDialog dialog) {}
@Override
public void onDismiss(final SeekBarDialog dialog) {}
@Override
public void onProgressChanged(final SeekBarDialog dialog) {}
@Override
public void onStartTrackingTouch(final SeekBarDialog dialog) {}
@Override
public void onStopTrackingTouch(final SeekBarDialog dialog) {}
}
private static final Listener EMPTY_ADAPTER = new Adapter();
private final AlertDialog mDialog;
private final Listener mListener;
private final TextView mValueView;
private final SeekBar mSeekBar;
private final String mValueFormat;
private int mValue;
private SeekBarDialog(final Builder builder) {
final AlertDialog.Builder dialogBuilder = builder.mDialogBuilder;
dialogBuilder.setView(builder.mView);
dialogBuilder.setPositiveButton(android.R.string.ok, this);
dialogBuilder.setNegativeButton(android.R.string.cancel, this);
if (builder.mNeutralButtonTextResId != 0) {
dialogBuilder.setNeutralButton(builder.mNeutralButtonTextResId, this);
}
mDialog = dialogBuilder.create();
mListener = (builder.mListener == null) ? EMPTY_ADAPTER : builder.mListener;
mValueView = (TextView)builder.mView.findViewById(R.id.seek_bar_dialog_value);
mSeekBar = (SeekBar)builder.mView.findViewById(R.id.seek_bar_dialog_bar);
mSeekBar.setMax(builder.mMaxValue);
mSeekBar.setOnSeekBarChangeListener(this);
if (builder.mValueFormatResId == 0) {
mValueFormat = "%s";
} else {
mValueFormat = mDialog.getContext().getString(builder.mValueFormatResId);
}
}
public void setValue(final int value, final boolean fromUser) {
mValue = value;
mValueView.setText(String.format(mValueFormat, value));
if (!fromUser) {
mSeekBar.setProgress(value);
}
}
public int getValue() {
return mValue;
}
public CharSequence getValueText() {
return mValueView.getText();
}
public void show() {
mDialog.show();
}
public void dismiss() {
mDialog.dismiss();
}
@Override
public void onClick(final DialogInterface dialog, final int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
mListener.onPositiveButtonClick(this);
break;
case DialogInterface.BUTTON_NEGATIVE:
mListener.onNegativeButtonClick(this);
break;
case DialogInterface.BUTTON_NEUTRAL:
mListener.onNeutralButtonClick(this);
break;
default:
return;
}
mListener.onDismiss(this);
}
@Override
public void onProgressChanged(final SeekBar seekBar, final int progress,
final boolean fromUser) {
setValue(progress, fromUser);
if (fromUser) {
mListener.onProgressChanged(this);
}
}
@Override
public void onStartTrackingTouch(final SeekBar seekBar) {
mListener.onStartTrackingTouch(this);
}
@Override
public void onStopTrackingTouch(final SeekBar seekBar) {
mListener.onStopTrackingTouch(this);
}
public static final class Builder {
final AlertDialog.Builder mDialogBuilder;
final View mView;
int mNeutralButtonTextResId;
int mMaxValue;
int mValueFormatResId;
int mValue;
Listener mListener;
public Builder(final Context context) {
mDialogBuilder = new AlertDialog.Builder(context);
mView = LayoutInflater.from(context).inflate(R.layout.seek_bar_dialog, null);
}
public Builder setTitle(final int resId) {
mDialogBuilder.setTitle(resId);
return this;
}
public Builder setNeutralButtonText(final int resId) {
mNeutralButtonTextResId = resId;
return this;
}
public Builder setMaxValue(final int max) {
mMaxValue = max;
mValue = Math.min(mValue, max);
return this;
}
public Builder setValueFromat(final int resId) {
mValueFormatResId = resId;
return this;
}
public Builder setValue(final int value) {
mValue = Math.min(value, mMaxValue);
return this;
}
public Builder setListener(final Listener listener) {
mListener = listener;
return this;
}
public SeekBarDialog create() {
final SeekBarDialog dialog = new SeekBarDialog(this);
dialog.setValue(mValue, false /* fromUser */);
return dialog;
}
}
}

View File

@ -0,0 +1,123 @@
/*
* Copyright (C) 2013 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.inputmethod.latin;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
public final class SeekBarDialogPreference extends DialogPreference
implements SeekBar.OnSeekBarChangeListener {
public interface ValueProxy {
public int readValue(final String key);
public int readDefaultValue(final String key);
public void writeValue(final int value, final String key);
public void feedbackValue(final int value);
}
private final int mValueFormatResId;
private final int mMaxValue;
private TextView mValueView;
private SeekBar mSeekBar;
private ValueProxy mValueProxy;
public SeekBarDialogPreference(final Context context, final AttributeSet attrs) {
super(context, attrs);
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.SeekBarDialogPreference, 0, 0);
mValueFormatResId = a.getResourceId(R.styleable.SeekBarDialogPreference_valueFormatText, 0);
mMaxValue = a.getInt(R.styleable.SeekBarDialogPreference_maxValue, 0);
a.recycle();
setDialogLayoutResource(R.layout.seek_bar_dialog);
}
public void setInterface(final ValueProxy proxy) {
mValueProxy = proxy;
setSummary(getValueText(proxy.readValue(getKey())));
}
private String getValueText(final int value) {
if (mValueFormatResId == 0) {
return Integer.toString(value);
} else {
return getContext().getString(mValueFormatResId, value);
}
}
@Override
protected View onCreateDialogView() {
final View view = super.onCreateDialogView();
mSeekBar = (SeekBar)view.findViewById(R.id.seek_bar_dialog_bar);
mSeekBar.setMax(mMaxValue);
mSeekBar.setOnSeekBarChangeListener(this);
mValueView = (TextView)view.findViewById(R.id.seek_bar_dialog_value);
return view;
}
private void setValue(final int value, final boolean fromUser) {
mValueView.setText(getValueText(value));
if (!fromUser) {
mSeekBar.setProgress(value);
}
}
@Override
protected void onBindDialogView(final View view) {
setValue(mValueProxy.readValue(getKey()), false /* fromUser */);
}
@Override
protected void onPrepareDialogBuilder(final AlertDialog.Builder builder) {
builder.setPositiveButton(android.R.string.ok, this)
.setNegativeButton(android.R.string.cancel, this)
.setNeutralButton(R.string.button_default, this);
}
@Override
public void onClick(final DialogInterface dialog, final int which) {
super.onClick(dialog, which);
if (which == DialogInterface.BUTTON_NEUTRAL) {
setValue(mValueProxy.readDefaultValue(getKey()), false /* fromUser */);
}
if (which != DialogInterface.BUTTON_NEGATIVE) {
setSummary(mValueView.getText());
mValueProxy.writeValue(mSeekBar.getProgress(), getKey());
}
}
@Override
public void onProgressChanged(final SeekBar seekBar, final int progress,
final boolean fromUser) {
setValue(progress, fromUser);
}
@Override
public void onStartTrackingTouch(final SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(final SeekBar seekBar) {
mValueProxy.feedbackValue(seekBar.getProgress());
}
}

View File

@ -26,7 +26,6 @@ import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen;
import android.view.inputmethod.InputMethodSubtype;
@ -36,8 +35,6 @@ import com.android.inputmethodcommon.InputMethodSettingsFragment;
public final class SettingsFragment extends InputMethodSettingsFragment
implements SharedPreferences.OnSharedPreferenceChangeListener {
private PreferenceScreen mKeypressVibrationDurationSettingsPref;
private PreferenceScreen mKeypressSoundVolumeSettingsPref;
private ListPreference mVoicePreference;
private ListPreference mShowCorrectionSuggestionsPreference;
private ListPreference mAutoCorrectionThresholdPreference;
@ -167,36 +164,8 @@ public final class SettingsFragment extends InputMethodSettingsFragment
getPreferenceScreen().removePreference(gestureTypingSettings);
}
mKeypressVibrationDurationSettingsPref =
(PreferenceScreen) findPreference(Settings.PREF_VIBRATION_DURATION_SETTINGS);
if (mKeypressVibrationDurationSettingsPref != null) {
mKeypressVibrationDurationSettingsPref.setOnPreferenceClickListener(
new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference arg0) {
showKeypressVibrationDurationSettingsDialog();
return true;
}
});
mKeypressVibrationDurationSettingsPref.setSummary(
res.getString(R.string.settings_keypress_vibration_duration,
Settings.readKeypressVibrationDuration(prefs, res)));
}
mKeypressSoundVolumeSettingsPref =
(PreferenceScreen) findPreference(Settings.PREF_KEYPRESS_SOUND_VOLUME);
if (mKeypressSoundVolumeSettingsPref != null) {
mKeypressSoundVolumeSettingsPref.setOnPreferenceClickListener(
new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference arg0) {
showKeypressSoundVolumeSettingDialog();
return true;
}
});
mKeypressSoundVolumeSettingsPref.setSummary(String.valueOf(
getCurrentKeyPressSoundVolumePercent(prefs, res)));
}
setupKeypressVibrationDurationSettings(prefs, res);
setupKeypressSoundVolumeSettings(prefs, res);
refreshEnablingsOfKeypressSoundAndVibrationSettings(prefs, res);
}
@ -287,127 +256,86 @@ public final class SettingsFragment extends InputMethodSettingsFragment
private void refreshEnablingsOfKeypressSoundAndVibrationSettings(
final SharedPreferences sp, final Resources res) {
if (mKeypressVibrationDurationSettingsPref != null) {
final boolean hasVibratorHardware =
AudioAndHapticFeedbackManager.getInstance().hasVibrator();
final boolean vibrateOnByUser = sp.getBoolean(Settings.PREF_VIBRATE_ON,
res.getBoolean(R.bool.config_default_vibration_enabled));
mKeypressVibrationDurationSettingsPref.setEnabled(
hasVibratorHardware && vibrateOnByUser);
}
final boolean hasVibratorHardware =
AudioAndHapticFeedbackManager.getInstance().hasVibrator();
final boolean vibrateOnByUser = sp.getBoolean(Settings.PREF_VIBRATE_ON,
res.getBoolean(R.bool.config_default_vibration_enabled));
setPreferenceEnabled(Settings.PREF_VIBRATION_DURATION_SETTINGS,
hasVibratorHardware && vibrateOnByUser);
if (mKeypressSoundVolumeSettingsPref != null) {
final boolean soundOn = sp.getBoolean(Settings.PREF_SOUND_ON,
res.getBoolean(R.bool.config_default_sound_enabled));
mKeypressSoundVolumeSettingsPref.setEnabled(soundOn);
}
final boolean soundOn = sp.getBoolean(Settings.PREF_SOUND_ON,
res.getBoolean(R.bool.config_default_sound_enabled));
setPreferenceEnabled(Settings.PREF_KEYPRESS_SOUND_VOLUME, soundOn);
}
private void showKeypressVibrationDurationSettingsDialog() {
final SharedPreferences sp = getPreferenceManager().getSharedPreferences();
final Context context = getActivity();
final PreferenceScreen settingsPref = mKeypressVibrationDurationSettingsPref;
final SeekBarDialog.Listener listener = new SeekBarDialog.Adapter() {
private void writePreference(final SharedPreferences sp, final int value) {
sp.edit().putInt(Settings.PREF_VIBRATION_DURATION_SETTINGS, value).apply();
private void setupKeypressVibrationDurationSettings(final SharedPreferences sp,
final Resources res) {
final SeekBarDialogPreference pref = (SeekBarDialogPreference)findPreference(
Settings.PREF_VIBRATION_DURATION_SETTINGS);
if (pref == null) {
return;
}
pref.setInterface(new SeekBarDialogPreference.ValueProxy() {
@Override
public void writeValue(final int value, final String key) {
sp.edit().putInt(key, value).apply();
}
private void feedbackSettingsValue(final int value) {
@Override
public int readValue(final String key) {
return Settings.readKeypressVibrationDuration(sp, res);
}
@Override
public int readDefaultValue(final String key) {
return Settings.readDefaultKeypressVibrationDuration(res);
}
@Override
public void feedbackValue(final int value) {
AudioAndHapticFeedbackManager.getInstance().vibrate(value);
}
@Override
public void onPositiveButtonClick(final SeekBarDialog dialog) {
writePreference(sp, dialog.getValue());
}
@Override
public void onNeutralButtonClick(final SeekBarDialog dialog) {
final int defaultValue =
Settings.readDefaultKeypressVibrationDuration(context.getResources());
dialog.setValue(defaultValue, false /* fromUser */);
writePreference(sp, defaultValue);
}
@Override
public void onDismiss(final SeekBarDialog dialog) {
if (settingsPref != null) {
settingsPref.setSummary(dialog.getValueText());
}
}
@Override
public void onStopTrackingTouch(final SeekBarDialog dialog) {
feedbackSettingsValue(dialog.getValue());
}
};
final int currentMs = Settings.readKeypressVibrationDuration(sp, getResources());
final SeekBarDialog.Builder builder = new SeekBarDialog.Builder(context);
builder.setTitle(R.string.prefs_keypress_vibration_duration_settings)
.setNeutralButtonText(R.string.button_default)
.setListener(listener)
.setMaxValue(AudioAndHapticFeedbackManager.MAX_KEYPRESS_VIBRATION_DURATION)
.setValueFromat(R.string.settings_keypress_vibration_duration)
.setValue(currentMs)
.create()
.show();
});
}
private static final int PERCENT_INT = 100;
private static final float PERCENT_FLOAT = 100.0f;
private void setupKeypressSoundVolumeSettings(final SharedPreferences sp, final Resources res) {
final SeekBarDialogPreference pref = (SeekBarDialogPreference)findPreference(
Settings.PREF_KEYPRESS_SOUND_VOLUME);
if (pref == null) {
return;
}
final AudioManager am = (AudioManager)getActivity().getSystemService(Context.AUDIO_SERVICE);
pref.setInterface(new SeekBarDialogPreference.ValueProxy() {
private static final float PERCENTAGE_FLOAT = 100.0f;
private static int getCurrentKeyPressSoundVolumePercent(final SharedPreferences sp,
final Resources res) {
return (int)(Settings.readKeypressSoundVolume(sp, res) * PERCENT_FLOAT);
}
private void showKeypressSoundVolumeSettingDialog() {
final SharedPreferences sp = getPreferenceManager().getSharedPreferences();
final Context context = getActivity();
final AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
final PreferenceScreen settingsPref = mKeypressSoundVolumeSettingsPref;
final SeekBarDialog.Listener listener = new SeekBarDialog.Adapter() {
private void writePreference(final SharedPreferences sp, final float value) {
sp.edit().putFloat(Settings.PREF_KEYPRESS_SOUND_VOLUME, value).apply();
private float getValueFromPercentage(final int percentage) {
return percentage / PERCENTAGE_FLOAT;
}
private void feedbackSettingsValue(final float value) {
am.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD, value);
private int getPercentageFromValue(final float floatValue) {
return (int)(floatValue * PERCENTAGE_FLOAT);
}
@Override
public void onPositiveButtonClick(final SeekBarDialog dialog) {
writePreference(sp, dialog.getValue() / PERCENT_FLOAT);
public void writeValue(final int value, final String key) {
sp.edit().putFloat(key, getValueFromPercentage(value)).apply();
}
@Override
public void onNeutralButtonClick(final SeekBarDialog dialog) {
final float defaultValue =
Settings.readDefaultKeypressSoundVolume(context.getResources());
dialog.setValue((int)(defaultValue * PERCENT_INT), false /* fromUser */);
writePreference(sp, defaultValue);
public int readValue(final String key) {
return getPercentageFromValue(Settings.readKeypressSoundVolume(sp, res));
}
@Override
public void onDismiss(final SeekBarDialog dialog) {
if (settingsPref != null) {
settingsPref.setSummary(dialog.getValueText());
}
public int readDefaultValue(final String key) {
return getPercentageFromValue(Settings.readDefaultKeypressSoundVolume(res));
}
@Override
public void onStopTrackingTouch(final SeekBarDialog dialog) {
feedbackSettingsValue(dialog.getValue() / PERCENT_FLOAT);
public void feedbackValue(final int value) {
am.playSoundEffect(
AudioManager.FX_KEYPRESS_STANDARD, getValueFromPercentage(value));
}
};
final SeekBarDialog.Builder builder = new SeekBarDialog.Builder(context);
final int currentVolumeInt = getCurrentKeyPressSoundVolumePercent(sp, getResources());
builder.setTitle(R.string.prefs_keypress_sound_volume_settings)
.setNeutralButtonText(R.string.button_default)
.setListener(listener)
.setMaxValue(PERCENT_INT)
.setValue(currentVolumeInt)
.create()
.show();
});
}
}