Prevent issue fix. 2Nabi issues fixed
[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         AppLogDebug("Enter");
239         int unknownRejectStatus = -1;
240         String disconnectTonePath=L"";
241         vconf_get_bool(VCONFKEY_CISSAPPL_CALL_END_TONE_BOOL, &unknownRejectStatus);
242         bool disconnectToneSetStatus = unknownRejectStatus;
243
244         if(disconnectToneSetStatus == true)
245         {
246                 AppLogDebug("disconnectToneSetStatus == true");
247                 disconnectTonePath.Append(CALLDISCONNECTTONE_PATH);
248                 if(__pPlayer == null)
249                 {
250                         AppLogDebug("__pPlayer == null");
251
252                         __pPlayer =  new (std::nothrow) Player();
253                         __pPlayer->Construct(*this);
254                         AppLogDebug("__pPlayer Construct");
255                 }
256                 if(disconnectTonePath.IsEmpty() == false)
257                 {
258                         AppLogDebug("__pPlayer OpenFile %ls",disconnectTonePath.GetPointer());
259                         __pPlayer->OpenFile(disconnectTonePath,false);
260                         AppLogDebug("__pPlayer OpenFile");
261                         __pPlayer->SetLooping(false);
262                         AppLogDebug("__pPlayer SetLooping");
263                         __pPlayer->SetVolume(80);
264                         AppLogDebug("__pPlayer SetVolume");
265                         SetSoundMode(SOUND_MODE_MEDIA);
266                         AppLogDebug("__pPlayer SetVolume");
267                         __pPlayer->Play();
268                 }
269         }
270         AppLogDebug("Exit");
271
272 }
273
274 void
275 SoundManager::SetMinuteReminderTone()
276 {
277
278         int unknownRejectStatus = -1;
279         vconf_get_bool(VCONFKEY_CISSAPPL_MINUTE_MINDER_BOOL, &unknownRejectStatus);
280         bool reminderToneSetStatus = unknownRejectStatus;
281
282         if(reminderToneSetStatus == true)
283         {
284                 __timer.StartAsRepeatable(60000);
285         }
286
287 }
288
289
290 void
291 SoundManager::SetConnectTone()
292 {
293
294         result r = E_FAILURE;
295         String connectTonePath;
296         int unknownRejectStatus = -1;
297         vconf_get_bool(VCONFKEY_CISSAPPL_CALL_CONNECT_TONE_BOOL, &unknownRejectStatus);
298         bool connectToneSetStatus = unknownRejectStatus;
299
300         connectTonePath.Append(CALLCONNECTTONE_PATH);
301
302         if(connectToneSetStatus == true)
303         {
304
305                 if(__pPlayer == null)
306                 {
307                         __pPlayer =  new (std::nothrow) Player();
308                         r = __pPlayer->Construct(*this);
309                 }
310                 if (connectTonePath.IsEmpty() == false)
311                 {
312
313                         r = __pPlayer->OpenFile(connectTonePath,false);
314                         r = __pPlayer->SetLooping(false);
315                         r = __pPlayer->SetVolume(80);
316                         r = SetSoundMode(SOUND_MODE_MEDIA);
317                         r = __pPlayer->Play();
318                 }
319         }
320 }
321
322 void
323 SoundManager::StartAlert(String& contactRingTone)
324 {
325         AppLogDebug("Enter");
326         if(__pSoundCallSession != null)
327         {
328                 CallAlertStatus status= CALL_ALERT_SOUND;
329                 int alertStatus = -1;
330                 int retVal = vconf_get_int(VCONFKEY_CISSAPPL_ALERT_ON_CALL_INT, &alertStatus);
331                 if (retVal == 0)
332                 {
333                         status = (CallAlertStatus) alertStatus;
334                 }
335                 AppLogDebug("No Alert already in call");
336                 if(status == CALL_ALERT_SOUND)
337                 {
338                         AppLogDebug("Playing alert tone");
339                         String waitingTonePath;
340                         waitingTonePath.Append(CALLWAITINGTONE_PATH);
341                         if(__pPlayer == null)
342                         {
343                                 __pPlayer =  new (std::nothrow) Player();
344                                 __pPlayer->Construct(*this);
345                         }
346                         if (waitingTonePath.IsEmpty() == false)
347                         {
348                                 __pPlayer->Close();
349                                 __pPlayer->OpenFile(waitingTonePath,false);
350                                 __pPlayer->SetLooping(true);
351                                 __pPlayer->SetVolume(80);
352                                 SetSoundMode(SOUND_MODE_MEDIA);
353                                 __pPlayer->Play();
354                         }
355                 }
356                 return;
357         }
358         else
359         {
360                 StartSession();
361         }
362         result r = E_FAILURE;
363         if(__pVibrator == null)
364         {
365                 __pVibrator = new (std::nothrow) Vibrator();
366                 r = __pVibrator->Construct();
367         }
368         if(__pPlayer == null)
369         {
370                 __pPlayer =  new (std::nothrow) Player();
371                 r = __pPlayer->Construct(*this);
372         }
373         int retVal = -1;
374         vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &retVal);
375         __isSoundStatusOn = retVal;
376         vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &retVal);
377         __isVibrateStatusOn = retVal;
378         //todo: do only if call alert mode is set
379         if (__isSoundStatusOn == true)
380         {
381                 String ringTonePath;
382                 //Check if contact has any custom ringtone, else play defautl ringtone.
383                 if(contactRingTone.IsEmpty() == true)
384                 {
385                         char* pRingTonePtr = vconf_get_str(VCONFKEY_SETAPPL_CALL_RINGTONE_PATH_STR);
386                         AppLog("Ringtone Path : %s",pRingTonePtr);
387                         ringTonePath.Append(pRingTonePtr);
388                 }
389                 else
390                 {
391                         AppLog("Ringtone Path : %ls",contactRingTone.GetPointer());
392                         ringTonePath.Append(contactRingTone);
393                 }
394
395                 //Check if file is present
396                 File file;
397                 result res = file.Construct(ringTonePath, "r");
398                 if(res != E_SUCCESS )
399                 {
400                         ringTonePath.Clear();
401                         ringTonePath.Append(RINGTONE_PATH);
402                 }
403                 __pPlayer->Close();
404                 r = __pPlayer->OpenFile(ringTonePath,false);
405                 r = __pPlayer->SetLooping(true);
406                 r = __pPlayer->SetVolume(80);
407                 r = SetSoundMode(SOUND_MODE_RINGTONE);
408                 r = __pPlayer->Play();
409         }
410         if (__isVibrateStatusOn == true)
411         {
412                 //todo: Get the level from settings
413                 r = __pVibrator->Start(3000,1000,1000);
414         }
415         AppLogDebug("Exit");
416         return;
417 }
418
419 void
420 SoundManager::StopAlert(void)
421 {
422         AppLogDebug("Enter");
423         if (__isSoundStatusOn == true)
424         {
425                 //todo: stop player
426                 AppLogDebug("Stopping ring tone");
427                 __pPlayer->Stop();
428                 __pPlayer->Close();
429                 SetSoundMode(SOUND_MODE_VOICE);
430                 __isSoundStatusOn = false;
431                 StopSession();
432         }
433         if (__isVibrateStatusOn == true)
434         {
435                 __pVibrator->Stop();
436                 __isVibrateStatusOn = false;
437         }
438         AppLogDebug("Exit");
439 }
440
441 void
442 SoundManager::OnPlayerOpened(result r)
443 {
444 }
445
446 void
447 SoundManager::OnPlayerEndOfClip(void)
448 {
449 }
450
451 void
452 SoundManager::OnPlayerSeekCompleted(result r)
453 {
454 }
455
456 void
457 SoundManager::OnPlayerBuffering(int percent)
458 {
459 }
460
461 void
462 SoundManager::OnPlayerErrorOccurred(Tizen::Media::PlayerErrorReason r)
463 {
464 }
465
466 void
467 SoundManager::OnPlayerInterrupted(void)
468 {
469 }
470
471 void
472 SoundManager::OnPlayerReleased(void)
473 {
474 }
475
476 bool
477 SoundManager::IsEarJackConnected(void)
478 {
479         int earJackStatus = -1;
480         if (vconf_get_int(VCONFKEY_SYSMAN_EARJACK, &earJackStatus) == 0)
481         {
482                 if (earJackStatus == VCONFKEY_SYSMAN_EARJACK_REMOVED)
483                 {
484                         return false;
485                 }
486                 else
487                 {
488                         return true;
489                 }
490         }
491         else
492         {
493                 return false;
494         }
495 }