コード例 #1
0
  /**
   * Get all contacts for a given number
   *
   * @param number
   * @return All contacts with that number.
   */
  public Collection<Contact> contactsByNumber(String number) {
    if (!contactsReadModuleInstalled()) return null;

    number = ContactNumber.cleanNumber(number);
    if (!ContactNumber.isNumber(number)) return null;

    Uri uri = Uri.withAppendedPath(MAXS_PHONE_LOOKUP_CONTENT_FILTER_URI, Uri.encode(number));
    final String[] projection = new String[] {PhoneLookup.LOOKUP_KEY, DISPLAY_NAME};
    Cursor c = mContentResolver.query(uri, projection, null, null, null);

    Map<String, Contact> contactMap = new HashMap<String, Contact>();
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
      String displayName = c.getString(c.getColumnIndexOrThrow(DISPLAY_NAME));
      String lookupKey = c.getString(c.getColumnIndexOrThrow(PhoneLookup.LOOKUP_KEY));

      Contact contact = contactMap.get(lookupKey);
      if (contact == null) {
        contact = new Contact(displayName, lookupKey);
        contactMap.put(lookupKey, contact);
        lookupContactNumbersFor(contact);
      }
    }
    c.close();

    return contactMap.values();
  }
コード例 #2
0
  /**
   * Magical method that tries to find contacts based on a given String.
   *
   * <p>Only returns null if the contacts module is not installed or on error.
   *
   * @param info
   * @return A collection of matching contacts.
   */
  public Collection<Contact> lookupContacts(String info) {
    if (!contactsReadModuleInstalled()) return null;

    String cleanNumber = ContactNumber.cleanNumber(info);
    if (ContactNumber.isNumber(cleanNumber)) return contactsByNumber(cleanNumber);

    Collection<Contact> contacts = contactsByNickname(info);
    if (contacts != null && contacts.size() > 0) return contacts;

    return contactsByName(info);
  }