2

My goal is to have a folder on the user's drive where he can drop files. My Android application should then detect (via polling) that a new file was created/uploaded in this folder on the drive and download it.

So far so good. I tried to do this with the Google Drive Android API, but I was given to understand that with the Google Drive Android API I do only have access to files created by my application. So if the user were to drop a photo in this folder via another device (e.g. desktop computer), I would not even see the file, let alone if it was newly created or not.

I was told that I could use the Google Drive REST API (v3) to extract the changes that happened since I last looked.

Drive.Changes.List request = drive.changes().list(startPageToken);
ChangeList changes = request.execute();

This does actually work. However, I have three problems with this approach:

  1. I do get a list of changes containing all the files that changed on the drive. I do not, however, get a changeset. I do not know exactly what has changed. Was it a rename, creation, move, deletion, content change? I googled and I seem to be the only one wondering about this. How do I know what exactly changed?
  2. As I described I would like to watch a single folder. How can I request the changes for only a single folder?
  3. And finally: my application is using the native Google Drive Android API. So with adding this feature I suddenly need two APIs instead of one, even though I need one of them only for the simple task of polling for changes. What makes it even worse is that the user could in theory log into one API with one account, and into the other with another account. This does not make any sense. I get a lot of additional overhead and a critical functionality depends on the user logging in with the same account twice. Is there a solution to this problem?

I find it very frustrating that there are two Google Drive APIs avilable to begin with. You have to dig quite a bit to even get started and discover the exact difference if you know nothing initially. I still do not get the difference or why one of them is not capable to see files not created by itself.

Marco7757
  • 735
  • 9
  • 16

1 Answers1

2

You can try checking the documentation about Listening for Change Events:

You can use change listeners to receive notifications whenever a specified file or folder has changes to its contents or metadata. A change listener implements the ChangeListener interface for the ChangeEvent and receives a direct callback from the Drive service to a currently connected client application.

The following example code shows the logic you might implement to add a change listener to a file:

DriveFile file = Drive.DriveApi.getFile(mGoogleApiClient,
        mSelectedFileId);
file.addChangeListener(mGoogleApiClient, changeListener);

In addition to adding the listener to the file or folder you wish to receive events for, your application must also implement the callback (changeListener in this example) you passed in the addChangeListener method call. The following demonstrates a callback that prints the received events to a log.

/**
 * A listener to handle file change events.
 */
final private ChangeListener changeListener = new ChangeListener() {
    @Override
    public void onChange(ChangeEvent event) {
        mLogTextView.setText(String.format("File change event: %s", event));
    }
};

For callbacks that take action, such as retrieving the latest version, you can use the hasContentChanged and hasMetadataChanged methods to determine whether the file contents, metadata, or both have changed.

For a full working example, see the ListenChangeEventsForFilesActivity sample in the Google Drive Android API Demos app.

You can also check this related so question 22980497 for additional information.

Hope it helps!

Community
  • 1
  • 1
Mr.Rebot
  • 6,703
  • 2
  • 16
  • 91
  • With the native Google Drive API I CANNOT see files created by other devices, meaning the change listener is useless, because it does not detect files dropped via a desktop client. – Marco7757 Sep 22 '16 at 10:50