Archive for the ‘Programming’ Category

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?

I guess I forgot to mention this:  On April 1, 2010, I received an e-mail from Microsoft that my MVP in C# was renewed for the third year.  This makes me a happy, because with Resharper 5 (and therefore Visual Studio 2010) and Windows Mobile 7, I’m hoping that this will be an exciting year for the .NET crowd. 

Well, okay, perhaps I didn’t forget.  Given that it was April 1st when I got the e-mail, I like to give things a few days to settle down.  I am acquainted with some mischievous pranksters who would think it was a funny April Fools joke.

MVP_Horizontal_FullColor

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

Just sitting here at Legoland in Carlsbad, CA waiting for my kids to get off Coastersaurus. Just thought I’d make a mention about my presentation on SOLID to the Inland Empire .NET User Group. This is my third year in a row as their March speaker. I’m glad to say that for the third year in a row they haven’t chased me away with pitch-forks and torches. Largely based on that reason, I’ll say the presentation went well. Well, that and the fact that the concepts did seem to be understood and there were no technical glitches.

Thanks James and his crew for agreeing to shift from the 2nd Tuesday to the 3rd Tuesday to accommodate my travel.

Here is a zip of the code and slide deck.

Yesterday I was at TechDays 2009 in Calgary for the day (well, the morning really).  I wasn’t there as an attendee, but as a speaker.  Thanks to everybody who came out to my two talks, the first one on an Introduction to ASP.NET MVC and the second one on SOLIDifying your ASP.NET MVC Application.  There were a few hiccups along the way:  as usual, I fell victim to the Technical Presentation Time Dilation Syndrome (i.e. ran out of time) on both talks.  I guess I’m just to long-winded for my own good.  As well, because I was running a EAP of Resharper 5, I had some Visual Studio 2008 issues – namely VS2008 would hang on me from time to time.  Note to self: maybe don’t use the bleeding edge tools in a talk.  And I really should break myself of wanting to type all the code – using snippets would really save me time.

Two things that suprised me:

  1. That there was still that much interest in ASP.NET MVC intro talks.  I just figured that everybody knew/knows about it.  Or maybe I just spend to much time with guys who have drunk the ASP.NET MVC Flavor Aid.  Either way, I think that there were some in the crowd that will convert from WebForms to ASP.NET MVC.
  2. I asked how many people wrote unit tests or developing their software using Test Driven Development was the number of people who didn’t raise their hands.  While I didn’t expect the majority of the crowd, I did expect a lot more people than I saw.  Maybe it was a quiet crowd, and people didn’t feel like saying they wrote unit tests.  Maybe I live in a bubble, and my perception of what is actually going on in the .NET world is skewed (that is to say, perhaps writing tests is the exception rather than the rule).  It did kind of make me a bit concerned, as I truly believe that TDD does help one write better software overall.

Anyway, here’s to hoping that TechDays 2010 will stop off in Edmonton.  And hopefully have a track that will focus on developer fundamentals, like TechDays had in Toronto and Vancouver.

Out of intellectual curiosity, I sparked up MonoDevelop and decided to see what would happen when I tried to compile the ASP.NET MVC source code. I figured that it would Just Work.  After all, I can use the .NET compiled assembly with no problems on Mono, so there really shouldn’t be any problems trying to compile the code.  Bad news is that there is one minor glitch.  Good news is the glitch is easy to work around and has already been fixed so it shouldn’t be a problem for future version of Mono.

When you compile, here is what you get:

Compilation failed: 1 error(s), 0 warnings

/home/tom/Projects/MVC-MS-Pl-original/MVC-MS-Pl/Mvc/FormCollection.cs(25,6): error CS0246: The type or namespace name `FormCollectionBinder’ could not be found. Are you missing a using directive or an assembly reference?

Build complete — 1 error, 0 warnings

So, this kind of surprised me.  Looking at the code, I did see the FormCollectionBinder in the code, it’s an embedded class within System.Web.Mvc.FormCollection, check out lines 68-88 below:

   1: /* **************************************************************************** 
   2:  * 
   3:  * Copyright (c) Microsoft Corporation. All rights reserved. 
   4:  * 
   5:  * This software is subject to the Microsoft Public License (Ms-PL).  
   6:  * A copy of the license can be found in the license.htm file included  
   7:  * in this distribution. 
   8:  * 
   9:  * You must not remove this notice, or any other, from this software. 
  10:  * 
  11:  * ***************************************************************************/ 
  12:  
  13: namespace System.Web.Mvc { 
  14:     using System; 
  15:     using System.Collections.Generic; 
  16:     using System.Collections.Specialized; 
  17:     using System.Diagnostics.CodeAnalysis; 
  18:     using System.Globalization; 
  19:     using System.Web.Mvc.Resources; 
  20:  
  21:     [SuppressMessage("Microsoft.Usage", "CA2237:MarkISerializableTypesWithSerializable", 
  22:         Justification = "It is not anticipated that users will need to serialize this type.")] 
  23:     [SuppressMessage("Microsoft.Design", "CA1035:ICollectionImplementationsHaveStronglyTypedMembers", 
  24:         Justification = "It is not anticipated that users will call FormCollection.CopyTo().")] 
  25:     [FormCollectionBinder] 
  26:     public class FormCollection : NameValueCollection { 
  27:  
  28:         public FormCollection() { 
  29:         } 
  30:  
  31:         public FormCollection(NameValueCollection collection) { 
  32:             if (collection == null) { 
  33:                 throw new ArgumentNullException("collection"); 
  34:             } 
  35:  
  36:             Add(collection); 
  37:         } 
  38:  
  39:         public IDictionary<string, ValueProviderResult> ToValueProvider() { 
  40:             CultureInfo currentCulture = CultureInfo.CurrentCulture; 
  41:  
  42:             Dictionary<string, ValueProviderResult> dict = new Dictionary<string, ValueProviderResult>(StringComparer.OrdinalIgnoreCase); 
  43:             string[] keys = AllKeys; 
  44:             foreach (string key in keys) { 
  45:                 string[] rawValue = GetValues(key); 
  46:                 string attemptedValue = this[key]; 
  47:                 ValueProviderResult vpResult = new ValueProviderResult(rawValue, attemptedValue, currentCulture); 
  48:                 dict[key] = vpResult; 
  49:             } 
  50:  
  51:             return dict; 
  52:         } 
  53:  
  54:         public virtual ValueProviderResult GetValue(string name) { 
  55:             if (String.IsNullOrEmpty(name)) { 
  56:                 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name"); 
  57:             } 
  58:  
  59:             string[] rawValue = GetValues(name); 
  60:             if (rawValue == null) { 
  61:                 return null; 
  62:             } 
  63:  
  64:             string attemptedValue = this[name]; 
  65:             return new ValueProviderResult(rawValue, attemptedValue, CultureInfo.CurrentCulture); 
  66:         } 
  67:  
  68:         private sealed class FormCollectionBinderAttribute : CustomModelBinderAttribute { 
  69:  
  70:             // since the FormCollectionModelBinder.BindModel() method is thread-safe, we only need to keep 
  71:             // a single instance of the binder around 
  72:             private static readonly FormCollectionModelBinder _binder = new FormCollectionModelBinder(); 
  73:  
  74:             public override IModelBinder GetBinder() { 
  75:                 return _binder; 
  76:             } 
  77:  
  78:             // this class is used for generating a FormCollection object 
  79:             private sealed class FormCollectionModelBinder : IModelBinder { 
  80:                 public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { 
  81:                     if (controllerContext == null) { 
  82:                         throw new ArgumentNullException("controllerContext"); 
  83:                     } 
  84:  
  85:                     return new FormCollection(controllerContext.HttpContext.Request.Form); 
  86:                 } 
  87:             } 
  88:         } 
  89:  
  90:     } 
  91: }

The version of Mono 2.4.3 I have doesn’t compile the class FormCollection because the FormCollectionBinderAttribute is inside the FormCollection class.  This is the bug, and it has since been fixed.  Easy ways to get around this:

  1. Just use the binary assembly from Microsoft for ASP.NET MVC.
  2. Build your own Mono with the fix included
  3. Move the code for FormCollectionBinderAttribute class outside of the code for the FormCollection class.

One question that has been bouncing around in my mind is what, exactly, does one have to do to be considered Agile?  Let’s look at the Agile Manifesto:

Individuals and interactions over processes and tools
Working software over comprehensive documentation
Customer collaboration over contract negotiation
Responding to change over following a plan

That is, while there is value in the items on
the right, we value the items on the left more.

Sounds pretty simple.  I notice that there are twelve Agile principles:

Our highest priority is to satisfy the customer
through early and continuous delivery
of valuable software.

Welcome changing requirements, even late in
development. Agile processes harness change for
the customer’s competitive advantage.

Deliver working software frequently, from a
couple of weeks to a couple of months, with a
preference to the shorter timescale.

Business people and developers must work
together daily throughout the project.

Build projects around motivated individuals.
Give them the environment and support they need,
and trust them to get the job done.

The most efficient and effective method of
conveying information to and within a development
team is face-to-face conversation.

Working software is the primary measure of progress.

Agile processes promote sustainable development.
The sponsors, developers, and users should be able
to maintain a constant pace indefinitely.

Continuous attention to technical excellence
and good design enhances agility.

Simplicity–the art of maximizing the amount
of work not done–is essential.

The best architectures, requirements, and designs
emerge from self-organizing teams.

At regular intervals, the team reflects on how
to become more effective, then tunes and adjusts
its behaviour accordingly.

The agile principles are interesting, because in a lot of ways I can see advocates of other methodologies (BDUF) saying the believe in the same thing.

Asking amongst some peers, and there didn’t really seem to be a concise definition of Agile and what you need to do to call yourself.  I’ve seen some places that will suggest that they are Agile because they stand up at meetings (or a manager wanders around once a day), avoid documentation, and have a meeting every three or four weeks that they call a “retrospective”.  So, does anybody care to offer up their thoughts on what one needs to do to be considered Agile?  I have my own thoughts, but figured I’d save them for a later blog post.

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.

Every developer, no matter how experienced, has to dig in and fix bugs.  This is, in my experience, the typical cycle:

  1. bug is reported
  2. bug is assigned to developer
  3. developer thinks the bug is fixed
  4. developer marks bug as resolved
  5. tester checks to see bug is fixed, but it isn’t.
  6. tester reopens bug, assigns it back to developer
  7. back to step number two.  Repeat until developer (or tester) climbs to the top of a 307 foot administrative building with high-powered rifles to demonstrate USMC-worthy marksmanship.

Now, even if you aren’t into the whole Lean Software development philosophy (i.e. eliminating waste), you can probably appreciate that it can be pretty wasteful of time and patience every time that #6 happens, so please indulge me as I elaborate on some of the finer points of bug reporting.  Now, where I say “bug reporter”, I typically mean somebody who is in a QA role – their job is to find bugs and report them.  This doesn’t always have to be the case though.

If you check out most open source projects, you will often find rules/guidelines for reporting bugs.  In the case that doesn’t exist then perhaps the following might be of use: When you log a bug, be explicit, and give lots of details.  Don’t be lazy.  If it’s hard to duplicate your bug, then odds are it either won’t get looked at, or won’t be fixed properly.  And when it doesn’t get fixed properly, there will be a lot of churn.

At the very least, when reporting a bug you should provide:

  • A detailed description of the problem.  Note the emphasis on problem.  Odds are you don’t have the solution, so don’t bother.  Let the programmer figure out the solution.  For example:  Using version 3.1415 of the application on 28-July-2009, I was expected a date to be calculated a certain way based on some values.  The application didn’t crash, but the correct date was not calculated.
  • What you expect to happen.  If you have some sort of formal spec, repeat it here, and cite how to find the spec.  i.e.  According to Page 1054, in section 1095.4.3.2 the new date is to be calculated as follows :  add the value of  ((a – b)- 10) /2 (rounding down) to the date Y for a new date of Z.
  • What actually happened.  i.e.  Using these values a=300, b= 150 and Y of 1-Sep-2009 I expected that the new date Z should be 2-Oct-2009
  • A step-by-step description of HOW to duplicate the bug.  This means explaining in excruciating detail HOW the programmer can duplicate what your reporting, i.e.
    • Go to the URL http://myproblem.com/is/here
    • Click on the text box next to the label “Value A:” and enter a value of 300
    • From the drop down list by “Value B:” and click the value 150 from the drop down list
    • Use the tab key to navigate to the “Calculate new date” button.
    • Hit Enter
    • The text box by the “New Date:” label now has the date of 4-July-1943.  I expected 2-Oct-2009.
  • How the developer can get a hold of you.  This comes in handy when for some reason the developer can’t duplicate your bug or doesn’t understand what you were doing.

When the developer flags the bug as resolved and it turns out not to be fixed, don’t just re-open the bug with curt “This is still broken”.

Ah, but now aren’t I the smug developer – shifting my inability to quickly resolve a bug to the poor reporting capabilities of my tester.  I’m pretty ungrateful too.  I mean, somebody took the time to test out my application, and then enter a bug like “I tried to calculate the new date and it didn’t work.  Please fix”.  Well, fear not – there is a lot developers should be doing as well, but that is the subject of a later post.

Of course, the key part in all of this is COMMUNICATION.  Don’t be afraid, as someone reporting a bug, to actually talk to the developer.  Nothing really beats face-to-face communication (or a phone call) if things seem unclear. Odds are you won’t have an empty whisky bottle thrown at you.  Most likely the personal grooming standards of the developer will be higher than that of a middle age serf during the Black Plague.  In theory, as a tester, you’re on the same side as the developer trying to fix the bug.

(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.