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

it.tidalwave.bluebill.mobile.android.taxonomy.factsheet.sound.TaxonSoundFactSheetAndroidController 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: TaxonSoundFactSheetAndroidController.java,v e02197e79482 2010/07/23 01:36:26 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy.factsheet.sound;

import javax.annotation.Nonnull;
import java.util.zip.GZIPInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.res.AssetManager;
import android.widget.BaseAdapter;
import it.tidalwave.bluebill.mobile.android.taxonomy.factsheet.NodesAdapter;
import it.tidalwave.bluebill.mobile.android.taxonomy.factsheet.TaxonFactSheetAndroidController;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.bluebill.taxonomy.Taxon;
import it.tidalwave.bluebill.mobile.android.util.Analytics;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.FactSheetProvider;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.SoundObservationNode;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.sound.TaxonSoundFactSheetController;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.sound.TaxonSoundFactSheetUI;
import it.tidalwave.bluebill.observation.Observation;
import it.tidalwave.bluebill.observation.ObservationSet;
import it.tidalwave.semantic.io.MimeTypes;
import javax.annotation.CheckForNull;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public class TaxonSoundFactSheetAndroidController extends TaxonSoundFactSheetController implements TaxonFactSheetAndroidController
  {
    private static final String CLASS = TaxonSoundFactSheetAndroidController.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    @CheckForNull
    private NodesAdapter nodesAdapter; 

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public TaxonSoundFactSheetAndroidController (final @Nonnull TaxonSoundFactSheetUI ui, 
                                                 final @Nonnull Taxon taxon,
                                                 final @Nonnull FactSheetProvider factSheetProvider)
      {
        super(ui, taxon, factSheetProvider);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void initialize()
      {
        new Thread()
          {
            @Override
            public void run()
              {
                try // FIXME: pull up
                  {
                    for (final Observation observation : loadObservationSet().find(Observation.class).results())
                      {
                        add(new SoundObservationNode(observation, TaxonSoundFactSheetAndroidController.this));
                      }

                    nodesAdapter = new NodesAdapter((Activity)ui, nodes);
                  }
                catch (IOException e)
                  {
                    // TODO: stop waiting
                    e.printStackTrace();
                  }
                
                ui.notifyDataLoaded();
              }
          }.start();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public BaseAdapter getListAdapter()
      {
        return nodesAdapter;
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    protected ObservationSet loadObservationSet()
      throws IOException
      {
        logger.info("loadObservationSet()");
        final String resourceName = "xenocanto-" + taxon.getId().stringValue().replaceAll(":", "_") + ".n3";
        logger.info(">>>> resourceName %s", resourceName);
        InputStream is = openResourceStream(resourceName);

        if (resourceName.endsWith(".gz"))
          {
            is = new GZIPInputStream(is);
          }

        final ObservationSet observationSet = super.loadObservationSet(is, MimeTypes.MIME_N3);
        is.close();
        return observationSet;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    protected InputStream openResourceStream (final @Nonnull String resourceName)
      throws IOException
      {
        return Locator.find(AssetManager.class).open(resourceName);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    protected void notifyStatusChanged()
      {
        ((Activity)ui).runOnUiThread(new Runnable()
          {
            public void run()
              {
                getListAdapter().notifyDataSetChanged();
              }
          });
      }

    private volatile boolean pinged;

    /*******************************************************************************************************************
     *
     * Pings the server and provides some anonymous information that will be use for statistics purpose. This should
     * be moved in the function to read news, when it will be implemented.
     *
     ******************************************************************************************************************/
    @Override
    protected void ping()
      {
        if (pinged)
          {
            return;
          }

        new Thread()
          {
            @Override
            public void run()
              {
                try
                  {
                    Analytics.createURL("http://bluebill.tidalwave.it/mobile/ping").openStream().close();
                  }
                catch (FileNotFoundException e)
                  {
                    pinged = true;
//                    e.printStackTrace();
                  }
                catch (IOException e)
                  {
//                    e.printStackTrace();
                  }
              }
          }.start();
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy