RemoteConference.java revision 58bacc513c5e665c62e7d82cc6f39196b4ff4302
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
17package android.telecom;
18
19import com.android.internal.telecom.IConnectionService;
20
21import android.os.RemoteException;
22
23import java.util.Collections;
24import java.util.List;
25import java.util.Set;
26import java.util.concurrent.CopyOnWriteArrayList;
27import java.util.concurrent.CopyOnWriteArraySet;
28
29/**
30 * Represents a conference call which can contain any number of {@link Connection} objects.
31 */
32public final class RemoteConference {
33
34    public abstract static class Callback {
35        public void onStateChanged(RemoteConference conference, int oldState, int newState) {}
36        public void onDisconnected(RemoteConference conference, DisconnectCause disconnectCause) {}
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
46    private final Set<Callback> mCallbacks = new CopyOnWriteArraySet<>();
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;
52    private DisconnectCause mDisconnectCause;
53    private int mCallCapabilities;
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        }
71        for (Callback c : mCallbacks) {
72            c.onDestroyed(this);
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;
89            for (Callback c : mCallbacks) {
90                c.onStateChanged(this, oldState, newState);
91            }
92        }
93    }
94
95    /** {@hide} */
96    void addConnection(RemoteConnection connection) {
97        if (!mChildConnections.contains(connection)) {
98            mChildConnections.add(connection);
99            connection.setConference(this);
100            for (Callback c : mCallbacks) {
101                c.onConnectionAdded(this, connection);
102            }
103        }
104    }
105
106    /** {@hide} */
107    void removeConnection(RemoteConnection connection) {
108        if (mChildConnections.contains(connection)) {
109            mChildConnections.remove(connection);
110            connection.setConference(null);
111            for (Callback c : mCallbacks) {
112                c.onConnectionRemoved(this, connection);
113            }
114        }
115    }
116
117    /** {@hide} */
118    void setCallCapabilities(int capabilities) {
119        if (mCallCapabilities != capabilities) {
120            mCallCapabilities = capabilities;
121            for (Callback c : mCallbacks) {
122                c.onCapabilitiesChanged(this, mCallCapabilities);
123            }
124        }
125    }
126
127    /** {@hide} */
128    void setDisconnected(DisconnectCause disconnectCause) {
129        if (mState != Connection.STATE_DISCONNECTED) {
130            mDisconnectCause = disconnectCause;
131            setState(Connection.STATE_DISCONNECTED);
132            for (Callback c : mCallbacks) {
133                c.onDisconnected(this, disconnectCause);
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
180    public DisconnectCause getDisconnectCause() {
181        return mDisconnectCause;
182    }
183
184    public void playDtmfTone(char digit) {
185        try {
186            mConnectionService.playDtmfTone(mId, digit);
187        } catch (RemoteException e) {
188        }
189    }
190
191    public void stopDtmfTone() {
192        try {
193            mConnectionService.stopDtmfTone(mId);
194        } catch (RemoteException e) {
195        }
196    }
197
198    public void setAudioState(AudioState state) {
199        try {
200            mConnectionService.onAudioStateChanged(mId, state);
201        } catch (RemoteException e) {
202        }
203    }
204
205    public final void registerCallback(Callback callback) {
206        mCallbacks.add(callback);
207    }
208
209    public final void unregisterCallback(Callback callback) {
210        mCallbacks.remove(callback);
211    }
212}
213