@Override public void resultFromActivity(Request request, int resultCode, Intent data) { if (resultCode != Activity.RESULT_OK) { Response response = request.createResponse(Response.ERR_CANCELLED); response.send(); return; } Uri contactData = data.getData(); String contactId = contactData.getLastPathSegment(); Response response = request.createResponse(Response.OK); populateResponse(response, contactId); response.send(); }
private void executeListContacts(Request request) { String query = request.getString("query"); ContentResolver contentResolver = getContext().getContentResolver(); Uri uri = ContactsContract.Data.CONTENT_URI; String selection = ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME + " = ?"; String[] selectionArguments = {query}; Cursor cursor = contentResolver.query(uri, null, selection, selectionArguments, null); Response response; while (cursor.moveToNext()) { String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.CONTACT_ID)); response = request.createResponse(Response.OK); populateResponse(response, contactId); response.send(); } cursor.close(); response = request.createResponse(Response.OK); response.lastForId(request.getRequestId()); response.send(); }
private void populateResponse(Response response, String contactId) { ContentResolver resolver = getContext().getContentResolver(); // Get name String nameGiven = null; String nameMiddle = null; String nameFamily = null; String nameDisplay = null; String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ?"; String[] whereNameParams = new String[] {ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, contactId}; Cursor nameCur = resolver.query( ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME); if (nameCur.moveToNext()) { nameGiven = nameCur.getString( nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)); nameMiddle = nameCur.getString( nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME)); nameFamily = nameCur.getString( nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME)); nameDisplay = nameCur.getString( nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME)); } nameCur.close(); // Get various phone numbers String phoneMain = getPhone(resolver, contactId, ContactsContract.CommonDataKinds.Phone.TYPE_MAIN); String phoneHome = getPhone(resolver, contactId, ContactsContract.CommonDataKinds.Phone.TYPE_HOME); String phoneWork = getPhone(resolver, contactId, ContactsContract.CommonDataKinds.Phone.TYPE_WORK); String phoneMobile = getPhone(resolver, contactId, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE); String phoneOther = getPhone(resolver, contactId, ContactsContract.CommonDataKinds.Phone.TYPE_OTHER); String faxHome = getPhone(resolver, contactId, ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME); String faxWork = getPhone(resolver, contactId, ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK); // Get various email addresses String emailHome = getEmail(resolver, contactId, ContactsContract.CommonDataKinds.Email.TYPE_HOME); String emailWork = getEmail(resolver, contactId, ContactsContract.CommonDataKinds.Email.TYPE_WORK); String emailOther = getEmail(resolver, contactId, ContactsContract.CommonDataKinds.Email.TYPE_OTHER); // Get various addresses String addressHome = getAddress( resolver, contactId, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME); String addressWork = getAddress( resolver, contactId, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK); String addressOther = getAddress( resolver, contactId, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_OTHER); Uri photoUri = getPhotoUri(resolver, contactId); // Send contact response.add("nameGiven", nameGiven); response.add("nameMiddle", nameMiddle); response.add("nameFamily", nameFamily); response.add("nameDisplay", nameDisplay); response.add("phoneMain", phoneMain); response.add("phoneHome", phoneHome); response.add("phoneWork", phoneWork); response.add("phoneMobile", phoneMobile); response.add("phoneOther", phoneOther); response.add("faxHome", faxHome); response.add("faxWork", faxWork); response.add("emailHome", emailHome); response.add("emailWork", emailWork); response.add("emailOther", emailOther); response.add("addressHome", addressHome); response.add("addressWork", addressWork); response.add("addressOther", addressOther); if (photoUri != null) { Log.d("WAB", photoUri.toString()); response.add( "photoUri", HTTPServer.genResourceUri( this.getConnectionInfo().getToken(), photoUri.toString(), "image/png")); } }