From b60e3c588094cba7d9a37e9bd23bb7d30b755a83 Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Wed, 19 Feb 2025 12:08:12 -0700 Subject: [PATCH] musikr: improve id3v2 tag mgmt --- .../musikr/metadata/MetadataExtractor.kt | 5 +- musikr/src/main/jni/src/jbuilder.rs | 46 ++++++++----------- musikr/src/main/jni/src/taglib/id3v2.rs | 24 +++++----- 3 files changed, 34 insertions(+), 41 deletions(-) diff --git a/musikr/src/main/java/org/oxycblt/musikr/metadata/MetadataExtractor.kt b/musikr/src/main/java/org/oxycblt/musikr/metadata/MetadataExtractor.kt index a15e187e2..10dac5cea 100644 --- a/musikr/src/main/java/org/oxycblt/musikr/metadata/MetadataExtractor.kt +++ b/musikr/src/main/java/org/oxycblt/musikr/metadata/MetadataExtractor.kt @@ -38,6 +38,9 @@ private object MetadataExtractorImpl : MetadataExtractor { withContext(Dispatchers.IO) { val fis = FileInputStream(fd.fileDescriptor) val input = NativeInputStream(deviceFile, fis) - MetadataJNI.openFile(input).also { fis.close() } + MetadataJNI.openFile(input).also { + Log.d("MetadataExtractor", "Metadata: ${it?.id3v2}") + fis.close() + } } } diff --git a/musikr/src/main/jni/src/jbuilder.rs b/musikr/src/main/jni/src/jbuilder.rs index 5b0f18c70..92ed9fd24 100644 --- a/musikr/src/main/jni/src/jbuilder.rs +++ b/musikr/src/main/jni/src/jbuilder.rs @@ -59,41 +59,31 @@ impl<'local, 'file_ref> JMetadataBuilder<'local, 'file_ref> { let mut first_pic = None; let mut front_cover_pic = None; - if let Some(frames) = tag.frames() { - for mut frame in frames.to_vec() { - if let Some(text_frame) = frame.as_text_identification() { - if let Some(field_list) = text_frame.field_list() { - let values: Vec = field_list - .to_vec() - .into_iter() - .map(|s| s.to_string()) - .collect(); - self.id3v2.add_id_list(frame.id().to_string_lossy(), values); - } - } else if let Some(user_text_frame) = frame.as_user_text_identification() { - if let Some(values) = user_text_frame.values() { - let mut values = values.to_vec(); - if !values.is_empty() { - let description = values.remove(0); - for value in values { - self.id3v2.add_combined( - frame.id().to_string_lossy(), - description.clone(), - value.to_string(), - ); - } + for mut frame in tag.frames().to_vec() { + if let Some(user_text_frame) = frame.as_user_text_identification() { + let values = user_text_frame.values(); + let mut values = values.to_vec(); + if !values.is_empty() { + let description = values.remove(0).to_string(); + let remaining: Vec = values.into_iter().map(|s| s.to_string()).collect(); + self.id3v2.add_combined_list(frame.id().to_string_lossy(), description, remaining); } - } + } else if let Some(text_frame) = frame.as_text_identification() { + let field_list = text_frame.field_list(); + let values: Vec = field_list + .to_vec() + .into_iter() + .map(|s| s.to_string()) + .collect(); + self.id3v2.add_id_list(frame.id().to_string_lossy(), values); } else if let Some(picture_frame) = frame.as_attached_picture() { if first_pic.is_none() { - first_pic = picture_frame.picture().map(|p| p.to_vec()); + first_pic = Some(picture_frame.picture().to_vec()); } - // TODO: Check for front cover type when bindings are available if let (Some(PictureType::FrontCover), None) = (picture_frame.picture_type(), &front_cover_pic) { - front_cover_pic = picture_frame.picture().map(|p| p.to_vec()); - } + front_cover_pic = Some(picture_frame.picture().to_vec()); } } } diff --git a/musikr/src/main/jni/src/taglib/id3v2.rs b/musikr/src/main/jni/src/taglib/id3v2.rs index b7a880edb..55d5acdb5 100644 --- a/musikr/src/main/jni/src/taglib/id3v2.rs +++ b/musikr/src/main/jni/src/taglib/id3v2.rs @@ -16,10 +16,10 @@ impl<'file_ref> ID3v2Tag<'file_ref> { Self { this } } - pub fn frames(&self) -> Option> { + pub fn frames(&self) -> FrameList<'file_ref> { let frames = bridge::Tag_frameList(self.this.as_ref()); - let this = OwnedThis::new(frames); - this.map(|this| FrameList::new(this)) + let this = OwnedThis::new(frames).unwrap(); + FrameList::new(this) } } @@ -94,10 +94,10 @@ impl<'file_ref> TextIdentificationFrame<'file_ref> { Self { this } } - pub fn field_list(&self) -> Option> { + pub fn field_list(&self) -> OwnedStringList<'file_ref> { let field_list = bridge::TextIdentificationFrame_fieldList(self.this.as_ref()); - let this = OwnedThis::new(field_list); - this.map(|this| StringList::new(this)) + let this = OwnedThis::new(field_list).unwrap(); + StringList::new(this) } } @@ -110,10 +110,10 @@ impl<'file_ref> UserTextIdentificationFrame<'file_ref> { Self { this } } - pub fn values(&self) -> Option> { + pub fn values(&self) -> OwnedStringList<'file_ref> { let values = bridge::UserTextIdentificationFrame_fieldList(self.this.as_ref()); - let this = OwnedThis::new(values); - this.map(|this| StringList::new(this)) + let this = OwnedThis::new(values).unwrap(); + StringList::new(this) } } @@ -131,9 +131,9 @@ impl<'file_ref> AttachedPictureFrame<'file_ref> { PictureType::from_u32(picture_type) } - pub fn picture(&self) -> Option> { + pub fn picture(&self) -> OwnedByteVector<'file_ref> { let picture = bridge::AttachedPictureFrame_picture(self.this.as_ref()); - let this = OwnedThis::new(picture); - this.map(|this| ByteVector::new(this)) + let this = OwnedThis::new(picture).unwrap(); + ByteVector::new(this) } }