-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for saving and loading codec information from shared pref…
…erences Issue: #21 Signed-off-by: Andrew Gunnerson <chillermillerlong@hotmail.com>
- Loading branch information
1 parent
5cfff11
commit 78e610e
Showing
7 changed files
with
178 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.chiller3.bcr.codec | ||
|
||
import android.content.Context | ||
import com.chiller3.bcr.Preferences | ||
|
||
object Codecs { | ||
val all: Array<Codec> = arrayOf(FlacCodec, OpusCodec, AacCodec) | ||
val default: Codec = all.first() | ||
|
||
/** Find output codec by name. */ | ||
fun getByName(name: String): Codec? = all.find { it.name == name } | ||
|
||
/** | ||
* Get the saved codec from the preferences or fall back to the default. | ||
* | ||
* The parameter, if set, is clamped to the codec's allowed parameter range. | ||
*/ | ||
fun fromPreferences(context: Context): Pair<Codec, UInt?> { | ||
val savedCodecName = Preferences.getCodecName(context) | ||
val codec = if (savedCodecName != null) { | ||
getByName(savedCodecName) ?: default | ||
} else { | ||
default | ||
} | ||
|
||
// Clamp to the codec's allowed parameter range in case the range is shrunk | ||
val param = Preferences.getCodecParam(context, codec.name)?.coerceIn(codec.paramRange) | ||
|
||
return Pair(codec, param) | ||
} | ||
|
||
/** Save the selected codec and its parameter to the preferences. */ | ||
fun saveToPreferences(context: Context, codec: Codec, param: UInt?) { | ||
Preferences.setCodecName(context, codec.name) | ||
Preferences.setCodecParam(context, codec.name, param) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters