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

it.tidalwave.bluebill.mobile.android.observation.ObservationActivity 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: ObservationActivity.java,v 3dac38e27f5f 2010/08/06 23:24:24 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.observation;

import javax.annotation.Nonnull;
import javax.inject.Inject;
import android.app.Dialog;
import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.widget.ImageButton;
import android.widget.TextView;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.mobile.ui.CommonUITasks;
import it.tidalwave.mobile.android.annotation.AndroidWidget;
import it.tidalwave.mobile.android.ui.AndroidCommonUITasks;
import it.tidalwave.mobile.android.ui.ControlFlow;
import it.tidalwave.bluebill.mobile.observation.ObservationUI;
import it.tidalwave.bluebill.mobile.android.R;
import it.tidalwave.bluebill.mobile.android.util.CommonOptionsMenuController;

/***********************************************************************************************************************
 *
 * The main activity for observations. It displays the current observation database and add / edit / etc operations can
 * be performed from here.
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public class ObservationActivity extends ExpandableListActivity implements ObservationUI
  {
    private static final String CLASS = ObservationActivity.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    private final AndroidObservationUIController controller;

    private final CommonUITasks commonUITasks = new AndroidCommonUITasks(this);

    @Inject @AndroidWidget(R.id.tvFooter)
    private TextView tvFooter;

    @Inject @AndroidWidget(R.id.empty)
    private View viEmpty;

    private final CommonOptionsMenuController commonOptionsMenuController = new CommonOptionsMenuController(this);

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public ObservationActivity()
      {
        controller = new AndroidObservationUIController(this);
      }

    /*******************************************************************************************************************
     *
     * For tests.
     *
     ******************************************************************************************************************/
    protected ObservationActivity (final @Nonnull AndroidObservationUIController controller)
      {
        this.controller = controller;
      }

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

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void setFooterText (final @Nonnull String string)
      {
        runOnUiThread(new Runnable()
          {
            public void run()
              {
                viEmpty.setVisibility(controller.getListAdapter().getGroupCount() == 0 ? View.VISIBLE : View.INVISIBLE);
                tvFooter.setText(string);
              }
          });
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public void onCreate (final @Nonnull Bundle savedInstanceState)
      {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.observations_activity);
        
        setListAdapter(controller.getListAdapter());
        registerForContextMenu(getExpandableListView());
        viEmpty = findViewById(R.id.empty);
        tvFooter = (TextView)findViewById(R.id.tvFooter);

        ((ImageButton)findViewById(R.id.btFactSheet)).setOnClickListener(new OnClickListener()
          {
            public void onClick (final @Nonnull View view)
              {
                controller.browseToFactSheet();
              }
          });

        ((ImageButton)findViewById(R.id.btAdd)).setOnClickListener(new OnClickListener()
          {
            public void onClick (final @Nonnull View view)
              {
                controller.startNewObservationSequence();
              }
          });
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    protected void onResume()
      {
        controller.fireRefresh(); // e.g. we changed rendering options - FIXME: is this really needed?
        super.onResume();
      }

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

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public void onCreateContextMenu (final @Nonnull ContextMenu menu,
                                     final @Nonnull View view,
                                     final @Nonnull ContextMenuInfo menuInfo)
      {
        final ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo)menuInfo;
        final int type = ExpandableListView.getPackedPositionType(info.packedPosition);
        int menuId = 0;

        switch (type)
          {
            case ExpandableListView.PACKED_POSITION_TYPE_GROUP:
                menuId = R.menu.observations_context_menu;
                break;

            case ExpandableListView.PACKED_POSITION_TYPE_CHILD:
                menuId = R.menu.observation_items_context_menu;
                break;
                
            default:
                return;
          }
        
        getMenuInflater().inflate(menuId, menu);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public boolean onContextItemSelected (final @Nonnull MenuItem item)
      {
        return controller.onContextItemSelected(item);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    protected Dialog onCreateDialog (final int id)
      {
        return controller.onCreateDialog(id);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    protected void onPrepareDialog (final int id, final @Nonnull Dialog dialog)
      {
        controller.onPrepareDialog(id, dialog);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    protected void onActivityResult (final int requestCode,
                                     final int resultCode,
                                     final @Nonnull Intent data)
      {
        logger.info("onActivityResult(%d, %d, %s)", requestCode, resultCode, data);

        if (requestCode == ControlFlow.findRequestCode(AddObservationControlFlow.class)) 
          {
            switch (resultCode)
              {
                case RESULT_OK:
                     // TODO: would be nice to have it called by the ControlFlow.
                    controller.commitNewObservation();
                    break;

                case RESULT_CANCELED:
                     // TODO: would be nice to have it called by the ControlFlow.
                    controller.cancelNewObservation();
                    break;
              }
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public boolean onOptionsItemSelected (final @Nonnull MenuItem item)
      {
        switch (item.getItemId())
          {
            case R.id.clearObservations:
                controller.deleteAllObservations();
                return true;

            case R.id.share:
                showDialog(R.id.share);
                return true;

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

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void highlightLatestObservation()
      {
        runOnUiThread(new Runnable()
          {
            public void run()
              {
                final ExpandableListView expandableListView = getExpandableListView();
                final int groupCount = expandableListView.getExpandableListAdapter().getGroupCount();

                if (groupCount > 0)
                  {
                    for (int i = 0; i < groupCount - 1; i++)
                      {
                        expandableListView.collapseGroup(i);
                      }

                    logger.info(">>>> expanding group %s...", groupCount - 1);
                    expandableListView.expandGroup(groupCount - 1);
                  }
              }
          });
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy