Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
Update 1.4.0 - Fixed #13, Fixed #11 and Closes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Pinheiro committed Oct 26, 2021
1 parent 6cf222b commit 0035b80
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 185 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## [1.4.0] - [10.26.2021]
### Features
- **[Added]** `original` parameter to `[getUri]`. Now you can get a `original` or `modified` path. - [#10](https://github.com/LucasPJS/on_audio_edit/issues/10)

### Fixes
- **[Fixed]** error when tring to read a `Flac` file. - [#11](https://github.com/LucasPJS/on_audio_edit/issues/11)
- **[Fixed]** error when tring to read a file without `artwork`. - [#12](https://github.com/LucasPJS/on_audio_edit/issues/12)

### Documentation
- Updated `README` documentation.

## [1.3.0] - [10.23.2021]
### Features
- **[Added]** `[getUri]` used to retrive the user selected folder path. - [#10](https://github.com/LucasPJS/on_audio_edit/issues/10)
Expand Down
4 changes: 2 additions & 2 deletions DEPRECATED.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
## [1.2.0] - [10.20.2021] -> [X.X.X] - [XX.XX.XXXX]
## [1.2.0] - [10.20.2021] -> [1.4.0] - [26.10.2021]
### Deprecated
- `[readAllAudio]`.
- Use `[readAudio]` instead.
- `[getImagePath]`.
- Use `[getImage]` instead.
- `[id]` from `[AudioModel]`.

## [1.1.0] - [10.20.2021] -> [X.X.X] - [XX.XX.XXXX]
## [1.1.0] - [10.20.2021] -> [1.4.0] - [26.10.2021]
### Deprecated
- `[AudiosTagModel]`.
- Use `[AudioModel]` instead.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ NOTE: Feel free to help with readme translations
Add the following code to your `pubspec.yaml`:
```yaml
dependencies:
on_audio_edit: ^1.3.0
on_audio_edit: ^1.4.0
```
#### Request Permission:
Expand Down
2 changes: 1 addition & 1 deletion README.pt-BR.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ NOTE: Fique à vontade para ajudar nas traduções
Adicione o seguinte codigo para seu `pubspec.yaml`:
```yaml
dependencies:
on_audio_edit: ^1.3.0
on_audio_edit: ^1.4.0
```
#### Solicitar Permissões:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.lucasjosino.on_audio_edit.extensions

import android.util.Log
import org.jaudiotagger.tag.FieldKey

fun String.tryInt(key: Int): Any? {
return when (key) {
1,
Expand Down Expand Up @@ -30,3 +33,11 @@ fun String.tryInt(key: Int): Any? {
else -> this
}
}

fun ArrayList<FieldKey>.checkFlac(data: String): ArrayList<FieldKey> {
val tmpArray: ArrayList<FieldKey> = this
if (data.endsWith(".flac")) {
tmpArray.removeAt(19)
}
return tmpArray
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,23 @@ class OnArtworkEdit10(private val context: Context, private val activity: Activi
// Main parameters
private val channelError = "on_audio_error"
private val onSharedPrefKeyUriCode = "on_audio_edit_uri"
private var searchInsideFolders: Boolean = false
private lateinit var uri: Uri
private lateinit var call: MethodCall

// Check if plugin already has uri.
private fun getUri() : String? = activity.getSharedPreferences("on_audio_edit",
Context.MODE_PRIVATE).getString(onSharedPrefKeyUriCode, "")
private fun getUri(): String? = activity.getSharedPreferences(
"on_audio_edit",
Context.MODE_PRIVATE
).getString(onSharedPrefKeyUriCode, "")

//
fun editArtwork(result: MethodChannel.Result, call: MethodCall, uri: Uri?) {
// This will write in file removing all unnecessary info.
TagOptionSingleton.getInstance().isId3v2PaddingWillShorten = true

//
this.searchInsideFolders = call.argument<Boolean>("searchInsideFolders")!!
this.uri = uri ?: Uri.parse(call.argument<String>("imagePath")!!)
this.call = call

Expand All @@ -56,7 +62,7 @@ class OnArtworkEdit10(private val context: Context, private val activity: Activi
}

@Suppress("BlockingMethodInNonBlockingContext")
private suspend fun doEverythingInBackground() : Boolean = withContext(Dispatchers.IO) {
private suspend fun doEverythingInBackground(): Boolean = withContext(Dispatchers.IO) {
// Get all information from Dart.
val data = call.argument<String>("data")!!
val type = checkArtworkFormat(call.argument<Int>("type")!!)
Expand All @@ -66,16 +72,35 @@ class OnArtworkEdit10(private val context: Context, private val activity: Activi
//
val internalData = File(data)

// Get and check if uri is null.
val uriFolder = Uri.parse(getUri()) ?: return@withContext false
// At this point, we already requested the 'special' path to user folder.
// If not, return false and a warn.
if (getUri() == null) {
Log.w("on_audio_exception", "Uri to folder path doesn't exist!")
return@withContext false
}

// Get the 'extra' uri.
val uriFolder: Uri = Uri.parse(getUri())

// Use DocumentFile to navigate and find specific data inside specific folder.
// We need this, cuz google blocked some access in Android >= 10/Q
var pUri: Uri = Uri.parse("") // This can be null but, we use inside try / catch
val dFile = DocumentFile.fromTreeUri(context, uriFolder)
// [findFile] will give a slow performance, so, we use Kotlin Coroutines and "doEverythingInBackground"
val fileList = dFile!!.findFile(internalData.name)
if (fileList != null) pUri = fileList.uri
val pUri: Uri
val dFile = DocumentFile.fromTreeUri(context, uriFolder) ?: return@withContext false
// [getFile] will give a slow performance, so, we use Kotlin Coroutines and "doEverythingInBackground"
val file = getFile(dFile, internalData)?.uri

//
if (file == null) {
Log.w(
"on_audio_exception",
"File: $data not found!\n " +
"Call [resetComplexPermission] and let the user choose the \"Root\" folder."
)
return@withContext false
}

// After checking that [file] is valid, keep the editing.
pUri = file

// Temp file just to write(rewrite) file path. Produce the same result as "scan"
val temp = File.createTempFile("tmp-media", '.' + Utils.getExtension(internalData))
Expand All @@ -97,15 +122,17 @@ class OnArtworkEdit10(private val context: Context, private val activity: Activi
artwork.pictureType = PictureTypes.DEFAULT_ID
artwork.mimeType = type
artwork.description = description
artwork.height = size ; artwork.width = size
artwork.height = size; artwork.width = size

//
audioTag.setField(artwork)
audioFile.file = temp

try {
AudioFileIO.write(audioFile)
} catch (e: Exception) { Log.i(channelError, e.toString()) }
} catch (e: Exception) {
Log.i(channelError, "$e")
}

// Start setup to write in folder
// Until this moment we only write inside audio file, but we need tell android that this file has some change.
Expand All @@ -129,17 +156,11 @@ class OnArtworkEdit10(private val context: Context, private val activity: Activi
}
}
} catch (e: Exception) {
Log.i("on_audio_exception", e.toString())
temp.delete()
return@withContext false
Log.i("on_audio_exception", "$e")
} catch (f: FileNotFoundException) {
Log.i("on_audio_FileNotFound", f.toString())
temp.delete()
return@withContext false
Log.i("on_audio_FileNotFound", "$f")
} catch (io: IOException) {
Log.i("on_audio_IOException", io.toString())
temp.delete()
return@withContext false
Log.i("on_audio_IOException", "$io")
}

// Delete temp folder.
Expand All @@ -149,7 +170,7 @@ class OnArtworkEdit10(private val context: Context, private val activity: Activi

@Suppress("DEPRECATION")
//
private fun findImage(uri: Uri) : String {
private fun findImage(uri: Uri): String {
val projection = arrayOf(MediaStore.Images.Media.DATA)
val cursor = context.contentResolver.query(uri, projection, null, null, null)

Expand All @@ -162,4 +183,18 @@ class OnArtworkEdit10(private val context: Context, private val activity: Activi
cursor?.close()
return imageData
}

private fun getFile(directory: DocumentFile, specificFile: File): DocumentFile? {
val files = directory.listFiles()
for (file in files) {
val data: DocumentFile? = if (file.isDirectory) {
// If [searchInsideFolders] is true, we keep searching, if not, no file was found.
if (searchInsideFolders) getFile(file, specificFile) else return null
} else {
if (file.name == specificFile.name) file else null
}
if (data != null) return data
}
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ class OnAudioEdit10(private val context: Context, private val activity: Activity
// Setting tags
for (info in getTagsAndInfo) {
// If value is null, ignore.
val value = info.value.toString()
val value = "${info.value}"
if (value.isNotEmpty()) audioTag.setField(info.key, value)
}
audioFile.file = temp

try {
AudioFileIO.write(audioFile)
} catch (e: Exception) {
Log.i(channelError, e.toString())
Log.i(channelError, "$e")
}

// Start setup to write in folder
Expand All @@ -140,17 +140,11 @@ class OnAudioEdit10(private val context: Context, private val activity: Activity
}
}
} catch (e: Exception) {
Log.i("on_audio_exception", e.toString())
temp.delete()
return@withContext false
Log.i("on_audio_exception", "$e")
} catch (f: FileNotFoundException) {
Log.i("on_audio_FileNotFound", f.toString())
temp.delete()
return@withContext false
Log.i("on_audio_FileNotFound", "$f")
} catch (io: IOException) {
Log.i("on_audio_IOException", io.toString())
temp.delete()
return@withContext false
Log.i("on_audio_IOException", "$io")
}

// Delete temp folder.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lucasjosino.on_audio_edit.methods.read

import com.lucasjosino.on_audio_edit.extensions.checkFlac
import com.lucasjosino.on_audio_edit.extensions.tryInt
import com.lucasjosino.on_audio_edit.types.checkTag
import com.lucasjosino.on_audio_edit.utils.checkAndGetExtraInfo
Expand All @@ -25,7 +26,7 @@ class OnAudioRead {

// Getting all tags
val tagsData: MutableMap<String, Any?> = HashMap()
for (tag in getAllProjection()) {
for (tag in getAllProjection().checkFlac(data)) {
val value = audioTag.getValue(tag, 0)
if (!value.isNullOrEmpty()) {
tagsData[tag.name] = value.tryInt(tag.ordinal)
Expand Down Expand Up @@ -56,7 +57,7 @@ class OnAudioRead {

// Getting all tags
val tagsData: MutableMap<String, Any?> = HashMap()
for (tag in getAllProjection()) {
for (tag in getAllProjection().checkFlac(pathData)) {
val value = audioTag.getValue(tag, 0)
if (!value.isNullOrEmpty()) {
tagsData[tag.name] = value.tryInt(tag.ordinal)
Expand Down Expand Up @@ -133,4 +134,6 @@ class OnAudioRead {
// Sending to Dart
result.success(tagsData)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fun checkAndGetExtraInfo(audioFile: AudioFile) : MutableMap<String, Any?> {
extraInfo["CHANNELS"] = audioFile.audioHeader.channels
extraInfo["TYPE"] = audioFile.audioHeader.encodingType
extraInfo["LENGTH"] = audioFile.file.length()
extraInfo["FIRST_ARTWORK"] = audioFile.tag.firstArtwork.binaryData
extraInfo["FIRST_ARTWORK"] = audioFile.tag?.firstArtwork?.binaryData
return extraInfo
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.lucasjosino.on_audio_edit.utils

import org.jaudiotagger.tag.FieldKey

fun getAllProjection() : Array<FieldKey> = allProjection
fun getAllProjection() : ArrayList<FieldKey> = allProjection

private var allProjection = arrayOf(
private var allProjection = arrayListOf(
FieldKey.ACOUSTID_FINGERPRINT, // 0
FieldKey.ACOUSTID_ID, // 1
FieldKey.ALBUM, // 2
Expand Down
8 changes: 3 additions & 5 deletions lib/details/models/audio_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ class AudioModel {
/// Return song [grouping]
String? get grouping => _info["GROUPING"];

/// Deprecated after [1.2.0]
@Deprecated("This method will be removed soon")
int? get id => _info["ID"];

/// Return song [isrc]
String? get isrc => _info["ISRC"];

Expand Down Expand Up @@ -272,7 +268,9 @@ class AudioModel {
@override
String toString() {
var tmpInfo = _info;
tmpInfo.update("FIRST_ARTWORK", (value) => "${value.length} (Bytes)");
if (tmpInfo["FIRST_ARTWORK"] != null) {
tmpInfo.update("FIRST_ARTWORK", (value) => "${value.length} (Bytes)");
}
return tmpInfo.toString();
}
}
Loading

0 comments on commit 0035b80

Please sign in to comment.