Skip to content

Instantly share code, notes, and snippets.

@mgp
Created April 3, 2013 03:41
Show Gist options
  • Save mgp/5298276 to your computer and use it in GitHub Desktop.
Save mgp/5298276 to your computer and use it in GitHub Desktop.
Custom activity to choose a contact with a phone number. But normally you just want new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI).
package com.example;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.MenuItem;
public class ChooseContactActivity extends SherlockListActivity {
static final String CHOSEN_NUMBER_RESULT = "com.example.ChooseContact.ChosenNumber";
private ListAdapter adapter;
private static final String[] FIELDS = new String[] {
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.PHOTO_ID,
};
private static final int[] LAYOUT_IDS = new int[] {
R.id.contact_name,
R.id.contact_photo,
};
private class ContactsCursorAdapter extends SimpleCursorAdapter {
@SuppressWarnings("deprecation")
private ContactsCursorAdapter(Cursor cursor) {
super(ChooseContactActivity.this, R.layout.choose_contact_item, cursor, FIELDS, LAYOUT_IDS);
}
@Override
public void setViewImage(ImageView imageView, String value) {
long id = getCursor().getLong(0);
Bitmap contactPhoto = loadContactPhoto(id);
imageView.setImageBitmap(contactPhoto);
}
// From http://stackoverflow.com/a/4240238/400717.
public Bitmap loadContactPhoto(long id) throws BurnerException {
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(
getContentResolver(), uri);
if (inputStream == null) {
return null;
}
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
try {
inputStream.close();
} catch (IOException e) {
throw new BurnerException(e);
}
return bitmap;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.choose_contact_activity);
Util.setHomeAsButtonAsUp(getSupportActionBar());
setTitle(getString(R.string.choose_contact_title));
populateContactList();
}
private void populateContactList() {
// Build adapter with contact entries.
Cursor cursor = getContactsCursor();
adapter = new ContactsCursorAdapter(cursor);
setListAdapter(adapter);
}
@SuppressWarnings("deprecation")
private Cursor getContactsCursor() {
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID,
};
String selection = new StringBuilder()
.append(ContactsContract.Contacts.IN_VISIBLE_GROUP).append("=?")
.append(" AND ")
.append(ContactsContract.Contacts.HAS_PHONE_NUMBER).append("=?")
.toString();
String[] selectionArgs = new String[] {
"1",
"1",
};
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private String getNumber(long id) {
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.LABEL,
};
String selection = ContactsContract.Data.CONTACT_ID + "=?";
String[] selectionArgs = new String[] {
String.valueOf(id),
};
String number = null;
Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, null);
cursor.moveToFirst();
if (!cursor.isAfterLast()) {
number = cursor.getString(0);
}
cursor.close();
return number;
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String number = getNumber(id);
Intent returnIntent = new Intent();
returnIntent.putExtra(CHOSEN_NUMBER_RESULT, number);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment