Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gmail Sync on Android phone

Tags:

android

gmail

Android has the Gmail push features, which means the new message arrives in the mailbox without checking or refreshing the mailbox.

As I understand, the sync processes are like these:
1) User turns on the sync
2) There will be a alert msg and the sync flag in the Gmail DB of this device will be True
3) When a new email reach the Gmail Server, it will check if the device sync value, if it's True then send the email

OK, here, I don't quite understand how exactly does it work,
For a WiFi and cell signal connection, does the phone has a TCP socket open keep listening to the Gmail Server, or when a new email arrives the Server and it sends a SMS alert to the phone, and the phone will open the data channel to fetch the email? Do the two ways of connections have different approaches?

And second question is which method is the priority one?
Say when you are in the middle of receiving data(emails), and suddenly the phone connect to a wireless network, will the data socket be closed and then reopened for the WiFi one? What's the behavior for the case when carrier's data channel and WiFi flips?

I have also downloaded the source code, anyone knows which part should I be looking into in order to solves my questions? I found a folder called "email" inside the folder "package", should I be looking at its code?

I know I asked quite some questions here, I'd appreciate if you know the answer for any of them, thanks very much!

like image 375
sunocky Avatar asked Mar 25 '10 23:03

sunocky


1 Answers

This technically is not part of Android, but rather Google's proprietary application suite that runs on Android. It also is not really implemented in Gmail -- the google apps collectively have a push data connection with google's servers (built on the gtalk data protocol, but mostly there for historical reasons) which they share. When new mail is available, the push connection is used to send a "tickle" to the device to say that something has changed in mail, and on the device this is then dispatched to the gmail app which opens a new socket connection with its server to synchronize the local state with whatever changes have happened. This same mechanism is used for contacts and calendar sync, as well as things like instigating downloads from market.

The way this works is an implementation detail, but currently it is done by keeping an open socket connection. On a network that can't do voice and data, the connection is dropped when a voice call takes place. When switching between wifi and cell, the connection is dropped and restarted.

Note that doing push notifications this way is quite resource intensive on the device, which is why this has been carefully designed to allow one such instance to support all Google services.

like image 182
hackbod Avatar answered Oct 09 '22 03:10

hackbod