forked from upstream-mirrors/OpenTracks
Add test cases about TripStatisticsUpdater.addLocation().
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* 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 list 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,
|
||||
final OnPreferenceChangeListener listener) {
|
||||
listPreference.setEntryValues(values);
|
||||
listPreference.setEntries(options);
|
||||
listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference pref, Object newValue) {
|
||||
updatePreferenceSummary(pref, summary, values, (String) newValue);
|
||||
if (listener != null) {
|
||||
listener.onPreferenceChange(pref, newValue);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
});
|
||||
updatePreferenceSummary(listPreference, summary, values, value);
|
||||
if (listener != null) {
|
||||
listener.onPreferenceChange(listPreference, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the preference summary.
|
||||
*
|
||||
* @param preference the preference
|
||||
* @param summary the summary array
|
||||
* @param values the values array
|
||||
* @param value the value
|
||||
*/
|
||||
private void updatePreferenceSummary(
|
||||
Preference preference, String[] summary, String[] values, String value) {
|
||||
int index = getIndex(values, value);
|
||||
if (index == -1) {
|
||||
preference.setSummary(R.string.value_unknown);
|
||||
} else {
|
||||
preference.setSummary(summary[index]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the array index for a value.
|
||||
*
|
||||
* @param values the array
|
||||
* @param value the value
|
||||
*/
|
||||
private int getIndex(String[] values, String value) {
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (value.equals(values[i])) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
+212
@@ -0,0 +1,212 @@
|
||||
/*
|
||||
* 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.io.sync.SyncUtils;
|
||||
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;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
/**
|
||||
* An activity for accessing the backup and reset settings.
|
||||
*
|
||||
* @author Jimmy Shih
|
||||
*/
|
||||
public class BackupResetSettingsActivity extends AbstractSettingsActivity {
|
||||
|
||||
private static final String TAG = BackupResetSettingsActivity.class.getSimpleName();
|
||||
private static final int DIALOG_CONFIRM_RESTORE_ID = 0;
|
||||
private static final int DIALOG_CONFIRM_RESET_ID = 1;
|
||||
|
||||
private SharedPreferences sharedPreferences;
|
||||
private Preference backupPreference;
|
||||
private Preference restoreNowPreference;
|
||||
private Preference resetPreference;
|
||||
|
||||
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(
|
||||
BackupResetSettingsActivity.this, R.string.recording_track_id_key))) {
|
||||
recordingTrackId = PreferencesUtils.getLong(
|
||||
BackupResetSettingsActivity.this, R.string.recording_track_id_key);
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateUi();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
protected void onCreate(Bundle bundle) {
|
||||
super.onCreate(bundle);
|
||||
addPreferencesFromResource(R.xml.backup_reset_settings);
|
||||
|
||||
sharedPreferences = getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
|
||||
|
||||
backupPreference = findPreference(getString(R.string.settings_backup_key));
|
||||
backupPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
Intent intent = IntentUtils.newIntent(
|
||||
BackupResetSettingsActivity.this, BackupActivity.class);
|
||||
startActivity(intent);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
restoreNowPreference = findPreference(getString(R.string.settings_restore_key));
|
||||
restoreNowPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
showDialog(DIALOG_CONFIRM_RESTORE_ID);
|
||||
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 Dialog onCreateDialog(int id) {
|
||||
Dialog dialog;
|
||||
switch (id) {
|
||||
case DIALOG_CONFIRM_RESTORE_ID:
|
||||
dialog = DialogUtils.createConfirmationDialog(this,
|
||||
R.string.settings_backup_restore_confirm_message,
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface d, int which) {
|
||||
Intent intent = IntentUtils.newIntent(
|
||||
BackupResetSettingsActivity.this, RestoreChooserActivity.class);
|
||||
startActivity(intent);
|
||||
}
|
||||
});
|
||||
break;
|
||||
case DIALOG_CONFIRM_RESET_ID:
|
||||
dialog = DialogUtils.createConfirmationDialog(
|
||||
this, R.string.settings_reset_confirm_message, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface d, int button) {
|
||||
onResetPreferencesConfirmed();
|
||||
}
|
||||
});
|
||||
break;
|
||||
default:
|
||||
dialog = null;
|
||||
}
|
||||
return dialog;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
|
||||
recordingTrackId = PreferencesUtils.getLong(this, R.string.recording_track_id_key);
|
||||
updateUi();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
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);
|
||||
backupPreference.setSummary(
|
||||
isRecording ? R.string.settings_not_while_recording : R.string.settings_backup_now_summary);
|
||||
|
||||
restoreNowPreference.setEnabled(!isRecording);
|
||||
restoreNowPreference.setSummary(isRecording ? R.string.settings_not_while_recording
|
||||
: R.string.settings_backup_restore_summary);
|
||||
|
||||
resetPreference.setEnabled(!isRecording);
|
||||
resetPreference.setSummary(
|
||||
isRecording ? R.string.settings_not_while_recording : R.string.settings_reset_summary);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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");
|
||||
|
||||
// Actually wipe preferences and save synchronously
|
||||
sharedPreferences.edit().clear().commit();
|
||||
|
||||
SyncUtils.disableSync(BackupResetSettingsActivity.this);
|
||||
SyncUtils.clearSyncState(BackupResetSettingsActivity.this);
|
||||
|
||||
// Give UI feedback in the UI thread
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Toast.makeText(
|
||||
BackupResetSettingsActivity.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,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,259 @@
|
||||
/*
|
||||
* Copyright 2013 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.sendtogoogle.PermissionCallback;
|
||||
import com.google.android.apps.mytracks.io.sendtogoogle.SendToGoogleUtils;
|
||||
import com.google.android.apps.mytracks.io.sync.SyncUtils;
|
||||
import com.google.android.apps.mytracks.util.DialogUtils;
|
||||
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.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.preference.CheckBoxPreference;
|
||||
import android.preference.ListPreference;
|
||||
import android.preference.Preference;
|
||||
import android.preference.Preference.OnPreferenceChangeListener;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* An activity for accessing the Google settings.
|
||||
*
|
||||
* @author Jimmy Shih
|
||||
*/
|
||||
public class GoogleSettingsActivity extends AbstractSettingsActivity {
|
||||
|
||||
private static final String ACCOUNT_NAME_KEY = "accountName";
|
||||
private static final int DIALOG_CONFIRM_SWITCH_ACCOUNT = 0;
|
||||
private static final int DIALOG_CONFIRM_DRIVE_SYNC_ON = 1;
|
||||
|
||||
private PermissionCallback permissionCallback = new PermissionCallback() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
handleSync(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure() {
|
||||
Toast.makeText(
|
||||
GoogleSettingsActivity.this, R.string.send_google_no_account_message, Toast.LENGTH_LONG)
|
||||
.show();
|
||||
}
|
||||
};
|
||||
|
||||
private ListPreference googleAccountPreference;
|
||||
private CheckBoxPreference driveSyncPreference;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
protected void onCreate(Bundle bundle) {
|
||||
super.onCreate(bundle);
|
||||
addPreferencesFromResource(R.xml.google_settings);
|
||||
|
||||
googleAccountPreference = (ListPreference) findPreference(
|
||||
getString(R.string.google_account_key));
|
||||
List<String> entries = new ArrayList<String>();
|
||||
List<String> entryValues = new ArrayList<String>();
|
||||
Account[] accounts = AccountManager.get(this).getAccountsByType(Constants.ACCOUNT_TYPE);
|
||||
for (Account account : accounts) {
|
||||
entries.add(account.name);
|
||||
entryValues.add(account.name);
|
||||
}
|
||||
entries.add(getString(R.string.value_none));
|
||||
entryValues.add(PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
|
||||
|
||||
googleAccountPreference.setEntries(entries.toArray(new CharSequence[entries.size()]));
|
||||
googleAccountPreference.setEntryValues(entryValues.toArray(new CharSequence[entries.size()]));
|
||||
googleAccountPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
String newGoogleAccount = (String) newValue;
|
||||
String googleAccount = PreferencesUtils.getString(
|
||||
GoogleSettingsActivity.this, R.string.google_account_key,
|
||||
PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
|
||||
if (googleAccount == null
|
||||
|| googleAccount.equals(PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT)) {
|
||||
updateUiByAccountName(newGoogleAccount);
|
||||
return true;
|
||||
}
|
||||
if (!googleAccount.equals(newGoogleAccount)) {
|
||||
Bundle newBundle = new Bundle();
|
||||
newBundle.putString(ACCOUNT_NAME_KEY, newGoogleAccount);
|
||||
showDialog(DIALOG_CONFIRM_SWITCH_ACCOUNT, newBundle);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
driveSyncPreference = (CheckBoxPreference) findPreference(getString(R.string.drive_sync_key));
|
||||
driveSyncPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
if ((Boolean) newValue) {
|
||||
showDialog(DIALOG_CONFIRM_DRIVE_SYNC_ON);
|
||||
} else {
|
||||
handleSync(false);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
CheckBoxPreference defaultMapPublicPreference = (CheckBoxPreference) findPreference(
|
||||
getString(R.string.default_map_public_key));
|
||||
defaultMapPublicPreference.setSummaryOn(getString(
|
||||
R.string.settings_google_maps_public_summary_on,
|
||||
getString(R.string.maps_public_unlisted_url)));
|
||||
defaultMapPublicPreference.setSummaryOff(getString(
|
||||
R.string.settings_google_maps_public_summary_off,
|
||||
getString(R.string.maps_public_unlisted_url)));
|
||||
|
||||
updateUiByAccountName(PreferencesUtils.getString(
|
||||
this, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
switch (requestCode) {
|
||||
case SendToGoogleUtils.DRIVE_PERMISSION_REQUEST_CODE:
|
||||
SendToGoogleUtils.cancelNotification(this, SendToGoogleUtils.DRIVE_NOTIFICATION_ID);
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
permissionCallback.onSuccess();
|
||||
} else {
|
||||
permissionCallback.onFailure();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Dialog onCreateDialog(int id, Bundle bundle) {
|
||||
Dialog dialog;
|
||||
switch (id) {
|
||||
case DIALOG_CONFIRM_SWITCH_ACCOUNT:
|
||||
dialog = DialogUtils.createConfirmationDialog(
|
||||
this, R.string.settings_google_account_confirm_message, null);
|
||||
break;
|
||||
case DIALOG_CONFIRM_DRIVE_SYNC_ON:
|
||||
dialog = DialogUtils.createConfirmationDialog(this,
|
||||
R.string.settings_google_drive_sync_confirm_message,
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface d, int button) {
|
||||
String googleAccount = PreferencesUtils.getString(
|
||||
GoogleSettingsActivity.this, R.string.google_account_key,
|
||||
PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
|
||||
SendToGoogleUtils.checkPermissionByActivity(GoogleSettingsActivity.this,
|
||||
googleAccount, SendToGoogleUtils.DRIVE_SCOPE,
|
||||
SendToGoogleUtils.DRIVE_PERMISSION_REQUEST_CODE, permissionCallback);
|
||||
}
|
||||
});
|
||||
break;
|
||||
default:
|
||||
dialog = null;
|
||||
}
|
||||
return dialog;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
|
||||
AlertDialog alertDialog = (AlertDialog) dialog;
|
||||
String googleAccount = PreferencesUtils.getString(
|
||||
this, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
|
||||
switch (id) {
|
||||
case DIALOG_CONFIRM_SWITCH_ACCOUNT:
|
||||
final String newValue = bundle.getString(ACCOUNT_NAME_KEY);
|
||||
alertDialog.setMessage(
|
||||
getString(R.string.settings_google_account_confirm_message, googleAccount));
|
||||
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, getString(android.R.string.ok),
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface d, int button) {
|
||||
googleAccountPreference.setValue(newValue);
|
||||
handleSync(false);
|
||||
updateUiByAccountName(newValue);
|
||||
}
|
||||
});
|
||||
break;
|
||||
case DIALOG_CONFIRM_DRIVE_SYNC_ON:
|
||||
alertDialog.setMessage(getString(
|
||||
R.string.settings_google_drive_sync_confirm_message, googleAccount,
|
||||
getString(R.string.my_tracks_app_name)));
|
||||
break;
|
||||
default:
|
||||
}
|
||||
super.onPrepareDialog(id, dialog, bundle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles sync.
|
||||
*
|
||||
* @param value true to sync
|
||||
*/
|
||||
private void handleSync(boolean value) {
|
||||
driveSyncPreference.setChecked(value);
|
||||
|
||||
// Turn off everything
|
||||
SyncUtils.disableSync(this);
|
||||
|
||||
if (value) {
|
||||
|
||||
// Turn on sync
|
||||
ContentResolver.setMasterSyncAutomatically(true);
|
||||
|
||||
// Enable sync for account
|
||||
String googleAccount = PreferencesUtils.getString(
|
||||
this, R.string.google_account_key, PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT);
|
||||
Account[] accounts = AccountManager.get(this).getAccountsByType(Constants.ACCOUNT_TYPE);
|
||||
for (Account account : accounts) {
|
||||
if (account.name.equals(googleAccount)) {
|
||||
SyncUtils.enableSync(account);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
SyncUtils.clearSyncState(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates UI by account.
|
||||
*
|
||||
* @param accountName the account name
|
||||
*/
|
||||
private void updateUiByAccountName(String accountName) {
|
||||
boolean hasAccount = !PreferencesUtils.GOOGLE_ACCOUNT_DEFAULT.equals(accountName);
|
||||
googleAccountPreference.setSummary(
|
||||
hasAccount ? accountName : getString(R.string.value_unknown));
|
||||
driveSyncPreference.setEnabled(hasAccount);
|
||||
driveSyncPreference.setSummaryOn(
|
||||
getString(R.string.settings_google_drive_sync_summary_on, accountName));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,285 @@
|
||||
/*
|
||||
* 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 slowPreference;
|
||||
private EditTextPreference mediumPreference;
|
||||
private EditTextPreference percentagePreference;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
protected void onCreate(Bundle bundle) {
|
||||
super.onCreate(bundle);
|
||||
addPreferencesFromResource(R.xml.map_settings);
|
||||
|
||||
slowPreference = (EditTextPreference) findPreference(
|
||||
getString(R.string.settings_map_slow_display_key));
|
||||
mediumPreference = (EditTextPreference) findPreference(
|
||||
getString(R.string.settings_map_medium_display_key));
|
||||
percentagePreference = (EditTextPreference) findPreference(
|
||||
getString(R.string.settings_map_percentage_display_key));
|
||||
|
||||
configTrackColorModePerference();
|
||||
configSpeedPreference(slowPreference, R.string.track_color_mode_slow_key,
|
||||
PreferencesUtils.TRACK_COLOR_MODE_SLOW_DEFAULT);
|
||||
configSpeedPreference(mediumPreference, R.string.track_color_mode_medium_key,
|
||||
PreferencesUtils.TRACK_COLOR_MODE_MEDIUM_DEFAULT);
|
||||
configPercentagePreference();
|
||||
|
||||
updateSpeedSummary(slowPreference, R.string.track_color_mode_slow_key,
|
||||
PreferencesUtils.TRACK_COLOR_MODE_SLOW_DEFAULT);
|
||||
updateSpeedSummary(mediumPreference, R.string.track_color_mode_medium_key,
|
||||
PreferencesUtils.TRACK_COLOR_MODE_MEDIUM_DEFAULT);
|
||||
updatePercentageSummary();
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the track color mode preference.
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
private void configTrackColorModePerference() {
|
||||
ListPreference preference = (ListPreference) findPreference(
|
||||
getString(R.string.track_color_mode_key));
|
||||
OnPreferenceChangeListener listener = new OnPreferenceChangeListener() {
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference pref, Object newValue) {
|
||||
updateUiByTrackColorMode((String) newValue);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
String value = PreferencesUtils.getString(
|
||||
this, R.string.track_color_mode_key, PreferencesUtils.TRACK_COLOR_MODE_DEFAULT);
|
||||
String[] values = getResources().getStringArray(R.array.track_color_mode_values);
|
||||
String[] options = getResources().getStringArray(R.array.track_color_mode_options);
|
||||
String[] summary = getResources().getStringArray(R.array.track_color_mode_summary);
|
||||
configureListPreference(preference, summary, options, values, value, listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the speed preference.
|
||||
*
|
||||
* @param preference the preference
|
||||
* @param key the key
|
||||
* @param defaultValue the default value
|
||||
*/
|
||||
private void configSpeedPreference(
|
||||
EditTextPreference preference, final int key, final int defaultValue) {
|
||||
preference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference pref, Object newValue) {
|
||||
|
||||
// Need to obtain the current min and max values
|
||||
int minValue;
|
||||
int maxValue;
|
||||
if (key == R.string.track_color_mode_slow_key) {
|
||||
minValue = 0;
|
||||
maxValue = PreferencesUtils.getInt(
|
||||
MapSettingsActivity.this, R.string.track_color_mode_medium_key,
|
||||
PreferencesUtils.TRACK_COLOR_MODE_MEDIUM_DEFAULT);
|
||||
} else {
|
||||
minValue = PreferencesUtils.getInt(
|
||||
MapSettingsActivity.this, R.string.track_color_mode_slow_key,
|
||||
PreferencesUtils.TRACK_COLOR_MODE_SLOW_DEFAULT);
|
||||
maxValue = Integer.MAX_VALUE;
|
||||
}
|
||||
storeSpeedValue(key, minValue, maxValue, defaultValue, (String) newValue);
|
||||
updateSpeedSummary(pref, key, defaultValue);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
preference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference pref) {
|
||||
int value = getSpeedDisplayValue(key, defaultValue);
|
||||
((EditTextPreference) pref).getEditText().setText(String.valueOf(value));
|
||||
return true;
|
||||
}
|
||||
});
|
||||
configImeActionDone(preference);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the percentage preference.
|
||||
*/
|
||||
private void configPercentagePreference() {
|
||||
percentagePreference.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;
|
||||
}
|
||||
});
|
||||
percentagePreference.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;
|
||||
}
|
||||
});
|
||||
configImeActionDone(percentagePreference);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the IME action done.
|
||||
*
|
||||
* @param editTextPreference the edit text preference
|
||||
*/
|
||||
private void configImeActionDone(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;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
slowPreference.setEnabled(isFixedValue);
|
||||
mediumPreference.setEnabled(isFixedValue);
|
||||
percentagePreference.setEnabled(isDynamicValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a speed summary.
|
||||
*
|
||||
* @param preference the preference
|
||||
* @param keyId the key id
|
||||
* @param defaultValue the default value
|
||||
*/
|
||||
private void updateSpeedSummary(Preference preference, int keyId, int defaultValue) {
|
||||
boolean metric = PreferencesUtils.getBoolean(
|
||||
this, R.string.metric_units_key, PreferencesUtils.METRIC_UNITS_DEFAULT);
|
||||
int displayValue = getSpeedDisplayValue(keyId, defaultValue);
|
||||
preference.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(this, R.string.track_color_mode_percentage_key,
|
||||
PreferencesUtils.TRACK_COLOR_MODE_PERCENTAGE_DEFAULT);
|
||||
percentagePreference.setSummary(getString(R.string.settings_map_percentage_summary, value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores a speed value, always in metric units.
|
||||
*
|
||||
* @param keyId the key id
|
||||
* @param minValue the min value
|
||||
* @param maxValue the max value
|
||||
* @param defaultValue the default value
|
||||
* @param displayValue the display value
|
||||
*/
|
||||
private void storeSpeedValue(
|
||||
int keyId, int minValue, int maxValue, int defaultValue, String displayValue) {
|
||||
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 a speed display value in metric or imperial depending on the preferred
|
||||
* units.
|
||||
*
|
||||
* @param keyId the key id
|
||||
* @param defaultValue the default value
|
||||
*/
|
||||
private int getSpeedDisplayValue(int keyId, int defaultValue) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
+339
@@ -0,0 +1,339 @@
|
||||
/*
|
||||
* 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);
|
||||
|
||||
configFrequencyPreference(R.string.voice_frequency_key,
|
||||
PreferencesUtils.VOICE_FREQUENCY_DEFAULT, R.array.frequency_values, metricUnits);
|
||||
configFrequencyPreference(R.string.split_frequency_key,
|
||||
PreferencesUtils.SPLIT_FREQUENCY_DEFAULT, R.array.frequency_values, metricUnits);
|
||||
configTrackName();
|
||||
configDefaultActivity();
|
||||
configListPreference(R.string.min_recording_interval_key,
|
||||
PreferencesUtils.MIN_RECORDING_INTERVAL_DEFAULT, R.array.min_recording_interval_values,
|
||||
metricUnits);
|
||||
configListPreference(R.string.min_recording_distance_key,
|
||||
PreferencesUtils.MIN_RECORDING_DISTANCE_DEFAULT, R.array.min_recording_distance_values,
|
||||
metricUnits);
|
||||
configListPreference(R.string.max_recording_distance_key,
|
||||
PreferencesUtils.MAX_RECORDING_DISTANCE_DEFAULT, R.array.max_recording_distance_values,
|
||||
metricUnits);
|
||||
configListPreference(R.string.min_required_accuracy_key,
|
||||
PreferencesUtils.MIN_REQUIRED_ACCURACY_DEFAULT, R.array.min_required_accuracy_values,
|
||||
metricUnits);
|
||||
configListPreference(R.string.auto_resume_track_timeout_key,
|
||||
PreferencesUtils.AUTO_RESUME_TRACK_TIMEOUT_DEFAULT,
|
||||
R.array.auto_resume_track_timeout_values, metricUnits);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private void configFrequencyPreference(
|
||||
int key, int defaultValue, int valueArray, boolean metricUnits) {
|
||||
ListPreference preference = (ListPreference) findPreference(getString(key));
|
||||
int value = PreferencesUtils.getInt(this, key, defaultValue);
|
||||
String[] values = getResources().getStringArray(valueArray);
|
||||
String[] options = StringUtils.getFrequencyOptions(this, metricUnits);
|
||||
configureListPreference(preference, options, options, values, String.valueOf(value), null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private void configTrackName() {
|
||||
ListPreference preference = (ListPreference) findPreference(getString(R.string.track_name_key));
|
||||
String value = PreferencesUtils.getString(
|
||||
this, R.string.track_name_key, PreferencesUtils.TRACK_NAME_DEFAULT);
|
||||
String[] values = getResources().getStringArray(R.array.track_name_values);
|
||||
String[] options = getResources().getStringArray(R.array.track_name_options);
|
||||
configureListPreference(preference, options, options, values, value, null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private void configDefaultActivity() {
|
||||
Preference preference = findPreference(getString(R.string.default_activity_key));
|
||||
String value = PreferencesUtils.getString(
|
||||
this, R.string.default_activity_key, PreferencesUtils.DEFAULT_ACTIVITY_DEFAULT);
|
||||
preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference pref, Object newValue) {
|
||||
String stringValue = (String) newValue;
|
||||
pref.setSummary(stringValue != null
|
||||
&& !stringValue.equals(PreferencesUtils.DEFAULT_ACTIVITY_DEFAULT) ? stringValue
|
||||
: getString(R.string.value_unknown));
|
||||
return true;
|
||||
}
|
||||
});
|
||||
preference.setSummary(
|
||||
value != null && !value.equals(PreferencesUtils.DEFAULT_ACTIVITY_DEFAULT) ? value
|
||||
: getString(R.string.value_unknown));
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private void configListPreference(
|
||||
int key, int defaultValue, int valueArray, boolean metricUnits) {
|
||||
ListPreference preference = (ListPreference) findPreference(getString(key));
|
||||
int value = PreferencesUtils.getInt(this, key, defaultValue);
|
||||
String[] values = getResources().getStringArray(valueArray);
|
||||
String[] options = new String[values.length];
|
||||
String[] summary = new String[values.length];
|
||||
switch (key) {
|
||||
case R.string.min_recording_interval_key:
|
||||
setMinRecordingIntervalSummaryAndOptions(summary, options, values);
|
||||
break;
|
||||
case R.string.min_recording_distance_key:
|
||||
setMinRecordingDistanceSummaryAndOptions(summary, options, values, metricUnits);
|
||||
break;
|
||||
case R.string.max_recording_distance_key:
|
||||
setMaxRecordingDistanceSummaryAndOptions(summary, options, values, metricUnits);
|
||||
break;
|
||||
case R.string.min_required_accuracy_key:
|
||||
setMinRequiredAccuracySummaryAndOptions(summary, options, values, metricUnits);
|
||||
break;
|
||||
case R.string.auto_resume_track_timeout_key:
|
||||
setAutoResumeTrackTimeoutSummaryAndOptions(summary, options, values);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
configureListPreference(preference, summary, options, values, String.valueOf(value), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the min recording interval summary and options.
|
||||
*
|
||||
* @param summary the summary
|
||||
* @param options the options
|
||||
* @param values the values
|
||||
*/
|
||||
private void setMinRecordingIntervalSummaryAndOptions(
|
||||
String[] summary, String[] options, String[] values) {
|
||||
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);
|
||||
summary[i] = options[i];
|
||||
break;
|
||||
case PreferencesUtils.MIN_RECORDING_INTERVAL_ADAPT_ACCURACY:
|
||||
options[i] = getString(R.string.value_adapt_accuracy);
|
||||
summary[i] = options[i];
|
||||
break;
|
||||
case PreferencesUtils.MIN_RECORDING_INTERVAL_DEFAULT:
|
||||
options[i] = getString(R.string.value_smallest_recommended);
|
||||
summary[i] = getString(R.string.value_smallest);
|
||||
break;
|
||||
default:
|
||||
options[i] = value < 60 ? getString(R.string.value_integer_second, value)
|
||||
: getString(R.string.value_integer_minute, value / 60);
|
||||
summary[i] = getString(
|
||||
R.string.settings_recording_location_frequency_summary, options[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the min recording distance summary and options.
|
||||
*
|
||||
* @param summary the summary
|
||||
* @param options the options
|
||||
* @param values the values
|
||||
* @param metricUnits true for metric units
|
||||
*/
|
||||
private void setMinRecordingDistanceSummaryAndOptions(
|
||||
String[] summary, String[] options, String[] values, boolean metricUnits) {
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
int value = Integer.parseInt(values[i]);
|
||||
String displayValue;
|
||||
if (metricUnits) {
|
||||
displayValue = getString(R.string.value_integer_meter, value);
|
||||
switch (value) {
|
||||
case PreferencesUtils.MIN_RECORDING_DISTANCE_DEFAULT:
|
||||
options[i] = getString(R.string.value_integer_meter_recommended, value);
|
||||
break;
|
||||
default:
|
||||
options[i] = displayValue;
|
||||
}
|
||||
} else {
|
||||
int feet = (int) (value * UnitConversions.M_TO_FT);
|
||||
displayValue = getString(R.string.value_integer_feet, feet);
|
||||
switch (value) {
|
||||
case PreferencesUtils.MIN_RECORDING_DISTANCE_DEFAULT:
|
||||
options[i] = getString(R.string.value_integer_feet_recommended, feet);
|
||||
break;
|
||||
default:
|
||||
options[i] = displayValue;
|
||||
}
|
||||
}
|
||||
summary[i] = getString(R.string.settings_recording_location_frequency_summary, displayValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the max recording distance summary and options.
|
||||
*
|
||||
* @param summary the summary
|
||||
* @param options the options
|
||||
* @param values the values
|
||||
* @param metricUnits true for metric units
|
||||
*/
|
||||
private void setMaxRecordingDistanceSummaryAndOptions(
|
||||
String[] summary, String[] options, String[] values, boolean metricUnits) {
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
int value = Integer.parseInt(values[i]);
|
||||
String displayValue;
|
||||
if (metricUnits) {
|
||||
displayValue = getString(R.string.value_integer_meter, value);
|
||||
switch (value) {
|
||||
case PreferencesUtils.MAX_RECORDING_DISTANCE_DEFAULT:
|
||||
options[i] = getString(R.string.value_integer_meter_recommended, value);
|
||||
break;
|
||||
default:
|
||||
options[i] = displayValue;
|
||||
}
|
||||
} else {
|
||||
int feet = (int) (value * UnitConversions.M_TO_FT);
|
||||
if (feet < 2000) {
|
||||
displayValue = getString(R.string.value_integer_feet, feet);
|
||||
switch (value) {
|
||||
case PreferencesUtils.MAX_RECORDING_DISTANCE_DEFAULT:
|
||||
options[i] = getString(R.string.value_integer_feet_recommended, feet);
|
||||
break;
|
||||
default:
|
||||
options[i] = displayValue;
|
||||
}
|
||||
} else {
|
||||
double mile = feet * UnitConversions.FT_TO_MI;
|
||||
displayValue = getString(R.string.value_float_mile, mile);
|
||||
options[i] = displayValue;
|
||||
}
|
||||
}
|
||||
summary[i] = getString(
|
||||
R.string.settings_recording_max_recording_distance_summary, displayValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the min required accuracy summary and options.
|
||||
*
|
||||
* @param summary the summary
|
||||
* @param options the options
|
||||
* @param values the values
|
||||
* @param metricUnits true for metric units
|
||||
*/
|
||||
private void setMinRequiredAccuracySummaryAndOptions(
|
||||
String[] summary, String[] options, String[] values, boolean metricUnits) {
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
int value = Integer.parseInt(values[i]);
|
||||
String displayValue;
|
||||
if (metricUnits) {
|
||||
displayValue = getString(R.string.value_integer_meter, value);
|
||||
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] = displayValue;
|
||||
}
|
||||
} else {
|
||||
int feet = (int) (value * UnitConversions.M_TO_FT);
|
||||
if (feet < 2000) {
|
||||
displayValue = getString(R.string.value_integer_feet, feet);
|
||||
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] = displayValue;
|
||||
}
|
||||
} else {
|
||||
double mile = feet * UnitConversions.FT_TO_MI;
|
||||
displayValue = getString(R.string.value_float_mile, mile);
|
||||
switch (value) {
|
||||
case PreferencesUtils.MIN_REQUIRED_ACCURACY_POOR:
|
||||
options[i] = getString(R.string.value_float_mile_poor_gps, mile);
|
||||
break;
|
||||
default:
|
||||
options[i] = displayValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
summary[i] = getString(
|
||||
R.string.settings_recording_min_required_accuracy_summary, displayValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the auto resume track timeout summary and options.
|
||||
*
|
||||
* @param summary the summary
|
||||
* @param options the options
|
||||
* @param values the values
|
||||
*/
|
||||
private void setAutoResumeTrackTimeoutSummaryAndOptions(
|
||||
String[] summary, String[] options, String[] values) {
|
||||
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);
|
||||
summary[i] = getString(
|
||||
R.string.settings_recording_auto_resume_track_timeout_never_summary);
|
||||
break;
|
||||
case PreferencesUtils.AUTO_RESUME_TRACK_TIMEOUT_ALWAYS:
|
||||
options[i] = getString(R.string.value_always);
|
||||
summary[i] = getString(
|
||||
R.string.settings_recording_auto_resume_track_timeout_always_summary);
|
||||
break;
|
||||
default:
|
||||
options[i] = getString(R.string.value_integer_minute, value);
|
||||
summary[i] = getString(
|
||||
R.string.settings_recording_auto_resume_track_timeout_summary, options[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
/*
|
||||
* 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);
|
||||
|
||||
configSensorType(hasAntSupport);
|
||||
|
||||
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)));
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
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.
|
||||
*
|
||||
* @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_ant_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_ant_not_connected);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
||||
// Update each time in case the list of bluetooth sensors has changed
|
||||
configBluetoothSensor();
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the bluetooth sensor.
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
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();
|
||||
if (bluetoothAdapter != null) {
|
||||
BluetoothDeviceUtils.populateDeviceLists(bluetoothAdapter, optionsList, valuesList);
|
||||
}
|
||||
String[] options = optionsList.toArray(new String[optionsList.size()]);
|
||||
String[] values = valuesList.toArray(new String[valuesList.size()]);
|
||||
|
||||
if (valuesList.size() == 1) {
|
||||
if (!valuesList.get(0).equals(value)) {
|
||||
value = valuesList.get(0);
|
||||
PreferencesUtils.setString(this, R.string.bluetooth_sensor_key, value);
|
||||
}
|
||||
} else {
|
||||
if (!valuesList.contains(value)) {
|
||||
value = PreferencesUtils.BLUETOOTH_SENSOR_DEFAULT;
|
||||
PreferencesUtils.setString(this, R.string.bluetooth_sensor_key, value);
|
||||
}
|
||||
}
|
||||
configureListPreference(preference, options, options, values, value, null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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.util.IntentUtils;
|
||||
import com.google.android.maps.mytracks.R;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.preference.Preference;
|
||||
import android.preference.Preference.OnPreferenceClickListener;
|
||||
|
||||
/**
|
||||
* An activity for accessing settings.
|
||||
*
|
||||
* @author Leif Hendrik Wilden
|
||||
* @author Rodrigo Damazio
|
||||
*/
|
||||
public class SettingsActivity extends AbstractSettingsActivity {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
protected void onCreate(Bundle bundle) {
|
||||
super.onCreate(bundle);
|
||||
addPreferencesFromResource(R.xml.settings);
|
||||
|
||||
configPreference(R.string.settings_google_key, GoogleSettingsActivity.class);
|
||||
configPreference(R.string.settings_map_key, MapSettingsActivity.class);
|
||||
configPreference(R.string.settings_chart_key, ChartSettingsActivity.class);
|
||||
configPreference(R.string.settings_stats_key, StatsSettingsActivity.class);
|
||||
configPreference(R.string.settings_recording_key, RecordingSettingsActivity.class);
|
||||
configPreference(R.string.settings_sharing_key, SharingSettingsActivity.class);
|
||||
configPreference(R.string.settings_sensor_key, SensorSettingsActivity.class);
|
||||
configPreference(R.string.settings_backup_reset_key, BackupResetSettingsActivity.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures a preference by starting a new activity when it is clicked.
|
||||
*
|
||||
* @param key the preference key
|
||||
* @param cl the class to start the new activity
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
private void configPreference(int key, final Class<?> cl) {
|
||||
Preference preference = findPreference(getString(key));
|
||||
preference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference pref) {
|
||||
Intent intent = IntentUtils.newIntent(SettingsActivity.this, cl);
|
||||
startActivity(intent);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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 allowAccessCheckBoxPreference;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
protected void onCreate(Bundle bundle) {
|
||||
super.onCreate(bundle);
|
||||
addPreferencesFromResource(R.xml.sharing_settings);
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user