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 | |
| 28 | import com.android.internal.telephony.Connection; |
| 29 | |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.HashMap; |
| 32 | import java.util.List; |
| 33 | import java.util.concurrent.atomic.AtomicInteger; |
| 34 | |
| 35 | /** |
| 36 | * Creates a Call model from Call state and data received from the telephony |
| 37 | * layer. The telephony layer maintains 3 conceptual objects: Phone, Call, |
| 38 | * Connection. |
| 39 | * |
| 40 | * Phone represents the radio and there is an implementation per technology |
| 41 | * type such as GSMPhone, SipPhone, CDMAPhone, etc. Generally, we will only ever |
| 42 | * deal with one instance of this object for the lifetime of this class. |
| 43 | * |
| 44 | * There are 3 Call instances that exist for the lifetime of this class which |
| 45 | * are created by CallTracker. The three are RingingCall, ForegroundCall, and |
| 46 | * BackgroundCall. |
| 47 | * |
| 48 | * A Connection most closely resembles what the layperson would consider a call. |
| 49 | * A Connection is created when a user dials and it is "owned" by one of the |
| 50 | * three Call instances. Which of the three Calls owns the Connection changes |
| 51 | * as the Connection goes between ACTIVE, HOLD, RINGING, and other states. |
| 52 | * |
| 53 | * This class models a new Call class from Connection objects received from |
| 54 | * the telephony layer. We use Connection references as identifiers for a call; |
| 55 | * new reference = new call. |
| 56 | * |
| 57 | * TODO(klp): Create a new Call class to replace the simple call Id ints |
| 58 | * being used currently. |
| 59 | * |
| 60 | * The new Call models are parcellable for transfer via the CallHandlerService |
| 61 | * API. |
| 62 | */ |
| 63 | public class CallModeler extends Handler { |
| 64 | |
| 65 | private static final String TAG = CallModeler.class.getSimpleName(); |
| 66 | |
| 67 | private static final int CALL_ID_START_VALUE = 1; |
| 68 | private static final int INVALID_CALL_ID = -1; |
| 69 | |
| 70 | private CallStateMonitor mCallStateMonitor; |
| 71 | private HashMap<Connection, Integer> mCallIdMap = Maps.newHashMap(); |
| 72 | private List<Listener> mListeners = Lists.newArrayList(); |
| 73 | private AtomicInteger mNextCallId = new AtomicInteger(CALL_ID_START_VALUE); |
| 74 | |
| 75 | public CallModeler(CallStateMonitor callStateMonitor) { |
| 76 | mCallStateMonitor = callStateMonitor; |
| 77 | |
| 78 | mCallStateMonitor.addListener(this); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void handleMessage(Message msg) { |
| 83 | switch(msg.what) { |
| 84 | case CallStateMonitor.PHONE_NEW_RINGING_CONNECTION: |
| 85 | onNewRingingConnection((AsyncResult) msg.obj); |
| 86 | break; |
| 87 | case CallStateMonitor.PHONE_DISCONNECT: |
| 88 | onDisconnect((AsyncResult) msg.obj); |
| 89 | default: |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public void addListener(Listener listener) { |
| 95 | Preconditions.checkNotNull(listener); |
| 96 | |
| 97 | mListeners.add(listener); |
| 98 | } |
| 99 | |
| 100 | private void onNewRingingConnection(AsyncResult r) { |
| 101 | final Connection conn = (Connection) r.result; |
| 102 | final int callId = getCallId(conn, true); |
| 103 | |
| 104 | for (Listener l : mListeners) { |
| 105 | l.onNewCall(callId); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | private void onDisconnect(AsyncResult r) { |
| 110 | final Connection conn = (Connection) r.result; |
| 111 | final int callId = getCallId(conn, false); |
| 112 | |
| 113 | if (INVALID_CALL_ID != callId) { |
| 114 | mCallIdMap.remove(conn); |
| 115 | |
| 116 | for (Listener l : mListeners) { |
| 117 | l.onDisconnect(callId); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Gets an existing callId for a connection, or creates one |
| 124 | * if none exists. |
| 125 | */ |
| 126 | private int getCallId(Connection conn, boolean createIfMissing) { |
| 127 | int callId = INVALID_CALL_ID; |
| 128 | |
| 129 | // Find the call id or create if missing and requested. |
| 130 | if (conn != null) { |
| 131 | if (mCallIdMap.containsKey(conn)) { |
| 132 | callId = mCallIdMap.get(conn).intValue(); |
| 133 | } else if (createIfMissing) { |
| 134 | int newNextCallId; |
| 135 | do { |
| 136 | callId = mNextCallId.get(); |
| 137 | |
| 138 | // protect against overflow |
| 139 | newNextCallId = (callId == Integer.MAX_VALUE ? |
| 140 | CALL_ID_START_VALUE : callId + 1); |
| 141 | |
| 142 | // Keep looping if the change was not atomic OR the value is already taken. |
| 143 | // The call to containsValue() is linear, however, most devices support a |
| 144 | // maximum of 7 connections so it's not expensive. |
| 145 | } while (!mNextCallId.compareAndSet(callId, newNextCallId) || |
| 146 | mCallIdMap.containsValue(callId)); |
| 147 | |
| 148 | mCallIdMap.put(conn, callId); |
| 149 | } |
| 150 | } |
| 151 | return callId; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Listener interface for changes to Calls. |
| 156 | */ |
| 157 | public interface Listener { |
| 158 | void onNewCall(int callId); |
| 159 | void onDisconnect(int callId); |
| 160 | } |
| 161 | } |