From 1d9868daa648bc6da4cf0df862890ed417cebe3d Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Tue, 17 Jan 2023 08:37:42 +0100 Subject: [PATCH] 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 --- .../talk/controllers/ChatController.kt | 159 ++++++------------ .../controllers/ConversationInfoController.kt | 138 +++++++-------- .../talk/controllers/ProfileController.kt | 20 +-- .../controllers/ServerSelectionController.kt | 53 +++--- .../talk/controllers/SettingsController.kt | 30 ++-- .../controllers/WebViewLoginController.kt | 50 +++--- 6 files changed, 179 insertions(+), 271 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt index 8dd60445a..98c3ef723 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt @@ -367,22 +367,16 @@ class ChatController(args: Bundle) : setTitle() participantPermissions = ParticipantPermissions(conversationUser, currentConversation!!) - try { - setupSwipeToReply() - setupMentionAutocomplete() - checkShowCallButtons() - checkShowMessageInputView() - checkLobbyState() + setupSwipeToReply() + setupMentionAutocomplete() + checkShowCallButtons() + checkShowMessageInputView() + checkLobbyState() - if (!inConversation) { - joinRoomWithPassword() - } else { - 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") + if (!inConversation) { + joinRoomWithPassword() + } else { + Log.d(TAG, "already inConversation. joinRoomWithPassword is skipped") } } @@ -714,43 +708,37 @@ class ChatController(args: Bundle) : @Suppress("Detekt.TooGenericExceptionCaught") override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { - try { - if (s.length >= lengthFilter) { - binding?.messageInputView?.inputEditText?.error = String.format( - Objects.requireNonNull(resources).getString(R.string.nc_limit_hit), - lengthFilter.toString() - ) - } else { - binding?.messageInputView?.inputEditText?.error = null - } + if (s.length >= lengthFilter) { + binding?.messageInputView?.inputEditText?.error = String.format( + Objects.requireNonNull(resources).getString(R.string.nc_limit_hit), + lengthFilter.toString() + ) + } else { + binding?.messageInputView?.inputEditText?.error = null + } - val editable = binding?.messageInputView?.inputEditText?.editableText - if (editable != null && binding?.messageInputView?.inputEditText != null) { - val mentionSpans = editable.getSpans( - 0, - binding?.messageInputView?.inputEditText!!.length(), - Spans.MentionChipSpan::class.java - ) - var mentionSpan: Spans.MentionChipSpan - for (i in mentionSpans.indices) { - mentionSpan = mentionSpans[i] - if (start >= editable.getSpanStart(mentionSpan) && - start < editable.getSpanEnd(mentionSpan) + val editable = binding?.messageInputView?.inputEditText?.editableText + if (editable != null && binding?.messageInputView?.inputEditText != null) { + val mentionSpans = editable.getSpans( + 0, + binding?.messageInputView?.inputEditText!!.length(), + Spans.MentionChipSpan::class.java + ) + var mentionSpan: Spans.MentionChipSpan + for (i in mentionSpans.indices) { + mentionSpan = mentionSpans[i] + if (start >= editable.getSpanStart(mentionSpan) && + start < editable.getSpanEnd(mentionSpan) + ) { + if (editable.subSequence( + editable.getSpanStart(mentionSpan), + editable.getSpanEnd(mentionSpan) + ).toString().trim { it <= ' ' } != mentionSpan.label ) { - if (editable.subSequence( - editable.getSpanStart(mentionSpan), - editable.getSpanEnd(mentionSpan) - ).toString().trim { it <= ' ' } != mentionSpan.label - ) { - editable.removeSpan(mentionSpan) - } + 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) binding?.messageInputView?.messageInput?.doAfterTextChanged { - try { - if (binding?.messageInputView?.messageInput?.text?.isEmpty() == true) { - showMicrophoneButton(true) - } else { - 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") + if (binding?.messageInputView?.messageInput?.text?.isEmpty() == true) { + showMicrophoneButton(true) + } else { + showMicrophoneButton(false) } } @@ -1771,13 +1753,7 @@ class ChatController(args: Bundle) : ) }, onEmojiClickListener = { - try { - 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") - } + binding?.messageInputView?.inputEditText?.editableText?.append(" ") } ) } @@ -1937,13 +1913,7 @@ class ChatController(args: Bundle) : setupWebsocket() - try { - 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") - } + checkLobbyState() if (isFirstMessagesProcessing) { pullChatMessages(0) @@ -2123,17 +2093,10 @@ class ChatController(args: Bundle) : override fun onNext(genericOverall: GenericOverall) { myFirstMessage = message - try { - if (binding?.popupBubbleView?.isShown == true) { - 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") + if (binding?.popupBubbleView?.isShown == true) { + binding?.popupBubbleView?.hide() } + binding?.messagesListView?.smoothScrollToPosition(0) } override fun onError(e: Throwable) { @@ -2246,19 +2209,13 @@ class ChatController(args: Bundle) : override fun onNext(response: Response<*>) { Log.d(TAG, "pullChatMessages - pullChatMessages[lookIntoFuture > 0] - got response") pullChatMessagesPending = false - try { - if (response.code() == HTTP_CODE_NOT_MODIFIED) { - Log.d(TAG, "pullChatMessages - quasi recursive call to pullChatMessages") - pullChatMessages(1, setReadMarker, xChatLastCommonRead) - } else if (response.code() == HTTP_CODE_PRECONDITION_FAILED) { - futurePreconditionFailed = true - } else { - 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") + if (response.code() == HTTP_CODE_NOT_MODIFIED) { + Log.d(TAG, "pullChatMessages - quasi recursive call to pullChatMessages") + pullChatMessages(1, setReadMarker, xChatLastCommonRead) + } else if (response.code() == HTTP_CODE_PRECONDITION_FAILED) { + futurePreconditionFailed = true + } else { + processMessagesResponse(response, true) } processExpiredMessages() @@ -2291,16 +2248,10 @@ class ChatController(args: Bundle) : override fun onNext(response: Response<*>) { Log.d(TAG, "pullChatMessages - pullChatMessages[lookIntoFuture <= 0] - got response") pullChatMessagesPending = false - try { - if (response.code() == HTTP_CODE_PRECONDITION_FAILED) { - pastPreconditionFailed = true - } else { - 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) + if (response.code() == HTTP_CODE_PRECONDITION_FAILED) { + pastPreconditionFailed = true + } else { + processMessagesResponse(response, false) } processExpiredMessages() diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ConversationInfoController.kt b/app/src/main/java/com/nextcloud/talk/controllers/ConversationInfoController.kt index 650ae3aa3..944eae8a9 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ConversationInfoController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/ConversationInfoController.kt @@ -493,13 +493,7 @@ class ConversationInfoController(args: Bundle) : @Suppress("Detekt.TooGenericExceptionCaught") override fun onNext(participantsOverall: ParticipantsOverall) { - try { - 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") - } + handleParticipants(participantsOverall.ocs!!.data!!) } override fun onError(e: Throwable) { @@ -646,83 +640,77 @@ class ConversationInfoController(args: Bundle) : @Suppress("Detekt.TooGenericExceptionCaught") override fun onNext(roomOverall: RoomOverall) { - try { - conversation = roomOverall.ocs!!.data + conversation = roomOverall.ocs!!.data - val conversationCopy = conversation + val conversationCopy = conversation - if (conversationCopy!!.canModerate(conversationUser)) { - binding?.addParticipantsAction?.visibility = VISIBLE - if (CapabilitiesUtilNew.hasSpreedFeatureCapability(conversationUser, "clear-history")) { - binding?.clearConversationHistory?.visibility = VISIBLE - } else { - binding?.clearConversationHistory?.visibility = GONE - } + if (conversationCopy!!.canModerate(conversationUser)) { + binding?.addParticipantsAction?.visibility = VISIBLE + if (CapabilitiesUtilNew.hasSpreedFeatureCapability(conversationUser, "clear-history")) { + binding?.clearConversationHistory?.visibility = VISIBLE } else { - binding?.addParticipantsAction?.visibility = GONE binding?.clearConversationHistory?.visibility = GONE } + } else { + binding?.addParticipantsAction?.visibility = GONE + binding?.clearConversationHistory?.visibility = GONE + } - if (isAttached && (!isBeingDestroyed || !isDestroyed)) { - binding?.ownOptions?.visibility = VISIBLE + if (isAttached && (!isBeingDestroyed || !isDestroyed)) { + binding?.ownOptions?.visibility = VISIBLE - setupWebinaryView() + setupWebinaryView() - if (!conversation!!.canLeave()) { - binding?.leaveConversationAction?.visibility = GONE - } else { - 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 + if (!conversation!!.canLeave()) { + binding?.leaveConversationAction?.visibility = GONE + } else { + binding?.leaveConversationAction?.visibility = VISIBLE } - } 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 (!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 } } diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ProfileController.kt b/app/src/main/java/com/nextcloud/talk/controllers/ProfileController.kt index 6a6e87b30..83318fc89 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ProfileController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/ProfileController.kt @@ -355,19 +355,13 @@ class ProfileController : BaseController(R.layout.controller_profile) { return } - try { - binding?.emptyList?.emptyListViewHeadline?.text = headline - binding?.emptyList?.emptyListViewText?.text = message - binding?.emptyList?.emptyListIcon?.setImageResource(errorResource) - binding?.emptyList?.emptyListIcon?.visibility = View.VISIBLE - binding?.emptyList?.emptyListViewText?.visibility = View.VISIBLE - binding?.userinfoList?.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") - } + binding?.emptyList?.emptyListViewHeadline?.text = headline + binding?.emptyList?.emptyListViewText?.text = message + binding?.emptyList?.emptyListIcon?.setImageResource(errorResource) + binding?.emptyList?.emptyListIcon?.visibility = View.VISIBLE + binding?.emptyList?.emptyListViewText?.visibility = View.VISIBLE + binding?.userinfoList?.visibility = View.GONE + binding?.loadingContent?.visibility = View.GONE } @Suppress("Detekt.LongMethod") diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ServerSelectionController.kt b/app/src/main/java/com/nextcloud/talk/controllers/ServerSelectionController.kt index 3ca396525..ec6ef2307 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ServerSelectionController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/ServerSelectionController.kt @@ -29,7 +29,6 @@ import android.net.Uri import android.os.Bundle import android.security.KeyChain import android.text.TextUtils -import android.util.Log import android.view.KeyEvent import android.view.View import android.view.inputmethod.EditorInfo @@ -199,27 +198,21 @@ class ServerSelectionController : @Suppress("Detekt.TooGenericExceptionCaught") private fun checkServerAndProceed() { dispose() - try { - var url: String = binding?.serverEntryTextInputEditText?.text.toString().trim { it <= ' ' } - binding?.serverEntryTextInputEditText?.isEnabled = false - showserverEntryProgressBar() - if (binding?.importOrChooseProviderText?.visibility != View.INVISIBLE) { - binding?.importOrChooseProviderText?.visibility = View.INVISIBLE - binding?.certTextView?.visibility = View.INVISIBLE - } - if (url.endsWith("/")) { - url = url.substring(0, url.length - 1) - } - val queryUrl = url + ApiUtils.getUrlPostfixForStatus() - if (UriUtils.hasHttpProtocollPrefixed(url)) { - checkServer(queryUrl, false) - } else { - 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") + var url: String = binding?.serverEntryTextInputEditText?.text.toString().trim { it <= ' ' } + binding?.serverEntryTextInputEditText?.isEnabled = false + showserverEntryProgressBar() + if (binding?.importOrChooseProviderText?.visibility != View.INVISIBLE) { + binding?.importOrChooseProviderText?.visibility = View.INVISIBLE + binding?.certTextView?.visibility = View.INVISIBLE + } + if (url.endsWith("/")) { + url = url.substring(0, url.length - 1) + } + val queryUrl = url + ApiUtils.getUrlPostfixForStatus() + if (UriUtils.hasHttpProtocollPrefixed(url)) { + checkServer(queryUrl, false) + } else { + checkServer("https://$queryUrl", true) } } @@ -357,18 +350,12 @@ class ServerSelectionController : private fun setCertTextView() { if (activity != null) { activity!!.runOnUiThread { - try { - if (!TextUtils.isEmpty(appPreferences!!.temporaryClientCertAlias)) { - binding?.certTextView?.setText(R.string.nc_change_cert_auth) - } else { - 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") + if (!TextUtils.isEmpty(appPreferences!!.temporaryClientCertAlias)) { + binding?.certTextView?.setText(R.string.nc_change_cert_auth) + } else { + binding?.certTextView?.setText(R.string.nc_configure_cert_auth) } + hideserverEntryProgressBar() } } } diff --git a/app/src/main/java/com/nextcloud/talk/controllers/SettingsController.kt b/app/src/main/java/com/nextcloud/talk/controllers/SettingsController.kt index bfd31d227..ed194a7c7 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/SettingsController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/SettingsController.kt @@ -848,26 +848,20 @@ class SettingsController : BaseController(R.layout.controller_settings) { private inner class ProxyTypeChangeListener : OnPreferenceValueChangedListener { @Suppress("Detekt.TooGenericExceptionCaught") override fun onChanged(newValue: String) { - try { - if (("No proxy" == newValue)) { - hideProxySettings() - } else { - when (newValue) { - "HTTP" -> - binding?.settingsProxyPortEdit?.value = "3128" - "DIRECT" -> - binding?.settingsProxyPortEdit?.value = "8080" - "SOCKS" -> - binding?.settingsProxyPortEdit?.value = "1080" - else -> { - } + if (("No proxy" == newValue)) { + hideProxySettings() + } else { + when (newValue) { + "HTTP" -> + binding?.settingsProxyPortEdit?.value = "3128" + "DIRECT" -> + binding?.settingsProxyPortEdit?.value = "8080" + "SOCKS" -> + binding?.settingsProxyPortEdit?.value = "1080" + else -> { } - showProxySettings() } - } 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") + showProxySettings() } } } diff --git a/app/src/main/java/com/nextcloud/talk/controllers/WebViewLoginController.kt b/app/src/main/java/com/nextcloud/talk/controllers/WebViewLoginController.kt index 25e2d09c2..424f0156a 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/WebViewLoginController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/WebViewLoginController.kt @@ -178,39 +178,33 @@ class WebViewLoginController(args: Bundle? = null) : BaseController( @Suppress("Detekt.TooGenericExceptionCaught") override fun onPageFinished(view: WebView, url: String) { - try { - loginStep++ - if (!basePageLoaded) { - binding?.progressBar?.visibility = View.GONE - binding?.webview?.visibility = View.VISIBLE + loginStep++ + if (!basePageLoaded) { + binding?.progressBar?.visibility = View.GONE + binding?.webview?.visibility = View.VISIBLE - basePageLoaded = true - } - if (!TextUtils.isEmpty(username)) { - if (loginStep == 1) { + basePageLoaded = true + } + if (!TextUtils.isEmpty(username)) { + if (loginStep == 1) { + binding?.webview?.loadUrl( + "javascript: {document.getElementsByClassName('login')[0].click(); };" + ) + } else if (!automatedLoginAttempted) { + automatedLoginAttempted = true + if (TextUtils.isEmpty(password)) { 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)