Android Open Source - saldo Coop Manager






From Project

Back to project page saldo.

License

The source code is released under:

GNU General Public License

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

/*
 * Saldo - http://github.com/kria/saldo/* w ww. ja v  a 2  s .c  o m*/
 * 
 * Copyright (C) 2010 Kristian Adrup
 * 
 * This file is part of Saldo.
 * 
 * Saldo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Saldo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.adrup.saldo.bank.coop;

import com.adrup.http.EasySSLSocketFactory;
import com.adrup.http.HttpException;
import com.adrup.http.HttpHelper;
import com.adrup.saldo.SaldoHttpClient;
import com.adrup.saldo.bank.Account;
import com.adrup.saldo.bank.AccountHashKey;
import com.adrup.saldo.bank.AuthenticationException;
import com.adrup.saldo.bank.BankException;
import com.adrup.saldo.bank.BankLogin;
import com.adrup.saldo.bank.BankManager;
import com.adrup.saldo.bank.RemoteAccount;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;

import android.content.Context;
import android.util.Log;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * A coop implementation of {@link BankManager}.
 * 
 * @author Kristian Adrup
 * 
 */
public class CoopManager implements BankManager {
  private static final String TAG = "CoopManager";
  private static final String NAME = "coop";
  private static final String LOGIN_URL = "https://www.coop.se/Mina-sidor/Oversikt/";
  private static final String ACCOUNT_URL = "https://www.coop.se/Mina-sidor/Oversikt/?t=MedMeraAccount";
  private static final String CREDIT_URL = "https://www.coop.se/Mina-sidor/Oversikt/?t=MedMeraVisa";
  private static final String USER_PARAM = "ctl00$ContentPlaceHolderMainPageContainer$ContentPlaceHolderPreContent$RegisterMediumUserForm$TextBoxUserName";
  private static final String PASS_PARAM = "ctl00$ContentPlaceHolderMainPageContainer$ContentPlaceHolderPreContent$RegisterMediumUserForm$TextBoxPassword";
  private static final String BUTTON_PARAM = "ctl00$ContentPlaceHolderMainPageContainer$ContentPlaceHolderPreContent$RegisterMediumUserForm$ButtonLogin";
  
  private static final String VIEWSTATE_PARAM = "__VIEWSTATE";
  
  /*
   * <div class="column6">
                <table class="randig">
                    <tbody><tr class="emphasize">
                        <td class="emphasize">Saldo:</td>

                        <td>28,12 kr</td>
                    </tr>
                    <tr>
                        <td class="emphasize">Disponibelt belopp:</td>
                        <td class="emphasize">28,12 kr</td>
                    </tr>
                    <tr class="emphasize">

                        <td class="emphasize">Ej bokf?rda k?p:</td>
                        <td>0,00 kr</td>
                    </tr>
                    <tr>
                        <td class="emphasize">Beviljad kredit:</td>
                        <td>0,00 kr</td>
                    </tr>
                    
                    <tbody><tr class="emphasize">
                        <td class="emphasize">Saldo:</td>
                        <td>-16.001,74 kr</td>
                    </tr>
                    <tr>
                        <td class="emphasize">Disponibelt belopp:</td>
                        <td class="emphasize">3.852,90 kr</td>

                    </tr>
                    <tr class="emphasize">
                        <td class="emphasize">Ej bokf?rda k?p:</td>
                        <td>-145,36 kr</td>
                    </tr>
                    <tr>
                        <td class="emphasize">Beviljad kredit:</td>

                        <td>20.000,00 kr</td>
                    </tr>
                    
                    
                    <tr class="emphasize">
                        <td class="emphasize">Fakturerat belopp:</td>
                        <td>5.313,34 kr</td>
                    </tr>
                    <tr>

                        <td class="emphasize">L?gsta belopp att betala:</td>
                        <td>200,00 kr</td>
                    </tr>
                    <tr class="emphasize">
                        <td class="emphasize">Oss tillhanda senast:</td>
                        <td>2010-05-31</td>
                    </tr>

                    
            </tbody>

   */
   
  private static final String VIEWSTATE_REGEX = "__VIEWSTATE\" value=\"([^\"]+)\"";
  private static final String ACCOUNTS_REGEX = "Disponibelt belopp[^\n]*[^>]*>([0-9., ]+)";
  
  private BankLogin mBankLogin;
  private Context mContext;

  public CoopManager(BankLogin bankLogin, Context context) {
    this.mBankLogin = bankLogin;
    this.mContext = context;
  }

  @Override
  public String getName() {
    return NAME;
  }

  @Override
  public Map<AccountHashKey, RemoteAccount> getAccounts() throws BankException {
    return getAccounts(new LinkedHashMap<AccountHashKey, RemoteAccount>());
  }
  
  @Override
  public Map<AccountHashKey, RemoteAccount> getAccounts(Map<AccountHashKey, RemoteAccount> accounts) throws BankException {
    Log.d(TAG, "getAccounts()");
    HttpClient httpClient = new SaldoHttpClient(mContext);
    
    try {
      // get login page

      Log.d(TAG, "getting login page");

      String res = HttpHelper.get(httpClient, LOGIN_URL);

      Matcher matcher = Pattern.compile(VIEWSTATE_REGEX).matcher(res);
      if (!matcher.find()) {
        Log.e(TAG, "No viewstate match.");
        Log.d(TAG, res);
        throw new CoopException("No viewState match.");
        
      }
      String viewState = matcher.group(1);
      Log.d(TAG, "viewState= " + viewState);
      
      // do login post, should redirect us to the accounts page
      List<NameValuePair> parameters = new ArrayList<NameValuePair>(3);

      parameters.add(new BasicNameValuePair(VIEWSTATE_PARAM, viewState));
      parameters.add(new BasicNameValuePair(USER_PARAM, mBankLogin.getUsername()));
      parameters.add(new BasicNameValuePair(PASS_PARAM, mBankLogin.getPassword()));
      parameters.add(new BasicNameValuePair(BUTTON_PARAM, "Logga in"));

      res = HttpHelper.post(httpClient, LOGIN_URL, parameters);

      if (res.contains("Felmeddelande")) {
        
        Log.d(TAG, "auth fail");
        throw new AuthenticationException("auth fail");
      }

      // get and extract account info
      res = HttpHelper.get(httpClient, ACCOUNT_URL);
      matcher = Pattern.compile(ACCOUNTS_REGEX).matcher(res);

      int remoteId = 1;
      int count = 0;
      while (matcher.find()) {
        count++;
        int ordinal = remoteId;
        String name = "MedMera Konto";//Html.fromHtml(matcher.group(1)).toString().trim();
        long balance = Long.parseLong(matcher.group(1).replaceAll("\\,|\\.| ", "")) / 100;
        accounts.put(new AccountHashKey(String.valueOf(remoteId), mBankLogin.getId()), new Account(String.valueOf(remoteId),
            mBankLogin.getId(), ordinal, name, balance));
        remoteId++;
      }
      
      res = HttpHelper.get(httpClient, CREDIT_URL);
      matcher = Pattern.compile(ACCOUNTS_REGEX).matcher(res);
      while (matcher.find()) {
        count++;
        int ordinal = remoteId;
        String name = "MedMera Visa";//Html.fromHtml(matcher.group(1)).toString().trim();
        long balance = Long.parseLong(matcher.group(1).replaceAll("\\,|\\.| ", "")) / 100;
        accounts.put(new AccountHashKey(String.valueOf(remoteId), mBankLogin.getId()), new Account(String.valueOf(remoteId),
            mBankLogin.getId(), ordinal, name, balance));
        remoteId++;
      }
      
      if (count == 0) {
        Log.d(TAG, "no accounts added");
        Log.d(TAG, res);
      }
    } catch (IOException e) {
      Log.e(TAG, e.getMessage(), e);
      throw new CoopException(e.getMessage(), e);
    } catch (HttpException e) {
      Log.e(TAG, e.getMessage(), e);
      throw new CoopException(e.getMessage(), e);
    } finally {
      httpClient.getConnectionManager().shutdown();
    }

    return accounts;
  }
}




Java Source Code List

com.adrup.http.EasySSLSocketFactory.java
com.adrup.http.EasyX509TrustManager.java
com.adrup.http.HttpException.java
com.adrup.http.HttpHelper.java
com.adrup.saldo.AccountsViewBinder.java
com.adrup.saldo.AutoUpdateReceiver.java
com.adrup.saldo.AutoUpdateService.java
com.adrup.saldo.BankListActivity.java
com.adrup.saldo.BankLoginEditActivity.java
com.adrup.saldo.Constants.java
com.adrup.saldo.DatabaseAdapter.java
com.adrup.saldo.SaldoHttpClient.java
com.adrup.saldo.Saldo.java
com.adrup.saldo.SettingsActivity.java
com.adrup.saldo.Util.java
com.adrup.saldo.bank.AccountHashKey.java
com.adrup.saldo.bank.Account.java
com.adrup.saldo.bank.AuthenticationException.java
com.adrup.saldo.bank.BankException.java
com.adrup.saldo.bank.BankLogin.java
com.adrup.saldo.bank.BankManagerFactory.java
com.adrup.saldo.bank.BankManager.java
com.adrup.saldo.bank.RemoteAccount.java
com.adrup.saldo.bank.coop.CoopException.java
com.adrup.saldo.bank.coop.CoopManager.java
com.adrup.saldo.bank.firstcard.FirstcardException.java
com.adrup.saldo.bank.firstcard.FirstcardManager.java
com.adrup.saldo.bank.ica.IcaException.java
com.adrup.saldo.bank.ica.IcaManager.java
com.adrup.saldo.bank.icabanken.IcabankenException.java
com.adrup.saldo.bank.icabanken.IcabankenManager.java
com.adrup.saldo.bank.lf.LfBankException.java
com.adrup.saldo.bank.lf.LfBankManager.java
com.adrup.saldo.bank.nordea.NordeaException.java
com.adrup.saldo.bank.nordea.NordeaManager.java
com.adrup.saldo.bank.preem.PreemException.java
com.adrup.saldo.bank.preem.PreemManager.java
com.adrup.saldo.bank.statoil.StatoilException.java
com.adrup.saldo.bank.statoil.StatoilManager.java
com.adrup.saldo.bank.swedbank.SwedbankException.java
com.adrup.saldo.bank.swedbank.SwedbankManager.java
com.adrup.saldo.widget.SaldoWidgetProvider.java
com.adrup.saldo.widget.WidgetConfigurationActivity.java
com.adrup.saldo.widget.WidgetService.java
com.adrup.util.HashCodeUtil.java
com.adrup.util.NumberUtil.java
com.adrup.util.SectionedAdapter.java