e8b76bc34e6245c6ca8f8b9f80e3de7e78d7f2b3
[apps/osp/VideoPlayer.git] / src / VpSoundPathPopup.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                VpSoundPathPopup.cpp
19  * @brief               This is the header file for SoundPathPopup class.
20  */
21
22 #include "VpSoundPathPopup.h"
23 #include "VpTypes.h"
24 #include "VpVideoPlayerForm.h"
25
26 using namespace Tizen::App;
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Collection;
29 using namespace Tizen::Base::Runtime;
30 using namespace Tizen::Graphics;
31 using namespace Tizen::Media;
32 using namespace Tizen::Ui;
33 using namespace Tizen::Ui::Controls;
34
35 static const int W_SOUNDPATH_POPUP = 550;
36 static const int H_SOUNDPATH_POPUP = 450;
37
38 static const int X_CHECK_BUTTON_1ST = 0;
39 static const int Y_CHECK_BUTTON_1ST = 0;
40 static const int W_CHECK_BUTTON_1ST = 510;
41 static const int H_CHECK_BUTTON_1ST = 90;
42
43 static const int X_CHECK_BUTTON_2ND = 0;
44 static const int Y_CHECK_BUTTON_2ND = 90;
45 static const int W_CHECK_BUTTON_2ND = 510;
46 static const int H_CHECK_BUTTON_2ND = 90;
47
48 static const int X_CLOSE_BUTTON = 65;
49 static const int Y_CLOSE_BUTTON = 210;
50 static const int W_CLOSE_BUTTON = 400;
51 static const int H_CLOSE_BUTTON = 90;
52
53 static const RequestId REQUEST_UPDATE_WIRED_ACCESSORY_AND_SPEAKER_MENU = 111;
54 static const RequestId REQUEST_UPDATE_BLUETOOTHA2DP_AND_SPEAKER_MENU = 112;
55 static const RequestId REQUEST_UPDATE_BLUETOOTHA2DP_AND_WIRED_ACCESSORY_MENU = 113;
56
57 SoundPathPopup::SoundPathPopup(void)
58         : __pCloseButton(null)
59         , __pCheckButton1st(null)
60         , __pCheckButton2nd(null)
61         , __audioRouteMode(0)
62 {
63 }
64
65 SoundPathPopup::~SoundPathPopup(void)
66 {
67 }
68
69 result
70 SoundPathPopup::Construct(int currentOutputDevice, String bluetoothA2dpName, int audioRouteMode)
71 {
72         result r = E_FAILURE;
73         OutputAudioDevice tempAudioDevice;
74
75         String checkBoxString;
76
77         AppResource* pAppResource = Application::GetInstance()->GetAppResource();
78
79         r = Popup::Construct(true, Dimension(W_SOUNDPATH_POPUP, H_SOUNDPATH_POPUP));
80         TryCatch(r == E_SUCCESS, , "Popup::Construct() Failed:%s", GetErrorMessage(r));
81
82         __pCheckButton1st = new (std::nothrow) CheckButton();
83         __pCheckButton2nd = new (std::nothrow) CheckButton();
84         __pCloseButton = new (std::nothrow) Button();
85
86         r = pAppResource->GetString(IDS_COM_HEADER_AUDIO_DEVICE_ABB, checkBoxString);
87         TryCatch(r == E_SUCCESS, , "pAppResource->GetString Failed:%s", GetErrorMessage(r));
88
89         r = SetTitleText(checkBoxString);
90         TryCatch(r == E_SUCCESS, , "SetTitleText() Failed:%s", GetErrorMessage(r));
91
92         tempAudioDevice = static_cast<OutputAudioDevice>(currentOutputDevice);
93
94         switch (audioRouteMode)
95         {
96         case AUDIO_ROUTE_BT_A2DP_AND_SPEAKER:
97                 {
98                         r = __pCheckButton1st->Construct(
99                                         Rectangle(X_CHECK_BUTTON_1ST, Y_CHECK_BUTTON_1ST, W_CHECK_BUTTON_1ST, H_CHECK_BUTTON_1ST),
100                                         CHECK_BUTTON_STYLE_ONOFF, BACKGROUND_STYLE_DEFAULT, false, bluetoothA2dpName);
101                         TryCatch(r == E_SUCCESS, , "__pCheckButton1st->Construct() Failed:%s", GetErrorMessage(r));
102
103                         r = pAppResource->GetString(IDS_COM_OPT_SPEAKER_ABB, checkBoxString);
104                         TryCatch(r == E_SUCCESS, , "pAppResource->GetString() Failed:%s", GetErrorMessage(r));
105
106                         r = __pCheckButton2nd->Construct(
107                                         Rectangle(X_CHECK_BUTTON_2ND, Y_CHECK_BUTTON_2ND, W_CHECK_BUTTON_2ND, H_CHECK_BUTTON_2ND),
108                                         CHECK_BUTTON_STYLE_ONOFF, BACKGROUND_STYLE_DEFAULT, false, checkBoxString);
109                         TryCatch(r == E_SUCCESS, , "__pCheckButton2nd->Construct() Failed:%s", GetErrorMessage(r));
110
111                         if (tempAudioDevice == OUTPUT_AUDIO_DEVICE_BT_A2DP)
112                         {
113                                 __pCheckButton1st->SetSelected(true);
114                                 __pCheckButton2nd->SetSelected(false);
115                         }
116                         else if (tempAudioDevice == OUTPUT_AUDIO_DEVICE_SPEAKER)
117                         {
118                                 __pCheckButton1st->SetSelected(false);
119                                 __pCheckButton2nd->SetSelected(true);
120                         }
121                         __audioRouteMode = audioRouteMode;
122                 }
123                 break;
124
125         case AUDIO_ROUTE_WIRED_ACCESSORY_AND_SPEAKER:
126                 {
127                         r = pAppResource->GetString(IDS_COM_OPT_HEADPHONES_ABB, checkBoxString);
128                         TryCatch(r == E_SUCCESS, , "pAppResource->GetString() Failed:%s", GetErrorMessage(r));
129
130                         r = __pCheckButton1st->Construct(
131                                         Rectangle(X_CHECK_BUTTON_1ST, Y_CHECK_BUTTON_1ST, W_CHECK_BUTTON_1ST, H_CHECK_BUTTON_1ST),
132                                         CHECK_BUTTON_STYLE_ONOFF, BACKGROUND_STYLE_DEFAULT, false, checkBoxString);
133                         TryCatch(r == E_SUCCESS, , "__pCheckButton1st->Construct() Failed:%s", GetErrorMessage(r));
134
135                         r = pAppResource->GetString(IDS_COM_OPT_SPEAKER_ABB, checkBoxString);
136                         TryCatch(r == E_SUCCESS, , "pAppResource->GetString() Failed:%s", GetErrorMessage(r));
137
138                         r = __pCheckButton2nd->Construct(
139                                         Rectangle(X_CHECK_BUTTON_2ND, Y_CHECK_BUTTON_2ND, W_CHECK_BUTTON_2ND, H_CHECK_BUTTON_2ND),
140                                         CHECK_BUTTON_STYLE_ONOFF, BACKGROUND_STYLE_DEFAULT, false, checkBoxString);
141                         TryCatch(r == E_SUCCESS, , "__pCheckButton2nd->Construct() Failed:%s", GetErrorMessage(r));
142
143                         if (tempAudioDevice == OUTPUT_AUDIO_DEVICE_WIRED_ACCESSORY)
144                         {
145                                 __pCheckButton1st->SetSelected(true);
146                                 __pCheckButton2nd->SetSelected(false);
147                         }
148                         else if (tempAudioDevice == OUTPUT_AUDIO_DEVICE_SPEAKER)
149                         {
150                                 __pCheckButton1st->SetSelected(false);
151                                 __pCheckButton2nd->SetSelected(true);
152                         }
153                         __audioRouteMode = audioRouteMode;
154                 }
155                 break;
156
157         case AUDIO_ROUTE_BT_A2DP_AND_WIRED_ACCESSORY:
158                 {
159                         r = __pCheckButton1st->Construct(
160                                         Rectangle(X_CHECK_BUTTON_1ST, Y_CHECK_BUTTON_1ST, W_CHECK_BUTTON_1ST, H_CHECK_BUTTON_1ST),
161                                         CHECK_BUTTON_STYLE_ONOFF, BACKGROUND_STYLE_DEFAULT, false, bluetoothA2dpName);
162                         TryCatch(r == E_SUCCESS, , "__pCheckButton1st->Construct() Failed:%s", GetErrorMessage(r));
163
164                         r = pAppResource->GetString(IDS_COM_OPT_HEADPHONES_ABB, checkBoxString);
165                         TryCatch(r == E_SUCCESS, , "pAppResource->GetString() Failed:%s", GetErrorMessage(r));
166
167                         r = __pCheckButton2nd->Construct(
168                                         Rectangle(X_CHECK_BUTTON_2ND, Y_CHECK_BUTTON_2ND, W_CHECK_BUTTON_2ND, H_CHECK_BUTTON_2ND),
169                                         CHECK_BUTTON_STYLE_ONOFF, BACKGROUND_STYLE_DEFAULT, false, checkBoxString);
170                         TryCatch(r == E_SUCCESS, , "__pCheckButton2nd->Construct() Failed:%s", GetErrorMessage(r));
171
172                         if (tempAudioDevice == OUTPUT_AUDIO_DEVICE_BT_A2DP)
173                         {
174                                 __pCheckButton1st->SetSelected(true);
175                                 __pCheckButton2nd->SetSelected(false);
176                         }
177                         else if (tempAudioDevice == OUTPUT_AUDIO_DEVICE_WIRED_ACCESSORY)
178                         {
179                                 __pCheckButton1st->SetSelected(false);
180                                 __pCheckButton2nd->SetSelected(true);
181                         }
182                         __audioRouteMode = audioRouteMode;
183                 }
184                 break;
185
186         default:
187                 break;
188         }
189
190         r = pAppResource->GetString(IDS_COM_POP_CLOSE, checkBoxString);
191         TryCatch(r == E_SUCCESS, , "pAppResource->GetString() Failed:%s", GetErrorMessage(r));
192
193         r = __pCloseButton->Construct(
194                         Rectangle(X_CLOSE_BUTTON, Y_CLOSE_BUTTON, W_CLOSE_BUTTON, H_CLOSE_BUTTON), checkBoxString);
195         TryCatch(r == E_SUCCESS, , "__pCloseButton->Construct() Failed:%s", GetErrorMessage(r));
196
197         __pCheckButton1st->SetActionId(IDA_BUTTON_CHECK1ST_CHECKED, IDA_BUTTON_CHECK1ST_UNCHECKED);
198         __pCheckButton2nd->SetActionId(IDA_BUTTON_CHECK2ND_CHECKED, IDA_BUTTON_CHECK2ND_UNCHECKED);
199         __pCloseButton->SetActionId(IDA_BUTTON_CLOSE);
200
201         __pCheckButton1st->AddActionEventListener(*this);
202         __pCheckButton2nd->AddActionEventListener(*this);
203         __pCloseButton->AddActionEventListener(*this);
204
205         r = AddControl(*__pCheckButton1st);
206         TryCatch(r == E_SUCCESS, , "AddControl(*__pCheckButton1st) Failed:%s", GetErrorMessage(r));
207
208         r = AddControl(*__pCheckButton2nd);
209         TryCatch(r == E_SUCCESS, , "AddControl(*__pCheckButton2nd) Failed:%s", GetErrorMessage(r));
210
211         r = AddControl(*__pCloseButton);
212         TryCatch(r == E_SUCCESS, , "AddControl(*__CloseButton) Failed:%s", GetErrorMessage(r));
213
214         return r;
215
216 CATCH:
217         delete __pCheckButton1st;
218         __pCheckButton1st = null;
219
220         delete __pCheckButton2nd;
221         __pCheckButton2nd = null;
222
223         delete __pCloseButton;
224         __pCloseButton = null;
225
226         return r;
227 }
228
229 void
230 SoundPathPopup::OnActionPerformed(const Control& source, int actionId)
231 {
232         result r = E_FAILURE;
233         Frame* pFrame = null;
234         VideoPlayerForm* mainForm = null;
235
236         AudioRouteInfo currentAudioRoute(INPUT_AUDIO_DEVICE_NONE, OUTPUT_AUDIO_DEVICE_NONE);
237         AudioRouteInfo audioRouteSpeaker(INPUT_AUDIO_DEVICE_NONE, OUTPUT_AUDIO_DEVICE_SPEAKER);
238         AudioRouteInfo audioRouteWiredAccessory(INPUT_AUDIO_DEVICE_NONE, OUTPUT_AUDIO_DEVICE_WIRED_ACCESSORY);
239         AudioRouteInfo audioRouteBluetooth(INPUT_AUDIO_DEVICE_NONE, OUTPUT_AUDIO_DEVICE_BT_A2DP);
240
241         pFrame = UiApp::GetInstance()->GetAppFrame()->GetFrame();
242         mainForm = static_cast<VideoPlayerForm*>(pFrame->GetCurrentForm());
243
244         switch (actionId)
245         {
246         case IDA_BUTTON_CHECK1ST_CHECKED:
247                 // fall through
248         case IDA_BUTTON_CHECK1ST_UNCHECKED:
249                 {
250                         if (__audioRouteMode == AUDIO_ROUTE_SPEAKER_ONLY)
251                         {
252                                 AppLogDebug("AUDIO_ROUTE_SPEAKER_ONLY");
253                         }
254                         else if (__audioRouteMode == AUDIO_ROUTE_BT_A2DP_AND_SPEAKER)
255                         {
256                                 if (currentAudioRoute.GetOutputAudioDevice() != OUTPUT_AUDIO_DEVICE_BT_A2DP)
257                                 {
258                                         r = mainForm->SetActiveAudioRoute(audioRouteBluetooth);
259                                         TryReturnVoid(r == E_SUCCESS, "__pAudioRouteManager->SetActiveAudioRoute() failed:%s",
260                                                         GetErrorMessage(r));
261                                 }
262                         }
263                         else if (__audioRouteMode == AUDIO_ROUTE_WIRED_ACCESSORY_AND_SPEAKER)
264                         {
265                                 if (currentAudioRoute.GetOutputAudioDevice() != OUTPUT_AUDIO_DEVICE_WIRED_ACCESSORY)
266                                 {
267                                         r = mainForm->SetActiveAudioRoute(audioRouteWiredAccessory);
268                                         TryReturnVoid(r == E_SUCCESS, "__pAudioRouteManager->SetActiveAudioRoute() failed:%s",
269                                                         GetErrorMessage(r));
270                                 }
271                         }
272                         else
273                         {
274                                 if (currentAudioRoute.GetOutputAudioDevice() != OUTPUT_AUDIO_DEVICE_BT_A2DP)
275                                 {
276                                         r = mainForm->SetActiveAudioRoute(audioRouteBluetooth);
277                                         TryReturnVoid(r == E_SUCCESS, "__pAudioRouteManager->SetActiveAudioRoute() failed:%s",
278                                                         GetErrorMessage(r));
279                                 }
280                         }
281
282                         __pCheckButton1st->SetSelected(true);
283                         __pCheckButton2nd->SetSelected(false);
284
285                         SetShowState(false);
286                 }
287                 break;
288
289         case IDA_BUTTON_CHECK2ND_CHECKED:
290                 // fall through
291         case IDA_BUTTON_CHECK2ND_UNCHECKED:
292                 {
293                         if (__audioRouteMode == AUDIO_ROUTE_SPEAKER_ONLY)
294                         {
295                                 AppLogDebug("AUDIO_ROUTE_SPEAKER_ONLY");
296                         }
297                         else if (__audioRouteMode == AUDIO_ROUTE_BT_A2DP_AND_SPEAKER)
298                         {
299                                 if (currentAudioRoute.GetOutputAudioDevice() != OUTPUT_AUDIO_DEVICE_SPEAKER)
300                                 {
301                                         r = mainForm->SetActiveAudioRoute(audioRouteSpeaker);
302                                         TryReturnVoid(r == E_SUCCESS, "mainForm->SetActiveAudioRoute() failed:%s",
303                                                         GetErrorMessage(r));
304                                 }
305                         }
306                         else if (__audioRouteMode == AUDIO_ROUTE_WIRED_ACCESSORY_AND_SPEAKER)
307                         {
308                                 if (currentAudioRoute.GetOutputAudioDevice() != OUTPUT_AUDIO_DEVICE_SPEAKER)
309                                 {
310                                         r = mainForm->SetActiveAudioRoute(audioRouteSpeaker);
311                                         TryReturnVoid(r == E_SUCCESS, "mainForm->SetActiveAudioRoute() failed:%s",
312                                                         GetErrorMessage(r));
313                                 }
314                         }
315                         else
316                         {
317                                 if (currentAudioRoute.GetOutputAudioDevice() != OUTPUT_AUDIO_DEVICE_WIRED_ACCESSORY)
318                                 {
319                                         r = mainForm->SetActiveAudioRoute(audioRouteWiredAccessory);
320                                         TryReturnVoid(r == E_SUCCESS, "__pAudioRouteManager->SetActiveAudioRoute() failed:%s",
321                                                         GetErrorMessage(r));
322                                 }
323                         }
324
325                         __pCheckButton1st->SetSelected(false);
326                         __pCheckButton2nd->SetSelected(true);
327
328                         SetShowState(false);
329                 }
330                 break;
331
332         case IDA_BUTTON_CLOSE:
333                 {
334                         r = SetShowState(false);
335                         TryReturnVoid(r == E_SUCCESS, "SetShowState() failed:%s", GetErrorMessage(r));
336                 }
337                 break;
338
339         default:
340                 break;
341         }
342 }
343
344 void
345 SoundPathPopup::ShowPopup(bool state)
346 {
347         result r = E_FAILURE;
348
349         r = SetShowState(state);
350         TryReturnVoid(r == E_SUCCESS, "SetShowState() failed:%s", GetErrorMessage(r));
351
352         r = Show();
353         TryReturnVoid(r == E_SUCCESS, "Show() failed:%s", GetErrorMessage(r));
354 }
355
356 void
357 SoundPathPopup::OnUserEventReceivedN(RequestId requestId, IList* pArgs)
358 {
359         result r = E_FAILURE;
360         int currentAudioDevice = 0;
361         String checkBoxString;
362         String* bluetoothA2dpName = null;
363         Integer* tempAudioRouteMode = null;
364         Integer* currentOutputAudioDevice = null;
365
366         AppResource* pAppResource = Application::GetInstance()->GetAppResource();
367
368         switch (requestId)
369         {
370         case REQUEST_UPDATE_WIRED_ACCESSORY_AND_SPEAKER_MENU:
371                 {
372                         r = pAppResource->GetString(IDS_COM_OPT_HEADPHONES_ABB, checkBoxString);
373                         TryReturnVoid(r == E_SUCCESS, "pAppResource->GetString() Failed:%s", GetErrorMessage(r));
374
375                         __pCheckButton1st->SetText(checkBoxString);
376
377                         r = pAppResource->GetString(IDS_COM_OPT_SPEAKER_ABB, checkBoxString);
378                         TryReturnVoid(r == E_SUCCESS, "pAppResource->GetString() Failed:%s", GetErrorMessage(r));
379
380                         __pCheckButton2nd->SetText(checkBoxString);
381                         TryReturnVoid(r == E_SUCCESS, "__pCheckButton2nd->Construct() Failed:%s", GetErrorMessage(r));
382
383                         tempAudioRouteMode = static_cast<Integer*>(pArgs->GetAt(0));
384                         __audioRouteMode = tempAudioRouteMode->value;
385
386                         currentOutputAudioDevice = static_cast<Integer*>(pArgs->GetAt(1));
387                         currentAudioDevice = currentOutputAudioDevice->value;
388
389                         if (currentAudioDevice == OUTPUT_AUDIO_DEVICE_WIRED_ACCESSORY)
390                         {
391                                 __pCheckButton1st->SetSelected(true);
392                                 __pCheckButton2nd->SetSelected(false);
393                         }
394                         else if (currentAudioDevice == OUTPUT_AUDIO_DEVICE_SPEAKER)
395                         {
396                                 __pCheckButton1st->SetSelected(false);
397                                 __pCheckButton2nd->SetSelected(true);
398                         }
399
400                         if (GetShowState() == true)
401                         {
402                                 Invalidate(true);
403                         }
404                 }
405                 break;
406
407         case REQUEST_UPDATE_BLUETOOTHA2DP_AND_SPEAKER_MENU:
408                 {
409                         bluetoothA2dpName = static_cast<String*>(pArgs->GetAt(0));
410
411                         __pCheckButton1st->SetText(*bluetoothA2dpName);
412
413                         r = pAppResource->GetString(IDS_COM_OPT_SPEAKER_ABB, checkBoxString);
414                         TryReturnVoid(r == E_SUCCESS, "pAppResource->GetString() Failed:%s", GetErrorMessage(r));
415
416                         __pCheckButton2nd->SetText(checkBoxString);
417                         TryReturnVoid(r == E_SUCCESS, "__pCheckButton2nd->Construct() Failed:%s", GetErrorMessage(r));
418
419                         __pCheckButton1st->SetSelected(true);
420                         __pCheckButton2nd->SetSelected(false);
421
422                         tempAudioRouteMode = static_cast<Integer*>(pArgs->GetAt(1));
423                         __audioRouteMode = tempAudioRouteMode->value;
424
425                         if (GetShowState() == true)
426                         {
427                                 Invalidate(true);
428                         }
429                 }
430                 break;
431
432         case REQUEST_UPDATE_BLUETOOTHA2DP_AND_WIRED_ACCESSORY_MENU:
433                 {
434                         bluetoothA2dpName = static_cast<String*>(pArgs->GetAt(0));
435
436                         __pCheckButton1st->SetText(*bluetoothA2dpName);
437
438                         r = pAppResource->GetString(IDS_COM_OPT_HEADPHONES_ABB, checkBoxString);
439                         TryReturnVoid(r == E_SUCCESS, "pAppResource->GetString() Failed:%s", GetErrorMessage(r));
440
441                         __pCheckButton2nd->SetText(checkBoxString);
442                         TryReturnVoid(r == E_SUCCESS, "__pCheckButton2nd->Construct() Failed:%s", GetErrorMessage(r));
443
444                         __pCheckButton1st->SetSelected(false);
445                         __pCheckButton2nd->SetSelected(true);
446
447                         if (GetShowState() == true)
448                         {
449                                 Invalidate(true);
450                         }
451                 }
452                 break;
453
454         default:
455                 break;
456         }
457 }