am be97ad65: Merge "Replaced deprecated Contacts Api with ContactsContract"

Merge commit 'be97ad655dedb392caa06ccc6c528b37ba5169fb' into gingerbread-plus-aosp

* commit 'be97ad655dedb392caa06ccc6c528b37ba5169fb':
  Replaced deprecated Contacts Api with ContactsContract
This commit is contained in:
Jean-Baptiste Queru
2010-08-30 10:25:04 -07:00
committed by Android Git Automerger
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_8_right">Right</string>
<string name="linear_layout_10_from">From:</string> <string name="linear_layout_10_from">From:</string>
<string name="linear_layout_10_to">To:</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_snack">Snack</string>
<string name="radio_group_selection">"You have selected: "</string> <string name="radio_group_selection">"You have selected: "</string>
<string name="radio_group_none">(none)</string> <string name="radio_group_none">(none)</string>
@@ -887,8 +886,8 @@
<!-- ============================ --> <!-- ============================ -->
<string name="sms_warning"> <string name="sms_warning">
WARNING: this demo can send actual text messages (one at a time), so be sure to 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. test with the Android emulator or have a text messaging plan with your carrier.
</string> </string>
<string name="sms_enable_receiver">Enable SMS broadcast receiver</string> <string name="sms_enable_receiver">Enable SMS broadcast receiver</string>
<string name="sms_recipient_label">Recipient #</string> <string name="sms_recipient_label">Recipient #</string>
@@ -898,4 +897,3 @@
<string name="reply">Reply</string> <string name="reply">Reply</string>
<string name="dismiss">Dismiss</string> <string name="dismiss">Dismiss</string>
</resources> </resources>

View File

@@ -22,13 +22,15 @@ import android.app.Activity;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.provider.Contacts; import android.provider.ContactsContract.Contacts;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AutoCompleteTextView; import android.widget.AutoCompleteTextView;
import android.widget.CursorAdapter; import android.widget.CursorAdapter;
import android.widget.FilterQueryProvider;
import android.widget.Filterable; import android.widget.Filterable;
import android.widget.TextView; import android.widget.TextView;
@@ -39,8 +41,9 @@ public class AutoComplete4 extends Activity {
setContentView(R.layout.autocomplete_4); setContentView(R.layout.autocomplete_4);
ContentResolver content = getContentResolver(); ContentResolver content = getContentResolver();
Cursor cursor = content.query(Contacts.People.CONTENT_URI, Cursor cursor = content.query(Contacts.CONTENT_URI,
PEOPLE_PROJECTION, null, null, Contacts.People.DEFAULT_SORT_ORDER); CONTACT_PROJECTION, null, null, null);
ContactListAdapter adapter = new ContactListAdapter(this, cursor); ContactListAdapter adapter = new ContactListAdapter(this, cursor);
AutoCompleteTextView textView = (AutoCompleteTextView) AutoCompleteTextView textView = (AutoCompleteTextView)
@@ -61,50 +64,40 @@ public class AutoComplete4 extends Activity {
final LayoutInflater inflater = LayoutInflater.from(context); final LayoutInflater inflater = LayoutInflater.from(context);
final TextView view = (TextView) inflater.inflate( final TextView view = (TextView) inflater.inflate(
android.R.layout.simple_dropdown_item_1line, parent, false); android.R.layout.simple_dropdown_item_1line, parent, false);
view.setText(cursor.getString(5)); view.setText(cursor.getString(COLUMN_DISPLAY_NAME));
return view; return view;
} }
@Override @Override
public void bindView(View view, Context context, Cursor cursor) { public void bindView(View view, Context context, Cursor cursor) {
((TextView) view).setText(cursor.getString(5)); ((TextView) view).setText(cursor.getString(COLUMN_DISPLAY_NAME));
} }
@Override @Override
public String convertToString(Cursor cursor) { public String convertToString(Cursor cursor) {
return cursor.getString(5); return cursor.getString(COLUMN_DISPLAY_NAME);
} }
@Override @Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) { public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
if (getFilterQueryProvider() != null) { FilterQueryProvider filter = getFilterQueryProvider();
return getFilterQueryProvider().runQuery(constraint); if (filter != null) {
return filter.runQuery(constraint);
} }
StringBuilder buffer = null; Uri uri = Uri.withAppendedPath(
String[] args = null; Contacts.CONTENT_FILTER_URI,
if (constraint != null) { Uri.encode(constraint.toString()));
buffer = new StringBuilder(); return mContent.query(uri, CONTACT_PROJECTION, null, null, null);
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);
} }
private ContentResolver mContent; private ContentResolver mContent;
} }
private static final String[] PEOPLE_PROJECTION = new String[] { public static final String[] CONTACT_PROJECTION = new String[] {
Contacts.People._ID, Contacts._ID,
Contacts.People.PRIMARY_PHONE_ID, Contacts.DISPLAY_NAME
Contacts.People.TYPE,
Contacts.People.NUMBER,
Contacts.People.LABEL,
Contacts.People.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.content.ContentResolver;
import android.database.Cursor; import android.database.Cursor;
import android.os.Bundle; import android.os.Bundle;
import android.provider.Contacts; import android.provider.ContactsContract.Contacts;
import android.widget.AutoCompleteTextView; import android.widget.AutoCompleteTextView;
public class AutoComplete5 extends Activity { public class AutoComplete5 extends Activity {
@@ -32,8 +32,8 @@ public class AutoComplete5 extends Activity {
setContentView(R.layout.autocomplete_5); setContentView(R.layout.autocomplete_5);
ContentResolver content = getContentResolver(); ContentResolver content = getContentResolver();
Cursor cursor = content.query(Contacts.People.CONTENT_URI, Cursor cursor = content.query(Contacts.CONTENT_URI,
PEOPLE_PROJECTION, null, null, Contacts.People.DEFAULT_SORT_ORDER); AutoComplete4.CONTACT_PROJECTION, null, null, null);
AutoComplete4.ContactListAdapter adapter = AutoComplete4.ContactListAdapter adapter =
new AutoComplete4.ContactListAdapter(this, cursor); new AutoComplete4.ContactListAdapter(this, cursor);
@@ -41,13 +41,4 @@ public class AutoComplete5 extends Activity {
findViewById(R.id.edit); findViewById(R.id.edit);
textView.setAdapter(adapter); 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; package com.example.android.apis.view;
import android.app.ExpandableListActivity; import android.app.ExpandableListActivity;
import android.content.ContentUris;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle; 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.ExpandableListAdapter;
import android.widget.SimpleCursorTreeAdapter; import android.widget.SimpleCursorTreeAdapter;
/** /**
* Demonstrates expandable lists backed by Cursors * Demonstrates expandable lists backed by Cursors
*/ */
public class ExpandableList2 extends ExpandableListActivity { public class ExpandableList2 extends ExpandableListActivity {
private int mGroupIdColumnIndex; private static final int COLUMN_CONTACT_ID = 0;
private String mPhoneNumberProjection[] = new String[] { private static final String[] CONTACT_PROJECTION = new String[] {
People.Phones._ID, People.Phones.NUMBER Contacts._ID,
Contacts.DISPLAY_NAME
};
private static final String[] PHONE_PROJECTION = new String[] {
Phone._ID,
Phone.CONTACT_ID,
Phone.NUMBER
}; };
private ExpandableListAdapter mAdapter; private ExpandableListAdapter mAdapter;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// Query for people // Query for people
Cursor groupCursor = managedQuery(People.CONTENT_URI, Cursor groupCursor = managedQuery(Contacts.CONTENT_URI,
new String[] {People._ID, People.NAME}, null, null, null); CONTACT_PROJECTION, null, null, null);
// Cache the ID column index
mGroupIdColumnIndex = groupCursor.getColumnIndexOrThrow(People._ID);
// Set up our adapter // Set up our adapter
mAdapter = new MyExpandableListAdapter(groupCursor, mAdapter = new MyExpandableListAdapter(groupCursor,
this, this,
android.R.layout.simple_expandable_list_item_1, android.R.layout.simple_expandable_list_item_1,
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 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}); new int[] {android.R.id.text1});
setListAdapter(mAdapter); setListAdapter(mAdapter);
} }
@@ -75,18 +75,13 @@ public class ExpandableList2 extends ExpandableListActivity {
@Override @Override
protected Cursor getChildrenCursor(Cursor groupCursor) { protected Cursor getChildrenCursor(Cursor groupCursor) {
// Given the group, we return a cursor for all the children within that group int contactId = groupCursor.getInt(COLUMN_CONTACT_ID);
// 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();
// The returned Cursor MUST be managed by us, so we use Activity's helper // The returned Cursor MUST be managed by us, so we use Activity's helper
// functionality to manage it for us. // 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; package com.example.android.apis.view;
import android.app.Activity; import android.app.Activity;
import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.provider.Contacts.People; import android.provider.ContactsContract.Contacts;
import android.os.Bundle; import android.os.Bundle;
import android.widget.Gallery; import android.widget.Gallery;
import android.widget.SimpleCursorAdapter; import android.widget.SimpleCursorAdapter;
@@ -37,16 +36,17 @@ public class Gallery2 extends Activity {
setContentView(R.layout.gallery_2); setContentView(R.layout.gallery_2);
// Get a cursor with all people // 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); startManagingCursor(c);
SpinnerAdapter adapter = new SimpleCursorAdapter(this, SpinnerAdapter adapter = new SimpleCursorAdapter(this,
// Use a template that displays a text view // Use a template that displays a text view
android.R.layout.simple_gallery_item, android.R.layout.simple_gallery_item,
// Give the cursor to the list adatper // Give the cursor to the list adatper
c, c,
// Map the NAME column in the people database to... // 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 // The "text1" view defined in the XML template
new int[] { android.R.id.text1 }); new int[] { android.R.id.text1 });
@@ -54,4 +54,8 @@ public class Gallery2 extends Activity {
g.setAdapter(adapter); g.setAdapter(adapter);
} }
} private static final String[] CONTACT_PROJECTION = new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME
};
}

View File

@@ -18,34 +18,40 @@ package com.example.android.apis.view;
import android.app.ListActivity; import android.app.ListActivity;
import android.database.Cursor; import android.database.Cursor;
import android.provider.Contacts.People; import android.provider.ContactsContract.Contacts;
import android.os.Bundle; import android.os.Bundle;
import android.widget.ListAdapter; import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter; import android.widget.SimpleCursorAdapter;
/** /**
* A list view example where the * A list view example where the
* data comes from a cursor. * data comes from a cursor.
*/ */
public class List2 extends ListActivity { public class List2 extends ListActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// Get a cursor with all people // 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); startManagingCursor(c);
ListAdapter adapter = new SimpleCursorAdapter(this, ListAdapter adapter = new SimpleCursorAdapter(this,
// Use a template that displays a text view // Use a template that displays a text view
android.R.layout.simple_list_item_1, android.R.layout.simple_list_item_1,
// Give the cursor to the list adatper // Give the cursor to the list adatper
c, c,
// Map the NAME column in the people database to... // 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 // The "text1" view defined in the XML template
new int[] {android.R.id.text1}); new int[] {android.R.id.text1});
setListAdapter(adapter); setListAdapter(adapter);
} }
}
private static final String[] CONTACT_PROJECTION = new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME
};
}

View File

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