Thursday, October 6, 2011

TDD and Android

Today I stumbled upon this post by Adam Goucher in which he laments the lack of support for TDD for Android. I've got to say I whole-heartedly agree with his points, especially the part where he complains about the way most Android books either ignore testing completely or treat it as an afterthought.

For my own Android development I have tried the approach of dividing the application into an Android independent part which I test using regular Java unit tests and an Android dependent part which I currently test the old-fashioned way, i.e. running the app on a real smartphone and watching the logcat output. For my simple app that works quite well but it's only halfway reliable because manual tests are obviously rather boring so I tend to test only the parts I've actively changed instead of doing a full test after each change.

But at least the automated tests cover most of the inner workings of the app which is a big step in the right direction and has enabled me to add features more quickly and reliably than I could before.

Saturday, July 16, 2011

Four months on the Market

It's been almost four months since I opened my developer's account on the Android market and published my first app. Much to my amazement that app is not only constantly downloaded by someone but a significant part of the downloaders find it useful (or harmless) enough to keep it. In fact the number of downloads just broke the 500 mark. So what did I learn from this experiment?

Thursday, May 19, 2011

Spring MVC: Mixing annotation based and classic controllers

One of the improvements in Spring MVC 3 was a nifty namespace simplifying the configuration. If you're starting a new project and want to use the default setup your MVC setup pretty much fits on a napkin:
<beans>
  <mvc:annotation-driven />
  <context:annotation-driven />
  <bean:component-scan base-package="org.acm.steidinger"/>
  <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
  </bean>
</beans>


Now I don’t know about you but I rarely have the pleasure to start a project with a clean slate. And if you already have an application which uses the old style of Spring MVC controllers, too many of them to port them to the new style overnight, you’ll have to have a setup which enables you to use the old and the new in parallel. In that case you can't use the namespace configuration and have to setup your MVC config explicitly.



Unfortunately the reference documentation leaves you on your own. There is no example for this kind of config and neither is there an explanation what <mvc:annotation-driven /> actually does. Of course there is always the source code, but it takes a bit of time to find out what exactly is going on. So I recently took the time to analyze the setup and here is my MVC configuration which allows me to mix annotation based controllers and validators with inheritance based controllers and validators:



<beans>
  <context:annotation-driven/>
  <bean:component-scan base-package="org.acm.steidinger"/>

  <bean id="localValidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
  <bean id="annotationHandlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
  <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
  <bean id="methodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="webBindingInitializer">
      <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
        <property name="validator" ref="localValidator" />
        <property name="conversionService" ref="conversionService" />
      </bean>
    </property>
  </bean>

  <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
      <props>
        <prop key="/classic.htm">classicController</prop>
      </props>
    </property>
  </bean>
  <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
  <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/messages" />
  </bean>
  <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
  </bean>

  <bean id="classicController" class="org.acm.steidinger.springExample.web.ClassicController">
    <property name="commandName" value="form" />
    <property name="formView" value="test" />
    <property name="successView" value="test" />
    <property name="validator">
      <bean class="org.acm.steidinger.springExample.web.ClassicFormValidator" />
    </property>
  </bean>
</beans>


The example above contains the beans necessary to setup the infrastructure as well as a single old style controller.  Pretty straightforward. The only part that was a bit tricky was the AnnotationMethodHandlerAdapter. For JSR 303 aka Bean Validation to work you need to configure the AnnotationMethodHandlerAdapter’s webBindingInitializer with a reference to the validator and conversionService. Otherwise everything will seem to be working but the validation will not work.

Sunday, March 20, 2011

Fooling around with Android

This weekend I finally found the time to dabble with Android development, something I'd planned for quite a while now. Since I'm a fan of Locale, which allows you to change various phone settings based on e.g. your location, I decided to try to write a plugin for Locale. There is already a plugin which lets you silence your phone whenever your calendar shows an appointment but I found that it doesn't work with older Android versions (Too bad my phone only runs Android 1.5.) So this was to be my first Android project...

The first obstacle was that Android doesn't have an official API to get at the calendar data stored on the phone and I really didn't want to hit the net every few minutes. Luckily Jim Blackler has an excellent blog post showing how to access the calendar on your phone, so that problem was solved quickly.
Another invaluable resource was the developer documentation and example plugin provided by Locale's developers, two forty four a.m. LLC.. A few hours later I actually had the first working version of the plugin on my phone (see screenshots).

It's still far from perfect, e.g. I fear it sucks the battery empty much faster than I'd like. Nevertheless I found it quite exciting how easy it was to get so far. If you're interested in the source code you can find it on Github but please bear in mind that it is still quite unpolished. You certainly won't find any clever tricks in there ;-) But unlike that other plugin (for which I paid, BTW), this one at least works...