diff --git a/ui/src/main/java/com/wireguard/android/activity/TvMainActivity.kt b/ui/src/main/java/com/wireguard/android/activity/TvMainActivity.kt index 1ee14ee6..3026d9a9 100644 --- a/ui/src/main/java/com/wireguard/android/activity/TvMainActivity.kt +++ b/ui/src/main/java/com/wireguard/android/activity/TvMainActivity.kt @@ -21,7 +21,7 @@ import android.view.View import android.widget.Toast import androidx.activity.addCallback 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.getSystemService 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.QuantityFormatter import com.wireguard.android.util.TunnelImporter +import com.wireguard.android.util.UserKnobs import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.io.File -class TvMainActivity : AppCompatActivity() { +class TvMainActivity : ThemeChangeAwareActivity() { private val tunnelFileImportResultLauncher = registerForActivityResult(object : ActivityResultContracts.GetContent() { override fun createIntent(context: Context, input: String): Intent { val intent = super.createIntent(context, input) @@ -110,6 +111,16 @@ class TvMainActivity : AppCompatActivity() { private val filesRoot = ObservableField("") 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) binding = TvActivityBinding.inflate(layoutInflater) lifecycleScope.launch { diff --git a/ui/src/main/java/com/wireguard/android/util/UserKnobs.kt b/ui/src/main/java/com/wireguard/android/util/UserKnobs.kt index 52672e45..e0144b63 100644 --- a/ui/src/main/java/com/wireguard/android/util/UserKnobs.kt +++ b/ui/src/main/java/com/wireguard/android/util/UserKnobs.kt @@ -41,6 +41,12 @@ object UserKnobs { 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") val allowRemoteControlIntents: Flow get() = Application.getPreferencesDataStore().data.map {