blob: 09752c71fb64ff4751352f856d857bca057b064f [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.incallui;
18
19import android.Manifest;
20import android.annotation.TargetApi;
21import android.content.AsyncQueryHandler;
22import android.content.ContentResolver;
23import android.content.Context;
24import android.database.Cursor;
25import android.database.SQLException;
26import android.net.Uri;
27import android.os.Build.VERSION;
28import android.os.Build.VERSION_CODES;
29import android.os.Handler;
30import android.os.Looper;
31import android.os.Message;
32import android.provider.ContactsContract;
33import android.provider.ContactsContract.Directory;
34import android.support.annotation.MainThread;
35import android.support.annotation.RequiresPermission;
36import android.support.annotation.WorkerThread;
37import android.telephony.PhoneNumberUtils;
38import android.text.TextUtils;
39import com.android.contacts.common.compat.DirectoryCompat;
40import com.android.dialer.phonenumbercache.CachedNumberLookupService;
41import com.android.dialer.phonenumbercache.CachedNumberLookupService.CachedContactInfo;
42import com.android.dialer.phonenumbercache.ContactInfoHelper;
43import com.android.dialer.phonenumbercache.PhoneNumberCache;
wangqie37d60c2017-09-27 10:13:49 -070044import com.android.dialer.strictmode.StrictModeUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080045import java.io.IOException;
46import java.io.InputStream;
47import java.util.ArrayList;
48import java.util.Arrays;
49
50/**
51 * Helper class to make it easier to run asynchronous caller-id lookup queries.
52 *
53 * @see CallerInfo
54 */
55@TargetApi(VERSION_CODES.M)
56public class CallerInfoAsyncQuery {
57
58 /** Interface for a CallerInfoAsyncQueryHandler result return. */
Eric Erfaniand5e47f62017-03-15 14:41:07 -070059 interface OnQueryCompleteListener {
Eric Erfanianccca3152017-02-22 16:32:36 -080060
61 /** Called when the query is complete. */
62 @MainThread
63 void onQueryComplete(int token, Object cookie, CallerInfo ci);
64
65 /** Called when data is loaded. Must be called in worker thread. */
66 @WorkerThread
67 void onDataLoaded(int token, Object cookie, CallerInfo ci);
68 }
69
70 private static final boolean DBG = false;
71 private static final String LOG_TAG = "CallerInfoAsyncQuery";
72
73 private static final int EVENT_NEW_QUERY = 1;
74 private static final int EVENT_ADD_LISTENER = 2;
75 private static final int EVENT_EMERGENCY_NUMBER = 3;
76 private static final int EVENT_VOICEMAIL_NUMBER = 4;
77 // If the CallerInfo query finds no contacts, should we use the
78 // PhoneNumberOfflineGeocoder to look up a "geo description"?
79 // (TODO: This could become a flag in config.xml if it ever needs to be
80 // configured on a per-product basis.)
81 private static final boolean ENABLE_UNKNOWN_NUMBER_GEO_DESCRIPTION = true;
82 /* Directory lookup related code - START */
83 private static final String[] DIRECTORY_PROJECTION = new String[] {Directory._ID};
84
85 /** Private constructor for factory methods. */
86 private CallerInfoAsyncQuery() {}
87
88 @RequiresPermission(Manifest.permission.READ_CONTACTS)
Eric Erfaniand5e47f62017-03-15 14:41:07 -070089 static void startQuery(
Eric Erfanianccca3152017-02-22 16:32:36 -080090 final int token,
91 final Context context,
92 final CallerInfo info,
93 final OnQueryCompleteListener listener,
94 final Object cookie) {
95 Log.d(LOG_TAG, "##### CallerInfoAsyncQuery startContactProviderQuery()... #####");
96 Log.d(LOG_TAG, "- number: " + info.phoneNumber);
97 Log.d(LOG_TAG, "- cookie: " + cookie);
98
99 OnQueryCompleteListener contactsProviderQueryCompleteListener =
100 new OnQueryCompleteListener() {
101 @Override
102 public void onQueryComplete(int token, Object cookie, CallerInfo ci) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700103 Log.d(LOG_TAG, "contactsProviderQueryCompleteListener onQueryComplete");
Eric Erfanianccca3152017-02-22 16:32:36 -0800104 // If there are no other directory queries, make sure that the listener is
105 // notified of this result. see b/27621628
106 if ((ci != null && ci.contactExists)
107 || !startOtherDirectoriesQuery(token, context, info, listener, cookie)) {
108 if (listener != null && ci != null) {
109 listener.onQueryComplete(token, cookie, ci);
110 }
111 }
112 }
113
114 @Override
115 public void onDataLoaded(int token, Object cookie, CallerInfo ci) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700116 Log.d(LOG_TAG, "contactsProviderQueryCompleteListener onDataLoaded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800117 listener.onDataLoaded(token, cookie, ci);
118 }
119 };
120 startDefaultDirectoryQuery(token, context, info, contactsProviderQueryCompleteListener, cookie);
121 }
122
123 // Private methods
124 private static void startDefaultDirectoryQuery(
125 int token,
126 Context context,
127 CallerInfo info,
128 OnQueryCompleteListener listener,
129 Object cookie) {
130 // Construct the URI object and query params, and start the query.
131 Uri uri = ContactInfoHelper.getContactInfoLookupUri(info.phoneNumber);
132 startQueryInternal(token, context, info, listener, cookie, uri);
133 }
134
135 /**
136 * Factory method to start the query based on a CallerInfo object.
137 *
138 * <p>Note: if the number contains an "@" character we treat it as a SIP address, and look it up
139 * directly in the Data table rather than using the PhoneLookup table. TODO: But eventually we
140 * should expose two separate methods, one for numbers and one for SIP addresses, and then have
141 * PhoneUtils.startGetCallerInfo() decide which one to call based on the phone type of the
142 * incoming connection.
143 */
144 private static void startQueryInternal(
145 int token,
146 Context context,
147 CallerInfo info,
148 OnQueryCompleteListener listener,
149 Object cookie,
150 Uri contactRef) {
151 if (DBG) {
152 Log.d(LOG_TAG, "==> contactRef: " + sanitizeUriToString(contactRef));
153 }
154
155 if ((context == null) || (contactRef == null)) {
156 throw new QueryPoolException("Bad context or query uri.");
157 }
158 CallerInfoAsyncQueryHandler handler = new CallerInfoAsyncQueryHandler(context, contactRef);
159
160 //create cookieWrapper, start query
161 CookieWrapper cw = new CookieWrapper();
162 cw.listener = listener;
163 cw.cookie = cookie;
164 cw.number = info.phoneNumber;
wangqi1420a222017-09-21 09:37:40 -0700165 cw.countryIso = info.countryIso;
Eric Erfanianccca3152017-02-22 16:32:36 -0800166
167 // check to see if these are recognized numbers, and use shortcuts if we can.
168 if (PhoneNumberUtils.isLocalEmergencyNumber(context, info.phoneNumber)) {
169 cw.event = EVENT_EMERGENCY_NUMBER;
170 } else if (info.isVoiceMailNumber()) {
171 cw.event = EVENT_VOICEMAIL_NUMBER;
172 } else {
173 cw.event = EVENT_NEW_QUERY;
174 }
175
176 String[] proejection = CallerInfo.getDefaultPhoneLookupProjection(contactRef);
177 handler.startQuery(
178 token,
179 cw, // cookie
180 contactRef, // uri
181 proejection, // projection
182 null, // selection
183 null, // selectionArgs
184 null); // orderBy
185 }
186
187 // Return value indicates if listener was notified.
188 private static boolean startOtherDirectoriesQuery(
189 int token,
190 Context context,
191 CallerInfo info,
192 OnQueryCompleteListener listener,
193 Object cookie) {
wangqie37d60c2017-09-27 10:13:49 -0700194 long[] directoryIds = StrictModeUtils.bypass(() -> getDirectoryIds(context));
Eric Erfanianccca3152017-02-22 16:32:36 -0800195 int size = directoryIds.length;
196 if (size == 0) {
197 return false;
198 }
199
200 DirectoryQueryCompleteListenerFactory listenerFactory =
201 new DirectoryQueryCompleteListenerFactory(context, size, listener);
202
203 // The current implementation of multiple async query runs in single handler thread
204 // in AsyncQueryHandler.
205 // intermediateListener.onQueryComplete is also called from the same caller thread.
206 // TODO(b/26019872): use thread pool instead of single thread.
207 for (int i = 0; i < size; i++) {
208 long directoryId = directoryIds[i];
209 Uri uri = ContactInfoHelper.getContactInfoLookupUri(info.phoneNumber, directoryId);
210 if (DBG) {
211 Log.d(LOG_TAG, "directoryId: " + directoryId + " uri: " + uri);
212 }
213 OnQueryCompleteListener intermediateListener = listenerFactory.newListener(directoryId);
214 startQueryInternal(token, context, info, intermediateListener, cookie, uri);
215 }
216 return true;
217 }
218
219 private static long[] getDirectoryIds(Context context) {
220 ArrayList<Long> results = new ArrayList<>();
221
222 Uri uri = Directory.CONTENT_URI;
223 if (VERSION.SDK_INT >= VERSION_CODES.N) {
224 uri = Uri.withAppendedPath(ContactsContract.AUTHORITY_URI, "directories_enterprise");
225 }
226
227 ContentResolver cr = context.getContentResolver();
228 Cursor cursor = cr.query(uri, DIRECTORY_PROJECTION, null, null, null);
229 addDirectoryIdsFromCursor(cursor, results);
230
231 long[] result = new long[results.size()];
232 for (int i = 0; i < results.size(); i++) {
233 result[i] = results.get(i);
234 }
235 return result;
236 }
237
238 private static void addDirectoryIdsFromCursor(Cursor cursor, ArrayList<Long> results) {
239 if (cursor != null) {
240 int idIndex = cursor.getColumnIndex(Directory._ID);
241 while (cursor.moveToNext()) {
242 long id = cursor.getLong(idIndex);
243 if (DirectoryCompat.isRemoteDirectoryId(id)) {
244 results.add(id);
245 }
246 }
247 cursor.close();
248 }
249 }
250
251 private static String sanitizeUriToString(Uri uri) {
252 if (uri != null) {
253 String uriString = uri.toString();
254 int indexOfLastSlash = uriString.lastIndexOf('/');
255 if (indexOfLastSlash > 0) {
256 return uriString.substring(0, indexOfLastSlash) + "/xxxxxxx";
257 } else {
258 return uriString;
259 }
260 } else {
261 return "";
262 }
263 }
264
265 /** Wrap the cookie from the WorkerArgs with additional information needed by our classes. */
266 private static final class CookieWrapper {
267
268 public OnQueryCompleteListener listener;
269 public Object cookie;
270 public int event;
271 public String number;
wangqi1420a222017-09-21 09:37:40 -0700272 public String countryIso;
Eric Erfanianccca3152017-02-22 16:32:36 -0800273 }
274 /* Directory lookup related code - END */
275
276 /** Simple exception used to communicate problems with the query pool. */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700277 private static class QueryPoolException extends SQLException {
Eric Erfanianccca3152017-02-22 16:32:36 -0800278
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700279 QueryPoolException(String error) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800280 super(error);
281 }
282 }
283
284 private static final class DirectoryQueryCompleteListenerFactory {
285
286 private final OnQueryCompleteListener mListener;
287 private final Context mContext;
288 // Make sure listener to be called once and only once
289 private int mCount;
290 private boolean mIsListenerCalled;
291
292 DirectoryQueryCompleteListenerFactory(
293 Context context, int size, OnQueryCompleteListener listener) {
294 mCount = size;
295 mListener = listener;
296 mIsListenerCalled = false;
297 mContext = context;
298 }
299
300 private void onDirectoryQueryComplete(
301 int token, Object cookie, CallerInfo ci, long directoryId) {
302 boolean shouldCallListener = false;
303 synchronized (this) {
304 mCount = mCount - 1;
305 if (!mIsListenerCalled && (ci.contactExists || mCount == 0)) {
306 mIsListenerCalled = true;
307 shouldCallListener = true;
308 }
309 }
310
311 // Don't call callback in synchronized block because mListener.onQueryComplete may
312 // take long time to complete
313 if (shouldCallListener && mListener != null) {
314 addCallerInfoIntoCache(ci, directoryId);
315 mListener.onQueryComplete(token, cookie, ci);
316 }
317 }
318
319 private void addCallerInfoIntoCache(CallerInfo ci, long directoryId) {
320 CachedNumberLookupService cachedNumberLookupService =
321 PhoneNumberCache.get(mContext).getCachedNumberLookupService();
322 if (ci.contactExists && cachedNumberLookupService != null) {
323 // 1. Cache caller info
324 CachedContactInfo cachedContactInfo =
325 CallerInfoUtils.buildCachedContactInfo(cachedNumberLookupService, ci);
326 String directoryLabel = mContext.getString(R.string.directory_search_label);
327 cachedContactInfo.setDirectorySource(directoryLabel, directoryId);
328 cachedNumberLookupService.addContact(mContext, cachedContactInfo);
329
330 // 2. Cache photo
331 if (ci.contactDisplayPhotoUri != null && ci.normalizedNumber != null) {
332 try (InputStream in =
333 mContext.getContentResolver().openInputStream(ci.contactDisplayPhotoUri)) {
334 if (in != null) {
335 cachedNumberLookupService.addPhoto(mContext, ci.normalizedNumber, in);
336 }
337 } catch (IOException e) {
338 Log.e(LOG_TAG, "failed to fetch directory contact photo", e);
339 }
340 }
341 }
342 }
343
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700344 OnQueryCompleteListener newListener(long directoryId) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800345 return new DirectoryQueryCompleteListener(directoryId);
346 }
347
348 private class DirectoryQueryCompleteListener implements OnQueryCompleteListener {
349
350 private final long mDirectoryId;
351
352 DirectoryQueryCompleteListener(long directoryId) {
353 mDirectoryId = directoryId;
354 }
355
356 @Override
357 public void onDataLoaded(int token, Object cookie, CallerInfo ci) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700358 Log.d(LOG_TAG, "DirectoryQueryCompleteListener.onDataLoaded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800359 mListener.onDataLoaded(token, cookie, ci);
360 }
361
362 @Override
363 public void onQueryComplete(int token, Object cookie, CallerInfo ci) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700364 Log.d(LOG_TAG, "DirectoryQueryCompleteListener.onQueryComplete");
Eric Erfanianccca3152017-02-22 16:32:36 -0800365 onDirectoryQueryComplete(token, cookie, ci, mDirectoryId);
366 }
367 }
368 }
369
370 /** Our own implementation of the AsyncQueryHandler. */
371 private static class CallerInfoAsyncQueryHandler extends AsyncQueryHandler {
372
373 /**
374 * The information relevant to each CallerInfo query. Each query may have multiple listeners, so
375 * each AsyncCursorInfo is associated with 2 or more CookieWrapper objects in the queue (one
376 * with a new query event, and one with a end event, with 0 or more additional listeners in
377 * between).
378 */
379 private Context mQueryContext;
380
381 private Uri mQueryUri;
382 private CallerInfo mCallerInfo;
383
384 /** Asynchronous query handler class for the contact / callerinfo object. */
385 private CallerInfoAsyncQueryHandler(Context context, Uri contactRef) {
386 super(context.getContentResolver());
387 this.mQueryContext = context;
388 this.mQueryUri = contactRef;
389 }
390
391 @Override
392 public void startQuery(
393 int token,
394 Object cookie,
395 Uri uri,
396 String[] projection,
397 String selection,
398 String[] selectionArgs,
399 String orderBy) {
400 if (DBG) {
401 // Show stack trace with the arguments.
402 Log.d(
403 LOG_TAG,
404 "InCall: startQuery: url="
405 + uri
406 + " projection=["
407 + Arrays.toString(projection)
408 + "]"
409 + " selection="
410 + selection
411 + " "
412 + " args=["
413 + Arrays.toString(selectionArgs)
414 + "]",
415 new RuntimeException("STACKTRACE"));
416 }
417 super.startQuery(token, cookie, uri, projection, selection, selectionArgs, orderBy);
418 }
419
420 @Override
421 protected Handler createHandler(Looper looper) {
422 return new CallerInfoWorkerHandler(looper);
423 }
424
425 /**
426 * Overrides onQueryComplete from AsyncQueryHandler.
427 *
428 * <p>This method takes into account the state of this class; we construct the CallerInfo object
429 * only once for each set of listeners. When the query thread has done its work and calls this
430 * method, we inform the remaining listeners in the queue, until we're out of listeners. Once we
431 * get the message indicating that we should expect no new listeners for this CallerInfo object,
432 * we release the AsyncCursorInfo back into the pool.
433 */
434 @Override
435 protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
436 Log.d(this, "##### onQueryComplete() ##### query complete for token: " + token);
437
438 CookieWrapper cw = (CookieWrapper) cookie;
439
440 if (cw.listener != null) {
441 Log.d(
442 this,
443 "notifying listener: "
444 + cw.listener.getClass().toString()
445 + " for token: "
446 + token
447 + mCallerInfo);
448 cw.listener.onQueryComplete(token, cw.cookie, mCallerInfo);
449 }
450 mQueryContext = null;
451 mQueryUri = null;
452 mCallerInfo = null;
453 }
454
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700455 void updateData(int token, Object cookie, Cursor cursor) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800456 try {
457 Log.d(this, "##### updateData() ##### for token: " + token);
458
459 //get the cookie and notify the listener.
460 CookieWrapper cw = (CookieWrapper) cookie;
461 if (cw == null) {
462 // Normally, this should never be the case for calls originating
463 // from within this code.
464 // However, if there is any code that calls this method, we should
465 // check the parameters to make sure they're viable.
466 Log.d(this, "Cookie is null, ignoring onQueryComplete() request.");
467 return;
468 }
469
470 // check the token and if needed, create the callerinfo object.
471 if (mCallerInfo == null) {
472 if ((mQueryContext == null) || (mQueryUri == null)) {
473 throw new QueryPoolException(
474 "Bad context or query uri, or CallerInfoAsyncQuery already released.");
475 }
476
477 // adjust the callerInfo data as needed, and only if it was set from the
478 // initial query request.
479 // Change the callerInfo number ONLY if it is an emergency number or the
480 // voicemail number, and adjust other data (including photoResource)
481 // accordingly.
482 if (cw.event == EVENT_EMERGENCY_NUMBER) {
483 // Note we're setting the phone number here (refer to javadoc
484 // comments at the top of CallerInfo class).
485 mCallerInfo = new CallerInfo().markAsEmergency(mQueryContext);
486 } else if (cw.event == EVENT_VOICEMAIL_NUMBER) {
487 mCallerInfo = new CallerInfo().markAsVoiceMail(mQueryContext);
488 } else {
489 mCallerInfo = CallerInfo.getCallerInfo(mQueryContext, mQueryUri, cursor);
490 Log.d(this, "==> Got mCallerInfo: " + mCallerInfo);
491
492 CallerInfo newCallerInfo =
493 CallerInfo.doSecondaryLookupIfNecessary(mQueryContext, cw.number, mCallerInfo);
494 if (newCallerInfo != mCallerInfo) {
495 mCallerInfo = newCallerInfo;
496 Log.d(this, "#####async contact look up with numeric username" + mCallerInfo);
497 }
wangqi1420a222017-09-21 09:37:40 -0700498 mCallerInfo.countryIso = cw.countryIso;
Eric Erfanianccca3152017-02-22 16:32:36 -0800499
500 // Final step: look up the geocoded description.
501 if (ENABLE_UNKNOWN_NUMBER_GEO_DESCRIPTION) {
502 // Note we do this only if we *don't* have a valid name (i.e. if
503 // no contacts matched the phone number of the incoming call),
504 // since that's the only case where the incoming-call UI cares
505 // about this field.
506 //
507 // (TODO: But if we ever want the UI to show the geoDescription
508 // even when we *do* match a contact, we'll need to either call
509 // updateGeoDescription() unconditionally here, or possibly add a
510 // new parameter to CallerInfoAsyncQuery.startQuery() to force
511 // the geoDescription field to be populated.)
512
513 if (TextUtils.isEmpty(mCallerInfo.name)) {
514 // Actually when no contacts match the incoming phone number,
515 // the CallerInfo object is totally blank here (i.e. no name
516 // *or* phoneNumber). So we need to pass in cw.number as
517 // a fallback number.
518 mCallerInfo.updateGeoDescription(mQueryContext, cw.number);
519 }
520 }
521
522 // Use the number entered by the user for display.
523 if (!TextUtils.isEmpty(cw.number)) {
524 mCallerInfo.phoneNumber = cw.number;
525 }
526 }
527
528 Log.d(this, "constructing CallerInfo object for token: " + token);
529
530 if (cw.listener != null) {
531 cw.listener.onDataLoaded(token, cw.cookie, mCallerInfo);
532 }
533 }
534
535 } finally {
536 // The cursor may have been closed in CallerInfo.getCallerInfo()
537 if (cursor != null && !cursor.isClosed()) {
538 cursor.close();
539 }
540 }
541 }
542
543 /**
544 * Our own query worker thread.
545 *
546 * <p>This thread handles the messages enqueued in the looper. The normal sequence of events is
547 * that a new query shows up in the looper queue, followed by 0 or more add listener requests,
548 * and then an end request. Of course, these requests can be interlaced with requests from other
549 * tokens, but is irrelevant to this handler since the handler has no state.
550 *
551 * <p>Note that we depend on the queue to keep things in order; in other words, the looper queue
552 * must be FIFO with respect to input from the synchronous startQuery calls and output to this
553 * handleMessage call.
554 *
555 * <p>This use of the queue is required because CallerInfo objects may be accessed multiple
556 * times before the query is complete. All accesses (listeners) must be queued up and informed
557 * in order when the query is complete.
558 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700559 class CallerInfoWorkerHandler extends WorkerHandler {
Eric Erfanianccca3152017-02-22 16:32:36 -0800560
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700561 CallerInfoWorkerHandler(Looper looper) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800562 super(looper);
563 }
564
565 @Override
566 public void handleMessage(Message msg) {
567 WorkerArgs args = (WorkerArgs) msg.obj;
568 CookieWrapper cw = (CookieWrapper) args.cookie;
569
570 if (cw == null) {
571 // Normally, this should never be the case for calls originating
572 // from within this code.
573 // However, if there is any code that this Handler calls (such as in
574 // super.handleMessage) that DOES place unexpected messages on the
575 // queue, then we need pass these messages on.
576 Log.d(
577 this,
578 "Unexpected command (CookieWrapper is null): "
579 + msg.what
580 + " ignored by CallerInfoWorkerHandler, passing onto parent.");
581
582 super.handleMessage(msg);
583 } else {
584 Log.d(
585 this,
586 "Processing event: "
587 + cw.event
588 + " token (arg1): "
589 + msg.arg1
590 + " command: "
591 + msg.what
592 + " query URI: "
593 + sanitizeUriToString(args.uri));
594
595 switch (cw.event) {
596 case EVENT_NEW_QUERY:
597 final ContentResolver resolver = mQueryContext.getContentResolver();
598
599 // This should never happen.
600 if (resolver == null) {
601 Log.e(this, "Content Resolver is null!");
602 return;
603 }
604 //start the sql command.
605 Cursor cursor;
606 try {
607 cursor =
608 resolver.query(
609 args.uri,
610 args.projection,
611 args.selection,
612 args.selectionArgs,
613 args.orderBy);
614 // Calling getCount() causes the cursor window to be filled,
615 // which will make the first access on the main thread a lot faster.
616 if (cursor != null) {
617 cursor.getCount();
618 }
619 } catch (Exception e) {
620 Log.e(this, "Exception thrown during handling EVENT_ARG_QUERY", e);
621 cursor = null;
622 }
623
624 args.result = cursor;
625 updateData(msg.arg1, cw, cursor);
626 break;
627
628 // shortcuts to avoid query for recognized numbers.
629 case EVENT_EMERGENCY_NUMBER:
630 case EVENT_VOICEMAIL_NUMBER:
631 case EVENT_ADD_LISTENER:
632 updateData(msg.arg1, cw, (Cursor) args.result);
633 break;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700634 default: // fall out
Eric Erfanianccca3152017-02-22 16:32:36 -0800635 }
636 Message reply = args.handler.obtainMessage(msg.what);
637 reply.obj = args;
638 reply.arg1 = msg.arg1;
639
640 reply.sendToTarget();
641 }
642 }
643 }
644 }
645}