Merge pull request #10191 from nextcloud/fix/some-detekt

Fix a bunch of Detekt issues
This commit is contained in:
Álvaro Brey 2022-05-09 10:04:47 +02:00 committed by GitHub
commit 91efa194c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 161 additions and 170 deletions

View file

@ -1,5 +1,5 @@
build: build:
maxIssues: 11 maxIssues: 3
weights: weights:
# complexity: 2 # complexity: 2
# LongParameterList: 1 # LongParameterList: 1

View file

@ -81,6 +81,9 @@ private const val LAST_HOUR_OF_DAY = 23
private const val LAST_MINUTE_OF_HOUR = 59 private const val LAST_MINUTE_OF_HOUR = 59
private const val LAST_SECOND_OF_MINUTE = 59 private const val LAST_SECOND_OF_MINUTE = 59
private const val CLEAR_AT_TYPE_PERIOD = "period"
private const val CLEAR_AT_TYPE_END_OF = "end-of"
class SetStatusDialogFragment : class SetStatusDialogFragment :
DialogFragment(), DialogFragment(),
PredefinedStatusClickListener, PredefinedStatusClickListener,
@ -147,26 +150,7 @@ class SetStatusDialogFragment :
accountManager = (activity as BaseActivity).userAccountManager accountManager = (activity as BaseActivity).userAccountManager
currentStatus?.let { currentStatus?.let {
binding.emoji.setText(it.icon) updateCurrentStatusViews(it)
binding.customStatusInput.text?.clear()
binding.customStatusInput.setText(it.message)
visualizeStatus(it.status)
if (it.clearAt > 0) {
binding.clearStatusAfterSpinner.visibility = View.GONE
binding.remainingClearTime.apply {
binding.clearStatusMessageTextView.text = getString(R.string.clear_status_message)
visibility = View.VISIBLE
text = DisplayUtils.getRelativeTimestamp(context, it.clearAt * ONE_SECOND_IN_MILLIS, true)
.toString()
.decapitalize(Locale.getDefault())
setOnClickListener {
visibility = View.GONE
binding.clearStatusAfterSpinner.visibility = View.VISIBLE
binding.clearStatusMessageTextView.text = getString(R.string.clear_status_message_after)
}
}
}
} }
adapter = PredefinedStatusListAdapter(this, requireContext()) adapter = PredefinedStatusListAdapter(this, requireContext())
@ -183,7 +167,7 @@ class SetStatusDialogFragment :
binding.clearStatus.setOnClickListener { clearStatus() } binding.clearStatus.setOnClickListener { clearStatus() }
binding.setStatus.setOnClickListener { setStatusMessage() } binding.setStatus.setOnClickListener { setStatusMessage() }
binding.emoji.setOnClickListener { openEmojiPopup() } binding.emoji.setOnClickListener { popup.show() }
popup = EmojiPopup.Builder popup = EmojiPopup.Builder
.fromRootView(view) .fromRootView(view)
@ -229,83 +213,84 @@ class SetStatusDialogFragment :
) )
} }
@Suppress("ComplexMethod") private fun updateCurrentStatusViews(it: Status) {
private fun setClearStatusAfterValue(item: Int) { binding.emoji.setText(it.icon)
when (item) { binding.customStatusInput.text?.clear()
POS_DONT_CLEAR -> { binding.customStatusInput.setText(it.message)
// don't clear visualizeStatus(it.status)
clearAt = null
}
if (it.clearAt > 0) {
binding.clearStatusAfterSpinner.visibility = View.GONE
binding.remainingClearTime.apply {
binding.clearStatusMessageTextView.text = getString(R.string.clear_status_message)
visibility = View.VISIBLE
text = DisplayUtils.getRelativeTimestamp(context, it.clearAt * ONE_SECOND_IN_MILLIS, true)
.toString()
.replaceFirstChar { it.lowercase(Locale.getDefault()) }
setOnClickListener {
visibility = View.GONE
binding.clearStatusAfterSpinner.visibility = View.VISIBLE
binding.clearStatusMessageTextView.text = getString(R.string.clear_status_message_after)
}
}
}
}
private fun setClearStatusAfterValue(item: Int) {
clearAt = when (item) {
POS_DONT_CLEAR -> null // don't clear
POS_HALF_AN_HOUR -> { POS_HALF_AN_HOUR -> {
// 30 minutes // 30 minutes
clearAt = System.currentTimeMillis() / ONE_SECOND_IN_MILLIS + THIRTY_MINUTES * ONE_MINUTE_IN_SECONDS System.currentTimeMillis() / ONE_SECOND_IN_MILLIS + THIRTY_MINUTES * ONE_MINUTE_IN_SECONDS
} }
POS_AN_HOUR -> { POS_AN_HOUR -> {
// one hour // one hour
clearAt = System.currentTimeMillis() / ONE_SECOND_IN_MILLIS + ONE_MINUTE_IN_SECONDS * ONE_MINUTE_IN_SECONDS
System.currentTimeMillis() / ONE_SECOND_IN_MILLIS + ONE_MINUTE_IN_SECONDS * ONE_MINUTE_IN_SECONDS
} }
POS_FOUR_HOURS -> { POS_FOUR_HOURS -> {
// four hours // four hours
clearAt = System.currentTimeMillis() / ONE_SECOND_IN_MILLIS +
System.currentTimeMillis() / ONE_SECOND_IN_MILLIS FOUR_HOURS * ONE_MINUTE_IN_SECONDS * ONE_MINUTE_IN_SECONDS
+FOUR_HOURS * ONE_MINUTE_IN_SECONDS * ONE_MINUTE_IN_SECONDS
} }
POS_TODAY -> { POS_TODAY -> {
// today // today
val date = Calendar.getInstance().apply { val date = getLastSecondOfToday()
set(Calendar.HOUR_OF_DAY, LAST_HOUR_OF_DAY) dateToSeconds(date)
set(Calendar.MINUTE, LAST_MINUTE_OF_HOUR)
set(Calendar.SECOND, LAST_SECOND_OF_MINUTE)
}
clearAt = date.timeInMillis / ONE_SECOND_IN_MILLIS
} }
POS_END_OF_WEEK -> { POS_END_OF_WEEK -> {
// end of week // end of week
val date = Calendar.getInstance().apply { val date = getLastSecondOfToday()
set(Calendar.HOUR_OF_DAY, LAST_HOUR_OF_DAY)
set(Calendar.MINUTE, LAST_MINUTE_OF_HOUR)
set(Calendar.SECOND, LAST_SECOND_OF_MINUTE)
}
while (date.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) { while (date.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
date.add(Calendar.DAY_OF_YEAR, 1) date.add(Calendar.DAY_OF_YEAR, 1)
} }
dateToSeconds(date)
clearAt = date.timeInMillis / ONE_SECOND_IN_MILLIS
} }
else -> clearAt
} }
} }
@Suppress("ReturnCount") private fun clearAtToUnixTime(clearAt: ClearAt?): Long = when {
private fun clearAtToUnixTime(clearAt: ClearAt?): Long { clearAt?.type == CLEAR_AT_TYPE_PERIOD -> {
if (clearAt != null) { System.currentTimeMillis() / ONE_SECOND_IN_MILLIS + clearAt.time.toLong()
if (clearAt.type.equals("period")) {
return System.currentTimeMillis() / ONE_SECOND_IN_MILLIS + clearAt.time.toLong()
} else if (clearAt.type.equals("end-of")) {
if (clearAt.time.equals("day")) {
val date = Calendar.getInstance().apply {
set(Calendar.HOUR_OF_DAY, LAST_HOUR_OF_DAY)
set(Calendar.MINUTE, LAST_MINUTE_OF_HOUR)
set(Calendar.SECOND, LAST_SECOND_OF_MINUTE)
}
return date.timeInMillis / ONE_SECOND_IN_MILLIS
}
}
} }
clearAt?.type == CLEAR_AT_TYPE_END_OF && clearAt.time == "day" -> {
return -1 val date = getLastSecondOfToday()
dateToSeconds(date)
}
else -> -1
} }
private fun openEmojiPopup() { private fun getLastSecondOfToday(): Calendar {
popup.show() val date = Calendar.getInstance().apply {
set(Calendar.HOUR_OF_DAY, LAST_HOUR_OF_DAY)
set(Calendar.MINUTE, LAST_MINUTE_OF_HOUR)
set(Calendar.SECOND, LAST_SECOND_OF_MINUTE)
}
return date
} }
private fun dateToSeconds(date: Calendar) = date.timeInMillis / ONE_SECOND_IN_MILLIS
private fun clearStatus() { private fun clearStatus() {
asyncRunner.postQuickTask( asyncRunner.postQuickTask(
ClearStatusTask(accountManager.currentOwnCloudAccount?.savedAccount, context), ClearStatusTask(accountManager.currentOwnCloudAccount?.savedAccount, context),
@ -425,28 +410,35 @@ class SetStatusDialogFragment :
binding.clearStatusAfterSpinner.visibility = View.VISIBLE binding.clearStatusAfterSpinner.visibility = View.VISIBLE
binding.clearStatusMessageTextView.text = getString(R.string.clear_status_message_after) binding.clearStatusMessageTextView.text = getString(R.string.clear_status_message_after)
if (predefinedStatus.clearAt == null) { val clearAt = predefinedStatus.clearAt
if (clearAt == null) {
binding.clearStatusAfterSpinner.setSelection(0) binding.clearStatusAfterSpinner.setSelection(0)
} else { } else {
val clearAt = predefinedStatus.clearAt!! when (clearAt.type) {
if (clearAt.type.equals("period")) { CLEAR_AT_TYPE_PERIOD -> updateClearAtViewsForPeriod(clearAt)
when (clearAt.time) { CLEAR_AT_TYPE_END_OF -> updateClearAtViewsForEndOf(clearAt)
"1800" -> binding.clearStatusAfterSpinner.setSelection(POS_HALF_AN_HOUR)
"3600" -> binding.clearStatusAfterSpinner.setSelection(POS_AN_HOUR)
"14400" -> binding.clearStatusAfterSpinner.setSelection(POS_FOUR_HOURS)
else -> binding.clearStatusAfterSpinner.setSelection(POS_DONT_CLEAR)
}
} else if (clearAt.type.equals("end-of")) {
when (clearAt.time) {
"day" -> binding.clearStatusAfterSpinner.setSelection(POS_TODAY)
"week" -> binding.clearStatusAfterSpinner.setSelection(POS_END_OF_WEEK)
else -> binding.clearStatusAfterSpinner.setSelection(POS_DONT_CLEAR)
}
} }
} }
setClearStatusAfterValue(binding.clearStatusAfterSpinner.selectedItemPosition) setClearStatusAfterValue(binding.clearStatusAfterSpinner.selectedItemPosition)
} }
private fun updateClearAtViewsForPeriod(clearAt: ClearAt) {
when (clearAt.time) {
"1800" -> binding.clearStatusAfterSpinner.setSelection(POS_HALF_AN_HOUR)
"3600" -> binding.clearStatusAfterSpinner.setSelection(POS_AN_HOUR)
"14400" -> binding.clearStatusAfterSpinner.setSelection(POS_FOUR_HOURS)
else -> binding.clearStatusAfterSpinner.setSelection(POS_DONT_CLEAR)
}
}
private fun updateClearAtViewsForEndOf(clearAt: ClearAt) {
when (clearAt.time) {
"day" -> binding.clearStatusAfterSpinner.setSelection(POS_TODAY)
"week" -> binding.clearStatusAfterSpinner.setSelection(POS_END_OF_WEEK)
else -> binding.clearStatusAfterSpinner.setSelection(POS_DONT_CLEAR)
}
}
@VisibleForTesting @VisibleForTesting
fun setPredefinedStatus(predefinedStatus: ArrayList<PredefinedStatus>) { fun setPredefinedStatus(predefinedStatus: ArrayList<PredefinedStatus>) {
adapter.list = predefinedStatus adapter.list = predefinedStatus

View file

@ -344,6 +344,7 @@ class SyncedFoldersActivity :
* @param mediaFolders the media folders * @param mediaFolders the media folders
* @return the merged list of SyncedFolderItems * @return the merged list of SyncedFolderItems
*/ */
@Suppress("NestedBlockDepth") // legacy code
private fun mergeFolderData( private fun mergeFolderData(
syncedFolders: List<SyncedFolder>, syncedFolders: List<SyncedFolder>,
mediaFolders: List<MediaFolder> mediaFolders: List<MediaFolder>

View file

@ -50,41 +50,42 @@ object UriUtils {
return displayName?.replace("/".toRegex(), "-") return displayName?.replace("/".toRegex(), "-")
} }
@Suppress("TooGenericExceptionCaught")
private fun getDisplayNameFromContentResolver(uri: Uri, context: Context): String? { private fun getDisplayNameFromContentResolver(uri: Uri, context: Context): String? {
val mimeType = context.contentResolver.getType(uri) ?: return null
val displayNameColumn: String = getDisplayNameColumnForMimeType(mimeType)
var displayName: String? = null var displayName: String? = null
val mimeType = context.contentResolver.getType(uri) try {
if (mimeType != null) { context.contentResolver.query(
val displayNameColumn: String = when { uri, arrayOf(displayNameColumn),
MimeTypeUtil.isImage(mimeType) -> { null,
MediaStore.Images.ImageColumns.DISPLAY_NAME null,
} null
MimeTypeUtil.isVideo(mimeType) -> { ).use { cursor ->
MediaStore.Video.VideoColumns.DISPLAY_NAME if (cursor != null) {
} cursor.moveToFirst()
MimeTypeUtil.isAudio(mimeType) -> { displayName = cursor.getString(cursor.getColumnIndexOrThrow(displayNameColumn))
MediaStore.Audio.AudioColumns.DISPLAY_NAME
}
else -> {
MediaStore.Files.FileColumns.DISPLAY_NAME
} }
} }
try { } catch (e: Exception) {
context.contentResolver.query( Log_OC.e(TAG, "Could not retrieve display name for $uri")
uri, arrayOf(displayNameColumn), // nothing else, displayName keeps null
null,
null,
null
).use { cursor ->
if (cursor != null) {
cursor.moveToFirst()
displayName = cursor.getString(cursor.getColumnIndexOrThrow(displayNameColumn))
}
}
} catch (e: Exception) {
Log_OC.e(TAG, "Could not retrieve display name for $uri")
// nothing else, displayName keeps null
}
} }
return displayName return displayName
} }
private fun getDisplayNameColumnForMimeType(mimeType: String?) = when {
MimeTypeUtil.isImage(mimeType) -> {
MediaStore.Images.ImageColumns.DISPLAY_NAME
}
MimeTypeUtil.isVideo(mimeType) -> {
MediaStore.Video.VideoColumns.DISPLAY_NAME
}
MimeTypeUtil.isAudio(mimeType) -> {
MediaStore.Audio.AudioColumns.DISPLAY_NAME
}
else -> {
MediaStore.Files.FileColumns.DISPLAY_NAME
}
}
} }

View file

@ -54,14 +54,7 @@ class HttpStreamFetcher internal constructor(
get.setRequestHeader(RemoteOperation.OCS_API_HEADER, RemoteOperation.OCS_API_HEADER_VALUE) get.setRequestHeader(RemoteOperation.OCS_API_HEADER, RemoteOperation.OCS_API_HEADER_VALUE)
val status = client.executeMethod(get) val status = client.executeMethod(get)
if (status == HttpStatus.SC_OK) { if (status == HttpStatus.SC_OK) {
val byteOutputStream = ByteArrayOutputStream() return getResponseAsInputStream(get)
get.responseBodyAsStream.use { input ->
byteOutputStream.use { output ->
input.copyTo(output)
}
}
return ByteArrayInputStream(byteOutputStream.toByteArray())
} else { } else {
client.exhaustResponse(get.responseBodyAsStream) client.exhaustResponse(get.responseBodyAsStream)
} }
@ -74,6 +67,17 @@ class HttpStreamFetcher internal constructor(
return null return null
} }
private fun getResponseAsInputStream(getMethod: GetMethod): ByteArrayInputStream {
val byteOutputStream = ByteArrayOutputStream()
getMethod.responseBodyAsStream.use { input ->
byteOutputStream.use { output ->
input.copyTo(output)
}
}
return ByteArrayInputStream(byteOutputStream.toByteArray())
}
override fun cleanup() { override fun cleanup() {
Log_OC.i(TAG, "Cleanup") Log_OC.i(TAG, "Cleanup")
} }

View file

@ -48,54 +48,43 @@ class ShareeListAdapterTest {
@Mock @Mock
private lateinit var themeAvatarUtils: ThemeAvatarUtils private lateinit var themeAvatarUtils: ThemeAvatarUtils
private val orderedShares = listOf(
OCShare("/1").apply {
shareType = ShareType.EMAIL
sharedDate = 1004
},
OCShare("/2").apply {
shareType = ShareType.PUBLIC_LINK
sharedDate = 1003
},
OCShare("/3").apply {
shareType = ShareType.PUBLIC_LINK
sharedDate = 1001
},
OCShare("/4").apply {
shareType = ShareType.EMAIL
sharedDate = 1000
},
OCShare("/5").apply {
shareType = ShareType.USER
sharedDate = 80
},
OCShare("/6").apply {
shareType = ShareType.CIRCLE
sharedDate = 20
}
)
@Test @Test
@Suppress("LongMethod")
fun testSorting() { fun testSorting() {
MockitoAnnotations.openMocks(this) MockitoAnnotations.openMocks(this)
val resources = Mockito.mock(Resources::class.java) val resources = Mockito.mock(Resources::class.java)
Mockito.`when`(context!!.resources).thenReturn(resources) Mockito.`when`(context!!.resources).thenReturn(resources)
Mockito.`when`(fileActivity!!.resources).thenReturn(resources) Mockito.`when`(fileActivity!!.resources).thenReturn(resources)
val expectedSortOrder: MutableList<OCShare?> = ArrayList()
expectedSortOrder.add(
OCShare("/1").apply {
shareType = ShareType.EMAIL
sharedDate = 1004
}
)
expectedSortOrder.add(
OCShare("/2").apply {
shareType = ShareType.PUBLIC_LINK
sharedDate = 1003
}
)
expectedSortOrder.add(
OCShare("/3").apply {
shareType = ShareType.PUBLIC_LINK
sharedDate = 1001
}
)
expectedSortOrder.add(
OCShare("/4").apply {
shareType = ShareType.EMAIL
sharedDate = 1000
}
)
expectedSortOrder.add(
OCShare("/5").apply {
shareType = ShareType.USER
sharedDate = 80
}
)
expectedSortOrder.add(
OCShare("/6").apply {
shareType = ShareType.CIRCLE
sharedDate = 20
}
)
val randomOrder: MutableList<OCShare?> = ArrayList(expectedSortOrder) val randomOrder = orderedShares.shuffled()
randomOrder.shuffle()
val user = AnonymousUser("nextcloud") val user = AnonymousUser("nextcloud")
val sut = ShareeListAdapter( val sut = ShareeListAdapter(
fileActivity, fileActivity,
randomOrder, randomOrder,
@ -108,20 +97,24 @@ class ShareeListAdapterTest {
sut.sortShares() sut.sortShares()
// compare // compare
assertSort(sut.shares)
}
private fun assertSort(shares: MutableList<OCShare>) {
var compare = true var compare = true
var i = 0 var i = 0
while (i < expectedSortOrder.size && compare) { while (i < orderedShares.size && compare) {
compare = expectedSortOrder[i] === sut.shares[i] compare = orderedShares[i] === shares[i]
i++ i++
} }
if (!compare) { if (!compare) {
println("Expected:") println("Expected:")
for (item in expectedSortOrder) { for (item in orderedShares) {
println(item!!.path + " " + item.shareType + " " + item.sharedDate) println(item.path + " " + item.shareType + " " + item.sharedDate)
} }
println() println()
println("Actual:") println("Actual:")
for (item in sut.shares) { for (item in shares) {
println(item.path + " " + item.shareType + " " + item.sharedDate) println(item.path + " " + item.shareType + " " + item.sharedDate)
} }
} }