Merge pull request #124 from nextcloud/sortInUploadFilesActivity

Sort in upload files activity
This commit is contained in:
Andy Scherzinger 2016-07-14 22:38:04 +02:00 committed by GitHub
commit d00893c66a
10 changed files with 273 additions and 44 deletions

View file

@ -26,4 +26,11 @@
android:title="@string/select_all"
android:icon="@drawable/ic_select_all"
app:showAsAction="always"/>
<item
android:id="@+id/action_sort"
android:icon="@drawable/ic_sort_variant"
android:orderInCategory="2"
app:showAsAction="ifRoom"
android:title="@string/actionbar_sort"
android:contentDescription="@string/actionbar_sort"/>
</menu>

View file

@ -22,6 +22,7 @@ package com.owncloud.android.db;
import android.content.Context;
import android.content.SharedPreferences;
import com.owncloud.android.files.services.FileUploader;
import com.owncloud.android.utils.FileStorageUtils;
/**
@ -35,6 +36,7 @@ public abstract class PreferenceManager {
private static final String AUTO_PREF__LAST_UPLOAD_PATH = "last_upload_path";
private static final String AUTO_PREF__SORT_ORDER = "sort_order";
private static final String AUTO_PREF__SORT_ASCENDING = "sort_ascending";
private static final String AUTO_PREF__UPLOADER_BEHAVIOR = "prefs_uploader_behaviour";
private static final String PREF__INSTANT_UPLOADING = "instant_uploading";
private static final String PREF__INSTANT_VIDEO_UPLOADING = "instant_video_uploading";
private static final String PREF__INSTANT_UPLOAD_PATH_USE_SUBFOLDERS = "instant_upload_path_use_subfolders";
@ -127,6 +129,27 @@ public abstract class PreferenceManager {
saveBooleanPreference(context, AUTO_PREF__SORT_ASCENDING, ascending);
}
/**
* Gets the uploader behavior which the user has set last.
*
* @param context Caller {@link Context}, used to access to shared preferences manager.
* @return uploader behavior the uploader behavior
*/
public static int getUploaderBehaviour(Context context) {
return getDefaultSharedPreferences(context)
.getInt(AUTO_PREF__UPLOADER_BEHAVIOR, FileUploader.LOCAL_BEHAVIOUR_COPY);
}
/**
* Saves the uploader behavior which the user has set last.
*
* @param context Caller {@link Context}, used to access to shared preferences manager.
* @param uploaderBehaviour the uploader behavior
*/
public static void setUploaderBehaviour(Context context, int uploaderBehaviour) {
saveIntPreference(context, AUTO_PREF__UPLOADER_BEHAVIOR, uploaderBehaviour);
}
private static void saveBooleanPreference(Context context, String key, boolean value) {
SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
appPreferences.putBoolean(key, value);

View file

@ -31,11 +31,10 @@ import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.IntentFilter;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.IntentFilter;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.os.Parcelable;
@ -67,9 +66,9 @@ import com.owncloud.android.operations.CreateFolderOperation;
import com.owncloud.android.operations.RefreshFolderOperation;
import com.owncloud.android.syncadapter.FileSyncAdapter;
import com.owncloud.android.ui.adapter.UploaderAdapter;
import com.owncloud.android.ui.asynctasks.CopyAndUploadContentUrisTask;
import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
import com.owncloud.android.ui.dialog.CreateFolderDialogFragment;
import com.owncloud.android.ui.asynctasks.CopyAndUploadContentUrisTask;
import com.owncloud.android.ui.fragment.TaskRetainerFragment;
import com.owncloud.android.ui.helpers.UriUploader;
import com.owncloud.android.utils.DisplayUtils;
@ -458,7 +457,7 @@ public class ReceiveExternalFilesActivity extends FileActivity
// Read sorting order, default to sort by name ascending
FileStorageUtils.mSortOrder = PreferenceManager.getSortOrder(this);
FileStorageUtils.mSortAscending = PreferenceManager.getSortAscending(this);
FileStorageUtils.sortFolder(files);
FileStorageUtils.sortOcFolder(files);
}
private String generatePath(Stack<String> dirs) {

View file

@ -22,12 +22,12 @@ package com.owncloud.android.ui.activity;
import android.accounts.Account;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.ActionBar;
import android.view.Menu;
@ -41,6 +41,7 @@ import android.widget.RadioButton;
import android.widget.TextView;
import com.owncloud.android.R;
import com.owncloud.android.db.PreferenceManager;
import com.owncloud.android.files.services.FileUploader;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
@ -129,10 +130,7 @@ public class UploadFilesActivity extends FileActivity implements
mUploadBtn = (Button) findViewById(R.id.upload_files_btn_upload);
mUploadBtn.setOnClickListener(this);
SharedPreferences appPreferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
Integer localBehaviour = appPreferences.getInt("prefs_uploader_behaviour", FileUploader.LOCAL_BEHAVIOUR_COPY);
int localBehaviour = PreferenceManager.getUploaderBehaviour(this);
mRadioBtnMoveFiles = (RadioButton) findViewById(R.id.upload_radio_move);
if (localBehaviour == FileUploader.LOCAL_BEHAVIOUR_MOVE){
@ -179,6 +177,7 @@ public class UploadFilesActivity extends FileActivity implements
activity.startActivityForResult(action, requestCode);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
mOptionsMenu = menu;
@ -205,6 +204,30 @@ public class UploadFilesActivity extends FileActivity implements
mFileListFragment.selectAllFiles(item.isChecked());
break;
}
case R.id.action_sort: {
// Read sorting order, default to sort by name ascending
Integer sortOrder = PreferenceManager.getSortOrder(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.actionbar_sort_title)
.setSingleChoiceItems(R.array.actionbar_sortby, sortOrder ,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
mFileListFragment.sortByName(true);
break;
case 1:
mFileListFragment.sortByDate(false);
break;
}
dialog.dismiss();
}
});
builder.create().show();
break;
}
default:
retval = super.onOptionsItemSelected(item);
}
@ -422,20 +445,13 @@ public class UploadFilesActivity extends FileActivity implements
Intent data = new Intent();
data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
SharedPreferences.Editor appPreferencesEditor = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext()).edit();
if (mRadioBtnMoveFiles.isChecked()){
setResult(RESULT_OK_AND_MOVE, data);
appPreferencesEditor.putInt("prefs_uploader_behaviour",
FileUploader.LOCAL_BEHAVIOUR_MOVE);
PreferenceManager.setUploaderBehaviour(getApplicationContext(), FileUploader.LOCAL_BEHAVIOUR_MOVE);
} else {
setResult(RESULT_OK, data);
appPreferencesEditor.putInt("prefs_uploader_behaviour",
FileUploader.LOCAL_BEHAVIOUR_COPY);
PreferenceManager.setUploaderBehaviour(getApplicationContext(), FileUploader.LOCAL_BEHAVIOUR_COPY);
}
appPreferencesEditor.apply();
finish();
} else {
// show a dialog to query the user if wants to move the selected files

View file

@ -394,7 +394,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
mFiles = null;
}
mFiles = FileStorageUtils.sortFolder(mFiles);
mFiles = FileStorageUtils.sortOcFolder(mFiles);
notifyDataSetChanged();
}
@ -424,7 +424,7 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
FileStorageUtils.mSortOrder = order;
FileStorageUtils.mSortAscending = ascending;
mFiles = FileStorageUtils.sortFolder(mFiles);
mFiles = FileStorageUtils.sortOcFolder(mFiles);
notifyDataSetChanged();
}

View file

@ -33,9 +33,11 @@ import android.widget.TextView;
import com.owncloud.android.R;
import com.owncloud.android.datamodel.ThumbnailsCacheManager;
import com.owncloud.android.db.PreferenceManager;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.utils.BitmapUtils;
import com.owncloud.android.utils.DisplayUtils;
import com.owncloud.android.utils.FileStorageUtils;
import com.owncloud.android.utils.MimetypeIconUtil;
import java.io.File;
@ -56,6 +58,11 @@ public class LocalFileListAdapter extends BaseAdapter implements ListAdapter {
public LocalFileListAdapter(File directory, Context context) {
mContext = context;
// Read sorting order, default to sort by name ascending
FileStorageUtils.mSortOrder = PreferenceManager.getSortOrder(context);
FileStorageUtils.mSortAscending =PreferenceManager.getSortAscending(context);
swapDirectory(directory);
}
@ -232,7 +239,20 @@ public class LocalFileListAdapter extends BaseAdapter implements ListAdapter {
}
});
mFiles = FileStorageUtils.sortLocalFolder(mFiles);
}
notifyDataSetChanged();
}
public void setSortOrder(Integer order, boolean ascending) {
PreferenceManager.setSortOrder(mContext, order);
PreferenceManager.setSortAscending(mContext, ascending);
FileStorageUtils.mSortOrder = order;
FileStorageUtils.mSortAscending = ascending;
mFiles = FileStorageUtils.sortLocalFolder(mFiles);
notifyDataSetChanged();
}
}

View file

@ -37,6 +37,7 @@ import android.widget.ListView;
import com.owncloud.android.R;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.ui.adapter.LocalFileListAdapter;
import com.owncloud.android.utils.FileStorageUtils;
/**
@ -229,6 +230,18 @@ public class LocalFileListFragment extends ExtendedListFragment {
return result.toArray(new String[result.size()]);
}
public void sortByName(boolean descending) {
mAdapter.setSortOrder(FileStorageUtils.SORT_NAME, descending);
}
public void sortByDate(boolean descending) {
mAdapter.setSortOrder(FileStorageUtils.SORT_DATE, descending);
}
public void sortBySize(boolean descending) {
mAdapter.setSortOrder(FileStorageUtils.SORT_SIZE, descending);
}
/**
* De-/select all elements in the local file list.
*

View file

@ -80,7 +80,7 @@ public class PreviewImagePagerAdapter extends FragmentStatePagerAdapter {
// TODO Enable when "On Device" is recovered ?
mImageFiles = mStorageManager.getFolderImages(parentFolder/*, false*/);
mImageFiles = FileStorageUtils.sortFolder(mImageFiles);
mImageFiles = FileStorageUtils.sortOcFolder(mImageFiles);
mObsoleteFragments = new HashSet<Object>();
mObsoletePositions = new HashSet<Integer>();

View file

@ -38,9 +38,12 @@ import com.owncloud.android.lib.resources.files.RemoteFile;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Vector;
@ -232,27 +235,46 @@ public class FileStorageUtils {
/**
* Sorts all filenames, regarding last user decision
*/
public static Vector<OCFile> sortFolder(Vector<OCFile> files){
public static Vector<OCFile> sortOcFolder(Vector<OCFile> files){
switch (mSortOrder){
case 0:
files = FileStorageUtils.sortByName(files);
files = FileStorageUtils.sortOCFilesByName(files);
break;
case 1:
files = FileStorageUtils.sortByDate(files);
files = FileStorageUtils.sortOCFilesByDate(files);
break;
case 2:
files = FileStorageUtils.sortBySize(files);
files = FileStorageUtils.sortOCFilesBySize(files);
break;
}
return files;
}
/**
* Sorts all filenames, regarding last user decision
*/
public static File[] sortLocalFolder(File[] files){
switch (mSortOrder){
case 0:
files = FileStorageUtils.sortLocalFilesByName(files);
break;
case 1:
files = FileStorageUtils.sortLocalFilesByDate(files);
break;
case 2:
files = FileStorageUtils.sortLocalFilesBySize(files);
break;
}
return files;
}
/**
* Sorts list by Date
* @param files
*/
public static Vector<OCFile> sortByDate(Vector<OCFile> files){
public static Vector<OCFile> sortOCFilesByDate(Vector<OCFile> files){
final int multiplier = mSortAscending ? 1 : -1;
Collections.sort(files, new Comparator<OCFile>() {
@ -277,10 +299,42 @@ public class FileStorageUtils {
return files;
}
/**
* Sorts list by Date
* @param filesArray
*/
public static File[] sortLocalFilesByDate(File[] filesArray){
final int multiplier = mSortAscending ? 1 : -1;
List<File> files = new ArrayList<File>(Arrays.asList(filesArray));
Collections.sort(files, new Comparator<File>() {
public int compare(File o1, File o2) {
if (o1.isDirectory() && o2.isDirectory()) {
Long obj1 = o1.lastModified();
return multiplier * obj1.compareTo(o2.lastModified());
}
else if (o1.isDirectory()) {
return -1;
} else if (o2.isDirectory()) {
return 1;
} else if (o1.lastModified() == 0 || o2.lastModified() == 0){
return 0;
} else {
Long obj1 = o1.lastModified();
return multiplier * obj1.compareTo(o2.lastModified());
}
}
});
File[] returnArray = new File[1];
return files.toArray(returnArray);
}
/**
* Sorts list by Size
*/
public static Vector<OCFile> sortBySize(Vector<OCFile> files){
public static Vector<OCFile> sortOCFilesBySize(Vector<OCFile> files){
final int multiplier = mSortAscending ? 1 : -1;
Collections.sort(files, new Comparator<OCFile>() {
@ -305,11 +359,42 @@ public class FileStorageUtils {
return files;
}
/**
* Sorts list by Size
*/
public static File[] sortLocalFilesBySize(File[] filesArray) {
final int multiplier = mSortAscending ? 1 : -1;
List<File> files = new ArrayList<File>(Arrays.asList(filesArray));
Collections.sort(files, new Comparator<File>() {
public int compare(File o1, File o2) {
if (o1.isDirectory() && o2.isDirectory()) {
Long obj1 = getFolderSize(o1);
return multiplier * obj1.compareTo(getFolderSize(o2));
}
else if (o1.isDirectory()) {
return -1;
} else if (o2.isDirectory()) {
return 1;
} else if (o1.length() == 0 || o2.length() == 0){
return 0;
} else {
Long obj1 = o1.length();
return multiplier * obj1.compareTo(o2.length());
}
}
});
File[] returnArray = new File[1];
return files.toArray(returnArray);
}
/**
* Sorts list by Name
* @param files files to sort
*/
public static Vector<OCFile> sortByName(Vector<OCFile> files){
public static Vector<OCFile> sortOCFilesByName(Vector<OCFile> files){
final int multiplier = mSortAscending ? 1 : -1;
Collections.sort(files, new Comparator<OCFile>() {
@ -328,6 +413,54 @@ public class FileStorageUtils {
return files;
}
/**
* Sorts list by Name
* @param filesArray files to sort
*/
public static File[] sortLocalFilesByName(File[] filesArray) {
final int multiplier = mSortAscending ? 1 : -1;
List<File> files = new ArrayList<File>(Arrays.asList(filesArray));
Collections.sort(files, new Comparator<File>() {
public int compare(File o1, File o2) {
if (o1.isDirectory() && o2.isDirectory()) {
return multiplier * o1.getPath().toLowerCase().compareTo(o2.getPath().toLowerCase());
} else if (o1.isDirectory()) {
return -1;
} else if (o2.isDirectory()) {
return 1;
}
return multiplier * new AlphanumComparator().compare(o1.getPath().toLowerCase(),
o2.getPath().toLowerCase());
}
});
File[] returnArray = new File[1];
return files.toArray(returnArray);
}
/**
* 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;
}
/**
* Mimetype String of a file
* @param path

View file

@ -23,7 +23,7 @@
*/
package third_parties.daveKoeller;
import java.text.Collator;
import java.io.File;
import java.util.Comparator;
import com.owncloud.android.datamodel.OCFile;
@ -49,12 +49,14 @@ public class AlphanumComparator implements Comparator<OCFile>
}
/** Length of string is passed in for improved efficiency (only need to calculate it once) **/
private final String getChunk(String s, int slength, int marker){
private final String getChunk(String s, int slength, int marker)
{
StringBuilder chunk = new StringBuilder();
char c = s.charAt(marker);
chunk.append(c);
marker++;
if (isDigit(c)){
if (isDigit(c))
{
while (marker < slength)
{
c = s.charAt(marker);
@ -63,7 +65,8 @@ public class AlphanumComparator implements Comparator<OCFile>
chunk.append(c);
marker++;
}
} else {
} else
{
while (marker < slength)
{
c = s.charAt(marker);
@ -77,15 +80,27 @@ public class AlphanumComparator implements Comparator<OCFile>
}
public int compare(OCFile o1, OCFile o2){
String s1 = (String)o1.getRemotePath().toLowerCase();
String s2 = (String)o2.getRemotePath().toLowerCase();
String s1 = o1.getRemotePath().toLowerCase();
String s2 = o2.getRemotePath().toLowerCase();
return compare(s1, s2);
}
public int compare(File f1, File f2){
String s1 = f1.getPath().toLowerCase();
String s2 = f2.getPath().toLowerCase();
return compare(s1, s2);
}
public int compare(String s1, String s2) {
int thisMarker = 0;
int thatMarker = 0;
int s1Length = s1.length();
int s2Length = s2.length();
while (thisMarker < s1Length && thatMarker < s2Length) {
while (thisMarker < s1Length && thatMarker < s2Length)
{
String thisChunk = getChunk(s1, s1Length, thisMarker);
thisMarker += thisChunk.length();
@ -94,23 +109,26 @@ public class AlphanumComparator implements Comparator<OCFile>
// If both chunks contain numeric characters, sort them numerically
int result = 0;
if (isDigit(thisChunk.charAt(0)) && isDigit(thatChunk.charAt(0))) {
if (isDigit(thisChunk.charAt(0)) && isDigit(thatChunk.charAt(0)))
{
// Simple chunk comparison by length.
int thisChunkLength = thisChunk.length();
result = thisChunkLength - thatChunk.length();
// If equal, the first different number counts
if (result == 0) {
for (int i = 0; i < thisChunkLength; i++) {
if (result == 0)
{
for (int i = 0; i < thisChunkLength; i++)
{
result = thisChunk.charAt(i) - thatChunk.charAt(i);
if (result != 0) {
if (result != 0)
{
return result;
}
}
}
} else {
Collator collator = Collator.getInstance();
collator.setStrength(Collator.PRIMARY);
result = collator.compare(thisChunk, thatChunk);
} else
{
result = thisChunk.compareTo(thatChunk);
}
if (result != 0)