From d05770edfaf33217b90a0443ab054189bdcebb05 Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Thu, 30 Oct 2014 18:28:50 +0100 Subject: [PATCH 1/2] - moved Sorting to FileStorageUtils - Slideshow obeys now the current sorting order --- .../ui/activity/FileDisplayActivity.java | 3 +- .../ui/adapter/FileListListAdapter.java | 152 ++---------------- .../ui/fragment/OCFileListFragment.java | 7 +- .../ui/preview/PreviewImagePagerAdapter.java | 8 +- .../android/utils/FileStorageUtils.java | 148 +++++++++++++++++ 5 files changed, 172 insertions(+), 146 deletions(-) diff --git a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java index b5d543a4e2..0206eb92c6 100644 --- a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -101,6 +101,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; /** @@ -510,7 +511,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) diff --git a/src/com/owncloud/android/ui/adapter/FileListListAdapter.java b/src/com/owncloud/android/ui/adapter/FileListListAdapter.java index 9a2a0d3af3..17a936d05b 100644 --- a/src/com/owncloud/android/ui/adapter/FileListListAdapter.java +++ b/src/com/owncloud/android/ui/adapter/FileListListAdapter.java @@ -70,11 +70,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( @@ -92,9 +88,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(); @@ -295,33 +291,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() { @@ -360,29 +337,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.sortDirectory(mFiles); notifyDataSetChanged(); } - + /** * Filter for getting only the folders * @param files @@ -415,105 +374,16 @@ 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() { - 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() { - 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() { - 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.sortDirectory(mFiles); + notifyDataSetChanged(); } } diff --git a/src/com/owncloud/android/ui/fragment/OCFileListFragment.java b/src/com/owncloud/android/ui/fragment/OCFileListFragment.java index 0b1059fc03..31f96cbafc 100644 --- a/src/com/owncloud/android/ui/fragment/OCFileListFragment.java +++ b/src/com/owncloud/android/ui/fragment/OCFileListFragment.java @@ -44,6 +44,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. @@ -388,15 +389,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); } } diff --git a/src/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java b/src/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java index f2a9a9b294..05671a0402 100644 --- a/src/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java +++ b/src/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java @@ -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.sortDirectory(mImageFiles); + mObsoleteFragments = new HashSet(); mObsoletePositions = new HashSet(); mDownloadErrors = new HashSet(); //mFragmentManager = fragmentManager; mCachedFragments = new HashMap(); } - /** * Returns the image files handled by the adapter. diff --git a/src/com/owncloud/android/utils/FileStorageUtils.java b/src/com/owncloud/android/utils/FileStorageUtils.java index 3895821d6c..de1a32076e 100644 --- a/src/com/owncloud/android/utils/FileStorageUtils.java +++ b/src/com/owncloud/android/utils/FileStorageUtils.java @@ -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) { @@ -123,5 +135,141 @@ public class FileStorageUtils { file.setRemoteId(ocFile.getRemoteId()); return file; } + + /** + * Sorts all filenames, regarding last user decision + */ + public static Vector sortDirectory(Vector 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 sortByDate(Vector files){ + final Integer val; + if (mSortAscending){ + val = 1; + } else { + val = -1; + } + + Collections.sort(files, new Comparator() { + 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 sortBySize(Vector files){ +// final Integer val; +// if (mSortAscending){ +// val = 1; +// } else { +// val = -1; +// } +// +// Collections.sort(files, new Comparator() { +// 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 sortByName(Vector files){ + final Integer val; + if (mSortAscending){ + val = 1; + } else { + val = -1; + } + + Collections.sort(files, new Comparator() { + 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; + } } From e23fa3acaa3eb1ab7d77c05c9c3d64b3ce2867c6 Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Thu, 18 Dec 2014 21:19:39 +0100 Subject: [PATCH 2/2] renamed sortDirectory to sortFolder --- src/com/owncloud/android/ui/adapter/FileListListAdapter.java | 4 ++-- .../owncloud/android/ui/preview/PreviewImagePagerAdapter.java | 2 +- src/com/owncloud/android/utils/FileStorageUtils.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/com/owncloud/android/ui/adapter/FileListListAdapter.java b/src/com/owncloud/android/ui/adapter/FileListListAdapter.java index 17a936d05b..563d31ed51 100644 --- a/src/com/owncloud/android/ui/adapter/FileListListAdapter.java +++ b/src/com/owncloud/android/ui/adapter/FileListListAdapter.java @@ -337,7 +337,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter { mFiles = null; } - mFiles = FileStorageUtils.sortDirectory(mFiles); + mFiles = FileStorageUtils.sortFolder(mFiles); notifyDataSetChanged(); } @@ -383,7 +383,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter { FileStorageUtils.mSortOrder = order; FileStorageUtils.mSortAscending = ascending; - mFiles = FileStorageUtils.sortDirectory(mFiles); + mFiles = FileStorageUtils.sortFolder(mFiles); notifyDataSetChanged(); } } diff --git a/src/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java b/src/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java index 05671a0402..3c15de7199 100644 --- a/src/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java +++ b/src/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.java @@ -78,7 +78,7 @@ public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter { mStorageManager = storageManager; mImageFiles = mStorageManager.getFolderImages(parentFolder); - mImageFiles = FileStorageUtils.sortDirectory(mImageFiles); + mImageFiles = FileStorageUtils.sortFolder(mImageFiles); mObsoleteFragments = new HashSet(); mObsoletePositions = new HashSet(); diff --git a/src/com/owncloud/android/utils/FileStorageUtils.java b/src/com/owncloud/android/utils/FileStorageUtils.java index de1a32076e..f17124c143 100644 --- a/src/com/owncloud/android/utils/FileStorageUtils.java +++ b/src/com/owncloud/android/utils/FileStorageUtils.java @@ -139,7 +139,7 @@ public class FileStorageUtils { /** * Sorts all filenames, regarding last user decision */ - public static Vector sortDirectory(Vector files){ + public static Vector sortFolder(Vector files){ switch (mSortOrder){ case 0: files = FileStorageUtils.sortByName(files);