Applied latest source code
[apps/native/preloaded/MusicPlayer.git] / src / MpMusicPlayerPresentationModel.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                MpMusicPlayerPresentationModel.cpp
19  * @brief               This is the implementation file for MusicPlayerPresentationModel class.
20  */
21
22 #include <time.h>
23 #include <FBase.h>
24 #include <FGraphics.h>
25 #include <FIo.h>
26 #include <FSystem.h>
27 #include <FUi.h>
28 #include "MpCommonUtil.h"
29 #include "MpIMusicPlayerEventListener.h"
30 #include "MpMusicPlayerPresentationModel.h"
31 #include "MpPlaylistDB.h"
32 #include "MpResourceManager.h"
33 #include "MpTypes.h"
34
35 using namespace Tizen::App;
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Base::Runtime;
39 using namespace Tizen::Base::Utility;
40 using namespace Tizen::Content;
41 using namespace Tizen::Graphics;
42 using namespace Tizen::Io;
43 using namespace Tizen::Media;
44 using namespace Tizen::System;
45 using namespace Tizen::Telephony;
46 using namespace Tizen::Ui::Controls;
47
48 #define BUFFER_INFO_OF_PLAYER   0
49
50 static const int PROGRESS_TIMER_DURATION = 150;
51 static const int PROGRESS_BAR_DURATION_TIMER = 1000;
52 static const int MIN_SLIDER_COUNT = 0;
53 static const int MAX_SLIDER_COUNT = 100;
54 static const int INIT_X_VALUE = 0;
55 static const int INIT_CONTENT_INDEX = -1;
56 static const int VOLUMEBAR_INTERVAL = 3000;
57 static const int PAUSE_AUTO_OFF_TIME = 1000 * 60 * 2;
58
59 PlayerPresentationModel* PlayerPresentationModel::pPlayerPresentationModel = null;
60
61 PlayerPresentationModel*
62 PlayerPresentationModel::GetInstance(void)
63 {
64         AppLogDebug("ENTER");
65         if (pPlayerPresentationModel == null)
66         {
67                 pPlayerPresentationModel = new (std::nothrow) PlayerPresentationModel();
68                 TryReturn(pPlayerPresentationModel != null, null, "PlayerPresentationModel instance is not created.");
69
70                 result r = pPlayerPresentationModel->Construct();
71                 TryCatch(r == E_SUCCESS, , null, "PlayerPresentationModel instance is not constructed.");
72         }
73         AppLogDebug("EXIT");
74         return pPlayerPresentationModel;
75
76 CATCH:
77         if (pPlayerPresentationModel != null)
78         {
79                 delete pPlayerPresentationModel;
80         }
81
82         return null;
83 }
84
85 PlayerPresentationModel::PlayerPresentationModel(void)
86         : __pPlayer(null)
87         , __pPlayStateTimer(null)
88         , __pFastForwardTimer(null)
89         , __pFastRewindTimer(null)
90         , __pAutoOffTimer(null)
91         , __pPauseAutoOffTimer(null)
92         , __pVolumeBarStateTimer(null)
93         , __pPlayContentList(null)
94         , __previousContentIndex(INIT_CONTENT_INDEX)
95         , __currentContentIndex(INIT_CONTENT_INDEX)
96         , __nextContentIndex(INIT_CONTENT_INDEX)
97         , __removedContentIndex(INIT_CONTENT_INDEX)
98         , __pCurrentPlayContentPath(null)
99         , __pCurrentPlayContentTitle(null)
100         , __pCurrentPlayContentThumbnail(null)
101         , __pCurrentPlayContentArtist(null)
102         , __pUuIdForFileOnly(null)
103         , __pCurrentAudioContentInfo(null)
104         , __currentPlayContentDuration(null)
105         , __repeatMode(null)
106         , __shuffleEnable(false)
107         , __seekCompleted(true)
108         , __isForceChanged(false)
109         , __playposition(MIN_SLIDER_COUNT)
110         , __previousPlayerState(PLAYER_STATE_ERROR)
111         , __xTimesValue(INIT_X_VALUE)
112         , __currentContentTotalDuration(0)
113         , __pCallManager(null)
114 {
115         AppLogDebug("ENTER");
116         AppLogDebug("EXIT");
117 }
118
119 PlayerPresentationModel::~PlayerPresentationModel(void)
120 {
121         AppLogDebug("ENTER");
122         result r = E_SUCCESS;
123
124         if (__repeatMode != CommonUtil::GetValue(REPEAT_MODE))
125         {
126                 CommonUtil::SetValue(REPEAT_MODE, __repeatMode);
127         }
128         if (__shuffleEnable != static_cast<bool>(CommonUtil::GetValue(SHUFFLE_MODE)))
129         {
130                 CommonUtil::SetValue(SHUFFLE_MODE, static_cast<int>(__shuffleEnable));
131         }
132         if (__pPlayContentList != null)
133         {
134                 __pPlayContentList->RemoveAll(true);
135                 delete __pPlayContentList;
136                 __pPlayContentList = null;
137         }
138         if (__pPlayStateTimer != null)
139         {
140                 __pPlayStateTimer->Cancel();
141                 delete __pPlayStateTimer;
142                 __pPlayStateTimer = null;
143         }
144         if (__pFastForwardTimer != null)
145         {
146                 __pFastForwardTimer->Cancel();
147                 delete __pFastForwardTimer;
148                 __pFastForwardTimer = null;
149         }
150         if (__pFastRewindTimer != null)
151         {
152                 __pFastRewindTimer->Cancel();
153                 delete __pFastRewindTimer;
154                 __pFastRewindTimer = null;
155         }
156         if (__pAutoOffTimer != null)
157         {
158                 __pAutoOffTimer->Cancel();
159                 delete __pAutoOffTimer;
160                 __pAutoOffTimer = null;
161         }
162         if (__pPauseAutoOffTimer != null)
163         {
164                 __pPauseAutoOffTimer->Cancel();
165                 delete __pPauseAutoOffTimer;
166                 __pPauseAutoOffTimer = null;
167         }
168         if (__pVolumeBarStateTimer != null)
169         {
170                 __pVolumeBarStateTimer->Cancel();
171                 delete __pVolumeBarStateTimer;
172         }
173
174         delete __pCurrentPlayContentPath;
175         delete __pCurrentPlayContentTitle;
176         delete __pCurrentPlayContentThumbnail;
177         delete __pCurrentPlayContentArtist;
178         delete __pUuIdForFileOnly;
179         delete __pCurrentAudioContentInfo;
180
181         if (__pCallManager != null)
182         {
183                 delete __pCallManager;
184         }
185
186         if (__pPlayer == null)
187         {
188                 AppLogDebug("EXIT");
189                         return;
190         }
191         if (__pPlayer != null)
192         {
193                 PlayerState state = __pPlayer->GetState();
194                 switch (state)
195                 {
196                 case PLAYER_STATE_PLAYING:
197                         // fall through
198                 case PLAYER_STATE_PAUSED:
199                         {
200                                 r = __pPlayer->Stop();
201                                 TryLog(r == E_SUCCESS, "[%s] Failed Stop",GetErrorMessage(r));
202                                 r = __pPlayer->Close();
203                                 TryLog(r == E_SUCCESS, "[%s] Failed Close",GetErrorMessage(r));
204                         }
205                         break;
206
207                 case PLAYER_STATE_ENDOFCLIP:
208                         // fall through
209                 case PLAYER_STATE_OPENED:
210                         // fall through                 
211                 case PLAYER_STATE_STOPPED:
212                         {
213                                 r = __pPlayer->Close();
214                                 TryLog(r == E_SUCCESS, "[%s] Failed Close",GetErrorMessage(r));
215                         }
216                         break;
217
218                 case PLAYER_STATE_CLOSED:
219                         {
220                         }
221                         break;
222
223                 default:
224                         break;
225                 }
226                 delete __pPlayer;
227                 __pPlayer = null;
228         }
229         AppLogDebug("EXIT");
230 }
231
232 void
233 PlayerPresentationModel::DestroyPlayerPresentationModel(void)
234 {
235         AppLogDebug("ENTER");
236         if (pPlayerPresentationModel != null)
237         {
238                 CallOnPlayPresentationModelDestroying();
239
240                 delete pPlayerPresentationModel;
241                 pPlayerPresentationModel = null;
242         }
243         AppLogDebug("EXIT");
244 }
245
246 void
247 PlayerPresentationModel::OnTelephonyCallStatusChangedN(Tizen::Telephony::CallStatus callStatus, Tizen::Telephony::CallInfo* pCallInfo)
248 {
249         AppLogDebug("ENTER");
250         AppLogDebug("callStatus : %d", callStatus);
251         if (callStatus == CALL_STATUS_IDLE || callStatus == CALL_STATUS_COMMUNICATING || callStatus == CALL_STATUS_DIALING)
252         {
253                 CallOnPlayContentChanged(__currentContentIndex);
254         }
255         AppLogDebug("EXIT");
256 }
257
258 result
259 PlayerPresentationModel::Construct(void)
260 {
261         AppLogDebug("ENTER");
262         result r = E_SUCCESS;
263
264         /*AudioSessionManager::GetInstance()->SetAudioSessionEventListener(this);
265         AudioSessionManager::GetInstance()->SetMode(AUDIO_SESSION_MODE_EXCLUSIVE);*/
266
267         __pPlayStateTimer = new (std::nothrow) Timer();
268         r = __pPlayStateTimer->Construct(*this);
269         TryCatch(r == E_SUCCESS, r = E_OUT_OF_MEMORY, "Unable to construct Timer.\n");
270
271         __pFastForwardTimer = new (std::nothrow) Timer();
272         r = __pFastForwardTimer->Construct(*this) ;
273         TryCatch(r == E_SUCCESS, r = E_OUT_OF_MEMORY, "Unable to construct FastForwardTimer.\n");
274
275         __pFastRewindTimer = new (std::nothrow) Timer();
276         r = __pFastRewindTimer->Construct(*this) ;
277         TryCatch(r == E_SUCCESS, r = E_OUT_OF_MEMORY, "Unable to construct FastRewindTimer.\n");
278
279         __pAutoOffTimer = new (std::nothrow) Timer();
280         r = __pAutoOffTimer->Construct(*this);
281         TryCatch(r == E_SUCCESS, r = E_OUT_OF_MEMORY, "Unable to construct __pAutoOffTimer.\n");
282
283         __pPauseAutoOffTimer = new (std::nothrow) Timer();
284         r = __pPauseAutoOffTimer->Construct(*this);
285         TryCatch(r == E_SUCCESS, r = E_OUT_OF_MEMORY, "Unable to construct __pPauseAutoOffTimer.\n");
286
287         __pVolumeBarStateTimer = new (std::nothrow) Timer();
288         r = __pVolumeBarStateTimer->Construct(*this);
289         TryCatch(r == E_SUCCESS, r = E_OUT_OF_MEMORY, "Unable to construct Timer.\n");
290
291         __pCurrentPlayContentPath = new (std::nothrow) String(L"");
292
293         __repeatMode = CommonUtil::GetValue(REPEAT_MODE);
294         __shuffleEnable = static_cast<bool>(CommonUtil::GetValue(SHUFFLE_MODE));
295
296         __pCallManager = new (std::nothrow) CallManager();
297         __pCallManager->Construct(*this);
298
299         __pUuIdForFileOnly = Tizen::Base::UuId::GenerateN();
300
301         InitializePlayer();
302
303         AppLogDebug("EXIT");
304         return r;
305
306 CATCH:
307         //AppLogException( "[%s]", GetErrorMessage(r));
308         if (__pPlayStateTimer != null)
309         {
310                 __pPlayStateTimer->Cancel();
311                 delete __pPlayStateTimer;
312                 __pPlayStateTimer = null;
313         }
314         if (__pFastForwardTimer != null)
315         {
316                 __pFastForwardTimer->Cancel();
317                 delete __pFastForwardTimer;
318                 __pFastForwardTimer = null;
319         }
320         if (__pFastRewindTimer != null)
321         {
322                 __pFastRewindTimer->Cancel();
323                 delete __pFastRewindTimer;
324                 __pFastRewindTimer = null;
325         }
326         if (__pAutoOffTimer != null)
327         {
328                 __pAutoOffTimer->Cancel();
329                 delete __pAutoOffTimer;
330                 __pAutoOffTimer = null;
331         }
332         if (__pPauseAutoOffTimer != null)
333         {
334                 __pPauseAutoOffTimer->Cancel();
335                 delete __pPauseAutoOffTimer;
336                 __pPauseAutoOffTimer = null;
337         }
338         if (__pVolumeBarStateTimer != null)
339         {
340                 __pVolumeBarStateTimer->Cancel();
341                 delete __pVolumeBarStateTimer;
342         }
343         return r;
344 }
345
346 result
347 PlayerPresentationModel::InitializePlayer(void)
348 {
349         AppLogDebug("ENTER");
350         result r = E_SUCCESS;
351
352         if (__pPlayer != null)
353         {
354                 delete __pPlayer;
355                 __pPlayer = null;
356         }
357
358         __pPlayer = new (std::nothrow) Player();
359         r = __pPlayer->Construct(*this, BUFFER_INFO_OF_PLAYER);
360         TryCatch(r == E_SUCCESS, r = E_OUT_OF_MEMORY, "Unable to construct Player.\n");
361
362         AppLogDebug("EXIT");
363         return r;
364
365 CATCH:
366         delete __pPlayer;
367         __pPlayer = null;
368
369         return r;
370 }
371
372 result
373 PlayerPresentationModel::StartAutoOffTimer(int autoOffValue)
374 {
375         AppLogDebug("ENTER");
376         result r = E_SUCCESS;
377         if (__pAutoOffTimer != null)
378         {
379                 if (autoOffValue == 0)
380                 {
381                         r = __pAutoOffTimer->Cancel();
382                         AppLogDebug("%s", GetErrorMessage(r));
383                 }
384                 else
385                 {
386                         __pAutoOffTimer->Cancel();
387                         r = __pAutoOffTimer->Start(autoOffValue);
388                         AppLogDebug("%s", GetErrorMessage(r));
389                         TryCatch(r == E_SUCCESS, r = E_OUT_OF_MEMORY, "Unable to Start __pAutoOffTimer.\n");
390                 }
391                 AppLogDebug("EXIT");
392                 return r;
393         }
394         AppLogDebug("EXIT");
395         return r;
396
397 CATCH:
398         //AppLogException( "[%s]", GetErrorMessage(r));
399         return r;
400 }
401
402 void
403 PlayerPresentationModel::RemoveContent(const Tizen::Base::String& filePath)
404 {
405         AppLogDebug("ENTER");
406         TryReturnVoid(__pPlayContentList != null, "There is not a playlist");
407         IEnumerator* pEnum = __pPlayContentList->GetEnumeratorN();
408         String* pFilePath = null;
409         int index = 0;
410
411         while (pEnum->MoveNext() == E_SUCCESS)
412         {
413                 pFilePath = static_cast<String*>(pEnum->GetCurrent());
414
415                 if (pFilePath->Equals(filePath))
416                 {
417                         __pPlayContentList->Remove(filePath);
418
419                         if (index < __currentContentIndex)
420                         {
421                                 __currentContentIndex--;
422                         }
423                         else if (index == __currentContentIndex)
424                         {
425                                 if (index == __pPlayContentList->GetCount())
426                                 {
427                                         __currentContentIndex--;
428                                 }
429                                 __removedContentIndex = index;
430                         }
431
432                         GenerateNextIndex();
433                         if (__pPlayContentList->GetCount() > 0)
434                         {
435                                 CallOnPlayContentRemoved(index);
436                         }
437                         break;
438                 }
439                 index++;
440         }
441         delete pEnum;
442         AppLogDebug("EXIT");
443         return;
444 }
445
446 PlayerState
447 PlayerPresentationModel::GetPlayerState(void)
448 {
449         AppLogDebug("ENTER");
450         if (__pPlayer == null)
451         {
452                 AppLogDebug("EXIT");
453                 return PLAYER_STATE_ERROR;
454         }
455         AppLogDebug("EXIT");
456         return __pPlayer->GetState();
457 }
458
459 bool
460 PlayerPresentationModel::IsDuringCall(void)
461 {
462         AppLogDebug("ENTER");
463         CallStatus callStatus = CALL_STATUS_UNDEFINED;
464         callStatus = __pCallManager->GetCurrentCallStatus();
465         AppLogDebug("callStatus = %d", callStatus);
466
467         if (callStatus == CALL_STATUS_IDLE)
468         {
469                 AppLogDebug("EXIT");
470                 return false;
471         }
472         else
473         {
474                 AppLogDebug("EXIT");
475                 return true;
476         }
477 }
478
479 void
480 PlayerPresentationModel::ClosePlayer(void)
481 {
482         AppLogDebug("ENTER");
483         result r = E_SUCCESS;
484
485         if (__pPlayer == null)
486         {
487                 AppLogDebug("EXIT");
488                 return;
489         }
490
491         __previousPlayerState = __pPlayer->GetState();
492
493         switch (__previousPlayerState)
494         {
495         case PLAYER_STATE_PLAYING:
496                 // fall through
497         case PLAYER_STATE_PAUSED:
498                 {
499                         r = __pPlayer->Stop();
500                         TryLog(r == E_SUCCESS, "[%s] Failed Stop", GetErrorMessage(r));
501                         r = __pPlayer->Close();
502                         TryLog(r == E_SUCCESS, "[%s] Failed Close", GetErrorMessage(r));
503                 }
504                 break;
505
506         case PLAYER_STATE_OPENED:
507                 // fall through
508         case PLAYER_STATE_STOPPED:
509                 {
510                         r = __pPlayer->Close();
511                         TryLog(r == E_SUCCESS, "[%s] Failed Close", GetErrorMessage(r));
512                 }
513                 break;
514
515         case PLAYER_STATE_CLOSED:
516                 {
517                 }
518                 break;
519
520         case PLAYER_STATE_ENDOFCLIP:
521                 {
522                 }
523                 break;
524
525         default:
526                 break;
527         }
528         AppLogDebug("EXIT");
529 }
530
531 Tizen::Base::String*
532 PlayerPresentationModel::GetPlayContentPath(void)
533 {
534         AppLogDebug("ENTER");
535         AppLogDebug("EXIT");
536         return __pCurrentPlayContentPath;
537 }
538
539 Tizen::Base::String*
540 PlayerPresentationModel::GetPlayContentTitle(void)
541 {
542         AppLogDebug("ENTER");
543         AppLogDebug("EXIT");
544         return __pCurrentPlayContentTitle;
545 }
546
547 Tizen::Base::String*
548 PlayerPresentationModel::GetPlayContentArtist(void)
549 {
550         AppLogDebug("ENTER");
551         AppLogDebug("EXIT");
552         return __pCurrentPlayContentArtist;
553 }
554
555 Tizen::Graphics::Bitmap*
556 PlayerPresentationModel::GetPlayContentThumbnail(void)
557 {
558         AppLogDebug("ENTER");
559         AppLogDebug("EXIT");
560         return __pCurrentPlayContentThumbnail;
561 }
562
563 long
564 PlayerPresentationModel::GetPlayContentDuration(void)
565 {
566         AppLogDebug("ENTER");
567         AppLogDebug("EXIT");
568         return __currentPlayContentDuration;
569 }
570
571 Tizen::Base::String*
572 PlayerPresentationModel::GetContentPath(int contentIndex)const
573 {
574         AppLogDebug("ENTER");
575         String* pFilePath = null;
576
577         TryReturn(contentIndex >= 0 && contentIndex < __pPlayContentList->GetCount(), null, "contentIndex is invalid.");
578         pFilePath = static_cast<String*>(__pPlayContentList->GetAt(contentIndex));
579
580         AppLogDebug("EXIT");
581         return pFilePath;
582 }
583
584 int
585 PlayerPresentationModel::GetPreviousContentIndex(void) const
586 {
587         AppLogDebug("ENTER");
588         AppLogDebug("EXIT");
589         return __previousContentIndex;
590 }
591
592 int
593 PlayerPresentationModel::GetCurrentContentIndex(void) const
594 {
595         AppLogDebug("ENTER");
596         AppLogDebug("EXIT");
597         return __currentContentIndex;
598 }
599
600 int
601 PlayerPresentationModel::GetNextContentIndex(void) const
602 {
603         AppLogDebug("ENTER");
604         AppLogDebug("EXIT");
605         return __nextContentIndex;
606 }
607
608 ContentInformation*
609 PlayerPresentationModel::GetContentInfoN(int contentIndex)
610 {
611         AppLogDebug("ENTER");
612         AudioContentInfo* pAudioContentInfo = null;
613
614         if (__pPlayContentList == null || contentIndex < INIT_VALUE)
615         {
616                 AppLogDebug("EXIT(index(%d) MUST be greater than or equal to 0 OR __pPlayContentList is null)", contentIndex);
617                 return null;
618         }
619
620         Tizen::Base::String* contentPath = static_cast<String*>(__pPlayContentList->GetAt(contentIndex));
621         if (contentPath == null || File::IsFileExist(*contentPath) == false)
622         {
623                 AppLogDebug("EXIT");
624                 return null;
625         }
626
627         ContentInformation* pContentInfo = new (std::nothrow) ContentInformation();
628         TryReturn(pContentInfo != null, null, "pContentInfo is null.");
629
630         pAudioContentInfo = CommonUtil::GetAudioContentInfoN(*contentPath);
631
632         if (pAudioContentInfo != null)
633         {
634                 if (pAudioContentInfo->GetArtist() == null)
635                 {
636                         pContentInfo->ArtistName = ResourceManager::GetString(L"IDS_MUSIC_BODY_UNKNOWN");
637                 }
638                 else
639                 {
640                         pContentInfo->ArtistName = pAudioContentInfo->GetArtist();
641                 }
642
643                 if (pAudioContentInfo->GetTitle() == null)
644                 {
645                         pContentInfo->TitleName = ResourceManager::GetString(L"IDS_MUSIC_BODY_UNKNOWN");
646                 }
647                 else
648                 {
649                         pContentInfo->TitleName = pAudioContentInfo->GetTitle();
650                 }
651
652                 if (__pPlayer != null)
653                 {
654                         pContentInfo->Duration  = __pPlayer->GetDuration();
655                 }
656                 else
657                 {
658                         pContentInfo->Duration  = pAudioContentInfo->GetDuration();
659                 }
660                 pContentInfo->contentId = pAudioContentInfo->GetContentId();
661
662                 if (pAudioContentInfo != __pCurrentAudioContentInfo)
663                 {
664                         delete pAudioContentInfo;
665                 }
666         }
667         else
668         {
669                 Tizen::Content::AudioMetadata* pAudioMeta = Tizen::Content::ContentManagerUtil::GetAudioMetaN(*static_cast<String*>(__pPlayContentList->GetAt(contentIndex)));
670                 if (pAudioMeta == null)
671                 {
672                         delete pContentInfo;
673                         return null;
674                 }
675
676                 if (pAudioMeta->GetArtist() == null)
677                 {
678                         pContentInfo->ArtistName = ResourceManager::GetString(L"IDS_MUSIC_BODY_UNKNOWN");
679                 }
680                 else
681                 {
682                         pContentInfo->ArtistName = pAudioMeta->GetArtist();
683                 }
684
685                 if (pAudioMeta->GetTitle() == null)
686                 {
687                         pContentInfo->TitleName = ResourceManager::GetString(L"IDS_MUSIC_BODY_UNKNOWN");
688                 }
689                 else
690                 {
691                         pContentInfo->TitleName = pAudioMeta->GetTitle();
692                 }
693
694                 if (__pPlayer != null)
695                 {
696                         pContentInfo->Duration = __pPlayer->GetDuration();
697                 }
698                 pContentInfo->contentId = *__pUuIdForFileOnly;
699                 delete pAudioMeta;
700         }
701
702         AppLogDebug("EXIT");
703         return pContentInfo;
704 }
705
706 Tizen::Graphics::Bitmap*
707 PlayerPresentationModel::GetContentAlbumArtN(int contentIndex)
708 {
709         AppLogDebug("ENTER");
710         Bitmap* pAlbumArtBitmap = null;
711         if (contentIndex <= -1)
712         {
713                 pAlbumArtBitmap = ResourceManager::GetBitmapN(L"34_thumb_07.png");
714                 AppLogDebug("EXIT");
715                 return pAlbumArtBitmap;
716         }
717
718         String* path = static_cast<String*>(__pPlayContentList->GetAt(contentIndex));
719         if (path == null)
720         {
721                 pAlbumArtBitmap = ResourceManager::GetBitmapN(L"34_thumb_07.png");
722                 AppLogDebug("EXIT");
723                 return pAlbumArtBitmap;
724         }
725
726         pAlbumArtBitmap = CommonUtil::GetContentAlbumArtN(*path);
727         if (pAlbumArtBitmap == null)
728         {
729                 pAlbumArtBitmap = ResourceManager::GetBitmapN(L"34_thumb_07.png");
730         }
731         AppLogDebug("EXIT");
732         return pAlbumArtBitmap;
733 }
734
735 result
736 PlayerPresentationModel::Play(int contentIndex, int sliderPosition, bool bPausePlay, bool bPlayPause)
737 {
738         AppLogDebug("ENTER");
739         result r = E_SUCCESS;
740
741         if (__pPlayer == null)
742         {
743                 AppLogDebug("EXIT");
744                 return E_FAILURE;
745         }
746
747         __seekCompleted = true;
748 /*      if (__seekCompleted == false)
749         {
750                 AppLogDebug("EXIT");
751                 return E_FAILURE;
752         }
753 */
754         PlayerState playerState;
755         String currentDuration;
756         String* pPath = null;
757
758         if (bPausePlay == false)
759         {
760                 if (GetContentListCount() == 0)
761                 {
762                         ClosePlayer();
763                         InitializeProgress();
764                 }
765                 TryCatch(contentIndex >= 0 && contentIndex < __pPlayContentList->GetCount(), , "contentIndex is invalid");
766                 pPath = GetContentPath(contentIndex);
767                 AppLogDebug("__pCurrentPlayContentPath = %ls", __pCurrentPlayContentPath->GetPointer());
768                 if (__pPlayer->GetState() == PLAYER_STATE_ENDOFCLIP
769                         || pPath->Equals(*__pCurrentPlayContentPath) == false || __currentContentIndex != contentIndex || __isForceChanged)
770                 {
771                         __isForceChanged = false;
772                         ClosePlayer();
773                         InitializeProgress();
774                         InitializePlayer();
775                         __previousPlayerState = PLAYER_STATE_INITIALIZED;
776                         __currentContentIndex = contentIndex;
777                         GenerateNextIndex();
778                         __pCurrentPlayContentPath->Clear();
779                         __pCurrentPlayContentPath->Append(pPath->GetPointer());
780
781                         if (__pCurrentAudioContentInfo != null)
782                         {
783                                 delete __pCurrentAudioContentInfo;
784                                 __pCurrentAudioContentInfo = null;
785                         }
786
787                         __pCurrentAudioContentInfo = CommonUtil::GetAudioContentInfoN(*(__pCurrentPlayContentPath));
788
789                         if (__pCurrentAudioContentInfo != null)
790                         {
791                                 if (__pCurrentPlayContentTitle != null)
792                                 {
793                                         delete __pCurrentPlayContentTitle;
794                                 }
795                                 if(__pCurrentPlayContentThumbnail != null)
796                                 {
797                                         delete __pCurrentPlayContentThumbnail;
798                                 }
799
800                                 __pCurrentPlayContentTitle = new (std::nothrow) String(__pCurrentAudioContentInfo->GetTitle());
801                                 __pCurrentPlayContentThumbnail = __pCurrentAudioContentInfo->GetThumbnailN();
802                                 if (__pCurrentPlayContentThumbnail == null)
803                                 {
804                                         __pCurrentPlayContentThumbnail = ResourceManager::GetBitmapN(L"34_thumb_07.png");
805                                 }
806
807                                 if (__pCurrentPlayContentArtist != null)
808                                 {
809                                         delete __pCurrentPlayContentArtist;
810                                 }
811                                 __pCurrentPlayContentArtist = new (std::nothrow) String(__pCurrentAudioContentInfo->GetArtist());
812                                 __currentPlayContentDuration = __pCurrentAudioContentInfo->GetDuration();
813                                 if (__currentPlayContentDuration == null)
814                                 {
815                                         __currentPlayContentDuration = __pPlayer->GetDuration();
816                                 }
817                         }
818                         else
819                         {
820                                 Tizen::Content::AudioMetadata* pAudioMeta = Tizen::Content::ContentManagerUtil::GetAudioMetaN(*static_cast<String*>(__pPlayContentList->GetAt(contentIndex)));
821                                 if (__pCurrentPlayContentTitle != null)
822                                 {
823                                         delete __pCurrentPlayContentTitle;
824                                 }
825                                 if (__pCurrentPlayContentArtist != null)
826                                 {
827                                         delete __pCurrentPlayContentArtist;
828                                 }
829
830                                 if (pAudioMeta == null)
831                                 {
832                                         __pCurrentPlayContentTitle = new (std::nothrow) String(ResourceManager::GetString(L"IDS_MUSIC_BODY_UNKNOWN"));
833                                         __pCurrentPlayContentArtist = new (std::nothrow) String(ResourceManager::GetString(L"IDS_MUSIC_BODY_UNKNOWN"));
834                                         __pCurrentPlayContentThumbnail = ResourceManager::GetBitmapN(L"34_thumb_07.png");
835                                         __currentPlayContentDuration = __pPlayer->GetDuration();
836                                 }
837                                 else
838                                 {
839                                         __pCurrentPlayContentTitle = new (std::nothrow) String(pAudioMeta->GetTitle());
840                                         if (__pCurrentPlayContentTitle == null)
841                                         {
842                                                 __pCurrentPlayContentTitle = new (std::nothrow) String(ResourceManager::GetString(L"IDS_MUSIC_BODY_UNKNOWN"));
843                                         }
844
845                                         __pCurrentPlayContentArtist = new (std::nothrow) String(pAudioMeta->GetArtist());
846                                         if (__pCurrentPlayContentArtist == null)
847                                         {
848                                                 __pCurrentPlayContentArtist = new (std::nothrow) String(ResourceManager::GetString(L"IDS_MUSIC_BODY_UNKNOWN"));
849                                         }
850
851                                         __pCurrentPlayContentThumbnail = pAudioMeta->GetThumbnailN();
852                                         if (__pCurrentPlayContentThumbnail == null)
853                                         {
854                                                 __pCurrentPlayContentThumbnail = ResourceManager::GetBitmapN(L"34_thumb_07.png");
855                                         }
856
857                                         __currentPlayContentDuration = pAudioMeta->GetDuration();
858                                         if (__currentPlayContentDuration == null)
859                                         {
860                                                 __currentPlayContentDuration = __pPlayer->GetDuration();
861                                         }
862
863                                         delete pAudioMeta;
864                                 }
865                         }
866                 }
867                 else
868                 {
869                 /*      IEnumerator* pEnum = __pPlayContentList->GetEnumeratorN();
870                         String* pFilePath = null;
871                         int index = 0;
872
873                         if(pEnum != null)
874                         {
875                                 while (pEnum->MoveNext() == E_SUCCESS)
876                                 {
877                                         pFilePath = static_cast<String*>(pEnum->GetCurrent());
878
879                                         if (pFilePath->Equals(*__pCurrentPlayContentPath))
880                                         {
881                                                 __currentContentIndex = index;
882                                                 GenerateNextIndex();
883                                                 break;
884                                         }
885                                         index++;
886                                 }
887
888                                 delete pEnum;
889                         }*/
890                         if (__pPlayer->GetState() == PLAYER_STATE_PLAYING)
891                         {
892                                 CallOnPlayContentChanged(__currentContentIndex);
893                                 return E_SUCCESS;
894                         }
895                 }
896         }
897
898         playerState = __pPlayer->GetState();
899
900         switch (playerState)
901         {
902         case PLAYER_STATE_PLAYING:
903                 {
904                         CallOnPlayContentChanged(__currentContentIndex);
905                 }
906                 break;
907
908         case PLAYER_STATE_STOPPED:
909                 // fall through
910         case PLAYER_STATE_OPENED:
911                 // fall through
912         case PLAYER_STATE_PAUSED:
913                 // fall through
914         case PLAYER_STATE_ENDOFCLIP:
915                 {
916                         if (__pPauseAutoOffTimer != null)
917                         {
918                                 r = __pPauseAutoOffTimer->Cancel();
919                                 TryLog(r == E_SUCCESS, "__pPauseAutoOffTimer cancel failed\n(%s)", GetErrorMessage(r));
920                         }
921
922                         AppLogDebug("%d", __pPlayer->GetState());
923                         r = __pPlayer->Play();
924                         TryCatch(r == E_SUCCESS, , "Play failed(%s)", GetErrorMessage(r));
925                         if (sliderPosition != 0 && playerState != PLAYER_STATE_PAUSED)
926                         {
927                                 r = __pPlayer->SeekTo(sliderPosition * (__pPlayer->GetDuration() / TIMER_INTERVAL));
928                                 TryCatch(r == E_SUCCESS, , "Unable to seek to.\n");
929                                 __playposition = sliderPosition;
930                         }
931                         if (bPausePlay == false)
932                         {
933                                 CallOnPlayContentChanged(__currentContentIndex);
934                         }
935                         if (__pPlayStateTimer != null)
936                         {
937                                 __pPlayStateTimer->Cancel();
938                                 r = __pPlayStateTimer->Start(MESSAGE_BOX_DELAY/2);
939                                 TryCatch(r == E_SUCCESS, , "__pPlayStateTimer start failed..error %s\n",GetErrorMessage(r));
940                         }
941                         currentDuration = CommonUtil::GetFormatDate(__pPlayer->GetPosition());
942                 //      CallOnPlayTimeChanged(currentDuration, __playposition);
943                 }
944                 break;
945
946         case PLAYER_STATE_ERROR:
947                 {
948                         AppLogDebug("PlayerState : %d", playerState);
949                         r = E_FAILURE;
950                         TryCatch(r == E_SUCCESS, , "PLAYER_STATE_ERROR occured.\n");
951                 }
952                 break;
953
954         case PLAYER_STATE_CLOSED:
955                 // fall through
956         case PLAYER_STATE_INITIALIZED:
957                 {
958                         TryCatch(__pPlayer != null && pPath != null, r = E_FAILURE, "Player is not initialized or pPath is null.");
959                         r = __pPlayer->OpenFile(*pPath);
960                         TryCatch(r == E_SUCCESS, , "[%ls] OpenFile failed\n(%s).", pPath->GetPointer(), GetErrorMessage(r));
961
962                         if ((__previousPlayerState == PLAYER_STATE_PLAYING) || (__previousPlayerState == PLAYER_STATE_ENDOFCLIP)
963                                 || (__previousPlayerState == PLAYER_STATE_INITIALIZED))
964                         {
965                                 if (__pPauseAutoOffTimer != null)
966                                 {
967                                         r = __pPauseAutoOffTimer->Cancel();
968                                         TryLog(r == E_SUCCESS, "__pPauseAutoOffTimer cancel failed\n(%s)", GetErrorMessage(r));
969                                 }
970                                 if (bPlayPause)
971                                 {
972                                         r = __pPlayer->SetMute(true);
973                                         TryLog(r == E_SUCCESS,"__pPlayer->SetMute(true) failed with %s",GetErrorMessage(r));
974                                 }
975                                 r = __pPlayer->Play();
976                                 TryCatch(r == E_SUCCESS, , "Play failed\n(%s)", GetErrorMessage(r));
977                                 if (sliderPosition != 0 && playerState != PLAYER_STATE_PAUSED)
978                                 {
979                                         r = __pPlayer->SeekTo(sliderPosition * (__pPlayer->GetDuration() / TIMER_INTERVAL));
980                                         TryCatch(r == E_SUCCESS, , "Unable to seek to.\n");
981                                         __playposition = sliderPosition;
982                                 }
983                                 __currentContentTotalDuration = __pPlayer->GetDuration();
984                                 if (__pPlayStateTimer != null)
985                                 {
986                                         __pPlayStateTimer->Cancel();
987                                         r = __pPlayStateTimer->Start(MESSAGE_BOX_DELAY/2);
988                                         TryCatch(r == E_SUCCESS, , "__pPlayStateTimer start failed\n(%s)", GetErrorMessage(r));
989                                 }
990                         }
991
992                         r = UpdateContentDB();
993                         if (r == E_STORAGE_FULL)
994                         {
995                                 int messageResult = 0;
996                                 MessageBox messageBox;
997                                 messageBox.Construct(L"",IDS_STORAGE_FULL,MSGBOX_STYLE_NONE,COUNT_MESSAGE_BOX_TIMEOUT);
998                                 messageBox.ShowAndWait(messageResult);
999                                 App::GetInstance()->Terminate();
1000                                 return r;
1001                         }
1002                         if (bPausePlay == false)
1003                         {
1004                                 CallOnPlayContentChanged(__currentContentIndex);
1005                                 currentDuration = CommonUtil::GetFormatDate(__pPlayer->GetPosition());
1006                                 CallOnPlayTimeChanged(currentDuration, sliderPosition);
1007                         }
1008                 }
1009                 break;
1010
1011         case PLAYER_STATE_OPENING:
1012                 {
1013                 }
1014                 break;
1015
1016         default:
1017                 break;
1018         }
1019
1020         CallOnPlayStateChanged(__pPlayer->GetState());
1021         AppLogDebug("EXIT");
1022         return E_SUCCESS;
1023
1024 CATCH:
1025         currentDuration = CommonUtil::GetFormatDate(MIN_SLIDER_COUNT);
1026         CallOnPlayTimeChanged(currentDuration,MIN_SLIDER_COUNT);
1027         CallOnPlayStateChanged(PLAYER_STATE_ERROR);
1028         StopForwardRewind();
1029         return E_FAILURE;
1030 }
1031
1032 void
1033 PlayerPresentationModel::Pause(void)
1034 {
1035         AppLogDebug("ENTER");
1036         if (__pPlayer == null)
1037         {
1038                 AppLogDebug("EXIT");
1039                 return;
1040         }
1041
1042         result r = E_SUCCESS;
1043         PlayerState playerState = __pPlayer->GetState() ;
1044         AppLogDebug("%d", playerState);
1045         if (playerState == PLAYER_STATE_PLAYING)
1046         {
1047                 r = __pPlayer->Pause();
1048                 TryCatch(r == E_SUCCESS, , "__pPlayer pause failed..%s\n",GetErrorMessage(r));
1049         }
1050         if (__pPlayer->GetState() == PLAYER_STATE_PAUSED)
1051         {
1052                 if (__pPlayStateTimer != null)
1053                 {
1054                         r = __pPlayStateTimer->Cancel();
1055                         TryCatch(r == E_SUCCESS, , "__pPlayStateTimer cancel failed..\n");
1056                 }
1057                 if (__pPauseAutoOffTimer != null)
1058                 {
1059                         /*__pPauseAutoOffTimer->Start(3000);*/
1060                         r = __pPauseAutoOffTimer->Start(PAUSE_AUTO_OFF_TIME);
1061                         TryCatch(r == E_SUCCESS, , "__pPauseAutoOffTimer start failed..\n");
1062                 }
1063         }
1064         CallOnPlayStateChanged(__pPlayer->GetState());
1065         AppLogDebug("EXIT");
1066         return;
1067
1068 CATCH:
1069         //AppLogException("[%ls]", GetErrorMessage(r));
1070         return;
1071 }
1072
1073 void PlayerPresentationModel::Mute(const bool isMute)
1074 {
1075         AppLogDebug("ENTER");
1076         if (__pPlayer == null)
1077         {
1078                 AppLogDebug("EXIT");
1079                 return;
1080         }
1081         __pPlayer->SetMute(isMute);
1082         AppLogDebug("EXIT");
1083 }
1084
1085 void
1086 PlayerPresentationModel::MovePreviousContent(bool forceMove)
1087 {
1088         AppLogDebug("ENTER");
1089         if (__pPlayer == null)
1090         {
1091                 AppLogDebug("EXIT");
1092                 return;
1093         }
1094
1095         result r = E_SUCCESS;
1096         String currentDuration;
1097
1098 //      if ((__pPlayer->GetState() == PLAYER_STATE_PLAYING || __pPlayer->GetState() == PLAYER_STATE_PAUSED) && (__playposition > MIN_REWIND_POSITION))
1099         if (__playposition > MIN_REWIND_POSITION && forceMove != true)
1100         {
1101                 r = __pPlayer->SeekTo(MIN_SLIDER_COUNT);
1102                 TryCatch(r == E_SUCCESS, , "SeekTo failed. [%ls]", GetErrorMessage(r));
1103                 __playposition = MIN_SLIDER_COUNT;
1104                 currentDuration = CommonUtil::GetFormatDate(__pPlayer->GetPosition());
1105                 CallOnPlayTimeChanged(currentDuration, __playposition);
1106                 AppLogDebug("EXIT");
1107                 return;
1108         }
1109         else
1110         {
1111                 __isForceChanged = true;
1112         }
1113
1114 //      ClosePlayer();
1115 //      InitializeProgress();
1116 //      InitializePlayer();
1117 /*      if(IsShuffleEnable())
1118         {
1119                 srand(int(time(NULL)));
1120                 __currentContentIndex = rand() % __pPlayContentList->GetCount();
1121         }
1122         else
1123         {
1124                 if (__currentContentIndex == 0 || __removedContentIndex == __pPlayContentList->GetCount())
1125         {
1126                 __currentContentIndex = __pPlayContentList->GetCount() - 1;
1127         }
1128         else
1129         {
1130                 __currentContentIndex--;
1131         }
1132
1133                 __removedContentIndex = INIT_CONTENT_INDEX;
1134         }*/
1135
1136         __currentContentIndex = __previousContentIndex;
1137         GenerateNextIndex();
1138         __removedContentIndex = INIT_CONTENT_INDEX;
1139
1140         if (__pPlayContentList->GetCount() > 0)
1141         {
1142                 CallOnPlayContentChanged(__currentContentIndex);
1143                 if (GetPlayerState() == PLAYER_STATE_PAUSED)
1144                 {
1145                         __seekCompleted = false;
1146                         Play(__currentContentIndex, MIN_SLIDER_COUNT,false,true);
1147                         Pause();
1148                         __pPlayer->SeekTo(MIN_SLIDER_COUNT);
1149                         //need to unmute the player as it is mute in case of moved next in pause state
1150                         __pPlayer->SetMute(false);
1151                         InitializeProgress();
1152                 }
1153         }
1154 //      Play(__currentContentIndex, MIN_SLIDER_COUNT);
1155
1156         AppLogDebug("EXIT");
1157         return;
1158
1159 CATCH:
1160         //AppLogException("[%s]", GetErrorMessage(r));
1161         return;
1162 }
1163
1164 void
1165 PlayerPresentationModel::MoveNextContent(void)
1166 {
1167         AppLogDebug("ENTER");
1168         if (__pPlayer == null)
1169         {
1170                 AppLogDebug("EXIT");
1171                 return;
1172         }
1173         __currentContentIndex = __nextContentIndex;
1174         GenerateNextIndex();
1175         __removedContentIndex = INIT_CONTENT_INDEX;
1176         __isForceChanged = true;
1177         if (__pPlayContentList->GetCount() > 0)
1178         {
1179                 CallOnPlayContentChanged(__currentContentIndex);
1180
1181                 if (GetPlayerState() == PLAYER_STATE_PAUSED)
1182                 {
1183                         __seekCompleted = false;
1184                         Play(__currentContentIndex, MIN_SLIDER_COUNT,false,true);
1185                         Pause();
1186 //                      __pPlayer->SeekTo(MIN_SLIDER_COUNT);
1187                         //need to unmute the player as it is mute in case of moved next in pause state
1188                         __pPlayer->SetMute(false);
1189                         InitializeProgress();
1190                 }
1191         }
1192 //      Play(__currentContentIndex, MIN_SLIDER_COUNT);
1193
1194         AppLogDebug("EXIT");
1195         return;
1196 }
1197
1198 void
1199 PlayerPresentationModel::SeekToRewind(void)
1200 {
1201         AppLogDebug("ENTER");
1202         int playDuration = __pPlayer->GetDuration();
1203         if(playDuration != 0)
1204         {
1205                 //__playposition = static_cast<int>(__pPlayer->GetPosition() / (playDuration / TIMER_INTERVAL));
1206                 __playposition = static_cast<int>(__pPlayer->GetPosition() * TIMER_INTERVAL / playDuration);
1207                 Rewind();
1208         }
1209         AppLogDebug("EXIT");
1210 }
1211
1212 void
1213 PlayerPresentationModel::Rewind(void)
1214 {
1215         AppLogDebug("ENTER");
1216         result r = E_SUCCESS;
1217         __pPlayStateTimer->Cancel();
1218         int newPosition = __playposition - __xTimesValue;
1219
1220         if (__playposition != newPosition)
1221         {
1222                 __playposition = newPosition;
1223                 r = SetPlayPosition(__playposition);
1224                 if (IsFailed(r))
1225                 {
1226                         StopForwardRewind();
1227                         // if there is some error in reading from the content file
1228                         // it needs to show the message and Update the Ui accordingly.
1229                         if (r == E_INVALID_DATA)
1230                         {
1231                                 __pPlayer->Stop();
1232                                 InitializeProgress();
1233                                 InitializePlayer();
1234                                 CallOnPlayStateChanged(PLAYER_STATE_ERROR);
1235                         }
1236                         return;
1237                 }
1238         }
1239
1240         if (__playposition < MIN_SLIDER_COUNT && GetPlayerState() == PLAYER_STATE_PAUSED)
1241         {
1242                 StopForwardRewind();
1243                 return;
1244         }
1245
1246         if (__pFastRewindTimer != null)
1247         {
1248                 r = __pFastRewindTimer->Start(PROGRESS_TIMER_DURATION);
1249                 TryCatch(r == E_SUCCESS, , "__pFastRewindTimer start failed. [%ls]", GetErrorMessage(r));
1250         }
1251
1252         AppLogDebug("EXIT");
1253         return;
1254
1255 CATCH:
1256         //AppLogException("[%ls]", GetErrorMessage(r));
1257         return;
1258 }
1259
1260 void
1261 PlayerPresentationModel::SeekToForward(void)
1262 {
1263         AppLogDebug("ENTER");
1264         int playDuration = __pPlayer->GetDuration();
1265         if (playDuration > 0)
1266         {
1267                 __playposition = static_cast<int>(__pPlayer->GetPosition() * TIMER_INTERVAL / playDuration);
1268                 Forward();
1269         }
1270
1271         AppLogDebug("EXIT");
1272 }
1273
1274 void
1275 PlayerPresentationModel::Forward(void)
1276 {
1277         AppLogDebug("ENTER");
1278         result r = E_SUCCESS;
1279         __pPlayStateTimer->Cancel();
1280         int newPosition = __playposition - __xTimesValue;
1281
1282         if (__playposition != newPosition)
1283         {
1284                 __playposition = newPosition;
1285                 __playposition = __playposition - __xTimesValue;
1286                 r = SetPlayPosition(__playposition);
1287                 if (IsFailed(r))
1288                 {
1289                         AppLogDebug("SetPlayPosition Failed %s",GetErrorMessage(r));
1290                         StopForwardRewind();
1291                         // if there is some error in reading from the content file
1292                         // it needs to show the message and Update the Ui accordingly.
1293                         if (r == E_INVALID_DATA)
1294                         {
1295                                 __pPlayer->Stop();
1296                                 InitializeProgress();
1297                                 InitializePlayer();
1298                                 CallOnPlayStateChanged(PLAYER_STATE_ERROR);
1299                         }
1300
1301                         return;
1302                 }
1303         }
1304
1305         if (__pFastForwardTimer != null)
1306         {
1307                 r = __pFastForwardTimer->Start(PROGRESS_TIMER_DURATION);
1308                 TryCatch(r == E_SUCCESS, , "__pFastForwardTimer start failed. [%ls]", GetErrorMessage(r));
1309         }
1310
1311         AppLogDebug("EXIT");
1312         return;
1313
1314 CATCH:
1315         //AppLogException("[%ls]", GetErrorMessage(r));
1316         return;
1317 }
1318
1319 void
1320 PlayerPresentationModel::StopForwardRewind(void)
1321 {
1322         AppLogDebug("ENTER");
1323         result r = E_SUCCESS;
1324
1325         if (__pFastForwardTimer != null)
1326         {
1327                 r = __pFastForwardTimer->Cancel();
1328                 TryLog(r == E_SUCCESS, "[%s] __pFastForwardTimer cancel failed", GetErrorMessage(r));
1329         }
1330
1331         if (__pFastRewindTimer != null)
1332         {
1333                 r = __pFastRewindTimer->Cancel();
1334                 TryLog(r == E_SUCCESS, "[%s] __pFastRewindTimer cancel failed", GetErrorMessage(r));
1335         }
1336         __xTimesValue = INIT_X_VALUE;
1337         __seekCompleted = true;
1338         __pPlayStateTimer->Cancel();
1339         r = __pPlayStateTimer->Start(MESSAGE_BOX_DELAY/2);
1340
1341         AppLogDebug("EXIT");
1342         return;
1343 }
1344
1345 result
1346 PlayerPresentationModel::SetPlayPosition(int playPosition)
1347 {
1348         AppLogDebug("ENTER %d",playPosition);
1349         if (__pPlayer == null)
1350         {
1351                 AppLogDebug("EXIT");
1352                 return E_FAILURE;
1353         }
1354
1355         result r = E_SUCCESS;
1356         String currentDuration;
1357         if (/*(playPosition >= MIN_SLIDER_COUNT) && (playPosition <= TIMER_INTERVAL) &&*/ __seekCompleted == true)
1358         {
1359                 if (GetPlayerState() == PLAYER_STATE_PLAYING || GetPlayerState() == PLAYER_STATE_PAUSED || GetPlayerState() == PLAYER_STATE_OPENED)
1360                 {
1361                         if (playPosition >= MAX_SLIDER_COUNT)
1362                         {
1363                                 r = __pPlayer->SeekTo(__pPlayer->GetDuration());
1364                                 TryCatch(r == E_SUCCESS, , "__pPlayer seekto failed..\n");
1365                                 __playposition = MAX_SLIDER_COUNT;
1366 //                              StopForwardRewind();
1367 //                              __seekCompleted = false;
1368 //                              return;
1369                         }
1370                         else if (playPosition <= MIN_SLIDER_COUNT)
1371                         {
1372                                 r = __pPlayer->SeekTo(MIN_SLIDER_COUNT);
1373                                 TryCatch(r == E_SUCCESS, , "__pPlayer seekto failed..\n");
1374                                 __playposition = MIN_SLIDER_COUNT;
1375                         }
1376                         else
1377                         {
1378                                 int position = playPosition * (__pPlayer->GetDuration() / TIMER_INTERVAL);
1379                                 AppLogDebug("position(%d)", position);
1380                                 r = __pPlayer->SeekTo(position);
1381                                 TryCatch(r == E_SUCCESS, , "__pPlayer seekto failed..\n");
1382                                 __playposition = playPosition;
1383                         }
1384                         TryCatch(r == E_SUCCESS, r = E_FAILURE, "Unable to seek to.\n");
1385                 //currentDuration = CommonUtil::GetFormatDate(__pPlayer->GetPosition());
1386                 //CallOnPlayTimeChanged(currentDuration, __playposition);
1387                         __seekCompleted = false;
1388                 }
1389                 else
1390                 {
1391 //                      __playposition = playPosition;
1392 //                      currentDuration = CommonUtil::GetFormatDate(playPosition * (__currentPlayContentDuration / TIMER_INTERVAL));
1393 //                      CallOnPlayTimeChanged(currentDuration, __playposition);
1394                         __seekCompleted = true;
1395                 }
1396         }
1397
1398         AppLogDebug("EXIT");
1399         return r;
1400
1401 CATCH:
1402         //AppLogException( "[%s]", GetErrorMessage(r));
1403         if (__pPlayer->GetDuration() > MIN_SLIDER_COUNT)
1404         {
1405                 currentDuration = CommonUtil::GetFormatDate(__pPlayer->GetPosition());
1406                 CallOnPlayTimeChanged(currentDuration, __playposition);
1407         }
1408         __seekCompleted = false;
1409         AppLogDebug("EXIT");
1410         return r;
1411 }
1412
1413 int
1414 PlayerPresentationModel::GetPlayPosition(void)
1415 {
1416         AppLogDebug("ENTER");
1417         AppLogDebug("EXIT");
1418         return __playposition;
1419 }
1420
1421 String
1422 PlayerPresentationModel::GetCurrentDuration(void)
1423 {
1424         AppLogDebug("ENTER");
1425         String currentDuration;
1426         if (__pPlayer != null)
1427         {
1428                 currentDuration = CommonUtil::GetFormatDate(__pPlayer->GetPosition());
1429         }
1430         else
1431         {
1432                 currentDuration = CommonUtil::GetFormatDate(INIT_VALUE);
1433         }
1434         AppLogDebug("EXIT");
1435         return currentDuration;
1436 }
1437
1438 result
1439 PlayerPresentationModel::SetContentList(IList* pArgs)
1440 {
1441         AppLogDebug("ENTER");
1442         TryReturn( pArgs != null && pArgs->GetCount() > 0, E_INVALID_ARG, "Invalid arguments");
1443         result r = E_SUCCESS;
1444         String* pTemp = null;
1445
1446         if (__pPlayContentList == null)
1447         {
1448                 __pPlayContentList = new (std::nothrow) ArrayList();
1449                 TryCatch(__pPlayContentList != null, r = E_OUT_OF_MEMORY, "Unable to create ArrayList.\n");
1450
1451                 r = __pPlayContentList->Construct();
1452                 TryCatch(r == E_SUCCESS, r = E_OUT_OF_MEMORY, "Unable to Construct ArrayList.\n");
1453         }
1454         else
1455         {
1456                 __pPlayContentList->RemoveAll(true);
1457         }
1458
1459         pTemp = static_cast<String*>(pArgs->GetAt(0));
1460         if (pTemp->Equals(MUSIC, true))
1461         {
1462                 ArrayList* pContentList = static_cast<ArrayList*>(pArgs->GetAt(2));
1463                 TryReturn(pContentList != null, E_FAILURE, "pArgs->GetAt(2) is null.")
1464
1465                 IEnumerator* pEnum = pContentList->GetEnumeratorN();
1466
1467                 if (pEnum != null)
1468                 {
1469                         while (pEnum->MoveNext() == E_SUCCESS)
1470                         {
1471                                 String* pContentPath = static_cast<String*>(pEnum->GetCurrent());
1472                                 AppLogDebug("%ls", pContentPath->GetPointer());
1473                                 __pPlayContentList->Add(new (std::nothrow) String(*pContentPath));
1474                         }
1475                         delete pEnum;
1476                 }
1477         }
1478         else
1479         {
1480                 ArrayList* pContentList = static_cast<ArrayList*>(pArgs->GetAt(0));
1481                 TryReturn(pContentList != null, E_FAILURE, "pArgs->GetAt(0) is null.")
1482
1483                 IEnumerator* pEnum = pContentList->GetEnumeratorN();
1484                 if (pEnum != null)
1485                 {
1486                         while (pEnum->MoveNext() == E_SUCCESS)
1487                         {
1488                                 String* pContentPath = static_cast<String*>(pEnum->GetCurrent());
1489                                 AppLogDebug("%ls", pContentPath->GetPointer());
1490                                 __pPlayContentList->Add(new (std::nothrow) String(*pContentPath));
1491                         }
1492
1493                         delete pEnum;
1494                 }
1495                 __isForceChanged = true;
1496         }
1497
1498         AppLogDebug("EXIT");
1499         return r;
1500
1501 CATCH:
1502         //AppLogException("[%s].\n",GetErrorMessage(r));
1503         if (__pPlayContentList != null)
1504         {
1505                 delete __pPlayContentList;
1506                 __pPlayContentList = null;
1507         }
1508         return r;
1509 }
1510
1511 int
1512 PlayerPresentationModel::GetContentListCount(void)const
1513 {
1514         AppLogDebug("ENTER");
1515         if (__pPlayContentList != null)
1516         {
1517                 AppLogDebug("EXIT");
1518                 return __pPlayContentList->GetCount();
1519         }
1520         else
1521         {
1522                 AppLogDebug("EXIT");
1523                 return INIT_VALUE;
1524         }
1525 }
1526
1527 result
1528 PlayerPresentationModel::SetVolume(int volume)
1529 {
1530         AppLogDebug("ENTER");
1531         String key(MEDIA_VOLUME);
1532         result r = E_SUCCESS;
1533
1534         if (__pVolumeBarStateTimer != null)
1535         {
1536                 r = __pVolumeBarStateTimer->Cancel();
1537                 TryLog(r == E_SUCCESS, "[%s] __pVolumeBarStateTimer cancel failed.", GetErrorMessage(r));
1538                 r = __pVolumeBarStateTimer->Start(VOLUMEBAR_INTERVAL);
1539                 TryCatch(r == E_SUCCESS, r = E_OUT_OF_MEMORY, "Unable to Start __pVolumeBarStateTimer.\n");
1540         }
1541
1542         if (volume >= 0 && volume <= 15)
1543         {
1544                 SettingInfo::SetValue(key, volume);
1545         }
1546         AppLogDebug("EXIT");
1547         return r;
1548
1549 CATCH:
1550         //AppLogException("[%ls]", GetErrorMessage(r));
1551         return r;
1552 }
1553
1554 int
1555 PlayerPresentationModel::GetVolume(void)
1556 {
1557         AppLogDebug("ENTER");
1558         int volume = 0;
1559         String key(MEDIA_VOLUME);
1560         SettingInfo::GetValue(key, volume);
1561         AppLogDebug("EXIT");
1562         return volume;
1563 }
1564
1565 void
1566 PlayerPresentationModel::SetShuffleEnable(bool shuffleEnable)
1567 {
1568         AppLogDebug("ENTER");
1569 /*      int __totalCount = 0;
1570
1571         if (__pPlayContentList != null)
1572         {
1573                 __totalCount = __pPlayContentList->GetCount();
1574         }
1575
1576         if (shuffleEnable == true)
1577         {// 0 - 1 (OFF, ON)
1578                 int nDust;
1579                 int nSour;
1580                 int nTemp;
1581                 srand(int(time(NULL)));
1582                 for (int i = 0; i < __totalCount; i++)
1583                 {
1584                         nDust = rand() % __totalCount;
1585                         nSour = rand() % __totalCount;
1586
1587                         nTemp = __pPlayContentIndexOrder[nDust];
1588                         __pPlayContentIndexOrder[nDust] = __pPlayContentIndexOrder[nSour];
1589                         __pPlayContentIndexOrder[nSour] = nTemp;
1590                 }
1591
1592                 for (int i = 0; i < __totalCount; i++)
1593                 {
1594                         if (__pPlayContentIndexOrder[i] - 1 == __currentContentIndex)
1595                         {
1596                                 nTemp = __pPlayContentIndexOrder[i];
1597                                 __pPlayContentIndexOrder[i] = __pPlayContentIndexOrder[__currentContentIndex];
1598                                 __pPlayContentIndexOrder[__currentContentIndex] = nTemp;
1599                         }
1600                 }
1601         }
1602         else
1603         {
1604                 int currentIndex = __currentContentIndex;
1605                 __currentContentIndex = __pPlayContentIndexOrder[currentIndex] - 1;
1606                 for (int i=0; i<__totalCount; i++)
1607                 {
1608                         __pPlayContentIndexOrder[i]=i + 1;
1609                 }
1610         }*/
1611         __shuffleEnable = shuffleEnable;
1612         GenerateNextIndex();
1613         AppLogDebug("EXIT");
1614 }
1615
1616 void
1617 PlayerPresentationModel::SetRepeatMode(int repeatMode)
1618 {
1619         AppLogDebug("ENTER");
1620         __repeatMode = repeatMode;
1621         AppLogDebug("EXIT");
1622 }
1623
1624 bool
1625 PlayerPresentationModel::IsShuffleEnable(void)
1626 {
1627         AppLogDebug("ENTER");
1628         AppLogDebug("EXIT");
1629         return __shuffleEnable;
1630 }
1631
1632 bool
1633 PlayerPresentationModel::IsFileExist(int contentIndex) const
1634 {
1635         AppLogDebug("ENTER");
1636         TryReturn(contentIndex >= 0 && contentIndex < __pPlayContentList->GetCount(), false, "contentIndex is invalid.")
1637         String* pFilePath = GetContentPath(contentIndex);
1638
1639         File file;
1640
1641         result r = file.Construct(*pFilePath, "r");
1642         TryReturn(r == E_SUCCESS, false, "File construct was failed.")
1643
1644         AppLogDebug("EXIT");
1645         return file.IsFileExist(*pFilePath);
1646 }
1647
1648 int
1649 PlayerPresentationModel::GetRepeatState(void)
1650 {
1651         AppLogDebug("ENTER");
1652         AppLogDebug("EXIT");
1653         return __repeatMode;
1654 }
1655
1656 String
1657 PlayerPresentationModel::GetFileName(String* pFilePath)
1658 {
1659         AppLogDebug("ENTER");
1660         String path = Tizen::Io::File::GetFileName(*pFilePath);
1661         String strRemoveTemp = IDS_BLANK;
1662         int removeIndex = -1 ;
1663         path.LastIndexOf(IDS_PERIOD, path.GetLength() - 1, removeIndex);
1664         path.SubString(removeIndex + 1, strRemoveTemp);
1665         String strTemp = IDS_BLANK;
1666
1667         path.Remove(removeIndex, strRemoveTemp.GetLength() + 1);
1668         path.SubString(0, strTemp);
1669         AppLogDebug("EXIT");
1670         return strTemp;
1671 }
1672
1673 void
1674 PlayerPresentationModel::InitializeProgress(void)
1675 {
1676         AppLogDebug("ENTER");
1677         result r = E_SUCCESS;
1678
1679         String currentDuration;
1680         if (__pPlayer != null)
1681         {
1682                 currentDuration = CommonUtil::GetFormatDate(__pPlayer->GetPosition());
1683         }
1684         else
1685         {
1686                 currentDuration = INITIAL_TIME;
1687         }
1688
1689         if (__pPlayStateTimer != null /*&& __previousPlayerState != PLAYER_STATE_INITIALIZED*/
1690                 && __previousPlayerState != PLAYER_STATE_PAUSED
1691                 && __previousPlayerState != PLAYER_STATE_CLOSED)
1692         {
1693                 r = __pPlayStateTimer->Cancel();
1694                 TryLog(r == E_SUCCESS, "[%s] __pPlayStateTimer cancel failed.", GetErrorMessage(r));
1695         }
1696         __playposition = MIN_SLIDER_COUNT;
1697 //      __seekCompleted = true;
1698         __xTimesValue = INIT_VALUE;
1699         //CallOnPlayTimeChanged(currentDuration, __playposition);
1700         AppLogDebug("EXIT");
1701         return;
1702 }
1703
1704 result
1705 PlayerPresentationModel::UpdateContentDB(void)
1706 {
1707         AppLogDebug("ENTER");
1708         result r = E_SUCCESS;
1709         int playCount = 0;
1710         String contentIdString;
1711         ArrayList* pTempList = null;
1712
1713         Tizen::Base::String contentPath = *static_cast<String*>(__pPlayContentList->GetAt(__currentContentIndex));
1714         if (File::IsFileExist(contentPath) == false)
1715         {
1716                 AppLogDebug("File not exist(%ls)", contentPath.GetPointer());
1717                 return GetLastResult();
1718         }
1719
1720         ContentId contentId = CommonUtil::GetContentId(contentPath);
1721         if (IsFailed(GetLastResult()))
1722         {
1723                 AppLogDebug("CommonUtil::GetContentId(%ls) failed", contentPath.GetPointer());
1724                 return GetLastResult();
1725         }
1726
1727         DateTime playTime;
1728         Tizen::System::SystemTime::GetCurrentTime(Tizen::System::TIME_MODE_STANDARD, playTime);
1729         Tizen::Base::String playTimeString = playTime.ToString();
1730
1731         PlaylistDB* pPlaylistDB = new (std::nothrow) PlaylistDB();
1732         r = pPlaylistDB->CreatePlaylistDatabase();
1733         TryCatch(r == E_SUCCESS,,"CreatePlaylistDatabase() failed with error %s",GetErrorMessage(r));
1734
1735 //      pTempList = new (std::nothrow) ArrayList();
1736 //      pTempList->Construct();
1737
1738         contentIdString = contentId.ToString();
1739         if (pPlaylistDB->Read(contentIdString) != E_KEY_ALREADY_EXIST)
1740         {
1741                 r = pPlaylistDB->Insert(contentIdString, playCount + 1, playTimeString);
1742         }
1743         else
1744         {
1745                 pTempList = pPlaylistDB->ReadValueN(contentIdString);
1746                 if (pTempList != null)
1747                 {
1748                         playCount = static_cast<Integer*>(pTempList->GetAt(0))->ToInt();
1749                 }
1750                 r = pPlaylistDB->Update(contentIdString, playCount + 1, playTimeString);
1751         }
1752
1753 CATCH:
1754
1755         if (pPlaylistDB != null)
1756         {
1757                 delete pPlaylistDB;
1758                 pPlaylistDB = null;
1759         }
1760
1761         if (pTempList != null)
1762         {
1763                 pTempList->RemoveAll(true);
1764                 delete pTempList;
1765                 pTempList = null;
1766         }
1767         AppLogDebug("EXIT %s",GetErrorMessage(r));
1768         return r;
1769 }
1770
1771 void
1772 PlayerPresentationModel::GenerateNextIndex(void)
1773 {
1774         AppLogDebug("ENTER");
1775         if (IsShuffleEnable())
1776         {
1777                 int totalCount = __pPlayContentList->GetCount();
1778
1779                 if (totalCount < 2)
1780                 {
1781                         __previousContentIndex = 0;
1782                         __nextContentIndex = 0;
1783                 }
1784                 else
1785                 {
1786                         srand(int(time(NULL)));
1787                         __previousContentIndex = rand() % totalCount;
1788                         while (__previousContentIndex == __currentContentIndex)
1789                         {
1790                                 __previousContentIndex = rand() % totalCount;
1791                         }
1792
1793                         __nextContentIndex = rand() % totalCount;
1794                         while (__nextContentIndex == __currentContentIndex)
1795                         {
1796                                 __nextContentIndex = rand() % totalCount;
1797                         }
1798                 }
1799         }
1800         else
1801         {
1802                 if (__pPlayContentList->GetCount() == 0)
1803                 {
1804                         __previousContentIndex = 0;
1805                         __nextContentIndex = 0;
1806                         return;
1807                 }
1808
1809                 if (__currentContentIndex == 0 || __removedContentIndex == __pPlayContentList->GetCount())
1810                 {
1811                         __previousContentIndex = __pPlayContentList->GetCount() - 1;
1812                 }
1813                 else
1814                 {
1815                         __previousContentIndex = __currentContentIndex - 1;
1816                 }
1817
1818                 if (__currentContentIndex == __pPlayContentList->GetCount() - 1
1819                         || __removedContentIndex == __pPlayContentList->GetCount())
1820                 {
1821                         __nextContentIndex = 0;
1822                 }
1823                 else
1824                 {
1825                         if (__removedContentIndex != INIT_CONTENT_INDEX)
1826                         {
1827                                 __nextContentIndex = __previousContentIndex + 1;
1828                                 if (__nextContentIndex > __pPlayContentList->GetCount() - 1)
1829                                 {
1830                                         __nextContentIndex = 0;
1831                                 }
1832                         }
1833                         else
1834                         {
1835                                 __nextContentIndex = __currentContentIndex + 1;
1836                         }
1837                 }
1838         }
1839         AppLogDebug("EXIT");
1840 }
1841
1842 void
1843 PlayerPresentationModel::OnPlayerOpened(result r)
1844 {
1845         AppLogDebug("ENTER");
1846         AppLogDebug("EXIT");
1847 }
1848
1849 void
1850 PlayerPresentationModel::OnPlayerReleased(void)
1851 {
1852         AppLogDebug("ENTER");
1853         if (__pPlayer != null && (__pPlayer->GetState() == PLAYER_STATE_PAUSED))
1854         {
1855                 Play(__currentContentIndex, MIN_SLIDER_COUNT);
1856         }
1857         AppLogDebug("EXIT");
1858 }
1859
1860 void
1861 PlayerPresentationModel::OnPlayerEndOfClip(void)
1862 {
1863         AppLogDebug("ENTER");
1864 //      StopForwardRewind();
1865
1866         if (__pPlayer == null)
1867         {
1868                 AppLogDebug("EXIT");
1869                 return;
1870         }
1871         if (__seekCompleted)
1872         {
1873                 String totalDuration = CommonUtil::GetFormatDate(__currentContentTotalDuration);
1874                 AppLogDebug("totalDuration(%ls), __currentContentTotalDuration(%d)", totalDuration.GetPointer(), __currentContentTotalDuration);
1875                 CallOnPlayTimeChanged(totalDuration, MAX_SLIDER_COUNT);
1876                 __playposition = MIN_PLAY_STATE_SLIDER_COUNT;
1877         }
1878
1879         __seekCompleted = true;
1880         AppLogDebug("GetRepeatState() : %d", GetRepeatState());
1881         switch (GetRepeatState())// 0 - 2 (OFF, ONE, ALL)
1882         {
1883         case 1:
1884                 {
1885                         AppLogDebug("Repeat State(%d)", 1);
1886                         while (__pPlayContentList->GetCount() > 0 && __currentContentIndex < __pPlayContentList->GetCount() && IsFileExist(__currentContentIndex) == false)
1887                         {
1888                                 String* pContentPath = GetContentPath(__currentContentIndex);
1889
1890                                 if (pContentPath != null)
1891                                 {
1892                                         RemoveContent(*pContentPath);
1893                                 }
1894                         }
1895
1896                         //Need to play the currently loaded song in case there is no other content available in "Now Playing List"
1897                         if (__pPlayContentList->GetCount() == 0)
1898                         {
1899                                 InitializeProgress();
1900                                 Play(__currentContentIndex, MIN_SLIDER_COUNT, true);
1901                                 break;
1902                         }
1903
1904                         ClosePlayer();
1905                         InitializeProgress();
1906                         InitializePlayer();
1907                         Play(__currentContentIndex, MIN_SLIDER_COUNT);
1908                 }
1909                 break;
1910
1911         case 0:
1912                 {
1913                         AppLogDebug("Repeat State(%d)", 0);
1914                         if (__pPlayContentList->GetCount() - 1 == __currentContentIndex)
1915                         {
1916                                 StopForwardRewind();
1917                                 InitializeProgress();
1918                                 Play(__currentContentIndex, MIN_SLIDER_COUNT, true);
1919                                 Pause();
1920                         }
1921                         else
1922                         {
1923                                 while (__pPlayContentList->GetCount() > 0 && GetNextContentIndex() < __pPlayContentList->GetCount() && IsFileExist(GetNextContentIndex()) == false)
1924                                 {
1925                                         String* pContentPath = GetContentPath(GetNextContentIndex());
1926
1927                                         if (pContentPath != null)
1928                                         {
1929                                                 RemoveContent(*pContentPath);
1930                                         }
1931                                 }
1932                                 MoveNextContent();
1933                                 InitializeProgress();
1934                                 Play(__currentContentIndex, MIN_SLIDER_COUNT);
1935                         }
1936                 }
1937                 break;
1938
1939         case 2:
1940                 {
1941                         AppLogDebug("Repeat State(%d)", 2);
1942
1943                         while (__pPlayContentList->GetCount() > 0 && GetNextContentIndex() < __pPlayContentList->GetCount() && IsFileExist(GetNextContentIndex()) == false)
1944                         {
1945                                 String* pContentPath = GetContentPath(GetNextContentIndex());
1946
1947                                 if (pContentPath != null)
1948                                 {
1949                                         RemoveContent(*pContentPath);
1950                                 }
1951                         }
1952
1953                         //Need to play the currently loaded song in case there is no other content available in "Now Playing List"
1954                         if (__pPlayContentList->GetCount() == 0)
1955                         {
1956                                 InitializeProgress();
1957                                 Play(__currentContentIndex, MIN_SLIDER_COUNT, true);
1958                                 break;
1959                         }
1960
1961                         MoveNextContent();
1962                         InitializeProgress();
1963                         Play(__currentContentIndex, MIN_SLIDER_COUNT);
1964                 }
1965                 break;
1966
1967         default:
1968                 break;
1969         }
1970         AppLogDebug("EXIT");
1971 }
1972
1973 void
1974 PlayerPresentationModel::OnPlayerBuffering(int percent)
1975 {
1976         AppLogDebug("ENTER");
1977         if (__pPlayer == null)
1978         {
1979                 AppLogDebug("EXIT");
1980                 return;
1981         }
1982         CallOnPlayStateChanged(__pPlayer->GetState());
1983         AppLogDebug("EXIT");
1984 }
1985
1986 void
1987 PlayerPresentationModel::OnPlayerErrorOccurred(PlayerErrorReason r)
1988 {
1989         AppLogDebug("ENTER Error Reason = %d", r);
1990         if (__pPlayer == null)
1991         {
1992                 AppLogDebug("EXIT");
1993                 return;
1994         }
1995         // in case of Device failed stop the player and reinitialize for next content
1996         PlayerState playerState = __pPlayer->GetState();
1997         if (r == PLAYER_ERROR_DEVICE_FAILED)
1998         {
1999                 __pPlayer->Stop();
2000                 InitializeProgress();
2001                 InitializePlayer();
2002         }
2003
2004         CallOnPlayStateChanged(playerState);
2005         AppLogDebug("EXIT");
2006 }
2007
2008 void
2009 PlayerPresentationModel::OnPlayerInterrupted(void)
2010 {
2011         AppLogDebug("ENTER");
2012         Pause();
2013         AppLogDebug("EXIT");
2014         return;
2015 }
2016
2017 void
2018 PlayerPresentationModel::OnPlayerSeekCompleted(result r)
2019 {
2020         AppLogDebug("ENTER %s",GetErrorMessage(r));
2021
2022         if (!__seekCompleted)
2023         {
2024                 String currentDuration = CommonUtil::GetFormatDate(__pPlayer->GetPosition());
2025                 CallOnPlayTimeChanged(currentDuration, __playposition);
2026                 AppLogDebug("current duration %ls",currentDuration.GetPointer());
2027         }
2028
2029         if (r == E_SUCCESS)
2030         {
2031                 __seekCompleted = true;
2032         }
2033         else
2034         {
2035                 __seekCompleted = false;
2036         }
2037         AppLogDebug("EXIT");
2038 }
2039
2040 void
2041 PlayerPresentationModel::OnPlayerAudioFocusChanged(void)
2042 {
2043         AppLogDebug("ENTER");
2044         /*__audioFocusChanged = true;*/
2045         Pause();
2046         AppLogDebug("EXIT");
2047 }
2048
2049 /*void
2050 PlayerPresentationModel::OnAudioSessionInterrupted(void)
2051 {
2052         AppLogDebug("ENTER");
2053         AppLogDebug("EXIT");
2054 }
2055
2056 void
2057 PlayerPresentationModel::OnAudioSessionInterruptReleased(void)
2058 {
2059         AppLogDebug("ENTER");
2060         AppLogDebug("EXIT");
2061 }
2062
2063 void
2064 PlayerPresentationModel::OnAudioSessionAudioFocusChanged(void)
2065 {
2066         AppLogDebug("ENTER");
2067         AppLogDebug("EXIT");
2068 }*/
2069
2070 void
2071 PlayerPresentationModel::OnTimerExpired(Timer& timer)
2072 {
2073 //      AppLogDebug("ENTER");
2074         if (__pPlayer == null)
2075         {
2076                 AppLogDebug("EXIT player is null");
2077                 return;
2078         }
2079
2080         result r = E_SUCCESS;
2081         if (__pPlayStateTimer == &timer)
2082         {
2083                 String currentDuration;
2084                 PlayerState playerState = __pPlayer->GetState() ;
2085                 long playDuration = __pPlayer->GetDuration();
2086                 TryReturnVoid(playDuration != 0, "playDuration is zero.");
2087
2088                 switch (playerState)
2089                 {
2090                 case PLAYER_STATE_PLAYING:
2091                         {
2092                                 if ( playDuration < 2000)
2093                                 {
2094                                         currentDuration = CommonUtil::GetFormatDate(playDuration);
2095                                         CallOnPlayTimeChanged(currentDuration, TIMER_INTERVAL);
2096                                         return;
2097                                 }
2098
2099                                 r = __pPlayStateTimer->Start(PROGRESS_BAR_DURATION_TIMER);
2100
2101                                 TryCatch(r == E_SUCCESS, , "__pPlayStateTimer start failed..\n");
2102                                 TryReturnVoid(__seekCompleted == true, "SeekTo is not complete.");
2103                                 //AppLogDebug("GetPosition(%d), playDuration(%d)", __pPlayer->GetPosition(), playDuration);
2104
2105                                 if (__pPlayer->GetPosition() <= playDuration)
2106                                 {
2107                                         currentDuration = CommonUtil::GetFormatDate(__pPlayer->GetPosition());
2108                                         __playposition = static_cast<int>(__pPlayer->GetPosition() * TIMER_INTERVAL / playDuration);
2109 //                                      AppLogDebug("currentDuration(%s), __playposition(%d)", currentDuration.GetPointer(), __playposition);
2110                                         CallOnPlayTimeChanged(currentDuration, __playposition);
2111                                 }
2112                         }
2113                         break;
2114
2115                 case PLAYER_STATE_PAUSED:
2116                         {
2117                                 TryReturnVoid(__seekCompleted == true, "SeekTo is not complete.");
2118                                 TryReturnVoid(playDuration != 0, "playDuration is zero.");
2119                                 currentDuration = CommonUtil::GetFormatDate(__pPlayer->GetPosition());
2120                                 __playposition = static_cast<int>(__pPlayer->GetPosition() * TIMER_INTERVAL / playDuration);
2121                                 CallOnPlayTimeChanged(currentDuration, __playposition);
2122                         }
2123                         break;
2124
2125                 default:
2126                         break;
2127                 }
2128         }
2129         else if (__pFastForwardTimer == &timer)
2130         {
2131                 __xTimesValue--;
2132                 Forward();
2133         }
2134         else if (__pFastRewindTimer == &timer)
2135         {
2136                 __xTimesValue++;
2137                 Rewind();
2138         }
2139         else if (__pAutoOffTimer == &timer)
2140         {
2141                 Application::GetInstance()->Terminate();
2142         }
2143         else if (__pPauseAutoOffTimer == &timer)
2144         {
2145                 Application::GetInstance()->Terminate();
2146         }
2147         else if (__pVolumeBarStateTimer == &timer)
2148         {
2149                 r = __pVolumeBarStateTimer->Cancel();
2150                 TryLog(r == E_SUCCESS, "[%s] __pVolumeBarStateTimer cancel Failed.", GetErrorMessage(r));
2151                 CallOnPlayVolumeBarStateChanged();
2152         }
2153         else
2154         {
2155                 //AppLogDebug("Timer is invalid value.");
2156         }
2157 //      AppLogDebug("EXIT");
2158
2159         return;
2160
2161 CATCH:
2162         //AppLogException("[%ls]", GetErrorMessage(r));
2163         return;
2164 }
2165
2166 void
2167 PlayerPresentationModel::AddMusicPlayerEventListener(const IMusicPlayerEventListener& listener)
2168 {
2169         AppLogDebug("ENTER");
2170         __musicPlayerEventListener.Add(const_cast<IMusicPlayerEventListener*>(&listener));
2171         AppLogDebug("EXIT");
2172 }
2173
2174 void
2175 PlayerPresentationModel::RemoveMusicPlayerEventListener(const IMusicPlayerEventListener& listener)
2176 {
2177         AppLogDebug("ENTER");
2178         __musicPlayerEventListener.Remove(const_cast<IMusicPlayerEventListener*>(&listener));
2179         AppLogDebug("EXIT");
2180 }
2181
2182 void
2183 PlayerPresentationModel::CallOnPlayStateChanged(PlayerState playerState)
2184 {
2185         AppLogDebug("ENTER");
2186         IMusicPlayerEventListener* pListener = null;
2187         IEnumeratorT<IMusicPlayerEventListener*>* pEnum = __musicPlayerEventListener.GetEnumeratorN();
2188         while (pEnum->MoveNext() == E_SUCCESS)
2189         {
2190                 pEnum->GetCurrent(pListener);
2191                 if (pListener != null)
2192                 {
2193                         pListener->OnPlayStateChanged(playerState);
2194                 }
2195         }
2196         delete pEnum;
2197         AppLogDebug("EXIT");
2198 }
2199
2200 void
2201 PlayerPresentationModel::CallOnPlayContentChanged(int currentContentIndex)
2202 {
2203         AppLogDebug("ENTER");
2204         IMusicPlayerEventListener* pListener = null;
2205         IEnumeratorT<IMusicPlayerEventListener*>* pEnum = __musicPlayerEventListener.GetEnumeratorN();
2206         while (pEnum->MoveNext() == E_SUCCESS)
2207         {
2208                 pEnum->GetCurrent(pListener);
2209                 if (pListener != null)
2210                 {
2211                         pListener->OnPlayContentChanged(currentContentIndex);
2212                 }
2213         }
2214         delete pEnum;
2215         AppLogDebug("EXIT");
2216 }
2217
2218 void
2219 PlayerPresentationModel::CallOnPlayContentRemoved(int removedContentIndex)
2220 {
2221         AppLogDebug("ENTER");
2222         IMusicPlayerEventListener* pListener = null;
2223         IEnumeratorT<IMusicPlayerEventListener*>* pEnum = __musicPlayerEventListener.GetEnumeratorN();
2224         while (pEnum->MoveNext() == E_SUCCESS)
2225         {
2226                 pEnum->GetCurrent(pListener);
2227                 if (pListener != null)
2228                 {
2229                         pListener->OnPlayContentRemoved(removedContentIndex);
2230                 }
2231         }
2232         delete pEnum;
2233         AppLogDebug("EXIT");
2234 }
2235
2236 void
2237 PlayerPresentationModel::CallOnPlayTimeChanged(Tizen::Base::String& currentDuration, int currentPosition)
2238 {
2239 //      AppLogDebug("ENTER");
2240         IMusicPlayerEventListener* pListener = null;
2241         IEnumeratorT<IMusicPlayerEventListener*>* pEnum = __musicPlayerEventListener.GetEnumeratorN();
2242         while (pEnum->MoveNext() == E_SUCCESS)
2243         {
2244                 pEnum->GetCurrent(pListener);
2245                 if (pListener != null)
2246                 {
2247                         pListener->OnPlayTimeChanged(currentDuration, currentPosition);
2248                 }
2249         }
2250         delete pEnum;
2251 //      AppLogDebug("EXIT");
2252 }
2253
2254 void
2255 PlayerPresentationModel::CallOnPlayVolumeBarStateChanged(void)
2256 {
2257         AppLogDebug("ENTER");
2258         IMusicPlayerEventListener* pListener = null;
2259         IEnumeratorT<IMusicPlayerEventListener*>* pEnum = __musicPlayerEventListener.GetEnumeratorN();
2260         while (pEnum->MoveNext() == E_SUCCESS)
2261         {
2262                 pEnum->GetCurrent(pListener);
2263                 if (pListener != null)
2264                 {
2265                         pListener->OnPlayVolumeBarStateChanged();
2266                 }
2267         }
2268         delete pEnum;
2269         AppLogDebug("EXIT");
2270 }
2271
2272 void
2273 PlayerPresentationModel::CallOnPlayPresentationModelDestroying(void)
2274 {
2275         AppLogDebug("ENTER");
2276         IMusicPlayerEventListener* pListener = null;
2277         IEnumeratorT<IMusicPlayerEventListener*>* pEnum = __musicPlayerEventListener.GetEnumeratorN();
2278         while (pEnum->MoveNext() == E_SUCCESS)
2279         {
2280                 pEnum->GetCurrent(pListener);
2281                 if (pListener != null)
2282                 {
2283                         pListener->OnPlayPresentationModelDestroying();
2284                 }
2285         }
2286         delete pEnum;
2287         AppLogDebug("EXIT");
2288 }