|
1 |
| -/** |
| 1 | +/* |
2 | 2 | * Copyright 2014 Google Inc. All Rights Reserved.
|
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
|
14 | 14 |
|
15 | 15 | package com.google.android.gms.drive.sample.conflict;
|
16 | 16 |
|
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 |
| - |
22 | 17 | import android.app.Activity;
|
23 | 18 | import android.content.Intent;
|
24 |
| -import android.content.IntentSender.SendIntentException; |
25 |
| -import android.os.Bundle; |
26 | 19 | import android.util.Log;
|
27 | 20 | import android.widget.Toast;
|
28 | 21 |
|
| 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 | + |
29 | 31 | /**
|
30 | 32 | * An abstract activity that handles authorization and connection to the Drive
|
31 | 33 | * services.
|
32 | 34 | */
|
33 |
| -public abstract class BaseDemoActivity extends Activity implements |
34 |
| - GoogleApiClient.ConnectionCallbacks, |
35 |
| - GoogleApiClient.OnConnectionFailedListener { |
36 |
| - |
| 35 | +public abstract class BaseDemoActivity extends Activity { |
37 | 36 | private static final String TAG = "BaseDemoActivity";
|
38 | 37 |
|
39 | 38 | /**
|
40 | 39 | * Request code for auto Google Play Services error resolution.
|
41 | 40 | */
|
42 |
| - protected static final int REQUEST_CODE_RESOLUTION = 1; |
| 41 | + protected static final int REQUEST_CODE_SIGN_IN = 0; |
43 | 42 |
|
44 | 43 | /**
|
45 |
| - * Google API client. |
| 44 | + * Handles high-level drive functions like sync |
46 | 45 | */
|
47 |
| - private GoogleApiClient mGoogleApiClient; |
| 46 | + private DriveClient mDriveClient; |
48 | 47 |
|
49 | 48 | /**
|
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. |
54 | 50 | */
|
| 51 | + private DriveResourceClient mDriveResourceClient; |
| 52 | + |
55 | 53 | @Override
|
56 | 54 | protected void onStart() {
|
57 | 55 | 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(); |
87 | 57 | }
|
88 | 58 |
|
89 | 59 | /**
|
90 | 60 | * Handles resolution callbacks.
|
91 | 61 | */
|
92 | 62 | @Override
|
93 |
| - protected void onActivityResult(int requestCode, int resultCode, |
94 |
| - Intent data) { |
| 63 | + protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
95 | 64 | 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 | + } |
100 | 73 |
|
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 | + } |
107 | 83 | }
|
108 | 84 |
|
109 | 85 | /**
|
110 |
| - * Called when {@code mGoogleApiClient} is disconnected. |
| 86 | + * Starts the sign-in process and initializes the Drive client. |
111 | 87 | */
|
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 | + } |
115 | 101 | }
|
116 | 102 |
|
117 | 103 | /**
|
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. |
121 | 106 | */
|
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(); |
135 | 111 | }
|
136 | 112 |
|
137 | 113 | /**
|
138 | 114 | * Shows a toast message.
|
139 | 115 | */
|
140 |
| - public void showMessage(String message) { |
| 116 | + protected void showMessage(String message) { |
141 | 117 | Toast.makeText(this, message, Toast.LENGTH_LONG).show();
|
142 | 118 | }
|
143 | 119 |
|
144 | 120 | /**
|
145 |
| - * Getter for the {@code GoogleApiClient}. |
| 121 | + * Called after the user has signed in and the Drive client has been initialized. |
146 | 122 | */
|
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; |
149 | 131 | }
|
150 | 132 | }
|
0 commit comments