Search code examples
androidgoogle-drive-apigoogle-drive-android-api

Check requestSync result status with Tasks Drive API


With the previous API (using PendingResult), I could do something like:

Drive.DriveApi.requestSync(mGoogleApiClient).setResultCallback(new ResultCallback<Status>() {
          @Override
          public void onResult(@NonNull Status status) {
            if (status.getStatusCode() == DRIVE_RATE_LIMIT_EXCEEDED) {
              //Here I know Drive rate limit was exceeded.
            }
          }
        });

How can I accomplish the same with the new TasksAPI?

For what I read in the documentation:

If the request has been rate-limited, the operation will fail with the DRIVE_RATE_LIMIT_EXCEEDED status

But how do I check the status of Task<Void> requestSync?

This is as far as I've got:

Task<Void> checkSync = myDriveClient.requestSync();
checkSync.addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
        public void onComplete(@NonNull Task<Void> task) {
            //how do I check the status here?
        }
});

Solution

  • I had the same problem. In fact it is the Exception of the onFailureListener. But I needed to cast it to an ApiException.

    Task<Void> checkSync = Drive.getDriveClient(getContext(), mGoogleSignInAccount).requestSync();
        checkSync.addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                //how do I check the status here?
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                ApiException apiException = (ApiException) e;
                if (DriveStatusCodes.DRIVE_RATE_LIMIT_EXCEEDED == apiException.getStatusCode()) {
                    //Here I know Drive rate limit was exceeded.
                }
            }
        });
    

    Hope it helps.