ui: force dark theme in TV mode

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2023-04-04 20:19:29 +02:00
parent 794a2a1925
commit cd812b03cd
2 changed files with 19 additions and 2 deletions

View File

@ -21,7 +21,7 @@ import android.view.View
import android.widget.Toast import android.widget.Toast
import androidx.activity.addCallback import androidx.activity.addCallback
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import androidx.core.view.forEach import androidx.core.view.forEach
@ -47,13 +47,14 @@ import com.wireguard.android.model.ObservableTunnel
import com.wireguard.android.util.ErrorMessages import com.wireguard.android.util.ErrorMessages
import com.wireguard.android.util.QuantityFormatter import com.wireguard.android.util.QuantityFormatter
import com.wireguard.android.util.TunnelImporter import com.wireguard.android.util.TunnelImporter
import com.wireguard.android.util.UserKnobs
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.io.File import java.io.File
class TvMainActivity : AppCompatActivity() { class TvMainActivity : ThemeChangeAwareActivity() {
private val tunnelFileImportResultLauncher = registerForActivityResult(object : ActivityResultContracts.GetContent() { private val tunnelFileImportResultLauncher = registerForActivityResult(object : ActivityResultContracts.GetContent() {
override fun createIntent(context: Context, input: String): Intent { override fun createIntent(context: Context, input: String): Intent {
val intent = super.createIntent(context, input) val intent = super.createIntent(context, input)
@ -110,6 +111,16 @@ class TvMainActivity : AppCompatActivity() {
private val filesRoot = ObservableField("") private val filesRoot = ObservableField("")
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
if (AppCompatDelegate.getDefaultNightMode() != AppCompatDelegate.MODE_NIGHT_YES) {
/* First set it this way to prevent a white flash. */
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
/* Then update the preference, which triggers the recreation. */
lifecycleScope.launch {
UserKnobs.setDarkTheme(true)
}
}
}
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
binding = TvActivityBinding.inflate(layoutInflater) binding = TvActivityBinding.inflate(layoutInflater)
lifecycleScope.launch { lifecycleScope.launch {

View File

@ -41,6 +41,12 @@ object UserKnobs {
it[DARK_THEME] ?: false it[DARK_THEME] ?: false
} }
suspend fun setDarkTheme(on: Boolean) {
Application.getPreferencesDataStore().edit {
it[DARK_THEME] = on
}
}
private val ALLOW_REMOTE_CONTROL_INTENTS = booleanPreferencesKey("allow_remote_control_intents") private val ALLOW_REMOTE_CONTROL_INTENTS = booleanPreferencesKey("allow_remote_control_intents")
val allowRemoteControlIntents: Flow<Boolean> val allowRemoteControlIntents: Flow<Boolean>
get() = Application.getPreferencesDataStore().data.map { get() = Application.getPreferencesDataStore().data.map {