X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/5a54bfdc4d91ee395552f39960ab3f5ba2bbabdb..e901b609baa4dd5f681e2a5257c9e504997e3377:/src/com/owncloud/android/ui/activity/FileDisplayActivity.java diff --git a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java index 63298334..49259e9f 100644 --- a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -21,8 +21,6 @@ package com.owncloud.android.ui.activity; import java.io.File; import java.io.IOException; -import org.apache.commons.httpclient.methods.PostMethod; - import android.accounts.Account; import android.accounts.AccountManager; import android.accounts.AuthenticatorException; @@ -33,6 +31,7 @@ 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; @@ -43,14 +42,17 @@ 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.support.v4.widget.SwipeRefreshLayout; import android.util.Log; import android.view.View; import android.view.ViewGroup; @@ -64,6 +66,7 @@ import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.Window; +import com.owncloud.android.BuildConfig; import com.owncloud.android.MainApp; import com.owncloud.android.R; import com.owncloud.android.datamodel.OCFile; @@ -80,6 +83,7 @@ import com.owncloud.android.lib.common.network.CertificateCombinedException; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; +import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.operations.CreateFolderOperation; import com.owncloud.android.operations.CreateShareOperation; import com.owncloud.android.operations.MoveFileOperation; @@ -90,6 +94,7 @@ import com.owncloud.android.operations.SynchronizeFolderOperation; import com.owncloud.android.operations.UnshareLinkOperation; import com.owncloud.android.services.observer.FileObserverService; import com.owncloud.android.syncadapter.FileSyncAdapter; +import com.owncloud.android.ui.adapter.FileListListAdapter; import com.owncloud.android.ui.dialog.CreateFolderDialogFragment; import com.owncloud.android.ui.dialog.SslUntrustedCertDialog; import com.owncloud.android.ui.dialog.SslUntrustedCertDialog.OnSslUntrustedCertListener; @@ -102,7 +107,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.Log_OC; +import com.owncloud.android.utils.UriUtils; /** @@ -114,7 +119,7 @@ import com.owncloud.android.utils.Log_OC; public class FileDisplayActivity extends HookActivity implements FileFragment.ContainerActivity, OnNavigationListener, -OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener { +OnSslUntrustedCertListener, OnEnforceableRefreshListener { private ArrayAdapter mDirectories; @@ -251,7 +256,7 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener { Log_OC.e(TAG, "Initializing Fragments in onAccountChanged.."); initFragmentsWithFile(); if (file.isFolder()) { - startSyncFolderOperation(file); + startSyncFolderOperation(file, false); } } else { @@ -292,7 +297,7 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener { if (listOfFiles != null) { listOfFiles.listDirectory(getCurrentDir()); } else { - Log.e(TAG, "Still have a chance to lose the initializacion of list fragment >("); + Log_OC.e(TAG, "Still have a chance to lose the initializacion of list fragment >("); } /// Second fragment @@ -308,12 +313,12 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener { } } else { - Log.wtf(TAG, "initFragments() called with invalid NULLs!"); + Log_OC.wtf(TAG, "initFragments() called with invalid NULLs!"); if (getAccount() == null) { - Log.wtf(TAG, "\t account is NULL"); + Log_OC.wtf(TAG, "\t account is NULL"); } if (getFile() == null) { - Log.wtf(TAG, "\t file is NULL"); + Log_OC.wtf(TAG, "\t file is NULL"); } } } @@ -452,6 +457,16 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener { } @Override + public boolean onPrepareOptionsMenu(Menu menu) { + if (BuildConfig.DEBUG) { + menu.findItem(R.id.action_logger).setVisible(true); + } else { + menu.findItem(R.id.action_logger).setVisible(false); + } + return super.onPrepareOptionsMenu(menu); + } + + @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getSherlock().getMenuInflater(); inflater.inflate(R.menu.main_menu, menu); @@ -481,6 +496,11 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener { startActivity(settingsIntent); break; } + case R.id.action_logger: { + Intent loggerIntent = new Intent(getApplicationContext(),LogHistoryActivity.class); + startActivity(loggerIntent); + break; + } case android.R.id.home: { FileFragment second = getSecondFragment(); OCFile currentDir = getCurrentDir(); @@ -491,6 +511,40 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener { } break; } + case R.id.action_sort: { + SharedPreferences appPreferences = PreferenceManager + .getDefaultSharedPreferences(this); + + // Read sorting order, default to sort by name ascending + Integer sortOrder = appPreferences + .getInt("sortOrder", FileListListAdapter.SORT_NAME); + + 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: + sortByName(true); + break; + case 1: + sortByDate(false); + break; + +// TODO re-enable when server-side folder size calculation is available +// case 2: +// sortBySize(false); +// break; + } + + dialog.dismiss(); + + } + }); + builder.create().show(); + break; + } default: retval = super.onOptionsItemSelected(item); } @@ -516,6 +570,11 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener { 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); } @@ -607,8 +666,12 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener { 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); @@ -640,10 +703,34 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener { } 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); @@ -819,22 +906,74 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener { 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; } @@ -1026,7 +1165,7 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener { } ocFileListFragment.setMessageForEmptyList(getString(message)); } else { - Log.e(TAG, "OCFileListFragment is null"); + Log_OC.e(TAG, "OCFileListFragment is null"); } } @@ -1153,7 +1292,7 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener { OCFile root = getStorageManager().getFileByPath(OCFile.ROOT_PATH); listOfFiles.listDirectory(root); setFile(listOfFiles.getCurrentFile()); - startSyncFolderOperation(root); + startSyncFolderOperation(root, false); } cleanSecondFragment(); } @@ -1168,7 +1307,7 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener { setNavigationListWithFolder(folder); listOfFiles.listDirectory(folder); setFile(listOfFiles.getCurrentFile()); - startSyncFolderOperation(folder); + startSyncFolderOperation(folder, false); } else { Log_OC.e(TAG, "Unexpected null when accessing list fragment"); } @@ -1187,7 +1326,7 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener { cleanSecondFragment(); // Sync Folder - startSyncFolderOperation(directory); + startSyncFolderOperation(directory, false); } @@ -1304,7 +1443,7 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener { @Override public void onSavedCertificate() { - startSyncFolderOperation(getCurrentDir()); + startSyncFolderOperation(getCurrentDir(), false); } @@ -1592,7 +1731,7 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener { return null; } - public void startSyncFolderOperation(OCFile folder) { + public void startSyncFolderOperation(OCFile folder, boolean ignoreETag) { long currentSyncTime = System.currentTimeMillis(); mSyncInProgress = true; @@ -1602,6 +1741,7 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener { currentSyncTime, false, getFileOperationsHelper().isSharedSupported(), + ignoreETag, getStorageManager(), getAccount(), getApplicationContext() @@ -1714,16 +1854,36 @@ OnSslUntrustedCertListener, SwipeRefreshLayout.OnRefreshListener { } @Override + public void onRefresh(boolean ignoreETag) { + refreshList(ignoreETag); + } + + @Override public void onRefresh() { + refreshList(true); + } + + private void refreshList(boolean ignoreETag) { OCFileListFragment listOfFiles = getListOfFilesFragment(); if (listOfFiles != null) { OCFile folder = listOfFiles.getCurrentFile(); if (folder != null) { /*mFile = mContainerActivity.getStorageManager().getFileById(mFile.getFileId()); listDirectory(mFile);*/ - startSyncFolderOperation(folder); + startSyncFolderOperation(folder, ignoreETag); } } } + private void sortByDate(boolean ascending){ + getListOfFilesFragment().sortByDate(ascending); + } + + private void sortBySize(boolean ascending){ + getListOfFilesFragment().sortBySize(ascending); + } + + private void sortByName(boolean ascending){ + getListOfFilesFragment().sortByName(ascending); + } }