Skip to content

Commit 72812eb

Browse files
author
Nicholas Harrison
committed
Tried new place for auth code, doesn't sign in
1 parent 1a9225e commit 72812eb

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

app/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,9 @@ dependencies {
174174
// implementation 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
175175
// implementation 'com.google.apis:google-api-services-drive:v3-rev110-1.23.0'
176176

177+
// implementation 'com.google.android.gms:play-services:17.0.0'
178+
implementation 'com.google.android.gms:play-services-drive:17.0.0'
177179
implementation 'com.google.android.gms:play-services-auth:19.0.0'
178-
// implementation 'com.google.android.gms:play-services-drive:17.0.0'
179180
implementation 'com.google.http-client:google-http-client-gson:1.26.0'
180181
implementation('com.google.api-client:google-api-client-android:1.26.0') {
181182
exclude group: 'org.apache.httpcomponents'

app/src/main/java/com/orgzly/android/repos/GoogleDriveClient.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.net.Uri;
77

88
import com.google.android.gms.auth.api.signin.GoogleSignIn;
9+
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
910

1011
import com.google.api.client.extensions.android.http.AndroidHttp;
1112
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
@@ -48,8 +49,15 @@ public class GoogleDriveClient {
4849

4950
private final Context mContext;
5051
private final long repoId;
52+
53+
private GoogleSignInAccount mGoogleAccount;
5154
private Drive mDriveService;
5255

56+
// Make static? Or maybe need to serialize or manage token.
57+
// SharedPreferences or SQLite
58+
// https://stackoverflow.com/questions/19274063/object-becomes-null
59+
// Thought that Google sign-in would handle it, but it's not working or not building.
60+
5361
private Map<String, String> pathIds;
5462
{
5563
pathIds = new HashMap<>();
@@ -63,14 +71,8 @@ public GoogleDriveClient(Context context, long id) {
6371
repoId = id;
6472
}
6573

66-
public void setService(Drive driveService) {
67-
mDriveService = driveService;
68-
}
69-
7074
public boolean isLinked() {
71-
// Check for existing Google Sign In account, if the user is already signed in
72-
// the GoogleSignInAccount will be non-null.
73-
return GoogleSignIn.getLastSignedInAccount(mContext) != null;
75+
return setService();
7476
}
7577

7678
private void linkedOrThrow() throws IOException {
@@ -79,6 +81,26 @@ private void linkedOrThrow() throws IOException {
7981
}
8082
}
8183

84+
public boolean setService() {
85+
// Check for existing Google Sign In account, if the user is already signed in
86+
// the GoogleSignInAccount will be non-null.
87+
if (mDriveService == null) {
88+
mGoogleAccount = GoogleSignIn.getLastSignedInAccount(mContext);
89+
if (mGoogleAccount != null) {
90+
// Use the authenticated account to sign in to the Drive service.
91+
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(mContext, Collections.singleton(DriveScopes.DRIVE));
92+
credential.setSelectedAccount(mGoogleAccount.getAccount());
93+
mDriveService = Drive.Builder(AndroidHttp.newCompatibleTransport(),
94+
mDriveService = new Drive.Builder(AndroidHttp.newCompatibleTransport(),
95+
new GsonFactory(),
96+
credential)
97+
.setApplicationName("Orgzly")
98+
.build();
99+
}
100+
}
101+
return mDriveService != null;
102+
}
103+
82104
private String findId(String path) throws IOException {
83105
if (pathIds.containsKey(path)) {
84106
return pathIds.get(path);

app/src/main/java/com/orgzly/android/ui/repo/googledrive/GoogleDriveRepoActivity.kt

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,19 +160,9 @@ class GoogleDriveRepoActivity : CommonActivity() {
160160
GoogleSignIn.getSignedInAccountFromIntent(result)
161161
.addOnSuccessListener({ googleAccount->
162162
Log.d(TAG, "Signed in as " + googleAccount.getEmail())
163-
// Use the authenticated account to sign in to the Drive service.
164-
val credential = GoogleAccountCredential.usingOAuth2(
165-
this, Collections.singleton(DriveScopes.DRIVE))
166-
credential.setSelectedAccount(googleAccount.getAccount())
167-
val googleDriveService = Drive.Builder(
168-
AndroidHttp.newCompatibleTransport(),
169-
GsonFactory(),
170-
credential)
171-
.setApplicationName("Orgzly")
172-
.build()
173163
// The DriveServiceHelper encapsulates all REST API and SAF functionality.
174164
// Its instantiation is required before handling any onClick actions.
175-
client.setService(googleDriveService);
165+
client.setService();
176166
showSnackbar(R.string.message_google_drive_linked)
177167
})
178168
.addOnFailureListener({ exception-> Log.d(TAG, "Unable to sign in." + exception) })

0 commit comments

Comments
 (0)