blog.datamazon.com
       

Android - Google chat

Posted in android by paul on the Janeiro 27th, 2008

Ok, this is the second post about this subject, and the chat client has advanced moderately well. There’s not too much different, but I think its well enough along to share. Of course, there is still lots to do. As it stands, though, it shows getting the roster, passing the selected buddy back to the main chat application, and getting message history. Also starting subActivities, passing data to a subActivity, getting data back from a subActivty, and some other interesting stuff. I did’t put any licensing info there since it seems a little spurious at this point, but if I had to choose, I guess you can choose any license you want. Apache? BSD? GPL? take your pick.

GoogleChat.tgz

Android XMPP - getting roster

Posted in android by paul on the Janeiro 23rd, 2008

I’m playing around with android and xmpp, part of the android API that I find very interesting. Unfortunately the API seems a little incomplete still, so everyone playing with this is being forced to use some hacks. The Android SDK comes with ApiDemos/apps/XmppDataSender and XmppDataReceiver. After getting connected (you can only connect to google talk servers), the demo applications are meant to send data between 2 android instances. Since they are DATA clients, you cannot use the Demos to chat. You have to have 2 instances of the android emulator running. 1 instance of the emulator slows down my poor machine noticeably, I haven’t had the courage to try running two. With my computer being kind of flakey anyway, this doesn’t seem like a good idea. So… challenge to self: create google talk chat client for Android. I’ll go by pieces, then hope to tie them together at the end. I guess the main pieces are 1) send 2) receive 3) present roster 4)move between activities (roster screen and msg screen) then 5) make it nicer.

1) Turns out to be trivial. In the sample code XmppDataMessageSender the onClick event,comment out the sendDataMessage and put in a sendTextMessage, like below.

try {
//mXmppSession.sendDataMessage(username, getIntentToSend());
mXmppSession.sendTextMessage(username, 0, msgtosend);
} catch (DeadObjectException ex) {}

voila, now you can send messages from Android to your other gtalk account.

2) Turns out to be harder. How do you register an Intent to receive text messages? We’ll come back to that in a bit

3) The roster. There actually is documentation that mentions how this can be done requestRosterAndSendInitialPresence .. so.. here’s the code for getting a roster and presenting it.

try {
mXmppSession.requestRosterAndSendInitialPresence();
} catch (DeadObjectException ex) {
Log.e(LOG_TAG, “caught ” + ex);
mXmppSession = null;
bindXmppService();
}
try {
if(mXmppSession.hasReceivedRoster()){

Cursor cursor = managedQuery(Im.Presence.CONTENT_URI, null,null, null, null);

Log.d(LOG_TAG, “got ” + cursor.count() + ” contacts”);
ListAdapter adapter = new SimpleCursorAdapter(this,
R.layout.show_roster_items,
cursor,
new String[]{Im.Contacts.USERNAME,Im.Contacts.PRESENCE_STATUS},
new int[]{R.id.contactname,R.id.contactstatus});
mInMessage.setAdapter(adapter);

}
} catch (DeadObjectException ex) {
Log.e(LOG_TAG, “caught ” + ex);
}

Thats the code. I’ll attach the rest of the project later, mainly the .xml files for displaying the above.

Note that this only gets users that are currently online. It does not retrieve your whole buddy list. I don’t know why. I also have not figured out where im/contacts lives, since it is not in the Im database (I guess that is worth another post).

Right now I’m going to try to use the roster for selecting a user, pass it to the xmppdatasender.

Javascript and the DOM

Posted in javascript, Html, CSS by paul on the Janeiro 16th, 2008

I’ve decided to mess around with javascript a little and see what I can do to a page dynamically. I’m not doing this gratuitously, but maybe the way I go about seems.. hackish.

Starting simple…. see what we can do about messing with tables. I’d like to be able to manipulate tables from the browser.. adding rows, deleting rows, setting styles, and so forth.

Sometimes it is hard to know where to start. The best reference,though, is the SOURCE. use the source. developer.mozilla.org. I’ve looked at other references, but they aren’t that good. I Love Jack Daniels has good cheat sheets, but its not comprehensive and actually using it in code is problematic. For example, have a look at his javascript cheat sheet (very nice) and try to find {table}.insertRow(); you won’t find it. Well… its part of the DOM, not exactly javascript. But javascript is basically the only way we can get to the DOM to manipulate it. Soooo…

I’ve decided the way to play with javascript is the firebug console. That way we get immediate feedback (or not) about what we’re doing. I guess the error console works too. The downside is that you don’t get command completion, but… I guess that is asking for a lot (!).

First attempt: adding a row to table.

there seem to be a couple of ways to add a row to the table. .appendChild and .insertRow.

.appendChild adds a row to the end of the table, .insertRow adds a row at the index we include ie… table.insertRow(index);

Lets have a go.

 

>>> mytable = document.getElementById(”tbl_dayview”);

<table id=”tbl_dayview” class=”ev_table” width=”100%” cellspacing=”0” cellpadding=”5” align=”center“>

>>> myrow = document.getElementById(”dc0800″);

<td id=”dc0800” class=”ev_row_yellow” cols=”” room=”” doctor=”” livesite=”http://localhost/dental” end_time=”08:30” start_time=”08:00” day=”16” month=”01” year=”2008“>

>>> newrow = document.createElement(”TR”);

<tr>

>>> newd1 = document.createElement(”TD”);

<td>

>>> newd2 = document.createElement(”TD”);

<td>

>>> newd1.appendChild(document.createTextNode(”col1″);

missing ) after argument list

newd1.appendChild(document.createTextNode(”col1″);

>>> newd1.appendChild(document.createTextNode(”col1″));

“col1″

>>> newd2.appendChild(document.createTextNode(”col2″));

“col2″

>>> newrow.appendChild(newd1);

<td>

>>> newrow.appendChild(newd2);

<td>

>>> mytable.insertRow(newrow);

<tr>

>>> mytable.appendChild(newrow);

<tr>

>>> mytable.insertRow(2);

<tr>

>>> newrow = mytable.insertRow(1);

<tr>

>>> newrow.appendChild(newd2);

<td>

>>> newrow.appendChild(newd1);

<td>

>>> newd1.style=”ev_td_left”;

setting a property that has only a getter

undefined javascript: with … (line 1)

>>> newd1.style.classname=”ev_td_left”;

“ev_td_left”

>>> newd1.setAttribute(”class”,”ev_row_yellow”);

ok.. next session I’ll comment the above.

 

 

Copyright ® 2007 Datamazon.com
Melhor visualizado com FireFox, Mozilla e Internet Explorer 7