Skip to content

Commit

Permalink
handle uiMode config changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tsynik committed Dec 14, 2023
1 parent b99e11b commit 54540ef
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<activity
android:name="ru.yourok.torrserve.ui.activities.main.MainActivity"
android:launchMode="singleInstance"
android:configChanges="uiMode"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
Expand All @@ -72,6 +73,7 @@
<activity
android:name="ru.yourok.torrserve.ui.activities.play.PlayActivity"
android:theme="@style/PlayDialog.DayNight"
android:configChanges="uiMode"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
Expand Down Expand Up @@ -205,6 +207,7 @@
</activity>

<activity android:name="ru.yourok.torrserve.ui.activities.main.SettingsActivity"
android:configChanges="uiMode"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ru.yourok.torrserve.ui.activities.main

import android.content.DialogInterface.BUTTON_POSITIVE
import android.content.Intent
import android.content.res.Configuration
import android.net.Uri
import android.os.Bundle
import android.util.Log
Expand Down Expand Up @@ -127,6 +128,11 @@ class MainActivity : AppCompatActivity() {
if (Settings.showFab()) setupFab()
}

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
themeUtil.onConfigurationChanged(this, newConfig)
}

private fun updateStatus() {
val host = viewModel.getHost()
host.observe(this) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.yourok.torrserve.ui.activities.main

import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.util.Log
Expand Down Expand Up @@ -89,4 +90,9 @@ class SettingsActivity : AppCompatActivity() {
themeUtil.onResume(this)
}

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
themeUtil.onConfigurationChanged(this, newConfig)
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.yourok.torrserve.ui.activities.play

import android.content.res.Configuration
import android.os.Bundle
import android.util.Log
import android.view.View
Expand Down Expand Up @@ -44,6 +45,9 @@ class PlayActivity : AppCompatActivity() {
private var userClose = false

var ad: AD? = null

private val themeUtil = ThemeUtil()

val infoFragment = InfoFragment()

private var firebaseAnalytics: FirebaseAnalytics? = null
Expand Down Expand Up @@ -144,6 +148,12 @@ class PlayActivity : AppCompatActivity() {
super.onDestroy()
}


override fun onConfigurationChanged(newConfig: Configuration) {
if (App.inForeground) super.onConfigurationChanged(newConfig)
themeUtil.onConfigurationChanged(this, newConfig)
}

private fun setWindow() {
setFinishOnTouchOutside(false)
val attr = window.attributes
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/ru/yourok/torrserve/utils/ThemeUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ class ThemeUtil {
}

fun onConfigurationChanged(activity: AppCompatActivity, newConfig: Configuration) {
val config = activity.resources.configuration
val isNightModeActive =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
activity.resources.configuration.isNightModeActive
config.isNightModeActive
} else {
newConfig.uiMode and
config.uiMode and
Configuration.UI_MODE_NIGHT_MASK ==
Configuration.UI_MODE_NIGHT_YES
}
if (BuildConfig.DEBUG) Log.d("*****", "onConfigurationChanged isNightModeActive = $isNightModeActive")
if (BuildConfig.DEBUG) Log.d("*****", "ThemeUtil onConfigurationChanged isNightModeActive = $isNightModeActive")
}

companion object {
Expand Down

0 comments on commit 54540ef

Please sign in to comment.