Fabrizio Giudici is a Senior Java Architect with a long Java experience in the industrial field. He runs Tidalwave, his own consultancy company, and has contributed to Java success stories in a number of fields, including Formula One. Fabrizio often appears as a speaker at international Java conferences such as JavaOne and Devoxx and is member of JUG Milano and the NetBeans Dream Team. Fabrizio is a DZone MVB and is not an employee of DZone and has posted 67 posts at DZone. You can read more from them at their website. View Full User Profile

Using org.openide.util.Lookup on Android

04.30.2010
| 11907 views |
  • submit to reddit

So, my solution for today is first to subclass Lookup:

package it.tidalwave.bluebill.mobile.android;

import android.content.Context;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;
import android.preference.PreferenceManager;
import it.tidalwave.bluebill.observation.ObservationManager;
import it.tidalwave.mobile.LazyLookup;
import it.tidalwave.mobile.FileSystem;
import it.tidalwave.mobile.LocationFinder;
import it.tidalwave.mobile.android.AndroidFileSystem;
import it.tidalwave.mobile.android.AndroidLocationFinder;
import it.tidalwave.bluebill.mobile.observation.ObservationClipboard;
import it.tidalwave.bluebill.mobile.observation.BlueBillObservationManager;
import it.tidalwave.bluebill.mobile.observation.DefaultObservationPreferences;
import it.tidalwave.bluebill.mobile.observation.ObservationPreferences;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonomyPreferences;
import it.tidalwave.bluebill.mobile.android.location.AndroidLocationPreferences;
import it.tidalwave.bluebill.mobile.location.LocationPreferences;
import it.tidalwave.bluebill.mobile.android.taxonomy.AndroidTaxonomyPreferences;


public class BlueBillLookup extends Lookup
{
private final LazyLookup lookup = new LazyLookup();

@CheckForNull
private Lookup extraLookup = Lookup.EMPTY;

public BlueBillLookup()
{
lookup.register(FileSystem.class,
AndroidFileSystem.class);
lookup.register(LocationFinder.class,
AndroidLocationFinder.class);
lookup.register(LocationPreferences.class,
AndroidLocationPreferences.class);
lookup.register(TaxonomyPreferences.class,
AndroidTaxonomyPreferences.class);
lookup.register(ObservationClipboard.class,
ObservationClipboard.class);
lookup.register(ObservationManager.class,
BlueBillObservationManager.class);
lookup.register(ObservationPreferences.class,
DefaultObservationPreferences.class);
}

@CheckForNull
public <T> T lookup (final @Nonnull Class<T> clazz)
{
final T r = extraLookup.lookup(clazz);
return (r != null) ? r : lookup.lookup(clazz);
}

@Override @Nonnull
public <T> Result<T> lookup (final @Nonnull Template<T> template)
{
final Result<T> r = extraLookup.lookup(template);
return (r != null) ? r : lookup.lookup(template);
}

public void setContext (final @Nonnull Context context)
{
extraLookup = Lookups.fixed(context,
context.getAssets(),
PreferenceManager.getDefaultSharedPreferences(context));
}
}

As you can see, the implementation is a proxy that searches for objects in two delegates. The former is initialized in the constructor and explicitly filled with all my service classes; the latter is created in a second time, after setContext() is called. This approach is required since apparently there's no static factory method for getting a Context, so it must be provided after the BlueBillLookup has been created. LazyLookup is a simple implementation that registers the class and postpones the actual instantiation of the required service as late as possible - this is consistent with the standard behaviour of Lookup implementations.

Published at DZone with permission of Fabrizio Giudici, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Jay Spring replied on Fri, 2010/04/30 - 5:07pm

I really want to learn Android. So I attempted to read this article. Though the sentences are so verbose I stopped reading after the second paragraph.

 

"Today I ran a fundamental round of refactorings trying to apply the best solution for the problem described by a specific question on the Android FAQ: "How do I pass data between Activities/Services within a single application?". For the most generic case (share any kind of Java object) there is no specific infrastructure in Android."

 

In English please?

Fabrizio Giudici replied on Fri, 2010/04/30 - 6:38pm

If it's verbose, it should suffice to drop the redundant parts :-) So:

"How do I pass data between Activities/Services within a single application?" There's no specific infrastructure in Android (for this purpose).

Then the rest of the article explains how you can do on your own.

Now, if you are asking what an Activity/Service is, you won't find an answer here, because I'm not writing Android tutorials for beginners - I think that there are a lot already available. 

Eugenios Ampelius replied on Wed, 2013/09/25 - 2:48pm

 Jay Spring, I also have the same problem. I "feel" the article is good, well written, but aside mass texting, I don't know how to do something on Android. Perhaps we should find some online courses and start from there. We are still too young for this website, apparently :).

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.