Applied latest source code
[apps/native/preloaded/Clock.git] / src / ClkAlarmRingingForm.cpp
1 //
2 // Tizen Native SDK
3 // Copyright (c) 2012 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 /**
19  * @file        ClkAlarmRingingForm.cpp
20  * @brief       Used to define AlarmRingingForm
21  */
22
23 #include <FApp.h>
24 #include <FAppUiApp.h>
25 #include <FBase.h>
26 #include <FIo.h>
27 #include <FMedia.h>
28 #include <FSystem.h>
29 #include <FTelCallInfo.h>
30 #include <FUi.h>
31
32 #include "ClkAlarmPresentationModel.h"
33 #include "ClkAlarmRingingForm.h"
34 #include "ClkClockApp.h"
35 #include "ClkCommonLib.h"
36 #include "ClkSceneRegister.h"
37 #include "ClkTypes.h"
38
39 using namespace Tizen::App;
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::Base::Runtime;
43 using namespace Tizen::Graphics;
44 using namespace Tizen::Io;
45 using namespace Tizen::Media;
46 using namespace Tizen::Shell;
47 using namespace Tizen::System;
48 using namespace Tizen::Telephony;
49 using namespace Tizen::Ui;
50 using namespace Tizen::Ui::Controls;
51 using namespace Tizen::Ui::Scenes;
52
53 static const int IDA_TABBAR_ITEM1_SNOOZE = 100;
54 static const int IDA_TABBAR_ITEM1_STOP = 101;
55 const RequestId REQUEST_ID_SETZORDER = 10000;
56 const RequestId REQUEST_ID_NEXTSCENEEXIST = 10001;
57
58 AlarmRingingForm::AlarmRingingForm()
59 : __hours(0),
60   __minutes(0),
61   __nextSceneExist(false),
62   __pAlarm(null),
63   __pAlarmTimeLabel(null),
64   __pAlarmTitleLabel(null),
65   __pClockLabel(null),
66   __pCurrentTimeLabel(null),
67   __pPlayer(null),
68   __pSnoozeButton(null),
69   __pStopButton(null),
70   __pTimer(null),
71   __pVibrator(null),
72   __timerExpired(false)
73 {
74
75 }
76
77 AlarmRingingForm::~AlarmRingingForm()
78 {
79
80         if ( __pTimer != null)
81         {
82                 delete __pTimer ;
83                 __pTimer = null;
84         }
85         if (__pPlayer != null)
86         {
87                 delete __pPlayer;
88                 __pPlayer = null;
89         }
90         if (__pVibrator != null)
91         {
92                 delete __pVibrator;
93                 __pVibrator = null;
94         }
95 }
96
97 bool
98 AlarmRingingForm::Initialize()
99 {
100         Form::Construct(L"IDL_ALARM_RINGING_FORM");
101         return true;
102 }
103
104 result
105 AlarmRingingForm::OnInitializing(void)
106 {
107         result r = E_FAILURE;
108
109         SettingInfo::GetValue(L"http://tizen.org/setting/locale.time.format.24hour", is24HourFormat);
110
111         PowerManager::AddScreenEventListener(*this);
112         AddOrientationEventListener(*this);
113         SceneManager* pSceneManager = SceneManager::GetInstance();
114
115         if (pSceneManager != null)
116         {
117                 r = pSceneManager->AddSceneEventListener(IDSCN_ALARM_RINGING, *this);
118
119                 if (IsFailed(r))
120                 {
121                         AppLogException("OnInitialized failed with %s", GetErrorMessage(r));
122                         return r;
123                 }
124         }
125
126         __pSnoozeButton = static_cast< Button* >(GetControl("IDC_BUTTON1"));
127
128         if (__pSnoozeButton != null)
129         {
130                 __pSnoozeButton->SetActionId(IDA_TABBAR_ITEM1_SNOOZE);
131                 __pSnoozeButton->AddActionEventListener(*this);
132         }
133
134         __pStopButton = static_cast< Button* >(GetControl("IDC_BUTTON2"));
135
136         if (__pStopButton != null)
137         {
138                 __pStopButton->SetActionId(IDA_TABBAR_ITEM1_STOP);
139                 __pStopButton->AddActionEventListener(*this);
140         }
141
142         __pClockLabel = static_cast< Label* >(GetControl("IDC_LABEL1"));
143         __pCurrentTimeLabel = static_cast< Label* >(GetControl("IDC_LABEL2"));
144         __pAlarmTimeLabel = static_cast< Label* >(GetControl("IDC_LABEL3"));
145         __pAlarmTitleLabel = static_cast< Label* >(GetControl("IDC_LABEL4"));
146
147         // Create Timer
148         __pTimer = new (std::nothrow) Timer();
149
150         if (__pTimer == null)
151         {
152                 return E_FAILURE;
153         }
154
155         __callManager.Construct(*this);
156
157         r = __pTimer->Construct(*this);
158
159         return r;
160 }
161
162 result
163 AlarmRingingForm::OnTerminating(void)
164 {
165         PowerManager::RemoveScreenEventListener(*this);
166         return E_SUCCESS;
167 }
168 void
169 AlarmRingingForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
170 {
171         int hours = 0;
172         String alarmTimeString = L"";
173         String alarmTonePath;
174         DateTime alarmTime;
175         int alarmVolume;
176         int buttonGap = 0;
177         bool snoozeOn = false;
178
179         //PowerManager::AddScreenEventListener(*this);
180         if (__nextSceneExist && __timerExpired)
181         {
182                 SendUserEvent(REQUEST_ID_NEXTSCENEEXIST, null);
183                 return;
184         }
185
186         if (__nextSceneExist && !__timerExpired)
187         {
188                 __pPlayer->Play();
189         }
190         __nextSceneExist = false;
191
192         if (pArgs != null)
193         {
194                 __pAlarm = dynamic_cast<AlarmData*>(pArgs->GetAt(0));
195
196                 if ( __pAlarm == null )
197                 {
198                         return;
199                 }
200
201                 snoozeOn = __pAlarm->GetSnoozeOn();
202
203                 if (snoozeOn && __pAlarm->GetSnoozeRepeatCount() != 0)
204                 {
205                         alarmTime = __pAlarm->GetAlarmTime();
206                         AppLog("alarmTime = %ls", alarmTime.ToString().GetPointer());
207                         if (is24HourFormat)
208                         {
209                                 hours = alarmTime.GetHour();
210
211                                 if ( hours < 10)
212                                 {
213                                         alarmTimeString.Append(L"0");
214                                 }
215
216                                 alarmTimeString.Append(hours);
217                         }
218                         else
219                         {
220                                 hours = alarmTime.GetHour() % 12;
221
222                                 if (hours == 0)
223                                 {
224                                         alarmTimeString.Append("12");
225                                 }
226                                 else
227                                 {
228                                         if ( hours < 10)
229                                         {
230                                                 alarmTimeString.Append(L"0");
231                                         }
232
233                                         alarmTimeString.Append(hours%12);
234                                 }
235                         }
236
237                         alarmTimeString.Append(":");
238
239                         if (alarmTime.GetMinute() < 10)
240                         {
241                                 alarmTimeString.Append(L"0");
242                         }
243
244                         alarmTimeString.Append(alarmTime.GetMinute());
245                         alarmTimeString.Append(" ");
246
247                         if (is24HourFormat == false)
248                         {
249                                 if (alarmTime.GetHour() >= 12)
250                                 {
251                                         alarmTimeString.Append("PM");
252                                 }
253                                 else
254                                 {
255                                         alarmTimeString.Append("AM");
256                                 }
257                         }
258
259
260                         __pAlarmTimeLabel->SetText(alarmTimeString);
261                 }
262                 else
263                 {
264                         __pAlarmTimeLabel->SetShowState(false);
265                         __pSnoozeButton->SetShowState(false);
266                         __pAlarmTitleLabel->SetBounds(Rectangle(__pAlarmTimeLabel->GetX(),__pAlarmTimeLabel->GetY(),__pAlarmTitleLabel->GetWidth(),__pAlarmTitleLabel->GetHeight()));
267
268                         if (GetOrientation() == ORIENTATION_LANDSCAPE || GetOrientation() == ORIENTATION_LANDSCAPE_REVERSE)
269                         {
270                                 buttonGap = 390;
271                         }
272                         else
273                         {
274                                 buttonGap = 110;
275                         }
276
277                         __pStopButton->SetBounds(Rectangle(buttonGap,__pStopButton->GetY(),GetClientAreaBounds().width - 2 * buttonGap, __pStopButton->GetHeight()));
278
279                 }
280                 __pAlarmTitleLabel->SetText(__pAlarm->GetAlarmName());
281
282                 alarmVolume = __pAlarm->GetRingVolume();
283                 alarmTonePath = __pAlarm->GetRingTone();
284
285                 if (__pAlarm->GetRingType() == ALARM_TYPE_MELODY)
286                 {
287                         StartAudio(alarmTonePath,alarmVolume);
288                 }
289                 else if(__pAlarm->GetRingType() == ALARM_TYPE_VIBRATION)
290                 {
291                         StartVibrator();
292                 }
293                 else if (__pAlarm->GetRingType() == ALARM_TYPE_MELODY_AND_VIBRATION)
294                 {
295                         StartAudio(alarmTonePath,alarmVolume);
296                         StartVibrator();
297                 }
298         }
299
300         if(__pTimer != null)
301         {
302                 __pTimer->Start(1);
303         }
304
305         SendUserEvent(REQUEST_ID_SETZORDER, null);
306
307 }
308
309 void
310 AlarmRingingForm::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs)
311 {
312         result r = E_FAILURE;
313
314         if (requestId == REQUEST_ID_SETZORDER)
315         {
316                 AppLog("the z-order is set");
317                 Frame* pFrame = UiApp::GetInstance()->GetFrame(L"Clock");
318
319                 if (pFrame != null)
320                 {
321                         if (LockManager::GetInstance()->IsLocked())
322                         {
323                                 pFrame->SetZOrderGroup(WINDOW_Z_ORDER_GROUP_HIGH);
324                         }
325                         //pFrame->SetZOrderGroup(WINDOW_Z_ORDER_GROUP_HIGH);
326                         Show();
327                         //pFrame->SetZOrderGroup(WINDOW_Z_ORDER_GROUP_NORMAL);
328                         //Show();
329                 }
330         }
331         else if (requestId == REQUEST_ID_NEXTSCENEEXIST)
332         {
333                 r = SceneManager::GetInstance()->GoBackward(BackwardSceneTransition());
334                 if( r == E_UNDERFLOW)
335                 {
336                         UiApp::GetInstance()->Terminate();
337                 }
338         }
339
340 }
341
342 void
343 AlarmRingingForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
344 {
345         if (nextSceneId == IDSCN_TIMER_RINGING || nextSceneId == IDSCN_TIMER_RINGING_LOCK)
346         {
347                 __nextSceneExist = true;
348         }
349
350         Frame* pFrame = UiApp::GetInstance()->GetFrame(L"Clock");
351
352         if (pFrame != null)
353         {
354                 pFrame->SetZOrderGroup(WINDOW_Z_ORDER_GROUP_NORMAL);
355         }
356
357         if(__pPlayer != null)
358         {
359                 if (__nextSceneExist == true)
360                 {
361                         __pPlayer->Pause();
362                 }
363                 else
364                 {
365                         __pPlayer->Stop();
366                         __pPlayer->Close();
367                 }
368         }
369         if(__pVibrator != null)
370         {
371                 __pVibrator->Stop();
372         }
373
374    /* if(__pTimer != null)
375     {
376         __pTimer->Cancel();
377     }*/
378 }
379
380 void
381 AlarmRingingForm::OnActionPerformed(const Control& source, int actionId)
382 {
383         result r = E_SUCCESS;
384         SceneManager* pSceneManager = null;
385         int count = 0;
386         int snoozeRepeatCount = 0;
387         AlarmData* pAlarm = null;
388         ArrayList* alarmList = null;
389         DateTime currentTime;
390         int alarmCount = 0;
391         AlarmPresentationModel* pAlarmPresentationModel = AlarmPresentationModel::GetInstance();
392         DataServiceProvider* pDataServiceProvider = DataServiceProvider::GetInstance();
393
394         if(__pTimer != null)
395         {
396                 __pTimer->Cancel();
397         }
398
399         switch (actionId)
400         {
401         case IDA_TABBAR_ITEM1_STOP:
402         {
403                 pAlarmPresentationModel->GetAlarmCount(count);
404                 alarmList = new (std::nothrow) ArrayList();
405
406                 if (alarmList == null)
407                 {
408                         return;
409                 }
410
411                 alarmList->Construct();
412                 pAlarmPresentationModel->GetAlarm(0,count,*alarmList);
413                 alarmCount = alarmList->GetCount();
414
415                 for (count = 0; count < alarmCount; count++)
416                 {
417                         pAlarm = dynamic_cast< AlarmData* >(alarmList->GetAt(count));
418
419                         if ( pAlarm != null && __pAlarm != null && pAlarm->GetAlarmId() == __pAlarm->GetAlarmId())
420                         {
421                                 pAlarm->SetSnoozeRepeatCount(-1);
422
423                                 if(!pAlarm->GetRepeatFlags().Contains(L"1"))
424                                 {
425                                         pAlarm->SetAlarmOnState(0);
426                                 }
427
428                                 r = pAlarmPresentationModel->SaveAlarm(*pAlarm);
429
430                                 if (r == E_STORAGE_FULL || r == E_INIT_FAILED || r == E_OUT_OF_MEMORY)
431                                 {
432                                         MessageBox msgBox;
433                                         int modalMsgBoxResult;
434                                         String msg = CommonUtil::GetString("IDS_MEMORY_FULL");
435                                         r = msgBox.Construct(L"", msg, MSGBOX_STYLE_NONE, 3000);
436                                         AppLog("the result is %s", GetErrorMessage(r));
437                                         msgBox.ShowAndWait(modalMsgBoxResult);
438                                 }
439
440                                 break;
441                         }
442                 }
443
444
445 /*              ClockApp* pClock = static_cast<ClockApp*>(ClockApp::GetInstance());
446                 if (pClock->activeAppId.GetLength() > 0 && pClock->activeAppId != ClockApp::GetInstance()->GetAppId())
447                 {
448                         AppManager::GetInstance()->LaunchApplication(pClock->activeAppId);
449                         return;
450                 }*/
451
452                 if (alarmList != null)
453                 {
454                         alarmList->RemoveAll();
455                         delete alarmList;
456                         alarmList = null;
457                 }
458                 pSceneManager = SceneManager::GetInstance();
459
460                 if (pSceneManager != null)
461                 {
462                         r = pSceneManager->GoBackward(BackwardSceneTransition());
463                         if( r == E_UNDERFLOW)
464                         {
465                                 UiApp::GetInstance()->Terminate();
466                         }
467                 }
468         }
469         break;
470
471         case IDA_TABBAR_ITEM1_SNOOZE:
472         {
473                 DateTime nextAlarmTime;
474                 pAlarmPresentationModel->GetAlarmCount(count);
475                 alarmList = new (std::nothrow) ArrayList();
476
477                 if (alarmList == null)
478                 {
479                         return;
480                 }
481
482                 alarmList->Construct();
483                 pAlarmPresentationModel->GetAlarm(0,count,*alarmList);
484                 pDataServiceProvider->GetCurrentDateTime(currentTime);
485                 alarmCount = alarmList->GetCount();
486
487                 for (count = 0; count < alarmCount; count++)
488                 {
489                         pAlarm = dynamic_cast< AlarmData* >(alarmList->GetAt(count));
490
491                         if ( pAlarm != null && pAlarm->GetAlarmId() == __pAlarm->GetAlarmId())
492                         {
493                                 nextAlarmTime.SetValue(__pAlarm->GetAlarmTime());
494                                 pAlarm->SetLastRingingTime(nextAlarmTime);
495                                 snoozeRepeatCount = pAlarm->GetSnoozeRepeatCount();
496                                 pAlarm->SetSnoozeRepeatCount(snoozeRepeatCount - 1);
497
498                                 if (snoozeRepeatCount <= 0 && !pAlarm->GetRepeatFlags().Contains(L"1"))
499                                 {
500                                         pAlarm->SetAlarmOnState(0);
501                                 }
502
503                                 r = pAlarmPresentationModel->SaveAlarm(*pAlarm);
504
505                                 if (r == E_STORAGE_FULL || r == E_INIT_FAILED || r == E_OUT_OF_MEMORY)
506                                 {
507                                         MessageBox msgBox;
508                                         int modalMsgBoxResult;
509                                         String msg = CommonUtil::GetString("IDS_MEMORY_FULL");
510                                         r = msgBox.Construct(L"", msg, MSGBOX_STYLE_NONE, 3000);
511                                         AppLog("the result is %s", GetErrorMessage(r));
512                                         msgBox.ShowAndWait(modalMsgBoxResult);
513                                 }
514
515                                 break;
516                         }
517                 }
518
519                 if (alarmList != null)
520                 {
521                         alarmList->RemoveAll();
522                         delete alarmList;
523                         alarmList = null;
524                 }
525
526                 pSceneManager = SceneManager::GetInstance();
527
528                 if (pSceneManager != null)
529                 {
530                         r = pSceneManager->GoBackward(BackwardSceneTransition());
531                         if( r == E_UNDERFLOW)
532                         {
533                                 UiApp::GetInstance()->Terminate();
534                         }
535                 }
536
537         }
538         break;
539
540         default:
541                 break;
542         }
543 }
544
545 void
546 AlarmRingingForm::ShowClock()
547 {
548         result r = E_FAILURE;
549         int seconds = 0;
550         int hours = 0;
551         Bitmap* pHourBitmap = null ;
552         Bitmap* pMinuteBitmap = null ;
553         Bitmap* pSecondBitmap = null ;
554         Bitmap* pClockBitmap = null;
555         Bitmap* pFinalBitmap = null;
556         Canvas* pCanvas = null;
557         DateTime currentTime;
558         String currentTimeString = L"";
559         int canvasWidth = 374;
560         int canvasHeight = 544;
561         String hourImagePath;
562         String minuteImagePath;
563         String secondImagePath;
564         Point centerPoint(51,94);
565
566     DataServiceProvider* pDataServiceProvider = DataServiceProvider::GetInstance();
567         pDataServiceProvider->GetCurrentDateTime(currentTime);
568
569         hourImagePath.Append(IDS_HOUR_IMAGE_PATH);
570         minuteImagePath.Append(IDS_MIN_IMAGE_PATH);
571         secondImagePath.Append(IDS_SEC_IMAGE_PATH);
572
573         if (__minutes != currentTime.GetMinute() || __hours != currentTime.GetHour())
574         {
575                 if (is24HourFormat)
576                 {
577                         hours = currentTime.GetHour();
578
579                         if ( hours < 10)
580                         {
581                                 currentTimeString.Append(L"0");
582                         }
583
584                         currentTimeString.Append(hours);
585                 }
586                 else
587                 {
588                         hours = currentTime.GetHour() % 12;
589
590                         if (hours == 0)
591                         {
592                                 currentTimeString.Append("12");
593                         }
594                         else
595                         {
596                                 if ( hours < 10)
597                                 {
598                                         currentTimeString.Append(L"0");
599                                 }
600
601                                 currentTimeString.Append(hours%12);
602                         }
603                 }
604
605                 currentTimeString.Append(":");
606
607                 if (currentTime.GetMinute() < 10)
608                 {
609                         currentTimeString.Append(L"0");
610                 }
611
612                 currentTimeString.Append(currentTime.GetMinute());
613                 currentTimeString.Append(" ");
614
615                 if (!is24HourFormat)
616                 {
617                         if (currentTime.GetHour() >= 12)
618                         {
619                                 currentTimeString.Append("PM");
620                         }
621                         else
622                         {
623                                 currentTimeString.Append("AM");
624                         }
625                 }
626
627                 __pCurrentTimeLabel->SetText(currentTimeString);
628         }
629
630         __hours = currentTime.GetHour();
631         __minutes = currentTime.GetMinute();
632         seconds = currentTime.GetSecond();
633
634         pClockBitmap = AppResource::GetInstance()->GetBitmapN(IDB_ALARM_CLOCK);
635
636         if ((__hours % 12) < 10)
637         {
638                 hourImagePath.Append(L"0");
639         }
640
641         hourImagePath.Append(__hours % 12);
642
643         if (__minutes < 12)
644         {
645                 hourImagePath.Append(L"_1.png");
646         }
647         else if (__minutes >= 12 && __minutes < 24)
648         {
649                 hourImagePath.Append(L"_2.png");
650         }
651         else if (__minutes >= 24 && __minutes < 36)
652         {
653                 hourImagePath.Append(L"_3.png");
654         }
655         else if (__minutes >= 36 && __minutes < 48)
656         {
657                 hourImagePath.Append(L"_4.png");
658         }
659         else if (__minutes >= 48 && __minutes < 60)
660         {
661                 hourImagePath.Append(L"_5.png");
662         }
663
664         if (__minutes < 10)
665         {
666                 minuteImagePath.Append(L"0");
667         }
668
669         minuteImagePath.Append(__minutes);
670         minuteImagePath.Append(L".png");
671
672         if (seconds < 10)
673         {
674                 secondImagePath.Append(L"0");
675         }
676
677         secondImagePath.Append(seconds);
678         secondImagePath.Append(L".png");
679
680         AppLog("hourImagePath = %ls minuteImagePath = %ls secondImagePath = %ls", hourImagePath.GetPointer(), minuteImagePath.GetPointer(), secondImagePath.GetPointer());
681         pMinuteBitmap = AppResource::GetInstance()->GetBitmapN(minuteImagePath);
682         pHourBitmap = AppResource::GetInstance()->GetBitmapN(hourImagePath);
683         pSecondBitmap = AppResource::GetInstance()->GetBitmapN(secondImagePath);
684
685         pCanvas = new (std::nothrow) Canvas();
686         pCanvas->Construct(Rectangle(0,0,canvasWidth,canvasHeight));
687         r = pCanvas->DrawBitmap(Point(0,0),*pClockBitmap);
688         pCanvas->DrawBitmap(centerPoint,*pHourBitmap);
689         pCanvas->DrawBitmap(centerPoint,*pMinuteBitmap);
690         pCanvas->DrawBitmap(centerPoint,*pSecondBitmap);
691
692         pFinalBitmap = new (std::nothrow) Bitmap();
693
694         if (pFinalBitmap != null)
695         {
696                 pFinalBitmap->Construct(*pCanvas,Rectangle(0,0,canvasWidth,canvasHeight));
697                 delete pCanvas;
698         }
699
700         __pClockLabel->SetBackgroundBitmap(*pFinalBitmap);
701
702         if (pClockBitmap != null)
703         {
704                 delete pClockBitmap;
705                 pClockBitmap = null;
706         }
707
708         if (pHourBitmap != null)
709         {
710                 delete pHourBitmap;
711                 pHourBitmap = null;
712         }
713
714         if (pMinuteBitmap != null)
715         {
716                 delete pMinuteBitmap;
717                 pMinuteBitmap = null;
718         }
719
720         if (pSecondBitmap != null)
721         {
722                 delete pSecondBitmap;
723                 pSecondBitmap = null;
724         }
725
726         if (pFinalBitmap != null)
727         {
728                 delete pFinalBitmap;
729                 pFinalBitmap = null;
730         }
731
732 }
733
734 void
735 AlarmRingingForm::OnTimerExpired(Timer&  timer)
736 {
737         DateTime currentTime, alarmTime;
738         int seconds = 0;
739         int minutes = 0;
740
741         DataServiceProvider* pDataServiceProvider = DataServiceProvider::GetInstance();
742         pDataServiceProvider->GetCurrentDateTime(currentTime);
743         if (__pAlarm != null)
744         {
745                 alarmTime = __pAlarm->GetAlarmTime();
746                 alarmTime.AddMinutes(__pAlarm->GetSnoozeDuration() * (__pAlarm->GetSnoozeRepeat() - __pAlarm->GetSnoozeRepeatCount()));
747         }
748         seconds = currentTime.GetSecond() - alarmTime.GetSecond();
749         minutes = currentTime.GetMinute() - alarmTime.GetMinute();
750         if (seconds < 0)
751         {
752                 seconds = seconds + 60;
753         }
754
755         if ( seconds == 0 && minutes == 1 )
756         {
757                 int alarmCount = 0;
758                 AlarmPresentationModel* pAlarmPresentationModel = AlarmPresentationModel::GetInstance();
759                 SceneManager* pSceneManager = null;
760                 int count = 0;
761                 int snoozeRepeatCount = 0;
762                 AlarmData* pAlarm = null;
763                 ArrayList* alarmList = null;
764                 result r = E_FAILURE;
765
766                 __timerExpired = true;
767                 pAlarmPresentationModel->GetAlarmCount(count);
768                 alarmList = new (std::nothrow) ArrayList();
769
770                 if (alarmList == null)
771                 {
772                         return;
773                 }
774
775                 alarmList->Construct();
776                 pAlarmPresentationModel->GetAlarm(0,count,*alarmList);
777                 pDataServiceProvider->GetCurrentDateTime(currentTime);
778                 alarmCount = alarmList->GetCount();
779
780                 for (count = 0; count < alarmCount; count++)
781                 {
782                         pAlarm = dynamic_cast< AlarmData* >(alarmList->GetAt(count));
783
784                         if ( pAlarm != null && __pAlarm != null && pAlarm->GetAlarmId() == __pAlarm->GetAlarmId())
785                         {
786                                 pAlarm->SetLastRingingTime(__pAlarm->GetAlarmTime());
787                                 snoozeRepeatCount = pAlarm->GetSnoozeRepeatCount();
788                                 pAlarm->SetSnoozeRepeatCount(snoozeRepeatCount - 1);
789                                 AppLog("AlarmRingingForm::OnTimerExpired 1");
790                                 if ((snoozeRepeatCount <= 0 || !pAlarm->GetSnoozeOn()) && !pAlarm->GetRepeatFlags().Contains(L"1"))
791                                 {
792                                         AppLog("AlarmRingingForm::OnTimerExpired 2");
793                                         pAlarm->SetAlarmOnState(0);
794                                 }
795
796                                 r = pAlarmPresentationModel->SaveAlarm(*pAlarm);
797
798                                 if (r == E_STORAGE_FULL || r == E_INIT_FAILED || r == E_OUT_OF_MEMORY)
799                                 {
800                                         MessageBox msgBox;
801                                         int modalMsgBoxResult;
802                                         String msg = CommonUtil::GetString("IDS_MEMORY_FULL");
803                                         r = msgBox.Construct(L"", msg, MSGBOX_STYLE_NONE, 3000);
804                                         AppLog("the result is %s", GetErrorMessage(r));
805                                         msgBox.ShowAndWait(modalMsgBoxResult);
806                                 }
807
808                                 break;
809                         }
810                 }
811
812                 if (alarmList != null)
813                 {
814                         alarmList->RemoveAll();
815                         delete alarmList;
816                         alarmList = null;
817                 }
818
819                 pSceneManager = SceneManager::GetInstance();
820
821                 if (pSceneManager != null)
822                 {
823                         if (!__nextSceneExist)
824                         {
825                                 r = pSceneManager->GoBackward(BackwardSceneTransition());
826                                 if( r == E_UNDERFLOW)
827                                 {
828                                         UiApp::GetInstance()->Terminate();
829                                 }
830                         }
831                 }
832         }
833         else
834         {
835                 __timerExpired = false;
836                 if(__pTimer != null)
837                 {
838                         __pTimer->Start(1000); // to refresh list after 1 second ( 1000 milliseconds)
839                 }
840                 PowerManager::KeepScreenOnState(true,false);
841                 ShowClock();
842                 Invalidate(true);
843         }
844
845 }
846
847 result
848 AlarmRingingForm::StartAudio(String& path, int volume)
849 {
850         AppLog("AlarmRingingLockForm::StartAudio");
851     result r = E_SUCCESS;
852         Tizen::Media::Player player;
853         __pPlayer = new(std::nothrow) Player();
854     r = __pPlayer->Construct(*this);
855
856     if (IsFailed(r))
857     {
858         AppLog("the result of construct is %s", GetErrorMessage(r));
859         return r;
860     }
861     __pPlayer->Close();
862
863     if(File::IsFileExist(path) == false)
864     {
865         path = AlarmPresentationModel::GetInstance()->defaultAudioPath;
866     }
867     r = __pPlayer->OpenFile(path, false);
868
869     if (IsFailed(r))
870     {
871         AppLog("the result of openfile is %s", GetErrorMessage(r));
872         return r;
873     }
874
875     __pPlayer->SetVolume(volume*100/15);
876
877     r = __pPlayer->SetLooping(true);
878     AppLog("the result is %s", GetErrorMessage(r));
879
880     __pPlayer->SetAudioStreamType(AUDIO_STREAM_TYPE_MEDIA);
881     r = __pPlayer->Play();
882
883     if (IsFailed(r))
884     {
885         AppLog("the result of play is %s", GetErrorMessage(r));
886         return r;
887     }
888
889     return E_SUCCESS;
890 }
891
892 result
893 AlarmRingingForm::StartVibrator(void)
894 {
895         AppLog("AlarmRingingForm::StartVibrator");
896         result r = E_SUCCESS;
897         __pVibrator = new(std::nothrow) Vibrator();
898     r = __pVibrator->Construct();
899
900     if (IsFailed(r))
901     {
902         AppLog("the result of vibrator is %s", GetErrorMessage(r));
903     }
904
905     IntensityDurationVibrationPattern *pPattern = new IntensityDurationVibrationPattern();
906     pPattern->duration = 60000;
907     pPattern->intensity = 100;
908
909     r = __pVibrator->Start(pPattern,10);
910
911     AppLog("the value is %s", GetErrorMessage(r));
912     delete pPattern;
913     pPattern = null;
914
915     return r;
916 }
917
918 void
919 AlarmRingingForm::OnTelephonyCallStatusChangedN (CallStatus callStatus, CallInfo *pCallInfo)
920 {
921         AppLog("callStatus %d", callStatus);
922         Frame* pFrame = UiApp::GetInstance()->GetFrame(L"Clock");
923
924         if(callStatus == CALL_STATUS_RINGING)
925         {
926                 if (pFrame != null)
927                 {
928                         pFrame->SetZOrderGroup(WINDOW_Z_ORDER_GROUP_NORMAL);
929                 }
930         }
931 }
932
933 void
934 AlarmRingingForm::OnSettingChanged(Tizen::Base::String& key)
935 {
936         int systemVolume = 0;
937         if ( key == L"http://tizen.org/setting/locale.time.format.24hour")
938         {
939                 SettingInfo::GetValue(key, is24HourFormat);
940         }
941
942         if (key == L"http://tizen.org/setting/sound.media.volume" )
943         {
944                 SettingInfo::GetValue(key, systemVolume);
945         }
946
947 }
948
949 void
950 AlarmRingingForm::OnOrientationChanged(const Control& source, OrientationStatus orientationStatus)
951 {
952         AppLog("OnOrientationChanged");
953         int hours = 0;
954         bool snoozeOn = false;
955         String alarmTimeString = L"";
956         DateTime alarmTime;
957         int buttonGap = 110;
958
959         snoozeOn = __pAlarm->GetSnoozeOn();
960
961         if (snoozeOn && __pAlarm->GetSnoozeRepeatCount() != 0)
962         {
963                 alarmTime = __pAlarm->GetAlarmTime();
964
965                 if (is24HourFormat)
966                 {
967                         hours = alarmTime.GetHour();
968
969                         if ( hours < 10)
970                         {
971                                 alarmTimeString.Append(L"0");
972                         }
973
974                         alarmTimeString.Append(hours);
975                 }
976                 else
977                 {
978                         hours = alarmTime.GetHour() % 12;
979
980                         if (hours == 0)
981                         {
982                                 alarmTimeString.Append("12");
983                         }
984                         else
985                         {
986                                 if ( hours < 10)
987                                 {
988                                         alarmTimeString.Append(L"0");
989                                 }
990
991                                 alarmTimeString.Append(hours%12);
992                         }
993                 }
994
995                 alarmTimeString.Append(":");
996
997                 if (alarmTime.GetMinute() < 10)
998                 {
999                         alarmTimeString.Append(L"0");
1000                 }
1001
1002                 alarmTimeString.Append(alarmTime.GetMinute());
1003                 alarmTimeString.Append(" ");
1004
1005                 if (is24HourFormat == false)
1006                 {
1007                         if (alarmTime.GetHour() >= 12)
1008                         {
1009                                 alarmTimeString.Append("PM");
1010                         }
1011                         else
1012                         {
1013                                 alarmTimeString.Append("AM");
1014                         }
1015                 }
1016
1017
1018                 __pAlarmTimeLabel->SetText(alarmTimeString);
1019         }
1020         else
1021         {
1022                 __pAlarmTimeLabel->SetShowState(false);
1023                 __pSnoozeButton->SetShowState(false);
1024                 __pAlarmTitleLabel->SetBounds(Rectangle(__pAlarmTimeLabel->GetX(),__pAlarmTimeLabel->GetY(),__pAlarmTitleLabel->GetWidth(),__pAlarmTitleLabel->GetHeight()));
1025
1026                 if (orientationStatus == ORIENTATION_STATUS_LANDSCAPE || orientationStatus == ORIENTATION_STATUS_LANDSCAPE_REVERSE)
1027                 {
1028                         buttonGap = 390;
1029                 }
1030                 else
1031                 {
1032                         buttonGap = 110;
1033                 }
1034                 __pStopButton->SetBounds(Rectangle(buttonGap,__pStopButton->GetY(),GetClientAreaBounds().width - 2 * buttonGap, __pStopButton->GetHeight()));
1035         }
1036
1037
1038 }
1039
1040 void
1041 AlarmRingingForm::OnScreenOff(void)
1042 {
1043         AppLog("AlarmRingingForm::OnScreenOff");
1044         result r = E_SUCCESS;
1045         SceneManager* pSceneManager = null;
1046         ArrayList* alarmList = null;
1047         AlarmPresentationModel* pAlarmPresentationModel = AlarmPresentationModel::GetInstance();
1048         int count = 0;
1049         int alarmCount = 0;
1050         AlarmData* pAlarm = null;
1051
1052         pAlarmPresentationModel->GetAlarmCount(count);
1053         alarmList = new (std::nothrow) ArrayList();
1054
1055         if (alarmList == null)
1056         {
1057                 return;
1058         }
1059
1060         alarmList->Construct();
1061         pAlarmPresentationModel->GetAlarm(0,count,*alarmList);
1062         alarmCount = alarmList->GetCount();
1063
1064         for (count = 0; count < alarmCount; count++)
1065         {
1066                 pAlarm = dynamic_cast< AlarmData* >(alarmList->GetAt(count));
1067
1068                 if ( pAlarm != null && __pAlarm != null && pAlarm->GetAlarmId() == __pAlarm->GetAlarmId())
1069                 {
1070                         pAlarm->SetSnoozeRepeatCount(-1);
1071
1072                         if(!pAlarm->GetRepeatFlags().Contains(L"1"))
1073                         {
1074                                 pAlarm->SetAlarmOnState(0);
1075                         }
1076
1077                         r = pAlarmPresentationModel->SaveAlarm(*pAlarm);
1078
1079                         if (r == E_STORAGE_FULL || r == E_INIT_FAILED || r == E_OUT_OF_MEMORY)
1080                         {
1081                                 MessageBox msgBox;
1082                                 int modalMsgBoxResult;
1083                                 String msg = CommonUtil::GetString("IDS_MEMORY_FULL");
1084                                 r = msgBox.Construct(L"", msg, MSGBOX_STYLE_NONE, 3000);
1085                                 AppLog("the result is %s", GetErrorMessage(r));
1086                                 msgBox.ShowAndWait(modalMsgBoxResult);
1087                         }
1088
1089                         break;
1090                 }
1091         }
1092
1093
1094 /*              ClockApp* pClock = static_cast<ClockApp*>(ClockApp::GetInstance());
1095         if (pClock->activeAppId.GetLength() > 0 && pClock->activeAppId != ClockApp::GetInstance()->GetAppId())
1096         {
1097                 AppManager::GetInstance()->LaunchApplication(pClock->activeAppId);
1098                 return;
1099         }*/
1100
1101         pSceneManager = SceneManager::GetInstance();
1102
1103         if (pSceneManager != null)
1104         {
1105                 r = pSceneManager->GoBackward(BackwardSceneTransition());
1106                 if( r == E_UNDERFLOW)
1107                 {
1108                         UiApp::GetInstance()->Terminate();
1109                 }
1110         }
1111 }
1112
1113