2012年4月12日 星期四

[Android]QuickContact 快速找到聯絡人(有顯示頭像照片功能)





<QuickContact.java>

   1: package com.android.quickcontact;
   2:  
   3: import java.io.InputStream;
   4:  
   5: import android.app.ListActivity;
   6: import android.content.ContentUris;
   7: import android.content.Context;
   8: import android.database.CharArrayBuffer;
   9: import android.database.Cursor;
  10: import android.graphics.Bitmap;
  11: import android.graphics.BitmapFactory;
  12: import android.net.Uri;
  13: import android.os.Bundle;
  14: import android.provider.ContactsContract.Contacts;
  15: import android.util.DisplayMetrics;
  16: import android.view.View;
  17: import android.view.ViewGroup;
  18: import android.widget.QuickContactBadge;
  19: import android.widget.ResourceCursorAdapter;
  20: import android.widget.TextView;
  21:  
  22: public class QuickContact extends ListActivity {
  23:     static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
  24:             Contacts._ID, // 0
  25:             Contacts.DISPLAY_NAME, // 1
  26:             Contacts.STARRED, // 2
  27:             Contacts.TIMES_CONTACTED, // 3
  28:             Contacts.CONTACT_PRESENCE, // 4
  29:             Contacts.PHOTO_ID, // 5
  30:             Contacts.LOOKUP_KEY, // 6
  31:             Contacts.HAS_PHONE_NUMBER, // 7
  32:     };    
  33:     static final int SUMMARY_ID_COLUMN_INDEX = 0;
  34:     static final int SUMMARY_NAME_COLUMN_INDEX = 1;
  35:     static final int SUMMARY_STARRED_COLUMN_INDEX = 2;
  36:     static final int SUMMARY_TIMES_CONTACTED_COLUMN_INDEX = 3;
  37:     static final int SUMMARY_PRESENCE_STATUS_COLUMN_INDEX = 4;
  38:     static final int SUMMARY_PHOTO_ID_COLUMN_INDEX = 5;
  39:     static final int SUMMARY_LOOKUP_KEY = 6;
  40:     static final int SUMMARY_HAS_PHONE_COLUMN_INDEX = 7;
  41:     
  42:     @Override
  43:     public void onCreate(Bundle savedInstanceState) {
  44:         super.onCreate(savedInstanceState);
  45:         String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
  46:                 + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
  47:                 + Contacts.DISPLAY_NAME + " != '' ))";
  48:         Cursor c =
  49:                 getContentResolver().query(Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select,
  50:                 null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
  51:         startManagingCursor(c);
  52:         ContactListItemAdapter adapter = new ContactListItemAdapter(this, R.layout.quick_contacts, c);
  53:         setListAdapter(adapter);
  54:  
  55:     }
  56:  
  57:     private final class ContactListItemAdapter extends ResourceCursorAdapter {
  58:         public ContactListItemAdapter(Context context, int layout, Cursor c) {
  59:             super(context, layout, c);
  60:         }
  61:  
  62:         @Override
  63:         public void bindView(View view, Context context, Cursor cursor) {
  64:             final ContactListItemCache cache = (ContactListItemCache) view.getTag();
  65:             QuickContactBadge photoView = cache.photoView;
  66:             // Set the name
  67:             cursor.copyStringToBuffer(SUMMARY_NAME_COLUMN_INDEX, cache.nameBuffer);
  68:             int size = cache.nameBuffer.sizeCopied;
  69:             cache.nameView.setText(cache.nameBuffer.data, 0, size);
  70:             final long contactId = cursor.getLong(SUMMARY_ID_COLUMN_INDEX);
  71:             final String lookupKey = cursor.getString(SUMMARY_LOOKUP_KEY);
  72:             cache.photoView.assignContactUri(Contacts.getLookupUri(contactId, lookupKey));
  73:             
  74:             Uri uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
  75:             InputStream input = Contacts.openContactPhotoInputStream(getContentResolver(), uri);
  76:             if (input != null)
  77:             {
  78:                 Bitmap img = BitmapFactory.decodeStream(input);
  79:                 img.setDensity(DisplayMetrics.DENSITY_HIGH);
  80:                 photoView.setImageBitmap(img);
  81:             }
  82:             else
  83:             {
  84:                 Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_picture);
  85:                 photoView.setImageBitmap(img);
  86:             }
  87:         }
  88:         
  89:         @Override
  90:         public View newView(Context context, Cursor cursor, ViewGroup parent) {
  91:             View view = super.newView(context, cursor, parent);
  92:             ContactListItemCache cache = new ContactListItemCache();
  93:             cache.nameView = (TextView) view.findViewById(R.id.name);
  94:             cache.photoView = (QuickContactBadge) view.findViewById(R.id.badge);
  95:             view.setTag(cache);
  96:  
  97:             return view;
  98:         }
  99:     }
 100:  
 101:     final static class ContactListItemCache {
 102:         public TextView nameView;
 103:         public QuickContactBadge photoView;
 104:         public CharArrayBuffer nameBuffer = new CharArrayBuffer(128);
 105:     }
 106: }


<quick_contacts.xml>

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   3:     android:layout_width="match_parent"
   4:     android:layout_height="wrap_content"
   5:     android:gravity="left"
   6:     android:minHeight="48dip"
   7:     android:paddingLeft="0dip"
   8:     android:paddingRight="9dip" >
   9:  
  10:     <QuickContactBadge
  11:         android:id="@+id/badge"
  12:         android:layout_marginLeft="2dip"
  13:         android:layout_marginRight="14dip"
  14:         android:layout_marginTop="4dip"
  15:         android:layout_marginBottom="3dip"
  16:         android:layout_alignParentLeft="true"
  17:         android:layout_alignParentTop="true"
  18:         android:layout_height= "wrap_content"
  19:         android:layout_width= "wrap_content"
  20:         android:src="@drawable/ic_contact_picture"
  21:         style="?android:attr/quickContactBadgeStyleWindowSmall" />
  22:  
  23:     <TextView
  24:         android:id="@+id/name"
  25:         android:layout_width="match_parent"
  26:         android:layout_height="wrap_content"
  27:         android:layout_centerVertical="true"
  28:         android:layout_toRightOf="@+id/badge"
  29:         android:textAppearance="?android:attr/textAppearanceMedium" />
  30:  
  31: </RelativeLayout>


<AndroidManifest.xml>

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   3:     package="com.android.quickcontact"
   4:     android:versionCode="1"
   5:     android:versionName="1.0" android:installLocation="auto">
   6:  
   7:     <uses-sdk android:minSdkVersion="8" />
   8:     <uses-permission android:name="android.permission.READ_CONTACTS"/>
   9:  
  10:     <application
  11:         android:icon="@drawable/contacts"
  12:         android:label="@string/app_name" >
  13:         <activity
  14:             android:name=".QuickContact"
  15:             android:label="@string/app_name" >
  16:             <intent-filter>
  17:                 <action android:name="android.intent.action.MAIN" />
  18:  
  19:                 <category android:name="android.intent.category.LAUNCHER" />
  20:             </intent-filter>
  21:         </activity>
  22:     </application>
  23:  
  24: </manifest>

沒有留言:

張貼留言