mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-02 17:43:06 +02:00
Use a custom dialog to reset preferences.
This commit is contained in:
@@ -135,7 +135,6 @@ limitations under the License.
|
||||
android:value=".SearchListActivity" />
|
||||
</activity>
|
||||
<activity android:name=".io.file.exporter.SaveActivity" />
|
||||
<activity android:name=".settings.AdvancedSettingsActivity" />
|
||||
<activity android:name=".settings.ChartSettingsActivity" />
|
||||
<activity android:name=".settings.RecordingSettingsActivity" />
|
||||
<activity android:name=".settings.SensorSettingsActivity" />
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package de.dennisguse.opentracks;
|
||||
|
||||
import android.app.Application;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
import de.dennisguse.opentracks.util.PreferencesUtils;
|
||||
|
||||
public class InitPreferences extends Application {
|
||||
@Override
|
||||
@@ -9,11 +10,6 @@ public class InitPreferences extends Application {
|
||||
super.onCreate();
|
||||
|
||||
//Set default values of preferences on first start.
|
||||
PreferenceManager.setDefaultValues(this, R.xml.settings, false);
|
||||
PreferenceManager.setDefaultValues(this, R.xml.settings_advanced, false);
|
||||
PreferenceManager.setDefaultValues(this, R.xml.settings_chart, false);
|
||||
PreferenceManager.setDefaultValues(this, R.xml.settings_recording, false);
|
||||
PreferenceManager.setDefaultValues(this, R.xml.settings_sensors, false);
|
||||
PreferenceManager.setDefaultValues(this, R.xml.settings_statistics, false);
|
||||
PreferencesUtils.resetPreferences(this, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,158 +0,0 @@
|
||||
/*
|
||||
* 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 de.dennisguse.opentracks.settings;
|
||||
|
||||
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;
|
||||
|
||||
import de.dennisguse.opentracks.Constants;
|
||||
import de.dennisguse.opentracks.R;
|
||||
import de.dennisguse.opentracks.util.DialogUtils;
|
||||
import de.dennisguse.opentracks.util.PreferencesUtils;
|
||||
|
||||
/**
|
||||
* An activity for advanced settings.
|
||||
*
|
||||
* @author Jimmy Shih
|
||||
*/
|
||||
public class AdvancedSettingsActivity extends AbstractSettingsActivity {
|
||||
|
||||
private static final String TAG = AdvancedSettingsActivity.class.getSimpleName();
|
||||
|
||||
private static final int DIALOG_CONFIRM_RESET_ID = 1;
|
||||
|
||||
private Preference resetPreference;
|
||||
|
||||
private SharedPreferences sharedPreferences;
|
||||
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(
|
||||
AdvancedSettingsActivity.this, R.string.recording_track_id_key))) {
|
||||
recordingTrackId = PreferencesUtils.getLong(
|
||||
AdvancedSettingsActivity.this, R.string.recording_track_id_key);
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateUi();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle bundle) {
|
||||
super.onCreate(bundle);
|
||||
addPreferencesFromResource(R.xml.settings_advanced);
|
||||
|
||||
sharedPreferences = PreferencesUtils.getSharedPreferences(this);
|
||||
|
||||
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();
|
||||
sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
|
||||
recordingTrackId = PreferencesUtils.getLong(this, R.string.recording_track_id_key);
|
||||
updateUi();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
sharedPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Dialog onCreateDialog(int id) {
|
||||
switch (id) {
|
||||
case DIALOG_CONFIRM_RESET_ID:
|
||||
return DialogUtils.createConfirmationDialog(this, R.string.settings_reset_confirm_title,
|
||||
getString(R.string.settings_reset_confirm_message),
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface d, int button) {
|
||||
onResetPreferencesConfirmed();
|
||||
}
|
||||
});
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the UI based on the recording state.
|
||||
*/
|
||||
private void updateUi() {
|
||||
boolean isRecording = recordingTrackId != PreferencesUtils.RECORDING_TRACK_ID_DEFAULT;
|
||||
resetPreference.setEnabled(!isRecording);
|
||||
resetPreference.setSummary(isRecording ? getString(R.string.settings_not_while_recording) : "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 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().apply();
|
||||
|
||||
// Give UI feedback in the UI thread
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Toast.makeText(AdvancedSettingsActivity.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,33 @@
|
||||
package de.dennisguse.opentracks.settings;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.preference.DialogPreference;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
import de.dennisguse.opentracks.R;
|
||||
import de.dennisguse.opentracks.util.PreferencesUtils;
|
||||
|
||||
public class ResetDialogPreference extends DialogPreference {
|
||||
|
||||
public ResetDialogPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected View onCreateDialogView() {
|
||||
return super.onCreateDialogView();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
super.onClick(dialog, which);
|
||||
if (which == DialogInterface.BUTTON_POSITIVE) {
|
||||
PreferencesUtils.resetPreferences(getContext(), true);
|
||||
|
||||
Toast.makeText(getContext(), R.string.settings_reset_done, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,12 +17,14 @@
|
||||
package de.dennisguse.opentracks.settings;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.preference.Preference;
|
||||
import android.preference.Preference.OnPreferenceClickListener;
|
||||
|
||||
import de.dennisguse.opentracks.util.IntentUtils;
|
||||
import de.dennisguse.opentracks.R;
|
||||
import de.dennisguse.opentracks.util.IntentUtils;
|
||||
import de.dennisguse.opentracks.util.PreferencesUtils;
|
||||
|
||||
/**
|
||||
* An activity for accessing settings.
|
||||
@@ -31,6 +33,27 @@ import de.dennisguse.opentracks.R;
|
||||
*/
|
||||
public class SettingsActivity extends AbstractSettingsActivity {
|
||||
|
||||
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 SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
|
||||
if (key != null && key.equals(PreferencesUtils.getKey(SettingsActivity.this, R.string.recording_track_id_key))) {
|
||||
recordingTrackId = PreferencesUtils.getLong(SettingsActivity.this, R.string.recording_track_id_key);
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateUI();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle bundle) {
|
||||
super.onCreate(bundle);
|
||||
@@ -41,7 +64,20 @@ public class SettingsActivity extends AbstractSettingsActivity {
|
||||
configPreference(R.string.settings_stats_key, StatsSettingsActivity.class);
|
||||
configPreference(R.string.settings_recording_key, RecordingSettingsActivity.class);
|
||||
configPreference(R.string.settings_sensor_key, SensorSettingsActivity.class);
|
||||
configPreference(R.string.settings_advanced_key, AdvancedSettingsActivity.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
PreferencesUtils.getSharedPreferences(this).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
|
||||
recordingTrackId = PreferencesUtils.getLong(this, R.string.recording_track_id_key);
|
||||
updateUI();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
PreferencesUtils.getSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,4 +97,12 @@ public class SettingsActivity extends AbstractSettingsActivity {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void updateUI() {
|
||||
//TODO Remove the following if recordingTrackId is replaced by direct communication rather than via preferences.
|
||||
boolean isRecording = recordingTrackId != PreferencesUtils.RECORDING_TRACK_ID_DEFAULT;
|
||||
Preference resetPreference = findPreference(getString(R.string.settings_reset_key));
|
||||
resetPreference.setEnabled(!isRecording);
|
||||
resetPreference.setSummary(isRecording ? getString(R.string.settings_not_while_recording) : "");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,6 +71,7 @@ public class PreferencesUtils {
|
||||
public static final int RECORDING_GPS_ACCURACY_EXCELLENT = 10;
|
||||
public static final int RECORDING_GPS_ACCURACY_POOR = 2000;
|
||||
|
||||
@Deprecated //NOTE: is at the moment still used to determine if a track is currently recorded; better ask the service directly.
|
||||
public static final long RECORDING_TRACK_ID_DEFAULT = -1L;
|
||||
public static final boolean RECORDING_TRACK_PAUSED_DEFAULT = true;
|
||||
|
||||
@@ -246,4 +247,12 @@ public class PreferencesUtils {
|
||||
public static boolean isChartByDistance(Context context) {
|
||||
return CHART_X_AXIS_DEFAULT.equals(getString(context, R.string.chart_x_axis_key, CHART_X_AXIS_DEFAULT));
|
||||
}
|
||||
|
||||
public static void resetPreferences(Context context, boolean readAgain) {
|
||||
PreferenceManager.setDefaultValues(context, R.xml.settings, readAgain);
|
||||
PreferenceManager.setDefaultValues(context, R.xml.settings_chart, readAgain);
|
||||
PreferenceManager.setDefaultValues(context, R.xml.settings_recording, readAgain);
|
||||
PreferenceManager.setDefaultValues(context, R.xml.settings_sensors, readAgain);
|
||||
PreferenceManager.setDefaultValues(context, R.xml.settings_statistics, readAgain);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,10 +31,16 @@ limitations under the License.
|
||||
android:key="@string/settings_sensor_key"
|
||||
android:persistent="false"
|
||||
android:title="@string/settings_sensor" />
|
||||
<Preference
|
||||
android:key="@string/settings_advanced_key"
|
||||
android:persistent="false"
|
||||
android:title="@string/settings_advanced" />
|
||||
<PreferenceCategory android:title="@string/settings_reset">
|
||||
<de.dennisguse.opentracks.settings.ResetDialogPreference
|
||||
android:dialogMessage="@string/settings_reset_confirm_message"
|
||||
android:dialogTitle="@string/settings_reset_confirm_title"
|
||||
android:key="@string/settings_reset_key"
|
||||
android:negativeButtonText="@string/generic_cancel"
|
||||
android:persistent="false"
|
||||
android:positiveButtonText="@string/generic_yes"
|
||||
android:title="@string/settings_reset" />
|
||||
</PreferenceCategory>
|
||||
<PreferenceCategory android:title="@string/about_preference_title">
|
||||
<Preference
|
||||
android:title="@string/about_preference_title">
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?><!--
|
||||
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.
|
||||
-->
|
||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:title="@string/settings_advanced">
|
||||
<Preference
|
||||
android:key="@string/settings_reset_key"
|
||||
android:persistent="false"
|
||||
android:title="@string/settings_reset" />
|
||||
</PreferenceScreen>
|
||||
Reference in New Issue
Block a user