Commit 0caf1f8e authored by Bastien Le Querrec's avatar Bastien Le Querrec

fix NPE if contacts database is not available

parent ebfcbc5a
......@@ -18,6 +18,7 @@ package org.smssecure.smssecure.contacts;
import android.content.Context;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.database.MergeCursor;
import android.support.v4.content.CursorLoader;
import android.text.TextUtils;
......@@ -50,18 +51,24 @@ public class ContactsCursorLoader extends CursorLoader {
@Override
public Cursor loadInBackground() {
ContactsDatabase contactsDatabase = DatabaseFactory.getContactsDatabase(getContext());
ArrayList<Cursor> cursorList = new ArrayList<>(3);
ArrayList<Cursor> cursorList = new ArrayList<>();
cursorList.add(contactsDatabase.querySilenceContacts(filter));
Cursor silenceContacts = contactsDatabase.querySilenceContacts(filter);
if (silenceContacts != null) cursorList.add(silenceContacts);
if (includeSmsContacts) {
cursorList.add(contactsDatabase.querySystemContacts(filter));
Cursor systemContacts = contactsDatabase.querySystemContacts(filter);
if (systemContacts != null) cursorList.add(systemContacts);
}
if (!TextUtils.isEmpty(filter) && NumberUtil.isValidSmsOrEmail(filter)) {
cursorList.add(contactsDatabase.getNewNumberCursor(filter));
}
return new MergeCursor(cursorList.toArray(new Cursor[0]));
if (cursorList.size() > 0) {
return new MergeCursor(cursorList.toArray(new Cursor[0]));
} else {
return null;
}
}
}
......@@ -23,10 +23,10 @@ import android.database.MatrixCursor;
import android.net.Uri;
import android.os.Build;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Pair;
import android.util.Log;
import org.smssecure.smssecure.R;
......@@ -61,7 +61,7 @@ public class ContactsDatabase {
this.context = context;
}
public @NonNull Cursor querySystemContacts(String filter) {
public @Nullable Cursor querySystemContacts(String filter) {
Uri uri;
if (!TextUtils.isEmpty(filter)) {
......@@ -90,17 +90,28 @@ public class ContactsDatabase {
put(LABEL_COLUMN, ContactsContract.CommonDataKinds.Phone.LABEL);
}};
Cursor cursor = context.getContentResolver().query(uri, projection,
ContactsContract.Data.SYNC2 + " IS NULL OR " +
ContactsContract.Data.SYNC2 + " != ?",
new String[] {"__TS"},
sort);
Cursor cursor;
try {
cursor = context.getContentResolver().query(uri, projection,
ContactsContract.Data.SYNC2 + " IS NULL OR " +
ContactsContract.Data.SYNC2 + " != ?",
new String[] {"__TS"},
sort);
} catch (NullPointerException npe) {
/*
* On a few phone (+ Sailfish OS 2.0), this throws a NPE. We just
* catch it and return a blank result.
*/
Log.w(TAG, npe);
return null;
}
return new ProjectionMappingCursor(cursor, projectionMap,
new Pair<String, Object>(CONTACT_TYPE_COLUMN, NORMAL_TYPE));
}
public @NonNull Cursor querySilenceContacts(String filter) {
public @Nullable Cursor querySilenceContacts(String filter) {
String[] projection = new String[] {ContactsContract.Data._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Data.DATA1};
......@@ -115,19 +126,28 @@ public class ContactsDatabase {
Cursor cursor;
if (TextUtils.isEmpty(filter)) {
cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
projection,
ContactsContract.Data.MIMETYPE + " = ?",
new String[] {"vnd.android.cursor.item/vnd.org.smssecure.smssecure.contact"},
sort);
} else {
cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
projection,
ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?",
new String[] {"vnd.android.cursor.item/vnd.org.smssecure.smssecure.contact",
"%" + filter + "%"},
sort);
try {
if (TextUtils.isEmpty(filter)) {
cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
projection,
ContactsContract.Data.MIMETYPE + " = ?",
new String[] {"vnd.android.cursor.item/vnd.org.smssecure.smssecure.contact"},
sort);
} else {
cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
projection,
ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?",
new String[] {"vnd.android.cursor.item/vnd.org.smssecure.smssecure.contact",
"%" + filter + "%"},
sort);
}
} catch (NullPointerException npe) {
/*
* On a few phone (+ Sailfish OS 2.0), this throws a NPE. We just
* catch it and return an empty result.
*/
Log.w(TAG, npe);
return null;
}
return new ProjectionMappingCursor(cursor, projectionMap,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment