16
16
import android .app .Activity ;
17
17
import android .content .Intent ;
18
18
import android .content .IntentSender ;
19
- import android .content .IntentSender .SendIntentException ;
20
19
import android .graphics .Bitmap ;
21
20
import android .os .Bundle ;
22
21
import android .provider .MediaStore ;
23
22
import android .support .annotation .NonNull ;
24
23
import android .support .annotation .Nullable ;
25
24
import android .util .Log ;
26
25
import com .google .android .gms .auth .api .signin .GoogleSignIn ;
27
- import com .google .android .gms .auth .api .signin .GoogleSignInAccount ;
28
26
import com .google .android .gms .auth .api .signin .GoogleSignInClient ;
29
27
import com .google .android .gms .auth .api .signin .GoogleSignInOptions ;
30
28
import com .google .android .gms .drive .CreateFileActivityOptions ;
33
31
import com .google .android .gms .drive .DriveContents ;
34
32
import com .google .android .gms .drive .DriveResourceClient ;
35
33
import com .google .android .gms .drive .MetadataChangeSet ;
34
+ import com .google .android .gms .tasks .Continuation ;
36
35
import com .google .android .gms .tasks .OnFailureListener ;
37
- import com .google .android .gms .tasks .OnSuccessListener ;
38
36
import com .google .android .gms .tasks .Task ;
39
37
import java .io .ByteArrayOutputStream ;
40
38
import java .io .IOException ;
@@ -87,9 +85,9 @@ private void saveFileToDrive() {
87
85
mDriveResourceClient
88
86
.createContents ()
89
87
.continueWithTask (
90
- new Continuation <DriveContents , Task <IntentSender >>() {
88
+ new Continuation <DriveContents , Task <Void >>() {
91
89
@ Override
92
- public Task <IntentSender > then (@ NonNull Task <DriveContents > task ) throws Exception {
90
+ public Task <Void > then (@ NonNull Task <DriveContents > task ) throws Exception {
93
91
return createFileIntentSender (task .getResult (), image );
94
92
}
95
93
})
@@ -154,10 +152,14 @@ protected void onActivityResult(final int requestCode, final int resultCode, fin
154
152
// Called after user is signed in.
155
153
if (resultCode == RESULT_OK ) {
156
154
Log .i (TAG , "Signed in successfully." );
157
- // Execute task to get sign in account.
158
- Task <GoogleSignInAccount > task =
159
- mGoogleSignInClient .getGoogleSignInAccountFromIntent (data );
160
- updateViewWithGoogleSignInAccountTask (task );
155
+ // Use the last signed in account here since it already have a Drive scope.
156
+ mDriveClient = Drive .getDriveClient (this , GoogleSignIn .getLastSignedInAccount (this ));
157
+ // Build a drive resource client.
158
+ mDriveResourceClient =
159
+ Drive .getDriveResourceClient (this , GoogleSignIn .getLastSignedInAccount (this ));
160
+ // Start camera.
161
+ startActivityForResult (
162
+ new Intent (MediaStore .ACTION_IMAGE_CAPTURE ), REQUEST_CODE_CAPTURE_IMAGE );
161
163
}
162
164
break ;
163
165
case REQUEST_CODE_CAPTURE_IMAGE :
@@ -183,34 +185,4 @@ protected void onActivityResult(final int requestCode, final int resultCode, fin
183
185
break ;
184
186
}
185
187
}
186
-
187
- /**
188
- * Update view with Google SignIn account task. If task was successful, build the drive client and
189
- * drive resource client, then start the camera. Otherwise print out the exception stacktrace.
190
- */
191
- private void updateViewWithGoogleSignInAccountTask (Task <GoogleSignInAccount > task ) {
192
- Log .i (TAG , "Update view with sign in account task" );
193
- task .addOnSuccessListener (
194
- new OnSuccessListener <GoogleSignInAccount >() {
195
- @ Override
196
- public void onSuccess (GoogleSignInAccount googleSignInAccount ) {
197
- Log .i (TAG , "Sign in success" );
198
- // Build a drive client.
199
- mDriveClient = Drive .getDriveClient (getApplicationContext (), googleSignInAccount );
200
- // Build a drive resource client.
201
- mDriveResourceClient =
202
- Drive .getDriveResourceClient (getApplicationContext (), googleSignInAccount );
203
- // Start camera.
204
- startActivityForResult (
205
- new Intent (MediaStore .ACTION_IMAGE_CAPTURE ), REQUEST_CODE_CAPTURE_IMAGE );
206
- }
207
- })
208
- .addOnFailureListener (
209
- new OnFailureListener () {
210
- @ Override
211
- public void onFailure (@ NonNull Exception e ) {
212
- Log .w (TAG , "Sign in failed" , e );
213
- }
214
- });
215
- }
216
188
}
0 commit comments