Update sensor settings

This commit is contained in:
Jimmy Shih
2013-01-17 17:41:50 -08:00
parent 7ad1c4fdf2
commit ab1d6d8582
5 changed files with 95 additions and 179 deletions
@@ -52,7 +52,7 @@ public class AbstractSettingsActivity extends PreferenceActivity {
preferenceManager.setSharedPreferencesMode(Context.MODE_PRIVATE);
// Set up automatic preferences backup
backupPreferencesListener = new BackupPreferencesListener(this);
backupPreferencesListener = new BackupPreferencesListener(this);
preferenceManager.getSharedPreferences()
.registerOnSharedPreferenceChangeListener(backupPreferencesListener);
@@ -79,15 +79,15 @@ public class AbstractSettingsActivity extends PreferenceActivity {
/**
* Configures a list preference.
*
* @param listPreference the preference
* @param listPreference the list preference
* @param summary the summary array
* @param options the options array
* @param values the values array
* @param value the value
* @param listener optional listener
*/
protected void configureListPreference(ListPreference listPreference,
final String[] summary, final String[] options, final String[] values, String value,
protected void configureListPreference(ListPreference listPreference, final String[] summary,
final String[] options, final String[] values, String value,
final OnPreferenceChangeListener listener) {
listPreference.setEntryValues(values);
listPreference.setEntries(options);
@@ -106,95 +106,17 @@ public class AbstractSettingsActivity extends PreferenceActivity {
listener.onPreferenceChange(listPreference, value);
}
}
/**
* Configures a preference.
*
* @param preference the preference
* @param options the list of displayed options
* @param values the list of stored values
* @param summaryId the summary id
* @param value the stored value
* @param listener listener to invoke
*/
@Deprecated
protected void configurePreference(final Preference preference, final String[] options,
final String[] values, final int summaryId, String value,
final OnPreferenceChangeListener listener) {
if (options != null) {
((ListPreference) preference).setEntries(options);
}
if (values != null) {
((ListPreference) preference).setEntryValues(values);
}
preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference pref, Object newValue) {
updatePreferenceSummary(pref, options, values, summaryId, (String) newValue);
if (listener != null) {
listener.onPreferenceChange(pref, newValue);
}
return true;
}
});
updatePreferenceSummary(preference, options, values, summaryId, value);
if (listener != null) {
listener.onPreferenceChange(preference, value);
}
}
/**
* Updates a preference when a stored value changes.
*
* @param preference the preference
* @param options the list of displayed options
* @param values the list of stored values
* @param summaryId the summary id
* @param value the stored value
*/
@Deprecated
private void updatePreferenceSummary(
Preference preference, String[] options, String[] values, int summaryId, String value) {
String option;
if (options != null && values != null) {
option = getOption(options, values, value);
if (option == null) {
option = getString(R.string.value_unknown);
}
} else {
option = value != null && value.length() != 0 ? value : getString(R.string.value_unknown);
}
String summary = getString(summaryId, option);
preference.setSummary(summary);
}
/**
* Gets the display option for a stored value.
*
* @param options the list of the display options
* @param values the list of the stored values
* @param value the store value
*/
@Deprecated
private String getOption(String[] options, String[] values, String value) {
for (int i = 0; i < values.length; i++) {
if (value.equals(values[i])) {
return options[i];
}
}
return null;
}
/**
* Update the preference summary.
*
* @param preference the preference
* @param summary the summary array
* @param values the value array
* @param values the values array
* @param value the value
*/
private void updatePreferenceSummary(
Preference preference, String[] summary, String[] values, String value) {
Preference preference, String[] summary, String[] values, String value) {
int index = getIndex(values, value);
if (index == -1) {
preference.setSummary(R.string.value_unknown);
@@ -49,23 +49,8 @@ public class SensorSettingsActivity extends AbstractSettingsActivity {
addPreferencesFromResource(R.xml.sensor_settings);
boolean hasAntSupport = AntInterface.hasAntSupport(this);
ListPreference sensorTypeListPreference = (ListPreference) findPreference(
getString(R.string.sensor_type_key));
String[] sensorTypeOptions = getResources().getStringArray(
hasAntSupport ? R.array.sensor_type_all_options : R.array.sensor_type_bluetooth_options);
String[] sensorTypeValues = getResources().getStringArray(
hasAntSupport ? R.array.sensor_type_all_values : R.array.sensor_type_bluetooth_values);
String sensorTypeValue = PreferencesUtils.getString(
this, R.string.sensor_type_key, PreferencesUtils.SENSOR_TYPE_DEFAULT);
OnPreferenceChangeListener listener = new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
updateUiBySensorType((String) newValue);
return true;
}
};
configurePreference(sensorTypeListPreference, sensorTypeOptions, sensorTypeValues,
R.string.settings_sensor_type_summary, sensorTypeValue, listener);
configSensorType(hasAntSupport);
findPreference(getString(R.string.settings_sensor_bluetooth_pairing_key))
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@@ -84,6 +69,31 @@ public class SensorSettingsActivity extends AbstractSettingsActivity {
}
}
private void configSensorType(boolean hasAntSupport) {
ListPreference preference = (ListPreference) findPreference(
getString(R.string.sensor_type_key));
String value = PreferencesUtils.getString(
this, R.string.sensor_type_key, PreferencesUtils.SENSOR_TYPE_DEFAULT);
String[] options = getResources().getStringArray(
hasAntSupport ? R.array.sensor_type_all_options : R.array.sensor_type_bluetooth_options);
String[] values = getResources().getStringArray(
hasAntSupport ? R.array.sensor_type_all_values : R.array.sensor_type_bluetooth_values);
if (!hasAntSupport && value.equals(R.string.sensor_type_value_ant)) {
value = PreferencesUtils.SENSOR_TYPE_DEFAULT;
PreferencesUtils.setString(this, R.string.sensor_type_key, value);
}
OnPreferenceChangeListener listener = new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference pref, Object newValue) {
updateUiBySensorType((String) newValue);
return true;
}
};
configureListPreference(preference, options, options, values, value, listener);
}
/**
* Updates the UI based on the sensor type.
*
@@ -120,7 +130,7 @@ public class SensorSettingsActivity extends AbstractSettingsActivity {
preference.setEnabled(enabled);
int deviceId = PreferencesUtils.getInt(this, valueKey, AntSensorManager.WILDCARD);
if (deviceId == AntSensorManager.WILDCARD) {
preference.setSummary(R.string.settings_sensor_not_connected);
preference.setSummary(R.string.settings_sensor_ant_not_connected);
} else {
preference.setSummary(getString(R.string.settings_sensor_ant_paired, deviceId));
}
@@ -128,7 +138,7 @@ public class SensorSettingsActivity extends AbstractSettingsActivity {
@Override
public boolean onPreferenceClick(Preference pref) {
PreferencesUtils.setInt(SensorSettingsActivity.this, valueKey, AntSensorManager.WILDCARD);
pref.setSummary(R.string.settings_sensor_not_connected);
pref.setSummary(R.string.settings_sensor_ant_not_connected);
return true;
}
});
@@ -138,14 +148,20 @@ public class SensorSettingsActivity extends AbstractSettingsActivity {
@Override
protected void onResume() {
super.onResume();
updateBluetoothSensorListPreference();
// Update each time in case the list of bluetooth sensors has changed
configBluetoothSensor();
}
/**
* Updates the bluetooth sensor list preference.
* Configures the bluetooth sensor.
*/
@SuppressWarnings("deprecation")
private void updateBluetoothSensorListPreference() {
private void configBluetoothSensor() {
ListPreference preference = (ListPreference) findPreference(
getString(R.string.bluetooth_sensor_key));
String value = PreferencesUtils.getString(
this, R.string.bluetooth_sensor_key, PreferencesUtils.BLUETOOTH_SENSOR_DEFAULT);
List<String> optionsList = new ArrayList<String>();
List<String> valuesList = new ArrayList<String>();
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
@@ -155,22 +171,17 @@ public class SensorSettingsActivity extends AbstractSettingsActivity {
String[] options = optionsList.toArray(new String[optionsList.size()]);
String[] values = valuesList.toArray(new String[valuesList.size()]);
String bluetoothSensorValue = PreferencesUtils.getString(
this, R.string.bluetooth_sensor_key, PreferencesUtils.BLUETOOTH_SENSOR_DEFAULT);
ListPreference bluetoothSensorListPreference = (ListPreference) findPreference(
getString(R.string.bluetooth_sensor_key));
if (valuesList.size() == 1) {
if (!valuesList.get(0).equals(bluetoothSensorValue)) {
bluetoothSensorValue = valuesList.get(0);
PreferencesUtils.setString(this, R.string.bluetooth_sensor_key, bluetoothSensorValue);
if (!valuesList.get(0).equals(value)) {
value = valuesList.get(0);
PreferencesUtils.setString(this, R.string.bluetooth_sensor_key, value);
}
} else {
if (!valuesList.contains(bluetoothSensorValue)) {
bluetoothSensorValue = PreferencesUtils.BLUETOOTH_SENSOR_DEFAULT;
PreferencesUtils.setString(this, R.string.bluetooth_sensor_key, bluetoothSensorValue);
if (!valuesList.contains(value)) {
value = PreferencesUtils.BLUETOOTH_SENSOR_DEFAULT;
PreferencesUtils.setString(this, R.string.bluetooth_sensor_key, value);
}
}
configurePreference(bluetoothSensorListPreference, options, values,
R.string.settings_sensor_bluetooth_sensor_summary, bluetoothSensorValue, null);
configureListPreference(preference, options, options, values, value, null);
}
}