|
1 | 1 | /*
|
2 | 2 | * Copyright 2014 Google Inc. All Rights Reserved.
|
3 | 3 | *
|
4 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | 5 | * in compliance with the License. You may obtain a copy of the License at
|
6 | 6 | *
|
7 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
8 | 8 | *
|
9 |
| - * Unless required by applicable law or agreed to in writing, software distributed under the |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the |
10 | 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
11 | 11 | * express or implied. See the License for the specific language governing permissions and
|
12 | 12 | * limitations under the License.
|
|
15 | 15 |
|
16 | 16 | import android.content.Context;
|
17 | 17 | import android.content.Intent;
|
18 |
| -import android.support.annotation.NonNull; |
19 | 18 | import android.support.v4.content.LocalBroadcastManager;
|
20 | 19 | import android.util.Log;
|
21 | 20 |
|
|
32 | 31 | import com.google.android.gms.drive.MetadataChangeSet;
|
33 | 32 | import com.google.android.gms.drive.events.CompletionEvent;
|
34 | 33 | 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; |
38 | 34 |
|
39 | 35 | import java.io.InputStream;
|
40 | 36 | import java.io.OutputStream;
|
@@ -88,103 +84,81 @@ void resolve() {
|
88 | 84 | GoogleSignIn.getClient(mContext, signInOptionsBuilder.build());
|
89 | 85 | signInClient.silentSignIn()
|
90 | 86 | .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; |
104 | 96 | })
|
105 | 97 | .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); |
114 | 102 | })
|
115 | 103 | .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); |
126 | 110 | })
|
127 | 111 | .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); |
135 | 116 |
|
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 | + } |
140 | 121 |
|
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(); |
154 | 135 |
|
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); |
161 | 141 | })
|
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); |
169 | 146 | })
|
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); |
181 | 155 | }
|
182 | 156 | });
|
183 | 157 | // [END resolve_conflict]
|
184 | 158 | }
|
185 | 159 |
|
186 | 160 | /**
|
187 |
| - * Notify the UI that the list should be updated |
| 161 | + * Notify the UI that the list should be updated. |
188 | 162 | *
|
189 | 163 | * @param resolution Resolved grocery list.
|
190 | 164 | */
|
|
0 commit comments