commonservices/activitymanager/src/activitymanager.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // CUserActivityManager class
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "activitymanager.h"
       
    19 #include "activitymanagerdebug.h"
       
    20 
       
    21 /**
       
    22 Factory function used to allocate and construct a new CUserActivityManager
       
    23 object. 
       
    24  
       
    25 @param  aPriority    Priority for the CTimer active object.
       
    26 @leave  KErrNoMemory When memory allocations fail
       
    27 @leave  ...          Any other one of the other system wide error codes
       
    28 @return CUserActivityManager*   Pointer to a new User Activity Manager object
       
    29 */
       
    30 EXPORT_C CUserActivityManager* CUserActivityManager::NewL(TInt aPriority)
       
    31 	{
       
    32 	CUserActivityManager* self = new (ELeave) CUserActivityManager(aPriority);
       
    33 	CleanupStack::PushL(self);
       
    34 	self->ConstructL();
       
    35 	CActiveScheduler::Add(self);
       
    36 	CleanupStack::Pop(self);
       
    37 	return self;
       
    38 	}
       
    39 	
       
    40 /**
       
    41 Default constructor.
       
    42 
       
    43 @param  aPriority Priority for the CTimer.
       
    44 @internalComponent
       
    45 */
       
    46 CUserActivityManager::CUserActivityManager(TInt aPriority)
       
    47 	: CTimer(aPriority), iInactiveCallback(0,0), iActiveCallback(0,0), iState(EStNotActive)
       
    48 	{
       
    49 	}
       
    50 
       
    51 /**
       
    52 Destructor. 
       
    53 */
       
    54 EXPORT_C CUserActivityManager::~CUserActivityManager()
       
    55 	{
       
    56 	}
       
    57 
       
    58 /**
       
    59 Starts the user activity monitoring using the callbacks supplied with the 
       
    60 specify inactivity interval in seconds. If aInterval is not positive then 
       
    61 in release builds it assumes an interval of 1 second but in debug builds it 
       
    62 will panic. The function pointers in the callback arguments may be NULL if the
       
    63 client is not interested in one or other events.
       
    64 
       
    65 When called following construction or while inactivity is being monitored the 
       
    66 manager will enter/stay in inactivity monitoring mode when aInterval is larger
       
    67 than the current elapsed inactivity time at the time of the call and 
       
    68 aInactiveCallback will be called first. Otherwise it will invoke 
       
    69 aInactiveCallback immediately and enter activity monitoring mode and 
       
    70 aActiveCallback will be called next.
       
    71 
       
    72 When called while user activity is being monitored the manager will stay in 
       
    73 activity monitoring mode and aActiveCallback will be called first.
       
    74 
       
    75 @param aInterval         Inactivity interval in seconds.
       
    76 @param aInactiveCallback This callback is used when there has not been user 
       
    77             activity for aInterval seconds.
       
    78 @param aActiveCallback   This callback is used when user activity is detected 
       
    79             after an inactivity period longer or equal to aInterval seconds.
       
    80 @panic "CUserActivityManager" KErrNotSupported raised when when aInterval is 
       
    81             not positive in DEBUG builds
       
    82 @see   CTimer::Inactivity()
       
    83 @see   RTimer::Inactivity()
       
    84 */
       
    85 EXPORT_C void CUserActivityManager::Start(TTimeIntervalSeconds aInterval,
       
    86 		TCallBack aInactiveCallback, TCallBack aActiveCallback) 
       
    87 	{  	
       
    88 	iInterval = aInterval;
       
    89 	iInactiveCallback = aInactiveCallback;
       
    90 	iActiveCallback = aActiveCallback;
       
    91 			
       
    92 	if	(iInterval.Int() <= 0)
       
    93 		{
       
    94 		// Panic if interval is not greater than zero. 
       
    95 		__ACTIVITYMANAGER_TRACE1("Activity Mgr: Negative or zero interval supplied to Start(): %d", iInterval.Int());			
       
    96 		__ASSERT_DEBUG(EFalse, UAM_PANIC(KErrNotSupported));
       
    97 		// In release mode interval will be set to one second
       
    98 		iInterval = TTimeIntervalSeconds(1);
       
    99 		}
       
   100 		
       
   101 	// Cancel outstanding timer, if any
       
   102 	CTimer::Cancel();
       
   103 	
       
   104 	if (iState == EStNotActive || iState == EStMonForInactivity) 
       
   105 		{
       
   106 		// Begin or contine monitoring for user inactivity
       
   107 		MonitorInactivity();
       
   108 		}
       
   109 	else 
       
   110 		{
       
   111 		// Continue monitoring for user activity
       
   112 		__ACTIVITYMANAGER_TRACE1("Activity Mgr: Starting activity monitoring. Inactive for %d", User::InactivityTime().Int());
       
   113 		Inactivity(0); 
       
   114 		}
       
   115 	}
       
   116 
       
   117 /**
       
   118 Re-starts activity monitoring with a new inactivity interval. See Start() for 
       
   119 behavioural description. 
       
   120 
       
   121 Requires Start() to have been called previously otherwise the method will 
       
   122 panic in debug builds if Start() has not be called before hand to
       
   123 set up at least one callback. This prevents wasting CPU cycles/power on 
       
   124 monitoring when no action will take place.
       
   125 
       
   126 @see   CUserActivityManager::Start()
       
   127 @param aInterval New inactivity interval in seconds.
       
   128 @panic "CUserActivityManager" KErrNotSupported Raised when aInterval is 
       
   129             not positive in debug builds
       
   130 @panic "CUserActivityManager" KErrNotReady Raised when Start() has not yet 
       
   131             been called in debug builds.
       
   132 */	     
       
   133 EXPORT_C void CUserActivityManager::SetInactivityTimeout(TTimeIntervalSeconds aInterval)
       
   134 	{
       
   135 	if (iState <= EStNotActive)
       
   136     	{
       
   137     	__ACTIVITYMANAGER_TRACE("Activity Mgr: SetInactivityTimeout() called before Start()");
       
   138     	__ASSERT_DEBUG(EFalse, UAM_PANIC(KErrNotReady));
       
   139     	}
       
   140     	
       
   141 	__ACTIVITYMANAGER_TRACE1("Activity Mgr: SetInactivityTimeout: %d", aInterval.Int());
       
   142 	Start(aInterval, iInactiveCallback, iActiveCallback); 
       
   143 	}
       
   144 
       
   145 /**
       
   146 This method attempts to enter inactivity monitoring mode. For this to occur the
       
   147 current ellapsed inactivity must be less than the iInterval otherwise it will
       
   148 invoke the inactivity callback and enter activity monitoring mode.
       
   149 
       
   150 @see CTimer::Inactivity()
       
   151 @see RTimer::Inactivity()
       
   152 @internalComponent
       
   153 */
       
   154 void CUserActivityManager::MonitorInactivity()
       
   155 	{
       
   156 	if (User::InactivityTime() >= iInterval) 
       
   157 		{
       
   158 		// Already inactive enough, monitor for activity
       
   159 		iState = EStMonForActivity;
       
   160 		// Monitor for activity, set timer before callback to avoid missing any
       
   161 		// user activity should callback take some time.
       
   162 		Inactivity(0); 
       
   163 		// Inform client that the inactivity interval has ended.
       
   164 		(void) iInactiveCallback.CallBack();
       
   165 		__ACTIVITYMANAGER_TRACE1("Activity Mgr::MonitorInactivity - Starting activity monitoring. Inactive for %d", User::InactivityTime().Int());
       
   166 		}
       
   167 	else
       
   168 		{
       
   169 		// Monitor for Inactivity
       
   170 		iState = EStMonForInactivity;
       
   171 		Inactivity(iInterval);
       
   172 		__ACTIVITYMANAGER_TRACE1("Activity Mgr::MonitorInactivity - Starting inactivity monitoring. Inactive for %d", User::InactivityTime().Int());
       
   173 		}
       
   174 	}
       
   175 
       
   176 /**
       
   177 This is called from CActiveScheduler when the timer registered using the 
       
   178 CTimer::Inactivity() call expires. This could indicate user inactivity or
       
   179 activity, iStatus is checked to determine which condition has been detected
       
   180 by CTimer.
       
   181 
       
   182 If the manager is in inactivity monitoring mode the iInactiveCallback will be
       
   183 invoked and the manager will switch to monitoring for activity.
       
   184 If the manager is in activity monitoring mode the iActiveCallback will be 
       
   185 invoked and the manager will attempt to switch to monitoring for inactivity. 
       
   186 This attempt may fail and it will remain in activity monitoring mode if an
       
   187 iInterval amount of inactivity has passed since RunL() was invoked.
       
   188 
       
   189 When CActive::iStatus indicates and error a panic is raised in debug builds. In
       
   190 release builds it returns to the active scheduler to handle.
       
   191 
       
   192 @see CUserActivityManager::MonitorInactivity()
       
   193 @see CTimer::Inactivity()
       
   194 @see RTimer::Inactivity()
       
   195 @internalComponent
       
   196 */	
       
   197 void CUserActivityManager::RunL()
       
   198 	{
       
   199 	if (iStatus == KErrNone)
       
   200 		{
       
   201 		if (iState == EStMonForInactivity) 
       
   202 			{
       
   203 			// Looking for Inactivity, found it
       
   204 			__ACTIVITYMANAGER_TRACE("Activity Mgr: User inactivity detected");
       
   205 	   	    // Monitor for activity, set timer before callback to avoid missing 
       
   206 		    // any user activity should callback take some time.
       
   207        		iState = EStMonForActivity;
       
   208 			Inactivity(0); 
       
   209 			// Inform client that the inactivity interval has ended.
       
   210  			(void) iInactiveCallback.CallBack();
       
   211 			}
       
   212 		else if (iState == EStMonForActivity)
       
   213 			{
       
   214 			// Looking for activity, found it
       
   215 			__ACTIVITYMANAGER_TRACE("Activity Mgr: User activity detected");
       
   216 			(void) iActiveCallback.CallBack();
       
   217 			// Monitor for activity unless MonitorInactivity overrides it
       
   218     		iState = EStMonForInactivity; 
       
   219 			MonitorInactivity();
       
   220 			}	
       
   221 		else
       
   222             { // Must never happen, indicates internal state of object incorrect.
       
   223             __ASSERT_ALWAYS(EFalse, UAM_PANIC(KErrGeneral));
       
   224             }
       
   225 		}
       
   226 	else
       
   227 		{
       
   228 		// KErrCancel, KErrArgument, KErrOverflow,...
       
   229 		__ACTIVITYMANAGER_TRACE1("Activity Mgr: RunL error: %d",iStatus.Int());			
       
   230 		// Irrecoverable error, no way to notify client of failure (callbacks
       
   231 		// have no parameters to pass back error codes).
       
   232 		__ASSERT_ALWAYS(EFalse, UAM_PANIC(iStatus.Int()));
       
   233 		}
       
   234 	}