Removed footer from ImageViewer:N_SE-49146
[apps/native/sample/MediaApp.git] / project / src / MediaPlayerForm.cpp
1 //
2 // Tizen Native SDK
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.1 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <FIo.h>
19 #include <FMediaImageBuffer.h>
20
21 #include "MediaPlayerForm.h"
22 #include "MultiForm.h"
23
24 using namespace Tizen::Base;
25 using namespace Tizen::Base::Utility;
26 using namespace Tizen::Ui::Controls;
27 using namespace Tizen::Media;
28 using namespace Tizen::Graphics;
29 using namespace Tizen::Base::Runtime;
30 using namespace Tizen::Io;
31 using namespace Tizen::App;
32 using namespace Tizen::System;
33 using namespace Tizen::Ui::Scenes;
34
35
36 static const int SPACE_FOR_PROGRESS_BAR = 160;
37 static const int SPACE_FOR_EDIT_AREA = 200;
38 static const int HEIGHT_OF_PROGRESS_BAR = 40;
39 static const int HEIGHT_OF_TIME_LABLE = 40;
40 static const int WIDTH_OF_TIME_LABLE = 160;
41 static const int FONT_SIZE_OF_TIME = 25;
42 static const int RANGE_OF_PROGRESS_BAR = 100;
43 static const int PROGRESS_BAR_X = 5;
44 static const int KILO_BYTE = 1024;
45 static const int MAX_ERROR_MSG_LENGTH = 3000;
46 static const int ASPECT_RATIO_WIDTH = 4;
47 static const int ASPECT_RATIO_HEIGHT = 3;
48 static const int REQUIRE_WIDTH_PERCENTAGE_LANDSCAPE = 85;
49 static const int MILI_SEC = 1000;
50 static const int SEC = 60;
51 static const int MAX_TIME = 10;
52
53 static const int DEVICE_PORTRAIT = 0;
54 static const int DEVICE_PORTRAIT_REVERSE = 1;
55 static const int DEVICE_LANDSCAPE = 3;
56 static const int DEVICE_LANDSCAPE_REVERSE = 2;
57
58 static const int TIMER_INTERVAL = 500;
59 static const int RESIZE_WIDTH = 320;
60 static const int RESIZE_HEIGHT = 240;
61
62 static const wchar_t* FORM_ID = L"MediaPlayerForm";
63 static const wchar_t* SCENE_ID = L"MediaPlayerScene";
64
65 MediaPlayerForm::MediaPlayerForm(void)
66                         :__isMediaAudio(false),
67                          __isMediaVideo(false),
68                          __isMediaImage(false),
69                          __fileSize(0),
70                          __progressBarWidth(0),
71                          __isPausedInInterrupt(false),
72                          __isPausedInBackGround(false),
73                          __isPausedBeforeSeek(false),
74                          __orientationInfo(0),
75                          __imageWidth(0),
76                          __imageHeight(0),
77                          __pDisplayText(null),
78                          __pTimer(null),
79                          __pRegion(null),
80                          __pPlayer(null),
81                          __pProgress(null),
82                          __pImageLable(null)
83 {
84 }
85
86 MediaPlayerForm::MediaPlayerForm(Form* pForm, String* pStreamType, String* pfilePath)
87                 :__isMediaAudio(false),
88                  __isMediaVideo(false),
89                  __isMediaImage(false),
90                  __fileSize(0),
91                  __progressBarWidth(0),
92                  __isPausedInInterrupt(false),
93                  __isPausedInBackGround(false),
94                  __isPausedBeforeSeek(false),
95                  __orientationInfo(0),
96                  __imageWidth(0),
97                  __imageHeight(0),
98                  __pDisplayText(null),
99                  __pCalledForm (pForm),
100                  __pFilePath(pfilePath),
101                  __pTimer(null),
102                  __pRegion(null),
103                  __pPlayer(null),
104                  __pProgress(null),
105                  __pImageLable(null)
106
107 {
108         if (pStreamType != null)
109         {
110                 if (pStreamType->Equals(L"AUDIO", false))
111                 {
112                         __isMediaAudio = true;
113                 }
114                 else if (pStreamType->Equals(L"VIDEO", false))
115                 {
116                         __isMediaVideo = true;
117                 }
118                 else if (pStreamType->Equals(L"IMAGE", false))
119                 {
120                         __isMediaImage = true;
121                 }
122         }
123 }
124
125 MediaPlayerForm::~MediaPlayerForm(void)
126 {
127         AppLog("~MediaPlayerForm ++");
128
129         if (__pPlayer != null)
130         {
131                 delete __pPlayer;
132                 __pPlayer = null;
133         }
134
135         if (__pRegion != null)
136         {
137                 delete __pRegion;
138                 __pRegion = null;
139
140         }
141
142         if (__pTimer != null)
143         {
144                 delete __pTimer;
145                 __pTimer = null;
146
147         }
148
149         AppLog("~MediaPlayerForm --");
150 }
151
152 MediaPlayerForm*
153 MediaPlayerForm::CreateFormN(const Tizen::Ui::Scenes::SceneId &sceneId)
154 {
155         AppLog("CreateFormN called");
156         SceneManager* pSceneManager = SceneManager::GetInstance();
157         MediaPlayerForm* pForm = new (std::nothrow) MediaPlayerForm();
158         pForm->Initialize();
159         pSceneManager->AddSceneEventListener(sceneId, *(pForm->GetListener()));
160         return pForm;
161 }
162
163 MultiForm* MediaPlayerForm::FormCreateFunc()
164 {
165         MediaPlayerForm* pForm = MediaPlayerForm::CreateFormN(SCENE_ID);
166         return pForm;
167 }
168
169 String MediaPlayerForm::GetFormId()
170 {
171         return FORM_ID;
172 }
173
174 SceneId MediaPlayerForm::GetSceneId()
175 {
176         return SCENE_ID;
177 }
178
179 MediaPlayerForm*
180 MediaPlayerForm::GetListener(void)
181 {
182         return this;
183 }
184
185
186 void
187 MediaPlayerForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId,
188                                                                    const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
189 {
190         TryReturnVoid(pArgs != null, "Arguments received are null");
191         TryReturnVoid(pArgs->GetCount() > 0, "Arguments GetCount is 0");
192
193         String* pMediaType;
194         result r = E_SUCCESS;
195         Integer* pDeviceOrientation;
196
197         AppLog("pArgs->GetCount() is %d", pArgs->GetCount());
198
199         pMediaType = (String*)pArgs->GetAt(0);
200         AppLog("Media Type is %ls",pMediaType->GetPointer());
201         __pFilePath = (String*)pArgs->GetAt(1);
202         AppLog("pFilePath is %ls",__pFilePath->GetPointer());
203         pDeviceOrientation = ((Integer*)pArgs->GetAt(2));
204
205         SetOrientationInfo(pDeviceOrientation->ToInt());
206
207         if (pMediaType != null)
208         {
209                 if (pMediaType->Equals(L"AUDIO", false))
210                 {
211                         __isMediaAudio = true;
212                         SetFormStyle(FORM_STYLE_INDICATOR | FORM_STYLE_NORMAL | FORM_STYLE_HEADER | FORM_STYLE_FOOTER);
213                 }
214                 else if (pMediaType->Equals(L"VIDEO", false))
215                 {
216                         __isMediaVideo = true;
217                         SetFormStyle(FORM_STYLE_INDICATOR | FORM_STYLE_NORMAL | FORM_STYLE_HEADER | FORM_STYLE_FOOTER);
218                 }
219                 else if (pMediaType->Equals(L"IMAGE", false))
220                 {
221                         __isMediaImage = true;
222                 }
223         }
224
225         r = InitializeMediaPlayer();
226         TryReturnVoid(r == E_SUCCESS, "[%s] InitializeMediaPlayer failed", GetErrorMessage(r));
227
228         SAFE_DELETE_ARRAYLIST(pArgs);
229 }
230 void
231 MediaPlayerForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
232                                                                         const Tizen::Ui::Scenes::SceneId& nextSceneId)
233 {
234         AppLog("OnSceneDeactivated called");
235 }
236
237 void
238 MediaPlayerForm::SetOrientationInfo(int orientation)
239 {
240         switch (orientation)
241         {
242         case DEVICE_PORTRAIT:
243                 __orientationInfo = CAMERA_EXIF_ORIENTATION_RIGHT_TOP;
244                 break;
245
246         case DEVICE_PORTRAIT_REVERSE:
247                 __orientationInfo = CAMERA_EXIF_ORIENTATION_LEFT_BOTTOM;
248                 break;
249
250         case DEVICE_LANDSCAPE:
251                 __orientationInfo = CAMERA_EXIF_ORIENTATION_TOP_LEFT;
252                 break;
253
254         case DEVICE_LANDSCAPE_REVERSE:
255                 __orientationInfo = CAMERA_EXIF_ORIENTATION_BOTTOM_RIGHT;
256                 break;
257
258         default:
259                 __orientationInfo = 0;
260                 break;
261         }
262
263         AppLog("SetOrientationInfo is %d", orientation);
264 }
265
266 bool
267 MediaPlayerForm::CreateProgressBar(void)
268 {
269         result r = E_SUCCESS;
270         unsigned int progBarY = ((GetClientAreaBounds().height - HEIGHT_OF_PROGRESS_BAR));
271         unsigned int timeLableY = ((GetClientAreaBounds().height - (HEIGHT_OF_PROGRESS_BAR + HEIGHT_OF_TIME_LABLE)));
272
273         __progressBarWidth = (GetClientAreaBounds().width - PROGRESS_BAR_X);
274
275         __pProgress = new (std::nothrow) Progress();
276         TryReturn(__pProgress != null, false,"E_OUT_OF_MEMORY in CreateProgressBar");
277
278         r = __pProgress->Construct(Rectangle(PROGRESS_BAR_X, progBarY, __progressBarWidth, HEIGHT_OF_PROGRESS_BAR), 0, RANGE_OF_PROGRESS_BAR);
279         TryReturn(r == E_SUCCESS, showPopupBool(L"Creation of Progress bar failed"),"[%s] __pProgress->Construct", GetErrorMessage(r));
280
281         __pProgress->SetValue(0);
282         __pProgress->SetRange(0, RANGE_OF_PROGRESS_BAR);
283         __pProgress->AddTouchEventListener(*this);
284
285         AddControl(__pProgress);
286
287         __startTime = L"";
288         __startTime.Append(L"00");
289         __startTime.Append(L"");
290         __startTime.Append(L":");
291         __startTime.Append(L"00");
292         __startTime.Append(L"");
293         __startTime.Append(L":");
294         __startTime.Append(L"00");
295
296         //Create elapsed and end time lable
297         __pElapsedTimeLable = new (std::nothrow) Label();
298         TryReturn(__pElapsedTimeLable != null, false,"E_OUT_OF_MEMORY for __pElapsedTimeLable");
299
300         __pElapsedTimeLable->Construct(Rectangle(0, timeLableY, WIDTH_OF_TIME_LABLE, HEIGHT_OF_TIME_LABLE), L"");
301         __pElapsedTimeLable->SetTextConfig(FONT_SIZE_OF_TIME, LABEL_TEXT_STYLE_ITALIC);
302         AddControl(__pElapsedTimeLable);
303
304         __pEndTimeLable = new (std::nothrow) Label();
305         TryReturn(__pEndTimeLable != null, false,"E_OUT_OF_MEMORY for __pEndTimeLable");
306
307         __pEndTimeLable->Construct(Rectangle((GetClientAreaBounds().width - WIDTH_OF_TIME_LABLE), timeLableY, WIDTH_OF_TIME_LABLE, HEIGHT_OF_TIME_LABLE), L"");
308         __pEndTimeLable->SetTextConfig(FONT_SIZE_OF_TIME, LABEL_TEXT_STYLE_ITALIC);
309         AddControl(__pEndTimeLable);
310
311         __pElapsedTimeLable->SetText(__startTime);
312         __pEndTimeLable->SetText(__startTime);
313
314         return true;
315 }
316
317
318 bool
319 MediaPlayerForm::Initialize(void)
320 {
321         result r = E_SUCCESS;
322         r = Form::Construct(FORM_STYLE_INDICATOR | FORM_STYLE_NORMAL | FORM_STYLE_HEADER);
323         TryReturn(r == E_SUCCESS, false,"[%s] Create() has failed", GetErrorMessage(r));
324
325         return true;
326 }
327
328 result
329 MediaPlayerForm::InitializeMediaPlayer(void)
330 {
331         result r =E_SUCCESS;
332
333         if (__isMediaVideo == true)
334         {
335                 SetHeaderText(L"Video Player");
336         }
337         else if (__isMediaAudio == true)
338         {
339                 SetHeaderText(L"Audio Player");
340         }
341         else if (__isMediaImage == true)
342         {
343                 SetHeaderText(L"Image Viewer");
344                 SetFooterStyle(FOOTER_STYLE_BUTTON_TEXT, true, this);
345                 SetFormBackEventListener(this);
346
347                 if (InitPlayer() == false)
348                 {
349                         AppLog("InitPlayer failed");
350                         showPopup(L"Creation of form failed");
351                         return E_FAILURE;
352                 }
353
354                 if (Start() == false)
355                 {
356                         showPopupBool(L"MediaPlayerStart() failed");
357                 }
358
359                 return r;
360         }
361
362         SetFooterStyle(FOOTER_STYLE_BUTTON_TEXT, true, this);
363         AddFooterItem(L"Stop", IDC_BUTTON_STOP);
364         AddFooterItem(L"Play", IDC_BUTTON_PLAY);
365         SetFormBackEventListener(this);
366         Tizen::System::PowerManager::KeepScreenOnState(false, true);
367         if (InitPlayer() == false)
368         {
369                 AppLog("InitPlayer failed");
370                 showPopup(L"Creation of form failed");
371                 return E_FAILURE;
372         }
373
374         if (Start() == false)
375         {
376                 showPopupBool(L"MediaPlayerStart() failed");
377         }
378
379         return r;
380 }
381
382 result
383 MediaPlayerForm::OnInitializing(void)
384 {
385         result r = E_SUCCESS;
386
387         AppLog("OnInitializing called");
388         return r;
389 }
390
391 void
392 MediaPlayerForm::OnTimerExpired(Timer& timer)
393 {
394         if (__pPlayer != null)
395         {
396                 AppLog("OnTimerExpired called");
397                 long currStep = 0;
398                 long position = __pPlayer->GetPosition();
399                 long clipDuration = __pPlayer->GetDuration();
400
401                 if ( (clipDuration > 0) && (position <= clipDuration))
402                 {
403                         currStep = (RANGE_OF_PROGRESS_BAR * position) / (long) clipDuration;
404                 }
405
406                 __pProgress->SetValue(currStep);
407                 __pProgress->RequestRedraw();
408                 UpdateTimeElapsed();
409
410                 if (__pPlayer->GetState() == PLAYER_STATE_PLAYING)
411                 {
412                         __pTimer->Start(TIMER_INTERVAL);
413                 }
414         }
415 }
416
417 void
418 MediaPlayerForm::OnTouchReleased(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
419 {
420 }
421
422 bool
423 MediaPlayerForm::InitPlayer(void)
424 {
425         result r = E_SUCCESS;
426         Tizen::Graphics::BufferInfo bufferInfo;
427         FileAttributes attr;
428
429         if (__isMediaImage == true)
430         {
431                 if (CreateImageViewerArea() == false)
432                 {
433                         AppLog("CreateImageViewerArea() failed");
434                         return false;
435                 }
436
437                 if (CreateImageEditArea() == false)
438                 {
439                         AppLog("CreateImageEditArea() failed");
440                         return false;
441                 }
442
443                 r = Tizen::Io::File::GetAttributes(*__pFilePath, attr);
444                 TryReturn(r == E_SUCCESS, false,"[%s] InitPlayer- File::GetAttributes", GetErrorMessage(r));
445
446                 __fileSize = attr.GetFileSize();
447
448                 return true;
449         }
450
451         if (__isMediaVideo == true || __isMediaAudio == true)
452         {
453                 __pPlayer = new (std::nothrow) Player();
454                 TryReturn(__pPlayer != null, false,"E_OUT_OF_MEMORY - Player creation failed");
455
456                 __pTimer = new (std::nothrow) Timer();
457                 TryReturn(__pTimer != null, false,"E_OUT_OF_MEMORY - __pTimer creation failed");
458
459                 r = __pTimer->Construct(*this);
460                 TryReturn(r == E_SUCCESS, false,"[%s] __pTimer->Construct", GetErrorMessage(r));
461
462                 if (CreateProgressBar() == false)
463                 {
464                         AppLog("InitPlayer->createProgressBar returned false");
465                         return false;
466                 }
467         }
468
469         if (__isMediaVideo == true)
470         {
471                 if (CreateOverlayRegion() == false)
472                 {
473                         AppLog("InitPlayer->CreateOverlayRegion returned false");
474                         return false;
475                 }
476
477                 r = __pRegion->GetBackgroundBufferInfo(bufferInfo);
478                 TryReturn(r == E_SUCCESS, false,"[%s] __pRegion->GetBackgroundBufferInfo", GetErrorMessage(r));
479
480                 r = __pPlayer->Construct(*this, &bufferInfo);
481                 TryReturn(r == E_SUCCESS, false,"[%s] __pPlayer->Construct", GetErrorMessage(r));
482
483         }
484         else if (__isMediaAudio == true)
485         {
486                 if (CreateAudioFileLogo() == false)
487                 {
488                         AppLog("InitPlayer->CreateAudioFileLogo returned false");
489                         return false;
490                 }
491                 r = __pPlayer->Construct(*this, null);
492                 TryReturn(r == E_SUCCESS, false,"[%s] __pPlayer->Construct", GetErrorMessage(r));
493         }
494         return true;
495 }
496
497 Bitmap*
498 MediaPlayerForm::CreateBitmapFromByteBufferN(ByteBuffer* pBuffer, int width, int height)
499 {
500         Dimension dim(width, height);
501         Bitmap* pBmp = new (std::nothrow) Bitmap;
502         TryReturn(pBmp != null, pBmp,"E_OUT_OF_MEMORY Bitmap creation failed");
503         result r = pBmp->Construct(*pBuffer, dim, Tizen::Graphics::BITMAP_PIXEL_FORMAT_ARGB8888);
504         TryReturn(r == E_SUCCESS, pBmp,"[%s] Bitmap->Construct", GetErrorMessage(r));
505         return pBmp;
506 }
507
508 Bitmap*
509 MediaPlayerForm::GetBitMapFromImageN(void) const
510 {
511         result r = E_SUCCESS;
512         Bitmap* pNormalBitmap = null;
513         Image image;
514
515         r = image.Construct();
516         TryReturn(r == E_SUCCESS, null,"[%s] image.Construct()", GetErrorMessage(r));
517
518         pNormalBitmap = image.DecodeN(*__pFilePath, BITMAP_PIXEL_FORMAT_ARGB8888);
519         TryReturn(pNormalBitmap != null, null,"[%s] image.DecodeN failed pBitmap is %x", GetErrorMessage(r), pNormalBitmap);
520
521         AppLog("Width is %d , height is %d", __imageWidth, __imageHeight);
522
523         return pNormalBitmap;
524 }
525
526 Bitmap*
527 MediaPlayerForm::RotateImageN(void)
528 {
529         result r = E_SUCCESS;
530         Bitmap* pNormalBitmap = null;
531         Bitmap* pRotatedBitmap = null;
532         ByteBuffer* pNormalByteBuff = null;
533         ByteBuffer* pRotatedByteBuff = null;
534         ByteBuffer* pResizeByteBuffer = null;
535         Image image;
536         int width = 0;
537         int height = 0;
538         ImageFormat imgFormat = IMG_FORMAT_NONE;
539         Dimension resizeDim;
540
541         r = image.Construct();
542         TryReturn(r == E_SUCCESS, null,"[%s] image.Construct()", GetErrorMessage(r));
543
544         r = ImageBuffer::GetImageInfo(*__pFilePath, imgFormat, width, height);
545         TryReturn(r == E_SUCCESS, null,"[%s] ImageBuffer::GetImageInfo", GetErrorMessage(r));
546
547         __imageWidth = width;
548         __imageHeight = height;
549
550         AppLog("Width is %d , height is %d", __imageWidth, __imageHeight);
551
552         //If orientation is in potrait mode or reverse Landscape mode then only rotate the image
553         if (__orientationInfo == CAMERA_EXIF_ORIENTATION_RIGHT_TOP ||
554                 __orientationInfo == CAMERA_EXIF_ORIENTATION_LEFT_BOTTOM ||
555                 __orientationInfo == CAMERA_EXIF_ORIENTATION_TOP_LEFT)
556         {
557                 pNormalByteBuff = image.DecodeToBufferN(*__pFilePath, BITMAP_PIXEL_FORMAT_ARGB8888, __imageWidth, __imageHeight);
558                 TryCatch(pNormalByteBuff != null, ,"pNormalByteBuff is null, no memory");
559
560                 resizeDim.width = RESIZE_WIDTH;
561                 resizeDim.height = RESIZE_HEIGHT;
562
563                 pResizeByteBuffer = new (std::nothrow) ByteBuffer;
564                 TryCatch(pResizeByteBuffer != null, ,"pResizeByteBuffer is null, no memory");
565
566                 r = pResizeByteBuffer->Construct(resizeDim.width * resizeDim.height * 4);
567                 TryCatch(r == E_SUCCESS, ,"[%s] pResizeByteBuffer Construct", GetErrorMessage(r));
568
569                 r = ImageUtil::Resize(*pNormalByteBuff, *pResizeByteBuffer, Dimension(__imageWidth, __imageHeight), resizeDim, MEDIA_PIXEL_FORMAT_RGBA8888);
570                 TryCatch(r == E_SUCCESS, ,"[%s] ImageUtil::Resize", GetErrorMessage(r));
571
572                 SAFE_DELETE(pNormalByteBuff);
573
574                 pRotatedByteBuff = new (std::nothrow) ByteBuffer;
575                 TryCatch(pRotatedByteBuff != null, ,"pRotatedByteBuff is null, no memory");
576
577                 r = pRotatedByteBuff->Construct(resizeDim.width * resizeDim.height * 4);
578                 TryCatch(r == E_SUCCESS, ,"[%s] pRotatedByteBuff Construct", GetErrorMessage(r));
579
580                 //potrait mode
581                 if (__orientationInfo == CAMERA_EXIF_ORIENTATION_RIGHT_TOP)
582                 {
583                         AppLog("Rotated by 90");
584                         r = ImageUtil::Rotate(*pResizeByteBuffer, *pRotatedByteBuff, resizeDim, IMAGE_ROTATION_90, MEDIA_PIXEL_FORMAT_RGBA8888);
585                         TryCatch(r == E_SUCCESS, ,"[%s] ImageUtil::Rotate 90", GetErrorMessage(r));
586
587                         pRotatedBitmap = CreateBitmapFromByteBufferN(pRotatedByteBuff, resizeDim.height, resizeDim.width);
588
589                 }
590                 //Reverse potrait mode
591                 else if (__orientationInfo == CAMERA_EXIF_ORIENTATION_LEFT_BOTTOM)
592                 {
593                         AppLog("Rotated by 270");
594                         r = ImageUtil::Rotate(*pResizeByteBuffer, *pRotatedByteBuff, resizeDim, IMAGE_ROTATION_270, MEDIA_PIXEL_FORMAT_RGBA8888);
595                         TryCatch(r == E_SUCCESS, ,"[%s] ImageUtil::Rotate 270", GetErrorMessage(r));
596
597                         pRotatedBitmap = CreateBitmapFromByteBufferN(pRotatedByteBuff, resizeDim.height, resizeDim.width);
598
599                 }
600                 //Landscape mode
601                 else if (__orientationInfo == CAMERA_EXIF_ORIENTATION_TOP_LEFT)
602                 {
603                         AppLog("Rotated by 180");
604                         r = ImageUtil::Rotate(*pResizeByteBuffer, *pRotatedByteBuff, resizeDim, IMAGE_ROTATION_180, MEDIA_PIXEL_FORMAT_RGBA8888);
605                         TryCatch(r == E_SUCCESS, ,"[%s] ImageUtil::Rotate 180", GetErrorMessage(r));
606
607                         pRotatedBitmap = CreateBitmapFromByteBufferN(pRotatedByteBuff, resizeDim.width, resizeDim.height);
608
609                 }
610
611                 //delete local create bytebuffers
612                 if (pRotatedByteBuff != null)
613                 {
614                         delete pRotatedByteBuff;
615                         pRotatedByteBuff = null;
616                 }
617                 if (pNormalBitmap != null)
618                 {
619                         delete pNormalBitmap;
620                         pNormalBitmap = null;
621                 }
622                 if (pResizeByteBuffer != null)
623                 {
624                         delete pResizeByteBuffer;
625                         pResizeByteBuffer = null;
626                 }
627
628                 return pRotatedBitmap;
629         }
630         else
631         {
632                 pNormalBitmap = image.DecodeN(*__pFilePath, BITMAP_PIXEL_FORMAT_ARGB8888);
633                 TryCatch(pNormalBitmap != null, ,"pNormalBitmap = image.DecodeN, no memory");
634
635                 __imageWidth = pNormalBitmap->GetWidth();
636                 __imageHeight = pNormalBitmap->GetHeight();
637
638                 return pNormalBitmap;
639         }
640
641 CATCH:
642
643         if (pRotatedByteBuff != null)
644         {
645                 delete pRotatedByteBuff;
646                 pRotatedByteBuff = null;
647         }
648         if (pNormalBitmap != null)
649         {
650                 delete pNormalBitmap;
651                 pNormalBitmap = null;
652         }
653         if (pResizeByteBuffer != null)
654         {
655                 delete pResizeByteBuffer;
656                 pResizeByteBuffer = null;
657         }
658
659         return null;
660 }
661
662 bool
663 MediaPlayerForm::DisplayImage(void)
664 {
665         result r = E_SUCCESS;
666         Rectangle imageLableRect;
667         Bitmap* pBitmap = RotateImageN();
668         TryReturn(pBitmap != null, false,"RotateImageN failed");
669
670         if (__pImageLable != null)
671         {
672                 imageLableRect = __pImageLable->GetBounds();
673                 //scale bitmap to image lable width and height
674                 r = pBitmap->Scale(Dimension(imageLableRect.width, imageLableRect.height));
675                 if (r != E_SUCCESS)
676                 {
677                         AppLog("bitmap.Scale failed %s", GetErrorMessage(r));
678
679                         if (pBitmap != null)
680                         {
681                                 delete pBitmap;
682                                 pBitmap = null;
683                         }
684                         return false;
685                 }
686
687                 __pImageLable->SetBackgroundBitmap(*pBitmap);
688                 AddControl(__pImageLable);
689                 __pImageLable->RequestRedraw();
690         }
691
692         //display file information
693         if (__pDisplayText != null)
694         {
695                 __pDisplayText->Clear();
696                 __pDisplayText->Log(L"\nFileName: ");
697                 GetOnlyFileName(__pFilePath);
698                 __pDisplayText->Log(*__pFilePath);
699                 __pDisplayText->Log("\nFileSize: %dKB\nDimension: %dx%d\n", __fileSize / KILO_BYTE, __imageWidth, __imageHeight);
700         }
701
702         if (pBitmap != null)
703         {
704                 delete pBitmap;
705                 pBitmap = null;
706         }
707
708         return true;
709 }
710
711 void
712 MediaPlayerForm::GetOnlyFileName(String* pFilePath) const
713 {
714         String delim(L"/");
715
716         // Create a StringTokenizer instance
717         StringTokenizer strTok(*pFilePath, delim);
718         String token;
719
720         //Iterate to the later token and extract the string
721         while (strTok.HasMoreTokens())
722         {
723                 strTok.GetNextToken(token);
724         }
725
726         pFilePath->Clear();
727         pFilePath->Append(token);
728 }
729
730 bool
731 MediaPlayerForm::CreateImageEditArea(void)
732 {
733         result r = E_SUCCESS;
734
735         __pDisplayText = new (std::nothrow) LogControl;
736         if (__pDisplayText == null)
737         {
738                 AppLog("__pDisplayText new (std::nothrow) failed, out of memory");
739                 return false;
740         }
741         TryReturn(__pDisplayText != null, false,"LogControl, no memory");
742
743         r = __pDisplayText->Construct(Rectangle(0, GetClientAreaBounds().height - SPACE_FOR_EDIT_AREA, GetClientAreaBounds().width, SPACE_FOR_EDIT_AREA));
744         TryReturn(r == E_SUCCESS, false,"[%s] __pDisplayText->Construct", GetErrorMessage(r));
745
746         r = __pDisplayText->SetEnabled(false);
747         TryReturn(r == E_SUCCESS, false,"[%s] __pDisplayText->SetEnabled", GetErrorMessage(r));
748
749         AddControl(__pDisplayText);
750
751         return true;
752 }
753
754 bool
755 MediaPlayerForm::CreateImageViewerArea(void)
756 {
757         result r = E_SUCCESS;
758
759         int width = 0;
760         int height = 0;
761         double aspectRatio = 0;
762         Image image;
763         Bitmap* pBitmap = null;
764         Rectangle clientRect = GetClientAreaBounds();
765         int marginX = 0;
766         int marginY = 0;
767         int requireHeight = 0;
768         int requireImgWidth = 0;
769
770         r = image.Construct();
771         TryReturn(r == E_SUCCESS, false,"[%s] image.Construct()", GetErrorMessage(r));
772
773         pBitmap = image.DecodeN(*__pFilePath, BITMAP_PIXEL_FORMAT_ARGB8888);
774         TryReturn(pBitmap != null, false,"[%s] image.DecodeN failed pBitmap is %x", GetErrorMessage(r), pBitmap);
775
776         width = pBitmap->GetWidth();
777         height = pBitmap->GetHeight();
778
779         requireImgWidth = ((double) (clientRect.width * REQUIRE_WIDTH_PERCENTAGE_LANDSCAPE)) / 100;
780         aspectRatio = ((double) width / (double) height);
781
782         AppLog("Actual image Aspect ratio %f", aspectRatio);
783
784         //Calculate the required height and width of the captured image to be displayer w.r.t aspect ratio
785         requireHeight = ((double) requireImgWidth / aspectRatio);
786
787         //If requiredHeight is more than client Area, round it off
788         if (requireHeight > (clientRect.height - SPACE_FOR_EDIT_AREA))
789         {
790                 requireHeight = (clientRect.height - SPACE_FOR_EDIT_AREA);
791         }
792
793         AppLog("New image label width is %d actual %d", requireImgWidth, clientRect.width);
794         AppLog("New image label height is %d actual %d", requireHeight, clientRect.height - SPACE_FOR_EDIT_AREA);
795
796
797         marginX = (clientRect.width - requireImgWidth) / 2;
798         marginY = (clientRect.height - SPACE_FOR_EDIT_AREA - requireHeight) / 2;
799
800         if (marginY < 0)
801         {
802                 marginY = 0;
803         }
804         if (marginX < 0)
805         {
806                 marginX = 0;
807         }
808
809         __pImageLable = new (std::nothrow) Label;
810         TryReturn(__pImageLable != null, false,"__pImageLable creation failed, no memory");
811
812         r = __pImageLable->Construct(Rectangle(0, 0, requireImgWidth, requireHeight), L"");
813         TryCatch(r == E_SUCCESS, ,"[%s] __pImageLable->Construct()", GetErrorMessage(r));
814
815         AppLog("margin is %d %d", marginX, marginY);
816
817         __pImageLable->SetPosition(marginX, marginY);
818         __pImageLable->SetBackgroundColor(Color(0xFF, 0xFF, 0xFF));
819
820         if (pBitmap != null)
821         {
822                 delete pBitmap;
823                 pBitmap = null;
824         }
825         return true;
826
827 CATCH:
828
829         if (pBitmap != null)
830         {
831                 delete pBitmap;
832                 pBitmap = null;
833         }
834
835         return false;
836
837 }
838
839 bool
840 MediaPlayerForm::CreateAudioFileLogo(void)
841 {
842         result r = E_SUCCESS;
843         Rectangle imageLabelRect;
844         Image image;
845         Bitmap* pBitmap = null;
846
847         r = image.Construct();
848         TryCatch(r == E_SUCCESS, ,"[%s] image.Construct()", GetErrorMessage(r));
849
850         pBitmap = image.DecodeN(App::GetInstance()->GetAppRootPath() + L"/res/audio-icon.png", BITMAP_PIXEL_FORMAT_ARGB8888);
851         TryCatch(pBitmap != null, ,"image.DecodeN failed pBitmap is %x", pBitmap);
852
853         __pImageLable = new (std::nothrow) Label;
854         TryCatch(__pImageLable != null, ,"_pImageLable new failed, out of memory");
855
856         imageLabelRect = GetClientAreaBounds();
857         imageLabelRect.height -= SPACE_FOR_PROGRESS_BAR;
858         imageLabelRect.x = imageLabelRect.width / 5;
859         imageLabelRect.y = imageLabelRect.height / 5;
860         imageLabelRect.width = imageLabelRect.width * 3 / 5;
861         imageLabelRect.height = imageLabelRect.height * 3 / 5;
862
863
864         r = __pImageLable->Construct(imageLabelRect, L"");
865         TryCatch(r == E_SUCCESS, ,"[%s] __pImageLable->Construct()", GetErrorMessage(r));
866
867         __pImageLable->SetBackgroundColor(Color(0xFF, 0xFF, 0xFF));
868
869         imageLabelRect = __pImageLable->GetBounds();
870         //scale bitmap
871         r = pBitmap->Scale(Dimension(imageLabelRect.width, imageLabelRect.height));
872         TryCatch(r == E_SUCCESS, ,"[%s] pBitmap->Scale", GetErrorMessage(r));
873
874         __pImageLable->SetBackgroundBitmap(*pBitmap);
875         AddControl(__pImageLable);
876         __pImageLable->RequestRedraw();
877
878         if (pBitmap != null)
879         {
880                 delete pBitmap;
881                 pBitmap = null;
882         }
883         return true;
884
885 CATCH:
886         if (pBitmap != null)
887         {
888                 delete pBitmap;
889                 pBitmap = null;
890         }
891         return false;
892 }
893
894 bool
895 MediaPlayerForm::CreateOverlayRegion(void)
896 {
897         int overlayRegionHeight = (GetClientAreaBounds().width * ASPECT_RATIO_HEIGHT / ASPECT_RATIO_WIDTH);
898
899         if (overlayRegionHeight > GetClientAreaBounds().height - SPACE_FOR_PROGRESS_BAR)
900         {
901                 overlayRegionHeight = GetClientAreaBounds().height - SPACE_FOR_PROGRESS_BAR;
902         }
903
904         Tizen::Graphics::Rectangle rect = Rectangle(0, GetClientAreaBounds().y, GetClientAreaBounds().width, overlayRegionHeight);
905
906         bool isModified = false;
907         bool isValidRect = OverlayRegion::EvaluateBounds(OVERLAY_REGION_EVALUATION_OPTION_LESS_THAN, rect, isModified);
908         TryReturn(isValidRect != false, false,"EvaluateBounds() has failed: [%s]", GetErrorMessage(GetLastResult()));
909
910         __pRegion = GetOverlayRegionN(rect, OVERLAY_REGION_TYPE_NORMAL);
911         TryReturn(__pRegion != null, false,"pRegion is null : %d %d %d %d", rect.x, rect.y, rect.width, rect.height);
912
913         return true;
914 }
915
916 result
917 MediaPlayerForm::OnTerminating(void)
918 {
919         AppLog("OnTerminating++");
920         if (__pPlayer != null)
921         {
922                 if (__pPlayer->GetState() == PLAYER_STATE_STOPPED)
923                 {
924                         __pPlayer->Close();
925                         delete __pPlayer;
926                         __pPlayer = null;
927                 }
928         }
929
930         AppLog("OnTerminating--");
931
932         return E_SUCCESS;
933 }
934
935 void
936 MediaPlayerForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
937 {
938         Tizen::System::BatteryLevel batteryLevel;
939         Tizen::System::Battery::GetCurrentLevel(batteryLevel);
940
941         if (batteryLevel == BATTERY_CRITICAL || batteryLevel == BATTERY_EMPTY || batteryLevel == BATTERY_LOW)
942         {
943                 bool isCharging = false;
944                 Tizen::System::RuntimeInfo::GetValue(String(L"IsCharging"), isCharging);
945                 if (isCharging == false && __pPlayer!= null)
946                 {
947                         HandleLowBattery();
948                         showPopup(L"Low Battery: Please charge to use");
949                         return;
950                 }
951         }
952
953         switch (actionId)
954         {
955         case IDC_BUTTON_PLAY:
956         {
957                 PlayerPlay();
958         }
959         break;
960
961         case IDC_BUTTON_PAUSE:
962         {
963                 PlayerPause();
964         }
965         break;
966
967         case IDC_BUTTON_STOP:
968         {
969                 PlayerStop();
970         }
971         break;
972         case ID_ERROR_POPUP:
973                 ClearErrorPopup();
974                 break;
975         default:
976                 break;
977         }
978 }
979
980 result
981 MediaPlayerForm::PlayerOpen(void)
982 {
983         result r = E_SUCCESS;
984         TryReturn(__pPlayer != null, r, "Player handle is null");
985         TryReturn(__pFilePath != null, r, "__pFilePath handle is null");
986
987         r = __pPlayer->OpenFile(*__pFilePath); // Sync open, at once
988         TryReturn(r == E_SUCCESS, showPopupResult(L"Opening file failed file path"),"[%s] pPlayer->OpenFile has failed", GetErrorMessage(r));
989
990         return r;
991 }
992
993 void
994 MediaPlayerForm::PlayerPause(void)
995 {
996         if (__pPlayer != null)
997         {
998                 result r = E_SUCCESS;
999                 // Pause the playing.
1000                 if (__pPlayer->GetState() == PLAYER_STATE_PLAYING)
1001                 {
1002                         AppLog("Player paused");
1003                         r = __pPlayer->Pause();
1004                         if (r != E_SUCCESS)
1005                         {
1006                                 AppLog("(Player::PlayerPause) Pause has failed: %s\n", GetErrorMessage(r));
1007                                 showPopup(L"Player Pause failed");
1008                                 SendUserEvent(PLAYER_ERROR_OCCURED, null);
1009                                 return;
1010                         }
1011
1012                         EnableFooterItem(IDC_BUTTON_STOP, true);
1013                         SetFooterItem(1, L"Play", IDC_BUTTON_PLAY);
1014                         EnableFooterItem(IDC_BUTTON_PLAY, true);
1015
1016                         __pTimer->Cancel();
1017                 }
1018         }
1019 }
1020
1021 void
1022 MediaPlayerForm::PlayerSeek(long int position)
1023 {
1024         if (__pPlayer != null)
1025         {
1026                 result r = E_SUCCESS;
1027
1028                 //Pause the playback if its in PLAYING state
1029                 if (__pPlayer->GetState() == PLAYER_STATE_PLAYING)
1030                 {
1031                         AppLog("Player paused");
1032                         r = __pPlayer->Pause();
1033                         if (r != E_SUCCESS)
1034                         {
1035                                 AppLog("__pPlayer->Pause() failed");
1036                                 showPopup(L"Player Pause failed");
1037                                 SendUserEvent(PLAYER_ERROR_OCCURED, null);
1038                                 return;
1039                         }
1040
1041                         __isPausedBeforeSeek = true;
1042                 }
1043
1044                 //Perform Seek and wait for OnPlayerSeekCompleted callback before resuming Play()
1045                 AppLog("Player Seeked position - %d duration - %d", position, __pPlayer->GetDuration());
1046                 r = __pPlayer->SeekTo(position);
1047                 if (r != E_SUCCESS)
1048                 {
1049                         AppLog("__pPlayer->SeekTo() failed");
1050                         showPopup(L"Player Seek failed");
1051                         SendUserEvent(PLAYER_ERROR_OCCURED, null);
1052                         return;
1053                 }
1054         }
1055 }
1056
1057 void
1058 MediaPlayerForm::OnPlayerAudioFocusChanged(void)
1059 {
1060         AppLog("OnPlayerAudioFocusChanged");
1061         if (__pPlayer->GetState() == PLAYER_STATE_PAUSED)
1062         {
1063                 ShowMessagePopup(String(L"Info"), String(L"Press Play to resume Playback"), ID_ERROR_POPUP);
1064                 EnableFooterItem(IDC_BUTTON_STOP, true);
1065                 SetFooterItem(1, L"Play", IDC_BUTTON_PLAY);
1066                 EnableFooterItem(IDC_BUTTON_PLAY, true);
1067         }
1068 }
1069
1070 void
1071 MediaPlayerForm::OnPlayerSeekCompleted(result r)
1072 {
1073         AppLog("OnPlayerSeekCompleted called, result is %s", GetErrorMessage(r));
1074
1075         if (__isPausedBeforeSeek == true)
1076         {
1077                 result r = E_SUCCESS;
1078
1079                 //Also check if player state is paused
1080                 if (__pPlayer->GetState() == PLAYER_STATE_PAUSED)
1081                 {
1082                         //After seek is completed, start the playback
1083                         AppLog("Player Play");
1084                         r = __pPlayer->Play();
1085                         if (r != E_SUCCESS)
1086                         {
1087                                 AppLog("__pPlayer->Play() failed");
1088                                 showPopup(L"Player Play failed");
1089                                 SendUserEvent(PLAYER_ERROR_OCCURED, null);
1090                                 return;
1091                         }
1092                 }
1093
1094                 //Reinitialise flag to false
1095                 __isPausedBeforeSeek = false;
1096         }
1097 }
1098
1099 void
1100 MediaPlayerForm::PlayerPlay(void)
1101 {
1102         if (__pPlayer != null)
1103         {
1104                 result r = E_SUCCESS;
1105                 PlayerState nowState = __pPlayer->GetState();
1106                 AppLog("PlayerPlay, player state is %d", nowState);
1107                 // Re-Play the playing.
1108                 if (nowState == PLAYER_STATE_ENDOFCLIP || nowState == PLAYER_STATE_OPENED ||
1109                         nowState == PLAYER_STATE_INITIALIZED || nowState == PLAYER_STATE_STOPPED ||
1110                         nowState == PLAYER_STATE_PAUSED)
1111                 {
1112                         AppLog("Player Play");
1113                         r = __pPlayer->Play();
1114                         if (r != E_SUCCESS)
1115                         {
1116                                 AppLog("(PlayerPlay Play has failed: %s\n", GetErrorMessage(r));
1117                                 if (nowState == PLAYER_STATE_ERROR)
1118                                 {
1119                                         showPopup(L"Player Play failed");
1120                                         AppLog("nowState == PLAYER_STATE_ERROR");
1121                                         SendUserEvent(PLAYER_ERROR_OCCURED, null);
1122                                 }
1123                                 else
1124                                 {
1125                                         switch(r)
1126                                         {
1127                                         case E_DEVICE_BUSY:
1128                                                 //situation is call, player will notice E_DEVICE_BUSY error
1129                                                 showPopup(L"Cannot play during call - player is paused");
1130                                                 break;
1131                                         case E_INVALID_STATE:
1132                                                 showPopup(L"player state is not valid");
1133                                                 break;
1134                                         default:
1135                                                 break;
1136                                         }
1137                                 }
1138                                 return;
1139                         }
1140
1141                         EnableFooterItem(IDC_BUTTON_STOP, true);
1142                         SetFooterItem(1, L"Pause", IDC_BUTTON_PAUSE);
1143                         EnableFooterItem(IDC_BUTTON_PAUSE, true);
1144
1145                         r = __pTimer->Start(TIMER_INTERVAL);
1146                         AppLog("Timer return = %s", GetErrorMessage(r));
1147                 }
1148         }
1149 }
1150
1151 void
1152 MediaPlayerForm::PlayerStop(void)
1153 {
1154         if (__pPlayer != null)
1155         {
1156                 result r = E_SUCCESS;
1157                 PlayerState nowState = __pPlayer->GetState();
1158
1159                 // Stop playing.
1160                 if (nowState == PLAYER_STATE_PLAYING || nowState == PLAYER_STATE_PAUSED)
1161                 {
1162                         __pTimer->Cancel();
1163
1164                         AppLog("Player Stop");
1165                         r = __pPlayer->Stop();
1166                         if (r != E_SUCCESS)
1167                         {
1168                                 AppLog("Stop has failed: %s\n", GetErrorMessage(r));
1169                                 showPopup(L"Player Stop failed");
1170                                 SendUserEvent(PLAYER_ERROR_OCCURED, null);
1171                                 return;
1172                         }
1173
1174                         //need not enable stop button, as its stopped
1175                         EnableFooterItem(IDC_BUTTON_STOP, false);
1176                         SetFooterItem(1, L"Play", IDC_BUTTON_PLAY);
1177                         EnableFooterItem(IDC_BUTTON_PLAY, true);
1178
1179                         ResetTimeElapsed();
1180                         UpdateTimeElapsed();
1181
1182                         __pProgress->SetValue(0);
1183                         __pProgress->RequestRedraw();
1184                 }
1185         }
1186 }
1187
1188 void
1189 MediaPlayerForm::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs)
1190 {
1191         switch (requestId)
1192         {
1193         case PLAYER_ERROR_OCCURED:
1194         {
1195                 AppLog("OnUserEventReceivedN PLAYER_ERROR_OCCURED");
1196                 HandlePlayerError();
1197         }
1198         break;
1199         }
1200
1201 }
1202
1203 void
1204 MediaPlayerForm::ResetTimeElapsed(void)
1205 {
1206         if ( (__pElapsedTimeLable != null) && (__pEndTimeLable != null))
1207         {
1208                 __pElapsedTimeLable->SetText(__startTime);
1209                 __pElapsedTimeLable->RequestRedraw();
1210         }
1211 }
1212
1213 void
1214 MediaPlayerForm::UpdateTimeElapsed(void)
1215 {
1216         long sec = 0;
1217         long min = 0;
1218         long hours = 0;
1219         long position = 0;
1220         long clipDuration = 0;
1221
1222         if (__pPlayer != null)
1223         {
1224                 position = __pPlayer->GetPosition();
1225                 clipDuration = __pPlayer->GetDuration();
1226         }
1227
1228         sec = position / MILI_SEC;
1229         min = sec / SEC;
1230         sec = sec - min * SEC;
1231         hours = min / SEC;
1232         min = min - hours * SEC;
1233
1234         String elapsedTime = L"";
1235         if (hours < MAX_TIME)
1236         {
1237                 elapsedTime.Append(L"0");
1238         }
1239         elapsedTime.Append(hours);
1240         elapsedTime.Append(L"");
1241         elapsedTime.Append(L":");
1242         if (min < MAX_TIME)
1243         {
1244                 elapsedTime.Append(L"0");
1245         }
1246         elapsedTime.Append(min);
1247         elapsedTime.Append(L"");
1248         elapsedTime.Append(L":");
1249         if (sec < MAX_TIME)
1250         {
1251                 elapsedTime.Append(L"0");
1252         }
1253         elapsedTime.Append(sec);
1254
1255         sec = clipDuration / MILI_SEC;
1256         min = sec / SEC;
1257         sec = sec - min * SEC;
1258         hours = min / SEC;
1259         min = min - hours * SEC;
1260
1261
1262         String endTime = L"";
1263
1264         if (hours < MAX_TIME)
1265         {
1266                 endTime.Append(L"0");
1267         }
1268         endTime.Append(hours);
1269         endTime.Append(L"");
1270         endTime.Append(L":");
1271         if (min < MAX_TIME)
1272         {
1273                 endTime.Append(L"0");
1274         }
1275         endTime.Append(min);
1276         endTime.Append(L"");
1277         endTime.Append(L":");
1278         if (sec < MAX_TIME)
1279         {
1280                 endTime.Append(L"0");
1281         }
1282         endTime.Append(sec);
1283
1284
1285         if ( (__pElapsedTimeLable != null) && (__pEndTimeLable != null) )
1286         {
1287                 __pElapsedTimeLable->SetText(elapsedTime);
1288                 __pElapsedTimeLable->RequestRedraw();
1289                 __pEndTimeLable->SetText(endTime);
1290                 __pEndTimeLable->RequestRedraw();
1291         }
1292 }
1293
1294 bool
1295 MediaPlayerForm::Start(void)
1296 {
1297         Tizen::System::PowerManager::KeepScreenOnState(true, false);
1298
1299         if (__isMediaImage == true)
1300         {
1301                 if (DisplayImage() == false)
1302                 {
1303                         showPopup(L"Displaying Image failed");
1304                         return false;
1305                 }
1306         }
1307         else
1308         {
1309                 if (PlayerOpen() != E_SUCCESS)
1310                 {
1311                         return false;
1312                 }
1313
1314                 AppLog("Player duration is %d",__pPlayer->GetDuration());
1315
1316                 UpdateTimeElapsed();
1317
1318                 PlayerPlay();
1319         }
1320
1321         return true;
1322 }
1323
1324 void
1325 MediaPlayerForm::OnFrameTerminating(const Tizen::Ui::Controls::Frame& source)
1326 {
1327         // When playing video clip, it's recommanded to call Player::Stop() on OnFrameTerminating().
1328
1329         if (__pPlayer != null)
1330         {
1331                 PlayerState nowState = __pPlayer->GetState();
1332                 if (nowState == PLAYER_STATE_ENDOFCLIP || nowState == PLAYER_STATE_PLAYING || nowState == PLAYER_STATE_OPENED || nowState == PLAYER_STATE_PAUSED)
1333                 {
1334                         __pPlayer->Stop();
1335                 }
1336         }
1337 }
1338
1339
1340 void
1341 MediaPlayerForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
1342 {
1343         AppLog("OnFormBackRequested");
1344         if (IsProcessedEvent())
1345         {
1346                 AppLog("Processed Event");
1347                 ResetProcessedEvent();
1348                 return;
1349         }
1350         FreeResource();
1351         AppLog("Deactivate start");
1352         Deactivate();
1353 }
1354
1355 void
1356 MediaPlayerForm::OnPlayerOpened(result r)
1357 {
1358 }
1359
1360 void
1361 MediaPlayerForm::OnPlayerEndOfClip(void)
1362 {
1363         AppLog("OnPlayerEndOfClip reached");
1364         EnableFooterItem(IDC_BUTTON_STOP, false);
1365         SetFooterItem(1, L"Play", IDC_BUTTON_PLAY);
1366         EnableFooterItem(IDC_BUTTON_PLAY, true);
1367
1368         __pProgress->SetValue(100);
1369         Draw();
1370
1371         ResetTimeElapsed();
1372         UpdateTimeElapsed();
1373
1374         __pProgress->SetValue(0);
1375         __pProgress->RequestRedraw();
1376         __pTimer->Cancel();
1377 }
1378
1379 void
1380 MediaPlayerForm::HandlePlayerError(void)
1381 {
1382         result res = E_SUCCESS;
1383         Tizen::Graphics::BufferInfo bufferInfo;
1384         AppLog("HandlePlayerError called");
1385
1386         //Reset flags and elapsed time
1387         __isPausedBeforeSeek = false;
1388
1389         EnableFooterItem(IDC_BUTTON_STOP, false);
1390         SetFooterItem(1, L"Play", IDC_BUTTON_PLAY);
1391         EnableFooterItem(IDC_BUTTON_PLAY, true);
1392
1393
1394         ResetTimeElapsed();
1395
1396         __pProgress->SetValue(0);
1397         __pProgress->RequestRedraw();
1398
1399         //When error occurs , delete the player and construct new (std::nothrow)
1400         if (__pPlayer != null)
1401         {
1402                 AppLog("player state is %d", __pPlayer->GetState());
1403                 delete __pPlayer;
1404                 __pPlayer = null;
1405         }
1406
1407         if (__pRegion != null)
1408         {
1409                 res = __pRegion->GetBackgroundBufferInfo(bufferInfo);
1410                 TryReturn(res == E_SUCCESS, showPopup(L"Region's GetBackgroundBufferInfo failed"),"GetBackgroundBufferInfo has failed");
1411
1412                 __pPlayer = new (std::nothrow) Player();
1413                 if (__pPlayer == null)
1414                 {
1415                         AppLog("pPlayer = new (std::nothrow) Player() has failed\n");
1416                         showPopup(L"Player new (std::nothrow) failed, out of memory");
1417                         return;
1418                 }
1419                 TryReturn(__pPlayer != null, showPopup(L"Player new failed, out of memory"),"pPlayer = new Player() has failed");
1420
1421                 res = __pPlayer->Construct(*this, &bufferInfo);
1422                 TryReturn(res == E_SUCCESS, showPopup(L"Player Construct failed"),"pPlayer->Construct has failed");
1423
1424                 //Open the file
1425                 if (PlayerOpen() != E_SUCCESS)
1426                 {
1427                         return;
1428                 }
1429         }
1430 }
1431 void
1432 MediaPlayerForm::OnPlayerBuffering(int percent)
1433 {
1434 }
1435
1436 void
1437 MediaPlayerForm::OnPlayerErrorOccurred(PlayerErrorReason r)
1438 {
1439         AppLog("OnPlayerErrorOccurred called");
1440         HandlePlayerError();
1441 }
1442
1443 void
1444 MediaPlayerForm::OnPlayerInterrupted(void)
1445 {
1446         __isPausedInInterrupt = true;
1447 }
1448
1449 void
1450 MediaPlayerForm::OnPlayerReleased(void)
1451 {
1452         TryReturnVoid(__pPlayer != null, "Player handle is null");
1453
1454         if ((__pPlayer->GetState() == PLAYER_STATE_PAUSED) && (__isPausedInInterrupt == true))
1455         {
1456                 __pPlayer->Play();
1457         }
1458         __isPausedInInterrupt = false;
1459 }
1460
1461 void
1462 MediaPlayerForm::OnForeground(void)
1463 {
1464         AppLog("OnForeground called");
1465         TryReturnVoid(__pPlayer != null, "Player handle is null");
1466
1467         PlayerState nowState = __pPlayer->GetState();
1468         if ((nowState == PLAYER_STATE_PAUSED || nowState == PLAYER_STATE_OPENED) && (__isPausedInBackGround == true))
1469         {
1470                 PlayerPlay();
1471         }
1472         __isPausedInBackGround = false;
1473
1474 }
1475
1476 void
1477 MediaPlayerForm::FreeResource(void)
1478 {
1479         TryReturnVoid(__pPlayer != null, "Player handle is null");
1480         TryReturnVoid(__pRegion != null, "overlayRegion handle is null");
1481         TryReturnVoid(__pTimer != null, "Timer handle is null");
1482
1483         __pPlayer->Stop();
1484         __pPlayer->Close();
1485
1486         SAFE_DELETE(__pPlayer);
1487         SAFE_DELETE(__pRegion);
1488         SAFE_DELETE(__pTimer);
1489
1490 }
1491
1492 void
1493 MediaPlayerForm::OnBatteryLevelChanged(Tizen::System::BatteryLevel batteryLevel)
1494 {
1495         if (batteryLevel == BATTERY_CRITICAL || batteryLevel == BATTERY_EMPTY || batteryLevel == BATTERY_LOW)
1496         {
1497                 bool isCharging = false;
1498                 Tizen::System::RuntimeInfo::GetValue(String(L"IsCharging"), isCharging);
1499                 if (isCharging == false && __pPlayer!= null)
1500                 {
1501                         HandleLowBattery();
1502                 }
1503         }
1504 }
1505
1506 void
1507 MediaPlayerForm::OnDeviceStateChanged(Tizen::System::DeviceType deviceType, const Tizen::Base::String &state)
1508 {
1509         if(deviceType == Charger)
1510         {
1511                 HandleLowBattery();
1512                 showPopup(L"Low Battery: Please charge to use");
1513         }
1514 }
1515
1516 void
1517 MediaPlayerForm::OnBackground(void)
1518 {
1519         AppLog("OnBackground called");
1520         TryReturnVoid(__pPlayer != null, "Player handle is null");
1521         if (__pPlayer->GetState() == PLAYER_STATE_PLAYING)
1522         {
1523                 PlayerPause();
1524                 __isPausedInBackGround = true;
1525         }
1526 }
1527
1528 void
1529 MediaPlayerForm::HandleLowBattery(void)
1530 {
1531         if (__pPlayer != null)
1532         {
1533                 //dont close the player
1534                 PlayerStop();
1535         }
1536
1537 }
1538
1539 result
1540 MediaPlayerForm::SetHeaderText(const Tizen::Base::String& title)
1541 {
1542         Header* pHeader = GetHeader();
1543         TryReturn(pHeader != null, E_FAILURE,"[%s] GetHeader failed", GetErrorMessage(GetLastResult()));
1544
1545         pHeader->SetTitleText(title);
1546         pHeader->RequestRedraw();
1547
1548         return E_SUCCESS;
1549
1550 }
1551
1552
1553 result
1554 MediaPlayerForm::SetFooterStyle(Tizen::Ui::Controls::FooterStyle style, bool showBack, Tizen::Ui::IActionEventListener* pListener)
1555 {
1556         result r = E_SUCCESS;
1557         Footer* pFooter = GetFooter();
1558         TryReturn(pFooter != null, E_FAILURE,"[%s] GetFooter failed", GetErrorMessage(GetLastResult()));
1559
1560         pFooter->SetStyle(style);
1561
1562         if (pListener != null)
1563         {
1564                 pFooter->AddActionEventListener(*pListener);
1565         }
1566
1567         return r;
1568 }
1569
1570
1571 result
1572 MediaPlayerForm::AddFooterItem(const Tizen::Base::String& text, int actionId)
1573 {
1574         result r = E_SUCCESS;
1575         FooterItem item;
1576         Footer* pFooter = GetFooter();
1577         int index = 0;
1578
1579         TryReturn(pFooter != null, E_FAILURE,"[%s] GetFooter failed", GetErrorMessage(GetLastResult()));
1580
1581         item.Construct(actionId);
1582         item.SetText(text);
1583
1584         index = pFooter->GetItemCount();
1585         TryReturn(index >= 0, E_FAILURE, "pFooter->GetItemCount() is less than 0 %d", index);
1586
1587         __footerActionId[index] = actionId;
1588
1589         pFooter->AddItem(item);
1590
1591         return r;
1592 }
1593
1594 result
1595 MediaPlayerForm::SetFooterItem(int index, const Tizen::Base::String& text, int actionId)
1596 {
1597         result r = E_SUCCESS;
1598         FooterItem item;
1599         Footer* pFooter = GetFooter();
1600
1601         TryReturn(pFooter != null, E_FAILURE,"[%s] GetFooter failed", GetErrorMessage(GetLastResult()));
1602
1603         item.Construct(actionId);
1604         item.SetText(text);
1605
1606         __footerActionId[index] = actionId;
1607
1608         pFooter->SetItemAt(index, item);
1609
1610         return r;
1611 }
1612
1613 result
1614 MediaPlayerForm::EnableFooterItem(int actionId, bool enable)
1615 {
1616         result r = E_SUCCESS;
1617         Footer* pFooter = GetFooter();
1618         unsigned int count = (sizeof(__footerActionId) / sizeof(__footerActionId[0]));
1619
1620         TryReturn(pFooter != null, E_FAILURE,"[%s] GetFooter failed", GetErrorMessage(GetLastResult()));
1621
1622         for (unsigned int i = 0; i < count; i++)
1623         {
1624                 if (__footerActionId[i] == actionId)
1625                 {
1626                         pFooter->SetItemEnabled(i, enable);
1627                         pFooter->RequestRedraw();
1628                         break;
1629                 }
1630         }
1631
1632         return r;
1633 }
1634
1635 void
1636 MediaPlayerForm::showPopup(String message)
1637 {
1638         AppLog("dispalying message");
1639         MessageBox messageBox;
1640         int modalResult = 0;
1641         messageBox.Construct(L"Info", message, MSGBOX_STYLE_OK, MAX_ERROR_MSG_LENGTH);
1642         messageBox.ShowAndWait(modalResult);
1643 }
1644
1645 result
1646 MediaPlayerForm::showPopupResult(String message)
1647 {
1648         AppLog("dispalying message");
1649         MessageBox messageBox;
1650         int modalResult = 0;
1651         messageBox.Construct(L"Info", message, MSGBOX_STYLE_OK, MAX_ERROR_MSG_LENGTH);
1652         messageBox.ShowAndWait(modalResult);
1653         return E_FAILURE;
1654 }
1655
1656 bool
1657 MediaPlayerForm::showPopupBool(String message)
1658 {
1659         AppLog("dispalying message");
1660         MessageBox messageBox;
1661         int modalResult = 0;
1662         messageBox.Construct(L"Info", message, MSGBOX_STYLE_OK, MAX_ERROR_MSG_LENGTH);
1663         messageBox.ShowAndWait(modalResult);
1664         return false;
1665 }