coreapplicationuis/SysAp/Src/powersavemode/sysappsmcontroller.cpp
changeset 0 2e3d3ce01487
child 81 676b6116ca93
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 /*
       
     2 * Copyright (c) 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:  SysAp power save mode controller implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32debug.h>
       
    20 #include <psmclient.h>
       
    21 #include <psmsettings.h>
       
    22 #include <centralrepository.h>
       
    23 #include <settingsinternalcrkeys.h>
       
    24 #include "sysappsmcontroller.h"
       
    25 #include "SysAp.hrh"
       
    26 
       
    27 // ======== MEMBER FUNCTIONS ========
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 // CSysApPsmController::CSysApPsmController
       
    31 // ---------------------------------------------------------------------------
       
    32 //
       
    33 CSysApPsmController::CSysApPsmController( MSysApPsmControllerNotifyCallback& aCallback ) : 
       
    34                                              iCallback( aCallback ),
       
    35                                              iPsmClient( NULL ),
       
    36                                              iActivePsm( EPsmsrvModeNormal ),
       
    37                                              iForcedPartialPsm( EFalse ),
       
    38                                              iUiItemId( 0 ),
       
    39                                              iNextUiOperation( EFalse ),
       
    40                                              iDiscardUiNotes( EFalse ),
       
    41                                              iBatteryLowCounter( 0 ),
       
    42                                              iChargerConnected( EFalse )
       
    43                                              
       
    44     {
       
    45     }
       
    46 
       
    47 // ---------------------------------------------------------------------------
       
    48 // CSysApPsmController::ConstructL
       
    49 // ---------------------------------------------------------------------------
       
    50 //
       
    51 void CSysApPsmController::ConstructL()
       
    52     {
       
    53     TRACES( RDebug::Print( _L("CSysApPsmController::ConstructL") ) );
       
    54     
       
    55     iPsmClient = CPsmClient::NewL( *this );
       
    56 
       
    57     // request explicitly to be notified if some other client changes power save mode
       
    58     iPsmClient->RequestPowerSaveModeNotification(); 
       
    59     
       
    60     TInt err = iPsmClient->PsmSettings().GetCurrentMode( iActivePsm );
       
    61     
       
    62     if ( err != KErrNone )
       
    63         {
       
    64         iActivePsm = EPsmsrvModeNormal;    
       
    65         }
       
    66     
       
    67     if ( iActivePsm == EPsmsrvPartialMode )
       
    68         {
       
    69         iForcedPartialPsm = ETrue;
       
    70         }
       
    71     
       
    72 
       
    73     TRACES( RDebug::Print( _L("CSysApPsmController::ConstructL: err=%d, iActivePsm=%d"), err, iActivePsm ) );
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // CSysApPsmController::NewL
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 CSysApPsmController* CSysApPsmController::NewL( MSysApPsmControllerNotifyCallback& aCallback )
       
    81     {
       
    82     CSysApPsmController* self = new( ELeave ) CSysApPsmController( aCallback );
       
    83     CleanupStack::PushL( self );
       
    84     self->ConstructL();
       
    85     CleanupStack::Pop( self );
       
    86     return self;
       
    87     }
       
    88 
       
    89 // ---------------------------------------------------------------------------
       
    90 // CSysApPsmController::~CSysApPsmController
       
    91 // ---------------------------------------------------------------------------
       
    92 //
       
    93 CSysApPsmController::~CSysApPsmController()
       
    94     {
       
    95     TRACES( RDebug::Print( _L("CSysApPsmController::~CSysApPsmController") ) );
       
    96     delete iPsmClient;
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // CSysApPsmController::SetPowerSaveMode
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 void CSysApPsmController::SetPowerSaveMode( TInt aNewMode )
       
   104     {
       
   105     TRACES( RDebug::Print( _L("CSysApPsmController::SetPowerSaveMode: aNewMode=%d"), aNewMode ) );
       
   106 
       
   107     iPsmClient->CancelPowerSaveModeNotificationRequest();
       
   108     iPsmClient->ChangePowerSaveMode( aNewMode );
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // CSysApPsmController::FullPsmEnabled
       
   113 // ---------------------------------------------------------------------------
       
   114 //
       
   115 TBool CSysApPsmController::FullPsmEnabled() const
       
   116     {
       
   117     return iActivePsm == EPsmsrvModePowerSave;
       
   118     }
       
   119     
       
   120 // ---------------------------------------------------------------------------
       
   121 // CSysApPsmController::PartialPsmEnabled
       
   122 // ---------------------------------------------------------------------------
       
   123 //
       
   124 TBool CSysApPsmController::PartialPsmEnabled() const
       
   125     {
       
   126     return iActivePsm == EPsmsrvPartialMode;
       
   127     }
       
   128 
       
   129 // ---------------------------------------------------------------------------
       
   130 // CSysApPsmController::UiItemId
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 TInt CSysApPsmController::UiItemId() const
       
   134     {
       
   135     return iUiItemId;    
       
   136     }
       
   137 
       
   138 // ---------------------------------------------------------------------------
       
   139 // CSysApPsmController::SetUiItemId
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 void CSysApPsmController::SetUiItemId( TInt aUiItemId )
       
   143     {
       
   144     iUiItemId = aUiItemId;
       
   145     }
       
   146     
       
   147 // ---------------------------------------------------------------------------
       
   148 // CSysApPsmController::DoEnablePartialPsm
       
   149 // ---------------------------------------------------------------------------
       
   150 //
       
   151 void CSysApPsmController::DoEnablePartialPsm( TBool aEnable )
       
   152     {
       
   153     TRACES( RDebug::Print( _L("CSysApPsmController::DoEnablePartialPsm: aEnable=%d"), aEnable ) );
       
   154 
       
   155     // full PSM is only controlled by the user so toggling partial PSM is only possible between itself and normal mode
       
   156     if ( aEnable )
       
   157         {
       
   158         // activate only from normal mode
       
   159         if( iActivePsm == EPsmsrvModeNormal )
       
   160             {
       
   161             SetPowerSaveMode( EPsmsrvPartialMode );
       
   162             iDiscardUiNotes = ETrue; // partial mode errors not reflected to UI    
       
   163             }    
       
   164         }
       
   165     else
       
   166         {
       
   167         // deactivate only from partial mode
       
   168         if ( iActivePsm == EPsmsrvPartialMode ) 
       
   169             {
       
   170             SetPowerSaveMode( EPsmsrvModeNormal );
       
   171             iDiscardUiNotes = ETrue; // partial mode errors not reflected to UI    
       
   172             }   
       
   173         }
       
   174     }
       
   175 
       
   176 
       
   177 // ---------------------------------------------------------------------------
       
   178 // CSysApPsmController::BatteryLow
       
   179 // ---------------------------------------------------------------------------
       
   180 //
       
   181 void CSysApPsmController::BatteryLow( TBool aBatteryIsLow )
       
   182     {
       
   183     TRACES( RDebug::Print( _L("CSysApPsmController::BatteryLow: aBatteryIsLow=%d"), aBatteryIsLow ) );
       
   184 
       
   185     if ( aBatteryIsLow )
       
   186     	{
       
   187     	iForcedPartialPsm = ETrue; 	// when battery is low, at least partial PSM must be active
       
   188 		}
       
   189 	else
       
   190 		{
       
   191 	    iForcedPartialPsm = EFalse; // releasing partial PSM when battery is not low
       
   192 
       
   193 	    iBatteryLowCounter = 0; 	// show activation query on next battery low event
       
   194 		}
       
   195     }
       
   196 
       
   197 // ---------------------------------------------------------------------------
       
   198 // CSysApPsmController::ConnectCharger
       
   199 // ---------------------------------------------------------------------------
       
   200 //
       
   201 void CSysApPsmController::ConnectCharger( TBool aConnect )
       
   202     {
       
   203     TRACES( RDebug::Print( _L("CSysApPsmController::ConnectCharger: aConnect=%d"), aConnect ) );
       
   204     
       
   205     BatteryLow( EFalse );
       
   206     
       
   207     iChargerConnected = aConnect;
       
   208     }
       
   209 
       
   210 // ---------------------------------------------------------------------------
       
   211 // CSysApPsmController::ChargerConnected
       
   212 // ---------------------------------------------------------------------------
       
   213 //
       
   214 TBool CSysApPsmController::ChargerConnected() const
       
   215     {
       
   216     return iChargerConnected;
       
   217     }
       
   218     
       
   219 // ---------------------------------------------------------------------------
       
   220 // CSysApPsmController::DoEnableFullPsm
       
   221 // ---------------------------------------------------------------------------
       
   222 //
       
   223 void CSysApPsmController::DoEnableFullPsm( TBool aEnable )
       
   224     {
       
   225     TRACES( RDebug::Print( _L("CSysApPsmController::DoEnableFullPsm: aEnable=%d, iActivePsm=%d, iForcedPartialPsm=%d"), 
       
   226             aEnable, iActivePsm, iForcedPartialPsm ) );
       
   227     
       
   228     if ( aEnable && iActivePsm != EPsmsrvModePowerSave )
       
   229         {
       
   230         SetPowerSaveMode( EPsmsrvModePowerSave );
       
   231         }
       
   232     else if ( !aEnable && iActivePsm != EPsmsrvModeNormal )
       
   233         {
       
   234         SetPowerSaveMode( EPsmsrvModeNormal );    
       
   235         }
       
   236     }
       
   237     
       
   238 // ---------------------------------------------------------------------------
       
   239 // CCSysApPsmController::SetNextUiOperation
       
   240 // ---------------------------------------------------------------------------
       
   241 //
       
   242 void CSysApPsmController::SetNextUiOperation( TBool aIsActivate )
       
   243     {
       
   244     TRACES( RDebug::Print( _L("CSysApPsmController::SetNextUiOperation: aIsActivate=%d"), aIsActivate ) );
       
   245     iNextUiOperation = aIsActivate;    
       
   246     }
       
   247     
       
   248 // ---------------------------------------------------------------------------
       
   249 // CCSysApPsmController::NextUiOperation
       
   250 // ---------------------------------------------------------------------------
       
   251 //
       
   252 TBool CSysApPsmController::NextUiOperation() const
       
   253     {
       
   254     return iNextUiOperation;
       
   255     }
       
   256 
       
   257 
       
   258 // ---------------------------------------------------------------------------
       
   259 // CSysApPsmController::PowerSaveModeChanged
       
   260 // from MPsmClientObserver
       
   261 // ---------------------------------------------------------------------------
       
   262 //
       
   263 void CSysApPsmController::PowerSaveModeChanged( const TPsmsrvMode aMode )
       
   264     {
       
   265     TRACES( RDebug::Print( _L("CSysApPsmController::PowerSaveModeChanged: iActivePsm=%d, aMode=%d, iForcedPartialPsm"),
       
   266             iActivePsm, aMode, iForcedPartialPsm ) );
       
   267     
       
   268     iPsmClient->RequestPowerSaveModeNotification(); // must be requested after every completion
       
   269     
       
   270     iActivePsm = aMode;
       
   271     TBool showUiNotes = !iDiscardUiNotes;
       
   272     iDiscardUiNotes = EFalse; // set now because if partial PSM is activated it sets this back to ETrue
       
   273     
       
   274     switch ( iActivePsm )
       
   275         {
       
   276         case EPsmsrvModeNormal:
       
   277             if ( showUiNotes )
       
   278                 {
       
   279                 TRAP_IGNORE( iCallback.NotifyPowerSaveModeL( MSysApPsmControllerNotifyCallback::EPsmDeactivationComplete ) );    
       
   280                 }
       
   281             
       
   282             if ( iForcedPartialPsm )
       
   283                 {
       
   284                 DoEnablePartialPsm( ETrue );
       
   285                 }
       
   286             break;
       
   287         
       
   288         case EPsmsrvModePowerSave:
       
   289             if ( showUiNotes )
       
   290                 {
       
   291                 TRAP_IGNORE( iCallback.NotifyPowerSaveModeL( MSysApPsmControllerNotifyCallback::EPsmActivationComplete ) );    
       
   292                 }
       
   293             break;
       
   294 
       
   295         case EPsmsrvPartialMode: // partial mode not shown in UI level        
       
   296         default:
       
   297             break;
       
   298         }
       
   299     }
       
   300 
       
   301 // ---------------------------------------------------------------------------
       
   302 // CSysApPsmController::PowerSaveModeChangeError
       
   303 // from MPsmClientObserver
       
   304 // ---------------------------------------------------------------------------
       
   305 //
       
   306 void CSysApPsmController::PowerSaveModeChangeError( const TInt aError )
       
   307     {
       
   308     TRACES( RDebug::Print( _L("CSysApPsmController::PowerSaveModeChangeError: aError=%d, iDiscardUiNotes=%d"),
       
   309             aError, iDiscardUiNotes ) );
       
   310     
       
   311     if ( aError != KErrCancel ) // ignore error if it is due cancellation of notification request
       
   312         {
       
   313         if ( aError != KErrServerTerminated ) // new request would cause another completion with this error code (infinitely)
       
   314             {
       
   315             iPsmClient->RequestPowerSaveModeNotification(); // must be requested after every completion
       
   316             }        
       
   317 
       
   318         if ( !iDiscardUiNotes ) // The user is not notified if activating partial mode fails
       
   319             {
       
   320             if ( iActivePsm == EPsmsrvModePowerSave )
       
   321                 {
       
   322                 TRAP_IGNORE( iCallback.NotifyPowerSaveModeL( MSysApPsmControllerNotifyCallback::EPsmDeactivationFailed ) );
       
   323                 }
       
   324             else
       
   325                 {
       
   326                 TRAP_IGNORE( iCallback.NotifyPowerSaveModeL( MSysApPsmControllerNotifyCallback::EPsmActivationFailed ) );
       
   327                 }    
       
   328             }
       
   329         iDiscardUiNotes = EFalse;
       
   330         }
       
   331     }
       
   332 
       
   333 
       
   334 // ---------------------------------------------------------------------------
       
   335 // CSysApPsmController::ShowActivateQuery
       
   336 // ---------------------------------------------------------------------------
       
   337 //
       
   338 TBool CSysApPsmController::ShowActivateQuery()
       
   339     {
       
   340     TBool retVal( EFalse );
       
   341     
       
   342     // conditions
       
   343     // power saving is not yet activated
       
   344     // this is the first battery low warning
       
   345     if ( iBatteryLowCounter == 0 && 
       
   346          iActivePsm == EPsmsrvModeNormal )
       
   347         {
       
   348         //  query must be activated in CenRep setting
       
   349         retVal = QueriesEnabled();
       
   350         }
       
   351     
       
   352     iBatteryLowCounter++; // query must not be shown on subsequent low events
       
   353     return retVal;    
       
   354     }
       
   355 
       
   356 // ---------------------------------------------------------------------------
       
   357 // CSysApPsmController::ShowDeactivateQuery
       
   358 // ---------------------------------------------------------------------------
       
   359 //
       
   360 TBool CSysApPsmController::ShowDeactivateQuery() const
       
   361     {
       
   362     TBool retVal( EFalse );
       
   363     
       
   364     // conditions
       
   365     // power saving has been activated
       
   366     // query has been activated in CenRep setting
       
   367     if ( iActivePsm == EPsmsrvModePowerSave )
       
   368         {
       
   369         //  query must be activated in CenRep setting
       
   370         retVal = QueriesEnabled();
       
   371         }
       
   372     
       
   373     return retVal;
       
   374     }
       
   375 
       
   376 // ---------------------------------------------------------------------------
       
   377 // CSysApPsmController::QueriesEnabled
       
   378 // ---------------------------------------------------------------------------
       
   379 //    
       
   380 TBool CSysApPsmController::QueriesEnabled() const
       
   381     {
       
   382     TBool enabled( ETrue ); // enabled by default
       
   383     
       
   384     CRepository* repository( NULL );
       
   385     
       
   386     TRAPD( err, repository = CRepository::NewL( KCRUidDeviceManagementSettings ) );
       
   387     
       
   388     if ( err == KErrNone )
       
   389         {
       
   390         TInt value( 0 );
       
   391         err = repository->Get( KSettingsPowerSavingQuery, value );
       
   392         
       
   393         if ( err == KErrNone )
       
   394             {
       
   395             enabled = value ? ETrue : EFalse;
       
   396             }
       
   397         }
       
   398     
       
   399     delete repository;                        
       
   400     
       
   401     TRACES( RDebug::Print( _L("CSysApPsmController::QueriesEnabled: enabled=%d, err=%d"), enabled, err ) );
       
   402     
       
   403     return enabled;
       
   404     }