blob: 517a5a9a571072db38746acf8a6e3708ecc6a155 [file] [log] [blame]
Santos Cordon63a84242013-07-23 13:32:52 -07001/*
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.phone;
18
19import com.google.android.collect.Lists;
20import com.google.android.collect.Maps;
21import com.google.common.base.Preconditions;
22
23import android.os.AsyncResult;
24import android.os.Handler;
25import android.os.Message;
26import android.util.Log;
27
Santos Cordona3d05142013-07-29 11:25:17 -070028import com.android.internal.telephony.CallManager;
Santos Cordon63a84242013-07-23 13:32:52 -070029import com.android.internal.telephony.Connection;
Santos Cordona3d05142013-07-29 11:25:17 -070030import com.android.internal.telephony.PhoneConstants;
Santos Cordon995c8162013-07-29 09:22:22 -070031import com.android.services.telephony.common.Call;
Santos Cordona3d05142013-07-29 11:25:17 -070032import com.android.services.telephony.common.Call.State;
Santos Cordon63a84242013-07-23 13:32:52 -070033
34import java.util.ArrayList;
35import java.util.HashMap;
36import java.util.List;
Santos Cordon249efd02013-08-05 03:33:56 -070037import java.util.Map.Entry;
Santos Cordon63a84242013-07-23 13:32:52 -070038import java.util.concurrent.atomic.AtomicInteger;
39
40/**
41 * Creates a Call model from Call state and data received from the telephony
42 * layer. The telephony layer maintains 3 conceptual objects: Phone, Call,
43 * Connection.
44 *
45 * Phone represents the radio and there is an implementation per technology
46 * type such as GSMPhone, SipPhone, CDMAPhone, etc. Generally, we will only ever
47 * deal with one instance of this object for the lifetime of this class.
48 *
49 * There are 3 Call instances that exist for the lifetime of this class which
50 * are created by CallTracker. The three are RingingCall, ForegroundCall, and
51 * BackgroundCall.
52 *
53 * A Connection most closely resembles what the layperson would consider a call.
54 * A Connection is created when a user dials and it is "owned" by one of the
55 * three Call instances. Which of the three Calls owns the Connection changes
56 * as the Connection goes between ACTIVE, HOLD, RINGING, and other states.
57 *
58 * This class models a new Call class from Connection objects received from
59 * the telephony layer. We use Connection references as identifiers for a call;
60 * new reference = new call.
61 *
62 * TODO(klp): Create a new Call class to replace the simple call Id ints
63 * being used currently.
64 *
65 * The new Call models are parcellable for transfer via the CallHandlerService
66 * API.
67 */
68public class CallModeler extends Handler {
69
70 private static final String TAG = CallModeler.class.getSimpleName();
71
72 private static final int CALL_ID_START_VALUE = 1;
Santos Cordon63a84242013-07-23 13:32:52 -070073
Santos Cordon998f42b2013-08-02 16:13:12 -070074 private final CallStateMonitor mCallStateMonitor;
75 private final CallManager mCallManager;
76 private final HashMap<Connection, Call> mCallMap = Maps.newHashMap();
77 private final AtomicInteger mNextCallId = new AtomicInteger(CALL_ID_START_VALUE);
Christine Chendaf7bf62013-08-05 19:12:31 -070078 private final ArrayList<Listener> mListeners = new ArrayList<Listener>();
Santos Cordon63a84242013-07-23 13:32:52 -070079
Santos Cordona3d05142013-07-29 11:25:17 -070080 public CallModeler(CallStateMonitor callStateMonitor, CallManager callManager) {
Santos Cordon63a84242013-07-23 13:32:52 -070081 mCallStateMonitor = callStateMonitor;
Santos Cordona3d05142013-07-29 11:25:17 -070082 mCallManager = callManager;
Santos Cordon63a84242013-07-23 13:32:52 -070083
84 mCallStateMonitor.addListener(this);
85 }
86
87 @Override
88 public void handleMessage(Message msg) {
89 switch(msg.what) {
90 case CallStateMonitor.PHONE_NEW_RINGING_CONNECTION:
91 onNewRingingConnection((AsyncResult) msg.obj);
92 break;
93 case CallStateMonitor.PHONE_DISCONNECT:
94 onDisconnect((AsyncResult) msg.obj);
Santos Cordon995c8162013-07-29 09:22:22 -070095 break;
96 case CallStateMonitor.PHONE_STATE_CHANGED:
97 onPhoneStateChanged((AsyncResult) msg.obj);
98 break;
Santos Cordon63a84242013-07-23 13:32:52 -070099 default:
100 break;
101 }
102 }
103
Christine Chendaf7bf62013-08-05 19:12:31 -0700104 public void addListener(Listener listener) {
Santos Cordon63a84242013-07-23 13:32:52 -0700105 Preconditions.checkNotNull(listener);
Christine Chendaf7bf62013-08-05 19:12:31 -0700106 Preconditions.checkNotNull(mListeners);
Christine Chen4748abd2013-08-07 15:44:15 -0700107 if (!mListeners.contains(listener)) {
108 mListeners.add(listener);
109 }
Santos Cordon998f42b2013-08-02 16:13:12 -0700110 }
111
112 public List<Call> getFullList() {
113 final List<Call> retval = Lists.newArrayList();
114 doUpdate(true, retval);
115 return retval;
Santos Cordon63a84242013-07-23 13:32:52 -0700116 }
117
Santos Cordon249efd02013-08-05 03:33:56 -0700118 public CallResult getCallWithId(int callId) {
119 // max 8 connections, so this should be fast even through we are traversing the entire map.
120 for (Entry<Connection, Call> entry : mCallMap.entrySet()) {
121 if (entry.getValue().getCallId() == callId) {
122 return new CallResult(entry.getValue(), entry.getKey());
123 }
124 }
125 return null;
126 }
127
Santos Cordon2eaff902013-08-05 04:37:55 -0700128 public boolean hasOutstandingActiveCall() {
129 for (Call call : mCallMap.values()) {
130 int state = call.getState();
131 if (Call.State.INVALID != state &&
132 Call.State.IDLE != state &&
133 Call.State.INCOMING != state) {
134 return true;
135 }
136 }
137
138 return false;
139 }
140
Santos Cordon63a84242013-07-23 13:32:52 -0700141 private void onNewRingingConnection(AsyncResult r) {
142 final Connection conn = (Connection) r.result;
Santos Cordon995c8162013-07-29 09:22:22 -0700143 final Call call = getCallFromConnection(conn, true);
Santos Cordona3d05142013-07-29 11:25:17 -0700144 call.setState(Call.State.INCOMING);
Santos Cordon63a84242013-07-23 13:32:52 -0700145
Christine Chendaf7bf62013-08-05 19:12:31 -0700146 for (int i = 0; i < mListeners.size(); ++i) {
147 mListeners.get(i).onUpdate(Lists.newArrayList(call), false);
Santos Cordon63a84242013-07-23 13:32:52 -0700148 }
149 }
150
151 private void onDisconnect(AsyncResult r) {
152 final Connection conn = (Connection) r.result;
Santos Cordon995c8162013-07-29 09:22:22 -0700153 final Call call = getCallFromConnection(conn, false);
Santos Cordon998f42b2013-08-02 16:13:12 -0700154 call.setState(Call.State.IDLE);
Santos Cordon63a84242013-07-23 13:32:52 -0700155
Santos Cordon995c8162013-07-29 09:22:22 -0700156 if (call != null) {
157 mCallMap.remove(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700158
Christine Chendaf7bf62013-08-05 19:12:31 -0700159 for (int i = 0; i < mListeners.size(); ++i) {
160 mListeners.get(i).onDisconnect(call);
Santos Cordon63a84242013-07-23 13:32:52 -0700161 }
162 }
163 }
164
Santos Cordona3d05142013-07-29 11:25:17 -0700165 /**
166 * Called when the phone state changes.
Santos Cordona3d05142013-07-29 11:25:17 -0700167 */
Santos Cordon995c8162013-07-29 09:22:22 -0700168 private void onPhoneStateChanged(AsyncResult r) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700169 final List<Call> updatedCalls = Lists.newArrayList();
170 doUpdate(false, updatedCalls);
171
Christine Chendaf7bf62013-08-05 19:12:31 -0700172 for (int i = 0; i < mListeners.size(); ++i) {
173 mListeners.get(i).onUpdate(updatedCalls, false);
Santos Cordon998f42b2013-08-02 16:13:12 -0700174 }
175 }
176
177
178 /**
179 * Go through the Calls from CallManager and return the list of calls that were updated.
180 * Or, the full list if requested.
181 */
182 private void doUpdate(boolean fullUpdate, List<Call> out) {
Santos Cordona3d05142013-07-29 11:25:17 -0700183 final List<com.android.internal.telephony.Call> telephonyCalls = Lists.newArrayList();
184 telephonyCalls.addAll(mCallManager.getRingingCalls());
185 telephonyCalls.addAll(mCallManager.getForegroundCalls());
186 telephonyCalls.addAll(mCallManager.getBackgroundCalls());
187
Santos Cordona3d05142013-07-29 11:25:17 -0700188 // Cycle through all the Connections on all the Calls. Update our Call objects
189 // to reflect any new state and send the updated Call objects to the handler service.
190 for (com.android.internal.telephony.Call telephonyCall : telephonyCalls) {
191 final int state = translateStateFromTelephony(telephonyCall.getState());
192
193 for (Connection connection : telephonyCall.getConnections()) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700194 // new connections return a Call with INVALID state, which does not translate to
195 // a state in the Connection object. This ensures that staleness check below
196 // fails and we always add the item to the update list if it is new.
Santos Cordona3d05142013-07-29 11:25:17 -0700197 final Call call = getCallFromConnection(connection, true);
198
Santos Cordon998f42b2013-08-02 16:13:12 -0700199 if (fullUpdate || call.getState() != state) {
Santos Cordona3d05142013-07-29 11:25:17 -0700200 call.setState(state);
Santos Cordon998f42b2013-08-02 16:13:12 -0700201 out.add(call);
Santos Cordona3d05142013-07-29 11:25:17 -0700202 }
203 }
204 }
Santos Cordona3d05142013-07-29 11:25:17 -0700205 }
206
207 private int translateStateFromTelephony(com.android.internal.telephony.Call.State teleState) {
208 int retval = State.IDLE;
209 switch (teleState) {
210 case ACTIVE:
211 retval = State.ACTIVE;
212 break;
213 case INCOMING:
214 retval = State.INCOMING;
215 break;
216 case DIALING:
217 case ALERTING:
218 retval = State.DIALING;
219 break;
220 case WAITING:
221 retval = State.CALL_WAITING;
222 break;
223 case HOLDING:
224 retval = State.ONHOLD;
225 break;
226 default:
227 }
228
229 return retval;
Santos Cordon995c8162013-07-29 09:22:22 -0700230 }
231
Santos Cordon63a84242013-07-23 13:32:52 -0700232 /**
233 * Gets an existing callId for a connection, or creates one
234 * if none exists.
235 */
Santos Cordon995c8162013-07-29 09:22:22 -0700236 private Call getCallFromConnection(Connection conn, boolean createIfMissing) {
237 Call call = null;
Santos Cordon63a84242013-07-23 13:32:52 -0700238
239 // Find the call id or create if missing and requested.
240 if (conn != null) {
Santos Cordon995c8162013-07-29 09:22:22 -0700241 if (mCallMap.containsKey(conn)) {
242 call = mCallMap.get(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700243 } else if (createIfMissing) {
Santos Cordon995c8162013-07-29 09:22:22 -0700244 int callId;
Santos Cordon63a84242013-07-23 13:32:52 -0700245 int newNextCallId;
246 do {
247 callId = mNextCallId.get();
248
249 // protect against overflow
250 newNextCallId = (callId == Integer.MAX_VALUE ?
251 CALL_ID_START_VALUE : callId + 1);
252
253 // Keep looping if the change was not atomic OR the value is already taken.
254 // The call to containsValue() is linear, however, most devices support a
255 // maximum of 7 connections so it's not expensive.
256 } while (!mNextCallId.compareAndSet(callId, newNextCallId) ||
Santos Cordon995c8162013-07-29 09:22:22 -0700257 mCallMap.containsValue(callId));
Santos Cordon63a84242013-07-23 13:32:52 -0700258
Santos Cordon995c8162013-07-29 09:22:22 -0700259 call = new Call(callId);
Santos Cordon179907f2013-07-31 09:40:55 -0700260 call.setNumber(conn.getAddress());
Santos Cordon995c8162013-07-29 09:22:22 -0700261 mCallMap.put(conn, call);
Santos Cordon63a84242013-07-23 13:32:52 -0700262 }
263 }
Santos Cordon995c8162013-07-29 09:22:22 -0700264 return call;
Santos Cordon63a84242013-07-23 13:32:52 -0700265 }
266
267 /**
268 * Listener interface for changes to Calls.
269 */
270 public interface Listener {
Santos Cordon995c8162013-07-29 09:22:22 -0700271 void onDisconnect(Call call);
Santos Cordon998f42b2013-08-02 16:13:12 -0700272 void onUpdate(List<Call> calls, boolean fullUpdate);
Santos Cordon63a84242013-07-23 13:32:52 -0700273 }
Santos Cordon249efd02013-08-05 03:33:56 -0700274
275 /**
276 * Result class for accessing a call by connection.
277 */
278 public static class CallResult {
279 public Call mCall;
280 public Connection mConnection;
281
282 private CallResult(Call call, Connection connection) {
283 mCall = call;
284 mConnection = connection;
285 }
286
287 public Call getCall() {
288 return mCall;
289 }
290
291 public Connection getConnection() {
292 return mConnection;
293 }
294 }
Santos Cordon63a84242013-07-23 13:32:52 -0700295}