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 android.os.Bundle;
20import android.os.RemoteException;
21
22import com.android.internal.telecom.IInCallAdapter;
23
24import java.util.List;
25
26/**
27 * Receives commands from {@link InCallService} implementations which should be executed by
28 * Telecom. When Telecom binds to a {@link InCallService}, an instance of this class is given to
29 * the in-call service through which it can manipulate live (active, dialing, ringing) calls. When
30 * the in-call service is notified of new calls, it can use the
31 * given call IDs to execute commands such as {@link #answerCall} for incoming calls or
32 * {@link #disconnectCall} for active calls the user would like to end. Some commands are only
33 * appropriate for calls in certain states; please consult each method for such limitations.
34 * <p>
35 * The adapter will stop functioning when there are no more calls.
36 *
37 * {@hide}
38 */
39public final class InCallAdapter {
40    private final IInCallAdapter mAdapter;
41
42    /**
43     * {@hide}
44     */
45    public InCallAdapter(IInCallAdapter adapter) {
46        mAdapter = adapter;
47    }
48
49    /**
50     * Instructs Telecom to answer the specified call.
51     *
52     * @param callId The identifier of the call to answer.
53     * @param videoState The video state in which to answer the call.
54     */
55    public void answerCall(String callId, int videoState) {
56        try {
57            mAdapter.answerCall(callId, videoState);
58        } catch (RemoteException e) {
59        }
60    }
61
62    /**
63     * Instructs Telecom to reject the specified call.
64     *
65     * @param callId The identifier of the call to reject.
66     * @param rejectWithMessage Whether to reject with a text message.
67     * @param textMessage An optional text message with which to respond.
68     */
69    public void rejectCall(String callId, boolean rejectWithMessage, String textMessage) {
70        try {
71            mAdapter.rejectCall(callId, rejectWithMessage, textMessage);
72        } catch (RemoteException e) {
73        }
74    }
75
76    /**
77     * Instructs Telecom to disconnect the specified call.
78     *
79     * @param callId The identifier of the call to disconnect.
80     */
81    public void disconnectCall(String callId) {
82        try {
83            mAdapter.disconnectCall(callId);
84        } catch (RemoteException e) {
85        }
86    }
87
88    /**
89     * Instructs Telecom to put the specified call on hold.
90     *
91     * @param callId The identifier of the call to put on hold.
92     */
93    public void holdCall(String callId) {
94        try {
95            mAdapter.holdCall(callId);
96        } catch (RemoteException e) {
97        }
98    }
99
100    /**
101     * Instructs Telecom to release the specified call from hold.
102     *
103     * @param callId The identifier of the call to release from hold.
104     */
105    public void unholdCall(String callId) {
106        try {
107            mAdapter.unholdCall(callId);
108        } catch (RemoteException e) {
109        }
110    }
111
112    /**
113     * Mute the microphone.
114     *
115     * @param shouldMute True if the microphone should be muted.
116     */
117    public void mute(boolean shouldMute) {
118        try {
119            mAdapter.mute(shouldMute);
120        } catch (RemoteException e) {
121        }
122    }
123
124    /**
125     * Sets the audio route (speaker, bluetooth, etc...). See {@link CallAudioState}.
126     *
127     * @param route The audio route to use.
128     */
129    public void setAudioRoute(int route) {
130        try {
131            mAdapter.setAudioRoute(route);
132        } catch (RemoteException e) {
133        }
134    }
135
136    /**
137     * Instructs Telecom to play a dual-tone multi-frequency signaling (DTMF) tone in a call.
138     *
139     * Any other currently playing DTMF tone in the specified call is immediately stopped.
140     *
141     * @param callId The unique ID of the call in which the tone will be played.
142     * @param digit A character representing the DTMF digit for which to play the tone. This
143     *         value must be one of {@code '0'} through {@code '9'}, {@code '*'} or {@code '#'}.
144     */
145    public void playDtmfTone(String callId, char digit) {
146        try {
147            mAdapter.playDtmfTone(callId, digit);
148        } catch (RemoteException e) {
149        }
150    }
151
152    /**
153     * Instructs Telecom to stop any dual-tone multi-frequency signaling (DTMF) tone currently
154     * playing.
155     *
156     * DTMF tones are played by calling {@link #playDtmfTone(String,char)}. If no DTMF tone is
157     * currently playing, this method will do nothing.
158     *
159     * @param callId The unique ID of the call in which any currently playing tone will be stopped.
160     */
161    public void stopDtmfTone(String callId) {
162        try {
163            mAdapter.stopDtmfTone(callId);
164        } catch (RemoteException e) {
165        }
166    }
167
168    /**
169     * Instructs Telecom to continue playing a post-dial DTMF string.
170     *
171     * A post-dial DTMF string is a string of digits entered after a phone number, when dialed,
172     * that are immediately sent as DTMF tones to the recipient as soon as the connection is made.
173     * While these tones are playing, Telecom will notify the {@link InCallService} that the call
174     * is in the post dial state.
175     *
176     * If the DTMF string contains a {@link TelecomManager#DTMF_CHARACTER_PAUSE} symbol, Telecom
177     * will temporarily pause playing the tones for a pre-defined period of time.
178     *
179     * If the DTMF string contains a {@link TelecomManager#DTMF_CHARACTER_WAIT} symbol, Telecom
180     * will pause playing the tones and notify the {@link InCallService} that the call is in the
181     * post dial wait state. When the user decides to continue the postdial sequence, the
182     * {@link InCallService} should invoke the {@link #postDialContinue(String,boolean)} method.
183     *
184     * @param callId The unique ID of the call for which postdial string playing should continue.
185     * @param proceed Whether or not to continue with the post-dial sequence.
186     */
187    public void postDialContinue(String callId, boolean proceed) {
188        try {
189            mAdapter.postDialContinue(callId, proceed);
190        } catch (RemoteException e) {
191        }
192    }
193
194    /**
195     * Instructs Telecom to add a PhoneAccountHandle to the specified call.
196     *
197     * @param callId The identifier of the call.
198     * @param accountHandle The PhoneAccountHandle through which to place the call.
199     * @param setDefault {@code True} if this account should be set as the default for calls.
200     */
201    public void phoneAccountSelected(String callId, PhoneAccountHandle accountHandle,
202            boolean setDefault) {
203        try {
204            mAdapter.phoneAccountSelected(callId, accountHandle, setDefault);
205        } catch (RemoteException e) {
206        }
207    }
208
209    /**
210     * Instructs Telecom to conference the specified call.
211     *
212     * @param callId The unique ID of the call.
213     * @hide
214     */
215    public void conference(String callId, String otherCallId) {
216        try {
217            mAdapter.conference(callId, otherCallId);
218        } catch (RemoteException ignored) {
219        }
220    }
221
222    /**
223     * Instructs Telecom to split the specified call from any conference call with which it may be
224     * connected.
225     *
226     * @param callId The unique ID of the call.
227     * @hide
228     */
229    public void splitFromConference(String callId) {
230        try {
231            mAdapter.splitFromConference(callId);
232        } catch (RemoteException ignored) {
233        }
234    }
235
236    /**
237     * Instructs Telecom to merge child calls of the specified conference call.
238     */
239    public void mergeConference(String callId) {
240        try {
241            mAdapter.mergeConference(callId);
242        } catch (RemoteException ignored) {
243        }
244    }
245
246    /**
247     * Instructs Telecom to swap the child calls of the specified conference call.
248     */
249    public void swapConference(String callId) {
250        try {
251            mAdapter.swapConference(callId);
252        } catch (RemoteException ignored) {
253        }
254    }
255
256    /**
257     * Instructs Telecom to pull an external call to the local device.
258     *
259     * @param callId The callId to pull.
260     */
261    public void pullExternalCall(String callId) {
262        try {
263            mAdapter.pullExternalCall(callId);
264        } catch (RemoteException ignored) {
265        }
266    }
267
268    /**
269     * Intructs Telecom to send a call event.
270     *
271     * @param callId The callId to send the event for.
272     * @param event The event.
273     * @param extras Extras associated with the event.
274     */
275    public void sendCallEvent(String callId, String event, Bundle extras) {
276        try {
277            mAdapter.sendCallEvent(callId, event, extras);
278        } catch (RemoteException ignored) {
279        }
280    }
281
282    /**
283     * Intructs Telecom to add extras to a call.
284     *
285     * @param callId The callId to add the extras to.
286     * @param extras The extras.
287     */
288    public void putExtras(String callId, Bundle extras) {
289        try {
290            mAdapter.putExtras(callId, extras);
291        } catch (RemoteException ignored) {
292        }
293    }
294
295    /**
296     * Intructs Telecom to add an extra to a call.
297     *
298     * @param callId The callId to add the extras to.
299     * @param key The extra key.
300     * @param value The extra value.
301     */
302    public void putExtra(String callId, String key, boolean value) {
303        try {
304            Bundle bundle = new Bundle();
305            bundle.putBoolean(key, value);
306            mAdapter.putExtras(callId, bundle);
307        } catch (RemoteException ignored) {
308        }
309    }
310
311    /**
312     * Intructs Telecom to add an extra to a call.
313     *
314     * @param callId The callId to add the extras to.
315     * @param key The extra key.
316     * @param value The extra value.
317     */
318    public void putExtra(String callId, String key, int value) {
319        try {
320            Bundle bundle = new Bundle();
321            bundle.putInt(key, value);
322            mAdapter.putExtras(callId, bundle);
323        } catch (RemoteException ignored) {
324        }
325    }
326
327    /**
328     * Intructs Telecom to add an extra to a call.
329     *
330     * @param callId The callId to add the extras to.
331     * @param key The extra key.
332     * @param value The extra value.
333     */
334    public void putExtra(String callId, String key, String value) {
335        try {
336            Bundle bundle = new Bundle();
337            bundle.putString(key, value);
338            mAdapter.putExtras(callId, bundle);
339        } catch (RemoteException ignored) {
340        }
341    }
342
343    /**
344     * Intructs Telecom to remove extras from a call.
345     * @param callId The callId to remove the extras from.
346     * @param keys The extra keys to remove.
347     */
348    public void removeExtras(String callId, List<String> keys) {
349        try {
350            mAdapter.removeExtras(callId, keys);
351        } catch (RemoteException ignored) {
352        }
353    }
354
355    /**
356     * Instructs Telecom to turn the proximity sensor on.
357     */
358    public void turnProximitySensorOn() {
359        try {
360            mAdapter.turnOnProximitySensor();
361        } catch (RemoteException ignored) {
362        }
363    }
364
365    /**
366     * Instructs Telecom to turn the proximity sensor off.
367     *
368     * @param screenOnImmediately If true, the screen will be turned on immediately if it was
369     * previously off. Otherwise, the screen will only be turned on after the proximity sensor
370     * is no longer triggered.
371     */
372    public void turnProximitySensorOff(boolean screenOnImmediately) {
373        try {
374            mAdapter.turnOffProximitySensor(screenOnImmediately);
375        } catch (RemoteException ignored) {
376        }
377    }
378}
379