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 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
;
33 * Helper class to set/unset background for api5 systems.
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
+ ")";
50 private static final String SORT_ORDER
= ContactsContract
.CommonDataKinds
.Phone
.STARRED
52 + ContactsContract
.CommonDataKinds
.Phone
.TIMES_CONTACTED
54 + ContactsContract
.CommonDataKinds
.Phone
.DISPLAY_NAME
55 + " ASC, " + ContactsContract
.CommonDataKinds
.Phone
.TYPE
;
57 /** Cursor's projection. */
58 private static final String
[] PROJECTION
= { //
60 ContactsContract
.Data
.DISPLAY_NAME
, // 1
61 ContactsContract
.CommonDataKinds
.Phone
.NUMBER
, // 2
62 ContactsContract
.CommonDataKinds
.Phone
.TYPE
// 3
66 * Check whether API5 is available.
68 * @return true if API5 is available
70 final boolean isAvailable() {
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) {
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
);
87 * Run Query to update data.
89 * @param mContentResolver
95 final Cursor
runQueryOnBackgroundThread(
96 final ContentResolver mContentResolver
,
97 final CharSequence constraint
) {
100 if (constraint
!= null) {
101 String filter
= DatabaseUtils
.sqlEscapeString('%' + constraint
104 StringBuilder s
= new StringBuilder();
105 s
.append("(" + ContactsContract
.Data
.DISPLAY_NAME
+ " LIKE ");
107 s
.append(") OR (" + ContactsContract
.CommonDataKinds
.Phone
.DATA1
112 if (WebSMS
.prefsMobilesOnly
) {
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.
128 * activity to get the cursor from
131 * @return name matching the number
133 final String
getNameForNumber(final WebSMS act
, final String number
) {
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
142 if (c
.moveToFirst()) {
144 .getColumnIndex(ContactsContract
.Data
.DISPLAY_NAME
));