Tizen 2.0 Release
[apps/osp/Phone.git] / src / PhnSoundManager.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    PhnSettingsMoreOptionsForm.cpp
19  * @brief   Sound manager class
20  */
21 #include "PhnSoundManager.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
31 static const wchar_t* RINGTONE_PATH = L"/opt/usr/apps/zktdpemtmw/shared/data/ringtone_sdk.mp3";
32
33 SoundManager::SoundManager(void)
34         : __pSoundCallSession(null)
35         , __pVibrator(null)
36         , __pPlayer(null)
37 {
38         __isSoundStatusOn = false;
39         __isVibrateStatusOn = false;
40 }
41
42 SoundManager::~SoundManager(void)
43 {
44         if(__pSoundCallSession != null)
45         {
46                 sound_manager_call_session_destroy(__pSoundCallSession);
47         }
48         if (__pVibrator != null)
49         {
50                 delete __pVibrator;
51         }
52         if (__pPlayer != null)
53         {
54                 delete __pPlayer;
55         }
56 }
57
58 result
59 SoundManager::StopSession(void)
60 {
61         AppLogDebug("Enter");
62         if(__pSoundCallSession != null)
63         {
64                 AppLogDebug("sound_manager_call_session_destroy");
65                 sound_manager_call_session_destroy(__pSoundCallSession);
66                 __pSoundCallSession = null;
67         }
68
69         return E_SUCCESS;
70 }
71
72 result
73 SoundManager::StartSession(void)
74 {
75         AppLogDebug("Enter");
76         if (__pSoundCallSession != null)
77         {
78                 sound_manager_call_session_set_mode(__pSoundCallSession,(sound_call_session_mode_e)SOUND_CALL_SESSION_MODE_VOICE);
79                 sound_manager_set_active_route((sound_route_e)SOUND_ROUTE_IN_MIC_OUT_RECEIVER);
80                 sound_manager_set_volume((sound_type_e)SOUND_TYPE_CALL,5);
81                 return E_SUCCESS;
82         }
83         int res = sound_manager_call_session_create((sound_call_session_type_e)SOUND_SESSION_TYPE_CALL, &__pSoundCallSession);
84         if (res != SOUND_MANAGER_ERROR_NONE)
85         {
86                 AppLogDebug("Sound manager creation failed");
87                 return E_FAILURE;
88         }
89         res = sound_manager_call_session_set_mode(__pSoundCallSession,(sound_call_session_mode_e)SOUND_CALL_SESSION_MODE_VOICE);
90         sound_manager_set_active_route((sound_route_e)SOUND_ROUTE_IN_MIC_OUT_RECEIVER);
91         //todo start listening for volume key and set the volume
92         sound_manager_set_volume((sound_type_e)SOUND_TYPE_CALL,5);
93         return E_SUCCESS;
94 }
95
96 result
97 SoundManager::SetSpeakerStatus(bool setSpeaker)
98 {
99         result r = E_FAILURE;
100         int res = -1;
101         bool isEarJackPresent = IsEarJackConnected();
102         sound_route_e soundRoute;
103         if (setSpeaker == true)
104         {
105                 soundRoute = SOUND_ROUTE_IN_MIC_OUT_SPEAKER;
106         }
107         else
108         {
109                 if (isEarJackPresent == true)
110                 {
111                         soundRoute = SOUND_ROUTE_IN_MIC_OUT_HEADPHONE;
112                 }
113                 else
114                 {
115                         soundRoute = SOUND_ROUTE_IN_MIC_OUT_RECEIVER;
116                 }
117         }
118         res = sound_manager_set_active_route(soundRoute);
119         if (res == SOUND_MANAGER_ERROR_NONE)
120         {
121                 r = E_SUCCESS;
122         }
123         else
124         {
125                 r = E_FAILURE;
126         }
127         return r;
128 }
129
130 result
131 SoundManager::SetSoundMode(SoundMode soundMode)
132 {
133         if (__pSoundCallSession == null)
134         {
135                 return E_FAILURE;
136         }
137         sound_call_session_mode_e sessionMode;
138         switch (soundMode)
139         {
140         case SOUND_MODE_RINGTONE:
141         {
142                 sessionMode = SOUND_CALL_SESSION_MODE_RINGTONE;
143                 break;
144         }
145         case SOUND_MODE_MEDIA:
146         {
147                 sessionMode = SOUND_CALL_SESSION_MODE_MEDIA;
148                 break;
149         }
150         case SOUND_MODE_VOICE:
151         {
152                 sessionMode = SOUND_CALL_SESSION_MODE_VOICE;
153         }
154         break;
155         default:
156         {
157                 sessionMode = SOUND_CALL_SESSION_MODE_RINGTONE;
158                 break;
159         }
160         }
161         int res = sound_manager_call_session_set_mode(__pSoundCallSession,sessionMode);
162         if (res != SOUND_MANAGER_ERROR_NONE)
163         {
164                 return E_FAILURE;
165         }
166         return E_SUCCESS;
167 }
168
169 void
170 SoundManager::StartAlert(String& contactRingTone)
171 {
172         AppLogDebug("Enter");
173         if(__pSoundCallSession != null)
174         {
175                 AppLogDebug("No Alert already in call");
176                 return;
177         }
178         else
179         {
180                 StartSession();
181         }
182         result r = E_FAILURE;
183         if(__pVibrator == null)
184         {
185                 __pVibrator = new (std::nothrow) Vibrator();
186                 r = __pVibrator->Construct();
187         }
188         if(__pPlayer == null)
189         {
190                 __pPlayer =  new (std::nothrow) Player();
191                 r = __pPlayer->Construct(*this);
192         }
193         int retVal = -1;
194         vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &retVal);
195         __isSoundStatusOn = retVal;
196         vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &retVal);
197         __isVibrateStatusOn = retVal;
198         //todo: do only if call alert mode is set
199         if (__isSoundStatusOn == true)
200         {
201                 String ringTonePath;
202                 //Check if contact has any custom ringtone, else play defautl ringtone.
203                 if(contactRingTone.IsEmpty() == true)
204                 {
205                         char* pRingTonePtr = vconf_get_str(VCONFKEY_SETAPPL_CALL_RINGTONE_PATH_STR);
206                         ringTonePath.Append(pRingTonePtr);
207                 }
208                 else
209                 {
210                         ringTonePath.Append(contactRingTone);
211                 }
212
213                 //Check if file is present
214                 File file;
215                 result res = file.Construct(ringTonePath, "r");
216                 if(res != E_SUCCESS )
217                 {
218                         ringTonePath.Clear();
219                         ringTonePath.Append(RINGTONE_PATH);
220                 }
221                 r = __pPlayer->OpenFile(ringTonePath,false);
222                 r = __pPlayer->SetLooping(true);
223                 r = __pPlayer->SetVolume(80);
224                 r = SetSoundMode(SOUND_MODE_VOICE);
225                 r = __pPlayer->Play();
226         }
227         if (__isVibrateStatusOn == true)
228         {
229                 //todo: Get the level from settings
230                 r = __pVibrator->Start(3000,1000,1000);
231         }
232         AppLogDebug("Exit");
233         return;
234 }
235
236 void
237 SoundManager::StopAlert(void)
238 {
239         AppLogDebug("Enter");
240         if (__isSoundStatusOn == true)
241         {
242                 //todo: stop player
243                 AppLogDebug("Stopping ring tone");
244                 __pPlayer->Stop();
245                 __pPlayer->Close();
246                 SetSoundMode(SOUND_MODE_VOICE);
247                 __isSoundStatusOn = false;
248                 StopSession();
249         }
250         if (__isVibrateStatusOn == true)
251         {
252                 __pVibrator->Stop();
253                 __isVibrateStatusOn = false;
254         }
255         AppLogDebug("Exit");
256 }
257
258 void
259 SoundManager::OnPlayerOpened(result r)
260 {
261 }
262
263 void
264 SoundManager::OnPlayerEndOfClip(void)
265 {
266 }
267
268 void
269 SoundManager::OnPlayerSeekCompleted(result r)
270 {
271 }
272
273 void
274 SoundManager::OnPlayerBuffering(int percent)
275 {
276 }
277
278 void
279 SoundManager::OnPlayerErrorOccurred(Tizen::Media::PlayerErrorReason r)
280 {
281 }
282
283 void
284 SoundManager::OnPlayerInterrupted(void)
285 {
286 }
287
288 void
289 SoundManager::OnPlayerReleased(void)
290 {
291 }
292
293 bool
294 SoundManager::IsEarJackConnected(void)
295 {
296         int earJackStatus = -1;
297         if (vconf_get_int(VCONFKEY_SYSMAN_EARJACK, &earJackStatus) == 0)
298         {
299                 if (earJackStatus == VCONFKEY_SYSMAN_EARJACK_REMOVED)
300                 {
301                         return false;
302                 }
303                 else
304                 {
305                         return true;
306                 }
307         }
308         else
309         {
310                 return false;
311         }
312 }