Android Open Source - SuiviConsoFreeMobile Telechargeur Generique






From Project

Back to project page SuiviConsoFreeMobile.

License

The source code is released under:

Ce logiciel est dans le domaine public.

If you think the Android project SuiviConsoFreeMobile 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

package net.yvesd.scfm;
/*from   w  w w .  j  a va  2 s .c  om*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;

import android.os.AsyncTask;
import android.util.Log;

public abstract class TelechargeurGenerique
    extends
    AsyncTask<DataRecuperatorParams, TelechargeurGenerique.ProgressUpdate, List<String>> {

  /**
   * URL de connexion par dfaut
   */
  private static final String URL_INITIALE_CONNEXION = "https://mobile.free.fr/moncompte/index.php?page=suiviconso";

  CanWaitForStream activity;
  List<String> messages = new ArrayList<String>();

  protected DefaultHttpClient httpClient;

  public TelechargeurGenerique(CanWaitForStream activity,
      DefaultHttpClient httpClient) {
    this.activity = activity;
    this.httpClient = httpClient;
  }

  protected abstract String getUrlCible();

  protected String getUrlConnexion() {
    return URL_INITIALE_CONNEXION;
  }

  @Override
  public List<String> doInBackground(DataRecuperatorParams... params) {

    List<String> results = new ArrayList<String>();
    for (DataRecuperatorParams param : params) {
      String s = downloadConsoData(param);
      results.add(s);
    }

    return results;
  }

  @Override
  protected void onProgressUpdate(
      TelechargeurGenerique.ProgressUpdate... values) {
    super.onProgressUpdate(values);

    for (ProgressUpdate pu : values) {
      activity.addToProgress(pu.getRes(), pu.getArgs());
      activity.setProgressStatus(pu.getProgress());
    }
  }

  protected String recupererPageConnexion(BasicHttpContext mHttpContext) {

    HttpGet get = new HttpGet(getUrlConnexion());
    HttpResponse getResponse;
    StringBuffer sb1 = new StringBuffer();
    try {
      getResponse = httpClient.execute(get, mHttpContext);
      HttpEntity responseEntity = getResponse.getEntity();
      InputStream is = responseEntity.getContent();
      BufferedReader in = new BufferedReader(new InputStreamReader(is));

      String l;
      while ((l = in.readLine()) != null) {
        sb1.append(l);
        sb1.append("\n");
      }
    } catch (Exception e) {
      publishProgress(new ProgressUpdate(R.string.log_erreur0600, 0));
      return "";
    }

    return sb1.toString();
  }

  private String downloadConsoData(DataRecuperatorParams param) {

    BasicHttpContext mHttpContext = new BasicHttpContext();
    CookieStore mCookieStore = new BasicCookieStore();
    mHttpContext.setAttribute(ClientContext.COOKIE_STORE, mCookieStore);

    String pageConnexion = recupererPageConnexion(mHttpContext);

    String identifiantCode = getScrambledIdent(pageConnexion,
        param.getLoginAbo());

    HttpPost post = new HttpPost(getUrlConnexion());

    post.addHeader("Referer", URL_INITIALE_CONNEXION);

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    nameValuePairs
        .add(new BasicNameValuePair("login_abo", identifiantCode));
    nameValuePairs
        .add(new BasicNameValuePair("pwd_abo", param.getPwdAbo()));
    try {
      post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException e1) {
      publishProgress(new ProgressUpdate(R.string.log_erreur0100, 100));
      return null;
    }

    HttpResponse postResponse;
    try {
      postResponse = httpClient.execute(post, mHttpContext);
      HttpEntity responseEntity = postResponse.getEntity();
      InputStream is = responseEntity.getContent();
      BufferedReader in = new BufferedReader(new InputStreamReader(is));

      String l;
      StringBuffer sb1 = new StringBuffer();
      while ((l = in.readLine()) != null) {
        sb1.append(l);
        sb1.append("\n");
      }

      String erreur = ExtracteurErreur.litErreur(sb1.toString());

      if (erreur == null) {
        publishProgress(new ProgressUpdate(R.string.log_lecturedonnees,
            66));
      } else {
        publishProgress(new ProgressUpdate(
            R.string.log_identificationimpossible, 100, erreur));

        publishProgress(new ProgressUpdate(
            R.string.log_instructionconfig, 100));

        return null;
      }

      String donnesLues = lectureDonnees(mHttpContext,
          param.getLoginAbo());

      return donnesLues;

    } catch (ClientProtocolException e) {
      publishProgress(new ProgressUpdate(R.string.log_erreur0200, 100));
      return null;
    } catch (IOException e) {
      publishProgress(new ProgressUpdate(R.string.log_erreur0300, 100));
      return null;
    }
  }

  protected String getScrambledIdent(String pageConnexion, String login) {
    Map<Integer, Integer> map = getIdentMap(pageConnexion);
    String scrambledLogin = "";
    for (int i = 0; i < login.length(); i++) {
      String charac = login.substring(i, i + 1);
      Integer characInteger = Integer.valueOf(charac);
      if (map.containsKey(characInteger)) {
        scrambledLogin += map.get(characInteger);
      }
    }
    Log.d("", "Scrambled login : " + scrambledLogin);
    return scrambledLogin;
  }

  /**
   * <p>
   * Mthode qui va rellement effectuer la requte "mtier" sur le site Free.
   * Elle doit galement retourner les donnes.
   * 
   * <p>
   * Cette mthode est destine  tre surcharge selon le type de requtes
   * qu'il y  a faire, et selon les paramtres a insrer dans la requte (en
   * particulier dans les cas de HTTP POST). La mthode ici est une
   * implmentation par dfaut HTTP GET.
   * 
   * @param mHttpContext
   *            Contexte HTTP (transporte notamment les cookies
   * @return Donnes lues
   * @throws IOException
   * @throws ClientProtocolException
   */
  protected String lectureDonnees(BasicHttpContext mHttpContext, String login)
      throws IOException, ClientProtocolException {

    HttpGet httpGet = new HttpGet(getUrlCible());

    HttpResponse getResponse = httpClient.execute(httpGet, mHttpContext);

    String donneesLues = litDonnesDepuisHttpResponse(getResponse);

    return donneesLues;
  }

  /**
   * Constitue une string  partir des de la HttpResponse
   * 
   * @param getResponse
   * @return
   * @throws IOException
   */
  protected String litDonnesDepuisHttpResponse(HttpResponse getResponse)
      throws IOException {
    HttpEntity responseEntity2 = getResponse.getEntity();
    InputStream is2 = responseEntity2.getContent();
    BufferedReader in2 = new BufferedReader(new InputStreamReader(is2));

    StringBuffer sb = new StringBuffer();
    String l2;
    while ((l2 = in2.readLine()) != null) {
      sb.append(l2);
    }
    return sb.toString();
  }

  @Override
  protected void onPostExecute(List<String> result) {
    super.onPostExecute(result);
    activity.handleResult(result);
  }

  static class ProgressUpdate {
    private int res;
    private int progress;
    private Object[] args;

    public ProgressUpdate(int res, int progress, Object... args) {
      super();
      this.res = res;
      this.progress = progress;
      this.args = args;
    }

    public int getRes() {
      return res;
    }

    public int getProgress() {
      return progress;
    }

    public Object[] getArgs() {
      return args;
    }
  }

  /**
   * 
   * @param fluxHtml
   * @return Map Cl : numro dans l'identifiant, Valeur : position
   */
  public Map<Integer, Integer> getIdentMap(String fluxHtml) {
    Map<Integer, Integer> map = new HashMap<Integer, Integer>();

    List<String> matches = new ArrayList<String>();

    Pattern p1 = Pattern
        .compile("(ident_addNumber\\([0-9]+,\\s*[0-9]+\\))");

    Matcher m1 = p1.matcher(fluxHtml);
    while (m1.find()) {
      matches.add(m1.group(1));
    }

    for (String s : matches) {
      Pattern p2 = Pattern
          .compile("ident_addNumber\\(([0-9]+),\\s*([0-9]+)\\)");
      Matcher m2 = p2.matcher(s);
      while (m2.find()) {
        String a1 = m2.group(1);
        String a2 = m2.group(2);
        Integer ai1 = Integer.valueOf(a1);
        Integer ai2 = Integer.valueOf(a2);

        map.put(ai1, ai2);
        Log.d("", ai1 + " ==> " + ai2);
      }
    }

    return map;
  }
}




Java Source Code List

net.yvesd.scfm.AfficherSuiviconsoDetailleActivity.java
net.yvesd.scfm.CanWaitForStream.java
net.yvesd.scfm.DataInterpreter.java
net.yvesd.scfm.DataRecuperatorMock.java
net.yvesd.scfm.DataRecuperatorParams.java
net.yvesd.scfm.DonnesCompteur.java
net.yvesd.scfm.EditAccountActivity.java
net.yvesd.scfm.EditAccountsActivity.java
net.yvesd.scfm.ExtracteurErreur.java
net.yvesd.scfm.GestionIcones.java
net.yvesd.scfm.GestionnaireThemes.java
net.yvesd.scfm.MyHttpClient.java
net.yvesd.scfm.OptionActivity.java
net.yvesd.scfm.SuiviConsoFreeMobileActivity.java
net.yvesd.scfm.TelechargeurDonnesConso.java
net.yvesd.scfm.TelechargeurGenerique.java
net.yvesd.scfm.TelechargeurSuiviConsoDetaille.java