Search in sources :

Example 26 with RemoteOperationResult

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

the class FileDownloader method downloadFile.

/**
     * Core download method: requests a file to download and stores it.
     *
     * @param downloadKey Key to access the download to perform, contained in mPendingDownloads
     */
private void downloadFile(String downloadKey) {
    mCurrentDownload = mPendingDownloads.get(downloadKey);
    if (mCurrentDownload != null) {
        // Detect if the account exists
        if (AccountUtils.exists(mCurrentDownload.getAccount(), getApplicationContext())) {
            Log_OC.d(TAG, "Account " + mCurrentDownload.getAccount().name + " exists");
            notifyDownloadStart(mCurrentDownload);
            RemoteOperationResult downloadResult = null;
            try {
                /// prepare client object to send the request to the ownCloud server
                if (mCurrentAccount == null || !mCurrentAccount.equals(mCurrentDownload.getAccount())) {
                    mCurrentAccount = mCurrentDownload.getAccount();
                    mStorageManager = new FileDataStorageManager(mCurrentAccount, getContentResolver());
                }
                // else, reuse storage manager from previous operation
                // always get client from client manager, to get fresh credentials in case
                // of update
                OwnCloudAccount ocAccount = new OwnCloudAccount(mCurrentAccount, this);
                mDownloadClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, this);
                /// perform the download
                downloadResult = mCurrentDownload.execute(mDownloadClient);
                if (downloadResult.isSuccess()) {
                    saveDownloadedFile();
                }
            } catch (Exception e) {
                Log_OC.e(TAG, "Error downloading", e);
                downloadResult = new RemoteOperationResult(e);
            } finally {
                Pair<DownloadFileOperation, String> removeResult = mPendingDownloads.removePayload(mCurrentAccount.name, mCurrentDownload.getRemotePath());
                /// notify result
                notifyDownloadResult(mCurrentDownload, downloadResult);
                sendBroadcastDownloadFinished(mCurrentDownload, downloadResult, removeResult.second);
            }
        } else {
            // Cancel the transfer
            Log_OC.d(TAG, "Account " + mCurrentDownload.getAccount().toString() + " doesn't exist");
            cancelDownloadsForAccount(mCurrentDownload.getAccount());
        }
    }
}
Also used : DownloadFileOperation(com.owncloud.android.operations.DownloadFileOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount)

Example 27 with RemoteOperationResult

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

the class GetSharesForFileOperation method run.

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    GetRemoteSharesForFileOperation operation = new GetRemoteSharesForFileOperation(mPath, mReshares, mSubfiles);
    RemoteOperationResult result = operation.execute(client);
    if (result.isSuccess()) {
        // Update DB with the response
        Log_OC.d(TAG, "File = " + mPath + " Share list size  " + result.getData().size());
        ArrayList<OCShare> shares = new ArrayList<OCShare>();
        for (Object obj : result.getData()) {
            shares.add((OCShare) obj);
        }
        getStorageManager().saveSharesDB(shares);
    } else if (result.getCode() == RemoteOperationResult.ResultCode.SHARE_NOT_FOUND) {
        // no share on the file - remove local shares
        getStorageManager().removeSharesForFile(mPath);
    }
    return result;
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ArrayList(java.util.ArrayList) OCShare(com.owncloud.android.lib.resources.shares.OCShare) GetRemoteSharesForFileOperation(com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation)

Example 28 with RemoteOperationResult

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

the class MoveFileOperation method run.

/**
     * Performs the operation.
     * 
     * @param   client      Client object to communicate with the remote ownCloud server.
     */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;
    /// 1. check move 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 move
    String targetPath = mTargetParentPath + mFile.getFileName();
    if (mFile.isFolder()) {
        targetPath += OCFile.PATH_SEPARATOR;
    }
    MoveRemoteFileOperation operation = new MoveRemoteFileOperation(mSrcPath, targetPath, false);
    result = operation.execute(client);
    /// 3. local move
    if (result.isSuccess()) {
        // stop observing changes if available offline
        boolean isAvailableOffline = mFile.getAvailableOfflineStatus().equals(OCFile.AvailableOfflineStatus.AVAILABLE_OFFLINE);
        // OCFile.AvailableOfflineStatus.AVAILABLE_OFFLINE_PARENT requires no action
        if (isAvailableOffline) {
            stopObservation();
        }
        getStorageManager().moveLocalFile(mFile, targetPath, mTargetParentPath);
        // adjust available offline status after move resume observation of file after rename
        OCFile updatedFile = getStorageManager().getFileById(mFile.getFileId());
        OCFile.AvailableOfflineStatus updatedAvOffStatus = updatedFile.getAvailableOfflineStatus();
        if (updatedAvOffStatus == OCFile.AvailableOfflineStatus.AVAILABLE_OFFLINE) {
            resumeObservation(targetPath);
        } else if (updatedAvOffStatus == OCFile.AvailableOfflineStatus.AVAILABLE_OFFLINE_PARENT) {
            // enforce ancestor to rescan subfolders for immediate observation
            OCFile ancestor = getStorageManager().getAvailableOfflineAncestorOf(updatedFile);
            FileObserverService.observeFile(MainApp.getAppContext(), ancestor, getStorageManager().getAccount(), true);
        }
    }
    return result;
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) MoveRemoteFileOperation(com.owncloud.android.lib.resources.files.MoveRemoteFileOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult)

Example 29 with RemoteOperationResult

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

the class OAuth2GetAccessToken method run.

/*
    public Map<String, String> getResultTokenMap() {
        return mResultTokenMap;
    }
    */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;
    PostMethod postMethod = null;
    try {
        parseAuthorizationResponse();
        if (mOAuth2ParsedAuthorizationResponse.keySet().contains(OAuth2Constants.KEY_ERROR)) {
            if (OAuth2Constants.VALUE_ERROR_ACCESS_DENIED.equals(mOAuth2ParsedAuthorizationResponse.get(OAuth2Constants.KEY_ERROR))) {
                result = new RemoteOperationResult(ResultCode.OAUTH2_ERROR_ACCESS_DENIED);
            } else {
                result = new RemoteOperationResult(ResultCode.OAUTH2_ERROR);
            }
        }
        if (result == null) {
            NameValuePair[] nameValuePairs = new NameValuePair[4];
            nameValuePairs[0] = new NameValuePair(OAuth2Constants.KEY_GRANT_TYPE, mGrantType);
            nameValuePairs[1] = new NameValuePair(OAuth2Constants.KEY_CODE, mOAuth2ParsedAuthorizationResponse.get(OAuth2Constants.KEY_CODE));
            nameValuePairs[2] = new NameValuePair(OAuth2Constants.KEY_REDIRECT_URI, mRedirectUri);
            nameValuePairs[3] = new NameValuePair(OAuth2Constants.KEY_CLIENT_ID, mClientId);
            //nameValuePairs[4] = new NameValuePair(OAuth2Constants.KEY_SCOPE, mOAuth2ParsedAuthorizationResponse.get(OAuth2Constants.KEY_SCOPE));         
            postMethod = new PostMethod(client.getWebdavUri().toString());
            postMethod.setRequestBody(nameValuePairs);
            int status = client.executeMethod(postMethod);
            String response = postMethod.getResponseBodyAsString();
            if (response != null && response.length() > 0) {
                JSONObject tokenJson = new JSONObject(response);
                parseAccessTokenResult(tokenJson);
                if (mResultTokenMap.get(OAuth2Constants.KEY_ERROR) != null || mResultTokenMap.get(OAuth2Constants.KEY_ACCESS_TOKEN) == null) {
                    result = new RemoteOperationResult(ResultCode.OAUTH2_ERROR);
                } else {
                    result = new RemoteOperationResult(true, postMethod);
                    ArrayList<Object> data = new ArrayList<Object>();
                    data.add(mResultTokenMap);
                    result.setData(data);
                }
            } else {
                result = new RemoteOperationResult(false, postMethod);
                client.exhaustResponse(postMethod.getResponseBodyAsStream());
            }
        }
    } catch (Exception e) {
        result = new RemoteOperationResult(e);
    } finally {
        if (postMethod != null)
            // let the connection available for other methods
            postMethod.releaseConnection();
        if (result.isSuccess()) {
            Log_OC.i(TAG, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse.get("code") + " to " + client.getWebdavUri() + ": " + result.getLogMessage());
        } else if (result.getException() != null) {
            Log_OC.e(TAG, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse.get("code") + " to " + client.getWebdavUri() + ": " + result.getLogMessage(), result.getException());
        } else if (result.getCode() == ResultCode.OAUTH2_ERROR) {
            Log_OC.e(TAG, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse.get("code") + " to " + client.getWebdavUri() + ": " + ((mResultTokenMap != null) ? mResultTokenMap.get(OAuth2Constants.KEY_ERROR) : "NULL"));
        } else {
            Log_OC.e(TAG, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse.get("code") + " to " + client.getWebdavUri() + ": " + result.getLogMessage());
        }
    }
    return result;
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) JSONObject(org.json.JSONObject) PostMethod(org.apache.commons.httpclient.methods.PostMethod) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ArrayList(java.util.ArrayList) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException)

Example 30 with RemoteOperationResult

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

the class RefreshFolderOperation method updateOCVersion.

private void updateOCVersion(OwnCloudClient client) {
    UpdateOCVersionOperation update = new UpdateOCVersionOperation(mAccount, mContext);
    RemoteOperationResult result = update.execute(client);
    if (result.isSuccess()) {
        mIsShareSupported = update.getOCVersion().isSharedSupported();
        // Update Capabilities for this account
        if (update.getOCVersion().isVersionWithCapabilitiesAPI()) {
            updateCapabilities();
        } else {
            Log_OC.d(TAG, "Capabilities API disabled");
        }
    }
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult)

Aggregations

RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)46 OCFile (com.owncloud.android.datamodel.OCFile)11 ArrayList (java.util.ArrayList)9 RemoteOperation (com.owncloud.android.lib.common.operations.RemoteOperation)7 OCShare (com.owncloud.android.lib.resources.shares.OCShare)7 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)6 ExistenceCheckRemoteOperation (com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation)6 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)5 OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)5 File (java.io.File)5 IOException (java.io.IOException)5 Account (android.accounts.Account)4 Uri (android.net.Uri)4 Intent (android.content.Intent)3 RemoteFile (com.owncloud.android.lib.resources.files.RemoteFile)3 GetRemoteSharesForFileOperation (com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation)3 UserInfo (com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation.UserInfo)3 AccountManager (android.accounts.AccountManager)2 Pair (android.util.Pair)2 OwnCloudClient (com.owncloud.android.lib.common.OwnCloudClient)2