Kazed

Pragmatic software development

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

Android presentation

E-mail Print PDF

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.

 

Last Updated on Wednesday, 15 April 2009 18:52
 

Logitech Presenter on Ubuntu Linux

E-mail Print PDF

Logitech PresenterI will soon do a presentation for a somewhat larger audience and I need a remote control for my slides. Since I use OpenOffice on Ubuntu Linux, I was not sure which devices would be compatible. I read some positive comments on some forums about the Logitech Presenter, so I ordered it.

A few days ago I received the presenter and I was pleasantly surprised; it works perfectly with OpenOffice on Ubuntu Linux. Just plugin the USB receiver and everything works great, even the volume controls work seamlessly. All that without any special configuration or drivers. Also the quality of the Logitech Presenter looks and feels great (did I mention that I have Logitech stocks?)
Last Updated on Saturday, 21 March 2009 10:49
 

Reflection performance with injection

E-mail Print PDF

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.

Last Updated on Wednesday, 14 January 2009 10:02
 

Simple setter injection with annotations

E-mail Print PDF

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

Last Updated on Tuesday, 13 January 2009 14:47
 

Android phone arrived

E-mail Print PDF

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.

Last Updated on Tuesday, 13 January 2009 14:37
 

Android development phone

E-mail Print PDF

Tags: android


You may have seen this in several blogs: you can buy an unlocked Android development phone at the Google Android market place.

If you are like me, in a country where the T-mobile G1 is not for sale yet, and would like to test your Android application on a real device, you can now have one. The development phone can be shipped to: the United States, Germany, Japan, India, France, United Kingdom, Taiwan, Spain, Australia, Poland, Switzerland, Austria, Hungary, Netherlands, Sweden, Finland, Singapore and Canada.

Keep in mind that the price of $399 does not include shipping and custom duties. These shipping/customs/import charges are:

Country Charge
Canada$ 264.49
UK $ 171.53
Hungary $ 199.99
Austria $ 189.99
Germany $ 178.90
France $ 183.81
Spain $ 170.14
Poland $ 210.09
Switzerland $ 130.43
Netherlands $ 172.99
Sweden $ 214.81
Finland $ 199.92
India $ 224.60
Japan $ 109.55
Taiwan $ 156.66
Australia $ 140.23
Singapore $ 119.36

I just ordered mine and should arrive here in the Netherlands in 5 to 10 days. Finally I can test my NextAction Android application.

 

Last Updated on Wednesday, 10 December 2008 11:48
 
  • «
  •  Start 
  •  Prev 
  •  1 
  •  2 
  •  Next 
  •  End 
  • »


Page 1 of 2

Polls

How long have you been using Java?
 

Login Form