blob: a03d7e25d4c565a8ab99936575fe011b2056fb12 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -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
Hall Liue2f17aa2019-10-31 15:17:58 -070017package android.telecom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
Makoto Onuki5692dcc2014-07-17 14:57:04 -070019import android.app.ActivityManager;
Artur Satayev9a5c3102020-01-23 16:49:36 +000020import android.compat.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.AsyncQueryHandler;
Makoto Onuki5692dcc2014-07-17 14:57:04 -070022import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.Context;
Makoto Onuki5692dcc2014-07-17 14:57:04 -070024import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.database.Cursor;
26import android.database.SQLException;
27import android.net.Uri;
28import android.os.Handler;
29import android.os.Looper;
30import android.os.Message;
Ceci Wu3b90d482016-08-25 14:48:19 +080031import android.os.SystemClock;
Makoto Onuki5692dcc2014-07-17 14:57:04 -070032import android.os.UserHandle;
33import android.os.UserManager;
Dmitri Plotnikov3c513ed2009-08-19 15:56:30 -070034import android.provider.ContactsContract.PhoneLookup;
Hall Liue2f17aa2019-10-31 15:17:58 -070035import android.telephony.PhoneNumberUtils;
36import android.telephony.SubscriptionManager;
Taesu Lee902b89d2020-10-07 14:55:25 +090037import android.telephony.TelephonyManager;
Artur Satayev7c3d1572019-08-13 18:03:58 +010038import android.text.TextUtils;
39
Ceci Wu3b90d482016-08-25 14:48:19 +080040import java.util.ArrayList;
41import java.util.List;
42
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043/**
David Brown94202fe2011-06-10 16:24:05 -070044 * Helper class to make it easier to run asynchronous caller-id lookup queries.
45 * @see CallerInfo
46 *
47 * {@hide}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049public class CallerInfoAsyncQuery {
Joe Onorato431bb222010-10-18 19:13:23 -040050 private static final boolean DBG = false;
Nicolas Cataniae2241582009-09-14 19:01:43 -070051 private static final String LOG_TAG = "CallerInfoAsyncQuery";
Wink Saville2563a3a2009-06-09 10:30:03 -070052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 private static final int EVENT_NEW_QUERY = 1;
54 private static final int EVENT_ADD_LISTENER = 2;
55 private static final int EVENT_END_OF_QUEUE = 3;
56 private static final int EVENT_EMERGENCY_NUMBER = 4;
57 private static final int EVENT_VOICEMAIL_NUMBER = 5;
Ceci Wu3b90d482016-08-25 14:48:19 +080058 private static final int EVENT_GET_GEO_DESCRIPTION = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60 private CallerInfoAsyncQueryHandler mHandler;
61
David Brown2ef46c62011-06-20 12:53:12 -070062 // If the CallerInfo query finds no contacts, should we use the
63 // PhoneNumberOfflineGeocoder to look up a "geo description"?
64 // (TODO: This could become a flag in config.xml if it ever needs to be
65 // configured on a per-product basis.)
David Browncec25c42011-06-23 14:17:27 -070066 private static final boolean ENABLE_UNKNOWN_NUMBER_GEO_DESCRIPTION = true;
David Brown2ef46c62011-06-20 12:53:12 -070067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 /**
69 * Interface for a CallerInfoAsyncQueryHandler result return.
70 */
71 public interface OnQueryCompleteListener {
72 /**
Wink Saville2563a3a2009-06-09 10:30:03 -070073 * Called when the query is complete.
74 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 public void onQueryComplete(int token, Object cookie, CallerInfo ci);
76 }
Wink Saville2563a3a2009-06-09 10:30:03 -070077
78
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 /**
80 * Wrap the cookie from the WorkerArgs with additional information needed by our
Wink Saville2563a3a2009-06-09 10:30:03 -070081 * classes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 */
83 private static final class CookieWrapper {
Artur Satayev7c3d1572019-08-13 18:03:58 +010084 @UnsupportedAppUsage
85 private CookieWrapper() {
86 }
87
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 public OnQueryCompleteListener listener;
89 public Object cookie;
90 public int event;
91 public String number;
Ceci Wu3b90d482016-08-25 14:48:19 +080092 public String geoDescription;
Wink Savillefb40dd42014-06-12 17:02:31 -070093
Wink Saville63f03dd2014-10-23 10:44:45 -070094 public int subId;
Wink Saville2563a3a2009-06-09 10:30:03 -070095 }
96
97
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 /**
99 * Simple exception used to communicate problems with the query pool.
100 */
101 public static class QueryPoolException extends SQLException {
102 public QueryPoolException(String error) {
103 super(error);
104 }
105 }
Wink Saville2563a3a2009-06-09 10:30:03 -0700106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 /**
Makoto Onuki5692dcc2014-07-17 14:57:04 -0700108 * @return {@link ContentResolver} for the "current" user.
109 */
110 static ContentResolver getCurrentProfileContentResolver(Context context) {
111
Hall Liue2f17aa2019-10-31 15:17:58 -0700112 if (DBG) Log.d(LOG_TAG, "Trying to get current content resolver...");
Makoto Onuki5692dcc2014-07-17 14:57:04 -0700113
114 final int currentUser = ActivityManager.getCurrentUser();
Adam Bookatz010d64f2021-10-01 00:20:00 -0700115 final int myUser = UserManager.get(context).getProcessUserId();
Makoto Onuki5692dcc2014-07-17 14:57:04 -0700116
Hall Liue2f17aa2019-10-31 15:17:58 -0700117 if (DBG) Log.d(LOG_TAG, "myUser=" + myUser + "currentUser=" + currentUser);
Makoto Onuki5692dcc2014-07-17 14:57:04 -0700118
119 if (myUser != currentUser) {
120 final Context otherContext;
121 try {
122 otherContext = context.createPackageContextAsUser(context.getPackageName(),
Chen Xu59d76e92019-08-05 11:39:13 -0700123 /* flags =*/ 0, UserHandle.of(currentUser));
Makoto Onuki5692dcc2014-07-17 14:57:04 -0700124 return otherContext.getContentResolver();
125 } catch (NameNotFoundException e) {
Hall Liue2f17aa2019-10-31 15:17:58 -0700126 Log.e(LOG_TAG, e, "Can't find self package");
Makoto Onuki5692dcc2014-07-17 14:57:04 -0700127 // Fall back to the primary user.
128 }
129 }
130 return context.getContentResolver();
131 }
132
133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * Our own implementation of the AsyncQueryHandler.
135 */
136 private class CallerInfoAsyncQueryHandler extends AsyncQueryHandler {
Wink Saville2563a3a2009-06-09 10:30:03 -0700137
Makoto Onuki5692dcc2014-07-17 14:57:04 -0700138 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 * The information relevant to each CallerInfo query. Each query may have multiple
140 * listeners, so each AsyncCursorInfo is associated with 2 or more CookieWrapper
141 * objects in the queue (one with a new query event, and one with a end event, with
142 * 0 or more additional listeners in between).
143 */
Makoto Onuki5692dcc2014-07-17 14:57:04 -0700144
145 /**
146 * Context passed by the caller.
147 *
148 * NOTE: The actual context we use for query may *not* be this context; since we query
149 * against the "current" contacts provider. In the constructor we pass the "current"
150 * context resolver (obtained via {@link #getCurrentProfileContentResolver) and pass it
151 * to the super class.
152 */
153 private Context mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 private Uri mQueryUri;
155 private CallerInfo mCallerInfo;
Ceci Wu3b90d482016-08-25 14:48:19 +0800156 private List<Runnable> mPendingListenerCallbacks = new ArrayList<>();
Wink Saville2563a3a2009-06-09 10:30:03 -0700157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 /**
159 * Our own query worker thread.
Wink Saville2563a3a2009-06-09 10:30:03 -0700160 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 * This thread handles the messages enqueued in the looper. The normal sequence
162 * of events is that a new query shows up in the looper queue, followed by 0 or
163 * more add listener requests, and then an end request. Of course, these requests
164 * can be interlaced with requests from other tokens, but is irrelevant to this
165 * handler since the handler has no state.
Wink Saville2563a3a2009-06-09 10:30:03 -0700166 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 * Note that we depend on the queue to keep things in order; in other words, the
Wink Saville2563a3a2009-06-09 10:30:03 -0700168 * looper queue must be FIFO with respect to input from the synchronous startQuery
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 * calls and output to this handleMessage call.
Wink Saville2563a3a2009-06-09 10:30:03 -0700170 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 * This use of the queue is required because CallerInfo objects may be accessed
172 * multiple times before the query is complete. All accesses (listeners) must be
173 * queued up and informed in order when the query is complete.
174 */
175 protected class CallerInfoWorkerHandler extends WorkerHandler {
176 public CallerInfoWorkerHandler(Looper looper) {
177 super(looper);
178 }
179
180 @Override
181 public void handleMessage(Message msg) {
182 WorkerArgs args = (WorkerArgs) msg.obj;
183 CookieWrapper cw = (CookieWrapper) args.cookie;
Wink Saville2563a3a2009-06-09 10:30:03 -0700184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 if (cw == null) {
186 // Normally, this should never be the case for calls originating
187 // from within this code.
Wink Saville2563a3a2009-06-09 10:30:03 -0700188 // However, if there is any code that this Handler calls (such as in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 // super.handleMessage) that DOES place unexpected messages on the
190 // queue, then we need pass these messages on.
Hall Liue2f17aa2019-10-31 15:17:58 -0700191 Log.i(LOG_TAG, "Unexpected command (CookieWrapper is null): " + msg.what +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 " ignored by CallerInfoWorkerHandler, passing onto parent.");
Wink Saville2563a3a2009-06-09 10:30:03 -0700193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 super.handleMessage(msg);
195 } else {
Wink Saville2563a3a2009-06-09 10:30:03 -0700196
Hall Liue2f17aa2019-10-31 15:17:58 -0700197 Log.d(LOG_TAG, "Processing event: " + cw.event + " token (arg1): " + msg.arg1 +
Wink Savillea4288072010-10-12 12:36:38 -0700198 " command: " + msg.what + " query URI: " + sanitizeUriToString(args.uri));
Wink Saville2563a3a2009-06-09 10:30:03 -0700199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 switch (cw.event) {
201 case EVENT_NEW_QUERY:
202 //start the sql command.
203 super.handleMessage(msg);
204 break;
205
206 // shortcuts to avoid query for recognized numbers.
207 case EVENT_EMERGENCY_NUMBER:
208 case EVENT_VOICEMAIL_NUMBER:
Wink Saville2563a3a2009-06-09 10:30:03 -0700209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 case EVENT_ADD_LISTENER:
211 case EVENT_END_OF_QUEUE:
212 // query was already completed, so just send the reply.
213 // passing the original token value back to the caller
214 // on top of the event values in arg1.
215 Message reply = args.handler.obtainMessage(msg.what);
216 reply.obj = args;
217 reply.arg1 = msg.arg1;
Wink Saville2563a3a2009-06-09 10:30:03 -0700218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 reply.sendToTarget();
Wink Saville2563a3a2009-06-09 10:30:03 -0700220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 break;
Ceci Wu3b90d482016-08-25 14:48:19 +0800222 case EVENT_GET_GEO_DESCRIPTION:
223 handleGeoDescription(msg);
224 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 default:
226 }
227 }
228 }
Ceci Wu3b90d482016-08-25 14:48:19 +0800229
230 private void handleGeoDescription(Message msg) {
231 WorkerArgs args = (WorkerArgs) msg.obj;
232 CookieWrapper cw = (CookieWrapper) args.cookie;
233 if (!TextUtils.isEmpty(cw.number) && cw.cookie != null && mContext != null) {
234 final long startTimeMillis = SystemClock.elapsedRealtime();
235 cw.geoDescription = CallerInfo.getGeoDescription(mContext, cw.number);
236 final long duration = SystemClock.elapsedRealtime() - startTimeMillis;
237 if (duration > 500) {
Hall Liue2f17aa2019-10-31 15:17:58 -0700238 if (DBG) Log.d(LOG_TAG, "[handleGeoDescription]" +
Ceci Wu3b90d482016-08-25 14:48:19 +0800239 "Spends long time to retrieve Geo description: " + duration);
240 }
241 }
242 Message reply = args.handler.obtainMessage(msg.what);
243 reply.obj = args;
244 reply.arg1 = msg.arg1;
245 reply.sendToTarget();
246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 }
Wink Saville2563a3a2009-06-09 10:30:03 -0700248
249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 /**
251 * Asynchronous query handler class for the contact / callerinfo object.
252 */
253 private CallerInfoAsyncQueryHandler(Context context) {
Makoto Onuki5692dcc2014-07-17 14:57:04 -0700254 super(getCurrentProfileContentResolver(context));
255 mContext = context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 }
257
258 @Override
259 protected Handler createHandler(Looper looper) {
260 return new CallerInfoWorkerHandler(looper);
261 }
262
263 /**
264 * Overrides onQueryComplete from AsyncQueryHandler.
Wink Saville2563a3a2009-06-09 10:30:03 -0700265 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 * This method takes into account the state of this class; we construct the CallerInfo
267 * object only once for each set of listeners. When the query thread has done its work
Wink Saville2563a3a2009-06-09 10:30:03 -0700268 * and calls this method, we inform the remaining listeners in the queue, until we're
269 * out of listeners. Once we get the message indicating that we should expect no new
270 * listeners for this CallerInfo object, we release the AsyncCursorInfo back into the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 * pool.
272 */
273 @Override
274 protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
Hall Liue2f17aa2019-10-31 15:17:58 -0700275 Log.d(LOG_TAG, "##### onQueryComplete() ##### query complete for token: " + token);
Wink Saville2563a3a2009-06-09 10:30:03 -0700276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 //get the cookie and notify the listener.
278 CookieWrapper cw = (CookieWrapper) cookie;
279 if (cw == null) {
280 // Normally, this should never be the case for calls originating
281 // from within this code.
Wink Saville2563a3a2009-06-09 10:30:03 -0700282 // However, if there is any code that calls this method, we should
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 // check the parameters to make sure they're viable.
Hall Liue2f17aa2019-10-31 15:17:58 -0700284 Log.i(LOG_TAG, "Cookie is null, ignoring onQueryComplete() request.");
Wink Savillefb40dd42014-06-12 17:02:31 -0700285 if (cursor != null) {
286 cursor.close();
287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 return;
289 }
Wink Saville2563a3a2009-06-09 10:30:03 -0700290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 if (cw.event == EVENT_END_OF_QUEUE) {
Ceci Wu3b90d482016-08-25 14:48:19 +0800292 for (Runnable r : mPendingListenerCallbacks) {
293 r.run();
294 }
295 mPendingListenerCallbacks.clear();
296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 release();
Wink Savillefb40dd42014-06-12 17:02:31 -0700298 if (cursor != null) {
299 cursor.close();
300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 return;
302 }
303
Ceci Wu3b90d482016-08-25 14:48:19 +0800304 // If the cw.event == EVENT_GET_GEO_DESCRIPTION, means it would not be the 1st
305 // time entering the onQueryComplete(), mCallerInfo should not be null.
306 if (cw.event == EVENT_GET_GEO_DESCRIPTION) {
307 if (mCallerInfo != null) {
308 mCallerInfo.geoDescription = cw.geoDescription;
309 }
310 // notify that we can clean up the queue after this.
311 CookieWrapper endMarker = new CookieWrapper();
312 endMarker.event = EVENT_END_OF_QUEUE;
313 startQuery(token, endMarker, null, null, null, null, null);
314 }
315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 // check the token and if needed, create the callerinfo object.
317 if (mCallerInfo == null) {
Makoto Onuki5692dcc2014-07-17 14:57:04 -0700318 if ((mContext == null) || (mQueryUri == null)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 throw new QueryPoolException
320 ("Bad context or query uri, or CallerInfoAsyncQuery already released.");
321 }
Wink Saville2563a3a2009-06-09 10:30:03 -0700322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 // adjust the callerInfo data as needed, and only if it was set from the
324 // initial query request.
325 // Change the callerInfo number ONLY if it is an emergency number or the
Wink Saville2563a3a2009-06-09 10:30:03 -0700326 // voicemail number, and adjust other data (including photoResource)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 // accordingly.
328 if (cw.event == EVENT_EMERGENCY_NUMBER) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 // Note we're setting the phone number here (refer to javadoc
Wink Saville2563a3a2009-06-09 10:30:03 -0700330 // comments at the top of CallerInfo class).
Makoto Onuki5692dcc2014-07-17 14:57:04 -0700331 mCallerInfo = new CallerInfo().markAsEmergency(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 } else if (cw.event == EVENT_VOICEMAIL_NUMBER) {
Hall Liue2f17aa2019-10-31 15:17:58 -0700333 mCallerInfo = new CallerInfo().markAsVoiceMail(mContext, cw.subId);
Wink Saville2563a3a2009-06-09 10:30:03 -0700334 } else {
Makoto Onuki5692dcc2014-07-17 14:57:04 -0700335 mCallerInfo = CallerInfo.getCallerInfo(mContext, mQueryUri, cursor);
Hall Liue2f17aa2019-10-31 15:17:58 -0700336 if (DBG) Log.d(LOG_TAG, "==> Got mCallerInfo: " + mCallerInfo);
David Brown158d3902010-09-27 16:29:14 -0700337
Hung-ying Tyan6fe795e2010-10-20 11:12:02 +0800338 CallerInfo newCallerInfo = CallerInfo.doSecondaryLookupIfNecessary(
Makoto Onuki5692dcc2014-07-17 14:57:04 -0700339 mContext, cw.number, mCallerInfo);
Hung-ying Tyan6fe795e2010-10-20 11:12:02 +0800340 if (newCallerInfo != mCallerInfo) {
341 mCallerInfo = newCallerInfo;
Hall Liue2f17aa2019-10-31 15:17:58 -0700342 if (DBG) Log.d(LOG_TAG, "#####async contact look up with numeric username"
Hung-ying Tyan6fe795e2010-10-20 11:12:02 +0800343 + mCallerInfo);
344 }
345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 // Use the number entered by the user for display.
347 if (!TextUtils.isEmpty(cw.number)) {
Chen Xub9a1eef2019-09-07 18:56:17 -0700348 mCallerInfo.setPhoneNumber(PhoneNumberUtils.formatNumber(cw.number,
David Brown94202fe2011-06-10 16:24:05 -0700349 mCallerInfo.normalizedNumber,
Chen Xub9a1eef2019-09-07 18:56:17 -0700350 CallerInfo.getCurrentCountryIso(mContext)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 }
Ceci Wu3b90d482016-08-25 14:48:19 +0800352
353 // This condition refer to the google default code for geo.
354 // If the number exists in Contacts, the CallCard would never show
355 // the geo description, so it would be unnecessary to query it.
356 if (ENABLE_UNKNOWN_NUMBER_GEO_DESCRIPTION) {
Chen Xub9a1eef2019-09-07 18:56:17 -0700357 if (TextUtils.isEmpty(mCallerInfo.getName())) {
Hall Liue2f17aa2019-10-31 15:17:58 -0700358 if (DBG) Log.d(LOG_TAG, "start querying geo description");
Ceci Wu3b90d482016-08-25 14:48:19 +0800359 cw.event = EVENT_GET_GEO_DESCRIPTION;
360 startQuery(token, cw, null, null, null, null, null);
361 return;
362 }
363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 }
Wink Saville2563a3a2009-06-09 10:30:03 -0700365
Hall Liue2f17aa2019-10-31 15:17:58 -0700366 if (DBG) Log.d(LOG_TAG, "constructing CallerInfo object for token: " + token);
Wink Saville2563a3a2009-06-09 10:30:03 -0700367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 //notify that we can clean up the queue after this.
369 CookieWrapper endMarker = new CookieWrapper();
370 endMarker.event = EVENT_END_OF_QUEUE;
David Brown158d3902010-09-27 16:29:14 -0700371 startQuery(token, endMarker, null, null, null, null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 }
Wink Saville2563a3a2009-06-09 10:30:03 -0700373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 //notify the listener that the query is complete.
375 if (cw.listener != null) {
Ceci Wu3b90d482016-08-25 14:48:19 +0800376 mPendingListenerCallbacks.add(new Runnable() {
377 @Override
378 public void run() {
Hall Liue2f17aa2019-10-31 15:17:58 -0700379 if (DBG) Log.d(LOG_TAG, "notifying listener: "
Ceci Wu3b90d482016-08-25 14:48:19 +0800380 + cw.listener.getClass().toString() + " for token: " + token
381 + mCallerInfo);
382 cw.listener.onQueryComplete(token, cw.cookie, mCallerInfo);
383 }
384 });
Hall Liu4c019102016-10-05 16:56:17 -0700385 } else {
Hall Liue2f17aa2019-10-31 15:17:58 -0700386 Log.w(LOG_TAG, "There is no listener to notify for this query.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 }
Wink Savillefb40dd42014-06-12 17:02:31 -0700388
389 if (cursor != null) {
390 cursor.close();
391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 }
393 }
Wink Saville2563a3a2009-06-09 10:30:03 -0700394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 /**
396 * Private constructor for factory methods.
397 */
398 private CallerInfoAsyncQuery() {
399 }
400
Wink Saville2563a3a2009-06-09 10:30:03 -0700401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 /**
403 * Factory method to start query with a Uri query spec
404 */
Wink Saville2563a3a2009-06-09 10:30:03 -0700405 public static CallerInfoAsyncQuery startQuery(int token, Context context, Uri contactRef,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 OnQueryCompleteListener listener, Object cookie) {
Wink Saville2563a3a2009-06-09 10:30:03 -0700407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 CallerInfoAsyncQuery c = new CallerInfoAsyncQuery();
409 c.allocate(context, contactRef);
410
Hall Liue2f17aa2019-10-31 15:17:58 -0700411 if (DBG) Log.d(LOG_TAG, "starting query for URI: " + contactRef + " handler: " + c.toString());
Wink Saville2563a3a2009-06-09 10:30:03 -0700412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 //create cookieWrapper, start query
414 CookieWrapper cw = new CookieWrapper();
415 cw.listener = listener;
416 cw.cookie = cookie;
417 cw.event = EVENT_NEW_QUERY;
Wink Saville2563a3a2009-06-09 10:30:03 -0700418
David Brown158d3902010-09-27 16:29:14 -0700419 c.mHandler.startQuery(token, cw, contactRef, null, null, null, null);
Wink Saville2563a3a2009-06-09 10:30:03 -0700420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 return c;
422 }
Wink Saville2563a3a2009-06-09 10:30:03 -0700423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 /**
David Brown158d3902010-09-27 16:29:14 -0700425 * Factory method to start the query based on a number.
426 *
427 * Note: if the number contains an "@" character we treat it
428 * as a SIP address, and look it up directly in the Data table
429 * rather than using the PhoneLookup table.
430 * TODO: But eventually we should expose two separate methods, one for
431 * numbers and one for SIP addresses, and then have
432 * PhoneUtils.startGetCallerInfo() decide which one to call based on
433 * the phone type of the incoming connection.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 */
Wink Saville2563a3a2009-06-09 10:30:03 -0700435 public static CallerInfoAsyncQuery startQuery(int token, Context context, String number,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 OnQueryCompleteListener listener, Object cookie) {
Wink Savillefb40dd42014-06-12 17:02:31 -0700437
Shishir Agrawal7ea3e8b2016-01-25 13:03:07 -0800438 int subId = SubscriptionManager.getDefaultSubscriptionId();
Wink Savillefb40dd42014-06-12 17:02:31 -0700439 return startQuery(token, context, number, listener, cookie, subId);
440 }
441
442 /**
443 * Factory method to start the query based on a number with specific subscription.
444 *
445 * Note: if the number contains an "@" character we treat it
446 * as a SIP address, and look it up directly in the Data table
447 * rather than using the PhoneLookup table.
448 * TODO: But eventually we should expose two separate methods, one for
449 * numbers and one for SIP addresses, and then have
450 * PhoneUtils.startGetCallerInfo() decide which one to call based on
451 * the phone type of the incoming connection.
452 */
453 public static CallerInfoAsyncQuery startQuery(int token, Context context, String number,
Wink Saville63f03dd2014-10-23 10:44:45 -0700454 OnQueryCompleteListener listener, Object cookie, int subId) {
Wink Savillefb40dd42014-06-12 17:02:31 -0700455
David Brown158d3902010-09-27 16:29:14 -0700456 if (DBG) {
Hall Liue2f17aa2019-10-31 15:17:58 -0700457 Log.d(LOG_TAG, "##### CallerInfoAsyncQuery startQuery()... #####");
458 Log.d(LOG_TAG, "- number: " + /*number*/ "xxxxxxx");
459 Log.d(LOG_TAG, "- cookie: " + cookie);
David Brown158d3902010-09-27 16:29:14 -0700460 }
461
462 // Construct the URI object and query params, and start the query.
463
Makoto Onukia2295e62014-07-10 15:32:16 -0700464 final Uri contactRef = PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI.buildUpon()
465 .appendPath(number)
466 .appendQueryParameter(PhoneLookup.QUERY_PARAMETER_SIP_ADDRESS,
467 String.valueOf(PhoneNumberUtils.isUriNumber(number)))
468 .build();
David Brown158d3902010-09-27 16:29:14 -0700469
470 if (DBG) {
Hall Liue2f17aa2019-10-31 15:17:58 -0700471 Log.d(LOG_TAG, "==> contactRef: " + sanitizeUriToString(contactRef));
David Brown158d3902010-09-27 16:29:14 -0700472 }
Wink Saville2563a3a2009-06-09 10:30:03 -0700473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 CallerInfoAsyncQuery c = new CallerInfoAsyncQuery();
475 c.allocate(context, contactRef);
476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 //create cookieWrapper, start query
478 CookieWrapper cw = new CookieWrapper();
479 cw.listener = listener;
480 cw.cookie = cookie;
481 cw.number = number;
Wink Savillefb40dd42014-06-12 17:02:31 -0700482 cw.subId = subId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483
Wink Saville2563a3a2009-06-09 10:30:03 -0700484 // check to see if these are recognized numbers, and use shortcuts if we can.
Taesu Lee902b89d2020-10-07 14:55:25 +0900485 TelephonyManager tm = context.getSystemService(TelephonyManager.class);
Tomasz Wasilczyk373cca22024-07-23 13:57:03 -0700486
Shuo Qian839508e2021-05-05 20:47:23 +0000487 boolean isEmergencyNumber = false;
488 try {
489 isEmergencyNumber = tm.isEmergencyNumber(number);
Tomasz Wasilczyk373cca22024-07-23 13:57:03 -0700490 } catch (IllegalStateException | UnsupportedOperationException ise) {
Shuo Qian839508e2021-05-05 20:47:23 +0000491 // Ignore the exception that Telephony is not up. Use PhoneNumberUtils API now.
492 // Ideally the PhoneNumberUtils API needs to be removed once the
493 // telphony service not up issue can be fixed (b/187412989)
Tomasz Wasilczyk373cca22024-07-23 13:57:03 -0700494 // UnsupportedOperationException: telephony.calling may not be supported on this device
Shuo Qian839508e2021-05-05 20:47:23 +0000495 isEmergencyNumber = PhoneNumberUtils.isLocalEmergencyNumber(context, number);
496 }
Tomasz Wasilczyk373cca22024-07-23 13:57:03 -0700497
498 boolean isVoicemailNumber;
499 try {
500 isVoicemailNumber = PhoneNumberUtils.isVoiceMailNumber(context, subId, number);
501 } catch (UnsupportedOperationException ex) {
502 isVoicemailNumber = false;
503 }
504
Shuo Qian839508e2021-05-05 20:47:23 +0000505 if (isEmergencyNumber) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 cw.event = EVENT_EMERGENCY_NUMBER;
Tomasz Wasilczyk373cca22024-07-23 13:57:03 -0700507 } else if (isVoicemailNumber) {
Nicolas Catania60d45f02009-09-15 18:32:02 -0700508 cw.event = EVENT_VOICEMAIL_NUMBER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 } else {
Nicolas Catania60d45f02009-09-15 18:32:02 -0700510 cw.event = EVENT_NEW_QUERY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 }
512
David Brown158d3902010-09-27 16:29:14 -0700513 c.mHandler.startQuery(token,
514 cw, // cookie
515 contactRef, // uri
516 null, // projection
Makoto Onukia2295e62014-07-10 15:32:16 -0700517 null, // selection
518 null, // selectionArgs
David Brown158d3902010-09-27 16:29:14 -0700519 null); // orderBy
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 return c;
David Brown158d3902010-09-27 16:29:14 -0700521 }
Wink Saville2563a3a2009-06-09 10:30:03 -0700522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 /**
524 * Method to add listeners to a currently running query
525 */
526 public void addQueryListener(int token, OnQueryCompleteListener listener, Object cookie) {
527
Hall Liue2f17aa2019-10-31 15:17:58 -0700528 if (DBG) Log.d(LOG_TAG, "adding listener to query: "
529 + sanitizeUriToString(mHandler.mQueryUri) + " handler: " + mHandler.toString());
Wink Saville2563a3a2009-06-09 10:30:03 -0700530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 //create cookieWrapper, add query request to end of queue.
532 CookieWrapper cw = new CookieWrapper();
533 cw.listener = listener;
534 cw.cookie = cookie;
535 cw.event = EVENT_ADD_LISTENER;
Wink Saville2563a3a2009-06-09 10:30:03 -0700536
David Brown158d3902010-09-27 16:29:14 -0700537 mHandler.startQuery(token, cw, null, null, null, null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 }
539
540 /**
541 * Method to create a new CallerInfoAsyncQueryHandler object, ensuring correct
542 * state of context and uri.
543 */
David Brown94202fe2011-06-10 16:24:05 -0700544 private void allocate(Context context, Uri contactRef) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 if ((context == null) || (contactRef == null)){
546 throw new QueryPoolException("Bad context or query uri.");
547 }
548 mHandler = new CallerInfoAsyncQueryHandler(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 mHandler.mQueryUri = contactRef;
550 }
551
552 /**
553 * Releases the relevant data.
554 */
Artur Satayev7c3d1572019-08-13 18:03:58 +0100555 @UnsupportedAppUsage
David Brown94202fe2011-06-10 16:24:05 -0700556 private void release() {
Makoto Onuki5692dcc2014-07-17 14:57:04 -0700557 mHandler.mContext = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 mHandler.mQueryUri = null;
559 mHandler.mCallerInfo = null;
560 mHandler = null;
561 }
Wink Saville2563a3a2009-06-09 10:30:03 -0700562
Wink Savillea4288072010-10-12 12:36:38 -0700563 private static String sanitizeUriToString(Uri uri) {
564 if (uri != null) {
565 String uriString = uri.toString();
566 int indexOfLastSlash = uriString.lastIndexOf('/');
567 if (indexOfLastSlash > 0) {
568 return uriString.substring(0, indexOfLastSlash) + "/xxxxxxx";
569 } else {
570 return uriString;
571 }
572 } else {
573 return "";
574 }
575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576}