1.Disconnecting call when no network 2.Support for Automatic:4dir
[apps/osp/Call.git] / src / CallSoundManager.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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
17 /**
18  * @file    CallSettingsMoreOptionsForm.cpp
19  * @brief   Sound manager class
20  */
21 #include "CallSoundManager.h"
22
23 using namespace Tizen::Base;
24 using namespace Tizen::Graphics;
25 using namespace Tizen::Io;
26 using namespace Tizen::Media;
27 using namespace Tizen::Social;
28 using namespace Tizen::System;
29 using namespace Tizen::Base::Collection;
30 using namespace Tizen::Base::Runtime;
31
32 static const wchar_t* RINGTONE_PATH = L"/opt/usr/apps/zktdpemtmw/shared/data/ringtone_sdk.mp3";
33 static const wchar_t* CALLCONNECTTONE_PATH = L"/opt/usr/apps/zktdpemtmw/shared/data/Call_Connect.ogg";
34 static const wchar_t* CALLDISCONNECTTONE_PATH = L"/opt/usr/apps/zktdpemtmw/shared/data/Call_Disconnect.ogg";
35 static const wchar_t* MINUTEREMINDERTONE_PATH = L"/opt/usr/apps/zktdpemtmw/shared/data/Call_RecordStart.wav";
36 static const wchar_t* CALLWAITINGTONE_PATH = L"/opt/usr/apps/zktdpemtmw/shared/data/Call_WaitingTone.wav";
37
38
39 SoundManager::SoundManager(void)
40         : __pSoundCallSession(null)
41         , __pVibrator(null)
42         , __pPlayer(null)
43         , __lastEndedConferenceCall(false)
44 {
45         __timer.Construct(*this);
46         __isSoundStatusOn = false;
47         __isVibrateStatusOn = false;
48 }
49
50 SoundManager::~SoundManager(void)
51 {
52         if(__pSoundCallSession != null)
53         {
54                 sound_manager_call_session_destroy(__pSoundCallSession);
55         }
56         if (__pVibrator != null)
57         {
58                 delete __pVibrator;
59         }
60         if (__pPlayer != null)
61         {
62                 delete __pPlayer;
63         }
64
65 }
66
67 void
68 SoundManager::SetlastEndedConferenceCall()
69 {
70         __lastEndedConferenceCall = true;
71 }
72
73 bool
74 SoundManager::GetLastConferenceCall()
75 {
76         return __lastEndedConferenceCall;
77 }
78
79 Timer*
80 SoundManager::GetTimer()
81 {
82         return &__timer;
83 }
84
85 result
86 SoundManager::StopSession(void)
87 {
88         AppLogDebug("Enter");
89         if(__pSoundCallSession != null)
90         {
91                 AppLogDebug("sound_manager_call_session_destroy");
92                 sound_manager_call_session_destroy(__pSoundCallSession);
93                 __pSoundCallSession = null;
94         }
95
96         return E_SUCCESS;
97 }
98
99 result
100 SoundManager::StartSession(void)
101 {
102         AppLogDebug("Enter");
103         if (__pSoundCallSession != null)
104         {
105                 sound_manager_call_session_set_mode(__pSoundCallSession,(sound_call_session_mode_e)SOUND_CALL_SESSION_MODE_VOICE);
106                 sound_manager_set_active_route((sound_route_e)SOUND_ROUTE_IN_MIC_OUT_RECEIVER);
107                 sound_manager_set_volume((sound_type_e)SOUND_TYPE_CALL,5);
108                 return E_SUCCESS;
109         }
110         int res = sound_manager_call_session_create((sound_call_session_type_e)SOUND_SESSION_TYPE_CALL, &__pSoundCallSession);
111         if (res != SOUND_MANAGER_ERROR_NONE)
112         {
113                 AppLogDebug("Sound manager creation failed");
114                 return E_FAILURE;
115         }
116         res = sound_manager_call_session_set_mode(__pSoundCallSession,(sound_call_session_mode_e)SOUND_CALL_SESSION_MODE_VOICE);
117         sound_manager_set_active_route((sound_route_e)SOUND_ROUTE_IN_MIC_OUT_RECEIVER);
118         //todo start listening for volume key and set the volume
119         sound_manager_set_volume((sound_type_e)SOUND_TYPE_CALL,5);
120         return E_SUCCESS;
121 }
122
123 result
124 SoundManager::SetSpeakerStatus(bool setSpeaker)
125 {
126         result r = E_FAILURE;
127         int res = -1;
128         bool isEarJackPresent = IsEarJackConnected();
129         sound_route_e soundRoute;
130         if (setSpeaker == true)
131         {
132                 soundRoute = SOUND_ROUTE_IN_MIC_OUT_SPEAKER;
133         }
134         else
135         {
136                 if (isEarJackPresent == true)
137                 {
138                         soundRoute = SOUND_ROUTE_IN_MIC_OUT_HEADPHONE;
139                 }
140                 else
141                 {
142                         soundRoute = SOUND_ROUTE_IN_MIC_OUT_RECEIVER;
143                 }
144         }
145         res = sound_manager_set_active_route(soundRoute);
146         if (res == SOUND_MANAGER_ERROR_NONE)
147         {
148                 r = E_SUCCESS;
149         }
150         else
151         {
152                 r = E_FAILURE;
153         }
154         return r;
155 }
156
157 result
158 SoundManager::SetSoundMode(SoundMode soundMode)
159 {
160         if (__pSoundCallSession == null)
161         {
162                 return E_FAILURE;
163         }
164         sound_call_session_mode_e sessionMode;
165         switch (soundMode)
166         {
167         case SOUND_MODE_RINGTONE:
168         {
169                 sessionMode = SOUND_CALL_SESSION_MODE_RINGTONE;
170                 break;
171         }
172         case SOUND_MODE_MEDIA:
173         {
174                 sessionMode = SOUND_CALL_SESSION_MODE_MEDIA;
175                 break;
176         }
177         case SOUND_MODE_VOICE:
178         {
179                 sessionMode = SOUND_CALL_SESSION_MODE_VOICE;
180         }
181         break;
182         default:
183         {
184                 sessionMode = SOUND_CALL_SESSION_MODE_RINGTONE;
185                 break;
186         }
187         }
188         int res = sound_manager_call_session_set_mode(__pSoundCallSession,sessionMode);
189         if (res != SOUND_MANAGER_ERROR_NONE)
190         {
191                 return E_FAILURE;
192         }
193         return E_SUCCESS;
194 }
195
196 void
197 SoundManager::OnTimerExpired(Timer& timer)
198 {
199         String reminderTonePath;
200         int unknownRejectStatus = -1;
201                 vconf_get_bool(VCONFKEY_CISSAPPL_MINUTE_MINDER_BOOL, &unknownRejectStatus);
202                 bool ReminderToneSetStatus = unknownRejectStatus;
203
204         if(ReminderToneSetStatus == true)
205         {
206
207                 if(__pPlayer == null)
208                 {
209                         __pPlayer =  new (std::nothrow) Player();
210                         __pPlayer->Construct(*this);
211                 }
212
213                 reminderTonePath.Append(MINUTEREMINDERTONE_PATH);
214                 if(reminderTonePath.IsEmpty() == false)
215                 {
216                         __pPlayer->Close();
217                         __pPlayer->OpenFile(reminderTonePath,false);
218                         __pPlayer->SetLooping(false);
219                         __pPlayer->SetVolume(80);
220                         SetSoundMode(SOUND_MODE_MEDIA);
221                         __pPlayer->Play();
222                         SetSoundMode(SOUND_MODE_VOICE);
223                 }
224         }
225         /*else if(ReminderToneSetStatus == false)
226         {
227                 timer.Cancel();
228         }*/
229
230 }
231
232
233
234 void
235 SoundManager::SetDisconnectTone(void)
236 {
237
238         int unknownRejectStatus = -1;
239         String disconnectTonePath;
240         vconf_get_bool(VCONFKEY_CISSAPPL_CALL_END_TONE_BOOL, &unknownRejectStatus);
241         bool disconnectToneSetStatus = unknownRejectStatus;
242
243         if(disconnectToneSetStatus == true)
244         {
245                 disconnectTonePath.Append(CALLDISCONNECTTONE_PATH);
246                 if(__pPlayer == null)
247                 {
248                         __pPlayer =  new (std::nothrow) Player();
249                         __pPlayer->Construct(*this);
250                 }
251                 if(disconnectTonePath.IsEmpty() == false)
252                 {
253                         __pPlayer->Close();
254                         __pPlayer->OpenFile(disconnectTonePath,false);
255                         __pPlayer->SetLooping(false);
256                         __pPlayer->SetVolume(80);
257                         SetSoundMode(SOUND_MODE_MEDIA);
258                         __pPlayer->Play();
259                 }
260         }
261
262 }
263
264 void
265 SoundManager::SetMinuteReminderTone()
266 {
267
268         int unknownRejectStatus = -1;
269         vconf_get_bool(VCONFKEY_CISSAPPL_MINUTE_MINDER_BOOL, &unknownRejectStatus);
270         bool reminderToneSetStatus = unknownRejectStatus;
271
272         if(reminderToneSetStatus == true)
273         {
274                 __timer.StartAsRepeatable(60000);
275         }
276
277 }
278
279
280 void
281 SoundManager::SetConnectTone()
282 {
283
284         result r = E_FAILURE;
285         String connectTonePath;
286         int unknownRejectStatus = -1;
287         vconf_get_bool(VCONFKEY_CISSAPPL_CALL_CONNECT_TONE_BOOL, &unknownRejectStatus);
288         bool connectToneSetStatus = unknownRejectStatus;
289
290         connectTonePath.Append(CALLCONNECTTONE_PATH);
291
292         if(connectToneSetStatus == true)
293         {
294
295                 if(__pPlayer == null)
296                 {
297                         __pPlayer =  new (std::nothrow) Player();
298                         r = __pPlayer->Construct(*this);
299                 }
300                 if (connectTonePath.IsEmpty() == false)
301                 {
302
303                         r = __pPlayer->OpenFile(connectTonePath,false);
304                         r = __pPlayer->SetLooping(false);
305                         r = __pPlayer->SetVolume(80);
306                         r = SetSoundMode(SOUND_MODE_MEDIA);
307                         r = __pPlayer->Play();
308                 }
309         }
310 }
311
312 void
313 SoundManager::StartAlert(String& contactRingTone)
314 {
315         AppLogDebug("Enter");
316         if(__pSoundCallSession != null)
317         {
318                 AppLogDebug("No Alert already in call");
319                 String waitingTonePath;
320                 waitingTonePath.Append(CALLWAITINGTONE_PATH);
321                 if(__pPlayer == null)
322                 {
323                         __pPlayer =  new (std::nothrow) Player();
324                         __pPlayer->Construct(*this);
325                 }
326                 if (waitingTonePath.IsEmpty() == false)
327                 {
328                         __pPlayer->Close();
329                         __pPlayer->OpenFile(waitingTonePath,false);
330                         __pPlayer->SetLooping(true);
331                         __pPlayer->SetVolume(80);
332                         SetSoundMode(SOUND_MODE_MEDIA);
333                         __pPlayer->Play();
334                 }
335                 return;
336         }
337         else
338         {
339                 StartSession();
340         }
341         result r = E_FAILURE;
342         if(__pVibrator == null)
343         {
344                 __pVibrator = new (std::nothrow) Vibrator();
345                 r = __pVibrator->Construct();
346         }
347         if(__pPlayer == null)
348         {
349                 __pPlayer =  new (std::nothrow) Player();
350                 r = __pPlayer->Construct(*this);
351         }
352         int retVal = -1;
353         vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &retVal);
354         __isSoundStatusOn = retVal;
355         vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &retVal);
356         __isVibrateStatusOn = retVal;
357         //todo: do only if call alert mode is set
358         if (__isSoundStatusOn == true)
359         {
360                 String ringTonePath;
361                 //Check if contact has any custom ringtone, else play defautl ringtone.
362                 if(contactRingTone.IsEmpty() == true)
363                 {
364                         char* pRingTonePtr = vconf_get_str(VCONFKEY_SETAPPL_CALL_RINGTONE_PATH_STR);
365                         AppLog("Ringtone Path : %s",pRingTonePtr);
366                         ringTonePath.Append(pRingTonePtr);
367                 }
368                 else
369                 {
370                         AppLog("Ringtone Path : %ls",contactRingTone.GetPointer());
371                         ringTonePath.Append(contactRingTone);
372                 }
373
374                 //Check if file is present
375                 File file;
376                 result res = file.Construct(ringTonePath, "r");
377                 if(res != E_SUCCESS )
378                 {
379                         ringTonePath.Clear();
380                         ringTonePath.Append(RINGTONE_PATH);
381                 }
382                 __pPlayer->Close();
383                 r = __pPlayer->OpenFile(ringTonePath,false);
384                 r = __pPlayer->SetLooping(true);
385                 r = __pPlayer->SetVolume(80);
386                 r = SetSoundMode(SOUND_MODE_RINGTONE);
387                 r = __pPlayer->Play();
388         }
389         if (__isVibrateStatusOn == true)
390         {
391                 //todo: Get the level from settings
392                 r = __pVibrator->Start(3000,1000,1000);
393         }
394         AppLogDebug("Exit");
395         return;
396 }
397
398 void
399 SoundManager::StopAlert(void)
400 {
401         AppLogDebug("Enter");
402         if (__isSoundStatusOn == true)
403         {
404                 //todo: stop player
405                 AppLogDebug("Stopping ring tone");
406                 __pPlayer->Stop();
407                 __pPlayer->Close();
408                 SetSoundMode(SOUND_MODE_VOICE);
409                 __isSoundStatusOn = false;
410                 StopSession();
411         }
412         if (__isVibrateStatusOn == true)
413         {
414                 __pVibrator->Stop();
415                 __isVibrateStatusOn = false;
416         }
417         AppLogDebug("Exit");
418 }
419
420 void
421 SoundManager::OnPlayerOpened(result r)
422 {
423 }
424
425 void
426 SoundManager::OnPlayerEndOfClip(void)
427 {
428 }
429
430 void
431 SoundManager::OnPlayerSeekCompleted(result r)
432 {
433 }
434
435 void
436 SoundManager::OnPlayerBuffering(int percent)
437 {
438 }
439
440 void
441 SoundManager::OnPlayerErrorOccurred(Tizen::Media::PlayerErrorReason r)
442 {
443 }
444
445 void
446 SoundManager::OnPlayerInterrupted(void)
447 {
448 }
449
450 void
451 SoundManager::OnPlayerReleased(void)
452 {
453 }
454
455 bool
456 SoundManager::IsEarJackConnected(void)
457 {
458         int earJackStatus = -1;
459         if (vconf_get_int(VCONFKEY_SYSMAN_EARJACK, &earJackStatus) == 0)
460         {
461                 if (earJackStatus == VCONFKEY_SYSMAN_EARJACK_REMOVED)
462                 {
463                         return false;
464                 }
465                 else
466                 {
467                         return true;
468                 }
469         }
470         else
471         {
472                 return false;
473         }
474 }