CallList.java revision 672b16d0620d033e10fab30ccd70f5dd23e823b7
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17package com.android.incallui; 18 19import com.google.android.collect.Lists; 20import com.google.android.collect.Maps; 21import com.google.android.collect.Sets; 22import com.google.common.base.Preconditions; 23 24import android.os.Handler; 25import android.os.Message; 26 27import com.android.services.telephony.common.Call; 28 29import java.util.ArrayList; 30import java.util.HashMap; 31import java.util.List; 32import java.util.Set; 33 34/** 35 * Maintains the list of active calls received from CallHandlerService and notifies interested 36 * classes of changes to the call list as they are received from the telephony stack. 37 * Primary lister of changes to this class is InCallPresenter. 38 */ 39public class CallList { 40 41 private static final int DISCONNECTED_CALL_SHORT_TIMEOUT_MS = 200; 42 private static final int DISCONNECTED_CALL_MEDIUM_TIMEOUT_MS = 2000; 43 private static final int DISCONNECTED_CALL_LONG_TIMEOUT_MS = 5000; 44 45 private static final int EVENT_DISCONNECTED_TIMEOUT = 1; 46 47 private static CallList sInstance = new CallList(); 48 49 private final HashMap<Integer, Call> mCallMap = Maps.newHashMap(); 50 private final HashMap<Integer, ArrayList<String>> mCallTextReponsesMap = 51 Maps.newHashMap(); 52 private final Set<Listener> mListeners = Sets.newArraySet(); 53 private final HashMap<Integer, List<CallUpdateListener>> mCallUpdateListenerMap = Maps 54 .newHashMap(); 55 56 57 /** 58 * Static singleton accessor method. 59 */ 60 public static CallList getInstance() { 61 return sInstance; 62 } 63 64 /** 65 * Private constructor. Instance should only be acquired through getInstance(). 66 */ 67 private CallList() { 68 } 69 70 /** 71 * Called when a single call has changed. 72 */ 73 public void onUpdate(Call call) { 74 Log.d(this, "onUpdate - ", call); 75 76 updateCallInMap(call); 77 notifyListenersOfChange(); 78 } 79 80 /** 81 * Called when a single call disconnects. 82 */ 83 public void onDisconnect(Call call) { 84 Log.d(this, "onDisconnect: ", call); 85 86 updateCallInMap(call); 87 88 notifyCallUpdateListeners(call); 89 notifyListenersOfChange(); 90 } 91 92 /** 93 * Called when a single call has changed. 94 */ 95 public void onIncoming(Call call, List<String> textMessages) { 96 Log.d(this, "onIncoming - " + call); 97 98 updateCallInMap(call); 99 updateCallTextMap(call, textMessages); 100 101 for (Listener listener : mListeners) { 102 listener.onIncomingCall(call); 103 } 104 } 105 106 /** 107 * Called when multiple calls have changed. 108 */ 109 public void onUpdate(List<Call> callsToUpdate) { 110 Log.d(this, "onUpdate(...)"); 111 112 Preconditions.checkNotNull(callsToUpdate); 113 for (Call call : callsToUpdate) { 114 Log.d(this, "\t" + call); 115 116 updateCallInMap(call); 117 updateCallTextMap(call, null); 118 119 notifyCallUpdateListeners(call); 120 } 121 122 notifyListenersOfChange(); 123 } 124 125 public void notifyCallUpdateListeners(Call call) { 126 final List<CallUpdateListener> listeners = mCallUpdateListenerMap.get(call.getCallId()); 127 if (listeners != null) { 128 for (CallUpdateListener listener : listeners) { 129 listener.onCallStateChanged(call); 130 } 131 } 132 } 133 134 /** 135 * Add a call update listener for a call id. 136 * 137 * @param callId The call id to get updates for. 138 * @param listener The listener to add. 139 */ 140 public void addCallUpdateListener(int callId, CallUpdateListener listener) { 141 List<CallUpdateListener> listeners = mCallUpdateListenerMap.get(callId); 142 if (listeners == null) { 143 listeners = Lists.newArrayList(); 144 mCallUpdateListenerMap.put(callId, listeners); 145 } 146 listeners.add(listener); 147 } 148 149 /** 150 * Remove a call update listener for a call id. 151 * 152 * @param callId The call id to remove the listener for. 153 * @param listener The listener to remove. 154 */ 155 public void removeCallUpdateListener(int callId, CallUpdateListener listener) { 156 List<CallUpdateListener> listeners = mCallUpdateListenerMap.get(callId); 157 if (listeners != null) { 158 listeners.remove(listener); 159 } 160 } 161 162 public void addListener(Listener listener) { 163 Preconditions.checkNotNull(listener); 164 165 mListeners.add(listener); 166 167 // Let the listener know about the active calls immediately. 168 listener.onCallListChange(this); 169 } 170 171 public void removeListener(Listener listener) { 172 Preconditions.checkNotNull(listener); 173 mListeners.remove(listener); 174 } 175 176 /** 177 * TODO: Change so that this function is not needed. Instead of assuming there is an active 178 * call, the code should rely on the status of a specific Call and allow the presenters to 179 * update the Call object when the active call changes. 180 */ 181 public Call getIncomingOrActive() { 182 Call retval = getIncomingCall(); 183 if (retval == null) { 184 retval = getActiveCall(); 185 } 186 return retval; 187 } 188 189 public Call getOutgoingCall() { 190 Call call = getFirstCallWithState(Call.State.DIALING); 191 if (call == null) { 192 call = getFirstCallWithState(Call.State.REDIALING); 193 } 194 return call; 195 } 196 197 public Call getActiveCall() { 198 return getFirstCallWithState(Call.State.ACTIVE); 199 } 200 201 public Call getBackgroundCall() { 202 return getFirstCallWithState(Call.State.ONHOLD); 203 } 204 205 public Call getDisconnectedCall() { 206 return getFirstCallWithState(Call.State.DISCONNECTED); 207 } 208 209 public Call getDisconnectingCall() { 210 return getFirstCallWithState(Call.State.DISCONNECTING); 211 } 212 213 public Call getSecondBackgroundCall() { 214 return getCallWithState(Call.State.ONHOLD, 1); 215 } 216 217 public Call getActiveOrBackgroundCall() { 218 Call call = getActiveCall(); 219 if (call == null) { 220 call = getBackgroundCall(); 221 } 222 return call; 223 } 224 225 public Call getIncomingCall() { 226 Call call = getFirstCallWithState(Call.State.INCOMING); 227 if (call == null) { 228 call = getFirstCallWithState(Call.State.CALL_WAITING); 229 } 230 231 return call; 232 } 233 234 public Call getFirstCall() { 235 // TODO: should we switch to a simple list and pull the first one? 236 Call result = getIncomingCall(); 237 if (result == null) { 238 result = getOutgoingCall(); 239 } 240 if (result == null) { 241 result = getFirstCallWithState(Call.State.ACTIVE); 242 } 243 return result; 244 } 245 246 public Call getCall(int callId) { 247 return mCallMap.get(callId); 248 } 249 250 public boolean existsLiveCall() { 251 for (Call call : mCallMap.values()) { 252 if (!isCallDead(call)) { 253 return true; 254 } 255 } 256 return false; 257 } 258 259 public ArrayList<String> getTextResponses(int callId) { 260 return mCallTextReponsesMap.get(callId); 261 } 262 263 /** 264 * Returns first call found in the call map with the specified state. 265 */ 266 public Call getFirstCallWithState(int state) { 267 return getCallWithState(state, 0); 268 } 269 270 /** 271 * Returns the [position]th call found in the call map with the specified state. 272 * TODO: Improve this logic to sort by call time. 273 */ 274 public Call getCallWithState(int state, int positionToFind) { 275 Call retval = null; 276 int position = 0; 277 for (Call call : mCallMap.values()) { 278 if (call.getState() == state) { 279 if (position >= positionToFind) { 280 retval = call; 281 break; 282 } else { 283 position++; 284 } 285 } 286 } 287 288 return retval; 289 } 290 291 /** 292 * This is called when the service disconnects, either expectedly or unexpectedly. 293 * For the expected case, it's because we have no calls left. For the unexpected case, 294 * it is likely a crash of phone and we need to clean up our calls manually. Without phone, 295 * there can be no active calls, so this is relatively safe thing to do. 296 */ 297 public void clearOnDisconnect() { 298 for (Call call : mCallMap.values()) { 299 final int state = call.getState(); 300 if (state != Call.State.IDLE && 301 state != Call.State.INVALID && 302 state != Call.State.DISCONNECTED) { 303 call.setState(Call.State.DISCONNECTED); 304 updateCallInMap(call); 305 } 306 } 307 notifyListenersOfChange(); 308 } 309 310 /** 311 * Sends a generic notification to all listeners that something has changed. 312 * It is up to the listeners to call back to determine what changed. 313 */ 314 private void notifyListenersOfChange() { 315 for (Listener listener : mListeners) { 316 listener.onCallListChange(this); 317 } 318 } 319 320 private void updateCallInMap(Call call) { 321 Preconditions.checkNotNull(call); 322 323 final Integer id = new Integer(call.getCallId()); 324 325 if (call.getState() == Call.State.DISCONNECTED) { 326 // update existing (but do not add!!) disconnected calls 327 if (mCallMap.containsKey(id)) { 328 329 // For disconnected calls, we want to keep them alive for a few seconds so that the 330 // UI has a chance to display anything it needs when a call is disconnected. 331 332 // Set up a timer to destroy the call after X seconds. 333 final Message msg = mHandler.obtainMessage(EVENT_DISCONNECTED_TIMEOUT, call); 334 mHandler.sendMessageDelayed(msg, getDelayForDisconnect(call)); 335 336 mCallMap.put(id, call); 337 } 338 } else if (!isCallDead(call)) { 339 mCallMap.put(id, call); 340 } else if (mCallMap.containsKey(id)) { 341 mCallMap.remove(id); 342 } 343 } 344 345 private int getDelayForDisconnect(Call call) { 346 Preconditions.checkState(call.getState() == Call.State.DISCONNECTED); 347 348 349 final Call.DisconnectCause cause = call.getDisconnectCause(); 350 final int delay; 351 switch (cause) { 352 case LOCAL: 353 delay = DISCONNECTED_CALL_SHORT_TIMEOUT_MS; 354 break; 355 case NORMAL: 356 delay = DISCONNECTED_CALL_MEDIUM_TIMEOUT_MS; 357 break; 358 case INCOMING_REJECTED: 359 case INCOMING_MISSED: 360 // no delay for missed/rejected incoming calls 361 delay = 0; 362 break; 363 default: 364 delay = DISCONNECTED_CALL_LONG_TIMEOUT_MS; 365 break; 366 } 367 368 return delay; 369 } 370 371 private void updateCallTextMap(Call call, List<String> textResponses) { 372 Preconditions.checkNotNull(call); 373 374 final Integer id = new Integer(call.getCallId()); 375 376 if (!isCallDead(call)) { 377 if (textResponses != null) { 378 mCallTextReponsesMap.put(id, (ArrayList<String>) textResponses); 379 } 380 } else if (mCallMap.containsKey(id)) { 381 mCallTextReponsesMap.remove(id); 382 } 383 } 384 385 private boolean isCallDead(Call call) { 386 final int state = call.getState(); 387 return Call.State.IDLE == state || Call.State.INVALID == state; 388 } 389 390 /** 391 * Sets up a call for deletion and notifies listeners of change. 392 */ 393 private void finishDisconnectedCall(Call call) { 394 call.setState(Call.State.IDLE); 395 updateCallInMap(call); 396 notifyListenersOfChange(); 397 } 398 399 /** 400 * Handles the timeout for destroying disconnected calls. 401 */ 402 private Handler mHandler = new Handler() { 403 @Override 404 public void handleMessage(Message msg) { 405 switch (msg.what) { 406 case EVENT_DISCONNECTED_TIMEOUT: 407 Log.d(this, "EVENT_DISCONNECTED_TIMEOUT ", msg.obj); 408 finishDisconnectedCall((Call) msg.obj); 409 break; 410 default: 411 Log.wtf(this, "Message not expected: " + msg.what); 412 break; 413 } 414 } 415 }; 416 417 /** 418 * Listener interface for any class that wants to be notified of changes 419 * to the call list. 420 */ 421 public interface Listener { 422 public void onCallListChange(CallList callList); 423 public void onIncomingCall(Call call); 424 } 425 426 public interface CallUpdateListener { 427 // TODO: refactor and limit arg to be call state. Caller info is not needed. 428 public void onCallStateChanged(Call call); 429 } 430} 431