Search in sources :

Example 96 with RemoteOperationResult

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

the class OCFileListAdapter method parseShares.

private void parseShares(ArrayList<Object> objects) {
    List<OCShare> shares = new ArrayList<>();
    for (int i = 0; i < objects.size(); i++) {
        // check type before cast as of long running data fetch it is possible that old result is filled
        if (objects.get(i) instanceof OCShare) {
            OCShare ocShare = (OCShare) objects.get(i);
            shares.add(ocShare);
            // get ocFile from Server to have an up-to-date copy
            ReadRemoteFileOperation operation = new ReadRemoteFileOperation(ocShare.getPath());
            RemoteOperationResult result = operation.execute(mAccount, mContext);
            if (result.isSuccess()) {
                OCFile file = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
                searchForLocalFileInDefaultPath(file);
                file = mStorageManager.saveFileWithParent(file, mContext);
                ShareType newShareType = ocShare.getShareType();
                if (newShareType == ShareType.PUBLIC_LINK) {
                    file.setShareViaLink(true);
                } else if (newShareType == ShareType.USER || newShareType == ShareType.GROUP || newShareType == ShareType.EMAIL || newShareType == ShareType.FEDERATED) {
                    file.setShareWithSharee(true);
                }
                mStorageManager.saveFile(file);
                if (!mFiles.contains(file)) {
                    mFiles.add(file);
                }
            } else {
                Log_OC.e(TAG, "Error in getting prop for file: " + ocShare.getPath());
            }
        }
    }
    mStorageManager.saveShares(shares);
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) OCShare(com.owncloud.android.lib.resources.shares.OCShare) ArrayList(java.util.ArrayList) ReadRemoteFileOperation(com.owncloud.android.lib.resources.files.ReadRemoteFileOperation) ShareType(com.owncloud.android.lib.resources.shares.ShareType)

Example 97 with RemoteOperationResult

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

the class GetUserProfileOperation method run.

/**
 * Performs the operation.
 *
 * Target user account is implicit in 'client'.
 *
 * Stored account is implicit in {@link #getStorageManager()}.
 *
 * @return      Result of the operation. If successful, includes an instance of
 *              {@link String} with the display name retrieved from the server.
 *              Call {@link RemoteOperationResult#getData()}.get(0) to get it.
 */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    // get display name
    GetRemoteUserInfoOperation getDisplayName = new GetRemoteUserInfoOperation();
    RemoteOperationResult result = getDisplayName.execute(client);
    if (result.isSuccess()) {
        // store display name with account data
        AccountManager accountManager = AccountManager.get(MainApp.getAppContext());
        UserInfo userInfo = (UserInfo) result.getData().get(0);
        Account storedAccount = getStorageManager().getAccount();
        accountManager.setUserData(storedAccount, AccountUtils.Constants.KEY_DISPLAY_NAME, userInfo.getDisplayName());
        accountManager.setUserData(storedAccount, AccountUtils.Constants.KEY_USER_ID, userInfo.getId());
    }
    return result;
}
Also used : Account(android.accounts.Account) GetRemoteUserInfoOperation(com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) AccountManager(android.accounts.AccountManager) UserInfo(com.owncloud.android.lib.common.UserInfo)

Example 98 with RemoteOperationResult

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

the class OAuth2GetAccessToken method run.

/*
    public Map<String, String> getResultTokenMap() {
        return mResultTokenMap;
    }
    */
@Override
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
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);
            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<>();
                    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();
        }
        final String code = "code";
        final String oauth_token_request = "OAuth2 TOKEN REQUEST with auth code ";
        if (result != null) {
            if (result.isSuccess()) {
                Log_OC.i(TAG, oauth_token_request + mOAuth2ParsedAuthorizationResponse.get(code) + " to " + client.getWebdavUri() + ": " + result.getLogMessage());
            } else if (result.getException() != null) {
                Log_OC.e(TAG, oauth_token_request + mOAuth2ParsedAuthorizationResponse.get(code) + " to " + client.getWebdavUri() + ": " + result.getLogMessage(), result.getException());
            } else if (result.getCode() == ResultCode.OAUTH2_ERROR) {
                Log_OC.e(TAG, oauth_token_request + mOAuth2ParsedAuthorizationResponse.get(code) + " to " + client.getWebdavUri() + ": " + ((mResultTokenMap != null) ? mResultTokenMap.get(OAuth2Constants.KEY_ERROR) : "NULL"));
            } else {
                Log_OC.e(TAG, oauth_token_request + 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 99 with RemoteOperationResult

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

the class UnshareOperation method existsFile.

private boolean existsFile(OwnCloudClient client, String remotePath) {
    ExistenceCheckRemoteOperation existsOperation = new ExistenceCheckRemoteOperation(remotePath, mContext, false);
    RemoteOperationResult result = existsOperation.execute(client);
    return result.isSuccess();
}
Also used : ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult)

Example 100 with RemoteOperationResult

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

the class ActivitiesListActivity method onActivityClicked.

@Override
public void onActivityClicked(RichObject richObject) {
    String path = FileUtils.PATH_SEPARATOR + richObject.getPath();
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            swipeEmptyListRefreshLayout.setVisibility(View.VISIBLE);
            swipeListRefreshLayout.setVisibility(View.GONE);
            setLoadingMessage();
        }
    });
    updateTask = new AsyncTask<String, Object, OCFile>() {

        @Override
        protected OCFile doInBackground(String... path) {
            OCFile ocFile = null;
            // always update file as it could be an old state saved in database
            ReadRemoteFileOperation operation = new ReadRemoteFileOperation(path[0]);
            RemoteOperationResult resultRemoteFileOp = operation.execute(ownCloudClient);
            if (resultRemoteFileOp.isSuccess()) {
                OCFile temp = FileStorageUtils.fillOCFile((RemoteFile) resultRemoteFileOp.getData().get(0));
                ocFile = getStorageManager().saveFileWithParent(temp, getBaseContext());
                if (ocFile.isFolder()) {
                    // perform folder synchronization
                    RemoteOperation synchFolderOp = new RefreshFolderOperation(ocFile, System.currentTimeMillis(), false, getFileOperationsHelper().isSharedSupported(), true, getStorageManager(), getAccount(), getApplicationContext());
                    synchFolderOp.execute(ownCloudClient);
                }
            }
            return ocFile;
        }

        @Override
        protected void onPostExecute(OCFile ocFile) {
            if (!isCancelled()) {
                if (ocFile == null) {
                    Toast.makeText(getBaseContext(), R.string.file_not_found, Toast.LENGTH_LONG).show();
                    swipeEmptyListRefreshLayout.setVisibility(View.GONE);
                    swipeListRefreshLayout.setVisibility(View.VISIBLE);
                    dismissLoadingDialog();
                } else {
                    Intent showDetailsIntent;
                    if (PreviewImageFragment.canBePreviewed(ocFile)) {
                        showDetailsIntent = new Intent(getBaseContext(), PreviewImageActivity.class);
                    } else {
                        showDetailsIntent = new Intent(getBaseContext(), FileDisplayActivity.class);
                    }
                    showDetailsIntent.putExtra(EXTRA_FILE, ocFile);
                    showDetailsIntent.putExtra(EXTRA_ACCOUNT, getAccount());
                    startActivity(showDetailsIntent);
                }
            }
        }
    };
    updateTask.execute(path);
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation) ReadRemoteFileOperation(com.owncloud.android.lib.resources.files.ReadRemoteFileOperation) RichObject(com.owncloud.android.lib.resources.activities.models.RichObject) Intent(android.content.Intent) BindString(butterknife.BindString) RemoteFile(com.owncloud.android.lib.resources.files.RemoteFile)

Aggregations

RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)172 OCFile (com.owncloud.android.datamodel.OCFile)48 RemoteOperation (com.owncloud.android.lib.common.operations.RemoteOperation)24 ArrayList (java.util.ArrayList)24 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)22 IOException (java.io.IOException)22 Account (android.accounts.Account)19 User (com.nextcloud.client.account.User)18 OCShare (com.owncloud.android.lib.resources.shares.OCShare)18 File (java.io.File)18 OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)17 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)16 Context (android.content.Context)15 OwnCloudClient (com.owncloud.android.lib.common.OwnCloudClient)12 RemoteFile (com.owncloud.android.lib.resources.files.model.RemoteFile)12 FileNotFoundException (java.io.FileNotFoundException)12 Intent (android.content.Intent)11 JSONObject (org.json.JSONObject)11 Test (org.junit.Test)11 Uri (android.net.Uri)10