Recreate activity when theme is changed through remote provisioning, fixed keep alive service automatically enabled after remote provisioning

This commit is contained in:
Sylvain Berfini 2024-12-05 16:08:50 +01:00
parent 935d463896
commit 75bb28cb2f
4 changed files with 38 additions and 3 deletions

View file

@ -106,6 +106,10 @@ class CoreContext @UiThread constructor(val context: Context) : HandlerThread("C
MutableLiveData<Event<Pair<String, Int>>>() MutableLiveData<Event<Pair<String, Int>>>()
} }
val provisioningAppliedEvent: MutableLiveData<Event<Boolean>> by lazy {
MutableLiveData<Event<Boolean>>()
}
@SuppressLint("HandlerLeak") @SuppressLint("HandlerLeak")
private lateinit var coreThread: Handler private lateinit var coreThread: Handler
@ -166,6 +170,7 @@ class CoreContext @UiThread constructor(val context: Context) : HandlerThread("C
) { ) {
Log.i("$TAG Configuring state changed [$status], message is [$message]") Log.i("$TAG Configuring state changed [$status], message is [$message]")
if (status == ConfiguringState.Successful) { if (status == ConfiguringState.Successful) {
provisioningAppliedEvent.postValue(Event(true))
corePreferences.firstLaunch = false corePreferences.firstLaunch = false
showGreenToastEvent.postValue( showGreenToastEvent.postValue(
Event( Event(
@ -339,6 +344,9 @@ class CoreContext @UiThread constructor(val context: Context) : HandlerThread("C
} }
override fun onAccountAdded(core: Core, account: Account) { override fun onAccountAdded(core: Core, account: Account) {
// Prevent this trigger when core is stopped/start in remote prov
if (core.globalState == GlobalState.Off) return
Log.i( Log.i(
"$TAG New account configured: [${account.params.identityAddress?.asStringUriOnly()}]" "$TAG New account configured: [${account.params.identityAddress?.asStringUriOnly()}]"
) )
@ -507,6 +515,7 @@ class CoreContext @UiThread constructor(val context: Context) : HandlerThread("C
} }
if (corePreferences.keepServiceAlive) { if (corePreferences.keepServiceAlive) {
Log.i("$TAG Starting keep alive service")
startKeepAliveService() startKeepAliveService()
} }

View file

@ -49,9 +49,11 @@ open class GenericActivity : AppCompatActivity() {
private lateinit var toastsArea: ViewGroup private lateinit var toastsArea: ViewGroup
private var mainColor: String = "orange"
override fun getTheme(): Resources.Theme { override fun getTheme(): Resources.Theme {
val mainColor = corePreferences.themeMainColor mainColor = corePreferences.themeMainColor
val theme = super.getTheme() val theme = super.theme
when (mainColor) { when (mainColor) {
"yellow" -> theme.applyStyle(R.style.Theme_LinphoneYellow, true) "yellow" -> theme.applyStyle(R.style.Theme_LinphoneYellow, true)
"green" -> theme.applyStyle(R.style.Theme_LinphoneGreen, true) "green" -> theme.applyStyle(R.style.Theme_LinphoneGreen, true)
@ -89,6 +91,13 @@ open class GenericActivity : AppCompatActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
} }
protected fun checkMainColorTheme() {
if (mainColor != corePreferences.themeMainColor) {
Log.i("$TAG Main color setting has changed, re-creating activity")
recreate()
}
}
fun setUpToastsArea(viewGroup: ViewGroup) { fun setUpToastsArea(viewGroup: ViewGroup) {
toastsArea = viewGroup toastsArea = viewGroup
} }

View file

@ -24,6 +24,7 @@ import androidx.annotation.UiThread
import androidx.annotation.WorkerThread import androidx.annotation.WorkerThread
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import org.linphone.LinphoneApplication.Companion.coreContext import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.core.ConfiguringState
import org.linphone.core.Core import org.linphone.core.Core
import org.linphone.core.CoreListenerStub import org.linphone.core.CoreListenerStub
import org.linphone.core.tools.Log import org.linphone.core.tools.Log
@ -38,6 +39,14 @@ class QrCodeViewModel @UiThread constructor() : GenericViewModel() {
val qrCodeFoundEvent = MutableLiveData<Event<Boolean>>() val qrCodeFoundEvent = MutableLiveData<Event<Boolean>>()
private val coreListener = object : CoreListenerStub() { private val coreListener = object : CoreListenerStub() {
@WorkerThread
override fun onConfiguringStatus(core: Core, status: ConfiguringState, message: String?) {
Log.i("$TAG Configuring state is [$status]")
if (status == ConfiguringState.Successful) {
qrCodeFoundEvent.postValue(Event(true))
}
}
@WorkerThread @WorkerThread
override fun onQrcodeFound(core: Core, result: String?) { override fun onQrcodeFound(core: Core, result: String?) {
Log.i("$TAG QR Code found: [$result]") Log.i("$TAG QR Code found: [$result]")
@ -47,6 +56,7 @@ class QrCodeViewModel @UiThread constructor() : GenericViewModel() {
val isValidUrl = Patterns.WEB_URL.matcher(result).matches() val isValidUrl = Patterns.WEB_URL.matcher(result).matches()
if (!isValidUrl) { if (!isValidUrl) {
Log.e("$TAG The content of the QR Code doesn't seem to be a valid web URL") Log.e("$TAG The content of the QR Code doesn't seem to be a valid web URL")
qrCodeFoundEvent.postValue(Event(false))
} else { } else {
Log.i( Log.i(
"$TAG QR code URL set, restarting the Core to apply configuration changes" "$TAG QR code URL set, restarting the Core to apply configuration changes"
@ -57,7 +67,6 @@ class QrCodeViewModel @UiThread constructor() : GenericViewModel() {
coreContext.core.start() coreContext.core.start()
Log.i("$TAG Core has been restarted") Log.i("$TAG Core has been restarted")
} }
qrCodeFoundEvent.postValue(Event(isValidUrl))
} }
} }
} }

View file

@ -45,6 +45,7 @@ import androidx.core.view.updatePadding
import androidx.databinding.DataBindingUtil import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.observe
import androidx.navigation.NavController import androidx.navigation.NavController
import androidx.navigation.NavDestination import androidx.navigation.NavDestination
import androidx.navigation.NavOptions import androidx.navigation.NavOptions
@ -295,6 +296,13 @@ class MainActivity : GenericActivity() {
} }
} }
coreContext.provisioningAppliedEvent.observe(this) {
it.consume {
Log.i("$TAG Remote provisioning was applied, checking if theme has changed")
checkMainColorTheme()
}
}
CarConnection(this).type.observe(this) { CarConnection(this).type.observe(this) {
val asString = when (it) { val asString = when (it) {
CarConnection.CONNECTION_TYPE_NOT_CONNECTED -> "NOT CONNECTED" CarConnection.CONNECTION_TYPE_NOT_CONNECTED -> "NOT CONNECTED"