From 2651f82337a8110d6f241a0eee0f377299ff456b Mon Sep 17 00:00:00 2001 From: onurays Date: Tue, 24 Mar 2020 11:03:41 +0300 Subject: [PATCH] Refactor duplicated code. --- .gitignore | 1 - .../session/content/ThumbnailExtractor.kt | 8 ++-- .../multipicker/ExampleInstrumentedTest.kt | 39 ------------------- .../vector/riotx/multipicker/AudioPicker.kt | 22 +---------- .../im/vector/riotx/multipicker/FilePicker.kt | 22 +---------- .../vector/riotx/multipicker/ImagePicker.kt | 22 +---------- .../im/vector/riotx/multipicker/Picker.kt | 23 +++++++++++ .../vector/riotx/multipicker/VideoPicker.kt | 22 +---------- .../riotx/multipicker/ExampleUnitTest.kt | 32 --------------- .../preview/AttachmentsPreviewFragment.kt | 1 - 10 files changed, 32 insertions(+), 160 deletions(-) delete mode 100644 multipicker/src/androidTest/java/im/vector/riotx/multipicker/ExampleInstrumentedTest.kt delete mode 100644 multipicker/src/test/java/im/vector/riotx/multipicker/ExampleUnitTest.kt diff --git a/.gitignore b/.gitignore index ab97ec340a..4a264a28d8 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,3 @@ /tmp ktlint - diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/ThumbnailExtractor.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/ThumbnailExtractor.kt index 2ce249ab80..ad23ff8d78 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/ThumbnailExtractor.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/ThumbnailExtractor.kt @@ -44,6 +44,7 @@ internal object ThumbnailExtractor { } private fun extractVideoThumbnail(context: Context, attachment: ContentAttachmentData): ThumbnailData? { + var thumbnailData: ThumbnailData? = null val mediaMetadataRetriever = MediaMetadataRetriever() try { mediaMetadataRetriever.setDataSource(context, attachment.queryUri) @@ -64,7 +65,7 @@ internal object ThumbnailExtractor { val thumbnailWidth = thumbnail.width val thumbnailHeight = thumbnail.height val thumbnailSize = outputStream.size() - val thumbnailData = ThumbnailData( + thumbnailData = ThumbnailData( width = thumbnailWidth, height = thumbnailHeight, size = thumbnailSize.toLong(), @@ -73,10 +74,11 @@ internal object ThumbnailExtractor { ) thumbnail.recycle() outputStream.reset() - return thumbnailData } catch (e: Exception) { Timber.e(e, "Cannot extract video thumbnail") - return null + } finally { + mediaMetadataRetriever.release() } + return thumbnailData } } diff --git a/multipicker/src/androidTest/java/im/vector/riotx/multipicker/ExampleInstrumentedTest.kt b/multipicker/src/androidTest/java/im/vector/riotx/multipicker/ExampleInstrumentedTest.kt deleted file mode 100644 index 25bf17559f..0000000000 --- a/multipicker/src/androidTest/java/im/vector/riotx/multipicker/ExampleInstrumentedTest.kt +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2020 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package im.vector.riotx.multipicker - -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.ext.junit.runners.AndroidJUnit4 -import junit.framework.Assert.assertEquals - -import org.junit.Test -import org.junit.runner.RunWith - -/** - * Instrumented test, which will execute on an Android device. - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getInstrumentation().targetContext - assertEquals("im.vector.riotx.multipicker.test", appContext.packageName) - } -} diff --git a/multipicker/src/main/java/im/vector/riotx/multipicker/AudioPicker.kt b/multipicker/src/main/java/im/vector/riotx/multipicker/AudioPicker.kt index 23873aae1c..0f45f90d2b 100644 --- a/multipicker/src/main/java/im/vector/riotx/multipicker/AudioPicker.kt +++ b/multipicker/src/main/java/im/vector/riotx/multipicker/AudioPicker.kt @@ -42,27 +42,7 @@ class AudioPicker(override val requestCode: Int) : Picker( val audioList = mutableListOf() - val selectedUriList = mutableListOf() - val dataUri = data?.data - val clipData = data?.clipData - - if (clipData != null) { - for (i in 0 until clipData.itemCount) { - selectedUriList.add(clipData.getItemAt(i).uri) - } - } else if (dataUri != null) { - selectedUriList.add(dataUri) - } else { - data?.extras?.get(Intent.EXTRA_STREAM)?.let { - @Suppress("UNCHECKED_CAST") - when (it) { - is List<*> -> selectedUriList.addAll(it as List) - else -> selectedUriList.add(it as Uri) - } - } - } - - selectedUriList.forEach { selectedUri -> + getSelectedUriList(data).forEach { selectedUri -> val projection = arrayOf( MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.SIZE diff --git a/multipicker/src/main/java/im/vector/riotx/multipicker/FilePicker.kt b/multipicker/src/main/java/im/vector/riotx/multipicker/FilePicker.kt index 0e1169755e..41b20341d0 100644 --- a/multipicker/src/main/java/im/vector/riotx/multipicker/FilePicker.kt +++ b/multipicker/src/main/java/im/vector/riotx/multipicker/FilePicker.kt @@ -41,27 +41,7 @@ class FilePicker(override val requestCode: Int) : Picker(re val fileList = mutableListOf() - val selectedUriList = mutableListOf() - val dataUri = data?.data - val clipData = data?.clipData - - if (clipData != null) { - for (i in 0 until clipData.itemCount) { - selectedUriList.add(clipData.getItemAt(i).uri) - } - } else if (dataUri != null) { - selectedUriList.add(dataUri) - } else { - data?.extras?.get(Intent.EXTRA_STREAM)?.let { - @Suppress("UNCHECKED_CAST") - when (it) { - is List<*> -> selectedUriList.addAll(it as List) - else -> selectedUriList.add(it as Uri) - } - } - } - - selectedUriList.forEach { selectedUri -> + getSelectedUriList(data).forEach { selectedUri -> context.contentResolver.query(selectedUri, null, null, null, null) ?.use { cursor -> val nameColumn = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) diff --git a/multipicker/src/main/java/im/vector/riotx/multipicker/ImagePicker.kt b/multipicker/src/main/java/im/vector/riotx/multipicker/ImagePicker.kt index 8bf589800d..bd27244ed5 100644 --- a/multipicker/src/main/java/im/vector/riotx/multipicker/ImagePicker.kt +++ b/multipicker/src/main/java/im/vector/riotx/multipicker/ImagePicker.kt @@ -45,27 +45,7 @@ class ImagePicker(override val requestCode: Int) : Picker( val imageList = mutableListOf() - val selectedUriList = mutableListOf() - val dataUri = data?.data - val clipData = data?.clipData - - if (clipData != null) { - for (i in 0 until clipData.itemCount) { - selectedUriList.add(clipData.getItemAt(i).uri) - } - } else if (dataUri != null) { - selectedUriList.add(dataUri) - } else { - data?.extras?.get(Intent.EXTRA_STREAM)?.let { - @Suppress("UNCHECKED_CAST") - when (it) { - is List<*> -> selectedUriList.addAll(it as List) - else -> selectedUriList.add(it as Uri) - } - } - } - - selectedUriList.forEach { selectedUri -> + getSelectedUriList(data).forEach { selectedUri -> val projection = arrayOf( MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.SIZE diff --git a/multipicker/src/main/java/im/vector/riotx/multipicker/Picker.kt b/multipicker/src/main/java/im/vector/riotx/multipicker/Picker.kt index f162dd7608..58754cd74e 100644 --- a/multipicker/src/main/java/im/vector/riotx/multipicker/Picker.kt +++ b/multipicker/src/main/java/im/vector/riotx/multipicker/Picker.kt @@ -44,4 +44,27 @@ abstract class Picker(open val requestCode: Int) { single = true return this } + + protected fun getSelectedUriList(data: Intent?): List { + val selectedUriList = mutableListOf() + val dataUri = data?.data + val clipData = data?.clipData + + if (clipData != null) { + for (i in 0 until clipData.itemCount) { + selectedUriList.add(clipData.getItemAt(i).uri) + } + } else if (dataUri != null) { + selectedUriList.add(dataUri) + } else { + data?.extras?.get(Intent.EXTRA_STREAM)?.let { + @Suppress("UNCHECKED_CAST") + when (it) { + is List<*> -> selectedUriList.addAll(it as List) + else -> selectedUriList.add(it as Uri) + } + } + } + return selectedUriList + } } diff --git a/multipicker/src/main/java/im/vector/riotx/multipicker/VideoPicker.kt b/multipicker/src/main/java/im/vector/riotx/multipicker/VideoPicker.kt index d4b8d6a985..739c24c6c7 100644 --- a/multipicker/src/main/java/im/vector/riotx/multipicker/VideoPicker.kt +++ b/multipicker/src/main/java/im/vector/riotx/multipicker/VideoPicker.kt @@ -42,27 +42,7 @@ class VideoPicker(override val requestCode: Int) : Picker( val videoList = mutableListOf() - val selectedUriList = mutableListOf() - val dataUri = data?.data - val clipData = data?.clipData - - if (clipData != null) { - for (i in 0 until clipData.itemCount) { - selectedUriList.add(clipData.getItemAt(i).uri) - } - } else if (dataUri != null) { - selectedUriList.add(dataUri) - } else { - data?.extras?.get(Intent.EXTRA_STREAM)?.let { - @Suppress("UNCHECKED_CAST") - when (it) { - is List<*> -> selectedUriList.addAll(it as List) - else -> selectedUriList.add(it as Uri) - } - } - } - - selectedUriList.forEach { selectedUri -> + getSelectedUriList(data).forEach { selectedUri -> val projection = arrayOf( MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.SIZE diff --git a/multipicker/src/test/java/im/vector/riotx/multipicker/ExampleUnitTest.kt b/multipicker/src/test/java/im/vector/riotx/multipicker/ExampleUnitTest.kt deleted file mode 100644 index 07e464699f..0000000000 --- a/multipicker/src/test/java/im/vector/riotx/multipicker/ExampleUnitTest.kt +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2020 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package im.vector.riotx.multipicker - -import junit.framework.Assert.assertEquals -import org.junit.Test - -/** - * Example local unit test, which will execute on the development machine (host). - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -class ExampleUnitTest { - @Test - fun addition_isCorrect() { - assertEquals(4, 2 + 2) - } -} diff --git a/vector/src/main/java/im/vector/riotx/features/attachments/preview/AttachmentsPreviewFragment.kt b/vector/src/main/java/im/vector/riotx/features/attachments/preview/AttachmentsPreviewFragment.kt index f059da7d85..3b1972ffbc 100644 --- a/vector/src/main/java/im/vector/riotx/features/attachments/preview/AttachmentsPreviewFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/attachments/preview/AttachmentsPreviewFragment.kt @@ -202,7 +202,6 @@ class AttachmentsPreviewFragment @Inject constructor( private fun doHandleEditAction() = withState(viewModel) { val currentAttachment = it.attachments.getOrNull(it.currentAttachmentIndex) ?: return@withState val destinationFile = File(requireContext().cacheDir, "${currentAttachment.name}_edited_image_${System.currentTimeMillis()}") - // Note: using currentAttachment.queryUri.toUri() make the app crash when sharing from Google Photos val uri = currentAttachment.queryUri UCrop.of(uri, destinationFile.toUri()) .withOptions(