remove try-catch for NPEs for bindings

since null checks are done for the nullable bindings by PR #2694, the try-catch blocks arent necessary anymore.

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2023-01-17 08:37:42 +01:00
parent 30879dfd49
commit 1d9868daa6
No known key found for this signature in database
GPG Key ID: C793F8B59F43CE7B
6 changed files with 179 additions and 271 deletions

View File

@ -367,22 +367,16 @@ class ChatController(args: Bundle) :
setTitle() setTitle()
participantPermissions = ParticipantPermissions(conversationUser, currentConversation!!) participantPermissions = ParticipantPermissions(conversationUser, currentConversation!!)
try { setupSwipeToReply()
setupSwipeToReply() setupMentionAutocomplete()
setupMentionAutocomplete() checkShowCallButtons()
checkShowCallButtons() checkShowMessageInputView()
checkShowMessageInputView() checkLobbyState()
checkLobbyState()
if (!inConversation) { if (!inConversation) {
joinRoomWithPassword() joinRoomWithPassword()
} else { } else {
Log.d(TAG, "already inConversation. joinRoomWithPassword is skipped") Log.d(TAG, "already inConversation. joinRoomWithPassword is skipped")
}
} catch (npe: NullPointerException) {
// view binding can be null
// since this is called asynchronously and UI might have been destroyed in the meantime
Log.i(TAG, "UI destroyed - view binding already gone")
} }
} }
@ -714,43 +708,37 @@ class ChatController(args: Bundle) :
@Suppress("Detekt.TooGenericExceptionCaught") @Suppress("Detekt.TooGenericExceptionCaught")
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
try { if (s.length >= lengthFilter) {
if (s.length >= lengthFilter) { binding?.messageInputView?.inputEditText?.error = String.format(
binding?.messageInputView?.inputEditText?.error = String.format( Objects.requireNonNull<Resources>(resources).getString(R.string.nc_limit_hit),
Objects.requireNonNull<Resources>(resources).getString(R.string.nc_limit_hit), lengthFilter.toString()
lengthFilter.toString() )
) } else {
} else { binding?.messageInputView?.inputEditText?.error = null
binding?.messageInputView?.inputEditText?.error = null }
}
val editable = binding?.messageInputView?.inputEditText?.editableText val editable = binding?.messageInputView?.inputEditText?.editableText
if (editable != null && binding?.messageInputView?.inputEditText != null) { if (editable != null && binding?.messageInputView?.inputEditText != null) {
val mentionSpans = editable.getSpans( val mentionSpans = editable.getSpans(
0, 0,
binding?.messageInputView?.inputEditText!!.length(), binding?.messageInputView?.inputEditText!!.length(),
Spans.MentionChipSpan::class.java Spans.MentionChipSpan::class.java
) )
var mentionSpan: Spans.MentionChipSpan var mentionSpan: Spans.MentionChipSpan
for (i in mentionSpans.indices) { for (i in mentionSpans.indices) {
mentionSpan = mentionSpans[i] mentionSpan = mentionSpans[i]
if (start >= editable.getSpanStart(mentionSpan) && if (start >= editable.getSpanStart(mentionSpan) &&
start < editable.getSpanEnd(mentionSpan) start < editable.getSpanEnd(mentionSpan)
) {
if (editable.subSequence(
editable.getSpanStart(mentionSpan),
editable.getSpanEnd(mentionSpan)
).toString().trim { it <= ' ' } != mentionSpan.label
) { ) {
if (editable.subSequence( editable.removeSpan(mentionSpan)
editable.getSpanStart(mentionSpan),
editable.getSpanEnd(mentionSpan)
).toString().trim { it <= ' ' } != mentionSpan.label
) {
editable.removeSpan(mentionSpan)
}
} }
} }
} }
} catch (npe: NullPointerException) {
// view binding can be null
// since this is called asynchronously and UI might have been destroyed in the meantime
Log.i(TAG, "UI destroyed - view binding already gone")
} }
} }
@ -768,16 +756,10 @@ class ChatController(args: Bundle) :
showMicrophoneButton(true) showMicrophoneButton(true)
binding?.messageInputView?.messageInput?.doAfterTextChanged { binding?.messageInputView?.messageInput?.doAfterTextChanged {
try { if (binding?.messageInputView?.messageInput?.text?.isEmpty() == true) {
if (binding?.messageInputView?.messageInput?.text?.isEmpty() == true) { showMicrophoneButton(true)
showMicrophoneButton(true) } else {
} else { showMicrophoneButton(false)
showMicrophoneButton(false)
}
} catch (npe: NullPointerException) {
// view binding can be null
// since this is called asynchronously and UI might have been destroyed in the meantime
Log.i(TAG, "UI destroyed - view binding already gone")
} }
} }
@ -1771,13 +1753,7 @@ class ChatController(args: Bundle) :
) )
}, },
onEmojiClickListener = { onEmojiClickListener = {
try { binding?.messageInputView?.inputEditText?.editableText?.append(" ")
binding?.messageInputView?.inputEditText?.editableText?.append(" ")
} catch (npe: NullPointerException) {
// view binding can be null
// since this is called asynchronously and UI might have been destroyed in the meantime
Log.i(WebViewLoginController.TAG, "UI destroyed - view binding already gone")
}
} }
) )
} }
@ -1937,13 +1913,7 @@ class ChatController(args: Bundle) :
setupWebsocket() setupWebsocket()
try { checkLobbyState()
checkLobbyState()
} catch (npe: NullPointerException) {
// view binding can be null
// since this is called asynchronously and UI might have been destroyed in the meantime
Log.i(TAG, "UI destroyed - view binding already gone")
}
if (isFirstMessagesProcessing) { if (isFirstMessagesProcessing) {
pullChatMessages(0) pullChatMessages(0)
@ -2123,17 +2093,10 @@ class ChatController(args: Bundle) :
override fun onNext(genericOverall: GenericOverall) { override fun onNext(genericOverall: GenericOverall) {
myFirstMessage = message myFirstMessage = message
try { if (binding?.popupBubbleView?.isShown == true) {
if (binding?.popupBubbleView?.isShown == true) { binding?.popupBubbleView?.hide()
binding?.popupBubbleView?.hide()
}
binding?.messagesListView?.smoothScrollToPosition(0)
} catch (npe: NullPointerException) {
// view binding can be null
// since this is called asynchronously and UI might have been destroyed in the meantime
Log.i(TAG, "UI destroyed - view binding already gone")
} }
binding?.messagesListView?.smoothScrollToPosition(0)
} }
override fun onError(e: Throwable) { override fun onError(e: Throwable) {
@ -2246,19 +2209,13 @@ class ChatController(args: Bundle) :
override fun onNext(response: Response<*>) { override fun onNext(response: Response<*>) {
Log.d(TAG, "pullChatMessages - pullChatMessages[lookIntoFuture > 0] - got response") Log.d(TAG, "pullChatMessages - pullChatMessages[lookIntoFuture > 0] - got response")
pullChatMessagesPending = false pullChatMessagesPending = false
try { if (response.code() == HTTP_CODE_NOT_MODIFIED) {
if (response.code() == HTTP_CODE_NOT_MODIFIED) { Log.d(TAG, "pullChatMessages - quasi recursive call to pullChatMessages")
Log.d(TAG, "pullChatMessages - quasi recursive call to pullChatMessages") pullChatMessages(1, setReadMarker, xChatLastCommonRead)
pullChatMessages(1, setReadMarker, xChatLastCommonRead) } else if (response.code() == HTTP_CODE_PRECONDITION_FAILED) {
} else if (response.code() == HTTP_CODE_PRECONDITION_FAILED) { futurePreconditionFailed = true
futurePreconditionFailed = true } else {
} else { processMessagesResponse(response, true)
processMessagesResponse(response, true)
}
} catch (npe: NullPointerException) {
// view binding can be null
// since this is called asynchronously and UI might have been destroyed in the meantime
Log.i(TAG, "UI destroyed - view binding already gone")
} }
processExpiredMessages() processExpiredMessages()
@ -2291,16 +2248,10 @@ class ChatController(args: Bundle) :
override fun onNext(response: Response<*>) { override fun onNext(response: Response<*>) {
Log.d(TAG, "pullChatMessages - pullChatMessages[lookIntoFuture <= 0] - got response") Log.d(TAG, "pullChatMessages - pullChatMessages[lookIntoFuture <= 0] - got response")
pullChatMessagesPending = false pullChatMessagesPending = false
try { if (response.code() == HTTP_CODE_PRECONDITION_FAILED) {
if (response.code() == HTTP_CODE_PRECONDITION_FAILED) { pastPreconditionFailed = true
pastPreconditionFailed = true } else {
} else { processMessagesResponse(response, false)
processMessagesResponse(response, false)
}
} catch (e: NullPointerException) {
// view binding can be null
// since this is called asynchronously and UI might have been destroyed in the meantime
Log.i(TAG, "UI destroyed - view binding already gone", e)
} }
processExpiredMessages() processExpiredMessages()

View File

@ -493,13 +493,7 @@ class ConversationInfoController(args: Bundle) :
@Suppress("Detekt.TooGenericExceptionCaught") @Suppress("Detekt.TooGenericExceptionCaught")
override fun onNext(participantsOverall: ParticipantsOverall) { override fun onNext(participantsOverall: ParticipantsOverall) {
try { handleParticipants(participantsOverall.ocs!!.data!!)
handleParticipants(participantsOverall.ocs!!.data!!)
} catch (npe: NullPointerException) {
// view binding can be null
// since this is called asynchronously and UI might have been destroyed in the meantime
Log.i(TAG, "UI destroyed - view binding already gone")
}
} }
override fun onError(e: Throwable) { override fun onError(e: Throwable) {
@ -646,83 +640,77 @@ class ConversationInfoController(args: Bundle) :
@Suppress("Detekt.TooGenericExceptionCaught") @Suppress("Detekt.TooGenericExceptionCaught")
override fun onNext(roomOverall: RoomOverall) { override fun onNext(roomOverall: RoomOverall) {
try { conversation = roomOverall.ocs!!.data
conversation = roomOverall.ocs!!.data
val conversationCopy = conversation val conversationCopy = conversation
if (conversationCopy!!.canModerate(conversationUser)) { if (conversationCopy!!.canModerate(conversationUser)) {
binding?.addParticipantsAction?.visibility = VISIBLE binding?.addParticipantsAction?.visibility = VISIBLE
if (CapabilitiesUtilNew.hasSpreedFeatureCapability(conversationUser, "clear-history")) { if (CapabilitiesUtilNew.hasSpreedFeatureCapability(conversationUser, "clear-history")) {
binding?.clearConversationHistory?.visibility = VISIBLE binding?.clearConversationHistory?.visibility = VISIBLE
} else {
binding?.clearConversationHistory?.visibility = GONE
}
} else { } else {
binding?.addParticipantsAction?.visibility = GONE
binding?.clearConversationHistory?.visibility = GONE binding?.clearConversationHistory?.visibility = GONE
} }
} else {
binding?.addParticipantsAction?.visibility = GONE
binding?.clearConversationHistory?.visibility = GONE
}
if (isAttached && (!isBeingDestroyed || !isDestroyed)) { if (isAttached && (!isBeingDestroyed || !isDestroyed)) {
binding?.ownOptions?.visibility = VISIBLE binding?.ownOptions?.visibility = VISIBLE
setupWebinaryView() setupWebinaryView()
if (!conversation!!.canLeave()) { if (!conversation!!.canLeave()) {
binding?.leaveConversationAction?.visibility = GONE binding?.leaveConversationAction?.visibility = GONE
} else { } else {
binding?.leaveConversationAction?.visibility = VISIBLE binding?.leaveConversationAction?.visibility = VISIBLE
}
if (!conversation!!.canDelete(conversationUser)) {
binding?.deleteConversationAction?.visibility = GONE
} else {
binding?.deleteConversationAction?.visibility = VISIBLE
}
if (Conversation.ConversationType.ROOM_SYSTEM == conversation!!.type) {
binding?.notificationSettingsView?.callNotifications?.visibility = GONE
}
if (conversation!!.notificationCalls === null) {
binding?.notificationSettingsView?.callNotifications?.visibility = GONE
} else {
binding?.notificationSettingsView?.callNotifications?.value =
conversationCopy.notificationCalls == 1
}
getListOfParticipants()
binding?.progressBar?.visibility = GONE
binding?.conversationInfoName?.visibility = VISIBLE
binding?.displayNameText?.text = conversation!!.displayName
if (conversation!!.description != null && !conversation!!.description!!.isEmpty()) {
binding?.descriptionText?.text = conversation!!.description
binding?.conversationDescription?.visibility = VISIBLE
}
loadConversationAvatar()
adjustNotificationLevelUI()
initExpiringMessageOption()
binding?.let {
GuestAccessHelper(
this@ConversationInfoController,
it,
conversation!!,
conversationUser
).setupGuestAccess()
}
binding?.notificationSettingsView?.notificationSettings?.visibility = VISIBLE
} }
} catch (npe: NullPointerException) {
// view binding can be null if (!conversation!!.canDelete(conversationUser)) {
// since this is called asynchronously and UI might have been destroyed in the meantime binding?.deleteConversationAction?.visibility = GONE
Log.i(TAG, "UI destroyed - view binding already gone") } else {
binding?.deleteConversationAction?.visibility = VISIBLE
}
if (Conversation.ConversationType.ROOM_SYSTEM == conversation!!.type) {
binding?.notificationSettingsView?.callNotifications?.visibility = GONE
}
if (conversation!!.notificationCalls === null) {
binding?.notificationSettingsView?.callNotifications?.visibility = GONE
} else {
binding?.notificationSettingsView?.callNotifications?.value =
conversationCopy.notificationCalls == 1
}
getListOfParticipants()
binding?.progressBar?.visibility = GONE
binding?.conversationInfoName?.visibility = VISIBLE
binding?.displayNameText?.text = conversation!!.displayName
if (conversation!!.description != null && !conversation!!.description!!.isEmpty()) {
binding?.descriptionText?.text = conversation!!.description
binding?.conversationDescription?.visibility = VISIBLE
}
loadConversationAvatar()
adjustNotificationLevelUI()
initExpiringMessageOption()
binding?.let {
GuestAccessHelper(
this@ConversationInfoController,
it,
conversation!!,
conversationUser
).setupGuestAccess()
}
binding?.notificationSettingsView?.notificationSettings?.visibility = VISIBLE
} }
} }

View File

@ -355,19 +355,13 @@ class ProfileController : BaseController(R.layout.controller_profile) {
return return
} }
try { binding?.emptyList?.emptyListViewHeadline?.text = headline
binding?.emptyList?.emptyListViewHeadline?.text = headline binding?.emptyList?.emptyListViewText?.text = message
binding?.emptyList?.emptyListViewText?.text = message binding?.emptyList?.emptyListIcon?.setImageResource(errorResource)
binding?.emptyList?.emptyListIcon?.setImageResource(errorResource) binding?.emptyList?.emptyListIcon?.visibility = View.VISIBLE
binding?.emptyList?.emptyListIcon?.visibility = View.VISIBLE binding?.emptyList?.emptyListViewText?.visibility = View.VISIBLE
binding?.emptyList?.emptyListViewText?.visibility = View.VISIBLE binding?.userinfoList?.visibility = View.GONE
binding?.userinfoList?.visibility = View.GONE binding?.loadingContent?.visibility = View.GONE
binding?.loadingContent?.visibility = View.GONE
} catch (npe: NullPointerException) {
// view binding can be null
// since this is called asynchronously and UI might have been destroyed in the meantime
Log.i(TAG, "UI destroyed - view binding already gone")
}
} }
@Suppress("Detekt.LongMethod") @Suppress("Detekt.LongMethod")

View File

@ -29,7 +29,6 @@ import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.security.KeyChain import android.security.KeyChain
import android.text.TextUtils import android.text.TextUtils
import android.util.Log
import android.view.KeyEvent import android.view.KeyEvent
import android.view.View import android.view.View
import android.view.inputmethod.EditorInfo import android.view.inputmethod.EditorInfo
@ -199,27 +198,21 @@ class ServerSelectionController :
@Suppress("Detekt.TooGenericExceptionCaught") @Suppress("Detekt.TooGenericExceptionCaught")
private fun checkServerAndProceed() { private fun checkServerAndProceed() {
dispose() dispose()
try { var url: String = binding?.serverEntryTextInputEditText?.text.toString().trim { it <= ' ' }
var url: String = binding?.serverEntryTextInputEditText?.text.toString().trim { it <= ' ' } binding?.serverEntryTextInputEditText?.isEnabled = false
binding?.serverEntryTextInputEditText?.isEnabled = false showserverEntryProgressBar()
showserverEntryProgressBar() if (binding?.importOrChooseProviderText?.visibility != View.INVISIBLE) {
if (binding?.importOrChooseProviderText?.visibility != View.INVISIBLE) { binding?.importOrChooseProviderText?.visibility = View.INVISIBLE
binding?.importOrChooseProviderText?.visibility = View.INVISIBLE binding?.certTextView?.visibility = View.INVISIBLE
binding?.certTextView?.visibility = View.INVISIBLE }
} if (url.endsWith("/")) {
if (url.endsWith("/")) { url = url.substring(0, url.length - 1)
url = url.substring(0, url.length - 1) }
} val queryUrl = url + ApiUtils.getUrlPostfixForStatus()
val queryUrl = url + ApiUtils.getUrlPostfixForStatus() if (UriUtils.hasHttpProtocollPrefixed(url)) {
if (UriUtils.hasHttpProtocollPrefixed(url)) { checkServer(queryUrl, false)
checkServer(queryUrl, false) } else {
} else { checkServer("https://$queryUrl", true)
checkServer("https://$queryUrl", true)
}
} catch (npe: NullPointerException) {
// view binding can be null
// since this is called asynchronously and UI might have been destroyed in the meantime
Log.i(TAG, "UI destroyed - view binding already gone")
} }
} }
@ -357,18 +350,12 @@ class ServerSelectionController :
private fun setCertTextView() { private fun setCertTextView() {
if (activity != null) { if (activity != null) {
activity!!.runOnUiThread { activity!!.runOnUiThread {
try { if (!TextUtils.isEmpty(appPreferences!!.temporaryClientCertAlias)) {
if (!TextUtils.isEmpty(appPreferences!!.temporaryClientCertAlias)) { binding?.certTextView?.setText(R.string.nc_change_cert_auth)
binding?.certTextView?.setText(R.string.nc_change_cert_auth) } else {
} else { binding?.certTextView?.setText(R.string.nc_configure_cert_auth)
binding?.certTextView?.setText(R.string.nc_configure_cert_auth)
}
hideserverEntryProgressBar()
} catch (npe: java.lang.NullPointerException) {
// view binding can be null
// since this is called asynchronously and UI might have been destroyed in the meantime
Log.i(TAG, "UI destroyed - view binding already gone")
} }
hideserverEntryProgressBar()
} }
} }
} }

View File

@ -848,26 +848,20 @@ class SettingsController : BaseController(R.layout.controller_settings) {
private inner class ProxyTypeChangeListener : OnPreferenceValueChangedListener<String> { private inner class ProxyTypeChangeListener : OnPreferenceValueChangedListener<String> {
@Suppress("Detekt.TooGenericExceptionCaught") @Suppress("Detekt.TooGenericExceptionCaught")
override fun onChanged(newValue: String) { override fun onChanged(newValue: String) {
try { if (("No proxy" == newValue)) {
if (("No proxy" == newValue)) { hideProxySettings()
hideProxySettings() } else {
} else { when (newValue) {
when (newValue) { "HTTP" ->
"HTTP" -> binding?.settingsProxyPortEdit?.value = "3128"
binding?.settingsProxyPortEdit?.value = "3128" "DIRECT" ->
"DIRECT" -> binding?.settingsProxyPortEdit?.value = "8080"
binding?.settingsProxyPortEdit?.value = "8080" "SOCKS" ->
"SOCKS" -> binding?.settingsProxyPortEdit?.value = "1080"
binding?.settingsProxyPortEdit?.value = "1080" else -> {
else -> {
}
} }
showProxySettings()
} }
} catch (npe: NullPointerException) { showProxySettings()
// view binding can be null
// since this is called asynchronously and UI might have been destroyed in the meantime
Log.i(TAG, "UI destroyed - view binding already gone")
} }
} }
} }

View File

@ -178,39 +178,33 @@ class WebViewLoginController(args: Bundle? = null) : BaseController(
@Suppress("Detekt.TooGenericExceptionCaught") @Suppress("Detekt.TooGenericExceptionCaught")
override fun onPageFinished(view: WebView, url: String) { override fun onPageFinished(view: WebView, url: String) {
try { loginStep++
loginStep++ if (!basePageLoaded) {
if (!basePageLoaded) { binding?.progressBar?.visibility = View.GONE
binding?.progressBar?.visibility = View.GONE binding?.webview?.visibility = View.VISIBLE
binding?.webview?.visibility = View.VISIBLE
basePageLoaded = true basePageLoaded = true
} }
if (!TextUtils.isEmpty(username)) { if (!TextUtils.isEmpty(username)) {
if (loginStep == 1) { if (loginStep == 1) {
binding?.webview?.loadUrl(
"javascript: {document.getElementsByClassName('login')[0].click(); };"
)
} else if (!automatedLoginAttempted) {
automatedLoginAttempted = true
if (TextUtils.isEmpty(password)) {
binding?.webview?.loadUrl( binding?.webview?.loadUrl(
"javascript: {document.getElementsByClassName('login')[0].click(); };" "javascript:var justStore = document.getElementById('user').value = '$username';"
)
} else {
binding?.webview?.loadUrl(
"javascript: {" +
"document.getElementById('user').value = '" + username + "';" +
"document.getElementById('password').value = '" + password + "';" +
"document.getElementById('submit').click(); };"
) )
} else if (!automatedLoginAttempted) {
automatedLoginAttempted = true
if (TextUtils.isEmpty(password)) {
binding?.webview?.loadUrl(
"javascript:var justStore = document.getElementById('user').value = '$username';"
)
} else {
binding?.webview?.loadUrl(
"javascript: {" +
"document.getElementById('user').value = '" + username + "';" +
"document.getElementById('password').value = '" + password + "';" +
"document.getElementById('submit').click(); };"
)
}
} }
} }
} catch (npe: NullPointerException) {
// view binding can be null
// since this is called asynchronously and UI might have been destroyed in the meantime
Log.i(TAG, "UI destroyed - view binding already gone")
} }
super.onPageFinished(view, url) super.onPageFinished(view, url)