Android Open Source - AndroIUT Async Service Call






From Project

Back to project page AndroIUT.

License

The source code is released under:

GNU General Public License

If you think the Android project AndroIUT listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * AsyncServiceCall.java/*w  w  w . j  a  v a 2  s .co m*/
 * 23/09/2011
 * TwAPIme for Android
 * Copyright(c) Ernandes Mourao Junior (ernandes@gmail.com)
 * All rights reserved
 * GNU General Public License (GPL) Version 2, June 1991
 */
package com.iutdijon.androiut2.util.loaders;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;

import com.iutdijon.androiut2.util.IOUtils;
import com.iutdijon.androiut2.util.UIUtil;

/**
 * Classe permettant de d?charger tout traitement dans un processus s?parer
 * Elle permet aussi de g?rer tout l'affichage de dialogue de progression
 * et la possibilit? de recommencer une op?ration en cas de probl?me
 * @author Morgan Funtowicz
 *
 * @param <P> Le type des param?tres en entr?e
 * @param <G> Le type des param?tres de progression
 * @param <R> Le type des param?tres de sortie
 */
public abstract class AsyncServiceCall<P, G, R> extends AsyncTask<P, G, Throwable> {
  
  private Activity context;
  private int progressStringId;
  private int successStringId;
  private int failureStringId;
  private ProgressDialog progressDialog;
  private R resultRun;
  private boolean retry;
  private boolean retryEnabled;
  
  /**
   * @param ctx
   */
  public AsyncServiceCall(Activity ctx) {
    if (ctx == null) {
      throw new IllegalArgumentException("Context must not be null.");
    }

    setContext(ctx);
    progressStringId = -1;
    successStringId = -1;
    failureStringId = -1;
    retryEnabled = true;
  }
  
  /**
   * Retourne l'identifiant de la chaine utilis?e dans la progress bar
   * @return L'identifiant de la chaine utilis?e dans la progress bar
   */
  public int getProgressStringId() {
    return progressStringId;
  }

  /**
   * D?finit la chaine ? utilis?e comme texte affich? dans la progress bar
   * @param strId Identifiant de la ressource
   */
  public void setProgressStringId(int strId) {
    this.progressStringId = strId;
  }

  /**
   * Retourne l'identifiant de la chaine utilis?e dans la popup en cas de succ?s
   * @return L'identifiant de la chaine utilis?e dans la popup en cas de succ?s
   */
  public int getSuccessStringId() {
    return successStringId;
  }

  /**
   * D?finit la chaine ? utilis?e comme texte dans la popup en cas de succ?s
   * @param strId Identifiant de la ressource
   */
  public void setSuccessStringId(int strId) {
    this.successStringId = strId;
  }

  /**
   * Retourne l'identifiant de la chaine utilis?e dans la popup en cas d'?chec
   * @return L'identifiant de la chaine utilis?e dans la popup en cas d'?chec
   */
  public int getFailureStringId() {
    return failureStringId;
  }

  /**
   * D?finit la chaine ? utiliser comme texte affich? dans la progress bar
   * @param strId Identifiant de la ressource
   */
  public void setFailureStringId(int strId) {
    this.failureStringId = strId;
  }
  
  /**
   * D?fini si l'application doit r?essayer en cas d'?chec 
   * @param enabled True si elle doit recommencer, false sinon
   */
  public void setRetry(boolean enabled) {
    retryEnabled = enabled;
  }
  
  /**
   * Renvoie le contexte utilis? lors de la cr?ation du processus
   * @return
   */
  protected Activity getContext() {
    return context;
  }

  /**
   * @see android.os.AsyncTask#onPreExecute()
   */
  @Override
  protected final void onPreExecute() {
    if (getProgressStringId() != -1) {
      progressDialog = ProgressDialog.show(getContext(),"",getContext().getString(getProgressStringId()),false);
    }
    //
    onPreRun();
  }

  /**
   * @see android.os.AsyncTask#doInBackground(Params[])
   */
  @Override
  protected final Throwable doInBackground(P... params) {
    Throwable error = null;
    //
    do {
      try {
        if (!IOUtils.isOnline(getContext())) {
          return new IOException();
        }
        //
        resultRun = run(params);
        //
        return null;
      } catch (Throwable e) {
        if (retryEnabled) {
          retry(e);
        }
        error = e;
      }
    } while (retry);
    //
    return error;
  }
  /**
   * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
   */
  @Override
  protected final void onPostExecute(Throwable result) {
    
    try{
      if (progressDialog != null) {
        progressDialog.dismiss();
      }
    }catch(Exception e){
      e.printStackTrace();
    }
    
    //
    if (result != null) {
      if (!retryEnabled) {
        if (getFailureStringId() != -1) {
          UIUtil.showMessage(getContext(), getFailureStringId());
        } else {
          UIUtil.showMessage(getContext(), result);
        }
      }
      //
      onFailedRun(result);
    } else {
      if (getSuccessStringId() != -1) {
        UIUtil.showMessage(getContext(), getSuccessStringId());
      }
      
      onPostRun(resultRun);
    }
  };

  protected void onPreRun() {}
  protected abstract R run(P... params) throws IOException;
  protected void onPostRun(R result) {}

  protected void onFailedRun(Throwable result) {}
  
  /**
   * Relance le processus
   * @param exception L'exception ayant conduit ? l'echec de l'essai pr?c?dent
   */
  private void retry(Throwable exception) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    //
    builder.setTitle(getContext().getString(com.iutdijon.androiut2.R.string.app_name));
    builder.setMessage(getRetryMessage(exception));
    builder.setCancelable(false);
    builder.setPositiveButton(
      getContext().getString(com.iutdijon.androiut2.R.string.yes),
      new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int id) {
        retry = true;
        //
        synchronized (AsyncServiceCall.this) {
          AsyncServiceCall.this.notify();
        }
      }
    });
    builder.setNegativeButton(
      getContext().getString(com.iutdijon.androiut2.R.string.no),
      new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int id) {
        retry = false;
        //
        synchronized (AsyncServiceCall.this) {
          AsyncServiceCall.this.notify();
        }
      }
    });
    //
    getContext().runOnUiThread(new Runnable() {
      @Override
      public void run() {
        builder.create().show();
      }
    });
    //
    synchronized (AsyncServiceCall.this) {
      try {
        wait();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }
  
  /**
   * Renvoie une popup pour demander si l'on doit recommencer ou non le traitement en cas d'?chec
   * @param exception L'exception ayant entrain?e l'?chec
   * @return
   */
  private String getRetryMessage(Throwable exception) {
    String retryMsg = getContext().getString(com.iutdijon.androiut2.R.string.try_again);
    //
    if (getFailureStringId() != -1) {
      retryMsg =
        getContext().getString(getFailureStringId()) + " " + retryMsg;
    } else {
      retryMsg =
        getContext().getString(
          UIUtil.getMessageId(exception)) + " " +retryMsg;
    }
    //
    return retryMsg;
  }

  /**
   * D?fini le contexte d'application du processus
   * @param context the context to set
   */
  public void setContext(Activity context) {
    this.context = context;
  }
}




Java Source Code List

.ADEResourcesDataTree.java
com.iutdijon.androiut2.ade.activities.ADEActivity.java
com.iutdijon.androiut2.ade.activities.ADEListResourcesChooser.java
com.iutdijon.androiut2.ade.activities.OnImageDownloadedCallback.java
com.iutdijon.androiut2.ade.service.ADEICalendarDownloader.java
com.iutdijon.androiut2.ftp.activities.FTPActivity.java
com.iutdijon.androiut2.ftp.adapters.FTPFileListAdapter.java
com.iutdijon.androiut2.ftp.data.FTPCommandResult.java
com.iutdijon.androiut2.ftp.data.FTPCommandWrapper.java
com.iutdijon.androiut2.ftp.data.FTPCommand.java
com.iutdijon.androiut2.ftp.services.FTPDownloadMonitor.java
com.iutdijon.androiut2.ftp.services.FTPServiceCall.java
com.iutdijon.androiut2.ftp.services.ProgressDownload.java
com.iutdijon.androiut2.global.AndroIUTApplication.java
com.iutdijon.androiut2.iut.activities.HomeActivity.java
com.iutdijon.androiut2.iut.activities.LoginActivity.java
com.iutdijon.androiut2.iut.data.Credential.java
com.iutdijon.androiut2.iut.data.UserFactory.java
com.iutdijon.androiut2.iut.data.account.StudentAccount.java
com.iutdijon.androiut2.iut.data.account.TeacherAccount.java
com.iutdijon.androiut2.iut.data.account.UserAccount.java
com.iutdijon.androiut2.iut.services.IUTLoginServiceCall.java
com.iutdijon.androiut2.mail.activities.MailActivity.java
com.iutdijon.androiut2.mail.activities.MailReaderActivity.java
com.iutdijon.androiut2.mail.adapters.MailDisplayAdapter.java
com.iutdijon.androiut2.mail.adapters.MailsListAdapter.java
com.iutdijon.androiut2.mail.services.EmailUtils.java
com.iutdijon.androiut2.mail.services.MailReader.java
com.iutdijon.androiut2.mail.services.MailService.java
com.iutdijon.androiut2.schooling.activities.SchoolingActivity.java
com.iutdijon.androiut2.schooling.activities.SchoolingTabActivity.java
com.iutdijon.androiut2.schooling.adapters.SectionedAdapter.java
com.iutdijon.androiut2.schooling.data.Absence.java
com.iutdijon.androiut2.schooling.data.Mark.java
com.iutdijon.androiut2.schooling.data.SchoolingData.java
com.iutdijon.androiut2.schooling.service.SchoolingDataServiceCall.java
com.iutdijon.androiut2.util.AndroIUTLogger.java
com.iutdijon.androiut2.util.IOUtils.java
com.iutdijon.androiut2.util.PreferencesManager.java
com.iutdijon.androiut2.util.UIUtil.java
com.iutdijon.androiut2.util.adapters.AbsencesAdapter.java
com.iutdijon.androiut2.util.adapters.BitmapAdapter.java
com.iutdijon.androiut2.util.adapters.CalendarAdapter.java
com.iutdijon.androiut2.util.adapters.IAdapter.java
com.iutdijon.androiut2.util.adapters.MarksAdapter.java
com.iutdijon.androiut2.util.adapters.UserAdapter.java
com.iutdijon.androiut2.util.adapters.XmlAdapter.java
com.iutdijon.androiut2.util.bridge.BridgeFinder.java
com.iutdijon.androiut2.util.loaders.AndroIUTHTTPConnection.java
com.iutdijon.androiut2.util.loaders.AsyncServiceCall.java
com.iutdijon.androiut2.util.loaders.BitmapDownloaderTask.java
com.iutdijon.androiut2.util.loaders.GetAsyncServiceCall.java
com.iutdijon.androiut2.util.loaders.ImageDownloader.java
com.iutdijon.androiut2.util.loaders.PostAsyncServiceCall.java
com.iutdijon.androiut2.util.observers.Observable.java
com.iutdijon.androiut2.util.observers.Observer.java
com.iutdijon.androiut2.util.security.SecureCenter.java
com.iutdijon.androiut2.util.view.ImageZoomView.java
com.iutdijon.androiut2.util.zoom.AspectQuotient.java
com.iutdijon.androiut2.util.zoom.DynamicZoomControl.java
com.iutdijon.androiut2.util.zoom.LongPressZoomListener.java
com.iutdijon.androiut2.util.zoom.PinchZoomListener.java
com.iutdijon.androiut2.util.zoom.SimpleZoomListener.java
com.iutdijon.androiut2.util.zoom.ZoomState.java
com.iutdijon.androiut2.util.zoom.util.Dynamics.java
com.iutdijon.androiut2.util.zoom.util.SpringDynamics.java