0
votes

I'm trying to download a list of RemoteFiles in Android using ownCloud. I can download the files perfectly fine but I'd like to notify the user when a file finishes. I'm downloading an entire directory:

@Override
public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
    if (operation instanceof ReadRemoteFolderOperation) {
        if (result.isSuccess()) {
            Toast.makeText(this, "Finished reading folder", Toast.LENGTH_SHORT).show();
            for (Object o : result.getData()) {
                RemoteFile remoteFile = (RemoteFile) o;
                String remotePath = remoteFile.getRemotePath();

                File targetDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
                        "/owncloud_download");

                downloadHelper.downloadFile(remoteFile, targetDirectory);
            }
        }
    }

    if (operation instanceof DownloadRemoteFileOperation) {
        if (result.isSuccess()) {
            // Notify the user here that the file finished
        }
    }
}

I've looked at the ownCloud library source but can't seem to find what a DownloadRemoteFileOperation returns as a result other than a boolean indicating success and an HTTP status code. I thought it might be in result.getLogMessage() but that just gives me an HTTP 200 status. How can I get the name of a file that's finished?

Edit: I also looked at result.getData() but that's null in a DownloadRemoteFileOperation.

1

1 Answers

0
votes

Here's my workaround for the time being. I didn't want to modify the ownCloud library source (again) so instead I just do a check in onTransferProgress like so:

@Override
public void onTransferProgress(long rate, long transferred, long total, String fileName) {
    if (transferred == total) {
        runOnUiThread(new Runnable() {
            // do the update here, file name is available
        }
    }
}

Here's another option. I needed the file being uploaded if the upload failed so I modified the ownCloud library source. This way I could return file names in the RemoteOperationResult.

RemoteOperationResult.java:

private String fileName;

public String getFileName() {
    return fileName;
}

public void setFileName(String name) {
    fileName = name;
}

DownloadRemoteFileOperation.java

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;

    /// download will be performed to a temporal file, then moved to the final location
    File tmpFile = new File(getTmpPath());

    /// perform the download
    try {
        tmpFile.getParentFile().mkdirs();
        int status = downloadFile(client, tmpFile);
        result = new RemoteOperationResult(isSuccess(status), status,
                (mGet != null ? mGet.getResponseHeaders() : null));
        Log_OC.i(TAG, "Download of " + mRemotePath + " to " + getTmpPath() + ": " +
                result.getLogMessage());

    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log_OC.e(TAG, "Download of " + mRemotePath + " to " + getTmpPath() + ": " +
                result.getLogMessage(), e);
    }
    // Added this line
    result.setFileName(mRemotePath);

    return result;
}

UploadRemoteFileOperation.java:

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;

    try {
        // / perform the upload
        synchronized (mCancellationRequested) {
            if (mCancellationRequested.get()) {
                throw new OperationCancelledException();
            } else {
                mPutMethod = new PutMethod(client.getWebdavUri() +
                        WebdavUtils.encodePath(mRemotePath));
            }
        }

        int status = uploadFile(client);
        if (mForbiddenCharsInServer){
            result = new RemoteOperationResult(
                    RemoteOperationResult.ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER);
        } else {
            result = new RemoteOperationResult(isSuccess(status), status,
                    (mPutMethod != null ? mPutMethod.getResponseHeaders() : null));
        }
    } catch (Exception e) {
        // TODO something cleaner with cancellations
        if (mCancellationRequested.get()) {
            result = new RemoteOperationResult(new OperationCancelledException());
        } else {
            result = new RemoteOperationResult(e);
        }
    }
    // Added this line
    result.setFileName(mLocalPath);

    return result;
}