From f62a351710264609188bc123022c236118ae7eec Mon Sep 17 00:00:00 2001 From: Nicholas Harrison Date: Sat, 12 Feb 2022 11:42:11 +1100 Subject: [PATCH] Tried new place for auth code, doesn't sign in --- app/build.gradle | 28 ++++++--------- .../android/repos/GoogleDriveClient.java | 36 +++++++++++++++---- .../googledrive/GoogleDriveRepoActivity.kt | 12 +------ build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- 5 files changed, 42 insertions(+), 38 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 11737e417..4acd74076 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -85,29 +85,20 @@ android { } } - lintOptions { - disable 'MissingTranslation' - disable 'MissingQuantity' - disable 'ImpliedQuantity' - - /* - * Added for: - * Invalid package reference in library; not included in Android: - * javax.servlet.http. Referenced from com.dropbox.core.DbxStandardSessionStore. - */ - disable 'InvalidPackage' - - checkDependencies true - } compileOptions { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 } - packagingOptions { - exclude 'META-INF/DEPENDENCIES' - exclude 'plugin.properties' + resources { + excludes += ['META-INF/DEPENDENCIES', 'plugin.properties'] + } + } + + lint { + checkDependencies true + disable 'MissingTranslation', 'MissingQuantity', 'ImpliedQuantity', 'InvalidPackage' } } @@ -174,8 +165,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' diff --git a/app/src/main/java/com/orgzly/android/repos/GoogleDriveClient.java b/app/src/main/java/com/orgzly/android/repos/GoogleDriveClient.java index afa4674e4..7749c142a 100644 --- a/app/src/main/java/com/orgzly/android/repos/GoogleDriveClient.java +++ b/app/src/main/java/com/orgzly/android/repos/GoogleDriveClient.java @@ -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; @@ -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 pathIds; { pathIds = new HashMap<>(); @@ -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 { @@ -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); diff --git a/app/src/main/java/com/orgzly/android/ui/repo/googledrive/GoogleDriveRepoActivity.kt b/app/src/main/java/com/orgzly/android/ui/repo/googledrive/GoogleDriveRepoActivity.kt index 13e69bfbe..ae328cb97 100644 --- a/app/src/main/java/com/orgzly/android/ui/repo/googledrive/GoogleDriveRepoActivity.kt +++ b/app/src/main/java/com/orgzly/android/ui/repo/googledrive/GoogleDriveRepoActivity.kt @@ -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) }) diff --git a/build.gradle b/build.gradle index 1c8774cec..cf1f2b2a6 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ buildscript { def versions = [:] - versions.android_gradle_plugin = '7.0.3' + versions.android_gradle_plugin = '7.2.1' versions.kotlin = '1.6.0' diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 836d9fac8..2dea15d34 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip