66import android .net .Uri ;
77
88import com .google .android .gms .auth .api .signin .GoogleSignIn ;
9+ import com .google .android .gms .auth .api .signin .GoogleSignInAccount ;
910
1011import com .google .api .client .extensions .android .http .AndroidHttp ;
1112import 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 );
0 commit comments