Merge "Replaced deprecated Contacts Api with ContactsContract"

This commit is contained in:
Jean-Baptiste Queru
2010-08-30 09:04:19 -07:00
committed by Android Code Review
8 changed files with 166 additions and 148 deletions

View File

@@ -506,7 +506,6 @@
<string name="linear_layout_8_right">Right</string>
<string name="linear_layout_10_from">From:</string>
<string name="linear_layout_10_to">To:</string>
<string name="list_7_nothing">Nothing\u2026</string>
<string name="radio_group_snack">Snack</string>
<string name="radio_group_selection">"You have selected: "</string>
<string name="radio_group_none">(none)</string>
@@ -887,8 +886,8 @@
<!-- ============================ -->
<string name="sms_warning">
WARNING: this demo can send actual text messages (one at a time), so be sure to
test with the Android emulator or have a text messaging plan with your carrier.
WARNING: this demo can send actual text messages (one at a time), so be sure to
test with the Android emulator or have a text messaging plan with your carrier.
</string>
<string name="sms_enable_receiver">Enable SMS broadcast receiver</string>
<string name="sms_recipient_label">Recipient #</string>
@@ -898,4 +897,3 @@
<string name="reply">Reply</string>
<string name="dismiss">Dismiss</string>
</resources>

View File

@@ -22,13 +22,15 @@ import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.ContactsContract.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.CursorAdapter;
import android.widget.FilterQueryProvider;
import android.widget.Filterable;
import android.widget.TextView;
@@ -39,8 +41,9 @@ public class AutoComplete4 extends Activity {
setContentView(R.layout.autocomplete_4);
ContentResolver content = getContentResolver();
Cursor cursor = content.query(Contacts.People.CONTENT_URI,
PEOPLE_PROJECTION, null, null, Contacts.People.DEFAULT_SORT_ORDER);
Cursor cursor = content.query(Contacts.CONTENT_URI,
CONTACT_PROJECTION, null, null, null);
ContactListAdapter adapter = new ContactListAdapter(this, cursor);
AutoCompleteTextView textView = (AutoCompleteTextView)
@@ -61,50 +64,40 @@ public class AutoComplete4 extends Activity {
final LayoutInflater inflater = LayoutInflater.from(context);
final TextView view = (TextView) inflater.inflate(
android.R.layout.simple_dropdown_item_1line, parent, false);
view.setText(cursor.getString(5));
view.setText(cursor.getString(COLUMN_DISPLAY_NAME));
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
((TextView) view).setText(cursor.getString(5));
((TextView) view).setText(cursor.getString(COLUMN_DISPLAY_NAME));
}
@Override
public String convertToString(Cursor cursor) {
return cursor.getString(5);
return cursor.getString(COLUMN_DISPLAY_NAME);
}
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
if (getFilterQueryProvider() != null) {
return getFilterQueryProvider().runQuery(constraint);
FilterQueryProvider filter = getFilterQueryProvider();
if (filter != null) {
return filter.runQuery(constraint);
}
StringBuilder buffer = null;
String[] args = null;
if (constraint != null) {
buffer = new StringBuilder();
buffer.append("UPPER(");
buffer.append(Contacts.ContactMethods.NAME);
buffer.append(") GLOB ?");
args = new String[] { constraint.toString().toUpperCase() + "*" };
}
return mContent.query(Contacts.People.CONTENT_URI, PEOPLE_PROJECTION,
buffer == null ? null : buffer.toString(), args,
Contacts.People.DEFAULT_SORT_ORDER);
Uri uri = Uri.withAppendedPath(
Contacts.CONTENT_FILTER_URI,
Uri.encode(constraint.toString()));
return mContent.query(uri, CONTACT_PROJECTION, null, null, null);
}
private ContentResolver mContent;
}
private static final String[] PEOPLE_PROJECTION = new String[] {
Contacts.People._ID,
Contacts.People.PRIMARY_PHONE_ID,
Contacts.People.TYPE,
Contacts.People.NUMBER,
Contacts.People.LABEL,
Contacts.People.NAME,
public static final String[] CONTACT_PROJECTION = new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME
};
private static final int COLUMN_DISPLAY_NAME = 1;
}

View File

@@ -22,7 +22,7 @@ import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.ContactsContract.Contacts;
import android.widget.AutoCompleteTextView;
public class AutoComplete5 extends Activity {
@@ -32,8 +32,8 @@ public class AutoComplete5 extends Activity {
setContentView(R.layout.autocomplete_5);
ContentResolver content = getContentResolver();
Cursor cursor = content.query(Contacts.People.CONTENT_URI,
PEOPLE_PROJECTION, null, null, Contacts.People.DEFAULT_SORT_ORDER);
Cursor cursor = content.query(Contacts.CONTENT_URI,
AutoComplete4.CONTACT_PROJECTION, null, null, null);
AutoComplete4.ContactListAdapter adapter =
new AutoComplete4.ContactListAdapter(this, cursor);
@@ -41,13 +41,4 @@ public class AutoComplete5 extends Activity {
findViewById(R.id.edit);
textView.setAdapter(adapter);
}
private static final String[] PEOPLE_PROJECTION = new String[] {
Contacts.People._ID,
Contacts.People.PRIMARY_PHONE_ID,
Contacts.People.TYPE,
Contacts.People.NUMBER,
Contacts.People.LABEL,
Contacts.People.NAME
};
}

View File

@@ -17,49 +17,49 @@
package com.example.android.apis.view;
import android.app.ExpandableListActivity;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleCursorTreeAdapter;
/**
* Demonstrates expandable lists backed by Cursors
*/
public class ExpandableList2 extends ExpandableListActivity {
private int mGroupIdColumnIndex;
private static final int COLUMN_CONTACT_ID = 0;
private String mPhoneNumberProjection[] = new String[] {
People.Phones._ID, People.Phones.NUMBER
private static final String[] CONTACT_PROJECTION = new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME
};
private static final String[] PHONE_PROJECTION = new String[] {
Phone._ID,
Phone.CONTACT_ID,
Phone.NUMBER
};
private ExpandableListAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Query for people
Cursor groupCursor = managedQuery(People.CONTENT_URI,
new String[] {People._ID, People.NAME}, null, null, null);
// Cache the ID column index
mGroupIdColumnIndex = groupCursor.getColumnIndexOrThrow(People._ID);
Cursor groupCursor = managedQuery(Contacts.CONTENT_URI,
CONTACT_PROJECTION, null, null, null);
// Set up our adapter
mAdapter = new MyExpandableListAdapter(groupCursor,
this,
android.R.layout.simple_expandable_list_item_1,
android.R.layout.simple_expandable_list_item_1,
new String[] {People.NAME}, // Name for group layouts
new String[] {Contacts.DISPLAY_NAME}, // Name for group layouts
new int[] {android.R.id.text1},
new String[] {People.NUMBER}, // Number for child layouts
new String[] {Phone.NUMBER}, // Number for child layouts
new int[] {android.R.id.text1});
setListAdapter(mAdapter);
}
@@ -75,18 +75,13 @@ public class ExpandableList2 extends ExpandableListActivity {
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
// Given the group, we return a cursor for all the children within that group
// Return a cursor that points to this contact's phone numbers
Uri.Builder builder = People.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, groupCursor.getLong(mGroupIdColumnIndex));
builder.appendEncodedPath(People.Phones.CONTENT_DIRECTORY);
Uri phoneNumbersUri = builder.build();
int contactId = groupCursor.getInt(COLUMN_CONTACT_ID);
// The returned Cursor MUST be managed by us, so we use Activity's helper
// functionality to manage it for us.
return managedQuery(phoneNumbersUri, mPhoneNumberProjection, null, null, null);
return managedQuery(Phone.CONTENT_URI,
PHONE_PROJECTION,
Phone.CONTACT_ID + " = " + contactId,
null, null);
}
}
}

View File

@@ -17,9 +17,8 @@
package com.example.android.apis.view;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.provider.Contacts.People;
import android.provider.ContactsContract.Contacts;
import android.os.Bundle;
import android.widget.Gallery;
import android.widget.SimpleCursorAdapter;
@@ -37,7 +36,8 @@ public class Gallery2 extends Activity {
setContentView(R.layout.gallery_2);
// Get a cursor with all people
Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
Cursor c = getContentResolver().query(Contacts.CONTENT_URI,
CONTACT_PROJECTION, null, null, null);
startManagingCursor(c);
SpinnerAdapter adapter = new SimpleCursorAdapter(this,
@@ -46,7 +46,7 @@ public class Gallery2 extends Activity {
// Give the cursor to the list adatper
c,
// Map the NAME column in the people database to...
new String[] {People.NAME},
new String[] {Contacts.DISPLAY_NAME},
// The "text1" view defined in the XML template
new int[] { android.R.id.text1 });
@@ -54,4 +54,8 @@ public class Gallery2 extends Activity {
g.setAdapter(adapter);
}
private static final String[] CONTACT_PROJECTION = new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME
};
}

View File

@@ -18,7 +18,7 @@ package com.example.android.apis.view;
import android.app.ListActivity;
import android.database.Cursor;
import android.provider.Contacts.People;
import android.provider.ContactsContract.Contacts;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
@@ -34,7 +34,8 @@ public class List2 extends ListActivity {
super.onCreate(savedInstanceState);
// Get a cursor with all people
Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
Cursor c = getContentResolver().query(Contacts.CONTENT_URI,
CONTACT_PROJECTION, null, null, null);
startManagingCursor(c);
ListAdapter adapter = new SimpleCursorAdapter(this,
@@ -43,9 +44,14 @@ public class List2 extends ListActivity {
// Give the cursor to the list adatper
c,
// Map the NAME column in the people database to...
new String[] {People.NAME} ,
new String[] {Contacts.DISPLAY_NAME},
// The "text1" view defined in the XML template
new int[] {android.R.id.text1});
setListAdapter(adapter);
}
private static final String[] CONTACT_PROJECTION = new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME
};
}

View File

@@ -16,13 +16,13 @@
package com.example.android.apis.view;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.Phones;
import android.widget.ListAdapter;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.View;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
/**
* A list view example where the
@@ -37,15 +37,48 @@ public class List3 extends ListActivity {
super.onCreate(savedInstanceState);
// Get a cursor with all phones
Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null, null, null);
Cursor c = getContentResolver().query(Phone.CONTENT_URI,
PHONE_PROJECTION, null, null, null);
startManagingCursor(c);
// Map Cursor columns to views defined in simple_list_item_2.xml
ListAdapter adapter = new SimpleCursorAdapter(this,
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, c,
new String[] { Phones.NAME, Phones.NUMBER },
new String[] {
Phone.TYPE,
Phone.NUMBER
},
new int[] { android.R.id.text1, android.R.id.text2 });
//Used to display a readable string for the phone type
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
//Let the adapter handle the binding if the column is not TYPE
if (columnIndex != COLUMN_TYPE) {
return false;
}
int type = cursor.getInt(COLUMN_TYPE);
String label = null;
//Custom type? Then get the custom label
if (type == Phone.TYPE_CUSTOM) {
label = cursor.getString(COLUMN_LABEL);
}
//Get the readable string
String text = (String) Phone.getTypeLabel(getResources(), type, label);
//Set text
((TextView) view).setText(text);
return true;
}
});
setListAdapter(adapter);
}
private static final String[] PHONE_PROJECTION = new String[] {
Phone._ID,
Phone.TYPE,
Phone.LABEL,
Phone.NUMBER
};
private static final int COLUMN_TYPE = 1;;
private static final int COLUMN_LABEL = 2;
}

View File

@@ -16,12 +16,15 @@
package com.example.android.apis.view;
// Need the following import to get access to the app resources, since this
// class is in a sub-package.
import com.example.android.apis.R;
import android.app.ListActivity;
import android.database.Cursor;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
@@ -33,69 +36,64 @@ import android.widget.TextView;
* A list view example where the data comes from a cursor.
*/
public class List7 extends ListActivity implements OnItemSelectedListener {
private static final String[] PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.Contacts.LOOKUP_KEY
};
private int mIdColumnIndex;
private int mHasPhoneColumnIndex;
private TextView mPhone;
private static final String[] PHONE_PROJECTION = new String[] {
Phone._ID,
Phone.TYPE,
Phone.LABEL,
Phone.NUMBER,
Phone.DISPLAY_NAME
};
private static final int COLUMN_PHONE_TYPE = 1;
private static final int COLUMN_PHONE_LABEL = 2;
private static final int COLUMN_PHONE_NUMBER = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_7);
mPhone = (TextView) findViewById(R.id.phone);
getListView().setOnItemSelectedListener(this);
// Get a cursor with all people
Cursor c = managedQuery(ContactsContract.Contacts.CONTENT_URI,
PROJECTION, null, null, null);
mIdColumnIndex = c.getColumnIndex(ContactsContract.Contacts._ID);
mHasPhoneColumnIndex = c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
// Get a cursor with all numbers.
// This query will only return contacts with phone numbers
Cursor c = getContentResolver().query(Phone.CONTENT_URI,
PHONE_PROJECTION, Phone.NUMBER + " NOT NULL", null, null);
startManagingCursor(c);
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, // Use a template
// that displays a
// text view
c, // Give the cursor to the list adapter
new String[] { ContactsContract.Contacts.DISPLAY_NAME }, // Map the NAME column in the
// people database to...
new int[] { android.R.id.text1 }); // The "text1" view defined in
// the XML template
// Use a template that displays a text view
android.R.layout.simple_list_item_1,
// Give the cursor to the list adapter
c,
// Map the DISPLAY_NAME column to...
new String[] {Phone.DISPLAY_NAME},
// The "text1" view defined in the XML template
new int[] {android.R.id.text1});
setListAdapter(adapter);
}
public void onItemSelected(AdapterView parent, View v, int position, long id) {
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
if (position >= 0) {
final Cursor c = (Cursor) parent.getItemAtPosition(position);
if (c.getInt(mHasPhoneColumnIndex) > 0) {
final long contactId = c.getLong(mIdColumnIndex);
final Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER },
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null,
ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY + " DESC");
try {
phones.moveToFirst();
mPhone.setText(phones.getString(0));
} finally {
phones.close();
}
} else {
mPhone.setText(R.string.list_7_nothing);
//Get current cursor
Cursor c = (Cursor) parent.getItemAtPosition(position);
int type = c.getInt(COLUMN_PHONE_TYPE);
String phone = c.getString(COLUMN_PHONE_NUMBER);
String label = null;
//Custom type? Then get the custom label
if (type == Phone.TYPE_CUSTOM) {
label = c.getString(COLUMN_PHONE_LABEL);
}
//Get the readable string
String numberType = (String) Phone.getTypeLabel(getResources(), type, label);
String text = numberType + ": " + phone;
mPhone.setText(text);
}
}
public void onNothingSelected(AdapterView parent) {
mPhone.setText(R.string.list_7_nothing);
public void onNothingSelected(AdapterView<?> parent) {
}
}