Tuesday, March 29, 2016

Google Official Drive API for Android

Limitations

Works locally

  • If one device add file to  a folder, the other device will not see the new file (on appFolder it worked).
  • If on the device I use Drive app to update a file, and app on the same device that use the drive api will not get a notification
  • Events (both ChangeSubsciption EventService, and changeListener)  work only if the app updates a file via drive api on the same device.
  • Events will not work at all on folders (checked regular folder and appFolder) checked cases of Updating folder "as starred" (which worked with files). Also uploading files didn't had effect on the folder event (event was not thrown).

Other Limitations

  • The appFolder is not visible in anyway and should be handled from the app only.
  • Looks like Google is aware of this limitations, and they was inserted on purpose (for security reasons, and it will not be changed anytime soon (if at all).


Things that work

  • Upload file (media file, or any other file). there is no upload in the api, there is an awkward way to achieve it:
final DriveContents driveContents = result.getDriveContents();

    // Perform I/O off the UI thread.    new Thread() {
        @Override        public void run() {
            // write content to DriveContents            OutputStream outputStream = driveContents.getOutputStream();

            try {
                File picFile;
                picFile = new File("/sdcard/DETECTED_GALLERY/1458374357190.jpg");
                byte[] byteStream = new byte[(int) picFile.length()];
                FileInputStream fileInputStream = new FileInputStream(picFile);
                fileInputStream.read(byteStream);
                fileInputStream.close();

                outputStream.write(byteStream);
                outputStream.close();
            } catch (IOException e) {
                Log.e(P.TAG, e.getMessage());
            }

            MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                    .setTitle("test_pic12")
                    .setMimeType("image/jpeg")
                    .setStarred(true).build();

            Drive.DriveApi.getAppFolder(getGoogleApiClient()).createFile(getGoogleApiClient(), changeSet, driveContents)
                    .setResultCallback(fileCallback);

           
        }
    }.start();
}

  • Create folder works, not sure if one device created the folder (via the api) the other device will see it (need to check).
  • The Event service works without starting it (when it works, see the limitations above) <service android:name=".MyDriveEventService" android:exported="true">
            <intent-filter>
                <action android:name="com.google.android.gms.drive.events.HANDLE_EVENT"/>
            </intent-filter>
        </service>
  • Using the appFolder I can list files that was added from another device.