My Technical Blog
Google Talk Status Countdown
A few days ago I was counting down to a presentation in my faculty. As I was setting counters all over my world (Side Bar Gadget, To Do List) I thought of a way to change my Google Talk status to a counter also.
Since I know that Google Talk uses XMPP protocol, and since my friends and I implemented XMPP last year in our faculty I tried to make a program to set my status to a counter; and here is the result:

The basic idea behind status changing
Since Google Talk uses XMPP then if you can connect using an XMPP client to your Google account you can use the client instead of Google talk. But indeed that’s not what we want here, we want to use Google Talk while changing the status automatically.
I searched to see if there were an API to communicate with Google Talk from any other program but I had no luck.
So the solution was to use XMPP’s Presence Priority attribute.
In XMPP, a user can connect from multiple clients with the same ID, each client will have a unique `resource` so messages sent from one client get their replies to the same client. But what about status? XMPP has an attribute sent with each status (Status is called Presence in XMPP) which is `Priority`, this attribute describes the priority of this resource presence. In Google Talk the highest priority is 24. So if you can change the priority of the automatic presence to 24 it will be considered high priority and its status will appear instead of the old one.
Programming it
I used Java for developing the program. There is no direct reason for using Java but that’s what I used ![]()
To be able to use XMPP I used the Smack XMPP Library because it is simple and serves me well.
The whole mechanism is too simple:
- Login.
- Calculate difference between now and the targeted date.
- Send the presence.
Using Smack makes the whole process so easy.
Login
import org.jivesoftware.smack.XMPPConnection;
public void connect() {
XMPPConnection connection = new XMPPConnection(server); //Server is gmail.com for Google Talk.
connection.connect();
connection.login(username, password); //Username and password.
}
Calculate difference between now and the targeted date
This process is done using Java Calendar and Date objects:
import java.util.Calendar;
import java.util.Date;
{
Calendar calendar1 = Calendar.getInstance();
Date d = new Date();
calendar1.setTime(d);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(endLine); //End line is the date we're counting to.
long milliseconds1 = calendar1.getTimeInMillis();
long milliseconds2 = calendar2.getTimeInMillis();
long diff = milliseconds2 - milliseconds1;
long diffDays = diff / (24 * 60 * 60 * 1000);
diff = diff % (24 * 60 * 60 * 1000);
long diffHours = diff / (60 * 60 * 1000);
diff = diff % (60 * 60 * 1000);
long diffMinutes = diff / (60 * 1000);
diff = diff % (60 * 1000);
}
This code calculates the difference between the two dates in days, hours and minutes.
Send the presence
After calculating the difference all we have to do is to send the presence:
import org.jivesoftware.smack.packet.Presence;
{
String remaining = Long.toString(diffDays) + " day(s), " + Long.toString(diffHours) + " hour(s), " + Long.toString(diffMinutes) + " minute(s) " + message; //Message is usually: Until "something".
Presence presence = new Presence(Presence.Type.available);
presence.setStatus(remaining);
presence.setPriority(24); //Highest priority in Google Talk
presence.setMode(presenceMode); //This is one of XMPP modes (Available, Chat, DND, Away, XA).
connection.sendPacket(presence);
}
After this point people will see your new status instead of the one in Google Talk. (Notice that you won’t be able to see the change inside Google Talk but rest assured it is changed
).
Testing
Last note
The GUI is self explanatory, just make sure you enter a big value for “Update Interval” because when you’re talking to others and your status changes they’ll have a red line with the new status in the conversation window and certainly you don’t want them to be distracted by the counter from the real conversation.
And BTW, you can use this program with any XMPP account, not just Google Talk. And it’s cross platform
Download
Smack library is attached to the files already, no need to download it separately.
Hope you use it
| Print article | This entry was posted by Abd Allah Diab on May 25, 2010 at 10:46 pm, and is filed under Programming. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |







about 3 months ago
PBUY friend,
There is no need to talk about wonderfulness because it already seems clearly.
Hope you get in future the better and better and then better
and finally better than what’s supposed to.
it’s so nice project.
regards
-Eyad
about 2 months ago
i still cant understand what does this do? does it auto change ur status based on a schedule?
about 2 months ago
It changes your status periodically to reflect a counter to a given event, see the testing image for an example.
about 3 weeks ago
HI,
GTalk Client desktop and GTalk Android Client, both set the priority to 24. Do you think that it is possible to set the priority for the Android GTalk to be always inferior to the GTalk desktop app, in a way similar to what you have done??
Regrds,
Andre
about 3 weeks ago
Hmmm, using my way you can override the presence message by sending another one with the same priority, but I don’t think that using the same method above you can lower the presence priority of another client.
To do so you should listen to the port that the client is using to connect to the server and change the presence priority when you recognize it. Not as easy as the method above but can work certainly.