Merge pull request #820 from owncloud/obeySortOrder_fixmerge

Obey sort order in gallery of images.
This commit is contained in:
David A. Velasco 2015-01-08 09:08:42 +01:00
commit 8da1926f02
5 changed files with 175 additions and 146 deletions

View file

@ -109,6 +109,7 @@ import com.owncloud.android.ui.preview.PreviewMediaFragment;
import com.owncloud.android.ui.preview.PreviewVideoActivity;
import com.owncloud.android.utils.DisplayUtils;
import com.owncloud.android.utils.ErrorMessageAdapter;
import com.owncloud.android.utils.FileStorageUtils;
import com.owncloud.android.utils.UriUtils;
@ -519,7 +520,7 @@ OnSslUntrustedCertListener, OnEnforceableRefreshListener {
// Read sorting order, default to sort by name ascending
Integer sortOrder = appPreferences
.getInt("sortOrder", FileListListAdapter.SORT_NAME);
.getInt("sortOrder", FileStorageUtils.SORT_NAME);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.actionbar_sort_title)

View file

@ -71,11 +71,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
private FileDataStorageManager mStorageManager;
private Account mAccount;
private ComponentsGetter mTransferServiceGetter;
private Integer mSortOrder;
public static final Integer SORT_NAME = 0;
public static final Integer SORT_DATE = 1;
public static final Integer SORT_SIZE = 2;
private Boolean mSortAscending;
private SharedPreferences mAppPreferences;
public FileListListAdapter(
@ -94,9 +90,9 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
.getDefaultSharedPreferences(mContext);
// Read sorting order, default to sort by name ascending
mSortOrder = mAppPreferences
FileStorageUtils.mSortOrder = mAppPreferences
.getInt("sortOrder", 0);
mSortAscending = mAppPreferences.getBoolean("sortAscending", true);
FileStorageUtils.mSortAscending = mAppPreferences.getBoolean("sortAscending", true);
// initialise thumbnails cache on background thread
new ThumbnailsCacheManager.InitDiskCacheTask().execute();
@ -294,33 +290,14 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
File dir = new File(path);
if (dir.exists()) {
long bytes = getFolderSize(dir);
long bytes = FileStorageUtils.getFolderSize(dir);
return DisplayUtils.bytesToHumanReadable(bytes);
}
return "0 B";
}
/**
* Local Folder size
* @param dir File
* @return Size in bytes
*/
private long getFolderSize(File dir) {
if (dir.exists()) {
long result = 0;
File[] fileList = dir.listFiles();
for(int i = 0; i < fileList.length; i++) {
if(fileList[i].isDirectory()) {
result += getFolderSize(fileList[i]);
} else {
result += fileList[i].length();
}
}
return result;
}
return 0;
}
@Override
public int getViewTypeCount() {
@ -359,29 +336,11 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
mFiles = null;
}
sortDirectory();
}
/**
* Sorts all filenames, regarding last user decision
*/
private void sortDirectory(){
switch (mSortOrder){
case 0:
sortByName(mSortAscending);
break;
case 1:
sortByDate(mSortAscending);
break;
case 2:
sortBySize(mSortAscending);
break;
}
mFiles = FileStorageUtils.sortFolder(mFiles);
notifyDataSetChanged();
}
/**
* Filter for getting only the folders
* @param files
@ -414,110 +373,24 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
&& file.getPermissions().contains(PERMISSION_SHARED_WITH_ME));
}
/**
* Sorts list by Date
* @param sortAscending true: ascending, false: descending
*/
private void sortByDate(boolean sortAscending){
final Integer val;
if (sortAscending){
val = 1;
} else {
val = -1;
}
Collections.sort(mFiles, new Comparator<OCFile>() {
public int compare(OCFile o1, OCFile o2) {
if (o1.isFolder() && o2.isFolder()) {
Long obj1 = o1.getModificationTimestamp();
return val * obj1.compareTo(o2.getModificationTimestamp());
}
else if (o1.isFolder()) {
return -1;
} else if (o2.isFolder()) {
return 1;
} else if (o1.getModificationTimestamp() == 0 || o2.getModificationTimestamp() == 0){
return 0;
} else {
Long obj1 = o1.getModificationTimestamp();
return val * obj1.compareTo(o2.getModificationTimestamp());
}
}
});
}
/**
* Sorts list by Size
* @param sortAscending true: ascending, false: descending
*/
private void sortBySize(boolean sortAscending){
final Integer val;
if (sortAscending){
val = 1;
} else {
val = -1;
}
Collections.sort(mFiles, new Comparator<OCFile>() {
public int compare(OCFile o1, OCFile o2) {
if (o1.isFolder() && o2.isFolder()) {
Long obj1 = getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o1)));
return val * obj1.compareTo(getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o2))));
}
else if (o1.isFolder()) {
return -1;
} else if (o2.isFolder()) {
return 1;
} else if (o1.getFileLength() == 0 || o2.getFileLength() == 0){
return 0;
} else {
Long obj1 = o1.getFileLength();
return val * obj1.compareTo(o2.getFileLength());
}
}
});
}
/**
* Sorts list by Name
* @param sortAscending true: ascending, false: descending
*/
private void sortByName(boolean sortAscending){
final Integer val;
if (sortAscending){
val = 1;
} else {
val = -1;
}
Collections.sort(mFiles, new Comparator<OCFile>() {
public int compare(OCFile o1, OCFile o2) {
if (o1.isFolder() && o2.isFolder()) {
return val * o1.getRemotePath().toLowerCase().compareTo(o2.getRemotePath().toLowerCase());
} else if (o1.isFolder()) {
return -1;
} else if (o2.isFolder()) {
return 1;
}
return val * new AlphanumComparator().compare(o1, o2);
}
});
}
public void setSortOrder(Integer order, boolean ascending) {
SharedPreferences.Editor editor = mAppPreferences.edit();
editor.putInt("sortOrder", order);
editor.putBoolean("sortAscending", ascending);
editor.commit();
mSortOrder = order;
mSortAscending = ascending;
FileStorageUtils.mSortOrder = order;
FileStorageUtils.mSortAscending = ascending;
sortDirectory();
mFiles = FileStorageUtils.sortFolder(mFiles);
notifyDataSetChanged();
}
private CharSequence showRelativeTimestamp(OCFile file){
return DisplayUtils.getRelativeDateTimeString(mContext, file.getModificationTimestamp(),
DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0);
}
}

View file

@ -48,6 +48,7 @@ import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
import com.owncloud.android.ui.preview.PreviewImageFragment;
import com.owncloud.android.ui.preview.PreviewMediaFragment;
import com.owncloud.android.utils.FileStorageUtils;
/**
* A Fragment that lists all files and folders in a given path.
@ -437,15 +438,15 @@ public class OCFileListFragment extends ExtendedListFragment {
}
public void sortByName(boolean descending) {
mAdapter.setSortOrder(FileListListAdapter.SORT_NAME, descending);
mAdapter.setSortOrder(FileStorageUtils.SORT_NAME, descending);
}
public void sortByDate(boolean descending) {
mAdapter.setSortOrder(FileListListAdapter.SORT_DATE, descending);
mAdapter.setSortOrder(FileStorageUtils.SORT_DATE, descending);
}
public void sortBySize(boolean descending) {
mAdapter.setSortOrder(FileListListAdapter.SORT_SIZE, descending);
mAdapter.setSortOrder(FileStorageUtils.SORT_SIZE, descending);
}
}

View file

@ -16,6 +16,8 @@
*/
package com.owncloud.android.ui.preview;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@ -31,7 +33,9 @@ import android.view.ViewGroup;
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.ui.adapter.FileListListAdapter;
import com.owncloud.android.ui.fragment.FileFragment;
import com.owncloud.android.utils.FileStorageUtils;
/**
* Adapter class that provides Fragment instances
@ -73,13 +77,15 @@ public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter {
mAccount = account;
mStorageManager = storageManager;
mImageFiles = mStorageManager.getFolderImages(parentFolder);
mImageFiles = FileStorageUtils.sortFolder(mImageFiles);
mObsoleteFragments = new HashSet<Object>();
mObsoletePositions = new HashSet<Integer>();
mDownloadErrors = new HashSet<Integer>();
//mFragmentManager = fragmentManager;
mCachedFragments = new HashMap<Integer, FileFragment>();
}
/**
* Returns the image files handled by the adapter.

View file

@ -18,6 +18,11 @@
package com.owncloud.android.utils;
import java.io.File;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;
import third_parties.daveKoeller.AlphanumComparator;
import com.owncloud.android.MainApp;
import com.owncloud.android.R;
@ -39,6 +44,13 @@ import android.os.StatFs;
* @author David A. Velasco
*/
public class FileStorageUtils {
public static Integer mSortOrder;
public static Boolean mSortAscending;
public static final Integer SORT_NAME = 0;
public static final Integer SORT_DATE = 1;
public static final Integer SORT_SIZE = 2;
//private static final String LOG_TAG = "FileStorageUtils";
public static final String getSavePath(String accountName) {
@ -137,5 +149,141 @@ public class FileStorageUtils {
file.setRemoteId(ocFile.getRemoteId());
return file;
}
/**
* Sorts all filenames, regarding last user decision
*/
public static Vector<OCFile> sortFolder(Vector<OCFile> files){
switch (mSortOrder){
case 0:
files = FileStorageUtils.sortByName(files);
break;
case 1:
files = FileStorageUtils.sortByDate(files);
break;
case 2:
// mFiles = FileStorageUtils.sortBySize(mSortAscending);
break;
}
return files;
}
/**
* Sorts list by Date
* @param sortAscending true: ascending, false: descending
*/
public static Vector<OCFile> sortByDate(Vector<OCFile> files){
final Integer val;
if (mSortAscending){
val = 1;
} else {
val = -1;
}
Collections.sort(files, new Comparator<OCFile>() {
public int compare(OCFile o1, OCFile o2) {
if (o1.isFolder() && o2.isFolder()) {
Long obj1 = o1.getModificationTimestamp();
return val * obj1.compareTo(o2.getModificationTimestamp());
}
else if (o1.isFolder()) {
return -1;
} else if (o2.isFolder()) {
return 1;
} else if (o1.getModificationTimestamp() == 0 || o2.getModificationTimestamp() == 0){
return 0;
} else {
Long obj1 = o1.getModificationTimestamp();
return val * obj1.compareTo(o2.getModificationTimestamp());
}
}
});
return files;
}
// /**
// * Sorts list by Size
// * @param sortAscending true: ascending, false: descending
// */
// public static Vector<OCFile> sortBySize(Vector<OCFile> files){
// final Integer val;
// if (mSortAscending){
// val = 1;
// } else {
// val = -1;
// }
//
// Collections.sort(files, new Comparator<OCFile>() {
// public int compare(OCFile o1, OCFile o2) {
// if (o1.isFolder() && o2.isFolder()) {
// Long obj1 = getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o1)));
// return val * obj1.compareTo(getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o2))));
// }
// else if (o1.isFolder()) {
// return -1;
// } else if (o2.isFolder()) {
// return 1;
// } else if (o1.getFileLength() == 0 || o2.getFileLength() == 0){
// return 0;
// } else {
// Long obj1 = o1.getFileLength();
// return val * obj1.compareTo(o2.getFileLength());
// }
// }
// });
//
// return files;
// }
/**
* Sorts list by Name
* @param sortAscending true: ascending, false: descending
*/
public static Vector<OCFile> sortByName(Vector<OCFile> files){
final Integer val;
if (mSortAscending){
val = 1;
} else {
val = -1;
}
Collections.sort(files, new Comparator<OCFile>() {
public int compare(OCFile o1, OCFile o2) {
if (o1.isFolder() && o2.isFolder()) {
return val * o1.getRemotePath().toLowerCase().compareTo(o2.getRemotePath().toLowerCase());
} else if (o1.isFolder()) {
return -1;
} else if (o2.isFolder()) {
return 1;
}
return val * new AlphanumComparator().compare(o1, o2);
}
});
return files;
}
/**
* Local Folder size
* @param dir File
* @return Size in bytes
*/
public static long getFolderSize(File dir) {
if (dir.exists()) {
long result = 0;
File[] fileList = dir.listFiles();
for(int i = 0; i < fileList.length; i++) {
if(fileList[i].isDirectory()) {
result += getFolderSize(fileList[i]);
} else {
result += fileList[i].length();
}
}
return result;
}
return 0;
}
}