In Chart View, no longer can use a double buffered

iterator since with pauses and resumes, need to remember
beyond the last two locations.
This commit is contained in:
Jimmy Shih
2012-11-12 18:18:20 -08:00
commit c38a5f3ba1
694 changed files with 66581 additions and 0 deletions
@@ -0,0 +1,168 @@
/*
* Copyright 2012 Google Inc.
*
* 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.google.android.apps.mytracks.settings;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.io.backup.BackupPreferencesListener;
import com.google.android.apps.mytracks.util.ApiAdapterFactory;
import com.google.android.maps.mytracks.R;
import android.content.Context;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.speech.tts.TextToSpeech;
import android.view.MenuItem;
/**
* An abstract activity for all the settings activities.
*
* @author Jimmy Shih
*/
public class AbstractSettingsActivity extends PreferenceActivity {
private BackupPreferencesListener backupPreferencesListener;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setVolumeControlStream(TextToSpeech.Engine.DEFAULT_STREAM);
ApiAdapterFactory.getApiAdapter().configureActionBarHomeAsUp(this);
PreferenceManager preferenceManager = getPreferenceManager();
preferenceManager.setSharedPreferencesName(Constants.SETTINGS_NAME);
preferenceManager.setSharedPreferencesMode(Context.MODE_PRIVATE);
// Set up automatic preferences backup
backupPreferencesListener = new BackupPreferencesListener(this);
preferenceManager.getSharedPreferences()
.registerOnSharedPreferenceChangeListener(backupPreferencesListener);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() != android.R.id.home) {
return super.onOptionsItemSelected(item);
}
finish();
return true;
}
@SuppressWarnings("deprecation")
@Override
protected void onDestroy() {
super.onDestroy();
PreferenceManager preferenceManager = getPreferenceManager();
preferenceManager.getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(backupPreferencesListener);
}
/**
* 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
*/
protected void configurePreference(final Preference preference, final String[] options,
final String[] values, final int summaryId, String value) {
configurePreference(preference, options, values, summaryId, value, null);
}
/**
* 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
*/
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
*/
private void updatePreferenceSummary(
Preference preference, String[] options, String[] values, int summaryId, String value) {
String summary = getString(summaryId);
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);
}
summary += "\n" + 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
*/
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;
}
}
@@ -0,0 +1,149 @@
/*
* Copyright 2012 Google Inc.
*
* 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.google.android.apps.mytracks.settings;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.io.backup.BackupActivity;
import com.google.android.apps.mytracks.io.backup.RestoreChooserActivity;
import com.google.android.apps.mytracks.util.DialogUtils;
import com.google.android.apps.mytracks.util.IntentUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
/**
* An activity for accessing the backup settings.
*
* @author Jimmy Shih
*/
public class BackupSettingsActivity extends AbstractSettingsActivity {
private static final int DIALOG_CONFIRM_RESTORE_ID = 0;
private SharedPreferences sharedPreferences;
private Preference backupPreference;
private Preference restorePreference;
private long recordingTrackId = PreferencesUtils.RECORDING_TRACK_ID_DEFAULT;
/*
* Note that sharedPreferenceChangeListenr cannot be an anonymous inner class.
* Anonymous inner class will get garbage collected.
*/
private final OnSharedPreferenceChangeListener
sharedPreferenceChangeListener = new OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
if (key == null || key.equals(PreferencesUtils.getKey(
BackupSettingsActivity.this, R.string.recording_track_id_key))) {
recordingTrackId = PreferencesUtils.getLong(
BackupSettingsActivity.this, R.string.recording_track_id_key);
}
if (key != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
updateUi();
}
});
}
}
};
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
sharedPreferences = getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
addPreferencesFromResource(R.xml.backup_settings);
backupPreference = findPreference(getString(R.string.settings_backup_now_key));
backupPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = IntentUtils.newIntent(BackupSettingsActivity.this, BackupActivity.class);
startActivity(intent);
return true;
}
});
restorePreference = findPreference(getString(R.string.settings_backup_restore_key));
restorePreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
showDialog(DIALOG_CONFIRM_RESTORE_ID);
return true;
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
if (id != DIALOG_CONFIRM_RESTORE_ID) {
return null;
}
return DialogUtils.createConfirmationDialog(this,
R.string.settings_backup_restore_confirm_message, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = IntentUtils.newIntent(
BackupSettingsActivity.this, RestoreChooserActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
sharedPreferenceChangeListener.onSharedPreferenceChanged(null, null);
}
@Override
protected void onResume() {
super.onResume();
updateUi();
}
@Override
protected void onStop() {
super.onStop();
sharedPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
}
/**
* Updates the UI based on the recording state.
*/
private void updateUi() {
boolean isRecording = recordingTrackId != PreferencesUtils.RECORDING_TRACK_ID_DEFAULT;
backupPreference.setEnabled(!isRecording);
restorePreference.setEnabled(!isRecording);
backupPreference.setSummary(
isRecording ? R.string.settings_not_while_recording : R.string.settings_backup_now_summary);
restorePreference.setSummary(isRecording ? R.string.settings_not_while_recording
: R.string.settings_backup_restore_summary);
}
}
@@ -0,0 +1,54 @@
/*
* Copyright 2012 Google Inc.
*
* 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.google.android.apps.mytracks.settings;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
/**
* An activity for accessing chart settings.
*
* @author Jimmy Shih
*/
public class ChartSettingsActivity extends AbstractSettingsActivity {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
addPreferencesFromResource(R.xml.chart_settings);
}
@Override
protected void onResume() {
super.onResume();
updateUi();
}
@SuppressWarnings("deprecation")
private void updateUi() {
CheckBoxPreference speedCheckBoxPreference = (CheckBoxPreference) findPreference(
getString(R.string.chart_show_speed_key));
boolean reportSpeed = PreferencesUtils.getBoolean(
this, R.string.report_speed_key, PreferencesUtils.REPORT_SPEED_DEFAULT);
speedCheckBoxPreference.setTitle(reportSpeed ? R.string.stats_speed
: R.string.stats_pace);
}
}
@@ -0,0 +1,276 @@
/*
* Copyright 2012 Google Inc.
*
* 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.google.android.apps.mytracks.settings;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.UnitConversions;
import com.google.android.maps.mytracks.R;
import android.app.Dialog;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;
/**
* An activity for accessing map settings.
*
* @author Jimmy Shih
*/
public class MapSettingsActivity extends AbstractSettingsActivity {
private static final String TAG = MapSettingsActivity.class.getSimpleName();
private EditTextPreference slowEditTextPreference;
private EditTextPreference mediumEditTextPreference;
private EditTextPreference percentageEditTextPreference;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
addPreferencesFromResource(R.xml.map_settings);
slowEditTextPreference = (EditTextPreference) findPreference(
getString(R.string.settings_map_slow_display_key));
mediumEditTextPreference = (EditTextPreference) findPreference(
getString(R.string.settings_map_medium_display_key));
percentageEditTextPreference = (EditTextPreference) findPreference(
getString(R.string.settings_map_percentage_display_key));
ListPreference trackColorModeListPreference = (ListPreference) findPreference(
getString(R.string.track_color_mode_key));
OnPreferenceChangeListener listener = new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
updateUiByTrackColorMode((String) newValue);
return true;
}
};
String trackColorModeValue = PreferencesUtils.getString(
this, R.string.track_color_mode_key, PreferencesUtils.TRACK_COLOR_MODE_DEFAULT);
configurePreference(trackColorModeListPreference,
getResources().getStringArray(R.array.track_color_mode_options),
getResources().getStringArray(R.array.track_color_mode_values),
R.string.settings_map_track_color_mode_summary, trackColorModeValue, listener);
configureSpeedEditTextPreference(R.string.track_color_mode_slow_key);
configureSpeedEditTextPreference(R.string.track_color_mode_medium_key);
configurePercentageEditTextPreference();
updatePercentageSummary();
}
/**
* Configures the speed edit text preference.
*
* @param keyId the key id
*/
private void configureSpeedEditTextPreference(final int keyId) {
final EditTextPreference editTextPreference = keyId == R.string.track_color_mode_slow_key ? slowEditTextPreference
: mediumEditTextPreference;
editTextPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
storeSpeedValue(keyId, (String) newValue);
updateSpeedSummary(keyId);
return true;
}
});
editTextPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
int value = getSpeedDisplayValue(keyId);
((EditTextPreference) preference).getEditText().setText(String.valueOf(value));
return true;
}
});
configureImeActionDone(editTextPreference);
}
/**
* Configures the percentage edit text preference.
*/
private void configurePercentageEditTextPreference() {
percentageEditTextPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String displayValue = (String) newValue;
int value;
try {
value = Integer.parseInt(displayValue);
} catch (NumberFormatException e) {
Log.e(TAG, "invalid value " + displayValue);
value = PreferencesUtils.TRACK_COLOR_MODE_PERCENTAGE_DEFAULT;
}
if (value < 0) {
value = PreferencesUtils.TRACK_COLOR_MODE_PERCENTAGE_DEFAULT;
}
PreferencesUtils.setInt(
MapSettingsActivity.this, R.string.track_color_mode_percentage_key, value);
updatePercentageSummary();
return true;
}
});
percentageEditTextPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
int value = PreferencesUtils.getInt(
MapSettingsActivity.this, R.string.track_color_mode_percentage_key,
PreferencesUtils.TRACK_COLOR_MODE_PERCENTAGE_DEFAULT);
((EditTextPreference) preference).getEditText().setText(String.valueOf(value));
return true;
}
});
configureImeActionDone(percentageEditTextPreference);
}
/**
* Configures the IME action done.
*
* @param editTextPreference the edit text preference
*/
private void configureImeActionDone(final EditTextPreference editTextPreference) {
editTextPreference.getEditText()
.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
Dialog dialog = editTextPreference.getDialog();
editTextPreference.onClick(dialog, Dialog.BUTTON_POSITIVE);
dialog.dismiss();
return true;
}
return false;
}
});
}
@Override
protected void onResume() {
super.onResume();
updateSpeedSummary(R.string.track_color_mode_slow_key);
updateSpeedSummary(R.string.track_color_mode_medium_key);
}
/**
* Updates the UI by the track color mode.
*
* @param trackColorMode the track color mode
*/
private void updateUiByTrackColorMode(String trackColorMode) {
boolean isFixedValue = trackColorMode.equals(
getString(R.string.settings_map_track_color_mode_fixed_value));
boolean isDynamicValue = trackColorMode.equals(
getString(R.string.settings_map_track_color_mode_dynamic_value));
slowEditTextPreference.setEnabled(isFixedValue);
mediumEditTextPreference.setEnabled(isFixedValue);
percentageEditTextPreference.setEnabled(isDynamicValue);
}
/**
* Stores the speed value, always in metric units.
*
* @param keyId the key id
* @param displayValue the display value
*/
private void storeSpeedValue(int keyId, String displayValue) {
int maxValue;
int minValue;
int defaultValue;
if (keyId == R.string.track_color_mode_slow_key) {
minValue = 0;
maxValue = PreferencesUtils.getInt(this, R.string.track_color_mode_medium_key,
PreferencesUtils.TRACK_COLOR_MODE_MEDIUM_DEFAULT);
defaultValue = PreferencesUtils.TRACK_COLOR_MODE_SLOW_DEFAULT;
} else {
minValue = PreferencesUtils.getInt(
this, R.string.track_color_mode_slow_key, PreferencesUtils.TRACK_COLOR_MODE_SLOW_DEFAULT);
maxValue = Integer.MAX_VALUE;
defaultValue = PreferencesUtils.TRACK_COLOR_MODE_MEDIUM_DEFAULT;
}
int value;
try {
value = Integer.parseInt(displayValue);
if (!PreferencesUtils.getBoolean(
this, R.string.metric_units_key, PreferencesUtils.METRIC_UNITS_DEFAULT)) {
value = (int) (value * UnitConversions.MI_TO_KM);
}
} catch (NumberFormatException e) {
Log.e(TAG, "invalid value " + displayValue);
value = defaultValue;
}
if (value > maxValue) {
value = maxValue;
}
if (value < minValue) {
value = minValue;
}
PreferencesUtils.setInt(this, keyId, value);
}
/**
* Gets the speed display value, in metric or imperial depending on the
* preferred units.
*
* @param keyId the key id
*/
private int getSpeedDisplayValue(int keyId) {
int defaultValue = keyId == R.string.track_color_mode_slow_key ? PreferencesUtils.TRACK_COLOR_MODE_SLOW_DEFAULT
: PreferencesUtils.TRACK_COLOR_MODE_MEDIUM_DEFAULT;
int value = PreferencesUtils.getInt(this, keyId, defaultValue);
if (!PreferencesUtils.getBoolean(
this, R.string.metric_units_key, PreferencesUtils.METRIC_UNITS_DEFAULT)) {
value = (int) (value * UnitConversions.KM_TO_MI);
}
return value;
}
/**
* Updates the speed summary.
*
* @param keyId the key id
*/
private void updateSpeedSummary(int keyId) {
EditTextPreference editTextPreference = keyId == R.string.track_color_mode_slow_key ? slowEditTextPreference
: mediumEditTextPreference;
boolean metric = PreferencesUtils.getBoolean(
this, R.string.metric_units_key, PreferencesUtils.METRIC_UNITS_DEFAULT);
int displayValue = getSpeedDisplayValue(keyId);
editTextPreference.setSummary(getString(
metric ? R.string.value_integer_kilometer_hour : R.string.value_integer_mile_hour,
displayValue));
}
/**
* Updates the percentage summary.
*/
private void updatePercentageSummary() {
int value = PreferencesUtils.getInt(MapSettingsActivity.this,
R.string.track_color_mode_percentage_key,
PreferencesUtils.TRACK_COLOR_MODE_PERCENTAGE_DEFAULT);
percentageEditTextPreference.setSummary(getString(R.string.settings_map_percentage_summary)
+ "\n" + getString(R.string.value_integer_percent, value));
}
}
@@ -0,0 +1,274 @@
/*
* Copyright 2012 Google Inc.
*
* 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.google.android.apps.mytracks.settings;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.StringUtils;
import com.google.android.apps.mytracks.util.UnitConversions;
import com.google.android.maps.mytracks.R;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
/**
* An activity for accessing recording settings.
*
* @author Jimmy Shih
*/
public class RecordingSettingsActivity extends AbstractSettingsActivity {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
addPreferencesFromResource(R.xml.recording_settings);
boolean metricUnits = PreferencesUtils.getBoolean(
this, R.string.metric_units_key, PreferencesUtils.METRIC_UNITS_DEFAULT);
int voiceFrequencyValue = PreferencesUtils.getInt(
this, R.string.voice_frequency_key, PreferencesUtils.VOICE_FREQUENCY_DEFAULT);
ListPreference voiceFrequencyListPreference = (ListPreference) findPreference(
getString(R.string.voice_frequency_key));
configurePreference(voiceFrequencyListPreference,
StringUtils.getFrequencyOptions(this, metricUnits),
getResources().getStringArray(R.array.frequency_values),
R.string.settings_recording_voice_frequency_summary, String.valueOf(voiceFrequencyValue));
int splitFrequencyValue = PreferencesUtils.getInt(
this, R.string.split_frequency_key, PreferencesUtils.SPLIT_FREQUENCY_DEFAULT);
ListPreference splitFrequencyListPreference = (ListPreference) findPreference(
getString(R.string.split_frequency_key));
configurePreference(splitFrequencyListPreference,
StringUtils.getFrequencyOptions(this, metricUnits),
getResources().getStringArray(R.array.frequency_values),
R.string.settings_recording_split_frequency_summary, String.valueOf(splitFrequencyValue));
String trackNameValue = PreferencesUtils.getString(
this, R.string.track_name_key, PreferencesUtils.TRACK_NAME_DEFAULT);
ListPreference trackNameListPreference = (ListPreference) findPreference(
getString(R.string.track_name_key));
configurePreference(trackNameListPreference,
getResources().getStringArray(R.array.track_name_options),
getResources().getStringArray(R.array.track_name_values),
R.string.settings_recording_track_name_summary, trackNameValue);
String defaultActivityValue = PreferencesUtils.getString(
this, R.string.default_activity_key, PreferencesUtils.DEFAULT_ACTIVITY_DEFAULT);
Preference defaultActivityPreference = findPreference(getString(R.string.default_activity_key));
configurePreference(defaultActivityPreference, null, null,
R.string.settings_recording_default_activity_summary, defaultActivityValue);
int minRecordingIntervalValue = PreferencesUtils.getInt(
this, R.string.min_recording_interval_key, PreferencesUtils.MIN_RECORDING_INTERVAL_DEFAULT);
ListPreference minRecordingIntervalListPreference = (ListPreference) findPreference(
getString(R.string.min_recording_interval_key));
configurePreference(minRecordingIntervalListPreference, getMinRecordingIntervalOptions(),
getResources().getStringArray(R.array.min_recording_interval_values),
R.string.settings_recording_min_recording_interval_summary,
String.valueOf(minRecordingIntervalValue));
int minRecordingDistanceValue = PreferencesUtils.getInt(
this, R.string.min_recording_distance_key, PreferencesUtils.MIN_RECORDING_DISTANCE_DEFAULT);
ListPreference minRecordingDistanceListPreference = (ListPreference) findPreference(
getString(R.string.min_recording_distance_key));
configurePreference(minRecordingDistanceListPreference,
getMinRecordingDistanceOptions(metricUnits),
getResources().getStringArray(R.array.min_recording_distance_values),
R.string.settings_recording_min_recording_distance_summary,
String.valueOf(minRecordingDistanceValue));
int maxRecordingDistanceValue = PreferencesUtils.getInt(
this, R.string.max_recording_distance_key, PreferencesUtils.MAX_RECORDING_DISTANCE_DEFAULT);
ListPreference maxRecordingDistanceListPreference = (ListPreference) findPreference(
getString(R.string.max_recording_distance_key));
configurePreference(maxRecordingDistanceListPreference,
getMaxRecordingDistanceOptions(metricUnits),
getResources().getStringArray(R.array.max_recording_distance_values),
R.string.settings_recording_max_recording_distance_summary,
String.valueOf(maxRecordingDistanceValue));
int minRequiredAcuracyValue = PreferencesUtils.getInt(
this, R.string.min_required_accuracy_key, PreferencesUtils.MIN_REQUIRED_ACCURACY_DEFAULT);
ListPreference minRequiredAccuracyListPreference = (ListPreference) findPreference(
getString(R.string.min_required_accuracy_key));
configurePreference(minRequiredAccuracyListPreference,
getMinRequiredAccuracyOptions(metricUnits),
getResources().getStringArray(R.array.min_required_accuracy_values),
R.string.settings_recording_min_required_accuracy_summary,
String.valueOf(minRequiredAcuracyValue));
int autoResumeTrackTimeoutValue = PreferencesUtils.getInt(this,
R.string.auto_resume_track_timeout_key, PreferencesUtils.AUTO_RESUME_TRACK_TIMEOUT_DEFAULT);
ListPreference autoResumeTrackTimeoutListPreference = (ListPreference) findPreference(
getString(R.string.auto_resume_track_timeout_key));
configurePreference(autoResumeTrackTimeoutListPreference, getAutoResumeTrackTimeoutOptions(),
getResources().getStringArray(R.array.auto_resume_track_timeout_values),
R.string.settings_recording_auto_resume_track_timeout_summary,
String.valueOf(autoResumeTrackTimeoutValue));
}
/**
* Gets the min recording interval display options.
*/
private String[] getMinRecordingIntervalOptions() {
String[] values = getResources().getStringArray(R.array.min_recording_interval_values);
String[] options = new String[values.length];
for (int i = 0; i < values.length; i++) {
int value = Integer.parseInt(values[i]);
switch (value) {
case PreferencesUtils.MIN_RECORDING_INTERVAL_ADAPT_BATTERY_LIFE:
options[i] = getString(R.string.value_adapt_battery_life);
break;
case PreferencesUtils.MIN_RECORDING_INTERVAL_ADAPT_ACCURACY:
options[i] = getString(R.string.value_adapt_accuracy);
break;
case PreferencesUtils.MIN_RECORDING_INTERVAL_DEFAULT:
options[i] = getString(R.string.value_smallest_recommended);
break;
default:
options[i] = value < 60 ? getString(R.string.value_integer_second, value)
: getString(R.string.value_integer_minute, value / 60);
}
}
return options;
}
/**
* Gets the min recording distance display options.
*
* @param metricUnits true to display metric units
*/
private String[] getMinRecordingDistanceOptions(boolean metricUnits) {
String[] values = getResources().getStringArray(R.array.min_recording_distance_values);
String[] options = new String[values.length];
for (int i = 0; i < values.length; i++) {
int value = Integer.parseInt(values[i]);
if (metricUnits) {
options[i] = getString(
value == PreferencesUtils.MIN_RECORDING_DISTANCE_DEFAULT ? R.string.value_integer_meter_recommended
: R.string.value_integer_meter, value);
} else {
int feet = (int) (value * UnitConversions.M_TO_FT);
options[i] = getString(
value == PreferencesUtils.MIN_RECORDING_DISTANCE_DEFAULT ? R.string.value_integer_feet_recommended
: R.string.value_integer_feet, feet);
}
}
return options;
}
/**
* Gets the max recording distance display options.
*
* @param metricUnits true to display metric units
*/
private String[] getMaxRecordingDistanceOptions(boolean metricUnits) {
String[] values = getResources().getStringArray(R.array.max_recording_distance_values);
String[] options = new String[values.length];
for (int i = 0; i < values.length; i++) {
int value = Integer.parseInt(values[i]);
if (metricUnits) {
options[i] = getString(
value == PreferencesUtils.MAX_RECORDING_DISTANCE_DEFAULT ? R.string.value_integer_meter_recommended
: R.string.value_integer_meter, value);
} else {
int feet = (int) (value * UnitConversions.M_TO_FT);
if (feet < 2000) {
options[i] = getString(
value == PreferencesUtils.MAX_RECORDING_DISTANCE_DEFAULT ? R.string.value_integer_feet_recommended
: R.string.value_integer_feet, feet);
} else {
double mile = feet * UnitConversions.FT_TO_MI;
options[i] = getString(R.string.value_float_mile, mile);
}
}
}
return options;
}
/**
* Gets the min required accuracy display options.
*
* @param metricUnits true to display metric units
*/
private String[] getMinRequiredAccuracyOptions(boolean metricUnits) {
String[] values = getResources().getStringArray(R.array.min_required_accuracy_values);
String[] options = new String[values.length];
for (int i = 0; i < values.length; i++) {
int value = Integer.parseInt(values[i]);
if (metricUnits) {
switch (value) {
case PreferencesUtils.MIN_REQUIRED_ACCURACY_DEFAULT:
options[i] = getString(R.string.value_integer_meter_recommended, value);
break;
case PreferencesUtils.MIN_REQUIRED_ACCURACY_EXCELLENT:
options[i] = getString(R.string.value_integer_meter_excellent_gps, value);
break;
case PreferencesUtils.MIN_REQUIRED_ACCURACY_POOR:
options[i] = getString(R.string.value_integer_meter_poor_gps, value);
break;
default:
options[i] = getString(R.string.value_integer_meter, value);
}
} else {
int feet = (int) (value * UnitConversions.M_TO_FT);
if (feet < 2000) {
switch (value) {
case PreferencesUtils.MIN_REQUIRED_ACCURACY_DEFAULT:
options[i] = getString(R.string.value_integer_feet_recommended, feet);
break;
case PreferencesUtils.MIN_REQUIRED_ACCURACY_EXCELLENT:
options[i] = getString(R.string.value_integer_feet_excellent_gps, feet);
break;
default:
options[i] = getString(R.string.value_integer_feet, feet);
}
} else {
double mile = feet * UnitConversions.FT_TO_MI;
options[i] = getString(
value == PreferencesUtils.MIN_REQUIRED_ACCURACY_POOR ? R.string.value_float_mile_poor_gps
: R.string.value_float_mile, mile);
}
}
}
return options;
}
/**
* Gets the auto resume track timeout display options.
*/
private String[] getAutoResumeTrackTimeoutOptions() {
String[] values = getResources().getStringArray(R.array.auto_resume_track_timeout_values);
String[] options = new String[values.length];
for (int i = 0; i < values.length; i++) {
int value = Integer.parseInt(values[i]);
switch (value) {
case PreferencesUtils.AUTO_RESUME_TRACK_TIMEOUT_NEVER:
options[i] = getString(R.string.value_never);
break;
case PreferencesUtils.AUTO_RESUME_TRACK_TIMEOUT_ALWAYS:
options[i] = getString(R.string.value_always);
break;
default:
options[i] = getString(R.string.value_integer_minute, value);
}
}
return options;
}
}
@@ -0,0 +1,176 @@
/*
* Copyright 2012 Google Inc.
*
* 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.google.android.apps.mytracks.settings;
import com.dsi.ant.AntInterface;
import com.google.android.apps.mytracks.services.sensors.ant.AntSensorManager;
import com.google.android.apps.mytracks.util.BluetoothDeviceUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceScreen;
import android.provider.Settings;
import java.util.ArrayList;
import java.util.List;
/**
* An activity for accessing sensor settings.
*
* @author Jimmy Shih
*/
public class SensorSettingsActivity extends AbstractSettingsActivity {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
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);
findPreference(getString(R.string.settings_sensor_bluetooth_pairing_key))
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Intent settingsIntent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
startActivity(settingsIntent);
return true;
}
});
if (!hasAntSupport) {
PreferenceScreen rootPreferenceScreen = (PreferenceScreen) findPreference(
getString(R.string.settings_sensor_root_key));
rootPreferenceScreen.removePreference(
findPreference(getString(R.string.settings_sensor_ant_key)));
}
}
/**
* Updates the UI based on the sensor type.
*
* @param sensorType the sensor type
*/
@SuppressWarnings("deprecation")
private void updateUiBySensorType(String sensorType) {
boolean isBluetooth = getString(R.string.sensor_type_value_polar).equals(sensorType)
|| getString(R.string.sensor_type_value_zephyr).equals(sensorType);
findPreference(getString(R.string.settings_sensor_bluetooth_key)).setEnabled(isBluetooth);
boolean isAnt = getString(R.string.sensor_type_value_ant).equals(sensorType);
updateAntSensor(R.string.settings_sensor_ant_reset_heart_rate_monitor_key,
R.string.ant_heart_rate_monitor_id_key, isAnt);
updateAntSensor(R.string.settings_sensor_ant_reset_speed_distance_monitor_key,
R.string.ant_speed_distance_monitor_id_key, isAnt);
updateAntSensor(R.string.settings_sensor_ant_reset_bike_cadence_sensor_key,
R.string.ant_bike_cadence_sensor_id_key, isAnt);
updateAntSensor(R.string.settings_sensor_ant_reset_combined_bike_sensor_key,
R.string.ant_combined_bike_sensor_id_key, isAnt);
}
/**
* Updates an ant sensor.
*
* @param preferenceKey the preference key
* @param valueKey the value key
* @param enabled true if enabled
*/
@SuppressWarnings("deprecation")
private void updateAntSensor(int preferenceKey, final int valueKey, boolean enabled) {
Preference preference = findPreference(getString(preferenceKey));
if (preference != null) {
preference.setEnabled(enabled);
int deviceId = PreferencesUtils.getInt(this, valueKey, AntSensorManager.WILDCARD);
if (deviceId == AntSensorManager.WILDCARD) {
preference.setSummary(R.string.settings_sensor_not_connected);
} else {
preference.setSummary(getString(R.string.settings_sensor_ant_paired, deviceId));
}
preference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference pref) {
PreferencesUtils.setInt(SensorSettingsActivity.this, valueKey, AntSensorManager.WILDCARD);
pref.setSummary(R.string.settings_sensor_not_connected);
return true;
}
});
}
}
@Override
protected void onResume() {
super.onResume();
updateBluetoothSensorListPreference();
}
/**
* Updates the bluetooth sensor list preference.
*/
@SuppressWarnings("deprecation")
private void updateBluetoothSensorListPreference() {
List<String> optionsList = new ArrayList<String>();
List<String> valuesList = new ArrayList<String>();
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null) {
BluetoothDeviceUtils.populateDeviceLists(bluetoothAdapter, optionsList, valuesList);
}
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);
}
} else {
if (!valuesList.contains(bluetoothSensorValue)) {
bluetoothSensorValue = PreferencesUtils.BLUETOOTH_SENSOR_DEFAULT;
PreferencesUtils.setString(this, R.string.bluetooth_sensor_key, bluetoothSensorValue);
}
}
configurePreference(bluetoothSensorListPreference, options, values,
R.string.settings_sensor_bluetooth_sensor_summary, bluetoothSensorValue);
}
}
@@ -0,0 +1,231 @@
/*
* Copyright 2008 Google Inc.
*
* 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.google.android.apps.mytracks.settings;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.util.DialogUtils;
import com.google.android.apps.mytracks.util.IntentUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* An activity for accessing settings.
*
* @author Leif Hendrik Wilden
* @author Rodrigo Damazio
*/
public class SettingsActivity extends AbstractSettingsActivity {
private static final String TAG = SettingsActivity.class.getSimpleName();
private static final int DIALOG_CONFIRM_RESET_ID = 0;
private ListPreference googleAccountListPreference;
private Preference resetPreference;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
addPreferencesFromResource(R.xml.settings);
Preference mapPreference = findPreference(getString(R.string.settings_map_key));
mapPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = IntentUtils.newIntent(SettingsActivity.this, MapSettingsActivity.class);
startActivity(intent);
return true;
}
});
Preference chartPreference = findPreference(getString(R.string.settings_chart_key));
chartPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = IntentUtils.newIntent(SettingsActivity.this, ChartSettingsActivity.class);
startActivity(intent);
return true;
}
});
Preference statsPreference = findPreference(getString(R.string.settings_stats_key));
statsPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = IntentUtils.newIntent(SettingsActivity.this, StatsSettingsActivity.class);
startActivity(intent);
return true;
}
});
Preference recordingPreference = findPreference(getString(R.string.settings_recording_key));
recordingPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = IntentUtils.newIntent(
SettingsActivity.this, RecordingSettingsActivity.class);
startActivity(intent);
return true;
}
});
Preference sharingPreference = findPreference(getString(R.string.settings_sharing_key));
sharingPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = IntentUtils.newIntent(SettingsActivity.this, SharingSettingsActivity.class);
startActivity(intent);
return true;
}
});
Preference sensorPreference = findPreference(getString(R.string.settings_sensor_key));
sensorPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = IntentUtils.newIntent(SettingsActivity.this, SensorSettingsActivity.class);
startActivity(intent);
return true;
}
});
Preference backupPreference = findPreference(getString(R.string.settings_backup_key));
backupPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = IntentUtils.newIntent(SettingsActivity.this, BackupSettingsActivity.class);
startActivity(intent);
return true;
}
});
googleAccountListPreference = (ListPreference) findPreference(getString(R.string.google_account_key));
List<String> entries = new ArrayList<String>();
Account[] accounts = AccountManager.get(this).getAccountsByType(Constants.ACCOUNT_TYPE);
for (Account account : accounts) {
entries.add(account.name);
}
googleAccountListPreference.setEntries(entries.toArray(new CharSequence[entries.size()]));
googleAccountListPreference.setEntryValues(entries.toArray(
new CharSequence[entries.size()]));
if (entries.size() == 1) {
googleAccountListPreference.setValueIndex(0);
}
String googleAccount = PreferencesUtils.getString(this, R.string.google_account_key,
PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
updateSwitchAccountSummary(googleAccount);
googleAccountListPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
updateSwitchAccountSummary((String) newValue);
return true;
}
});
resetPreference = findPreference(getString(R.string.settings_reset_key));
resetPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference arg0) {
showDialog(DIALOG_CONFIRM_RESET_ID);
return true;
}
});
}
@Override
protected void onResume() {
super.onResume();
boolean isRecording = PreferencesUtils.getLong(this, R.string.recording_track_id_key)
!= PreferencesUtils.RECORDING_TRACK_ID_DEFAULT;
resetPreference.setEnabled(!isRecording);
resetPreference.setSummary(isRecording ? R.string.settings_not_while_recording
: R.string.settings_reset_summary);
}
/**
* Updates the switch account summary.
*
* @param value the value
*/
private void updateSwitchAccountSummary(String value) {
googleAccountListPreference.setSummary(
PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT.equals(value) ? getString(R.string.value_unknown)
: value);
}
@Override
protected Dialog onCreateDialog(int id) {
if (id != DIALOG_CONFIRM_RESET_ID) {
return null;
}
return DialogUtils.createConfirmationDialog(
this, R.string.settings_reset_confirm_message, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int button) {
onResetPreferencesConfirmed();
}
});
}
/**
* Callback when the user confirms resetting all settings.
*/
private void onResetPreferencesConfirmed() {
// Change preferences in a separate thread
new Thread() {
@Override
public void run() {
Log.i(TAG, "Resetting all settings");
SharedPreferences sharedPreferences = getSharedPreferences(
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
// Actually wipe preferences and save synchronously
sharedPreferences.edit().clear().commit();
// Give UI feedback in the UI thread
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(SettingsActivity.this, R.string.settings_reset_done, Toast.LENGTH_SHORT)
.show();
// Restart the settings activity so all changes are loaded
Intent intent = getIntent()
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
}.start();
}
}
@@ -0,0 +1,85 @@
/*
* Copyright 2012 Google Inc.
*
* 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.google.android.apps.mytracks.settings;
import com.google.android.apps.mytracks.util.DialogUtils;
import com.google.android.maps.mytracks.R;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
/**
* An activity for accessing the sharing settings.
*
* @author Jimmy Shih
*/
public class SharingSettingsActivity extends AbstractSettingsActivity {
private static final int DIALOG_CONFIRM_ALLOW_ACCESS_ID = 0;
private CheckBoxPreference defaultMapPublicCheckBoxPreference;
private CheckBoxPreference allowAccessCheckBoxPreference;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
addPreferencesFromResource(R.xml.sharing_settings);
defaultMapPublicCheckBoxPreference = (CheckBoxPreference) findPreference(
getString(R.string.default_map_public_key));
defaultMapPublicCheckBoxPreference.setSummaryOn(getString(
R.string.settings_sharing_new_map_public_summary_on,
getString(R.string.maps_public_unlisted_url)));
defaultMapPublicCheckBoxPreference.setSummaryOff(getString(
R.string.settings_sharing_new_map_public_summary_off,
getString(R.string.maps_public_unlisted_url)));
allowAccessCheckBoxPreference = (CheckBoxPreference) findPreference(
getString(R.string.allow_access_key));
allowAccessCheckBoxPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if ((Boolean) newValue) {
showDialog(DIALOG_CONFIRM_ALLOW_ACCESS_ID);
return false;
} else {
return true;
}
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
if (id != DIALOG_CONFIRM_ALLOW_ACCESS_ID) {
return null;
}
return DialogUtils.createConfirmationDialog(this,
R.string.settings_sharing_allow_access_confirm_message,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int button) {
allowAccessCheckBoxPreference.setChecked(true);
}
});
}
}
@@ -0,0 +1,101 @@
/*
* Copyright 2012 Google Inc.
*
* 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.google.android.apps.mytracks.settings;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
/**
* An activity for accessing stats settings.
*
* @author Jimmy Shih
*/
public class StatsSettingsActivity extends AbstractSettingsActivity {
/*
* Note that sharedPreferenceChangeListenr cannot be an anonymous inner class.
* Anonymous inner class will get garbage collected.
*/
private final OnSharedPreferenceChangeListener
sharedPreferenceChangeListener = new OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
if (key == null || key.equals(
PreferencesUtils.getKey(StatsSettingsActivity.this, R.string.metric_units_key))) {
metricUnits = PreferencesUtils.getBoolean(StatsSettingsActivity.this,
R.string.metric_units_key, PreferencesUtils.METRIC_UNITS_DEFAULT);
}
if (key != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
updateUi();
}
});
}
}
};
private SharedPreferences sharedPreferences;
private boolean metricUnits = PreferencesUtils.METRIC_UNITS_DEFAULT;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
addPreferencesFromResource(R.xml.stats_settings);
sharedPreferences = getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
}
@Override
protected void onStart() {
super.onStart();
sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
sharedPreferenceChangeListener.onSharedPreferenceChanged(null, null);
}
@Override
protected void onResume() {
super.onResume();
updateUi();
}
@Override
protected void onStop() {
super.onStop();
sharedPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
}
@SuppressWarnings("deprecation")
private void updateUi() {
CheckBoxPreference reportSpeedCheckBoxPreference = (CheckBoxPreference) findPreference(
getString(R.string.report_speed_key));
reportSpeedCheckBoxPreference.setSummaryOn(
metricUnits ? getString(R.string.description_speed_metric)
: getString(R.string.description_speed_imperial));
reportSpeedCheckBoxPreference.setSummaryOff(
metricUnits ? getString(R.string.description_pace_metric)
: getString(R.string.description_pace_imperial));
}
}