Skip to content

Commit cd812b0

Browse files
committed
ui: force dark theme in TV mode
Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent 794a2a1 commit cd812b0

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

ui/src/main/java/com/wireguard/android/activity/TvMainActivity.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import android.view.View
2121
import android.widget.Toast
2222
import androidx.activity.addCallback
2323
import androidx.activity.result.contract.ActivityResultContracts
24-
import androidx.appcompat.app.AppCompatActivity
24+
import androidx.appcompat.app.AppCompatDelegate
2525
import androidx.core.content.ContextCompat
2626
import androidx.core.content.getSystemService
2727
import androidx.core.view.forEach
@@ -47,13 +47,14 @@ import com.wireguard.android.model.ObservableTunnel
4747
import com.wireguard.android.util.ErrorMessages
4848
import com.wireguard.android.util.QuantityFormatter
4949
import com.wireguard.android.util.TunnelImporter
50+
import com.wireguard.android.util.UserKnobs
5051
import kotlinx.coroutines.Dispatchers
5152
import kotlinx.coroutines.delay
5253
import kotlinx.coroutines.launch
5354
import kotlinx.coroutines.withContext
5455
import java.io.File
5556

56-
class TvMainActivity : AppCompatActivity() {
57+
class TvMainActivity : ThemeChangeAwareActivity() {
5758
private val tunnelFileImportResultLauncher = registerForActivityResult(object : ActivityResultContracts.GetContent() {
5859
override fun createIntent(context: Context, input: String): Intent {
5960
val intent = super.createIntent(context, input)
@@ -110,6 +111,16 @@ class TvMainActivity : AppCompatActivity() {
110111
private val filesRoot = ObservableField("")
111112

112113
override fun onCreate(savedInstanceState: Bundle?) {
114+
if (AppCompatDelegate.getDefaultNightMode() != AppCompatDelegate.MODE_NIGHT_YES) {
115+
/* First set it this way to prevent a white flash. */
116+
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
117+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
118+
/* Then update the preference, which triggers the recreation. */
119+
lifecycleScope.launch {
120+
UserKnobs.setDarkTheme(true)
121+
}
122+
}
123+
}
113124
super.onCreate(savedInstanceState)
114125
binding = TvActivityBinding.inflate(layoutInflater)
115126
lifecycleScope.launch {

ui/src/main/java/com/wireguard/android/util/UserKnobs.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ object UserKnobs {
4141
it[DARK_THEME] ?: false
4242
}
4343

44+
suspend fun setDarkTheme(on: Boolean) {
45+
Application.getPreferencesDataStore().edit {
46+
it[DARK_THEME] = on
47+
}
48+
}
49+
4450
private val ALLOW_REMOTE_CONTROL_INTENTS = booleanPreferencesKey("allow_remote_control_intents")
4551
val allowRemoteControlIntents: Flow<Boolean>
4652
get() = Application.getPreferencesDataStore().data.map {

0 commit comments

Comments
 (0)