Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Using the Google Drive API for android

For my App I need to have a List sync with Google Drive. I have already implemented the SignIn and had my Main_Activity implement both:

com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks,
com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener

Even though I read the whole Google Drive API for Android documentation, and more specifically the Store Application Data part. And looked through the example on GitHub, I wasn't able to get it to work. I personally think that this documentation is really confusing to read. It's not even clear what is the difference between the Google Drive API for android and the Google Drive REST API, and which one I should use for my scenario.

Also I noticed that the example on GitHub extends a custom Activity that has other methods in it.

Can any of you explain step by step how to use the android drive API?

like image 497
Daniele Avatar asked Oct 18 '22 12:10

Daniele


1 Answers

I am familiar with your frustration. There is the "Google Drive Android API" (GDAA) and the "Drive REST API." There is some good documentation on the web, but finding it and making sense of it can be a challenge, especially since the names of the packages are so similar. In addition, there are at least two extant versions of "Drive REST API" and you have to keep the versions straight.

Regarding GDAA, you have already found this documentation, but you should take a closer look. I suggest that you do a simple activity such as creating a file and work from there.

I think that the custom Activity in the example on GitHub that you reference is the BaseDemoActivity. That class simply provides some of the life cycle methods and some other common routines.

The Google Drive Android API (GDAA) is tightly integrated with Play Services and Google claims offers better performance. (See the note here):

Note: This quickstart illustrates the use of the Drive REST API in an Android application. However, in the majority of cases production Drive apps will strongly benefit by making use of the Drive API for Android, which is integrated with Google Play Services and offers better performance. Before using the Drive REST API in your Android application, you should carefully review the Drive API for Android and use it in your application if possible. A Drive API for Android Quickstart is available if you want to learn more.

Even though, I have abandoned GDAA in most instances due to tightening restrictions on the frequency of syncing. (For more detail, see the notes at the bottom of this post.)

When working with GDAA, one key thing to keep in mind is that even though your code can run on the UI thread, GDAA cannot due to potentially lengthy tasks that it performs on your behalf. That implies that once you request GDAA to accomplish some task from the UI thread, GDAA will do that work in the background (not on the UI thread) and deliver results to you through the callbacks.

This structure, although necessary, means that your code will be a series of methods that are invoked by GDAA and will not necessarily demonstrate a clear sequential format that you may be used to. I think of it as a Pachinko machine in software.

Although not a step-by-step set of instructions, I hope this helps point you in the right direction.


Regarding Sync Frequency: More specifically, uploads to the server will occur as specified with DrivePreferencesApi. Uploads usually happens fairly quickly. Downloads, however, are rate limited. See this documentation.

In order to avoid excessive load on the device and the server, sync requests are rate limited. In this case, the operation will fail with DRIVE_RATE_LIMIT_EXCEEDED status, which indicates that a sync already happened quite recently so there is no need for another sync. The operation will succeed when reattempted after a sufficient backoff duration.

I believe that the "backoff duration" is dependent upon the version of Play Services installed. In my experience, this duration varied from a couple of minutes to 1/2 hour or more. This may have changed and I tried to find documentation on this but was unsuccessful.

If the download limitation of GDAA doesn't work for you, then you may want to consider Drive REST. You can also look into Firebase as a possible solution.

like image 90
Cheticamp Avatar answered Oct 21 '22 07:10

Cheticamp