Skip to content

Commit de0bf25

Browse files
author
Arco Xu
committed
Update quickstart sample apps with new changes in GoogleSignIn and GoogleSignInClient. Correct the play services version.
Change-Id: I6d30f33949a234200712ced71ed8e4be06b32393
1 parent bda30cc commit de0bf25

File tree

2 files changed

+17
-45
lines changed

2 files changed

+17
-45
lines changed

app/build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 25
5-
buildToolsVersion '25.0.0'
4+
compileSdkVersion 26
5+
buildToolsVersion '25.0.2'
66

77
defaultConfig {
88
applicationId "com.google.android.gms.drive.sample.quickstart"
99
minSdkVersion 14
10-
targetSdkVersion 25
10+
targetSdkVersion 26
1111
versionCode 1
1212
versionName "1.0"
1313
}
@@ -21,8 +21,8 @@ android {
2121

2222
dependencies {
2323
compile fileTree(dir: 'libs', include: ['*.jar'])
24-
compile 'com.android.support:appcompat-v7:25.3.1'
24+
compile 'com.android.support:appcompat-v7:26.1.0'
2525

26-
compile 'com.google.android.gms:play-services-drive:11.4.0'
27-
compile 'com.google.android.gms:play-services-auth:11.4.0'
26+
compile 'com.google.android.gms:play-services-drive:11.6.0'
27+
compile 'com.google.android.gms:play-services-auth:11.6.0'
2828
}

app/src/main/java/com/google/android/gms/drive/sample/quickstart/MainActivity.java

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616
import android.app.Activity;
1717
import android.content.Intent;
1818
import android.content.IntentSender;
19-
import android.content.IntentSender.SendIntentException;
2019
import android.graphics.Bitmap;
2120
import android.os.Bundle;
2221
import android.provider.MediaStore;
2322
import android.support.annotation.NonNull;
2423
import android.support.annotation.Nullable;
2524
import android.util.Log;
2625
import com.google.android.gms.auth.api.signin.GoogleSignIn;
27-
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
2826
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
2927
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
3028
import com.google.android.gms.drive.CreateFileActivityOptions;
@@ -33,8 +31,8 @@
3331
import com.google.android.gms.drive.DriveContents;
3432
import com.google.android.gms.drive.DriveResourceClient;
3533
import com.google.android.gms.drive.MetadataChangeSet;
34+
import com.google.android.gms.tasks.Continuation;
3635
import com.google.android.gms.tasks.OnFailureListener;
37-
import com.google.android.gms.tasks.OnSuccessListener;
3836
import com.google.android.gms.tasks.Task;
3937
import java.io.ByteArrayOutputStream;
4038
import java.io.IOException;
@@ -87,9 +85,9 @@ private void saveFileToDrive() {
8785
mDriveResourceClient
8886
.createContents()
8987
.continueWithTask(
90-
new Continuation<DriveContents, Task<IntentSender>>() {
88+
new Continuation<DriveContents, Task<Void>>() {
9189
@Override
92-
public Task<IntentSender> then(@NonNull Task<DriveContents> task) throws Exception {
90+
public Task<Void> then(@NonNull Task<DriveContents> task) throws Exception {
9391
return createFileIntentSender(task.getResult(), image);
9492
}
9593
})
@@ -154,10 +152,14 @@ protected void onActivityResult(final int requestCode, final int resultCode, fin
154152
// Called after user is signed in.
155153
if (resultCode == RESULT_OK) {
156154
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);
161163
}
162164
break;
163165
case REQUEST_CODE_CAPTURE_IMAGE:
@@ -183,34 +185,4 @@ protected void onActivityResult(final int requestCode, final int resultCode, fin
183185
break;
184186
}
185187
}
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-
}
216188
}

0 commit comments

Comments
 (0)