From 71c9e7e04e4a8f2b7d59853ce2666844361e1d13 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 18 Jun 2016 14:26:55 +0200 Subject: [PATCH] order the directories properly by timestamp --- .../gallery/activities/MainActivity.java | 22 ++++++++++++------- .../gallery/activities/MediaActivity.java | 4 ++-- .../gallery/models/Directory.java | 21 ++++++++++++++++-- .../gallery/models/Medium.java | 8 +++---- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java index 0f0ee707d..f201ca3aa 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java @@ -35,6 +35,7 @@ import com.simplemobiletools.gallery.models.Directory; import java.io.File; import java.util.ArrayList; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -126,7 +127,7 @@ public class MainActivity extends AppCompatActivity private void initializeGallery() { toBeDeleted = new ArrayList<>(); - dirs = new ArrayList<>(getDirectories().values()); + dirs = getDirectories(); final DirectoryAdapter adapter = new DirectoryAdapter(this, dirs); gridView.setAdapter(adapter); @@ -135,7 +136,7 @@ public class MainActivity extends AppCompatActivity gridView.setOnTouchListener(this); } - private Map getDirectories() { + private List getDirectories() { final Map directories = new LinkedHashMap<>(); final List invalidFiles = new ArrayList<>(); for (int i = 0; i < 2; i++) { @@ -149,7 +150,7 @@ public class MainActivity extends AppCompatActivity uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } - final String[] columns = {MediaStore.Images.Media.DATA}; + final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_TAKEN}; final String order = MediaStore.Images.Media.DATE_MODIFIED + " DESC"; final Cursor cursor = getContentResolver().query(uri, columns, null, null, order); @@ -165,23 +166,28 @@ public class MainActivity extends AppCompatActivity continue; } + final int dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN); + final long timestamp = cursor.getLong(dateIndex); if (directories.containsKey(fileDir)) { final Directory directory = directories.get(fileDir); final int newImageCnt = directory.getMediaCnt() + 1; directory.setMediaCnt(newImageCnt); } else if (!toBeDeleted.contains(fileDir)) { final String dirName = Utils.getFilename(fileDir); - directories.put(fileDir, new Directory(fileDir, path, dirName, 1)); + directories.put(fileDir, new Directory(fileDir, path, dirName, 1, timestamp)); } } while (cursor.moveToNext()); cursor.close(); } } + final List dirs = new ArrayList<>(directories.values()); + Collections.sort(dirs); + final String[] invalids = invalidFiles.toArray(new String[invalidFiles.size()]); MediaScannerConnection.scanFile(getApplicationContext(), invalids, null, null); - return directories; + return dirs; } private void prepareForDeleting() { @@ -202,7 +208,7 @@ public class MainActivity extends AppCompatActivity } private void notifyDeletion(int cnt) { - dirs = new ArrayList<>(getDirectories().values()); + dirs = getDirectories(); final CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.coordinator_layout); final Resources res = getResources(); @@ -250,7 +256,7 @@ public class MainActivity extends AppCompatActivity snackbar.dismiss(); isSnackbarShown = false; toBeDeleted.clear(); - dirs = new ArrayList<>(getDirectories().values()); + dirs = getDirectories(); updateGridView(); } }; @@ -423,7 +429,7 @@ public class MainActivity extends AppCompatActivity private void scanCompleted(final String path) { final File dir = new File(path); if (dir.isDirectory()) { - dirs = new ArrayList<>(getDirectories().values()); + dirs = getDirectories(); runOnUiThread(new Runnable() { @Override diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java index a5b8b17b6..e8e52fa4f 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java @@ -132,13 +132,13 @@ public class MediaActivity extends AppCompatActivity if (cursor != null && cursor.moveToFirst()) { final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); - final int dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN); do { final String curPath = cursor.getString(pathIndex); if (curPath.matches(pattern) && !toBeDeleted.contains(curPath)) { final File file = new File(curPath); if (file.exists()) { - final int timestamp = cursor.getInt(dateIndex); + final int dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN); + final long timestamp = cursor.getLong(dateIndex); myMedia.add(new Medium(curPath, (i == 1), timestamp)); } else { invalidFiles.add(file.getAbsolutePath()); diff --git a/app/src/main/java/com/simplemobiletools/gallery/models/Directory.java b/app/src/main/java/com/simplemobiletools/gallery/models/Directory.java index ada17568f..f8454a890 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/models/Directory.java +++ b/app/src/main/java/com/simplemobiletools/gallery/models/Directory.java @@ -1,16 +1,18 @@ package com.simplemobiletools.gallery.models; -public class Directory { +public class Directory implements Comparable { private final String path; private final String thumbnail; private final String name; + private final long timestamp; private int mediaCnt; - public Directory(String path, String thumbnail, String name, int mediaCnt) { + public Directory(String path, String thumbnail, String name, int mediaCnt, long timestamp) { this.path = path; this.thumbnail = thumbnail; this.name = name; this.mediaCnt = mediaCnt; + this.timestamp = timestamp; } public String getPath() { @@ -32,4 +34,19 @@ public class Directory { public void setMediaCnt(int cnt) { mediaCnt = cnt; } + + public long getTimestamp() { + return timestamp; + } + + @Override + public int compareTo(Object object) { + final Directory directory = (Directory) object; + if (this.timestamp < directory.getTimestamp()) { + return 1; + } else if (this.timestamp > directory.getTimestamp()) { + return -1; + } + return 0; + } } diff --git a/app/src/main/java/com/simplemobiletools/gallery/models/Medium.java b/app/src/main/java/com/simplemobiletools/gallery/models/Medium.java index 5b0b188f5..3855959cf 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/models/Medium.java +++ b/app/src/main/java/com/simplemobiletools/gallery/models/Medium.java @@ -6,9 +6,9 @@ public class Medium implements Serializable, Comparable { private static final long serialVersionUID = -6543139465975455L; private final String path; private final boolean isVideo; - private final int timestamp; + private final long timestamp; - public Medium(String path, boolean isVideo, int timestamp) { + public Medium(String path, boolean isVideo, long timestamp) { this.path = path; this.isVideo = isVideo; this.timestamp = timestamp; @@ -22,13 +22,13 @@ public class Medium implements Serializable, Comparable { return isVideo; } - public int getTimestamp() { + public long getTimestamp() { return timestamp; } @Override public int compareTo(Object object) { - Medium medium = (Medium) object; + final Medium medium = (Medium) object; if (this.timestamp < medium.getTimestamp()) { return 1; } else if (this.timestamp > medium.getTimestamp()) {