Fixed NullPointer exceptions when cleaning notifications and courses on

preferences change
This commit is contained in:
Juan Miguel Boyero Corral 2013-03-03 18:27:30 +01:00
parent 7a310f220f
commit 673e7ae08b
4 changed files with 33 additions and 11 deletions

View File

@ -66,6 +66,7 @@
<Preference
android:defaultValue=""
android:summary="@string/author_preferences"
android:key="aboutPref"
android:title="@string/organization_preferences" />
</PreferenceCategory>

View File

@ -36,8 +36,8 @@ import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import es.ugr.swad.swadroid.modules.Courses;
import es.ugr.swad.swadroid.modules.notifications.Notifications;
import android.util.Log;
import es.ugr.swad.swadroid.model.DataBaseHelper;
import es.ugr.swad.swadroid.utils.Base64;
/**
@ -53,6 +53,10 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang
* User identifier.
*/
private String userID;
/**
* Database Helper.
*/
private static DataBaseHelper dbHelper;
/**
* User identifier preference name.
*/
@ -316,6 +320,16 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang
userIDPref.setSummary(prefs.getString(USERIDPREF, ""));
userPasswordPref.setSummary(stars);
}
/**
* Deletes notifications and courses data from database
*/
private void cleanDatabase() {
dbHelper.emptyTable(Global.DB_TABLE_NOTIFICATIONS);
dbHelper.emptyTable(Global.DB_TABLE_COURSES);
setLastCourseSelected(0);
Global.setDbCleaned(true);
}
/**
* Initializes preferences of activity.
@ -342,6 +356,17 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang
//Restore preferences
addPreferencesFromResource(R.xml.preferences);
prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
//Initialize database
try {
dbHelper = new DataBaseHelper(this);
} catch (Exception ex) {
Log.e(ex.getClass().getSimpleName(), ex.getMessage());
ex.printStackTrace();
//Send exception details to Bugsense
BugSenseHandler.sendException(ex);
}
userID = prefs.getString(USERIDPREF, "");
userPassword = prefs.getString(USERPASSWORDPREF, "");
@ -507,14 +532,11 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang
*/
public boolean onPreferenceChange(Preference preference, Object newValue) {
String key = preference.getKey();
Notifications n = new Notifications();
Courses c = new Courses();
//If preferences have changed, logout and save new preferences
if (USERIDPREF.equals(key) || USERPASSWORDPREF.equals(key)) {
Global.setLogged(false);
n.clearNotifications(this);
c.clearCourses(this);
cleanDatabase();
Global.setPreferencesChanged();
editor.commit();
}
@ -529,9 +551,8 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang
if (SERVERPREF.equals(key)) {
Global.setLogged(false);
n.clearNotifications(this);
c.clearCourses(this);
Global.setPreferencesChanged();
cleanDatabase();
Global.setPreferencesChanged();
editor.commit();
}

View File

@ -92,7 +92,7 @@ public class MenuActivity extends Activity {
}
/**
* Deletes notifications and tests data from database
* Deletes all data from database
*/
protected void cleanDatabase() {
dbHelper.cleanTables();

View File

@ -93,7 +93,7 @@ public class MenuExpandableListActivity extends ExpandableListActivity {
}
/**
* Deletes notifications and tests data from database
* Deletes all data from database
*/
protected void cleanDatabase() {
dbHelper.cleanTables();