Skip to content

Commit

Permalink
preparing for update
Browse files Browse the repository at this point in the history
  • Loading branch information
patil215 committed Feb 2, 2014
1 parent 55b99f7 commit 93a5835
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 31 deletions.
4 changes: 2 additions & 2 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.patil.quickhac"
android:versionCode="1"
android:versionName="1.0" >
android:versionCode="2"
android:versionName="1.1" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Expand Down
32 changes: 30 additions & 2 deletions src/com/patil/quickhac/CourseSaver.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public void saveCourses(Course[] courses, String username, String id) {
editor.commit();
}

public void eraseCourses(String username, String id) {
String fileName = username + "%" + id;
SharedPreferences prefs = context.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.remove("savedCourses");
editor.remove("lastUpdated");
editor.commit();
}

/*
* Saves GPA value
*/
Expand Down Expand Up @@ -78,6 +88,24 @@ public double getUnweightedGPA(String username, String id) {
float gpa = prefs.getFloat("unweightedGPA", 0);
return (double) gpa;
}

public void eraseWeightedGPA(String username, String id) {
String fileName = username + "%" + id;
SharedPreferences prefs = context.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.remove("weightedGPA");
editor.commit();
}

public void eraseUnweightedGPA(String username, String id) {
String fileName = username + "%" + id;
SharedPreferences prefs = context.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.remove("unweightedGPA");
editor.commit();
}

/*
* Returns the cached courses
Expand Down Expand Up @@ -108,14 +136,14 @@ public void saveLatestResponse(String response, String username, String id) {
editor.commit();
Log.d("BackgroundGrades", "saved response ");
}

public String getLatestResponse(String username, String id) {
String fileName = username + "%" + id;
SharedPreferences prefs = context.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
String savedResponse = prefs.getString("savedResponse", null);
Log.d("BackgroundGrades", "got saved response ");
if(savedResponse == null) {
if (savedResponse == null) {
Log.d("BackgroundGrades", "saved response is null");
}
return savedResponse;
Expand Down
85 changes: 62 additions & 23 deletions src/com/patil/quickhac/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.AsyncTask;
Expand Down Expand Up @@ -135,7 +137,6 @@ protected void onCreate(Bundle savedInstanceState) {
}
loggedIn = false;
utils = new Utils(this);
settingsManager = new SettingsManager(this);
colorGenerator = new ColorGenerator(this);
saver = new CourseSaver(this);
currentTitle = "Overview";
Expand All @@ -148,6 +149,10 @@ protected void onCreate(Bundle savedInstanceState) {
makeDrawer();
}

public void handleUpdate() {

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
Expand Down Expand Up @@ -263,6 +268,39 @@ public boolean onPrepareOptionsMenu(Menu menu) {
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
this.menu = menu;
settingsManager = new SettingsManager(this);

Log.d("JustUpdate", "Handling update");
// Erase credentials if just updated to avoid weird data things
if (settingsManager.justUpdated()) {
Log.d("JustUpdate", "Just updated so wiping things");
studentList = settingsManager.getStudentList();
if (studentList != null) {
// Erase student info and saved grades
for (int i = 0; i < studentList.length; i++) {
String user = studentList[i].split("%")[0];
String id = studentList[i].split("%")[1];
settingsManager.eraseCredentials(user, id);
saver.eraseCourses(user, id);
saver.eraseWeightedGPA(user, id);
saver.eraseUnweightedGPA(user, id);
}
// Erase student list
settingsManager.eraseStudentList();
}
// Save the new current version
PackageInfo pInfo = null;
try {
pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (pInfo != null) {
settingsManager.saveCurrentVersion(pInfo.versionCode);
}
}

if (!alreadyLoadedGrades) {
startDisplayingGrades();
alreadyLoadedGrades = true;
Expand Down Expand Up @@ -298,12 +336,9 @@ protected void onPostCreate(Bundle savedInstanceState) {
public void startDisplayingGrades() {
String selectedStudent = settingsManager.getSelectedStudent();
String[] students = settingsManager.getStudentList();
if (students != null) {
if (selectedStudent != null && students != null) {
studentList = students;
} else {
studentList = new String[0];
}
if (selectedStudent != null) {
Log.d("JustUpdated", "students aren't null wtf");
// We have a student to load
String[] credentials = settingsManager
.getLoginInfo(selectedStudent);
Expand All @@ -328,7 +363,8 @@ public void startDisplayingGrades() {
credentials[2]);
// check if it's been less than 30 minutes since grades
// updated
if (timeSinceLastUpdated < Constants.GRADE_LENGTH && !startedFromRefresh) {
if (timeSinceLastUpdated < Constants.GRADE_LENGTH
&& !startedFromRefresh) {
// Grades updated less than 30 min ago and we aren't
// trying to refresh, don't bother
// getting new grades
Expand Down Expand Up @@ -372,34 +408,37 @@ public void startDisplayingGrades() {
selectedStudent.split("%")[1]);
restartActivity();
}
makeStudentSpinner();
} else {
currentUsername = "";
currentId = "";
currentDistrict = "";
signInButton.setVisibility(View.VISIBLE);
startLogin();
}
makeStudentSpinner();
}

@Override
protected void onResume() {
super.onResume();
long lastLogin = settingsManager.getLastLogin(currentUsername,
currentId);
if (System.currentTimeMillis() - lastLogin > Constants.LOGIN_TIMEOUT) {
Log.d("Resuming",
"it's been a while"
+ String.valueOf(System.currentTimeMillis()
- lastLogin));
// Since the login should have timed out, set loggedin to false so
// that it'll log in again to avoid timeout issues
loggedIn = false;
} else {
Log.d("Resuming",
"it's been not that long"
+ String.valueOf(System.currentTimeMillis()
- lastLogin));
if (settingsManager != null) {
long lastLogin = settingsManager.getLastLogin(currentUsername,
currentId);
if (System.currentTimeMillis() - lastLogin > Constants.LOGIN_TIMEOUT) {
Log.d("Resuming",
"it's been a while"
+ String.valueOf(System.currentTimeMillis()
- lastLogin));
// Since the login should have timed out, set loggedin to false
// so
// that it'll log in again to avoid timeout issues
loggedIn = false;
} else {
Log.d("Resuming",
"it's been not that long"
+ String.valueOf(System.currentTimeMillis()
- lastLogin));
}
}
}

Expand Down
48 changes: 44 additions & 4 deletions src/com/patil/quickhac/SettingsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.preference.PreferenceManager;

public class SettingsManager {
Expand Down Expand Up @@ -76,10 +78,19 @@ public String[] getStudentList() {
if (studentListSet != null) {
studentList = studentListSet.toArray(new String[0]);
} else {
studentList = new String[0];
studentList = null;
}
return studentList;
}

/*
* Erases student list
*/
public void eraseStudentList() {
Editor edit = defaultPrefs.edit();
edit.remove("studentList");
edit.commit();
}

/*
* Saves a studentlist
Expand Down Expand Up @@ -166,7 +177,7 @@ public void removeStudent(String username, String id) {
}
}

private void eraseCredentials(String username, String id) {
public void eraseCredentials(String username, String id) {
String fileName = username + "%" + id;
SharedPreferences prefs = context.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
Expand All @@ -177,7 +188,36 @@ private void eraseCredentials(String username, String id) {
edit.putString("district", "");
edit.commit();
}


public boolean justUpdated() {
// Get the current version of the app
PackageInfo pInfo = null;
try {
pInfo = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (pInfo != null) {
int currentVersion = pInfo.versionCode;
int savedVersion = defaultPrefs.getInt("savedVersion", 0);
// If the version saved is different from the version we currently
// have
if (savedVersion != currentVersion) {
// we just updated
return true;
}
}
return false;
}

public void saveCurrentVersion(int version) {
Editor edit = defaultPrefs.edit();
edit.putInt("savedVersion", version);
edit.commit();
}

public void saveLastLogin(String username, String id, long millis) {
String fileName = username + "%" + id;
SharedPreferences prefs = context.getSharedPreferences(fileName,
Expand All @@ -186,7 +226,7 @@ public void saveLastLogin(String username, String id, long millis) {
edit.putLong("lastLogin", millis);
edit.commit();
}

public long getLastLogin(String username, String id) {
String fileName = username + "%" + id;
SharedPreferences prefs = context.getSharedPreferences(fileName,
Expand Down

0 comments on commit 93a5835

Please sign in to comment.