2

The Android's developer documentation states that you can use a DriveApi.OnSyncFinishCallback to (presumably) handle when a synchronization between your local contet and your google drive account is completed. Normally such synchronization appens automatically, trasparently managed by Google Play Services, but apparently you can force a sync request with a call to:

Drive.DriveApi.requestSync(mGoogleApiClient);

I say "apparently" because the official documentation of this function is very poor, at least (https://developer.android.com/reference/com/google/android/gms/drive/DriveApi.html#requestSync(com.google.android.gms.common.api.GoogleApiClient))

Anyway, a OnSyncFinishCallback can be instantiated with this code:

OnSyncFinishCallback myCallback = new OnSyncFinishCallback(){

    @Override
    public void onSyncFinish(com.google.android.gms.common.api.Status arg0) {
        // TODO Auto-generated method stub

    }

};

My question is where and how can I register this callback so it will be called automatically when the sync is completed? The requestSync call returns a PendingResult that only have a setResultCallback(ResultCallback arg0) method, that can't be used for a OnSyncFinishCallback.

Alessandro Prete
  • 420
  • 4
  • 10

3 Answers3

3

I must say that requestSync is working absolutely fine for me (January 2015, with Google Play Services 6.5.87). I do a backup of my database on one device and restore it on another device, but before the restore I call requestSync this way:

    Drive.DriveApi.requestSync(mGoogleApiClient)
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(Status result) {
                    if (!result.isSuccess()) {
                        // Sync not ok
                        showMessage("Sync error");
                        return;
                    }
                    // Sync ok. I can safely do a query to get
                    // the database file and restore it.
                    ...

By the way, I'm using the root folder, not the appfolder. The appfolder might have additional synchronization issues when installing/uninstalling the app from different devices, so for the moment I prefer to stick with root folder.

jmart
  • 2,769
  • 21
  • 36
  • In my project I need to know when the app has finished uploading a large file, maybe requestSync works only for download (the official documentation say nothing)? Anyway now the correct way to get notified when an operation is completed is via the new Completion Events added in Google Play Services Drive API 6.1 – Alessandro Prete Jan 16 '15 at 09:32
2

OnSyncFinishCallback is a red herring, it shouldn't be exposed.

Just add a callback handler to requestSync like any other GoogleApiClient method:

Drive.Drive.requestSync(mGoogleApiClient).setResultCallback(
  new ResultCallback<Success>() {
     //...
  });
Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
  • Unfortunately a ResultCallback on requestSync is called nearly immediately and not when the sync operation is effectively completed. It seems, instead, that onSyncFinish should be designed to be called when the sync is actually completed, that is what I need. – Alessandro Prete May 30 '14 at 10:51
  • Read Alessandro Prete answer – Ilya Gazman Mar 30 '15 at 00:32
2

It turned out that OnSyncFinishCallback was removed from API and DriveAPI.requestSync() doesn't do what it's supposed to. Fortunately Google just introduced new Drive API for Android in version 6.1 of Google Play Services, in particular the Completion Events, that makes exactly what OnSyncFinishCallback was supposed to do. More official detail here https://developers.google.com/drive/android/completion

Alessandro Prete
  • 420
  • 4
  • 10
  • But you cannot attach/pass an ExecutionOptions object to the requestSync method, so Completion Events offer no help w.r.t. syncing. I am observing the same app (same APP_ID) on 2 different devices, incapable of finding a common folder...so it seems, as at August 2015, it is not possible to share files between devices (even when apps share the same APP_ID). – straya Aug 31 '15 at 02:16