Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 android.telecomm; |
| 18 | |
| 19 | import android.net.Uri; |
| 20 | |
| 21 | import android.os.Bundle; |
| 22 | import android.os.RemoteException; |
| 23 | import android.telephony.DisconnectCause; |
| 24 | |
| 25 | import com.android.internal.telecomm.ICallService; |
| 26 | |
| 27 | import java.util.HashSet; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | /** |
| 31 | * RemoteConnection object used by RemoteConnectionService. |
| 32 | */ |
| 33 | public final class RemoteConnection { |
| 34 | public interface Listener { |
| 35 | void onStateChanged(RemoteConnection connection, int state); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 36 | void onDisconnected(RemoteConnection connection, int cause, String message); |
| 37 | void onRequestingRingback(RemoteConnection connection, boolean ringback); |
| 38 | void onPostDialWait(RemoteConnection connection, String remainingDigits); |
| 39 | void onDestroyed(RemoteConnection connection); |
| 40 | } |
| 41 | |
| 42 | private final ICallService mCallService; |
| 43 | private final String mConnectionId; |
| 44 | private final Set<Listener> mListeners = new HashSet<>(); |
| 45 | |
| 46 | private int mState; |
| 47 | private int mDisconnectCause = DisconnectCause.NOT_VALID; |
| 48 | private String mDisconnectMessage; |
| 49 | private boolean mRequestingRingback; |
| 50 | private boolean mConnected; |
| 51 | |
| 52 | /** |
| 53 | * @hide |
| 54 | */ |
| 55 | RemoteConnection(ICallService callService, String connectionId) { |
| 56 | mCallService = callService; |
| 57 | mConnectionId = connectionId; |
| 58 | |
| 59 | mConnected = true; |
| 60 | } |
| 61 | |
| 62 | public void addListener(Listener listener) { |
| 63 | mListeners.add(listener); |
| 64 | } |
| 65 | |
| 66 | public void removeListener(Listener listener) { |
| 67 | mListeners.remove(listener); |
| 68 | } |
| 69 | |
| 70 | public int getDisconnectCause() { |
| 71 | return mDisconnectCause; |
| 72 | } |
| 73 | |
| 74 | public String getDisconnectMessage() { |
| 75 | return mDisconnectMessage; |
| 76 | } |
| 77 | |
Sailesh Nepal | 091768c | 2014-06-30 15:15:23 -0700 | [diff] [blame^] | 78 | public void abort() { |
| 79 | try { |
| 80 | if (mConnected) { |
| 81 | mCallService.abort(mConnectionId); |
| 82 | } |
| 83 | } catch (RemoteException ignored) { |
| 84 | } |
| 85 | } |
| 86 | |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 87 | public void answer() { |
| 88 | try { |
| 89 | if (mConnected) { |
| 90 | mCallService.answer(mConnectionId); |
| 91 | } |
| 92 | } catch (RemoteException ignored) { |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | public void reject() { |
| 97 | try { |
| 98 | if (mConnected) { |
| 99 | mCallService.reject(mConnectionId); |
| 100 | } |
| 101 | } catch (RemoteException ignored) { |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | public void hold() { |
| 106 | try { |
| 107 | if (mConnected) { |
| 108 | mCallService.hold(mConnectionId); |
| 109 | } |
| 110 | } catch (RemoteException ignored) { |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | public void unhold() { |
| 115 | try { |
| 116 | if (mConnected) { |
| 117 | mCallService.unhold(mConnectionId); |
| 118 | } |
| 119 | } catch (RemoteException ignored) { |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | public void disconnect() { |
| 124 | try { |
| 125 | if (mConnected) { |
| 126 | mCallService.disconnect(mConnectionId); |
| 127 | } |
| 128 | } catch (RemoteException ignored) { |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | public void playDtmf(char digit) { |
| 133 | try { |
| 134 | if (mConnected) { |
| 135 | mCallService.playDtmfTone(mConnectionId, digit); |
| 136 | } |
| 137 | } catch (RemoteException ignored) { |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | public void stopDtmf() { |
| 142 | try { |
| 143 | if (mConnected) { |
| 144 | mCallService.stopDtmfTone(mConnectionId); |
| 145 | } |
| 146 | } catch (RemoteException ignored) { |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | public void postDialContinue(boolean proceed) { |
| 151 | try { |
| 152 | if (mConnected) { |
| 153 | mCallService.onPostDialContinue(mConnectionId, proceed); |
| 154 | } |
| 155 | } catch (RemoteException ignored) { |
| 156 | } |
| 157 | } |
| 158 | |
Sailesh Nepal | 091768c | 2014-06-30 15:15:23 -0700 | [diff] [blame^] | 159 | public void setAudioState(CallAudioState state) { |
| 160 | try { |
| 161 | if (mConnected) { |
| 162 | mCallService.onAudioStateChanged(mConnectionId, state); |
| 163 | } |
| 164 | } catch (RemoteException ignored) { |
| 165 | } |
| 166 | } |
| 167 | |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 168 | /** |
| 169 | * @hide |
| 170 | */ |
| 171 | void setState(int state) { |
| 172 | if (mState != state) { |
| 173 | mState = state; |
| 174 | for (Listener l: mListeners) { |
| 175 | l.onStateChanged(this, state); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * @hide |
| 182 | */ |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 183 | void setDisconnected(int cause, String message) { |
| 184 | if (mState != Connection.State.DISCONNECTED) { |
| 185 | mState = Connection.State.DISCONNECTED; |
| 186 | mDisconnectCause = cause; |
| 187 | mDisconnectMessage = message; |
| 188 | |
| 189 | for (Listener l : mListeners) { |
| 190 | l.onDisconnected(this, cause, message); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * @hide |
| 197 | */ |
| 198 | void setRequestingRingback(boolean ringback) { |
| 199 | if (mRequestingRingback != ringback) { |
| 200 | mRequestingRingback = ringback; |
| 201 | for (Listener l : mListeners) { |
| 202 | l.onRequestingRingback(this, ringback); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * @hide |
| 209 | */ |
| 210 | void setDestroyed() { |
| 211 | if (!mListeners.isEmpty()) { |
| 212 | // Make sure that the listeners are notified that the call is destroyed first. |
| 213 | if (mState != Connection.State.DISCONNECTED) { |
| 214 | setDisconnected(DisconnectCause.ERROR_UNSPECIFIED, "Connection destroyed."); |
| 215 | } |
| 216 | |
| 217 | Set<Listener> listeners = new HashSet<Listener>(mListeners); |
| 218 | mListeners.clear(); |
| 219 | for (Listener l : listeners) { |
| 220 | l.onDestroyed(this); |
| 221 | } |
| 222 | |
| 223 | mConnected = false; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * @hide |
| 229 | */ |
| 230 | void setPostDialWait(String remainingDigits) { |
| 231 | for (Listener l : mListeners) { |
| 232 | l.onPostDialWait(this, remainingDigits); |
| 233 | } |
| 234 | } |
| 235 | } |