Android Open Source - PrepayCredit Usage Fetcher






From Project

Back to project page PrepayCredit.

License

The source code is released under:

GNU General Public License

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

/*
 * This file is part of Prepay Credit for Android
 */* w w  w . jav  a 2 s.c  o  m*/
 * Copyright  2014  Damien O'Reilly
 *
 * Prepay Credit for Android 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.
 * Prepay Credit for Android 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 Prepay Credit for Android.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Report bugs or new features at: https://github.com/DamienOReilly/PrepayCredit
 * Contact the author at:          damienreilly@gmail.com
 */

package damo.three.ie.prepay;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import damo.three.ie.net.ProcessRequest;
import damo.three.ie.net.ThreeHttpClient;
import damo.three.ie.util.AccountException;
import damo.three.ie.util.HtmlUtilities;
import damo.three.ie.util.PrepayException;
import org.acra.ACRA;
import org.apache.http.NameValuePair;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;

import java.io.IOException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Damien O'Reilly
 */
class UsageFetcher {

    private String pageContent = null;
    private JSONArray jsonArray = null;
    private List<NameValuePair> postData = null;
    private ProcessRequest processRequest;
    private boolean runningInBackground = false;
    private final Context context;

    public UsageFetcher(Context context, boolean runningInBackground) throws CertificateException,
            NoSuchAlgorithmException, KeyStoreException,
            IOException {
        this.runningInBackground = runningInBackground;
        this.context = context;
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        DefaultHttpClient httpClient = ThreeHttpClient.getInstance(context).getHttpClient();
        processRequest = new ProcessRequest(httpClient);
        postData = new ArrayList<NameValuePair>();

        jsonArray = new JSONArray();

        addPropertyToPostData("username", sharedPreferences.getString("mobile", ""));
        addPropertyToPostData("password", sharedPreferences.getString("password", ""));
    }

    /**
     * Begin fetching the usage
     */
    public JSONArray getUsages() throws IOException, AccountException, JSONException, PrepayException {

        // Some change on 22-08-2014 means that when we have cookies cached from previous login and this login session
        // is expired, if we perform a login and go through login steps, we are greeted with login page again. Clear
        // cookies prior to prevent this.
        try {
            ThreeHttpClient.getInstance(context).getHttpClient().getCookieStore().clear();
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        // Attempt to log in.
        pageContent = processRequest.process(Constants.MY3_ACCOUNT_PAGE);
        Log.d(Constants.TAG, "using: my3account.three.ie");

        // Were we brought to the login page? If so, login. We sometimes skip this if our cookie still holds a valid
        // session.
        if (pageContent.contains("<label for=\"username\" class=\"portlet-form-input-label\">")) {
            Pattern p1 = Pattern.compile(Constants.LOGIN_TOKEN_REGEX, Pattern.DOTALL);
            Matcher m1 = p1.matcher(pageContent);
            if (m1.matches()) {
                Log.d(Constants.TAG, "Logging in...");
                addPropertyToPostData("lt", m1.group(1));
                pageContent = processRequest.process(Constants.MY3_ACCOUNT_PAGE, postData);
                if (pageContent.contains("Sorry, you've entered an invalid")) {
                    throw new AccountException("Invalid 3 mobile number or password.");
                } else if (pageContent.contains("You have entered your login details incorrectly too many times")) {
                    throw new AccountException("Account is temporarily disabled due to too many incorrect logins. " +
                            "Please try again later.");
                }
            }
        }

        // We end up here if we have logged in, or if our cookie was still valid.
        if (pageContent.contains("here</a> to access the service you requested.</p>")) {
            Pattern p1 = Pattern.compile(Constants.MY3_SERVICE_REGEX, Pattern.DOTALL);
            Matcher m1 = p1.matcher(pageContent);
            if (m1.matches()) {
                pageContent = processRequest.process(m1.group(1));
            }

            //As of 22-08-2014, this page comes up a second time, with a new token. This time without SSL!
            if (pageContent.contains("here</a> to access the service you requested.</p>")) {
                Pattern p2 = Pattern.compile(Constants.MY3_SERVICE_REGEX, Pattern.DOTALL);
                Matcher m2 = p2.matcher(pageContent);
                if (m2.matches()) {
                    pageContent = processRequest.process(m2.group(1));
                }
            }

            if (pageContent.contains("<p><strong>Your account balance.</strong></p>")) {
                parseUsage();
            } else {
                if (!runningInBackground) {
                    reportErrorFetchingUsage();
                }
            }

        } else if (pageContent.contains("This service is currently unavailable. Please try again later.")) {
            // Known service error from three.
            throw new PrepayException("my3account.three.ie is reporting: \"This service is currently unavailable. " +
                    "Please try again later.\"");
        } else {
            // Unknown response
            if (!runningInBackground) {
                reportErrorFetchingUsage();
            }
        }
        return jsonArray;
    }

    /**
     * There was some problem fetching the usage, Alert the user, and log report for unexpected application state.
     * Might be useful for debugging.
     */
    private void reportErrorFetchingUsage() throws PrepayException {

        String msg = "Unexpected response from server. Are you a 3Pay Ireland user? Or is my3account.three.ie down?";

        // There was some problem logging in. Log a bug report in-case 3 changed their page and we cannot parse it.
        PrepayException ex = new PrepayException(msg);
        ACRA.getErrorReporter().putCustomData("CURRENT_PAGE_CONTENT", pageContent);
        ACRA.getErrorReporter().handleSilentException(ex);

        //still let the user know we couldn't fetch the usage.
        throw new PrepayException(ex);
    }

    /**
     * Clean up the HTML, and parse. Convert usages into JSON.
     */
    private void parseUsage() throws JSONException {
        jsonArray = HtmlUtilities.parseUsageAsJSONArray(pageContent);
    }


    /**
     * add a POST property/value as {@link org.apache.http.message.BasicNameValuePair} to {@link List<NameValuePair>}
     *
     * @param property POST property
     * @param value    POST value
     */
    private void addPropertyToPostData(String property, String value) {
        postData.add(new BasicNameValuePair(property, value));
    }
}




Java Source Code List

damo.three.ie.PrepayCreditApplication.java
damo.three.ie.activity.AboutActivity.java
damo.three.ie.activity.InternetExpirationActivity.java
damo.three.ie.activity.LoginActivity.java
damo.three.ie.activity.PrepayCreditActivity.java
damo.three.ie.activity.SettingsActivity.java
damo.three.ie.fragment.AboutFragment.java
damo.three.ie.fragment.ChangeLogFragment.java
damo.three.ie.fragment.SettingsFragment.java
damo.three.ie.fragment.UpdateFragment.java
damo.three.ie.net.EasySSLSocketFactory.java
damo.three.ie.net.EasyX509TrustManager.java
damo.three.ie.net.ProcessRequest.java
damo.three.ie.net.ThreeHttpClient.java
damo.three.ie.net.ThreeHttpParameters.java
damo.three.ie.prepay.ConnectivityReceiver.java
damo.three.ie.prepay.Constants.java
damo.three.ie.prepay.InternetAddonAlarmManagerReceiver.java
damo.three.ie.prepay.InternetAddonExpireReceiver.java
damo.three.ie.prepay.UpdateAlarmManagerReceiver.java
damo.three.ie.prepay.UpdateAsyncTask.java
damo.three.ie.prepay.UpdateReceiver.java
damo.three.ie.prepay.UpdateService.java
damo.three.ie.prepay.UsageFetcher.java
damo.three.ie.prepayusage.BasicUsageItemExpireSorter.java
damo.three.ie.prepayusage.BasicUsageItemTypeSorter.java
damo.three.ie.prepayusage.BasicUsageItem.java
damo.three.ie.prepayusage.BasicUsageItemsGrouped.java
damo.three.ie.prepayusage.ExpireGroupType.java
damo.three.ie.prepayusage.InternetUsageRegistry.java
damo.three.ie.prepayusage.ItemFactory.java
damo.three.ie.prepayusage.UsageItem.java
damo.three.ie.prepayusage.items.Data.java
damo.three.ie.prepayusage.items.FreeCash.java
damo.three.ie.prepayusage.items.InternetAddon.java
damo.three.ie.prepayusage.items.OutOfBundle.java
damo.three.ie.prepayusage.items.SkypeCalls.java
damo.three.ie.prepayusage.items.Texts.java
damo.three.ie.prepayusage.items.Three2ThreeCalls.java
damo.three.ie.prepayusage.items.TopUp.java
damo.three.ie.prepayusage.items.WeekendVoiceMinutes.java
damo.three.ie.ui.BasicUsageLayout.java
damo.three.ie.ui.ExtendedScrollView.java
damo.three.ie.ui.OutOfBundleLayout.java
damo.three.ie.ui.SlidingTabLayout.java
damo.three.ie.ui.SlidingTabStrip.java
damo.three.ie.ui.ViewPagerAdapter.java
damo.three.ie.util.AccountException.java
damo.three.ie.util.CustomTagHandler.java
damo.three.ie.util.DateUtils.java
damo.three.ie.util.FileUtils.java
damo.three.ie.util.HtmlUtilities.java
damo.three.ie.util.JSONUtils.java
damo.three.ie.util.NumberUtils.java
damo.three.ie.util.PrepayException.java
damo.three.ie.util.UsageUtils.java