move some code
[andGMXsms.git] / src / de / ub0r / android / andGMXsms / HelperAPI5Contacts.java
blob725b9c9f1204bad88841f80383590ea827a03523
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 java.lang.reflect.Method;
23 import android.app.Notification;
24 import android.app.Service;
25 import android.content.ContentResolver;
26 import android.database.Cursor;
27 import android.database.DatabaseUtils;
28 import android.provider.BaseColumns;
29 import android.provider.ContactsContract;
30 import android.util.Log;
32 /**
33 * Helper class to set/unset background for api5 systems.
35 * @author flx
37 public class HelperAPI5Contacts {
38 /** Tag for output. */
39 private static final String TAG = "WebSMS.api5c";
41 /** Error message if API5 is not available. */
42 private static final String ERRORMESG = "no API5c available";
44 /** SQL to select mobile numbers only. */
45 private static final String MOBILES_ONLY = ") AND ("
46 + ContactsContract.CommonDataKinds.Phone.TYPE + " = "
47 + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE + ")";
49 /** Sort Order. */
50 private static final String SORT_ORDER = ContactsContract.CommonDataKinds.Phone.STARRED
51 + " DESC, "
52 + ContactsContract.CommonDataKinds.Phone.TIMES_CONTACTED
53 + " DESC, "
54 + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
55 + " ASC, " + ContactsContract.CommonDataKinds.Phone.TYPE;
57 /** Cursor's projection. */
58 private static final String[] PROJECTION = { //
59 BaseColumns._ID, // 0
60 ContactsContract.Data.DISPLAY_NAME, // 1
61 ContactsContract.CommonDataKinds.Phone.NUMBER, // 2
62 ContactsContract.CommonDataKinds.Phone.TYPE // 3
65 /**
66 * Check whether API5 is available.
68 * @return true if API5 is available
70 final boolean isAvailable() {
71 try {
72 Method mDebugMethod = Service.class.getMethod("startForeground",
73 new Class[] { Integer.TYPE, Notification.class });
74 /* success, this is a newer device */
75 if (mDebugMethod != null) {
76 return true;
78 } catch (Throwable e) {
79 Log.d(TAG, ERRORMESG, e);
80 throw new VerifyError(ERRORMESG);
82 Log.d(TAG, ERRORMESG);
83 throw new VerifyError(ERRORMESG);
86 /**
87 * Run Query to update data.
89 * @param mContentResolver
90 * a Content Resolver
91 * @param constraint
92 * filter string
93 * @return cursor
95 final Cursor runQueryOnBackgroundThread(
96 final ContentResolver mContentResolver,
97 final CharSequence constraint) {
98 String where = null;
100 if (constraint != null) {
101 String filter = DatabaseUtils.sqlEscapeString('%' + constraint
102 .toString() + '%');
104 StringBuilder s = new StringBuilder();
105 s.append("(" + ContactsContract.Data.DISPLAY_NAME + " LIKE ");
106 s.append(filter);
107 s.append(") OR (" + ContactsContract.CommonDataKinds.Phone.DATA1
108 + " LIKE ");
109 s.append(filter);
110 s.append(")");
112 if (WebSMS.prefsMobilesOnly) {
113 s.insert(0, "(");
114 s.append(MOBILES_ONLY);
117 where = s.toString();
119 return mContentResolver.query(
120 ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION,
121 where, null, SORT_ORDER);
125 * Get a Name for a given number.
127 * @param act
128 * activity to get the cursor from
129 * @param number
130 * number to look for
131 * @return name matching the number
133 final String getNameForNumber(final WebSMS act, final String number) {
134 String ret = null;
136 Cursor c = act.managedQuery(
137 ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
138 new String[] { ContactsContract.Data.DISPLAY_NAME,
139 ContactsContract.CommonDataKinds.Phone.NUMBER },
140 ContactsContract.CommonDataKinds.Phone.DATA1 + " = '" + number
141 + "'", null, null);
142 if (c.moveToFirst()) {
143 ret = c.getString(c
144 .getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
147 return ret;