Skip to content

Commit

Permalink
Fixed crash when importing from XML with malformed datas
Browse files Browse the repository at this point in the history
  • Loading branch information
lubenard committed Jun 6, 2021
1 parent 66ac169 commit a00c539
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
40 changes: 25 additions & 15 deletions app/src/main/java/com/lubenard/oring_reminder/BackupRestore.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,32 +289,42 @@ private void restoreDatasFromXml(InputStream inputStream) {
XmlPullParser myParser = xmlFactoryObject.newPullParser();
DbManager dbManager = MainActivity.getDbManager();
int isRunning;
long lastEntryInsertedId = 0;
long lastEntryInsertedId = -1;

myParser.setInput(inputStream, null);

// Skip the first element
int eventType = myParser.next();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG && myParser.getName().equals("session")) {
isRunning = myParser.getAttributeValue(null, "dateTimeRemoved").equals("NOT SET YET") ? 1 : 0;
lastEntryInsertedId = dbManager.createNewDatesRing(myParser.getAttributeValue(null, "dateTimePut"), myParser.getAttributeValue(null, "dateTimeRemoved"), isRunning);
if (isRunning == 1) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Calendar calendar = Calendar.getInstance();
calendar.setTime(Utils.getdateParsed(myParser.getAttributeValue(null, "dateTimePut")));
calendar.add(Calendar.HOUR_OF_DAY, Integer.parseInt(preferences.getString("myring_wearing_time", "15")));
EditEntryFragment.setAlarm(this, Utils.getdateFormatted(calendar.getTime()) , lastEntryInsertedId, true);
// Check if we have the minimum infos about the session to recreate
if (myParser.getAttributeValue(null, "dateTimePut") != null && myParser.getAttributeValue(null, "dateTimeRemoved") != null
&& Utils.checkDateInputSanity(myParser.getAttributeValue(null, "dateTimePut")) == 1) {
isRunning = myParser.getAttributeValue(null, "dateTimeRemoved").equals("NOT SET YET") ? 1 : 0;
lastEntryInsertedId = dbManager.createNewDatesRing(myParser.getAttributeValue(null, "dateTimePut"), myParser.getAttributeValue(null, "dateTimeRemoved"), isRunning);
if (isRunning == 1) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Calendar calendar = Calendar.getInstance();
calendar.setTime(Utils.getdateParsed(myParser.getAttributeValue(null, "dateTimePut")));
calendar.add(Calendar.HOUR_OF_DAY, Integer.parseInt(preferences.getString("myring_wearing_time", "15")));
EditEntryFragment.setAlarm(this, Utils.getdateFormatted(calendar.getTime()), lastEntryInsertedId, true);
}
} else {
Toast.makeText(this, R.string.bad_import_date_xml, Toast.LENGTH_SHORT).show();
lastEntryInsertedId = -1;
}
}
if (eventType == XmlPullParser.START_TAG && myParser.getName().equals("pause")) {
Log.d(TAG, "Restauring pause for entryId: " + lastEntryInsertedId);
isRunning = myParser.getAttributeValue(null, "dateTimeRemoved").equals("NOT SET YET") ? 1 : 0;
if (lastEntryInsertedId != 0) {
dbManager.createNewPause(lastEntryInsertedId, myParser.getAttributeValue(null, "dateTimeRemoved"), myParser.getAttributeValue(null, "dateTimePut"), isRunning);
if (isRunning == 1)
EditEntryFragment.cancelAlarm(this, lastEntryInsertedId);
}
if (myParser.getAttributeValue(null, "dateTimeRemoved") != null && myParser.getAttributeValue(null, "dateTimePut") != null && lastEntryInsertedId != -1) {
isRunning = myParser.getAttributeValue(null, "dateTimePut").equals("NOT SET YET") ? 1 : 0;
if (lastEntryInsertedId != 0) {
dbManager.createNewPause(lastEntryInsertedId, myParser.getAttributeValue(null, "dateTimeRemoved"), myParser.getAttributeValue(null, "dateTimePut"), isRunning);
if (isRunning == 1)
EditEntryFragment.cancelAlarm(this, lastEntryInsertedId);
}
} else
Toast.makeText(this, R.string.bad_import_date_pause_xml ,Toast.LENGTH_SHORT).show();
}
eventType = myParser.next();
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,6 @@
<string name="custom_useful_links">Liens utils :</string>
<string name="settings_other_useful_links_description">Une poignée de liens utils !</string>
<string name="number_of_days_between_entries">Nombre de jours entre la prenière et dernière entrée :</string>
<string name="bad_import_date_xml">La date malformée d\'une session à été trouvée dans la sauvegarde. La session ne sera pas importée</string>
<string name="bad_import_date_pause_xml">La date d\'une pause malformée à été trouvée dans la sauvegarde. La session ne sera pas importée</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,6 @@
<string name="settings_other_useful_links_description">A bunch of useful links!</string>
<string name="custom_useful_links">Useful links</string>
<string name="number_of_days_between_entries">Number of days between the first and last entry:</string>
<string name="bad_import_date_xml">A malformed date for a session has been found in the save. It will not be imported</string>
<string name="bad_import_date_pause_xml">A malformed date for a break has been found in the save. It will not be imported</string>
</resources>

0 comments on commit a00c539

Please sign in to comment.