1 /* 2 * Copyright (C) 2008 Esmertec AG. 3 * Copyright (C) 2008 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.im.app; 19 20 import android.content.Context; 21 import android.database.Cursor; 22 import android.database.DatabaseUtils; 23 import android.net.Uri; 24 import android.util.AttributeSet; 25 import android.view.View; 26 import android.widget.AdapterView; 27 import android.widget.Filter; 28 import android.widget.LinearLayout; 29 import android.widget.ListView; 30 import android.widget.ResourceCursorAdapter; 31 import android.widget.AdapterView.OnItemClickListener; 32 33 import com.android.im.R; 34 import com.android.im.provider.Imps; 35 36 public class ContactListFilterView extends LinearLayout { 37 38 private ListView mContactListView; 39 private Filter mFilter; 40 private ContactAdapter mContactAdapter; 41 42 private Uri mUri; 43 44 public ContactListFilterView(Context context, AttributeSet attrs) { 45 super(context, attrs); 46 } 47 48 @Override 49 protected void onFinishInflate() { 50 51 mContactListView = (ListView) findViewById(R.id.filteredList); 52 mContactListView.setTextFilterEnabled(true); 53 54 mContactListView.setOnItemClickListener(new OnItemClickListener() { 55 public void onItemClick(AdapterView parent, View view, int position, 56 long id) { 57 if (mContext instanceof ContactListActivity) { 58 ContactListActivity list = (ContactListActivity) mContext; 59 mContactListView.setSelection(position); 60 Cursor c = (Cursor) mContactListView.getSelectedItem(); 61 list.mContactListView.startChat(c); 62 list.showContactListView(); 63 } 64 } 65 }); 66 } 67 68 public ListView getListView() { 69 return mContactListView; 70 } 71 72 public Cursor getContactAtPosition(int position) { 73 return (Cursor) mContactAdapter.getItem(position); 74 } 75 76 public void doFilter(Uri uri, String filterString) { 77 if (!uri.equals(mUri)) { 78 mUri = uri; 79 80 Cursor contactCursor = runQuery(filterString); 81 82 if (mContactAdapter == null) { 83 mContactAdapter = new ContactAdapter(mContext, contactCursor); 84 mFilter = mContactAdapter.getFilter(); 85 mContactListView.setAdapter(mContactAdapter); 86 } else { 87 mContactAdapter.changeCursor(contactCursor); 88 } 89 } else { 90 mFilter.filter(filterString); 91 } 92 } 93 94 Cursor runQuery(CharSequence constraint) { 95 StringBuilder buf = new StringBuilder(); 96 97 // exclude chatting contact 98 buf.append(Imps.Chats.LAST_MESSAGE_DATE); 99 buf.append(" IS NULL"); 100 101 if (constraint != null) { 102 buf.append(" AND "); 103 buf.append(Imps.Contacts.NICKNAME); 104 buf.append(" LIKE "); 105 DatabaseUtils.appendValueToSql(buf, "%" + constraint + "%"); 106 } 107 108 return mContext.getContentResolver().query(mUri, ContactView.CONTACT_PROJECTION, 109 buf == null ? null : buf.toString(), null, Imps.Contacts.DEFAULT_SORT_ORDER); 110 } 111 112 private class ContactAdapter extends ResourceCursorAdapter { 113 private String mSearchString; 114 115 public ContactAdapter(Context context, Cursor cursor) { 116 super(context, R.layout.contact_view, cursor); 117 } 118 119 @Override 120 public void bindView(View view, Context context, Cursor cursor) { 121 ContactView v = (ContactView) view; 122 v.setPadding(0, 0, 0, 0); 123 v.bind(cursor, mSearchString, false); 124 } 125 126 @Override 127 public Cursor runQueryOnBackgroundThread(CharSequence constraint) { 128 if (constraint != null) { 129 mSearchString = constraint.toString(); 130 } 131 return ContactListFilterView.this.runQuery(constraint); 132 } 133 } 134 135 } 136