Replacing deprecated Tasks.call while using Google Drive API

Darksymphony

New member
Nov 16, 2021
1
0
0
Visit site
In my android app I have an option to backup the database to Google Drive. For that I am using DriveServiceHelper class, but I just noticed, in Android 11 the Task.call is deprecated.

This is a part of the DriveServiceHelper class:

Code:
      public Task<FileList> queryFiles() {
    return Tasks.call(mExecutor, () ->
            mDriveService.files().list().setSpaces("drive").execute());
}

And from my BackupActivity then I call queryFiles from backup method:

Code:
 public void backup(View v) {
        driveServiceHelper.queryFiles()
                .addOnSuccessListener(fileList -> {
                  // another code
                })
                .addOnFailureListener(e -> showMsgSnack(getString(R.string.uploaderror)));

I did not find any solution how to deal with this to avoid complete rework of that class.

What I tried:

I tried to replace with runnable, also callable, but it doesn't work as Task is expected to be returned, not Filelist.

also I tried to use TaskCompletionSource:

Code:
 public Task<FileList> queryFiles(int delay) throws IOException {
        TaskCompletionSource<FileList> taskCompletionSource = new TaskCompletionSource<>();
    
        FileList result = mDriveService.files().list().setSpaces("drive").execute();
        new Handler().postDelayed(() -> taskCompletionSource.setResult(result), delay);
    
        return taskCompletionSource.getTask();

}

But when I call queryFiles(1000) from my main class, it gives me error:

java.lang.IllegalStateException: Calling this from your main thread can lead to deadlock.

I'll appreciate any help.