1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.dialer.database;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabaseCorruptException;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
import com.android.dialer.compat.FilteredNumberCompat;
import com.android.dialer.database.FilteredNumberContract.FilteredNumber;
import com.android.dialer.database.FilteredNumberContract.FilteredNumberColumns;
import com.android.dialer.database.FilteredNumberContract.FilteredNumberTypes;
public class FilteredNumberAsyncQueryHandler extends AsyncQueryHandler {
private static final int NO_TOKEN = 0;
public FilteredNumberAsyncQueryHandler(ContentResolver cr) {
super(cr);
}
/**
* Methods for FilteredNumberAsyncQueryHandler result returns.
*/
private static abstract class Listener {
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
}
protected void onInsertComplete(int token, Object cookie, Uri uri) {
}
protected void onUpdateComplete(int token, Object cookie, int result) {
}
protected void onDeleteComplete(int token, Object cookie, int result) {
}
}
public interface OnCheckBlockedListener {
/**
* Invoked after querying if a number is blocked.
* @param id The ID of the row if blocked, null otherwise.
*/
void onCheckComplete(Integer id);
}
public interface OnBlockNumberListener {
/**
* Invoked after inserting a blocked number.
* @param uri The uri of the newly created row.
*/
void onBlockComplete(Uri uri);
}
public interface OnUnblockNumberListener {
/**
* Invoked after removing a blocked number
* @param rows The number of rows affected (expected value 1).
* @param values The deleted data (used for restoration).
*/
void onUnblockComplete(int rows, ContentValues values);
}
public interface OnHasBlockedNumbersListener {
/**
* @param hasBlockedNumbers {@code true} if any blocked numbers are stored.
* {@code false} otherwise.
*/
void onHasBlockedNumbers(boolean hasBlockedNumbers);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
if (cookie != null) {
((Listener) cookie).onQueryComplete(token, cookie, cursor);
}
}
@Override
protected void onInsertComplete(int token, Object cookie, Uri uri) {
if (cookie != null) {
((Listener) cookie).onInsertComplete(token, cookie, uri);
}
}
@Override
protected void onUpdateComplete(int token, Object cookie, int result) {
if (cookie != null) {
((Listener) cookie).onUpdateComplete(token, cookie, result);
}
}
@Override
protected void onDeleteComplete(int token, Object cookie, int result) {
if (cookie != null) {
((Listener) cookie).onDeleteComplete(token, cookie, result);
}
}
public final void incrementFilteredCount(Integer id) {
// No concept of counts with new filtering
if (FilteredNumberCompat.useNewFiltering()) {
return;
}
startUpdate(NO_TOKEN, null,
ContentUris.withAppendedId(FilteredNumber.CONTENT_URI_INCREMENT_FILTERED_COUNT, id),
null, null, null);
}
public final void hasBlockedNumbers(final OnHasBlockedNumbersListener listener) {
startQuery(NO_TOKEN,
new Listener() {
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
listener.onHasBlockedNumbers(cursor != null && cursor.getCount() > 0);
}
},
FilteredNumberCompat.getContentUri(null),
new String[]{ FilteredNumberCompat.getIdColumnName() },
FilteredNumberCompat.useNewFiltering() ? null : FilteredNumberColumns.TYPE
+ "=" + FilteredNumberTypes.BLOCKED_NUMBER,
null,
null);
}
/**
* Check if this number has been blocked.
*
* @return {@code false} if the number was invalid and couldn't be checked,
* {@code true} otherwise,
*/
public boolean isBlockedNumber(
final OnCheckBlockedListener listener, String number, String countryIso) {
final String e164Number = PhoneNumberUtils.formatNumberToE164(number, countryIso);
if (TextUtils.isEmpty(e164Number)) {
return false;
}
startQuery(NO_TOKEN,
new Listener() {
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
if (cursor == null || cursor.getCount() != 1) {
listener.onCheckComplete(null);
return;
}
cursor.moveToFirst();
// New filtering doesn't have a concept of type
if (!FilteredNumberCompat.useNewFiltering()
&& cursor.getInt(cursor.getColumnIndex(FilteredNumberColumns.TYPE))
!= FilteredNumberTypes.BLOCKED_NUMBER) {
listener.onCheckComplete(null);
return;
}
listener.onCheckComplete(
cursor.getInt(cursor.getColumnIndex(FilteredNumberColumns._ID)));
}
},
FilteredNumberCompat.getContentUri(null),
FilteredNumberCompat.filter(new String[]{FilteredNumberCompat.getIdColumnName(),
FilteredNumberCompat.getTypeColumnName()}),
FilteredNumberCompat.getE164NumberColumnName() + " = ?",
new String[]{e164Number},
null);
return true;
}
public void blockNumber(
final OnBlockNumberListener listener, String number, @Nullable String countryIso) {
blockNumber(listener, null, number, countryIso);
}
/**
* Add a number manually blocked by the user.
*/
public void blockNumber(
final OnBlockNumberListener listener,
@Nullable String normalizedNumber,
String number,
@Nullable String countryIso) {
blockNumber(listener, FilteredNumberCompat.newBlockNumberContentValues(number,
normalizedNumber, countryIso));
}
/**
* Block a number with specified ContentValues. Can be manually added or a restored row
* from performing the 'undo' action after unblocking.
*/
public void blockNumber(final OnBlockNumberListener listener, ContentValues values) {
startInsert(NO_TOKEN,
new Listener() {
@Override
public void onInsertComplete(int token, Object cookie, Uri uri) {
if (listener != null ) {
listener.onBlockComplete(uri);
}
}
}, FilteredNumberCompat.getContentUri(null), values);
}
/**
* Unblocks the number with the given id.
*
* @param listener (optional) The {@link OnUnblockNumberListener} called after the number is
* unblocked.
* @param id The id of the number to unblock.
*/
public void unblock(@Nullable final OnUnblockNumberListener listener, Integer id) {
if (id == null) {
throw new IllegalArgumentException("Null id passed into unblock");
}
unblock(listener, FilteredNumberCompat.getContentUri(id));
}
/**
* Removes row from database.
* @param listener (optional) The {@link OnUnblockNumberListener} called after the number is
* unblocked.
* @param uri The uri of row to remove, from
* {@link FilteredNumberAsyncQueryHandler#blockNumber}.
*/
public void unblock(@Nullable final OnUnblockNumberListener listener, final Uri uri) {
startQuery(NO_TOKEN, new Listener() {
@Override
public void onQueryComplete(int token, Object cookie, Cursor cursor) {
int rowsReturned = cursor == null ? 0 : cursor.getCount();
if (rowsReturned != 1) {
throw new SQLiteDatabaseCorruptException
("Returned " + rowsReturned + " rows for uri "
+ uri + "where 1 expected.");
}
cursor.moveToFirst();
final ContentValues values = new ContentValues();
DatabaseUtils.cursorRowToContentValues(cursor, values);
values.remove(FilteredNumberCompat.getIdColumnName());
startDelete(NO_TOKEN, new Listener() {
@Override
public void onDeleteComplete(int token, Object cookie, int result) {
if (listener != null) {
listener.onUnblockComplete(result, values);
}
}
}, uri, null, null);
}
}, uri, null, null, null, null);
}
}
|