3
0

galaxys2: advanced settings: fix jumping vibrator strength

Change-Id: I20b24aba4b5f3deffd3e627c43da0429d4b04d79
This commit is contained in:
Daniel Hillenbrand 2013-06-10 18:41:00 +02:00
parent b895376dfd
commit b48b30b3f1

View File

@ -40,8 +40,7 @@ import java.lang.Math;
import java.text.DecimalFormat; import java.text.DecimalFormat;
/** /**
* Special preference type that allows configuration of vibrator intensity settings on Sony * Special preference type that allows configuration of vibrator intensity settings on Samsung devices
* Devices
*/ */
public class VibratorTuningPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener { public class VibratorTuningPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener {
private static final String TAG = "DeviceSettings_Vibrator"; private static final String TAG = "DeviceSettings_Vibrator";
@ -52,19 +51,18 @@ public class VibratorTuningPreference extends DialogPreference implements SeekBa
private static int DEFAULT_VALUE; private static int DEFAULT_VALUE;
private static int MIN_VALUE; private static int MIN_VALUE;
private Context mContext;
private SeekBar mSeekBar; private SeekBar mSeekBar;
private TextView mValue; private TextView mValue;
private TextView mWarning; private TextView mWarning;
private String mOriginalValue; private String mOriginalValue;
private int mOriginalPercent;
private Drawable mProgressDrawable; private Drawable mProgressDrawable;
private Drawable mProgressThumb; private Drawable mProgressThumb;
private LightingColorFilter mRedFilter; private LightingColorFilter mRedFilter;
public VibratorTuningPreference(Context context, AttributeSet attrs) { public VibratorTuningPreference(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
mContext = context;
FILE_PATH = context.getResources().getString(R.string.vibrator_sysfs_file); FILE_PATH = context.getResources().getString(R.string.vibrator_sysfs_file);
MAX_VALUE = Integer.valueOf(context.getResources().getString(R.string.intensity_max_value)); MAX_VALUE = Integer.valueOf(context.getResources().getString(R.string.intensity_max_value));
@ -104,11 +102,15 @@ public class VibratorTuningPreference extends DialogPreference implements SeekBa
mRedFilter = new LightingColorFilter(Color.BLACK, mRedFilter = new LightingColorFilter(Color.BLACK,
getContext().getResources().getColor(android.R.color.holo_red_light)); getContext().getResources().getColor(android.R.color.holo_red_light));
// Read the current value from sysfs in case user wants to dismiss his changes
mOriginalValue = Utils.readOneLine(FILE_PATH); mOriginalValue = Utils.readOneLine(FILE_PATH);
mOriginalPercent = strengthToPercent(Integer.parseInt(mOriginalValue));
// Restore percent value from SharedPreferences object
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
int percent = settings.getInt("percent", strengthToPercent(DEFAULT_VALUE));
mSeekBar.setOnSeekBarChangeListener(this); mSeekBar.setOnSeekBarChangeListener(this);
mSeekBar.setProgress(Integer.valueOf(mOriginalPercent)); mSeekBar.setProgress(Integer.valueOf(percent));
} }
@Override @Override
@ -132,13 +134,13 @@ public class VibratorTuningPreference extends DialogPreference implements SeekBa
super.onDialogClosed(positiveResult); super.onDialogClosed(positiveResult);
if (positiveResult) { if (positiveResult) {
Editor editor = getEditor(); // Store percent value in SharedPreferences object
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
int strength = percentToStrength(mSeekBar.getProgress()); SharedPreferences.Editor editor = settings.edit();
editor.putString(FILE_PATH, String.valueOf(strength)); editor.putInt("percent", mSeekBar.getProgress());
editor.commit(); editor.commit();
} else { } else {
Utils.writeValue(FILE_PATH, String.valueOf(mOriginalPercent)); Utils.writeValue(FILE_PATH, String.valueOf(mOriginalValue));
} }
} }
@ -149,13 +151,15 @@ public class VibratorTuningPreference extends DialogPreference implements SeekBa
return; return;
} }
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); MAX_VALUE = Integer.valueOf(context.getResources().getString(R.string.intensity_max_value));
final String value = prefs.getString(FILE_PATH, null); DEFAULT_VALUE = Integer.valueOf(context.getResources().getString(R.string.intensity_default_value));
MIN_VALUE = Integer.valueOf(context.getResources().getString(R.string.intensity_min_value));
if (value != null) { SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
Log.d(TAG, "Restoring vibration setting: " + value); int strength = percentToStrength(settings.getInt("percent", strengthToPercent(DEFAULT_VALUE)));
Utils.writeValue(FILE_PATH, value);
} Log.d(TAG, "Restoring vibration setting: " + strength);
Utils.writeValue(FILE_PATH, String.valueOf(strength));
} }
public static boolean isSupported(String filePath) { public static boolean isSupported(String filePath) {