Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data Usage Implementation #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions VirginMobileMinutesChecker/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@

<string name="currentVersion">1.11</string>
<string name="currentVersionSummary">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.</string>
<string name="dataUsed">Data Used:</string>

</resources>
11 changes: 10 additions & 1 deletion VirginMobileMinutesChecker/src/com/baker/vm/PreferencesUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
}
Expand Down
24 changes: 24 additions & 0 deletions VirginMobileMinutesChecker/src/com/baker/vm/VMAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
{
Expand All @@ -102,6 +110,8 @@ public VMAccount(final UsernamePassword iAuth, final String html, final IVMCScra
dueDate = null;
chargedOn = null;
minutesUsed = null;
dataUsed = null;
dataTotal = null;
}
}

Expand All @@ -116,6 +126,8 @@ private VMAccount(final UsernamePassword iAuth)
dueDate = null;
chargedOn = null;
minutesUsed = null;
dataUsed = null;
dataTotal = null;
}

private final UsernamePassword auth;
Expand All @@ -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()
{
Expand Down Expand Up @@ -205,6 +219,16 @@ public int getMinutesTotal()
}
return total;
}

public String getDataUsed()
{
return dataUsed;
}

public String getDataTotal()
{
return dataTotal;
}

public UsernamePassword getAuth()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,39 @@ public String getMinutesUsed(final String str)
return str.substring(start + srch.length(), end).replaceFirst(
"</strong>", "");
}

@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;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down