Fixed crash when app is started from chat notification and app day/night mode doesn't match the system's one, causing activity to be recreated and navigation event to happen without a valid FragmentManager

This commit is contained in:
Sylvain Berfini 2021-01-30 20:51:36 +01:00
parent f6cc435396
commit cacf1f4472
4 changed files with 26 additions and 3 deletions

View file

@ -36,6 +36,9 @@ import org.linphone.core.tools.Log
abstract class GenericActivity : AppCompatActivity() {
private var timer: Timer? = null
private var _isDestructionPending = false
val isDestructionPending: Boolean
get() = _isDestructionPending
@SuppressLint("SourceLockedOrientationActivity")
override fun onCreate(savedInstanceState: Bundle?) {
@ -49,19 +52,24 @@ abstract class GenericActivity : AppCompatActivity() {
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
}
_isDestructionPending = false
val nightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
val darkModeEnabled = corePreferences.darkMode
when (nightMode) {
Configuration.UI_MODE_NIGHT_NO, Configuration.UI_MODE_NIGHT_UNDEFINED -> {
if (darkModeEnabled == 1) {
// Force dark mode
Log.w("[Generic Activity] Forcing night mode")
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
_isDestructionPending = true
}
}
Configuration.UI_MODE_NIGHT_YES -> {
if (darkModeEnabled == 0) {
// Force light mode
Log.w("[Generic Activity] Forcing day mode")
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
_isDestructionPending = true
}
}
}

View file

@ -29,6 +29,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.R
import org.linphone.activities.GenericActivity
import org.linphone.activities.main.MainActivity
import org.linphone.activities.main.chat.adapters.ChatRoomsListAdapter
import org.linphone.activities.main.chat.viewmodels.ChatRoomsListViewModel
@ -141,8 +142,13 @@ class MasterChatRoomsFragment : MasterFragment<ChatRoomMasterFragmentBinding, Ch
adapter.selectedChatRoomEvent.observe(viewLifecycleOwner, {
it.consume { chatRoom ->
sharedViewModel.selectedChatRoom.value = chatRoom
navigateToChatRoom()
if ((requireActivity() as GenericActivity).isDestructionPending) {
Log.w("[Chat] Activity is pending destruction, don't start navigating now!")
sharedViewModel.destructionPendingChatRoom = chatRoom
} else {
sharedViewModel.selectedChatRoom.value = chatRoom
navigateToChatRoom()
}
}
})
@ -159,6 +165,14 @@ class MasterChatRoomsFragment : MasterFragment<ChatRoomMasterFragmentBinding, Ch
navigateToChatRoomCreation(true)
}
val pendingDestructionChatRoom = sharedViewModel.destructionPendingChatRoom
if (pendingDestructionChatRoom != null) {
Log.w("[Chat] Found pending chat room from before activity was recreated")
sharedViewModel.destructionPendingChatRoom = null
sharedViewModel.selectedChatRoom.value = pendingDestructionChatRoom
navigateToChatRoom()
}
val localSipUri = arguments?.getString("LocalSipUri")
val remoteSipUri = arguments?.getString("RemoteSipUri")
if (localSipUri != null && remoteSipUri != null) {

View file

@ -59,7 +59,7 @@ class AdvancedSettingsFragment : GenericFragment<SettingsAdvancedFragmentBinding
when (value) {
0 -> AppCompatDelegate.MODE_NIGHT_NO
1 -> AppCompatDelegate.MODE_NIGHT_YES
else -> AppCompatDelegate.MODE_NIGHT_UNSPECIFIED
else -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
}
)
}

View file

@ -36,6 +36,7 @@ class SharedMainViewModel : ViewModel() {
/* Chat */
val selectedChatRoom = MutableLiveData<ChatRoom>()
var destructionPendingChatRoom: ChatRoom? = null
val selectedGroupChatRoom = MutableLiveData<ChatRoom>()