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 e478eae6271a 2010/06/27 01:07:27 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy.factsheet.sound;

import java.io.UnsupportedEncodingException;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import java.util.zip.GZIPInputStream;
import java.io.InputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Build;
import it.tidalwave.bluebill.mobile.android.about.AboutActivity;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.bluebill.taxonomy.Taxon;
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.bluebill.observation.simple.SimpleObservationSet;
import java.io.FileNotFoundException;
import java.net.URL;
import java.net.URLEncoder;
import org.openide.util.NbBundle;

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

    @Nonnull
    private final TaxonSoundFactSheetUI ui;

    private SoundAdapter soundsAdapter;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public TaxonSoundFactSheetAndroidController (final @Nonnull TaxonSoundFactSheetUI ui, final @Nonnull Taxon taxon)
      throws IOException
      {
        super(ui, taxon);
        this.ui = ui;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void initialize()
      {
        soundsAdapter = new SoundAdapter((Activity)ui, new SimpleObservationSet()); // FIXME: empty
        
        new Thread()
          {
            @Override
            public void run()
              {
                try
                  {
                    soundsAdapter = new SoundAdapter((Activity)ui, loadObservationSet());
                  }
                catch (IOException e)
                  {
                    // TODO: stop waiting
                    e.printStackTrace();
                  }
                
                ui.notifyDataLoaded();
              }
          }.start();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public SoundAdapter getSoundsAdapter()
      {
        return soundsAdapter;
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    protected ObservationSet loadObservationSet()
      throws IOException
      {
        logger.info("loadObservationSet()");
        final String resourceName = "xenocanto-" + taxon.getId().stringValue().replaceAll(".*#", "") + ".json";
        logger.info(">>>> resourceName %s", resourceName);
        InputStream is = openResourceStream(resourceName);

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

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

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    protected Observation getObservation (final @Nonnegative int index)
      {
        return soundsAdapter.getItem(index);
      }

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

    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
                  {
                    final StringBuilder buffer = new StringBuilder("http://bluebill.tidalwave.it/mobile/ping?");
                    buffer.append("blueBillVersion=").append(encode(NbBundle.getMessage(AboutActivity.class, "title")));
                    buffer.append("&osVersion=").append(encode(System.getProperty("os.version")));
                    buffer.append("&javaVersion=").append(encode(System.getProperty("java.version")));
                    buffer.append("&userLanguage=").append(encode(System.getProperty("user.language")));
                    buffer.append("&userRegion=").append(encode(System.getProperty("user.region")));
                    buffer.append("&board=").append(encode(Build.BOARD));
                    buffer.append("&brand=").append(encode(Build.BRAND));
                    buffer.append("&device=").append(encode(Build.DEVICE));
                    buffer.append("&model=").append(encode(Build.MODEL));
                    buffer.append("&product=").append(encode(Build.PRODUCT));
                    buffer.append("&versionIncremental=").append(encode(Build.VERSION.INCREMENTAL));
                    buffer.append("&versionRelease=").append(encode(Build.VERSION.RELEASE));
                    buffer.append("&versionSDK=").append(encode(Build.VERSION.SDK));
                    new URL(buffer.toString()).openStream().close();
                  }
                catch (FileNotFoundException e)
                  {
                    pinged = true;
//                    e.printStackTrace();
                  }
                catch (IOException e)
                  {
//                    e.printStackTrace();
                  }
              }
          }.start();
      }

    @Nonnull
    private static String encode (final @Nonnull String string)
      throws UnsupportedEncodingException
      {
        return (string == null) ? "" : URLEncoder.encode(string, "UTF-8");
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy