From: jabarros Date: Fri, 26 Dec 2014 10:10:22 +0000 (+0100) Subject: Merge branch 'develop' into obeySortOrder_fixmerge X-Git-Tag: oc-android-1.7.0_signed~47^2 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/4257aa1d9c53facbd1b59cc037b14dadeec5d2f2?hp=-c Merge branch 'develop' into obeySortOrder_fixmerge --- 4257aa1d9c53facbd1b59cc037b14dadeec5d2f2 diff --combined src/com/owncloud/android/ui/activity/FileDisplayActivity.java index 0206eb92,f5837493..3bec9167 --- a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java @@@ -25,12 -25,15 +25,15 @@@ import android.accounts.Account import android.accounts.AccountManager; import android.accounts.AuthenticatorException; import android.accounts.OperationCanceledException; + import android.annotation.SuppressLint; + import android.annotation.TargetApi; import android.app.AlertDialog; import android.app.Dialog; import android.app.ProgressDialog; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.ContentResolver; + import android.content.ContentUris; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@@ -41,13 -44,18 +44,18 @@@ import android.content.SyncRequest import android.content.res.Resources.NotFoundException; import android.database.Cursor; import android.net.Uri; + import android.os.Build; import android.os.Bundle; + import android.os.Environment; import android.os.IBinder; import android.preference.PreferenceManager; + import android.provider.DocumentsContract; import android.provider.MediaStore; + import android.provider.OpenableColumns; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; + import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; @@@ -101,7 -109,7 +109,8 @@@ import com.owncloud.android.ui.preview. 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; /** @@@ -152,7 -160,7 +161,7 @@@ OnSslUntrustedCertListener, OnEnforceab private String DIALOG_UNTRUSTED_CERT; private OCFile mWaitingToSend; - + @Override protected void onCreate(Bundle savedInstanceState) { Log_OC.d(TAG, "onCreate() start"); @@@ -511,7 -519,7 +520,7 @@@ // 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) @@@ -564,6 -572,11 +573,11 @@@ builder.setExpedited(true); builder.setManual(true); builder.syncOnce(); + + // Fix bug in Android Lollipop when you click on refresh the whole account + Bundle extras = new Bundle(); + builder.setExtras(extras); + SyncRequest request = builder.build(); ContentResolver.requestSync(request); } @@@ -594,18 -607,27 +608,27 @@@ /** * Called, when the user selected something for uploading + * */ + @TargetApi(Build.VERSION_CODES.JELLY_BEAN) protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == ACTION_SELECT_CONTENT_FROM_APPS && (resultCode == RESULT_OK || resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)) { - requestSimpleUpload(data, resultCode); - + //getClipData is only supported on api level 16+, Jelly Bean + if (data.getData() == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){ + for( int i = 0; i < data.getClipData().getItemCount(); i++){ + Intent intent = new Intent(); + intent.setData(data.getClipData().getItemAt(i).getUri()); + requestSimpleUpload(intent, resultCode); + } + }else { + requestSimpleUpload(data, resultCode); + } } else if (requestCode == ACTION_SELECT_MULTIPLE_FILES && (resultCode == RESULT_OK || resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE)) { requestMultipleUpload(data, resultCode); - } else if (requestCode == ACTION_MOVE_FILES && (resultCode == RESULT_OK || - resultCode == MoveActivity.RESULT_OK_AND_MOVE)){ + } else if (requestCode == ACTION_MOVE_FILES && resultCode == RESULT_OK){ final Intent fData = data; final int fResultCode = resultCode; @@@ -655,8 -677,12 +678,12 @@@ private void requestSimpleUpload(Intent data, int resultCode) { String filepath = null; + String mimeType = null; + + Uri selectedImageUri = data.getData(); + try { - Uri selectedImageUri = data.getData(); + mimeType = getContentResolver().getType(selectedImageUri); String filemanagerstring = selectedImageUri.getPath(); String selectedImagePath = getPath(selectedImageUri); @@@ -688,10 -714,34 +715,34 @@@ } if (!remotepath.endsWith(OCFile.PATH_SEPARATOR)) remotepath += OCFile.PATH_SEPARATOR; - remotepath += new File(filepath).getName(); + + if (filepath.startsWith(UriUtils.URI_CONTENT_SCHEME)) { + + Cursor cursor = MainApp.getAppContext().getContentResolver() + .query(Uri.parse(filepath), null, null, null, null, null); + + try { + if (cursor != null && cursor.moveToFirst()) { + String displayName = cursor.getString( + cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); + Log.i(TAG, "Display Name: " + displayName + "; mimeType: " + mimeType); + + displayName.replace(File.separatorChar, '_'); + displayName.replace(File.pathSeparatorChar, '_'); + remotepath += displayName + DisplayUtils.getComposedFileExtension(filepath); + + } + } finally { + cursor.close(); + } + + } else { + remotepath += new File(filepath).getName(); + } i.putExtra(FileUploader.KEY_LOCAL_FILE, filepath); i.putExtra(FileUploader.KEY_REMOTE_FILE, remotepath); + i.putExtra(FileUploader.KEY_MIME_TYPE, mimeType); i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE); if (resultCode == UploadFilesActivity.RESULT_OK_AND_MOVE) i.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, FileUploader.LOCAL_BEHAVIOUR_MOVE); @@@ -705,8 -755,8 +756,8 @@@ * @param resultCode Result code received */ private void requestMoveOperation(Intent data, int resultCode) { - OCFile folderToMoveAt = (OCFile) data.getParcelableExtra(MoveActivity.EXTRA_CURRENT_FOLDER); - OCFile targetFile = (OCFile) data.getParcelableExtra(MoveActivity.EXTRA_TARGET_FILE); + OCFile folderToMoveAt = (OCFile) data.getParcelableExtra(FolderPickerActivity.EXTRA_FOLDER); + OCFile targetFile = (OCFile) data.getParcelableExtra(FolderPickerActivity.EXTRA_FILE); getFileOperationsHelper().moveFile(folderToMoveAt, targetFile); } @@@ -839,6 -889,10 +890,10 @@@ } else if (item == 1) { Intent action = new Intent(Intent.ACTION_GET_CONTENT); action = action.setType("*/*").addCategory(Intent.CATEGORY_OPENABLE); + //Intent.EXTRA_ALLOW_MULTIPLE is only supported on api level 18+, Jelly Bean + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + action.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); + } startActivityForResult(Intent.createChooser(action, getString(R.string.upload_chooser_title)), ACTION_SELECT_CONTENT_FROM_APPS); } @@@ -867,22 -921,74 +922,74 @@@ return dialog; } - /** - * Translates a content URI of an image to a physical path - * on the disk + * Translates a content URI of an content to a physical path on the disk + * * @param uri The URI to resolve - * @return The path to the image or null if it could not be found + * @return The path to the content or null if it could not be found */ public String getPath(Uri uri) { - String[] projection = { MediaStore.Images.Media.DATA }; - Cursor cursor = managedQuery(uri, projection, null, null, null); - if (cursor != null) { - int column_index = cursor - .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); - cursor.moveToFirst(); - return cursor.getString(column_index); - } + final boolean isKitKatOrLater = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; + + // DocumentProvider + if (isKitKatOrLater && DocumentsContract.isDocumentUri(getApplicationContext(), uri)) { + // ExternalStorageProvider + if (UriUtils.isExternalStorageDocument(uri)) { + final String docId = DocumentsContract.getDocumentId(uri); + final String[] split = docId.split(":"); + final String type = split[0]; + + if ("primary".equalsIgnoreCase(type)) { + return Environment.getExternalStorageDirectory() + "/" + split[1]; + } + } + // DownloadsProvider + else if (UriUtils.isDownloadsDocument(uri)) { + + final String id = DocumentsContract.getDocumentId(uri); + final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), + Long.valueOf(id)); + + return UriUtils.getDataColumn(getApplicationContext(), contentUri, null, null); + } + // MediaProvider + else if (UriUtils.isMediaDocument(uri)) { + final String docId = DocumentsContract.getDocumentId(uri); + final String[] split = docId.split(":"); + final String type = split[0]; + + Uri contentUri = null; + if ("image".equals(type)) { + contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; + } else if ("video".equals(type)) { + contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; + } else if ("audio".equals(type)) { + contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; + } + + final String selection = "_id=?"; + final String[] selectionArgs = new String[] { split[1] }; + + return UriUtils.getDataColumn(getApplicationContext(), contentUri, selection, selectionArgs); + } + // Documents providers returned as content://... + else if (UriUtils.isContentDocument(uri)) { + return uri.toString(); + } + } + // MediaStore (and general) + else if ("content".equalsIgnoreCase(uri.getScheme())) { + + // Return the remote address + if (UriUtils.isGooglePhotosUri(uri)) + return uri.getLastPathSegment(); + + return UriUtils.getDataColumn(getApplicationContext(), uri, null, null); + } + // File + else if ("file".equalsIgnoreCase(uri.getScheme())) { + return uri.getPath(); + } return null; } diff --combined src/com/owncloud/android/ui/adapter/FileListListAdapter.java index 563d31ed,1df1211c..be470614 --- a/src/com/owncloud/android/ui/adapter/FileListListAdapter.java +++ b/src/com/owncloud/android/ui/adapter/FileListListAdapter.java @@@ -29,6 -29,7 +29,7 @@@ import android.content.Context import android.content.SharedPreferences; import android.graphics.Bitmap; import android.preference.PreferenceManager; + import android.text.format.DateUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@@ -70,7 -71,11 +71,7 @@@ public class FileListListAdapter extend 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( @@@ -78,22 -83,24 +79,24 @@@ Context context, ComponentsGetter transferServiceGetter ) { - + mJustFolders = justFolders; mContext = context; mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext); + mTransferServiceGetter = transferServiceGetter; mAppPreferences = PreferenceManager .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(); + } @Override @@@ -177,9 -184,7 +180,7 @@@ fileSizeV.setVisibility(View.VISIBLE); fileSizeV.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength())); lastModV.setVisibility(View.VISIBLE); - lastModV.setText( - DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()) - ); + lastModV.setText(showRelativeTimestamp(file)); // this if-else is needed even thoe fav icon is visible by default // because android reuses views in listview if (!file.keepInSync()) { @@@ -213,7 -218,7 +214,7 @@@ if (ThumbnailsCacheManager.cancelPotentialWork(file, fileIcon)) { final ThumbnailsCacheManager.ThumbnailGenerationTask task = new ThumbnailsCacheManager.ThumbnailGenerationTask( - fileIcon, mStorageManager + fileIcon, mStorageManager, mAccount ); if (thumbnail == null) { thumbnail = ThumbnailsCacheManager.mDefaultImg; @@@ -247,9 -252,7 +248,7 @@@ // } lastModV.setVisibility(View.VISIBLE); - lastModV.setText( - DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()) - ); + lastModV.setText(showRelativeTimestamp(file)); checkBoxV.setVisibility(View.GONE); view.findViewById(R.id.imageView3).setVisibility(View.GONE); @@@ -291,14 -294,33 +290,14 @@@ 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() { @@@ -337,11 -359,29 +336,11 @@@ 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 @@@ -374,16 -414,110 +373,24 @@@ && 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.sortFolder(mFiles); + notifyDataSetChanged(); - } ++ + } + + private CharSequence showRelativeTimestamp(OCFile file){ + return DisplayUtils.getRelativeDateTimeString(mContext, file.getModificationTimestamp(), + DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0); + } ++ } diff --combined src/com/owncloud/android/ui/fragment/OCFileListFragment.java index 31f96cba,73ae5dc2..9c85dd98 --- a/src/com/owncloud/android/ui/fragment/OCFileListFragment.java +++ b/src/com/owncloud/android/ui/fragment/OCFileListFragment.java @@@ -18,8 -18,10 +18,10 @@@ package com.owncloud.android.ui.fragment; import java.io.File; + import java.util.Vector; import android.app.Activity; + import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v4.widget.SwipeRefreshLayout; @@@ -29,6 -31,8 +31,8 @@@ import android.view.MenuItem import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.AdapterContextMenuInfo; + import android.widget.TextView; + import android.view.LayoutInflater; import com.owncloud.android.R; import com.owncloud.android.datamodel.FileDataStorageManager; @@@ -36,7 -40,7 +40,7 @@@ import com.owncloud.android.datamodel.O import com.owncloud.android.files.FileMenuFilter; import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.ui.activity.FileDisplayActivity; - import com.owncloud.android.ui.activity.MoveActivity; + import com.owncloud.android.ui.activity.FolderPickerActivity; import com.owncloud.android.ui.activity.OnEnforceableRefreshListener; import com.owncloud.android.ui.adapter.FileListListAdapter; import com.owncloud.android.ui.dialog.ConfirmationDialogFragment; @@@ -44,7 -48,6 +48,7 @@@ import com.owncloud.android.ui.dialog.R 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. @@@ -71,6 -74,7 +75,7 @@@ public class OCFileListFragment extend private OCFile mFile = null; private FileListListAdapter mAdapter; + private View mFooterView; private OCFile mTargetFile; @@@ -113,24 -117,28 +118,28 @@@ public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Log_OC.e(TAG, "onActivityCreated() start"); - + if (savedInstanceState != null) { mFile = savedInstanceState.getParcelable(KEY_FILE); } - + + mFooterView = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate( + R.layout.list_footer, null, false); + setFooterView(mFooterView); + Bundle args = getArguments(); boolean justFolders = (args == null) ? false : args.getBoolean(ARG_JUST_FOLDERS, false); mAdapter = new FileListListAdapter( justFolders, getSherlockActivity(), mContainerActivity - ); + ); setListAdapter(mAdapter); - + registerForContextMenu(getListView()); getListView().setOnCreateContextMenuListener(this); - } - + } + /** * Saves the current listed folder. */ @@@ -249,15 -257,9 +258,9 @@@ ); mf.filter(menu); } - - /// additional restrictions for this fragment - // TODO allow in the future 'open with' for previewable files - MenuItem item = menu.findItem(R.id.action_open_file_with); - if (item != null) { - item.setVisible(false); - item.setEnabled(false); - } + /// TODO break this direct dependency on FileDisplayActivity... if possible + MenuItem item = menu.findItem(R.id.action_open_file_with); FileFragment frag = ((FileDisplayActivity)getSherlockActivity()).getSecondFragment(); if (frag != null && frag instanceof FileDetailFragment && frag.getFile().getFileId() == targetFile.getFileId()) { @@@ -283,6 -285,10 +286,10 @@@ mContainerActivity.getFileOperationsHelper().shareFileWithLink(mTargetFile); return true; } + case R.id.action_open_file_with: { + mContainerActivity.getFileOperationsHelper().openFile(mTargetFile); + return true; + } case R.id.action_unshare_file: { mContainerActivity.getFileOperationsHelper().unshareFileWithLink(mTargetFile); return true; @@@ -323,10 -329,10 +330,10 @@@ return true; } case R.id.action_move: { - Intent action = new Intent(getActivity(), MoveActivity.class); + Intent action = new Intent(getActivity(), FolderPickerActivity.class); // Pass mTargetFile that contains info of selected file/folder - action.putExtra(MoveActivity.EXTRA_TARGET_FILE, mTargetFile); + action.putExtra(FolderPickerActivity.EXTRA_FILE, mTargetFile); getActivity().startActivityForResult(action, FileDisplayActivity.ACTION_MOVE_FILES); return true; } @@@ -385,19 -391,61 +392,61 @@@ mList.setSelectionFromTop(0, 0); } mFile = directory; + + // Update Footer + TextView footerText = (TextView) mFooterView.findViewById(R.id.footerText); + Log_OC.d("footer", String.valueOf(System.currentTimeMillis())); + footerText.setText(generateFooterText(directory)); + Log_OC.d("footer", String.valueOf(System.currentTimeMillis())); + } + } + + private String generateFooterText(OCFile directory) { + Integer files = 0; + Integer folders = 0; + + FileDataStorageManager storageManager = mContainerActivity.getStorageManager(); + Vector mFiles = storageManager.getFolderContent(mFile); + + for (OCFile ocFile : mFiles) { + if (ocFile.isFolder()) { + folders++; + } else { + files++; + } + } + + String output = ""; + + if (files > 0){ + if (files == 1) { + output = output + files.toString() + " " + getResources().getString(R.string.file_list_file); + } else { + output = output + files.toString() + " " + getResources().getString(R.string.file_list_files); + } + } + if (folders > 0 && files > 0){ + output = output + ", "; } + if (folders == 1) { + output = output + folders.toString() + " " + getResources().getString(R.string.file_list_folder); + } else if (folders > 1) { + output = output + folders.toString() + " " + getResources().getString(R.string.file_list_folders); + } + + return output; } 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 --combined src/com/owncloud/android/utils/FileStorageUtils.java index f17124c1,892a1ca8..b462cd95 --- a/src/com/owncloud/android/utils/FileStorageUtils.java +++ b/src/com/owncloud/android/utils/FileStorageUtils.java @@@ -18,11 -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; @@@ -44,13 -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) { @@@ -93,6 -81,20 +93,20 @@@ String value = uploadPath + OCFile.PATH_SEPARATOR + (fileName == null ? "" : fileName); return value; } + + /** + * Gets the composed path when video is or must be stored + * @param context + * @param fileName: video file name + * @return String: video file path composed + */ + public static String getInstantVideoUploadFilePath(Context context, String fileName) { + SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context); + String uploadVideoPathdef = context.getString(R.string.instant_upload_path); + String uploadVideoPath = pref.getString("instant_video_upload_path", uploadVideoPathdef); + String value = uploadVideoPath + OCFile.PATH_SEPARATOR + (fileName == null ? "" : fileName); + return value; + } public static String getParentPath(String remotePath) { String parentPath = new File(remotePath).getParent(); @@@ -135,141 -137,5 +149,141 @@@ file.setRemoteId(ocFile.getRemoteId()); return file; } + + /** + * Sorts all filenames, regarding last user decision + */ + public static Vector sortFolder(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; + } }