Android: loading contacts with CursorLoader, not all being loaded - android

My android app is supposed to load all contacts recorded in the device and display them in a list. I haven't been able to figure out why, but not all contacts are being loaded.
Here is the code I'm using to start the query with a CursorLoader:
public android.support.v4.content.Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (id == ContactsQuery.QUERY_ID) {
Uri contentUri;
if (mSearchTerm == null) {
contentUri = ContactsQuery.CONTENT_URI;
} else {
contentUri =
Uri.withAppendedPath(ContactsQuery.FILTER_URI, Uri.encode(mSearchTerm));
}
return new CursorLoader(getActivity(),
contentUri,
ContactsQuery.PROJECTION,
ContactsQuery.SELECTION,
null,
ContactsQuery.SORT_ORDER);
ContactsQuery is defined as follows:
public interface ContactsQuery {
final static int QUERY_ID = 1;
final static Uri CONTENT_URI = Contacts.CONTENT_URI;
final static Uri FILTER_URI = Contacts.CONTENT_FILTER_URI;
#SuppressLint("InlinedApi")
final static String SELECTION =
(Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME) +
"<>''" + " AND " + Contacts.IN_VISIBLE_GROUP + "=1";
#SuppressLint("InlinedApi")
final static String SORT_ORDER =
Utils.hasHoneycomb() ? Contacts.SORT_KEY_PRIMARY : Contacts.DISPLAY_NAME;
#SuppressLint("InlinedApi")
final static String[] PROJECTION = {
// The contact's row id
Contacts._ID,
Contacts.LOOKUP_KEY,
Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME,
Utils.hasHoneycomb() ? Contacts.PHOTO_THUMBNAIL_URI : Contacts._ID,
SORT_ORDER,
};
final static int ID = 0;
final static int LOOKUP_KEY = 1;
final static int DISPLAY_NAME = 2;
final static int PHOTO_THUMBNAIL_DATA = 3;
final static int SORT_KEY = 4;
}
Why aren't all contacts being loaded when mSearchTerm is null?

You are only pulling down contents of the phone's contacts - the contacts that are stored on the SIM may or may not be accessible due to the phone's settings (for example if the phone has disabled access to SIM contacts).
Here is a post that will show you how to read them separately.
You can potentially discern between the two using the following:
//for SIM Card
ContentValues values = new ContentValues();
values.put(RawContacts.ACCOUNT_TYPE, "com.android.contacts.sim");
values.put(RawContacts.ACCOUNT_NAME, "SIM");
Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
//for everyone else
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.DISPLAY_NAME, "Name");

The problem was in the selection. The offending line is
final static String SELECTION =
(Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME) +
"<>''" + " AND " + Contacts.IN_VISIBLE_GROUP + "=1";
The missing contacts had Contacts.IN_VISIBLE_GROUP <> 1. This behavior seems a bit pathological because it seems that perfectly valid contacts that are displayed in the contact list for the built in apps, such as my mother, are not in the visible group. I implemented a kludge:
final static String SELECTION =
(Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME) +
"<>'' AND ("+Contacts.HAS_PHONE_NUMBER+"=1 OR "+Contacts.IN_VISIBLE_GROUP+" = 1)";
This is sufficient for my app.

Related

Get events from public google calendar Android

I would like to have a calendar view that would present all of the events of a public google calendar.
I have an OAuth 2.0 client ID for my user and my calendar is set to PUBLIC.
Then I went trough the Calendar Provider tutorial (https://developer.android.com/guide/topics/providers/calendar-provider.html) and use this code:
public class MainActivity extends Activity {
// Projection array. Creating indices for this array instead of doing
// dynamic lookups improves performance.
public static final String[] EVENT_PROJECTION = new String[]{
CalendarContract.Calendars._ID, // 0
CalendarContract.Calendars.ACCOUNT_NAME, // 1
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, // 2
CalendarContract.Calendars.OWNER_ACCOUNT // 3
};
// The indices for the projection array above.
private static final int PROJECTION_ID_INDEX = 0;
private static final int PROJECTION_ACCOUNT_NAME_INDEX = 1;
private static final int PROJECTION_DISPLAY_NAME_INDEX = 2;
private static final int PROJECTION_OWNER_ACCOUNT_INDEX = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Run query
Cursor cur = null;
ContentResolver cr = getContentResolver();
Uri uri = CalendarContract.Calendars.CONTENT_URI;
String selection = "((" + CalendarContract.Calendars.ACCOUNT_NAME + " = ?) AND ("
+ CalendarContract.Calendars.ACCOUNT_TYPE + " = ?) AND ("
+ CalendarContract.Calendars.OWNER_ACCOUNT + " = ?))";
String[] selectionArgs = new String[]{"****#gmail.com", "google.com",
"****#gmail.com"};
// Submit the query and get a Cursor object back.
cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs, null);
// Use the cursor to step through the returned records
while (cur.moveToNext()) {
long calID = 0;
String displayName = null;
String accountName = null;
String ownerName = null;
// Get the field values
calID = cur.getLong(PROJECTION_ID_INDEX);
displayName = cur.getString(PROJECTION_DISPLAY_NAME_INDEX);
accountName = cur.getString(PROJECTION_ACCOUNT_NAME_INDEX);
ownerName = cur.getString(PROJECTION_OWNER_ACCOUNT_INDEX);
}
}
}
Unfortunately, the Cursor stayed empty. I was also trying to define the ACCOUNT_TYPE to "LOCAL", but it didn't work.
Target SDK set to 21, and i have all the necessary permissions.
How can I retrieve the events of my calendar? Please help /:

How to search Contacts with address (FORMATTED_ADDRESS) with one query?

I try to implement a live search over the users contacts, and I want to get the name, thumbnail and address (if there is one) of each matching contact.
The live search is running while the user is typing.
So he types ma and will get 'martin', 'matthews'...
He'll continue with mat and will only see 'matthews'
I try to achieve this with a single query like the following, but I always get the contact number in the FORMATTED_ADRESS field. I guess I have a JOIN problem, because I'm using ContactsContract.CommonDataKinds and ContactsContract.Contacts in the same query?
public static List<ContactModel> getContactsForQuery(Context context, String query) {
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
Contacts.PHOTO_THUMBNAIL_URI,
ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS
};
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String selection = ContactsContract.Contacts.DISPLAY_NAME + " LIKE '%" + query + "%'";
Cursor cursor = context.getContentResolver().query(uri, projection, selection, null,null);
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(0);
String thumbail = cursor.getString(1);
String formattedADress = cursor.getString(2);
}
while (cursor.moveToNext());
}
I actually solved my issue, with
querying for Contacts._ID, Contacts.DISPLAY_NAME
start a second query with the Contacts._ID like the following
Cursor detailCursor = context.getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
new String[]{
CommonDataKinds.StructuredPostal.STREET,
CommonDataKinds.StructuredPostal.CITY,
CommonDataKinds.StructuredPostal.POSTCODE
},
ContactsContract.Data.CONTACT_ID + "=? AND "
+ CommonDataKinds.StructuredPostal.MIMETYPE + "=?",
new String[]{
String.valueOf(contactID),
CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE
},
null);
but this will start a second query for every contact, which might not be the best approach.
So my final question is: is it possible to get this work with the first query?
Mmmh, very sad, that no one was able to answer my question and grab the bounty points ;-(
For the record, here's my working example. It solves the issue but I still think it's producing a big overload. On every user entry (afterTextchange) I call the getContactsDetailsQuery which first gets all users with their ID containing the query in their name (cursor) and afterwards I start another query (detailCursor) for every user to get the adress. To prevent the overload, i added an limit..
public static List<SearchModel> getContactDetailsForQuery(Context context, String query, int limit) {
final int CONTACT_ID_INDEX = 0;
final int CONTACT_NAME_INDEX = 1;
final int CONTACT_THUMBNAIL_INDEX = 2;
//my custom model to hold my results
List<SearchModel> results = new ArrayList<SearchModel>();
final String[] selectUser = new String[]{
Contacts._ID,
Contacts.DISPLAY_NAME,
Contacts.PHOTO_THUMBNAIL_URI};
String selection = Contacts.DISPLAY_NAME + " LIKE ?";
String[] selectionArgs = new String[]{"%" + query + "%"};
String sortOrder = Contacts.DISPLAY_NAME + " ASC";
Cursor cursor = context.getContentResolver().query(Contacts.CONTENT_URI, selectUser, selection, selectionArgs, sortOrder, null);
int contactCounter = 0;
if (cursor != null && cursor.moveToFirst()) {
do {
String contactID = cursor.getString(CONTACT_ID_INDEX);
String displayName = cursor.getString(CONTACT_NAME_INDEX);
String thumbnail = cursor.getString(CONTACT_THUMBNAIL_INDEX);
//get user details with user id (this is the query i wanted to change in my question!!)
Cursor detailCursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[]{
CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS},
ContactsContract.Data.CONTACT_ID + "=? AND " +
CommonDataKinds.StructuredPostal.MIMETYPE + "=?",
new String[]{String.valueOf(contactID), CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE},
null);
if (detailCursor != null && detailCursor.moveToFirst()) {
//special case: user has several address, query all of them
do {
String formattedAddress = detailCursor.getString(detailCursor.getColumnIndex(CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
//user has serveral adress -> init model for each adress
SearchModel contact = new SearchModel();
results.add(contact);
contactCounter++;
} while (detailCursor.moveToNext() && contactCounter < limit);
} else {
//user has no adress -> init model
SearchModel contact = new SearchModel();
results.add(contact);
contactCounter++;
}
detailCursor.close();
} while (cursor.moveToNext() && contactCounter < limit);
}
cursor.close();
return results;
}

RxJava: Reduce or Combine or Merge

What I want to do is query the Android ContentProvider for Contacts.
The Cursor returns contains multiple duplicates for a contact where they may have more than one number registered against their contact_id)
So far, I have queried the DB, and am iterating through the Cursor rows.
I map() these rows and converting them into a ValueObjects
Next I want to go through all the list of VO’s and merge the ones that have the same contact_id (the VO would store an array of label & numbers)
But, I am stuck, I can not figure out how to perform the last part, how can I loop through the list of ValueObjects, merging the duplicates into one and then disposing the unneeded ones.
This is a sample of the Cursor returned by the ContentProvider:
86 {
_id=5190
contact_id=2167
display_name=John Doe
data1=+44 20 7123 7890
data2=3
data3=null
photo_thumb_uri=content://com.android.contacts/contacts/2167/photo
lookup=731i7g4b3e9879f40515
}
87 {
_id=5191
contact_id=2167
display_name=John Doe
data1=+44 7967 123 789
data2=2
data3=null
photo_thumb_uri=content://com.android.contacts/contacts/2167/photo
lookup=731i7g4b3e9879f40515
}
88 {
_id=5192
contact_id=2167
display_name=John Doe
data1=+44 208 123 7890
data2=1
data3=null
photo_thumb_uri=content://com.android.contacts/contacts/2167/photo
lookup=731i7g4b3e9879f40515
}
Sample of the function
public static Observable<List<ContactVO>> fetchAllContacts(final Context context) {
allContactsQuery(context);
return ContentObservable.fromCursor(allContactsQuery(context))
.map(mapToContactVO())
.toList()
// I am stuck
}
private static Cursor allContactsQuery(Context context) {
final String[] CONTACTS = new String[]{
Phone._ID, //.....0
Phone.CONTACT_ID, //.....1
Contacts.DISPLAY_NAME_PRIMARY, //.....2
Phone.NUMBER, //.....3
Phone.TYPE, //.....4
Phone.LABEL, //.....5
Contacts.PHOTO_THUMBNAIL_URI, //.....6
Contacts.LOOKUP_KEY, //.....7
};
String SELECTION = Contacts.DISPLAY_NAME_PRIMARY +
"<>''" + " AND " + Contacts.IN_VISIBLE_GROUP + "=1" +
" AND " + Contacts.HAS_PHONE_NUMBER + "=1";
final String[] SELECTION_ARGS = null;
final String SORT_ORDER = Contacts.SORT_KEY_PRIMARY;
Cursor cursor = context.getContentResolver().query(
Phone.CONTENT_URI,
CONTACTS,
SELECTION,
SELECTION_ARGS,
SORT_ORDER);
return cursor;
}
#NonNull
private static Func1<Cursor, ContactVO> mapToContactVO() {
return cursor -> {
int len = cursor.getCount();
final ContactVO contact = new ContactVO();
contact.contactId = cursor.getString(CONTACT_ID);
contact.displayName = cursor.getString(DISPLAY_NAME);
contact.photoThumbnailUri = cursor.getString(PHOTO_THUMBNAIL_URI);
contact.lookUp = cursor.getString(LOOK_UP);
contact.addData(
new Pair<String, String>(
cursor.getString(PHONE_TYPE),
cursor.getString(PHONE_NUMBER)
)
);
return contact;
};
}
public final static int CONTACT_ID = 1;
public final static int DISPLAY_NAME = 2;
public final static int PHONE_NUMBER = 3;
public final static int PHONE_TYPE = 4;
public final static int PHONE_LABEL = 5;
public final static int PHOTO_THUMBNAIL_URI = 6;
public final static int LOOK_UP = 7;
Use groupBy to get the records with the same contactId together then flatMap and reduce to merge the records
ContentObservable.fromCursor(allContactsQuery(context))
.map(mapToContactVO())
.groupBy(contact -> contact.contactId)
.flatMap(g -> g.reduce(mergeContacts()));

content resolver notifyChange() not working

I have setup a fragment to pull data from a custom content provider using a CursorLoader.
The problem is that when i update a record in the SQLite table using the content resolver, the cursor does not refresh i.e. the getContext().getContentResolver().notifyChange(myUri, null) has no effect. I have to exit the fragment and open it again to see the change.
I think the problem is that the URI i have used to update a row is not being observed by the loader :
URI to create loader -content://com.myapp.provider/MyTable/Set/22
URI to update row -content://com.myapp.provider/MyTable/167
167 identifies a unique row in the table. 22 identifies a set of rows in the table. Is there some way to tell the loader that the row 167 comes within the set 22, so it should reset the cursor?
Here is the code in case it brings more clarity :
Creating CursorLoader in Fragment :
#Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle queryBundle) {
CursorLoader cursorLoader = new CursorLoader(getActivity(), Uri.parse("content://com.myapp.provider/MyTable/Set/22"), myProjection, null, null, null);
return cursorLoader;
}
on button click in fragment :
mContext.getContentResolver().update("content://com.myapp.provider/MyTable/167", values, null, null);
Content Provider class :
private static final String AUTHORITY = "com.myapp.provider";
private static final String TABLE_PATH = "MyTable";
public static final String CONTENT_URI_BASEPATH = "content://" + AUTHORITY + "/" + TABLE_PATH;
private static final int URITYPE_TABLE = 1;
private static final int URITYPE_SINGLE_SET = 2;
private static final int URITYPE_SINGLE_ROW = 3;
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static{
sUriMatcher.addURI(AUTHORITY, TABLE_PATH,URITYPE_TABLE);
sUriMatcher.addURI(AUTHORITY, TABLE_PATH + "/Set/#", URITYPE_SINGLE_SET);
sUriMatcher.addURI(AUTHORITY, TABLE_PATH + "/#", URITYPE_SINGLE_ROW);
}
#Override
public int update(Uri myUri, ContentValues values, String selection, String[] selectionArgs){
int rowCount = 0;
String id;
SQLiteDatabase db = localDB.getWritableDatabase();
int uriType = sUriMatcher.match(myUri);
switch(uriType){
case URITYPE_SINGLE_ROW :
id = uri.getLastPathSegment();
//selection and selectionArgs are ignored since the URI itself identifies a unique row.
rowCount = db.update(MyTable.TABLE_NAME, values, MyTable.COLUMN_ID + " = ?", new String[] {id});
}
getContext().getContentResolver().notifyChange(myUri, null);
return rowCount;
}
I has a similar issue and found the solution here.
In brief, it turned out I needed to call setNotificationUri(ContentResolver cr, Uri uri) on the cursor returned by the query() method of my content provider.
The solution is to call notifyChange() on the Uri that is being observed i.e. the set and not on the row.
To achieve this, we need to make some changes :
Include the set ID in the URI when calling the update :
mContext.getContentResolver().update("content://com.myapp.provider/MyTable/Set/22/167", values, null, null);
Change the URI pattern of a single row from "/#" to "/Set/#/#"
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static{
sUriMatcher.addURI(AUTHORITY, TABLE_PATH,URITYPE_TABLE);
sUriMatcher.addURI(AUTHORITY, TABLE_PATH + "/Set/#", URITYPE_SINGLE_SET);
sUriMatcher.addURI(AUTHORITY, TABLE_PATH + "/Set/#/#", URITYPE_SINGLE_ROW);
}
Then in the update function, construct a new Uri that has to be notified :
List<String> pathSegments = uri.getPathSegments();
String mySetID = pathSegments.get(2);
Uri mySetUri = Uri.parse("content://" + AUTHORITY + "/" + TABLE_PATH + "/Set/" + mySetID);
getContext().getContentResolver().notifyChange(mySetUri, null);

content://sms/ address column not always return "sender's" number

When I query the "content://sms/" content provider and pull information from the address column; I always get the phone number that the message is "from" or "sent to". If I receive a message, then address is the number from the other person's phone. When I send a message, then address is the message I am sending to.
How do I differentiate if a message in "content://sms/" folder is a sent message or received message without querying the respective inbox/sent folders?
Uri uri = Uri.parse("content://sms/");
String[] columns = new String[] { "_id", "thread_id", "address", "person", "date", "body" };
String selection = "thread_id = " + threadId;
String sortOrder = "date DESC";
String limit = "LIMIT " + String.valueOf(mItemsOnPage);
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
String deviceNumber = tm.getLine1Number();
Cursor cursor = getContentResolver().query(uri, columns, selection, null,
sortOrder + " " + limit);
if (cursor != null) {
cursor.moveToLast();
while (!cursor.isBeforeFirst()) {
long messageId = cursor.getLong(0);
String address = cursor.getString(2);
long date = cursor.getLong(4);
String body = cursor.getString(5);
long person = cursor.getLong(3);
cursor.moveToPrevious();
}
}
cursor.close();
You need to include the column type in your query. It contains a long indicating whether you are dealing with a received (type == 1) or sent (type == 2) message.
This way you'll know how to interpret the address column.
public static final Uri SMS_Inbox = Uri.parse("content://sms/inbox");
public static final Uri SMS_Sent = Uri.parse("content://sms/sent");
public static final Uri SMS_draft = Uri.parse("content://sms/draft");
public static final Uri SMS_Queued = Uri.parse("content://sms/queued");
public static final Uri SMS_ALL = Uri.parse("content://sms/");
public static final int INBOX = 1;
public static final int SEND = 2;
public static final int DRAFT = 3;
public static final int QUEUED = 6;
type 5 seems to be a draft message type (for samsung i9100 at least)

Categories

Resources