idlefw/src/framework/aistatemanager.cpp
branchRCL_3
changeset 9 d0529222e3f0
child 18 bd874ee5e5e2
equal deleted inserted replaced
4:1a2a00e78665 9:d0529222e3f0
       
     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:  State Manager
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 
       
    20 // User includes
       
    21 #include <hscontentpublisher.h>
       
    22 #include <aifwdefs.h>
       
    23 
       
    24 #include "aipluginfactory.h"
       
    25 
       
    26 #include "aistatemanager.h"
       
    27 
       
    28 #include "debug.h"
       
    29 
       
    30 // Constants
       
    31 _LIT( KOnlineOffline, "online_offline" );
       
    32 
       
    33 // ======== LOCAL FUNCTIONS ========
       
    34 // ----------------------------------------------------------------------------
       
    35 // StartReason
       
    36 // 
       
    37 // ----------------------------------------------------------------------------
       
    38 //
       
    39 static CHsContentPublisher::TStartReason StartReason( TAiFwLoadReason aReason )
       
    40     {
       
    41     CHsContentPublisher::TStartReason reason; 
       
    42              
       
    43     if ( aReason == EAiFwPageStartup )
       
    44         {
       
    45         reason = CHsContentPublisher::EPageStartup; 
       
    46         }
       
    47     else if ( aReason == EAiFwPluginStartup )
       
    48         {
       
    49         reason = CHsContentPublisher::EPluginStartup;
       
    50         }
       
    51     else
       
    52         {
       
    53         reason = CHsContentPublisher::ESystemStartup;
       
    54         }
       
    55     
       
    56     return reason;
       
    57     }
       
    58 
       
    59 // ----------------------------------------------------------------------------
       
    60 // StopReason
       
    61 // 
       
    62 // ----------------------------------------------------------------------------
       
    63 //
       
    64 static CHsContentPublisher::TStopReason StopReason( TAiFwDestroyReason aReason )
       
    65     {
       
    66     CHsContentPublisher::TStopReason reason; 
       
    67              
       
    68     if ( aReason == EAiFwPageShutdown )
       
    69         {
       
    70         reason = CHsContentPublisher::EPageShutdown; 
       
    71         }
       
    72     else if ( aReason == EAiFwPluginShutdown )
       
    73         {
       
    74         reason = CHsContentPublisher::EPluginShutdown;
       
    75         }
       
    76     else
       
    77         {
       
    78         reason = CHsContentPublisher::ESystemShutdown;
       
    79         }
       
    80     
       
    81     return reason;
       
    82     }
       
    83 
       
    84 // ======== MEMBER FUNCTIONS ========
       
    85 // ----------------------------------------------------------------------------
       
    86 // CAiStateManager::NewL()
       
    87 // Two-phased constructor.
       
    88 // ----------------------------------------------------------------------------
       
    89 //
       
    90 CAiStateManager* CAiStateManager::NewL( CAiPluginFactory& aFactory )
       
    91     {
       
    92     CAiStateManager* self = CAiStateManager::NewLC( aFactory );
       
    93     CleanupStack::Pop( self );
       
    94     return self;
       
    95     }
       
    96 
       
    97 // ----------------------------------------------------------------------------
       
    98 // CAiStateManager::NewLC()
       
    99 // Two-phased constructor.
       
   100 // ----------------------------------------------------------------------------
       
   101 //
       
   102 CAiStateManager* CAiStateManager::NewLC( CAiPluginFactory& aFactory )
       
   103     {
       
   104     CAiStateManager* self = new ( ELeave ) CAiStateManager( aFactory );
       
   105     CleanupStack::PushL( self );
       
   106     self->ConstructL();
       
   107     return self;
       
   108     }
       
   109 
       
   110 // ----------------------------------------------------------------------------
       
   111 // CAiStateManager::~CAiStateManager()
       
   112 // C++ default destructor.
       
   113 // ----------------------------------------------------------------------------
       
   114 //
       
   115 CAiStateManager::~CAiStateManager()
       
   116     {  
       
   117     }
       
   118 
       
   119 // ----------------------------------------------------------------------------
       
   120 // CAiStateManager::CAiStateManager()
       
   121 // C++ default constructor.
       
   122 // ----------------------------------------------------------------------------
       
   123 //
       
   124 CAiStateManager::CAiStateManager( CAiPluginFactory& aFactory )
       
   125     : iFactory( aFactory )
       
   126     {
       
   127     }
       
   128 
       
   129 // ----------------------------------------------------------------------------
       
   130 // CAiStateManager::ConstructL()
       
   131 // 2nd phase constructor
       
   132 // ----------------------------------------------------------------------------
       
   133 //
       
   134 void CAiStateManager::ConstructL()
       
   135     {        
       
   136     }
       
   137 
       
   138 // ----------------------------------------------------------------------------
       
   139 // CAiStateManager::NotifyStateChange()
       
   140 // 
       
   141 // ----------------------------------------------------------------------------
       
   142 //
       
   143 void CAiStateManager::NotifyStateChange( TAiFwState aState )    
       
   144     {       
       
   145     if ( aState == EAiFwUiShutdown )
       
   146         {     
       
   147         __PRINTS( "CAiStateManager::NotifyStateChange, aState: EAiFwUiShutdown" );
       
   148         
       
   149         iFlags.Set( EShutdown );
       
   150         
       
   151         DestroyPlugins();
       
   152         
       
   153         return;
       
   154         }    
       
   155     else if ( aState == EAiFwUiStartup )
       
   156         {
       
   157         __PRINTS( "CAiStateManager::NotifyStateChange, aState: EAiFwUiStartup" );
       
   158         
       
   159         iFlags.Clear( EShutdown );
       
   160         
       
   161         return;
       
   162         }
       
   163        
       
   164     TBitFlags32 flags( iFlags );
       
   165     
       
   166     if ( aState == EAiFwForeground )
       
   167         {
       
   168         __PRINTS( "CAiStateManager::NotifyStateChange, aState: EAiFwForeground" );
       
   169         
       
   170         iFlags.Set( EIsForeground );                                    
       
   171         }
       
   172     else if ( aState == EAiFwBackground )
       
   173         {
       
   174         __PRINTS( "CAiStateManager::NotifyStateChange, aState: EAiFwBackground" );
       
   175         
       
   176         iFlags.Clear( EIsForeground );
       
   177         }
       
   178     else if ( aState == EAiFwBacklightOn )
       
   179         {
       
   180         __PRINTS( "CAiStateManager::NotifyStateChange, aState: EAiFwBacklightOn" );
       
   181         
       
   182         iFlags.Set( EIsLightsOn );
       
   183         }
       
   184     else if ( aState == EAiFwBacklightOff )
       
   185         {
       
   186         __PRINTS( "CAiStateManager::NotifyStateChange, aState: EAiFwBacklightOff" );
       
   187         
       
   188         iFlags.Clear( EIsLightsOn );
       
   189         }
       
   190     else if ( aState == EAiFwOnline )
       
   191         {
       
   192         __PRINTS( "CAiStateManager::NotifyStateChange, aState: EAiFwOnline" );
       
   193         
       
   194         iFlags.Set( EIsOnline );
       
   195         
       
   196         ProcessOnlineStateChange();
       
   197         }
       
   198     else if ( aState == EAiFwOffline )
       
   199         {
       
   200         __PRINTS( "CAiStateManager::NotifyStateChange, aState: EAiFwOffline" );
       
   201         
       
   202         iFlags.Clear( EIsOnline );
       
   203         
       
   204         ProcessOnlineStateChange();
       
   205         }
       
   206     else if ( aState == EAiFwGeneralThemeChange )
       
   207         {
       
   208         __PRINTS( "CAiStateManager::NotifyStateChange, aState: EAiFwGeneralThemeChange" );
       
   209         
       
   210         ProcessGeneralThemeChange();
       
   211         }
       
   212     else if ( aState == EAiFwBackupRestoreStart || 
       
   213         aState == EAiFwBackupRestoreEnd )
       
   214         {
       
   215         __PRINTS( "CAiStateManager::NotifyStateChange, aState: EAiFwBackupRestoreStart/End" );
       
   216         
       
   217         ProcessBackupRestore( aState == EAiFwBackupRestoreStart );
       
   218         }
       
   219     else
       
   220         {
       
   221         __PRINTS( "CAiStateManager::NotifyStateChange, aState: Unknown" );
       
   222         }
       
   223 
       
   224     // State change evaluation and state change trial is done always here
       
   225     __PRINTS( "CAiStateManager::NotifyStateChange, Run state change" );    
       
   226     ProcessStateChange( EvaluateNextState() );            
       
   227     }
       
   228 
       
   229 // ----------------------------------------------------------------------------
       
   230 // CAiStateManager::NotifyLoadPlugin()
       
   231 // 
       
   232 // ----------------------------------------------------------------------------
       
   233 //
       
   234 TInt CAiStateManager::NotifyLoadPlugin( const THsPublisherInfo& aPublisherInfo,
       
   235     TAiFwLoadReason aReason )
       
   236     {                
       
   237     __PRINT( __DBG_FORMAT( 
       
   238         "CAiStateManager::NotifyLoadPlugin: name: %S, reason: %d " ), 
       
   239               &aPublisherInfo.Name(), (TInt) aReason );      
       
   240           
       
   241     __TIME_MARK( time );
       
   242       
       
   243     // Create plugin
       
   244     TInt retval( iFactory.CreatePlugin( aPublisherInfo ) );
       
   245     
       
   246     if ( retval == KErrNone )
       
   247         {
       
   248         CHsContentPublisher* plugin( iFactory.PluginByInfo( aPublisherInfo ) );
       
   249 
       
   250         // Do startup state transition    
       
   251         StartPlugin( *plugin, StartReason( aReason ) );            
       
   252         }
       
   253       
       
   254     __TIME_ENDMARK( "CAiStateManager::NotifyLoadPlugin, construction", time );
       
   255     
       
   256     return retval;
       
   257     }
       
   258 
       
   259 // ----------------------------------------------------------------------------
       
   260 // CAiStateManager::NotifyDestroyPlugin()
       
   261 // 
       
   262 // ----------------------------------------------------------------------------
       
   263 //
       
   264 void CAiStateManager::NotifyDestroyPlugin( 
       
   265     const THsPublisherInfo& aPublisherInfo, TAiFwDestroyReason aReason )    
       
   266     {
       
   267     __PRINT( __DBG_FORMAT( 
       
   268         "CAiStateManager::NotifyDestroyPlugin: name: %S, reason: %d " ), 
       
   269             &aPublisherInfo.Name(), (TInt)aReason );              
       
   270     
       
   271     __TIME_MARK( time );
       
   272     
       
   273     // Resolve plugin
       
   274     CHsContentPublisher* plugin( iFactory.PluginByInfo( aPublisherInfo ) );
       
   275             
       
   276     if ( plugin )
       
   277         {                  
       
   278         // Do shutdown state transition
       
   279         StopPlugin( *plugin, StopReason( aReason ) );
       
   280         
       
   281         // Destroy plugin
       
   282         iFactory.DestroyPlugin( aPublisherInfo );                                    
       
   283         }
       
   284            
       
   285     __TIME_ENDMARK( "CAiStateManager::DestroyPlugin, done", time );
       
   286     }
       
   287 
       
   288 // ----------------------------------------------------------------------------
       
   289 // CAiStateManager::NotifyUpdatePlugins()
       
   290 // 
       
   291 // ----------------------------------------------------------------------------
       
   292 //
       
   293 void CAiStateManager::NotifyUpdatePlugins()
       
   294     {
       
   295     __PRINTS( "CAiStateManager::NotifyUpdatePlugins" );
       
   296     
       
   297     RArray< THsPublisherInfo > publishers;
       
   298         
       
   299     // Get plugins which has upgrade available
       
   300     TRAP_IGNORE( iFactory.ResolvePluginsToUpgradeL( publishers ) );
       
   301     
       
   302     for ( TInt i = 0; i < publishers.Count(); i++ )
       
   303         {        
       
   304         THsPublisherInfo info( publishers[i] );
       
   305         
       
   306         // Update by destroy - load sequence 
       
   307         NotifyDestroyPlugin( info, EAiFwSystemShutdown );
       
   308         NotifyLoadPlugin( info, EAiFwSystemStartup );         
       
   309         }
       
   310     
       
   311     publishers.Reset();
       
   312     
       
   313     __PRINTS( "CAiStateManager::NotifyUpdatePlugins, done" );
       
   314     }
       
   315 
       
   316 // ----------------------------------------------------------------------------
       
   317 // CAiStateManager::OnlineStateInUse()
       
   318 // 
       
   319 // ----------------------------------------------------------------------------
       
   320 //
       
   321 TBool CAiStateManager::OnlineStateInUse() const
       
   322     {
       
   323     __PRINTS( "CAiStateManager::OnlineStateInUse" );        
       
   324     
       
   325     RPointerArray< CHsContentPublisher >& plugins( iFactory.Publishers() );
       
   326     
       
   327     for( TInt i = 0; i < plugins.Count(); i++ )
       
   328         {
       
   329         CHsContentPublisher* plugin( plugins[i] );
       
   330         
       
   331         if ( plugin->HasMenuItem( KOnlineOffline() ) )
       
   332             {
       
   333             return ETrue;
       
   334             }
       
   335         }
       
   336     
       
   337     return EFalse;
       
   338     }
       
   339 
       
   340 // ----------------------------------------------------------------------------
       
   341 // CAiStateManager::EvaluateNextState()
       
   342 // 
       
   343 // ----------------------------------------------------------------------------
       
   344 //
       
   345 CAiStateManager::TState CAiStateManager::EvaluateNextState() const
       
   346     {
       
   347     TState nextState( iCurrentState );
       
   348         
       
   349     if ( iFlags.IsSet( EIsForeground ) && iFlags.IsSet( EIsLightsOn ) )
       
   350         {
       
   351         nextState = EAlive;
       
   352         }
       
   353     else 
       
   354         {
       
   355         nextState = ESuspended;
       
   356         } 
       
   357 
       
   358     __PRINT( __DBG_FORMAT( 
       
   359         "CAiStateManager::EvaluateNextState: current state: %d, next state: %d" ), 
       
   360             (TInt)iCurrentState, (TInt)nextState );     
       
   361     
       
   362     return nextState;
       
   363     }
       
   364 
       
   365 // ----------------------------------------------------------------------------
       
   366 // CAiStateManager::ProcessStateChange()
       
   367 // 
       
   368 // ----------------------------------------------------------------------------
       
   369 //
       
   370 void CAiStateManager::ProcessStateChange( TState aNextState )
       
   371     {
       
   372     __PRINT( __DBG_FORMAT( 
       
   373         "CAiStateManager::ProcessStateChange: current state: %d, next state: %d, halt: &d" ), 
       
   374             (TInt)iCurrentState, (TInt)aNextState, iHalt );     
       
   375         
       
   376     __TIME_MARK( time );
       
   377     
       
   378     if ( aNextState != iCurrentState )
       
   379         {
       
   380         // Update state
       
   381         iCurrentState = aNextState;
       
   382         
       
   383         RPointerArray< CHsContentPublisher >& plugins( iFactory.Publishers() );
       
   384                        
       
   385         // Process state for all
       
   386         for( TInt i = 0; !iHalt && i < plugins.Count(); i++ )
       
   387             {
       
   388             CHsContentPublisher* plugin( plugins[i] );
       
   389             
       
   390             const THsPublisherInfo& info( plugin->PublisherInfo() );
       
   391                 
       
   392             __PRINT( __DBG_FORMAT( 
       
   393                 "CAiStateManager::ProcessStateChange: name: %S" ), &info.Name() ); 
       
   394             
       
   395             if ( iCurrentState == EAlive )
       
   396                 {
       
   397                 __TIME( "CAiStateManager::ProcessStateChange, EAlive", 
       
   398                         
       
   399                 plugin->Resume( CHsContentPublisher::EForeground ) );         
       
   400                 }
       
   401             else if ( iCurrentState == ESuspended )
       
   402                 {
       
   403                 __TIME( "CAiStateManager::ProcessStateChange, ESuspended",
       
   404                         
       
   405                 plugin->Suspend( CHsContentPublisher::EBackground ) );
       
   406                 }            
       
   407             }
       
   408         }
       
   409     else
       
   410         {
       
   411         __PRINTS( "CAiStateManager::ProcessStateChange, no state change" );
       
   412         }
       
   413     
       
   414     __TIME_ENDMARK( "CAiStateManager::ProcessStateChange, done", time );
       
   415     }
       
   416 
       
   417 // ----------------------------------------------------------------------------
       
   418 // CAiStateManager::ProcessGeneralThemeChange()
       
   419 // 
       
   420 // ----------------------------------------------------------------------------
       
   421 //
       
   422 void CAiStateManager::ProcessGeneralThemeChange()
       
   423     {
       
   424     __PRINTS( "CAiStateManager::ProcessGeneralThemeChange" );
       
   425     __TIME_MARK( time );
       
   426     
       
   427     RPointerArray< CHsContentPublisher >& plugins( iFactory.Publishers() );
       
   428             
       
   429     for( TInt i = 0; i < plugins.Count(); i++ )
       
   430         {
       
   431         CHsContentPublisher* plugin( plugins[i] );
       
   432         
       
   433         plugin->Suspend( CHsContentPublisher::EGeneralThemeChange );
       
   434                 
       
   435         // Resume publisher immediately if in alive state 
       
   436         if( !iHalt && iCurrentState == EAlive )
       
   437             {
       
   438             plugin->Resume( CHsContentPublisher::EForeground );
       
   439             }        
       
   440         } 
       
   441     
       
   442     __TIME_ENDMARK( "CAiStateManager::ProcessGeneralThemeChange, done", time );
       
   443     }
       
   444 
       
   445 // ----------------------------------------------------------------------------
       
   446 // CAiStateManager::ProcessBackupRestore()
       
   447 // 
       
   448 // ----------------------------------------------------------------------------
       
   449 //
       
   450 void CAiStateManager::ProcessBackupRestore( TBool aStart )
       
   451     {
       
   452     __PRINTS( "CAiStateManager::ProcessBackupRestore" );
       
   453     __TIME_MARK( time );
       
   454     
       
   455     iHalt = aStart;
       
   456 
       
   457     RPointerArray< CHsContentPublisher >& plugins( iFactory.Publishers() );
       
   458             
       
   459     for( TInt i = 0; i < plugins.Count(); i++ )
       
   460         {
       
   461         CHsContentPublisher* plugin( plugins[i] );
       
   462         
       
   463         if ( aStart )
       
   464             {
       
   465             plugin->Suspend( CHsContentPublisher::EBackupRestore );
       
   466             }
       
   467         else
       
   468             {
       
   469             // Resume publisher immediately if in alive state
       
   470             if ( !iHalt && iCurrentState == EAlive )
       
   471                 {
       
   472                 plugin->Resume( CHsContentPublisher::EForeground );
       
   473                 }
       
   474             }
       
   475         }
       
   476             
       
   477     __TIME_ENDMARK( "CAiStateManager::ProcessBackupRestore, done", time );
       
   478     }
       
   479 
       
   480 // ----------------------------------------------------------------------------
       
   481 // CAiStateManager::ProcessOnlineStateChange()
       
   482 // 
       
   483 // ----------------------------------------------------------------------------
       
   484 //
       
   485 void CAiStateManager::ProcessOnlineStateChange()
       
   486     {
       
   487     __PRINTS( "CAiStateManager::ProcessOnlineStateChange" );
       
   488     
       
   489     RPointerArray< CHsContentPublisher >& plugins( iFactory.Publishers() );
       
   490     
       
   491     for( TInt i = 0; i < plugins.Count(); i++ )
       
   492         {
       
   493         CHsContentPublisher* plugin( plugins[i] );
       
   494         
       
   495         if ( iFlags.IsSet( EIsOnline ) )
       
   496             {
       
   497             plugin->SetOnline();
       
   498             }
       
   499         else
       
   500             {
       
   501             plugin->SetOffline();
       
   502             }
       
   503         }               
       
   504     }
       
   505 
       
   506 // ----------------------------------------------------------------------------
       
   507 // CAiStateManager::StartPlugin()
       
   508 // 
       
   509 // ----------------------------------------------------------------------------
       
   510 //
       
   511 void CAiStateManager::StartPlugin( CHsContentPublisher& aPlugin,
       
   512     CHsContentPublisher::TStartReason aReason )
       
   513     {
       
   514     __PRINTS( "CAiStateManager::StartPlugin" );
       
   515     
       
   516     aPlugin.Start( aReason );
       
   517     
       
   518     if ( iCurrentState == EAlive )
       
   519         {
       
   520         aPlugin.Resume( CHsContentPublisher::EForeground );
       
   521         }
       
   522     else if ( iCurrentState == ESuspended )
       
   523         {
       
   524         aPlugin.Suspend( CHsContentPublisher::EBackground );
       
   525         }    
       
   526     
       
   527     if ( iFlags.IsSet( EIsOnline ) )
       
   528         {
       
   529         aPlugin.SetOnline();
       
   530         }
       
   531     else
       
   532         {
       
   533         aPlugin.SetOffline();
       
   534         }
       
   535     }
       
   536 
       
   537 // ----------------------------------------------------------------------------
       
   538 // CAiStateManager::StopPlugin()
       
   539 // 
       
   540 // ----------------------------------------------------------------------------
       
   541 //
       
   542 void CAiStateManager::StopPlugin( CHsContentPublisher& aPlugin,
       
   543     CHsContentPublisher::TStopReason aReason )
       
   544     {
       
   545     __PRINTS( "CAiStateManager::StopPlugin" );
       
   546                 
       
   547     if ( iCurrentState == EAlive )
       
   548         {
       
   549         aPlugin.Suspend( CHsContentPublisher::EBackground );
       
   550         }
       
   551     
       
   552     aPlugin.Stop( aReason );                    
       
   553     }
       
   554 
       
   555 // ----------------------------------------------------------------------------
       
   556 // CAiStateManager::DestroyPlugins()
       
   557 // 
       
   558 // ----------------------------------------------------------------------------
       
   559 //
       
   560 void CAiStateManager::DestroyPlugins()
       
   561     {
       
   562     __PRINTS( "CAiStateManager::DestroyPlugins, start" );    
       
   563     __TIME_MARK( time );
       
   564     
       
   565     RPointerArray< CHsContentPublisher >& plugins( iFactory.Publishers() );
       
   566     
       
   567     for ( TInt i = 0; i < plugins.Count(); i++ )
       
   568         {
       
   569         CHsContentPublisher* plugin( plugins[i] );
       
   570 
       
   571         // Do shutdown state transition
       
   572         StopPlugin( *plugin, CHsContentPublisher::ESystemShutdown );        
       
   573         }    
       
   574     
       
   575     // Finally get rid of all plugins
       
   576     plugins.ResetAndDestroy();
       
   577     
       
   578     __TIME_ENDMARK( "CAiStateManager::DestroyPlugins, done", time );
       
   579     }
       
   580 
       
   581 // End of file