1/*
2 * Copyright (C) 2013 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 com.android.incallui;
18
19import android.os.RemoteException;
20
21
22import com.android.services.telephony.common.AudioMode;
23import com.android.services.telephony.common.ICallCommandService;
24import com.android.services.telephony.common.Call;
25
26/**
27 * Main interface for phone related commands.
28 */
29public class CallCommandClient {
30
31    private static CallCommandClient sInstance;
32
33    public static synchronized CallCommandClient getInstance() {
34        if (sInstance == null) {
35            sInstance = new CallCommandClient();
36        }
37        return sInstance;
38    }
39
40    private ICallCommandService mCommandService;
41
42    private CallCommandClient() {
43    }
44
45    public void setService(ICallCommandService service) {
46        mCommandService = service;
47    }
48
49    public void answerCall(int callId) {
50        Log.i(this, "answerCall: " + callId);
51        if (mCommandService == null) {
52            Log.e(this, "Cannot answer call; CallCommandService == null");
53            return;
54        }
55        try {
56            mCommandService.answerCall(callId);
57        } catch (RemoteException e) {
58            Log.e(this, "Error answering call.", e);
59        }
60    }
61
62    public void rejectCall(Call call, boolean rejectWithMessage, String message) {
63        Log.i(this, "rejectCall: " + call.getCallId() +
64                ", with rejectMessage? " + rejectWithMessage);
65        if (mCommandService == null) {
66            Log.e(this, "Cannot reject call; CallCommandService == null");
67            return;
68        }
69        try {
70            mCommandService.rejectCall(call, rejectWithMessage, message);
71        } catch (RemoteException e) {
72            Log.e(this, "Error rejecting call.", e);
73        }
74    }
75
76    public void disconnectCall(int callId) {
77        Log.i(this, "disconnect Call: " + callId);
78        if (mCommandService == null) {
79            Log.e(this, "Cannot disconnect call; CallCommandService == null");
80            return;
81        }
82        try {
83            mCommandService.disconnectCall(callId);
84        } catch (RemoteException e) {
85            Log.e(this, "Error disconnecting call.", e);
86        }
87    }
88
89    public void separateCall(int callId) {
90        Log.i(this, "separate Call: " + callId);
91        if (mCommandService == null) {
92            Log.e(this, "Cannot separate call; CallCommandService == null");
93            return;
94        }
95        try {
96            mCommandService.separateCall(callId);
97        } catch (RemoteException e) {
98            Log.e(this, "Error separating call.", e);
99        }
100    }
101
102    public void mute(boolean onOff) {
103        Log.i(this, "mute: " + onOff);
104        if (mCommandService == null) {
105            Log.e(this, "Cannot mute call; CallCommandService == null");
106            return;
107        }
108        try {
109            mCommandService.mute(onOff);
110        } catch (RemoteException e) {
111            Log.e(this, "Error muting phone.", e);
112        }
113    }
114
115    public void hold(int callId, boolean onOff) {
116        Log.i(this, "hold call(" + onOff + "): " + callId);
117        if (mCommandService == null) {
118            Log.e(this, "Cannot hold call; CallCommandService == null");
119            return;
120        }
121        try {
122            mCommandService.hold(callId, onOff);
123        } catch (RemoteException e) {
124            Log.e(this, "Error holding call.", e);
125        }
126    }
127
128    public void merge() {
129        Log.i(this, "merge calls");
130        if (mCommandService == null) {
131            Log.e(this, "Cannot merge call; CallCommandService == null");
132            return;
133        }
134        try {
135            mCommandService.merge();
136        } catch (RemoteException e) {
137            Log.e(this, "Error merging calls.", e);
138        }
139    }
140
141    public void swap() {
142        Log.i(this, "swap active/hold calls");
143        if (mCommandService == null) {
144            Log.e(this, "Cannot swap call; CallCommandService == null");
145            return;
146        }
147        try {
148            mCommandService.swap();
149        } catch (RemoteException e) {
150            Log.e(this, "Error merging calls.", e);
151        }
152    }
153
154    public void addCall() {
155        Log.i(this, "add a new call");
156        if (mCommandService == null) {
157            Log.e(this, "Cannot add call; CallCommandService == null");
158            return;
159        }
160        try {
161            mCommandService.addCall();
162        } catch (RemoteException e) {
163            Log.e(this, "Error merging calls.", e);
164        }
165    }
166
167    public void setAudioMode(int mode) {
168        Log.i(this, "Set Audio Mode: " + AudioMode.toString(mode));
169        if (mCommandService == null) {
170            Log.e(this, "Cannot set audio mode; CallCommandService == null");
171            return;
172        }
173        try {
174            mCommandService.setAudioMode(mode);
175        } catch (RemoteException e) {
176            Log.e(this, "Error setting speaker.", e);
177        }
178    }
179
180    public void playDtmfTone(char digit, boolean timedShortTone) {
181        if (mCommandService == null) {
182            Log.e(this, "Cannot start dtmf tone; CallCommandService == null");
183            return;
184        }
185        try {
186            Log.v(this, "Sending dtmf tone " + digit);
187            mCommandService.playDtmfTone(digit, timedShortTone);
188        } catch (RemoteException e) {
189            Log.e(this, "Error setting speaker.", e);
190        }
191
192    }
193
194    public void stopDtmfTone() {
195        if (mCommandService == null) {
196            Log.e(this, "Cannot stop dtmf tone; CallCommandService == null");
197            return;
198        }
199        try {
200            Log.v(this, "Stop dtmf tone ");
201            mCommandService.stopDtmfTone();
202        } catch (RemoteException e) {
203            Log.e(this, "Error setting speaker.", e);
204        }
205    }
206
207    public void postDialWaitContinue(int callId) {
208        if (mCommandService == null) {
209            Log.e(this, "Cannot postDialWaitContinue(); CallCommandService == null");
210            return;
211        }
212        try {
213            Log.v(this, "postDialWaitContinue()");
214            mCommandService.postDialWaitContinue(callId);
215        } catch (RemoteException e) {
216            Log.e(this, "Error on postDialWaitContinue().", e);
217        }
218    }
219
220    public void postDialCancel(int callId) {
221        if (mCommandService == null) {
222            Log.e(this, "Cannot postDialCancel(); CallCommandService == null");
223            return;
224        }
225        try {
226            Log.v(this, "postDialCancel()");
227            mCommandService.postDialCancel(callId);
228        } catch (RemoteException e) {
229            Log.e(this, "Error on postDialCancel().", e);
230        }
231    }
232
233    public void setSystemBarNavigationEnabled(boolean enable) {
234        if (mCommandService == null) {
235            Log.e(this, "Cannot setSystemBarNavigationEnabled(); CallCommandService == null");
236            return;
237        }
238        try {
239            Log.v(this, "setSystemBarNavigationEnabled() enabled = " + enable);
240            mCommandService.setSystemBarNavigationEnabled(enable);
241        } catch (RemoteException e) {
242            Log.d(this, "Error on setSystemBarNavigationEnabled().");
243        }
244    }
245
246}
247