it.tidalwave.bluebill.mobile.android.taxonomy.browser.TaxonBrowserActivity Maven / Gradle / Ivy
The newest version!
/***********************************************************************************************************************
*
* blueBill Mobile - Android - open source birding
* Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* 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.
*
***********************************************************************************************************************
*
* WWW: http://bluebill.tidalwave.it/mobile
* SCM: https://java.net/hg/bluebill-mobile~android-src
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy.browser;
import it.tidalwave.role.Displayable;
import it.tidalwave.bluebill.mobile.android.util.CommonOptionsMenuController;
import it.tidalwave.bluebill.mobile.android.util.CommonOptionsMenuControllerProvider;
import it.tidalwave.bluebill.mobile.taxonomy.ui.TaxonomyFooterController;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.bluebill.taxonomy.mobile.Taxonomy;
import it.tidalwave.bluebill.taxonomy.mobile.Taxon.Rank;
import it.tidalwave.mobile.android.ui.AndroidUtilities;
import it.tidalwave.bluebill.mobile.taxonomy.text.TaxonCountFormat;
import it.tidalwave.bluebill.mobile.preferences.TaxonomyPreferences;
import it.tidalwave.bluebill.mobile.android.R;
import javax.inject.Provider;
import static org.openide.util.NbBundle.*;
/***********************************************************************************************************************
*
* TODO: should be refactored so it can dynamically handle all the levels in a single class, instead of requiring
* subclassing for each rank. For this, it needs control of the back button.
*
* FIXME: no good separation with controller stuff.
*
* @stereotype View
* @stereotype Activity
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
public abstract class TaxonBrowserActivity extends ListActivity
{
private static final String CLASS = TaxonBrowserActivity.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
@Nonnull
private final Rank rank;
private ListView list;
private TextView tvFooter;
private TextView tvTaxonomy;
private TextView tvTaxonLanguages;
@CheckForNull /** Lazily created and eventually later replaced. */
private TaxonomyBrowserController controller;
private final TaxonomyFooterController taxonomyFooterController = new TaxonomyFooterController();
private final CommonOptionsMenuController commonOptionsMenuController = Locator.find(CommonOptionsMenuControllerProvider.class).createCommonOptionsMenuController(this);
private final TaxonCountFormat taxonCountFormat;
@Nonnull
private final Provider taxonomyPreferences = Locator.createProviderFor(TaxonomyPreferences.class);
@CheckForNull /** Only used to track changes in the current Taxonomy. */
private String currentTaxonomyName; // FIXME: replace with the id
/*******************************************************************************************************************
*
* The support for an AsyncTask that runs something in background while blocking the UI.
*
******************************************************************************************************************/
abstract class PleaseWaitTaskSupport extends AsyncTask
{
protected ProgressDialog dialog;
@Override
protected void onPreExecute()
{
final String message = getMessage(TaxonomyBrowserController.class, "pleaseWait");
dialog = ProgressDialog.show(TaxonBrowserActivity.this, "", message, true);
}
protected void complete()
{
updateHeaderAndFooter();
dialog.dismiss();
}
}
/*******************************************************************************************************************
*
* A background task that loads the current Taxonomy in the UI and then binds the controller.
*
******************************************************************************************************************/
class LoadTaxonomyTask extends PleaseWaitTaskSupport
{
@Override
protected Void doInBackground (Void ... params)
{
logger.info("loading taxonomy");
disposeController();
controller = new TaxonomyBrowserController(TaxonBrowserActivity.this, rank, getIntent());
return null;
}
@Override
protected void onPostExecute (Void result)
{
final BaseAdapter taxonBrowserAdapter = (BaseAdapter)controller.getTaxonBrowserAdapter();
// re-register listeners
list.setAdapter(taxonBrowserAdapter);
list.setOnItemClickListener(controller.getTaxonBrowserListener());
taxonBrowserAdapter.registerDataSetObserver(new DataSetObserver()
{
@Override
public void onChanged()
{
updateHeaderAndFooter();
}
});
complete();
}
}
/*******************************************************************************************************************
*
* A task that ensures that taxa are sorted in the UI.
*
******************************************************************************************************************/
class EnsureTaxaSortedTask extends PleaseWaitTaskSupport
{
@Override
protected Void doInBackground (Void ... params)
{
controller.ensureTaxaAreSorted();
return null;
}
@Override
protected void onPostExecute (Void result)
{
// TODO: if you can avoid re-creating the controller, this section can be moved to onCreate()
((BaseAdapter)controller.getTaxonBrowserAdapter()).notifyDataSetChanged();
complete();
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public TaxonBrowserActivity (final @Nonnull Rank rank)
{
this.rank = rank;
this.taxonCountFormat = new TaxonCountFormat(rank);
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override
protected void onCreate (final @Nonnull Bundle savedInstanceState)
{
logger.info("onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_taxon_browser);
list = (ListView)findViewById(android.R.id.list);
tvFooter = (TextView)findViewById(R.id.tvFooter);
tvTaxonomy = (TextView)findViewById(R.id.tvTaxonomy);
tvTaxonLanguages = (TextView)findViewById(R.id.tvPrimaryLanguage);
((ImageButton)findViewById(R.id.btFilter)).setOnClickListener(AndroidUtilities.createShowSoftKeyboardOnClickListener(list));
}
/*******************************************************************************************************************
*
* {@inheritDoc}
* Eventually loads the data - remember that this is called both after the Activity creation and after a real
* resume. While the activity was suspended, the user might have changed the Taxonomy or the sort order. which would
* need some care be taken of.
*
******************************************************************************************************************/
@Override
protected void onResume()
{
logger.info("onResume()");
super.onResume();
final Taxonomy taxonomy = taxonomyPreferences.get().getTaxonomy();
final String newTaxonomyName = taxonomy.as(Displayable.class).getDisplayName();
logger.info(">>>> current taxonomy: %s new taxonomy: %s", currentTaxonomyName, newTaxonomyName);
if ((currentTaxonomyName == null) || !currentTaxonomyName.equals(newTaxonomyName))
{
currentTaxonomyName = newTaxonomyName;
new LoadTaxonomyTask().execute();
}
if ((controller != null) && controller.needsResorting())
{
new EnsureTaxaSortedTask().execute();
}
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override
protected void onDestroy()
{
logger.info("onDestroy()");
disposeController();
super.onDestroy();
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override
public boolean onCreateOptionsMenu (final @Nonnull Menu menu)
{
getMenuInflater().inflate(R.menu.common_options_menu, menu);
return true;
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override
public boolean onOptionsItemSelected (final @Nonnull MenuItem item)
{
return commonOptionsMenuController.onOptionsItemSelected(item);
}
/*******************************************************************************************************************
*
* {@inheritDoc}
* Propagates the result back.
*
******************************************************************************************************************/
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
logger.info("onActivityResult(%d, %d, %s)", requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
setResult(resultCode, data);
finish();
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private void updateHeaderAndFooter()
{
tvFooter.setText(taxonCountFormat.format(list.getAdapter().getCount()));
tvTaxonomy.setText(taxonomyFooterController.getTaxonomyDisplayName());
tvTaxonLanguages.setText(taxonomyFooterController.getTaxonLanguages());
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private void disposeController()
{
if (controller != null)
{
controller.dispose();
}
}
}