Wednesday, July 29, 2009

XMPP on Android using Smack

I just went through the experience of running XMPP on Android, using the excellent Smack library. I take very little credit for getting it working, and just wanted to document my research in one place. The end result: XMPP running on Android SDK 1.5, using Smack 3.0.4 (svn revision 10869), including full support for bundled extensions. Note that at the time of writing, Smack 3.1.0 is available, but 3.0.4 was sufficient for my needs.

The first useful post I found was from Davanum Srinivas, found here. I had mixed results, and looked for a more source-level approach.

Reading through the comments, I found a link to Peter Neubauer's post here. The smackdiff file was exactly what I was looking for.

To apply the patch and build your own .jar, you'll need three tools installed: svn, diff, and ant. Looking at the .diff, the patch is against revision 10869 of the smack repository: http://svn.igniterealtime.org/svn/repos/smack/trunk. I checked out the source and applied the patch like this:

$ svn co -r 10869 \
     http://svn.igniterealtime.org/svn/repos/smack/trunk smack-android
$ cd smack-android/source
$ patch -p0 -i /path/to/smack.diff
$ cd ../build
$ ant
$ cd ../target

If all went well, you should see smack.jar and smackx.jar, and a few others. These .jar's should work on android, but there's one more catch. My application is using the MultiUserChat extension. The smack extensions are loaded based on a file in smack.jar's META-INF directory. So, when I build my .apk, I simply copied the contents of both smack.jar/META-INF and smackx.jar/META-INF into my apk's META-INF, and everything worked!

Note that one important function of the patch is to remove SSL from Smack, so SSL will not work on this build.

An issue I have not yet worked through is how to automatically import the files from this META-INF directory. I am using Maven to build my .apk (using maven-android-plugin) and have not yet found the best practice for copying these files, so if you have ideas, please post them.

Many thanks to Peter Neubauer and the Smack/Android teams for their work!

Sunday, July 19, 2009

Inline Wiki Syntax using JQuery

I have been kicking this idea for a while and finally got to writing some demo code for it in the form of a JQuery plugin. The idea is to write Wiki code directly into an HTML element, and have it be converted on the fly in the browser.

There are a few benefits to this. First of all, you can use a simple wiki syntax in any webpage (a blog is a good candidate), without the need for a full-blown server-side wiki engine. Second, if you're building a wiki engine, you can have a real-time preview of wiki edits without the need for a server-driven refresh.

Currently, the plugin supports:

* Lists, both ordered (#) and unordered (*) * Basic text formatting- two single apostrophes for ''emphasis'' and three for '''strong''' (five for '''''both''''') * Section headers (!!, !!!, etc.)

For a demo of the code in action, view the source code for the above section (Pretty sneaky.) The JQuery plugin itself can also be found on this page. Here's a more complete example:

This is my standard HTML page. I can have inline wiki code:
<div class="wiki">
!! Welcome to my wiki!
!!! it's pretty simple.
You can have lists:
* pretty easy.
* it's unordered
## but the sublist
## it's ''ordered''!!
* so handy.
!!! it works.
So cool. and You can have ''some'' '''crazy''' '''''formatting'''''.
</div>

Becomes:
This is my standard HTML page. I can have inline wiki code:
!! Welcome to my wiki! !!! it's pretty simple. You can have lists: * pretty easy. * it's unordered ## but the sublist ## it's ''ordered''!! * so handy. !!! it works. So cool. and You can have ''some'' '''crazy''' '''''formatting'''''.

Sunday, July 12, 2009

Civic EX USB Audio Tools

A few months ago, I got myself a 2009 Civic EX, which I totally love. One of the cool features on it is a USB audio interface. You can plug in a USB stick full of music and have it play back. It works very well, but with a problem- it's pretty impossible to navigate 16GB of music with a one line display. I put together a little PHP script to print out the contents of my stick in the same order as is read by the Civic. It's a dead-simple script but might save someone a few minutes. I've tested it on Linux only, but it should work across platforms (but I believe it will require PHP5) I also came across a useful shell script that forces the directory ordering to be alphanumeric, which you can find at the bottom of this thread. Below is the code to print a directory listing. By default, it only prints folders. Two flags are supported: "-t" will print tracks, and "-n" will hide directory names. One last thing that might save you some hunting- according to the Civic manual, the system is capable of handling up to 700 folders and about 65,000+ tracks. I've only pushed it to 100 folders and about 1100 tracks so far.
#!/usr/bin/php
<?php

$media_types="mp3|m4a|wma";

// check options
$list_tracks=false;
$list_directories=true;

for ($i=1;$i<sizeof($argv)-1;$i++){
 if ($argv[$i][0]=='-') {
  if (false !== stripos($argv[$i],'t')) {
   $list_tracks=true; 
  }
  if (false !== stripos($argv[$i],'n')) {
   $list_directories=false; 
  }
 } else {
  die("Invalid option " + $argv[$i]);
 }
}



$path = realpath($argv[sizeof($argv)-1]);
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);

$di=0;
$ti=1;
$media_dir=false;
$working=false;
$media_pattern='/\.'.$media_types.'$/i';
$tr_pad = ($list_tracks && $list_directories) ? '  ' : '';
foreach ($objects as $name => $object) {
 if ($object->isFile()) {
  if ($working != $object->getPath()) {
   // start new dir
   if ($media_dir && $list_tracks) {
    echo "\n";
   }
   $media_dir=false;
   $working = $object->getPath();
   $ti=1;
  }

  
  if (!$media_dir) {
   if (preg_match($media_pattern,$name)) {
    $media_dir=true;
    $di++;
    if ($list_directories) {
     $el = str_replace($path.DIRECTORY_SEPARATOR,'',$object->getPath());
     echo substr(str_pad($di,2,'0',STR_PAD_LEFT),-2) . ' ' . str_replace(DIRECTORY_SEPARATOR,' :: ', $el) . "\n";
    }
   }
  }
  

  if ($list_tracks) {
   if (preg_match($media_pattern,$name)) {
    echo $tr_pad . substr(str_pad($di,2,'0',STR_PAD_LEFT),-3) . '-' . str_pad(($ti++),3,'0',STR_PAD_LEFT) . ' ' . $object->getBaseName() . "\n";
   }
  }

 }
}
Search Terms: civic directory order, civic music, USB Audio directory order

First Post

Hello, World. I've finally caved in and started my very own blog. But don't worry, I won't be posting about my boring life (god willing). I just wanted a place to jot down solutions to problems I've come across, and let the almighty Google throw it in its index so others (you?) might find it useful. Enjoy. Or don't.