it.tidalwave.bluebill.mobile.android.taxonomy.browser.TaxonomyBrowserController Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* blueBill Mobile - open source birdwatching
* ==========================================
*
* Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
* http://bluebill.tidalwave.it/mobile/
*
***********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
***********************************************************************************************************************
*
* $Id: TaxonomyBrowserController.java,v a0ce401c4c48 2010/08/26 11:23:42 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy.browser;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.bluebill.taxonomy.Taxon;
import it.tidalwave.bluebill.taxonomy.Taxonomy;
import it.tidalwave.bluebill.taxonomy.Taxonomy.Type;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonomyPreferences;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import it.tidalwave.mobile.android.ui.ControlFlow;
import it.tidalwave.bluebill.mobile.android.taxonomy.TaxonIntentHelper;
import it.tidalwave.bluebill.mobile.android.taxonomy.TaxonAdapter;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id: $
*
**********************************************************************************************************************/
public class TaxonomyBrowserController
{
private static final String CLASS = TaxonomyBrowserController.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
public final static int PICK = 1;
@Nonnull
private final Context context;
@Nonnull
private final SharedPreferences sharedPreferences;
@Nonnull
private final TaxonomyPreferences preferences;
@Nonnull
private final Type type;
private final List taxa = new ArrayList();
private Locale latestSortLocale;
@Nonnull
private final TaxonAdapter adapter;
private ControlFlow controlFlow;
private boolean useCache;
private static String cachedTaxonomyName = "";
private static List cachedSortedTaxa;
private static Locale cachedSortLocale;
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private final OnItemClickListener taxonBrowserListener = new OnItemClickListener()
{
@Override
public void onItemClick (final @Nonnull AdapterView> a,
final @Nonnull View view,
final @Nonnegative int position,
final @Nonnegative long id)
{
final Taxon taxon = (Taxon)adapter.getItem(position);
try
{
final Intent intent = TaxonIntentHelper.intentFor(taxon);
if (type.equals(Type.SPECIES)) // final one
{
controlFlow.toNextStep(intent);
}
else
{
controlFlow.toNextStep(intent, ControlFlow.USE_INTENT_FILTER);
}
}
catch (NotFoundException e)
{
throw new RuntimeException(e);
}
}
};
/*******************************************************************************************************************
*
* Recomputes the data whenever a preference have been changed. This is important because changing taxonomy or
* locale needs to re-sort data.
*
******************************************************************************************************************/
private final OnSharedPreferenceChangeListener preferenceChangeListener = new OnSharedPreferenceChangeListener()
{
public void onSharedPreferenceChanged (final @Nonnull SharedPreferences sp, final @Nonnull String string)
{
latestSortLocale = null; // mark "dirty"
clearCache();
}
};
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public TaxonomyBrowserController (final @Nonnull Activity activity,
final @Nonnull TaxonomyPreferences preferences,
final @Nonnull Type type,
final @Nonnull Taxonomy taxonomy,
final @Nonnull Intent intent)
{
this.context = activity.getBaseContext();
this.preferences = preferences;
this.type = type;
computeTaxa(taxonomy, intent);
adapter = new TaxonAdapter(context, preferences, this.taxa);
sharedPreferences = Locator.find(SharedPreferences.class);
sharedPreferences.registerOnSharedPreferenceChangeListener(preferenceChangeListener);
controlFlow = ControlFlow.forActivity(activity);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private static void clearCache()
{
logger.info("clearCache()");
cachedSortedTaxa = null;
cachedSortLocale = null;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public void dispose()
{
sharedPreferences.unregisterOnSharedPreferenceChangeListener(preferenceChangeListener);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public ListAdapter getTaxonBrowserAdapter()
{
return adapter;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public OnItemClickListener getTaxonBrowserListener()
{
return taxonBrowserListener;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public boolean needsResorting()
{
return (latestSortLocale == null) || !latestSortLocale.equals(preferences.getTaxonomyLocales().get(0));
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public void ensureTaxaAreSorted()
{
final String taxonomyName = preferences.getTaxonomy().getDisplayName();
if (needsResorting())
{
latestSortLocale = preferences.getTaxonomyLocales().get(0);
if (useCache
&& (cachedSortedTaxa != null)
&& taxonomyName.equals(cachedTaxonomyName)
&& latestSortLocale.equals(cachedSortLocale))
{
logger.info("not sorting taxa, using cached value");
taxa.clear();
taxa.addAll(cachedSortedTaxa);
}
else
{
logger.info("starting sort %d taxa...", taxa.size());
final long time = System.currentTimeMillis();
Collections.sort(taxa, preferences.getTaxonComparator());
logger.info(">>>> %d taxa sorted in %d msec", taxa.size(), System.currentTimeMillis() - time);
if (useCache)
{
cachedSortedTaxa = new ArrayList(taxa);
cachedTaxonomyName = taxonomyName;
cachedSortLocale = latestSortLocale;
}
}
}
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
@Nonnull
private void computeTaxa (final @Nonnull Taxonomy taxonomy, final @Nonnull Intent intent)
{
taxa.clear();
useCache = false;
try
{
taxa.addAll(TaxonIntentHelper.getTaxon(intent).getChildren());
}
//
// Can also happen when you've changed taxonomy and the selected taxon is not available in the new taxonomy.
// If taxa are all the species, cache their sorted list, as it might take a long time to be sorted.
// TODO: This management of cache is cumbersome. Using Finders with sorting capabilities could be a solution,
// moving all the caching logics to the Finder themselves (and eventually could opaquely implement sorting
// based on OpenSesame query that could be fast enough to avoid caching).
//
catch (NotFoundException e)
{
taxa.addAll(taxonomy.findTaxaByType(type));
useCache = type == Taxonomy.Type.SPECIES;
}
ensureTaxaAreSorted();
}
}