| 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 | d2f962a | 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; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.AsyncQueryHandler; | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 21 | import android.content.ContentResolver; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.Context; | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 23 | import android.content.pm.PackageManager.NameNotFoundException; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.database.Cursor; | 
 | 25 | import android.database.SQLException; | 
 | 26 | import android.net.Uri; | 
 | 27 | import android.os.Handler; | 
 | 28 | import android.os.Looper; | 
 | 29 | import android.os.Message; | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 30 | import android.os.SystemClock; | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 31 | import android.os.UserHandle; | 
 | 32 | import android.os.UserManager; | 
| Dmitri Plotnikov | 3c513ed | 2009-08-19 15:56:30 -0700 | [diff] [blame] | 33 | import android.provider.ContactsContract.PhoneLookup; | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 34 | import android.telephony.PhoneNumberUtils; | 
 | 35 | import android.telephony.SubscriptionManager; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.text.TextUtils; | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 37 | import java.util.ArrayList; | 
 | 38 | import java.util.List; | 
 | 39 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | /** | 
| David Brown | 94202fe | 2011-06-10 16:24:05 -0700 | [diff] [blame] | 41 |  * Helper class to make it easier to run asynchronous caller-id lookup queries. | 
 | 42 |  * @see CallerInfo | 
 | 43 |  * | 
 | 44 |  * {@hide} | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 |  */ | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | public class CallerInfoAsyncQuery { | 
| Joe Onorato | 431bb22 | 2010-10-18 19:13:23 -0400 | [diff] [blame] | 47 |     private static final boolean DBG = false; | 
| Nicolas Catania | e224158 | 2009-09-14 19:01:43 -0700 | [diff] [blame] | 48 |     private static final String LOG_TAG = "CallerInfoAsyncQuery"; | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 49 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 |     private static final int EVENT_NEW_QUERY = 1; | 
 | 51 |     private static final int EVENT_ADD_LISTENER = 2; | 
 | 52 |     private static final int EVENT_END_OF_QUEUE = 3; | 
 | 53 |     private static final int EVENT_EMERGENCY_NUMBER = 4; | 
 | 54 |     private static final int EVENT_VOICEMAIL_NUMBER = 5; | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 55 |     private static final int EVENT_GET_GEO_DESCRIPTION = 6; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 |  | 
 | 57 |     private CallerInfoAsyncQueryHandler mHandler; | 
 | 58 |  | 
| David Brown | 2ef46c6 | 2011-06-20 12:53:12 -0700 | [diff] [blame] | 59 |     // If the CallerInfo query finds no contacts, should we use the | 
 | 60 |     // PhoneNumberOfflineGeocoder to look up a "geo description"? | 
 | 61 |     // (TODO: This could become a flag in config.xml if it ever needs to be | 
 | 62 |     // configured on a per-product basis.) | 
| David Brown | cec25c4 | 2011-06-23 14:17:27 -0700 | [diff] [blame] | 63 |     private static final boolean ENABLE_UNKNOWN_NUMBER_GEO_DESCRIPTION = true; | 
| David Brown | 2ef46c6 | 2011-06-20 12:53:12 -0700 | [diff] [blame] | 64 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 |     /** | 
 | 66 |      * Interface for a CallerInfoAsyncQueryHandler result return. | 
 | 67 |      */ | 
 | 68 |     public interface OnQueryCompleteListener { | 
 | 69 |         /** | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 70 |          * Called when the query is complete. | 
 | 71 |          */ | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 |         public void onQueryComplete(int token, Object cookie, CallerInfo ci); | 
 | 73 |     } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 74 |  | 
 | 75 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 |     /** | 
 | 77 |      * Wrap the cookie from the WorkerArgs with additional information needed by our | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 78 |      * classes. | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 |      */ | 
 | 80 |     private static final class CookieWrapper { | 
 | 81 |         public OnQueryCompleteListener listener; | 
 | 82 |         public Object cookie; | 
 | 83 |         public int event; | 
 | 84 |         public String number; | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 85 |         public String geoDescription; | 
| Wink Saville | fb40dd4 | 2014-06-12 17:02:31 -0700 | [diff] [blame] | 86 |  | 
| Wink Saville | 63f03dd | 2014-10-23 10:44:45 -0700 | [diff] [blame] | 87 |         public int subId; | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 88 |     } | 
 | 89 |  | 
 | 90 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 |     /** | 
 | 92 |      * Simple exception used to communicate problems with the query pool. | 
 | 93 |      */ | 
 | 94 |     public static class QueryPoolException extends SQLException { | 
 | 95 |         public QueryPoolException(String error) { | 
 | 96 |             super(error); | 
 | 97 |         } | 
 | 98 |     } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 99 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 |     /** | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 101 |      * @return {@link ContentResolver} for the "current" user. | 
 | 102 |      */ | 
 | 103 |     static ContentResolver getCurrentProfileContentResolver(Context context) { | 
 | 104 |  | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 105 |         if (DBG) Log.d(LOG_TAG, "Trying to get current content resolver..."); | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 106 |  | 
 | 107 |         final int currentUser = ActivityManager.getCurrentUser(); | 
 | 108 |         final int myUser = UserManager.get(context).getUserHandle(); | 
 | 109 |  | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 110 |         if (DBG) Log.d(LOG_TAG, "myUser=" + myUser + "currentUser=" + currentUser); | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 111 |  | 
 | 112 |         if (myUser != currentUser) { | 
 | 113 |             final Context otherContext; | 
 | 114 |             try { | 
 | 115 |                 otherContext = context.createPackageContextAsUser(context.getPackageName(), | 
| Chen Xu | e5ea21a | 2019-08-05 11:39:13 -0700 | [diff] [blame] | 116 |                         /* flags =*/ 0, UserHandle.of(currentUser)); | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 117 |                 return otherContext.getContentResolver(); | 
 | 118 |             } catch (NameNotFoundException e) { | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 119 |                 Log.e(LOG_TAG, e, "Can't find self package"); | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 120 |                 // Fall back to the primary user. | 
 | 121 |             } | 
 | 122 |         } | 
 | 123 |         return context.getContentResolver(); | 
 | 124 |     } | 
 | 125 |  | 
 | 126 |     /** | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 |      * Our own implementation of the AsyncQueryHandler. | 
 | 128 |      */ | 
 | 129 |     private class CallerInfoAsyncQueryHandler extends AsyncQueryHandler { | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 130 |  | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 131 |         /* | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 |          * The information relevant to each CallerInfo query.  Each query may have multiple | 
 | 133 |          * listeners, so each AsyncCursorInfo is associated with 2 or more CookieWrapper | 
 | 134 |          * objects in the queue (one with a new query event, and one with a end event, with | 
 | 135 |          * 0 or more additional listeners in between). | 
 | 136 |          */ | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 137 |  | 
 | 138 |         /** | 
 | 139 |          * Context passed by the caller. | 
 | 140 |          * | 
 | 141 |          * NOTE: The actual context we use for query may *not* be this context; since we query | 
 | 142 |          * against the "current" contacts provider.  In the constructor we pass the "current" | 
 | 143 |          * context resolver (obtained via {@link #getCurrentProfileContentResolver) and pass it | 
 | 144 |          * to the super class. | 
 | 145 |          */ | 
 | 146 |         private Context mContext; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 |         private Uri mQueryUri; | 
 | 148 |         private CallerInfo mCallerInfo; | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 149 |         private List<Runnable> mPendingListenerCallbacks = new ArrayList<>(); | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 150 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 |         /** | 
 | 152 |          * Our own query worker thread. | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 153 |          * | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 |          * This thread handles the messages enqueued in the looper.  The normal sequence | 
 | 155 |          * of events is that a new query shows up in the looper queue, followed by 0 or | 
 | 156 |          * more add listener requests, and then an end request.  Of course, these requests | 
 | 157 |          * can be interlaced with requests from other tokens, but is irrelevant to this | 
 | 158 |          * handler since the handler has no state. | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 159 |          * | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 |          * 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] | 161 |          * 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] | 162 |          * calls and output to this handleMessage call. | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 163 |          * | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 |          * This use of the queue is required because CallerInfo objects may be accessed | 
 | 165 |          * multiple times before the query is complete.  All accesses (listeners) must be | 
 | 166 |          * queued up and informed in order when the query is complete. | 
 | 167 |          */ | 
 | 168 |         protected class CallerInfoWorkerHandler extends WorkerHandler { | 
 | 169 |             public CallerInfoWorkerHandler(Looper looper) { | 
 | 170 |                 super(looper); | 
 | 171 |             } | 
 | 172 |  | 
 | 173 |             @Override | 
 | 174 |             public void handleMessage(Message msg) { | 
 | 175 |                 WorkerArgs args = (WorkerArgs) msg.obj; | 
 | 176 |                 CookieWrapper cw = (CookieWrapper) args.cookie; | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 177 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 |                 if (cw == null) { | 
 | 179 |                     // Normally, this should never be the case for calls originating | 
 | 180 |                     // from within this code. | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 181 |                     // 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] | 182 |                     // super.handleMessage) that DOES place unexpected messages on the | 
 | 183 |                     // queue, then we need pass these messages on. | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 184 |                     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] | 185 |                             " ignored by CallerInfoWorkerHandler, passing onto parent."); | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 186 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 |                     super.handleMessage(msg); | 
 | 188 |                 } else { | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 189 |  | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 190 |                     Log.d(LOG_TAG, "Processing event: " + cw.event + " token (arg1): " + msg.arg1 + | 
| Wink Saville | a428807 | 2010-10-12 12:36:38 -0700 | [diff] [blame] | 191 |                         " command: " + msg.what + " query URI: " + sanitizeUriToString(args.uri)); | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 192 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 |                     switch (cw.event) { | 
 | 194 |                         case EVENT_NEW_QUERY: | 
 | 195 |                             //start the sql command. | 
 | 196 |                             super.handleMessage(msg); | 
 | 197 |                             break; | 
 | 198 |  | 
 | 199 |                         // shortcuts to avoid query for recognized numbers. | 
 | 200 |                         case EVENT_EMERGENCY_NUMBER: | 
 | 201 |                         case EVENT_VOICEMAIL_NUMBER: | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 202 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 |                         case EVENT_ADD_LISTENER: | 
 | 204 |                         case EVENT_END_OF_QUEUE: | 
 | 205 |                             // query was already completed, so just send the reply. | 
 | 206 |                             // passing the original token value back to the caller | 
 | 207 |                             // on top of the event values in arg1. | 
 | 208 |                             Message reply = args.handler.obtainMessage(msg.what); | 
 | 209 |                             reply.obj = args; | 
 | 210 |                             reply.arg1 = msg.arg1; | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 211 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 |                             reply.sendToTarget(); | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 213 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 |                             break; | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 215 |                         case EVENT_GET_GEO_DESCRIPTION: | 
 | 216 |                             handleGeoDescription(msg); | 
 | 217 |                             break; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 |                         default: | 
 | 219 |                     } | 
 | 220 |                 } | 
 | 221 |             } | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 222 |  | 
 | 223 |             private void handleGeoDescription(Message msg) { | 
 | 224 |                 WorkerArgs args = (WorkerArgs) msg.obj; | 
 | 225 |                 CookieWrapper cw = (CookieWrapper) args.cookie; | 
 | 226 |                 if (!TextUtils.isEmpty(cw.number) && cw.cookie != null && mContext != null) { | 
 | 227 |                     final long startTimeMillis = SystemClock.elapsedRealtime(); | 
 | 228 |                     cw.geoDescription = CallerInfo.getGeoDescription(mContext, cw.number); | 
 | 229 |                     final long duration = SystemClock.elapsedRealtime() - startTimeMillis; | 
 | 230 |                     if (duration > 500) { | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 231 |                         if (DBG) Log.d(LOG_TAG, "[handleGeoDescription]" + | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 232 |                                 "Spends long time to retrieve Geo description: " + duration); | 
 | 233 |                     } | 
 | 234 |                 } | 
 | 235 |                 Message reply = args.handler.obtainMessage(msg.what); | 
 | 236 |                 reply.obj = args; | 
 | 237 |                 reply.arg1 = msg.arg1; | 
 | 238 |                 reply.sendToTarget(); | 
 | 239 |             } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 |         } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 241 |  | 
 | 242 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 |         /** | 
 | 244 |          * Asynchronous query handler class for the contact / callerinfo object. | 
 | 245 |          */ | 
 | 246 |         private CallerInfoAsyncQueryHandler(Context context) { | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 247 |             super(getCurrentProfileContentResolver(context)); | 
 | 248 |             mContext = context; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 |         } | 
 | 250 |  | 
 | 251 |         @Override | 
 | 252 |         protected Handler createHandler(Looper looper) { | 
 | 253 |             return new CallerInfoWorkerHandler(looper); | 
 | 254 |         } | 
 | 255 |  | 
 | 256 |         /** | 
 | 257 |          * Overrides onQueryComplete from AsyncQueryHandler. | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 258 |          * | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 |          * This method takes into account the state of this class; we construct the CallerInfo | 
 | 260 |          * 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] | 261 |          * and calls this method, we inform the remaining listeners in the queue, until we're | 
 | 262 |          * out of listeners.  Once we get the message indicating that we should expect no new | 
 | 263 |          * 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] | 264 |          * pool. | 
 | 265 |          */ | 
 | 266 |         @Override | 
 | 267 |         protected void onQueryComplete(int token, Object cookie, Cursor cursor) { | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 268 |             Log.d(LOG_TAG, "##### onQueryComplete() #####   query complete for token: " + token); | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 269 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 |             //get the cookie and notify the listener. | 
 | 271 |             CookieWrapper cw = (CookieWrapper) cookie; | 
 | 272 |             if (cw == null) { | 
 | 273 |                 // Normally, this should never be the case for calls originating | 
 | 274 |                 // from within this code. | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 275 |                 // 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] | 276 |                 // check the parameters to make sure they're viable. | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 277 |                 Log.i(LOG_TAG, "Cookie is null, ignoring onQueryComplete() request."); | 
| Wink Saville | fb40dd4 | 2014-06-12 17:02:31 -0700 | [diff] [blame] | 278 |                 if (cursor != null) { | 
 | 279 |                     cursor.close(); | 
 | 280 |                 } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 |                 return; | 
 | 282 |             } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 283 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 |             if (cw.event == EVENT_END_OF_QUEUE) { | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 285 |                 for (Runnable r : mPendingListenerCallbacks) { | 
 | 286 |                     r.run(); | 
 | 287 |                 } | 
 | 288 |                 mPendingListenerCallbacks.clear(); | 
 | 289 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 |                 release(); | 
| Wink Saville | fb40dd4 | 2014-06-12 17:02:31 -0700 | [diff] [blame] | 291 |                 if (cursor != null) { | 
 | 292 |                     cursor.close(); | 
 | 293 |                 } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 |                 return; | 
 | 295 |             } | 
 | 296 |  | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 297 |             // If the cw.event == EVENT_GET_GEO_DESCRIPTION, means it would not be the 1st | 
 | 298 |             // time entering the onQueryComplete(), mCallerInfo should not be null. | 
 | 299 |             if (cw.event == EVENT_GET_GEO_DESCRIPTION) { | 
 | 300 |                 if (mCallerInfo != null) { | 
 | 301 |                     mCallerInfo.geoDescription = cw.geoDescription; | 
 | 302 |                 } | 
 | 303 |                 // notify that we can clean up the queue after this. | 
 | 304 |                 CookieWrapper endMarker = new CookieWrapper(); | 
 | 305 |                 endMarker.event = EVENT_END_OF_QUEUE; | 
 | 306 |                 startQuery(token, endMarker, null, null, null, null, null); | 
 | 307 |             } | 
 | 308 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 |             // check the token and if needed, create the callerinfo object. | 
 | 310 |             if (mCallerInfo == null) { | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 311 |                 if ((mContext == null) || (mQueryUri == null)) { | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 |                     throw new QueryPoolException | 
 | 313 |                             ("Bad context or query uri, or CallerInfoAsyncQuery already released."); | 
 | 314 |                 } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 315 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 |                 // adjust the callerInfo data as needed, and only if it was set from the | 
 | 317 |                 // initial query request. | 
 | 318 |                 // 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] | 319 |                 // voicemail number, and adjust other data (including photoResource) | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 |                 // accordingly. | 
 | 321 |                 if (cw.event == EVENT_EMERGENCY_NUMBER) { | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 |                     // Note we're setting the phone number here (refer to javadoc | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 323 |                     // comments at the top of CallerInfo class). | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 324 |                     mCallerInfo = new CallerInfo().markAsEmergency(mContext); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 |                 } else if (cw.event == EVENT_VOICEMAIL_NUMBER) { | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 326 |                     mCallerInfo = new CallerInfo().markAsVoiceMail(mContext, cw.subId); | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 327 |                 } else { | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 328 |                     mCallerInfo = CallerInfo.getCallerInfo(mContext, mQueryUri, cursor); | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 329 |                     if (DBG) Log.d(LOG_TAG, "==> Got mCallerInfo: " + mCallerInfo); | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 330 |  | 
| Hung-ying Tyan | 6fe795e | 2010-10-20 11:12:02 +0800 | [diff] [blame] | 331 |                     CallerInfo newCallerInfo = CallerInfo.doSecondaryLookupIfNecessary( | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 332 |                             mContext, cw.number, mCallerInfo); | 
| Hung-ying Tyan | 6fe795e | 2010-10-20 11:12:02 +0800 | [diff] [blame] | 333 |                     if (newCallerInfo != mCallerInfo) { | 
 | 334 |                         mCallerInfo = newCallerInfo; | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 335 |                         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] | 336 |                                 + mCallerInfo); | 
 | 337 |                     } | 
 | 338 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 |                     // Use the number entered by the user for display. | 
 | 340 |                     if (!TextUtils.isEmpty(cw.number)) { | 
| Chen Xu | fba9ca4 | 2019-09-07 18:56:17 -0700 | [diff] [blame] | 341 |                         mCallerInfo.setPhoneNumber(PhoneNumberUtils.formatNumber(cw.number, | 
| David Brown | 94202fe | 2011-06-10 16:24:05 -0700 | [diff] [blame] | 342 |                                 mCallerInfo.normalizedNumber, | 
| Chen Xu | fba9ca4 | 2019-09-07 18:56:17 -0700 | [diff] [blame] | 343 |                                 CallerInfo.getCurrentCountryIso(mContext))); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 |                     } | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 345 |  | 
 | 346 |                     // This condition refer to the google default code for geo. | 
 | 347 |                     // If the number exists in Contacts, the CallCard would never show | 
 | 348 |                     // the geo description, so it would be unnecessary to query it. | 
 | 349 |                     if (ENABLE_UNKNOWN_NUMBER_GEO_DESCRIPTION) { | 
| Chen Xu | fba9ca4 | 2019-09-07 18:56:17 -0700 | [diff] [blame] | 350 |                         if (TextUtils.isEmpty(mCallerInfo.getName())) { | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 351 |                             if (DBG) Log.d(LOG_TAG, "start querying geo description"); | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 352 |                             cw.event = EVENT_GET_GEO_DESCRIPTION; | 
 | 353 |                             startQuery(token, cw, null, null, null, null, null); | 
 | 354 |                             return; | 
 | 355 |                         } | 
 | 356 |                     } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 |                 } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 358 |  | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 359 |                 if (DBG) Log.d(LOG_TAG, "constructing CallerInfo object for token: " + token); | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 360 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 |                 //notify that we can clean up the queue after this. | 
 | 362 |                 CookieWrapper endMarker = new CookieWrapper(); | 
 | 363 |                 endMarker.event = EVENT_END_OF_QUEUE; | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 364 |                 startQuery(token, endMarker, null, null, null, null, null); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 |             } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 366 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 |             //notify the listener that the query is complete. | 
 | 368 |             if (cw.listener != null) { | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 369 |                 mPendingListenerCallbacks.add(new Runnable() { | 
 | 370 |                     @Override | 
 | 371 |                     public void run() { | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 372 |                         if (DBG) Log.d(LOG_TAG, "notifying listener: " | 
| Ceci Wu | 3b90d48 | 2016-08-25 14:48:19 +0800 | [diff] [blame] | 373 |                                 + cw.listener.getClass().toString() + " for token: " + token | 
 | 374 |                                 + mCallerInfo); | 
 | 375 |                         cw.listener.onQueryComplete(token, cw.cookie, mCallerInfo); | 
 | 376 |                     } | 
 | 377 |                 }); | 
| Hall Liu | 4c01910 | 2016-10-05 16:56:17 -0700 | [diff] [blame] | 378 |             } else { | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 379 |                 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] | 380 |             } | 
| Wink Saville | fb40dd4 | 2014-06-12 17:02:31 -0700 | [diff] [blame] | 381 |  | 
 | 382 |             if (cursor != null) { | 
 | 383 |                cursor.close(); | 
 | 384 |             } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 |         } | 
 | 386 |     } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 387 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 |     /** | 
 | 389 |      * Private constructor for factory methods. | 
 | 390 |      */ | 
 | 391 |     private CallerInfoAsyncQuery() { | 
 | 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 |      * Factory method to start query with a Uri query spec | 
 | 397 |      */ | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 398 |     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] | 399 |             OnQueryCompleteListener listener, Object cookie) { | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 400 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 |         CallerInfoAsyncQuery c = new CallerInfoAsyncQuery(); | 
 | 402 |         c.allocate(context, contactRef); | 
 | 403 |  | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 404 |         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] | 405 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 |         //create cookieWrapper, start query | 
 | 407 |         CookieWrapper cw = new CookieWrapper(); | 
 | 408 |         cw.listener = listener; | 
 | 409 |         cw.cookie = cookie; | 
 | 410 |         cw.event = EVENT_NEW_QUERY; | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 411 |  | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 412 |         c.mHandler.startQuery(token, cw, contactRef, null, null, null, null); | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 413 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 |         return c; | 
 | 415 |     } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 416 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 |     /** | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 418 |      * Factory method to start the query based on a number. | 
 | 419 |      * | 
 | 420 |      * Note: if the number contains an "@" character we treat it | 
 | 421 |      * as a SIP address, and look it up directly in the Data table | 
 | 422 |      * rather than using the PhoneLookup table. | 
 | 423 |      * TODO: But eventually we should expose two separate methods, one for | 
 | 424 |      * numbers and one for SIP addresses, and then have | 
 | 425 |      * PhoneUtils.startGetCallerInfo() decide which one to call based on | 
 | 426 |      * the phone type of the incoming connection. | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 |      */ | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 428 |     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] | 429 |             OnQueryCompleteListener listener, Object cookie) { | 
| Wink Saville | fb40dd4 | 2014-06-12 17:02:31 -0700 | [diff] [blame] | 430 |  | 
| Shishir Agrawal | 7ea3e8b | 2016-01-25 13:03:07 -0800 | [diff] [blame] | 431 |         int subId = SubscriptionManager.getDefaultSubscriptionId(); | 
| Wink Saville | fb40dd4 | 2014-06-12 17:02:31 -0700 | [diff] [blame] | 432 |         return startQuery(token, context, number, listener, cookie, subId); | 
 | 433 |     } | 
 | 434 |  | 
 | 435 |     /** | 
 | 436 |      * Factory method to start the query based on a number with specific subscription. | 
 | 437 |      * | 
 | 438 |      * Note: if the number contains an "@" character we treat it | 
 | 439 |      * as a SIP address, and look it up directly in the Data table | 
 | 440 |      * rather than using the PhoneLookup table. | 
 | 441 |      * TODO: But eventually we should expose two separate methods, one for | 
 | 442 |      * numbers and one for SIP addresses, and then have | 
 | 443 |      * PhoneUtils.startGetCallerInfo() decide which one to call based on | 
 | 444 |      * the phone type of the incoming connection. | 
 | 445 |      */ | 
 | 446 |     public static CallerInfoAsyncQuery startQuery(int token, Context context, String number, | 
| Wink Saville | 63f03dd | 2014-10-23 10:44:45 -0700 | [diff] [blame] | 447 |             OnQueryCompleteListener listener, Object cookie, int subId) { | 
| Wink Saville | fb40dd4 | 2014-06-12 17:02:31 -0700 | [diff] [blame] | 448 |  | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 449 |         if (DBG) { | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 450 |             Log.d(LOG_TAG, "##### CallerInfoAsyncQuery startQuery()... #####"); | 
 | 451 |             Log.d(LOG_TAG, "- number: " + /*number*/ "xxxxxxx"); | 
 | 452 |             Log.d(LOG_TAG, "- cookie: " + cookie); | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 453 |         } | 
 | 454 |  | 
 | 455 |         // Construct the URI object and query params, and start the query. | 
 | 456 |  | 
| Makoto Onuki | a2295e6 | 2014-07-10 15:32:16 -0700 | [diff] [blame] | 457 |         final Uri contactRef = PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI.buildUpon() | 
 | 458 |                 .appendPath(number) | 
 | 459 |                 .appendQueryParameter(PhoneLookup.QUERY_PARAMETER_SIP_ADDRESS, | 
 | 460 |                         String.valueOf(PhoneNumberUtils.isUriNumber(number))) | 
 | 461 |                 .build(); | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 462 |  | 
 | 463 |         if (DBG) { | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 464 |             Log.d(LOG_TAG, "==> contactRef: " + sanitizeUriToString(contactRef)); | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 465 |         } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 466 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 |         CallerInfoAsyncQuery c = new CallerInfoAsyncQuery(); | 
 | 468 |         c.allocate(context, contactRef); | 
 | 469 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 |         //create cookieWrapper, start query | 
 | 471 |         CookieWrapper cw = new CookieWrapper(); | 
 | 472 |         cw.listener = listener; | 
 | 473 |         cw.cookie = cookie; | 
 | 474 |         cw.number = number; | 
| Wink Saville | fb40dd4 | 2014-06-12 17:02:31 -0700 | [diff] [blame] | 475 |         cw.subId = subId; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 |  | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 477 |         // check to see if these are recognized numbers, and use shortcuts if we can. | 
| Yorke Lee | 282129f | 2014-06-05 08:41:47 -0700 | [diff] [blame] | 478 |         if (PhoneNumberUtils.isLocalEmergencyNumber(context, number)) { | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 |             cw.event = EVENT_EMERGENCY_NUMBER; | 
| Jordan Liu | 360d567 | 2016-11-09 13:23:42 -0800 | [diff] [blame] | 480 |         } else if (PhoneNumberUtils.isVoiceMailNumber(context, subId, number)) { | 
| Nicolas Catania | 60d45f0 | 2009-09-15 18:32:02 -0700 | [diff] [blame] | 481 |             cw.event = EVENT_VOICEMAIL_NUMBER; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 |         } else { | 
| Nicolas Catania | 60d45f0 | 2009-09-15 18:32:02 -0700 | [diff] [blame] | 483 |             cw.event = EVENT_NEW_QUERY; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 |         } | 
 | 485 |  | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 486 |         c.mHandler.startQuery(token, | 
 | 487 |                               cw,  // cookie | 
 | 488 |                               contactRef,  // uri | 
 | 489 |                               null,  // projection | 
| Makoto Onuki | a2295e6 | 2014-07-10 15:32:16 -0700 | [diff] [blame] | 490 |                               null,  // selection | 
 | 491 |                               null,  // selectionArgs | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 492 |                               null);  // orderBy | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 |         return c; | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 494 |     } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 495 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 |     /** | 
 | 497 |      * Method to add listeners to a currently running query | 
 | 498 |      */ | 
 | 499 |     public void addQueryListener(int token, OnQueryCompleteListener listener, Object cookie) { | 
 | 500 |  | 
| Hall Liu | d2f962a | 2019-10-31 15:17:58 -0700 | [diff] [blame] | 501 |         if (DBG) Log.d(LOG_TAG, "adding listener to query: " | 
 | 502 |                 + sanitizeUriToString(mHandler.mQueryUri) + " handler: " + mHandler.toString()); | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 503 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 |         //create cookieWrapper, add query request to end of queue. | 
 | 505 |         CookieWrapper cw = new CookieWrapper(); | 
 | 506 |         cw.listener = listener; | 
 | 507 |         cw.cookie = cookie; | 
 | 508 |         cw.event = EVENT_ADD_LISTENER; | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 509 |  | 
| David Brown | 158d390 | 2010-09-27 16:29:14 -0700 | [diff] [blame] | 510 |         mHandler.startQuery(token, cw, null, null, null, null, null); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 |     } | 
 | 512 |  | 
 | 513 |     /** | 
 | 514 |      * Method to create a new CallerInfoAsyncQueryHandler object, ensuring correct | 
 | 515 |      * state of context and uri. | 
 | 516 |      */ | 
| David Brown | 94202fe | 2011-06-10 16:24:05 -0700 | [diff] [blame] | 517 |     private void allocate(Context context, Uri contactRef) { | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 |         if ((context == null) || (contactRef == null)){ | 
 | 519 |             throw new QueryPoolException("Bad context or query uri."); | 
 | 520 |         } | 
 | 521 |         mHandler = new CallerInfoAsyncQueryHandler(context); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 |         mHandler.mQueryUri = contactRef; | 
 | 523 |     } | 
 | 524 |  | 
 | 525 |     /** | 
 | 526 |      * Releases the relevant data. | 
 | 527 |      */ | 
| David Brown | 94202fe | 2011-06-10 16:24:05 -0700 | [diff] [blame] | 528 |     private void release() { | 
| Makoto Onuki | 5692dcc | 2014-07-17 14:57:04 -0700 | [diff] [blame] | 529 |         mHandler.mContext = null; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 530 |         mHandler.mQueryUri = null; | 
 | 531 |         mHandler.mCallerInfo = null; | 
 | 532 |         mHandler = null; | 
 | 533 |     } | 
| Wink Saville | 2563a3a | 2009-06-09 10:30:03 -0700 | [diff] [blame] | 534 |  | 
| Wink Saville | a428807 | 2010-10-12 12:36:38 -0700 | [diff] [blame] | 535 |     private static String sanitizeUriToString(Uri uri) { | 
 | 536 |         if (uri != null) { | 
 | 537 |             String uriString = uri.toString(); | 
 | 538 |             int indexOfLastSlash = uriString.lastIndexOf('/'); | 
 | 539 |             if (indexOfLastSlash > 0) { | 
 | 540 |                 return uriString.substring(0, indexOfLastSlash) + "/xxxxxxx"; | 
 | 541 |             } else { | 
 | 542 |                 return uriString; | 
 | 543 |             } | 
 | 544 |         } else { | 
 | 545 |             return ""; | 
 | 546 |         } | 
 | 547 |     } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 548 | } |