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 f62a351
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 38 deletions.
28 changes: 10 additions & 18 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand Down Expand Up @@ -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'
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f62a351

Please sign in to comment.