3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://floralicense.org/license/
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.
19 * @file ClkClockApp.cpp
20 * @brief Used to define ClockApp
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"
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;
42 static const int TIMER_START = 117;
43 static const int TIMER_RESTART = 119;
46 ClockApp::ClockApp(void)
47 :__currentTimeKey("currentTime"),
48 __isAlarmLaunchedAutomatically(false),
50 __isTimerLaunchedAutomatically(false),
57 __timerResetKey("timerReset"),
58 __timerTimeKey("timerTime"),
59 __userSetValueKey("serTimerSetValue")
64 ClockApp::~ClockApp(void)
75 ClockApp::CreateInstance(void)
77 return new (std::nothrow) ClockApp();
81 ClockApp::OnAppInitializing(AppRegistry& appRegistry)
83 AppLog("ClockApp::OnAppInitializing");
89 String timerTimeString;
92 TimerPresentationModel::GetInstance();
93 StopWatchPresentationModel::GetInstance();
94 AlarmPresentationModel* pAlarmPresentationModel = AlarmPresentationModel::GetInstance();
95 WorldClockPresentationModel* pWorldClockPresentationModel = WorldClockPresentationModel::GetInstance();
96 DataServiceProvider* pDataServiceProvider = DataServiceProvider::GetInstance();
98 AppManager::GetInstance()->SetAppLaunchConditionEventListener(this);
100 SettingInfo::AddSettingEventListener(*this);
101 int timerResetValue = 0;
103 pAlarmPresentationModel->AddEventListener(this);
104 __pAppRegistry = GetAppRegistry();
105 __sceneId = IDSCN_ALARM_LIST;
106 r = __pAppRegistry->Get(__nameKey, __sceneId);
108 if (r == E_KEY_NOT_FOUND)
110 __pAppRegistry->Add(__nameKey, IDSCN_ALARM_LIST);
113 r = __pAppRegistry->Get(__timerResetKey, timerResetValue);
115 if (r == E_KEY_NOT_FOUND)
117 __pAppRegistry->Add(__timerResetKey, timerResetValue);
120 r = __pAppRegistry->Get(__timerTimeKey, timerTimeString);
122 if (r == E_KEY_NOT_FOUND)
124 __pAppRegistry->Add(__timerTimeKey, timerTimeString);
125 __timerTimeReached.SetValue(9999,1,1);
129 DateTime::Parse(timerTimeString, __timerTimeReached);
132 r = __pAppRegistry->Get(__userSetValueKey, TimerPresentationModel::GetInstance()->userValue);
134 if (r == E_KEY_NOT_FOUND)
136 __pAppRegistry->Add(__userSetValueKey, TimerPresentationModel::GetInstance()->userValue);
139 pDataServiceProvider->Initialize();
140 pAlarmPresentationModel->Initialize();
141 pWorldClockPresentationModel->Initialize();
143 //RegisterAppForTimer();// after change in settings
145 __pTimer = new (std::nothrow) Timer();
146 __pTimer->Construct(*this);
152 ClockApp::OnAppInitialized(void)
154 AppLog("ClockApp::OnAppInitialized");
156 MainFrame* pClockFrame = null;
158 if (__pAlarm != null)
163 __pAlarm = new (std::nothrow) AlarmData();
165 pClockFrame = new (std::nothrow) MainFrame();
166 pClockFrame->Construct();
167 pClockFrame->SetName(L"Clock");
168 AddFrame(*pClockFrame);
172 if(__isTimerLaunchedAutomatically == true)
179 ShowTimerRingingForm();
181 else if (__isAlarmLaunchedAutomatically == true)
183 ShowAlarmRingingForm();
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)
187 SceneManager::GetInstance()->GoForward(ForwardSceneTransition(IDSCN_ALARM_LIST, SCENE_TRANSITION_ANIMATION_TYPE_NONE));
189 else if(__sceneId == IDSCN_CITY_LIST || __sceneId == IDSCN_WORLD_CLOCK_DELETE_LIST || __sceneId == IDSCN_WORLD_CLOCK_REORDER || __sceneId == IDSCN_WORLD_CLOCK_LIST)
191 SceneManager::GetInstance()->GoForward(ForwardSceneTransition(IDSCN_WORLD_CLOCK_LIST, SCENE_TRANSITION_ANIMATION_TYPE_NONE));
193 else if(__sceneId == IDSCN_STOP_WATCH)
195 SceneManager::GetInstance()->GoForward(ForwardSceneTransition(IDSCN_STOP_WATCH, SCENE_TRANSITION_ANIMATION_TYPE_NONE));
197 else if(__sceneId == IDSCN_TIMER_VIEW || __sceneId == IDSCN_TIMER_RINGING || __sceneId == IDSCN_TIMER_RINGING_LOCK )
199 if (__sceneId == IDSCN_TIMER_RINGING || __sceneId == IDSCN_TIMER_RINGING_LOCK)
202 TimerPresentationModel::GetInstance()->SetStatus(status);
205 SceneManager::GetInstance()->GoForward(ForwardSceneTransition(IDSCN_TIMER_VIEW, SCENE_TRANSITION_ANIMATION_TYPE_NONE));
209 SceneManager::GetInstance()->GoForward(ForwardSceneTransition(__sceneId, SCENE_TRANSITION_ANIMATION_TYPE_NONE));
212 String key1 = L"http://tizen.org/setting/locale.language";
213 SettingInfo::GetValue(key1 , __langValue);
219 ClockApp::OnAppWillTerminate(void)
221 AppLog("OnAppWillTerminate");
222 SettingInfo::RemoveSettingEventListener(*this);
229 ClockApp::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
231 AppLog("ClockApp::OnAppTerminating %d", forcedTermination);
232 result r = E_FAILURE;
233 DateTime recentAlarmTime;
236 DateTime currentTime;
237 String registeredTime;
239 String remainingTime;
247 AlarmPresentationModel* pAlarmPresentationModel = AlarmPresentationModel::GetInstance();
248 TimerPresentationModel* pTimerPresentationModel = TimerPresentationModel::GetInstance();
249 DataServiceProvider* pDataServiceProvider = DataServiceProvider::GetInstance();
251 SceneManager* pSceneManager = SceneManager::GetInstance();
253 if (pSceneManager != null)
255 //if(pSceneManager->GetCurrentSceneId() != IDSCN_TIMER_RINGING)
257 r = __pAppRegistry->Set(__nameKey, pSceneManager->GetCurrentSceneId());
261 r = __pAppRegistry->Add(__nameKey, pSceneManager->GetCurrentSceneId());
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());
278 cmp = DateTime::Compare(currentTime, timerTime);
280 if((status == TIMER_START || status == TIMER_RESTART) && cmp <= 0)
282 r = __pAppRegistry->Set(__timerTimeKey, timerTime.ToString());
286 r = __pAppRegistry->Add(__timerTimeKey, timerTime.ToString());
296 timerTime.SetValue(9999,1,1);
297 r = __pAppRegistry->Set(__timerTimeKey, timerTime.ToString());
301 r = __pAppRegistry->Add(__timerTimeKey, timerTime.ToString());
310 r = __pAppRegistry->Set(__userSetValueKey, TimerPresentationModel::GetInstance()->userValue);
313 __pAppRegistry->Add(__userSetValueKey, TimerPresentationModel::GetInstance()->userValue);
316 r = __pAppRegistry->Set(__currentTimeKey, currentTime.ToString());
319 r = __pAppRegistry->Add(__currentTimeKey, currentTime.ToString());
326 __pAppRegistry->Save();
328 if (__pTimer != null)
338 ClockApp::OnForeground(void)
345 ClockApp::OnBackground(void)
347 /* SceneManager* pSceneManager = SceneManager::GetInstance();
349 if (pSceneManager != null)
351 if(pSceneManager->GetCurrentSceneId() == IDSCN_TIMER_RINGING)
354 result r = E_FAILURE;
357 TimerPresentationModel* pTimerPresentationModel = TimerPresentationModel::GetInstance();
359 if ( pTimerPresentationModel == null )
363 pTimerPresentationModel->SetStatus(status);
365 if (pSceneManager != null)
367 r = pSceneManager->GoBackward(BackwardSceneTransition());
371 pApp = UiApp::GetInstance();
380 ClockApp::OnLowMemory(void)
386 ClockApp::OnBatteryLevelChanged(Tizen::System::BatteryLevel batteryLevel)
392 ClockApp::OnAlarmExpired(Tizen::System::Alarm& alarm)
394 AppLog("ClockApp::OnAlarmExpired");
398 ClockApp::ShowAlarmRingingForm(void)
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();
410 pAlarmPresentationModel->GetAlarmCount(alarmCount);
411 pAlarmPresentationModel->GetAlarm(0, alarmCount, alarmsList);
413 for(int count = 0; count < alarmCount; count++)
415 AlarmData *pCurAlarm = dynamic_cast<AlarmData*>(alarmsList.GetAt(count));
416 if(pCurAlarm != null && __ringingAlarmId != -1 && pCurAlarm->GetAlarmId() == __ringingAlarmId)
419 __pAlarm = pCurAlarm;
424 for(int count = 0; count < alarmCount; count++)
426 AlarmData *pCurAlarm = dynamic_cast<AlarmData*>(alarmsList.GetAt(count));
428 if (pCurAlarm != null && pAlarm != null)
430 int cmp = DateTime::Compare(pCurAlarm->GetNextRingingTime(), pAlarm->GetNextRingingTime());
432 if(pCurAlarm != null && cmp == 0 && pCurAlarm->GetAlarmId() != pAlarm->GetAlarmId())
434 if (pCurAlarm->GetSnoozeOn())
437 DateTime nextAlarmTime;
438 int snoozeRepeatCount;
439 nextAlarmTime.SetValue(pCurAlarm->GetAlarmTime());
440 pCurAlarm->SetLastRingingTime(nextAlarmTime);
441 snoozeRepeatCount = pCurAlarm->GetSnoozeRepeatCount();
442 pCurAlarm->SetSnoozeRepeatCount(snoozeRepeatCount - 1);
444 if (snoozeRepeatCount <= 0 && !pCurAlarm->GetRepeatFlags().Contains(L"1"))
446 pCurAlarm->SetAlarmOnState(0);
452 pCurAlarm->SetSnoozeRepeatCount(-1);
454 if(!pCurAlarm->GetRepeatFlags().Contains(L"1"))
456 pCurAlarm->SetAlarmOnState(0);
461 r = pAlarmPresentationModel->SaveAlarm(*pCurAlarm);
463 if (r == E_STORAGE_FULL || r == E_INIT_FAILED || r == E_OUT_OF_MEMORY)
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);
476 if (pArgList != null && pAlarm != null)
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);
487 if (pSceneManager != null)
489 if (isScreenOn && !isLocked)
491 r = pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALARM_RINGING), pArgList);
494 __pTimer->Start(1000);
496 AppLog("ShowAlarmRingingForm result = %s", GetErrorMessage(r));
500 //bring application in foreground
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);
512 if (pArgList != null)
514 pArgList->RemoveAll();
521 ClockApp::ShowTimerRingingForm(void)
523 result r = E_FAILURE;
524 bool isScreenOn = PowerManager::IsScreenOn();
525 bool isLocked = LockManager::GetInstance()->IsLocked();
526 ArrayList* pArgList = null;
529 timerTime.SetValue(9999,1,1);
530 r = __pAppRegistry->Get(__timerTimeKey, timerTimeStr);
531 AppLog("Timer: %ls", timerTimeStr.GetPointer());
532 DateTime::Parse(timerTimeStr, timerTime);
534 pArgList = new (std::nothrow) ArrayList();
536 if (pArgList != null)
538 pArgList->Construct();
539 pArgList->Add(TimerPresentationModel::GetInstance()->userValue);
542 Form *pTimerForm = dynamic_cast<Form*>(GetFrameAt(0)->GetControl("TimerForm"));
543 if(pTimerForm != NULL)
545 pTimerForm->SendUserEvent(10000, null);
548 if (isScreenOn && !isLocked)
550 SceneManager::GetInstance()->GoForward(ForwardSceneTransition(IDSCN_TIMER_RINGING, SCENE_TRANSITION_ANIMATION_TYPE_NONE), pArgList);
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);
564 ClockApp::OnSettingChanged(Tizen::Base::String& key)
567 SettingInfo::GetValue(key, value);
569 if ((key == L"http://tizen.org/setting/locale.language" && __langValue != value)|| key == L"http://tizen.org/setting/font.size")
573 if(key == L"http://tizen.org/setting/locale.date_time")
575 /*AppLog("ClockApp::OnSettingChanged");
578 DataServiceProvider::GetCurrentDateTime(currTime);
579 AlarmPresentationModel::GetInstance()->GetRecentAlarm(currTime, alarmData);
580 AppLog("currTime : %S alarmData : %S", currTime.ToString().GetPointer(), alarmData.GetAlarmTime().ToString().GetPointer());
582 AlarmPresentationModel::GetInstance()->ReRegisterAlarm();
583 RegisterAppForTimer();
588 ClockApp::OnAppLaunchConditionMetN (const Tizen::Base::String &condition, Tizen::Base::Object *pExtraData, Tizen::Base::Collection::IList *pArguments)
590 AppLog("ClockApp::OnAppLaunchConditionMetN");
591 /* AppManager::GetInstance()->GetActiveApp(activeAppId);
592 AppLog("appid: %ls, the error: %s",activeAppId.GetPointer(),GetErrorMessage(GetLastResult()));*/
594 if (pArguments != null)
596 String* str = dynamic_cast<String*>(pArguments->GetAt(0));
602 if (str != null && *str == "Timer")
604 if(GetAppState() == RUNNING)
606 ShowTimerRingingForm();
610 __isTimerLaunchedAutomatically = true;
616 if(str->GetLength() > 0)
619 Integer::Parse(*str, alarmId);
620 if(GetLastResult() == E_SUCCESS)
622 AppLog("AlarmId: %d", alarmId);
623 __ringingAlarmId = alarmId;
626 if(GetAppState() == RUNNING)
628 ShowAlarmRingingForm();
632 __isAlarmLaunchedAutomatically = true;
639 ClockApp::ResetValues()
641 result r = E_FAILURE;
642 String previousCurrentTime;
644 DateTime previousTime;
645 DateTime currentTime;
647 previousTime.SetValue(9999,1,1);
648 r = __pAppRegistry->Get(__currentTimeKey, previousCurrentTime);
650 if (r == E_KEY_NOT_FOUND)
652 __pAppRegistry->Add(__currentTimeKey, previousTime.ToString());
656 DateTime::Parse(previousCurrentTime, previousTime);
659 AppLog("previousTime = %S", previousTime.ToString().GetPointer());
662 SystemTime::GetUptime(uptime);
663 r = previousTime.AddTicks(uptime.GetTicks());
670 DataServiceProvider::GetInstance()->GetCurrentDateTime(currentTime);
671 int cmp = DateTime::Compare(previousTime, currentTime);
672 AppLog("previousTime = %S currentTime = %S", previousTime.ToString().GetPointer(), currentTime.ToString().GetPointer());
675 AppLog("coming here");
682 String registeredTime;
684 TimerPresentationModel* pTimerPresentationModel = TimerPresentationModel::GetInstance();
685 pTimerPresentationModel->SetTimerValue(dateTime, status, startTick, tempTick, restartTick, stopTick, userSetValue, registeredTime);
687 StopWatchPresentationModel::GetInstance()->SetStatus(status);
693 ClockApp::RegisterAppForTimer(void)
695 AppLog("ClockApp::RegisterAppForTimer");
697 DateTime currentTime;
698 String remainingTime;
700 result r = E_FAILURE;
706 String registeredTime;
716 TimerPresentationModel::GetInstance()->GetTimerValue(timerTime, remainingTime, status, startTick, tempTick, restartTick, stopTick, userSetValue, registeredTime);
718 if (status == TIMER_START || status == TIMER_RESTART)
720 DateTime::Parse(registeredTime, timerTime);
721 str2 = new (std::nothrow) String("DateTime=");
729 str2->Append(timerTime.ToString());
731 AppManager::GetInstance()->UnregisterAppLaunch(App::GetInstance()->GetAppId(), str2);
739 long long start, temp;
740 LongLong::Parse(startTick, start);
741 LongLong::Parse(tempTick, temp);
743 SystemTime::GetUptime(uptime);
744 tick = uptime.GetTicks();
745 startTick.Append(start);
747 if (status == TIMER_START)
751 else if (status == TIMER_RESTART)
753 tick = temp + start - tick;
756 TimeSpan span = static_cast<long>(tick);
757 spanHours = span.GetHours() + span.GetDays()*24;
758 spanMinutes = span.GetMinutes();
759 spanSeconds = span.GetSeconds();
761 DataServiceProvider::GetInstance()->GetCurrentDateTime(currentTime);
762 timerTime = currentTime;
763 timerTime.AddHours(spanHours);
764 timerTime.AddMinutes(spanMinutes);
765 timerTime.AddSeconds(spanSeconds);
767 cmp = DateTime::Compare(currentTime, timerTime);
771 AppLog("ClockApp::RegisterAppForTimer 3");
772 str1 = new (std::nothrow) String(App::GetInstance()->GetAppId());
779 str2 = new (std::nothrow) String("DateTime=");
790 str2->Append(timerTime.ToString());
793 String *timerType = new String("Timer");
794 ArrayList* pArgList = new (std::nothrow) ArrayList();
796 if (pArgList != null)
798 pArgList->Construct();
799 pArgList->Add(timerType);
802 r = AppManager::GetInstance()->RegisterAppLaunch(*str1,*str2, pArgList, AppManager::LAUNCH_OPTION_DEFAULT);
803 AppLog("__registeredTime result = %s", GetErrorMessage(r));
807 timerTime.AddSeconds(1);
811 str2->Append(timerTime.ToString());
813 r = AppManager::GetInstance()->RegisterAppLaunch(*str1,*str2, pArgList, AppManager::LAUNCH_OPTION_DEFAULT);
814 AppLog("__registeredTime result = %s", GetErrorMessage(r));
817 TimerPresentationModel::GetInstance()->SetRegisteredTime(timerTime);
833 AppLog("ClockApp::RegisterAppForTimer end");
837 ClockApp::OnTimerExpired(Timer& timer)
839 DateTime currentTime;
840 ArrayList* pArgList = new (std::nothrow) ArrayList();
841 SceneManager* pSceneManager = SceneManager::GetInstance();
842 result r = E_FAILURE;
844 if (pArgList != null && __pAlarm != null)
846 bool isScreenOn = PowerManager::IsScreenOn();
847 bool isLocked = LockManager::GetInstance()->IsLocked();
848 pArgList->Construct();
849 pArgList->Add(*__pAlarm);
851 if (pSceneManager != null)
853 if (isScreenOn && !isLocked)
855 r = pSceneManager->GoForward(ForwardSceneTransition(IDSCN_ALARM_RINGING), pArgList);
856 AppLog("ShowAlarmRingingForm result = %s", GetErrorMessage(r));
861 if (pArgList != null)
863 pArgList->RemoveAll();
870 ClockApp::GetFontSize()
874 SettingInfo::GetValue("http://tizen.org/setting/font.size", fontSizeStr);
875 AppLog("the font: %ls",fontSizeStr.GetPointer());
877 if(fontSizeStr == "medium")
881 else if(fontSizeStr == "huge")
885 else if(fontSizeStr == "giant")
889 else if(fontSizeStr == "small")
893 else if(fontSizeStr == "large")
907 ClockApp::GetHeightOffset()
909 int fontSize = GetFontSize();
910 return fontSize - 44;
913 ClockApp::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs)
916 if (requestId == REQUEST_ID_SHOW_TIMERFORM)
918 ShowTimerRingingForm();