browserplugins/browsersysteminfoplugin/src/PowerObserver.cpp
changeset 51 48e827313edd
parent 37 481242ead638
child 53 f427d27b98d8
equal deleted inserted replaced
37:481242ead638 51:48e827313edd
     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 the License "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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <limits.h>
       
    21 #include "PowerObserver.h"
       
    22 #include "SystemInfoPlugin.h"
       
    23 
       
    24 // ============================ MEMBER FUNCTIONS ===============================
       
    25 
       
    26 // -----------------------------------------------------------------------------
       
    27 // CPowerObserver::NewL
       
    28 // Two-phased constructor.
       
    29 // -----------------------------------------------------------------------------
       
    30 CPowerObserver* CPowerObserver::NewL( CSystemInfoPlugin* aPlugin )
       
    31     {
       
    32     CPowerObserver* self = new (ELeave) CPowerObserver( aPlugin );
       
    33     CleanupStack::PushL( self );
       
    34     self->ConstructL();
       
    35     CleanupStack::Pop( self );
       
    36     return self;
       
    37     }
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CPowerObserver::CPowerObserver
       
    41 // C++ default constructor can NOT contain any code, that
       
    42 // might leave.
       
    43 // -----------------------------------------------------------------------------
       
    44 CPowerObserver::CPowerObserver( CSystemInfoPlugin* aPlugin )
       
    45     : iSystemInfoPlugin( aPlugin ),
       
    46       iBatteryInfoPckg( iBatteryInfo ),
       
    47       iIndicatorPckg( iIndicator )
       
    48     {
       
    49     }
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // CPowerObserver::ConstructL
       
    53 // Symbian 2nd phase constructor can leave.
       
    54 // -----------------------------------------------------------------------------
       
    55 void CPowerObserver::ConstructL()
       
    56     {
       
    57     iBatteryInfoObserver = CSystemInfoObserver::NewL( this, EIdBatteryInfo );
       
    58     iIndicatorObserver = CSystemInfoObserver::NewL( this, EIdIndicator );
       
    59 
       
    60     iTelephony = CTelephony::NewL();
       
    61     iTelephony2 = CTelephony::NewL();
       
    62 
       
    63     // bootstrap monitoring
       
    64 
       
    65     // battery
       
    66     iBatteryInfo.iChargeLevel = 0; // default indicates unknown
       
    67     iBatteryInfoObserver->RequestNotificationL();
       
    68 
       
    69     // charger
       
    70     iIndicator.iIndicator = 0;
       
    71     iIndicatorObserver->RequestNotificationL();
       
    72     }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CPowerObserver::~CPowerObserver()
       
    76 // Destructor
       
    77 // -----------------------------------------------------------------------------
       
    78 CPowerObserver::~CPowerObserver()
       
    79     {
       
    80     delete iIndicatorObserver;
       
    81     delete iBatteryInfoObserver;
       
    82     delete iBatteryInfoChangedFunction;
       
    83     delete iIndicatorChangedFunction;
       
    84     delete iTelephony2;
       
    85     delete iTelephony;
       
    86     }
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // CPowerObserver::ChargeLevel
       
    90 // -----------------------------------------------------------------------------
       
    91 TInt CPowerObserver::ChargeLevel() const
       
    92     {
       
    93     if ( !iBatteryInfoStarted )
       
    94         {
       
    95         // value indicates unknown
       
    96         return INT_MIN;
       
    97         }
       
    98     return STATIC_CAST( TInt, iBatteryInfo.iChargeLevel );
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // CPowerObserver::ChargerConnected
       
   103 // -----------------------------------------------------------------------------
       
   104 TInt CPowerObserver::ChargerConnected() const
       
   105     {
       
   106     if ( !iIndicatorStarted )
       
   107         {
       
   108         // value indicates unknown
       
   109         return INT_MIN;
       
   110         }
       
   111     return ( CTelephony::KIndChargerConnected & iIndicator.iIndicator ) ? 1 : 0;
       
   112     }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CPowerObserver::RequestNotificationL
       
   116 // -----------------------------------------------------------------------------
       
   117 void CPowerObserver::RequestNotificationL( TIdPowerItem aId, HBufC8* aFunctionName )
       
   118     {
       
   119     if ( aId == EIdBatteryInfo )
       
   120         {
       
   121         delete iBatteryInfoChangedFunction;
       
   122         iBatteryInfoChangedFunction = aFunctionName;
       
   123         // already monitoring, don't request notification again
       
   124         }
       
   125     else if ( aId == EIdIndicator )
       
   126         {
       
   127         delete iIndicatorChangedFunction;
       
   128         iIndicatorChangedFunction = aFunctionName;
       
   129         // already monitoring, don't request notification again
       
   130         }
       
   131     }
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CPowerObserver::SubscribeL
       
   135 // -----------------------------------------------------------------------------
       
   136 void CPowerObserver::SubscribeL( TInt aId, TRequestStatus& aStatus )
       
   137     {
       
   138     if ( aId == EIdBatteryInfo )
       
   139         {
       
   140         if ( iBatteryInfoStarted )
       
   141             {
       
   142             iTelephony->NotifyChange( aStatus,
       
   143                                       CTelephony::EBatteryInfoChange,
       
   144                                       iBatteryInfoPckg );
       
   145             }
       
   146         else
       
   147             {
       
   148             iTelephony->GetBatteryInfo( aStatus, iBatteryInfoPckg );
       
   149             }
       
   150         }
       
   151     else if ( aId == EIdIndicator )
       
   152         {
       
   153         if ( iIndicatorStarted )
       
   154             {
       
   155             iTelephony->NotifyChange( aStatus,
       
   156                                       CTelephony::EIndicatorChange,
       
   157                                       iIndicatorPckg );
       
   158             }
       
   159         else
       
   160             {
       
   161             iTelephony->GetIndicator( aStatus, iIndicatorPckg );
       
   162             }
       
   163         }
       
   164     }
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // CPowerObserver::UpdateL
       
   168 // -----------------------------------------------------------------------------
       
   169 void CPowerObserver::UpdateL( TInt aId )
       
   170     {
       
   171     if ( aId == EIdBatteryInfo )
       
   172         {
       
   173         iBatteryInfoStarted = 1;
       
   174         if ( iBatteryInfoChangedFunction )
       
   175             {
       
   176             iSystemInfoPlugin->InvokeCallback( *iBatteryInfoChangedFunction, NULL, 0 );
       
   177             }
       
   178         }
       
   179     else if ( aId == EIdIndicator )
       
   180         {
       
   181         iIndicatorStarted = 1;
       
   182         if ( iIndicatorChangedFunction )
       
   183             {
       
   184             iSystemInfoPlugin->InvokeCallback( *iIndicatorChangedFunction, NULL, 0 );
       
   185             }
       
   186         }
       
   187     }
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CPowerObserver::Cancel
       
   191 // -----------------------------------------------------------------------------
       
   192 void CPowerObserver::Cancel( TInt aId )
       
   193     {
       
   194     if ( aId == EIdBatteryInfo )
       
   195         {
       
   196         (void) iTelephony->CancelAsync( ( iBatteryInfoStarted ?
       
   197                                           CTelephony::EBatteryInfoChangeCancel
       
   198                                           : CTelephony::EGetBatteryInfoCancel ) );
       
   199         }
       
   200     else if ( aId == EIdIndicator )
       
   201         {
       
   202         (void) iTelephony->CancelAsync( ( iIndicatorStarted ?
       
   203                                           CTelephony::EIndicatorChangeCancel
       
   204                                           : CTelephony::EGetIndicatorCancel ) );
       
   205         }
       
   206     }
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // CPowerObserver::Close
       
   210 // -----------------------------------------------------------------------------
       
   211 void CPowerObserver::Close( TInt /*aId*/ )
       
   212     {
       
   213     }
       
   214 
       
   215 //  End of File