| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 17 | package android.telecom; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 |  | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 19 | import android.app.ActivityManager; | 
| Artur Satayev | 9a5c310 | 2020-01-23 16:49:36 +0000 | [diff] [blame] | 20 | import android.compat.annotation.UnsupportedAppUsage; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.AsyncQueryHandler; | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 22 | import android.content.ContentResolver; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.Context; | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageManager.NameNotFoundException; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 31 | import android.os.SystemClock; | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 32 | import android.os.UserHandle; | 
|  | 33 | import android.os.UserManager; | 
| Dmitri Plotnikov | 3c513ed | 2009-08-19 15:56:30 -0700 | [diff] [blame] | 34 | import android.provider.ContactsContract.PhoneLookup; | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 35 | import android.telephony.PhoneNumberUtils; | 
|  | 36 | import android.telephony.SubscriptionManager; | 
| Taesu Lee | 902b89d | 2020-10-07 14:55:25 +0900 | [diff] [blame] | 37 | import android.telephony.TelephonyManager; | 
| Artur Satayev | 7c3d157 | 2019-08-13 18:03:58 +0100 | [diff] [blame] | 38 | import android.text.TextUtils; | 
|  | 39 |  | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 40 | import java.util.ArrayList; | 
|  | 41 | import java.util.List; | 
|  | 42 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | /** | 
| David Brown | 94202fe | 2011-06-10 16:24:05 -0700 | [diff] [blame] | 44 | * Helper class to make it easier to run asynchronous caller-id lookup queries. | 
|  | 45 | * @see CallerInfo | 
|  | 46 | * | 
|  | 47 | * {@hide} | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | */ | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | public class CallerInfoAsyncQuery { | 
| Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 50 | private static final boolean DBG = false; | 
| Nicolas Catania | e224158 | 2009-09-14 19:01:43 -0700 | [diff] [blame] | 51 | private static final String LOG_TAG = "CallerInfoAsyncQuery"; | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 52 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 58 | private static final int EVENT_GET_GEO_DESCRIPTION = 6; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 |  | 
|  | 60 | private CallerInfoAsyncQueryHandler mHandler; | 
|  | 61 |  | 
| David Brown | 2ef46c6 | 2011-06-20 12:53:12 -0700 | [diff] [blame] | 62 | // 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 Brown | cec25c4 | 2011-06-23 14:17:27 -0700 | [diff] [blame] | 66 | private static final boolean ENABLE_UNKNOWN_NUMBER_GEO_DESCRIPTION = true; | 
| David Brown | 2ef46c6 | 2011-06-20 12:53:12 -0700 | [diff] [blame] | 67 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | /** | 
|  | 69 | * Interface for a CallerInfoAsyncQueryHandler result return. | 
|  | 70 | */ | 
|  | 71 | public interface OnQueryCompleteListener { | 
|  | 72 | /** | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 73 | * Called when the query is complete. | 
|  | 74 | */ | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | public void onQueryComplete(int token, Object cookie, CallerInfo ci); | 
|  | 76 | } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 77 |  | 
|  | 78 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | /** | 
|  | 80 | * Wrap the cookie from the WorkerArgs with additional information needed by our | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 81 | * classes. | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | */ | 
|  | 83 | private static final class CookieWrapper { | 
| Artur Satayev | 7c3d157 | 2019-08-13 18:03:58 +0100 | [diff] [blame] | 84 | @UnsupportedAppUsage | 
|  | 85 | private CookieWrapper() { | 
|  | 86 | } | 
|  | 87 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | public OnQueryCompleteListener listener; | 
|  | 89 | public Object cookie; | 
|  | 90 | public int event; | 
|  | 91 | public String number; | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 92 | public String geoDescription; | 
| Wink Saville | fb40dd4 | 2014-06-12 17:02:31 -0700 | [diff] [blame] | 93 |  | 
| Wink Saville | 63f03dd | 2014-10-23 10:44:45 -0700 | [diff] [blame] | 94 | public int subId; | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 95 | } | 
|  | 96 |  | 
|  | 97 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | /** | 
|  | 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 Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 106 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | /** | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 108 | * @return {@link ContentResolver} for the "current" user. | 
|  | 109 | */ | 
|  | 110 | static ContentResolver getCurrentProfileContentResolver(Context context) { | 
|  | 111 |  | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 112 | if (DBG) Log.d(LOG_TAG, "Trying to get current content resolver..."); | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 113 |  | 
|  | 114 | final int currentUser = ActivityManager.getCurrentUser(); | 
| Adam Bookatz | 010d64f | 2021-10-01 00:20:00 -0700 | [diff] [blame] | 115 | final int myUser = UserManager.get(context).getProcessUserId(); | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 116 |  | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 117 | if (DBG) Log.d(LOG_TAG, "myUser=" + myUser + "currentUser=" + currentUser); | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 118 |  | 
|  | 119 | if (myUser != currentUser) { | 
|  | 120 | final Context otherContext; | 
|  | 121 | try { | 
|  | 122 | otherContext = context.createPackageContextAsUser(context.getPackageName(), | 
| Chen Xu | 59d76e9 | 2019-08-05 11:39:13 -0700 | [diff] [blame] | 123 | /* flags =*/ 0, UserHandle.of(currentUser)); | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 124 | return otherContext.getContentResolver(); | 
|  | 125 | } catch (NameNotFoundException e) { | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 126 | Log.e(LOG_TAG, e, "Can't find self package"); | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 127 | // Fall back to the primary user. | 
|  | 128 | } | 
|  | 129 | } | 
|  | 130 | return context.getContentResolver(); | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | /** | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | * Our own implementation of the AsyncQueryHandler. | 
|  | 135 | */ | 
|  | 136 | private class CallerInfoAsyncQueryHandler extends AsyncQueryHandler { | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 137 |  | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 138 | /* | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | * 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 Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 144 |  | 
|  | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | private Uri mQueryUri; | 
|  | 155 | private CallerInfo mCallerInfo; | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 156 | private List<Runnable> mPendingListenerCallbacks = new ArrayList<>(); | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 157 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | /** | 
|  | 159 | * Our own query worker thread. | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 160 | * | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | * 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 Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 166 | * | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | * Note that we depend on the queue to keep things in order; in other words, the | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 168 | * looper queue must be FIFO with respect to input from the synchronous startQuery | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | * calls and output to this handleMessage call. | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 170 | * | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | * 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 Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 184 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | if (cw == null) { | 
|  | 186 | // Normally, this should never be the case for calls originating | 
|  | 187 | // from within this code. | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 188 | // However, if there is any code that this Handler calls (such as in | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | // super.handleMessage) that DOES place unexpected messages on the | 
|  | 190 | // queue, then we need pass these messages on. | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 191 | Log.i(LOG_TAG, "Unexpected command (CookieWrapper is null): " + msg.what + | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | " ignored by CallerInfoWorkerHandler, passing onto parent."); | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 193 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | super.handleMessage(msg); | 
|  | 195 | } else { | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 196 |  | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 197 | Log.d(LOG_TAG, "Processing event: " + cw.event + " token (arg1): " + msg.arg1 + | 
| Wink Saville | a428807 | 2010-10-12 12:36:38 -0700 | [diff] [blame] | 198 | " command: " + msg.what + " query URI: " + sanitizeUriToString(args.uri)); | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 199 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | 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 Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 209 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | 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 Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 218 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | reply.sendToTarget(); | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 220 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | break; | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 222 | case EVENT_GET_GEO_DESCRIPTION: | 
|  | 223 | handleGeoDescription(msg); | 
|  | 224 | break; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | default: | 
|  | 226 | } | 
|  | 227 | } | 
|  | 228 | } | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 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) { | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 238 | if (DBG) Log.d(LOG_TAG, "[handleGeoDescription]" + | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 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 | } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 248 |  | 
|  | 249 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | /** | 
|  | 251 | * Asynchronous query handler class for the contact / callerinfo object. | 
|  | 252 | */ | 
|  | 253 | private CallerInfoAsyncQueryHandler(Context context) { | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 254 | super(getCurrentProfileContentResolver(context)); | 
|  | 255 | mContext = context; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | } | 
|  | 257 |  | 
|  | 258 | @Override | 
|  | 259 | protected Handler createHandler(Looper looper) { | 
|  | 260 | return new CallerInfoWorkerHandler(looper); | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | /** | 
|  | 264 | * Overrides onQueryComplete from AsyncQueryHandler. | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 265 | * | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | * 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 Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 268 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | * pool. | 
|  | 272 | */ | 
|  | 273 | @Override | 
|  | 274 | protected void onQueryComplete(int token, Object cookie, Cursor cursor) { | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 275 | Log.d(LOG_TAG, "##### onQueryComplete() #####   query complete for token: " + token); | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 276 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | //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 Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 282 | // However, if there is any code that calls this method, we should | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | // check the parameters to make sure they're viable. | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 284 | Log.i(LOG_TAG, "Cookie is null, ignoring onQueryComplete() request."); | 
| Wink Saville | fb40dd4 | 2014-06-12 17:02:31 -0700 | [diff] [blame] | 285 | if (cursor != null) { | 
|  | 286 | cursor.close(); | 
|  | 287 | } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | return; | 
|  | 289 | } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 290 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | if (cw.event == EVENT_END_OF_QUEUE) { | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 292 | for (Runnable r : mPendingListenerCallbacks) { | 
|  | 293 | r.run(); | 
|  | 294 | } | 
|  | 295 | mPendingListenerCallbacks.clear(); | 
|  | 296 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | release(); | 
| Wink Saville | fb40dd4 | 2014-06-12 17:02:31 -0700 | [diff] [blame] | 298 | if (cursor != null) { | 
|  | 299 | cursor.close(); | 
|  | 300 | } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | return; | 
|  | 302 | } | 
|  | 303 |  | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 304 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | // check the token and if needed, create the callerinfo object. | 
|  | 317 | if (mCallerInfo == null) { | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 318 | if ((mContext == null) || (mQueryUri == null)) { | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | throw new QueryPoolException | 
|  | 320 | ("Bad context or query uri, or CallerInfoAsyncQuery already released."); | 
|  | 321 | } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 322 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | // 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 Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 326 | // voicemail number, and adjust other data (including photoResource) | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | // accordingly. | 
|  | 328 | if (cw.event == EVENT_EMERGENCY_NUMBER) { | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | // Note we're setting the phone number here (refer to javadoc | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 330 | // comments at the top of CallerInfo class). | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 331 | mCallerInfo = new CallerInfo().markAsEmergency(mContext); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | } else if (cw.event == EVENT_VOICEMAIL_NUMBER) { | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 333 | mCallerInfo = new CallerInfo().markAsVoiceMail(mContext, cw.subId); | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 334 | } else { | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 335 | mCallerInfo = CallerInfo.getCallerInfo(mContext, mQueryUri, cursor); | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 336 | if (DBG) Log.d(LOG_TAG, "==> Got mCallerInfo: " + mCallerInfo); | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 337 |  | 
| Hung-ying Tyan | 6fe795e | 2010-10-20 11:12:02 +0800 | [diff] [blame] | 338 | CallerInfo newCallerInfo = CallerInfo.doSecondaryLookupIfNecessary( | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 339 | mContext, cw.number, mCallerInfo); | 
| Hung-ying Tyan | 6fe795e | 2010-10-20 11:12:02 +0800 | [diff] [blame] | 340 | if (newCallerInfo != mCallerInfo) { | 
|  | 341 | mCallerInfo = newCallerInfo; | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 342 | if (DBG) Log.d(LOG_TAG, "#####async contact look up with numeric username" | 
| Hung-ying Tyan | 6fe795e | 2010-10-20 11:12:02 +0800 | [diff] [blame] | 343 | + mCallerInfo); | 
|  | 344 | } | 
|  | 345 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | // Use the number entered by the user for display. | 
|  | 347 | if (!TextUtils.isEmpty(cw.number)) { | 
| Chen Xu | b9a1eef | 2019-09-07 18:56:17 -0700 | [diff] [blame] | 348 | mCallerInfo.setPhoneNumber(PhoneNumberUtils.formatNumber(cw.number, | 
| David Brown | 94202fe | 2011-06-10 16:24:05 -0700 | [diff] [blame] | 349 | mCallerInfo.normalizedNumber, | 
| Chen Xu | b9a1eef | 2019-09-07 18:56:17 -0700 | [diff] [blame] | 350 | CallerInfo.getCurrentCountryIso(mContext))); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | } | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 352 |  | 
|  | 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 Xu | b9a1eef | 2019-09-07 18:56:17 -0700 | [diff] [blame] | 357 | if (TextUtils.isEmpty(mCallerInfo.getName())) { | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 358 | if (DBG) Log.d(LOG_TAG, "start querying geo description"); | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 359 | cw.event = EVENT_GET_GEO_DESCRIPTION; | 
|  | 360 | startQuery(token, cw, null, null, null, null, null); | 
|  | 361 | return; | 
|  | 362 | } | 
|  | 363 | } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 365 |  | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 366 | if (DBG) Log.d(LOG_TAG, "constructing CallerInfo object for token: " + token); | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 367 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | //notify that we can clean up the queue after this. | 
|  | 369 | CookieWrapper endMarker = new CookieWrapper(); | 
|  | 370 | endMarker.event = EVENT_END_OF_QUEUE; | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 371 | startQuery(token, endMarker, null, null, null, null, null); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 373 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | //notify the listener that the query is complete. | 
|  | 375 | if (cw.listener != null) { | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 376 | mPendingListenerCallbacks.add(new Runnable() { | 
|  | 377 | @Override | 
|  | 378 | public void run() { | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 379 | if (DBG) Log.d(LOG_TAG, "notifying listener: " | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 380 | + cw.listener.getClass().toString() + " for token: " + token | 
|  | 381 | + mCallerInfo); | 
|  | 382 | cw.listener.onQueryComplete(token, cw.cookie, mCallerInfo); | 
|  | 383 | } | 
|  | 384 | }); | 
| Hall Liu | 4c01910 | 2016-10-05 16:56:17 -0700 | [diff] [blame] | 385 | } else { | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 386 | Log.w(LOG_TAG, "There is no listener to notify for this query."); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | } | 
| Wink Saville | fb40dd4 | 2014-06-12 17:02:31 -0700 | [diff] [blame] | 388 |  | 
|  | 389 | if (cursor != null) { | 
|  | 390 | cursor.close(); | 
|  | 391 | } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | } | 
|  | 393 | } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 394 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | /** | 
|  | 396 | * Private constructor for factory methods. | 
|  | 397 | */ | 
|  | 398 | private CallerInfoAsyncQuery() { | 
|  | 399 | } | 
|  | 400 |  | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 401 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | /** | 
|  | 403 | * Factory method to start query with a Uri query spec | 
|  | 404 | */ | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 405 | public static CallerInfoAsyncQuery startQuery(int token, Context context, Uri contactRef, | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | OnQueryCompleteListener listener, Object cookie) { | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 407 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | CallerInfoAsyncQuery c = new CallerInfoAsyncQuery(); | 
|  | 409 | c.allocate(context, contactRef); | 
|  | 410 |  | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 411 | if (DBG) Log.d(LOG_TAG, "starting query for URI: " + contactRef + " handler: " + c.toString()); | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 412 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | //create cookieWrapper, start query | 
|  | 414 | CookieWrapper cw = new CookieWrapper(); | 
|  | 415 | cw.listener = listener; | 
|  | 416 | cw.cookie = cookie; | 
|  | 417 | cw.event = EVENT_NEW_QUERY; | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 418 |  | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 419 | c.mHandler.startQuery(token, cw, contactRef, null, null, null, null); | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 420 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | return c; | 
|  | 422 | } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 423 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | /** | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 425 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | */ | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 435 | public static CallerInfoAsyncQuery startQuery(int token, Context context, String number, | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | OnQueryCompleteListener listener, Object cookie) { | 
| Wink Saville | fb40dd4 | 2014-06-12 17:02:31 -0700 | [diff] [blame] | 437 |  | 
| Shishir Agrawal | 7ea3e8b | 2016-01-25 13:03:07 -0800 | [diff] [blame] | 438 | int subId = SubscriptionManager.getDefaultSubscriptionId(); | 
| Wink Saville | fb40dd4 | 2014-06-12 17:02:31 -0700 | [diff] [blame] | 439 | 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 Saville | 63f03dd | 2014-10-23 10:44:45 -0700 | [diff] [blame] | 454 | OnQueryCompleteListener listener, Object cookie, int subId) { | 
| Wink Saville | fb40dd4 | 2014-06-12 17:02:31 -0700 | [diff] [blame] | 455 |  | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 456 | if (DBG) { | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 457 | Log.d(LOG_TAG, "##### CallerInfoAsyncQuery startQuery()... #####"); | 
|  | 458 | Log.d(LOG_TAG, "- number: " + /*number*/ "xxxxxxx"); | 
|  | 459 | Log.d(LOG_TAG, "- cookie: " + cookie); | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 460 | } | 
|  | 461 |  | 
|  | 462 | // Construct the URI object and query params, and start the query. | 
|  | 463 |  | 
| Makoto Onuki | a2295e6 | 2014-07-10 15:32:16 -0700 | [diff] [blame] | 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(); | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 469 |  | 
|  | 470 | if (DBG) { | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 471 | Log.d(LOG_TAG, "==> contactRef: " + sanitizeUriToString(contactRef)); | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 472 | } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 473 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | CallerInfoAsyncQuery c = new CallerInfoAsyncQuery(); | 
|  | 475 | c.allocate(context, contactRef); | 
|  | 476 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | //create cookieWrapper, start query | 
|  | 478 | CookieWrapper cw = new CookieWrapper(); | 
|  | 479 | cw.listener = listener; | 
|  | 480 | cw.cookie = cookie; | 
|  | 481 | cw.number = number; | 
| Wink Saville | fb40dd4 | 2014-06-12 17:02:31 -0700 | [diff] [blame] | 482 | cw.subId = subId; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 |  | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 484 | // check to see if these are recognized numbers, and use shortcuts if we can. | 
| Taesu Lee | 902b89d | 2020-10-07 14:55:25 +0900 | [diff] [blame] | 485 | TelephonyManager tm = context.getSystemService(TelephonyManager.class); | 
| Shuo Qian | 839508e | 2021-05-05 20:47:23 +0000 | [diff] [blame] | 486 | boolean isEmergencyNumber = false; | 
|  | 487 | try { | 
|  | 488 | isEmergencyNumber = tm.isEmergencyNumber(number); | 
|  | 489 | } catch (IllegalStateException ise) { | 
|  | 490 | // Ignore the exception that Telephony is not up. Use PhoneNumberUtils API now. | 
|  | 491 | // Ideally the PhoneNumberUtils API needs to be removed once the | 
|  | 492 | // telphony service not up issue can be fixed (b/187412989) | 
|  | 493 | isEmergencyNumber = PhoneNumberUtils.isLocalEmergencyNumber(context, number); | 
|  | 494 | } | 
|  | 495 | if (isEmergencyNumber) { | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | cw.event = EVENT_EMERGENCY_NUMBER; | 
| Jordan Liu | 360d567 | 2016-11-09 13:23:42 -0800 | [diff] [blame] | 497 | } else if (PhoneNumberUtils.isVoiceMailNumber(context, subId, number)) { | 
| Nicolas Catania | 60d45f0 | 2009-09-15 18:32:02 -0700 | [diff] [blame] | 498 | cw.event = EVENT_VOICEMAIL_NUMBER; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | } else { | 
| Nicolas Catania | 60d45f0 | 2009-09-15 18:32:02 -0700 | [diff] [blame] | 500 | cw.event = EVENT_NEW_QUERY; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | } | 
|  | 502 |  | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 503 | c.mHandler.startQuery(token, | 
|  | 504 | cw,  // cookie | 
|  | 505 | contactRef,  // uri | 
|  | 506 | null,  // projection | 
| Makoto Onuki | a2295e6 | 2014-07-10 15:32:16 -0700 | [diff] [blame] | 507 | null,  // selection | 
|  | 508 | null,  // selectionArgs | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 509 | null);  // orderBy | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | return c; | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 511 | } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 512 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | /** | 
|  | 514 | * Method to add listeners to a currently running query | 
|  | 515 | */ | 
|  | 516 | public void addQueryListener(int token, OnQueryCompleteListener listener, Object cookie) { | 
|  | 517 |  | 
| Hall Liu | e2f17aa | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 518 | if (DBG) Log.d(LOG_TAG, "adding listener to query: " | 
|  | 519 | + sanitizeUriToString(mHandler.mQueryUri) + " handler: " + mHandler.toString()); | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 520 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | //create cookieWrapper, add query request to end of queue. | 
|  | 522 | CookieWrapper cw = new CookieWrapper(); | 
|  | 523 | cw.listener = listener; | 
|  | 524 | cw.cookie = cookie; | 
|  | 525 | cw.event = EVENT_ADD_LISTENER; | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 526 |  | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 527 | mHandler.startQuery(token, cw, null, null, null, null, null); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | } | 
|  | 529 |  | 
|  | 530 | /** | 
|  | 531 | * Method to create a new CallerInfoAsyncQueryHandler object, ensuring correct | 
|  | 532 | * state of context and uri. | 
|  | 533 | */ | 
| David Brown | 94202fe | 2011-06-10 16:24:05 -0700 | [diff] [blame] | 534 | private void allocate(Context context, Uri contactRef) { | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | if ((context == null) || (contactRef == null)){ | 
|  | 536 | throw new QueryPoolException("Bad context or query uri."); | 
|  | 537 | } | 
|  | 538 | mHandler = new CallerInfoAsyncQueryHandler(context); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 539 | mHandler.mQueryUri = contactRef; | 
|  | 540 | } | 
|  | 541 |  | 
|  | 542 | /** | 
|  | 543 | * Releases the relevant data. | 
|  | 544 | */ | 
| Artur Satayev | 7c3d157 | 2019-08-13 18:03:58 +0100 | [diff] [blame] | 545 | @UnsupportedAppUsage | 
| David Brown | 94202fe | 2011-06-10 16:24:05 -0700 | [diff] [blame] | 546 | private void release() { | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 547 | mHandler.mContext = null; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 548 | mHandler.mQueryUri = null; | 
|  | 549 | mHandler.mCallerInfo = null; | 
|  | 550 | mHandler = null; | 
|  | 551 | } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 552 |  | 
| Wink Saville | a428807 | 2010-10-12 12:36:38 -0700 | [diff] [blame] | 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 | } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | } |