2 * Copyright (C) 2010 Felix Bechstein
4 * This file is part of WebSMS.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free Software
8 * Foundation; either version 3 of the License, or (at your option) any later
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License along with
17 * this program; If not, see <http://www.gnu.org/licenses/>.
19 package de
.ub0r
.android
.andGMXsms
;
21 import android
.content
.ContentResolver
;
22 import android
.content
.Context
;
23 import android
.database
.Cursor
;
24 import android
.database
.DatabaseUtils
;
25 import android
.provider
.BaseColumns
;
26 import android
.provider
.Contacts
.PeopleColumns
;
27 import android
.provider
.Contacts
.Phones
;
28 import android
.provider
.Contacts
.PhonesColumns
;
29 import android
.view
.View
;
30 import android
.widget
.ResourceCursorAdapter
;
31 import android
.widget
.TextView
;
32 import de
.ub0r
.android
.websms
.connector
.common
.Utils
;
35 * CursorAdapter getting Name, Phone from DB.
39 @SuppressWarnings("deprecation")
40 public class MobilePhoneAdapter
extends ResourceCursorAdapter
{
42 public static final int NAME_INDEX
= 1;
44 public static final int NUMBER_INDEX
= 2;
46 public static final int NUMBER_TYPE
= 3;
48 /** SQL to select mobile numbers only. */
49 private static final String MOBILES_ONLY
= ") AND (" + PhonesColumns
.TYPE
50 + " = " + PhonesColumns
.TYPE_MOBILE
+ ")";
53 private static final String SORT_ORDER
= PeopleColumns
.STARRED
+ " DESC, "
54 + PeopleColumns
.TIMES_CONTACTED
+ " DESC, " + PeopleColumns
.NAME
55 + " ASC, " + PhonesColumns
.TYPE
;
57 /** Global ContentResolver. */
58 private ContentResolver mContentResolver
;
60 /** Cursor's projection. */
61 private static final String
[] PROJECTION
= { //
63 PeopleColumns
.NAME
, // 1
64 PhonesColumns
.NUMBER
, // 2
65 PhonesColumns
.TYPE
// 3
74 public MobilePhoneAdapter(final Context context
) {
75 super(context
, R
.layout
.recipient_dropdown_item
, null);
76 this.mContentResolver
= context
.getContentResolver();
83 public final void bindView(final View view
, final Context context
,
84 final Cursor cursor
) {
85 ((TextView
) view
.findViewById(R
.id
.text1
)).setText(cursor
86 .getString(NAME_INDEX
));
87 ((TextView
) view
.findViewById(R
.id
.text2
)).setText(cursor
88 .getString(NUMBER_INDEX
));
89 int i
= cursor
.getInt(NUMBER_TYPE
) - 1;
90 String
[] types
= context
.getResources().getStringArray(
91 android
.R
.array
.phoneTypes
);
92 if (i
>= 0 && i
< types
.length
) {
93 ((TextView
) view
.findViewById(R
.id
.text3
)).setText(types
[i
]);
95 ((TextView
) view
.findViewById(R
.id
.text3
)).setText("");
103 public final String
convertToString(final Cursor cursor
) {
104 String name
= cursor
.getString(NAME_INDEX
);
105 String number
= cursor
.getString(NUMBER_INDEX
);
106 if (name
== null || name
.length() == 0) {
107 return Utils
.cleanRecipient(number
);
109 return name
+ " <" + Utils
.cleanRecipient(number
) + '>';
116 public final Cursor
runQueryOnBackgroundThread(final CharSequence constraint
) {
117 if (WebSMS
.helperAPI5c
!= null) {
118 // switch to API 5 if needed.
120 return WebSMS
.helperAPI5c
.runQueryOnBackgroundThread(
121 this.mContentResolver
, constraint
);
122 } catch (NoClassDefFoundError e
) {
123 WebSMS
.helperAPI5c
= null;
128 if (constraint
!= null) {
129 String filter
= DatabaseUtils
.sqlEscapeString('%' + constraint
132 StringBuilder s
= new StringBuilder();
133 s
.append("(" + PeopleColumns
.NAME
+ " LIKE ");
135 s
.append(") OR (" + PhonesColumns
.NUMBER
+ " LIKE ");
139 if (WebSMS
.prefsMobilesOnly
) {
141 s
.append(MOBILES_ONLY
);
144 where
= s
.toString();
147 return this.mContentResolver
.query(Phones
.CONTENT_URI
, PROJECTION
,
148 where
, null, SORT_ORDER
);