| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -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 | |
| Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 17 | package android.telecom; |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 18 | |
| Tyler Gunn | ef9f6f9 | 2014-09-12 22:16:17 -0700 | [diff] [blame] | 19 | import com.android.internal.telecom.IConnectionService; |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 20 | |
| 21 | import android.os.RemoteException; |
| 22 | import android.telephony.DisconnectCause; |
| 23 | |
| 24 | import java.util.Collections; |
| 25 | import java.util.List; |
| 26 | import java.util.Set; |
| 27 | import java.util.concurrent.CopyOnWriteArrayList; |
| 28 | import java.util.concurrent.CopyOnWriteArraySet; |
| 29 | |
| 30 | /** |
| 31 | * Represents a conference call which can contain any number of {@link Connection} objects. |
| 32 | */ |
| 33 | public final class RemoteConference { |
| 34 | |
| Nancy Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 35 | public abstract static class Callback { |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 36 | public void onStateChanged(RemoteConference conference, int oldState, int newState) {} |
| 37 | public void onDisconnected(RemoteConference conference, int cause, String message) {} |
| 38 | public void onConnectionAdded(RemoteConference conference, RemoteConnection connection) {} |
| 39 | public void onConnectionRemoved(RemoteConference conference, RemoteConnection connection) {} |
| 40 | public void onCapabilitiesChanged(RemoteConference conference, int capabilities) {} |
| 41 | public void onDestroyed(RemoteConference conference) {} |
| 42 | } |
| 43 | |
| 44 | private final String mId; |
| 45 | private final IConnectionService mConnectionService; |
| 46 | |
| Nancy Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 47 | private final Set<Callback> mCallbacks = new CopyOnWriteArraySet<>(); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 48 | private final List<RemoteConnection> mChildConnections = new CopyOnWriteArrayList<>(); |
| 49 | private final List<RemoteConnection> mUnmodifiableChildConnections = |
| 50 | Collections.unmodifiableList(mChildConnections); |
| 51 | |
| 52 | private int mState = Connection.STATE_NEW; |
| 53 | private int mDisconnectCause = DisconnectCause.NOT_VALID; |
| 54 | private int mCallCapabilities; |
| 55 | private String mDisconnectMessage; |
| 56 | |
| 57 | /** {@hide} */ |
| 58 | RemoteConference(String id, IConnectionService connectionService) { |
| 59 | mId = id; |
| 60 | mConnectionService = connectionService; |
| 61 | } |
| 62 | |
| 63 | /** {@hide} */ |
| 64 | String getId() { |
| 65 | return mId; |
| 66 | } |
| 67 | |
| 68 | /** {@hide} */ |
| 69 | void setDestroyed() { |
| 70 | for (RemoteConnection connection : mChildConnections) { |
| 71 | connection.setConference(null); |
| 72 | } |
| Nancy Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 73 | for (Callback c : mCallbacks) { |
| 74 | c.onDestroyed(this); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | /** {@hide} */ |
| 79 | void setState(int newState) { |
| 80 | if (newState != Connection.STATE_ACTIVE && |
| 81 | newState != Connection.STATE_HOLDING && |
| 82 | newState != Connection.STATE_DISCONNECTED) { |
| 83 | Log.w(this, "Unsupported state transition for Conference call.", |
| 84 | Connection.stateToString(newState)); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | if (mState != newState) { |
| 89 | int oldState = mState; |
| 90 | mState = newState; |
| Nancy Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 91 | for (Callback c : mCallbacks) { |
| 92 | c.onStateChanged(this, oldState, newState); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** {@hide} */ |
| 98 | void addConnection(RemoteConnection connection) { |
| 99 | if (!mChildConnections.contains(connection)) { |
| 100 | mChildConnections.add(connection); |
| 101 | connection.setConference(this); |
| Nancy Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 102 | for (Callback c : mCallbacks) { |
| 103 | c.onConnectionAdded(this, connection); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** {@hide} */ |
| 109 | void removeConnection(RemoteConnection connection) { |
| 110 | if (mChildConnections.contains(connection)) { |
| 111 | mChildConnections.remove(connection); |
| 112 | connection.setConference(null); |
| Nancy Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 113 | for (Callback c : mCallbacks) { |
| 114 | c.onConnectionRemoved(this, connection); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** {@hide} */ |
| 120 | void setCallCapabilities(int capabilities) { |
| 121 | if (mCallCapabilities != capabilities) { |
| 122 | mCallCapabilities = capabilities; |
| Nancy Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 123 | for (Callback c : mCallbacks) { |
| 124 | c.onCapabilitiesChanged(this, mCallCapabilities); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** {@hide} */ |
| 130 | void setDisconnected(int cause, String message) { |
| 131 | if (mState != Connection.STATE_DISCONNECTED) { |
| 132 | mDisconnectCause = cause; |
| 133 | mDisconnectMessage = message; |
| 134 | setState(Connection.STATE_DISCONNECTED); |
| Nancy Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 135 | for (Callback c : mCallbacks) { |
| 136 | c.onDisconnected(this, cause, message); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | public final List<RemoteConnection> getConnections() { |
| 142 | return mUnmodifiableChildConnections; |
| 143 | } |
| 144 | |
| 145 | public final int getState() { |
| 146 | return mState; |
| 147 | } |
| 148 | |
| 149 | public final int getCallCapabilities() { |
| 150 | return mCallCapabilities; |
| 151 | } |
| 152 | |
| 153 | public void disconnect() { |
| 154 | try { |
| 155 | mConnectionService.disconnect(mId); |
| 156 | } catch (RemoteException e) { |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | public void separate(RemoteConnection connection) { |
| 161 | if (mChildConnections.contains(connection)) { |
| 162 | try { |
| 163 | mConnectionService.splitFromConference(connection.getId()); |
| 164 | } catch (RemoteException e) { |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | public void hold() { |
| 170 | try { |
| 171 | mConnectionService.hold(mId); |
| 172 | } catch (RemoteException e) { |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | public void unhold() { |
| 177 | try { |
| 178 | mConnectionService.unhold(mId); |
| 179 | } catch (RemoteException e) { |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | public int getDisconnectCause() { |
| 184 | return mDisconnectCause; |
| 185 | } |
| 186 | |
| 187 | public String getDisconnectMessage() { |
| 188 | return mDisconnectMessage; |
| 189 | } |
| 190 | |
| Andrew Lee | 100e293 | 2014-09-08 15:34:24 -0700 | [diff] [blame] | 191 | public final void registerCallback(Callback callback) { |
| Nancy Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 192 | mCallbacks.add(callback); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| Andrew Lee | 100e293 | 2014-09-08 15:34:24 -0700 | [diff] [blame] | 195 | public final void unregisterCallback(Callback callback) { |
| Nancy Chen | 1d834f5 | 2014-09-05 11:03:21 -0700 | [diff] [blame] | 196 | mCallbacks.remove(callback); |
| Ihab Awad | b8e85c7 | 2014-08-23 20:34:57 -0700 | [diff] [blame] | 197 | } |
| 198 | } |