Fixed jira issues
[apps/osp/MusicPlayer.git] / src / MpMusicPlayerApp.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (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                MpMusicPlayerApp.cpp
19  * @brief               This is the implementation file for MusicPlayerApp class.
20  */
21
22 #include <FGraphics.h>
23 #include "MpBaseForm.h"
24 #include "MpBasePanel.h"
25 #include "MpCommonUtil.h"
26 #include "MpIMusicContentUpdateEventListner.h"
27 #include "MpMusicPlayerApp.h"
28 #include "MpMusicPlayerForm.h"
29 #include "MpResourceManager.h"
30 #include "MpSceneRegister.h"
31 #include "MpThumbnailProvider.h"
32 #include "MpTypes.h"
33
34 using namespace Tizen::App;
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Collection;
37 using namespace Tizen::Base::Runtime;
38 using namespace Tizen::Content;
39 using namespace Tizen::Graphics;
40 using namespace Tizen::System;
41 using namespace Tizen::Ui;
42 using namespace Tizen::Ui::Controls;
43 using namespace Tizen::Ui::Scenes;
44
45 const wchar_t* TIZEN_APPCONTORL_PROVIDER_AUDIO = L"http://tizen.org/appcontrol/provider/musicplayer";
46 const wchar_t* TIZEN_APPCONTROL_OPERATION_PLAY = L"http://tizen.org/appcontrol/operation/play";
47 const wchar_t* TIZEN_APPCONTROL_OPERATION_VIEW = L"http://tizen.org/appcontrol/operation/view";
48 const wchar_t* TIZEN_APPCONTROL_OPERATION_CONFIGURE = L"http://tizen.org/appcontrol/operation/configure";
49 //const static Tizen::Base::String APPCONTROL_KEYFIELD_PATH = L"path";
50 static const wchar_t* PARAM_LANGUAGE = L"http://tizen.org/setting/locale.language";
51 static const wchar_t* PARAM_COUNTRY = L"http://tizen.org/setting/locale.country";
52
53 static const int TIMER_CONTENT_UPDATE_DELAY = 500;
54
55 MusicPlayerApp::MusicPlayerApp(void)
56         : __requestId(null)
57         , __operationId(null)
58         , __sceneId(IDSCN_ALL_LIST)
59         , __pDataList(null)
60         , __pMainFrame(null)
61         , __pContentManager(null)
62         , __pContentUpdateCompleteTimer(null)
63         , __pContentUpdateInProgressPopup(null)
64 {
65         AppLogDebug("ENTER");
66         AppLogDebug("EXIT");
67 }
68
69 MusicPlayerApp::~MusicPlayerApp(void)
70 {
71         AppLogDebug("ENTER");
72         AppLogDebug("EXIT");
73 }
74
75 UiApp*
76 MusicPlayerApp::CreateInstance(void)
77 {
78         AppLogDebug("ENTER");
79         AppLogDebug("EXIT");
80         return new (std::nothrow) MusicPlayerApp();
81 }
82
83 bool
84 MusicPlayerApp::OnAppInitializing(AppRegistry& appRegistry)
85 {
86         AppLogDebug("ENTER");
87         result r = E_SUCCESS;
88         AppControlProviderManager* pAppControlProviderManager = null;
89
90         CommonUtil::SetValue(AUTO_OFF_LEVEL, 0);
91
92         pAppControlProviderManager = AppControlProviderManager::GetInstance();
93         TryCatch(pAppControlProviderManager != null, r = E_OBJ_NOT_FOUND, "Failed to get AppControlProviderManager");
94
95         r = pAppControlProviderManager->SetAppControlProviderEventListener(this);
96         TryCatch(r == E_SUCCESS, , "Failed SetAppControlProviderEventListener");
97
98         AppLogDebug("EXIT");
99         return true;
100
101 CATCH:
102         //AppLogException("[%s]",GetErrorMessage(r));
103         return false;
104 }
105
106 bool
107 MusicPlayerApp::OnAppInitialized(void)
108 {
109         AppLogDebug("ENTER");
110         result r = E_SUCCESS;
111         __pMainFrame = new (std::nothrow) MainFrame();
112         if (__pMainFrame == null)
113         {
114                 AppLogDebug("EXIT");
115                 return false;
116         }
117
118         __pContentManager = new (std::nothrow) ContentManager();
119         r = __pContentManager->Construct();
120         TryCatch(r == E_SUCCESS, r = E_FAILURE, "AddFrame failed.\n");
121
122         __pContentManager->AddContentUpdateEventListener(*this);
123
124         __pContentUpdateCompleteTimer = new (std::nothrow) Timer();
125         r = __pContentUpdateCompleteTimer->Construct(*this);
126         TryCatch(r == E_SUCCESS, r = E_FAILURE, "AddFrame failed.\n");
127
128         __pMainFrame->Construct();
129         r = AddFrame(*__pMainFrame);
130         TryCatch(r == E_SUCCESS, r = E_FAILURE, "AddFrame failed.\n");
131
132         SettingInfo::AddSettingEventListener(*this);
133
134         if (__sceneId.IsEmpty())
135         {
136                 __sceneId = IDSCN_ALL_LIST;
137         }
138         r = __pMainFrame->GoToInitialScene(__sceneId, __pDataList);
139         if (r == E_SUCCESS)
140         {
141                 __pDataList = null;
142         }
143
144         AppLogDebug("EXIT");
145         return true;
146
147 CATCH:
148         delete __pContentUpdateCompleteTimer;
149         __pContentUpdateCompleteTimer = null;
150         delete __pContentManager;
151         __pContentManager = null;
152
153         AppLogDebug("EXIT(%s)", GetErrorMessage(r));
154         return false;
155 }
156
157 bool
158 MusicPlayerApp::OnAppWillTerminate(void)
159 {
160         AppLogDebug("ENTER");
161         if (__pContentManager != null)
162         {
163                 __pContentManager->RemoveContentUpdateEventListener(*this);
164         }
165
166         if (__pContentUpdateCompleteTimer != null)
167         {
168                 __pContentUpdateCompleteTimer->Cancel();
169         }
170         AppLogDebug("EXIT");
171         return true;
172 }
173
174 bool
175 MusicPlayerApp::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
176 {
177         AppLogDebug("ENTER");
178         CommonUtil::SetValue(AUTO_OFF_LEVEL, 0);
179         /*if (__operationId.Equals(TIZEN_APPCONTROL_OPERATION_PLAY, true)
180                 || __operationId.Equals(TIZEN_APPCONTROL_OPERATION_VIEW, true)
181                 || __operationId.Equals(TIZEN_APPCONTROL_OPERATION_CONFIGURE, true))
182         {
183                 AppControlProviderManager::GetInstance()->SendAppControlResult(__requestId, APP_CTRL_RESULT_TERMINATED, null);
184         }*/
185         ThumbnailProvider* pThumbnailProvider = dynamic_cast<ThumbnailProvider*>(ThumbnailProvider::GetInstance());
186         if (pThumbnailProvider != null)
187         {
188                 pThumbnailProvider->Quit();
189                 pThumbnailProvider->Join();
190                 pThumbnailProvider->DestroyInstance();
191         }
192
193         delete __pContentUpdateCompleteTimer;
194         __pContentUpdateCompleteTimer = null;
195         delete __pContentManager;
196         __pContentManager = null;
197
198         AppLogDebug("EXIT");
199         return true;
200 }
201
202 void
203 MusicPlayerApp::OnForeground(void)
204 {
205         AppLogDebug("ENTER");
206         AppLogDebug("EXIT");
207 }
208
209 void
210 MusicPlayerApp::OnBackground(void)
211 {
212         AppLogDebug("ENTER");
213         Frame* pFrame = GetFrameAt(0);
214         AppAssert(pFrame);
215
216         if (pFrame->IsEnabled() == false)
217         {
218                 pFrame->SetEnabled(true);
219                 pFrame->Invalidate(true);
220         }
221         AppLogDebug("EXIT");
222 }
223
224 void
225 MusicPlayerApp::OnLowMemory(void)
226 {
227         AppLogDebug("ENTER");
228         AppLogDebug("EXIT");
229 }
230
231 void
232 MusicPlayerApp::OnBatteryLevelChanged(BatteryLevel batteryLevel)
233 {
234         AppLogDebug("ENTER");
235         AppLogDebug("EXIT");
236 }
237
238 void
239 MusicPlayerApp::OnScreenOn(void)
240 {
241         AppLogDebug("ENTER");
242         AppLogDebug("EXIT");
243 }
244
245 void
246 MusicPlayerApp::OnScreenOff(void)
247 {
248         AppLogDebug("ENTER");
249         AppLogDebug("EXIT");
250 }
251
252 void
253 MusicPlayerApp::OnSettingChanged(Tizen::Base::String& key)
254 {
255         AppLogDebug("ENTER");
256         if (key.Equals(PARAM_LANGUAGE, true) || key.Equals(PARAM_COUNTRY, true))
257         {
258                 UiApp* pApp = UiApp::GetInstance();
259                 pApp->Terminate();
260         }
261         AppLogDebug("EXIT");
262 }
263
264 void
265 MusicPlayerApp::OnAppControlRequestReceived(RequestId reqId, const Tizen::Base::String& operationId, const Tizen::Base::String* pUriData,
266                                                                                                         const Tizen::Base::String* pMimeType, const Tizen::Base::Collection::IMap* pExtraData)
267 {
268         AppLogDebug("ENTER");
269 //      result r = E_SUCCESS;
270 //      TryCatch(__pDataList == null, r = E_OBJ_ALREADY_EXIST, "The application has already launched");
271
272         __requestId = reqId;
273         __operationId = operationId;
274
275         if (operationId.Equals(TIZEN_APPCONTROL_OPERATION_PLAY, true) || operationId.Equals(TIZEN_APPCONTROL_OPERATION_VIEW, true))
276         {
277                 ParsingArgument(pUriData, pExtraData);
278                 __sceneId = IDSCN_PLAYER;
279         }
280         else if (operationId.Equals(TIZEN_APPCONTROL_OPERATION_CONFIGURE, true))
281         {
282                 __sceneId = IDSCN_MUSIC_SETTING;
283         }
284         else
285         {
286                 __sceneId = IDSCN_ALL_LIST;
287         }
288
289         if (__pMainFrame != null)
290         {
291                 SceneManager* pSceneManager = SceneManager::GetInstance();
292                 AppAssert(pSceneManager);
293
294                 result r = E_FAILURE;
295                 SceneId currentSceneId = pSceneManager->GetCurrentSceneId();
296                 if (__sceneId.Equals(IDSCN_PLAYER, false))
297                 {
298         
299                         Scene* pScene = pSceneManager->GetCurrentScene();
300                         PlayerForm* pPlayerForm = static_cast<PlayerForm*>(pScene->GetForm());
301                         if (currentSceneId.Equals(IDSCN_PLAYER, false))
302                         {       
303                                 pPlayerForm->OnChangedContentListByAppControlN(__pDataList);
304                                 r = E_SUCCESS;
305                         }
306                         else if(pSceneManager->IsSceneAlive(IDSCN_PLAYER) && currentSceneId.Equals(IDSCN_CONTENT_DETAIL, false))
307                         {
308                                 r = pSceneManager->GoBackward(BackwardSceneTransition(IDSCN_PLAYER), __pDataList);
309                         }
310                         else
311                         {
312                                 r = __pMainFrame->GoToInitialScene(__sceneId, __pDataList);
313                         }
314                 }
315                 else
316                 {
317                         r = __pMainFrame->GoToInitialScene(__sceneId, __pDataList);
318                 }
319
320                 if (r == E_SUCCESS)
321                 {
322                         __pDataList = null;
323                 }
324         }
325         AppLogDebug("EXIT");
326         return;
327 }
328
329 void
330 MusicPlayerApp::ParsingArgument(const Tizen::Base::String* pUriData, const Tizen::Base::Collection::IMap* pArgs)
331 {
332         AppLogDebug("ENTER");
333 //      const String* pContentPath = null;
334         String sourceStr;
335
336         if (pUriData != null)
337         {
338                 String original(L"file://");
339                 String replace(L"");
340
341                 sourceStr.Append(*pUriData);
342                 sourceStr.Replace(original, replace);
343         //      pContentPath = &sourceStr;
344
345                 if (__pDataList != null)
346                 {
347                         __pDataList->RemoveAll();
348                         delete __pDataList;
349                 }
350
351                 __pDataList = new (std::nothrow) ArrayList();
352                 __pDataList->Construct();
353
354                 ArrayList* pContentPathArrayList = new (std::nothrow) ArrayList();
355                 pContentPathArrayList->Construct();
356
357                 AppLogDebug("sourceStr : %ls", sourceStr.GetPointer());
358                 pContentPathArrayList->Add((new (std::nothrow) String(sourceStr)));
359
360                 __pDataList->Add(pContentPathArrayList);
361                 __pDataList->Add(new (std::nothrow) Integer(0));
362         }
363
364 /*For multiple file path*/
365
366 /*      else if (pArgs != null && pArgs->GetCoun__pDataListt() > 0)
367         {
368                 pContentPath = static_cast<const String*>(pArgs->GetValue(APPCONTROL_KEYFIELD_PATH));
369         }
370         else
371         {
372                 return;
373         }
374
375         ArrayList* pContentPathArrayList = new (std::nothrow) ArrayList();
376         pContentPathArrayList->Construct();
377
378         if (pContentPath != null)
379         {
380                 String token;
381                 String delim(L";");
382
383                 AppLogDebug("pContentPath : %ls", pContentPath->GetPointer());
384
385                 if (__pDataList == null)
386                 {
387                         __pDataList = new (std::nothrow) ArrayList();
388                         __pDataList->Construct();
389                 }
390                 else
391                 {
392                         __pDataList->RemoveAll(true);
393                 }
394
395                 Tizen::Base::Utility::StringTokenizer PathStringTokenizer(*pContentPath, delim);
396
397                 while (PathStringTokenizer.HasMoreTokens())
398                 {
399                         PathStringTokenizer.GetNextToken(token);
400 //                      if (File::IsFileExist(token))
401                         {
402                                 AppLogDebug("token : %ls", token.GetPointer());
403                                 pContentPathArrayList->Add((new (std::nothrow) String(token)));
404                         }
405                 }
406                 __pDataList->Add(pContentPathArrayList);
407                 __pDataList->Add(*(new (std::nothrow) Integer(0)));
408         }
409 */
410         AppLogDebug("EXIT");
411 }
412
413 Tizen::Base::String
414 MusicPlayerApp::GetOperationId(void)
415 {
416         AppLogDebug("ENTER");
417         AppLogDebug("EXIT");
418         return __operationId;
419 }
420
421 RequestId
422 MusicPlayerApp::GetRequestId(void)
423 {
424         return __requestId;
425 }
426
427 void
428 MusicPlayerApp::ResetRequestId(void)
429 {
430         __requestId = null;
431 }
432
433 void
434 MusicPlayerApp::OnContentFileCreated(Tizen::Content::ContentId contentId, Tizen::Content::ContentType contentType, result r)
435 {
436         AppLogDebug("ENTER");
437         if (contentType == CONTENT_TYPE_AUDIO && !IsFailed(r))
438         {
439                 ReStartContentUpdateTimer();
440         }
441         AppLogDebug("EXIT");
442 }
443
444 void
445 MusicPlayerApp::OnContentFileUpdated(Tizen::Content::ContentId contentId, Tizen::Content::ContentType contentType, result r)
446 {
447         AppLogDebug("ENTER");
448         if (contentType == CONTENT_TYPE_AUDIO && !IsFailed(r))
449         {
450                 ReStartContentUpdateTimer();
451         }
452         AppLogDebug("EXIT");
453 }
454
455 void
456 MusicPlayerApp::OnContentFileDeleted(Tizen::Content::ContentId contentId, Tizen::Content::ContentType contentType, result r)
457 {
458         AppLogDebug("ENTER");
459         if (contentType == CONTENT_TYPE_AUDIO && !IsFailed(r))
460         {
461                 ReStartContentUpdateTimer();
462         }
463         AppLogDebug("EXIT");
464 }
465
466 void
467 MusicPlayerApp::OnContentDirectoryScanCompleted(const Tizen::Base::String& directoryPath, result r)
468 {
469         AppLogDebug("ENTER");
470         if (!IsFailed(r))
471         {
472                 ReStartContentUpdateTimer();
473         }
474         AppLogDebug("EXIT");
475 }
476
477 void
478 MusicPlayerApp::ReStartContentUpdateTimer(void)
479 {
480         AppLogDebug("ENTER");
481         if (__pContentUpdateCompleteTimer != null)
482         {
483                 __pContentUpdateCompleteTimer->Cancel();
484                 __pContentUpdateCompleteTimer->Start(TIMER_CONTENT_UPDATE_DELAY);
485                 ShowContentUpdateInProgressPopup();
486         }
487         AppLogDebug("EXIT");
488 }
489
490 void
491 MusicPlayerApp::ShowContentUpdateInProgressPopup(void)
492 {
493         AppLogDebug("ENTER");
494         if (__pContentUpdateInProgressPopup != null)
495         {
496                 AppLogDebug("EXIT");
497                 return;
498         }
499
500         __pContentUpdateInProgressPopup = new (std::nothrow) Popup();
501         result r = __pContentUpdateInProgressPopup->Construct(IDL_CONTENTS_UPDATE_POPUP);
502         if (IsFailed(r))
503         {
504                 AppLogDebug("EXIT(%s)", GetErrorMessage(r));
505                 delete __pContentUpdateInProgressPopup;
506                 return;
507         }
508
509         __pContentUpdateInProgressPopup->SetShowState(true);
510         __pContentUpdateInProgressPopup->Show();
511         AppLogDebug("EXIT");
512 }
513
514 void
515 MusicPlayerApp::HideContentUpdateInProgressPopup(void)
516 {
517         AppLogDebug("ENTER");
518         if (__pContentUpdateInProgressPopup == null)
519         {
520                 AppLogDebug("EXIT");
521                 return;
522         }
523
524         __pContentUpdateInProgressPopup->SetShowState(false);
525         delete __pContentUpdateInProgressPopup;
526         __pContentUpdateInProgressPopup = null;
527         AppLogDebug("EXIT");
528 }
529
530 void
531 MusicPlayerApp::OnTimerExpired(Tizen::Base::Runtime::Timer& timer)
532 {
533         AppLogDebug("ENTER");
534         if (__pContentUpdateCompleteTimer != &timer)
535         {
536                 AppLogDebug("EXIT(UnKnown Timer)");
537                 return;
538         }
539
540         CallOnMusicContentUpdateCompleted();
541         HideContentUpdateInProgressPopup();
542         AppLogDebug("EXIT");
543 }
544
545 void
546 MusicPlayerApp::CallOnMusicContentUpdateCompleted(void)
547 {
548         AppLogDebug("ENTER");
549         UiApp* pApp = UiApp::GetInstance();
550         AppAssert(pApp);
551 #if 0
552         if (pApp->GetAppUiState() == APP_UI_STATE_FOREGROUND || pApp->GetAppUiState() == APP_UI_STATE_PARTIAL_BACKGROUND)
553         {
554                 SceneManager* pSceneManager = SceneManager::GetInstance();
555                 AppAssert(pSceneManager);
556
557                 IMusicContentUpdateEventListner* pEventListner = dynamic_cast<IMusicContentUpdateEventListner*>(pSceneManager->GetCurrentScene()->GetForm());
558                 if (pEventListner == null)
559                 {
560                         pEventListner = dynamic_cast<IMusicContentUpdateEventListner*>(pSceneManager->GetCurrentScene()->GetPanel());
561                 }
562
563                 if (pEventListner != null)
564                 {
565                         pEventListner->OnMusicContentUpdateCompleted();
566
567                 }
568                 AppLogDebug("EXIT");
569                 return;
570         }
571 #endif
572         int frameChildControlCount = __pMainFrame->GetControlCount();
573         for (int iCount = 0; iCount < frameChildControlCount; iCount++)
574         {
575                 Form* pForm = dynamic_cast<Form*>(__pMainFrame->GetControl(iCount));
576                 if (pForm == null)
577                 {
578                         continue;
579                 }
580
581                 IMusicContentUpdateEventListner* pEventListner = dynamic_cast<IMusicContentUpdateEventListner*>(pForm);
582                 if (pEventListner != null)
583                 {
584                         pEventListner->OnMusicContentUpdateCompleted();
585                         continue;
586                 }
587
588                 int formChildControlCount = pForm->GetControlCount();
589                 for (int iCount = 0; iCount < formChildControlCount; iCount++)
590                 {
591                         IMusicContentUpdateEventListner* pEventListner = dynamic_cast<IMusicContentUpdateEventListner*>(pForm->GetControl(iCount));
592                         if (pEventListner != null)
593                         {
594                                 pEventListner->OnMusicContentUpdateCompleted();
595                         }
596                 }
597         }
598         AppLogDebug("EXIT");
599 }