Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.apis.app;
     18 
     19 import com.example.android.apis.R;
     20 
     21 import android.app.ListActivity;
     22 import android.content.Context;
     23 import android.database.CharArrayBuffer;
     24 import android.database.Cursor;
     25 import android.os.Bundle;
     26 import android.provider.ContactsContract.Contacts;
     27 import android.view.View;
     28 import android.view.ViewGroup;
     29 import android.widget.QuickContactBadge;
     30 import android.widget.ResourceCursorAdapter;
     31 import android.widget.TextView;
     32 
     33 public class QuickContactsDemo extends ListActivity {
     34     static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
     35             Contacts._ID, // 0
     36             Contacts.DISPLAY_NAME, // 1
     37             Contacts.STARRED, // 2
     38             Contacts.TIMES_CONTACTED, // 3
     39             Contacts.CONTACT_PRESENCE, // 4
     40             Contacts.PHOTO_ID, // 5
     41             Contacts.LOOKUP_KEY, // 6
     42             Contacts.HAS_PHONE_NUMBER, // 7
     43     };
     44 
     45     static final int SUMMARY_ID_COLUMN_INDEX = 0;
     46     static final int SUMMARY_NAME_COLUMN_INDEX = 1;
     47     static final int SUMMARY_STARRED_COLUMN_INDEX = 2;
     48     static final int SUMMARY_TIMES_CONTACTED_COLUMN_INDEX = 3;
     49     static final int SUMMARY_PRESENCE_STATUS_COLUMN_INDEX = 4;
     50     static final int SUMMARY_PHOTO_ID_COLUMN_INDEX = 5;
     51     static final int SUMMARY_LOOKUP_KEY = 6;
     52     static final int SUMMARY_HAS_PHONE_COLUMN_INDEX = 7;
     53 
     54 
     55     @Override
     56     public void onCreate(Bundle savedInstanceState) {
     57         super.onCreate(savedInstanceState);
     58         String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
     59                 + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
     60                 + Contacts.DISPLAY_NAME + " != '' ))";
     61         Cursor c =
     62                 getContentResolver().query(Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select,
     63                 null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
     64         startManagingCursor(c);
     65         ContactListItemAdapter adapter = new ContactListItemAdapter(this, R.layout.quick_contacts, c);
     66         setListAdapter(adapter);
     67 
     68     }
     69 
     70     private final class ContactListItemAdapter extends ResourceCursorAdapter {
     71         public ContactListItemAdapter(Context context, int layout, Cursor c) {
     72             super(context, layout, c);
     73         }
     74 
     75         @Override
     76         public void bindView(View view, Context context, Cursor cursor) {
     77             final ContactListItemCache cache = (ContactListItemCache) view.getTag();
     78             // Set the name
     79             cursor.copyStringToBuffer(SUMMARY_NAME_COLUMN_INDEX, cache.nameBuffer);
     80             int size = cache.nameBuffer.sizeCopied;
     81             cache.nameView.setText(cache.nameBuffer.data, 0, size);
     82             final long contactId = cursor.getLong(SUMMARY_ID_COLUMN_INDEX);
     83             final String lookupKey = cursor.getString(SUMMARY_LOOKUP_KEY);
     84             cache.photoView.assignContactUri(Contacts.getLookupUri(contactId, lookupKey));
     85         }
     86 
     87         @Override
     88         public View newView(Context context, Cursor cursor, ViewGroup parent) {
     89             View view = super.newView(context, cursor, parent);
     90             ContactListItemCache cache = new ContactListItemCache();
     91             cache.nameView = (TextView) view.findViewById(R.id.name);
     92             cache.photoView = (QuickContactBadge) view.findViewById(R.id.badge);
     93             view.setTag(cache);
     94 
     95             return view;
     96         }
     97     }
     98 
     99     final static class ContactListItemCache {
    100         public TextView nameView;
    101         public QuickContactBadge photoView;
    102         public CharArrayBuffer nameBuffer = new CharArrayBuffer(128);
    103     }
    104 }
    105