
it.tidalwave.bluebill.mobile.android.taxonomy.TaxonIntentHelper Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* 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://kenai.com/hg/bluebill~android-src
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import it.tidalwave.util.Id;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.bluebill.taxonomy.mobile.Taxon;
import it.tidalwave.bluebill.taxonomy.mobile.Taxon.Rank;
import it.tidalwave.bluebill.mobile.preferences.TaxonomyPreferences;
import android.content.Context;
import android.content.Intent;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
public final class TaxonIntentHelper
{
public static final String TAXON_ID = "taxonId";
@CheckForNull
private static String actionPackage;
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private TaxonIntentHelper()
{
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Intent intentFor (final @Nonnull Rank rank)
{
return new Intent(getActionPackage() + rank.name());
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Intent intentFor (final @Nonnull Taxon taxon)
throws NotFoundException
{
// FIXME: you should really use the Uri, but it fails to find the Intent
// FIXME: probably it needs a further declaration in the Android manifest
// return new Intent(ACTION_TAXON, Uri.parse(taxon.getId().stringValue()));
final Intent intent = new Intent(getActionPackage() + taxon.getRank().getNarrower().name());
intent.putExtra(TAXON_ID, taxon.getId().stringValue());
return intent;
}
/*******************************************************************************************************************
*
* Creates an {@link Intent} for activating the given {@code Activity}, passing a {@link Taxon} as parameter.
*
* @param taxon the {@code Taxon}
* @param activityClass the {@code Activity}
*
******************************************************************************************************************/
@Nonnull
public static Intent intentFor (final @Nonnull Taxon taxon,
final @Nonnull Class> activityClass)
throws NotFoundException
{
// FIXME: you should really use the Uri, but it fails to find the Intent
// FIXME: probably it needs a further declaration in the Android manifest
// return new Intent(ACTION_TAXON, Uri.parse(taxon.getId().stringValue()));
final Intent intent = new Intent(Locator.find(Context.class), activityClass);
intent.putExtra(TAXON_ID, taxon.getId().stringValue());
return intent;
}
/*******************************************************************************************************************
*
* Retrieves the {@link Taxon} parameter from an {@link Intent}.
*
* @return the {@code Taxon}
* @throws NotFoundException if no {@code Taxon} has been passed as a parameter
*
******************************************************************************************************************/
@Nonnull
public static Taxon getTaxon (final @Nonnull Intent intent)
throws NotFoundException
{
NotFoundException.throwWhenNull(intent, "No Intent");
final String id = NotFoundException.throwWhenNull(intent.getExtras(), "No extras").getString(TAXON_ID);
NotFoundException.throwWhenNull(id, "No taxonId");
return Locator.find(TaxonomyPreferences.class).getTaxonomy().findTaxonById(new Id(id));
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
private static String getActionPackage()
{
if (actionPackage == null)
{
actionPackage = Locator.find(Context.class).getPackageName() + ".action.";
}
return actionPackage;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy