getting started with the Finch in NetBeans

This year the robotics team is using The Finch for teaching the new programmers about programming.  The robot is cute and I know I’d want to play with it so I bought one too.

While I like Eclipse better than NetBeans, the programming team uses NetBeans.  I decided to try out NetBeans with the Finch so I’m familiar with their platform/interface.  I’ll want it soon enough anyway to check out NetBeans projects from git.

Step 1 – Install NetBeans on the Mac

  1. Download NetBeans – the basic JSE edition
  2. Open dmg file which allows you to choose to run the installer.
  3. Choose defaults and install
  4. When first launch NetBeans asks if want to install JUnit.  (not clear on why JUnit doesn’t come with NetBeans – it comes with Eclipse)
Step 2 – Import Finch project
This one took me a few minutes.  You need to
  1. Download the NetBeans project
  2. Copy it to the NetBeans project directory which is /Users/me/NetBeansProjects on a mac
  3. In NetBeans, File > “open project” and then drill down to Finch.
Step 3 – Run a program
  1. Connect the Finch’s USB to your computer.  (The tutorial doesn’t say this, but it is implied.)
  2. Per the tutorial, choose Run > Run File.
  3. Enjoy the Finch.  It’s so cute!
Step 4 – Run a different programs
I very carefully misunderstood the instructions about not choosing Run > Run and did it anyway.  Luckily, you can choose Run > Run File and have it ignore your preset default.  Or you can completely unset it by editing the file FinchBeans/nbproject/project.properties and removing the main class line.  Then I can remember to choose Run > Run File.

Android Development Gotchas

Android Development Gotchas

This isn’t designed to be a definitive list of unexpected occurrences while developing for Android, but it is a list that I scratched down while I was picking up some skill for myself. It won’t save you from every issue but there may be cases where you can stop and think “Yeah, Dave told me that would happen”.

My development environment included Eclipse 3.5 with the Google ADT plugin and all testing was against a rooted HTC Desire and unroot Samsung Galaxy SII both running Android 2.2, development code targeted Android 2.1. Problems may be an issue with ADT, HTC, Samsung, Android 2.1 or 2.2, rooting or the specific hand sets 🙂

Java 1.6 on a Dell XPS 1530 running Ubuntu 10.10, if that matters.

Virtual Devices

I found these too slow and unusable.

Early on in development I gave up trying to use the the ADT virtual devices and tested directly against the target hardware devices. Luckily for me the application was for internal use only and would be released to specific clients with specific handsets, but I was disappointed at how slow the VMs were start and how slow they ran. The develop-deploy-test cycle was just too slow using the ADT.

Virtual Devices Cannot Support Bluetooth

ADT Virtual Devices cannot support Bluetooth, and as our application includes Bluetooth communication this was another reason ADT development was excluded.

Inconsistent Bluetooth Support Between Vendors

This was a strange one to us, and not one that we have (to this point) been able to resolve although my guess is that it is a problem with the HTC code.

Our application involved having a file sent to the phone using OBEX over Bluetooth. No problems so far. Pair the devices, enter the code, initiate the file transfer.

The Samsung Galaxy SII displayed the preferred behaviour, where the incoming file prompt the phone user to accept the file, but also provides a checkbox to always accept files from this device. If you don’t select the checkbox then you are prompted each time a file arrives, but you are still able to check the box later on.

When the first file arrives, HTC Desire asks if you want to allow the remote device to synchronize contacts, and whether you always want to allow this. If you don’t allow this, you aren’t able to get to the next step where you accept the file. If you don’t accept contact swap all the time you’ll be prompted each time. Regardless of whether you swap contacts or not, you must accept each file as it comes in. Accepting every file manually is not our preferred behaviour.

Android Apps Run on a Single Thread

Some of this cuts into Android’s Handlers, task scheduling and communication across threads, but it may come as a surprise during development that all standard processing occurs on a single Thread. This is something that you’ll want to play with and become familiar with as it may be surprising that UI performance is impacted by Runnables and Services.

I do recommend you have a play.

Creating new Threads is one option, although Android’s AsyncTask is the preferred mechanism for truly asynchronous processing that doesn’t freeze the GUI.

… and that Thread is a UI Thread

Following on from the previous point, the main thread is a UI thread and that that thread is only active when the UI is active. Sort of. Check the Android fundamentals processes and threads page, but just be aware that if you want something to process while (for example) the screen is locked, you need to be aware of the processing thread.

Beware Empty Elements in XML Layouts

If you’re like me, you like tidy XML and prefer an empty element (where applicable) to an element which is not ’empty’ but has no child elements.

	<!-- this element has no children -->
	<ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@android:id/list"></ListView>
	<!-- this is an empty element -->
	<TextView android:id="@android:id/empty" style="@style/EmptyContent" android:text="@string/emptyResponseEntries" />

In this example, the TextView is empty and causes no problems, but the ListView will. If you make the ListView an empty XML element, you can write items to the List, but they won’t show up on the screen. There won’t be any error reported, it just won’t show up.

Can’t Read Property Defaults

Property defaults were also something I found perplexing. If you specify a properties.xml file, each property is able to define a default value. Likewise when reading properties via the SharedPreferences you can specify a default value if the property doesn’t exist, but the two default values aren’t related. You can’t read the default value as defined in the preferences.xml file for use elsewhere, the API doesn’t provide access.

Note that recommended properties management is via the PreferenceFragment

Notifications Can Be Empty

I found this one amusing. It is possible to create an empty Notification using new Notification(), specify a notification sound, and annoy users without providing any indication of where the annoying sounds are coming from.

installing postgres and tomcat on the mac

Back when I was setting up my Mac, I installed Eclipse 3.7.  I also installed Postgres and Tomcat so I could run locally.  Postgres was either unnecessarily frustrating or it required knowledge I didn’t know I was missing.  The whole shared memory thing should have been a clue.  The good news is that I think I did almost everything wrong one could so I have a nice list of problems.  I wrote this up two months ago, but waited to post until I understood better what was going on.

I learned I didn’t know where the applications folder is on the hard drive.   First I learned you can view the path of a file in the finder. Which is all well and good.  It tells me the path is Macintosh HD > Applications > Eclipse.  Awesome.  On a UNIX file system, a directory has a slash up front.  Ok.  So I’m now at a UNIX prompt looking for it.  No Mac HD at the root level.  Volumes looks promising so I cd there and see Macintosh HD as a choice.  Great.  And there’s even an Applications folder under it with my apps!  I found it without having to use “find.”  That wasn’t bad, but it was more surprising than I expected.  I also noticed there is /Applications which will be more convenient next time.

Also, the “one click installer that doesn’t tell you what it is doing” installs some things by unzipping to the directory you are in, some files to /Library and presumably some files to who knows where.  Now I see the scripts I was expecting.

initdb doesn’t have enough privileges as me so I tried root and got initdb cannot be run as root.  Ok so it’s a real UNIX install where I need a user to run as.  I wasn’t expecting this because all my previous installs on the Mac have shielded me from a real UNIX install.

I thought I needed to create a  postgres user to run the database.  In Apple’s system preferences it tells me an id with that name already exists.  But /etc/passwd says nothing about it.  Running

dscl . -list /Users UniqueID

does turn up a postgres id. I need to figure out how the Mac Directory Service thing works.  Anyway, now that I know the postgres users exists I did “sudo passwd postgres” to reset the password to something I know.  Turns out the “sorry” message when trying to sudo to postgres wasn’t from an incorrect password.  Eureka!  I needed to type “sudo su postgres” and then type *my* admin password to switch.  Which kind of makes sense because my regular id isn’t allowed to switch user but my sudo admin user is.

I then changed owner of /Library/PostgreSQL recursively to postgres user.  A nice easy step.  (sudo chown -R postgres /Library/Post*)

Finally, I could run initdb and get prompted for the new superuser password!

I then tried to start the database and got a complaint the lock file already exists.

FATAL:  lock file “/tmp/.s.PGSQL.5432.lock” already exists

HINT:  Is another postmaster (PID 94) using socket file “/tmp/.s.PGSQL.5432”?bash-3.2$

Tried rebooting.  Error still there.  Turns out postgres automatically starts up.  I went on to create a user and database using the usual commands.

Then I opened pgadmin to see if I could access the database.

Connected – maintenance db is database name.

Almost there.  I used the wrong password for the id I created in postgres.  (This matters because the coderanch integration tests assume a certain password.  And because the one I picked I will never remember.)

alter user postgres with password ‘better_password’;

Importing data from file went smoothly.

Ongoing Little problems

  1. Copy paste from Eclipse doesn’t work.  You have to go through textedit.
  2. Postgres uses shift arrow key to select a line.  I wish Apple apps would all use the same convention.

Resources

  • http://developer.apple.com/internet/opensource/postgres.html
  • http://zanshin.net/2009/09/07/installing-postgresql-on-mac-10-6-snow-leopard/ – except for the building from source parts this was useful

Tomcat

Given how many problems I had with Postgres, I decided to follow a tutorial for Tomcat.  This one was easy to understand.  I know enough about the Mac to understand what the sudo is for (to be an admin.)  And the rest is pure UNIX – permissions, making a symbolic link, etc.