private static List<Result> queryContacts(ContentResolver resolver,String input,HashSet<String> addressesRetVal){
  String where=null;
  String[] whereArgs=null;
  if (!TextUtils.isEmpty(input)) {
    where=CONTACTS_WHERE;
    String param1=input + "%";
    String param2="% " + input + "%";
    whereArgs=new String[]{param1,param2,param1,param2};
  }
  Cursor c=resolver.query(CommonDataKinds.StructuredPostal.CONTENT_URI,CONTACTS_PROJECTION,where,whereArgs,Contacts.DISPLAY_NAME + " ASC");
  try {
    Map<String,List<Result>> nameToAddresses=new HashMap<String,List<Result>>();
    c.moveToPosition(-1);
    while (c.moveToNext()) {
      String name=c.getString(CONTACTS_INDEX_DISPLAY_NAME);
      String address=c.getString(CONTACTS_INDEX_ADDRESS);
      if (name != null) {
        List<Result> addressesForName=nameToAddresses.get(name);
        Result result;
        if (addressesForName == null) {
          Uri contactPhotoUri=null;
          if (c.getLong(CONTACTS_INDEX_PHOTO_ID) > 0) {
            contactPhotoUri=ContentUris.withAppendedId(Contacts.CONTENT_URI,c.getLong(CONTACTS_INDEX_CONTACT_ID));
          }
          addressesForName=new ArrayList<Result>();
          nameToAddresses.put(name,addressesForName);
          result=new Result(name,address,R.drawable.ic_contact_picture,contactPhotoUri);
        }
 else {
          result=new Result(null,address,null,null);
        }
        addressesForName.add(result);
        addressesRetVal.add(address);
      }
    }
    List<Result> allResults=new ArrayList<Result>();
    for (    List<Result> result : nameToAddresses.values()) {
      allResults.addAll(result);
    }
    return allResults;
  }
  finally {
    if (c != null) {
      c.close();
    }
  }
}

private static List < Result > queryContacts ( ContentResolver resolver , String input , HashSet < String > addressesRetVal ) { String where = null ; String [ ] whereArgs = null ; if ( ! TextUtils . isEmpty ( input ) ) { where = CONTACTS_WHERE ; String param1 = input + "%" ; String param2 = "% " + input + "%" ; whereArgs = new String [ ] { param1 , param2 , param1 , param2 } ; } Cursor c = resolver . query ( CommonDataKinds . StructuredPostal . CONTENT_URI , CONTACTS_PROJECTION , where , whereArgs , Contacts . DISPLAY_NAME + " ASC" ) ; try { Map < String , List < Result > > nameToAddresses = new HashMap < String , List < Result > > ( ) ; c . moveToPosition ( - 1 ) ; while ( c . moveToNext ( ) ) { String name = c . getString ( CONTACTS_INDEX_DISPLAY_NAME ) ; String address = c . getString ( CONTACTS_INDEX_ADDRESS ) ; if ( name != null ) { List < Result > addressesForName = nameToAddresses . get ( name ) ; Result result ; if ( addressesForName == null ) { Uri contactPhotoUri = null ; if ( c . getLong ( CONTACTS_INDEX_PHOTO_ID ) > 0 ) { contactPhotoUri = ContentUris . withAppendedId ( Contacts . CONTENT_URI , c . getLong ( CONTACTS_INDEX_CONTACT_ID ) ) ; } addressesForName = new ArrayList < Result > ( ) ; nameToAddresses . put ( name , addressesForName ) ; result = new Result ( name , address , R . drawable . ic_contact_picture , contactPhotoUri ) ; } else { result = new Result ( null , address , null , null ) ; } addressesForName . add ( result ) ; addressesRetVal . add ( address ) ; } } List < Result > allResults = new ArrayList < Result > ( ) ; for ( List < Result > result : nameToAddresses . values ( ) ) { allResults . addAll ( result ) ; } return allResults ; } finally { if ( c != null ) { c . close ( ) ; } } }
matches the input string against contacts names and addresses .
