it.tidalwave.bluebill.mobile.android.taxonomy.AndroidTaxonomyPickerController 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: AndroidTaxonomyPickerController.java,v 8ca4217fa57c 2010/08/25 22:08:37 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonomyPickerUI;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonomyPickerController;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonomyPreferencesSupport;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import it.tidalwave.bluebill.mobile.android.R;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id: $
*
**********************************************************************************************************************/
public class AndroidTaxonomyPickerController extends TaxonomyPickerController
{
private static final String CLASS = AndroidTaxonomyPickerController.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
@Nonnull
private final SharedPreferences preferences = Locator.find(SharedPreferences.class);
private final TaxonomyAdapter taxonomyAdapter;
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private final OnItemClickListener onItemClickListener = new OnItemClickListener()
{
@Override
public void onItemClick (final @Nonnull AdapterView> adapterView,
final @Nonnull View view,
final @Nonnegative int position,
final long id)
{
final String taxonomyId = taxonomies.get(position).getId();
logger.info("Selected taxonomy id: %s", taxonomyId);
if (!taxonomyId.equals(preferences.getString(TaxonomyPreferencesSupport.PREF_TAXONOMY, "")))
{
final Editor editor = preferences.edit();
editor.putString(TaxonomyPreferencesSupport.PREF_TAXONOMY, taxonomyId);
editor.commit();
}
ui.finish();
}
};
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public AndroidTaxonomyPickerController (final @Nonnull Context context,
final @Nonnull TaxonomyPickerUI ui)
{
super(ui);
taxonomyAdapter = new TaxonomyAdapter(context, R.layout.taxonomy_list_row, taxonomies);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public OnItemClickListener getOnItemClickListener()
{
return onItemClickListener;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public TaxonomyAdapter getListAdapter()
{
return taxonomyAdapter;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnegative
public int getSelectedIndex()
{
final String selectedTaxonomyId = preferences.getString(TaxonomyPreferencesSupport.PREF_TAXONOMY, "");
for (int index = 0; index < taxonomies.size(); index++)
{
final String taxonomyId = taxonomies.get(index).getId();
if (taxonomyId.equals(selectedTaxonomyId))
{
return index;
}
}
return 0;
}
}