move some more code
[andGMXsms.git] / src / de / ub0r / android / andGMXsms / MobilePhoneAdapter.java
blob01aeabca46ab3071058cf85e72e3475d009ee0d0
1 /*
2 * Copyright (C) 2010 Felix Bechstein
3 *
4 * This file is part of WebSMS.
5 *
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
9 * version.
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
14 * details.
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;
34 /**
35 * CursorAdapter getting Name, Phone from DB.
37 * @author flx
39 @SuppressWarnings("deprecation")
40 public class MobilePhoneAdapter extends ResourceCursorAdapter {
41 /** INDEX: name. */
42 public static final int NAME_INDEX = 1;
43 /** INDEX: number. */
44 public static final int NUMBER_INDEX = 2;
45 /** INDEX: type. */
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 + ")";
52 /** Sort Order. */
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 = { //
62 BaseColumns._ID, // 0
63 PeopleColumns.NAME, // 1
64 PhonesColumns.NUMBER, // 2
65 PhonesColumns.TYPE // 3
68 /**
69 * Constructor.
71 * @param context
72 * context
74 public MobilePhoneAdapter(final Context context) {
75 super(context, R.layout.recipient_dropdown_item, null);
76 this.mContentResolver = context.getContentResolver();
79 /**
80 * {@inheritDoc}
82 @Override
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]);
94 } else {
95 ((TextView) view.findViewById(R.id.text3)).setText("");
99 /**
100 * {@inheritDoc}
102 @Override
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) + '>';
113 * {@inheritDoc}
115 @Override
116 public final Cursor runQueryOnBackgroundThread(final CharSequence constraint) {
117 if (WebSMS.helperAPI5c != null) {
118 // switch to API 5 if needed.
119 try {
120 return WebSMS.helperAPI5c.runQueryOnBackgroundThread(
121 this.mContentResolver, constraint);
122 } catch (NoClassDefFoundError e) {
123 WebSMS.helperAPI5c = null;
126 String where = null;
128 if (constraint != null) {
129 String filter = DatabaseUtils.sqlEscapeString('%' + constraint
130 .toString() + '%');
132 StringBuilder s = new StringBuilder();
133 s.append("(" + PeopleColumns.NAME + " LIKE ");
134 s.append(filter);
135 s.append(") OR (" + PhonesColumns.NUMBER + " LIKE ");
136 s.append(filter);
137 s.append(")");
139 if (WebSMS.prefsMobilesOnly) {
140 s.insert(0, "(");
141 s.append(MOBILES_ONLY);
144 where = s.toString();
147 return this.mContentResolver.query(Phones.CONTENT_URI, PROJECTION,
148 where, null, SORT_ORDER);