Selecting/Joining tables from contacts table but get no ouput - Android - android

I'm trying to go over all contacts using the contacts, raw_contacts and data tables.
From reading online I believe that Contacts._id is connected with RawContacts.contact_id and Data.raw_contact_id is connected with RowContacts._id. Am I right?
Believing in this I built this method:
public void testingContactsDatabase(String contactId)
{
final String[] projection = new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME,
Data.RAW_CONTACT_ID,
Data.MIMETYPE,
StructuredName.DISPLAY_NAME,
StructuredName.FAMILY_NAME,
StructuredName.GIVEN_NAME,
StructuredName.MIDDLE_NAME,
StructuredName.PREFIX, // Common prefixes in English names are "Mr", "Ms", "Dr" etc.
StructuredName.SUFFIX // Common suffixes in English names are "Sr", "Jr", "III" etc.
};
final String selection = Contacts._ID + " = " + contactId + " AND "
+ Contacts._ID + " = " + RawContacts.CONTACT_ID + " AND "
+ Data.RAW_CONTACT_ID + " = " + RawContacts._ID;
final Cursor curStructuredName = context.getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
projection,
selection, null,
//new String[] {contactId, ContactsContract.RawContacts.CONTACT_ID, RawContacts._ID},
null
);
if(curStructuredName.moveToFirst())
{
Log.d("XYZ", "Found something");
}
}
But it never finds anything. I'm not sure if I'm using the correct URI, I've tried with different URIs and either got exceptions or same ouput.
Can someone point me out where I'm doing something wrong?
Thanks

I've solved my problem. Here is my code working now:
// Get the structured name fields
public ArrayList<String> getStructuredName(String contactId)
{
ArrayList<String> structuredList = new ArrayList<String>();
final String[] projection = new String[] {
StructuredName.DISPLAY_NAME,
StructuredName.FAMILY_NAME,
StructuredName.GIVEN_NAME,
StructuredName.MIDDLE_NAME,
StructuredName.PREFIX, // Common prefixes in English names are "Mr", "Ms", "Dr" etc.
StructuredName.SUFFIX // Common suffixes in English names are "Sr", "Jr", "III" etc.
};
final String filter = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ?";
final String parameters[] = {StructuredName.CONTENT_ITEM_TYPE, contactId};
Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
projection, filter, parameters, null);
if(cursor.moveToFirst())
{
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
{
final String displayName = cursor.getString(cursor.getColumnIndex(StructuredName.DISPLAY_NAME));
final String familyName = cursor.getString(cursor.getColumnIndex(StructuredName.FAMILY_NAME));
final String givenName = cursor.getString(cursor.getColumnIndex(StructuredName.GIVEN_NAME));
final String middleName = cursor.getString(cursor.getColumnIndex(StructuredName.MIDDLE_NAME));
final String prefix = cursor.getString(cursor.getColumnIndex(StructuredName.PREFIX));
final String suffix = cursor.getString(cursor.getColumnIndex(StructuredName.SUFFIX));
structuredList.add(displayName);
structuredList.add(familyName);
structuredList.add(givenName);
structuredList.add(middleName);
structuredList.add(prefix);
structuredList.add(suffix);
Log.d(tag, "/////////////////////////////////////////////////////");
Log.d(tag, "displayName: " + displayName );
Log.d(tag, "familyName: " + familyName );
Log.d(tag, "givenName: " + givenName );
Log.d(tag, "middleName: " + middleName );
Log.d(tag, "prefix: " + prefix );
Log.d(tag, "suffix: " + suffix );
Log.d(tag, "/////////////////////////////////////////////////////");
}
}
cursor.close();
return structuredList;
}

Related

Retrieving contacts

I have tried to get phone number also but i didn't no how to get using this code . This the code i am using please tell how to get number by same code for same id
// method to get name, contact id, and birthday
private Cursor getContactsBirthdays() {
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.START_DATE
};
String where =
ContactsContract.Data.MIMETYPE + "= ? AND " +
ContactsContract.CommonDataKinds.Event.TYPE + "=" +
ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
String[] selectionArgs = new String[] {
ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE
};
String sortOrder = null;
return managedQuery(uri, projection, where, selectionArgs, sortOrder);
}
// iterate through all Contact's Birthdays and print in log
Cursor cursor = getContactsBirthdays();
int bDayColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE);
while (cursor.moveToNext()) {
String bDay = cursor.getString(bDayColumn);
Log.d(TAG, "Birthday: " + bDay);
}
You need to first get a list of all contact-ids that has birthdays, then query for the phones of all those contacts, and then print the combined results.
Cursor cursor = getContactsBirthdays();
// get contact-ids for phones query
List<String> ids = new ArrayList<>();
while (cursor.moveToNext()) {
ids.add(cursor.getString(1));
}
String[] projection = new String[] { Phone.NUMBER, Phone.CONTACT_ID };
StringBuilder where = new StringBuilder(Data.MIMETYPE + " = " Phone.CONTENT_ITEM_TYPE + " AND " + Contacts.CONTACT_ID + " IN (");
for (String id : ids) {
where.append(id).append(",");
}
where.deleteCharAt(where.length() - 1);
where.append(")");
Cursor cur2 = getContentResolver().query(Data.CONTENT_URI, projection, where.toString(), null, null);
Map<Long, String> contactIdToPhone = new HashMap<>();
while (cur2.moveToNext()) {
contactIdToPhone.put(cur2.get(1), cur2.get(0));
}
cur2.close();
cursor.moveToPosition(-1);
while (cursor.moveToNext()) {
Long id = cursor.getLong(1);
Log.i(TAG, "Birthday: id=" + id + ", name=" + cursor.getString(0) + ", date=" + cursor.getString(2) + ", phone=" + contactIdToPhone.get(id));
}
cursor.close();

Android: Retrieve Name, Phone Number, Email, Birthday from Contact

I am getting only name & birthday using the code below. But I need phone number & email also. It would be great if anyone can help me out. Thanks.
private void getContacts() {
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[]{
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Event.START_DATE
};
String where = ContactsContract.Data.MIMETYPE + "= ? AND " +
ContactsContract.CommonDataKinds.Event.TYPE + "=" +
ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
String[] selectionArgs = new String[]{
ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE
};
String sortOrder = null;
ContentResolver contentResolver = this.getActivity().getContentResolver();
Cursor cursor = contentResolver.query(uri, projection, where, selectionArgs, sortOrder);
int nameColumn = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
int numberColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int emailColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
int bithDayColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE);
while (cursor.moveToNext()) {
String name = cursor.getString(nameColumn);
String number = cursor.getString(numberColumn);
String email = cursor.getString(emailColumn);
String birthDay = cursor.getString(bithDayColumn);
Log.d(TAG, "Birthday: " + birthDay);
}
}
In your projection you're limiting your query to rows of MIMETYPE CommonDataKinds.Event.CONTENT_ITEM_TYPE only, so you'll only get birthdays.
You need to ask for emails and phones mimetypes, but note that these additional information will come in separate rows for the same contact.
For example, for contact A that has 2 phones, 3 emails and a birthday, you'll get 6 results in your cursor. So you need to group them all together using the CONTACT_ID field.
Here's simple code to get you started, print the resulting HashMap and you'll get for each contact all his/hers name, emails, phones and birthday:
Map<Long, List<String>> contacts = new HashMap<Long, List<String>>();
String[] projection = {Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1, Data.DATA2, Data.DATA3};
// query only emails/phones/events
String selection = Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "', '" + Event.CONTENT_ITEM_TYPE"', '" + Email.CONTENT_ITEM_TYPE + "')";
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null);
while (cur != null && cur.moveToNext()) {
long id = cur.getLong(0);
String name = cur.getString(1); // full name
String mime = cur.getString(2); // type of data (phone / birthday / email)
String data = cur.getString(3); // the actual info, e.g. +1-212-555-1234
String kind = "unknown";
switch (mime) {
case Phone.CONTENT_ITEM_TYPE:
kind = "phone";
break;
case Event.CONTENT_ITEM_TYPE:
kind = "birthday";
break;
case Email.CONTENT_ITEM_TYPE:
kind = "email";
break;
}
Log.d(TAG, "got " + id + ", " + name + ", " + kind + " - " + data);
// add info to existing list if this contact-id was already found, or create a new list in case it's new
List<String> infos;
if (contacts.containsKey(id)) {
infos = contacts.get(id);
} else {
infos = new ArrayList<String>();
infos.add("name = " + name);
contacts.put(id, infos);
}
infos.add(kind + " = " + data);
}

How to compare a particular field to a string in SQLite

The Cons.getpersonIdForRegisteredUser() method returns a string.
My query has two parts: one is to check the person id and the other is data enabled
#Override
public Loader<Cursor> onCreateLoader(final int i, final Bundle bundle) {
String sortOrder = ContactProviderContract.ContactDatabaseEntry.COLUMN_NAME_FIRST_NAME
+ " COLLATE NOCASE ASC";
String selection = ContactProviderContract.
ContactDatabaseEntry.COLUMN_NAME_PERSON_ID + " != " + Cons.getPersonIdForRegisteredUser(getActivity().getApplicationContext())
+ " AND "
+ ContactProviderContract.ContactDatabaseEntry.COLUMN_NAME_DATA_ENABLED + " = ?";
String[] selectionArgs = { "1" };
return new CursorLoader(getActivity().getApplicationContext(),
ContactProviderContract.CONTACTS_URI,
ContactProviderContract.ContactDatabaseEntry.MIN_DETAILS_COLUMNS,
selection,
selectionArgs,
sortOrder);
}
The error is that the string retrieved from the method Cons.getpersonid...() is considered as a column and not as a string to compare.
If I understand the error correctly, then you should place that Cons.getPersonId... into the selectionArgs.
String selection = ContactProviderContract.
ContactDatabaseEntry.COLUMN_NAME_PERSON_ID + " != ?" +
+ " AND "
+ ContactProviderContract.ContactDatabaseEntry.COLUMN_NAME_DATA_ENABLED + " = ?";
String[] selectionArgs = { "1" , Cons.getPersonIdForRegisteredUser(getActivity().getApplicationContext()) };

Query to the ContactsContract database comparing empty fields with empty strings - Android

I'm creating methods to edit a contact's field.
Before editing I'm making sure that those fields do exist and correspond to the company I want to edit, so I make a select and look for if(cursor.moveToFirst()) but it is not going inside the if for some reason I can not understand. I just know that the problem is on my empty Strings as if I remove them the code works fine.
I've downloaded the phones contact database so I know exactly what is in the contacts database. That is why I'm hardcoding the variables.
Here is my code:
public Boolean editRawContactOrganization(String rawContactId, String oldCompany, String oldType, String oldLabel, String oldTitle,
String oldDepartment, String oldJobDescription, String oldSymbol, String oldPhoneticName, String oldLocation,
String newCompany, String newType, String newLabel, String newTitle, String newDepartment, String newJobDescription,
String newSymbol, String newPhoneticName, String newOfficeLocation)
{
final ContentValues cv = new ContentValues();
final String filter = Organization.RAW_CONTACT_ID + "=? AND "
+ Organization.COMPANY + "=? AND "
+ Organization.TYPE_WORK + "=? AND "
+ Organization.LABEL + "=? AND "
+ Organization.TITLE + "=? AND "
+ Organization.DEPARTMENT + "=? AND "
+ Organization.JOB_DESCRIPTION + "=? AND "
+ Organization.SYMBOL + "=? AND "
+ Organization.PHONETIC_NAME + "=? AND "
+ Organization.OFFICE_LOCATION + "=? AND "
+ Data.MIMETYPE + "=?";
oldCompany = "Tone Inc.";
oldType = "2";
oldLabel = "";
oldTitle = "The Big Boss";
oldDepartment = "";
oldJobDescription = "";
oldSymbol = "";
oldPhoneticName = "";
oldLocation = "";
final String[] selectionArgs = new String[] {rawContactId, oldCompany, oldType, oldLabel, oldTitle, oldDepartment, oldJobDescription,
oldSymbol, oldPhoneticName, oldLocation, Organization.CONTENT_ITEM_TYPE};
final String[] projection = new String[] { Organization._ID };
final Cursor cursor = contentResolver.query(Data.CONTENT_URI,
projection,
filter,
selectionArgs,
null);
if(cursor.moveToFirst())
{
Log.d(TAG, "SUCCESS: Organization found! ID: " + cursor.getString(0));
cursor.close();
}
else
{
Log.d(TAG, "ERROR: Organization not found...");
cursor.close();
return false;
}
return false;
}
As you can see, these vars are hard coded with the corresponding values I see on the contacts database I just downloaded from the phone.
oldCompany = "Tone Inc.";
oldType = "2";
oldLabel = "";
oldTitle = "The Big Boss";
oldDepartment = "";
oldJobDescription = "";
oldSymbol = "";
oldPhoneticName = "";
oldLocation = "";
But I always get the ERROR: Organization not found... output on logcat
Can anyone point me to where is the error?
Here is a working example, if I uncoment the commented code I get the not found output (the same problem as above). Same pattern as above, the commented column has no value inside. Even when I'm sending and empty string why doesn't is match empty with empty?
public Boolean editRawContactWebsite(String rawContactId, String oldWebsite, String oldType, String oldLabel,
String newWebsite, String newType, String newLabel)
{
final ContentValues cv = new ContentValues();
final String filter = Website.RAW_CONTACT_ID + "=? AND "
+ Website.URL + "=? AND "
+ Website.TYPE + "=? AND "
//+ Website.LABEL + "=? AND "
+ Data.MIMETYPE + "=?";
oldWebsite = "www.sitr.com";
oldType = "7";
oldLabel = "";
final String[] selectionArgs = new String[] {rawContactId, oldWebsite, oldType, /*oldLabel,*/ Website.CONTENT_ITEM_TYPE};
final String[] projection = new String[] { Website._ID };
final Cursor cursor = contentResolver.query(Data.CONTENT_URI,
projection,
filter,
selectionArgs,
null);
if(cursor.moveToFirst())
{
Log.d(TAG, "SUCCESS: website found! ID: " + cursor.getString(0));
cursor.close();
}
else
{
Log.d(TAG, "ERROR: website not found...");
cursor.close();
return false;
}
return false;
}
I fixed this problem by passing an ID corresponding to the line I want to edit in the contacts database.

How to access MISSED_CALL Log in Android

I am developing an app in which i want to access MISSED_CALL log. Using below code....
private Cursor getItemsToSync() {
G = "Log method accessing";
ContentResolver r = getContentResolver();
String selections = String.format("%s > ?", CallLog.Calls.DATE,CallLog.Calls.MISSED_TYPE);
String[] selectionArgs = new String[] { String.valueOf(getMaxSyncedDate())};
String sortOrder = SmsConsts.DATE + " LIMIT " + PrefStore.getMaxItemsPerSync(this);
N = CallLog.Calls.CACHED_NAME;
return r.query(Uri.parse("content://call_log/calls"), null,selections,selectionArgs, sortOrder);}
its provide All Call Log. Please suggest me how to get only MISSED_CALL Call log. Thanks in advance
String[] strFields = {android.provider.CallLog.Calls.CACHED_NAME, android.provider.CallLog.Calls.NUMBER,android.provider.CallLog.Calls.DATE, android.provider.CallLog.Calls.TYPE
};
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
Cursor mCallCursor = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI,strFields, null, null, strOrder);
if (mCallCursor.moveToFirst()) {
do {
boolean missed = mCallCursor.getInt(mCallCursor.getColumnIndex(CallLog.Calls.TYPE)) == CallLog.Calls.MISSED_TYPE;
if (missed) {
String name = mCallCursor.getString(mCallCursor
.getColumnIndex(CallLog.Calls.CACHED_NAME));
String number = mCallCursor.getString(mCallCursor
.getColumnIndex(CallLog.Calls.NUMBER));
Log.d("PhoneLog", "You have a missed call from " + name + " on " + number // + " at " + time); }
} while (mCallCursor.moveToNext());
}

Categories

Resources