Motorola DROID on Ubuntu 9.10 (Karmic Koala)

January 14, 2010

If you are developing for Android on a Motorola DROID using Eclipse (Galileo) on Ubuntu 9.10 and you get a pop-up window in Eclipse about a ‘NullPointerException’ when you connect your DROID and Click ‘Run’ (to install and run your application on the DROID) AND if you are getting the following when you run ‘adb devices’ in terminal:

$ adb devices
List of devices attached
???????????? no permissions

The solution is to do the following:

  1. Create/Edit the file located at ‘/etc/udev/rules.d/10-motorola-droid.rules’ .
    sudo gedit /etc/udev/rules.d/10-motorola-droid.rules

  2. Enter the following into the (possibly blank/new) file:
    (SUBSYSTEMS=="usb", ATTRS{idVendor}=="22b8", ATTRS{idProduct} =="41db", MODE="0600", OWNER="<insert username>"

    Note that you must replace the part that says “insert_username” with your own username.

  3. Save File and Close text editor.
  4. Restart udev.
    sudo service udev restart
  5. Restart adb.
    adb kill-server
    adb start-server
  6. Unplug and Replug-in the USB cable to the DROID.

That should do the trick. If you run ‘adb devices’ again, you should see something similar to the following output:
$ adb devices
List of devices attached
0043482916003106 device

You may need to restart Eclipse to get the debugging support (Logcat, etc.), but you should be able to download and run your development applications on the DROID right away.


C++ Library Not Found

January 12, 2010

With the Android NDK, you can port native C and C++ code to use in your Android applications. Recently, I tried using a library that I had compiled for a previous application, but when I tried to use the same library in my new application, I got an error that read:

01-12 07:06:08.832: ERROR/AndroidRuntime(14371): Uncaught handler: thread Thread-7 exiting due to uncaught exception
01-12 07:06:08.839: ERROR/AndroidRuntime(14371): java.lang.UnsatisfiedLinkError: surfDetDes
01-12 07:06:08.839: ERROR/AndroidRuntime(14371):     at research.util.imageproc.AndroidSURF.surfDetDes(Native Method)
01-12 07:06:08.839: ERROR/AndroidRuntime(14371):     at research.apps.mobilefinder.TakePicture$1$1.run(TakePicture.java:68)

At first I was confused. How could a library that worked with a previous application not be found by a new one. I thought about a lot of reasons why this could have happened such as Android API version, Library name conflict (since the library was named the same as the when I used it in my previous program).

Then I opened the JNI Interface file that I had created for my library and it became clear why my new application was not able to find it. The reason was that the new application was created in a new Java package. The library’s JNI Interface specifies the package name as part of each JNI function name.

To be more detailed, one of the JNI interfaces was declared as:

JNIEXPORT
jfloatArray
JNICALL
Java_examples_android_com_AndroidSURF_surfDetDes

And in my old application, the package name (examples.android.com) was a match.

In my new application the library was accessed from the package research.util.imageproc and therefore the namespaces did not match.

Changing the name of the package in my new application to examples.android.com solved the problem. Likewise, I could have recompiled the library for the research.util.imageproc package by changing the function listing in the C++ JNI file.


Sending Binary with JSON

January 5, 2010

Sending a binary file (i.e. a ZIP archive) over the network with JSON is a little tricky. In my work, I used Python on the packaging side and Java on the unpacking side.

First, you need to open the file. Since the file is binary, you have to open it with an additional “b” flag. Here is an example in Python:

f = open("myzip.zip", "rb")

Next, you have to do binary-to-text encoding on the binary file because the JSON API will complain if you try to feed it a binary string. To overcome this obstacle, you need to apply Base64 encoding. Base64 is not your only option — you can also use Huffman encoding or Hexadecimal. Here is the Python code to do the Base64 encoding:

b64_text = base64.b64encode(f.read())

After you have a text representation of the data, you can then use JSON to package it. I used the simplejson package, so the syntax may be different from other implementations. Nevertheless, here is the code:

json.dumps( { "payload": b64_text } )

A note here is that the “payload”, “{“, and “}” have to be added to build the structure of the JSON structure. What I’m saying in the above call is that I have an ID called “payload” with the value that is held by “b64_text”. There are more complext examples on the JSON website.

With all that done, you can ship out the data across the network.

Once the data arrives on the other end (Java), you have to unpack it. As it stands, the outermost shell is JSON, then Base64, then ZIP. So we start with stripping away the JSON. Java has built-in JSON support, so this is pretty easy with the JSONObject class.

String base64_text = response.getString("payload");

Once the JSON is peeled off, the next step is to take off the Base64 encoding.The most important thing to remember here is to use byte arrays instead of Strings. Something weird happens, probably with the conversion from bytes to characters. But this is the reason why I a lot of my time trying to figure out why I couldn’t unpack my file.

byte[] binary_stuff = Base64.decode(base64_text)

Java does not come with Base64 support for some reason, so I had to download an open-source Java implementation of Base64 from here. There’s also another one here.

The last part of the unpacking use the ZIP archive API from Java to unpack each individual file from the archive.


Comparing Python JSON Implementations

December 23, 2009

In my search for the right Python JSON implementation to use, I compiled the following links to some comparative studies. Here they are:


Installing simplejson on Mac OSX 10.5

December 23, 2009

Just finished installing simplejson (version 2.0.9) because the json package only comes with Python-2.6 which is not in Mac OSX 10.5 (Leopard) yet. The install was a breeze. Here are the steps.

1. Download the ’simplejson’ source code (here)
2. Unpack the tar.gz file
3. Using Terminal, go into the ’simplejson-2.0.9′ directory (cd simplejson-2.0.9)
4. Install ‘Easy Install’ (sudo python ez_setup.py)
5. Update ‘Easy Install’ (sudo python ez_setup.py -U setuptools)
6. Back up to the directory containing the simplejson-2.0.9 directory (cd ..)
7. Install ’simplejson’ (sudo easy_install simplejson-2.0.9)

And you’re done!


Copy a File to Android Emulator

November 11, 2009

The Command to copy a file from your local filesystem to an Android Emulator is:

adb push <path/to/local-file/local-file-name> <absolute/path/on/android/emulator/new-file-name>

Why this was so hard to find, I don’t know. Hope this helps somebody.


Compiling gcc-4.0.2 with gcc-4.3.3

November 5, 2009

I had to compile some code downloaded from a research team halfway around the world that was completed a few years ago which required g++ 4.0.2 in order to compile the code which accesses a library which was compiled with the same version.

Getting gcc-4.0.2 was not so straightforward as I thought. I tried using apt-get, but I could not get the version I wanted by using ‘apt-get install gcc=4.0.2′ because when I entered that, I got: ‘E: Version ‘4.0.2′ for ‘gcc’ was not found”as the error message.

What I had to do instead was compile from source, which didn’t seem all that bad until it didn’t work. I downloaded the source code from http://gcc-ca.internet.bs/releases/, extracted the ‘gcc-4.0.2′ directory (filled with all the source), went to my terminal, changed into the gcc-4.0.2 directory, ran ‘./configure –enable-languages=c,c++’, ran ‘make’, and was greeted with a message saying that I shouldn’t build the source code in the same directory where the source code exists.

Fair enough. I then created a top-level directory (called build-gcc) above the gcc-4.0.2 directory (so, since this was all on my Desktop, it looked like ~/Desktop/build-gcc/gcc-4.0.2/) and tried again. This time, I got further and bunch of code actually compiled, but then I got an error message which looked like this:

In function 'open',
     inlined from 'collect_execute' at ./collect2.c:1580:
 /usr/include/bits/fcntl2.h:51: erreur: call to '__open_missing_mode'
 declared with attribute error: open with O_CREAT in second argument needs 3
 arguments

Luckily, Google-ing for the error message showed another person who had already run into this same problem and even more luckily, someone who offered a solution. But no conclusion was really given to say whether the suggestion worked out. The suggestion was to modify the code that was causing the error, which was a call to ‘open()’ which takes 2 arguments, or, optionally, 3. The error message I was getting during compilation was because the 3rd argument was not given. I found out by tracing through the source code that the 3rd argument is a “mode” that is used during the creation of new files. This basically meant that when a file was created, the mode would determine what the permissions would be for that file. Having that knowledge, I then added the 3rd argument to the function call with an integer (the type is techinically called ‘mode_t’) which I chose to be 0666, the default for programs like vi and touch.

After that change, I saved the file and recompiled and it the compilation finished successfully! From there, it was just a matter of running ’sudo make install’ to move all the binary files into /usr/local/.

As a follow up to this, I decided to take a look at how gcc-4.3.3 was implemented and see if setting the mode to 0666 was actually the right thing to do. I will update this post when I have some results


New SportsMap Icon

August 8, 2009

I got tired of looking at the old SportsMap icon, so I made a new one. I wanted to draw something that described the world (a globe) and have the initials of SportsMap on it in a staggered setup. I got inspiration from a font that I found on dafont.com, called “EA Font v1.5 by Ghettoshark” which is made to look like the font used on EA Sports’ -Live video games. I tried to capture the theme of the EA Sports products by using primarily Black, Red, and Gray. I got tips for making the sphere shiny and glass-like from this tutorial.

It looks decent, a big improvement over what I had before. Next will be a refacing of the overlay items for each sport. I want to make each one have a pointed bottom as I’ve seen on other map-related applications.

New icon:

sportsmapicon

In Android emulated environment:


Replicating Default Android Toast

July 23, 2009

Here is the instructions to replicate the default settings for a Toast object for the Google Android platform. The default Toast is dark gray with a lighter gray border, rounded corners, whitish text, and slightly transparent. Here’s how to create a replica of the default Toast that comes with the Android SDK.

First, create a new XML file in <Project_Home>/res/layout/ (for this example, we’ll use toast_layout.xml).  Fill it in with the following:

<?xml version=“1.0″ encoding=“UTF-8″?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:id=“@+id/toast_layout_root”

android:orientation=“horizontal”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

android:padding=“10dp”

android:background=“@drawable/my_border”

>

<ImageView android:id=“@+id/image”

android:layout_width=“wrap_content”

android:layout_height=“fill_parent”

android:layout_marginRight=“10dp”

/>

<TextView android:id=“@+id/text”

android:layout_width=“wrap_content”

android:layout_height=“fill_parent”

android:textColor=“#FFF”

android:gravity=“center”

/>

</LinearLayout>

Second, create another XML file for specifying a shape that will be the replica of the default Toast. Create the file in <Project_Home>/res/drawable/ (for this example, we’ll use my_border.xml). Fill it in with the following:

<?xml version=“1.0″ encoding=“UTF-8″?>

<shape xmlns:android=“http://schemas.android.com/apk/res/android”>

<stroke android:width=“2dp” android:color=“#ee777777″ />

<solid android:color=“#ee444444″/>

<padding android:left=“20dp” android:top=“2dp”

android:right=“20dp” android:bottom=“2dp” />

<corners android:radius=“5dp” />

</shape>

Lastly, call your new replica Toast in your application using the following:

LayoutInflater inflater = getLayoutInflater();

View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

ImageView image = (ImageView) layout.findViewById(R.id.image);

image.setImageResource(R.drawable.basketball);

TextView text = (TextView) layout.findViewById(R.id.text);

text.setText(tapped.getSnippet());

Toast toast = new Toast(getApplicationContext());

//toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

toast.setDuration(Toast.LENGTH_SHORT);

toast.setView(layout);

toast.show();


Android Shapes

July 23, 2009

Here are some  instructions to create a simple rectangular shape with rounded corners. This shape can be used as a background for a LinearLayout as I will show near the end of this post.

  1. Start by creating a new XML file in your Android project. Save the file into your <Project_Home>/res/drawable/ directory. For this example, we’ll call our file border.xml.
  2. Now fill in the file with the following:
    <?xml version=“1.0″ encoding=“UTF-8″?>
    <shape xmlns:android=“http://schemas.android.com/apk/res/android”>
    <stroke android:width=“2dp” android:color=“#ee777777″ />
    <solid android:color=“#ee444444″/>
    <padding android:left=“20dp” android:top=“2dp”
    android:right=“20dp” android:bottom=“2dp” />
    <corners android:radius=“5dp” />
    </shape>
  3. Then, you can use this shape in your LinearLayout by setting the background to the shape:
    <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
    android:background=“@drawable/border”