MainActivity: add new theme picking with new sound

master
dece 3 years ago
parent 7f43a5c5d2
commit f94aed8f99

@ -2,7 +2,10 @@ package io.lowrespalmtree.harvestdawn
import android.Manifest
import android.app.Activity
import android.app.AlertDialog
import android.content.ContentValues
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
@ -81,15 +84,10 @@ class MainActivity : AppCompatActivity() {
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_share -> {
share(); true
}
R.id.action_save -> {
save(); true
}
R.id.action_clear_cache -> {
clearVideoCache(); true
}
R.id.action_share -> { share(); true }
R.id.action_save -> { save(); true }
R.id.action_clear_cache -> { clearVideoCache(); true }
R.id.action_pick_theme -> { showThemeDialog(); true }
else -> super.onOptionsItemSelected(item)
}
}
@ -185,9 +183,10 @@ class MainActivity : AppCompatActivity() {
// Save the soundtrack as well cause we can't pass a resource to FFMPEG.
// Should be done only once, then it's kept in app internal storage.
val soundFile = File(filesDir, "harvestdawn.m4a")
val themeId = getThemeRawResource(getThemeId())
val soundFile = File(filesDir, "$themeId.m4a")
if (!soundFile.exists()) {
resources.openRawResource(R.raw.harvestdawn).use { resource ->
resources.openRawResource(themeId).use { resource ->
FileOutputStream(soundFile).use { fos ->
fos.buffered().write(resource.buffered().readBytes())
}
@ -346,6 +345,38 @@ class MainActivity : AppCompatActivity() {
startActivity(Intent.createChooser(shareIntent, getString(R.string.send)))
}
private fun getThemeId() =
getSharedPreferences("app", Context.MODE_PRIVATE).getInt("theme", 0)
private fun setThemeId(id: Int) =
getSharedPreferences("app", Context.MODE_PRIVATE).edit().apply {
putInt("theme", id)
apply()
}
private fun getThemeRawResource(id: Int) =
when (id) {
0 -> R.raw.harvestdawn
1 -> R.raw.sunriseofflutes
else -> R.raw.harvestdawn
}
private fun showThemeDialog() {
val currentId = getThemeId()
val dialog = AlertDialog.Builder(this).run {
var selection = currentId
setTitle(R.string.action_theme)
setSingleChoiceItems(R.array.pick_theme, currentId) { _, which -> selection = which }
setPositiveButton(android.R.string.ok) { dialog, _ ->
setThemeId(selection)
dialog.dismiss()
}
setNegativeButton(android.R.string.cancel) { dialog, _ -> dialog.dismiss() }
create()
}
dialog.show()
}
private fun toast(text: String) {
Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
}

@ -2,6 +2,11 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="io.lowrespalmtree.harvestdawn.MainActivity">
<item
android:id="@+id/action_pick_theme"
android:orderInCategory="100"
android:title="@string/action_theme"
app:showAsAction="never" />
<item
android:id="@+id/action_save"
android:orderInCategory="100"

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="pick_theme">
<item>@string/theme_harvestdawn</item>
<item>@string/theme_sunriseofflutes</item>
</string-array>
</resources>

@ -7,4 +7,7 @@
<string name="send">Share</string>
<string name="action_save">Save</string>
<string name="action_clear_cache">Clear cache</string>
<string name="action_theme">Theme</string>
<string name="theme_harvestdawn">Harvest Dawn</string>
<string name="theme_sunriseofflutes">Sunrise of Flutes</string>
</resources>
Loading…
Cancel
Save