Ihab Awad | e63fadb | 2014-07-09 21:52:04 -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 | |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame^] | 17 | package android.telecom; |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 18 | |
Gabriel Peal | b95f169 | 2014-08-19 14:24:18 -0700 | [diff] [blame] | 19 | import android.annotation.SystemApi; |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 20 | import android.util.ArrayMap; |
| 21 | |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 22 | import java.util.Collections; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | import java.util.Objects; |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 26 | import java.util.concurrent.CopyOnWriteArrayList; |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * A unified virtual device providing a means of voice (and other) communication on a device. |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 30 | * |
| 31 | * {@hide} |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 32 | */ |
Gabriel Peal | b95f169 | 2014-08-19 14:24:18 -0700 | [diff] [blame] | 33 | @SystemApi |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 34 | public final class Phone { |
| 35 | |
| 36 | public abstract static class Listener { |
| 37 | /** |
| 38 | * Called when the audio state changes. |
| 39 | * |
| 40 | * @param phone The {@code Phone} calling this method. |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 41 | * @param audioState The new {@link AudioState}. |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 42 | */ |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 43 | public void onAudioStateChanged(Phone phone, AudioState audioState) { } |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Called to bring the in-call screen to the foreground. The in-call experience should |
| 47 | * respond immediately by coming to the foreground to inform the user of the state of |
| 48 | * ongoing {@code Call}s. |
| 49 | * |
| 50 | * @param phone The {@code Phone} calling this method. |
| 51 | * @param showDialpad If true, put up the dialpad when the screen is shown. |
| 52 | */ |
| 53 | public void onBringToForeground(Phone phone, boolean showDialpad) { } |
| 54 | |
| 55 | /** |
| 56 | * Called when a {@code Call} has been added to this in-call session. The in-call user |
| 57 | * experience should add necessary state listeners to the specified {@code Call} and |
| 58 | * immediately start to show the user information about the existence |
| 59 | * and nature of this {@code Call}. Subsequent invocations of {@link #getCalls()} will |
| 60 | * include this {@code Call}. |
| 61 | * |
| 62 | * @param phone The {@code Phone} calling this method. |
| 63 | * @param call A newly added {@code Call}. |
| 64 | */ |
| 65 | public void onCallAdded(Phone phone, Call call) { } |
| 66 | |
| 67 | /** |
| 68 | * Called when a {@code Call} has been removed from this in-call session. The in-call user |
| 69 | * experience should remove any state listeners from the specified {@code Call} and |
| 70 | * immediately stop displaying any information about this {@code Call}. |
| 71 | * Subsequent invocations of {@link #getCalls()} will no longer include this {@code Call}. |
| 72 | * |
| 73 | * @param phone The {@code Phone} calling this method. |
| 74 | * @param call A newly removed {@code Call}. |
| 75 | */ |
| 76 | public void onCallRemoved(Phone phone, Call call) { } |
| 77 | } |
| 78 | |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame^] | 79 | // A Map allows us to track each Call by its Telecom-specified call ID |
| 80 | private final Map<String, Call> mCallByTelecomCallId = new ArrayMap<>(); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 81 | |
| 82 | // A List allows us to keep the Calls in a stable iteration order so that casually developed |
| 83 | // user interface components do not incur any spurious jank |
Santos Cordon | f30d7e9 | 2014-08-26 09:54:33 -0700 | [diff] [blame] | 84 | private final List<Call> mCalls = new CopyOnWriteArrayList<>(); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 85 | |
| 86 | // An unmodifiable view of the above List can be safely shared with subclass implementations |
| 87 | private final List<Call> mUnmodifiableCalls = Collections.unmodifiableList(mCalls); |
| 88 | |
| 89 | private final InCallAdapter mInCallAdapter; |
| 90 | |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 91 | private AudioState mAudioState; |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 92 | |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 93 | private final List<Listener> mListeners = new CopyOnWriteArrayList<>(); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 94 | |
| 95 | /** {@hide} */ |
| 96 | Phone(InCallAdapter adapter) { |
| 97 | mInCallAdapter = adapter; |
| 98 | } |
| 99 | |
| 100 | /** {@hide} */ |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 101 | final void internalAddCall(ParcelableCall parcelableCall) { |
| 102 | Call call = new Call(this, parcelableCall.getId(), mInCallAdapter); |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame^] | 103 | mCallByTelecomCallId.put(parcelableCall.getId(), call); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 104 | mCalls.add(call); |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 105 | checkCallTree(parcelableCall); |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame^] | 106 | call.internalUpdate(parcelableCall, mCallByTelecomCallId); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 107 | fireCallAdded(call); |
| 108 | } |
| 109 | |
| 110 | /** {@hide} */ |
| 111 | final void internalRemoveCall(Call call) { |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame^] | 112 | mCallByTelecomCallId.remove(call.internalGetCallId()); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 113 | mCalls.remove(call); |
| 114 | fireCallRemoved(call); |
| 115 | } |
| 116 | |
| 117 | /** {@hide} */ |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 118 | final void internalUpdateCall(ParcelableCall parcelableCall) { |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame^] | 119 | Call call = mCallByTelecomCallId.get(parcelableCall.getId()); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 120 | if (call != null) { |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 121 | checkCallTree(parcelableCall); |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame^] | 122 | call.internalUpdate(parcelableCall, mCallByTelecomCallId); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | /** {@hide} */ |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame^] | 127 | final void internalSetPostDialWait(String telecomId, String remaining) { |
| 128 | Call call = mCallByTelecomCallId.get(telecomId); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 129 | if (call != null) { |
| 130 | call.internalSetPostDialWait(remaining); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** {@hide} */ |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 135 | final void internalAudioStateChanged(AudioState audioState) { |
| 136 | if (!Objects.equals(mAudioState, audioState)) { |
| 137 | mAudioState = audioState; |
| 138 | fireAudioStateChanged(audioState); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | /** {@hide} */ |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame^] | 143 | final Call internalGetCallByTelecomId(String telecomId) { |
| 144 | return mCallByTelecomCallId.get(telecomId); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** {@hide} */ |
| 148 | final void internalBringToForeground(boolean showDialpad) { |
| 149 | fireBringToForeground(showDialpad); |
| 150 | } |
| 151 | |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 152 | /** |
Santos Cordon | f30d7e9 | 2014-08-26 09:54:33 -0700 | [diff] [blame] | 153 | * Called to destroy the phone and cleanup any lingering calls. |
| 154 | * @hide |
| 155 | */ |
| 156 | final void destroy() { |
| 157 | for (Call call : mCalls) { |
| 158 | if (call.getState() != Call.STATE_DISCONNECTED) { |
| 159 | call.internalSetDisconnected(); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 165 | * Adds a listener to this {@code Phone}. |
| 166 | * |
| 167 | * @param listener A {@code Listener} object. |
| 168 | */ |
| 169 | public final void addListener(Listener listener) { |
| 170 | mListeners.add(listener); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Removes a listener from this {@code Phone}. |
| 175 | * |
| 176 | * @param listener A {@code Listener} object. |
| 177 | */ |
| 178 | public final void removeListener(Listener listener) { |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 179 | if (listener != null) { |
| 180 | mListeners.remove(listener); |
| 181 | } |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Obtains the current list of {@code Call}s to be displayed by this in-call experience. |
| 186 | * |
| 187 | * @return A list of the relevant {@code Call}s. |
| 188 | */ |
| 189 | public final List<Call> getCalls() { |
| 190 | return mUnmodifiableCalls; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Sets the microphone mute state. When this request is honored, there will be change to |
| 195 | * the {@link #getAudioState()}. |
| 196 | * |
| 197 | * @param state {@code true} if the microphone should be muted; {@code false} otherwise. |
| 198 | */ |
| 199 | public final void setMuted(boolean state) { |
| 200 | mInCallAdapter.mute(state); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Sets the audio route (speaker, bluetooth, etc...). When this request is honored, there will |
| 205 | * be change to the {@link #getAudioState()}. |
| 206 | * |
| 207 | * @param route The audio route to use. |
| 208 | */ |
| 209 | public final void setAudioRoute(int route) { |
| 210 | mInCallAdapter.setAudioRoute(route); |
| 211 | } |
| 212 | |
| 213 | /** |
Yorke Lee | 0d6ea71 | 2014-07-28 14:39:23 -0700 | [diff] [blame] | 214 | * Turns the proximity sensor on. When this request is made, the proximity sensor will |
| 215 | * become active, and the touch screen and display will be turned off when the user's face |
| 216 | * is detected to be in close proximity to the screen. This operation is a no-op on devices |
| 217 | * that do not have a proximity sensor. |
| 218 | */ |
| 219 | public final void setProximitySensorOn() { |
| 220 | mInCallAdapter.turnProximitySensorOn(); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Turns the proximity sensor off. When this request is made, the proximity sensor will |
| 225 | * become inactive, and no longer affect the touch screen and display. This operation is a |
| 226 | * no-op on devices that do not have a proximity sensor. |
| 227 | * |
| 228 | * @param screenOnImmediately If true, the screen will be turned on immediately if it was |
| 229 | * previously off. Otherwise, the screen will only be turned on after the proximity sensor |
| 230 | * is no longer triggered. |
| 231 | */ |
| 232 | public final void setProximitySensorOff(boolean screenOnImmediately) { |
| 233 | mInCallAdapter.turnProximitySensorOff(screenOnImmediately); |
| 234 | } |
| 235 | |
| 236 | /** |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 237 | * Obtains the current phone call audio state of the {@code Phone}. |
| 238 | * |
| 239 | * @return An object encapsulating the audio state. |
| 240 | */ |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 241 | public final AudioState getAudioState() { |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 242 | return mAudioState; |
| 243 | } |
| 244 | |
| 245 | private void fireCallAdded(Call call) { |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 246 | for (Listener listener : mListeners) { |
| 247 | listener.onCallAdded(this, call); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | private void fireCallRemoved(Call call) { |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 252 | for (Listener listener : mListeners) { |
| 253 | listener.onCallRemoved(this, call); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Ihab Awad | b19a0bc | 2014-08-07 19:46:01 -0700 | [diff] [blame] | 257 | private void fireAudioStateChanged(AudioState audioState) { |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 258 | for (Listener listener : mListeners) { |
| 259 | listener.onAudioStateChanged(this, audioState); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
| 263 | private void fireBringToForeground(boolean showDialpad) { |
Jay Shrauner | 229e382 | 2014-08-15 09:23:07 -0700 | [diff] [blame] | 264 | for (Listener listener : mListeners) { |
| 265 | listener.onBringToForeground(this, showDialpad); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 269 | private void checkCallTree(ParcelableCall parcelableCall) { |
| 270 | if (parcelableCall.getParentCallId() != null && |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame^] | 271 | !mCallByTelecomCallId.containsKey(parcelableCall.getParentCallId())) { |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 272 | Log.wtf(this, "ParcelableCall %s has nonexistent parent %s", |
| 273 | parcelableCall.getId(), parcelableCall.getParentCallId()); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 274 | } |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 275 | if (parcelableCall.getChildCallIds() != null) { |
| 276 | for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) { |
Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame^] | 277 | if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) { |
Santos Cordon | 88b771d | 2014-07-19 13:10:40 -0700 | [diff] [blame] | 278 | Log.wtf(this, "ParcelableCall %s has nonexistent child %s", |
| 279 | parcelableCall.getId(), parcelableCall.getChildCallIds().get(i)); |
Ihab Awad | e63fadb | 2014-07-09 21:52:04 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | } |