Skip to content

Commit f62a351

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

File tree

5 files changed

+42
-38
lines changed

5 files changed

+42
-38
lines changed

app/build.gradle

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,29 +85,20 @@ android {
8585
}
8686
}
8787

88-
lintOptions {
89-
disable 'MissingTranslation'
90-
disable 'MissingQuantity'
91-
disable 'ImpliedQuantity'
92-
93-
/*
94-
* Added for:
95-
* Invalid package reference in library; not included in Android:
96-
* javax.servlet.http. Referenced from com.dropbox.core.DbxStandardSessionStore.
97-
*/
98-
disable 'InvalidPackage'
99-
100-
checkDependencies true
101-
}
10288

10389
compileOptions {
10490
sourceCompatibility JavaVersion.VERSION_11
10591
targetCompatibility JavaVersion.VERSION_11
10692
}
107-
10893
packagingOptions {
109-
exclude 'META-INF/DEPENDENCIES'
110-
exclude 'plugin.properties'
94+
resources {
95+
excludes += ['META-INF/DEPENDENCIES', 'plugin.properties']
96+
}
97+
}
98+
99+
lint {
100+
checkDependencies true
101+
disable 'MissingTranslation', 'MissingQuantity', 'ImpliedQuantity', 'InvalidPackage'
111102
}
112103
}
113104

@@ -174,8 +165,9 @@ dependencies {
174165
// implementation 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
175166
// implementation 'com.google.apis:google-api-services-drive:v3-rev110-1.23.0'
176167

168+
// implementation 'com.google.android.gms:play-services:17.0.0'
169+
implementation 'com.google.android.gms:play-services-drive:17.0.0'
177170
implementation 'com.google.android.gms:play-services-auth:19.0.0'
178-
// implementation 'com.google.android.gms:play-services-drive:17.0.0'
179171
implementation 'com.google.http-client:google-http-client-gson:1.26.0'
180172
implementation('com.google.api-client:google-api-client-android:1.26.0') {
181173
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) })

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ buildscript {
44

55
def versions = [:]
66

7-
versions.android_gradle_plugin = '7.0.3'
7+
versions.android_gradle_plugin = '7.2.1'
88

99
versions.kotlin = '1.6.0'
1010

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip

0 commit comments

Comments
 (0)