Archive for the ‘Android’ Category

Thanks to all who attended my “Induction into the Android Army” talk this afternoon at the monthly Edmonton Java User’s Group meeting.  I’d say it was a good turn out, especially when one considers that this is only the second monthly meeting for EJUG.  It was a pretty basic talk, and didn’t dive to deeply into the “fun” Android stuff.  If anybody from EJUG wants a follow up presentation that’s a bit more in depth, give a shout out on the EJUG mailing list.  If there is enough interest, I’d be happy to put something together.

If you want to browse the code and don’t want to download the Android SDK, you can do so at the Android website.  Otherwise if you have downloaded the SDK,  you can the samples/Notepad folder.  For those who want the PowerPoint slide deck, please hang tight and I’ll get a link to that shortly.  Basil already has a copy of it, and he’ll probably post it to the Google Group shortly as well.

Google-Android-army

Just a heads up for those interested:  On Tuesday, June 15th the Edmonton Java User’s Group is having it’s monthly meeting at noon at the Canadian Western Bank Building.  The speaker is none other than yours truly.  I’ll be giving a brief introduction to application development to Android, using my trusty G1 and IntelliJ.

It’s free to attend, so stop by if you’re so inclined.

Gotta love companies that “get it”.  Late last night I was hacking away on some Android stuff using IntelliJ 9.0.2 (on Ubuntu 10.04). For reasons unknown to me, none of my breakpoints seemed to be working.  In fact, IntelliJ just didn’t seem to be working.  I narrowed it down to the breakpoints I was setting – it seemed that every time the breakpoints were being hit.  I managed to narrow it down to this error:

[ 166030]  ERROR – lij.debugger.impl.InvokeThread – null
java.lang.UnsupportedOperationException
at com.sun.tools.jdi.ReferenceTypeImpl.sourceDebugExtension(ReferenceTypeImpl.java:774)
at org.jetbrains.plugins.ruby.jruby.debug.JRubyPositionManager.getPath(JRubyPositionManager.java:141)
at org.jetbrains.plugins.ruby.jruby.debug.JRubyPositionManager.getPsiFileByLocation(JRubyPositionManager.java:156)
at org.jetbrains.plugins.ruby.jruby.debug.JRubyPositionManager.getSourcePosition(JRubyPositionManager.java:51)
at com.intellij.debugger.engine.CompoundPositionManager.getSourcePosition(CompoundPositionManager.java:51)
at com.intellij.debugger.engine.ContextUtil.getSourcePosition(ContextUtil.java:63)
at com.intellij.debugger.impl.DebuggerSession$MyDebugProcessListener$2.compute(DebuggerSession.java:462)
at com.intellij.debugger.impl.DebuggerSession$MyDebugProcessListener$2.compute(DebuggerSession.java:460)
at com.intellij.psi.impl.PsiDocumentManagerImpl$3.run(PsiDocumentManagerImpl.java:298)
at com.intellij.psi.impl.PsiDocumentManagerImpl$4.run(PsiDocumentManagerImpl.java:321)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:695)
at com.intellij.psi.impl.PsiDocumentManagerImpl.commitAndRunReadAction(PsiDocumentManagerImpl.java:317)
at com.intellij.psi.impl.PsiDocumentManagerImpl.commitAndRunReadAction(PsiDocumentManagerImpl.java:296)
at com.intellij.debugger.impl.DebuggerSession$MyDebugProcessListener.paused(DebuggerSession.java:460)
at com.intellij.debugger.engine.DebugProcessAdapterImpl.paused(DebugProcessAdapterImpl.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at com.intellij.util.EventDispatcher.dispatch(EventDispatcher.java:87)
at com.intellij.util.EventDispatcher.access$100(EventDispatcher.java:33)
at com.intellij.util.EventDispatcher$1.invoke(EventDispatcher.java:64)
at $Proxy84.paused(Unknown Source)
at com.intellij.debugger.engine.SuspendManagerImpl.notifyPaused(SuspendManagerImpl.java:306)
at com.intellij.debugger.engine.SuspendManagerImpl.b(SuspendManagerImpl.java:299)
at com.intellij.debugger.engine.SuspendManagerImpl.voteSuspend(SuspendManagerImpl.java:318)
at com.intellij.debugger.engine.DebugProcessEvents$1.contextAction(DebugProcessEvents.java:412)
at com.intellij.debugger.engine.events.SuspendContextCommandImpl.action(SuspendContextCommandImpl.java:62)
at com.intellij.debugger.engine.events.DebuggerCommandImpl.run(DebuggerCommandImpl.java:44)
at com.intellij.debugger.engine.DebuggerManagerThreadImpl.processEvent(DebuggerManagerThreadImpl.java:148)
at com.intellij.debugger.engine.DebuggerManagerThreadImpl.processEvent(DebuggerManagerThreadImpl.java:36)
at com.intellij.debugger.impl.InvokeThread.run(InvokeThread.java:135)
at com.intellij.debugger.impl.InvokeThread$WorkerThreadRequest.run(InvokeThread.java:52)
at com.intellij.openapi.application.impl.ApplicationImpl$5.run(ApplicationImpl.java:329)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:636)
at com.intellij.openapi.application.impl.ApplicationImpl$1$1.run(ApplicationImpl.java:125)
[ 166036]  ERROR – lij.debugger.impl.InvokeThread – IntelliJ IDEA 9.0.2  Build #IU-95.66
[ 166036]  ERROR – lij.debugger.impl.InvokeThread – JDK: 1.6.0_18
[ 166036]  ERROR – lij.debugger.impl.InvokeThread – VM: OpenJDK Server VM
[ 166036]  ERROR – lij.debugger.impl.InvokeThread – Vendor: Sun Microsystems Inc.
[ 166036]  ERROR – lij.debugger.impl.InvokeThread – OS: Linux
[ 166036]  ERROR – lij.debugger.impl.InvokeThread – Last Action: Debug

So, the problem to me seemed to be something wonky with IntelliJ.  I e-mailed Jetbrains, explaining the symptoms and the above stack trace.  This morning, I was pleased to find an e-mail from Serge  at Jetbrains.  He suggests disabling the Ruby plug-in that I have installed.

BINGO!

Worked like a charm.  Problem goes away, and in less than 12 hours since I asked for help.

I just uploaded an update to HistoricalBuildings.  The list of historical buildings used to be sorted alphabetically, by name.  Now they are sorted by the distance from your current location (assuming the GPS can figure that out).

works-on-my-machine-starburst Well, for the brave, criminally insane, curious, or otherwise bored I have a alpha version of Historical Buildings – download the APK if you want to try it out.  This is just, at this time, the application just shows a simple list of historical buildings in Edmonton (according to the City of Edmonton’s Open Data Catalogue).  Click on a building, and it will show you on Google Maps where the building is in the city.  The code for this is, in my opinion (and to say the least) – rough.  But it is a start.  Definitely needs some improvement.  Or maybe I just need to change my thinking to more of a Android/Java mindset.  Anyway the usual caveats apply:  use at your own risk / batteries not include / do not eat / void where prohibited by law, etc, etc, etc

Anyway, if you use it let me know.  There are bound to be bugs, but hey it “Works for me!”.  Time to work on some other stuff for it.  I think the next neat thing is would be to show the closest building to you.  Either that, or handle some of the seedy code issues and infrastructure stuff that bugs me. 

Screen shot of the list of historical buildings   Clicking on a build shows you where the building is

 

MapList

Thanks to the MapView, it’s drop dead easy to put Google Maps into your application.  There are lots of posts out there how to do it.  Interestingly (to me anyway), when I did a quick search of the Android developers mailing list, I was surprised to see that a lot of people had the same problem:  basically, when you look at your MapView, you end up with a grid of white squares.  It kind of sucks.  There are a lot of blog posts and articles out there on how to fix this problem, but this one is for me, and to help me remember it.

The trick to fixing this is setting your Google Maps API key.  Note the layout below:

   1: <?xml version="1.0" encoding="utf-8"?>


   2: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"


   3:     android:id="@+id/mainlayout"


   4:     android:orientation="vertical"


   5:     android:layout_width="fill_parent"


   6:     android:layout_height="fill_parent" >


   7:


   8:     <com.google.android.maps.MapView


   9:         android:id="@+id/mapview"


  10:         android:layout_width="fill_parent"


  11:         android:layout_height="fill_parent"


  12:         android:clickable="true"


  13:         android:apiKey="Your Maps API Key"


  14:     />


  15:


  16: </RelativeLayout>

hotel-key

In particular, pay attention to line #13.  What you have to do is create a Google Maps API key, and then paste it in there.  That’s simple.  How do you do that?  Well it’s simple too, but not as simple as it could be.  The first thing you need to do is to sign your application.  This isn’t a big deal, you have to sign your application before you can publish it anyway.  Android will NOT install an application otherwise.

So, it seems we have two steps here:

  1. Sign your application
  2. Use your signed application to get a Google Maps apiKey.

Generate a private key.  Keep this safer, as if it were the Holy Grail.  Lose this, and as far as the public is concerned, you’re locked out from your own application – you will not be able to update it.  The image below will show you the steps to go through

    "C:\Program Files (x86)\Java\jdk1.6.0_19\bin\keytool" -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -validity 10000

keytoolsample

Once you have that done, build your application (in release mode).  I leave this as an exercise for the reader.

Now that you have your application built, you need to sign it.  This is where jarsigner comes in:

jarsignerAnd the final step is to zipalign your  APK.  You have to do zipalign last.  Basically, you do zipalign for performance reasons.  If you want to know more, RTFM.

C:\android-sdk-windows\tools\zipalign -v 4 HistoricalBuildings-unalign.apk HistoricalBuildings.apk

zipalign

Now, of course, both Eclipse and IntelliJ will handle these steps for you.  But where is the fun in that?

YegDataListNot that long ago, the City of Edmonton announced it’s Open Data Catalogue.  I noticed that one of the data catalogues was a list of historical buildings in the city.  Yeah, I know that some people in other cities might consider this a pretty weak list.  I mean, the oldest building in Edmonton isn’t even 150 years old.  I’d wager that some parts of the world consider 150 year old buildings to be “new construction”.  Anyway, as I’ve been dabbling with Android now for about the past year, I thought a handy little project to try out would be to write an app for Android that would show me where the historical buildings are in the city.

I’ve got the foundation of a simple application started over a GitHub called, not surprisingly, Historical Buildings (if you pull the source code, then just a heads up that I use IntelliJ for my Android development).  This application currently works in the emulator, my next step is to installing it on my phone and trying it out for real.  Right now all the application does is present you with a list of buildings.  You click on one, and you’ll see on Google Maps where the building.

Some other ideas for features that may or may not happen:

  • Figure out the whole unit testing story in Android.  Then setup a build script.  Then a build server.  CI rocks, but as I’m trying to learn the Android framework and figure out what all the parts are, I’m not to focused on that at the moment.
  • Don’t always fetch the data – store it locally
    • I don’t think the way I’m parsing the JSON result is very efficient or “proper”, but it does get the job done.
  • Notify the user when they are within a certain distance of a historical building.
  • Currently YEG has a PDF explaining the significance of the building.  Not very friendly/convenient for mobile devices.  Need something better.
    • One thought would be to create Wikipedia entries and link to that.
    • Another thought is to petition the City of Edmonton to convert their PDF’s to a more neutral format (HTML anyone)?
    • The Edmonton Public Library has some information about the “Lost Building of Edmonton”.  Maybe incorporate that somehow – perhaps showing what buildings used to exist at a given address?
  • Perhaps allow the user to update their coordinates for YEG’s historical buildings.  I’ve notice (on the emulator) that sometimes the lat/long isn’t exactly accurate.  If you look at the image below for the old Arlington Apartments, you notice that the marker should be over the pile of rubble slightly to the left of where it currently is.
  • Maybe include buildings that aren’t necessarily designated as historical, but are interesting architecturally or otherwise.
  • Instead of using YEG OpenData, perhaps an independent cloud based data store (Azure maybe?)

If you’ve got any other suggestions, I’m happy to hear them.

MapList

After a bit of tinkering, I managed to provision my ADP1 setup without a SIM card.  A bit of google, and here is what I did

  1. Download the Android SDK.  In my case, I unzipped it to C:\android-sdk-windows-1.1_r1.
  2. Connection the phone via the USB cable to my computer.  When the phone asks for a device.
  3. You’ll get the new hardware dialog, when prompted for the drivers, you’ll need to specify the location.  The Android SDK has the drivers, in my case they were at C:\android-sdk-windows-1.1_r1\usb_driver\x86.
  4. Once you have the drivers installed cd to c:\android-sdk-windows-1.1_r1\tools.
  5. Type adb devices.  This should list all the Android devices that you have connected.  If you don’t see any devices listed, then you have a problem.
  6. Type adb shell.  This will direct your commands to your ADP1
  7. Now while at the adb shell, su to root, then : 
  8. cd /data/data/com.android/providers.settings/databases/
  9. sqlite3 settings.db
  10. INSERT INTO system (name, value) VALUES (‘device_provisioned’, 1);
  11. .quit
  12. Reboot the phone
  13. adb shell
  14. am start –a android.intent.action.MAIN –n com.android.settings/.Settings

Once all that is done, you should be enable to use your ADP1 as if you had a SIM card in it (we’ll except for the phoning part).

Useful link:  FAQ: Unlocking/Activating a G1 or ADP1 Without A Sim Card.

(Note:  this article is pretty dated, and used IntelliJ 8.  I plan a follow-up covering IntelliJ 9)

So I have this semi-fancy Google Android Dev Phone 1.  Lately I’ve been devoting part of my spare time to learning about programming for Android (the OS of the phone).  Google (probably because they didn’t ask for my opinion and/or input) decided to use Java as the lingua franca for Android programming.  If you ask me – and I know you will – they should have used C# and Mono (I might be a bit biased here).  Luckily, years ago I had done Java programming, so I wasn’t that intimidated by the use of Java on Android.

The first big question that every developer faces is which IDE?  There are a few Java IDE’s out there, but if you ask me the only ones worth considering are Eclipse and IntelliJ.  The documentation for Android points you to using Eclipse.  Eclipse is a good IDE.  However, back in the day when I was getting paid for Java development, my employer got us all copies of IntelliJ, from JetBrains.  I liked it.  I like it enough that if I landed a contract tomorrow that involved Java, I’d buy me a copy of IntelliJ.

So, all that being said, I figured I’d give IntelliJ a spin as I travelled down the stack-trace of Android programming.  Here are my observations, in general:

  1. There is an Android plug-in for IntelliJ.  Now, you might believe that development on the Android plug is dead.  Not true.  The plug-in is undergoing active development – it just seems to be kind of slow
  2. I found the installation of the Android plug-in for IntelliJ far easier than for Eclipse.  Just download the most current release, and then unzip to your plugins folder in your IntelliJ installation.  With Eclipse, it is simple, but not when the documentation is wrong.
  3. The IntelliJ plug-in is simple, and seems to get the job done.  It’s a bit to simple at this time, if you ask me.  The ADT for Eclipse provides a far richer dev-centric experience for Android coding.  For example there are designers to help you with laying out your form, a lot more control over starting up the Android emulator, and better tooling for hooking up the debugger to either the emulator or an app running on the physical phone.
  4. Being a Resharper junkie, I found that IntelliJ was more natural for me to use.
  5. I didn’t run into to many problems when I was trying to use/convert Eclipse project with IntelliJ. This is good, because the vast majority (all?) projects I’ve seen are all Eclipse based.

So, my conclusion at the end of the day, is that I’m going to stick with Eclipse for my Android development.  There just seems to be less friction at the moment if you’re using Eclipse.  In a couple of months maybe I revisit IntelliJ and see what’s new with it and Android development.  However, at this time, I’d like to really concentrate on learning the Android SDK, and it seems simplest to me right now with Eclipse.

After a bit of tinkering, I managed to provision my ADP1 setup without a SIM card.  A bit of google, and here is what I did:

  1. Download the Android SDK.  In my case, I unzipped it to C:\android-sdk-windows-1.1_r1.
  2. Connection the phone via the USB cable to my computer.  When the phone asks for a device.
  3. You’ll get the new hardware dialog, when prompted for the drivers, you’ll need to specify the location.  The Android SDK has the drivers, in my case they were at C:\android-sdk-windows-1.1_r1\usb_driver\x86.
  4. Once you have the drivers installed cd to c:\android-sdk-windows-1.1_r1\tools.
  5. Type adb devices.  This should list all the Android devices that you have connected.  If you don’t see any devices listed, then you have a problem.
  6. Type adb shell.  This will direct your commands to your ADP1
  7. Now while at the adb shell, su to root, then : 
  8. cd /data/data/com.android/providers.settings/databases/
  9. sqlite3 settings.db
  10. INSERT INTO system (name, value) VALUES (‘device_provisioned’, 1);
  11. .quit
  12. Reboot the phone
  13. adb shell
  14. am start -a android.intent.action.MAIN -n com.android.settings/.Settings

Once all that is done, you should be enable to use your ADP1 as if you had a SIM card in it (we’ll except for the phoning part).

Useful link:  FAQ: Unlocking/Activating a G1 or ADP1 Without A Sim Card.