Skip to content

Commit

Permalink
Remove preference default initialization
Browse files Browse the repository at this point in the history
Related code improvements.
  • Loading branch information
pilot51 committed Jan 18, 2024
1 parent 26c948b commit 6ac3cb6
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 88 deletions.
9 changes: 6 additions & 3 deletions app/src/main/java/com/pilot51/voicenotify/MainScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ import com.google.accompanist.permissions.rememberPermissionState
import com.pilot51.voicenotify.NotifyList.NotificationLogDialog
import com.pilot51.voicenotify.PermissionHelper.RationaleDialog
import com.pilot51.voicenotify.PermissionHelper.requestPermission
import com.pilot51.voicenotify.PreferenceHelper.DEFAULT_AUDIO_FOCUS
import com.pilot51.voicenotify.PreferenceHelper.DEFAULT_IGNORE_EMPTY
import com.pilot51.voicenotify.PreferenceHelper.DEFAULT_IGNORE_GROUPS
import com.pilot51.voicenotify.PreferenceHelper.KEY_AUDIO_FOCUS
import com.pilot51.voicenotify.PreferenceHelper.KEY_IGNORE_EMPTY
import com.pilot51.voicenotify.PreferenceHelper.KEY_IGNORE_GROUPS
Expand Down Expand Up @@ -261,7 +264,7 @@ private fun MainScreen(
titleRes = R.string.audio_focus,
subtitleRes = R.string.audio_focus_summary,
key = KEY_AUDIO_FOCUS,
default = true
default = DEFAULT_AUDIO_FOCUS
)
PreferenceRowLink(
titleRes = R.string.shake_to_silence,
Expand All @@ -282,13 +285,13 @@ private fun MainScreen(
titleRes = R.string.ignore_empty,
subtitleRes = R.string.ignore_empty_summary_on,
key = KEY_IGNORE_EMPTY,
default = true
default = DEFAULT_IGNORE_EMPTY
)
PreferenceRowCheckbox(
titleRes = R.string.ignore_groups,
subtitleRes = R.string.ignore_groups_summary_on,
key = KEY_IGNORE_GROUPS,
default = true
default = DEFAULT_IGNORE_GROUPS
)
PreferenceRowLink(
titleRes = R.string.ignore_repeat,
Expand Down
22 changes: 8 additions & 14 deletions app/src/main/java/com/pilot51/voicenotify/NotificationInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.pilot51.voicenotify
import android.app.Notification
import android.util.Log
import androidx.core.app.NotificationCompat
import com.pilot51.voicenotify.PreferenceHelper.DEFAULT_MAX_LENGTH
import com.pilot51.voicenotify.PreferenceHelper.DEFAULT_TTS_STRING
import com.pilot51.voicenotify.PreferenceHelper.KEY_MAX_LENGTH
import com.pilot51.voicenotify.PreferenceHelper.KEY_TTS_STRING
Expand Down Expand Up @@ -124,27 +125,20 @@ data class NotificationInfo(
if (app != null && (ttsMessage == null || ttsMessage == app.label) && !isComposePreview) {
ttsMessage = appContext.getString(R.string.notification_from, app.label)
}
if (!ttsMessage.isNullOrEmpty()) {
ttsMessage?.takeIf { it.isNotEmpty() }?.let {
val ttsTextReplace = if (isComposePreview) null else {
prefs.getString(KEY_TTS_TEXT_REPLACE, null)
}
val textReplaceList = Common.convertTextReplaceStringToList(ttsTextReplace)
for (pair in textReplaceList) {
ttsMessage = ttsMessage!!.replace(
ttsMessage = it.replace(
"(?i)${Pattern.quote(pair.first)}".toRegex(), pair.second)
}
}
if (ttsMessage != null) {
try {
val maxLength = if (isComposePreview) 0 else {
prefs.getString(KEY_MAX_LENGTH, null)
?.takeIf { it.isNotEmpty() }?.toInt() ?: 0
}
if (maxLength > 0) {
ttsMessage = ttsMessage!!.substring(0, min(maxLength, ttsMessage!!.length))
}
} catch (e: NumberFormatException) {
Log.w(TAG, "Failed to parse Maximum Message: ${e.message}")
val maxLength = if (isComposePreview) 0 else {
prefs.getString(KEY_MAX_LENGTH, null)?.toIntOrNull() ?: DEFAULT_MAX_LENGTH
}
if (maxLength > 0 && maxLength < it.length) {
ttsMessage = it.substring(0, min(maxLength, it.length))
}
}
}
Expand Down
53 changes: 12 additions & 41 deletions app/src/main/java/com/pilot51/voicenotify/PreferenceHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package com.pilot51.voicenotify

import android.content.SharedPreferences
import android.media.AudioManager
import androidx.core.content.edit
import androidx.core.text.isDigitsOnly
import androidx.preference.PreferenceManager
import com.pilot51.voicenotify.VNApplication.Companion.appContext

Expand Down Expand Up @@ -75,56 +75,27 @@ object PreferenceHelper {
val prefs: SharedPreferences by lazy {
PreferenceManager.getDefaultSharedPreferences(appContext).apply {
convertOldStreamPref()
initDefaultPrefs()
}
}

/** @return The selected audio stream matching the `STREAM_` constant from [AudioManager]. */
fun getSelectedAudioStream() = prefs.getString(KEY_TTS_STREAM, null)?.toIntOrNull() ?: DEFAULT_TTS_STREAM

private fun SharedPreferences.initDefaultPrefs() {
if (!contains(KEY_SHAKE_THRESHOLD)) {
edit {
initPref(this, KEY_AUDIO_FOCUS, DEFAULT_AUDIO_FOCUS)
initPref(this, KEY_SHAKE_THRESHOLD, DEFAULT_SHAKE_THRESHOLD.toString())
initPref(this, KEY_IGNORE_EMPTY, DEFAULT_IGNORE_EMPTY)
initPref(this, KEY_IGNORE_GROUPS, DEFAULT_IGNORE_GROUPS)
initPref(this, KEY_IGNORE_REPEAT, DEFAULT_IGNORE_REPEAT.toString())
initPref(this, KEY_TTS_STRING, DEFAULT_TTS_STRING)
initPref(this, KEY_MAX_LENGTH, DEFAULT_MAX_LENGTH.toString())
initPref(this, KEY_TTS_STREAM, DEFAULT_TTS_STREAM.toString())
apply()
}
}
}

/**
* Initializes a preference to [default] if it doesn't already have a value.
* This only calls the `put` function matching [default]'s type and does not commit.
*/
private fun <T> SharedPreferences.initPref(
editor: SharedPreferences.Editor,
key: String,
default: T
) {
if (!contains(key)) {
when (default) {
is String -> editor.putString(key, default)
is Boolean -> editor.putBoolean(key, default)
}
}
}
fun getSelectedAudioStream() = prefs.getString(KEY_TTS_STREAM, null)
?.toIntOrNull() ?: DEFAULT_TTS_STREAM

/**
* If necessary, converts audio stream preference from obsolete word string to integer string.
* @since v1.0.11
*/
private fun SharedPreferences.convertOldStreamPref() {
val currentStream = getString(KEY_TTS_STREAM, null)
currentStream?.toIntOrNull() ?: run {
val newStream = if (currentStream == "notification") {
AudioManager.STREAM_NOTIFICATION
} else AudioManager.STREAM_MUSIC
getString(KEY_TTS_STREAM, null)?.takeUnless { it.isDigitsOnly() }?.let {
val newStream = when (it) {
"media" -> AudioManager.STREAM_MUSIC
"notification" -> AudioManager.STREAM_NOTIFICATION
else -> {
edit().remove(KEY_TTS_STREAM).apply()
return
}
}
edit().putString(KEY_TTS_STREAM, newStream.toString()).apply()
}
}
Expand Down
27 changes: 6 additions & 21 deletions app/src/main/java/com/pilot51/voicenotify/Service.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import com.pilot51.voicenotify.PermissionHelper.isPermissionGranted
import com.pilot51.voicenotify.PreferenceHelper.DEFAULT_AUDIO_FOCUS
import com.pilot51.voicenotify.PreferenceHelper.DEFAULT_IGNORE_EMPTY
import com.pilot51.voicenotify.PreferenceHelper.DEFAULT_IGNORE_GROUPS
import com.pilot51.voicenotify.PreferenceHelper.DEFAULT_IGNORE_REPEAT
import com.pilot51.voicenotify.PreferenceHelper.DEFAULT_IS_SUSPENDED
import com.pilot51.voicenotify.PreferenceHelper.DEFAULT_QUIET_TIME
import com.pilot51.voicenotify.PreferenceHelper.DEFAULT_SPEAK_HEADSET_OFF
Expand Down Expand Up @@ -245,35 +246,19 @@ class Service : NotificationListenerService() {
info.ignoreReasons.add(IgnoreReason.STRING_IGNORED)
}
}
val ignoreRepeat = try {
prefs.getString(KEY_IGNORE_REPEAT, null)
?.takeIf { it.isNotEmpty() }?.toInt() ?: -1
} catch (e: NumberFormatException) {
Log.w(TAG, "Failed to parse Ignore Repeats: ${e.message}")
-1
}
val ignoreRepeat = prefs.getString(KEY_IGNORE_REPEAT, DEFAULT_IGNORE_REPEAT.toString())
?.toIntOrNull() ?: -1
if (lastMsg.containsKey(app)) {
if (lastMsg[app] == ttsMsg && (ignoreRepeat == -1 || msgTime - lastMsgTime[app]!! < ignoreRepeat * 1000)) {
info.addIgnoreReasonIdentical(ignoreRepeat)
}
}
NotifyList.addNotification(info)
if (info.ignoreReasons.isEmpty()) {
val delay = try {
prefs.getString(KEY_TTS_DELAY, null)
?.takeIf { it.isNotEmpty() }?.toDouble()?.takeUnless { it < 0.0 } ?: 0.0
} catch (e: NumberFormatException) {
Log.w(TAG, "Failed to parse TTS Delay: ${e.message}")
0.0
}
val delay = prefs.getString(KEY_TTS_DELAY, null)
?.toDoubleOrNull()?.coerceAtLeast(0.0) ?: 0.0
if (!isScreenOn()) {
val interval = try {
prefs.getString(KEY_TTS_REPEAT, null)
?.takeIf { it.isNotEmpty() }?.toDouble() ?: 0.0
} catch (e: NumberFormatException) {
Log.w(TAG, "Failed to parse TTS Repeat: ${e.message}")
0.0
}
val interval = prefs.getString(KEY_TTS_REPEAT, null)?.toDoubleOrNull() ?: 0.0
if (interval > 0) {
synchronized(repeatList) { repeatList.add(info) }
if (repeater == null) {
Expand Down
13 changes: 4 additions & 9 deletions app/src/main/java/com/pilot51/voicenotify/Shake.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2023 Mark Injerd
* Copyright 2011-2024 Mark Injerd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,7 @@ import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.util.Log
import com.pilot51.voicenotify.PreferenceHelper.DEFAULT_SHAKE_THRESHOLD
import com.pilot51.voicenotify.PreferenceHelper.KEY_SHAKE_THRESHOLD
import com.pilot51.voicenotify.PreferenceHelper.prefs
import kotlin.math.abs
Expand All @@ -38,13 +38,8 @@ class Shake(context: Context) : SensorEventListener {

fun enable() {
if (onShake == null) return
threshold = try {
prefs.getString(KEY_SHAKE_THRESHOLD, null)
?.takeIf { it.isNotEmpty() }?.toDouble() ?: return
} catch (e: NumberFormatException) {
Log.w(TAG, "Failed to parse shake threshold: ${e.message}")
return
}
threshold = prefs.getString(KEY_SHAKE_THRESHOLD, DEFAULT_SHAKE_THRESHOLD.toString())
?.toDoubleOrNull() ?: return
manager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL)
}

Expand Down

0 comments on commit 6ac3cb6

Please sign in to comment.