0

I have an SplashActivity class that logins to drive API:

private void handleSignInIntent(Intent data) {

    GoogleSignIn.getSignedInAccountFromIntent(data).addOnSuccessListener(new OnSuccessListener<GoogleSignInAccount>() {
        @Override
        public void onSuccess(GoogleSignInAccount googleSignInAccount) {

            GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(SplashActivity.this, Collections.singleton(DriveScopes.DRIVE_FILE));
            credential.setSelectedAccount(googleSignInAccount.getAccount());

            Drive googleDriveService = new Drive.Builder(
                    AndroidHttp.newCompatibleTransport(),
                    new GsonFactory(), credential)
                    .setApplicationName("MyApp")
                    .build();

            DriveServiceHelper driveServiceHelper = new DriveServiceHelper(googleDriveService); //Need the driveServiceHelper
            startApp(); //Goes to Main Activity

        }
    })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                }
            });
}

Which creates DriveServiceHelper instance. I need to use this instance in the MainActivity like so:

private void uploadFile()
{
    String filePath = "path";
    driveServiceHelper.createFile(filePath).addOnSuccessListener(new OnSuccessListener<String>() { //Need to use the driveServiceHelper instance
        @Override
        public void onSuccess(String s) {
            Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_LONG).show();
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
    });
}

I cannot make class DriveServiceHelper Serializable and pass it trough intent, and I am not sure that I can make function createFile static and use it from anywhere because mDriveService in DriveServiceHelper may be null.

DriveServiceHelper:

public class DriveServiceHelper
{
    private final Executor mExecutor = Executors.newSingleThreadExecutor();
    private Drive mDriveService;

    public DriveServiceHelper(Drive mDriveService)
    {
        this.mDriveService = mDriveService;
    }

    public Task<String> createFile(String filePath) {
        return Tasks.call(mExecutor, () -> {

            String fileId = "------------------------------------";

            File gDriveFile = mDriveService.files().get(fileId).execute();

            java.io.File fileContent = new java.io.File(filePath);
            FileContent mediaContent = new FileContent("text/plain", fileContent);

            File fileObjectWithUpdates = new File();
            File updatedFile = mDriveService.files().update(gDriveFile.getId(), fileObjectWithUpdates, mediaContent).execute();

            if (updatedFile == null) {
                throw new IOException("Null result");
            }
            return updatedFile.getId();


        });
    }


}

What is the best approach for this?

Dim
  • 4,527
  • 15
  • 80
  • 139

1 Answers1

-1

Have you tried to wrap the Drive object in wrapper class and use a singleton pattern to use it else where in your application. Something like this solution link

DemoDemo
  • 372
  • 1
  • 12