order the directories properly by timestamp

This commit is contained in:
tibbi 2016-06-18 14:26:55 +02:00
parent c2aea6b5fb
commit 71c9e7e04e
4 changed files with 39 additions and 16 deletions

View File

@ -35,6 +35,7 @@ import com.simplemobiletools.gallery.models.Directory;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -126,7 +127,7 @@ public class MainActivity extends AppCompatActivity
private void initializeGallery() { private void initializeGallery() {
toBeDeleted = new ArrayList<>(); toBeDeleted = new ArrayList<>();
dirs = new ArrayList<>(getDirectories().values()); dirs = getDirectories();
final DirectoryAdapter adapter = new DirectoryAdapter(this, dirs); final DirectoryAdapter adapter = new DirectoryAdapter(this, dirs);
gridView.setAdapter(adapter); gridView.setAdapter(adapter);
@ -135,7 +136,7 @@ public class MainActivity extends AppCompatActivity
gridView.setOnTouchListener(this); gridView.setOnTouchListener(this);
} }
private Map<String, Directory> getDirectories() { private List<Directory> getDirectories() {
final Map<String, Directory> directories = new LinkedHashMap<>(); final Map<String, Directory> directories = new LinkedHashMap<>();
final List<String> invalidFiles = new ArrayList<>(); final List<String> invalidFiles = new ArrayList<>();
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
@ -149,7 +150,7 @@ public class MainActivity extends AppCompatActivity
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; 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 String order = MediaStore.Images.Media.DATE_MODIFIED + " DESC";
final Cursor cursor = getContentResolver().query(uri, columns, null, null, order); final Cursor cursor = getContentResolver().query(uri, columns, null, null, order);
@ -165,23 +166,28 @@ public class MainActivity extends AppCompatActivity
continue; continue;
} }
final int dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN);
final long timestamp = cursor.getLong(dateIndex);
if (directories.containsKey(fileDir)) { if (directories.containsKey(fileDir)) {
final Directory directory = directories.get(fileDir); final Directory directory = directories.get(fileDir);
final int newImageCnt = directory.getMediaCnt() + 1; final int newImageCnt = directory.getMediaCnt() + 1;
directory.setMediaCnt(newImageCnt); directory.setMediaCnt(newImageCnt);
} else if (!toBeDeleted.contains(fileDir)) { } else if (!toBeDeleted.contains(fileDir)) {
final String dirName = Utils.getFilename(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()); } while (cursor.moveToNext());
cursor.close(); cursor.close();
} }
} }
final List<Directory> dirs = new ArrayList<>(directories.values());
Collections.sort(dirs);
final String[] invalids = invalidFiles.toArray(new String[invalidFiles.size()]); final String[] invalids = invalidFiles.toArray(new String[invalidFiles.size()]);
MediaScannerConnection.scanFile(getApplicationContext(), invalids, null, null); MediaScannerConnection.scanFile(getApplicationContext(), invalids, null, null);
return directories; return dirs;
} }
private void prepareForDeleting() { private void prepareForDeleting() {
@ -202,7 +208,7 @@ public class MainActivity extends AppCompatActivity
} }
private void notifyDeletion(int cnt) { private void notifyDeletion(int cnt) {
dirs = new ArrayList<>(getDirectories().values()); dirs = getDirectories();
final CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.coordinator_layout); final CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
final Resources res = getResources(); final Resources res = getResources();
@ -250,7 +256,7 @@ public class MainActivity extends AppCompatActivity
snackbar.dismiss(); snackbar.dismiss();
isSnackbarShown = false; isSnackbarShown = false;
toBeDeleted.clear(); toBeDeleted.clear();
dirs = new ArrayList<>(getDirectories().values()); dirs = getDirectories();
updateGridView(); updateGridView();
} }
}; };
@ -423,7 +429,7 @@ public class MainActivity extends AppCompatActivity
private void scanCompleted(final String path) { private void scanCompleted(final String path) {
final File dir = new File(path); final File dir = new File(path);
if (dir.isDirectory()) { if (dir.isDirectory()) {
dirs = new ArrayList<>(getDirectories().values()); dirs = getDirectories();
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override

View File

@ -132,13 +132,13 @@ public class MediaActivity extends AppCompatActivity
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
final int dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN);
do { do {
final String curPath = cursor.getString(pathIndex); final String curPath = cursor.getString(pathIndex);
if (curPath.matches(pattern) && !toBeDeleted.contains(curPath)) { if (curPath.matches(pattern) && !toBeDeleted.contains(curPath)) {
final File file = new File(curPath); final File file = new File(curPath);
if (file.exists()) { 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)); myMedia.add(new Medium(curPath, (i == 1), timestamp));
} else { } else {
invalidFiles.add(file.getAbsolutePath()); invalidFiles.add(file.getAbsolutePath());

View File

@ -1,16 +1,18 @@
package com.simplemobiletools.gallery.models; package com.simplemobiletools.gallery.models;
public class Directory { public class Directory implements Comparable {
private final String path; private final String path;
private final String thumbnail; private final String thumbnail;
private final String name; private final String name;
private final long timestamp;
private int mediaCnt; 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.path = path;
this.thumbnail = thumbnail; this.thumbnail = thumbnail;
this.name = name; this.name = name;
this.mediaCnt = mediaCnt; this.mediaCnt = mediaCnt;
this.timestamp = timestamp;
} }
public String getPath() { public String getPath() {
@ -32,4 +34,19 @@ public class Directory {
public void setMediaCnt(int cnt) { public void setMediaCnt(int cnt) {
mediaCnt = 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;
}
} }

View File

@ -6,9 +6,9 @@ public class Medium implements Serializable, Comparable {
private static final long serialVersionUID = -6543139465975455L; private static final long serialVersionUID = -6543139465975455L;
private final String path; private final String path;
private final boolean isVideo; 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.path = path;
this.isVideo = isVideo; this.isVideo = isVideo;
this.timestamp = timestamp; this.timestamp = timestamp;
@ -22,13 +22,13 @@ public class Medium implements Serializable, Comparable {
return isVideo; return isVideo;
} }
public int getTimestamp() { public long getTimestamp() {
return timestamp; return timestamp;
} }
@Override @Override
public int compareTo(Object object) { public int compareTo(Object object) {
Medium medium = (Medium) object; final Medium medium = (Medium) object;
if (this.timestamp < medium.getTimestamp()) { if (this.timestamp < medium.getTimestamp()) {
return 1; return 1;
} else if (this.timestamp > medium.getTimestamp()) { } else if (this.timestamp > medium.getTimestamp()) {