Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.phone; |
| 18 | |
| 19 | import com.google.android.collect.Lists; |
| 20 | import com.google.android.collect.Maps; |
| 21 | import com.google.common.base.Preconditions; |
| 22 | |
| 23 | import android.os.AsyncResult; |
| 24 | import android.os.Handler; |
| 25 | import android.os.Message; |
| 26 | import android.util.Log; |
| 27 | |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 28 | import com.android.internal.telephony.CallManager; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 29 | import com.android.internal.telephony.Connection; |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 30 | import com.android.internal.telephony.PhoneConstants; |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 31 | import com.android.services.telephony.common.Call; |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 32 | import com.android.services.telephony.common.Call.State; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 33 | |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.HashMap; |
| 36 | import java.util.List; |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame^] | 37 | import java.util.Map.Entry; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 38 | import 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 | */ |
| 68 | public 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 Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 73 | |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 74 | 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 Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 79 | |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 80 | public CallModeler(CallStateMonitor callStateMonitor, CallManager callManager) { |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 81 | mCallStateMonitor = callStateMonitor; |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 82 | mCallManager = callManager; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 83 | |
| 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 Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 95 | break; |
| 96 | case CallStateMonitor.PHONE_STATE_CHANGED: |
| 97 | onPhoneStateChanged((AsyncResult) msg.obj); |
| 98 | break; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 99 | default: |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 104 | public void setListener(Listener listener) { |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 105 | Preconditions.checkNotNull(listener); |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 106 | Preconditions.checkState(mListener == null); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 107 | |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 108 | // 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 Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame^] | 120 | 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 Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 130 | private void onNewRingingConnection(AsyncResult r) { |
| 131 | final Connection conn = (Connection) r.result; |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 132 | final Call call = getCallFromConnection(conn, true); |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 133 | call.setState(Call.State.INCOMING); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 134 | |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 135 | if (call != null && mListener != null) { |
| 136 | mListener.onUpdate(Lists.newArrayList(call), false); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | private void onDisconnect(AsyncResult r) { |
| 141 | final Connection conn = (Connection) r.result; |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 142 | final Call call = getCallFromConnection(conn, false); |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 143 | call.setState(Call.State.IDLE); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 144 | |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 145 | if (call != null) { |
| 146 | mCallMap.remove(conn); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 147 | |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 148 | if (mListener != null) { |
| 149 | mListener.onDisconnect(call); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 154 | /** |
| 155 | * Called when the phone state changes. |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 156 | */ |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 157 | private void onPhoneStateChanged(AsyncResult r) { |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 158 | final List<Call> updatedCalls = Lists.newArrayList(); |
| 159 | doUpdate(false, updatedCalls); |
| 160 | |
| 161 | if (mListener != null) { |
| 162 | mListener.onUpdate(updatedCalls, false); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /** |
| 168 | * Go through the Calls from CallManager and return the list of calls that were updated. |
| 169 | * Or, the full list if requested. |
| 170 | */ |
| 171 | private void doUpdate(boolean fullUpdate, List<Call> out) { |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 172 | final List<com.android.internal.telephony.Call> telephonyCalls = Lists.newArrayList(); |
| 173 | telephonyCalls.addAll(mCallManager.getRingingCalls()); |
| 174 | telephonyCalls.addAll(mCallManager.getForegroundCalls()); |
| 175 | telephonyCalls.addAll(mCallManager.getBackgroundCalls()); |
| 176 | |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 177 | // Cycle through all the Connections on all the Calls. Update our Call objects |
| 178 | // to reflect any new state and send the updated Call objects to the handler service. |
| 179 | for (com.android.internal.telephony.Call telephonyCall : telephonyCalls) { |
| 180 | final int state = translateStateFromTelephony(telephonyCall.getState()); |
| 181 | |
| 182 | for (Connection connection : telephonyCall.getConnections()) { |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 183 | // new connections return a Call with INVALID state, which does not translate to |
| 184 | // a state in the Connection object. This ensures that staleness check below |
| 185 | // fails and we always add the item to the update list if it is new. |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 186 | final Call call = getCallFromConnection(connection, true); |
| 187 | |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 188 | if (fullUpdate || call.getState() != state) { |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 189 | call.setState(state); |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 190 | out.add(call); |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | } |
Santos Cordon | a3d0514 | 2013-07-29 11:25:17 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | private int translateStateFromTelephony(com.android.internal.telephony.Call.State teleState) { |
| 197 | int retval = State.IDLE; |
| 198 | switch (teleState) { |
| 199 | case ACTIVE: |
| 200 | retval = State.ACTIVE; |
| 201 | break; |
| 202 | case INCOMING: |
| 203 | retval = State.INCOMING; |
| 204 | break; |
| 205 | case DIALING: |
| 206 | case ALERTING: |
| 207 | retval = State.DIALING; |
| 208 | break; |
| 209 | case WAITING: |
| 210 | retval = State.CALL_WAITING; |
| 211 | break; |
| 212 | case HOLDING: |
| 213 | retval = State.ONHOLD; |
| 214 | break; |
| 215 | default: |
| 216 | } |
| 217 | |
| 218 | return retval; |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 221 | /** |
| 222 | * Gets an existing callId for a connection, or creates one |
| 223 | * if none exists. |
| 224 | */ |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 225 | private Call getCallFromConnection(Connection conn, boolean createIfMissing) { |
| 226 | Call call = null; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 227 | |
| 228 | // Find the call id or create if missing and requested. |
| 229 | if (conn != null) { |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 230 | if (mCallMap.containsKey(conn)) { |
| 231 | call = mCallMap.get(conn); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 232 | } else if (createIfMissing) { |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 233 | int callId; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 234 | int newNextCallId; |
| 235 | do { |
| 236 | callId = mNextCallId.get(); |
| 237 | |
| 238 | // protect against overflow |
| 239 | newNextCallId = (callId == Integer.MAX_VALUE ? |
| 240 | CALL_ID_START_VALUE : callId + 1); |
| 241 | |
| 242 | // Keep looping if the change was not atomic OR the value is already taken. |
| 243 | // The call to containsValue() is linear, however, most devices support a |
| 244 | // maximum of 7 connections so it's not expensive. |
| 245 | } while (!mNextCallId.compareAndSet(callId, newNextCallId) || |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 246 | mCallMap.containsValue(callId)); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 247 | |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 248 | call = new Call(callId); |
Santos Cordon | 179907f | 2013-07-31 09:40:55 -0700 | [diff] [blame] | 249 | call.setNumber(conn.getAddress()); |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 250 | mCallMap.put(conn, call); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 253 | return call; |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Listener interface for changes to Calls. |
| 258 | */ |
| 259 | public interface Listener { |
Santos Cordon | 995c816 | 2013-07-29 09:22:22 -0700 | [diff] [blame] | 260 | void onDisconnect(Call call); |
Santos Cordon | 998f42b | 2013-08-02 16:13:12 -0700 | [diff] [blame] | 261 | void onUpdate(List<Call> calls, boolean fullUpdate); |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 262 | } |
Santos Cordon | 249efd0 | 2013-08-05 03:33:56 -0700 | [diff] [blame^] | 263 | |
| 264 | /** |
| 265 | * Result class for accessing a call by connection. |
| 266 | */ |
| 267 | public static class CallResult { |
| 268 | public Call mCall; |
| 269 | public Connection mConnection; |
| 270 | |
| 271 | private CallResult(Call call, Connection connection) { |
| 272 | mCall = call; |
| 273 | mConnection = connection; |
| 274 | } |
| 275 | |
| 276 | public Call getCall() { |
| 277 | return mCall; |
| 278 | } |
| 279 | |
| 280 | public Connection getConnection() { |
| 281 | return mConnection; |
| 282 | } |
| 283 | } |
Santos Cordon | 63a8424 | 2013-07-23 13:32:52 -0700 | [diff] [blame] | 284 | } |