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

it.tidalwave.bluebill.mobile.android.taxonomy.factsheet.TaxonFactSheetAbstractActivity 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: TaxonFactSheetAbstractActivity.java,v 8bf7f1661fe7 2010/08/19 13:10:28 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy.factsheet;

import it.tidalwave.bluebill.factsheet.bbc.FactSheet;
import it.tidalwave.util.As;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.bluebill.taxonomy.Taxon;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import it.tidalwave.bluebill.mobile.android.R;
import it.tidalwave.bluebill.mobile.android.taxonomy.TaxonIntentHelper;
import it.tidalwave.bluebill.mobile.android.about.AboutActivity;
import it.tidalwave.bluebill.mobile.android.preferences.BlueBillPreferenceActivity;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.FactSheetProvider;
import it.tidalwave.mobile.android.ui.AndroidCommonUITasks;
import it.tidalwave.mobile.ui.CommonUITasks;
import it.tidalwave.util.logging.Logger;
import java.io.IOException;
import org.openide.util.NbBundle;

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

    @CheckForNull
    protected Taxon taxon;

    private final AndroidCommonUITasks androidUI = new AndroidCommonUITasks(this);

    protected ProgressDialog progressDialog;

    protected ListView list;

    protected T controller;

    private final int contentViewId;

    protected FactSheetProvider factSheetProvider;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public TaxonFactSheetAbstractActivity (final int contentViewId)
      {
        this.contentViewId = contentViewId;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public CommonUITasks getCommonUITasks()
      {
        return androidUI;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public void onCreate (final @Nonnull Bundle savedInstanceState)
      {
        super.onCreate(savedInstanceState);
        setContentView(contentViewId);

        try
          {
            taxon = TaxonIntentHelper.getTaxon(getIntent());
            factSheetProvider = new FactSheetProvider()
              {
                @Nonnull
                public FactSheet findFactSheetFor (final @Nonnull As as)
                  throws IOException
                  {
//                    try
//                      {
                        throw new IOException("BBC Fact sheet disabled");
//                        return new BbcFactSheetProvider().createFactSheet(taxon);
//                      }
//                    catch (NotFoundException e)
//                      {
//                        throw new IOException(e.toString()); // FIXME: return empty fact sheet
//                      }
                  }
              };
          }
        catch (NotFoundException e)
          {
          }

        final String preparingData = NbBundle.getMessage(TaxonFactSheetContainerActivity.class, "preparingData");
        progressDialog = ProgressDialog.show(this, "", preparingData, true);

        try
          {
            controller = createController();
            list = (ListView)findViewById(R.id.list);

            if (controller != null) // FIXME: temporary
              {
                if (list != null) // FIXME: temporary
                  {
                    list.setAdapter(controller.getListAdapter());
                    registerForContextMenu(list);
                  }

                controller.initialize();
              }
          }
        catch (Exception e)
          {
            logger.warning("While setting up %s", e);
            logger.throwing("onCreate()", CLASS, e);
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public boolean onCreateOptionsMenu (final @Nonnull Menu menu)
      {
        getMenuInflater().inflate(R.menu.common_options_menu, menu);
        return true;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public boolean onOptionsItemSelected (final @Nonnull MenuItem item)
      {
        switch (item.getItemId())
          {
            case R.id.preferences:
                startActivity(new Intent(getBaseContext(), BlueBillPreferenceActivity.class));
                return true;

            case R.id.about:
                startActivity(new Intent(getBaseContext(), AboutActivity.class));
                return true;

            default:
                return super.onOptionsItemSelected(item);
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void openWebPage (final @Nonnull String url) 
      {
        logger.info("openWebPage(%s)", url);
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void notify (final @Nonnull String message)
      {
        if (list != null) // FIXME: temporary until there are mock fact sheets
          {
            list.post(new Runnable()
              {
                public void run()
                  {
                    getCommonUITasks().showTemporaryMessage(message);
                  }
              });
          }
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void notifyDataLoaded()
      {
        runOnUiThread(new Runnable()
          {
            public void run()
              {
                if (list != null) // FIXME: temporary until there are mock fact sheets
                  {
                    list.setAdapter(controller.getListAdapter());
                  }

                progressDialog.dismiss();
              }
          });
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    protected abstract T createController();
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy