camerauis/cameraapp/generic/src/cambatterypanecontroller.cpp
branchRCL_3
changeset 24 bac7acad7cb3
parent 0 1ddebce53859
child 25 2c87b2808fd7
equal deleted inserted replaced
23:61bc0f252b2b 24:bac7acad7cb3
       
     1 /*
       
     2 * Copyright (c) 2007-2009 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:  Implemantation for CCamBatteryPaneController class.
       
    15 *
       
    16 *  Copyright © 2007-2008 Nokia.  All rights reserved.
       
    17 *  This material, including documentation and any related computer
       
    18 *  programs, is protected by copyright controlled by Nokia.  All
       
    19 *  rights are reserved.  Copying, including reproducing, storing,
       
    20 *  adapting or translating, any or all of this material requires the
       
    21 *  prior written consent of Nokia.  This material also contains
       
    22 *  confidential information which may not be disclosed to others
       
    23 *  without the prior written consent of Nokia.
       
    24 
       
    25 *
       
    26 *
       
    27 */
       
    28 
       
    29 
       
    30 #include "cambatterypanecontroller.h"
       
    31 
       
    32 #include <e32base.h>
       
    33 #include <e32property.h>                // RProperty
       
    34 #include <hwrmpowerstatesdkpskeys.h>    // P&S keys
       
    35 #include <eikdef.h>                     // KEikColorResourceChange
       
    36 #include <AknsConstants.h>              // KAknsMessageSkinChange 
       
    37 
       
    38 #include "cambatterypanedrawer.h"
       
    39 #include "CamPropertyWatcher.h"
       
    40 #include "CamUtility.h"
       
    41 #include "CamAppUi.h"
       
    42 
       
    43 
       
    44 // ---------------------------------------------------------------------------
       
    45 // CCamBatteryPaneController::CCamBatteryPaneController
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 CCamBatteryPaneController::CCamBatteryPaneController(
       
    49     MCamBatteryPaneObserver& aObserver, TBool aCallbackActive ) :
       
    50     iObserver( aObserver ), iCallbackActive( aCallbackActive )
       
    51     {
       
    52     }
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // CCamBatteryPaneController::~CCamBatteryPaneController
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 CCamBatteryPaneController::~CCamBatteryPaneController()
       
    59   {
       
    60   PRINT ( _L("Camera => ~CCamBatteryPaneController") );
       
    61   
       
    62   if (iTicker)
       
    63     {
       
    64     iTicker->Cancel();
       
    65     }
       
    66   delete iTicker;	
       
    67   delete iDrawer;	 
       
    68   delete iBatteryStrengthWatcher;    
       
    69   delete iBatteryChargingWatcher;
       
    70   
       
    71   PRINT ( _L("Camera <= ~CCamBatteryPaneController") ); 	   
       
    72   }
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // CCamBatteryPaneController::NewL
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 CCamBatteryPaneController* CCamBatteryPaneController::NewL(
       
    79     MCamBatteryPaneObserver& aObserver,
       
    80     TBool aCallbackActive )
       
    81     {
       
    82     PRINT ( _L("Camera => CCamBatteryPaneController::NewL") );		
       
    83     
       
    84     CCamBatteryPaneController* self = new (ELeave) CCamBatteryPaneController(
       
    85             aObserver, aCallbackActive );
       
    86     CleanupStack::PushL( self );
       
    87     self->ConstructL();
       
    88     CleanupStack::Pop(); // self
       
    89     PRINT ( _L("Camera <= CCamBatteryPaneController::NewL") );
       
    90     return self;    
       
    91     }
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 // CCamBatteryPaneController::ConstructL
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 void CCamBatteryPaneController::ConstructL()
       
    98     {
       
    99     PRINT ( _L("Camera => CCamBatteryPaneController::ConstructL") );	
       
   100     
       
   101     iDrawer = CCamBatteryPaneDrawer::NewL();
       
   102     iTicker = CPeriodic::NewL( CActive::EPriorityHigh );
       
   103     
       
   104     // Construct property watchers for battery level and charging status
       
   105     iBatteryStrengthWatcher = CCamPropertyWatcher::NewL( *this, KPSUidHWRMPowerState, KHWRMBatteryLevel );  
       
   106     iBatteryChargingWatcher = CCamPropertyWatcher::NewL( *this, KPSUidHWRMPowerState, KHWRMChargingStatus );
       
   107       
       
   108     // Subscribe to the properties 
       
   109     iBatteryStrengthWatcher->Subscribe();
       
   110     iBatteryChargingWatcher->Subscribe();   
       
   111     
       
   112     // Read initial values for battery level and charging status
       
   113     ReadCurrentState();
       
   114     
       
   115     PRINT ( _L( "Camera <= CCamBatteryPaneController::ConstructL" ) );	
       
   116     }
       
   117 
       
   118 
       
   119 // ---------------------------------------------------------------------------
       
   120 // CCamBatteryPaneController::Pause
       
   121 // ---------------------------------------------------------------------------
       
   122 //    
       
   123 void CCamBatteryPaneController::Pause( TBool aPause )
       
   124     {
       
   125     PRINT1 ( _L("Camera => CCamBatteryPaneController::Pause( %d)"), aPause );	
       
   126     if( aPause )
       
   127       {
       
   128       // Pause battery pane controller  
       
   129       if( !iPaused )
       
   130         {  
       
   131         // Prevent callbacks
       
   132         TBool oldCbState = iCallbackActive; 
       
   133         iCallbackActive = EFalse;
       
   134         // Stop recharging
       
   135         if( iRecharging )
       
   136           {
       
   137           StopRecharging(); 
       
   138           }
       
   139         // Cancel notifications of battery events      
       
   140         iBatteryStrengthWatcher->Cancel();
       
   141         iBatteryChargingWatcher->Cancel();  
       
   142         iPaused = ETrue;
       
   143         // Restore callback state
       
   144         iCallbackActive = oldCbState;
       
   145         } 
       
   146       }
       
   147     else
       
   148       {
       
   149       // Restart battery pane controller  
       
   150       if( iPaused )          
       
   151         {
       
   152         // Re-subscribe to battery events  
       
   153         iBatteryStrengthWatcher->Subscribe();
       
   154         iBatteryChargingWatcher->Subscribe();
       
   155         ReadCurrentState();
       
   156         iPaused = EFalse;
       
   157         }
       
   158       }  
       
   159     PRINT( _L("Camera <= CCamBatteryPaneController::Pause") );	  
       
   160     }
       
   161 
       
   162 // ---------------------------------------------------------------------------
       
   163 // CCamBatteryPaneController::SetCallbackActive
       
   164 // ---------------------------------------------------------------------------
       
   165 //    
       
   166 void CCamBatteryPaneController::SetCallbackActive( TBool aActive )
       
   167     {
       
   168     iCallbackActive = aActive;
       
   169     }
       
   170 
       
   171 // ---------------------------------------------------------------------------
       
   172 // CCamBatteryPaneController::SetBatteryStrength
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 void CCamBatteryPaneController::SetBatteryStrength( TInt aStrength )
       
   176     {    	
       
   177     PRINT( _L("Camera => CCamBatteryPaneController::SetBatteryStrength") );	    	
       
   178     
       
   179     // Drawer uses battery strength for indexing a table, so make
       
   180     // sure it stays within valid boundaries
       
   181     if( aStrength > KMaxBatteryStrength )
       
   182         {
       
   183         iBatteryStrength = KMaxBatteryStrength;
       
   184         }
       
   185     else if( aStrength < KMinBatteryStrength )
       
   186         {
       
   187         iBatteryStrength = KMinBatteryStrength;        
       
   188         }
       
   189     else
       
   190         {
       
   191         iBatteryStrength = aStrength;    
       
   192         }
       
   193         
       
   194     if( !iRecharging )
       
   195         {        
       
   196         iDrawer->SetBatteryStrength( iBatteryStrength );
       
   197         NotifyObserver();
       
   198         }
       
   199         
       
   200     PRINT( _L("Camera <= CCamBatteryPaneController::SetBatteryStrength") );	        
       
   201     }    
       
   202  
       
   203 // ---------------------------------------------------------------------------
       
   204 // CCamBatteryPaneController::SetLocation
       
   205 // ---------------------------------------------------------------------------    
       
   206 //
       
   207 void CCamBatteryPaneController::SetLocation( TPoint aLocation )
       
   208     {
       
   209     iDrawer->SetLocation( aLocation );
       
   210     }
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // CCamBatteryPaneController::Rect
       
   214 // ---------------------------------------------------------------------------    
       
   215 //
       
   216 TRect CCamBatteryPaneController::Rect() const
       
   217 	{
       
   218 	return iDrawer->Rect();	
       
   219 	}
       
   220 
       
   221 // ---------------------------------------------------------------------------
       
   222 // CCamBatteryPaneController::Draw
       
   223 // ---------------------------------------------------------------------------
       
   224 //
       
   225 void CCamBatteryPaneController::Draw( CBitmapContext& aGc ) const
       
   226 //void CCamBatteryPaneController::Draw( CWindowGc& aGc ) const
       
   227     {
       
   228     CCamAppUi* appUi = static_cast<CCamAppUi*>(CEikonEnv::Static()->AppUi());
       
   229     
       
   230     // When battery recharge animation runs over, indicator needs clearing
       
   231     // PostCap view has redraw functions of its own, so it doesn't need this
       
   232     if ( iRechargeBatteryStrength == 0 && 
       
   233          iRecharging &&
       
   234          !appUi->IsBurstEnabled() &&
       
   235 		 appUi->CurrentViewState() != ECamViewStatePostCapture && 
       
   236          !(appUi->IsSecondCameraEnabled() && appUi->IsQwerty2ndCamera() ) )
       
   237         {    
       
   238         iDrawer->ClearBattery( aGc );
       
   239         }
       
   240     
       
   241     iDrawer->Draw( aGc );
       
   242     }    
       
   243 
       
   244 // ---------------------------------------------------------------------------
       
   245 // CCamBatteryPaneController::StartRecharging
       
   246 // ---------------------------------------------------------------------------
       
   247 //
       
   248 void CCamBatteryPaneController::StartRecharging()
       
   249     {
       
   250     PRINT( _L("Camera => CCamBatteryPaneController::StartRecharging") );  	
       
   251     
       
   252     // Stop current timer
       
   253     iTicker->Cancel();
       
   254     
       
   255     // Initialize
       
   256     iRecharging = ETrue;
       
   257     iRechargeBatteryStrength = KMinBatteryStrength; 
       
   258       
       
   259     // Give initial strength value to drawer and notify observer
       
   260     iDrawer->SetBatteryStrength( iRechargeBatteryStrength );
       
   261     NotifyObserver();
       
   262     
       
   263     // Start the animation
       
   264     iTicker->Start( KBatteryRechargeTick, KBatteryRechargeTick, TCallBack(TickerCallback, this) );
       
   265     
       
   266     PRINT( _L("Camera => CCamBatteryPaneController::StartRecharging") );  
       
   267     }
       
   268 
       
   269 // ---------------------------------------------------------------------------
       
   270 // CCamBatteryPaneController::StopRecharging
       
   271 // ---------------------------------------------------------------------------
       
   272 //
       
   273 void CCamBatteryPaneController::StopRecharging()
       
   274     {
       
   275     PRINT( _L("Camera => CCamBatteryPaneController::StopRecharging") ); 	
       
   276     
       
   277     // Stop the timer and give the original battery
       
   278     // strength to drawer class
       
   279     iTicker->Cancel();
       
   280     iRecharging = EFalse;
       
   281     iDrawer->SetBatteryStrength( iBatteryStrength );
       
   282     NotifyObserver();
       
   283     
       
   284     PRINT( _L("Camera <= CCamBatteryPaneController::StopRecharging") ); 
       
   285     }
       
   286 
       
   287 // ---------------------------------------------------------------------------
       
   288 // CCamBatteryPaneController::NotifyObserver
       
   289 // ---------------------------------------------------------------------------    
       
   290 //
       
   291 void CCamBatteryPaneController::NotifyObserver()
       
   292     {
       
   293     if( iCallbackActive )
       
   294         {
       
   295         iObserver.BatteryPaneUpdated();
       
   296         }
       
   297     }
       
   298     
       
   299 // ---------------------------------------------------------------------------
       
   300 // CCamBatteryPaneController::UpdateRechargeBatteryStrength
       
   301 // ---------------------------------------------------------------------------    
       
   302 //
       
   303 void CCamBatteryPaneController::UpdateRechargeBatteryStrength()
       
   304     {    
       
   305     PRINT( _L("Camera => CCamBatteryPaneController::UpdateRechargeBatteryStrength") );	
       
   306     
       
   307     iRechargeBatteryStrength++;
       
   308     if( iRechargeBatteryStrength > KMaxBatteryStrength )
       
   309         {
       
   310         iRechargeBatteryStrength = KMinBatteryStrength;        
       
   311         }
       
   312     iDrawer->SetBatteryStrength( iRechargeBatteryStrength );
       
   313     
       
   314     NotifyObserver();   
       
   315     
       
   316     PRINT( _L("Camera <= CCamBatteryPaneController::UpdateRechargeBatteryStrength") );	     
       
   317     }
       
   318 
       
   319 // ---------------------------------------------------------------------------
       
   320 // CCamBatteryPaneController::TickerCallback
       
   321 // ---------------------------------------------------------------------------    
       
   322 //
       
   323 TInt CCamBatteryPaneController::TickerCallback( TAny* aThis )
       
   324     {    	
       
   325 	CCamBatteryPaneController* self = static_cast<CCamBatteryPaneController*>( aThis );
       
   326 	if( self )
       
   327 		{
       
   328 		self->UpdateRechargeBatteryStrength();		
       
   329 		}    
       
   330     return ETrue;    
       
   331     }
       
   332 
       
   333 // ---------------------------------------------------------------------------
       
   334 // CCamBatteryPaneController::ReadCurrentState
       
   335 // ---------------------------------------------------------------------------
       
   336 //
       
   337 void CCamBatteryPaneController::ReadCurrentState()
       
   338     {
       
   339     PRINT( _L("Camera => CCamBatteryPaneController::ReadCurrentState") ); 	
       
   340     
       
   341     TInt batteryStrength = 0;
       
   342     TInt chargingStatus = 0;
       
   343     TInt err = KErrNone;
       
   344 
       
   345     // Disable callbacks to avoid two separate callbacks for charging
       
   346     // status and battery level
       
   347     TBool callbackActive = iCallbackActive;
       
   348     iCallbackActive = EFalse;
       
   349     
       
   350     // Get the battery strength    
       
   351     err = iBatteryStrengthWatcher->Get( batteryStrength );
       
   352     if( KErrNone == err && -1 != batteryStrength )
       
   353         {
       
   354         // We got an acceptable value
       
   355         SetBatteryStrength( batteryStrength );
       
   356         }
       
   357     else
       
   358         {
       
   359         // Default to lowest value
       
   360         SetBatteryStrength( KMinBatteryStrength );
       
   361         }    
       
   362     
       
   363     // Get current recharging state       
       
   364     err = iBatteryChargingWatcher->Get( chargingStatus );
       
   365     if ( KErrNone == err )
       
   366         {
       
   367         HandleChargingStatusChange( chargingStatus );
       
   368         }
       
   369     else
       
   370         {        
       
   371         StopRecharging();                
       
   372         }        
       
   373     
       
   374     // Restore callbacks and notify the observer
       
   375     iCallbackActive = callbackActive;
       
   376     NotifyObserver();                
       
   377     
       
   378     PRINT( _L("Camera <= CCamBatteryPaneController::ReadCurrentState") ); 	
       
   379     }
       
   380     
       
   381 
       
   382 // ---------------------------------------------------------------------------
       
   383 // CCamBatteryPaneController::HandleChargingStatusChange
       
   384 // ---------------------------------------------------------------------------
       
   385 //
       
   386 void CCamBatteryPaneController::HandleChargingStatusChange( TInt aStatus )
       
   387     {
       
   388     PRINT( _L("Camera => CCamBatteryPaneController::HandleChargingStatusChange") );	    	
       
   389     	
       
   390     if( EChargingStatusCharging == aStatus || 
       
   391         EChargingStatusAlmostComplete == aStatus ||
       
   392         EChargingStatusChargingContinued == aStatus )
       
   393         {
       
   394         // The battery is being recharged
       
   395         
       
   396         if( !iRecharging )
       
   397             {
       
   398             // Start the recharging animation
       
   399             StartRecharging();
       
   400             }
       
   401         }
       
   402     else
       
   403         {
       
   404         // The battery is currently not being recharged
       
   405         if( iRecharging )
       
   406             {
       
   407             // Stop the recharging animation
       
   408             StopRecharging();
       
   409             }        
       
   410         }    
       
   411         
       
   412     PRINT( _L("Camera <= CCamBatteryPaneController::HandleChargingStatusChange") );	        
       
   413     }
       
   414     
       
   415 // ---------------------------------------------------------------------------
       
   416 // CCamBatteryPaneController::HandlePropertyChangedL
       
   417 // ---------------------------------------------------------------------------    
       
   418 //
       
   419 void CCamBatteryPaneController::HandlePropertyChangedL( const TUid& aCategory, const TUint aKey )
       
   420     {
       
   421     PRINT( _L("Camera => CCamBatteryPaneController::HandlePropertyChangedL") ); 	    	
       
   422     
       
   423     TInt value;
       
   424     if( KPSUidHWRMPowerState == aCategory && KHWRMBatteryLevel == aKey )
       
   425         {
       
   426         TInt err = iBatteryStrengthWatcher->Get( value );
       
   427         if( KErrNone == err && -1 != value )
       
   428             {
       
   429             SetBatteryStrength( value );
       
   430             }
       
   431         }
       
   432     else if( KPSUidHWRMPowerState == aCategory && KHWRMChargingStatus == aKey )
       
   433         {
       
   434         TInt err = iBatteryChargingWatcher->Get( value );
       
   435         if( KErrNone == err && -1 != value )
       
   436             {
       
   437             HandleChargingStatusChange( value );
       
   438             }
       
   439         }
       
   440     else
       
   441         {
       
   442         // Do nothing. For Lint.
       
   443         }
       
   444 
       
   445     PRINT( _L("Camera <= CCamBatteryPaneController::HandlePropertyChangedL") );
       
   446     }
       
   447     
       
   448 // ---------------------------------------------------------------------------
       
   449 // CCamBatteryPaneController::HandleResourceChange
       
   450 // ---------------------------------------------------------------------------    
       
   451 //   
       
   452 void CCamBatteryPaneController::HandleResourceChange( TInt aType )
       
   453     {
       
   454     PRINT( _L("Camera => CCamBatteryPaneController::HandleResourceChange") );	
       
   455     
       
   456     if( KAknsMessageSkinChange == aType ||
       
   457         KEikColorResourceChange == aType )
       
   458         {
       
   459         iDrawer->HandleResourceChange( aType );
       
   460         }
       
   461         
       
   462 		PRINT( _L("Camera <= CCamBatteryPaneController::HandleResourceChange") );	        
       
   463     }
       
   464 
       
   465 // End of file
       
   466