-
-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathPreferences.java
More file actions
372 lines (324 loc) · 12 KB
/
Preferences.java
File metadata and controls
372 lines (324 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package net.osmtracker.activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.preference.EditTextPreference;
import androidx.preference.EditTextPreferenceDialogFragmentCompat;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import net.osmtracker.OSMTracker;
import net.osmtracker.R;
import java.io.File;
/**
* Manages preferences screen
*/
public class Preferences extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
}
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
private static final String EXTRA_DEFAULT_VALUE = "DEFAULT_VALUE";
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(requireContext());
// General settings
setupVoiceRecDuration();
// Notes
setupListPreference(
OSMTracker.Preferences.KEY_USE_NOTES,
getString(R.string.prefs_notes_summary)
);
// OSM track visibility
setupListPreference(
OSMTracker.Preferences.KEY_OSM_TRACK_VISIBILITY,
getString(R.string.prefs_osm_visibility_summary)
);
setupOSMAuthClearData(prefs);
// GPS Settings
//Open Android GPS Settings screen
setupPreferenceNavigation(
OSMTracker.Preferences.KEY_GPS_OSSETTINGS,
new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
//GPSLogging Interval
setupEditTextNum(
OSMTracker.Preferences.KEY_GPS_LOGGING_INTERVAL,
getString(R.string.prefs_gps_logging_interval_seconds),
getString(R.string.prefs_gps_logging_interval_summary),
getString(R.string.prefs_gps_logging_interval_empty),
OSMTracker.Preferences.VAL_GPS_LOGGING_INTERVAL
);
//GPS Logging Min Distance
setupEditTextNum(
OSMTracker.Preferences.KEY_GPS_LOGGING_MIN_DISTANCE,
getString(R.string.prefs_gps_logging_min_distance_meters),
getString(R.string.prefs_gps_logging_min_distance_summary),
getString(R.string.prefs_gps_logging_min_distance_empty),
OSMTracker.Preferences.VAL_GPS_LOGGING_MIN_DISTANCE
);
// GPS Logging Minimum Accuracy
setupEditTextNum(
OSMTracker.Preferences.KEY_GPS_LOGGING_MIN_ACCURACY,
getString(R.string.prefs_gps_logging_min_distance_meters),
getString(R.string.prefs_gps_min_accuracy_summary),
getString(R.string.prefs_gps_min_accuracy_empty),
OSMTracker.Preferences.VAL_GPS_LOGGING_MIN_ACCURACY
);
// GPX Settings
setupStorageDirectory();
//Filename
setupListPreference(
OSMTracker.Preferences.KEY_OUTPUT_FILENAME,
getString(R.string.prefs_output_filename_summary)
);
//Accuracy
setupListPreference(
OSMTracker.Preferences.KEY_OUTPUT_ACCURACY,
getString(R.string.prefs_output_accuracy_summary)
);
//Compas Heading
setupListPreference(
OSMTracker.Preferences.KEY_OUTPUT_COMPASS,
getString(R.string.prefs_compass_heading_summary)
);
// User Interface Settings
// Camera
setupListPreference(
OSMTracker.Preferences.KEY_UI_PICTURE_SOURCE,
getString(R.string.prefs_ui_picture_source_summary)
);
// App Theme
setupListPreference(
OSMTracker.Preferences.KEY_UI_THEME,
getString(R.string.prefs_theme_summary)
);
//Explicit execution of buttons presets window
setupPreferenceNavigation(
OSMTracker.Preferences.KEY_UI_BUTTONS_LAYOUT,
new Intent(requireContext(), ButtonsPresets.class));
//Map tile provider
setupListPreference(
OSMTracker.Preferences.KEY_UI_MAP_TILE,
getString(R.string.prefs_map_tile_summary)
);
// Screen Orientation
setupListPreference(
OSMTracker.Preferences.KEY_UI_ORIENTATION,
getString(R.string.prefs_ui_orientation_summary)
);
}
/**
*
*/
private void setupStorageDirectory() {
// External storage directory
EditTextPreference storageDirPref = findPreference(
OSMTracker.Preferences.KEY_STORAGE_DIR);
if (storageDirPref == null) return;
// Set summary provider
storageDirPref.setSummaryProvider(preference -> {
String val = storageDirPref.getText();
if (TextUtils.isEmpty(val)) {
return OSMTracker.Preferences.VAL_STORAGE_DIR;
}
return val;
});
// Enforce the leading slash
storageDirPref.setOnPreferenceChangeListener((preference, newValue) -> {
String val = newValue.toString().trim();
// Empty
if (TextUtils.isEmpty(val)) {
Toast.makeText(requireContext(),
R.string.prefs_storage_dir_empty,
Toast.LENGTH_SHORT).show();
return false;
}
// Ensure there is always a leading slash
if (!val.startsWith(File.separator)) {
String fixedVal = File.separator + val;
((EditTextPreference) preference).setText(fixedVal);
return false; //ignores the user input
}
return true;
});
}
/**
* Voice record duration: set a custom SummaryProvider
*/
private void setupVoiceRecDuration() {
Preference voiceRec = findPreference(OSMTracker.Preferences.KEY_VOICEREC_DURATION);
if (voiceRec == null) return;
voiceRec.setSummaryProvider(
(Preference.SummaryProvider<ListPreference>) preference -> {
// Return your combined string
return preference.getEntry() + " "
+ getString(R.string.prefs_voicerec_duration_seconds);
});
}
/**
* Clear OSM data: Disable if there's no OSM data stored
*
* @param prefs SharedPreferences
*/
private void setupOSMAuthClearData(SharedPreferences prefs) {
Preference OSMAuthClearData = findPreference(
OSMTracker.Preferences.KEY_OSM_OAUTH_CLEAR_DATA);
if (OSMAuthClearData == null) return;
String tokenKey = OSMTracker.Preferences.KEY_OSM_OAUTH2_ACCESSTOKEN;
OSMAuthClearData.setEnabled(prefs.contains(tokenKey));
// Set a Click Listener to show the confirmation dialog
OSMAuthClearData.setOnPreferenceClickListener(preference -> {
new androidx.appcompat.app.AlertDialog.Builder(requireContext())
.setTitle(preference.getTitle())
.setMessage(R.string.prefs_osm_clear_oauth_data_dialog)
.setIcon(preference.getIcon())
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
// User clicked OK: Clear the data
prefs.edit().remove(tokenKey).apply();
// Disable the button now that data is gone
preference.setEnabled(false);
})
.setNegativeButton(android.R.string.cancel, null)
.show();
return true;
});
}
/**
* Set up a preference that launches an activity via Intent
* @param preferenceKey The preference key
* @param intent The intent to launch
*/
private void setupPreferenceNavigation(String preferenceKey, Intent intent) {
Preference preference = findPreference(preferenceKey);
if (preference == null) return;
preference.setOnPreferenceClickListener(p -> {
startActivity(intent);
return true;
});
}
/**
*
* @param preferenceKey from OSMTracker.Preferences
* @param valueSuffix appended to the end of the value, shown in the summary
* @param summary static summary to be appended to the end of the summary
* @param validationError in case of empty value
* @param defaultValue value to be used for the reset button
*/
private void setupEditTextNum(String preferenceKey, String valueSuffix, String summary,
String validationError, String defaultValue) {
EditTextPreference numInputPref = findPreference(preferenceKey);
if (numInputPref == null) return;
// Store default value in Extras so it can be retrieved by the Reset Dialog
numInputPref.getExtras().putString(EXTRA_DEFAULT_VALUE, defaultValue);
// Set input type to number and move cursor to the end
numInputPref.setOnBindEditTextListener(editText -> {
editText.setInputType(android.text.InputType.TYPE_CLASS_NUMBER);
editText.setSelection(editText.getText().length());
});
// Set summary provider
numInputPref.setSummaryProvider(preference -> {
EditTextPreference editTextPreference = (EditTextPreference) preference;
return editTextPreference.getText() + " " + valueSuffix + ". " + summary;
});
numInputPref.setOnPreferenceChangeListener((preference, newValue) -> {
String val = (String) newValue;
if (TextUtils.isEmpty(val)) {
Toast.makeText(requireContext(), validationError, Toast.LENGTH_SHORT).show();
return false;
}
return true;
});
}
@SuppressWarnings("deprecation") // Required to link the dialog to the fragment
@Override
public void onDisplayPreferenceDialog(Preference preference) {
// Retrieve the default value defined in extras.
// If null, it means this preference doesn't support the reset feature.
// Fallback to the default dialog behavior.
String defaultValue = preference.getExtras().getString(EXTRA_DEFAULT_VALUE);
if (defaultValue == null) {
super.onDisplayPreferenceDialog(preference);
return;
}
// Create the standard dialog fragment
final EditTextPreferenceDialogFragmentCompat dialogFragment =
EditTextPreferenceDialogFragmentCompat.newInstance(preference.getKey());
dialogFragment.setTargetFragment(this, 0);
dialogFragment.show(
getParentFragmentManager(),
"androidx.preference.PreferenceFragment.DIALOG");
// Inject the button after the dialog is shown
getParentFragmentManager().registerFragmentLifecycleCallbacks(
new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentStarted(
@androidx.annotation.NonNull FragmentManager fm,
@androidx.annotation.NonNull androidx.fragment.app.Fragment f) {
if (f == dialogFragment) {
android.app.Dialog dialog = dialogFragment.getDialog();
if (dialog instanceof androidx.appcompat.app.AlertDialog alertDialog) {
// Configure the Neutral Button for reset default value
Button btnReset = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
btnReset.setText(R.string.prefs_reset_default_value);
btnReset.setVisibility(android.view.View.VISIBLE);
btnReset.setOnClickListener(v -> {
if (preference instanceof EditTextPreference) {
((EditTextPreference) preference).setText(defaultValue);
alertDialog.dismiss();
}
});
}
// Cleanup
getParentFragmentManager().unregisterFragmentLifecycleCallbacks(this);
}
}
}, false);
}
/**
* Set up a ListPreference with a custom two lines summary, displays the selected entry
* on the first line, and the static summary on the second line.
*
* @param preferenceKey preference identifier
* @param staticSummary text to show on the second line
*/
private void setupListPreference(String preferenceKey, String staticSummary) {
ListPreference listPref = findPreference(preferenceKey);
if (listPref == null) return;
listPref.setSummaryProvider(preference -> {
ListPreference lp = (ListPreference) preference;
CharSequence entry = lp.getEntry();
// Handle cases where no value has been selected yet. (backwards compatibility)
String displayValue;
if (entry == null || TextUtils.isEmpty(entry)) {
// Fallback text if no value is set.
displayValue = getString(R.string.prefs_not_set);
} else {
displayValue = entry.toString();
}
return displayValue + ".\n" + staticSummary;
});
}
}
}