com.google.android.gms.drive.ExecutionOptions Java Examples

The following examples show how to use com.google.android.gms.drive.ExecutionOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MainActivity.java    From android-samples with Apache License 2.0 5 votes vote down vote up
private Task<Void> saveFile() {
    Log.d(TAG, "Saving file.");
    // [START drive_android_reopen_for_write]
    Task<DriveContents> reopenTask =
            getDriveResourceClient().reopenContentsForWrite(mDriveContents);
    // [END drive_android_reopen_for_write]
    return reopenTask
            .continueWithTask(task -> {
                // [START drive_android_write_conflict_strategy]
                DriveContents driveContents = task.getResult();
                OutputStream outputStream = driveContents.getOutputStream();
                try (Writer writer = new OutputStreamWriter(outputStream)) {
                    writer.write(mEditText.getText().toString());
                }
                // ExecutionOptions define the conflict strategy to be used.
                // [START drive_android_execution_options]
                ExecutionOptions executionOptions =
                        new ExecutionOptions.Builder()
                                .setNotifyOnCompletion(true)
                                .setConflictStrategy(
                                        ExecutionOptions.CONFLICT_STRATEGY_KEEP_REMOTE)
                                .build();
                return getDriveResourceClient().commitContents(
                        driveContents, null, executionOptions);
                // [END drive_android_execution_options]
                // [END drive_android_write_conflict_strategy]
            })
            .continueWithTask(task -> {
                showMessage(getString(R.string.file_saved));
                Log.d(TAG, "Reopening file for read.");
                return loadContents(mGroceryListFile);
            });
}
 
Example #2
Source File: ConflictResolver.java    From android-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Initiate the resolution process by connecting the GoogleApiClient.
 */
void resolve() {
    // [START drive_android_resolve_conflict]
    // A new DriveResourceClient should be created to handle each new CompletionEvent since each
    // event is tied to a specific user account. Any DriveFile action taken must be done using
    // the correct account.
    GoogleSignInOptions.Builder signInOptionsBuilder =
            new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestScopes(Drive.SCOPE_FILE)
                    .requestScopes(Drive.SCOPE_APPFOLDER);
    if (mConflictedCompletionEvent.getAccountName() != null) {
        signInOptionsBuilder.setAccountName(mConflictedCompletionEvent.getAccountName());
    }
    GoogleSignInClient signInClient =
            GoogleSignIn.getClient(mContext, signInOptionsBuilder.build());
    signInClient.silentSignIn()
            .continueWith(mExecutorService,
                    (Continuation<GoogleSignInAccount, Void>) signInTask -> {
                        mDriveResourceClient = Drive.getDriveResourceClient(
                                mContext, signInTask.getResult());
                        mBaseContent = ConflictUtil.getStringFromInputStream(
                                mConflictedCompletionEvent.getBaseContentsInputStream());
                        mModifiedContent = ConflictUtil.getStringFromInputStream(
                                mConflictedCompletionEvent
                                        .getModifiedContentsInputStream());
                        return null;
                    })
            .continueWithTask(mExecutorService,
                    task -> {
                        DriveId driveId = mConflictedCompletionEvent.getDriveId();
                        return mDriveResourceClient.openFile(
                                driveId.asDriveFile(), DriveFile.MODE_READ_ONLY);
                    })
            .continueWithTask(mExecutorService,
                    task -> {
                        mDriveContents = task.getResult();
                        InputStream serverInputStream = task.getResult().getInputStream();
                        mServerContent =
                                ConflictUtil.getStringFromInputStream(serverInputStream);
                        return mDriveResourceClient.reopenContentsForWrite(mDriveContents);
                    })
            .continueWithTask(mExecutorService,
                    task -> {
                        DriveContents contentsForWrite = task.getResult();
                        mResolvedContent = ConflictUtil.resolveConflict(
                                mBaseContent, mServerContent, mModifiedContent);

                        OutputStream outputStream = contentsForWrite.getOutputStream();
                        try (Writer writer = new OutputStreamWriter(outputStream)) {
                            writer.write(mResolvedContent);
                        }

                        // It is not likely that resolving a conflict will result in another
                        // conflict, but it can happen if the file changed again while this
                        // conflict was resolved. Since we already implemented conflict
                        // resolution and we never want to miss user data, we commit here
                        // with execution options in conflict-aware mode (otherwise we would
                        // overwrite server content).
                        ExecutionOptions executionOptions =
                                new ExecutionOptions.Builder()
                                        .setNotifyOnCompletion(true)
                                        .setConflictStrategy(
                                                ExecutionOptions
                                                        .CONFLICT_STRATEGY_KEEP_REMOTE)
                                        .build();

                        // Commit resolved contents.
                        MetadataChangeSet modifiedMetadataChangeSet =
                                mConflictedCompletionEvent.getModifiedMetadataChangeSet();
                        return mDriveResourceClient.commitContents(contentsForWrite,
                                modifiedMetadataChangeSet, executionOptions);
            })
            .addOnSuccessListener(aVoid -> {
                mConflictedCompletionEvent.dismiss();
                Log.d(TAG, "resolved list");
                sendResult(mModifiedContent);
            })
            .addOnFailureListener(e -> {
                // The contents cannot be reopened at this point, probably due to
                // connectivity, so by snoozing the event we will get it again later.
                Log.d(TAG, "Unable to write resolved content, snoozing completion event.",
                        e);
                mConflictedCompletionEvent.snooze();
                if (mDriveContents != null) {
                    mDriveResourceClient.discardContents(mDriveContents);
                }
            });
    // [END drive_android_resolve_conflict]
}