Skip to content

Commit c9bdb8f

Browse files
committed
Migrate to connectionless client
Change-Id: I2669eb205bf0f47226a226063af871a22a5f9279
1 parent 7ac7cba commit c9bdb8f

File tree

10 files changed

+481
-525
lines changed

10 files changed

+481
-525
lines changed

app/build.gradle

Lines changed: 7 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 23
5-
buildToolsVersion "23.0.1"
4+
compileSdkVersion 26
5+
buildToolsVersion "26.0.1"
66

77
defaultConfig {
88
applicationId "com.google.android.gms.drive.sample.conflict"
9-
minSdkVersion 9
10-
targetSdkVersion 23
9+
minSdkVersion 21
10+
targetSdkVersion 26
1111
versionCode 2
1212
versionName "1.1"
1313
}
@@ -20,6 +20,7 @@ android {
2020
}
2121

2222
dependencies {
23-
compile 'com.android.support:support-v4:23.0.1'
24-
compile 'com.google.android.gms:play-services-drive:8.4.0'
23+
compile 'com.android.support:support-v4:26.0.2'
24+
compile 'com.google.android.gms:play-services-auth:11.8.0-SNAPSHOT'
25+
compile 'com.google.android.gms:play-services-drive:11.8.0-SNAPSHOT'
2526
}

app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,12 @@ limitations under the License.
2727
<!-- [START_EXCLUDE silent] -->
2828
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
2929
<!-- [END_EXCLUDE] -->
30-
<!-- Begin DriveEventService service declaration -->
3130
<service android:name=".MyDriveEventService"
3231
android:exported="true" >
3332
<intent-filter>
3433
<action android:name="com.google.android.gms.drive.events.HANDLE_EVENT" />
3534
</intent-filter>
3635
</service>
37-
<!-- End DriveEventService service declaration -->
3836
<!-- [START_EXCLUDE silent] -->
3937
<activity
4038
android:name=".MainActivity"
Lines changed: 66 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Copyright 2014 Google Inc. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
@@ -14,137 +14,119 @@
1414

1515
package com.google.android.gms.drive.sample.conflict;
1616

17-
import com.google.android.gms.common.ConnectionResult;
18-
import com.google.android.gms.common.GoogleApiAvailability;
19-
import com.google.android.gms.common.api.GoogleApiClient;
20-
import com.google.android.gms.drive.Drive;
21-
2217
import android.app.Activity;
2318
import android.content.Intent;
24-
import android.content.IntentSender.SendIntentException;
25-
import android.os.Bundle;
2619
import android.util.Log;
2720
import android.widget.Toast;
2821

22+
import com.google.android.gms.auth.api.signin.GoogleSignIn;
23+
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
24+
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
25+
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
26+
import com.google.android.gms.drive.Drive;
27+
import com.google.android.gms.drive.DriveClient;
28+
import com.google.android.gms.drive.DriveResourceClient;
29+
import com.google.android.gms.tasks.Task;
30+
2931
/**
3032
* An abstract activity that handles authorization and connection to the Drive
3133
* services.
3234
*/
33-
public abstract class BaseDemoActivity extends Activity implements
34-
GoogleApiClient.ConnectionCallbacks,
35-
GoogleApiClient.OnConnectionFailedListener {
36-
35+
public abstract class BaseDemoActivity extends Activity {
3736
private static final String TAG = "BaseDemoActivity";
3837

3938
/**
4039
* Request code for auto Google Play Services error resolution.
4140
*/
42-
protected static final int REQUEST_CODE_RESOLUTION = 1;
41+
protected static final int REQUEST_CODE_SIGN_IN = 0;
4342

4443
/**
45-
* Google API client.
44+
* Handles high-level drive functions like sync
4645
*/
47-
private GoogleApiClient mGoogleApiClient;
46+
private DriveClient mDriveClient;
4847

4948
/**
50-
* Called when activity is started. A connection to Drive services needs to
51-
* be initiated as soon as the activity is started. Registers
52-
* {@code ConnectionCallbacks} and {@code OnConnectionFailedListener} on the
53-
* activities itself.
49+
* Handle access to Drive resources/files.
5450
*/
51+
private DriveResourceClient mDriveResourceClient;
52+
5553
@Override
5654
protected void onStart() {
5755
super.onStart();
58-
if (mGoogleApiClient == null) {
59-
mGoogleApiClient = new GoogleApiClient.Builder(this)
60-
.addApi(Drive.API)
61-
// Scopes Drive.SCOPE_FILE and Drive.SCOPE_APPFOLDER are added here to ensure
62-
// we can resolve conflicts on any file touched by our app.
63-
.addScope(Drive.SCOPE_FILE)
64-
.addScope(Drive.SCOPE_APPFOLDER)
65-
.addConnectionCallbacks(this)
66-
.addOnConnectionFailedListener(this)
67-
// If supporting multiple accounts setAccountName could be used here to define
68-
// which of the accounts the GoogleApiClient should use. CompletionEvents are
69-
// tied to a specific account so when creating a GoogleApiClient to handle them
70-
// (like in MyDriveEventService) use the account name from the CompletionEvent
71-
// to set the account name of the GoogleApiClient.
72-
.build();
73-
}
74-
mGoogleApiClient.connect();
75-
}
76-
77-
/**
78-
* Called when activity is stopped. Connection to Drive service needs to
79-
* be disconnected as soon as an activity is stopped.
80-
*/
81-
@Override
82-
protected void onStop() {
83-
if (mGoogleApiClient != null) {
84-
mGoogleApiClient.disconnect();
85-
}
86-
super.onStop();
56+
signIn();
8757
}
8858

8959
/**
9060
* Handles resolution callbacks.
9161
*/
9262
@Override
93-
protected void onActivityResult(int requestCode, int resultCode,
94-
Intent data) {
63+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
9564
super.onActivityResult(requestCode, resultCode, data);
96-
if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) {
97-
mGoogleApiClient.connect();
98-
}
99-
}
65+
if (requestCode == REQUEST_CODE_SIGN_IN) {
66+
if (resultCode != RESULT_OK) {
67+
// Sign-in may fail or be cancelled by the user. For this sample, sign-in is
68+
// required and is fatal. For apps where sign-in is optional, handle appropriately
69+
Log.e(TAG, "Sign-in failed.");
70+
finish();
71+
return;
72+
}
10073

101-
/**
102-
* Called when {@code mGoogleApiClient} is connected.
103-
*/
104-
@Override
105-
public void onConnected(Bundle connectionHint) {
106-
Log.i(TAG, "GoogleApiClient connected");
74+
Task<GoogleSignInAccount> getAccountTask =
75+
GoogleSignInClient.getGoogleSignInAccountFromIntent(data);
76+
if (getAccountTask.isSuccessful()) {
77+
initializeDriveClient(getAccountTask.getResult());
78+
} else {
79+
Log.e(TAG, "Sign-in failed.");
80+
finish();
81+
}
82+
}
10783
}
10884

10985
/**
110-
* Called when {@code mGoogleApiClient} is disconnected.
86+
* Starts the sign-in process and initializes the Drive client.
11187
*/
112-
@Override
113-
public void onConnectionSuspended(int cause) {
114-
Log.i(TAG, "GoogleApiClient connection suspended");
88+
protected void signIn() {
89+
GoogleSignInOptions signInOptions =
90+
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
91+
.requestScopes(Drive.SCOPE_FILE)
92+
.requestScopes(Drive.SCOPE_APPFOLDER)
93+
.build();
94+
GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(this, signInOptions);
95+
GoogleSignInAccount signInAccount = googleSignInClient.getLastSignedInAccount();
96+
if (signInAccount != null) {
97+
initializeDriveClient(signInAccount);
98+
} else {
99+
startActivityForResult(googleSignInClient.getSignInIntent(), REQUEST_CODE_SIGN_IN);
100+
}
115101
}
116102

117103
/**
118-
* Called when {@code mGoogleApiClient} is trying to connect but failed.
119-
* Handle {@code result.getResolution()} if there is a resolution is
120-
* available.
104+
* Continues the sign-in process, initializing the DriveResourceClient with the current
105+
* user's account.
121106
*/
122-
@Override
123-
public void onConnectionFailed(ConnectionResult result) {
124-
Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
125-
if (!result.hasResolution()) {
126-
// show the localized error dialog.
127-
GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
128-
return;
129-
}
130-
try {
131-
result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
132-
} catch (SendIntentException e) {
133-
Log.e(TAG, "Exception while starting resolution activity", e);
134-
}
107+
private void initializeDriveClient(GoogleSignInAccount signInAccount) {
108+
mDriveClient = Drive.getDriveClient(getApplicationContext(), signInAccount);
109+
mDriveResourceClient = Drive.getDriveResourceClient(getApplicationContext(), signInAccount);
110+
onDriveClientReady();
135111
}
136112

137113
/**
138114
* Shows a toast message.
139115
*/
140-
public void showMessage(String message) {
116+
protected void showMessage(String message) {
141117
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
142118
}
143119

144120
/**
145-
* Getter for the {@code GoogleApiClient}.
121+
* Called after the user has signed in and the Drive client has been initialized.
146122
*/
147-
public GoogleApiClient getGoogleApiClient() {
148-
return mGoogleApiClient;
123+
protected abstract void onDriveClientReady();
124+
125+
protected DriveClient getDriveClient() {
126+
return mDriveClient;
127+
}
128+
129+
protected DriveResourceClient getDriveResourceClient() {
130+
return mDriveResourceClient;
149131
}
150132
}

0 commit comments

Comments
 (0)