Applied latest source code
[apps/native/preloaded/Clock.git] / src / ClkClockApp.cpp
1 //
2 // Tizen C++ SDK
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //l
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        ClkClockApp.cpp
20  * @brief       Used to define ClockApp
21  */
22
23 #include "ClkAlarmPresentationModel.h"
24 #include "ClkClockApp.h"
25 #include "ClkCommonLib.h"
26 #include "ClkMainFrame.h"
27 #include "ClkSceneRegister.h"
28 #include "ClkStopWatchPresentationModel.h"
29 #include "ClkTimerPresentationModel.h"
30 #include "ClkWorldClockPresentationModel.h"
31
32 using namespace Tizen::App;
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Base::Runtime;
36 using namespace Tizen::System;
37 using namespace Tizen::Shell;
38 using namespace Tizen::Ui;
39 using namespace Tizen::Ui::Controls;
40 using namespace Tizen::Ui::Scenes;
41
42 static const int TIMER_START = 117;
43 static const int TIMER_RESTART = 119;
44
45
46 ClockApp::ClockApp(void)
47 :__currentTimeKey("currentTime"),
48  __isAlarmLaunchedAutomatically(false),
49  __isrebooted(false),
50  __isTimerLaunchedAutomatically(false),
51  __langValue(""),
52  __nameKey("SceneId"),
53  __pAlarm(null),
54  __pAppRegistry(null),
55  __pTimer(null),
56  __ringingAlarmId(-1),
57  __timerResetKey("timerReset"),
58  __timerTimeKey("timerTime"),
59  __userSetValueKey("serTimerSetValue")
60 {
61
62 }
63
64 ClockApp::~ClockApp(void)
65 {
66         if (__pAlarm != null)
67         {
68                 delete __pAlarm;
69                 __pAlarm = null;
70         }
71
72 }
73
74 UiApp*
75 ClockApp::CreateInstance(void)
76 {
77         return new (std::nothrow) ClockApp();
78 }
79
80 bool
81 ClockApp::OnAppInitializing(AppRegistry& appRegistry)
82 {
83         AppLog("ClockApp::OnAppInitializing");
84
85         result r = E_FAILURE;
86
87         DateTime timerTime;
88         DateTime currentTime;
89         String timerTimeString;
90         String userSetValue;
91
92         TimerPresentationModel::GetInstance();
93         StopWatchPresentationModel::GetInstance();
94         AlarmPresentationModel* pAlarmPresentationModel = AlarmPresentationModel::GetInstance();
95         WorldClockPresentationModel* pWorldClockPresentationModel = WorldClockPresentationModel::GetInstance();
96         DataServiceProvider* pDataServiceProvider = DataServiceProvider::GetInstance();
97
98         AppManager::GetInstance()->SetAppLaunchConditionEventListener(this);
99
100         SettingInfo::AddSettingEventListener(*this);
101         int timerResetValue = 0;
102
103         pAlarmPresentationModel->AddEventListener(this);
104         __pAppRegistry = GetAppRegistry();
105         __sceneId = IDSCN_ALARM_LIST;
106         r = __pAppRegistry->Get(__nameKey, __sceneId);
107
108         if (r == E_KEY_NOT_FOUND)
109         {
110                 __pAppRegistry->Add(__nameKey, IDSCN_ALARM_LIST);
111         }
112
113         r = __pAppRegistry->Get(__timerResetKey, timerResetValue);
114
115         if (r == E_KEY_NOT_FOUND)
116         {
117                 __pAppRegistry->Add(__timerResetKey, timerResetValue);
118         }
119
120         r = __pAppRegistry->Get(__timerTimeKey, timerTimeString);
121
122         if (r == E_KEY_NOT_FOUND)
123         {
124                 __pAppRegistry->Add(__timerTimeKey, timerTimeString);
125                 __timerTimeReached.SetValue(9999,1,1);
126         }
127         else
128         {
129                 DateTime::Parse(timerTimeString, __timerTimeReached);
130         }
131
132         r = __pAppRegistry->Get(__userSetValueKey, TimerPresentationModel::GetInstance()->userValue);
133
134         if (r == E_KEY_NOT_FOUND)
135         {
136                 __pAppRegistry->Add(__userSetValueKey, TimerPresentationModel::GetInstance()->userValue);
137         }
138
139         pDataServiceProvider->Initialize();
140         pAlarmPresentationModel->Initialize();
141         pWorldClockPresentationModel->Initialize();
142
143         //RegisterAppForTimer();// after change in settings
144
145         __pTimer = new (std::nothrow) Timer();
146         __pTimer->Construct(*this);
147
148         return true;
149 }
150
151 bool
152 ClockApp::OnAppInitialized(void)
153 {
154         AppLog("ClockApp::OnAppInitialized");
155
156         MainFrame* pClockFrame = null;
157
158         if (__pAlarm != null)
159         {
160                 delete __pAlarm;
161                 __pAlarm = null;
162         }
163         __pAlarm = new (std::nothrow) AlarmData();
164
165         pClockFrame = new (std::nothrow) MainFrame();
166         pClockFrame->Construct();
167         pClockFrame->SetName(L"Clock");
168         AddFrame(*pClockFrame);
169
170         ResetValues();
171
172         if(__isTimerLaunchedAutomatically == true)
173         {
174                 if (__isrebooted)
175                 {
176                         Terminate();
177                 }
178
179                 ShowTimerRingingForm();
180         }
181         else if (__isAlarmLaunchedAutomatically == true)
182         {
183                 ShowAlarmRingingForm();
184         }
185         else if(__sceneId.GetLength() == 0 || __sceneId == IDSCN_ALARM_DELETE_LIST || __sceneId == IDSCN_CREATE_ALARM || __sceneId == IDSCN_EDIT_SNOOZE || __sceneId == IDSCN_ALARM_LIST || __sceneId == IDSCN_ALARM_RINGING || __sceneId == IDSCN_ALARM_RINGING_LOCK)
186         {
187                 SceneManager::GetInstance()->GoForward(ForwardSceneTransition(IDSCN_ALARM_LIST, SCENE_TRANSITION_ANIMATION_TYPE_NONE));
188         }
189         else if(__sceneId == IDSCN_CITY_LIST || __sceneId == IDSCN_WORLD_CLOCK_DELETE_LIST || __sceneId == IDSCN_WORLD_CLOCK_REORDER || __sceneId == IDSCN_WORLD_CLOCK_LIST)
190         {
191                 SceneManager::GetInstance()->GoForward(ForwardSceneTransition(IDSCN_WORLD_CLOCK_LIST, SCENE_TRANSITION_ANIMATION_TYPE_NONE));
192         }
193         else if(__sceneId == IDSCN_STOP_WATCH)
194         {
195                 SceneManager::GetInstance()->GoForward(ForwardSceneTransition(IDSCN_STOP_WATCH, SCENE_TRANSITION_ANIMATION_TYPE_NONE));
196         }
197         else if(__sceneId == IDSCN_TIMER_VIEW || __sceneId == IDSCN_TIMER_RINGING || __sceneId == IDSCN_TIMER_RINGING_LOCK )
198         {
199                 if (__sceneId == IDSCN_TIMER_RINGING || __sceneId == IDSCN_TIMER_RINGING_LOCK)
200                 {
201                         int status = 0;
202                         TimerPresentationModel::GetInstance()->SetStatus(status);
203                 }
204
205                 SceneManager::GetInstance()->GoForward(ForwardSceneTransition(IDSCN_TIMER_VIEW, SCENE_TRANSITION_ANIMATION_TYPE_NONE));
206         }
207         else
208         {
209                 SceneManager::GetInstance()->GoForward(ForwardSceneTransition(__sceneId, SCENE_TRANSITION_ANIMATION_TYPE_NONE));
210         }
211
212         String key1 = L"http://tizen.org/setting/locale.language";
213         SettingInfo::GetValue(key1 , __langValue);
214
215         return true;
216 }
217
218 bool
219 ClockApp::OnAppWillTerminate(void)
220 {
221         AppLog("OnAppWillTerminate");
222         SettingInfo::RemoveSettingEventListener(*this);
223
224         return true;
225 }
226
227
228 bool
229 ClockApp::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
230 {
231         AppLog("ClockApp::OnAppTerminating %d", forcedTermination);
232         result r = E_FAILURE;
233         DateTime recentAlarmTime;
234         DateTime timerTime;
235         int status = 0;
236         DateTime currentTime;
237         String registeredTime;
238         DateTime alarmTime;
239         String remainingTime;
240         String tempTick;
241         String startTick;
242         String restartTick;
243         String stopTick;
244         int cmp = 0;
245         String userSetValue;
246
247         AlarmPresentationModel* pAlarmPresentationModel = AlarmPresentationModel::GetInstance();
248         TimerPresentationModel* pTimerPresentationModel = TimerPresentationModel::GetInstance();
249         DataServiceProvider* pDataServiceProvider = DataServiceProvider::GetInstance();
250
251         SceneManager* pSceneManager = SceneManager::GetInstance();
252
253         if (pSceneManager != null)
254         {
255                 //if(pSceneManager->GetCurrentSceneId() != IDSCN_TIMER_RINGING)
256                 {
257                         r = __pAppRegistry->Set(__nameKey, pSceneManager->GetCurrentSceneId());
258
259                         if (IsFailed(r))
260                         {
261                                 r = __pAppRegistry->Add(__nameKey, pSceneManager->GetCurrentSceneId());
262
263                                 if (IsFailed(r))
264                                 {
265                                         return false;
266                                 }
267                         }
268                 }
269         }
270
271         pDataServiceProvider->GetCurrentDateTime(currentTime);
272         pAlarmPresentationModel->GetRecentAlarm(recentAlarmTime, *__pAlarm);
273         pAlarmPresentationModel->SetAlarm(*__pAlarm);
274         timerTime = currentTime;
275         pTimerPresentationModel->GetTimerValue(timerTime, remainingTime, status, startTick, tempTick, restartTick, stopTick, userSetValue, registeredTime);
276         AppLog("timer time = %ls currentTime = %ls status = %d __pAlarm time = %ls", timerTime.ToString().GetPointer(), currentTime.ToString().GetPointer(), status, __pAlarm->GetAlarmTime().ToString().GetPointer());
277
278         cmp = DateTime::Compare(currentTime, timerTime);
279
280         if((status == TIMER_START || status == TIMER_RESTART) && cmp <= 0)
281         {
282                 r = __pAppRegistry->Set(__timerTimeKey, timerTime.ToString());
283
284                 if (IsFailed(r))
285                 {
286                         r = __pAppRegistry->Add(__timerTimeKey, timerTime.ToString());
287
288                         if (IsFailed(r))
289                         {
290                                 return false;
291                         }
292                 }
293         }
294         else
295         {
296                 timerTime.SetValue(9999,1,1);
297                 r = __pAppRegistry->Set(__timerTimeKey, timerTime.ToString());
298
299                 if (IsFailed(r))
300                 {
301                         r = __pAppRegistry->Add(__timerTimeKey, timerTime.ToString());
302
303                         if (IsFailed(r))
304                         {
305                                 return false;
306                         }
307                 }
308         }
309
310         r = __pAppRegistry->Set(__userSetValueKey, TimerPresentationModel::GetInstance()->userValue);
311         if(r != E_SUCCESS)
312         {
313                 __pAppRegistry->Add(__userSetValueKey, TimerPresentationModel::GetInstance()->userValue);
314         }
315
316         r = __pAppRegistry->Set(__currentTimeKey, currentTime.ToString());
317         if (r != E_SUCCESS)
318         {
319                 r = __pAppRegistry->Add(__currentTimeKey, currentTime.ToString());
320                 if (r != E_SUCCESS)
321                 {
322                         return false;
323                 }
324         }
325
326         __pAppRegistry->Save();
327
328         if (__pTimer != null)
329         {
330                 __pTimer->Cancel();
331         }
332         AppLog("End");
333
334         return true;
335 }
336
337 void
338 ClockApp::OnForeground(void)
339 {
340
341
342 }
343
344 void
345 ClockApp::OnBackground(void)
346 {
347 /*      SceneManager* pSceneManager = SceneManager::GetInstance();
348
349         if (pSceneManager != null)
350         {
351                 if(pSceneManager->GetCurrentSceneId() == IDSCN_TIMER_RINGING)
352                 {
353
354                         result r = E_FAILURE;
355                         UiApp* pApp = null;
356                         int status = 0;
357                         TimerPresentationModel* pTimerPresentationModel = TimerPresentationModel::GetInstance();
358
359                         if ( pTimerPresentationModel == null )
360                         {
361                                 return;
362                         }
363                         pTimerPresentationModel->SetStatus(status);
364
365                         if (pSceneManager != null)
366                         {
367                                 r = pSceneManager->GoBackward(BackwardSceneTransition());
368
369                                 if(IsFailed(r))
370                                 {
371                                         pApp = UiApp::GetInstance();
372                                         pApp->Terminate();
373                                 }
374                         }
375                 }
376         }*/
377 }
378
379 void
380 ClockApp::OnLowMemory(void)
381 {
382
383 }
384
385 void
386 ClockApp::OnBatteryLevelChanged(Tizen::System::BatteryLevel batteryLevel)
387 {
388
389 }
390
391 void
392 ClockApp::OnAlarmExpired(Tizen::System::Alarm& alarm)
393 {
394         AppLog("ClockApp::OnAlarmExpired");
395 }
396
397 void
398 ClockApp::ShowAlarmRingingForm(void)
399 {
400         AppLog("ClockApp::ShowAlarmRingingForm");
401         DateTime currentTime;
402         AlarmData* pAlarm = null;
403         ArrayList* pArgList = new (std::nothrow) ArrayList();
404         SceneManager* pSceneManager = SceneManager::GetInstance();
405         AlarmPresentationModel* pAlarmPresentationModel = AlarmPresentationModel::GetInstance();
406         result r = E_FAILURE;
407         ArrayList alarmsList;
408         alarmsList.Construct();
409         int alarmCount = 0;
410         pAlarmPresentationModel->GetAlarmCount(alarmCount);
411         pAlarmPresentationModel->GetAlarm(0, alarmCount, alarmsList);
412
413         for(int count = 0; count < alarmCount; count++)
414         {
415                 AlarmData *pCurAlarm = dynamic_cast<AlarmData*>(alarmsList.GetAt(count));
416                 if(pCurAlarm != null && __ringingAlarmId != -1 && pCurAlarm->GetAlarmId() == __ringingAlarmId)
417                 {
418                         pAlarm = pCurAlarm;
419                         __pAlarm = pCurAlarm;
420                         break;
421                 }
422         }
423
424         for(int count = 0; count < alarmCount; count++)
425         {
426                 AlarmData *pCurAlarm = dynamic_cast<AlarmData*>(alarmsList.GetAt(count));
427
428                 if (pCurAlarm != null && pAlarm != null)
429                 {
430                         int cmp = DateTime::Compare(pCurAlarm->GetNextRingingTime(), pAlarm->GetNextRingingTime());
431
432                         if(pCurAlarm != null && cmp == 0 && pCurAlarm->GetAlarmId() != pAlarm->GetAlarmId())
433                         {
434                                 if (pCurAlarm->GetSnoozeOn())
435                                 {
436                                         //snooze alarm
437                                         DateTime nextAlarmTime;
438                                         int snoozeRepeatCount;
439                                         nextAlarmTime.SetValue(pCurAlarm->GetAlarmTime());
440                                         pCurAlarm->SetLastRingingTime(nextAlarmTime);
441                                         snoozeRepeatCount = pCurAlarm->GetSnoozeRepeatCount();
442                                         pCurAlarm->SetSnoozeRepeatCount(snoozeRepeatCount - 1);
443
444                                         if (snoozeRepeatCount <= 0 && !pCurAlarm->GetRepeatFlags().Contains(L"1"))
445                                         {
446                                                 pCurAlarm->SetAlarmOnState(0);
447                                         }
448
449                                 }
450                                 else
451                                 {
452                                         pCurAlarm->SetSnoozeRepeatCount(-1);
453
454                                         if(!pCurAlarm->GetRepeatFlags().Contains(L"1"))
455                                         {
456                                                 pCurAlarm->SetAlarmOnState(0);
457                                         }
458
459                                 }
460
461                                 r = pAlarmPresentationModel->SaveAlarm(*pCurAlarm);
462
463                                 if (r == E_STORAGE_FULL || r == E_INIT_FAILED || r == E_OUT_OF_MEMORY)
464                                 {
465                                         MessageBox msgBox;
466                                         int modalMsgBoxResult;
467                                         String msg = CommonUtil::GetString("IDS_MEMORY_FULL");
468                                         r = msgBox.Construct(L"", msg, MSGBOX_STYLE_NONE, 3000);
469                                         AppLog("the result is %s", GetErrorMessage(r));
470                                         msgBox.ShowAndWait(modalMsgBoxResult);
471                                 }
472                         }
473                 }
474         }
475
476         if (pArgList != null && pAlarm != null)
477         {
478                 bool isScreenOn = PowerManager::IsScreenOn();
479                 bool isLocked = LockManager::GetInstance()->IsLocked();
480                 pArgList->Construct();
481                 //              DataServiceProvider::GetCurrentDateTime(currentTime);
482                 //              pAlarm = new(std::nothrow) AlarmData();
483                 //              pAlarmPresentationModel->GetRecentAlarm(currentTime, *pAlarm);
484                 pAlarmPresentationModel->SetAlarm(*pAlarm);
485                 pArgList->Add(*pAlarm);
486
487                 if (pSceneManager != null)
488                 {
489                         if (isScreenOn && !isLocked)
490                         {
491                                 r = pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALARM_RINGING), pArgList);
492                                 if (IsFailed(r))
493                                 {
494                                         __pTimer->Start(1000);
495                                 }
496                                 AppLog("ShowAlarmRingingForm result = %s", GetErrorMessage(r));
497                         }
498                         else
499                         {
500                                 //bring application in foreground
501
502                                 AppManager* pAppMgr = AppManager::GetInstance();
503                                 pAppMgr->LaunchApplication(L"PhYwYqDa1I",null);
504                                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALARM_RINGING_LOCK), pArgList);
505                                 PowerManager::TurnScreenOn();
506                                 PowerManager::KeepScreenOnState(true,false);
507                         }
508
509                 }
510         }
511
512         if (pArgList != null)
513         {
514                 pArgList->RemoveAll();
515                 delete pArgList;
516                 pArgList = null;
517         }
518 }
519
520 void
521 ClockApp::ShowTimerRingingForm(void)
522 {
523         result r = E_FAILURE;
524         bool isScreenOn = PowerManager::IsScreenOn();
525         bool isLocked = LockManager::GetInstance()->IsLocked();
526         ArrayList* pArgList = null;
527         String timerTimeStr;
528         DateTime timerTime;
529         timerTime.SetValue(9999,1,1);
530         r = __pAppRegistry->Get(__timerTimeKey, timerTimeStr);
531         AppLog("Timer: %ls", timerTimeStr.GetPointer());
532         DateTime::Parse(timerTimeStr, timerTime);
533
534         pArgList = new (std::nothrow) ArrayList();
535
536         if (pArgList != null)
537         {
538                 pArgList->Construct();
539                 pArgList->Add(TimerPresentationModel::GetInstance()->userValue);
540         }
541
542         Form *pTimerForm = dynamic_cast<Form*>(GetFrameAt(0)->GetControl("TimerForm"));
543         if(pTimerForm != NULL)
544         {
545                 pTimerForm->SendUserEvent(10000, null);
546         }
547
548         if (isScreenOn && !isLocked)
549         {
550                 SceneManager::GetInstance()->GoForward(ForwardSceneTransition(IDSCN_TIMER_RINGING, SCENE_TRANSITION_ANIMATION_TYPE_NONE), pArgList);
551         }
552         else
553         {
554                 //bring application in foreground
555                 PowerManager::TurnScreenOn();
556                 AppManager* pAppMgr = AppManager::GetInstance();
557                 pAppMgr->LaunchApplication(L"PhYwYqDa1I",null);
558                 PowerManager::KeepScreenOnState(true,false);
559                 SceneManager::GetInstance()->GoForward(ForwardSceneTransition(IDSCN_TIMER_RINGING_LOCK, SCENE_TRANSITION_ANIMATION_TYPE_NONE), pArgList);
560         }
561 }
562
563 void
564 ClockApp::OnSettingChanged(Tizen::Base::String& key)
565 {
566         String value;
567         SettingInfo::GetValue(key, value);
568
569         if ((key == L"http://tizen.org/setting/locale.language" && __langValue != value)|| key == L"http://tizen.org/setting/font.size")
570         {
571                 Terminate();
572         }
573         if(key == L"http://tizen.org/setting/locale.date_time")
574         {
575                 /*AppLog("ClockApp::OnSettingChanged");
576                 DateTime currTime;
577                 AlarmData alarmData;
578                 DataServiceProvider::GetCurrentDateTime(currTime);
579                 AlarmPresentationModel::GetInstance()->GetRecentAlarm(currTime, alarmData);
580                 AppLog("currTime : %S alarmData : %S", currTime.ToString().GetPointer(), alarmData.GetAlarmTime().ToString().GetPointer());
581                  */
582                 AlarmPresentationModel::GetInstance()->ReRegisterAlarm();
583                 RegisterAppForTimer();
584         }
585 }
586
587 void
588 ClockApp::OnAppLaunchConditionMetN (const Tizen::Base::String &condition, Tizen::Base::Object *pExtraData, Tizen::Base::Collection::IList *pArguments)
589 {
590         AppLog("ClockApp::OnAppLaunchConditionMetN");
591         /*      AppManager::GetInstance()->GetActiveApp(activeAppId);
592         AppLog("appid: %ls, the error: %s",activeAppId.GetPointer(),GetErrorMessage(GetLastResult()));*/
593
594         if (pArguments != null)
595         {
596                 String* str = dynamic_cast<String*>(pArguments->GetAt(0));
597                 if(str == null)
598                 {
599                         return;
600                 }
601
602                 if (str != null && *str == "Timer")
603                 {
604                         if(GetAppState() == RUNNING)
605                         {
606                                 ShowTimerRingingForm();
607                         }
608                         else
609                         {
610                                 __isTimerLaunchedAutomatically = true;
611                         }
612                 }
613                 else
614                 {
615                         str->Remove(0, 8);
616                         if(str->GetLength() > 0)
617                         {
618                                 int alarmId = -1;
619                                 Integer::Parse(*str, alarmId);
620                                 if(GetLastResult() == E_SUCCESS)
621                                 {
622                                         AppLog("AlarmId: %d", alarmId);
623                                         __ringingAlarmId = alarmId;
624                                 }
625                         }
626                         if(GetAppState() == RUNNING)
627                         {
628                                 ShowAlarmRingingForm();
629                         }
630                         else
631                         {
632                                 __isAlarmLaunchedAutomatically = true;
633                         }
634                 }
635         }
636 }
637
638 void
639 ClockApp::ResetValues()
640 {
641         result r = E_FAILURE;
642         String previousCurrentTime;
643         String upTimeValue;
644         DateTime previousTime;
645         DateTime currentTime;
646
647         previousTime.SetValue(9999,1,1);
648         r = __pAppRegistry->Get(__currentTimeKey, previousCurrentTime);
649
650         if (r == E_KEY_NOT_FOUND)
651         {
652                 __pAppRegistry->Add(__currentTimeKey, previousTime.ToString());
653         }
654         else
655         {
656                 DateTime::Parse(previousCurrentTime, previousTime);
657         }
658
659         AppLog("previousTime = %S", previousTime.ToString().GetPointer());
660
661         TimeSpan uptime(0);
662         SystemTime::GetUptime(uptime);
663         r = previousTime.AddTicks(uptime.GetTicks());
664
665         if (IsFailed(r))
666         {
667                 return;
668         }
669
670         DataServiceProvider::GetInstance()->GetCurrentDateTime(currentTime);
671         int cmp = DateTime::Compare(previousTime, currentTime);
672         AppLog("previousTime = %S currentTime = %S", previousTime.ToString().GetPointer(), currentTime.ToString().GetPointer());
673         if (cmp < 0)
674         {
675                 AppLog("coming here");
676                 String startTick;
677                 String tempTick;
678                 String restartTick;
679                 String stopTick;
680                 DateTime dateTime;
681                 String userSetValue;
682                 String registeredTime;
683                 int status = 0;
684                 TimerPresentationModel* pTimerPresentationModel = TimerPresentationModel::GetInstance();
685                 pTimerPresentationModel->SetTimerValue(dateTime, status, startTick, tempTick, restartTick, stopTick, userSetValue, registeredTime);
686
687                 StopWatchPresentationModel::GetInstance()->SetStatus(status);
688                 __isrebooted = true;
689         }
690 }
691
692 void
693 ClockApp::RegisterAppForTimer(void)
694 {
695         AppLog("ClockApp::RegisterAppForTimer");
696         DateTime timerTime;
697         DateTime currentTime;
698         String remainingTime;
699         String userSetValue;
700         result r = E_FAILURE;
701         String tempTick;
702         String startTick;
703         String restartTick;
704         String stopTick;
705         int status = 0;
706         String registeredTime;
707         int cmp = 0;
708         int spanHours = 0;
709         int spanMinutes = 0;
710         int spanSeconds = 0;
711         long long tick ;
712         String* str1;
713         String* str2;
714         TimeSpan uptime(0);
715
716         TimerPresentationModel::GetInstance()->GetTimerValue(timerTime, remainingTime, status, startTick, tempTick, restartTick, stopTick, userSetValue, registeredTime);
717
718         if (status == TIMER_START || status == TIMER_RESTART)
719         {
720                 DateTime::Parse(registeredTime, timerTime);
721                 str2 = new (std::nothrow) String("DateTime=");
722
723                 if (str2 == null)
724                 {
725                         return;
726                 }
727
728                 str2->Append("'");
729                 str2->Append(timerTime.ToString());
730                 str2->Append("'");
731                 AppManager::GetInstance()->UnregisterAppLaunch(App::GetInstance()->GetAppId(), str2);
732
733                 if (str2 != null)
734                 {
735                         delete str2;
736                         str2 = null;
737                 }
738
739                 long long start, temp;
740                 LongLong::Parse(startTick, start);
741                 LongLong::Parse(tempTick, temp);
742
743                 SystemTime::GetUptime(uptime);
744                 tick = uptime.GetTicks();
745                 startTick.Append(start);
746
747                 if (status == TIMER_START)
748                 {
749                         tick = start - tick;
750                 }
751                 else if (status == TIMER_RESTART)
752                 {
753                         tick = temp + start - tick;
754                 }
755
756                 TimeSpan span = static_cast<long>(tick);
757                 spanHours = span.GetHours() + span.GetDays()*24;
758                 spanMinutes = span.GetMinutes();
759                 spanSeconds = span.GetSeconds();
760
761                 DataServiceProvider::GetInstance()->GetCurrentDateTime(currentTime);
762                 timerTime = currentTime;
763                 timerTime.AddHours(spanHours);
764                 timerTime.AddMinutes(spanMinutes);
765                 timerTime.AddSeconds(spanSeconds);
766
767                 cmp = DateTime::Compare(currentTime, timerTime);
768
769                 if(cmp <=0 )
770                 {
771                         AppLog("ClockApp::RegisterAppForTimer 3");
772                         str1 = new (std::nothrow) String(App::GetInstance()->GetAppId());
773
774                         if (str1 == null)
775                         {
776                                 return;
777                         }
778
779                         str2 = new (std::nothrow) String("DateTime=");
780
781                         if (str2 == null)
782                         {
783                                 delete str1;
784                                 str1 = null;
785
786                                 return;
787                         }
788
789                         str2->Append("'");
790                         str2->Append(timerTime.ToString());
791                         str2->Append("'");
792
793                         String *timerType = new String("Timer");
794                         ArrayList* pArgList = new (std::nothrow) ArrayList();
795
796                         if (pArgList != null)
797                         {
798                                 pArgList->Construct();
799                                 pArgList->Add(timerType);
800                         }
801
802                         r = AppManager::GetInstance()->RegisterAppLaunch(*str1,*str2, pArgList, AppManager::LAUNCH_OPTION_DEFAULT);
803                         AppLog("__registeredTime result = %s", GetErrorMessage(r));
804
805                         if (IsFailed(r))
806                         {
807                                 timerTime.AddSeconds(1);
808
809                                 str2->Clear();
810                                 str2->Append("'");
811                                 str2->Append(timerTime.ToString());
812                                 str2->Append("'");
813                                 r = AppManager::GetInstance()->RegisterAppLaunch(*str1,*str2, pArgList, AppManager::LAUNCH_OPTION_DEFAULT);
814                                 AppLog("__registeredTime result = %s", GetErrorMessage(r));
815                         }
816
817                         TimerPresentationModel::GetInstance()->SetRegisteredTime(timerTime);
818
819                         if (str1 != null)
820                         {
821                                 delete str1;
822                                 str1 = null;
823                         }
824
825                         if (str2 != null)
826                         {
827                                 delete str2;
828                                 str2 = null;
829                         }
830                 }
831         }
832
833         AppLog("ClockApp::RegisterAppForTimer end");
834 }
835
836 void
837 ClockApp::OnTimerExpired(Timer&  timer)
838 {
839         DateTime currentTime;
840         ArrayList* pArgList = new (std::nothrow) ArrayList();
841         SceneManager* pSceneManager = SceneManager::GetInstance();
842         result r = E_FAILURE;
843
844         if (pArgList != null && __pAlarm != null)
845         {
846                 bool isScreenOn = PowerManager::IsScreenOn();
847                 bool isLocked = LockManager::GetInstance()->IsLocked();
848                 pArgList->Construct();
849                 pArgList->Add(*__pAlarm);
850
851                 if (pSceneManager != null)
852                 {
853                         if (isScreenOn && !isLocked)
854                         {
855                                 r = pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALARM_RINGING), pArgList);
856                                 AppLog("ShowAlarmRingingForm result = %s", GetErrorMessage(r));
857                         }
858                 }
859         }
860
861         if (pArgList != null)
862         {
863                 pArgList->RemoveAll();
864                 delete pArgList;
865                 pArgList = null;
866         }
867 }
868
869 int
870 ClockApp::GetFontSize()
871 {
872         int fontSize = 0;
873         fontSizeStr = L"";
874         SettingInfo::GetValue("http://tizen.org/setting/font.size", fontSizeStr);
875         AppLog("the font: %ls",fontSizeStr.GetPointer());
876
877         if(fontSizeStr == "medium")
878         {
879                 fontSize = 44;
880         }
881         else if(fontSizeStr == "huge")
882         {
883                 fontSize = 81;
884         }
885         else if(fontSizeStr == "giant")
886         {
887                 fontSize = 98;
888         }
889         else if(fontSizeStr == "small")
890         {
891                 fontSize = 36;
892         }
893         else if(fontSizeStr == "large")
894         {
895                 fontSize = 64;
896         }
897         else
898         {
899                 fontSize = 44;
900         }
901
902         return fontSize;
903
904 }
905
906 int
907 ClockApp::GetHeightOffset()
908 {
909         int fontSize = GetFontSize();
910         return fontSize - 44;
911 }
912 void
913 ClockApp::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs)
914 {
915
916         if (requestId == REQUEST_ID_SHOW_TIMERFORM)
917         {
918                 ShowTimerRingingForm();
919         }
920
921 }