/** * This function returns the best matching object * * @return structure : { name:... , phone: ... , email: ... } */ @JavascriptInterface public String match(String compare, float minscore) { Cursor cursor = null; try { JSONArray obj = new JSONArray(); cursor = context .getContentResolver() .query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); String ID = null; String name = null; while (cursor.moveToNext()) { String nam = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); float score = Utils.match(nam, compare); if (score > minscore) { minscore = score; ID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); name = nam; } } cursor.close(); cursor = null; if (ID == null) return ""; String phone = getPhone(ID); String email = getEmail(ID); JSONObject jo = new JSONObject(); jo.put("name", name.toLowerCase()); jo.put("phone", phone); jo.put("email", email); return jo.toString(); } catch (Exception e) { Utils.safe(e); } if (cursor != null) { try { cursor.close(); } catch (Exception e) { } } return ""; }
private String getPhone(String ID) { Cursor phones = context .getContentResolver() .query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + ID, null, null); String s = ""; if (phones.moveToNext()) { s = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); } phones.close(); return s; }
private String getEmail(String ID) { Cursor emails = context .getContentResolver() .query( ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + ID, null, null); String s = ""; if (emails.moveToNext()) { s = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); } emails.close(); return s; }
/** * This function returns the full phone directory * * @return a Json array of phone / email fields structure : [ { name:... , phone: ... , email: ... * }, ..... ] */ @JavascriptInterface public String list() { Cursor cursor = null; try { JSONArray obj = new JSONArray(); cursor = context .getContentResolver() .query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); String ID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); String phone = getPhone(ID); String email = getEmail(ID); JSONObject jo = new JSONObject(); jo.put("name", name.toLowerCase()); jo.put("phone", phone); jo.put("email", email); obj.put(jo); } cursor.close(); cursor = null; return obj.toString(); } catch (Exception e) { Utils.safe(e); } if (cursor != null) { try { cursor.close(); } catch (Exception e) { } } return ""; }