Thoughts by Techxplorer

Thoughts on my experiences with technology

Reading and Writing Google Protocol Buffers with Files

By: Fernando de Sousa

One of my current tasks with the Serval Project is the redevelopment of the Serval Maps application, which was the focus of my honours thesis last year.

As part of the redevelopment I’ve implemented a new data interchange format that takes advantage of the Rhizome capabilities of the Serval Mesh software. I’ve decided to use Google Protocol Buffers as the basis for my binary file format.

To write the messages to the file it is necessary to use the writeDelimitedTo method of my Message class to write the messages to the file. By using this method each message is delimited in the file.

The code looks something like this:

try {
	FileOutputStream mOutput = new FileOutputStream(mOutputPath + mFileName, true);
	mMessageBuilder.build().writeDelimitedTo(mOutput);
	mOutput.close();
} catch (FileNotFoundException e) {
	Log.e(TAG, "unable to create the output file", e);
	return;
} catch (IOException e) {
	Log.e(TAG, "unable to write to the output file", e);
	return;
}

Reading back the messages from the file requires the use of the mergeDelimitedFrom method to read the messages out of the file and process them. The code looks like this:

try {
	while(mMessageBuilder.mergeDelimitedFrom(mInput) == true) {

		// process each message in turn
	}
} catch (IOException e) {
	Log.e(TAG, "unable to read from the input file", e);
	return;
} finally {
	try {
		mInput.close();
	} catch (IOException e) {
		Log.e(TAG, "unable to close input file", e);
	}
}

It took me some time this afternoon to work this out, including a large number of Google searches. I hope this post helps someone else who is working through similar issues.

The photo “Binary” was uploaded to Flickr by Fernando de Sousa and used under the terms of a Creative Commons License.

Binding a Cursor to a ListView in Android

One of my current tasks is the development of the MeshMS Gateway application. The purpose of the application is to relay MeshMS messages that are being distributed via Rhizome on a Serval Project mesh network to another network or device. In this first instance MeshMS messages will be relayed over a satellite connection.

A key component of the application is restricting the destination phone numbers that messages will be relayed for. My solution was to use a database table to list the phone numbers and provide a user interface for the user to add and remove entries. The final UI looks like this:

Screenshot of the UI showing a populated ListView

It is implemented in the RelayNumbers activity.

My google-fu was letting me down yesterday. All I could find were examples of populating a ListView using an array of items, or examples of using custom layouts with a Cursor in a ListView. Neither of these were what I needed.

The solution came when I discovered that Android ships with a number of simple built in layouts for use with ListViews. The are referenced via the android.R.layout class and the XML for the layouts are stored in the following directory:

{android-sdk}/platforms/{platform-version}/data/res/layout

Using a cursor with a ListView is a multi stage process.

The first stage is getting the data that will be used to populate the ListView. For example this is implemented in my activity like this:

ContentResolver mContentResolver = getContentResolver();

Cursor mCursor = mContentResolver.query(GatewayItemsContract.Contacts.CONTENT_URI, null, null, null, null);

startManagingCursor(mCursor);

This is standard code for creating a cursor in an activity. The only item specific to my code is the

GatewayItemsContract

class which specifies the attributes of the data in my content provider.

The next task is to add the cursor to a SimpleCursorAdapter. This class maps data in the cursor to items in the layout. In my activity the code looks like this:

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
        android.R.layout.simple_list_item_single_choice,
        mCursor,
        new String[] {GatewayItemsContract.Contacts.Table.PHONE_NUMBER},
        new int[] { android.R.id.text1});

The key elements here are:

  1. The use of the provided layout for a list with a single item associated with an option button on line 2
  2. The mapping between the database field in cursor as identified on line 4 and the component of the view as identified on line 5

Once the SimpleCursorAdapter is configured it is necessary to associate it with the ListView using code like this:

contactList.setAdapter(mAdapter);
contactList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

Now when the ListView is displayed the content in the identified field is mapped to the identified element in the ListView and the user can select only one item at a time with a visual indication of which item is selected.

The last two components are to use a custom OnItemClickListener to capture the touch on the selected item and store it for later processing by the method behind the Delete Contact button.

// capture touches on the listview
contactList.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // store the selected contact for later
    TextView textView = (TextView) view;
    contactToDelete = textView.getText().toString();
  }
});

And clear the selected item index using the clearChoices() method  when the actual item is deleted so that the item immediately below the one that was deleted isn’t automatically selected when the data in the cursor refreshes.

contactList.clearChoices();

For a full code listing please take a look at the RelayNumbers activity.

Extracting Textual Data from a Zip File in Java

One of my development tasks this past week was to extract the contents of a file that was compressed and stored in a zip file using Java for the Serval Maps Android application that I’m developing as part of my work for the Serval Project.

The file in question contains a list of coordinates that I use as a source of location data that I can provide to my application using mock locations. Using this class I can provide geo-coordinates to my application during testing that appear to come from the GPS hardware, from the perspective of the rest of the code. The coordinates are in fact sourced from the file. I’ll write about this improved class in a future post.

The code that I developed to extract the contents of the file looks something like this:

// declare helper constants
private final String LOCATION_FILE = "mock-locations.txt";
private final String LOCATION_ZIP_FILE = "mock-locations.zip";

// open the zip file and get the required file inside
ZipInputStream mZipInput = new ZipInputStream(context.getAssets().open(LOCATION_ZIP_FILE));
ZipEntry mZipEntry;

// look for the required file
while((mZipEntry = mZipInput.getNextEntry())!= null) {

  // read the bytes from the file and convert them to a string
  if(mZipEntry.getName().equals(LOCATION_FILE)) {

    // store the incoming data in a byte array stream
    ByteArrayOutputStream mByteStream = new ByteArrayOutputStream();
    byte[] mBuffer = new byte[1024];
    int mCount;

    // read in the data until it is all gone
    while((mCount = mZipInput.read(mBuffer)) != -1) {
      mByteStream.write(mBuffer, 0, mCount);
    }

    // store the data for later
    locations = new String(mByteStream.toByteArray(), "UTF-8");
  }

  // play nice and tidy up
  mZipInput.closeEntry();
}

// play nice and tidy up
mZipInput.close();

The basic process that this code undertakes is to:

  1. Open the zip file stored in the assets folder using the ZipInputStream class
  2. Loop through the contents of the zip file looking for the required file by examining the series ZipEntry objects
  3. Once found read the contents of the file into a ByteArrayOutputStream
  4. Convert the byte array into a string and store it for later use

 

Linux.conf.au Presentations

Earlier this month Linux.conf.au 2012 was held in Ballarat, Vic. The Serval Project was fortunate to be awarded two presentation slots at the conference. I was scheduled to present, but unfortunately had to pull out at the last moment due to illness.

My presentation was to focus on my work on the prototype Serval Maps application, which was the topic of my honours thesis last year.

Fortunately Dr Paul Gardner-Stephen, Serval Project Co-Founder and Shuttleworth Fellow, was able to attend and presented instead. You can read his notes on the conference here.

The presentation by Paul is now available on YouTube here.

The Serval Project also had another presentation at the conference which focused on our Rhizome technology. That presentation, by Jeremy Lakeman, is also available on YouTube here.

It is unfortunate that I was unable to attend and present, it was something that I was looking forward to. Hopefully I’ll be able to present next year on one of the many interesting things that the Serval Project is undertaking this year.

Relocation is Complete

By: Trevor Dennis

The relocation of Thoughts by Techxplorer to a new host is complete. It was surprising simple but I know this is only due to the efforts of the developers of WordPress for the excellent export / import functionality, and the admins at Dreamhost for implementing sane defaults in their web server configurations.

Please let me know if you notice anything not working as it should.

The photo “Moving House – Kiwi Style” was uploaded to Flickr by Trevor Dennis and used under the terms of a Creative Commons License.

Preparing for linux.conf.au

This time next week I’ll be in Ballarat getting ready for registration at linux.conf.au 2012. A colleague and I are presenting two talks related to the Serval Project on the Wednesday afternoon.

My talk will focus on the Serval Maps application that I developed as the focus of my honours study last year. My honours thesis is available online. After the talk the presentation slides will be available on my website.

Speaking of my presentation slides, I really should stop procrastinating and get back to working on them.

My Honours Thesis is Online

By: Sam Hames

This past year I’ve been studying part time for a Bachelor of Science (Honours) degree specialising in Computer Science at Flinders University. The focus of my studies has been my honours thesis. My thesis explored the following research question:

Is it possible to provide collaborative mapping services on mobile devices in an infrastructure independent manner?

To explore this question I developed an Android based application that could support four core objectives. They were:

  1. have the users own geographic location displayed on a map;
  2. add incidents, represented by a marker, onto a map;
  3. be able to see the geographic location of other users of the application on the map; and
  4. share details of incidents with other users of the application on the network.

The software that I developed uses the resilient Ad Hoc mesh network provided by the Serval Project to ensure that communication between instances of the application are infrastructure independent.

The thesis is available online on my Stuff by Techxplorer website.

The photo “Writing Thesis” was uploaded to Flickr by Sam Hames and used under the terms of a Creative Commons License.

First Version of Techxplorer’s CMS Released

By: depone

Today I released the first version of Techxplorer’s CMS. The source code is available on GitHub.

Techxplorer’s CMS is a series of PHP scripts, and accompanying JavaScript files, that I use in conjunction with MultiMarkdown to manage the Stuff by Techxplorer website. The CMS was designed with similar goals to that of the MultiMarkdown CMS. I decided to develop my own system for three main reasons:

  1. I wanted to see if I could do it
  2. I wanted something in PHP as I’m allergic to Perl
  3. I didn’t need all of the functionality that the MultiMarkdown CMS provides

The current version of the system provides the following capabilities:

  1. Convert MultiMarkdown source files into HTML files when the md file is newer or there is no corresponding HTML file
  2. Match the URL of the request to a HTML file in the content directory
  3. Incorporate the HTML from the file into the website template
  4. Automatically add a table of contents via JavaScript when required
  5. Automatically create links from citations to a bibliography when required
  6. Add an image cycle to a page
  7. Automatically include page, directory and site specific metadata

The photo “Content Management” was uploaded to Flickr by depone and used under the terms of a Creative Commons License.

My Expo Poster Won an Award

In November this year my honours course required that I develop a poster presentation. A poster presentation is basically an A1 sized poster that you stand in front of while people wander around the expo. It is a great opportunity for the school to promote the research and projects that have been undertaken over the course of the year by the students.

In the morning members of the school are invited to attend and check out what we’ve been doing. Some are assigned individual students and they must assess their posters. At the same time a group of industry representatives is also working their way through the expo evaluating a number of projects for awards.

My poster, and presentation, was selected for the “Most Outstanding Software Engineering Project 2011″. On Christmas Eve the award arrived in the mail, which was a very pleasant surprise.

The poster is available as a PDF via my bytechxplorer.com site and my honours thesis is also available.

Many thanks to my supervisors for providing feedback on my poster and for giving me the opportunity to work on the Serval Project.