From 8cb290ab19663d9fafdd74451ed70695f337284a Mon Sep 17 00:00:00 2001 From: sebastian Date: Fri, 1 Nov 2024 09:19:57 +0100 Subject: [PATCH] Sort NoteSheets by Date --- .../java/core/notevault/MainActivity.java | 1 + .../core/notevault/ui/home/HomeFragment.java | 3 ++ .../core/notevault/util/NoteSheetsUtil.java | 54 +++++++++++++++++++ .../core/notevault/util/UriTimestamp.java | 21 ++++++++ 4 files changed, 79 insertions(+) create mode 100644 app/src/main/java/core/notevault/util/NoteSheetsUtil.java create mode 100644 app/src/main/java/core/notevault/util/UriTimestamp.java diff --git a/app/src/main/java/core/notevault/MainActivity.java b/app/src/main/java/core/notevault/MainActivity.java index f347e7b..ebf6563 100644 --- a/app/src/main/java/core/notevault/MainActivity.java +++ b/app/src/main/java/core/notevault/MainActivity.java @@ -19,6 +19,7 @@ import core.notevault.data.MusicNote; import core.notevault.data.NoteSheet; import core.notevault.databinding.ActivityMainBinding; import core.notevault.ui.metadatadialog.MetaDataDialog; +import core.notevault.util.NoteSheetsUtil; import java.io.*; import java.nio.file.Files; diff --git a/app/src/main/java/core/notevault/ui/home/HomeFragment.java b/app/src/main/java/core/notevault/ui/home/HomeFragment.java index 15f7cc9..72231b4 100644 --- a/app/src/main/java/core/notevault/ui/home/HomeFragment.java +++ b/app/src/main/java/core/notevault/ui/home/HomeFragment.java @@ -24,6 +24,7 @@ import core.notevault.data.MusicNote; import core.notevault.data.MusicNoteDAO; import core.notevault.databinding.FragmentHomeBinding; import core.notevault.ui.metadatadialog.MetaDataDialog; +import core.notevault.util.NoteSheetsUtil; import java.util.ArrayList; import java.util.List; @@ -92,6 +93,8 @@ public class HomeFragment extends Fragment { private void handleFile(Uri... uris) { // Hier kannst du die Logik zum Speichern oder Anzeigen der Datei implementieren + NoteSheetsUtil.sortNoteSheetsByTimestamp(getContext(), uris); + MetaDataDialog metaDataDialog = new MetaDataDialog(); metaDataDialog.setFileUri(uris); metaDataDialog.show(getParentFragmentManager(), MetaDataDialog.TAG); diff --git a/app/src/main/java/core/notevault/util/NoteSheetsUtil.java b/app/src/main/java/core/notevault/util/NoteSheetsUtil.java new file mode 100644 index 0000000..f98ea4a --- /dev/null +++ b/app/src/main/java/core/notevault/util/NoteSheetsUtil.java @@ -0,0 +1,54 @@ +package core.notevault.util; + +import android.content.Context; +import android.database.Cursor; +import android.media.ExifInterface; +import android.net.Uri; +import android.provider.MediaStore; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.*; + +public class NoteSheetsUtil { + + private static long getImageTimestamp(Context context, Uri imageUri) { + try { + InputStream inputStream = context.getContentResolver().openInputStream(imageUri); + ExifInterface exif = new ExifInterface(inputStream); + + String dateTime = exif.getAttribute(ExifInterface.TAG_DATETIME); + if(dateTime != null) { + SimpleDateFormat format = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss", Locale.getDefault()); + Date date = format.parse(dateTime); + return date != null ? date.getTime() : 0; + } else { + File file = new File(imageUri.getPath()); + return file.lastModified(); + } + } catch (IOException | ParseException e) { + throw new RuntimeException(e); + } + } + + public static void sortNoteSheetsByTimestamp(Context context, Uri[] uris) { + ArrayList uriTimestamps = new ArrayList<>(); + + for(Uri uri : uris) { + long timestamp = getImageTimestamp(context, uri); + uriTimestamps.add(new UriTimestamp(uri, timestamp)); + } + + Collections.sort(uriTimestamps, Comparator.comparingLong(UriTimestamp::getTimestamp)); + + for(int i=0; i