androidcloudsaf

How can I implement of updating file on the server (file cloud) using SAF?


My mobile cloud application gets all content from the remote server, and it should use Storage Access Framework http://developer.android.com/guide/topics/providers/document-provider.html and implement DocumentsProvider http://developer.android.com/reference/android/provider/DocumentsProvider.html to be displayed in third-party apps (like Microsoft Office) in list of apps which supported SAF on opening or saving. After opening file from SAF app and modifing it in third-party app (Microsof Office for example) this file should be overriden on the server, but I can not find corresponding method in SAF framework for this.

Now I see the only way to implement this:

  1. Download a file in temporal place inside app folder or sdcard on
    DocumentsProvider.openDocument()
  2. Set file observer (FileObserver) of this file
  3. Catch CLOSE_WRITE event and try to send this modified file to the server
  4. In case of any network errors or if app has been relaunched - store info about this file in the application database to send this file later.

Can you offer simpler way to achieve desired result? Or maybe you see some errors in my approach. The similar functionality is already implemented in dropbox app. Thanks!


Solution

  • openDocument() returns a ParcelFileDescriptor and if you use the ParcelFileDescriptor.open(File, int, Handler, OnCloseListener), then you'll get a callback to the OnCloseListener's onClose() - at that point, you know the external application has finished with the file and you can start a sync operation.

    // A file representing the local copy of the file
    File file = ...;
    return ParcelFileDescriptor.open(
        file,
        ParcelFileDescriptor.parseMode(mode),
        new Handler(), // use the current thread
        onCloseListener);