From 7c22589f395108da2b6765f2a50604f0b544f467 Mon Sep 17 00:00:00 2001 From: Bobby Graese Date: Thu, 31 May 2012 00:09:43 -0500 Subject: [PATCH] Data Usage Basic data usage implementation. Could probably use some work, but tested and working on my Motorola Triumph. --- .../res/values/strings.xml | 1 + .../src/com/baker/vm/PreferencesUtil.java | 11 ++++- .../src/com/baker/vm/VMAccount.java | 24 +++++++++ .../baker/vm/ui/MultipleAccountsActivity.java | 10 +++- .../virginminuteschecker/IVMCScraper.java | 2 + .../ReferenceScraper.java | 34 +++++++++++++ .../virginminuteschecker/WebsiteScraper.java | 49 +++++++++++++++++++ 7 files changed, 128 insertions(+), 3 deletions(-) diff --git a/VirginMobileMinutesChecker/res/values/strings.xml b/VirginMobileMinutesChecker/res/values/strings.xml index be7fdb7..ce14199 100644 --- a/VirginMobileMinutesChecker/res/values/strings.xml +++ b/VirginMobileMinutesChecker/res/values/strings.xml @@ -67,5 +67,6 @@ 1.11 Updated in this version: \n\n - Fix issues due to VM\'s website updates. Thanks to everyone who let me know! \n\n This means you can go back and update your ratings to 5 stars again :) \n\n Many have asked if this app will be updated to reflect VM\'s data cap. I hope to do this, but we\'ll have wait until the cap is implemented before developing. \n\n For the Android developers out there: this app is open-source, so please feel free to fork on github! There are a lot of feature requests and plenty of opportunity to lend your expertise. + Data Used: diff --git a/VirginMobileMinutesChecker/src/com/baker/vm/PreferencesUtil.java b/VirginMobileMinutesChecker/src/com/baker/vm/PreferencesUtil.java index c010360..0af8112 100644 --- a/VirginMobileMinutesChecker/src/com/baker/vm/PreferencesUtil.java +++ b/VirginMobileMinutesChecker/src/com/baker/vm/PreferencesUtil.java @@ -29,6 +29,7 @@ private PreferencesUtil() public static final String CACHE_MINUTES_TOTAL = "cache_minutes_total"; public static final String CACHE_BALANCE = "cache_balance"; public static final String CACHE_DUE_DATE = "cache_due_date"; + public static final String CACHE_DATA = "cache_data"; /** Keys in auth preferences. */ public static final String USER_PREFIX = "USER"; @@ -93,6 +94,11 @@ public static String getDueDate(final Context context) return getCache(context).getString(CACHE_DUE_DATE, ""); } + public static String getData(final Context context) + { + return getCache(context).getString(CACHE_DATA, ""); + } + public static boolean getInboundCall(final Context context) { return getPrefs(context).getBoolean(SETTINGS_INBOUND_CALL, true); @@ -135,6 +141,7 @@ public static void clearCache(final Context context) editor.putString(CACHE_DUE_DATE, ""); editor.putString(CACHE_BALANCE, ""); + editor.putString(CACHE_DATA, ""); } public static void setCache(final Context activity, final VMAccount account) @@ -163,7 +170,9 @@ public static void setCache(final Context activity, final VMAccount account) editor.putString(CACHE_DUE_DATE, account.getChargedOn()); editor.putLong(CACHE_TS, System.currentTimeMillis()); - + + // Handle data + editor.putString(CACHE_DATA, account.getDataUsed() + " / " + account.getDataTotal()); editor.commit(); } diff --git a/VirginMobileMinutesChecker/src/com/baker/vm/VMAccount.java b/VirginMobileMinutesChecker/src/com/baker/vm/VMAccount.java index 05846d5..6985eaf 100644 --- a/VirginMobileMinutesChecker/src/com/baker/vm/VMAccount.java +++ b/VirginMobileMinutesChecker/src/com/baker/vm/VMAccount.java @@ -32,6 +32,8 @@ public static VMAccount createEmulatorAccount() ret.dueDate = "05/15/11"; ret.chargedOn = "05/15/11"; ret.minutesUsed = "400 / 1200"; + ret.dataUsed = "345.0"; + ret.dataTotal = "2560.0"; ret.isValid = true; return ret; @@ -47,6 +49,8 @@ public static VMAccount createTest(final UsernamePassword auth) ret.dueDate = "04/25/11"; ret.chargedOn = "04/25/11"; ret.minutesUsed = "650 / 1200"; + ret.dataUsed = "345.0"; + ret.dataTotal = "2560.0"; ret.isValid = true; return ret; @@ -62,6 +66,8 @@ public static VMAccount createTest() ret.dueDate = "04/31/11"; ret.chargedOn = "04/31/11"; ret.minutesUsed = "400 / 1200"; + ret.dataUsed = "345.0"; + ret.dataTotal = "2560.0"; ret.isValid = true; return ret; @@ -92,6 +98,8 @@ public VMAccount(final UsernamePassword iAuth, final String html, final IVMCScra dueDate = scraper.getDateDue(html); chargedOn = scraper.getChargedOn(html); minutesUsed = scraper.getMinutesUsed(html); + dataUsed = scraper.getDataUsed(html); + dataTotal = scraper.getDataTotal(html); } else { @@ -102,6 +110,8 @@ public VMAccount(final UsernamePassword iAuth, final String html, final IVMCScra dueDate = null; chargedOn = null; minutesUsed = null; + dataUsed = null; + dataTotal = null; } } @@ -116,6 +126,8 @@ private VMAccount(final UsernamePassword iAuth) dueDate = null; chargedOn = null; minutesUsed = null; + dataUsed = null; + dataTotal = null; } private final UsernamePassword auth; @@ -127,6 +139,8 @@ private VMAccount(final UsernamePassword iAuth) private String dueDate; private String chargedOn; private String minutesUsed; + private String dataUsed; + private String dataTotal; public boolean isValid() { @@ -205,6 +219,16 @@ public int getMinutesTotal() } return total; } + + public String getDataUsed() + { + return dataUsed; + } + + public String getDataTotal() + { + return dataTotal; + } public UsernamePassword getAuth() { diff --git a/VirginMobileMinutesChecker/src/com/baker/vm/ui/MultipleAccountsActivity.java b/VirginMobileMinutesChecker/src/com/baker/vm/ui/MultipleAccountsActivity.java index 90a1e89..d8a6393 100644 --- a/VirginMobileMinutesChecker/src/com/baker/vm/ui/MultipleAccountsActivity.java +++ b/VirginMobileMinutesChecker/src/com/baker/vm/ui/MultipleAccountsActivity.java @@ -237,19 +237,23 @@ public void updateLayout(final UsernamePassword auth) final int widest = getMaxWidth(R.string.currentBalance, R.string.minutesUsed, R.string.chargedOn, - R.string.monthlyCharge); + R.string.monthlyCharge, + R.string.dataUsed); String balance = ""; String minutes = ""; String dueDate = ""; + String data = ""; if (getUsersTelephoneNumber().equals(auth.user)) { balance = PreferencesUtil.getBalance(this); minutes = PreferencesUtil.getMinutesString(this); dueDate = PreferencesUtil.getDueDate(this); + data = PreferencesUtil.getData(this); } addRow(table, R.string.currentBalance, balance, widest, true); addRow(table, R.string.minutesUsed, minutes, widest, true); + addRow(table, R.string.dataUsed, data, widest, true); addRow(table, R.string.chargedOn, dueDate, widest, true); addRow(table, R.string.monthlyCharge, "", widest, true); } @@ -292,9 +296,11 @@ public void updateLayout(final VMAccount acct) final int widest = getMaxWidth(R.string.currentBalance, R.string.minutesUsed, R.string.chargedOn, - R.string.monthlyCharge); + R.string.monthlyCharge, + R.string.dataUsed); addRow(table, R.string.currentBalance, acct.getBalance(), widest, false); addRow(table, R.string.minutesUsed, acct.getMinutesUsed(), widest, false); + addRow(table, R.string.dataUsed, acct.getDataUsed() + " / " + acct.getDataTotal(), widest, false); addRow(table, R.string.chargedOn, acct.getChargedOn(), widest, false); addRow(table, R.string.monthlyCharge, acct.getMonthlyCharge(), widest, false); diff --git a/VirginMobileMinutesChecker/src/com/jaygoel/virginminuteschecker/IVMCScraper.java b/VirginMobileMinutesChecker/src/com/jaygoel/virginminuteschecker/IVMCScraper.java index 8366fa2..45c1d62 100644 --- a/VirginMobileMinutesChecker/src/com/jaygoel/virginminuteschecker/IVMCScraper.java +++ b/VirginMobileMinutesChecker/src/com/jaygoel/virginminuteschecker/IVMCScraper.java @@ -12,4 +12,6 @@ public interface IVMCScraper { String getDateDue(String str); String getChargedOn(String str); String getMinutesUsed(String str); + String getDataUsed(String str); + String getDataTotal(String str); } \ No newline at end of file diff --git a/VirginMobileMinutesChecker/src/com/jaygoel/virginminuteschecker/ReferenceScraper.java b/VirginMobileMinutesChecker/src/com/jaygoel/virginminuteschecker/ReferenceScraper.java index ee514aa..bb390fd 100644 --- a/VirginMobileMinutesChecker/src/com/jaygoel/virginminuteschecker/ReferenceScraper.java +++ b/VirginMobileMinutesChecker/src/com/jaygoel/virginminuteschecker/ReferenceScraper.java @@ -114,5 +114,39 @@ public String getMinutesUsed(final String str) return str.substring(start + srch.length(), end).replaceFirst( "", ""); } + + @Override + public String getDataUsed(final String str) + { + String srch = "MB Used: "; + int start = str.indexOf(srch); + int end = str.indexOf(" MB", start); + + if((start > 0) && (end > 0)) + { + return str.substring(start + srch.length(), end); + } + else + { + return null; + } + } + + @Override + public String getDataTotal(final String str) + { + String srch = "Data speeds may be reduced at "; + int start = str.indexOf(srch); + int end = str.indexOf(" MB", start); + + if((start > 0) && (end > 0)) + { + return str.substring(start + srch.length(), end); + } + else + { + return null; + } + } } diff --git a/VirginMobileMinutesChecker/src/com/jaygoel/virginminuteschecker/WebsiteScraper.java b/VirginMobileMinutesChecker/src/com/jaygoel/virginminuteschecker/WebsiteScraper.java index 1d9d986..5de4cef 100644 --- a/VirginMobileMinutesChecker/src/com/jaygoel/virginminuteschecker/WebsiteScraper.java +++ b/VirginMobileMinutesChecker/src/com/jaygoel/virginminuteschecker/WebsiteScraper.java @@ -90,6 +90,55 @@ public void checkServerTrusted( } connection.disconnect(); + + // Now, try to grab data usage + String cookies = ""; + // 1. Grab and store cookies + String headerName=null; + for (int i=1; (headerName = connection.getHeaderFieldKey(i))!=null; i++) { + if (headerName.equals("Set-Cookie")) { + String cookie = connection.getHeaderField(i); + cookie = cookie.substring(0, cookie.indexOf(";")); + String cookieName = cookie.substring(0, cookie.indexOf("=")); + String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length()); + cookies = cookies + cookieName + "=" + cookieValue + "; "; + } + } + + // 2. Grab the next page + connection = (HttpsURLConnection) new URL("https://www1.virginmobileusa.com/myaccount/dataPlanHistory.do").openConnection(); + ((HttpsURLConnection) connection).setHostnameVerifier(new AllowAllHostnameVerifier()); + + //connection.setFollowRedirects(true); + connection.setDoOutput(true); + connection.setRequestProperty("Cookie", cookies); + + //connection.connect(); + + in = new InputStreamReader((InputStream) connection.getContent()); + + buff = new BufferedReader(in); + + sb = new StringBuilder(); + + String dataPage; + while ((dataPage = buff.readLine()) != null) { + sb.append(dataPage); + } + + int dataContentIndex = sb.indexOf("id=\"mainContent\""); + if (dataContentIndex == -1) { + dataPage = ""; + } else { + dataPage = sb.substring(dataContentIndex); + } + + // Simply concat the output with our data page output + if(line != null) { + line = line + dataPage; + } + + connection.disconnect(); } catch (Exception e) { e.printStackTrace(); //System.err.println("exception 83");