javaextensions/midppush/pushregistryplugin/src/pushalarmhandler.cpp
changeset 21 2a9601315dfc
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <limits.h>
       
    20 
       
    21 #include "pushalarmhandler.h"
       
    22 #include "pushdbhandler.h"
       
    23 #include "logger.h"
       
    24 #include "pushexception.h"
       
    25 #include "pusherrorcodes.h"
       
    26 #include "coreinterface.h"
       
    27 #include "rtcinterface.h"
       
    28 #include "pushtimerutils.h"
       
    29 #include "pushconstant.h"
       
    30 #include "javauid.h"
       
    31 #include "javacommonutils.h"
       
    32 
       
    33 using namespace java::push;
       
    34 using namespace java::captain;
       
    35 using namespace java::comms;
       
    36 using namespace java::util;
       
    37 using namespace std;
       
    38 
       
    39 /**
       
    40  *
       
    41  */
       
    42 PushAlarmHandler::PushAlarmHandler(java::captain::CoreInterface& aCore,
       
    43                                    PushAlarmUtilsInterface& aLaunchInstance,
       
    44                                    PushDBHandler& aDbHandler)
       
    45         : mCore(aCore),mPushDbHandler(aDbHandler),mPushTimer(NULL),mUtilsInstance(aLaunchInstance)
       
    46 {
       
    47     JELOG2(EJavaPush);
       
    48     mPushTimer.reset(new PushTimerContainer(*aCore.getTimerServer(),*this));
       
    49 }
       
    50 
       
    51 /**
       
    52  *
       
    53  */
       
    54 PushAlarmHandler::~PushAlarmHandler()
       
    55 {
       
    56     JELOG2(EJavaPush);
       
    57     mNotLaunchedExpiredAlarms.clear();
       
    58 }
       
    59 
       
    60 /**
       
    61  *
       
    62  */
       
    63 void PushAlarmHandler::readAndStartAlarms()
       
    64 {
       
    65     JELOG2(EJavaPush);
       
    66 
       
    67     Uid emptyUid;
       
    68     readAndStartAlarms(emptyUid);
       
    69 }
       
    70 
       
    71 /**
       
    72  *
       
    73  */
       
    74 void PushAlarmHandler::readAndStartAlarms(const Uid& aUid)
       
    75 {
       
    76     JELOG2(EJavaPush);
       
    77     LOG1WSTR(EJavaPush,EInfo,"readAndStartAlarms(): Uid: %s",aUid.toString());
       
    78 
       
    79     try
       
    80     {
       
    81         std::list<DbAlarmData> alarms;
       
    82         mPushDbHandler.getAlarms(aUid,alarms);
       
    83         if (0 == alarms.size())
       
    84             return;
       
    85 
       
    86         mPushTimer->setAlarmTimers(alarms);
       
    87         alarms.clear();
       
    88     }
       
    89     catch (...)
       
    90     {
       
    91         ELOG(EJavaPush,"ERROR!!! Exception caught in the PushAlarmHandler::readAndStartAlarms()");
       
    92     }
       
    93 }
       
    94 
       
    95 /**
       
    96  *
       
    97  */
       
    98 long long PushAlarmHandler::setAlarm(const Uid& aUid,const long long& aAlarmTimeInMilliSecs,
       
    99                                      bool aIsUidOfThisMidlet)
       
   100 {
       
   101     JELOG2(EJavaPush);
       
   102 
       
   103     LOG1WSTR(EJavaPush,EInfo, "setAlarm() aUid: %s",aUid.toString());
       
   104     long long curTimeInMilliSecs = mPushTimer->getCurrentTime();
       
   105     long long curTimeInMilliSecsPlusSomeExtra =
       
   106         curTimeInMilliSecs + NO_NEED_TO_INITIALZE_TIMER_TIME_IN_MILLI_SECS;
       
   107 
       
   108     long long currentWakeUpTime = mPushTimer->getAlarmTime(aUid);
       
   109     //Alarm can be first deleted from the Storage and cancelled.
       
   110     if (0LL != currentWakeUpTime)
       
   111     {
       
   112         cancelAlarm(aUid);
       
   113         deleteAlarm(aUid);
       
   114     }
       
   115 
       
   116     if (aAlarmTimeInMilliSecs < curTimeInMilliSecsPlusSomeExtra)
       
   117     {
       
   118         LOG1(EJavaPush,EInfo,"aAlarmTimeInMilliSecs %lld",aAlarmTimeInMilliSecs);
       
   119         LOG1(EJavaPush,EInfo,"curTimeInMilliSecsPlusSomeExtra %lld",curTimeInMilliSecsPlusSomeExtra);
       
   120         //We can return 0 in the case wherein MIDlet registers alarm to itself and alarm is max.
       
   121         //'current time + 1'.
       
   122         if (false == aIsUidOfThisMidlet)
       
   123             handleLessOrEqualCurrentTimeOfOtherMidlet(curTimeInMilliSecs,aAlarmTimeInMilliSecs,aUid);
       
   124 
       
   125         //Zero is returned if alarm time has passed.
       
   126         if (curTimeInMilliSecs < aAlarmTimeInMilliSecs)
       
   127         {
       
   128             LOG(EJavaPush,EInfo,"Returns 0");
       
   129             return 0LL;
       
   130         }
       
   131 
       
   132         LOG(EJavaPush,EInfo,"Returns currentWakeUpTime");
       
   133         return currentWakeUpTime;
       
   134     }
       
   135 
       
   136     LOG(EJavaPush,EInfo,"Setting alarm");
       
   137     std::list<DbAlarmData> validAlarms;
       
   138     DbAlarmData newAlaramDataObj(aUid,aAlarmTimeInMilliSecs,false);
       
   139     validAlarms.push_back(newAlaramDataObj);
       
   140     mPushTimer->setAlarmTimers(validAlarms);
       
   141     validAlarms.clear();
       
   142 
       
   143     try
       
   144     {
       
   145         mPushDbHandler.storeAlarm(aUid,aAlarmTimeInMilliSecs);
       
   146     }
       
   147     catch (...)
       
   148     {
       
   149         cancelAlarm(aUid);
       
   150         throw;
       
   151     }
       
   152     return currentWakeUpTime;
       
   153 }
       
   154 
       
   155 /**
       
   156  *
       
   157  */
       
   158 void PushAlarmHandler::handleLessOrEqualCurrentTimeOfOtherMidlet
       
   159 (const long long& aCurTimeInMilliSecs,const long long& aAlarmTimeInMilliSecs,
       
   160  const Uid& aUid)
       
   161 {
       
   162     JELOG2(EJavaPush);
       
   163 
       
   164     JavaTime currentTimeObj(aCurTimeInMilliSecs);
       
   165     JavaTime alarmTimeObj(aAlarmTimeInMilliSecs);
       
   166     //In this case alarm time is older than two weeks so MIDlet is not launched.
       
   167     bool flag = PushTimerUtils::isTimeOlderThanTwoWeeks(mPushTimer->getTimerServerInterface(),
       
   168                 currentTimeObj,alarmTimeObj);
       
   169     if (true == flag)
       
   170         return;
       
   171 
       
   172     //In this case alarm time is less than 'currentTime + NO_NEED_TO_INITIALZE_TIMER_TIME_IN_MILLI_SECS'
       
   173     //or less than two weeks old so MIDlet is launched.
       
   174     timerExpired(aUid,aAlarmTimeInMilliSecs,false);
       
   175 }
       
   176 
       
   177 /**
       
   178  *
       
   179  */
       
   180 void PushAlarmHandler::cancelAlarm(const java::util::Uid& aUid)
       
   181 {
       
   182     JELOG2(EJavaPush);
       
   183 
       
   184     mPushTimer->cancelAlarm(aUid);
       
   185 }
       
   186 
       
   187 /**
       
   188  *
       
   189  */
       
   190 void PushAlarmHandler::handleDriveWasMissingMidlets(const unsigned int aMediaId)
       
   191 {
       
   192     JELOG2(EJavaPush);
       
   193 
       
   194     list<Uid> handledUidsList;
       
   195     uidAlarmTimeContainerIter_t iter = mNotLaunchedExpiredAlarms.begin();
       
   196     for (; iter != mNotLaunchedExpiredAlarms.end(); ++iter)
       
   197     {
       
   198         int mediaId = mUtilsInstance.getMediaIdByMidletUid(iter->first);
       
   199         if (mediaId == aMediaId)
       
   200         {
       
   201             handledUidsList.push_back(iter->first);
       
   202             TimerServerInterface* timerInterface = mCore.getTimerServer();
       
   203             JavaTime currentTime;
       
   204             timerInterface->getCurrentJavaTime(currentTime);
       
   205             JavaTime alarmTime(iter->second);
       
   206             bool flag = PushTimerUtils::isTimeOlderThanTwoWeeks(*timerInterface,
       
   207                         currentTime,alarmTime);
       
   208             if (true == flag)
       
   209             {
       
   210                 deleteAlarm(iter->first);
       
   211             }
       
   212             else
       
   213             {
       
   214                 //Now we know that alarm is less than current time but not
       
   215                 //older than two weeks so MIDlet can be launched.
       
   216                 timerExpired(iter->first,iter->second);
       
   217             }
       
   218         }//end if(mediaId == aMediaId)
       
   219     }//end for
       
   220 
       
   221     //Removing handled Uids from mNotLaunchedExpiredAlarms container.
       
   222     list<Uid>::iterator iter2 = handledUidsList.begin();
       
   223     for (; iter2 != handledUidsList.end(); ++iter2)
       
   224     {
       
   225         uidAlarmTimeContainerIter_t iter = mNotLaunchedExpiredAlarms.find((*iter2));
       
   226         if (iter != mNotLaunchedExpiredAlarms.end())
       
   227             mNotLaunchedExpiredAlarms.erase(iter);
       
   228     }
       
   229     handledUidsList.clear();
       
   230 }
       
   231 
       
   232 /**
       
   233  *
       
   234  */
       
   235 void PushAlarmHandler::timerExpired(const Uid& aUid,const long long& aAlarmTime)
       
   236 {
       
   237     timerExpired(aUid,aAlarmTime,true);
       
   238 }
       
   239 
       
   240 /**
       
   241  *
       
   242  */
       
   243 void PushAlarmHandler::timerExpired(const Uid& aUid,const long long& aAlarmTime,
       
   244                                     bool aDeleteAlarmFromDb)
       
   245 {
       
   246     JELOG2(EJavaPush);
       
   247 
       
   248     LOG1WSTR(EJavaPush,EInfo,"timerExpired(), Uid: %s",aUid.toString());
       
   249     bool driveAvailable =
       
   250         mUtilsInstance.launchMidlet(aUid,java::captain::RTC_LAUNCH_TYPE_AUTO_INVOCATION_C);
       
   251     if ((true == driveAvailable) && (true == aDeleteAlarmFromDb))
       
   252     {
       
   253         deleteAlarm(aUid);
       
   254     }
       
   255     else
       
   256     {
       
   257         mNotLaunchedExpiredAlarms.insert(std::pair<Uid,long long>(aUid,aAlarmTime));
       
   258     }
       
   259 }
       
   260 
       
   261 /**
       
   262  *
       
   263  */
       
   264 void PushAlarmHandler::deleteAlarm(const java::util::Uid& aUid)
       
   265 {
       
   266     JELOG2(EJavaPush);
       
   267 
       
   268     //Removing alarm from the db and internal "drive was not available when alarm expired"
       
   269     //list if uid exists in this list.
       
   270     try
       
   271     {
       
   272         mPushDbHandler.deleteAlarm(aUid);
       
   273     }
       
   274     catch (...)
       
   275     {
       
   276         //Exceptions are ignored in this case.
       
   277         ELOG(EJavaPush, "ERROR!!! Unexpected error occurred in the deletion of the alarm from the db");
       
   278     }
       
   279 }
       
   280 
       
   281