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 af5e94efb8ca 2010/06/26 18:14:20 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;

/***********************************************************************************************************************
 *
 * @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)
      {
        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 (DialogInterface dialog, 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 (positiveBehaviour != null)
          {
            builder.setNegativeButton(android.R.string.no, null);
          }

        builder.show();
      }

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

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Object createTimePickerDialog (final @Nonnull ActionListener listener)
      {
        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));
              }
          }, 0, 0, true);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Object createDatePickerDialog (final @Nonnull ActionListener listener)
      {
        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));
              }
          }, 0, 0, 0);
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy