locationtriggering/ltstrategyengine/src/lbtstrategytimer.cpp
changeset 0 667063e416a2
equal deleted inserted replaced
-1:000000000000 0:667063e416a2
       
     1 /*
       
     2 * Copyright (c) 2006, 2007 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:  Implementation of Timer class 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "lbtstrategytimer.h"
       
    21 #include "lbtlogger.h"
       
    22 
       
    23 
       
    24 // ======== MEMBER FUNCTIONS ========
       
    25 
       
    26 // -----------------------------------------------------------------------------
       
    27 // CLbtStrategyTimer::NewL
       
    28 // 
       
    29 // -----------------------------------------------------------------------------
       
    30 //
       
    31 CLbtStrategyTimer* CLbtStrategyTimer::NewL()
       
    32     {
       
    33     CLbtStrategyTimer* timer = new ( ELeave ) CLbtStrategyTimer;
       
    34     CleanupStack::PushL( timer );
       
    35     timer->ConstructL();
       
    36     CleanupStack::Pop();
       
    37     return timer;
       
    38     }
       
    39 
       
    40     
       
    41 // -----------------------------------------------------------------------------
       
    42 // CLbtStrategyTimer::~CLbtStrategyTimer
       
    43 // 
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 CLbtStrategyTimer::~CLbtStrategyTimer()
       
    47     {
       
    48     FUNC_ENTER("CLbtStrategyTimer::~CLbtStrategyTimer");
       
    49     iTimer.Close();
       
    50     if( iUtilizeMovementDetection )
       
    51     	{
       
    52     	if( iMovtDetector )
       
    53 			{
       
    54 			iMovtDetector->StopNotification();
       
    55 			}
       
    56     	}    	
       
    57     delete iMovtDetector;
       
    58     }
       
    59 
       
    60     
       
    61 // -----------------------------------------------------------------------------
       
    62 // CLbtStrategyTimer::Set
       
    63 // 
       
    64 // -----------------------------------------------------------------------------
       
    65 //    
       
    66 void CLbtStrategyTimer::Set( TRequestStatus& aStatus, 
       
    67                              TTimeIntervalSeconds aSleepInterval,
       
    68                              TBool aUtilizeMovementDetection )
       
    69     {
       
    70     FUNC_ENTER("CLbtStrategyTimer::Set");
       
    71     LOG1("Sleeping for interval : %d", aSleepInterval.Int() );
       
    72     // Initialize sleeptime with respect to current time. 
       
    73     iSleepTime = aSleepInterval;
       
    74     
       
    75     iUtilizeMovementDetection = aUtilizeMovementDetection;
       
    76     
       
    77     // Update start time of sleep
       
    78     iStartTime.UniversalTime();
       
    79     iInterrupt = EFalse;
       
    80     
       
    81     aStatus = KRequestPending;    
       
    82     
       
    83     iMovtDetectorNotified = EFalse;
       
    84     //Initiate Movt Detector
       
    85     if( iMovtDetector && iUtilizeMovementDetection )
       
    86         {
       
    87         TRAPD( err, iMovtDetector->RequestNotificationL( this , iSleepTime.Int() ) );
       
    88         if( KErrNone == err )
       
    89             {
       
    90             iStatusPtr = &aStatus;
       
    91             return;
       
    92             }
       
    93         else
       
    94             {
       
    95             delete iMovtDetector;
       
    96             iMovtDetector = NULL;
       
    97             }
       
    98         }
       
    99    	iTimerInitialized = ETrue;
       
   100     iTimer.AtUTC( aStatus, ( iStartTime + iSleepTime ) );
       
   101     }
       
   102 
       
   103     
       
   104 // -----------------------------------------------------------------------------
       
   105 // CLbtStrategyTimer::ResetInterval
       
   106 // 
       
   107 // -----------------------------------------------------------------------------
       
   108 //  
       
   109 void CLbtStrategyTimer::ResetInterval( TRequestStatus& aStatus,
       
   110                                        TTimeIntervalSeconds aSleepInterval,
       
   111                                        TBool aUtilizeMovementDetection )
       
   112 	{
       
   113 	FUNC_ENTER("CLbtStrategyTimer::ResetInterval");
       
   114 	LOG1("ResetInterval;=%d", aSleepInterval.Int());
       
   115 	if( !iMovtDetectorNotified )
       
   116 		{
       
   117 		Set( aStatus, aSleepInterval,aUtilizeMovementDetection );
       
   118 		return;
       
   119 		}
       
   120 	iUtilizeMovementDetection = aUtilizeMovementDetection;
       
   121 		
       
   122 	TTime sleepTime;
       
   123     sleepTime.UniversalTime();
       
   124     sleepTime += aSleepInterval;
       
   125     
       
   126     iTimer.AtUTC( aStatus, sleepTime );
       
   127 	iSleepTime = aSleepInterval;
       
   128 	iStartTime.UniversalTime();
       
   129     iInterrupt = EFalse;    
       
   130 	}
       
   131 
       
   132     
       
   133 // -----------------------------------------------------------------------------
       
   134 // CLbtStrategyTimer::Cancel
       
   135 // 
       
   136 // -----------------------------------------------------------------------------
       
   137 //
       
   138 void CLbtStrategyTimer::Cancel()
       
   139     {
       
   140     FUNC_ENTER("CLbtStrategyTimer::Cancel");
       
   141     if( iTimerInitialized )
       
   142         {
       
   143         iTimer.Cancel();
       
   144         }
       
   145     if( iMovtDetector && iUtilizeMovementDetection )
       
   146         {
       
   147         iMovtDetector->StopNotification();
       
   148         }
       
   149     if(iStatusPtr != NULL)
       
   150     	{
       
   151     	User::RequestComplete(iStatusPtr, KErrCancel);
       
   152     	}
       
   153     //  iMovtDetectorNotified = EFalse;
       
   154     iInterrupt = ETrue;
       
   155     }
       
   156     
       
   157   
       
   158 // -----------------------------------------------------------------------------
       
   159 // CLbtStrategyTimer::RemainingSleepInterval
       
   160 // 
       
   161 // -----------------------------------------------------------------------------
       
   162 //    
       
   163 TBool CLbtStrategyTimer::RemainingSleepInterval( 
       
   164                                     TTimeIntervalSeconds& aRemainingInterval )
       
   165     {
       
   166     return RemainingSleepInterval( iSleepTime, aRemainingInterval );
       
   167     }
       
   168 
       
   169 
       
   170 // -----------------------------------------------------------------------------
       
   171 // CLbtStrategyTimer::RemainingSleepInterval
       
   172 // 
       
   173 // -----------------------------------------------------------------------------
       
   174 //    
       
   175 TBool CLbtStrategyTimer::RemainingSleepInterval( 
       
   176                                 TTimeIntervalSeconds aEstimatedTime, 
       
   177                                 TTimeIntervalSeconds& aRemainingInterval )
       
   178     {
       
   179     TTime currentTime;
       
   180 	TTimeIntervalSeconds interval;
       
   181 	currentTime.UniversalTime();
       
   182 	currentTime.SecondsFrom( iStartTime, interval );
       
   183     
       
   184     aRemainingInterval = aEstimatedTime.Int() - interval.Int(); 
       
   185              
       
   186 	if( aRemainingInterval.Int() <= 0 )
       
   187         {
       
   188         return EFalse;
       
   189         }
       
   190 	else
       
   191         {
       
   192         return ETrue;
       
   193         }
       
   194     }
       
   195  
       
   196  
       
   197 // -----------------------------------------------------------------------------
       
   198 // CLbtStrategyTimer::SleepIntervalInterrupted
       
   199 // 
       
   200 // -----------------------------------------------------------------------------
       
   201 //    
       
   202 TBool CLbtStrategyTimer::SleepIntervalInterrupted()
       
   203     {
       
   204     TBool interrupted = iInterrupt;
       
   205     iInterrupt = EFalse;
       
   206     return interrupted;
       
   207     }
       
   208 
       
   209 
       
   210 // -----------------------------------------------------------------------------
       
   211 // CLbtStrategyTimer::ElapsedTime
       
   212 // 
       
   213 // -----------------------------------------------------------------------------
       
   214 //    
       
   215 CLbtStrategyTimer::CLbtStrategyTimer() : iTimerInitialized( EFalse ), 
       
   216 										 iMovtDetectorNotified( EFalse ),
       
   217 										 iMovtDetector( NULL )
       
   218     {
       
   219     }
       
   220 
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // CLbtStrategyTimer::ElapsedTime
       
   224 // 
       
   225 // -----------------------------------------------------------------------------
       
   226 //    
       
   227 void CLbtStrategyTimer::ConstructL()
       
   228     {
       
   229     iInterrupt = EFalse;
       
   230     User::LeaveIfError( iTimer.CreateLocal() );
       
   231     iMovtDetector = CLbtMovementDetector::NewL();
       
   232     iUtilizeMovementDetection = ETrue;
       
   233     iStatusPtr = NULL;
       
   234     }
       
   235 
       
   236 
       
   237 // -----------------------------------------------------------------------------
       
   238 // CLbtStrategyEngine::HandleDetectorNotification
       
   239 // Handles Movt detection notification
       
   240 // -----------------------------------------------------------------------------
       
   241 //
       
   242 void CLbtStrategyTimer::HandleDetectorNotification( const TMovementObserverEvent /*aEvent*/ )
       
   243 	{
       
   244 	FUNC_ENTER("CLbtStrategyTimer::HandleDetectorNotification");
       
   245 	LOG("Movement detected");
       
   246 	 // aEvent need not be checked as the handling will remain the same  
       
   247 	iMovtDetector->StopNotification();
       
   248 	iMovtDetectorNotified = ETrue;
       
   249 	
       
   250 	TTime currentTime;
       
   251   	currentTime.UniversalTime();
       
   252     
       
   253   	if( currentTime > ( iStartTime + iSleepTime ) )
       
   254    		{
       
   255    		//Notification  after the completion of sleepinterval => Notify the engine immediately
       
   256    		User::RequestComplete( iStatusPtr, KErrNone ); 
       
   257    		iStatusPtr = NULL;
       
   258    		}
       
   259   	else
       
   260    		{
       
   261    		//Notification before the completion of sleepinterval => Notify the engine after the completion 
       
   262  		//of sleep interval
       
   263 		iTimerInitialized = ETrue;
       
   264 		iTimer.AtUTC( *iStatusPtr, ( iStartTime + iSleepTime ) );
       
   265 		iStatusPtr = NULL;
       
   266    		}
       
   267    	}