android.provider.ContactsContract.CommonDataKinds Java Examples

The following examples show how to use android.provider.ContactsContract.CommonDataKinds. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LinphoneContact.java    From Linphone4Android with GNU General Public License v3.0 6 votes vote down vote up
public void setPhoto(byte[] photo) {
	if (photo != null) {
		if (isAndroidContact()) {
			if (androidRawId != null) {
				changesToCommit.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
					.withValue(ContactsContract.Data.RAW_CONTACT_ID, androidRawId)
					.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
					.withValue(CommonDataKinds.Photo.PHOTO, photo)
					.withValue(ContactsContract.Data.IS_PRIMARY, 1)
					.withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
					.build());
			} else {
				changesToCommit.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
			        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
					.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
					.withValue(CommonDataKinds.Photo.PHOTO, photo)
					.build());
			}
		}
	}
}
 
Example #2
Source File: ContactUtil.java    From haxsync with GNU General Public License v2.0 6 votes vote down vote up
public static void addBirthday(long rawContactId, String birthday){
	String where = ContactsContract.Data.RAW_CONTACT_ID + " = '" + rawContactId 
			+ "' AND " + ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE
			+ "' AND " + ContactsContract.CommonDataKinds.Event.TYPE + " = '" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY + "'";
	Cursor cursor = ContactsSyncAdapterService.mContentResolver.query(ContactsContract.Data.CONTENT_URI, null, where, null, null);
	int count = cursor.getCount();
	cursor.close();
	if (count <= 0){
		ContentValues contentValues = new ContentValues();
		contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
		contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
		contentValues.put(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY);
		contentValues.put(ContactsContract.CommonDataKinds.Event.START_DATE, birthday);
		
		try {
			ContactsSyncAdapterService.mContentResolver.insert(ContactsContract.Data.CONTENT_URI, contentValues);
		//	mContentResolver.applyBatch(ContactsContract.AUTHORITY,	operationList);
		} catch (Exception e) {
			e.printStackTrace();
			//Log.e("ERROR:" , e.^);
		}
	}
}
 
Example #3
Source File: ContactUtil.java    From haxsync with GNU General Public License v2.0 6 votes vote down vote up
public static void addEmail(Context c, long rawContactId, String email){
	DeviceUtil.log(c, "adding email", email);
	String where = ContactsContract.Data.RAW_CONTACT_ID + " = '" + rawContactId 
			+ "' AND " + ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE+ "'";
	Cursor cursor = c.getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] { RawContacts.CONTACT_ID}, where, null, null);
	if (cursor.getCount() == 0){
		ContentValues contentValues = new ContentValues();
		//op.put(ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID, );
		contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
		contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
		contentValues.put(ContactsContract.CommonDataKinds.Email.ADDRESS, email);
		c.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, contentValues);
	}
	cursor.close();

}
 
Example #4
Source File: ContactUtil.java    From haxsync with GNU General Public License v2.0 6 votes vote down vote up
public static Photo getPhoto(ContentResolver c, long rawContactId){
	Photo photo = new Photo();
	String where = ContactsContract.Data.RAW_CONTACT_ID + " = '" + rawContactId 
			+ "' AND " + ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";



	Cursor c1 = c.query(ContactsContract.Data.CONTENT_URI, new String[] {ContactsContract.CommonDataKinds.Photo.PHOTO, ContactsContract.Data.SYNC2, ContactsContract.Data.SYNC3 }, where , null, null);
	if (c1.getCount() > 0){
		c1.moveToLast();
		photo.data = c1.getBlob(c1.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO));
		photo.timestamp = Long.valueOf(c1.getString(c1.getColumnIndex(ContactsContract.Data.SYNC2)));
		photo.url = c1.getString(c1.getColumnIndex(ContactsContract.Data.SYNC3));
	}
	c1.close();
	return photo;
}
 
Example #5
Source File: ContactsUtils5.java    From CSipSimple with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Intent getAddContactIntent(String displayName, String csipUri) {
    Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT, Contacts.CONTENT_URI);
    intent.setType(Contacts.CONTENT_ITEM_TYPE);

    if (!TextUtils.isEmpty(displayName)) {
        intent.putExtra(Insert.NAME, displayName);
    }

    if (!TextUtils.isEmpty(csipUri)) {
        ArrayList<ContentValues> data = new ArrayList<ContentValues>();
        ContentValues csipProto = new ContentValues();
        csipProto.put(Data.MIMETYPE, CommonDataKinds.Im.CONTENT_ITEM_TYPE);
        csipProto.put(CommonDataKinds.Im.PROTOCOL, CommonDataKinds.Im.PROTOCOL_CUSTOM);
        csipProto.put(CommonDataKinds.Im.CUSTOM_PROTOCOL, SipManager.PROTOCOL_CSIP);
        csipProto.put(CommonDataKinds.Im.DATA, SipUri.getCanonicalSipContact(csipUri, false));
        data.add(csipProto);

        intent.putParcelableArrayListExtra(Insert.DATA, data);
    }

    return intent;
}
 
Example #6
Source File: ContactsManager.java    From react-native-contacts with MIT License 6 votes vote down vote up
private int mapStringToEmailType(String label) {
    int emailType;
    switch (label) {
        case "home":
            emailType = CommonDataKinds.Email.TYPE_HOME;
            break;
        case "work":
            emailType = CommonDataKinds.Email.TYPE_WORK;
            break;
        case "mobile":
            emailType = CommonDataKinds.Email.TYPE_MOBILE;
            break;
        default:
            emailType = CommonDataKinds.Email.TYPE_CUSTOM;
            break;
    }
    return emailType;
}
 
Example #7
Source File: ContactsManager.java    From react-native-contacts with MIT License 5 votes vote down vote up
private int mapStringToPhoneType(String label) {
    int phoneType;
    switch (label) {
        case "home":
            phoneType = CommonDataKinds.Phone.TYPE_HOME;
            break;
        case "work":
            phoneType = CommonDataKinds.Phone.TYPE_WORK;
            break;
        case "mobile":
            phoneType = CommonDataKinds.Phone.TYPE_MOBILE;
            break;
        case "main":
            phoneType = CommonDataKinds.Phone.TYPE_MAIN;
            break;
        case "work fax":
            phoneType = CommonDataKinds.Phone.TYPE_FAX_WORK;
            break;
        case "home fax":
            phoneType = CommonDataKinds.Phone.TYPE_FAX_HOME;
            break;
        case "pager":
            phoneType = CommonDataKinds.Phone.TYPE_PAGER;
            break;
        case "work_pager":
            phoneType = CommonDataKinds.Phone.TYPE_WORK_PAGER;
            break;
        case "work_mobile":
            phoneType = CommonDataKinds.Phone.TYPE_WORK_MOBILE;
            break;
        default:
            phoneType = CommonDataKinds.Phone.TYPE_CUSTOM;
            break;
    }
    return phoneType;
}
 
Example #8
Source File: ContactDetailsActivity.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onClick(DialogInterface dialog, int which) {
    Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
    intent.setType(Contacts.CONTENT_ITEM_TYPE);
    intent.putExtra(Intents.Insert.IM_HANDLE, contact.getJid().toEscapedString());
    intent.putExtra(Intents.Insert.IM_PROTOCOL, CommonDataKinds.Im.PROTOCOL_JABBER);
    intent.putExtra("finishActivityOnSaveCompleted", true);
    try {
        ContactDetailsActivity.this.startActivityForResult(intent, 0);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(ContactDetailsActivity.this, R.string.no_application_found_to_view_contact, Toast.LENGTH_SHORT).show();
    }
}
 
Example #9
Source File: ContactUtil.java    From haxsync with GNU General Public License v2.0 5 votes vote down vote up
public static void removeContactLocations(Context c, Account account){
	ContactsSyncAdapterService.mContentResolver = c.getContentResolver();
	HashMap<String, ContactsSyncAdapterService.SyncEntry> localContacts = ContactsSyncAdapterService.getLocalContacts(account);
	for (ContactsSyncAdapterService.SyncEntry s : localContacts.values()){
		ContactsSyncAdapterService.mContentResolver.delete(ContactsContract.Data.CONTENT_URI, ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE + "' AND " + ContactsContract.Data.RAW_CONTACT_ID + " = " + s.raw_id, null); 
	}			
	
}
 
Example #10
Source File: ContactUtil.java    From haxsync with GNU General Public License v2.0 5 votes vote down vote up
public static void updateContactPhoto(ContentResolver c, long rawContactId, Photo pic, boolean primary){
	ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
	

	
	//insert new picture
	try {
		if(pic.data != null) {
               //delete old picture
               String where = ContactsContract.Data.RAW_CONTACT_ID + " = '" + rawContactId
                       + "' AND " + ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
               Log.i(TAG, "Deleting picture: "+where);

               ContentProviderOperation.Builder builder = ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI);
               builder.withSelection(where, null);
               operationList.add(builder.build());
			builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
			builder.withValue(ContactsContract.CommonDataKinds.Photo.RAW_CONTACT_ID, rawContactId);
			builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
			builder.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, pic.data);
			builder.withValue(ContactsContract.Data.SYNC2, String.valueOf(pic.timestamp));
			builder.withValue(ContactsContract.Data.SYNC3, pic.url);
               if (primary)
			    builder.withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
			operationList.add(builder.build());

			
		}
		c.applyBatch(ContactsContract.AUTHORITY,	operationList);

	} catch (Exception e) {
		// TODO Auto-generated catch block
		Log.e("ERROR:" , e.toString());
	}


}
 
Example #11
Source File: ContactablesLoaderCallbacks.java    From android-BasicContactables with Apache License 2.0 5 votes vote down vote up
@Override
public Loader<Cursor> onCreateLoader(int loaderIndex, Bundle args) {
    // Where the Contactables table excels is matching text queries,
    // not just data dumps from Contacts db.  One search term is used to query
    // display name, email address and phone number.  In this case, the query was extracted
    // from an incoming intent in the handleIntent() method, via the
    // intent.getStringExtra() method.

    // BEGIN_INCLUDE(uri_with_query)
    String query = args.getString(QUERY_KEY);
    Uri uri = Uri.withAppendedPath(
            CommonDataKinds.Contactables.CONTENT_FILTER_URI, query);
    // END_INCLUDE(uri_with_query)


    // BEGIN_INCLUDE(cursor_loader)
    // Easy way to limit the query to contacts with phone numbers.
    String selection =
            CommonDataKinds.Contactables.HAS_PHONE_NUMBER + " = " + 1;

    // Sort results such that rows for the same contact stay together.
    String sortBy = CommonDataKinds.Contactables.LOOKUP_KEY;

    return new CursorLoader(
            mContext,  // Context
            uri,       // URI representing the table/resource to be queried
            null,      // projection - the list of columns to return.  Null means "all"
            selection, // selection - Which rows to return (condition rows must match)
            null,      // selection args - can be provided separately and subbed into selection.
            sortBy);   // string specifying sort order
    // END_INCLUDE(cursor_loader)
}
 
Example #12
Source File: FavoritesFragmentContainer.java    From Contacts with MIT License 5 votes vote down vote up
@Override
protected String getAdditionalFilters()
{
	if (mPosition == 0)
	{
		return " AND " + Contacts.STARRED + " = " + 1;
	}
	else
	{
		return " AND " + Data.MIMETYPE + " = ? AND " + CommonDataKinds.GroupMembership.GROUP_ROW_ID + " = ?";
	}
}
 
Example #13
Source File: FavoritesFragmentContainer2.java    From Contacts with MIT License 5 votes vote down vote up
@Override
protected String getAdditionalFilters()
{
	if (mPosition == 0)
	{
		return " AND " + Data.STARRED + " = " + 1;
	}
	else
	{
		Group group = (Group) mGroups.get(mPosition);
		return " AND " + CommonDataKinds.GroupMembership.GROUP_ROW_ID + " = " + group.getId();
	}
}
 
Example #14
Source File: ContactDetailsActivity.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onClick(DialogInterface dialog, int which) {
    Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
    intent.setType(Contacts.CONTENT_ITEM_TYPE);
    intent.putExtra(Intents.Insert.IM_HANDLE, contact.getJid().toEscapedString());
    intent.putExtra(Intents.Insert.IM_PROTOCOL, CommonDataKinds.Im.PROTOCOL_JABBER);
    intent.putExtra("finishActivityOnSaveCompleted", true);
    try {
        ContactDetailsActivity.this.startActivityForResult(intent, 0);
    } catch (ActivityNotFoundException e) {
        ToastCompat.makeText(ContactDetailsActivity.this, R.string.no_application_found_to_view_contact, Toast.LENGTH_SHORT).show();
    }
}
 
Example #15
Source File: ContactsManager.java    From react-native-contacts with MIT License 5 votes vote down vote up
private int mapStringToPostalAddressType(String label) {
    int postalAddressType;
    switch (label) {
        case "home":
            postalAddressType = CommonDataKinds.StructuredPostal.TYPE_HOME;
            break;
        case "work":
            postalAddressType = CommonDataKinds.StructuredPostal.TYPE_WORK;
            break;
        default:
            postalAddressType = CommonDataKinds.StructuredPostal.TYPE_CUSTOM;
            break;
    }
    return postalAddressType;
}
 
Example #16
Source File: ContactsManager.java    From Linphone4Android with GNU General Public License v3.0 5 votes vote down vote up
private Cursor getContactsCursor(ContentResolver cr) {
	String req = "(" + Data.MIMETYPE + " = '" + CommonDataKinds.Phone.CONTENT_ITEM_TYPE
			+ "' AND " + CommonDataKinds.Phone.NUMBER + " IS NOT NULL "
			+ " OR (" + Data.MIMETYPE + " = '" + CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE
			+ "' AND " + CommonDataKinds.SipAddress.SIP_ADDRESS + " IS NOT NULL))";
	String[] projection = new String[] { Data.CONTACT_ID, Data.DISPLAY_NAME };
	String query = Data.DISPLAY_NAME + " IS NOT NULL AND (" + req + ")";

	Cursor cursor = cr.query(Data.CONTENT_URI, projection, query, null, " lower(" + Data.DISPLAY_NAME + ") COLLATE UNICODE ASC");
	if (cursor == null) {
		return cursor;
	}

	MatrixCursor result = new MatrixCursor(cursor.getColumnNames());
	Set<String> groupBy = new HashSet<String>();
	while (cursor.moveToNext()) {
	    String name = cursor.getString(cursor.getColumnIndex(Data.DISPLAY_NAME));
	    if (!groupBy.contains(name)) {
	    	groupBy.add(name);
	    	Object[] newRow = new Object[cursor.getColumnCount()];

	    	int contactID = cursor.getColumnIndex(Data.CONTACT_ID);
	    	int displayName = cursor.getColumnIndex(Data.DISPLAY_NAME);

	    	newRow[contactID] = cursor.getString(contactID);
	    	newRow[displayName] = cursor.getString(displayName);

	        result.addRow(newRow);
    	}
    }
	cursor.close();
	return result;
}
 
Example #17
Source File: LinphoneContact.java    From Linphone4Android with GNU General Public License v3.0 5 votes vote down vote up
private void createLinphoneContactTag() {
	ArrayList<ContentProviderOperation> batch = new ArrayList<ContentProviderOperation>();

	batch.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
		.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, ContactsManager.getInstance().getString(R.string.sync_account_type))
		.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, ContactsManager.getInstance().getString(R.string.sync_account_name))
		.withValue(ContactsContract.RawContacts.AGGREGATION_MODE, ContactsContract.RawContacts.AGGREGATION_MODE_DEFAULT)
		.build());

	batch.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
		.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
		.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
		.withValue(CommonDataKinds.StructuredName.DISPLAY_NAME, getFullName())
		.build());

	batch.add(ContentProviderOperation.newUpdate(ContactsContract.AggregationExceptions.CONTENT_URI)
		.withValue(ContactsContract.AggregationExceptions.TYPE, ContactsContract.AggregationExceptions.TYPE_KEEP_TOGETHER)
		.withValue(ContactsContract.AggregationExceptions.RAW_CONTACT_ID1, androidRawId)
		.withValueBackReference(ContactsContract.AggregationExceptions.RAW_CONTACT_ID2, 0)
		.build());

	if (changesToCommit2.size() > 0) {
		for(ContentProviderOperation cpo : changesToCommit2) {
			batch.add(cpo);
		}
	}

	try {
		ContactsManager.getInstance().getContentResolver().applyBatch(ContactsContract.AUTHORITY, batch);
		androidTagId = findLinphoneRawContactId();
	} catch (Exception e) {
		Log.e(e);
	}
}
 
Example #18
Source File: LinphoneContact.java    From Linphone4Android with GNU General Public License v3.0 5 votes vote down vote up
public void setFirstNameAndLastName(String fn, String ln) {
	if (fn != null && fn.length() == 0 && ln != null && ln.length() == 0) return;

	if (isAndroidContact()) {
		if (firstName != null || lastName != null) {
			String select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" + CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'";
			String[] args = new String[]{ getAndroidId() };

			changesToCommit.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
				.withSelection(select, args)
				.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
				.withValue(CommonDataKinds.StructuredName.GIVEN_NAME, fn)
				.withValue(CommonDataKinds.StructuredName.FAMILY_NAME, ln)
				.build()
			);
		} else {
			changesToCommit.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
		        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
		        .withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
		        .withValue(CommonDataKinds.StructuredName.GIVEN_NAME, fn)
		        .withValue(CommonDataKinds.StructuredName.FAMILY_NAME, ln)
		        .build());
		}
	}

	firstName = fn;
	lastName = ln;
	if (firstName != null && lastName != null && firstName.length() > 0 && lastName.length() > 0) {
		fullName = firstName + " " + lastName;
	} else if (firstName != null && firstName.length() > 0) {
		fullName = firstName;
	} else if (lastName != null && lastName.length() > 0) {
		fullName = lastName;
	}
}
 
Example #19
Source File: LinphoneContact.java    From Linphone4Android with GNU General Public License v3.0 5 votes vote down vote up
public void setOrganization(String org) {
	if (isAndroidContact()) {
		if (androidRawId != null) {
			String select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" + CommonDataKinds.Organization.CONTENT_ITEM_TYPE + "'";
			String[] args = new String[]{ getAndroidId() };

			if (organization != null) {
				changesToCommit.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
					.withSelection(select, args)
					.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
					.withValue(CommonDataKinds.Organization.COMPANY, org)
					.build());
			} else {
				changesToCommit.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
					.withValue(ContactsContract.Data.RAW_CONTACT_ID, androidRawId)
					.withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
					.withValue(CommonDataKinds.Organization.COMPANY, org)
					.build());
			}
		} else {
			changesToCommit.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
		        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
		        .withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
		        .withValue(CommonDataKinds.Organization.COMPANY, org)
		        .build());
		}
	}

	organization = org;
}
 
Example #20
Source File: LinphoneContact.java    From Linphone4Android with GNU General Public License v3.0 5 votes vote down vote up
private void getContactNames() {
	ContentResolver resolver = ContactsManager.getInstance().getContentResolver();
	String[] proj = new String[]{ CommonDataKinds.StructuredName.GIVEN_NAME, CommonDataKinds.StructuredName.FAMILY_NAME, ContactsContract.Contacts.DISPLAY_NAME };
	String select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?";
	String[] args = new String[]{ getAndroidId(), CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
	Cursor c = resolver.query(ContactsContract.Data.CONTENT_URI, proj, select, args, null);
	if (c != null) {
		if (c.moveToFirst()) {
			firstName = c.getString(c.getColumnIndex(CommonDataKinds.StructuredName.GIVEN_NAME));
			lastName = c.getString(c.getColumnIndex(CommonDataKinds.StructuredName.FAMILY_NAME));
        	fullName = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
		}
		c.close();
	}
}
 
Example #21
Source File: LinphoneContact.java    From Linphone4Android with GNU General Public License v3.0 5 votes vote down vote up
private void getNativeContactOrganization() {
	ContentResolver resolver = ContactsManager.getInstance().getContentResolver();
	String[] proj = new String[]{ CommonDataKinds.Organization.COMPANY };
	String select = ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?";
	String[] args = new String[]{ getAndroidId(), CommonDataKinds.Organization.CONTENT_ITEM_TYPE };
	Cursor c = resolver.query(ContactsContract.Data.CONTENT_URI, proj, select, args, null);
	if (c != null) {
		if (c.moveToFirst()) {
			organization = c.getString(c.getColumnIndex(CommonDataKinds.Organization.COMPANY));
		}
		c.close();
	}
}
 
Example #22
Source File: LinphoneContact.java    From Linphone4Android with GNU General Public License v3.0 5 votes vote down vote up
private List<LinphoneNumberOrAddress> getAddressesAndNumbersForAndroidContact() {
	List<LinphoneNumberOrAddress> result = new ArrayList<LinphoneNumberOrAddress>();
	ContentResolver resolver = ContactsManager.getInstance().getContentResolver();

	String select = ContactsContract.Data.CONTACT_ID + " =? AND (" + ContactsContract.Data.MIMETYPE + "=? OR " + ContactsContract.Data.MIMETYPE + "=?)";
	String[] projection = new String[] { CommonDataKinds.SipAddress.SIP_ADDRESS, ContactsContract.Data.MIMETYPE }; // PHONE_NUMBER == SIP_ADDRESS == "data1"...
	Cursor c = resolver.query(ContactsContract.Data.CONTENT_URI, projection, select, new String[]{ getAndroidId(), CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE, CommonDataKinds.Phone.CONTENT_ITEM_TYPE }, null);
	if (c != null) {
		while (c.moveToNext()) {
			String mime = c.getString(c.getColumnIndex(ContactsContract.Data.MIMETYPE));
			if (mime != null && mime.length() > 0) {
				boolean found = false;
				boolean isSIP = false;
				if (mime.equals(CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE)) {
					found = true;
					isSIP = true;
				} else if (mime.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
					found = true;
				}

				if (found) {
					String number = c.getString(c.getColumnIndex(CommonDataKinds.SipAddress.SIP_ADDRESS)); // PHONE_NUMBER == SIP_ADDRESS == "data1"...
					if (number != null && number.length() > 0) {
						if (isSIP && !number.startsWith("sip:")) {
							number = "sip:" + number;
						}
						if (isSIP && !number.contains("@")) {
							number = number + "@" + ContactsManager.getInstance().getString(R.string.default_domain);
						}
						result.add(new LinphoneNumberOrAddress(number, isSIP));
					}
				}
			}
		}
		c.close();
	}
	Collections.sort(result);
	return result;
}
 
Example #23
Source File: Relation.java    From react-native-paged-contacts with MIT License 5 votes vote down vote up
private String getLabelFromType(Integer type, String name) {
    if (type == null) {
        throw new InvalidCursorTypeException();
    }
    switch (type) {
        case CommonDataKinds.Relation.TYPE_CUSTOM:
            return name;
        case CommonDataKinds.Relation.TYPE_ASSISTANT:
            return "assistant";
        case CommonDataKinds.Relation.TYPE_BROTHER:
            return "brother";
        case CommonDataKinds.Relation.TYPE_CHILD:
            return "child";
        case CommonDataKinds.Relation.TYPE_DOMESTIC_PARTNER:
            return "domestic partner";
        case CommonDataKinds.Relation.TYPE_FATHER:
            return "father";
        case CommonDataKinds.Relation.TYPE_FRIEND:
            return "friend";
        case CommonDataKinds.Relation.TYPE_MANAGER:
            return "manager";
        case CommonDataKinds.Relation.TYPE_MOTHER:
            return "mother";
        case CommonDataKinds.Relation.TYPE_PARENT:
            return "parent";
        case CommonDataKinds.Relation.TYPE_PARTNER:
            return "partner";
        case CommonDataKinds.Relation.TYPE_REFERRED_BY:
            return "referred by";
        case CommonDataKinds.Relation.TYPE_RELATIVE:
            return "relative";
        case CommonDataKinds.Relation.TYPE_SISTER:
            return "sister";
        case CommonDataKinds.Relation.TYPE_SPOUSE:
            return "spouse";
        default:
            return "other";
    }
}
 
Example #24
Source File: ContactsUtils5.java    From CSipSimple with GNU General Public License v3.0 5 votes vote down vote up
@Override
public List<String> getCSipPhonesContact(Context ctxt, Long contactId) {
    ArrayList<String> results = new ArrayList<String>();
    Uri dataUri = Data.CONTENT_URI;
    String dataQuery = Data.MIMETYPE + "='" + CommonDataKinds.Im.CONTENT_ITEM_TYPE + "' "
            + " AND "
            + CommonDataKinds.Im.PROTOCOL + "=" + CommonDataKinds.Im.PROTOCOL_CUSTOM
            + " AND "
            + " LOWER(" + CommonDataKinds.Im.CUSTOM_PROTOCOL + ")='"+SipManager.PROTOCOL_CSIP+"'";
    // get csip data
    Cursor dataCursor = ctxt.getContentResolver()
            .query(dataUri,
                    new String[] {
                            CommonDataKinds.Im._ID,
                            CommonDataKinds.Im.DATA,
                    },
                    dataQuery + " AND " + CommonDataKinds.Im.CONTACT_ID + "=?",
                    new String[] {
                        Long.toString(contactId)
                    }, null);

    try {
        if (dataCursor != null && dataCursor.getCount() > 0) {
            dataCursor.moveToFirst();
            String val = dataCursor.getString(dataCursor
                    .getColumnIndex(CommonDataKinds.Im.DATA));
            if (!TextUtils.isEmpty(val)) {
                results.add(val);
            }
        }
    } catch (Exception e) {
        Log.e(THIS_FILE, "Error while looping on data", e);
    } finally {
        dataCursor.close();
    }
    
    return results;
}
 
Example #25
Source File: AndroidContact.java    From linphone-android with GNU General Public License v3.0 4 votes vote down vote up
void setOrganization(String org, String previousValue) {
    if (org == null || org.isEmpty()) {
        if (mAndroidId == null) {
            Log.e("[Contact] Can't set organization to null or empty for new contact");
            return;
        }
    }
    if (mAndroidId == null) {
        Log.i("[Contact] Setting organization " + org + " to new contact.");
        addChangesToCommit(
                ContentProviderOperation.newInsert(Data.CONTENT_URI)
                        .withValueBackReference(Data.RAW_CONTACT_ID, 0)
                        .withValue(
                                Data.MIMETYPE, CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
                        .withValue(CommonDataKinds.Organization.COMPANY, org)
                        .build());
    } else {
        if (previousValue != null) {
            String select =
                    ContactsContract.Data.CONTACT_ID
                            + "=? AND "
                            + ContactsContract.Data.MIMETYPE
                            + "=? AND "
                            + ContactsContract.CommonDataKinds.Organization.COMPANY
                            + "=?";
            String[] args =
                    new String[] {
                        getAndroidId(),
                        ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE,
                        previousValue
                    };

            Log.i(
                    "[Contact] Updating organization "
                            + org
                            + " to existing contact "
                            + mAndroidId
                            + " ("
                            + mAndroidRawId
                            + ")");
            addChangesToCommit(
                    ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                            .withSelection(select, args)
                            .withValue(
                                    ContactsContract.Data.MIMETYPE,
                                    ContactsContract.CommonDataKinds.Organization
                                            .CONTENT_ITEM_TYPE)
                            .withValue(
                                    ContactsContract.CommonDataKinds.Organization.COMPANY, org)
                            .build());
        } else {
            Log.i(
                    "[Contact] Setting organization "
                            + org
                            + " to existing contact "
                            + mAndroidId
                            + " ("
                            + mAndroidRawId
                            + ")");
            addChangesToCommit(
                    ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValue(ContactsContract.Data.RAW_CONTACT_ID, mAndroidRawId)
                            .withValue(
                                    ContactsContract.Data.MIMETYPE,
                                    ContactsContract.CommonDataKinds.Organization
                                            .CONTENT_ITEM_TYPE)
                            .withValue(
                                    ContactsContract.CommonDataKinds.Organization.COMPANY, org)
                            .build());
        }
    }
}
 
Example #26
Source File: ContactsUtils5.java    From CSipSimple with GNU General Public License v3.0 4 votes vote down vote up
private String getContactDataCustomProtocolFilter(String protocol) {
    return String.format(" %s='%s' AND %s=%s AND LOWER(%s)='%s'",
            Data.MIMETYPE, CommonDataKinds.Im.CONTENT_ITEM_TYPE,
            CommonDataKinds.Im.PROTOCOL, CommonDataKinds.Im.PROTOCOL_CUSTOM,
            CommonDataKinds.Im.CUSTOM_PROTOCOL, protocol.toLowerCase());
}
 
Example #27
Source File: ContactsUtils5.java    From CSipSimple with GNU General Public License v3.0 4 votes vote down vote up
public List<Phone> getPhoneNumbers(Context ctxt, long contactId, int flag) {
    String id = Long.toString(contactId);
    ArrayList<Phone> phones = new ArrayList<Phone>();
    Cursor pCur;
    
    if ((flag & ContactsWrapper.URI_NBR) > 0) {
        pCur = ctxt.getContentResolver().query(
                CommonDataKinds.Phone.CONTENT_URI,
                null,
                CommonDataKinds.Phone.CONTACT_ID + " = ?",
                new String[] {
                    id
                }, null);
        while (pCur.moveToNext()) {
            phones.add(new Phone(
                    pCur.getString(pCur
                            .getColumnIndex(CommonDataKinds.Phone.NUMBER)),
                    pCur.getString(pCur.getColumnIndex(CommonDataKinds.Phone.TYPE))
                    ));

        }
        pCur.close();
    }

    // Add any custom IM named 'sip' and set its type to 'sip'
    if ((flag & ContactsWrapper.URI_IM) > 0) {
        pCur = ctxt.getContentResolver().query(
                Data.CONTENT_URI,
                null,
                Data.CONTACT_ID + " = ? AND " + Data.MIMETYPE
                        + " = ?",
                new String[] {
                        id, CommonDataKinds.Im.CONTENT_ITEM_TYPE
                }, null);
        while (pCur.moveToNext()) {
            // Could also use some other IM type but may be confusing. Are there
            // phones with no 'custom' IM type?
            if (pCur.getInt(pCur.getColumnIndex(CommonDataKinds.Im.PROTOCOL)) == CommonDataKinds.Im.PROTOCOL_CUSTOM) {
                String proto = pCur.getString(pCur
                        .getColumnIndex(CommonDataKinds.Im.CUSTOM_PROTOCOL));
                if (SipManager.PROTOCOL_SIP.equalsIgnoreCase(proto) || SipManager.PROTOCOL_CSIP.equalsIgnoreCase(proto)) {
                    phones.add(new Phone(pCur.getString(pCur
                            .getColumnIndex(CommonDataKinds.Im.DATA)), SipManager.PROTOCOL_SIP));
                }
            }

        }
        pCur.close();
    }
    
    // Add any SIP uri if android 9
    if (Compatibility.isCompatible(9) && ((flag & ContactsWrapper.URI_SIP) > 0)) {
        pCur = ctxt.getContentResolver().query(
                Data.CONTENT_URI,
                null,
                Data.CONTACT_ID + " = ? AND " + Data.MIMETYPE
                        + " = ?",
                new String[] {
                        id, CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE
                }, null);
        while (pCur.moveToNext()) {
            // Could also use some other IM type but may be confusing. Are
            // there phones with no 'custom' IM type?
            phones.add(new Phone(pCur.getString(pCur
                    .getColumnIndex(Data.DATA1)), SipManager.PROTOCOL_SIP));
        }
        pCur.close();
    }

    return (phones);
}
 
Example #28
Source File: AndroidContact.java    From linphone-android with GNU General Public License v3.0 4 votes vote down vote up
void setName(String fn, String ln) {
    if ((fn == null || fn.isEmpty()) && (ln == null || ln.isEmpty())) {
        Log.e("[Contact] Can't set both first and last name to null or empty");
        return;
    }

    if (mAndroidId == null) {
        Log.i("[Contact] Setting given & family name " + fn + " " + ln + " to new contact.");
        addChangesToCommit(
                ContentProviderOperation.newInsert(Data.CONTENT_URI)
                        .withValueBackReference(Data.RAW_CONTACT_ID, 0)
                        .withValue(
                                Data.MIMETYPE, CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                        .withValue(CommonDataKinds.StructuredName.GIVEN_NAME, fn)
                        .withValue(CommonDataKinds.StructuredName.FAMILY_NAME, ln)
                        .build());
    } else {
        Log.i(
                "[Contact] Setting given & family name "
                        + fn
                        + " "
                        + ln
                        + " to existing contact "
                        + mAndroidId
                        + " ("
                        + mAndroidRawId
                        + ")");
        String select =
                ContactsContract.Data.CONTACT_ID
                        + "=? AND "
                        + ContactsContract.Data.MIMETYPE
                        + "=?";
        String[] args =
                new String[] {
                    getAndroidId(),
                    ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE
                };

        addChangesToCommit(
                ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                        .withSelection(select, args)
                        .withValue(
                                ContactsContract.Data.MIMETYPE,
                                ContactsContract.CommonDataKinds.StructuredName
                                        .CONTENT_ITEM_TYPE)
                        .withValue(
                                ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, fn)
                        .withValue(
                                ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, ln)
                        .build());
    }
}
 
Example #29
Source File: ContactablesLoaderCallbacks.java    From android-BasicContactables with Apache License 2.0 4 votes vote down vote up
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
    TextView tv  = (TextView) ((Activity)mContext).findViewById(R.id.sample_output);
    if(tv == null) {
        Log.e(TAG, "TextView is null?!");
    } else if (mContext == null) {
        Log.e(TAG, "Context is null?");
    } else {
        Log.e(TAG, "Nothing is null?!");
    }

    // Reset text in case of a previous query
    tv.setText(mContext.getText(R.string.intro_message) + "\n\n");

    if (cursor.getCount() == 0) {
        return;
    }

    // Pulling the relevant value from the cursor requires knowing the column index to pull
    // it from.
    // BEGIN_INCLUDE(get_columns)
    int phoneColumnIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);
    int emailColumnIndex = cursor.getColumnIndex(CommonDataKinds.Email.ADDRESS);
    int nameColumnIndex = cursor.getColumnIndex(CommonDataKinds.Contactables.DISPLAY_NAME);
    int lookupColumnIndex = cursor.getColumnIndex(CommonDataKinds.Contactables.LOOKUP_KEY);
    int typeColumnIndex = cursor.getColumnIndex(CommonDataKinds.Contactables.MIMETYPE);
    // END_INCLUDE(get_columns)

    cursor.moveToFirst();
    // Lookup key is the easiest way to verify a row of data is for the same
    // contact as the previous row.
    String lookupKey = "";
    do {
        // BEGIN_INCLUDE(lookup_key)
        String currentLookupKey = cursor.getString(lookupColumnIndex);
        if (!lookupKey.equals(currentLookupKey)) {
            String displayName = cursor.getString(nameColumnIndex);
            tv.append(displayName + "\n");
            lookupKey = currentLookupKey;
        }
        // END_INCLUDE(lookup_key)

        // BEGIN_INCLUDE(retrieve_data)
        // The data type can be determined using the mime type column.
        String mimeType = cursor.getString(typeColumnIndex);
        if (mimeType.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
            tv.append("\tPhone Number: " + cursor.getString(phoneColumnIndex) + "\n");
        } else if (mimeType.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
            tv.append("\tEmail Address: " + cursor.getString(emailColumnIndex) + "\n");
        }
        // END_INCLUDE(retrieve_data)

        // Look at DDMS to see all the columns returned by a query to Contactables.
        // Behold, the firehose!
        for(String column : cursor.getColumnNames()) {
            Log.d(TAG, column + column + ": " +
                    cursor.getString(cursor.getColumnIndex(column)) + "\n");
        }
    } while (cursor.moveToNext());
}
 
Example #30
Source File: ContactsUtils5.java    From CSipSimple with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean isExternalPhoneNumber(Context context, Cursor cursor) {
    String mimeType = cursor.getString(cursor.getColumnIndex(Data.MIMETYPE));
    return CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equalsIgnoreCase(mimeType);
}