Option to persist task in video sample app.

This commit is contained in:
Nicholas Tinsley
2024-01-23 12:43:20 -05:00
parent e71bb33b23
commit 9cb2024334
2 changed files with 101 additions and 13 deletions

View File

@@ -7,6 +7,7 @@ package org.thoughtcrime.video.app
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences
import android.media.MediaScannerConnection import android.media.MediaScannerConnection
import android.os.Bundle import android.os.Bundle
import android.os.Environment import android.os.Environment
@@ -14,11 +15,20 @@ import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Checkbox
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import org.signal.core.util.logging.Log import org.signal.core.util.logging.Log
import org.thoughtcrime.video.app.playback.PlaybackTestActivity import org.thoughtcrime.video.app.playback.PlaybackTestActivity
import org.thoughtcrime.video.app.transcode.TranscodeTestActivity import org.thoughtcrime.video.app.transcode.TranscodeTestActivity
@@ -29,30 +39,85 @@ import org.thoughtcrime.video.app.ui.theme.SignalTheme
* Main activity for this sample app. * Main activity for this sample app.
*/ */
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
companion object {
private val TAG = Log.tag(MainActivity::class.java) private val TAG = Log.tag(MainActivity::class.java)
private var appLaunch = true
}
private val sharedPref: SharedPreferences by lazy {
getSharedPreferences(
getString(R.string.preference_file_key),
Context.MODE_PRIVATE
)
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val startPlaybackScreen = Intent(this, PlaybackTestActivity::class.java) val startPlaybackScreen = { saveChoice: Boolean -> proceed(Screen.TEST_PLAYBACK, saveChoice) }
val startTranscodeScreen = Intent(this, TranscodeTestActivity::class.java) val startTranscodeScreen = { saveChoice: Boolean -> proceed(Screen.TEST_TRANSCODE, saveChoice) }
setContent { setContent {
Body(startPlaybackScreen, startTranscodeScreen)
}
refreshMediaProviderForExternalStorage(this, arrayOf("video/*"))
if (appLaunch) {
appLaunch = false
getLaunchChoice()?.let {
proceed(it, false)
}
}
}
@Composable
private fun Body(startPlaybackScreen: (Boolean) -> Unit, startTranscodeScreen: (Boolean) -> Unit) {
var rememberChoice by remember { mutableStateOf(getLaunchChoice() != null) }
SignalTheme { SignalTheme {
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) { Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
Column( Column(
verticalArrangement = Arrangement.Center, verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally horizontalAlignment = Alignment.CenterHorizontally
) { ) {
LabeledButton("Test Playback") { startActivity(startPlaybackScreen) } LabeledButton("Test Playback") {
LabeledButton("Test Transcode") { startActivity(startTranscodeScreen) } startPlaybackScreen(rememberChoice)
}
LabeledButton("Test Transcode") {
startTranscodeScreen(rememberChoice)
}
Row(
verticalAlignment = Alignment.CenterVertically
) {
Checkbox(
checked = rememberChoice,
onCheckedChange = { isChecked ->
rememberChoice = isChecked
if (!isChecked) {
clearLaunchChoice()
}
}
)
Text(text = "Remember & Skip This Screen", style = MaterialTheme.typography.labelLarge)
} }
} }
} }
} }
} }
override fun onStart() { private fun getLaunchChoice(): Screen? {
super.onStart() val screenName = sharedPref.getString(getString(R.string.preference_activity_shortcut_key), null) ?: return null
refreshMediaProviderForExternalStorage(this, arrayOf("video/*")) return Screen.valueOf(screenName)
}
private fun clearLaunchChoice() {
with(sharedPref.edit()) {
remove(getString(R.string.preference_activity_shortcut_key))
apply()
}
}
private fun saveLaunchChoice(choice: Screen) {
with(sharedPref.edit()) {
putString(getString(R.string.preference_activity_shortcut_key), choice.name)
apply()
}
} }
private fun refreshMediaProviderForExternalStorage(context: Context, mimeTypes: Array<String>) { private fun refreshMediaProviderForExternalStorage(context: Context, mimeTypes: Array<String>) {
@@ -65,4 +130,25 @@ class MainActivity : AppCompatActivity() {
Log.i(TAG, "Re-scan of external storage for media completed.") Log.i(TAG, "Re-scan of external storage for media completed.")
} }
} }
private fun proceed(screen: Screen, saveChoice: Boolean) {
if (saveChoice) {
saveLaunchChoice(screen)
}
when (screen) {
Screen.TEST_PLAYBACK -> startActivity(Intent(this, PlaybackTestActivity::class.java))
Screen.TEST_TRANSCODE -> startActivity(Intent(this, TranscodeTestActivity::class.java))
}
}
private enum class Screen {
TEST_PLAYBACK,
TEST_TRANSCODE
}
@Preview
@Composable
private fun PreviewBody() {
Body({}, {})
}
} }

View File

@@ -10,4 +10,6 @@
<string name="cancel_transcode">Cancel</string> <string name="cancel_transcode">Cancel</string>
<string name="channel_name">Transcoding progress updates.</string> <string name="channel_name">Transcoding progress updates.</string>
<string name="channel_description">Persistent notifications that allow the transcode job to complete when the app is in the background.</string> <string name="channel_description">Persistent notifications that allow the transcode job to complete when the app is in the background.</string>
<string name="preference_file_key">settings</string>
<string name="preference_activity_shortcut_key">activity_shortcut</string>
</resources> </resources>