| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.telecom; |
| 18 |
|
| 19 |
import android.app.ActivityManager; |
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.content.AsyncQueryHandler; |
| 22 |
import android.content.ContentResolver; |
| 23 |
import android.content.Context; |
| 24 |
import android.content.pm.PackageManager.NameNotFoundException; |
| 25 |
import android.database.Cursor; |
| 26 |
import android.database.SQLException; |
| 27 |
import android.net.Uri; |
| 28 |
import android.os.Handler; |
| 29 |
import android.os.Looper; |
| 30 |
import android.os.Message; |
| 31 |
import android.os.SystemClock; |
| 32 |
import android.os.UserHandle; |
| 33 |
import android.os.UserManager; |
| 34 |
import android.provider.ContactsContract.PhoneLookup; |
| 35 |
import android.telephony.PhoneNumberUtils; |
| 36 |
import android.telephony.SubscriptionManager; |
| 37 |
import android.telephony.TelephonyManager; |
| 38 |
import android.text.TextUtils; |
| 39 |
|
| 40 |
import java.util.ArrayList; |
| 41 |
import java.util.List; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public class CallerInfoAsyncQuery { |
| 50 |
private static final boolean DBG = false; |
| 51 |
private static final String LOG_TAG = "CallerInfoAsyncQuery"; |
| 52 |
|
| 53 |
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; |
| 58 |
private static final int EVENT_GET_GEO_DESCRIPTION = 6; |
| 59 |
|
| 60 |
private CallerInfoAsyncQueryHandler mHandler; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
private static final boolean ENABLE_UNKNOWN_NUMBER_GEO_DESCRIPTION = true; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public interface OnQueryCompleteListener { |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public void onQueryComplete(int token, Object cookie, CallerInfo ci); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
private static final class CookieWrapper { |
| 84 |
@UnsupportedAppUsage |
| 85 |
private CookieWrapper() { |
| 86 |
} |
| 87 |
|
| 88 |
public OnQueryCompleteListener listener; |
| 89 |
public Object cookie; |
| 90 |
public int event; |
| 91 |
public String number; |
| 92 |
public String geoDescription; |
| 93 |
|
| 94 |
public int subId; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public static class QueryPoolException extends SQLException { |
| 102 |
public QueryPoolException(String error) { |
| 103 |
super(error); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
static ContentResolver getCurrentProfileContentResolver(Context context) { |
| 111 |
|
| 112 |
if (DBG) Log.d(LOG_TAG, "Trying to get current content resolver..."); |
| 113 |
|
| 114 |
final int currentUser = ActivityManager.getCurrentUser(); |
| 115 |
final int myUser = UserManager.get(context).getProcessUserId(); |
| 116 |
|
| 117 |
if (DBG) Log.d(LOG_TAG, "myUser=" + myUser + "currentUser=" + currentUser); |
| 118 |
|
| 119 |
if (myUser != currentUser) { |
| 120 |
final Context otherContext; |
| 121 |
try { |
| 122 |
otherContext = context.createPackageContextAsUser(context.getPackageName(), |
| 123 |
0, UserHandle.of(currentUser)); |
| 124 |
return otherContext.getContentResolver(); |
| 125 |
} catch (NameNotFoundException e) { |
| 126 |
Log.e(LOG_TAG, e, "Can't find self package"); |
| 127 |
|
| 128 |
} |
| 129 |
} |
| 130 |
return context.getContentResolver(); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
private class CallerInfoAsyncQueryHandler extends AsyncQueryHandler { |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
private Context mContext; |
| 154 |
private Uri mQueryUri; |
| 155 |
private CallerInfo mCallerInfo; |
| 156 |
private List<Runnable> mPendingListenerCallbacks = new ArrayList<>(); |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 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; |
| 184 |
|
| 185 |
if (cw == null) { |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
Log.i(LOG_TAG, "Unexpected command (CookieWrapper is null): " + msg.what + |
| 192 |
" ignored by CallerInfoWorkerHandler, passing onto parent."); |
| 193 |
|
| 194 |
super.handleMessage(msg); |
| 195 |
} else { |
| 196 |
|
| 197 |
Log.d(LOG_TAG, "Processing event: " + cw.event + " token (arg1): " + msg.arg1 + |
| 198 |
" command: " + msg.what + " query URI: " + sanitizeUriToString(args.uri)); |
| 199 |
|
| 200 |
switch (cw.event) { |
| 201 |
case EVENT_NEW_QUERY: |
| 202 |
|
| 203 |
super.handleMessage(msg); |
| 204 |
break; |
| 205 |
|
| 206 |
|
| 207 |
case EVENT_EMERGENCY_NUMBER: |
| 208 |
case EVENT_VOICEMAIL_NUMBER: |
| 209 |
|
| 210 |
case EVENT_ADD_LISTENER: |
| 211 |
case EVENT_END_OF_QUEUE: |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
Message reply = args.handler.obtainMessage(msg.what); |
| 216 |
reply.obj = args; |
| 217 |
reply.arg1 = msg.arg1; |
| 218 |
|
| 219 |
reply.sendToTarget(); |
| 220 |
|
| 221 |
break; |
| 222 |
case EVENT_GET_GEO_DESCRIPTION: |
| 223 |
handleGeoDescription(msg); |
| 224 |
break; |
| 225 |
default: |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 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) { |
| 238 |
if (DBG) Log.d(LOG_TAG, "[handleGeoDescription]" + |
| 239 |
"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 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
private CallerInfoAsyncQueryHandler(Context context) { |
| 254 |
super(getCurrentProfileContentResolver(context)); |
| 255 |
mContext = context; |
| 256 |
} |
| 257 |
|
| 258 |
@Override |
| 259 |
protected Handler createHandler(Looper looper) { |
| 260 |
return new CallerInfoWorkerHandler(looper); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
@Override |
| 274 |
protected void onQueryComplete(int token, Object cookie, Cursor cursor) { |
| 275 |
Log.d(LOG_TAG, "##### onQueryComplete() ##### query complete for token: " + token); |
| 276 |
|
| 277 |
|
| 278 |
CookieWrapper cw = (CookieWrapper) cookie; |
| 279 |
if (cw == null) { |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
Log.i(LOG_TAG, "Cookie is null, ignoring onQueryComplete() request."); |
| 285 |
if (cursor != null) { |
| 286 |
cursor.close(); |
| 287 |
} |
| 288 |
return; |
| 289 |
} |
| 290 |
|
| 291 |
if (cw.event == EVENT_END_OF_QUEUE) { |
| 292 |
for (Runnable r : mPendingListenerCallbacks) { |
| 293 |
r.run(); |
| 294 |
} |
| 295 |
mPendingListenerCallbacks.clear(); |
| 296 |
|
| 297 |
release(); |
| 298 |
if (cursor != null) { |
| 299 |
cursor.close(); |
| 300 |
} |
| 301 |
return; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
if (cw.event == EVENT_GET_GEO_DESCRIPTION) { |
| 307 |
if (mCallerInfo != null) { |
| 308 |
mCallerInfo.geoDescription = cw.geoDescription; |
| 309 |
} |
| 310 |
|
| 311 |
CookieWrapper endMarker = new CookieWrapper(); |
| 312 |
endMarker.event = EVENT_END_OF_QUEUE; |
| 313 |
startQuery(token, endMarker, null, null, null, null, null); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
if (mCallerInfo == null) { |
| 318 |
if ((mContext == null) || (mQueryUri == null)) { |
| 319 |
throw new QueryPoolException |
| 320 |
("Bad context or query uri, or CallerInfoAsyncQuery already released."); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
if (cw.event == EVENT_EMERGENCY_NUMBER) { |
| 329 |
|
| 330 |
|
| 331 |
mCallerInfo = new CallerInfo().markAsEmergency(mContext); |
| 332 |
} else if (cw.event == EVENT_VOICEMAIL_NUMBER) { |
| 333 |
mCallerInfo = new CallerInfo().markAsVoiceMail(mContext, cw.subId); |
| 334 |
} else { |
| 335 |
mCallerInfo = CallerInfo.getCallerInfo(mContext, mQueryUri, cursor); |
| 336 |
if (DBG) Log.d(LOG_TAG, "==> Got mCallerInfo: " + mCallerInfo); |
| 337 |
|
| 338 |
CallerInfo newCallerInfo = CallerInfo.doSecondaryLookupIfNecessary( |
| 339 |
mContext, cw.number, mCallerInfo); |
| 340 |
if (newCallerInfo != mCallerInfo) { |
| 341 |
mCallerInfo = newCallerInfo; |
| 342 |
if (DBG) Log.d(LOG_TAG, "#####async contact look up with numeric username" |
| 343 |
+ mCallerInfo); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
if (!TextUtils.isEmpty(cw.number)) { |
| 348 |
mCallerInfo.setPhoneNumber(PhoneNumberUtils.formatNumber(cw.number, |
| 349 |
mCallerInfo.normalizedNumber, |
| 350 |
CallerInfo.getCurrentCountryIso(mContext))); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
if (ENABLE_UNKNOWN_NUMBER_GEO_DESCRIPTION) { |
| 357 |
if (TextUtils.isEmpty(mCallerInfo.getName())) { |
| 358 |
if (DBG) Log.d(LOG_TAG, "start querying geo description"); |
| 359 |
cw.event = EVENT_GET_GEO_DESCRIPTION; |
| 360 |
startQuery(token, cw, null, null, null, null, null); |
| 361 |
return; |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
if (DBG) Log.d(LOG_TAG, "constructing CallerInfo object for token: " + token); |
| 367 |
|
| 368 |
|
| 369 |
CookieWrapper endMarker = new CookieWrapper(); |
| 370 |
endMarker.event = EVENT_END_OF_QUEUE; |
| 371 |
startQuery(token, endMarker, null, null, null, null, null); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
if (cw.listener != null) { |
| 376 |
mPendingListenerCallbacks.add(new Runnable() { |
| 377 |
@Override |
| 378 |
public void run() { |
| 379 |
if (DBG) Log.d(LOG_TAG, "notifying listener: " |
| 380 |
+ cw.listener.getClass().toString() + " for token: " + token |
| 381 |
+ mCallerInfo); |
| 382 |
cw.listener.onQueryComplete(token, cw.cookie, mCallerInfo); |
| 383 |
} |
| 384 |
}); |
| 385 |
} else { |
| 386 |
Log.w(LOG_TAG, "There is no listener to notify for this query."); |
| 387 |
} |
| 388 |
|
| 389 |
if (cursor != null) { |
| 390 |
cursor.close(); |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
private CallerInfoAsyncQuery() { |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
public static CallerInfoAsyncQuery startQuery(int token, Context context, Uri contactRef, |
| 406 |
OnQueryCompleteListener listener, Object cookie) { |
| 407 |
|
| 408 |
CallerInfoAsyncQuery c = new CallerInfoAsyncQuery(); |
| 409 |
c.allocate(context, contactRef); |
| 410 |
|
| 411 |
if (DBG) Log.d(LOG_TAG, "starting query for URI: " + contactRef + " handler: " + c.toString()); |
| 412 |
|
| 413 |
|
| 414 |
CookieWrapper cw = new CookieWrapper(); |
| 415 |
cw.listener = listener; |
| 416 |
cw.cookie = cookie; |
| 417 |
cw.event = EVENT_NEW_QUERY; |
| 418 |
|
| 419 |
c.mHandler.startQuery(token, cw, contactRef, null, null, null, null); |
| 420 |
|
| 421 |
return c; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
public static CallerInfoAsyncQuery startQuery(int token, Context context, String number, |
| 436 |
OnQueryCompleteListener listener, Object cookie) { |
| 437 |
|
| 438 |
int subId = SubscriptionManager.getDefaultSubscriptionId(); |
| 439 |
return startQuery(token, context, number, listener, cookie, subId); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
public static CallerInfoAsyncQuery startQuery(int token, Context context, String number, |
| 454 |
OnQueryCompleteListener listener, Object cookie, int subId) { |
| 455 |
|
| 456 |
if (DBG) { |
| 457 |
Log.d(LOG_TAG, "##### CallerInfoAsyncQuery startQuery()... #####"); |
| 458 |
Log.d(LOG_TAG, "- number: " + "xxxxxxx"); |
| 459 |
Log.d(LOG_TAG, "- cookie: " + cookie); |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
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(); |
| 469 |
|
| 470 |
if (DBG) { |
| 471 |
Log.d(LOG_TAG, "==> contactRef: " + sanitizeUriToString(contactRef)); |
| 472 |
} |
| 473 |
|
| 474 |
CallerInfoAsyncQuery c = new CallerInfoAsyncQuery(); |
| 475 |
c.allocate(context, contactRef); |
| 476 |
|
| 477 |
|
| 478 |
CookieWrapper cw = new CookieWrapper(); |
| 479 |
cw.listener = listener; |
| 480 |
cw.cookie = cookie; |
| 481 |
cw.number = number; |
| 482 |
cw.subId = subId; |
| 483 |
|
| 484 |
|
| 485 |
TelephonyManager tm = context.getSystemService(TelephonyManager.class); |
| 486 |
boolean isEmergencyNumber = false; |
| 487 |
try { |
| 488 |
isEmergencyNumber = tm.isEmergencyNumber(number); |
| 489 |
} catch (IllegalStateException ise) { |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
isEmergencyNumber = PhoneNumberUtils.isLocalEmergencyNumber(context, number); |
| 494 |
} |
| 495 |
if (isEmergencyNumber) { |
| 496 |
cw.event = EVENT_EMERGENCY_NUMBER; |
| 497 |
} else if (PhoneNumberUtils.isVoiceMailNumber(context, subId, number)) { |
| 498 |
cw.event = EVENT_VOICEMAIL_NUMBER; |
| 499 |
} else { |
| 500 |
cw.event = EVENT_NEW_QUERY; |
| 501 |
} |
| 502 |
|
| 503 |
c.mHandler.startQuery(token, |
| 504 |
cw, |
| 505 |
contactRef, |
| 506 |
null, |
| 507 |
null, |
| 508 |
null, |
| 509 |
null); |
| 510 |
return c; |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
public void addQueryListener(int token, OnQueryCompleteListener listener, Object cookie) { |
| 517 |
|
| 518 |
if (DBG) Log.d(LOG_TAG, "adding listener to query: " |
| 519 |
+ sanitizeUriToString(mHandler.mQueryUri) + " handler: " + mHandler.toString()); |
| 520 |
|
| 521 |
|
| 522 |
CookieWrapper cw = new CookieWrapper(); |
| 523 |
cw.listener = listener; |
| 524 |
cw.cookie = cookie; |
| 525 |
cw.event = EVENT_ADD_LISTENER; |
| 526 |
|
| 527 |
mHandler.startQuery(token, cw, null, null, null, null, null); |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
private void allocate(Context context, Uri contactRef) { |
| 535 |
if ((context == null) || (contactRef == null)){ |
| 536 |
throw new QueryPoolException("Bad context or query uri."); |
| 537 |
} |
| 538 |
mHandler = new CallerInfoAsyncQueryHandler(context); |
| 539 |
mHandler.mQueryUri = contactRef; |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
@UnsupportedAppUsage |
| 546 |
private void release() { |
| 547 |
mHandler.mContext = null; |
| 548 |
mHandler.mQueryUri = null; |
| 549 |
mHandler.mCallerInfo = null; |
| 550 |
mHandler = null; |
| 551 |
} |
| 552 |
|
| 553 |
private static String sanitizeUriToString(Uri uri) { |
| 554 |
if (uri != null) { |
| 555 |
String uriString = uri.toString(); |
| 556 |
int indexOfLastSlash = uriString.lastIndexOf('/'); |
| 557 |
if (indexOfLastSlash > 0) { |
| 558 |
return uriString.substring(0, indexOfLastSlash) + "/xxxxxxx"; |
| 559 |
} else { |
| 560 |
return uriString; |
| 561 |
} |
| 562 |
} else { |
| 563 |
return ""; |
| 564 |
} |
| 565 |
} |
| 566 |
} |