imagehandlingutilities/thumbnailmanager/tmcommon/src/tmactivitymanager.cpp
changeset 54 48dd0f169f0d
parent 42 2e2a89493e2b
equal deleted inserted replaced
42:2e2a89493e2b 54:48dd0f169f0d
     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:  Activity Manager 
       
    15 *
       
    16 */
       
    17 
       
    18 #include "tmactivitymanager.h"
       
    19 #include "thumbnaillog.h"
       
    20 #include "OstTraceDefinitions.h"
       
    21 #ifdef OST_TRACE_COMPILER_IN_USE
       
    22 #include "tmactivitymanagerTraces.h"
       
    23 #endif
       
    24 
       
    25 // ---------------------------------------------------------------------------
       
    26 // CTMActivityManager::NewL()
       
    27 // ---------------------------------------------------------------------------
       
    28 //
       
    29 CTMActivityManager* CTMActivityManager::NewL(MTMActivityManagerObserver* aObserver, TInt aTimeout)
       
    30     {
       
    31     TN_DEBUG1( "CTMActivityManager::NewL()");
       
    32     OstTrace0( TRACE_NORMAL, CTMACTIVITYMANAGER_NEWL, "CTMActivityManager::NewL" );
       
    33     
       
    34     CTMActivityManager* self = new (ELeave) CTMActivityManager(aObserver, aTimeout);
       
    35     CleanupStack::PushL(self);
       
    36     self->ConstructL();
       
    37     CleanupStack::Pop(self);
       
    38     return self;
       
    39     }
       
    40  
       
    41 // ---------------------------------------------------------------------------
       
    42 // CTMActivityManager::CTMActivityManager()
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 CTMActivityManager::CTMActivityManager(MTMActivityManagerObserver* aObserver, TInt aTimeout)
       
    46 : CActive(CActive::EPriorityHigh), iObserver(aObserver), iTimeout(aTimeout), iPreviousStatus(KErrNotFound)
       
    47     {
       
    48     TN_DEBUG1( "CTMActivityManager::CTMActivityManager()");
       
    49     OstTrace0( TRACE_NORMAL, CTMACTIVITYMANAGER_CTMACTIVITYMANAGER, "CTMActivityManager::CTMActivityManager" );
       
    50     
       
    51     CActiveScheduler::Add(this);
       
    52     }
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // CTMActivityManager::~CTMActivityManager()
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 CTMActivityManager::~CTMActivityManager()
       
    59     {
       
    60     TN_DEBUG1( "CTMActivityManager::~CTMActivityManager()");
       
    61     OstTrace0( TRACE_NORMAL, DUP1_CTMACTIVITYMANAGER_CTMACTIVITYMANAGER, "CTMActivityManager::~CTMActivityManager" );
       
    62     
       
    63     iObserver = NULL;
       
    64     Cancel();
       
    65     
       
    66 #ifdef MONITOR_LIGHTS
       
    67     delete iLight;
       
    68     iLight = NULL;
       
    69 #endif
       
    70 
       
    71     iTimer.Cancel();
       
    72     iTimer.Close();
       
    73     }
       
    74  
       
    75 // ---------------------------------------------------------------------------
       
    76 // CTMActivityManager::ConstructL()
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 void CTMActivityManager::ConstructL()
       
    80     {
       
    81     TN_DEBUG1( "CTMActivityManager::ConstructL()");
       
    82     OstTrace0( TRACE_NORMAL, CTMACTIVITYMANAGER_CONSTRUCTL, "CTMActivityManager::ConstructL" );
       
    83     
       
    84     iTimer.CreateLocal();
       
    85     iWatch = ENone;
       
    86     }
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // CTMActivityManager::SetTimeout()
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 void CTMActivityManager::SetTimeout(TInt aTimeout)
       
    93     {
       
    94     TN_DEBUG1( "CTMActivityManager::SetTimeout()");
       
    95     OstTrace0( TRACE_NORMAL, CTMACTIVITYMANAGER_SETTIMEOUT, "CTMActivityManager::SetTimeout" );
       
    96     
       
    97     iTimeout = aTimeout;
       
    98     Reset();
       
    99     }
       
   100 
       
   101 // ---------------------------------------------------------------------------
       
   102 // CTMActivityManager::Reset()
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 void CTMActivityManager::Reset()
       
   106     {
       
   107     TN_DEBUG1( "CTMActivityManager::Reset()");
       
   108     OstTrace0( TRACE_NORMAL, CTMACTIVITYMANAGER_RESET, "CTMActivityManager::Reset" );
       
   109 
       
   110 #ifdef MONITOR_LIGHTS
       
   111     delete iLight;
       
   112     iLight = NULL;
       
   113 #endif
       
   114     
       
   115     Cancel();
       
   116     Start();
       
   117     }
       
   118  
       
   119 // ---------------------------------------------------------------------------
       
   120 // CTMActivityManager::DoCancel()
       
   121 // ---------------------------------------------------------------------------
       
   122 void CTMActivityManager::DoCancel()
       
   123     {
       
   124     TN_DEBUG1( "CTMActivityManager::DoCancel()");
       
   125     OstTrace0( TRACE_NORMAL, CTMACTIVITYMANAGER_DOCANCEL, "CTMActivityManager::DoCancel" );
       
   126     
       
   127 #ifdef MONITOR_LIGHTS
       
   128     delete iLight;
       
   129     iLight = NULL;
       
   130 #endif
       
   131     
       
   132     iTimer.Cancel();
       
   133     iWatch = ENone;
       
   134     }
       
   135 
       
   136 // ---------------------------------------------------------------------------
       
   137 // CTMActivityManager::Start()
       
   138 // ---------------------------------------------------------------------------
       
   139 //
       
   140 void CTMActivityManager::Start()
       
   141     {
       
   142     TN_DEBUG1( "CTMActivityManager::Start()");
       
   143     OstTrace0( TRACE_NORMAL, CTMACTIVITYMANAGER_START, "CTMActivityManager::Start" );
       
   144     
       
   145 #ifdef MONITOR_LIGHTS
       
   146     if(!iLight)
       
   147         {
       
   148         TRAP_IGNORE(iLight = CHWRMLight::NewL(this));
       
   149         }
       
   150 #endif
       
   151     
       
   152     if ( iWatch == ENone )
       
   153         {
       
   154         iFirstRound = ETrue;
       
   155         
       
   156         if( !IsActive() )
       
   157             {
       
   158             SetActive();
       
   159             TRequestStatus* statusPtr = &iStatus;
       
   160             User::RequestComplete( statusPtr, KErrNone );
       
   161             }
       
   162         }
       
   163     }
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // CTMActivityManager::RunL()
       
   167 // ---------------------------------------------------------------------------
       
   168 //
       
   169 void CTMActivityManager::RunL()
       
   170     {
       
   171     TN_DEBUG4( "CTMActivityManager::RunL(0x%08x) %d, observer = 0x%08x", this, iStatus.Int(), iObserver);
       
   172     OstTrace1( TRACE_NORMAL, CTMACTIVITYMANAGER_RUNL, "CTMActivityManager::RunL;this=%o", this );
       
   173     OstTrace1( TRACE_NORMAL, DUP5_CTMACTIVITYMANAGER_RUNL, "CTMActivityManager::RunL;iStatus.Int()=%d", iStatus.Int() );
       
   174     
       
   175     if(iFirstRound)
       
   176         {
       
   177         TN_DEBUG1( "CTMActivityManager::RunL() iFirstRound");
       
   178         OstTrace0( TRACE_NORMAL, DUP1_CTMACTIVITYMANAGER_RUNL, "CTMActivityManager::RunL - iFirstRound" );
       
   179         iFirstRound = EFalse;
       
   180         if (!IsActive())
       
   181              {
       
   182              iWatch = EWaitingForInactivity;
       
   183              iTimer.Inactivity(iStatus, iTimeout);
       
   184              SetActive();
       
   185              }
       
   186         NotifyObserver();
       
   187         return;
       
   188         }
       
   189     
       
   190     if (iStatus == KErrNone)
       
   191         {
       
   192         if (iWatch == EWaitingForInactivity)
       
   193             {
       
   194             TInt inactivity = User::InactivityTime().Int();
       
   195             if (inactivity >= iTimeout)
       
   196                 {
       
   197                 TN_DEBUG1( "CTMActivityManager::RunL() inactive");
       
   198                 OstTrace0( TRACE_NORMAL, DUP2_CTMACTIVITYMANAGER_RUNL, "CTMActivityManager::RunL - inactive" );
       
   199                 NotifyObserver();
       
   200 
       
   201                 if (!IsActive()) //observer might have called a Reset()
       
   202                     {
       
   203                     iTimer.Inactivity(iStatus,0);
       
   204                     iWatch = EWaitingForActivity;
       
   205                     }
       
   206                 }
       
   207             else
       
   208                 {
       
   209                 iTimer.Inactivity(iStatus,iTimeout);
       
   210                 }
       
   211             }
       
   212         else if (iWatch == EWaitingForActivity)
       
   213             {
       
   214             TN_DEBUG1( "CTMActivityManager::RunL() active");
       
   215             OstTrace0( TRACE_NORMAL, DUP3_CTMACTIVITYMANAGER_RUNL, "CTMActivityManager::RunL - active" );
       
   216             NotifyObserver();
       
   217              
       
   218             if (!IsActive()) //observer might have called a Reset()
       
   219                 {
       
   220                 iTimer.Inactivity(iStatus,iTimeout);
       
   221                 iWatch = EWaitingForInactivity;
       
   222                 }
       
   223             }
       
   224     
       
   225         if (!IsActive()) //observer might have called a Reset()
       
   226             {
       
   227             SetActive();
       
   228             }
       
   229         }
       
   230     else
       
   231         {
       
   232         iWatch = ENone;
       
   233         }
       
   234         TN_DEBUG1( "CTMActivityManager::RunL() end");
       
   235         OstTrace0( TRACE_NORMAL, DUP4_CTMACTIVITYMANAGER_RUNL, "CTMActivityManager::RunL - end" );
       
   236     }
       
   237 
       
   238 // ---------------------------------------------------------------------------
       
   239 // CTMActivityManager::RunError()
       
   240 // ---------------------------------------------------------------------------
       
   241 //
       
   242 TInt CTMActivityManager::RunError(TInt aError)
       
   243     {
       
   244     TN_DEBUG2( "CTMActivityManager::RunError() %d", aError);
       
   245     OstTrace1( TRACE_NORMAL, CTMACTIVITYMANAGER_RUNERROR, "CTMActivityManager::RunError;aError=%d", aError );
       
   246     
       
   247     if (aError != KErrNone)
       
   248         {
       
   249         Reset();
       
   250         }
       
   251     
       
   252     // nothing to do
       
   253     return KErrNone;
       
   254     }
       
   255 
       
   256 // -----------------------------------------------------------------------------
       
   257 // IsInactive()
       
   258 // -----------------------------------------------------------------------------
       
   259 //
       
   260 TBool CTMActivityManager::IsInactive()
       
   261     {
       
   262 #ifdef MONITOR_LIGHTS
       
   263 #ifdef _DEBUG
       
   264 TInt inactivityTime = User::InactivityTime().Int();
       
   265 TN_DEBUG3( "CTMActivityManager::IsInactive()= %d, iLights = %d", User::InactivityTime().Int(), iLights);
       
   266 OstTrace1( TRACE_NORMAL, DUP6_CTMACTIVITYMANAGER_RUNL, "CTMActivityManager::RunL;inactivityTime=%d", inactivityTime );
       
   267 #endif
       
   268 #else
       
   269 TInt inactivityTime = User::InactivityTime().Int();
       
   270 TN_DEBUG2( "CTMActivityManager::IsInactive()= %d", User::InactivityTime().Int());
       
   271 OstTrace1( TRACE_NORMAL, DUP7_CTMACTIVITYMANAGER_RUNL, "CTMActivityManager::RunL;inactivityTime=%d", inactivityTime );
       
   272 #endif
       
   273 
       
   274     //if lights are off or inactivity timer is less that target the device is not idle
       
   275     if( User::InactivityTime() >= TTimeIntervalSeconds(iTimeout)
       
   276 #ifdef MONITOR_LIGHTS
       
   277             || !iLights
       
   278 #endif
       
   279             )
       
   280       {
       
   281       TN_DEBUG1( "CTMActivityManager::IsInactive() ETrue");
       
   282       OstTrace0( TRACE_NORMAL, CTMACTIVITYMANAGER_ISINACTIVE, "CTMActivityManager::IsInactive - ETrue" );
       
   283       return ETrue;
       
   284       }
       
   285     TN_DEBUG1( "CTMActivityManager::IsInactive() EFalse");
       
   286     OstTrace0( TRACE_NORMAL, DUP1_CTMACTIVITYMANAGER_ISINACTIVE, "CTMActivityManager::IsInactive - EFalse" );
       
   287     return EFalse;
       
   288     }
       
   289 
       
   290 #ifdef MONITOR_LIGHTS
       
   291 // -----------------------------------------------------------------------------
       
   292 // LightStatusChanged()
       
   293 // -----------------------------------------------------------------------------
       
   294 //
       
   295 void CTMActivityManager::LightStatusChanged(TInt aTarget, CHWRMLight::TLightStatus aStatus)
       
   296     {
       
   297     TN_DEBUG3( "CTMActivityManager::LightStatusChanged() aTarget = %d, aStatus == %d", aTarget, aStatus);
       
   298     OstTraceExt2( TRACE_NORMAL, CTMACTIVITYMANAGER_LIGHTSTATUSCHANGED, "CTMActivityManager::LightStatusChanged;aTarget=%d;aStatus=%u", aTarget, aStatus );    
       
   299     
       
   300     if(aTarget & CHWRMLight::EPrimaryDisplay)
       
   301         {
       
   302          if( aStatus == CHWRMLight::ELightOff )
       
   303             {
       
   304             TN_DEBUG1( "CTMActivityManager::LightStatusChanged() -- OFF");
       
   305             OstTrace0( TRACE_NORMAL, DUP1_CTMACTIVITYMANAGER_LIGHTSTATUSCHANGED, "CTMActivityManager::LightStatusChanged - OFF" );
       
   306             iLights = EFalse;
       
   307             }
       
   308         else
       
   309             {
       
   310             TN_DEBUG1( "CTMActivityManager::LightStatusChanged() -- ON");
       
   311             OstTrace0( TRACE_NORMAL, DUP2_CTMACTIVITYMANAGER_LIGHTSTATUSCHANGED, "CTMActivityManager::LightStatusChanged - ON" );
       
   312             iLights = ETrue;
       
   313             }
       
   314      
       
   315         NotifyObserver();
       
   316         }
       
   317     }
       
   318 #endif
       
   319 
       
   320 // -----------------------------------------------------------------------------
       
   321 // NotifyObserver()
       
   322 // -----------------------------------------------------------------------------
       
   323 //
       
   324 void CTMActivityManager::NotifyObserver()
       
   325     {
       
   326     TN_DEBUG1( "void CTMAActivityManager::NotifyObserver()");
       
   327     OstTrace0( TRACE_NORMAL, CTMACTIVITYMANAGER_NOTIFYOBSERVER, "CTMActivityManager::NotifyObserver" );
       
   328     
       
   329     TBool status = IsInactive();
       
   330     
       
   331     if( iPreviousStatus != status )
       
   332         {
       
   333         iPreviousStatus = status; 
       
   334         if (iObserver)
       
   335             {
       
   336             iObserver->ActivityChanged(!status);
       
   337             }
       
   338         }
       
   339     }