All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.bluebill.mobile.android.taxonomy.TaxonIntentHelper 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: TaxonIntentHelper.java,v 035efa0e40a2 2010/08/19 21:44:20 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.openide.util.Lookup;
import it.tidalwave.util.Id;
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.mobile.taxonomy.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 Taxonomy.Type type)
      {
        return new Intent(getActionPackage() + type.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.getType().getNarrower().name());
        intent.putExtra(TAXON_ID, taxon.getId().stringValue());
        return intent;
      }

    /*******************************************************************************************************************
     *
     * FIXME: probably useless now?
     *
     ******************************************************************************************************************/
    @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;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @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 Lookup.getInstance().getTaxonomy().findTaxonById(new Id(intent.getData().toString()));
        return Lookup.getDefault().lookup(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 - 2024 Weber Informatics LLC | Privacy Policy