Skip to content

Commit

Permalink
Add initial preference UI for the output encoding format
Browse files Browse the repository at this point in the history
Issue: #21
Signed-off-by: Andrew Gunnerson <chillermillerlong@hotmail.com>
  • Loading branch information
chenxiaolong committed May 26, 2022
1 parent c9b35f4 commit 955aeef
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
28 changes: 24 additions & 4 deletions app/src/main/java/com/chiller3/bcr/Preferences.kt
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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()
}
}
29 changes: 29 additions & 0 deletions app/src/main/java/com/chiller3/bcr/SettingsActivity.kt
Expand Up @@ -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?) {
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -151,6 +175,11 @@ class SettingsActivity : AppCompatActivity() {
refreshOutputDir()
return true
}
prefOutputFormat -> {
Preferences.resetAllCodecs(requireContext())
refreshOutputFormat()
return true
}
}

return false
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -13,6 +13,9 @@
<string name="pref_output_dir_name">Output directory</string>
<string name="pref_output_dir_desc">Pick a directory to store recordings. Long press to reset to the default directory.</string>

<string name="pref_output_format_name">Output format</string>
<string name="pref_output_format_desc">Select an encoding format for the recordings. Long press to reset all codec settings to the default.</string>

<string name="pref_inhibit_batt_opt_name">Disable battery optimization</string>
<string name="pref_inhibit_batt_opt_desc">Reduces the chance of the app being killed by the system.</string>

Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/xml/root_preferences.xml
Expand Up @@ -17,6 +17,14 @@
app:summary="@string/pref_output_dir_desc"
app:iconSpaceReserved="false" />

<com.chiller3.bcr.LongClickablePreference
app:dependency="call_recording"
app:key="output_format"
app:persistent="false"
app:title="@string/pref_output_format_name"
app:summary="@string/pref_output_format_desc"
app:iconSpaceReserved="false" />

<SwitchPreferenceCompat
app:key="inhibit_batt_opt"
app:title="@string/pref_inhibit_batt_opt_name"
Expand Down

0 comments on commit 955aeef

Please sign in to comment.