diff --git a/app/src/main/java/com/chiller3/bcr/Preferences.kt b/app/src/main/java/com/chiller3/bcr/Preferences.kt index 035054a13..68a6dd7da 100644 --- a/app/src/main/java/com/chiller3/bcr/Preferences.kt +++ b/app/src/main/java/com/chiller3/bcr/Preferences.kt @@ -8,12 +8,15 @@ import java.io.File object Preferences { const val PREF_CALL_RECORDING = "call_recording" - const val PREF_CODEC_NAME = "codec_name" - const val PREF_CODEC_PARAM_FORMAT = "codec_param_%s" const val PREF_OUTPUT_DIR = "output_dir" + const val PREF_OUTPUT_FORMAT = "output_format" const val PREF_INHIBIT_BATT_OPT = "inhibit_batt_opt" const val PREF_VERSION = "version" + // Not associated with a UI preference + private const val PREF_CODEC_NAME = "codec_name" + private const val PREF_CODEC_PARAM_PREFIX = "codec_param_" + /** * Get the default output directory. The directory should always be writable and is suitable for * use as a fallback. @@ -127,7 +130,7 @@ object Preferences { */ fun getCodecParam(context: Context, name: String): UInt? { val prefs = PreferenceManager.getDefaultSharedPreferences(context) - val key = PREF_CODEC_PARAM_FORMAT.format(name) + val key = PREF_CODEC_PARAM_PREFIX + name // Use a sentinel value because doing contains + getInt results in TOCTOU issues val value = prefs.getInt(key, -1) @@ -153,7 +156,7 @@ object Preferences { val prefs = PreferenceManager.getDefaultSharedPreferences(context) val editor = prefs.edit() - val key = PREF_CODEC_PARAM_FORMAT.format(name) + val key = PREF_CODEC_PARAM_PREFIX + name if (param == null) { editor.remove(key) @@ -163,4 +166,21 @@ object Preferences { editor.apply() } + + /** + * Remove the default codec preference and the parameters for all codecs. + */ + fun resetAllCodecs(context: Context) { + val prefs = PreferenceManager.getDefaultSharedPreferences(context) + val keys = prefs.all.keys.filter { + it == PREF_CODEC_NAME || it.startsWith(PREF_CODEC_PARAM_PREFIX) + } + val editor = prefs.edit() + + for (key in keys) { + editor.remove(key) + } + + editor.apply() + } } \ No newline at end of file diff --git a/app/src/main/java/com/chiller3/bcr/SettingsActivity.kt b/app/src/main/java/com/chiller3/bcr/SettingsActivity.kt index 5cd44dcde..9acfe0201 100644 --- a/app/src/main/java/com/chiller3/bcr/SettingsActivity.kt +++ b/app/src/main/java/com/chiller3/bcr/SettingsActivity.kt @@ -9,6 +9,8 @@ import androidx.appcompat.app.AppCompatActivity import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import androidx.preference.SwitchPreferenceCompat +import com.chiller3.bcr.codec.CodecParamType +import com.chiller3.bcr.codec.Codecs class SettingsActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -31,6 +33,7 @@ class SettingsActivity : AppCompatActivity() { SharedPreferences.OnSharedPreferenceChangeListener { private lateinit var prefCallRecording: SwitchPreferenceCompat private lateinit var prefOutputDir: LongClickablePreference + private lateinit var prefOutputFormat: LongClickablePreference private lateinit var prefInhibitBatteryOpt: SwitchPreferenceCompat private lateinit var prefVersion: Preference @@ -71,6 +74,11 @@ class SettingsActivity : AppCompatActivity() { prefOutputDir.onPreferenceLongClickListener = this refreshOutputDir() + prefOutputFormat = findPreference(Preferences.PREF_OUTPUT_FORMAT)!! + prefOutputFormat.onPreferenceClickListener = this + prefOutputFormat.onPreferenceLongClickListener = this + refreshOutputFormat() + prefInhibitBatteryOpt = findPreference(Preferences.PREF_INHIBIT_BATT_OPT)!! prefInhibitBatteryOpt.onPreferenceChangeListener = this @@ -100,6 +108,18 @@ class SettingsActivity : AppCompatActivity() { prefOutputDir.summary = "${summary}\n\n${outputDir}" } + private fun refreshOutputFormat() { + val (codec, codecParamSaved) = Codecs.fromPreferences(requireContext()) + val codecParam = codecParamSaved ?: codec.paramDefault + val summary = getString(R.string.pref_output_format_desc) + val paramText = when (codec.paramType) { + CodecParamType.CompressionLevel -> codecParam.toString() + CodecParamType.Bitrate -> "${codecParam / 1_000u} kbps" + } + + prefOutputFormat.summary = "${summary}\n\n${codec.name} (${paramText})" + } + private fun refreshInhibitBatteryOptState() { val inhibiting = Permissions.isInhibitingBatteryOpt(requireContext()) prefInhibitBatteryOpt.isChecked = inhibiting @@ -134,6 +154,10 @@ class SettingsActivity : AppCompatActivity() { requestSafOutputDir.launch(null) return true } + prefOutputFormat -> { + // TODO: Open codec configuration dialog + return true + } prefVersion -> { val uri = Uri.parse(BuildConfig.PROJECT_URL_AT_COMMIT) startActivity(Intent(Intent.ACTION_VIEW, uri)) @@ -151,6 +175,11 @@ class SettingsActivity : AppCompatActivity() { refreshOutputDir() return true } + prefOutputFormat -> { + Preferences.resetAllCodecs(requireContext()) + refreshOutputFormat() + return true + } } return false diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b092b9bf3..f4269f432 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -13,6 +13,9 @@ Output directory Pick a directory to store recordings. Long press to reset to the default directory. + Output format + Select an encoding format for the recordings. Long press to reset all encoder settings to the default. + Disable battery optimization Reduces the chance of the app being killed by the system. diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index 3cdd2cb61..1ba70a962 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -17,6 +17,14 @@ app:summary="@string/pref_output_dir_desc" app:iconSpaceReserved="false" /> + +