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