Skip to content

Commit

Permalink
Guard against null pointer cursor when retrieving contacts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristi Libotean committed Oct 13, 2018
1 parent ea3ad8b commit b2111b3
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions app/src/main/java/com/eblis/whenwasit/utils/ContactsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,30 +142,37 @@ private List<Person> getAllContactsWithBirthdays(ContentResolver contentResolver

Cursor cursor = getContactsCursor(contentResolver);

while (cursor.moveToNext()) {
final String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Event.CONTACT_ID));
final String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
final String dateString = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
final int type = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE));
final String label = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Event.LABEL));
final String anniversary = getTypeLabel(context.getResources(), type, label);

final long date;
if (cursor != null) {
try {
date = Utils.formatDateToLong(dateString);
} catch (Exception e) {
continue;
while (cursor.moveToNext()) {
final String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Event.CONTACT_ID));
final String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
final String dateString = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
final int type = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE));
final String label = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Event.LABEL));
final String anniversary = getTypeLabel(context.getResources(), type, label);

final long date;
try {
date = Utils.formatDateToLong(dateString);
} catch (Exception e) {
continue;
}
if (date == 0) continue;

boolean yearUnknown = Utils.isYearUnknown(dateString);
String phoneNumber = getContactPhoneNumber(contentResolver, id);
String email = getContactEmail(contentResolver, id);

Person person = new Person(Long.valueOf(id), name, date, yearUnknown, phoneNumber, email, anniversary, getAnniversaryType(type));
contacts.add(person);
}
}
finally {
cursor.close();
}
if (date == 0) continue;

boolean yearUnknown = Utils.isYearUnknown(dateString);
String phoneNumber = getContactPhoneNumber(contentResolver, id);
String email = getContactEmail(contentResolver, id);

Person person = new Person(Long.valueOf(id), name, date, yearUnknown, phoneNumber, email, anniversary, getAnniversaryType(type));
contacts.add(person);
}
cursor.close();

return contacts;
}

Expand Down

0 comments on commit b2111b3

Please sign in to comment.