blob: ee96dbf619688e4d9a1d8b17ecfcdf53648ae780 [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;
37import java.util.concurrent.atomic.AtomicInteger;
38
39/**
40 * Creates a Call model from Call state and data received from the telephony
41 * layer. The telephony layer maintains 3 conceptual objects: Phone, Call,
42 * Connection.
43 *
44 * Phone represents the radio and there is an implementation per technology
45 * type such as GSMPhone, SipPhone, CDMAPhone, etc. Generally, we will only ever
46 * deal with one instance of this object for the lifetime of this class.
47 *
48 * There are 3 Call instances that exist for the lifetime of this class which
49 * are created by CallTracker. The three are RingingCall, ForegroundCall, and
50 * BackgroundCall.
51 *
52 * A Connection most closely resembles what the layperson would consider a call.
53 * A Connection is created when a user dials and it is "owned" by one of the
54 * three Call instances. Which of the three Calls owns the Connection changes
55 * as the Connection goes between ACTIVE, HOLD, RINGING, and other states.
56 *
57 * This class models a new Call class from Connection objects received from
58 * the telephony layer. We use Connection references as identifiers for a call;
59 * new reference = new call.
60 *
61 * TODO(klp): Create a new Call class to replace the simple call Id ints
62 * being used currently.
63 *
64 * The new Call models are parcellable for transfer via the CallHandlerService
65 * API.
66 */
67public class CallModeler extends Handler {
68
69 private static final String TAG = CallModeler.class.getSimpleName();
70
71 private static final int CALL_ID_START_VALUE = 1;
Santos Cordon63a84242013-07-23 13:32:52 -070072
Santos Cordon998f42b2013-08-02 16:13:12 -070073 private final CallStateMonitor mCallStateMonitor;
74 private final CallManager mCallManager;
75 private final HashMap<Connection, Call> mCallMap = Maps.newHashMap();
76 private final AtomicInteger mNextCallId = new AtomicInteger(CALL_ID_START_VALUE);
77 private Listener mListener;
Santos Cordon63a84242013-07-23 13:32:52 -070078
Santos Cordona3d05142013-07-29 11:25:17 -070079 public CallModeler(CallStateMonitor callStateMonitor, CallManager callManager) {
Santos Cordon63a84242013-07-23 13:32:52 -070080 mCallStateMonitor = callStateMonitor;
Santos Cordona3d05142013-07-29 11:25:17 -070081 mCallManager = callManager;
Santos Cordon63a84242013-07-23 13:32:52 -070082
83 mCallStateMonitor.addListener(this);
84 }
85
86 @Override
87 public void handleMessage(Message msg) {
88 switch(msg.what) {
89 case CallStateMonitor.PHONE_NEW_RINGING_CONNECTION:
90 onNewRingingConnection((AsyncResult) msg.obj);
91 break;
92 case CallStateMonitor.PHONE_DISCONNECT:
93 onDisconnect((AsyncResult) msg.obj);
Santos Cordon995c8162013-07-29 09:22:22 -070094 break;
95 case CallStateMonitor.PHONE_STATE_CHANGED:
96 onPhoneStateChanged((AsyncResult) msg.obj);
97 break;
Santos Cordon63a84242013-07-23 13:32:52 -070098 default:
99 break;
100 }
101 }
102
Santos Cordon998f42b2013-08-02 16:13:12 -0700103 public void setListener(Listener listener) {
Santos Cordon63a84242013-07-23 13:32:52 -0700104 Preconditions.checkNotNull(listener);
Santos Cordon998f42b2013-08-02 16:13:12 -0700105 Preconditions.checkState(mListener == null);
Santos Cordon63a84242013-07-23 13:32:52 -0700106
Santos Cordon998f42b2013-08-02 16:13:12 -0700107 // only support setting listener once.
108 // We only have one listener anyway and supporting multiple means maintaining state for
109 // each of the listeners so that we can do proper diffs.
110 mListener = listener;
111 }
112
113 public List<Call> getFullList() {
114 final List<Call> retval = Lists.newArrayList();
115 doUpdate(true, retval);
116 return retval;
Santos Cordon63a84242013-07-23 13:32:52 -0700117 }
118
119 private void onNewRingingConnection(AsyncResult r) {
120 final Connection conn = (Connection) r.result;
Santos Cordon995c8162013-07-29 09:22:22 -0700121 final Call call = getCallFromConnection(conn, true);
Santos Cordona3d05142013-07-29 11:25:17 -0700122 call.setState(Call.State.INCOMING);
Santos Cordon63a84242013-07-23 13:32:52 -0700123
Santos Cordon998f42b2013-08-02 16:13:12 -0700124 if (call != null && mListener != null) {
125 mListener.onUpdate(Lists.newArrayList(call), false);
Santos Cordon63a84242013-07-23 13:32:52 -0700126 }
127 }
128
129 private void onDisconnect(AsyncResult r) {
130 final Connection conn = (Connection) r.result;
Santos Cordon995c8162013-07-29 09:22:22 -0700131 final Call call = getCallFromConnection(conn, false);
Santos Cordon998f42b2013-08-02 16:13:12 -0700132 call.setState(Call.State.IDLE);
Santos Cordon63a84242013-07-23 13:32:52 -0700133
Santos Cordon995c8162013-07-29 09:22:22 -0700134 if (call != null) {
135 mCallMap.remove(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700136
Santos Cordon998f42b2013-08-02 16:13:12 -0700137 if (mListener != null) {
138 mListener.onDisconnect(call);
Santos Cordon63a84242013-07-23 13:32:52 -0700139 }
140 }
141 }
142
Santos Cordona3d05142013-07-29 11:25:17 -0700143 /**
144 * Called when the phone state changes.
Santos Cordona3d05142013-07-29 11:25:17 -0700145 */
Santos Cordon995c8162013-07-29 09:22:22 -0700146 private void onPhoneStateChanged(AsyncResult r) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700147 final List<Call> updatedCalls = Lists.newArrayList();
148 doUpdate(false, updatedCalls);
149
150 if (mListener != null) {
151 mListener.onUpdate(updatedCalls, false);
152 }
153 }
154
155
156 /**
157 * Go through the Calls from CallManager and return the list of calls that were updated.
158 * Or, the full list if requested.
159 */
160 private void doUpdate(boolean fullUpdate, List<Call> out) {
Santos Cordona3d05142013-07-29 11:25:17 -0700161 final List<com.android.internal.telephony.Call> telephonyCalls = Lists.newArrayList();
162 telephonyCalls.addAll(mCallManager.getRingingCalls());
163 telephonyCalls.addAll(mCallManager.getForegroundCalls());
164 telephonyCalls.addAll(mCallManager.getBackgroundCalls());
165
Santos Cordona3d05142013-07-29 11:25:17 -0700166 // Cycle through all the Connections on all the Calls. Update our Call objects
167 // to reflect any new state and send the updated Call objects to the handler service.
168 for (com.android.internal.telephony.Call telephonyCall : telephonyCalls) {
169 final int state = translateStateFromTelephony(telephonyCall.getState());
170
171 for (Connection connection : telephonyCall.getConnections()) {
Santos Cordon998f42b2013-08-02 16:13:12 -0700172 // new connections return a Call with INVALID state, which does not translate to
173 // a state in the Connection object. This ensures that staleness check below
174 // fails and we always add the item to the update list if it is new.
Santos Cordona3d05142013-07-29 11:25:17 -0700175 final Call call = getCallFromConnection(connection, true);
176
Santos Cordon998f42b2013-08-02 16:13:12 -0700177 if (fullUpdate || call.getState() != state) {
Santos Cordona3d05142013-07-29 11:25:17 -0700178 call.setState(state);
Santos Cordon998f42b2013-08-02 16:13:12 -0700179 out.add(call);
Santos Cordona3d05142013-07-29 11:25:17 -0700180 }
181 }
182 }
Santos Cordona3d05142013-07-29 11:25:17 -0700183 }
184
185 private int translateStateFromTelephony(com.android.internal.telephony.Call.State teleState) {
186 int retval = State.IDLE;
187 switch (teleState) {
188 case ACTIVE:
189 retval = State.ACTIVE;
190 break;
191 case INCOMING:
192 retval = State.INCOMING;
193 break;
194 case DIALING:
195 case ALERTING:
196 retval = State.DIALING;
197 break;
198 case WAITING:
199 retval = State.CALL_WAITING;
200 break;
201 case HOLDING:
202 retval = State.ONHOLD;
203 break;
204 default:
205 }
206
207 return retval;
Santos Cordon995c8162013-07-29 09:22:22 -0700208 }
209
Santos Cordon63a84242013-07-23 13:32:52 -0700210 /**
211 * Gets an existing callId for a connection, or creates one
212 * if none exists.
213 */
Santos Cordon995c8162013-07-29 09:22:22 -0700214 private Call getCallFromConnection(Connection conn, boolean createIfMissing) {
215 Call call = null;
Santos Cordon63a84242013-07-23 13:32:52 -0700216
217 // Find the call id or create if missing and requested.
218 if (conn != null) {
Santos Cordon995c8162013-07-29 09:22:22 -0700219 if (mCallMap.containsKey(conn)) {
220 call = mCallMap.get(conn);
Santos Cordon63a84242013-07-23 13:32:52 -0700221 } else if (createIfMissing) {
Santos Cordon995c8162013-07-29 09:22:22 -0700222 int callId;
Santos Cordon63a84242013-07-23 13:32:52 -0700223 int newNextCallId;
224 do {
225 callId = mNextCallId.get();
226
227 // protect against overflow
228 newNextCallId = (callId == Integer.MAX_VALUE ?
229 CALL_ID_START_VALUE : callId + 1);
230
231 // Keep looping if the change was not atomic OR the value is already taken.
232 // The call to containsValue() is linear, however, most devices support a
233 // maximum of 7 connections so it's not expensive.
234 } while (!mNextCallId.compareAndSet(callId, newNextCallId) ||
Santos Cordon995c8162013-07-29 09:22:22 -0700235 mCallMap.containsValue(callId));
Santos Cordon63a84242013-07-23 13:32:52 -0700236
Santos Cordon995c8162013-07-29 09:22:22 -0700237 call = new Call(callId);
Santos Cordon179907f2013-07-31 09:40:55 -0700238 call.setNumber(conn.getAddress());
Santos Cordon995c8162013-07-29 09:22:22 -0700239 mCallMap.put(conn, call);
Santos Cordon63a84242013-07-23 13:32:52 -0700240 }
241 }
Santos Cordon995c8162013-07-29 09:22:22 -0700242 return call;
Santos Cordon63a84242013-07-23 13:32:52 -0700243 }
244
245 /**
246 * Listener interface for changes to Calls.
247 */
248 public interface Listener {
Santos Cordon995c8162013-07-29 09:22:22 -0700249 void onDisconnect(Call call);
Santos Cordon998f42b2013-08-02 16:13:12 -0700250 void onUpdate(List<Call> calls, boolean fullUpdate);
Santos Cordon63a84242013-07-23 13:32:52 -0700251 }
252}