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

it.tidalwave.mobile.android.util.ProgressDialogController Maven / Gradle / Ivy

/***********************************************************************************************************************
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 **********************************************************************************************************************/
package it.tidalwave.mobile.android.util;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import it.tidalwave.mobile.util.ProgressListener;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public class ProgressDialogController implements ProgressListener
  {
    private final static String PROGRESS_NAME = "progressName";
    private final static String STEP_COUNT = "stepCount";
    private final static String STEP = "step";
    private final static String DISMISS = "dismiss";
    private final static String KEY = "key";
    private final static String VALUE = "value";

    @Nonnull
    private final Activity activity;

    @CheckForNull
    private ProgressDialog progressDialog;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private final Handler handler = new Handler()
      {
        @Override
        public void handleMessage (final @Nonnull Message message)
          {
            final Bundle data = message.getData();
            final String key = data.getString(KEY);

            if (PROGRESS_NAME.equals(key))
              {
                progressDialog.setMessage(data.getString(VALUE));
              }
            else if (STEP_COUNT.equals(key))
              {
                progressDialog.setMax(data.getInt(VALUE));
              }
            else if (STEP.equals(key))
              {
                progressDialog.setProgress(data.getInt(VALUE));
              }
            else if (DISMISS.equals(key))
              {
                progressDialog.dismiss();
              }
          }
      };

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

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public synchronized  ProgressDialog getProgressDialog()
      {
        if (progressDialog == null)
          {
            progressDialog = new ProgressDialog(activity);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
          }

        return progressDialog;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void setProgressName (final @Nonnull String progressName)
      {
        sendMessage(PROGRESS_NAME, progressName);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void setStepCount (final @Nonnegative int stepCount)
      {
        sendMessage(STEP_COUNT, stepCount);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void setStep (final @Nonnegative int step)
      {
        sendMessage(STEP, step);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void start()
      {
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void stop()
      {
        sendMessage(DISMISS, "");
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void sendMessage (final String key, final Object value)
      {
        final Message message = handler.obtainMessage();
        final Bundle bundle = new Bundle();
        bundle.putString(KEY, key);

        if (value instanceof Integer)
          {
            bundle.putInt(VALUE, (Integer)value);
          }
        else if (value instanceof String)
          {
            bundle.putString(VALUE, (String)value);
          }

        message.setData(bundle);
        handler.sendMessage(message);
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy