public MessageHeader(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; mContacts = Contacts.getInstance(mContext); }
/** * Get instance of the Contacts class. * * <p>Note: This is left over from the days when we needed to have SDK-specific code to access * the contacts.</p> * * @param context A {@link Context} instance. * @return Appropriate {@link Contacts} instance for this device. */ public static Contacts getInstance(Context context) { return new Contacts(context); }
@Override protected void onStart() { super.onStart(); Contacts.clearCache(); }
/** * Does the device actually have a Contacts application suitable for * picking a contact. As hard as it is to believe, some vendors ship * without it. * * @return True, if the device supports picking contacts. False, otherwise. */ private boolean hasContactPicker() { if (hasContactPicker == null) { Contacts contacts = Contacts.getInstance(context); PackageManager packageManager = context.getPackageManager(); List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(contacts.contactPickerIntent(), 0); hasContactPicker = !resolveInfoList.isEmpty(); } return hasContactPicker; }
private boolean shouldShowPicturesFromSender(ShowPictures showPicturesSetting, Message message) { if (showPicturesSetting != ShowPictures.ONLY_FROM_CONTACTS) { return false; } String senderEmailAddress = getSenderEmailAddress(message); if (senderEmailAddress == null) { return false; } Contacts contacts = Contacts.getInstance(getContext()); return contacts.isInContacts(senderEmailAddress); }
@Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case MENU_ITEM_PHONE_CALL: { Uri uri = Uri.parse(WebView.SCHEME_TEL + phoneNumber); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivityIfAvailable(getContext(), intent); break; } case MENU_ITEM_PHONE_SAVE: { Contacts contacts = Contacts.getInstance(getContext()); contacts.addPhoneContact(phoneNumber); break; } case MENU_ITEM_PHONE_COPY: { String label = getContext().getString( R.string.webview_contextmenu_phone_clipboard_label); mClipboardManager.setText(label, phoneNumber); break; } } return true; } };
@Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case MENU_ITEM_EMAIL_SEND: { Uri uri = Uri.parse(WebView.SCHEME_MAILTO + email); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivityIfAvailable(getContext(), intent); break; } case MENU_ITEM_EMAIL_SAVE: { Contacts contacts = Contacts.getInstance(getContext()); contacts.createContact(new Address(email)); break; } case MENU_ITEM_EMAIL_COPY: { String label = getContext().getString( R.string.webview_contextmenu_email_clipboard_label); mClipboardManager.setText(label, email); break; } } return true; } };
.withTag(account); Uri photoUri = Contacts.getInstance(parent).getPhotoUri(account.getEmail()); if (photoUri != null && !photoUris.contains(photoUri)) { photoUris.add(photoUri);
/** * Check whether one of the provided addresses belongs to one of the contacts. * * @param addresses The addresses to search in contacts * @return <tt>true</tt>, if one address belongs to a contact. * <tt>false</tt>, otherwise. */ public boolean isAnyInContacts(final Address[] addresses) { if (addresses == null) { return false; } for (Address addr : addresses) { if (isInContacts(addr.getAddress())) { return true; } } return false; }
/** * Check whether the provided email address belongs to one of the contacts. * * @param emailAddress The email address to look for. * @return <tt>true</tt>, if the email address belongs to a contact. * <tt>false</tt>, otherwise. */ public boolean isInContacts(final String emailAddress) { boolean result = false; final Cursor c = getContactByAddress(emailAddress); if (c != null) { if (c.getCount() > 0) { result = true; } c.close(); } return result; }
public void showContactPicker(int requestCode) { requestCode |= REQUEST_MASK_RECIPIENT_PRESENTER; isInSubActivity = true; startActivityForResult(contacts.contactPickerIntent(), requestCode); }
private void onAddSenderToContacts() { if (mMessage != null) { try { final Address senderEmail = mMessage.getFrom()[0]; mContacts.createContact(senderEmail); } catch (Exception e) { Timber.e(e, "Couldn't create contact"); } } }
/* package, for testing */ static CharSequence toFriendly(Address address, Contacts contacts, boolean showCorrespondentNames, boolean changeContactNameColor, int contactNameColor) { if (!showCorrespondentNames) { return address.getAddress(); } else if (contacts != null) { final String name = contacts.getNameForAddress(address.getAddress()); if (name != null) { if (changeContactNameColor) { final SpannableString coloredName = new SpannableString(name); coloredName.setSpan(new ForegroundColorSpan(contactNameColor), 0, coloredName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); return coloredName; } else { return name; } } } if (!TextUtils.isEmpty(address.getPersonal()) && !isSpoofAddress(address.getPersonal())) { return address.getPersonal(); } else { return address.getAddress(); } }
/** * Get the name of the contact an email address belongs to. * * @param address The email address to search for. * @return The name of the contact the email address belongs to. Or * <tt>null</tt> if there's no matching contact. */ public String getNameForAddress(String address) { if (address == null) { return null; } else if (nameCache.containsKey(address)) { return nameCache.get(address); } final Cursor c = getContactByAddress(address); String name = null; if (c != null) { if (c.getCount() > 0) { c.moveToFirst(); name = c.getString(NAME_INDEX); } c.close(); } nameCache.put(address, name); return name; }
public CharSequence getDisplayName(Account account, Address[] fromAddrs, Address[] toAddrs) { final Contacts contactHelper = K9.showContactName() ? Contacts.getInstance(mContext) : null; CharSequence displayName; if (fromAddrs.length > 0 && account.isAnIdentity(fromAddrs[0])) { CharSequence to = toFriendly(toAddrs, contactHelper); displayName = new SpannableStringBuilder( resourceProvider.contactDisplayNamePrefix()).append(to); } else { displayName = toFriendly(fromAddrs, contactHelper); } return displayName; }
/** * Start the activity to add a phone number to an existing contact or add a new one. * * @param phoneNumber * The phone number to add to a contact, or to use when creating a new contact. */ public void addPhoneContact(final String phoneNumber) { Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT); addIntent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE); addIntent.putExtra(ContactsContract.Intents.Insert.PHONE, Uri.decode(phoneNumber)); addIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mContext.startActivity(addIntent); clearCache(); }
@Before public void setUp() throws Exception { Context context = RuntimeEnvironment.application; contacts = new Contacts(context); contactsWithFakeContact = new Contacts(context) { @Override public String getNameForAddress(String address) { if ("test@testor.com".equals(address)) { return "Tim Testor"; } else { return null; } } }; contactsWithFakeSpoofContact = new Contacts(context) { @Override public String getNameForAddress(String address) { if ("test@testor.com".equals(address)) { return "Tim@Testor"; } else { return null; } } }; }
/** * Mark contacts with the provided email addresses as contacted. * * @param addresses Array of {@link Address} objects describing the * contacts to be marked as contacted. */ public void markAsContacted(final Address[] addresses) { //TODO: Optimize! Potentially a lot of database queries for (final Address address : addresses) { final Cursor c = getContactByAddress(address.getAddress()); if (c != null) { if (c.getCount() > 0) { c.moveToFirst(); final long personId = c.getLong(CONTACT_ID_INDEX); ContactsContract.Contacts.markAsContacted(mContentResolver, personId); } c.close(); } } }
private String getMessageSender(Account account, Message message) { boolean isSelf = false; final Contacts contacts = K9.showContactName() ? Contacts.getInstance(context) : null; final Address[] fromAddresses = message.getFrom(); if (fromAddresses != null) { isSelf = account.isAnIdentity(fromAddresses); if (!isSelf && fromAddresses.length > 0) { return MessageHelper.toFriendly(fromAddresses[0], contacts).toString(); } } if (isSelf) { // show To: if the message was sent from me Address[] recipients = message.getRecipients(Message.RecipientType.TO); if (recipients != null && recipients.length > 0) { String recipientDisplayName = MessageHelper.toFriendly(recipients[0], contacts).toString(); return resourceProvider.recipientDisplayName(recipientDisplayName); } } return null; }
/** * Start the activity to add information to an existing contact or add a * new one. * * @param email An {@link Address} instance containing the email address * of the entity you want to add to the contacts. Optionally * the instance also contains the (display) name of that * entity. */ public void createContact(final Address email) { final Uri contactUri = Uri.fromParts("mailto", email.getAddress(), null); final Intent contactIntent = new Intent(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT); contactIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); contactIntent.setData(contactUri); // Pass along full email string for possible create dialog contactIntent.putExtra(ContactsContract.Intents.EXTRA_CREATE_DESCRIPTION, email.toString()); // Only provide personal name hint if we have one final String senderPersonal = email.getPersonal(); if (senderPersonal != null) { contactIntent.putExtra(ContactsContract.Intents.Insert.NAME, senderPersonal); } mContext.startActivity(contactIntent); clearCache(); }