diff --git a/samples/XmlAdapters/Android.mk b/samples/XmlAdapters/Android.mk new file mode 100644 index 000000000..24a332740 --- /dev/null +++ b/samples/XmlAdapters/Android.mk @@ -0,0 +1,16 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := samples + +# Only compile source java files in this apk. +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +LOCAL_PACKAGE_NAME := XmlAdaptersSample + +LOCAL_PROGUARD_ENABLED := disabled + +include $(BUILD_PACKAGE) + +# Use the following include to make our test apk. +include $(call all-makefiles-under,$(LOCAL_PATH)) diff --git a/samples/XmlAdapters/AndroidManifest.xml b/samples/XmlAdapters/AndroidManifest.xml new file mode 100644 index 000000000..af040adff --- /dev/null +++ b/samples/XmlAdapters/AndroidManifest.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + diff --git a/samples/XmlAdapters/res/drawable-hdpi/ic_contact_picture.png b/samples/XmlAdapters/res/drawable-hdpi/ic_contact_picture.png new file mode 100644 index 000000000..a60565abe Binary files /dev/null and b/samples/XmlAdapters/res/drawable-hdpi/ic_contact_picture.png differ diff --git a/samples/XmlAdapters/res/layout/contact_item.xml b/samples/XmlAdapters/res/layout/contact_item.xml new file mode 100644 index 000000000..6fcb10908 --- /dev/null +++ b/samples/XmlAdapters/res/layout/contact_item.xml @@ -0,0 +1,39 @@ + + + + + + + + + + diff --git a/samples/XmlAdapters/res/layout/contacts_list.xml b/samples/XmlAdapters/res/layout/contacts_list.xml new file mode 100644 index 000000000..973fe4be1 --- /dev/null +++ b/samples/XmlAdapters/res/layout/contacts_list.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + diff --git a/samples/XmlAdapters/res/values/strings.xml b/samples/XmlAdapters/res/values/strings.xml new file mode 100644 index 000000000..3a0b5fed9 --- /dev/null +++ b/samples/XmlAdapters/res/values/strings.xml @@ -0,0 +1,20 @@ + + + + + Xml Contacts Adapter + No contacts available + diff --git a/samples/XmlAdapters/res/xml/contacts.xml b/samples/XmlAdapters/res/xml/contacts.xml new file mode 100644 index 000000000..b33d948b5 --- /dev/null +++ b/samples/XmlAdapters/res/xml/contacts.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + diff --git a/samples/XmlAdapters/src/com/example/android/xmladapters/ContactPhotoBinder.java b/samples/XmlAdapters/src/com/example/android/xmladapters/ContactPhotoBinder.java new file mode 100644 index 000000000..947eb2ab3 --- /dev/null +++ b/samples/XmlAdapters/src/com/example/android/xmladapters/ContactPhotoBinder.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.xmladapters; + +import android.content.ContentUris; +import android.content.Context; +import android.content.res.Resources; +import android.database.Cursor; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.provider.ContactsContract; +import android.view.View; +import android.widget.Adapters; +import android.widget.TextView; + +import java.io.InputStream; +import java.util.HashMap; + +/** + * This custom cursor binder is used by the adapter defined in res/xml to + * bin contacts photos to their respective list item. This binder simply + * queries a contact's photo based on the contact's id and sets the + * photo as a compound drawable on the TextView used to display the contact's + * name. + */ +public class ContactPhotoBinder extends Adapters.CursorBinder { + private static final int PHOTO_SIZE_DIP = 54; + + private final Drawable mDefault; + private final HashMap mCache; + private final Resources mResources; + private final int mPhotoSize; + + public ContactPhotoBinder(Context context, Adapters.CursorTransformation transformation) { + super(context, transformation); + + mResources = mContext.getResources(); + // Default picture used when a contact does not provide one + mDefault = mResources.getDrawable(R.drawable.ic_contact_picture); + // Cache used to avoid requerying contacts photos every time + mCache = new HashMap(); + // Compute the size of the photo based on the display's density + mPhotoSize = (int) (PHOTO_SIZE_DIP * mResources.getDisplayMetrics().density + 0.5f); + } + + @Override + public boolean bind(View view, Cursor cursor, int columnIndex) { + final long id = cursor.getLong(columnIndex); + + // First check whether we have already cached the contact's photo + Drawable d = mCache.get(id); + + if (d == null) { + // If the photo wasn't in the cache, ask the contacts provider for + // an input stream we can use to load the photo + Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id); + InputStream stream = ContactsContract.Contacts.openContactPhotoInputStream( + mContext.getContentResolver(), uri); + + // Creates the drawable for the contact's photo or use our fallback drawable + if (stream != null) { + Bitmap bitmap = BitmapFactory.decodeStream(stream); + d = new BitmapDrawable(mResources, bitmap); + } else { + d = mDefault; + } + + d.setBounds(0, 0, mPhotoSize, mPhotoSize); + ((TextView) view).setCompoundDrawables(d, null, null, null); + + // Remember the photo associated with this contact + mCache.put(id, d); + } else { + ((TextView) view).setCompoundDrawables(d, null, null, null); + } + + return true; + } +} diff --git a/samples/XmlAdapters/src/com/example/android/xmladapters/ContactsListActivity.java b/samples/XmlAdapters/src/com/example/android/xmladapters/ContactsListActivity.java new file mode 100644 index 000000000..bf5ab58bc --- /dev/null +++ b/samples/XmlAdapters/src/com/example/android/xmladapters/ContactsListActivity.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.xmladapters; + +import android.app.ListActivity; +import android.os.Bundle; + +/** + * This activity demonstrates how to create a complex UI using a ListView + * and an adapter defined in XML. + * + * The following activity shows a list of contacts, their starred status + * and their photos, using the adapter defined in res/xml. + */ +public class ContactsListActivity extends ListActivity { + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.contacts_list); + } +}