it.tidalwave.mobile.android.util.ProgressDialogController Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* blueBill Mobile - Android - open source birding
* Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* 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.
*
***********************************************************************************************************************
*
* WWW: http://bluebill.tidalwave.it/mobile
* SCM: https://java.net/hg/bluebill-mobile~android-src
*
**********************************************************************************************************************/
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;
/***********************************************************************************************************************
*
* @stereotype Controller
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
public class ProgressDialogController implements ProgressListener
{
private static final String PROGRESS_NAME = "progressName";
private static final String STEP_COUNT = "stepCount";
private static final String STEP = "step";
private static final String DISMISS = "dismiss";
private static final String KEY = "key";
private static final 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 - 2025 Weber Informatics LLC | Privacy Policy