Search in sources :

Example 1 with OwnCloudAccount

use of com.owncloud.android.lib.common.OwnCloudAccount in project android by owncloud.

the class GetShareWithUsersAsyncTask method doInBackground.

@Override
protected Pair<RemoteOperation, RemoteOperationResult> doInBackground(Object... params) {
    GetSharesForFileOperation operation = null;
    RemoteOperationResult result = null;
    if (params != null && params.length == 3) {
        OCFile file = (OCFile) params[0];
        Account account = (Account) params[1];
        FileDataStorageManager fileDataStorageManager = (FileDataStorageManager) params[2];
        try {
            // Get shares request
            operation = new GetSharesForFileOperation(file.getRemotePath(), false, false);
            OwnCloudAccount ocAccount = new OwnCloudAccount(account, MainApp.getAppContext());
            OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, MainApp.getAppContext());
            result = operation.execute(client, fileDataStorageManager);
        } catch (Exception e) {
            result = new RemoteOperationResult(e);
            Log_OC.e(TAG, "Exception while getting shares", e);
        }
    } else {
        result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
    }
    return new Pair(operation, result);
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) GetSharesForFileOperation(com.owncloud.android.operations.GetSharesForFileOperation) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) Pair(android.util.Pair)

Example 2 with OwnCloudAccount

use of com.owncloud.android.lib.common.OwnCloudAccount in project android by owncloud.

the class FileUploader method uploadFile.

/**
     * Core upload method: sends the file(s) to upload
     *
     * @param uploadKey Key to access the upload to perform, contained in mPendingUploads
     */
public void uploadFile(String uploadKey) {
    mCurrentUpload = mPendingUploads.get(uploadKey);
    if (mCurrentUpload != null) {
        /// Check account existence
        if (!AccountUtils.exists(mCurrentUpload.getAccount(), this)) {
            Log_OC.w(TAG, "Account " + mCurrentUpload.getAccount().name + " does not exist anymore -> cancelling all its uploads");
            cancelUploadsForAccount(mCurrentUpload.getAccount());
            return;
        }
        /// OK, let's upload
        mUploadsStorageManager.updateDatabaseUploadStart(mCurrentUpload);
        notifyUploadStart(mCurrentUpload);
        sendBroadcastUploadStarted(mCurrentUpload);
        RemoteOperationResult uploadResult = null;
        try {
            /// prepare client object to send the request to the ownCloud server
            if (mCurrentAccount == null || !mCurrentAccount.equals(mCurrentUpload.getAccount())) {
                mCurrentAccount = mCurrentUpload.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);
            mUploadClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, this);
            /// perform the upload
            uploadResult = mCurrentUpload.execute(mUploadClient, mStorageManager);
        } catch (Exception e) {
            Log_OC.e(TAG, "Error uploading", e);
            uploadResult = new RemoteOperationResult(e);
        } finally {
            Pair<UploadFileOperation, String> removeResult;
            if (mCurrentUpload.wasRenamed()) {
                removeResult = mPendingUploads.removePayload(mCurrentAccount.name, mCurrentUpload.getOldFile().getRemotePath());
            /** TODO: grant that name is also updated for mCurrentUpload.getOCUploadId */
            } else {
                removeResult = mPendingUploads.removePayload(mCurrentAccount.name, mCurrentUpload.getRemotePath());
            }
            mUploadsStorageManager.updateDatabaseUploadResult(uploadResult, mCurrentUpload);
            /// notify result
            notifyUploadResult(mCurrentUpload, uploadResult);
            sendBroadcastUploadFinished(mCurrentUpload, uploadResult, removeResult.second);
        }
    }
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) UploadFileOperation(com.owncloud.android.operations.UploadFileOperation)

Example 3 with OwnCloudAccount

use of com.owncloud.android.lib.common.OwnCloudAccount in project android by owncloud.

the class SyncFolderHandler method doOperation.

/**
     * Performs the next operation in the queue
     */
private void doOperation(Account account, String remotePath) {
    mCurrentSyncOperation = mPendingOperations.get(account.name, remotePath);
    if (mCurrentSyncOperation != null) {
        RemoteOperationResult result = null;
        try {
            if (mCurrentAccount == null || !mCurrentAccount.equals(account)) {
                mCurrentAccount = account;
                mStorageManager = new FileDataStorageManager(account, mService.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(account, mService);
            mOwnCloudClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, mService);
            result = mCurrentSyncOperation.execute(mOwnCloudClient, mStorageManager);
        } catch (AccountsException e) {
            Log_OC.e(TAG, "Error while trying to get authorization", e);
        } catch (IOException e) {
            Log_OC.e(TAG, "Error while trying to get authorization", e);
        } finally {
            mPendingOperations.removePayload(account.name, remotePath);
            mService.dispatchResultToOperationListeners(mCurrentSyncOperation, result);
            sendBroadcastFinishedSyncFolder(account, remotePath, result.isSuccess());
        }
    }
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) IOException(java.io.IOException) AccountsException(android.accounts.AccountsException)

Example 4 with OwnCloudAccount

use of com.owncloud.android.lib.common.OwnCloudAccount 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 5 with OwnCloudAccount

use of com.owncloud.android.lib.common.OwnCloudAccount in project android by owncloud.

the class AuthenticatorActivity method updateAccountAuthentication.

/**
     * Updates the authentication token.
     *
     * Sets the proper response so that the AccountAuthenticator that started this activity
     * saves a new authorization token for mAccount.
     *
     * Kills the session kept by OwnCloudClientManager so that a new one will created with
     * the new credentials when needed.
     */
private void updateAccountAuthentication() throws AccountNotFoundException {
    Bundle response = new Bundle();
    response.putString(AccountManager.KEY_ACCOUNT_NAME, mAccount.name);
    response.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccount.type);
    if (AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType()).equals(mAuthTokenType)) {
        response.putString(AccountManager.KEY_AUTHTOKEN, mAuthToken);
        // the next line is necessary, notifications are calling directly to the 
        // AuthenticatorActivity to update, without AccountManager intervention
        mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);
    } else if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) {
        response.putString(AccountManager.KEY_AUTHTOKEN, mAuthToken);
        // the next line is necessary; by now, notifications are calling directly to the 
        // AuthenticatorActivity to update, without AccountManager intervention
        mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken);
    } else {
        response.putString(AccountManager.KEY_AUTHTOKEN, mPasswordInput.getText().toString());
        mAccountMgr.setPassword(mAccount, mPasswordInput.getText().toString());
    }
    // remove managed clients for this account to enforce creation with fresh credentials
    OwnCloudAccount ocAccount = new OwnCloudAccount(mAccount, this);
    OwnCloudClientManagerFactory.getDefaultSingleton().removeClientFor(ocAccount);
    setAccountAuthenticatorResult(response);
    final Intent intent = new Intent();
    intent.putExtras(response);
    setResult(RESULT_OK, intent);
}
Also used : Bundle(android.os.Bundle) Intent(android.content.Intent) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount)

Aggregations

OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)11 Account (android.accounts.Account)4 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)4 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)4 Intent (android.content.Intent)3 TextView (android.widget.TextView)3 LayoutInflater (android.view.LayoutInflater)2 View (android.view.View)2 ImageView (android.widget.ImageView)2 OCFile (com.owncloud.android.datamodel.OCFile)2 OwnCloudClient (com.owncloud.android.lib.common.OwnCloudClient)2 AccountManager (android.accounts.AccountManager)1 AccountsException (android.accounts.AccountsException)1 AuthenticatorException (android.accounts.AuthenticatorException)1 SuppressLint (android.annotation.SuppressLint)1 DialogInterface (android.content.DialogInterface)1 OnCancelListener (android.content.DialogInterface.OnCancelListener)1 OnClickListener (android.content.DialogInterface.OnClickListener)1 NotFoundException (android.content.res.Resources.NotFoundException)1 Bitmap (android.graphics.Bitmap)1