From 1af014a527db765529e8d6ba1c1230d114e2c8cd Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Thu, 19 Aug 2021 11:52:11 +0200 Subject: [PATCH] Work around voice message crashes on Android 5 - Catch NPE in onVisibilityChanged (happens for SDK 21 and 22) - Do not allow voice messages on SDK 21: segfaults as soon as recording stops Change-Id: I0cfc55fbb7880dc29e29df4fba14d57af1f82bba --- .../detail/composer/VoiceMessageRecorderView.kt | 13 +++++++++---- .../app/features/settings/VectorPreferences.kt | 6 ++++-- .../features/settings/VectorSettingsLabsFragment.kt | 3 +++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/VoiceMessageRecorderView.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/VoiceMessageRecorderView.kt index 7f1c4f0759..40bc3db2f8 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/VoiceMessageRecorderView.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/VoiceMessageRecorderView.kt @@ -90,10 +90,15 @@ class VoiceMessageRecorderView @JvmOverloads constructor( override fun onVisibilityChanged(changedView: View, visibility: Int) { super.onVisibilityChanged(changedView, visibility) - if (changedView == this && visibility == VISIBLE) { - views.voiceMessageMicButton.contentDescription = context.getString(R.string.a11y_start_voice_message) - } else { - views.voiceMessageMicButton.contentDescription = "" + // On Android 5, this can cause a NPE for some unknown reason + try { + if (changedView == this && visibility == VISIBLE) { + views.voiceMessageMicButton.contentDescription = context.getString(R.string.a11y_start_voice_message) + } else { + views.voiceMessageMicButton.contentDescription = "" + } + } catch (e: Exception) { + e.printStackTrace() } } diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt b/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt index b1b9ccfa70..15e1d366fe 100755 --- a/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt @@ -19,6 +19,7 @@ import android.content.Context import android.content.SharedPreferences import android.media.RingtoneManager import android.net.Uri +import android.os.Build import android.provider.MediaStore import androidx.core.content.edit import com.squareup.seismic.ShakeDetector @@ -202,7 +203,7 @@ class VectorPreferences @Inject constructor(private val context: Context) { private const val SETTINGS_USER_COLOR_MODE_DEFAULT = "SETTINGS_USER_COLOR_MODE_DEFAULT" private const val SETTINGS_USER_COLOR_MODE_PUBLIC_ROOM = "SETTINGS_USER_COLOR_MODE_PUBLIC_ROOM" private const val SETTINGS_OPEN_CHATS_AT_FIRST_UNREAD = "SETTINGS_OPEN_CHATS_AT_FIRST_UNREAD" - private const val SETTINGS_VOICE_MESSAGE = "SETTINGS_VOICE_MESSAGE" + const val SETTINGS_VOICE_MESSAGE = "SETTINGS_VOICE_MESSAGE" private const val DID_ASK_TO_ENABLE_SESSION_PUSH = "DID_ASK_TO_ENABLE_SESSION_PUSH" @@ -1004,7 +1005,8 @@ class VectorPreferences @Inject constructor(private val context: Context) { // Element removed this, SC added it back (but this time, default to true) fun useVoiceMessage(): Boolean { - return defaultPrefs.getBoolean(SETTINGS_VOICE_MESSAGE, true) + // Voice messages crash on SDK 21 + return Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP && defaultPrefs.getBoolean(SETTINGS_VOICE_MESSAGE, true) } /** diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsLabsFragment.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsLabsFragment.kt index dc74a1fa4f..e12e9353d2 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsLabsFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsLabsFragment.kt @@ -16,6 +16,7 @@ package im.vector.app.features.settings +import android.os.Build import androidx.preference.Preference import im.vector.app.R import im.vector.app.core.preference.VectorSwitchPreference @@ -56,6 +57,8 @@ class VectorSettingsLabsFragment @Inject constructor( findPreference(VectorPreferences.SETTINGS_ALLOW_URL_PREVIEW_IN_ENCRYPTED_ROOM_KEY)?.isEnabled = vectorPreferences.showUrlPreviews() + findPreference(VectorPreferences.SETTINGS_VOICE_MESSAGE)?.isEnabled = Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP + findPreference(VectorPreferences.SETTINGS_LABS_SPACES_HOME_AS_ORPHAN)!!.let { pref -> pref.setOnPreferenceChangeListener { _, _ -> MainActivity.restartApp(requireActivity(), MainActivityArgs(clearCache = false))