Search in sources :

Example 1 with UserRecoverableAuthIOException

use of com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException in project keepass2android by PhilippC.

the class GoogleDriveFileStorage method initializeAccountOrPath.

private void initializeAccountOrPath(final JavaFileStorage.FileStorageSetupActivity setupAct, final String accountNameOrPath) {
    final Activity activity = ((Activity) setupAct);
    final Context appContext = activity.getApplicationContext();
    String accountNameTemp;
    GDrivePath gdrivePath = null;
    if (accountNameOrPath.startsWith(getProtocolPrefix())) {
        gdrivePath = new GDrivePath();
        // don't verify yet, we're not yet initialized:
        try {
            gdrivePath.setPathWithoutVerify(accountNameOrPath);
        } catch (Exception e) {
            finishWithError(setupAct, e);
        }
        accountNameTemp = gdrivePath.getAccount();
    } else
        accountNameTemp = accountNameOrPath;
    final String accountName = accountNameTemp;
    AsyncTask<Object, Void, AsyncTaskResult<String>> task = new AsyncTask<Object, Void, AsyncTaskResult<String>>() {

        @Override
        protected AsyncTaskResult<String> doInBackground(Object... arg0) {
            try {
                initializeAccount(appContext, accountName);
                if (setupAct.getProcessName().equals(PROCESS_NAME_SELECTFILE))
                    setupAct.getState().putString(EXTRA_PATH, getRootPathForAccount(accountName));
                return new AsyncTaskResult<String>("ok");
            } catch (Exception anyError) {
                return new AsyncTaskResult<String>(anyError);
            }
        }

        @Override
        protected void onPostExecute(AsyncTaskResult<String> result) {
            Exception error = result.getError();
            if (error != null) {
                if (UserRecoverableAuthIOException.class.isAssignableFrom(error.getClass())) {
                    mAccountData.remove(accountName);
                    activity.startActivityForResult(((UserRecoverableAuthIOException) error).getIntent(), REQUEST_AUTHORIZATION);
                } else {
                    finishWithError(setupAct, error);
                }
            } else if (isCancelled()) {
                // cancel handling here
                logDebug("Async Task cancelled!");
                activity.setResult(Activity.RESULT_CANCELED);
                activity.finish();
            } else {
                // all right!
                finishActivityWithSuccess(setupAct);
            }
        }
    };
    task.execute(new Object[] {});
}
Also used : Context(android.content.Context) AsyncTask(android.os.AsyncTask) Activity(android.app.Activity) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) UserRecoverableAuthIOException(com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with UserRecoverableAuthIOException

use of com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException in project Aegis by Decad3nce.

the class BackupGoogleAccountsActivity method saveFileToDrive.

private void saveFileToDrive() {
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                if (!isAegisFolderAvailable()) {
                    createAegisFolder();
                }
                FileContent mediaContent = null;
                File body = new File();
                File file = null;
                if (callLogs) {
                    Log.i(TAG, "Generating new file to upload: " + callLogFileUri);
                    java.io.File callFileContent = new java.io.File(callLogFileUri.getPath());
                    mediaContent = new FileContent("text/plain", callFileContent);
                    body.setTitle(callFileContent.getName());
                    body.setParents(Arrays.asList(new ParentReference().setId(getAegisFolder())));
                    body.setMimeType("text/plain");
                    file = service.files().insert(body, mediaContent).execute();
                    if (file != null) {
                        Log.i(TAG, "File uploaded successfully: " + file.getTitle());
                        Utils.sendSMS(context, address, context.getResources().getString(R.string.util_sendsms_data_recovery_pass) + " " + file.getTitle() + " to Google Drive");
                        deleteFile(file.getTitle());
                        finish();
                    }
                }
                if (smsLogs) {
                    Log.i(TAG, "Generating new file to upload: " + smsLogFileUri);
                    java.io.File smsFileContent = new java.io.File(smsLogFileUri.getPath());
                    mediaContent = new FileContent("text/plain", smsFileContent);
                    body.setTitle(smsFileContent.getName());
                    body.setParents(Arrays.asList(new ParentReference().setId(getAegisFolder())));
                    body.setMimeType("text/plain");
                    file = service.files().insert(body, mediaContent).execute();
                    if (file != null) {
                        Log.i(TAG, "File uploaded successfully: " + file.getTitle());
                        Utils.sendSMS(context, address, context.getResources().getString(R.string.util_sendsms_data_recovery_pass) + " " + file.getTitle() + " to Google Drive");
                        deleteFile(file.getTitle());
                        finish();
                    }
                }
            } catch (UserRecoverableAuthIOException e) {
                Log.i(TAG, "Exception: " + e.toString());
                startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
            } catch (IOException e) {
                Log.i(TAG, "Exception: " + e.toString());
                e.printStackTrace();
                Utils.sendSMS(context, address, context.getResources().getString(R.string.util_sendsms_data_recovery_fail));
            }
        }
    });
    t.start();
}
Also used : FileContent(com.google.api.client.http.FileContent) ParentReference(com.google.api.services.drive.model.ParentReference) UserRecoverableAuthIOException(com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException) UserRecoverableAuthIOException(com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException) IOException(java.io.IOException) File(com.google.api.services.drive.model.File)

Example 3 with UserRecoverableAuthIOException

use of com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException in project Aegis by Decad3nce.

the class BackupGoogleAccountsActivity method createAegisFolder.

private void createAegisFolder() {
    Log.i(TAG, "Creating aeGis folder");
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                File body = new File();
                body.setTitle("aeGis Backup");
                body.setDescription("Backup stored by aeGis");
                body.setMimeType("application/vnd.google-apps.folder");
                File file = service.files().insert(body).execute();
                if (file != null) {
                    finish();
                }
            } catch (UserRecoverableAuthIOException e) {
                startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    t.start();
}
Also used : UserRecoverableAuthIOException(com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException) UserRecoverableAuthIOException(com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException) IOException(java.io.IOException) File(com.google.api.services.drive.model.File)

Aggregations

UserRecoverableAuthIOException (com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException)3 IOException (java.io.IOException)3 File (com.google.api.services.drive.model.File)2 Activity (android.app.Activity)1 Context (android.content.Context)1 AsyncTask (android.os.AsyncTask)1 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 FileContent (com.google.api.client.http.FileContent)1 ParentReference (com.google.api.services.drive.model.ParentReference)1 FileNotFoundException (java.io.FileNotFoundException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1