From 8febcdf8d87d85899963af245b10a6b0ecbe7445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Kr=C3=BCger?= Date: Wed, 4 May 2022 09:18:18 +0200 Subject: [PATCH 1/3] Replace deprecated Detekt configuration options MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tim Krüger --- detekt.yml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/detekt.yml b/detekt.yml index 734b224e5..d1b2a89e7 100644 --- a/detekt.yml +++ b/detekt.yml @@ -69,7 +69,8 @@ complexity: excludes: ['**/androidTest/**'] LongParameterList: active: true - threshold: 6 + functionThreshold: 6 + constructorThreshold: 7 ignoreDefaultParameters: false MethodOverloading: active: false @@ -333,7 +334,7 @@ potential-bugs: active: false LateinitUsage: active: false - excludeAnnotatedProperties: "" + ignoreAnnotated: [] ignoreOnClassesPattern: "" UnconditionalJumpStatementInLoop: active: false @@ -439,10 +440,10 @@ style: active: false UnderscoresInNumericLiterals: active: false - acceptableDecimalLength: 5 + acceptableLength: 5 UnnecessaryAbstractClass: active: false - excludeAnnotatedClasses: "dagger.Module" + ignoreAnnotated: ["dagger.Module"] UnnecessaryApply: active: false UnnecessaryInheritance: @@ -462,7 +463,7 @@ style: allowedNames: "(_|ignored|expected|serialVersionUID)" UseDataClass: active: false - excludeAnnotatedClasses: "" + ignoreAnnotated: [] UtilityClassWithPublicConstructor: active: false VarCouldBeVal: From 377e1711562c9eeee60fa337ba1d3265d3504627 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Kr=C3=BCger?= Date: Wed, 4 May 2022 09:35:21 +0200 Subject: [PATCH 2/3] Fix Detekt issues in ChatUtils MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ComplexMethod - 11/10 - [getParsedMessage] - NestedBlockDepth - 4/4 - [getParsedMessage] Signed-off-by: Tim Krüger --- .../talk/models/json/chat/ChatUtils.kt | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatUtils.kt b/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatUtils.kt index 030de6b14..c1251ae49 100644 --- a/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatUtils.kt +++ b/app/src/main/java/com/nextcloud/talk/models/json/chat/ChatUtils.kt @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 Mario Danic * @author Marcel Hibbe * Copyright (C) 2021 Marcel Hibbe + * @author Tim Krüger + * Copyright (C) 2022 Tim Krüger * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,24 +28,32 @@ class ChatUtils { companion object { fun getParsedMessage(message: String?, messageParameters: HashMap>?): String? { - var resultMessage = message if (messageParameters != null && messageParameters.size > 0) { - for (key in messageParameters.keys) { - val individualHashMap = messageParameters[key] - val type = individualHashMap?.get("type") - if (type == "user" || type == "guest" || type == "call") { - resultMessage = resultMessage?.replace("{$key}", "@" + individualHashMap["name"]) - } else if (type == "geo-location") { - resultMessage = individualHashMap.get("name") - } else if (individualHashMap?.containsKey("link") == true) { - resultMessage = if (type == "file") { - resultMessage?.replace("{$key}", individualHashMap["name"].toString()) - } else { - individualHashMap["link"].toString() - } + return parse(messageParameters, message) + } + return message + } + + private fun parse( + messageParameters: HashMap>, + message: String? + ): String? { + var resultMessage = message + for (key in messageParameters.keys) { + val individualHashMap = messageParameters[key] + val type = individualHashMap?.get("type") + if (type == "user" || type == "guest" || type == "call") { + resultMessage = resultMessage?.replace("{$key}", "@" + individualHashMap["name"]) + } else if (type == "geo-location") { + resultMessage = individualHashMap.get("name") + } else if (individualHashMap?.containsKey("link") == true) { + resultMessage = if (type == "file") { + resultMessage?.replace("{$key}", individualHashMap["name"].toString()) } else { - resultMessage = individualHashMap?.get("name")?.let { resultMessage?.replace("{$key}", it) } + individualHashMap["link"].toString() } + } else { + resultMessage = individualHashMap?.get("name")?.let { resultMessage?.replace("{$key}", it) } } } return resultMessage From 0c1303e0afa81e8c0b29d209029113a7fd5d6457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Kr=C3=BCger?= Date: Wed, 4 May 2022 11:28:30 +0200 Subject: [PATCH 3/3] Fix Detekt issues in LocationPickerController MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - LongMethod - 94/60 - [initMap] - ComplexMethod - 15/10 - [initMap] - ReturnCount - [areAllGranted] Signed-off-by: Tim Krüger --- .../controllers/LocationPickerController.kt | 150 +++++++++--------- 1 file changed, 78 insertions(+), 72 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/controllers/LocationPickerController.kt b/app/src/main/java/com/nextcloud/talk/controllers/LocationPickerController.kt index 87618c774..6ba4803da 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/LocationPickerController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/LocationPickerController.kt @@ -213,7 +213,7 @@ class LocationPickerController(args: Bundle) : return true } - @Suppress("Detekt.TooGenericExceptionCaught") + @Suppress("Detekt.TooGenericExceptionCaught", "Detekt.ComplexMethod") private fun initMap() { binding.map.setTileSource(TileSourceFactory.MAPNIK) binding.map.onResume() @@ -223,43 +223,7 @@ class LocationPickerController(args: Bundle) : if (!isLocationPermissionsGranted()) { requestLocationPermissions() } else { - try { - when { - locationManager!!.isProviderEnabled(LocationManager.NETWORK_PROVIDER) -> { - locationManager!!.requestLocationUpdates( - LocationManager.NETWORK_PROVIDER, - MIN_LOCATION_UPDATE_TIME, - MIN_LOCATION_UPDATE_DISTANCE, - this - ) - } - locationManager!!.isProviderEnabled(LocationManager.GPS_PROVIDER) -> { - locationManager!!.requestLocationUpdates( - LocationManager.GPS_PROVIDER, - MIN_LOCATION_UPDATE_TIME, - MIN_LOCATION_UPDATE_DISTANCE, - this - ) - Log.d(TAG, "LocationManager.NETWORK_PROVIDER falling back to LocationManager.GPS_PROVIDER") - } - else -> { - Log.e( - TAG, - "Error requesting location updates. Probably this is a phone without google services" + - " and there is no alternative like UnifiedNlp installed. Furthermore no GPS is " + - "supported." - ) - Toast.makeText(context, context?.getString(R.string.nc_location_unknown), Toast.LENGTH_LONG) - .show() - } - } - } catch (e: SecurityException) { - Log.e(TAG, "Error when requesting location updates. Permissions may be missing.", e) - Toast.makeText(context, context?.getString(R.string.nc_location_unknown), Toast.LENGTH_LONG).show() - } catch (e: Exception) { - Log.e(TAG, "Error when requesting location updates.", e) - Toast.makeText(context, context?.getString(R.string.nc_common_error_sorry), Toast.LENGTH_LONG).show() - } + requestLocationUpdates() } val copyrightOverlay = CopyrightOverlay(context) @@ -317,41 +281,84 @@ class LocationPickerController(args: Bundle) : } binding.map.addMapListener( - DelayedMapListener( - object : MapListener { - @Suppress("Detekt.TooGenericExceptionCaught") - override fun onScroll(paramScrollEvent: ScrollEvent): Boolean { - try { - when { - moveToCurrentLocationWasClicked -> { - setLocationDescription(isGpsLocation = true, isGeocodedResult = false) - moveToCurrentLocationWasClicked = false - } - receivedChosenGeocodingResult -> { - binding.shareLocation.isClickable = true - setLocationDescription(isGpsLocation = false, isGeocodedResult = true) - receivedChosenGeocodingResult = false - } - else -> { - binding.shareLocation.isClickable = true - setLocationDescription(isGpsLocation = false, isGeocodedResult = false) - } - } - } catch (e: NullPointerException) { - Log.d(TAG, "UI already closed") - } - - readyToShareLocation = true - return true - } - - override fun onZoom(event: ZoomEvent): Boolean { - return false - } - }) + delayedMapListener() ) } + private fun delayedMapListener() = DelayedMapListener( + object : MapListener { + @Suppress("Detekt.TooGenericExceptionCaught") + override fun onScroll(paramScrollEvent: ScrollEvent): Boolean { + try { + when { + moveToCurrentLocationWasClicked -> { + setLocationDescription(isGpsLocation = true, isGeocodedResult = false) + moveToCurrentLocationWasClicked = false + } + receivedChosenGeocodingResult -> { + binding.shareLocation.isClickable = true + setLocationDescription(isGpsLocation = false, isGeocodedResult = true) + receivedChosenGeocodingResult = false + } + else -> { + binding.shareLocation.isClickable = true + setLocationDescription(isGpsLocation = false, isGeocodedResult = false) + } + } + } catch (e: NullPointerException) { + Log.d(TAG, "UI already closed") + } + + readyToShareLocation = true + return true + } + + override fun onZoom(event: ZoomEvent): Boolean { + return false + } + }) + + @Suppress("Detekt.TooGenericExceptionCaught") + private fun requestLocationUpdates() { + try { + when { + locationManager!!.isProviderEnabled(LocationManager.NETWORK_PROVIDER) -> { + locationManager!!.requestLocationUpdates( + LocationManager.NETWORK_PROVIDER, + MIN_LOCATION_UPDATE_TIME, + MIN_LOCATION_UPDATE_DISTANCE, + this + ) + } + locationManager!!.isProviderEnabled(LocationManager.GPS_PROVIDER) -> { + locationManager!!.requestLocationUpdates( + LocationManager.GPS_PROVIDER, + MIN_LOCATION_UPDATE_TIME, + MIN_LOCATION_UPDATE_DISTANCE, + this + ) + Log.d(TAG, "LocationManager.NETWORK_PROVIDER falling back to LocationManager.GPS_PROVIDER") + } + else -> { + Log.e( + TAG, + "Error requesting location updates. Probably this is a phone without google services" + + " and there is no alternative like UnifiedNlp installed. Furthermore no GPS is " + + "supported." + ) + Toast.makeText(context, context?.getString(R.string.nc_location_unknown), Toast.LENGTH_LONG) + .show() + } + } + } catch (e: SecurityException) { + Log.e(TAG, "Error when requesting location updates. Permissions may be missing.", e) + Toast.makeText(context, context?.getString(R.string.nc_location_unknown), Toast.LENGTH_LONG).show() + } catch (e: Exception) { + Log.e(TAG, "Error when requesting location updates.", e) + Toast.makeText(context, context?.getString(R.string.nc_common_error_sorry), Toast.LENGTH_LONG).show() + } + } + private fun setLocationDescription(isGpsLocation: Boolean, isGeocodedResult: Boolean) { when { isGpsLocation -> { @@ -467,11 +474,10 @@ class LocationPickerController(args: Bundle) : grantResults: IntArray ) { fun areAllGranted(grantResults: IntArray): Boolean { - if (grantResults.isEmpty()) return false grantResults.forEach { if (it == PackageManager.PERMISSION_DENIED) return false } - return true + return grantResults.isNotEmpty() } if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE && areAllGranted(grantResults)) {