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

it.tidalwave.mobile.android.ui.AndroidCommonUITasks 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: AndroidCommonUITasks.java,v f0876bdfda44 2010/08/12 11:42:04 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.mobile.android.ui;

import android.app.AlertDialog.Builder;
import java.util.Date;
import javax.annotation.Nonnull;
import android.content.DialogInterface;
import android.widget.DatePicker;
import android.widget.Toast;
import android.widget.TimePicker;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.text.Html;
import it.tidalwave.mobile.ui.ActionEvent;
import it.tidalwave.mobile.ui.ActionListener;
import it.tidalwave.mobile.ui.CommonUITasks;
import it.tidalwave.mobile.util.DateUpdater;
import java.util.Calendar;
import javax.annotation.CheckForNull;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public class AndroidCommonUITasks implements CommonUITasks
  {
    @Nonnull
    private final Activity activity;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public AndroidCommonUITasks (final @Nonnull Activity activity)
      {
        this.activity = activity;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void showTemporaryMessage (final @Nonnull String message)
      {
        Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void alertDialog (final @Nonnull String title,
                             final @Nonnull String message,
                             final @Nonnull Runnable positiveBehaviour)
      {
        activity.runOnUiThread(new Runnable()
          {
            public void run()
              {
                doAlertDialog(title, message, positiveBehaviour, null, true);
              }
          });
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void alertDialog (final @Nonnull String title,
                             final @Nonnull String message,
                             final @Nonnull Runnable positiveBehaviour,
                             final @Nonnull Runnable negativeBehaviour)
      {
        activity.runOnUiThread(new Runnable()
          {
            public void run()
              {
                doAlertDialog(title, message, positiveBehaviour, negativeBehaviour, true);
              }
          });
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void alertDialog2 (final @Nonnull String title,
                              final @Nonnull String message,
                              final @Nonnull Runnable positiveBehaviour)
      {
        activity.runOnUiThread(new Runnable()
          {
            public void run()
              {
                doAlertDialog(title, message, positiveBehaviour, null, false);
              }
          });
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void doAlertDialog (final @Nonnull String title,
                                final @Nonnull String message,
                                final @CheckForNull Runnable positiveBehaviour,
                                final @CheckForNull Runnable negativeBehaviour,
                                final boolean canShowCancel)
      {
        Builder builder = new AlertDialog.Builder(activity);
        builder.setIcon(android.R.drawable.ic_dialog_alert)
               .setTitle(title)
               .setMessage(Html.fromHtml(message))
               .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()
                  {
                    @Override
                    public void onClick (final @Nonnull DialogInterface dialog, final int which)
                      {
                        if (positiveBehaviour != null)
                          {
                            positiveBehaviour.run();
                          }
                      }
                  });
        // The positive button is always set as a dismiss button; the negative only if the positive behaviours exists
        if (canShowCancel && (positiveBehaviour != null) || (negativeBehaviour != null))
          {
            builder.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener()
              {
                @Override
                public void onClick (final @Nonnull DialogInterface dialog, final int which)
                  {
                    if (negativeBehaviour != null)
                      {
                        negativeBehaviour.run();
                      }
                  }
              });
          }

        builder.show();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void showDialog (final int id)
      {
        activity.showDialog(id);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Object createTimePickerDialog (final @Nonnull ActionListener listener)
      {
        final Calendar calendar = Calendar.getInstance();
        return new TimePickerDialog(activity, new OnTimeSetListener() 
          {
            public void onTimeSet (final @Nonnull TimePicker timePicker, final int hour, final int minute) 
              {
                final DateUpdater timeUpdater = new DateUpdater()
                  {
                    @Nonnull
                    public Date update (final @Nonnull Date date)
                      {
                        return AndroidUtilities.getDate(timePicker, date);
                      }
                  };

                listener.actionPerformed(new ActionEvent(timePicker, timeUpdater));
              }
          }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Object createDatePickerDialog (final @Nonnull ActionListener listener)
      {
        final Calendar calendar = Calendar.getInstance();
        return new DatePickerDialog(activity, new OnDateSetListener()
          {
            public void onDateSet (final @Nonnull DatePicker datePicker, final int year, final int month, final int day)
              {
                final DateUpdater dateUpdater = new DateUpdater()
                  {
                    @Nonnull
                    public Date update (final @Nonnull Date date) 
                      {
                        return AndroidUtilities.getDate(datePicker, date);
                      }
                  };               

                listener.actionPerformed(new ActionEvent(datePicker, dateUpdater));
              }
          }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy