Remove unnecessary safe calls and non-null assertions

Signed-off-by: Tim Krüger <t@timkrueger.me>
This commit is contained in:
Tim Krüger 2022-09-06 17:18:17 +02:00
parent 439385e280
commit 8f6e759d48
No known key found for this signature in database
GPG Key ID: FECE3A7222C52A4E

View File

@ -583,8 +583,8 @@ class ChatController(args: Bundle) :
) )
var senderId = "" var senderId = ""
if (!conversationUser?.userId.equals("?")) { if (!conversationUser.userId.equals("?")) {
senderId = "users/" + conversationUser?.userId senderId = "users/" + conversationUser.userId
} else { } else {
senderId = currentConversation?.actorType + "/" + currentConversation?.actorId senderId = currentConversation?.actorType + "/" + currentConversation?.actorId
} }
@ -618,7 +618,7 @@ class ChatController(args: Bundle) :
R.id.playPauseBtn R.id.playPauseBtn
) { view, message -> ) { view, message ->
val filename = message.selectedIndividualHashMap!!["name"] val filename = message.selectedIndividualHashMap!!["name"]
val file = File(context!!.cacheDir, filename!!) val file = File(context.cacheDir, filename!!)
if (file.exists()) { if (file.exists()) {
if (message.isPlayingVoiceMessage) { if (message.isPlayingVoiceMessage) {
pausePlayback(message) pausePlayback(message)
@ -771,7 +771,7 @@ class ChatController(args: Bundle) :
requestRecordAudioPermissions() requestRecordAudioPermissions()
return true return true
} }
if (!UploadAndShareFilesWorker.isStoragePermissionGranted(context!!)) { if (!UploadAndShareFilesWorker.isStoragePermissionGranted(context)) {
UploadAndShareFilesWorker.requestStoragePermission(this@ChatController) UploadAndShareFilesWorker.requestStoragePermission(this@ChatController)
return true return true
} }
@ -806,7 +806,7 @@ class ChatController(args: Bundle) :
Log.d(TAG, "voiceRecordDuration: " + voiceRecordDuration) Log.d(TAG, "voiceRecordDuration: " + voiceRecordDuration)
Toast.makeText( Toast.makeText(
context, context,
context!!.getString(R.string.nc_voice_message_hold_to_record_info), context.getString(R.string.nc_voice_message_hold_to_record_info),
Toast.LENGTH_SHORT Toast.LENGTH_SHORT
).show() ).show()
stopAndDiscardAudioRecording() stopAndDiscardAudioRecording()
@ -993,7 +993,7 @@ class ChatController(args: Bundle) :
if (mediaPlayer == null) { if (mediaPlayer == null) {
val fileName = message.selectedIndividualHashMap!!["name"] val fileName = message.selectedIndividualHashMap!!["name"]
val absolutePath = context!!.cacheDir.absolutePath + "/" + fileName val absolutePath = context.cacheDir.absolutePath + "/" + fileName
mediaPlayer = MediaPlayer().apply { mediaPlayer = MediaPlayer().apply {
setDataSource(absolutePath) setDataSource(absolutePath)
prepare() prepare()
@ -1050,7 +1050,7 @@ class ChatController(args: Bundle) :
// check if download worker is already running // check if download worker is already running
val workers = WorkManager.getInstance( val workers = WorkManager.getInstance(
context!! context
).getWorkInfosByTag(fileId!!) ).getWorkInfosByTag(fileId!!)
try { try {
for (workInfo in workers.get()) { for (workInfo in workers.get()) {
@ -1081,7 +1081,7 @@ class ChatController(args: Bundle) :
WorkManager.getInstance().enqueue(downloadWorker) WorkManager.getInstance().enqueue(downloadWorker)
WorkManager.getInstance(context!!).getWorkInfoByIdLiveData(downloadWorker.id) WorkManager.getInstance(context).getWorkInfoByIdLiveData(downloadWorker.id)
.observeForever { workInfo: WorkInfo -> .observeForever { workInfo: WorkInfo ->
if (workInfo.state == WorkInfo.State.SUCCEEDED) { if (workInfo.state == WorkInfo.State.SUCCEEDED) {
startPlayback(message) startPlayback(message)
@ -1096,13 +1096,13 @@ class ChatController(args: Bundle) :
val date: String = simpleDateFormat.format(Date()) val date: String = simpleDateFormat.format(Date())
val fileNameWithoutSuffix = String.format( val fileNameWithoutSuffix = String.format(
context!!.resources.getString(R.string.nc_voice_message_filename), context.resources.getString(R.string.nc_voice_message_filename),
date, date,
currentConversation!!.displayName currentConversation!!.displayName
) )
val fileName = fileNameWithoutSuffix + VOICE_MESSAGE_FILE_SUFFIX val fileName = fileNameWithoutSuffix + VOICE_MESSAGE_FILE_SUFFIX
currentVoiceRecordFile = "${context!!.cacheDir.absolutePath}/$fileName" currentVoiceRecordFile = "${context.cacheDir.absolutePath}/$fileName"
} }
private fun showRecordAudioUi(show: Boolean) { private fun showRecordAudioUi(show: Boolean) {
@ -1124,14 +1124,14 @@ class ChatController(args: Bundle) :
binding.messageInputView.smileyButton.visibility = View.VISIBLE binding.messageInputView.smileyButton.visibility = View.VISIBLE
binding.messageInputView.messageInput.visibility = View.VISIBLE binding.messageInputView.messageInput.visibility = View.VISIBLE
binding.messageInputView.messageInput.hint = binding.messageInputView.messageInput.hint =
context?.resources?.getString(R.string.nc_hint_enter_a_message) context.resources?.getString(R.string.nc_hint_enter_a_message)
} }
} }
private fun isRecordAudioPermissionGranted(): Boolean { private fun isRecordAudioPermissionGranted(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return PermissionChecker.checkSelfPermission( return PermissionChecker.checkSelfPermission(
context!!, context,
Manifest.permission.RECORD_AUDIO Manifest.permission.RECORD_AUDIO
) == PermissionChecker.PERMISSION_GRANTED ) == PermissionChecker.PERMISSION_GRANTED
} else { } else {
@ -1211,7 +1211,7 @@ class ChatController(args: Bundle) :
} }
fun vibrate() { fun vibrate() {
val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
if (Build.VERSION.SDK_INT >= O) { if (Build.VERSION.SDK_INT >= O) {
vibrator.vibrate(VibrationEffect.createOneShot(SHORT_VIBRATE, VibrationEffect.DEFAULT_AMPLITUDE)) vibrator.vibrate(VibrationEffect.createOneShot(SHORT_VIBRATE, VibrationEffect.DEFAULT_AMPLITUDE))
} else { } else {
@ -1411,10 +1411,10 @@ class ChatController(args: Bundle) :
} }
val confirmationQuestion = when (filesToUpload.size) { val confirmationQuestion = when (filesToUpload.size) {
1 -> context?.resources?.getString(R.string.nc_upload_confirm_send_single)?.let { 1 -> context.resources?.getString(R.string.nc_upload_confirm_send_single)?.let {
String.format(it, title.trim()) String.format(it, title.trim())
} }
else -> context?.resources?.getString(R.string.nc_upload_confirm_send_multiple)?.let { else -> context.resources?.getString(R.string.nc_upload_confirm_send_multiple)?.let {
String.format(it, title.trim()) String.format(it, title.trim())
} }
} }
@ -1445,11 +1445,11 @@ class ChatController(args: Bundle) :
dialog.getButton(AlertDialog.BUTTON_NEGATIVE) dialog.getButton(AlertDialog.BUTTON_NEGATIVE)
) )
} catch (e: IllegalStateException) { } catch (e: IllegalStateException) {
Toast.makeText(context, context?.resources?.getString(R.string.nc_upload_failed), Toast.LENGTH_LONG) Toast.makeText(context, context.resources?.getString(R.string.nc_upload_failed), Toast.LENGTH_LONG)
.show() .show()
Log.e(javaClass.simpleName, "Something went wrong when trying to upload file", e) Log.e(javaClass.simpleName, "Something went wrong when trying to upload file", e)
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
Toast.makeText(context, context?.resources?.getString(R.string.nc_upload_failed), Toast.LENGTH_LONG) Toast.makeText(context, context.resources?.getString(R.string.nc_upload_failed), Toast.LENGTH_LONG)
.show() .show()
Log.e(javaClass.simpleName, "Something went wrong when trying to upload file", e) Log.e(javaClass.simpleName, "Something went wrong when trying to upload file", e)
} }
@ -1460,8 +1460,8 @@ class ChatController(args: Bundle) :
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
val id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID)) val id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID))
val fileName = ContactUtils.getDisplayNameFromDeviceContact(context!!, id) + ".vcf" val fileName = ContactUtils.getDisplayNameFromDeviceContact(context, id) + ".vcf"
val file = File(context?.cacheDir, fileName) val file = File(context.cacheDir, fileName)
writeContactToVcfFile(cursor, file) writeContactToVcfFile(cursor, file)
val shareUri = FileProvider.getUriForFile( val shareUri = FileProvider.getUriForFile(
@ -1486,7 +1486,7 @@ class ChatController(args: Bundle) :
} }
require(filesToUpload.isNotEmpty()) require(filesToUpload.isNotEmpty())
if (UploadAndShareFilesWorker.isStoragePermissionGranted(context!!)) { if (UploadAndShareFilesWorker.isStoragePermissionGranted(context)) {
uploadFiles(filesToUpload, false) uploadFiles(filesToUpload, false)
} else { } else {
UploadAndShareFilesWorker.requestStoragePermission(this) UploadAndShareFilesWorker.requestStoragePermission(this)
@ -1494,7 +1494,7 @@ class ChatController(args: Bundle) :
} catch (e: IllegalStateException) { } catch (e: IllegalStateException) {
Toast.makeText( Toast.makeText(
context, context,
context?.resources?.getString(R.string.nc_upload_failed), context.resources?.getString(R.string.nc_upload_failed),
Toast.LENGTH_LONG Toast.LENGTH_LONG
) )
.show() .show()
@ -1502,7 +1502,7 @@ class ChatController(args: Bundle) :
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
Toast.makeText( Toast.makeText(
context, context,
context?.resources?.getString(R.string.nc_upload_failed), context.resources?.getString(R.string.nc_upload_failed),
Toast.LENGTH_LONG Toast.LENGTH_LONG
) )
.show() .show()
@ -1554,7 +1554,7 @@ class ChatController(args: Bundle) :
} }
} else { } else {
Toast Toast
.makeText(context, context?.getString(R.string.read_storage_no_permission), Toast.LENGTH_LONG) .makeText(context, context.getString(R.string.read_storage_no_permission), Toast.LENGTH_LONG)
.show() .show()
} }
} else if (requestCode == REQUEST_RECORD_AUDIO_PERMISSION) { } else if (requestCode == REQUEST_RECORD_AUDIO_PERMISSION) {
@ -1563,7 +1563,7 @@ class ChatController(args: Bundle) :
} else { } else {
Toast.makeText( Toast.makeText(
context, context,
context!!.getString(R.string.nc_voice_message_missing_audio_permission), context.getString(R.string.nc_voice_message_missing_audio_permission),
Toast.LENGTH_LONG Toast.LENGTH_LONG
).show() ).show()
} }
@ -1574,17 +1574,17 @@ class ChatController(args: Bundle) :
} else { } else {
Toast.makeText( Toast.makeText(
context, context,
context!!.getString(R.string.nc_share_contact_permission), context.getString(R.string.nc_share_contact_permission),
Toast.LENGTH_LONG Toast.LENGTH_LONG
).show() ).show()
} }
} else if (requestCode == REQUEST_CAMERA_PERMISSION) { } else if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "launch cam activity since permission for cam has been granted") Log.d(TAG, "launch cam activity since permission for cam has been granted")
startActivityForResult(TakePhotoActivity.createIntent(context!!), REQUEST_CODE_PICK_CAMERA) startActivityForResult(TakePhotoActivity.createIntent(context), REQUEST_CODE_PICK_CAMERA)
} else { } else {
Toast Toast
.makeText(context, context?.getString(R.string.take_photo_permission), Toast.LENGTH_LONG) .makeText(context, context.getString(R.string.take_photo_permission), Toast.LENGTH_LONG)
.show() .show()
} }
} }
@ -1621,12 +1621,12 @@ class ChatController(args: Bundle) :
if (!isVoiceMessage) { if (!isVoiceMessage) {
Toast.makeText( Toast.makeText(
context, context,
context?.getString(R.string.nc_upload_in_progess), context.getString(R.string.nc_upload_in_progess),
Toast.LENGTH_LONG Toast.LENGTH_LONG
).show() ).show()
} }
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
Toast.makeText(context, context?.resources?.getString(R.string.nc_upload_failed), Toast.LENGTH_LONG).show() Toast.makeText(context, context.resources?.getString(R.string.nc_upload_failed), Toast.LENGTH_LONG).show()
Log.e(javaClass.simpleName, "Something went wrong when trying to upload file", e) Log.e(javaClass.simpleName, "Something went wrong when trying to upload file", e)
} }
} }
@ -1640,7 +1640,7 @@ class ChatController(args: Bundle) :
startActivityForResult( startActivityForResult(
Intent.createChooser( Intent.createChooser(
action, action,
context?.resources?.getString( context.resources?.getString(
R.string.nc_upload_choose_local_files R.string.nc_upload_choose_local_files
) )
), ),
@ -1743,13 +1743,13 @@ class ChatController(args: Bundle) :
onEmojiPopupShownListener = { onEmojiPopupShownListener = {
if (resources != null) { if (resources != null) {
smileyButton?.setImageDrawable( smileyButton?.setImageDrawable(
ContextCompat.getDrawable(context!!, R.drawable.ic_baseline_keyboard_24) ContextCompat.getDrawable(context, R.drawable.ic_baseline_keyboard_24)
) )
} }
}, },
onEmojiPopupDismissListener = { onEmojiPopupDismissListener = {
smileyButton?.setImageDrawable( smileyButton?.setImageDrawable(
ContextCompat.getDrawable(context!!, R.drawable.ic_insert_emoticon_black_24dp) ContextCompat.getDrawable(context, R.drawable.ic_insert_emoticon_black_24dp)
) )
}, },
onEmojiClickListener = { onEmojiClickListener = {
@ -1881,7 +1881,7 @@ class ChatController(args: Bundle) :
var apiVersion = 1 var apiVersion = 1
// FIXME Fix API checking with guests? // FIXME Fix API checking with guests?
if (conversationUser != null) { if (conversationUser != null) {
apiVersion = ApiUtils.getConversationApiVersion(conversationUser!!, intArrayOf(ApiUtils.APIv4, 1)) apiVersion = ApiUtils.getConversationApiVersion(conversationUser, intArrayOf(ApiUtils.APIv4, 1))
} }
val startNanoTime = System.nanoTime() val startNanoTime = System.nanoTime()
@ -2055,7 +2055,7 @@ class ChatController(args: Bundle) :
if (conversationUser != null) { if (conversationUser != null) {
val apiVersion = ApiUtils.getChatApiVersion(conversationUser, intArrayOf(1)) val apiVersion = ApiUtils.getChatApiVersion(conversationUser, intArrayOf(1))
ncApi!!.sendChatMessage( ncApi.sendChatMessage(
credentials, credentials,
ApiUtils.getUrlForChat(apiVersion, conversationUser.baseUrl, roomToken), ApiUtils.getUrlForChat(apiVersion, conversationUser.baseUrl, roomToken),
message, message,
@ -2388,7 +2388,7 @@ class ChatController(args: Bundle) :
unreadChatMessage.jsonMessageId = -1 unreadChatMessage.jsonMessageId = -1
unreadChatMessage.actorId = "-1" unreadChatMessage.actorId = "-1"
unreadChatMessage.timestamp = chatMessageList[0].timestamp unreadChatMessage.timestamp = chatMessageList[0].timestamp
unreadChatMessage.message = context?.getString(R.string.nc_new_messages) unreadChatMessage.message = context.getString(R.string.nc_new_messages)
adapter?.addToStart(unreadChatMessage, false) adapter?.addToStart(unreadChatMessage, false)
} }
@ -2760,7 +2760,7 @@ class ChatController(args: Bundle) :
currentConversation, currentConversation,
isShowMessageDeletionButton(message), isShowMessageDeletionButton(message),
hasChatPermission, hasChatPermission,
ncApi!! ncApi
).show() ).show()
} }
} }
@ -2838,7 +2838,7 @@ class ChatController(args: Bundle) :
message?.user?.id?.substring(INVITE_LENGTH), message?.user?.id?.substring(INVITE_LENGTH),
null null
) )
ncApi!!.createRoom( ncApi.createRoom(
credentials, credentials,
retrofitBucket.url, retrofitBucket.url,
retrofitBucket.queryMap retrofitBucket.queryMap
@ -2857,7 +2857,7 @@ class ChatController(args: Bundle) :
bundle.putString(KEY_ROOM_ID, roomOverall.ocs!!.data!!.roomId) bundle.putString(KEY_ROOM_ID, roomOverall.ocs!!.data!!.roomId)
// FIXME once APIv2+ is used only, the createRoom already returns all the data // FIXME once APIv2+ is used only, the createRoom already returns all the data
ncApi!!.getRoom( ncApi.getRoom(
credentials, credentials,
ApiUtils.getUrlForRoom( ApiUtils.getUrlForRoom(
apiVersion, apiVersion,
@ -2921,7 +2921,7 @@ class ChatController(args: Bundle) :
fun markAsUnread(message: IMessage?) { fun markAsUnread(message: IMessage?) {
val chatMessage = message as ChatMessage? val chatMessage = message as ChatMessage?
if (chatMessage!!.previousMessageId > NO_PREVIOUS_MESSAGE_ID) { if (chatMessage!!.previousMessageId > NO_PREVIOUS_MESSAGE_ID) {
ncApi!!.setChatReadMarker( ncApi.setChatReadMarker(
credentials, credentials,
ApiUtils.getUrlForSetChatReadMarker( ApiUtils.getUrlForSetChatReadMarker(
ApiUtils.getChatApiVersion(conversationUser, intArrayOf(ApiUtils.APIv1)), ApiUtils.getChatApiVersion(conversationUser, intArrayOf(ApiUtils.APIv1)),
@ -2966,7 +2966,7 @@ class ChatController(args: Bundle) :
return !message.isDeleted || // copy message return !message.isDeleted || // copy message
message.replyable || // reply to message.replyable || // reply to
message.replyable && // reply privately message.replyable && // reply privately
conversationUser?.userId?.isNotEmpty() == true && conversationUser?.userId != "?" && conversationUser?.userId?.isNotEmpty() == true && conversationUser.userId != "?" &&
message.user.id.startsWith("users/") && message.user.id.startsWith("users/") &&
message.user.id.substring(ACTOR_LENGTH) != currentConversation?.actorId && message.user.id.substring(ACTOR_LENGTH) != currentConversation?.actorId &&
currentConversation?.type != Conversation.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL || currentConversation?.type != Conversation.ConversationType.ROOM_TYPE_ONE_TO_ONE_CALL ||
@ -2993,7 +2993,7 @@ class ChatController(args: Bundle) :
quotedMessage?.ellipsize = TextUtils.TruncateAt.END quotedMessage?.ellipsize = TextUtils.TruncateAt.END
quotedMessage?.text = it.text quotedMessage?.text = it.text
binding.messageInputView.findViewById<EmojiTextView>(R.id.quotedMessageAuthor)?.text = binding.messageInputView.findViewById<EmojiTextView>(R.id.quotedMessageAuthor)?.text =
it.actorDisplayName ?: context!!.getText(R.string.nc_nick_guest) it.actorDisplayName ?: context.getText(R.string.nc_nick_guest)
conversationUser?.let { currentUser -> conversationUser?.let { currentUser ->
val quotedMessageImage = binding val quotedMessageImage = binding
@ -3091,7 +3091,7 @@ class ChatController(args: Bundle) :
val isOlderThanSixHours = message val isOlderThanSixHours = message
.createdAt .createdAt
?.before(Date(System.currentTimeMillis() - AGE_THREHOLD_FOR_DELETE_MESSAGE)) == true .before(Date(System.currentTimeMillis() - AGE_THREHOLD_FOR_DELETE_MESSAGE)) == true
return when { return when {
!isUserAllowedByPrivileges -> false !isUserAllowedByPrivileges -> false
@ -3219,7 +3219,7 @@ class ChatController(args: Bundle) :
if (!permissionUtil.isCameraPermissionGranted()) { if (!permissionUtil.isCameraPermissionGranted()) {
requestCameraPermissions() requestCameraPermissions()
} else { } else {
startActivityForResult(TakePhotoActivity.createIntent(context!!), REQUEST_CODE_PICK_CAMERA) startActivityForResult(TakePhotoActivity.createIntent(context), REQUEST_CODE_PICK_CAMERA)
} }
} }