mirror of
https://github.com/ankidroid/Anki-Android.git
synced 2024-09-20 12:02:16 +02:00
refactor: generify handleMediaSelection
To help with adding videos in a future commit Issue 10315
This commit is contained in:
parent
27fe37ee21
commit
4a53e869fa
@ -91,18 +91,18 @@ class BasicAudioClipFieldController : FieldControllerBase(), IFieldController {
|
|||||||
@KotlinCleanup("make data non-null")
|
@KotlinCleanup("make data non-null")
|
||||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
if (resultCode != Activity.RESULT_CANCELED && requestCode == ACTIVITY_SELECT_AUDIO_CLIP) {
|
if (resultCode != Activity.RESULT_CANCELED && requestCode == ACTIVITY_SELECT_AUDIO_CLIP) {
|
||||||
executeSafe(mActivity, "handleAudioSelection:unhandled") {
|
executeSafe(mActivity, "handleMediaSelection:unhandled") {
|
||||||
handleAudioSelection(data!!)
|
handleMediaSelection(data!!, "ankidroid_audioclip_")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleAudioSelection(data: Intent) {
|
private fun handleMediaSelection(data: Intent, clipNamePrefix: String) {
|
||||||
val selectedClip = data.data
|
val selectedClip = data.data
|
||||||
|
|
||||||
// Get information about the selected document
|
// Get information about the selected document
|
||||||
val queryColumns = arrayOf(MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.SIZE, MediaStore.MediaColumns.MIME_TYPE)
|
val queryColumns = arrayOf(MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.SIZE, MediaStore.MediaColumns.MIME_TYPE)
|
||||||
var audioClipFullNameParts: Array<String>
|
var mediaClipFullNameParts: Array<String>
|
||||||
mActivity.contentResolver.query(selectedClip!!, queryColumns, null, null, null).use { cursor ->
|
mActivity.contentResolver.query(selectedClip!!, queryColumns, null, null, null).use { cursor ->
|
||||||
if (cursor == null) {
|
if (cursor == null) {
|
||||||
showThemedToast(
|
showThemedToast(
|
||||||
@ -112,19 +112,19 @@ class BasicAudioClipFieldController : FieldControllerBase(), IFieldController {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
cursor.moveToFirst()
|
cursor.moveToFirst()
|
||||||
var audioClipFullName = cursor.getString(0)
|
var mediaClipFullName = cursor.getString(0)
|
||||||
audioClipFullName = checkFileName(audioClipFullName)
|
mediaClipFullName = checkFileName(mediaClipFullName)
|
||||||
audioClipFullNameParts = audioClipFullName.split("\\.").toTypedArray()
|
mediaClipFullNameParts = mediaClipFullName.split("\\.").toTypedArray()
|
||||||
if (audioClipFullNameParts.size < 2) {
|
if (mediaClipFullNameParts.size < 2) {
|
||||||
audioClipFullNameParts = try {
|
mediaClipFullNameParts = try {
|
||||||
Timber.i("Audio clip name does not have extension, using second half of mime type")
|
Timber.i("Media clip name does not have extension, using second half of mime type")
|
||||||
arrayOf(audioClipFullName, cursor.getString(2).split("/").toTypedArray()[1])
|
arrayOf(mediaClipFullName, cursor.getString(2).split("/").toTypedArray()[1])
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.w(e)
|
Timber.w(e)
|
||||||
// This code is difficult to stabilize - it is not clear how to handle files with no extension
|
// This code is difficult to stabilize - it is not clear how to handle files with no extension
|
||||||
// and apparently we may fail to get MIME_TYPE information - in that case we will gather information
|
// and apparently we may fail to get MIME_TYPE information - in that case we will gather information
|
||||||
// about what people are experiencing in the real world and decide later, but without crashing at least
|
// about what people are experiencing in the real world and decide later, but without crashing at least
|
||||||
AnkiDroidApp.sendExceptionReport(e, "Audio Clip addition failed. Name " + audioClipFullName + " / cursor mime type column type " + cursor.getType(2))
|
AnkiDroidApp.sendExceptionReport(e, "Media Clip addition failed. Name " + mediaClipFullName + " / cursor mime type column type " + cursor.getType(2))
|
||||||
showThemedToast(
|
showThemedToast(
|
||||||
AnkiDroidApp.getInstance().applicationContext,
|
AnkiDroidApp.getInstance().applicationContext,
|
||||||
AnkiDroidApp.getInstance().getString(R.string.multimedia_editor_something_wrong), true
|
AnkiDroidApp.getInstance().getString(R.string.multimedia_editor_something_wrong), true
|
||||||
@ -138,14 +138,14 @@ class BasicAudioClipFieldController : FieldControllerBase(), IFieldController {
|
|||||||
val clipCopy: File
|
val clipCopy: File
|
||||||
try {
|
try {
|
||||||
clipCopy = File.createTempFile(
|
clipCopy = File.createTempFile(
|
||||||
"ankidroid_audioclip_" + audioClipFullNameParts[0],
|
clipNamePrefix + mediaClipFullNameParts[0],
|
||||||
"." + audioClipFullNameParts[1],
|
"." + mediaClipFullNameParts[1],
|
||||||
storingDirectory
|
storingDirectory
|
||||||
)
|
)
|
||||||
Timber.d("audio clip picker file path is: %s", clipCopy.absolutePath)
|
Timber.d("media clip picker file path is: %s", clipCopy.absolutePath)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.e(e, "Could not create temporary audio file. ")
|
Timber.e(e, "Could not create temporary media file. ")
|
||||||
AnkiDroidApp.sendExceptionReport(e, "handleAudioSelection:tempFile")
|
AnkiDroidApp.sendExceptionReport(e, "handleMediaSelection:tempFile")
|
||||||
showThemedToast(
|
showThemedToast(
|
||||||
AnkiDroidApp.getInstance().applicationContext,
|
AnkiDroidApp.getInstance().applicationContext,
|
||||||
AnkiDroidApp.getInstance().getString(R.string.multimedia_editor_something_wrong), true
|
AnkiDroidApp.getInstance().getString(R.string.multimedia_editor_something_wrong), true
|
||||||
@ -165,8 +165,8 @@ class BasicAudioClipFieldController : FieldControllerBase(), IFieldController {
|
|||||||
tvAudioClip!!.visibility = View.VISIBLE
|
tvAudioClip!!.visibility = View.VISIBLE
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.e(e, "Unable to copy audio file from ContentProvider")
|
Timber.e(e, "Unable to copy media file from ContentProvider")
|
||||||
AnkiDroidApp.sendExceptionReport(e, "handleAudioSelection:copyFromProvider")
|
AnkiDroidApp.sendExceptionReport(e, "handleMediaSelection:copyFromProvider")
|
||||||
showThemedToast(
|
showThemedToast(
|
||||||
AnkiDroidApp.getInstance().applicationContext,
|
AnkiDroidApp.getInstance().applicationContext,
|
||||||
AnkiDroidApp.getInstance().getString(R.string.multimedia_editor_something_wrong), true
|
AnkiDroidApp.getInstance().getString(R.string.multimedia_editor_something_wrong), true
|
||||||
@ -188,17 +188,18 @@ class BasicAudioClipFieldController : FieldControllerBase(), IFieldController {
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val ACTIVITY_SELECT_AUDIO_CLIP = 1
|
private const val ACTIVITY_SELECT_AUDIO_CLIP = 1
|
||||||
|
private const val ACTIVITY_SELECT_VIDEO_CLIP = 2
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method replaces any character that isn't a number, letter or underscore with underscore in file name.
|
* This method replaces any character that isn't a number, letter or underscore with underscore in file name.
|
||||||
* This method doesn't check that file name is valid or not it simply operates on all file name.
|
* This method doesn't check that file name is valid or not it simply operates on all file name.
|
||||||
* @param audioClipFullName name of the file.
|
* @param mediaClipFullName name of the file.
|
||||||
* @return file name which is valid.
|
* @return file name which is valid.
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
fun checkFileName(audioClipFullName: String): String {
|
fun checkFileName(mediaClipFullName: String): String {
|
||||||
return audioClipFullName.replace("[^\\w.]+".toRegex(), "_")
|
return mediaClipFullName.replace("[^\\w.]+".toRegex(), "_")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user