Skip to content

Commit 2577053

Browse files
asrivas-devrelGerrit Code Review
authored and
Gerrit Code Review
committed
Merge "Lambda-fy the Drive Conflict Android app, address minor nits and update the compile options to Java 1.8"
2 parents c7eb28d + 17be9e8 commit 2577053

File tree

6 files changed

+147
-214
lines changed

6 files changed

+147
-214
lines changed

Diff for: drive/conflict/app/build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ android {
1717
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
1818
}
1919
}
20+
compileOptions {
21+
sourceCompatibility JavaVersion.VERSION_1_8
22+
targetCompatibility JavaVersion.VERSION_1_8
23+
}
2024
}
2125

2226
dependencies {

Diff for: drive/conflict/app/src/main/java/com/google/android/gms/drive/sample/conflict/BaseDemoActivity.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.google.android.gms.drive.Drive;
2626
import com.google.android.gms.drive.DriveClient;
2727
import com.google.android.gms.drive.DriveResourceClient;
28-
import com.google.android.gms.tasks.Task;
2928

3029
/**
3130
* An abstract activity that handles authorization and connection to the Drive
@@ -37,7 +36,7 @@ public abstract class BaseDemoActivity extends Activity {
3736
/**
3837
* Request code for auto Google Play Services error resolution.
3938
*/
40-
protected static final int REQUEST_CODE_SIGN_IN = 0;
39+
private static final int REQUEST_CODE_SIGN_IN = 0;
4140

4241
/**
4342
* Handles high-level drive functions like sync.
@@ -78,7 +77,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
7877
/**
7978
* Starts the sign-in process and initializes the Drive client.
8079
*/
81-
protected void signIn() {
80+
private void signIn() {
8281
GoogleSignInOptions signInOptions =
8382
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
8483
.requestScopes(Drive.SCOPE_FILE)
@@ -101,7 +100,7 @@ private void initializeDriveClient(GoogleSignInAccount signInAccount) {
101100
/**
102101
* Shows a toast message.
103102
*/
104-
protected void showMessage(String message) {
103+
void showMessage(String message) {
105104
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
106105
}
107106

@@ -110,11 +109,11 @@ protected void showMessage(String message) {
110109
*/
111110
protected abstract void onDriveClientReady();
112111

113-
protected DriveClient getDriveClient() {
112+
DriveClient getDriveClient() {
114113
return mDriveClient;
115114
}
116115

117-
protected DriveResourceClient getDriveResourceClient() {
116+
DriveResourceClient getDriveResourceClient() {
118117
return mDriveResourceClient;
119118
}
120119
}

Diff for: drive/conflict/app/src/main/java/com/google/android/gms/drive/sample/conflict/ConflictResolver.java

+57-83
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import android.content.Context;
1717
import android.content.Intent;
18-
import android.support.annotation.NonNull;
1918
import android.support.v4.content.LocalBroadcastManager;
2019
import android.util.Log;
2120

@@ -32,9 +31,6 @@
3231
import com.google.android.gms.drive.MetadataChangeSet;
3332
import com.google.android.gms.drive.events.CompletionEvent;
3433
import com.google.android.gms.tasks.Continuation;
35-
import com.google.android.gms.tasks.OnFailureListener;
36-
import com.google.android.gms.tasks.OnSuccessListener;
37-
import com.google.android.gms.tasks.Task;
3834

3935
import java.io.InputStream;
4036
import java.io.OutputStream;
@@ -88,96 +84,74 @@ void resolve() {
8884
GoogleSignIn.getClient(mContext, signInOptionsBuilder.build());
8985
signInClient.silentSignIn()
9086
.continueWith(mExecutorService,
91-
new Continuation<GoogleSignInAccount, Void>() {
92-
@Override
93-
public Void then(@NonNull Task<GoogleSignInAccount> signInTask)
94-
throws Exception {
95-
mDriveResourceClient = Drive.getDriveResourceClient(
96-
mContext, signInTask.getResult());
97-
mBaseContent = ConflictUtil.getStringFromInputStream(
98-
mConflictedCompletionEvent.getBaseContentsInputStream());
99-
mModifiedContent = ConflictUtil.getStringFromInputStream(
100-
mConflictedCompletionEvent
101-
.getModifiedContentsInputStream());
102-
return null;
103-
}
87+
(Continuation<GoogleSignInAccount, Void>) signInTask -> {
88+
mDriveResourceClient = Drive.getDriveResourceClient(
89+
mContext, signInTask.getResult());
90+
mBaseContent = ConflictUtil.getStringFromInputStream(
91+
mConflictedCompletionEvent.getBaseContentsInputStream());
92+
mModifiedContent = ConflictUtil.getStringFromInputStream(
93+
mConflictedCompletionEvent
94+
.getModifiedContentsInputStream());
95+
return null;
10496
})
10597
.continueWithTask(mExecutorService,
106-
new Continuation<Void, Task<DriveContents>>() {
107-
@Override
108-
public Task<DriveContents> then(@NonNull Task<Void> task)
109-
throws Exception {
110-
DriveId driveId = mConflictedCompletionEvent.getDriveId();
111-
return mDriveResourceClient.openFile(
112-
driveId.asDriveFile(), DriveFile.MODE_READ_ONLY);
113-
}
98+
task -> {
99+
DriveId driveId = mConflictedCompletionEvent.getDriveId();
100+
return mDriveResourceClient.openFile(
101+
driveId.asDriveFile(), DriveFile.MODE_READ_ONLY);
114102
})
115103
.continueWithTask(mExecutorService,
116-
new Continuation<DriveContents, Task<DriveContents>>() {
117-
@Override
118-
public Task<DriveContents> then(@NonNull Task<DriveContents> task)
119-
throws Exception {
120-
mDriveContents = task.getResult();
121-
InputStream serverInputStream = task.getResult().getInputStream();
122-
mServerContent =
123-
ConflictUtil.getStringFromInputStream(serverInputStream);
124-
return mDriveResourceClient.reopenContentsForWrite(mDriveContents);
125-
}
104+
task -> {
105+
mDriveContents = task.getResult();
106+
InputStream serverInputStream = task.getResult().getInputStream();
107+
mServerContent =
108+
ConflictUtil.getStringFromInputStream(serverInputStream);
109+
return mDriveResourceClient.reopenContentsForWrite(mDriveContents);
126110
})
127111
.continueWithTask(mExecutorService,
128-
new Continuation<DriveContents, Task<Void>>() {
129-
@Override
130-
public Task<Void> then(@NonNull Task<DriveContents> task)
131-
throws Exception {
132-
DriveContents contentsForWrite = task.getResult();
133-
mResolvedContent = ConflictUtil.resolveConflict(
134-
mBaseContent, mServerContent, mModifiedContent);
112+
task -> {
113+
DriveContents contentsForWrite = task.getResult();
114+
mResolvedContent = ConflictUtil.resolveConflict(
115+
mBaseContent, mServerContent, mModifiedContent);
135116

136-
OutputStream outputStream = contentsForWrite.getOutputStream();
137-
try (Writer writer = new OutputStreamWriter(outputStream)) {
138-
writer.write(mResolvedContent);
139-
}
117+
OutputStream outputStream = contentsForWrite.getOutputStream();
118+
try (Writer writer = new OutputStreamWriter(outputStream)) {
119+
writer.write(mResolvedContent);
120+
}
140121

141-
// It is not likely that resolving a conflict will result in another
142-
// conflict, but it can happen if the file changed again while this
143-
// conflict was resolved. Since we already implemented conflict
144-
// resolution and we never want to miss user data, we commit here
145-
// with execution options in conflict-aware mode (otherwise we would
146-
// overwrite server content).
147-
ExecutionOptions executionOptions =
148-
new ExecutionOptions.Builder()
149-
.setNotifyOnCompletion(true)
150-
.setConflictStrategy(
151-
ExecutionOptions
152-
.CONFLICT_STRATEGY_KEEP_REMOTE)
153-
.build();
122+
// It is not likely that resolving a conflict will result in another
123+
// conflict, but it can happen if the file changed again while this
124+
// conflict was resolved. Since we already implemented conflict
125+
// resolution and we never want to miss user data, we commit here
126+
// with execution options in conflict-aware mode (otherwise we would
127+
// overwrite server content).
128+
ExecutionOptions executionOptions =
129+
new ExecutionOptions.Builder()
130+
.setNotifyOnCompletion(true)
131+
.setConflictStrategy(
132+
ExecutionOptions
133+
.CONFLICT_STRATEGY_KEEP_REMOTE)
134+
.build();
154135

155-
// Commit resolved contents.
156-
MetadataChangeSet modifiedMetadataChangeSet =
157-
mConflictedCompletionEvent.getModifiedMetadataChangeSet();
158-
return mDriveResourceClient.commitContents(contentsForWrite,
159-
modifiedMetadataChangeSet, executionOptions);
160-
}
136+
// Commit resolved contents.
137+
MetadataChangeSet modifiedMetadataChangeSet =
138+
mConflictedCompletionEvent.getModifiedMetadataChangeSet();
139+
return mDriveResourceClient.commitContents(contentsForWrite,
140+
modifiedMetadataChangeSet, executionOptions);
161141
})
162-
.addOnSuccessListener(new OnSuccessListener<Void>() {
163-
@Override
164-
public void onSuccess(Void aVoid) {
165-
mConflictedCompletionEvent.dismiss();
166-
Log.d(TAG, "resolved list");
167-
sendResult(mModifiedContent);
168-
}
142+
.addOnSuccessListener(aVoid -> {
143+
mConflictedCompletionEvent.dismiss();
144+
Log.d(TAG, "resolved list");
145+
sendResult(mModifiedContent);
169146
})
170-
.addOnFailureListener(new OnFailureListener() {
171-
@Override
172-
public void onFailure(@NonNull Exception e) {
173-
// The contents cannot be reopened at this point, probably due to
174-
// connectivity, so by snoozing the event we will get it again later.
175-
Log.d(TAG, "Unable to write resolved content, snoozing completion event.",
176-
e);
177-
mConflictedCompletionEvent.snooze();
178-
if (mDriveContents != null) {
179-
mDriveResourceClient.discardContents(mDriveContents);
180-
}
147+
.addOnFailureListener(e -> {
148+
// The contents cannot be reopened at this point, probably due to
149+
// connectivity, so by snoozing the event we will get it again later.
150+
Log.d(TAG, "Unable to write resolved content, snoozing completion event.",
151+
e);
152+
mConflictedCompletionEvent.snooze();
153+
if (mDriveContents != null) {
154+
mDriveResourceClient.discardContents(mDriveContents);
181155
}
182156
});
183157
// [END resolve_conflict]

0 commit comments

Comments
 (0)