UPDATE (07/22/2015)
Thanks to Steven Bazyl's prompt response (see Comments below), I finally found a satisfactory solution using Completion Events . Here are two reduced code snippets that deliver the ResourceId to the application as soon as the newly created file is distributed to disk:
Creating a file, adding a change subscription:
public class CreateEmptyFileActivity extends BaseDemoActivity { private static final String TAG = "_X_"; @Override public void onConnected(Bundle connectionHint) { super.onConnected(connectionHint); MetadataChangeSet meta = new MetadataChangeSet.Builder() .setTitle("EmptyFile.txt").setMimeType("text/plain") .build(); Drive.DriveApi.getRootFolder(getGoogleApiClient()) .createFile(getGoogleApiClient(), meta, null, new ExecutionOptions.Builder() .setNotifyOnCompletion(true) .build() ) .setResultCallback(new ResultCallback<DriveFileResult>() { @Override public void onResult(DriveFileResult result) { if (result.getStatus().isSuccess()) { DriveId driveId = result.getDriveFile().getDriveId(); Log.d(TAG, "Created a empty file: " + driveId); DriveFile file = Drive.DriveApi.getFile(getGoogleApiClient(), driveId); file.addChangeSubscription(getGoogleApiClient()); } } }); } }
Event service completes execution:
public class ChngeSvc extends DriveEventService { private static final String TAG = "_X_"; @Override public void onCompletion(CompletionEvent event) { super.onCompletion(event); DriveId driveId = event.getDriveId(); Log.d(TAG, "onComplete: " + driveId.getResourceId()); switch (event.getStatus()) { case CompletionEvent.STATUS_CONFLICT: Log.d(TAG, "STATUS_CONFLICT"); event.dismiss(); break; case CompletionEvent.STATUS_FAILURE: Log.d(TAG, "STATUS_FAILURE"); event.dismiss(); break; case CompletionEvent.STATUS_SUCCESS: Log.d(TAG, "STATUS_SUCCESS "); event.dismiss(); break; } } }
Under normal conditions (wifi) I get a ResourceId almost immediately.
20:40:53.247﹕Created a empty file: DriveId:CAESABiiAiDGsfO61VMoAA== 20:40:54.305: onComplete, ResourceId: 0BxOS7mTBMR_bMHZRUjJ5NU1ZOWs
... done now.
ORIGINAL MAIL, deprecated, left here for reference.
I let this answer sit for a year, hoping that the GDAA will develop a solution that will work. The reason for my grunt is simple. If my application creates a file, he needs to pass this fact on to his friends (for example, other devices) with an identifier that makes sense (i.e., ResourceId). This is a trivial task in REST Api where the ResourceId is returned as soon as the file is successfully created.
The needle says that I understand the GDAA philosophy of protecting the application from network primitives, caching, batch processing ... But it is clear that in this situation, the ResourceID is available long before it is delivered to the application.
I originally implemented the Cheryl Simon suggestion and added a ChangeListener to the newly created file, hoping to get the ResourceID when distributing the file. Using the classic CreateEmptyFileActivity from android-demos, I applied the following test code:
public class CreateEmptyFileActivity extends BaseDemoActivity { private static final String TAG = "CreateEmptyFileActivity"; final private ChangeListener mChgeLstnr = new ChangeListener() { @Override public void onChange(ChangeEvent event) { Log.d(TAG, "event: " + event + " resId: " + event.getDriveId().getResourceId()); } }; @Override public void onConnected(Bundle connectionHint) { super.onConnected(connectionHint); MetadataChangeSet meta = new MetadataChangeSet.Builder() .setTitle("EmptyFile.txt").setMimeType("text/plain") .build(); Drive.DriveApi.getRootFolder(getGoogleApiClient()) .createFile(getGoogleApiClient(), meta, null) .setResultCallback(new ResultCallback<DriveFileResult>() { @Override public void onResult(DriveFileResult result) { if (result.getStatus().isSuccess()) { DriveId driveId = result.getDriveFile().getDriveId(); Log.d(TAG, "Created a empty file: " + driveId); Drive.DriveApi.getFile(getGoogleApiClient(), driveId).addChangeListener(getGoogleApiClient(), mChgeLstnr); } } }); } }
... and waited for something to happen. The file was uploaded to Drive in a few seconds, but not onChange (). 10 minutes, 20 minutes ... I could not find a way to make ChangeListener wake up.
So the only other solution I could come up with is to push the GDAA. So I implemented a simple poker handler that tickles metadata until something happens:
public class CreateEmptyFileActivity extends BaseDemoActivity { private static final String TAG = "CreateEmptyFileActivity"; final private ChangeListener mChgeLstnr = new ChangeListener() { @Override public void onChange(ChangeEvent event) { Log.d(TAG, "event: " + event + " resId: " + event.getDriveId().getResourceId()); } }; static DriveId driveId; private static final int ENOUGH = 4;
And voila, after 4 seconds (give or take), ChangeListener delivers a new brilliant ResourceId. Of course, ChangeListener is becoming obsolete as well, as Poker Roulette also gets a ResourceId.
So this is the answer for those who cannot wait for ResourceId. Which raises the following question:
Why do I need to tickle the metadata (or intercept the content), which is likely to create unnecessary network traffic in order to receive the onChange () event, when I clearly see that the file was distributed a long time ago, and the GDAA resource is available?