Refactor duplicated code.

This commit is contained in:
onurays 2020-03-24 11:03:41 +03:00
parent f7fd23b153
commit 2651f82337
10 changed files with 32 additions and 160 deletions

1
.gitignore vendored
View File

@ -14,4 +14,3 @@
/tmp
ktlint

View File

@ -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
}
}

View File

@ -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)
}
}

View File

@ -42,27 +42,7 @@ class AudioPicker(override val requestCode: Int) : Picker<MultiPickerAudioType>(
val audioList = mutableListOf<MultiPickerAudioType>()
val selectedUriList = mutableListOf<Uri>()
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<Uri>)
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

View File

@ -41,27 +41,7 @@ class FilePicker(override val requestCode: Int) : Picker<MultiPickerFileType>(re
val fileList = mutableListOf<MultiPickerFileType>()
val selectedUriList = mutableListOf<Uri>()
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<Uri>)
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)

View File

@ -45,27 +45,7 @@ class ImagePicker(override val requestCode: Int) : Picker<MultiPickerImageType>(
val imageList = mutableListOf<MultiPickerImageType>()
val selectedUriList = mutableListOf<Uri>()
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<Uri>)
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

View File

@ -44,4 +44,27 @@ abstract class Picker<T>(open val requestCode: Int) {
single = true
return this
}
protected fun getSelectedUriList(data: Intent?): List<Uri> {
val selectedUriList = mutableListOf<Uri>()
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<Uri>)
else -> selectedUriList.add(it as Uri)
}
}
}
return selectedUriList
}
}

View File

@ -42,27 +42,7 @@ class VideoPicker(override val requestCode: Int) : Picker<MultiPickerVideoType>(
val videoList = mutableListOf<MultiPickerVideoType>()
val selectedUriList = mutableListOf<Uri>()
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<Uri>)
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

View File

@ -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)
}
}

View File

@ -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(