uifw/AvKon/src/aknbattery.cpp
changeset 0 2f259fa3e83a
child 4 8ca85d2f0db7
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2002-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:  Control for status pane's battery pane.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <coemain.h>
       
    21 #include <barsread.h>
       
    22 #include <avkon.mbg>
       
    23 #include <AknsDrawUtils.h>
       
    24 #include <eikspane.h>
       
    25 #include <aknlayoutscalable_avkon.cdl.h>
       
    26 #include <layoutmetadata.cdl.h>
       
    27 
       
    28 #include <AknTasHook.h>
       
    29 #include "AknUtils.h"
       
    30 #include "aknconsts.h"
       
    31 #include "AknBattery.h"
       
    32 #include "AknStatuspaneUtils.h"
       
    33 #include "AknBatteryIcon.h"
       
    34 #include "AknBatteryStrength.h"
       
    35 #include "aknappui.h"
       
    36 #include "AknDef.h"
       
    37 #include "aknbatterydataobserver.h"
       
    38 
       
    39 
       
    40 // Flags for battery pane
       
    41 enum TBatteryPaneControlFlags
       
    42     {
       
    43     EAknBatteryPaneButton1DownInBatteryRect = 0x00000001
       
    44     };
       
    45 
       
    46 // Battery recharge animation frame interval.
       
    47 const TInt KBatteryRechargeTick = 500000; // 0.5 seconds
       
    48 
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // CAknBatteryPane::CAknBatteryPane
       
    52 // Default constructor.
       
    53 // ---------------------------------------------------------------------------
       
    54 //
       
    55 EXPORT_C CAknBatteryPane::CAknBatteryPane()
       
    56     {
       
    57     AKNTASHOOK_ADD( this, "CAknBatteryPane" );
       
    58     }
       
    59 
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // CAknBatteryPane::~CAknBatteryPane
       
    63 // Destructor.
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 EXPORT_C CAknBatteryPane::~CAknBatteryPane()
       
    67     {
       
    68     AKNTASHOOK_REMOVE();
       
    69     AknsUtils::DeregisterControlPosition( this );
       
    70 
       
    71     delete iBatteryIconControl;
       
    72     delete iBatteryStrengthControl;
       
    73 
       
    74     if ( iTicker )
       
    75         {
       
    76         iTicker->Cancel();
       
    77         }
       
    78 
       
    79     delete iTicker;
       
    80     delete iDataObserver;
       
    81     }
       
    82 
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // CAknBatteryPane::ConstructL
       
    86 // Second-phase constructor.
       
    87 // ---------------------------------------------------------------------------
       
    88 //
       
    89 EXPORT_C void CAknBatteryPane::ConstructL()
       
    90     {
       
    91     iBatteryIconControl = CAknBatteryIcon::NewL();
       
    92     iBatteryIconControl->SetContainerWindowL( *this );
       
    93     iBatteryStrengthControl = CAknBatteryStrength::NewL();
       
    94     iBatteryStrengthControl->SetContainerWindowL( *this );
       
    95     iPrivateFlags = 0; // reset flags
       
    96     iDataObserver = new (ELeave) CAknBatteryDataObserver( this );
       
    97     iIsActiveIdle = AknStatuspaneUtils::IsActiveIdle();
       
    98     }
       
    99 
       
   100 
       
   101 // ---------------------------------------------------------------------------
       
   102 // CAknBatteryPane::ConstructFromResourceL
       
   103 // Resource constructor.
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 EXPORT_C void CAknBatteryPane::ConstructFromResourceL(
       
   107     TResourceReader& aReader )
       
   108     {
       
   109     ConstructL();
       
   110     SetBatteryLevel( aReader.ReadInt16() ); // Initial battery level.
       
   111     }
       
   112 
       
   113 
       
   114 // ---------------------------------------------------------------------------
       
   115 // CAknBatteryPane::SetBatteryLevel
       
   116 // Sets the battery level.
       
   117 // ---------------------------------------------------------------------------
       
   118 //
       
   119 EXPORT_C void CAknBatteryPane::SetBatteryLevel( TInt aLevel )
       
   120     {
       
   121     iBatteryLevel = aLevel;
       
   122 
       
   123     if ( !iBatteryStrengthControl->Recharging() )
       
   124         {
       
   125         iBatteryStrengthControl->SetBatteryLevel( aLevel );
       
   126         DrawDeferred();
       
   127         }
       
   128     }
       
   129 
       
   130 
       
   131 // ---------------------------------------------------------------------------
       
   132 // CAknBatteryPane::BatteryLevel
       
   133 // Returns the current battery level.
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 TInt CAknBatteryPane::BatteryLevel()
       
   137     {
       
   138     return iBatteryLevel;
       
   139     }
       
   140 
       
   141 
       
   142 // ---------------------------------------------------------------------------
       
   143 // CAknBatteryPane::SetRecharge
       
   144 // Sets the recharging state on/off.
       
   145 // ---------------------------------------------------------------------------
       
   146 //
       
   147 void CAknBatteryPane::SetRecharge( TBool aRecharging )
       
   148     {
       
   149     if ( aRecharging )
       
   150         {
       
   151         if ( !iBatteryStrengthControl->Recharging() )
       
   152             {
       
   153             if ( !iTicker )
       
   154                 {
       
   155                 TRAP_IGNORE( iTicker = CPeriodic::NewL(
       
   156                                 CActive::EPriorityLow ) );
       
   157                 }
       
   158             else
       
   159                 {
       
   160                 iTicker->Cancel();
       
   161                 }
       
   162 
       
   163             if ( iTicker )
       
   164                 {
       
   165                 iTicker->Start( KBatteryRechargeTick,
       
   166                                 KBatteryRechargeTick,
       
   167                                 TCallBack( TickerCallback, this ) );
       
   168                 }
       
   169             }
       
   170         }
       
   171 
       
   172     iBatteryStrengthControl->SetRecharging( aRecharging );
       
   173 
       
   174     if ( !iBatteryStrengthControl->Recharging() )
       
   175         {
       
   176         SetBatteryLevel( iBatteryLevel ); // Set battery level to right value.
       
   177         }
       
   178     }
       
   179 
       
   180 
       
   181 // ---------------------------------------------------------------------------
       
   182 // CAknBatteryPane::Recharging
       
   183 // Returns the current recharging state.
       
   184 // ---------------------------------------------------------------------------
       
   185 //
       
   186 TBool CAknBatteryPane::Recharging()
       
   187     {
       
   188     if ( iBatteryStrengthControl )
       
   189         {
       
   190         return iBatteryStrengthControl->Recharging();
       
   191         }
       
   192     else
       
   193         {
       
   194         return EFalse;
       
   195         }
       
   196     }
       
   197 
       
   198 
       
   199 // ---------------------------------------------------------------------------
       
   200 // CAknBatteryPane::BatteryState
       
   201 // Returns the current battery state.
       
   202 // ---------------------------------------------------------------------------
       
   203 //
       
   204 TInt CAknBatteryPane::BatteryState()
       
   205     {
       
   206     if ( iBatteryIconControl )
       
   207         {
       
   208         return iBatteryIconControl->IconState();
       
   209         }
       
   210     else
       
   211         {
       
   212         return KErrNotFound;
       
   213         }
       
   214     }
       
   215 
       
   216 
       
   217 // ---------------------------------------------------------------------------
       
   218 // CAknBatteryPane::SetBatteryStateL
       
   219 // Sets the battery state.
       
   220 // ---------------------------------------------------------------------------
       
   221 //
       
   222 void CAknBatteryPane::SetBatteryStateL( TInt aState )
       
   223     {
       
   224     // Change battery icon only if the new state is
       
   225     // different than the current one.
       
   226     if ( iBatteryIconControl &&
       
   227          iBatteryIconControl->IconState() != aState )
       
   228         {
       
   229         iBatteryIconControl->SetBatteryIconL( aState );
       
   230         }
       
   231     }
       
   232 
       
   233 
       
   234 // ---------------------------------------------------------------------------
       
   235 // CAknBatteryPane::TickerCallback
       
   236 // Static callback function for recharge animation timer.
       
   237 // ---------------------------------------------------------------------------
       
   238 //
       
   239 TInt CAknBatteryPane::TickerCallback( TAny* aThis )
       
   240     {
       
   241     return static_cast<CAknBatteryPane*>( aThis )->DoTick();
       
   242     }
       
   243 
       
   244 
       
   245 // ---------------------------------------------------------------------------
       
   246 // CAknBatteryPane::DoTick
       
   247 // Callback function for recharge animation timer.
       
   248 // ---------------------------------------------------------------------------
       
   249 //
       
   250 TInt CAknBatteryPane::DoTick()
       
   251     {
       
   252     if ( !iBatteryStrengthControl->Recharging() )
       
   253         {
       
   254         iTicker->Cancel();
       
   255         return EFalse;
       
   256         }
       
   257 
       
   258     TInt newLevel = iBatteryStrengthControl->BatteryLevel() + 1;
       
   259 
       
   260     if ( newLevel > KBatteryLevelMax )
       
   261         {
       
   262         newLevel = KBatteryLevelMin;
       
   263         }
       
   264 
       
   265     iBatteryStrengthControl->SetBatteryLevel( newLevel );
       
   266     DrawDeferred(); // Must be DrawDeferred (not DrawNow) so that we don't
       
   267                     // block application thread in high load situations
       
   268     return ETrue;
       
   269     }
       
   270 
       
   271 
       
   272 // ---------------------------------------------------------------------------
       
   273 // CAknBatteryPane::SizeChanged
       
   274 // Handles size change events.
       
   275 // ---------------------------------------------------------------------------
       
   276 //
       
   277 EXPORT_C void CAknBatteryPane::SizeChanged()
       
   278     {
       
   279     // No fading if staconpane is active.
       
   280     SetContainerWindowNonFading( AknStatuspaneUtils::StaconPaneActive() );
       
   281 
       
   282     TAknWindowComponentLayout batteryIconLayout;
       
   283     TAknWindowComponentLayout batteryStrenghtLayout;
       
   284 
       
   285     TRect parent( Rect() );
       
   286 
       
   287     if ( AknStatuspaneUtils::StaconPaneActive() )
       
   288         {
       
   289         // Battery pane in STACONPANE layout
       
   290         // battery pane in status/controlpane layout used e.g. in landscape modes
       
   291         // layout 0.75 has new varieties
       
   292         TInt varietyIndex = AknStatuspaneUtils::StaconSoftKeysLeft() ? 1 : 0;
       
   293         batteryIconLayout =
       
   294             AknLayoutScalable_Avkon::battery_pane_stacon_g2( varietyIndex );
       
   295         batteryStrenghtLayout =
       
   296             AknLayoutScalable_Avkon::battery_pane_stacon_g1( varietyIndex );
       
   297         }
       
   298     else if ( AknStatuspaneUtils::FlatLayoutActive() )
       
   299         {
       
   300         batteryIconLayout = AknLayoutScalable_Avkon::battery_pane_g2( 1 );
       
   301         batteryStrenghtLayout = AknLayoutScalable_Avkon::battery_pane_g1( 1 );
       
   302         }
       
   303 	 else if ((AknStatuspaneUtils::CurrentStatusPaneLayoutResId() 
       
   304                  == R_AVKON_STATUS_PANE_LAYOUT_POWER_OFF_RECHARGE ||
       
   305              AknStatuspaneUtils::CurrentStatusPaneLayoutResId() 
       
   306                  == R_AVKON_STATUS_PANE_LAYOUT_POWER_OFF_RECHARGE_MIRRORED) &&
       
   307 			    Layout_Meta_Data::IsLandscapeOrientation())
       
   308 		  {
       
   309         batteryIconLayout = AknLayoutScalable_Avkon::battery_pane_g2(1);
       
   310         batteryStrenghtLayout = AknLayoutScalable_Avkon::battery_pane_g1(1);
       
   311 	     }
       
   312     else
       
   313         {
       
   314         // Battery pane in NORMAL of WIDESCREEN STATUS PANE layout.
       
   315         TBool hdLayout( AknStatuspaneUtils::HDLayoutActive() );
       
   316         batteryIconLayout =
       
   317             AknLayoutScalable_Avkon::battery_pane_g2( hdLayout ? 2 : 0 );
       
   318         batteryStrenghtLayout =
       
   319             AknLayoutScalable_Avkon::battery_pane_g1( hdLayout ? 2 : 0 );
       
   320         }
       
   321 
       
   322     AknLayoutUtils::LayoutControl(
       
   323         iBatteryIconControl, parent, batteryIconLayout );
       
   324 
       
   325     AknLayoutUtils::LayoutControl(
       
   326         iBatteryStrengthControl, parent, batteryStrenghtLayout );
       
   327 
       
   328     AknsUtils::RegisterControlPosition( this );
       
   329     }
       
   330 
       
   331 
       
   332 // ---------------------------------------------------------------------------
       
   333 // CAknBatteryPane::SizeChanged
       
   334 // Handles position change events.
       
   335 // ---------------------------------------------------------------------------
       
   336 //
       
   337 EXPORT_C void CAknBatteryPane::PositionChanged()
       
   338     {
       
   339     AknsUtils::RegisterControlPosition( this );
       
   340     }
       
   341 
       
   342 
       
   343 // ---------------------------------------------------------------------------
       
   344 // CAknBatteryPane::Draw
       
   345 // Draws the battery pane.
       
   346 // ---------------------------------------------------------------------------
       
   347 //
       
   348 EXPORT_C void CAknBatteryPane::Draw( const TRect& /*aRect*/ ) const
       
   349     {
       
   350     if ( iIsActiveIdle )
       
   351         {
       
   352         return;
       
   353         }
       
   354 
       
   355     if ( AknStatuspaneUtils::StaconPaneActive() )
       
   356         {
       
   357         DrawInStaconPane( Rect() );
       
   358         }
       
   359     else if ( AknStatuspaneUtils::FlatLayoutActive() )
       
   360         {
       
   361         DrawInFlatStatusPane( Rect() );
       
   362         }
       
   363     else
       
   364         {
       
   365         DrawInNormalStatusPane( Rect() );
       
   366         }
       
   367     }
       
   368 
       
   369 
       
   370 // ---------------------------------------------------------------------------
       
   371 // CAknBatteryPane::HandleResourceChange
       
   372 // Handles resource change events.
       
   373 // ---------------------------------------------------------------------------
       
   374 //
       
   375 EXPORT_C void CAknBatteryPane::HandleResourceChange( TInt aType )
       
   376     {
       
   377     CCoeControl::HandleResourceChange( aType );
       
   378 
       
   379     if ( aType == KEikDynamicLayoutVariantSwitch ||
       
   380          aType == KAknsMessageSkinChange )
       
   381         {
       
   382         DrawDeferred();
       
   383         }
       
   384     }
       
   385 
       
   386 
       
   387 // ---------------------------------------------------------------------------
       
   388 // CAknBatteryPane::CountComponentControls
       
   389 // Returns the amount of component controls.
       
   390 // ---------------------------------------------------------------------------
       
   391 //
       
   392 EXPORT_C TInt CAknBatteryPane::CountComponentControls() const
       
   393     {
       
   394     return 2;
       
   395     }
       
   396 
       
   397 
       
   398 // ---------------------------------------------------------------------------
       
   399 // CAknBatteryPane::ComponentControl
       
   400 // Returns a component control by a control index.
       
   401 // ---------------------------------------------------------------------------
       
   402 //
       
   403 EXPORT_C CCoeControl* CAknBatteryPane::ComponentControl( TInt aIndex ) const
       
   404     {
       
   405     CCoeControl* control = NULL;
       
   406 
       
   407     switch ( aIndex )
       
   408         {
       
   409         case 0:
       
   410             {
       
   411             control = iBatteryStrengthControl;
       
   412             break;
       
   413             }
       
   414         case 1:
       
   415             {
       
   416             control = iBatteryIconControl;
       
   417             break;
       
   418             }
       
   419         }
       
   420 
       
   421     return control;
       
   422     }
       
   423 
       
   424 
       
   425 // ---------------------------------------------------------------------------
       
   426 // CAknBatteryPane::DrawInNormalStatusPane
       
   427 // Draws the battery pane in normal status pane layout.
       
   428 // ---------------------------------------------------------------------------
       
   429 //
       
   430 void CAknBatteryPane::DrawInNormalStatusPane( const TRect& /*aRect*/ ) const
       
   431     {
       
   432     CWindowGc& gc = SystemGc();
       
   433     TRect rect( Rect() );
       
   434     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   435     MAknsControlContext* cc = AknsDrawUtils::ControlContext( this );
       
   436 
       
   437     // Solid or wipe comes from background
       
   438     if ( !AknsDrawUtils::Background( skin, cc, this, gc, rect ) )
       
   439         {
       
   440         // Default drawing if skinning is not available
       
   441         gc.Clear( rect );
       
   442         gc.SetPenStyle( CGraphicsContext::ENullPen );
       
   443         gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
       
   444         gc.SetBrushColor(
       
   445             AKN_LAF_COLOR( KStatusPaneBackgroundGraphicsColorUsual ) );
       
   446         gc.DrawRect( rect );
       
   447         }
       
   448     }
       
   449 
       
   450 
       
   451 // ---------------------------------------------------------------------------
       
   452 // CAknBatteryPane::DrawInStaconPane
       
   453 // Draws the battery pane in stacon pane layout.
       
   454 // ---------------------------------------------------------------------------
       
   455 //
       
   456 void CAknBatteryPane::DrawInStaconPane( const TRect& /*aRect*/ ) const
       
   457     {
       
   458     CWindowGc& gc = SystemGc();
       
   459     TRect rect( Rect() );
       
   460     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   461     MAknsControlContext* cc = AknsDrawUtils::ControlContext( this );
       
   462 
       
   463     if ( !AknsDrawUtils::Background( skin, cc, this, gc, rect ) )
       
   464         {
       
   465         gc.SetPenStyle( CGraphicsContext::ENullPen );
       
   466         gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
       
   467         gc.SetBrushColor(
       
   468             AKN_LAF_COLOR( KStatusPaneBackgroundGraphicsColorUsual ) );
       
   469         gc.DrawRect( rect );
       
   470         }
       
   471     }
       
   472 
       
   473 
       
   474 // ---------------------------------------------------------------------------
       
   475 // CAknBatteryPane::DrawInFlatStatusPane
       
   476 // Draws the battery pane in flat status pane layout.
       
   477 // ---------------------------------------------------------------------------
       
   478 //
       
   479 void CAknBatteryPane::DrawInFlatStatusPane( const TRect& /*aRect*/ ) const
       
   480     {
       
   481     CWindowGc& gc = SystemGc();
       
   482     TRect rect( Rect() );
       
   483     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   484     MAknsControlContext* cc = AknsDrawUtils::ControlContext( this );
       
   485 
       
   486     if ( !AknsDrawUtils::Background( skin, cc, this, gc, rect ) )
       
   487         {
       
   488         gc.SetPenStyle( CGraphicsContext::ENullPen );
       
   489         gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
       
   490         gc.SetBrushColor(
       
   491             AKN_LAF_COLOR( KStatusPaneBackgroundGraphicsColorUsual ) );
       
   492         gc.DrawRect( rect );
       
   493         }
       
   494     }
       
   495 
       
   496 
       
   497 // ---------------------------------------------------------------------------
       
   498 // CAknBatteryPane::ExtensionInterface
       
   499 // Not implemented.
       
   500 // ---------------------------------------------------------------------------
       
   501 //
       
   502 EXPORT_C void* CAknBatteryPane::ExtensionInterface( TUid /*aInterface*/ )
       
   503     {
       
   504     return NULL;
       
   505     }
       
   506 
       
   507 
       
   508 // ---------------------------------------------------------------------------
       
   509 // CAknBatteryPane::HandlePointerEventL
       
   510 // Processes battery pane's pointer events. Actually this does nothing yet.
       
   511 // ---------------------------------------------------------------------------
       
   512 //
       
   513 void CAknBatteryPane::HandlePointerEventL( const TPointerEvent& aPointerEvent )
       
   514     {
       
   515     if ( AknLayoutUtils::PenEnabled() )
       
   516         {
       
   517         if ( IsDimmed() )
       
   518             {
       
   519             iPrivateFlags &= (~EAknBatteryPaneButton1DownInBatteryRect);
       
   520             return;
       
   521             }
       
   522 
       
   523         // Get the rect of battery pane.
       
   524         TRect rect( Rect() );
       
   525 
       
   526         // Switch by type
       
   527         switch ( aPointerEvent.iType )
       
   528             {
       
   529             case TPointerEvent::EButton1Down:
       
   530                 {
       
   531                 // if battery pane's rect contains pointer down position
       
   532                 if ( rect.Contains( aPointerEvent.iPosition ) )
       
   533                     {
       
   534                     // set flag that pointerdown was inside battery pane
       
   535                     iPrivateFlags |= EAknBatteryPaneButton1DownInBatteryRect;
       
   536                     }
       
   537                 }
       
   538                 break;
       
   539 
       
   540             case TPointerEvent::EButton1Up:
       
   541                 {
       
   542                 // if battery pane's rect contains pointer down position and
       
   543                 // Button1Down was clicked inside battery rect
       
   544                 if ( iPrivateFlags&EAknBatteryPaneButton1DownInBatteryRect &&
       
   545                     rect.Contains( aPointerEvent.iPosition ) )
       
   546                     {
       
   547                     // Up happened inside battery pane's rect
       
   548                     // activate something
       
   549                     }
       
   550 
       
   551                 // Up happened, reset button down flag
       
   552                 iPrivateFlags &= (~EAknBatteryPaneButton1DownInBatteryRect);
       
   553                 }
       
   554                 break;
       
   555 
       
   556             default:
       
   557                 break;
       
   558             }
       
   559         }
       
   560     }
       
   561 
       
   562 
       
   563 // ---------------------------------------------------------------------------
       
   564 // CAknBatteryPane::SetContainerWindowNonFading
       
   565 // Allows/disallows fading of battery pane.
       
   566 // ---------------------------------------------------------------------------
       
   567 //
       
   568 void CAknBatteryPane::SetContainerWindowNonFading( TBool aNonFading )
       
   569     {
       
   570     CEikStatusPaneBase* statusPane = CEikStatusPaneBase::Current();
       
   571     if ( statusPane )
       
   572         {
       
   573         CCoeControl* control = NULL;
       
   574         TRAP_IGNORE( control = statusPane->ContainerControlL(
       
   575                                 TUid::Uid( EEikStatusPaneUidBattery ) ) );
       
   576         if ( control )
       
   577             {
       
   578             control->DrawableWindow()->SetNonFading( aNonFading );
       
   579             }
       
   580         }
       
   581     }
       
   582 
       
   583 //  End of File