CallList.java revision 9f22c7496af167f2eada26aa998364d4cf03a619
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; 23import com.google.common.collect.ImmutableList; 24import com.google.common.collect.ImmutableMap; 25 26import com.android.services.telephony.common.Call; 27 28import java.util.HashMap; 29import java.util.List; 30import java.util.Map; 31import java.util.Set; 32 33/** 34 * Maintains the list of active calls received from CallHandlerService and notifies interested 35 * classes of changes to the call list as they are received from the telephony stack. 36 * Primary lister of changes to this class is InCallPresenter. 37 */ 38public class CallList { 39 private static final Map<Integer, String> STATE_MAP = ImmutableMap.<Integer, String>builder() 40 .put(Call.State.ACTIVE, "ACTIVE") 41 .put(Call.State.CALL_WAITING, "CALL_WAITING") 42 .put(Call.State.DIALING, "DIALING") 43 .put(Call.State.IDLE, "IDLE") 44 .put(Call.State.INCOMING, "INCOMING") 45 .put(Call.State.ONHOLD, "ONHOLD") 46 .put(Call.State.INVALID, "INVALID") 47 .build(); 48 49 private static CallList sInstance; 50 51 private final HashMap<Integer, Call> mCallMap = Maps.newHashMap(); 52 private final Set<Listener> mListeners = Sets.newArraySet(); 53 54 /** 55 * Static singleton accessor method. 56 */ 57 public static synchronized CallList getInstance() { 58 if (sInstance == null) { 59 sInstance = new CallList(); 60 } 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 Logger.d(this, "onUpdate - " + safeCallString(call)); 75 76 updateCallInMap(call); 77 notifyListenersOfChange(); 78 } 79 80 /** 81 * Called when multiple calls have changed. 82 */ 83 public void onUpdate(List<Call> callsToUpdate) { 84 Logger.d(this, "onUpdate(...)"); 85 86 Preconditions.checkNotNull(callsToUpdate); 87 for (Call call : callsToUpdate) { 88 Logger.d(this, "\t" + safeCallString(call)); 89 90 updateCallInMap(call); 91 } 92 93 notifyListenersOfChange(); 94 } 95 96 public void addListener(Listener listener) { 97 Preconditions.checkNotNull(listener); 98 99 mListeners.add(listener); 100 101 // Let the listener know about the active calls immediately. 102 listener.onCallListChange(this); 103 } 104 105 public void removeListener(Listener listener) { 106 Preconditions.checkNotNull(listener); 107 mListeners.remove(listener); 108 } 109 110 /** 111 * TODO(klp): Change so that this function is not needed. Instead of assuming there is an active 112 * call, the code should rely on the status of a specific Call and allow the presenters to 113 * update the Call object when the active call changes. 114 */ 115 public Call getIncomingOrActive() { 116 Call retval = getIncomingCall(); 117 if (retval == null) { 118 retval = getActiveCall(); 119 } 120 return retval; 121 } 122 123 public Call getActiveCall() { 124 return getFirstCallWithState(Call.State.ACTIVE); 125 } 126 127 public Call getBackgroundCall() { 128 return getFirstCallWithState(Call.State.ONHOLD); 129 } 130 131 public Call getIncomingCall() { 132 return getFirstCallWithState(Call.State.INCOMING); 133 } 134 135 public boolean existsLiveCall() { 136 for (Call call : mCallMap.values()) { 137 if (!isCallDead(call)) { 138 return true; 139 } 140 } 141 return false; 142 } 143 144 /** 145 * Returns first call found in the call map with the specified state. 146 */ 147 public Call getFirstCallWithState(int state) { 148 Call retval = null; 149 for (Call call : mCallMap.values()) { 150 if (call.getState() == state) { 151 retval = call; 152 break; 153 } 154 } 155 156 Logger.d(this, "Found " + (retval == null ? "no " : "") + "call with state: " + 157 STATE_MAP.get(state)); 158 return retval; 159 } 160 161 /** 162 * Sends a generic notification to all listeners that something has changed. 163 * It is up to the listeners to call back to determine what changed. 164 */ 165 private void notifyListenersOfChange() { 166 for (Listener listener : mListeners) { 167 listener.onCallListChange(this); 168 } 169 } 170 171 private void updateCallInMap(Call call) { 172 Preconditions.checkNotNull(call); 173 174 final Integer id = new Integer(call.getCallId()); 175 176 if (!isCallDead(call)) { 177 mCallMap.put(id, call); 178 } else if (mCallMap.containsKey(id)) { 179 mCallMap.remove(id); 180 } 181 } 182 183 private boolean isCallDead(Call call) { 184 final int state = call.getState(); 185 return Call.State.IDLE == state || Call.State.INVALID == state; 186 } 187 188 /** 189 * Creates a log-safe string for call objects. 190 */ 191 private String safeCallString(Call call) { 192 final StringBuffer buffer = new StringBuffer(); 193 buffer.append("Call (") 194 .append(call.getCallId()) 195 .append("), ") 196 .append(STATE_MAP.get(call.getState())); 197 return buffer.toString(); 198 } 199 200 /** 201 * Listener interface for any class that wants to be notified of changes 202 * to the call list. 203 */ 204 public interface Listener { 205 public void onCallListChange(CallList callList); 206 } 207} 208