blob: 8aed6e5a66fedce34ed7bc247cb2dfe884795a6d [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);
78 private Listener mListener;
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
Santos Cordon998f42b2013-08-02 16:13:12 -0700104 public void setListener(Listener listener) {
Santos Cordon63a84242013-07-23 13:32:52 -0700105 Preconditions.checkNotNull(listener);
Santos Cordon998f42b2013-08-02 16:13:12 -0700106 Preconditions.checkState(mListener == null);
Santos Cordon63a84242013-07-23 13:32:52 -0700107
Santos Cordon998f42b2013-08-02 16:13:12 -0700108 // only support setting listener once.
109 // We only have one listener anyway and supporting multiple means maintaining state for
110 // each of the listeners so that we can do proper diffs.
111 mListener = listener;
112 }
113
114 public List<Call> getFullList() {
115 final List<Call> retval = Lists.newArrayList();
116 doUpdate(true, retval);
117 return retval;
Santos Cordon63a84242013-07-23 13:32:52 -0700118 }
119
Santos Cordon249efd02013-08-05 03:33:56 -0700120 public CallResult getCallWithId(int callId) {
121 // max 8 connections, so this should be fast even through we are traversing the entire map.
122 for (Entry<Connection, Call> entry : mCallMap.entrySet()) {
123 if (entry.getValue().getCallId() == callId) {
124 return new CallResult(entry.getValue(), entry.getKey());
125 }
126 }
127 return null;
128 }
129
Santos Cordon2eaff902013-08-05 04:37:55 -0700130 public boolean hasOutstandingActiveCall() {
131 for (Call call : mCallMap.values()) {
132 int state = call.getState();
133 if (Call.State.INVALID != state &&
134 Call.State.IDLE != state &&
135 Call.State.INCOMING != state) {
136 return true;
137 }
138 }
139
140 return false;
141 }
142
Santos Cordon63a84242013-07-23 13:32:52 -0700143 private void onNewRingingConnection(AsyncResult r) {
144 final Connection conn = (Connection) r.result;
Santos Cordon995c8162013-07-29 09:22:22 -0700145 final Call call = getCallFromConnection(conn, true);
Santos Cordona3d05142013-07-29 11:25:17 -0700146 call.setState(Call.State.INCOMING);
Santos Cordon63a84242013-07-23 13:32:52 -0700147
Santos Cordon998f42b2013-08-02 16:13:12 -0700148 if (call != null && mListener != null) {
149 mListener.onUpdate(Lists.newArrayList(call), false);
Santos Cordon63a84242013-07-23 13:32:52 -0700150 }
151 }
152
153 private void onDisconnect(AsyncResult r) {
154 final Connection conn = (Connection) r.result;
Santos Cordon995c8162013-07-29 09:22:22 -0700155 final Call call = getCallFromConnection(conn, false);
Santos Cordon998f42b2013-08-02 16:13:12 -0700156 call.setState(Call.State.IDLE);
Santos Cordon63a84242013-07-23 13:32:52 -0700157
Santos Cordon995c8162013-07-29 09:22:22 -0700158 if (call != null) {
159 mCallMap.remove(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700160
Santos Cordon998f42b2013-08-02 16:13:12 -0700161 if (mListener != null) {
162 mListener.onDisconnect(call);
Santos Cordon63a84242013-07-23 13:32:52 -0700163 }
164 }
165 }
166
Santos Cordona3d05142013-07-29 11:25:17 -0700167 /**
168 * Called when the phone state changes.
Santos Cordona3d05142013-07-29 11:25:17 -0700169 */
Santos Cordon995c8162013-07-29 09:22:22 -0700170 private void onPhoneStateChanged(AsyncResult r) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700171 final List<Call> updatedCalls = Lists.newArrayList();
172 doUpdate(false, updatedCalls);
173
174 if (mListener != null) {
175 mListener.onUpdate(updatedCalls, false);
176 }
177 }
178
179
180 /**
181 * Go through the Calls from CallManager and return the list of calls that were updated.
182 * Or, the full list if requested.
183 */
184 private void doUpdate(boolean fullUpdate, List<Call> out) {
Santos Cordona3d05142013-07-29 11:25:17 -0700185 final List<com.android.internal.telephony.Call> telephonyCalls = Lists.newArrayList();
186 telephonyCalls.addAll(mCallManager.getRingingCalls());
187 telephonyCalls.addAll(mCallManager.getForegroundCalls());
188 telephonyCalls.addAll(mCallManager.getBackgroundCalls());
189
Santos Cordona3d05142013-07-29 11:25:17 -0700190 // Cycle through all the Connections on all the Calls. Update our Call objects
191 // to reflect any new state and send the updated Call objects to the handler service.
192 for (com.android.internal.telephony.Call telephonyCall : telephonyCalls) {
193 final int state = translateStateFromTelephony(telephonyCall.getState());
194
195 for (Connection connection : telephonyCall.getConnections()) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700196 // new connections return a Call with INVALID state, which does not translate to
197 // a state in the Connection object. This ensures that staleness check below
198 // fails and we always add the item to the update list if it is new.
Santos Cordona3d05142013-07-29 11:25:17 -0700199 final Call call = getCallFromConnection(connection, true);
200
Santos Cordon998f42b2013-08-02 16:13:12 -0700201 if (fullUpdate || call.getState() != state) {
Santos Cordona3d05142013-07-29 11:25:17 -0700202 call.setState(state);
Santos Cordon998f42b2013-08-02 16:13:12 -0700203 out.add(call);
Santos Cordona3d05142013-07-29 11:25:17 -0700204 }
205 }
206 }
Santos Cordona3d05142013-07-29 11:25:17 -0700207 }
208
209 private int translateStateFromTelephony(com.android.internal.telephony.Call.State teleState) {
210 int retval = State.IDLE;
211 switch (teleState) {
212 case ACTIVE:
213 retval = State.ACTIVE;
214 break;
215 case INCOMING:
216 retval = State.INCOMING;
217 break;
218 case DIALING:
219 case ALERTING:
220 retval = State.DIALING;
221 break;
222 case WAITING:
223 retval = State.CALL_WAITING;
224 break;
225 case HOLDING:
226 retval = State.ONHOLD;
227 break;
228 default:
229 }
230
231 return retval;
Santos Cordon995c8162013-07-29 09:22:22 -0700232 }
233
Santos Cordon63a84242013-07-23 13:32:52 -0700234 /**
235 * Gets an existing callId for a connection, or creates one
236 * if none exists.
237 */
Santos Cordon995c8162013-07-29 09:22:22 -0700238 private Call getCallFromConnection(Connection conn, boolean createIfMissing) {
239 Call call = null;
Santos Cordon63a84242013-07-23 13:32:52 -0700240
241 // Find the call id or create if missing and requested.
242 if (conn != null) {
Santos Cordon995c8162013-07-29 09:22:22 -0700243 if (mCallMap.containsKey(conn)) {
244 call = mCallMap.get(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700245 } else if (createIfMissing) {
Santos Cordon995c8162013-07-29 09:22:22 -0700246 int callId;
Santos Cordon63a84242013-07-23 13:32:52 -0700247 int newNextCallId;
248 do {
249 callId = mNextCallId.get();
250
251 // protect against overflow
252 newNextCallId = (callId == Integer.MAX_VALUE ?
253 CALL_ID_START_VALUE : callId + 1);
254
255 // Keep looping if the change was not atomic OR the value is already taken.
256 // The call to containsValue() is linear, however, most devices support a
257 // maximum of 7 connections so it's not expensive.
258 } while (!mNextCallId.compareAndSet(callId, newNextCallId) ||
Santos Cordon995c8162013-07-29 09:22:22 -0700259 mCallMap.containsValue(callId));
Santos Cordon63a84242013-07-23 13:32:52 -0700260
Santos Cordon995c8162013-07-29 09:22:22 -0700261 call = new Call(callId);
Santos Cordon179907f2013-07-31 09:40:55 -0700262 call.setNumber(conn.getAddress());
Santos Cordon995c8162013-07-29 09:22:22 -0700263 mCallMap.put(conn, call);
Santos Cordon63a84242013-07-23 13:32:52 -0700264 }
265 }
Santos Cordon995c8162013-07-29 09:22:22 -0700266 return call;
Santos Cordon63a84242013-07-23 13:32:52 -0700267 }
268
269 /**
270 * Listener interface for changes to Calls.
271 */
272 public interface Listener {
Santos Cordon995c8162013-07-29 09:22:22 -0700273 void onDisconnect(Call call);
Santos Cordon998f42b2013-08-02 16:13:12 -0700274 void onUpdate(List<Call> calls, boolean fullUpdate);
Santos Cordon63a84242013-07-23 13:32:52 -0700275 }
Santos Cordon249efd02013-08-05 03:33:56 -0700276
277 /**
278 * Result class for accessing a call by connection.
279 */
280 public static class CallResult {
281 public Call mCall;
282 public Connection mConnection;
283
284 private CallResult(Call call, Connection connection) {
285 mCall = call;
286 mConnection = connection;
287 }
288
289 public Call getCall() {
290 return mCall;
291 }
292
293 public Connection getConnection() {
294 return mConnection;
295 }
296 }
Santos Cordon63a84242013-07-23 13:32:52 -0700297}