From: jabarros Date: Tue, 11 Nov 2014 11:27:57 +0000 (+0100) Subject: Add workaround for Drive app in which a creation of a temporal copy is needed X-Git-Tag: oc-android-1.7.0_signed~101^2~10 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/903b66595f80fa2ff05b53797f27a7c072443b57 Add workaround for Drive app in which a creation of a temporal copy is needed --- diff --git a/src/com/owncloud/android/operations/UploadFileOperation.java b/src/com/owncloud/android/operations/UploadFileOperation.java index 43fcaa55..819d218f 100644 --- a/src/com/owncloud/android/operations/UploadFileOperation.java +++ b/src/com/owncloud/android/operations/UploadFileOperation.java @@ -31,11 +31,16 @@ import java.util.concurrent.atomic.AtomicBoolean; import org.apache.commons.httpclient.methods.PutMethod; import org.apache.commons.httpclient.methods.RequestEntity; +import android.accounts.Account; +import android.content.Context; +import android.net.Uri; + +import com.owncloud.android.MainApp; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.files.services.FileUploader; -import com.owncloud.android.lib.common.network.ProgressiveDataTransferer; -import com.owncloud.android.lib.common.network.OnDatatransferProgressListener; import com.owncloud.android.lib.common.OwnCloudClient; +import com.owncloud.android.lib.common.network.OnDatatransferProgressListener; +import com.owncloud.android.lib.common.network.ProgressiveDataTransferer; import com.owncloud.android.lib.common.operations.OperationCancelledException; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; @@ -46,9 +51,6 @@ import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation; import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation; import com.owncloud.android.utils.FileStorageUtils; -import android.accounts.Account; -import android.content.Context; - /** * Remote operation performing the upload of a file to an ownCloud server @@ -92,10 +94,9 @@ public class UploadFileOperation extends RemoteOperation { throw new IllegalArgumentException("Illegal NULL account in UploadFileOperation creation"); if (file == null) throw new IllegalArgumentException("Illegal NULL file in UploadFileOperation creation"); - if (file.getStoragePath() == null || file.getStoragePath().length() <= 0 - || !(new File(file.getStoragePath()).exists())) { + if (file.getStoragePath() == null || file.getStoragePath().length() <= 0) { throw new IllegalArgumentException( - "Illegal file in UploadFileOperation; storage path invalid or file not found: " + "Illegal file in UploadFileOperation; storage path invalid: " + file.getStoragePath()); } @@ -218,52 +219,76 @@ public class UploadFileOperation extends RemoteOperation { // copied } else { + String temporalPath = FileStorageUtils.getTemporalPath(mAccount.name) + mFile.getRemotePath(); mFile.setStoragePath(temporalPath); temporalFile = new File(temporalPath); - if (!mOriginalStoragePath.equals(temporalPath)) { // preventing - // weird - // but - // possible - // situation - InputStream in = null; - OutputStream out = null; - try { - File temporalParent = temporalFile.getParentFile(); - temporalParent.mkdirs(); - if (!temporalParent.isDirectory()) { - throw new IOException("Unexpected error: parent directory could not be created"); - } - temporalFile.createNewFile(); - if (!temporalFile.isFile()) { - throw new IOException("Unexpected error: target file could not be created"); - } - in = new FileInputStream(originalFile); + + File temporalParent = temporalFile.getParentFile(); + temporalParent.mkdirs(); + if (!temporalParent.isDirectory()) { + throw new IOException("Unexpected error: parent directory could not be created"); + } + temporalFile.createNewFile(); + if (!temporalFile.isFile()) { + throw new IOException("Unexpected error: target file could not be created"); + } + + InputStream in = null; + OutputStream out = null; + + try { + + if (mOriginalStoragePath.startsWith("content://")) { + + Uri uri = Uri.parse(mOriginalStoragePath); + + in = MainApp.getAppContext().getContentResolver().openInputStream(uri); out = new FileOutputStream(temporalFile); - byte[] buf = new byte[1024]; - int len; - while ((len = in.read(buf)) > 0) { - out.write(buf, 0, len); - } - } catch (Exception e) { - result = new RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_COPIED); - return result; - - } finally { - try { - if (in != null) - in.close(); - } catch (Exception e) { - Log_OC.d(TAG, "Weird exception while closing input stream for " + mOriginalStoragePath + " (ignoring)", e); + int nRead; + byte[] data = new byte[16384]; + + while ((nRead = in.read(data, 0, data.length)) != -1) { + out.write(data, 0, nRead); } - try { - if (out != null) - out.close(); - } catch (Exception e) { - Log_OC.d(TAG, "Weird exception while closing output stream for " + expectedPath + " (ignoring)", e); + + out.flush(); + + } else { + if (!mOriginalStoragePath.equals(temporalPath)) { // preventing + // weird + // but + // possible + // situation + + in = new FileInputStream(originalFile); + out = new FileOutputStream(temporalFile); + byte[] buf = new byte[1024]; + int len; + while ((len = in.read(buf)) > 0) { + out.write(buf, 0, len); + } } } + + } catch (Exception e) { + result = new RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_COPIED); + return result; + + } finally { + try { + if (in != null) + in.close(); + } catch (Exception e) { + Log_OC.d(TAG, "Weird exception while closing input stream for " + mOriginalStoragePath + " (ignoring)", e); + } + try { + if (out != null) + out.close(); + } catch (Exception e) { + Log_OC.d(TAG, "Weird exception while closing output stream for " + expectedPath + " (ignoring)", e); + } } } } @@ -417,5 +442,4 @@ public class UploadFileOperation extends RemoteOperation { public void cancel() { mUploadOperation.cancel(); } - } diff --git a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java index ad6a072a..bc0231fb 100644 --- a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -49,9 +49,11 @@ 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; @@ -691,7 +693,34 @@ OnSslUntrustedCertListener, OnEnforceableRefreshListener { } if (!remotepath.endsWith(OCFile.PATH_SEPARATOR)) remotepath += OCFile.PATH_SEPARATOR; - remotepath += new File(filepath).getName(); + + if (filepath.startsWith("content://")) { + // The query, since it only applies to a single document, will only return + // one row. There's no need to filter, sort, or select fields, since we want + // all fields for one document. + Cursor cursor = MainApp.getAppContext().getContentResolver() + .query(Uri.parse(filepath), null, null, null, null, null); + + try { + // moveToFirst() returns false if the cursor has 0 rows. Very handy for + // "if there's anything to look at, look at it" conditionals. + if (cursor != null && cursor.moveToFirst()) { + + // Note it's called "Display Name". This is + // provider-specific, and might not necessarily be the file name. + String displayName = cursor.getString( + cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); + Log.i(TAG, "Display Name: " + displayName); + + remotepath += displayName; + } + } finally { + cursor.close(); + } + + } else { + remotepath += new File(filepath).getName(); + } i.putExtra(FileUploader.KEY_LOCAL_FILE, filepath); i.putExtra(FileUploader.KEY_REMOTE_FILE, remotepath); @@ -919,7 +948,11 @@ OnSslUntrustedCertListener, OnEnforceableRefreshListener { final String[] selectionArgs = new String[] { split[1] }; return getDataColumn(getApplicationContext(), contentUri, selection, selectionArgs); - } + } + // Google Drive + else if (isGoogleDriveDocument(uri)) { + return uri.toString(); + } } // MediaStore (and general) else if ("content".equalsIgnoreCase(uri.getScheme())) {