Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Silence
Silence-Android
Commits
0caf1f8e
Commit
0caf1f8e
authored
May 21, 2016
by
Bastien Le Querrec
Browse files
fix NPE if contacts database is not available
parent
ebfcbc5a
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/org/smssecure/smssecure/contacts/ContactsCursorLoader.java
View file @
0caf1f8e
...
...
@@ -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
;
}
}
}
src/org/smssecure/smssecure/contacts/ContactsDatabase.java
View file @
0caf1f8e
...
...
@@ -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
@N
onN
ull
Cursor
querySystemContacts
(
String
filter
)
{
public
@Null
able
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
@N
onN
ull
Cursor
querySilenceContacts
(
String
filter
)
{
public
@Null
able
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
,
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment