Not all contacts returned - android

I Have tied to get all contacts by using following code
getContentResolver().query(uri, null, null, null, null)
But it doesn't return all contacts. It seems that it returns only that contacts which have in column "single_is_restricted" value "1", but this column cannot be asseccable from aplication, I found it when I was viewing table "contacts" directly through sqlite.
How I can get all contacts?
Thanks.

Check the create statement for the table/view.
For example in ContactManager example you will see this code block;
private Cursor getContacts()
{
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
(mShowInvisible ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
If you can get an exception (for example you can change selection...), you can get view/table name from log cat, like this (you can see "ASDF" in log cat, just get an exception);
04-28 12:16:34.682: E/AndroidRuntime(466): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.contactmanager/com.example.android.contactmanager.ContactManager}: android.database.sqlite.SQLiteException: near "'1'": syntax error: , while compiling: SELECT _id, display_name FROM view_contacts_restricted WHERE (in_visible_group =ASDF '1') ORDER BY display_name COLLATE LOCALIZED ASC
Point is; FROM view_contacts_restricted
And now you can check this view's create statement. You can install a root browser app. And copy sqlite file to SDCARD or install sqlite3 to Android Device. then open database from command line;
sqlite3 '/home/semeteycoskun/Desktop/contacts2.db'
Check view;
.schema view_contacts_restricted
Result is;
CREATE VIEW view_contacts_restricted
AS SELECT contacts._id AS _id
, contacts.custom_ringtone AS custom_ringtone
, name_raw_contact.display_name_source AS display_name_source
, name_raw_contact.display_name AS display_name
, name_raw_contact.display_name_alt AS display_name_alt
, name_raw_contact.phonetic_name AS phonetic_name
, name_raw_contact.phonetic_name_style AS phonetic_name_style
, name_raw_contact.sort_key AS sort_key
, name_raw_contact.sort_key_alt AS sort_key_alt
, name_raw_contact.sort_priority AS sort_priority
, name_raw_contact.sort_priority_alt AS sort_priority_alt
, name_raw_contact.sort_locale AS sort_locale
, name_raw_contact.sort_locale_alt AS sort_locale_alt
, name_raw_contact.contact_in_visible_group AS in_visible_group
, has_phone_number, lookup, photo_id
, contacts.last_time_contacted AS last_time_contacted
, contacts.send_to_voicemail AS send_to_voicemail
, contacts.starred AS starred
, contacts.times_contacted AS times_contacted
, status_update_id
, dirty_contact
, has_email
, link_count
, raw_contact_linkpriority1
, link_type1
, raw_contact_linkpriority2
, link_type2
, raw_contact_linkpriority3
, link_type3
, raw_contact_linkpriority4
, link_type4
, raw_contact_linkpriority5
, link_type5
FROM contacts
JOIN raw_contacts AS name_raw_contact ON(name_raw_contact_id=name_raw_contact._id)
WHERE single_is_restricted=0;
If create statements include single_is_restricted=0 you can not access the rows that single_is_restricted=1.
[sorry for my english]

Some manufacturers like Samsung have deals with Facebook in which Facebook syncs to the device contacts with names, phones, pics, etc. but those contacts can't be accessed via the Contacts APIs.
This is the reason some contacts can't be accessed by your app, but are visible in the stock contacts app.

Related

sqlite.SQLiteException: no such column: data1 (Sqlite code 1):

I've searched the internet for this problem but couldn't get any working response.
I believe it's from the selection but I don't know what is wrong with it.
I get this error :
android.database.sqlite.SQLiteException: no such column: data1 (Sqlite code 1): , while compiling: SELECT sort_key, photo_uri ...
after executing this code to retrieve the phone contacts:
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String no07 = "%07";
String no407 = "%+407";
String selection = "((" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY + " NOTNULL) AND ("
+ ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY + " != '' ) AND (("
+ ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '" + no07 + "' ) OR ("
+ ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '" + no407 + "' )))";
Cursor phones = getActivity()
.getContentResolver()
.query(uri, null, selection, null, null);
I would appreciate any help.
According to your error there is no such a column data1 from the URI you are querying. The ContactsContract.CommonDataKinds.Phone.NUMBER column included in your selection should be causing the exception.
You might want to query a different table from the ContactsContract which is ContactsContract.Data that as stated at the overview from the ContactsContract
A row in the ContactsContract.Data table can store any kind of personal data, such as a phone number or email addresses.
Change Your Query URI.
You are using a URI that is not meant for searching/filtering Contacts and does not have access to those columns:
Uri uri = ContactsContract.Contacts.CONTENT_URI;
You need to use a URI that has access to the columns you're trying to filter on, like this:
Uri uri = ContactsContract.Data.CONTENT_URI;
There's a decent breakdown of what URIs to use and when on the Android SDK Documentation:
If you need to read an individual contact, consider using CONTENT_LOOKUP_URI instead of CONTENT_URI.
If you need to look up a contact by the phone number, use PhoneLookup.CONTENT_FILTER_URI, which is optimized for this purpose.
If you need to look up a contact by partial name, e.g. to produce filter-as-you-type suggestions, use the CONTENT_FILTER_URI URI.
If you need to look up a contact by some data element like email address, nickname, etc, use a query against the ContactsContract.Data table. The result will contain contact ID, name etc.

How to query android call log data with group by date

I am querying call dates in call log database in android phone. After reading some articles, they said this needs to make some injection in my query method like this.
Cursor cursor = activity.getContentResolver().query(
CallLog.Calls.CONTENT_URI,
new String[] { CallLog.Calls.DATE},
"" + ") GROUP BY (" + CallLog.Calls.DATE,
null, CallLog.Calls.DATE + " DESC");
When I run my app and I got the following error message.
near ")": syntax error (code 1): , while compiling: SELECT date FROM calls WHERE ((() GROUP BY (date) AND ((type != 4)))) ORDER BY date DESC
Actually, I can't figure out how this query was built and why ((type !=4)) filter was appended to GROUP BY clause. I am currently using (compileSdkVersion 19, minSdkVersion 10 and targetSdkVersion 19). Is there any way to make raw query to Call Log Database because I want to write the query with my own to group dates.
CursorLoader cl = new CursorLoader(getActivity(), CallsQuery.CONTENT_URI, CallsQuery.PROJECTION, null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
You can use the DEFAULT_SORT_ORDER which is date DESC
public static final String DEFAULT_SORT_ORDER
The default sort order for this table
Constant Value: `"date DESC"`
Just remove the extra brackets from your query and change it as below:
Cursor cursor =mActivity.getContentResolver().query(
CallLog.Calls.CONTENT_URI,
new String[] { CallLog.Calls.DATE},
" GROUP BY " + CallLog.Calls.DATE,null,CallLog.Calls.DATE + " DESC");

Contacts are not sorting in Android

I have inserted some raw contacts (given account type and name null). the native contact app of android shows all contacts are sorted ( merged previews and newly given). But in my app (a listview for displaying contacts), it shows first the previous contatcs (sorted by display name) and then newly inserted contacs (also sorted). I have tried whatever combination possible , but no luck. please help any one.
Query Code
String PROJECTION[] = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
private final String SORT_ORDER = ContactsContract.Contacts.DISPLAY_NAME + " ASC";
Uri uri = ContactsContract.Contacts.CONTENT_URI;
Cursor contacts = cr.query(uri, PROJECTION, null ,null, SORT_ORDER);
Update*strong text*
however , i was using handler , and now after converting to cusorloadr with loader manager. problem solved
Use getShort() method of Cursor to sort on the bases of a particular column.
try this as query :
Cursor cursor = getContentResolver.query(ContactsContract.Contacts.CONTENT_URI, PROJECTION, null ,null, Phone.DISPLAY_NAME + " ASC");

Android application contact groups

I need phone contacts to be divided into groups in my application, but only in my application so i don't want to add any groups to original phone contact database. I have made my own database to have my groups table:
groups(group_id int, group_name text, message text)
and contacts_group table.
contacts_group(contact_id, group_id)
Now the problem i am facing: How should i create the query to get all contacts from my group (from phone contact databse).
I need something like this: Select ...DISPLAY_NAME, ...NUMBER where ..._ID in (String[] ids) while String[] ids is an array of contact_ids from contacts_group table. Is it possible to put my string array in '?' in raw query? As.. Select... where .._ID in ?, string[] ?
Thanks for help in advance
Regards
Probelm solved. Solution:
public static Cursor getContactsFromGroup(String[] ids, SQLiteDatabase db) {
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = { ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
Cursor c = context.getContentResolver().query(uri, projection,
"_ID IN (" + makePlaceHolders(ids.length) + ")", ids, null);
}
where makePlacHoldes(int) puts "?" chars, as many as long is param array.
Regards

Distinct CONTACT_ID from ContactsContract.Data

I need to make a query to ContactsContract.Data table and values in CONTACT_ID column would be different (distinct).
Code:
final Uri uri = ContactsContract.Data.CONTENT_URI;
final String[] projection = new String[] {//
ContactsContract.Data.CONTACT_ID, //
ContactsContract.Data._ID, //
ContactsContract.Data.DISPLAY_NAME,//
ContactsContract.Data.LOOKUP_KEY //
};
final StringBuilder selectionBuilder = new StringBuilder();
selectionBuilder.append(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID);
selectionBuilder.append("= ? AND ");
selectionBuilder.append(ContactsContract.Data.MIMETYPE);
selectionBuilder.append("= ? ");
final String selection = selectionBuilder.toString();
final String[] selectionArgs = new String[] {//
String.valueOf(groupId), //
ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE //
};
return context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
First of all, I've tried to add "DISTINCT " to ContactsContract.Data.CONTACT_ID in projection. But there was an exception: java.lang.IllegalArgumentException: Invalid column DISTINCT contact_id
Then, I write this way:
"'DISTINCT "+ContactsContract.Data.CONTACT_ID+"'".
java.lang.IllegalArgumentException: Invalid column 'DISTINCT contact_id'
Then, I add to selectionBuilder:
selectionBuilder.append(" GROUP BY ").append(ContactsContract.Data.CONTACT_ID);
Once again, an exception: android.database.sqlite.SQLiteException: near "GROUP": syntax error: , while compiling: SELECT contact_id, _id, display_name, lookup FROM view_data_restricted data WHERE (1) AND (data1= ? AND mimetype= ? GROUP BY contact_id) ORDER BY display_name ASC
At last, I've append "group by" statement right after sortOrder, but:
android.database.sqlite.SQLiteException: near "GROUP": syntax error: , while compiling: SELECT contact_id, _id, display_name, lookup FROM view_data_restricted data WHERE (1) AND (data1= ? AND mimetype= ? ) ORDER BY display_name ASC GROUP BY contact_id
Is it ever possible to make query with distinct?
Maybe, I should append something to URI?
If you are targeting devices below ICS, you can use the GROUP_BY clause by adding a ) before the group by and a ( after:
selectionBuilder.append(") GROUP BY (")
As of ICS and above, the query interpretor is smarter and closes any unclosed parenthesis to prevent injection.
However, I don't see why you need distinct contact_ids here. A contact should probably have only one Data to make the association with one group, so you probably receive a different contact on each line.
Also, there may be something to do with http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html#CONTENT_GROUP_URI it is not documented, but given its position, it may well be a direct access to Contacts belonging to a Group. You would use that Uri :
Uri uri = ContentUri.withAppendedId(ContactsContract.Contacts.CONTENT_GROUP_URI, groupId);
And then query it like the Contacts.CONTENT_URI

Categories

Resources