From: Jorge Antonio Diaz-Benito Soriano Date: Thu, 23 Oct 2014 20:34:42 +0000 (+0200) Subject: Added copy action X-Git-Tag: oc-android-1.8~22^2~18 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/fe5b6aa4a81d1878f4b5ad610cb9141e86630b1a Added copy action --- diff --git a/owncloud-android-library b/owncloud-android-library index dbc8c325..b281585c 160000 --- a/owncloud-android-library +++ b/owncloud-android-library @@ -1 +1 @@ -Subproject commit dbc8c325d74f3f7e8da8236c5abe77a141ae4019 +Subproject commit b281585c93a7eec52e467f51850811f8b0a92955 diff --git a/res/values/strings.xml b/res/values/strings.xml index dd8ce04f..7c422201 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -331,7 +331,13 @@ An error occurred while trying to move this file or folder to move this file - Instant Uploads + Unable to copy. Please check whether the file exists + It is not possible to copy a folder into a descendant + The file exists already in the destination folder + An error occurred while trying to copy this file or folder + to copy this file + + Instant Uploads Security Upload Video Path diff --git a/src/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/com/owncloud/android/datamodel/FileDataStorageManager.java index c5fc94d1..febb3b69 100644 --- a/src/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -50,6 +50,12 @@ import com.owncloud.android.lib.resources.shares.OCShare; import com.owncloud.android.lib.resources.shares.ShareType; import com.owncloud.android.utils.FileStorageUtils; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + public class FileDataStorageManager { public static final int ROOT_PARENT_ID = 0; @@ -60,7 +66,7 @@ public class FileDataStorageManager { private static String TAG = FileDataStorageManager.class.getSimpleName(); - + public FileDataStorageManager(Account account, ContentResolver cr) { mContentProviderClient = null; mContentResolver = cr; @@ -73,7 +79,7 @@ public class FileDataStorageManager { mAccount = account; } - + public void setAccount(Account account) { mAccount = account; } @@ -97,7 +103,7 @@ public class FileDataStorageManager { public ContentProviderClient getContentProviderClient() { return mContentProviderClient; } - + public OCFile getFileByPath(String path) { Cursor c = getCursorForValue(ProviderTableMeta.FILE_PATH, path); @@ -141,7 +147,7 @@ public class FileDataStorageManager { return fileExists(ProviderTableMeta.FILE_PATH, path); } - + public Vector getFolderContent(OCFile f/*, boolean onlyOnDevice*/) { if (f != null && f.isFolder() && f.getFileId() != -1) { // TODO Enable when "On Device" is recovered ? @@ -151,7 +157,7 @@ public class FileDataStorageManager { return new Vector(); } } - + public Vector getFolderImages(OCFile folder/*, boolean onlyOnDevice*/) { Vector ret = new Vector(); @@ -183,7 +189,7 @@ public class FileDataStorageManager { cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype()); cv.put(ProviderTableMeta.FILE_NAME, file.getFileName()); //if (file.getParentId() != DataStorageManager.ROOT_PARENT_ID) - cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId()); + cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId()); cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath()); if (!file.isFolder()) cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); @@ -215,12 +221,12 @@ public class FileDataStorageManager { if (getContentResolver() != null) { getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?", - new String[] { String.valueOf(file.getFileId()) }); + new String[]{String.valueOf(file.getFileId())}); } else { try { getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?", - new String[] { String.valueOf(file.getFileId()) }); + new String[]{String.valueOf(file.getFileId())}); } catch (RemoteException e) { Log_OC.e(TAG, "Fail to insert insert file to database " @@ -246,7 +252,7 @@ public class FileDataStorageManager { long new_id = Long.parseLong(result_uri.getPathSegments() .get(1)); file.setFileId(new_id); - } + } } // if (file.isFolder()) { @@ -254,17 +260,17 @@ public class FileDataStorageManager { // } else { // updateFolderSize(file.getParentId()); // } - + return overriden; } /** * Inserts or updates the list of files contained in a given folder. - * + *

* CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD. * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED - * + * * @param folder * @param updatedFiles * @param filesToRemove @@ -314,9 +320,9 @@ public class FileDataStorageManager { // updating an existing file operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI). withValues(cv). - withSelection( ProviderTableMeta._ID + "=?", - new String[] { String.valueOf(file.getFileId()) }) - .build()); + withSelection(ProviderTableMeta._ID + "=?", + new String[]{String.valueOf(file.getFileId())}) + .build()); } else { // adding a new file @@ -324,9 +330,9 @@ public class FileDataStorageManager { withValues(cv).build()); } } - + // prepare operations to remove files in the given folder - String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?" + " AND " + + String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?" + " AND " + ProviderTableMeta.FILE_PATH + "=?"; String [] whereArgs = null; for (OCFile file : filesToRemove) { @@ -360,7 +366,7 @@ public class FileDataStorageManager { } } } - + // update metadata of folder ContentValues cv = new ContentValues(); cv.put(ProviderTableMeta.FILE_MODIFIED, folder.getModificationTimestamp()); @@ -383,12 +389,12 @@ public class FileDataStorageManager { cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, folder.getPublicLink()); cv.put(ProviderTableMeta.FILE_PERMISSIONS, folder.getPermissions()); cv.put(ProviderTableMeta.FILE_REMOTE_ID, folder.getRemoteId()); - + operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI). withValues(cv). - withSelection( ProviderTableMeta._ID + "=?", - new String[] { String.valueOf(folder.getFileId()) }) - .build()); + withSelection(ProviderTableMeta._ID + "=?", + new String[]{String.valueOf(folder.getFileId())}) + .build()); // apply operations in batch ContentProviderResult[] results = null; @@ -413,7 +419,7 @@ public class FileDataStorageManager { long newId; Iterator filesIt = updatedFiles.iterator(); OCFile file = null; - for (int i=0; i 0); + success &= (deleted > 0); } String localPath = file.getStoragePath(); if (removeLocalCopy && file.isDown() && localPath != null && success) { @@ -509,12 +515,12 @@ public class FileDataStorageManager { } return success; } - + public boolean removeFolder(OCFile folder, boolean removeDBData, boolean removeLocalContent) { boolean success = true; if (folder != null && folder.isFolder()) { - if (removeDBData && folder.getFileId() != -1) { + if (removeDBData && folder.getFileId() != -1) { success = removeFolderInDb(folder); } if (removeLocalContent && success) { @@ -525,7 +531,7 @@ public class FileDataStorageManager { } private boolean removeFolderInDb(OCFile folder) { - Uri folder_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, "" + + Uri folder_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, "" + folder.getFileId()); // URI for recursive deletion String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?" + " AND " + ProviderTableMeta.FILE_PATH + "=?"; @@ -538,7 +544,7 @@ public class FileDataStorageManager { e.printStackTrace(); } } else { - deleted = getContentResolver().delete(folder_uri, where, whereArgs); + deleted = getContentResolver().delete(folder_uri, where, whereArgs); } return deleted > 0; } @@ -597,54 +603,54 @@ public class FileDataStorageManager { /** * Updates database and file system for a file or folder that was moved to a different location. - * + * * TODO explore better (faster) implementations * TODO throw exceptions up ! */ public void moveLocalFile(OCFile file, String targetPath, String targetParentPath) { if (file != null && file.fileExists() && !OCFile.ROOT_PATH.equals(file.getFileName())) { - + OCFile targetParent = getFileByPath(targetParentPath); if (targetParent == null) { throw new IllegalStateException("Parent folder of the target path does not exist!!"); } - + /// 1. get all the descendants of the moved element in a single QUERY Cursor c = null; if (getContentProviderClient() != null) { try { c = getContentProviderClient().query( - ProviderTableMeta.CONTENT_URI, - null, - ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + - ProviderTableMeta.FILE_PATH + " LIKE ? ", - new String[] { - mAccount.name, - file.getRemotePath() + "%" - }, - ProviderTableMeta.FILE_PATH + " ASC " + ProviderTableMeta.CONTENT_URI, + null, + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + + ProviderTableMeta.FILE_PATH + " LIKE ? ", + new String[]{ + mAccount.name, + file.getRemotePath() + "%" + }, + ProviderTableMeta.FILE_PATH + " ASC " ); } catch (RemoteException e) { Log_OC.e(TAG, e.getMessage()); } - + } else { c = getContentResolver().query( - ProviderTableMeta.CONTENT_URI, - null, - ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + - ProviderTableMeta.FILE_PATH + " LIKE ? ", - new String[] { - mAccount.name, - file.getRemotePath() + "%" - }, - ProviderTableMeta.FILE_PATH + " ASC " + ProviderTableMeta.CONTENT_URI, + null, + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + + ProviderTableMeta.FILE_PATH + " LIKE ? ", + new String[]{ + mAccount.name, + file.getRemotePath() + "%" + }, + ProviderTableMeta.FILE_PATH + " ASC " ); } /// 2. prepare a batch of update operations to change all the descendants - ArrayList operations = + ArrayList operations = new ArrayList(c.getCount()); String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name); List originalPathsToTriggerMediaScan = new ArrayList(); @@ -656,13 +662,13 @@ public class FileDataStorageManager { ContentValues cv = new ContentValues(); // keep construction in the loop OCFile child = createFileInstance(c); cv.put( - ProviderTableMeta.FILE_PATH, - targetPath + child.getRemotePath().substring(lengthOfOldPath) + ProviderTableMeta.FILE_PATH, + targetPath + child.getRemotePath().substring(lengthOfOldPath) ); - if (child.getStoragePath() != null && + if (child.getStoragePath() != null && child.getStoragePath().startsWith(defaultSavePath)) { // update link to downloaded content - but local move is not done here! - String targetLocalPath = defaultSavePath + targetPath + + String targetLocalPath = defaultSavePath + targetPath + child.getStoragePath().substring(lengthOfOldStoragePath); cv.put(ProviderTableMeta.FILE_STORAGE_PATH, targetLocalPath); @@ -675,17 +681,17 @@ public class FileDataStorageManager { cv.put( ProviderTableMeta.FILE_PARENT, targetParent.getFileId() - ); + ); } operations.add( - ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI). - withValues(cv). - withSelection( - ProviderTableMeta._ID + "=?", - new String[] { String.valueOf(child.getFileId()) } + ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI). + withValues(cv). + withSelection( + ProviderTableMeta._ID + "=?", + new String[]{String.valueOf(child.getFileId())} ) - .build()); - + .build()); + } while (c.moveToNext()); } c.close(); @@ -730,9 +736,60 @@ public class FileDataStorageManager { } } } - + } - + + public void copyLocalFile(OCFile file, String targetPath) { + + if (file != null && file.fileExists() && !OCFile.ROOT_PATH.equals(file.getFileName())) { + String localPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file); + File localFile = new File(localPath); + boolean copied = false; + String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name); + if (localFile.exists()) { + File targetFile = new File(defaultSavePath + targetPath); + File targetFolder = targetFile.getParentFile(); + if (!targetFolder.exists()) { + targetFolder.mkdirs(); + } + copied = copyFile(localFile, targetFile); + } + Log_OC.d(TAG, "Local file COPIED : " + copied); + } + } + + private static boolean copyFile(File src, File target) { + boolean ret = true; + + InputStream in = null; + OutputStream out = null; + + try { + in = new FileInputStream(src); + out = new FileOutputStream(target); + byte[] buf = new byte[1024]; + int len; + while ((len = in.read(buf)) > 0) { + out.write(buf, 0, len); + } + } catch (IOException ex) { + ret = false; + } finally { + if (in != null) try { + in.close(); + } catch (IOException e) { + e.printStackTrace(System.err); + } + if (out != null) try { + out.close(); + } catch (IOException e) { + e.printStackTrace(System.err); + } + } + + return ret; + } + private Vector getFolderContent(long parentId/*, boolean onlyOnDevice*/) { @@ -745,17 +802,17 @@ public class FileDataStorageManager { if (getContentProviderClient() != null) { try { - c = getContentProviderClient().query(req_uri, null, - ProviderTableMeta.FILE_PARENT + "=?" , - new String[] { String.valueOf(parentId)}, null); + c = getContentProviderClient().query(req_uri, null, + ProviderTableMeta.FILE_PARENT + "=?", + new String[]{String.valueOf(parentId)}, null); } catch (RemoteException e) { Log_OC.e(TAG, e.getMessage()); return ret; } } else { - c = getContentResolver().query(req_uri, null, - ProviderTableMeta.FILE_PARENT + "=?" , - new String[] { String.valueOf(parentId)}, null); + c = getContentResolver().query(req_uri, null, + ProviderTableMeta.FILE_PARENT + "=?", + new String[]{String.valueOf(parentId)}, null); } if (c.moveToFirst()) { @@ -774,8 +831,8 @@ public class FileDataStorageManager { return ret; } - - + + private OCFile createRootDir() { OCFile file = new OCFile(OCFile.ROOT_PATH); file.setMimetype("DIR"); @@ -793,7 +850,7 @@ public class FileDataStorageManager { cmp_key + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", - new String[] { value, mAccount.name }, null); + new String[]{value, mAccount.name}, null); } else { try { c = getContentProviderClient().query( @@ -801,7 +858,7 @@ public class FileDataStorageManager { null, cmp_key + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", - new String[] { value, mAccount.name }, null); + new String[]{value, mAccount.name}, null); } catch (RemoteException e) { Log_OC.e(TAG, "Couldn't determine file existance, assuming non existance: " @@ -823,14 +880,14 @@ public class FileDataStorageManager { key + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", - new String[] { value, mAccount.name }, null); + new String[]{value, mAccount.name}, null); } else { try { c = getContentProviderClient().query( ProviderTableMeta.CONTENT_URI, null, key + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER - + "=?", new String[] { value, mAccount.name }, + + "=?", new String[]{value, mAccount.name}, null); } catch (RemoteException e) { Log_OC.e(TAG, "Could not get file details: " + e.getMessage()); @@ -839,7 +896,7 @@ public class FileDataStorageManager { } return c; } - + private OCFile createFileInstance(Cursor c) { OCFile file = null; @@ -893,10 +950,11 @@ public class FileDataStorageManager { } return file; } - + /** * Returns if the file/folder is shared by link or not - * @param path Path of the file/folder + * + * @param path Path of the file/folder * @return */ public boolean isShareByLink(String path) { @@ -908,10 +966,11 @@ public class FileDataStorageManager { c.close(); return file.isShareByLink(); } - + /** * Returns the public link of the file/folder - * @param path Path of the file/folder + * + * @param path Path of the file/folder * @return */ public String getPublicLink(String path) { @@ -923,8 +982,8 @@ public class FileDataStorageManager { c.close(); return file.getPublicLink(); } - - + + // Methods for Shares public boolean saveShare(OCShare share) { boolean overriden = false; @@ -946,19 +1005,19 @@ public class FileDataStorageManager { cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId()); cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getIdRemoteShared()); cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name); - + if (shareExists(share.getIdRemoteShared())) { // for renamed files overriden = true; if (getContentResolver() != null) { getContentResolver().update(ProviderTableMeta.CONTENT_URI_SHARE, cv, ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?", - new String[] { String.valueOf(share.getIdRemoteShared()) }); + new String[]{String.valueOf(share.getIdRemoteShared())}); } else { try { getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_SHARE, cv, ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?", - new String[] { String.valueOf(share.getIdRemoteShared()) }); + new String[]{String.valueOf(share.getIdRemoteShared())}); } catch (RemoteException e) { Log_OC.e(TAG, "Fail to insert insert file to database " @@ -984,7 +1043,7 @@ public class FileDataStorageManager { long new_id = Long.parseLong(result_uri.getPathSegments() .get(1)); share.setId(new_id); - } + } } return overriden; @@ -1000,7 +1059,7 @@ public class FileDataStorageManager { ProviderTableMeta.OCSHARES_PATH + "=? AND " + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? AND " + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?", - new String[] { path, Integer.toString(type.getValue()), mAccount.name }, + new String[]{path, Integer.toString(type.getValue()), mAccount.name}, null); } else { try { @@ -1010,7 +1069,7 @@ public class FileDataStorageManager { ProviderTableMeta.OCSHARES_PATH + "=? AND " + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? AND " + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?", - new String[] { path, Integer.toString(type.getValue()), mAccount.name }, + new String[]{path, Integer.toString(type.getValue()), mAccount.name}, null); } catch (RemoteException e) { @@ -1025,7 +1084,7 @@ public class FileDataStorageManager { c.close(); return share; } - + private OCShare createShareInstance(Cursor c) { OCShare share = null; if (c != null) { @@ -1066,7 +1125,7 @@ public class FileDataStorageManager { cmp_key + "=? AND " + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?", - new String[] { value, mAccount.name }, null); + new String[]{value, mAccount.name}, null); } else { try { c = getContentProviderClient().query( @@ -1074,7 +1133,7 @@ public class FileDataStorageManager { null, cmp_key + "=? AND " + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?", - new String[] { value, mAccount.name }, null); + new String[]{value, mAccount.name}, null); } catch (RemoteException e) { Log_OC.e(TAG, "Couldn't determine file existance, assuming non existance: " @@ -1086,7 +1145,7 @@ public class FileDataStorageManager { c.close(); return retval; } - + private boolean shareExists(long remoteId) { return shareExists(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, String.valueOf(remoteId)); } @@ -1096,8 +1155,8 @@ public class FileDataStorageManager { cv.put(ProviderTableMeta.FILE_SHARE_BY_LINK, false); cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, ""); String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?"; - String [] whereArgs = new String[]{mAccount.name}; - + String[] whereArgs = new String[]{mAccount.name}; + if (getContentResolver() != null) { getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs); @@ -1117,7 +1176,7 @@ public class FileDataStorageManager { ContentValues cv = new ContentValues(); cv.put(ProviderTableMeta.FILE_SHARE_BY_LINK, false); cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, ""); - String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + + String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + ProviderTableMeta.FILE_PARENT + "=?"; String [] whereArgs = new String[] { mAccount.name , String.valueOf(folder.getFileId()) }; @@ -1138,8 +1197,8 @@ public class FileDataStorageManager { private void cleanShares() { String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?"; - String [] whereArgs = new String[]{mAccount.name}; - + String[] whereArgs = new String[]{mAccount.name}; + if (getContentResolver() != null) { getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE, where, whereArgs); @@ -1154,7 +1213,7 @@ public class FileDataStorageManager { } } } - + public void saveShares(Collection shares) { cleanShares(); if (shares != null) { @@ -1203,7 +1262,7 @@ public class FileDataStorageManager { ); } } - + // apply operations in batch if (operations.size() > 0) { @SuppressWarnings("unused") @@ -1219,21 +1278,21 @@ public class FileDataStorageManager { } else { results = getContentProviderClient().applyBatch(operations); } - + } catch (OperationApplicationException e) { Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage()); - + } catch (RemoteException e) { Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage()); } } } - + } - + public void updateSharedFiles(Collection sharedFiles) { cleanSharedFiles(); - + if (sharedFiles != null) { ArrayList operations = new ArrayList(sharedFiles.size()); @@ -1297,7 +1356,7 @@ public class FileDataStorageManager { ); } } - + // apply operations in batch if (operations.size() > 0) { @SuppressWarnings("unused") @@ -1313,21 +1372,21 @@ public class FileDataStorageManager { } else { results = getContentProviderClient().applyBatch(operations); } - + } catch (OperationApplicationException e) { Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage()); - + } catch (RemoteException e) { Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage()); } } } - - } - - public void removeShare(OCShare share){ + + } + + public void removeShare(OCShare share) { Uri share_uri = ProviderTableMeta.CONTENT_URI_SHARE; - String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?" + " AND " + + String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?" + " AND " + ProviderTableMeta.FILE_PATH + "=?"; String [] whereArgs = new String[]{mAccount.name, share.getPath()}; if (getContentProviderClient() != null) { @@ -1337,10 +1396,10 @@ public class FileDataStorageManager { e.printStackTrace(); } } else { - getContentResolver().delete(share_uri, where, whereArgs); + getContentResolver().delete(share_uri, where, whereArgs); } } - + public void saveSharesDB(ArrayList shares) { saveShares(shares); @@ -1351,7 +1410,7 @@ public class FileDataStorageManager { String path = share.getPath(); if (share.isFolder()) { path = path + FileUtils.PATH_SEPARATOR; - } + } // Update OCFile with data from share: ShareByLink and publicLink OCFile file = getFileByPath(path); @@ -1360,18 +1419,18 @@ public class FileDataStorageManager { file.setShareByLink(true); sharedFiles.add(file); } - } + } } - + updateSharedFiles(sharedFiles); } - + public void saveSharesInFolder(ArrayList shares, OCFile folder) { cleanSharedFilesInFolder(folder); ArrayList operations = new ArrayList(); operations = prepareRemoveSharesInFolder(folder, operations); - + if (shares != null) { // prepare operations to insert or update files to save in the given folder for (OCShare share : shares) { @@ -1415,7 +1474,7 @@ public class FileDataStorageManager { //} } } - + // apply operations in batch if (operations.size() > 0) { @SuppressWarnings("unused") @@ -1437,13 +1496,13 @@ public class FileDataStorageManager { } } //} - + } private ArrayList prepareRemoveSharesInFolder( OCFile folder, ArrayList preparedOperations) { if (folder != null) { - String where = ProviderTableMeta.OCSHARES_PATH + "=?" + " AND " + String where = ProviderTableMeta.OCSHARES_PATH + "=?" + " AND " + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?"; String [] whereArgs = new String[]{ "", mAccount.name }; diff --git a/src/com/owncloud/android/files/FileOperationsHelper.java b/src/com/owncloud/android/files/FileOperationsHelper.java index ddee2050..57b766ec 100644 --- a/src/com/owncloud/android/files/FileOperationsHelper.java +++ b/src/com/owncloud/android/files/FileOperationsHelper.java @@ -21,8 +21,6 @@ package com.owncloud.android.files; -import org.apache.http.protocol.HTTP; - import android.accounts.Account; import android.content.Intent; import android.net.Uri; @@ -35,7 +33,6 @@ import com.owncloud.android.authentication.AccountUtils; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder; import com.owncloud.android.files.services.FileUploader.FileUploaderBinder; - import com.owncloud.android.lib.common.network.WebdavUtils; import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.resources.status.OwnCloudVersion; @@ -44,20 +41,22 @@ import com.owncloud.android.services.observer.FileObserverService; import com.owncloud.android.ui.activity.FileActivity; import com.owncloud.android.ui.dialog.ShareLinkToDialog; +import org.apache.http.protocol.HTTP; + /** * */ public class FileOperationsHelper { private static final String TAG = FileOperationsHelper.class.getName(); - - private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG"; + + private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG"; protected FileActivity mFileActivity = null; /// Identifier of operation in progress which result shouldn't be lost private long mWaitingForOpId = Long.MAX_VALUE; - + public FileOperationsHelper(FileActivity fileActivity) { mFileActivity = fileActivity; } @@ -67,7 +66,7 @@ public class FileOperationsHelper { if (file != null) { String storagePath = file.getStoragePath(); String encodedStoragePath = WebdavUtils.encodePath(storagePath); - + Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW); intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype()); intentForSavedMimeType.setFlags( @@ -94,29 +93,29 @@ public class FileOperationsHelper { } else { chooserIntent = Intent.createChooser(intentForSavedMimeType, mFileActivity.getString(R.string.actionbar_open_with)); } - + mFileActivity.startActivity(chooserIntent); - + } else { Log_OC.wtf(TAG, "Trying to open a NULL OCFile"); } } - - + + public void shareFileWithLink(OCFile file) { - + if (isSharedSupported()) { if (file != null) { String link = "https://fake.url"; Intent intent = createShareWithLinkIntent(link); - String[] packagesToExclude = new String[] { mFileActivity.getPackageName() }; + String[] packagesToExclude = new String[]{mFileActivity.getPackageName()}; DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intent, packagesToExclude, file); chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG); - + } else { Log_OC.wtf(TAG, "Trying to share a NULL OCFile"); } - + } else { // Show a Message Toast t = Toast.makeText( @@ -125,13 +124,13 @@ public class FileOperationsHelper { t.show(); } } - + public void shareFileWithLinkToApp(OCFile file, String password, Intent sendIntent) { if (file != null) { mFileActivity.showLoadingDialog(); - + Intent service = new Intent(mFileActivity, OperationsService.class); service.setAction(OperationsService.ACTION_CREATE_SHARE); service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount()); @@ -144,18 +143,18 @@ public class FileOperationsHelper { Log_OC.wtf(TAG, "Trying to open a NULL OCFile"); } } - - + + private Intent createShareWithLinkIntent(String link) { Intent intentToShareLink = new Intent(Intent.ACTION_SEND); intentToShareLink.putExtra(Intent.EXTRA_TEXT, link); intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE); - return intentToShareLink; + return intentToShareLink; } - - + + /** - * @return 'True' if the server supports the Share API + * @return 'True' if the server supports the Share API */ public boolean isSharedSupported() { if (mFileActivity.getAccount() != null) { @@ -164,10 +163,10 @@ public class FileOperationsHelper { } return false; } - - + + public void unshareFileWithLink(OCFile file) { - + if (isSharedSupported()) { // Unshare the file Intent service = new Intent(mFileActivity, OperationsService.class); @@ -177,15 +176,15 @@ public class FileOperationsHelper { mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service); mFileActivity.showLoadingDialog(); - + } else { // Show a Message Toast t = Toast.makeText(mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG); t.show(); - + } } - + public void sendDownloadedFile(OCFile file) { if (file != null) { String storagePath = file.getStoragePath(); @@ -197,7 +196,7 @@ public class FileOperationsHelper { sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action // Show dialog, without the own app - String[] packagesToExclude = new String[] { mFileActivity.getPackageName() }; + String[] packagesToExclude = new String[]{mFileActivity.getPackageName()}; DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent, packagesToExclude, file); chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG); @@ -205,10 +204,10 @@ public class FileOperationsHelper { Log_OC.wtf(TAG, "Trying to send a NULL OCFile"); } } - - + + public void syncFile(OCFile file) { - + if (!file.isFolder()){ Intent intent = new Intent(mFileActivity, OperationsService.class); intent.setAction(OperationsService.ACTION_SYNC_FILE); @@ -269,8 +268,8 @@ public class FileOperationsHelper { mFileActivity.showLoadingDialog(); } - - + + public void createFolder(String remotePath, boolean createFullPath) { // Create Folder Intent service = new Intent(mFileActivity, OperationsService.class); @@ -317,8 +316,9 @@ public class FileOperationsHelper { /** * Start move file operation - * @param newfile File where it is going to be moved - * @param currentFile File with the previous info + * + * @param newfile File where it is going to be moved + * @param currentFile File with the previous info */ public void moveFile(OCFile newfile, OCFile currentFile) { // Move files @@ -332,6 +332,23 @@ public class FileOperationsHelper { mFileActivity.showLoadingDialog(); } + /** + * Start copy file operation + * + * @param newfile File where it is going to be moved + * @param currentFile File with the previous info + */ + public void copyFile(OCFile newfile, OCFile currentFile) { + // Copy files + Intent service = new Intent(mFileActivity, OperationsService.class); + service.setAction(OperationsService.ACTION_COPY_FILE); + service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath()); + service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath()); + service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount()); + mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service); + + mFileActivity.showLoadingDialog(); + } public long getOpIdWaitingFor() { return mWaitingForOpId; @@ -341,7 +358,7 @@ public class FileOperationsHelper { public void setOpIdWaitingFor(long waitingForOpId) { mWaitingForOpId = waitingForOpId; } - + /** * @return 'True' if the server doesn't need to check forbidden characters */ diff --git a/src/com/owncloud/android/operations/CopyFileOperation.java b/src/com/owncloud/android/operations/CopyFileOperation.java index dca51e30..6f0f9081 100644 --- a/src/com/owncloud/android/operations/CopyFileOperation.java +++ b/src/com/owncloud/android/operations/CopyFileOperation.java @@ -1,4 +1,103 @@ +/* ownCloud Android client application + * Copyright (C) 2012-2014 ownCloud Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + package com.owncloud.android.operations; -public class CopyFileOperation { +import android.accounts.Account; +import android.util.Log; + +import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.lib.common.OwnCloudClient; +import com.owncloud.android.lib.common.operations.RemoteOperationResult; +import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; +import com.owncloud.android.lib.resources.files.CopyRemoteFileOperation; +import com.owncloud.android.operations.common.SyncOperation; + + +/** + * Operation copying an {@link OCFile} to a different folder. + * + * @author David A. Velasco + */ +public class CopyFileOperation extends SyncOperation { + + //private static final String TAG = MoveFileOperation.class.getSimpleName(); + + private String mSrcPath; + private String mTargetParentPath; + + private OCFile mFile; + + + /** + * Constructor + * + * @param srcPath Remote path of the {@link OCFile} to move. + * @param targetParentPath Path to the folder where the file will be copied into. + * @param account OwnCloud account containing both the file and the target folder + */ + public CopyFileOperation(String srcPath, String targetParentPath, Account account) { + mSrcPath = srcPath; + mTargetParentPath = targetParentPath; + if (!mTargetParentPath.endsWith(OCFile.PATH_SEPARATOR)) { + mTargetParentPath += OCFile.PATH_SEPARATOR; + } + + mFile = null; + } + + /** + * Performs the operation. + * + * @param client Client object to communicate with the remote ownCloud server. + */ + @Override + protected RemoteOperationResult run(OwnCloudClient client) { + RemoteOperationResult result; + + /// 1. check copy validity + if (mTargetParentPath.startsWith(mSrcPath)) { + return new RemoteOperationResult(ResultCode.INVALID_MOVE_INTO_DESCENDANT); + } + mFile = getStorageManager().getFileByPath(mSrcPath); + if (mFile == null) { + return new RemoteOperationResult(ResultCode.FILE_NOT_FOUND); + } + + /// 2. remote copy + String targetPath = mTargetParentPath + mFile.getFileName(); + if (mFile.isFolder()) { + targetPath += OCFile.PATH_SEPARATOR; + } + CopyRemoteFileOperation operation = new CopyRemoteFileOperation( + mSrcPath, + targetPath, + false + ); + result = operation.execute(client); + + /// 3. local copy + if (result.isSuccess()) { + getStorageManager().copyLocalFile(mFile, targetPath); + } + // TODO handle ResultCode.PARTIAL_COPY_DONE in client Activity, for the moment + + return result; + } + + } diff --git a/src/com/owncloud/android/services/OperationsService.java b/src/com/owncloud/android/services/OperationsService.java index 2514c4de..f7034f0a 100644 --- a/src/com/owncloud/android/services/OperationsService.java +++ b/src/com/owncloud/android/services/OperationsService.java @@ -19,11 +19,21 @@ package com.owncloud.android.services; -import java.io.IOException; -import java.util.Iterator; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.ConcurrentMap; +import android.accounts.Account; +import android.accounts.AccountsException; +import android.accounts.AuthenticatorException; +import android.accounts.OperationCanceledException; +import android.app.Service; +import android.content.Intent; +import android.net.Uri; +import android.os.Binder; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.IBinder; +import android.os.Looper; +import android.os.Message; +import android.os.Process; +import android.util.Pair; import com.owncloud.android.MainApp; import com.owncloud.android.R; @@ -42,7 +52,7 @@ import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.resources.shares.ShareType; import com.owncloud.android.lib.resources.status.OwnCloudVersion; import com.owncloud.android.lib.resources.users.GetRemoteUserNameOperation; -import com.owncloud.android.operations.common.SyncOperation; +import com.owncloud.android.operations.CopyFileOperation; import com.owncloud.android.operations.CreateFolderOperation; import com.owncloud.android.operations.CreateShareOperation; import com.owncloud.android.operations.GetServerInfoOperation; @@ -53,28 +63,18 @@ import com.owncloud.android.operations.RenameFileOperation; import com.owncloud.android.operations.SynchronizeFileOperation; import com.owncloud.android.operations.SynchronizeFolderOperation; import com.owncloud.android.operations.UnshareLinkOperation; +import com.owncloud.android.operations.common.SyncOperation; -import android.accounts.Account; -import android.accounts.AccountManager; -import android.accounts.AccountsException; -import android.accounts.AuthenticatorException; -import android.accounts.OperationCanceledException; -import android.app.Service; -import android.content.Intent; -import android.net.Uri; -import android.os.Binder; -import android.os.Handler; -import android.os.HandlerThread; -import android.os.IBinder; -import android.os.Looper; -import android.os.Message; -import android.os.Process; -import android.util.Pair; +import java.io.IOException; +import java.util.Iterator; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ConcurrentMap; public class OperationsService extends Service { - + private static final String TAG = OperationsService.class.getSimpleName(); - + public static final String EXTRA_ACCOUNT = "ACCOUNT"; public static final String EXTRA_SERVER_URL = "SERVER_URL"; public static final String EXTRA_OAUTH2_QUERY_PARAMETERS = "OAUTH2_QUERY_PARAMETERS"; @@ -90,7 +90,7 @@ public class OperationsService extends Service { public static final String EXTRA_PASSWORD_SHARE = "PASSWORD_SHARE"; public static final String EXTRA_COOKIE = "COOKIE"; - + public static final String ACTION_CREATE_SHARE = "CREATE_SHARE"; public static final String ACTION_UNSHARE = "UNSHARE"; public static final String ACTION_GET_SERVER_INFO = "GET_SERVER_INFO"; @@ -102,22 +102,24 @@ public class OperationsService extends Service { public static final String ACTION_SYNC_FILE = "SYNC_FILE"; public static final String ACTION_SYNC_FOLDER = "SYNC_FOLDER";//for the moment, just to download public static final String ACTION_MOVE_FILE = "MOVE_FILE"; - + public static final String ACTION_COPY_FILE = "COPY_FILE"; + public static final String ACTION_OPERATION_ADDED = OperationsService.class.getName() + ".OPERATION_ADDED"; public static final String ACTION_OPERATION_FINISHED = OperationsService.class.getName() + ".OPERATION_FINISHED"; - private ConcurrentMap> - mUndispatchedFinishedOperations = + + private ConcurrentMap> + mUndispatchedFinishedOperations = new ConcurrentHashMap>(); - + private static class Target { public Uri mServerUrl = null; public Account mAccount = null; public String mCookie = null; - + public Target(Account account, Uri serverUrl, String cookie) { mAccount = account; mServerUrl = serverUrl; @@ -151,11 +153,11 @@ public class OperationsService extends Service { mSyncFolderHandler = new SyncFolderHandler(thread.getLooper(), this); } - + /** * Entry point to add a new operation to the queue of operations. - * - * New operations are added calling to startService(), resulting in a call to this method. + *

+ * New operations are added calling to startService(), resulting in a call to this method. * This ensures the service will keep on working although the caller activity goes away. */ @Override @@ -200,19 +202,13 @@ public class OperationsService extends Service { // Saving cookies try { OwnCloudClientManagerFactory.getDefaultSingleton(). - saveAllClients(this, MainApp.getAccountType()); - + saveAllClients(this, MainApp.getAccountType()); + // TODO - get rid of these exceptions - } catch (AccountNotFoundException e) { - e.printStackTrace(); - } catch (AuthenticatorException e) { - e.printStackTrace(); - } catch (OperationCanceledException e) { - e.printStackTrace(); - } catch (IOException e) { + } catch (AccountNotFoundException | AuthenticatorException | OperationCanceledException | IOException e) { e.printStackTrace(); } - + mUndispatchedFinishedOperations.clear(); mOperationsBinder = null; @@ -227,8 +223,8 @@ public class OperationsService extends Service { } /** - * Provides a binder object that clients can use to perform actions on the queue of operations, - * except the addition of new operations. + * Provides a binder object that clients can use to perform actions on the queue of operations, + * except the addition of new operations. */ @Override public IBinder onBind(Intent intent) { @@ -236,7 +232,7 @@ public class OperationsService extends Service { return mOperationsBinder; } - + /** * Called when ALL the bound clients were unbound. */ @@ -248,19 +244,19 @@ public class OperationsService extends Service { /** - * Binder to let client components to perform actions on the queue of operations. - * - * It provides by itself the available operations. + * Binder to let client components to perform actions on the queue of operations. + *

+ * It provides by itself the available operations. */ public class OperationsServiceBinder extends Binder /* implements OnRemoteOperationListener */ { - + /** * Map of listeners that will be reported about the end of operations from a * {@link OperationsServiceBinder} instance */ - private ConcurrentMap mBoundListeners = + private final ConcurrentMap mBoundListeners = new ConcurrentHashMap(); - + private ServiceHandler mServiceHandler = null; public OperationsServiceBinder(ServiceHandler serviceHandler) { @@ -280,14 +276,14 @@ public class OperationsService extends Service { public void clearListeners() { - + mBoundListeners.clear(); } - + /** * Adds a listener interested in being reported about the end of operations. - * + * * @param listener Object to notify about the end of operations. * @param callbackHandler {@link Handler} to access the listener without * breaking Android threading protection. @@ -298,15 +294,15 @@ public class OperationsService extends Service { mBoundListeners.put(listener, callbackHandler); } } - - + + /** * Removes a listener from the list of objects interested in the being reported about * the end of operations. * * @param listener Object to notify about progress of transfer. */ - public void removeOperationListener (OnRemoteOperationListener listener) { + public void removeOperationListener(OnRemoteOperationListener listener) { synchronized (mBoundListeners) { mBoundListeners.remove(listener); } @@ -314,7 +310,7 @@ public class OperationsService extends Service { /** - * TODO - IMPORTANT: update implementation when more operations are moved into the service + * TODO - IMPORTANT: update implementation when more operations are moved into the service * * @return 'True' when an operation that enforces the user to wait for completion is * in process. @@ -343,7 +339,7 @@ public class OperationsService extends Service { return Long.MAX_VALUE; } } - + public boolean dispatchResultIfFinished(int operationId, OnRemoteOperationListener listener) { @@ -378,7 +374,7 @@ public class OperationsService extends Service { /** - * Operations worker. Performs the pending operations in the order they were requested. + * Operations worker. Performs the pending operations in the order they were requested. * * Created with the Looper of a new thread, started in {@link OperationsService#onCreate()}. */ @@ -388,7 +384,7 @@ public class OperationsService extends Service { OperationsService mService; - + private ConcurrentLinkedQueue> mPendingOperations = new ConcurrentLinkedQueue>(); @@ -412,7 +408,7 @@ public class OperationsService extends Service { Log_OC.d(TAG, "Stopping after command with id " + msg.arg1); mService.stopSelf(msg.arg1); } - + /** * Performs the next operation in the queue @@ -517,7 +513,7 @@ public class OperationsService extends Service { } - + /** * Creates a new operation, as described by operationIntent. @@ -626,7 +622,13 @@ public class OperationsService extends Service { // Move file/folder String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH); String newParentPath = operationIntent.getStringExtra(EXTRA_NEW_PARENT_PATH); - operation = new MoveFileOperation(remotePath,newParentPath,account); + operation = new MoveFileOperation(remotePath, newParentPath, account); + + } else if (action.equals(ACTION_COPY_FILE)) { + // Copy file/folder + String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH); + String newParentPath = operationIntent.getStringExtra(EXTRA_NEW_PARENT_PATH); + operation = new CopyFileOperation(remotePath, newParentPath, account); } } @@ -642,11 +644,11 @@ public class OperationsService extends Service { return null; } } - + /** * Sends a broadcast when a new operation is added to the queue. - * + * * Local broadcasts are only delivered to activities in the same process, but can't be * done sticky :\ * @@ -656,43 +658,43 @@ public class OperationsService extends Service { private void sendBroadcastNewOperation(Target target, RemoteOperation operation) { Intent intent = new Intent(ACTION_OPERATION_ADDED); if (target.mAccount != null) { - intent.putExtra(EXTRA_ACCOUNT, target.mAccount); + intent.putExtra(EXTRA_ACCOUNT, target.mAccount); } else { - intent.putExtra(EXTRA_SERVER_URL, target.mServerUrl); + intent.putExtra(EXTRA_SERVER_URL, target.mServerUrl); } //LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this); //lbm.sendBroadcast(intent); sendStickyBroadcast(intent); } - + // TODO - maybe add a notification for real start of operations - + /** * Sends a LOCAL broadcast when an operations finishes in order to the interested activities c * an update their view * * Local broadcasts are only delivered to activities in the same process. - * - * @param target Account or URL pointing to an OC server. - * @param operation Finished operation. - * @param result Result of the operation. + * + * @param target Account or URL pointing to an OC server. + * @param operation Finished operation. + * @param result Result of the operation. */ private void sendBroadcastOperationFinished(Target target, RemoteOperation operation, RemoteOperationResult result) { Intent intent = new Intent(ACTION_OPERATION_FINISHED); intent.putExtra(EXTRA_RESULT, result); if (target.mAccount != null) { - intent.putExtra(EXTRA_ACCOUNT, target.mAccount); + intent.putExtra(EXTRA_ACCOUNT, target.mAccount); } else { - intent.putExtra(EXTRA_SERVER_URL, target.mServerUrl); + intent.putExtra(EXTRA_SERVER_URL, target.mServerUrl); } //LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this); //lbm.sendBroadcast(intent); sendStickyBroadcast(intent); } - + /** * Notifies the currently subscribed listeners about the end of an operation. * @@ -720,9 +722,9 @@ public class OperationsService extends Service { } if (count == 0) { //mOperationResults.put(operation.hashCode(), result); - Pair undispatched = - new Pair(operation, result); - mUndispatchedFinishedOperations.put(operation.hashCode(), undispatched); + Pair undispatched = + new Pair<>(operation, result); + mUndispatchedFinishedOperations.put(((Runnable) operation).hashCode(), undispatched); } Log_OC.d(TAG, "Called " + count + " listeners"); } diff --git a/src/com/owncloud/android/ui/activity/CopyActivity.java b/src/com/owncloud/android/ui/activity/CopyActivity.java index e0317137..c7c4a6ca 100644 --- a/src/com/owncloud/android/ui/activity/CopyActivity.java +++ b/src/com/owncloud/android/ui/activity/CopyActivity.java @@ -20,7 +20,6 @@ package com.owncloud.android.ui.activity; import android.accounts.Account; import android.accounts.AccountManager; import android.accounts.AuthenticatorException; -import android.accounts.OperationCanceledException; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -54,7 +53,6 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCo import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.operations.CreateFolderOperation; import com.owncloud.android.operations.RefreshFolderOperation; -import com.owncloud.android.operations.SynchronizeFolderOperation; import com.owncloud.android.syncadapter.FileSyncAdapter; import com.owncloud.android.ui.dialog.CreateFolderDialogFragment; import com.owncloud.android.ui.fragment.FileFragment; @@ -62,15 +60,13 @@ import com.owncloud.android.ui.fragment.OCFileListFragment; import com.owncloud.android.utils.DisplayUtils; import com.owncloud.android.utils.ErrorMessageAdapter; -import java.io.IOException; - public class CopyActivity extends HookActivity implements FileFragment.ContainerActivity, OnClickListener, SwipeRefreshLayout.OnRefreshListener { public static final String EXTRA_CURRENT_FOLDER = UploadFilesActivity.class.getCanonicalName() + ".EXTRA_CURRENT_FOLDER"; public static final String EXTRA_TARGET_FILE = UploadFilesActivity.class.getCanonicalName() + "EXTRA_TARGET_FILE"; - public static final int RESULT_OK_AND_MOVE = 1; + public static final int RESULT_OK_AND_COPY = 1; private SyncBroadcastReceiver mSyncBroadcastReceiver; @@ -371,12 +367,12 @@ public class CopyActivity extends HookActivity implements FileFragment.Container finish(); } else if (v == mChooseBtn) { Intent i = getIntent(); - OCFile targetFile = i.getParcelableExtra(FolderPickerActivity.EXTRA_FILE); + OCFile targetFile = i.getParcelableExtra(CopyActivity.EXTRA_TARGET_FILE); Intent data = new Intent(); data.putExtra(EXTRA_CURRENT_FOLDER, getCurrentFolder()); data.putExtra(EXTRA_TARGET_FILE, targetFile); - setResult(RESULT_OK_AND_MOVE, data); + setResult(RESULT_OK_AND_COPY, data); finish(); } } diff --git a/src/com/owncloud/android/ui/activity/FileActivity.java b/src/com/owncloud/android/ui/activity/FileActivity.java index 4e38c523..63f700c3 100644 --- a/src/com/owncloud/android/ui/activity/FileActivity.java +++ b/src/com/owncloud/android/ui/activity/FileActivity.java @@ -76,8 +76,6 @@ import com.owncloud.android.ui.NavigationDrawerItem; import com.owncloud.android.ui.adapter.NavigationDrawerListAdapter; import com.owncloud.android.ui.dialog.LoadingDialog; import com.owncloud.android.ui.dialog.SharePasswordDialogFragment; -import com.owncloud.android.ui.fragment.FileDetailFragment; -import com.owncloud.android.ui.fragment.FileFragment; import com.owncloud.android.utils.ErrorMessageAdapter; import java.util.ArrayList; @@ -473,7 +471,7 @@ public class FileActivity extends ActionBarActivity */ private void swapToDefaultAccount() { // default to the most recently used account - Account newAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext()); + Account newAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext()); if (newAccount == null) { /// no account available: force account creation createFirstAccount(); @@ -561,7 +559,7 @@ public class FileActivity extends ActionBarActivity } /** - * @return 'True' when the Activity is finishing to enforce the setup of a new account. + * @return 'True' when the Activity is finishing to enforce the setup of a new account. */ protected boolean isRedirectingToSetupAccount() { return mRedirectingToSetupAccount; @@ -816,7 +814,7 @@ public class FileActivity extends ActionBarActivity /** * Dismiss loading dialog */ - public void dismissLoadingDialog(){ + public void dismissLoadingDialog() { Fragment frag = getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG); if (frag != null) { LoadingDialog loading = (LoadingDialog) frag; diff --git a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java index 699107c9..ee812547 100644 --- a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -74,6 +74,7 @@ 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.CopyFileOperation; import com.owncloud.android.operations.CreateFolderOperation; import com.owncloud.android.operations.CreateShareOperation; import com.owncloud.android.operations.MoveFileOperation; @@ -103,9 +104,6 @@ import com.owncloud.android.utils.UriUtils; import java.io.File; -import java.io.File; -import java.io.IOException; - /** * Displays, what files the user has available in his ownCloud. @@ -621,6 +619,21 @@ public class FileDisplayActivity extends HookActivity DELAY_TO_REQUEST_OPERATION_ON_ACTIVITY_RESULTS ); + } else if (requestCode == ACTION_COPY_FILES && (resultCode == RESULT_OK || + resultCode == CopyActivity.RESULT_OK_AND_COPY)) { + + final Intent fData = data; + final int fResultCode = resultCode; + getHandler().postDelayed( + new Runnable() { + @Override + public void run() { + requestCopyOperation(fData, fResultCode); + } + }, + DELAY_TO_REQUEST_OPERATION_ON_ACTIVITY_RESULTS + ); + } else { super.onActivityResult(requestCode, resultCode, data); } @@ -736,6 +749,18 @@ public class FileDisplayActivity extends HookActivity getFileOperationsHelper().moveFile(folderToMoveAt, targetFile); } + /** + * Request the operation for copying the file/folder from one path to another + * + * @param data Intent received + * @param resultCode Result code received + */ + private void requestCopyOperation(Intent data, int resultCode) { + OCFile folderToMoveAt = data.getParcelableExtra(CopyActivity.EXTRA_CURRENT_FOLDER); + OCFile targetFile = data.getParcelableExtra(CopyActivity.EXTRA_TARGET_FILE); + getFileOperationsHelper().copyFile(folderToMoveAt, targetFile); + } + @Override public void onBackPressed() { OCFileListFragment listOfFiles = getListOfFilesFragment(); @@ -1290,6 +1315,9 @@ public class FileDisplayActivity extends HookActivity } else if (operation instanceof MoveFileOperation) { onMoveFileOperationFinish((MoveFileOperation) operation, result); + + } else if (operation instanceof CopyFileOperation) { + onCopyFileOperationFinish((CopyFileOperation) operation, result); } } @@ -1398,6 +1426,30 @@ public class FileDisplayActivity extends HookActivity } } + /** + * Updates the view associated to the activity after the finish of an operation trying to copy a + * file. + * + * @param operation Copy operation performed. + * @param result Result of the copy operation. + */ + private void onCopyFileOperationFinish(CopyFileOperation operation, RemoteOperationResult result) { + if (result.isSuccess()) { + dismissLoadingDialog(); + refreshListOfFilesFragment(); + } else { + dismissLoadingDialog(); + try { + Toast msg = Toast.makeText(FileDisplayActivity.this, + ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()), + Toast.LENGTH_LONG); + msg.show(); + + } catch (NotFoundException e) { + Log_OC.e(TAG, "Error while trying to show fail message ", e); + } + } + } /** * Updates the view associated to the activity after the finish of an operation trying to rename diff --git a/src/com/owncloud/android/ui/fragment/OCFileListFragment.java b/src/com/owncloud/android/ui/fragment/OCFileListFragment.java index 365d2a01..6e4d7565 100644 --- a/src/com/owncloud/android/ui/fragment/OCFileListFragment.java +++ b/src/com/owncloud/android/ui/fragment/OCFileListFragment.java @@ -354,7 +354,7 @@ public class OCFileListFragment extends ExtendedListFragment { Intent action = new Intent(getActivity(), CopyActivity.class); // Pass mTargetFile that contains info of selected file/folder - action.putExtra(FolderPickerActivity.EXTRA_FILE, mTargetFile); + action.putExtra(CopyActivity.EXTRA_TARGET_FILE, mTargetFile); getActivity().startActivityForResult(action, FileDisplayActivity.ACTION_COPY_FILES); return true; default: diff --git a/src/com/owncloud/android/utils/ErrorMessageAdapter.java b/src/com/owncloud/android/utils/ErrorMessageAdapter.java index 75736e53..c86346b3 100644 --- a/src/com/owncloud/android/utils/ErrorMessageAdapter.java +++ b/src/com/owncloud/android/utils/ErrorMessageAdapter.java @@ -21,17 +21,13 @@ package com.owncloud.android.utils; -import java.io.File; -import java.net.SocketTimeoutException; - -import org.apache.commons.httpclient.ConnectTimeoutException; - import android.content.res.Resources; import com.owncloud.android.R; 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.operations.CopyFileOperation; import com.owncloud.android.operations.CreateFolderOperation; import com.owncloud.android.operations.CreateShareOperation; import com.owncloud.android.operations.DownloadFileOperation; @@ -43,6 +39,11 @@ import com.owncloud.android.operations.SynchronizeFolderOperation; import com.owncloud.android.operations.UnshareLinkOperation; import com.owncloud.android.operations.UploadFileOperation; +import org.apache.commons.httpclient.ConnectTimeoutException; + +import java.io.File; +import java.net.SocketTimeoutException; + /** * Class to choose proper error messages to show to the user depending on the results of operations, * always following the same policy @@ -51,16 +52,16 @@ import com.owncloud.android.operations.UploadFileOperation; public class ErrorMessageAdapter { public ErrorMessageAdapter() { - + } public static String getErrorCauseMessage(RemoteOperationResult result, RemoteOperation operation, Resources res) { String message = null; - + if (operation instanceof UploadFileOperation) { - + if (result.isSuccess()) { message = String.format( res.getString(R.string.uploader_upload_succeeded_content_single), @@ -76,7 +77,7 @@ public class ErrorMessageAdapter { } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) { message = res.getString(R.string.failed_upload_quota_exceeded_text); */ - + } else if (result.getCode() == ResultCode.FORBIDDEN) { message = String.format(res.getString(R.string.forbidden_permissions), res.getString(R.string.uploader_upload_forbidden_permissions)); @@ -90,14 +91,14 @@ public class ErrorMessageAdapter { ((UploadFileOperation) operation).getFileName()); } } - + } else if (operation instanceof DownloadFileOperation) { - + if (result.isSuccess()) { message = String.format( res.getString(R.string.downloader_download_succeeded_content), new File(((DownloadFileOperation) operation).getSavePath()).getName()); - + } else { if (result.getCode() == ResultCode.FILE_NOT_FOUND) { message = res.getString(R.string.downloader_download_file_not_found); @@ -108,11 +109,11 @@ public class ErrorMessageAdapter { ((DownloadFileOperation) operation).getSavePath()).getName()); } } - + } else if (operation instanceof RemoveFileOperation) { if (result.isSuccess()) { message = res.getString(R.string.remove_success_msg); - + } else { if (result.getCode().equals(ResultCode.FORBIDDEN)) { // Error --> No permissions @@ -120,7 +121,7 @@ public class ErrorMessageAdapter { res.getString(R.string.forbidden_permissions_delete)); } else if (isNetworkError(result.getCode())) { message = getErrorMessage(result, res); - + } else { message = res.getString(R.string.remove_fail_msg); } @@ -140,19 +141,19 @@ public class ErrorMessageAdapter { } else if (isNetworkError(result.getCode())) { message = getErrorMessage(result, res); - + } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) { message = res.getString(R.string.filename_forbidden_charaters_from_server); } else { - message = res.getString(R.string.rename_server_fail_msg); + message = res.getString(R.string.rename_server_fail_msg); } - + } else if (operation instanceof SynchronizeFileOperation) { if (!((SynchronizeFileOperation) operation).transferWasRequested()) { message = res.getString(R.string.sync_file_nothing_to_do_msg); } - + } else if (operation instanceof CreateFolderOperation) { if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) { message = res.getString(R.string.filename_forbidden_characters); @@ -163,7 +164,7 @@ public class ErrorMessageAdapter { } else if (isNetworkError(result.getCode())) { message = getErrorMessage(result, res); - + } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) { message = res.getString(R.string.filename_forbidden_charaters_from_server); } else { @@ -172,7 +173,7 @@ public class ErrorMessageAdapter { } else if (operation instanceof CreateShareOperation) { if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND message = res.getString(R.string.share_link_file_no_exist); - + } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) { // Error --> No permissions message = String.format(res.getString(R.string.forbidden_permissions), @@ -180,17 +181,17 @@ public class ErrorMessageAdapter { } else if (isNetworkError(result.getCode())) { message = getErrorMessage(result, res); - + } else { // Generic error // Show a Message, operation finished without success message = res.getString(R.string.share_link_file_error); } - + } else if (operation instanceof UnshareLinkOperation) { - + if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND message = res.getString(R.string.unshare_link_file_no_exist); - + } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) { // Error --> No permissions message = String.format(res.getString(R.string.forbidden_permissions), @@ -198,7 +199,7 @@ public class ErrorMessageAdapter { } else if (isNetworkError(result.getCode())) { message = getErrorMessage(result, res); - + } else { // Generic error // Show a Message, operation finished without success message = res.getString(R.string.unshare_link_file_error); @@ -207,8 +208,8 @@ public class ErrorMessageAdapter { if (result.getCode() == ResultCode.FILE_NOT_FOUND) { message = res.getString(R.string.move_file_not_found); - - } else if (result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT) { + + } else if (result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT) { message = res.getString(R.string.move_file_invalid_into_descendent); } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) { @@ -240,44 +241,62 @@ public class ErrorMessageAdapter { folderPathName); } } + } else if (operation instanceof CopyFileOperation) { + + if (result.getCode() == ResultCode.FILE_NOT_FOUND) { + message = res.getString(R.string.copy_file_not_found); + + } else if (result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT) { + message = res.getString(R.string.copy_file_invalid_into_descendent); + + } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) { + message = res.getString(R.string.copy_file_invalid_overwrite); + + } else if (result.getCode() == ResultCode.FORBIDDEN) { + message = String.format(res.getString(R.string.forbidden_permissions), + res.getString(R.string.forbidden_permissions_copy)); + + } else { // Generic error + // Show a Message, operation finished without success + message = res.getString(R.string.copy_file_error); + } } - + return message; } - - private static String getErrorMessage(RemoteOperationResult result , Resources res) { - + + private static String getErrorMessage(RemoteOperationResult result, Resources res) { + String message = null; - + if (!result.isSuccess()) { - + if (result.getCode() == ResultCode.WRONG_CONNECTION) { message = res.getString(R.string.network_error_socket_exception); - + } else if (result.getCode() == ResultCode.TIMEOUT) { message = res.getString(R.string.network_error_socket_exception); - + if (result.getException() instanceof SocketTimeoutException) { message = res.getString(R.string.network_error_socket_timeout_exception); - } else if(result.getException() instanceof ConnectTimeoutException) { + } else if (result.getException() instanceof ConnectTimeoutException) { message = res.getString(R.string.network_error_connect_timeout_exception); - } - + } + } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) { message = res.getString(R.string.network_host_not_available); } } - + return message; } - + private static boolean isNetworkError(RemoteOperationResult.ResultCode code) { - if (code == ResultCode.WRONG_CONNECTION || - code == ResultCode.TIMEOUT || + if (code == ResultCode.WRONG_CONNECTION || + code == ResultCode.TIMEOUT || code == ResultCode.HOST_NOT_AVAILABLE) { return true; - } - else + } else return false; } }