trkpt/app/src/main/java/org/y20k/trackbook/SettingsFragment.kt

264 lines
12 KiB
Kotlin
Raw Normal View History

2020-01-02 17:00:37 +00:00
/*
* SettingsFragment.kt
* Implements the SettingsFragment fragment
* A SettingsFragment displays the user accessible settings of the app
*
* This file is part of
* TRACKBOOK - Movement Recorder for Android
*
* Copyright (c) 2016-20 - Y20K.org
* Licensed under the MIT-License
* http://opensource.org/licenses/MIT
*
* Trackbook uses osmdroid - OpenStreetMap-Tools for Android
* https://github.com/osmdroid/osmdroid
*/
package org.y20k.trackbook
import YesNoDialog
import android.content.ClipData
import android.content.ClipboardManager
2020-01-02 17:00:37 +00:00
import android.content.Context
import android.content.Intent
import android.net.Uri
2020-01-02 17:00:37 +00:00
import android.os.Bundle
import android.view.View
import android.widget.Toast
2020-01-02 17:00:37 +00:00
import androidx.preference.*
import kotlinx.coroutines.*
import org.y20k.trackbook.core.Tracklist
2020-01-28 16:39:45 +00:00
import org.y20k.trackbook.helpers.AppThemeHelper
import org.y20k.trackbook.helpers.FileHelper
2020-01-02 17:00:37 +00:00
import org.y20k.trackbook.helpers.LengthUnitHelper
import org.y20k.trackbook.helpers.LogHelper
/*
* SettingsFragment class
*/
class SettingsFragment : PreferenceFragmentCompat(), YesNoDialog.YesNoDialogListener {
2020-01-02 17:00:37 +00:00
/* Define log tag */
private val TAG: String = LogHelper.makeLogTag(SettingsFragment::class.java)
/* Overrides onViewCreated from PreferenceFragmentCompat */
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// set the background color
view.setBackgroundColor(resources.getColor(R.color.app_window_background, null))
// add padding - necessary because translucent status bar is used
val topPadding = this.resources.displayMetrics.density * 24 // 24 dp * display density
view.setPadding(0, topPadding.toInt(), 0, 0)
}
/* Overrides onCreatePreferences from PreferenceFragmentCompat */
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
val context = preferenceManager.context
val screen = preferenceManager.createPreferenceScreen(context)
// set up "Restrict to GPS" preference
2020-08-01 01:04:52 +00:00
val preferenceGpsOnly = SwitchPreferenceCompat(activity as Context)
2020-01-02 17:00:37 +00:00
preferenceGpsOnly.title = getString(R.string.pref_gps_only_title)
preferenceGpsOnly.setIcon(R.drawable.ic_gps_24dp)
2020-01-02 17:00:37 +00:00
preferenceGpsOnly.key = Keys.PREF_GPS_ONLY
preferenceGpsOnly.summaryOn = getString(R.string.pref_gps_only_summary_gps_only)
preferenceGpsOnly.summaryOff = getString(R.string.pref_gps_only_summary_gps_and_network)
preferenceGpsOnly.setDefaultValue(false)
2020-01-09 12:23:26 +00:00
// set up "Use Imperial Measurements" preference
2020-08-01 01:04:52 +00:00
val preferenceImperialMeasurementUnits = SwitchPreferenceCompat(activity as Context)
2020-08-01 00:06:53 +00:00
preferenceImperialMeasurementUnits.title =
getString(R.string.pref_imperial_measurement_units_title)
preferenceImperialMeasurementUnits.setIcon(R.drawable.ic_square_foot_24px)
2020-01-09 12:23:26 +00:00
preferenceImperialMeasurementUnits.key = Keys.PREF_USE_IMPERIAL_UNITS
2020-08-01 00:06:53 +00:00
preferenceImperialMeasurementUnits.summaryOn =
getString(R.string.pref_imperial_measurement_units_summary_imperial)
preferenceImperialMeasurementUnits.summaryOff =
getString(R.string.pref_imperial_measurement_units_summary_metric)
2020-01-09 12:23:26 +00:00
preferenceImperialMeasurementUnits.setDefaultValue(LengthUnitHelper.useImperialUnits())
2020-01-28 16:39:45 +00:00
// set up "App Theme" preference
2020-08-01 01:04:52 +00:00
val preferenceThemeSelection = ListPreference(activity as Context)
2020-01-28 16:39:45 +00:00
preferenceThemeSelection.title = getString(R.string.pref_theme_selection_title)
preferenceThemeSelection.setIcon(R.drawable.ic_smartphone_24dp)
2020-01-28 16:39:45 +00:00
preferenceThemeSelection.key = Keys.PREF_THEME_SELECTION
2020-08-01 00:06:53 +00:00
preferenceThemeSelection.summary =
"${getString(R.string.pref_theme_selection_summary)} ${AppThemeHelper.getCurrentTheme(
activity as Context
)}"
preferenceThemeSelection.entries = arrayOf(
getString(R.string.pref_theme_selection_mode_device_default),
getString(R.string.pref_theme_selection_mode_light),
getString(R.string.pref_theme_selection_mode_dark)
)
preferenceThemeSelection.entryValues = arrayOf(
Keys.STATE_THEME_FOLLOW_SYSTEM,
Keys.STATE_THEME_LIGHT_MODE,
Keys.STATE_THEME_DARK_MODE
)
2020-01-28 16:39:45 +00:00
preferenceThemeSelection.setOnPreferenceChangeListener { preference, newValue ->
if (preference is ListPreference) {
val index: Int = preference.entryValues.indexOf(newValue)
2020-08-01 00:06:53 +00:00
preferenceThemeSelection.summary =
2020-08-01 01:04:52 +00:00
"${getString(R.string.pref_theme_selection_summary)} ${preference.entries[index]}"
2020-01-28 16:39:45 +00:00
return@setOnPreferenceChangeListener true
} else {
return@setOnPreferenceChangeListener false
}
}
// set up "Delete Non-Starred" preference
2020-08-01 01:04:52 +00:00
val preferenceDeleteNonStarred = Preference(activity as Context)
preferenceDeleteNonStarred.title = getString(R.string.pref_delete_non_starred_title)
preferenceDeleteNonStarred.setIcon(R.drawable.ic_delete_24dp)
preferenceDeleteNonStarred.summary = getString(R.string.pref_delete_non_starred_summary)
2020-08-01 00:06:53 +00:00
preferenceDeleteNonStarred.setOnPreferenceClickListener {
YesNoDialog(this as YesNoDialog.YesNoDialogListener).show(
context = activity as Context,
type = Keys.DIALOG_DELETE_NON_STARRED,
message = R.string.dialog_yes_no_message_delete_non_starred,
yesButton = R.string.dialog_yes_no_positive_button_delete_non_starred
)
return@setOnPreferenceClickListener true
}
2020-01-28 16:39:45 +00:00
2020-01-02 17:00:37 +00:00
// set up "Accuracy Threshold" preference
2020-08-01 01:04:52 +00:00
val preferenceAccuracyThreshold = SeekBarPreference(activity as Context)
2020-01-02 17:00:37 +00:00
preferenceAccuracyThreshold.title = getString(R.string.pref_accuracy_threshold_title)
preferenceAccuracyThreshold.setIcon(R.drawable.ic_timeline_24dp)
2020-01-02 17:00:37 +00:00
preferenceAccuracyThreshold.key = Keys.PREF_LOCATION_ACCURACY_THRESHOLD
preferenceAccuracyThreshold.summary = getString(R.string.pref_accuracy_threshold_summary)
preferenceAccuracyThreshold.showSeekBarValue = true
preferenceAccuracyThreshold.max = 50
preferenceAccuracyThreshold.setDefaultValue(Keys.DEFAULT_THRESHOLD_LOCATION_ACCURACY)
// set up "Reset" preference
2020-08-01 01:04:52 +00:00
val preferenceResetAdvanced = Preference(activity as Context)
2020-01-02 17:00:37 +00:00
preferenceResetAdvanced.title = getString(R.string.pref_reset_advanced_title)
preferenceResetAdvanced.setIcon(R.drawable.ic_undo_24dp)
2020-01-02 17:00:37 +00:00
preferenceResetAdvanced.summary = getString(R.string.pref_reset_advanced_summary)
2020-08-01 00:06:53 +00:00
preferenceResetAdvanced.setOnPreferenceClickListener {
2020-01-02 17:00:37 +00:00
preferenceAccuracyThreshold.value = Keys.DEFAULT_THRESHOLD_LOCATION_ACCURACY
return@setOnPreferenceClickListener true
}
// set up "App Version" preference
2020-08-01 01:04:52 +00:00
val preferenceAppVersion = Preference(context)
preferenceAppVersion.title = getString(R.string.pref_app_version_title)
preferenceAppVersion.setIcon(R.drawable.ic_info_24dp)
2020-08-01 00:06:53 +00:00
preferenceAppVersion.summary =
"${getString(R.string.pref_app_version_summary)} ${BuildConfig.VERSION_NAME} (${getString(
R.string.app_version_name
)})"
preferenceAppVersion.setOnPreferenceClickListener {
// copy to clipboard
val clip: ClipData = ClipData.newPlainText("simple text", preferenceAppVersion.summary)
2020-08-01 00:06:53 +00:00
val cm: ClipboardManager =
context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
cm.setPrimaryClip(clip)
2020-08-01 00:06:53 +00:00
Toast.makeText(
activity as Context,
R.string.toast_message_copied_to_clipboard,
Toast.LENGTH_LONG
).show()
return@setOnPreferenceClickListener true
}
// set up "Report Issue" preference
2020-08-01 01:04:52 +00:00
val preferenceReportIssue = Preference(context)
preferenceReportIssue.title = getString(R.string.pref_report_issue_title)
preferenceReportIssue.setIcon(R.drawable.ic_bug_report_24dp)
preferenceReportIssue.summary = getString(R.string.pref_report_issue_summary)
preferenceReportIssue.setOnPreferenceClickListener {
// open web browser
val intent = Intent().apply {
action = Intent.ACTION_VIEW
data = Uri.parse("https://github.com/y20k/trackbook/issues")
}
startActivity(intent)
return@setOnPreferenceClickListener true
}
2020-01-02 17:00:37 +00:00
// set preference categories
2020-08-01 01:04:52 +00:00
val preferenceCategoryGeneral = PreferenceCategory(activity as Context)
2020-01-02 17:00:37 +00:00
preferenceCategoryGeneral.title = getString(R.string.pref_general_title)
preferenceCategoryGeneral.contains(preferenceImperialMeasurementUnits)
preferenceCategoryGeneral.contains(preferenceGpsOnly)
2020-08-01 01:04:52 +00:00
val preferenceCategoryMaintenance =
2020-08-01 00:06:53 +00:00
PreferenceCategory(activity as Context)
preferenceCategoryMaintenance.title = getString(R.string.pref_maintenance_title)
preferenceCategoryMaintenance.contains(preferenceDeleteNonStarred)
2020-08-01 01:04:52 +00:00
val preferenceCategoryAdvanced = PreferenceCategory(activity as Context)
2020-01-02 17:00:37 +00:00
preferenceCategoryAdvanced.title = getString(R.string.pref_advanced_title)
preferenceCategoryAdvanced.contains(preferenceAccuracyThreshold)
preferenceCategoryAdvanced.contains(preferenceResetAdvanced)
2020-08-01 01:04:52 +00:00
val preferenceCategoryAbout = PreferenceCategory(context)
preferenceCategoryAbout.title = getString(R.string.pref_about_title)
preferenceCategoryAbout.contains(preferenceAppVersion)
preferenceCategoryAbout.contains(preferenceReportIssue)
2020-01-02 17:00:37 +00:00
// setup preference screen
screen.addPreference(preferenceCategoryGeneral)
screen.addPreference(preferenceGpsOnly)
2020-01-09 12:23:26 +00:00
screen.addPreference(preferenceImperialMeasurementUnits)
2020-01-28 16:39:45 +00:00
screen.addPreference(preferenceThemeSelection)
screen.addPreference(preferenceCategoryMaintenance)
screen.addPreference(preferenceDeleteNonStarred)
2020-01-02 17:00:37 +00:00
screen.addPreference(preferenceCategoryAdvanced)
screen.addPreference(preferenceAccuracyThreshold)
screen.addPreference(preferenceResetAdvanced)
screen.addPreference(preferenceCategoryAbout)
screen.addPreference(preferenceAppVersion)
screen.addPreference(preferenceReportIssue)
2020-01-02 17:00:37 +00:00
preferenceScreen = screen
}
/* Overrides onYesNoDialog from YesNoDialogListener */
2020-08-01 00:06:53 +00:00
override fun onYesNoDialog(
type: Int,
dialogResult: Boolean,
payload: Int,
payloadString: String
) {
when (type) {
Keys.DIALOG_DELETE_NON_STARRED -> {
when (dialogResult) {
// user tapped delete
true -> {
deleteNonStarred(activity as Context)
}
}
}
else -> {
super.onYesNoDialog(type, dialogResult, payload, payloadString)
}
}
}
/* Removes track and track files for given position - used by TracklistFragment */
2020-08-01 01:04:52 +00:00
private fun deleteNonStarred(context: Context) {
val backgroundJob = Job()
val uiScope = CoroutineScope(Dispatchers.Main + backgroundJob)
uiScope.launch {
var tracklist: Tracklist = FileHelper.readTracklist(context)
2020-08-01 00:06:53 +00:00
val deferred: Deferred<Tracklist> =
async { FileHelper.deleteNonStarredSuspended(context, tracklist) }
// wait for result and store in tracklist
tracklist = deferred.await()
backgroundJob.cancel()
}
}
2020-01-02 17:00:37 +00:00
}