Search in sources :

Example 1 with OperationCancelledException

use of com.owncloud.android.lib.common.operations.OperationCancelledException in project android by owncloud.

the class SynchronizeFolderOperation method syncContents.

/**
     * Performs a list of synchronization operations, determining if a download or upload is needed
     * or if exists conflict due to changes both in local and remote contents of the each file.
     *
     * If download or upload is needed, request the operation to the corresponding service and goes
     * on.
     */
private void syncContents() throws OperationCancelledException {
    Log_OC.v(TAG, "Starting content synchronization... ");
    RemoteOperationResult contentsResult;
    for (SyncOperation op : mFilesToSyncContents) {
        if (mCancellationRequested.get()) {
            throw new OperationCancelledException();
        }
        contentsResult = op.execute(getStorageManager(), mContext);
        if (!contentsResult.isSuccess()) {
            if (contentsResult.getCode() == ResultCode.SYNC_CONFLICT) {
                mConflictsFound++;
            } else {
                mFailsInFileSyncsFound++;
                if (contentsResult.getException() != null) {
                    Log_OC.e(TAG, // Vs " av-off file"
                    "Error while synchronizing file : " + contentsResult.getLogMessage(), contentsResult.getException());
                } else {
                    Log_OC.e(TAG, "Error while synchronizing file : " + contentsResult.getLogMessage());
                }
            }
        }
    // won't let these fails break the synchronization process
    }
    for (Intent intent : mFoldersToSyncContents) {
        if (mCancellationRequested.get()) {
            throw new OperationCancelledException();
        }
        mContext.startService(intent);
    }
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) SyncOperation(com.owncloud.android.operations.common.SyncOperation) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) Intent(android.content.Intent)

Example 2 with OperationCancelledException

use of com.owncloud.android.lib.common.operations.OperationCancelledException in project android by owncloud.

the class SynchronizeFolderOperation method fetchRemoteFolder.

/**
     * Get list of files in folder from remote server.
     *
     * @param client      {@link OwnCloudClient} instance used to access the server.
     * @return            Result of the fetch, including list of remote files in the sync'ed folder.
     * @throws OperationCancelledException
     */
@NonNull
private RemoteOperationResult fetchRemoteFolder(OwnCloudClient client) throws OperationCancelledException {
    Log_OC.d(TAG, "Fetching list of files in  " + mAccount.name + mRemotePath + ", if changed");
    if (mCancellationRequested.get()) {
        throw new OperationCancelledException();
    }
    ReadRemoteFolderOperation readFolderOperation = new ReadRemoteFolderOperation(mRemotePath);
    return readFolderOperation.execute(client);
}
Also used : ReadRemoteFolderOperation(com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) NonNull(android.support.annotation.NonNull)

Example 3 with OperationCancelledException

use of com.owncloud.android.lib.common.operations.OperationCancelledException in project android by owncloud.

the class SynchronizeFolderOperation method mergeRemoteFolder.

/**
     *  Synchronizes the data retrieved from the server about the contents of the target folder
     *  with the current data in the local database.
     *
     *  Grants that mFoldersToVisit is updated with fresh data after execution.
     *
     *  @param folderAndFiles   Remote folder and children files in folder
     */
private void mergeRemoteFolder(ArrayList<Object> folderAndFiles) throws OperationCancelledException {
    Log_OC.d(TAG, "Synchronizing " + mAccount.name + mRemotePath);
    FileDataStorageManager storageManager = getStorageManager();
    // parse data from remote folder
    OCFile updatedFolder = FileStorageUtils.createOCFileFrom((RemoteFile) folderAndFiles.get(0));
    // NOTE: updates ETag with remote value; that's INTENDED
    updatedFolder.copyLocalPropertiesFrom(mLocalFolder);
    Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath() + " changed - starting update of local data ");
    List<OCFile> updatedFiles = new Vector<>(folderAndFiles.size() - 1);
    mFoldersToVisit = new Vector<>(folderAndFiles.size() - 1);
    mFilesToSyncContents.clear();
    if (mCancellationRequested.get()) {
        throw new OperationCancelledException();
    }
    // get current data about local contents of the folder to synchronize
    List<OCFile> localFiles = storageManager.getFolderContent(mLocalFolder);
    Map<String, OCFile> localFilesMap = new HashMap<>(localFiles.size());
    for (OCFile file : localFiles) {
        localFilesMap.put(file.getRemotePath(), file);
    }
    // loop to synchronize every child
    OCFile remoteFile, localFile, updatedLocalFile;
    RemoteFile r;
    int foldersToExpand = 0;
    for (int i = 1; i < folderAndFiles.size(); i++) {
        /// new OCFile instance with the data from the server
        r = (RemoteFile) folderAndFiles.get(i);
        remoteFile = FileStorageUtils.createOCFileFrom(r);
        /// new OCFile instance to merge fresh data from server with local state
        updatedLocalFile = FileStorageUtils.createOCFileFrom(r);
        /// retrieve local data for the read file
        localFile = localFilesMap.remove(remoteFile.getRemotePath());
        /// add to updatedFile data about LOCAL STATE (not existing in server)
        updatedLocalFile.setLastSyncDateForProperties(mCurrentSyncTime);
        if (localFile != null) {
            updatedLocalFile.copyLocalPropertiesFrom(localFile);
            // remote eTag will not be set unless file CONTENTS are synchronized
            updatedLocalFile.setEtag(localFile.getEtag());
            if (!updatedLocalFile.isFolder() && remoteFile.isImage() && remoteFile.getModificationTimestamp() != localFile.getModificationTimestamp()) {
                updatedLocalFile.setNeedsUpdateThumbnail(true);
            }
        } else {
            updatedLocalFile.setParentId(mLocalFolder.getFileId());
            // remote eTag will not be set unless file CONTENTS are synchronized
            updatedLocalFile.setEtag("");
            // new files need to check av-off status of parent folder!
            if (updatedFolder.isAvailableOffline()) {
                updatedLocalFile.setAvailableOfflineStatus(OCFile.AvailableOfflineStatus.AVAILABLE_OFFLINE_PARENT);
            }
        }
        /// check and fix, if needed, local storage path
        searchForLocalFileInDefaultPath(updatedLocalFile);
        /// prepare content synchronizations
        boolean serverUnchanged = addToSyncContents(updatedLocalFile, remoteFile);
        if (updatedLocalFile.isFolder() && !serverUnchanged) {
            foldersToExpand++;
        }
        updatedFiles.add(updatedLocalFile);
    }
    // save updated contents in local database
    if (foldersToExpand == 0) {
        updatedFolder.setTreeEtag(updatedFolder.getEtag());
    // TODO - propagate up
    }
    storageManager.saveFolder(updatedFolder, updatedFiles, localFilesMap.values());
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) HashMap(java.util.HashMap) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) Vector(java.util.Vector) RemoteFile(com.owncloud.android.lib.resources.files.RemoteFile)

Example 4 with OperationCancelledException

use of com.owncloud.android.lib.common.operations.OperationCancelledException in project android by owncloud.

the class DownloadFileOperation method run.

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result;
    File newFile;
    boolean moved;
    /// download will be performed to a temporal file, then moved to the final location
    File tmpFile = new File(getTmpPath());
    String tmpFolder = getTmpFolder();
    /// perform the download
    synchronized (mCancellationRequested) {
        if (mCancellationRequested.get()) {
            return new RemoteOperationResult(new OperationCancelledException());
        }
    }
    mDownloadOperation = new DownloadRemoteFileOperation(mFile.getRemotePath(), tmpFolder);
    Iterator<OnDatatransferProgressListener> listener = mDataTransferListeners.iterator();
    while (listener.hasNext()) {
        mDownloadOperation.addDatatransferProgressListener(listener.next());
    }
    result = mDownloadOperation.execute(client);
    if (result.isSuccess()) {
        mModificationTimestamp = mDownloadOperation.getModificationTimestamp();
        mEtag = mDownloadOperation.getEtag();
        if (FileStorageUtils.getUsableSpace(mAccount.name) < tmpFile.length()) {
            Log_OC.w(TAG, "Not enough space to copy " + tmpFile.getAbsolutePath());
        }
        newFile = new File(getSavePath());
        Log_OC.d(TAG, "Save path: " + newFile.getAbsolutePath());
        File parent = newFile.getParentFile();
        boolean created = parent.mkdirs();
        Log_OC.d(TAG, "Creation of parent folder " + parent.getAbsolutePath() + " succeeded: " + created);
        Log_OC.d(TAG, "Parent folder " + parent.getAbsolutePath() + " exists: " + parent.exists());
        Log_OC.d(TAG, "Parent folder " + parent.getAbsolutePath() + " is directory: " + parent.isDirectory());
        moved = tmpFile.renameTo(newFile);
        Log_OC.d(TAG, "New file " + newFile.getAbsolutePath() + " exists: " + newFile.exists());
        Log_OC.d(TAG, "New file " + newFile.getAbsolutePath() + " is directory: " + newFile.isDirectory());
        if (!moved) {
            result = new RemoteOperationResult(RemoteOperationResult.ResultCode.LOCAL_STORAGE_NOT_MOVED);
        }
    }
    Log_OC.i(TAG, "Download of " + mFile.getRemotePath() + " to " + getSavePath() + ": " + result.getLogMessage());
    return result;
}
Also used : OnDatatransferProgressListener(com.owncloud.android.lib.common.network.OnDatatransferProgressListener) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) DownloadRemoteFileOperation(com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File)

Example 5 with OperationCancelledException

use of com.owncloud.android.lib.common.operations.OperationCancelledException in project android by owncloud.

the class UploadFileOperation method copy.

/**
     * TODO rewrite with homogeneous fail handling, remove dependency on {@link RemoteOperationResult},
     * TODO     use Exceptions instead
     *
     * @param   sourceFile      Source file to copy.
     * @param   targetFile      Target location to copy the file.
     * @return  {@link RemoteOperationResult}
     * @throws  IOException
     */
private RemoteOperationResult copy(File sourceFile, File targetFile) throws IOException {
    Log_OC.d(TAG, "Copying local file");
    RemoteOperationResult result = null;
    if (FileStorageUtils.getUsableSpace(mAccount.name) < sourceFile.length()) {
        result = new RemoteOperationResult(ResultCode.LOCAL_STORAGE_FULL);
        // error condition when the file should be copied
        return result;
    } else {
        Log_OC.d(TAG, "Creating temporal folder");
        File temporalParent = targetFile.getParentFile();
        temporalParent.mkdirs();
        if (!temporalParent.isDirectory()) {
            throw new IOException("Unexpected error: parent directory could not be created");
        }
        Log_OC.d(TAG, "Creating temporal file");
        targetFile.createNewFile();
        if (!targetFile.isFile()) {
            throw new IOException("Unexpected error: target file could not be created");
        }
        Log_OC.d(TAG, "Copying file contents");
        InputStream in = null;
        OutputStream out = null;
        try {
            if (!mOriginalStoragePath.equals(targetFile.getAbsolutePath())) {
                // In case document provider schema as 'content://'
                if (mOriginalStoragePath.startsWith(UriUtils.URI_CONTENT_SCHEME)) {
                    Uri uri = Uri.parse(mOriginalStoragePath);
                    in = mContext.getContentResolver().openInputStream(uri);
                } else {
                    in = new FileInputStream(sourceFile);
                }
                out = new FileOutputStream(targetFile);
                int nRead;
                byte[] buf = new byte[4096];
                while (!mCancellationRequested.get() && (nRead = in.read(buf)) > -1) {
                    out.write(buf, 0, nRead);
                }
                out.flush();
            }
            if (mCancellationRequested.get()) {
                result = new RemoteOperationResult(new OperationCancelledException());
                return result;
            }
        } 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 " + targetFile.getAbsolutePath() + " (ignoring)", e);
            }
        }
    }
    return result;
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) OCFile(com.owncloud.android.datamodel.OCFile) RemoteFile(com.owncloud.android.lib.resources.files.RemoteFile) File(java.io.File) Uri(android.net.Uri) FileInputStream(java.io.FileInputStream) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) IOException(java.io.IOException)

Aggregations

OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)7 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)5 OCFile (com.owncloud.android.datamodel.OCFile)4 RemoteFile (com.owncloud.android.lib.resources.files.RemoteFile)3 File (java.io.File)3 OnDatatransferProgressListener (com.owncloud.android.lib.common.network.OnDatatransferProgressListener)2 IOException (java.io.IOException)2 Intent (android.content.Intent)1 Uri (android.net.Uri)1 NonNull (android.support.annotation.NonNull)1 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)1 ChunkedUploadRemoteFileOperation (com.owncloud.android.lib.resources.files.ChunkedUploadRemoteFileOperation)1 DownloadRemoteFileOperation (com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation)1 ReadRemoteFolderOperation (com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation)1 UploadRemoteFileOperation (com.owncloud.android.lib.resources.files.UploadRemoteFileOperation)1 SyncOperation (com.owncloud.android.operations.common.SyncOperation)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1