Kazed

Pragmatic software development

  • Increase font size
  • Default font size
  • Decrease font size
Home Android

NextAction timer


NextAction timer

 Timer - short breakI created a timer function into the NextAction application, inspired by the Pomodoro technique.

When you start the timer, it will display a notification in the Android notification bar.



Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! Mixx! Free and Open Source Software News Google! Live! Facebook! StumbleUpon! Yahoo! Free Joomla PHP extensions, software, information and tutorials.
 

PhotoMaps published

Tags: android


QR codeI published my first Android application: Photomaps

With this application you can view your current location on a map without needing Internet access. You need a photo or other image of a map and calibrate it with three reference points with known GPS coordinates.

 



Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! Mixx! Free and Open Source Software News Google! Live! Facebook! StumbleUpon! Yahoo! Free Joomla PHP extensions, software, information and tutorials.
 

Android presentation


J-Spring logoI have done a presentation application development for the Android platform at the JSpring 2009 of NLJUG.

Here is the content of the presentation.

 



Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! Mixx! Free and Open Source Software News Google! Live! Facebook! StumbleUpon! Yahoo! Free Joomla PHP extensions, software, information and tutorials.
 

Reflection performance with injection

Tags: android | java | testing


I am experimenting with a simple form of dependency injection that uses reflection and annotations. I was wondering what the performance of this solution is, since reflection once was rumored to be very slow.

First I measured the speed of setting up a HashMap with the service bindings; this consists of a simple map.put() operation:

public void bind(Class interfaceClass, Class implementationClass) {
    binding.put(interfaceClass, implementationClass);
}

This turns out to be very fast: this takes 0.0002 milliseconds.

 I also provide a way of binding a service that checks the existence of an annotation:

public void bind(Class implementationClass) {
    Component component = implementationClass.getAnnotation(Component.class);
    binding.put(component.value(), implementationClass);
}

 Here, the additional annotation lookup makes this a bit slower: 0.0005 milliseconds.

 The injection uses reflection to lookup the setter methods and is a bit more work:

public void inject(Object target) {
    Class targetClass = target.getClass();
    Method[] methods = targetClass.getMethods();
    for (Method method : methods) {
        if (method.getName().startsWith("set")) {
            // method is a setter, check if method has "@Autowired" annotation
            Autowired inject = method.getAnnotation(Autowired.class);
            if (inject != null) {
                // method has annotation
                findParametersAndInvoke(target, method);
            }
        }
    }
}

This takes about 0.030 milliseconds.

To conclude these very simple benchmarks, which I have done in a for loop a few thousand times to get measurable results which may or may not be realistic, setting up the application context with a HashMap is very fast and injecting a dependency into an object less fast. I think that a typical application would not use injection into an object less often, so dependency injection should produce only minimal overhead.



Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! Mixx! Free and Open Source Software News Google! Live! Facebook! StumbleUpon! Yahoo! Free Joomla PHP extensions, software, information and tutorials.
 

Simple setter injection with annotations

Tags: android | java | testing


To implement simple setter dependency injection with annotations, you can use reflection
to find all setters and lookup the corresponding singletons that should be injected as
parameters.

Example use to inject dependencies:

ApplicationContext appContext = new ApplicationContext();
 // use ServiceImpl as implementation of Service
appContext.bind(Service.class, ServiceImpl.class);

Activity1 activity = new Activity1();
// inject dependencies into activity: use setService to set ServiceImpl singleton
appContext.inject(activity);

String value = activity.execute();

 

Lookup all methods and invoker setters:

public void inject(Object target) {
    Class targetClass = target.getClass();
    Method[] methods = targetClass.getMethods();
    for (Method method : methods) {
        if (method.getName().startsWith("set")) {
            // method is a setter, check if method has "@Autowired" annotation
            Autowired inject = method.getAnnotation(Autowired.class);
            if (inject != null) {
                // method has annotation
                findParametersAndInvoke(target, method);
            }
        }
    }
}


Try to find parameters in the registered singleton map and invoke the setter:

private void findParametersAndInvoke(Object target, Method method) {
    Class[] parameterTypes = method.getParameterTypes();
    Object[] parameters = new Object[parameterTypes.length];
    int parameterIndex = 0;
    boolean allParametersInstantiated = true;
    for (Class parameterType : parameterTypes) {
        if (singletons.containsKey(parameterType)) {
            // singleton for parameter type is registered
            parameters[parameterIndex] = singletons.get(parameterType);
        } else if (binding.containsKey(parameterType)) {
            // type is registered and not instantiated yet
            parameters[parameterIndex] = createSingleton(parameterType);
        } else {
            allParametersInstantiated = false;
        }
        parameterIndex++;
    }
    if (allParametersInstantiated) {
        try {
            method.invoke(target, parameters);
        } catch (IllegalArgumentException e) {
            throw new InjectException(e);
        } catch (IllegalAccessException e) {
            throw new InjectException(e);
        } catch (InvocationTargetException e) {
            throw new InjectException(e);
        }
    }
}


You might be thinking "Why make your own application context while we have Spring, Guice
and many other IoC containers?". The reason is Android: I would like to use something like
Spring or Guice in my Android application. Although these containers are considered
"lightweight" (compared to EJB, I guess), they are still a bit too heavy to use on a phone.
The size of jars and the fact that your Android application might be stopped and started
frequently does not work well with the existing containers.

That, and also the learning experience makes me experimenting with this stuff. So far,
working with annotations makes this very easy to implement and seems efficient enough.

Implementation: setter-annocation-injection.zip



Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! Mixx! Free and Open Source Software News Google! Live! Facebook! StumbleUpon! Yahoo! Free Joomla PHP extensions, software, information and tutorials.
 

Android phone arrived

Tags: android


Good news: I ordered an Android developer phone last week and I received it here in the Netherlands within a week!

Costs for me: $577 (USD), which includes the phone, shipping, customs and other taxes. It comes in a no-frills box, very short instruction card. As a developer we can figure out how to setup the mobile network stuff (it took me some googling), and I would not recommend this to non-technical people.

The phone works great, even though I currently do not have a data subscription yet. I can use WIFI at home and the phone will synchronize there and away from home I can still read Gmail because it seems to store unread messages on the phone itself.

This is a great motivation to work on my own Android application.



Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! Mixx! Free and Open Source Software News Google! Live! Facebook! StumbleUpon! Yahoo! Free Joomla PHP extensions, software, information and tutorials.
 
More Articles...
  • «
  •  Start 
  •  Prev 
  •  1 
  •  2 
  •  Next 
  •  End 
  • »


Page 1 of 2