Skip to content

Commit

Permalink
Attempt to guard against native errors when retrieving phone numbers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristi Libotean committed Oct 13, 2018
1 parent b2111b3 commit 29ca6c9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ android {
applicationId "com.eblis.whenwasit"
minSdkVersion 16
targetSdkVersion 27
versionCode 29
versionCode 30
versionName "1.0.${versionCode}"
resConfigs "auto"
vectorDrawables.useSupportLibrary = true
Expand Down
29 changes: 20 additions & 9 deletions app/src/main/java/com/eblis/whenwasit/utils/ContactsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,32 @@ public String getContactName(ContentResolver contentResolver, String id) {
/**
* Returns phone number from certain contact
*/
public String getContactPhoneNumber(ContentResolver contentResolver, String id) {
String phoneNumber = "";
Cursor phoneCursor = contentResolver.query(
public synchronized String getContactPhoneNumber(ContentResolver contentResolver, String id) {
final Cursor phoneCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
if (phoneCursor != null && phoneCursor.moveToFirst()) {
phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
if (phoneCursor != null) {
phoneCursor.close();
try {
phoneCursor.moveToFirst();
return phoneCursor.getString(phoneCursor.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
catch (Exception ex) {
//no-op
}
finally {
try {
phoneCursor.close();
}
catch (Exception ex) {
//nothing we can do :(
}
}
}
return phoneNumber;

return "";
}

/**
Expand Down

0 comments on commit 29ca6c9

Please sign in to comment.