Skip to content

Commit

Permalink
Tried new place for auth code, doesn't sign in
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Harrison committed Jun 24, 2022
1 parent 1a9225e commit 72812eb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ dependencies {
// implementation 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
// implementation 'com.google.apis:google-api-services-drive:v3-rev110-1.23.0'

// implementation 'com.google.android.gms:play-services:17.0.0'
implementation 'com.google.android.gms:play-services-drive:17.0.0'
implementation 'com.google.android.gms:play-services-auth:19.0.0'
// implementation 'com.google.android.gms:play-services-drive:17.0.0'
implementation 'com.google.http-client:google-http-client-gson:1.26.0'
implementation('com.google.api-client:google-api-client-android:1.26.0') {
exclude group: 'org.apache.httpcomponents'
Expand Down
36 changes: 29 additions & 7 deletions app/src/main/java/com/orgzly/android/repos/GoogleDriveClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.net.Uri;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;

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

private final Context mContext;
private final long repoId;

private GoogleSignInAccount mGoogleAccount;
private Drive mDriveService;

// Make static? Or maybe need to serialize or manage token.
// SharedPreferences or SQLite
// https://stackoverflow.com/questions/19274063/object-becomes-null
// Thought that Google sign-in would handle it, but it's not working or not building.

private Map<String, String> pathIds;
{
pathIds = new HashMap<>();
Expand All @@ -63,14 +71,8 @@ public GoogleDriveClient(Context context, long id) {
repoId = id;
}

public void setService(Drive driveService) {
mDriveService = driveService;
}

public boolean isLinked() {
// Check for existing Google Sign In account, if the user is already signed in
// the GoogleSignInAccount will be non-null.
return GoogleSignIn.getLastSignedInAccount(mContext) != null;
return setService();
}

private void linkedOrThrow() throws IOException {
Expand All @@ -79,6 +81,26 @@ private void linkedOrThrow() throws IOException {
}
}

public boolean setService() {
// Check for existing Google Sign In account, if the user is already signed in
// the GoogleSignInAccount will be non-null.
if (mDriveService == null) {
mGoogleAccount = GoogleSignIn.getLastSignedInAccount(mContext);
if (mGoogleAccount != null) {
// Use the authenticated account to sign in to the Drive service.
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(mContext, Collections.singleton(DriveScopes.DRIVE));
credential.setSelectedAccount(mGoogleAccount.getAccount());
mDriveService = Drive.Builder(AndroidHttp.newCompatibleTransport(),
mDriveService = new Drive.Builder(AndroidHttp.newCompatibleTransport(),
new GsonFactory(),
credential)
.setApplicationName("Orgzly")
.build();
}
}
return mDriveService != null;
}

private String findId(String path) throws IOException {
if (pathIds.containsKey(path)) {
return pathIds.get(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,9 @@ class GoogleDriveRepoActivity : CommonActivity() {
GoogleSignIn.getSignedInAccountFromIntent(result)
.addOnSuccessListener({ googleAccount->
Log.d(TAG, "Signed in as " + googleAccount.getEmail())
// Use the authenticated account to sign in to the Drive service.
val credential = GoogleAccountCredential.usingOAuth2(
this, Collections.singleton(DriveScopes.DRIVE))
credential.setSelectedAccount(googleAccount.getAccount())
val googleDriveService = Drive.Builder(
AndroidHttp.newCompatibleTransport(),
GsonFactory(),
credential)
.setApplicationName("Orgzly")
.build()
// The DriveServiceHelper encapsulates all REST API and SAF functionality.
// Its instantiation is required before handling any onClick actions.
client.setService(googleDriveService);
client.setService();
showSnackbar(R.string.message_google_drive_linked)
})
.addOnFailureListener({ exception-> Log.d(TAG, "Unable to sign in." + exception) })
Expand Down

0 comments on commit 72812eb

Please sign in to comment.