videofeeds/scheduleddlplugin/src/iptvbatterywatcher.cpp
branchRCL_3
changeset 57 befca0ec475f
parent 0 96612d01cf9f
equal deleted inserted replaced
56:839377eedc2b 57:befca0ec475f
       
     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:    Publish & Subscribe watcher client for battery values.*
       
    15 */
       
    16 
       
    17 
       
    18 
       
    19 
       
    20 #include <hwrmpowerstatesdkpskeys.h>
       
    21 #include "IptvDebug.h"
       
    22 
       
    23 #include "iptvbatterywatcher.h"
       
    24 #include "iptvbatterywatcherobserver.h"
       
    25 
       
    26 
       
    27 
       
    28 // ---------------------------------------------------------------------------
       
    29 // CIptvBatteryWatcher::CIptvBatteryWatcher
       
    30 // ---------------------------------------------------------------------------
       
    31 //
       
    32 CIptvBatteryWatcher::CIptvBatteryWatcher() : CActive( EPriorityNormal )
       
    33     {
       
    34     }
       
    35 
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // CIptvBatteryWatcher::ConstructL
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 void CIptvBatteryWatcher::ConstructL()
       
    42     {
       
    43     User::LeaveIfError( iProperty.Attach(
       
    44             KPSUidHWRMPowerState,
       
    45             KHWRMBatteryStatus,
       
    46             EOwnerThread ) );
       
    47 
       
    48     CActiveScheduler::Add(this);
       
    49     // Do initial subscription and process current property value
       
    50     RunL();
       
    51     }
       
    52 
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // CIptvBatteryWatcher::NewL
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 CIptvBatteryWatcher* CIptvBatteryWatcher::NewL()
       
    59     {
       
    60     CIptvBatteryWatcher* self = CIptvBatteryWatcher::NewLC();
       
    61     CleanupStack::Pop( self );
       
    62     return self;
       
    63     }
       
    64 
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // CIptvBatteryWatcher::NewLC
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 CIptvBatteryWatcher* CIptvBatteryWatcher::NewLC()
       
    71     {
       
    72     CIptvBatteryWatcher* self = new( ELeave ) CIptvBatteryWatcher;
       
    73     CleanupStack::PushL( self );
       
    74     self->ConstructL();
       
    75     return self;
       
    76     }
       
    77 
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // CIptvBatteryWatcher::~CIptvBatteryWatcher
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 CIptvBatteryWatcher::~CIptvBatteryWatcher()
       
    84     {
       
    85     Cancel();
       
    86     iProperty.Close();
       
    87     iObservers.Reset();
       
    88     }
       
    89 
       
    90 
       
    91 // ---------------------------------------------------------------------------
       
    92 // CIptvBatteryWatcher::GetBatteryStatus
       
    93 // ---------------------------------------------------------------------------
       
    94 //
       
    95 TInt CIptvBatteryWatcher::GetBatteryStatus( TInt& aBatteryStatus )
       
    96     {
       
    97     IPTVLOGSTRING_HIGH_LEVEL(">>> CIptvBatteryWatcher::GetBatteryStatus");
       
    98 
       
    99     RProperty batteryProperty;
       
   100 
       
   101     TInt err = batteryProperty.Attach(
       
   102             KPSUidHWRMPowerState,
       
   103             KHWRMBatteryStatus,
       
   104             EOwnerThread );
       
   105 
       
   106     if (err == KErrNone)
       
   107         {
       
   108         err = batteryProperty.Get( aBatteryStatus );
       
   109         }
       
   110     IPTVLOGSTRING2_HIGH_LEVEL("CIptvBatteryWatcher::GetBatteryStatus %d", aBatteryStatus);
       
   111     IPTVLOGSTRING_HIGH_LEVEL("<<< CIptvBatteryWatcher::GetBatteryStatus");
       
   112 
       
   113     batteryProperty.Close();
       
   114 
       
   115     return err;
       
   116     }
       
   117 
       
   118 
       
   119 // ---------------------------------------------------------------------------
       
   120 // CIptvBatteryWatcher::GetChargerStatus
       
   121 // ---------------------------------------------------------------------------
       
   122 //
       
   123 TInt CIptvBatteryWatcher::GetChargerStatus( TInt& aChargerStatus )
       
   124     {
       
   125     IPTVLOGSTRING_HIGH_LEVEL(">>> CIptvBatteryWatcher::GetChargerStatus");
       
   126 
       
   127     RProperty chargingProperty;
       
   128 
       
   129     TInt err = chargingProperty.Attach(
       
   130             KPSUidHWRMPowerState,
       
   131             KHWRMChargingStatus,
       
   132             EOwnerThread );
       
   133 
       
   134     if (err == KErrNone)
       
   135         {
       
   136         err = chargingProperty.Get( aChargerStatus );
       
   137         }
       
   138 
       
   139     IPTVLOGSTRING2_HIGH_LEVEL("CIptvBatteryWatcher::GetChargerStatus %d", aChargerStatus);
       
   140     IPTVLOGSTRING_HIGH_LEVEL("<<< CIptvBatteryWatcher::GetChargerStatus");
       
   141 
       
   142     chargingProperty.Close();
       
   143 
       
   144     return err;
       
   145     }
       
   146 
       
   147 
       
   148 // ---------------------------------------------------------------------------
       
   149 // CIptvBatteryWatcher::IsLowPower
       
   150 // ---------------------------------------------------------------------------
       
   151 //
       
   152 TBool CIptvBatteryWatcher::IsLowPower()
       
   153     {
       
   154     IPTVLOGSTRING_HIGH_LEVEL(">>> CIptvBatteryWatcher::IsLowPower");
       
   155 
       
   156     TInt batteryStatus = EBatteryStatusUnknown;
       
   157     TBool lowBattery = EFalse;
       
   158 
       
   159     // Try to get the battery status
       
   160     GetBatteryStatus(batteryStatus);
       
   161 
       
   162     // If it is not ok or we didn't get it, do additional stuff
       
   163     if (batteryStatus != EBatteryStatusOk)
       
   164         {
       
   165         // Assume low or empty battery status
       
   166         // In empty case just inform observers
       
   167         lowBattery = ETrue;
       
   168         if (batteryStatus == EBatteryStatusLow)
       
   169             {
       
   170             //  Low battery, check the charging state also
       
   171             //  EBatteryStatusLow status is send multiple times
       
   172             TInt chargingStatus = EChargingStatusNotConnected;
       
   173 
       
   174             if (GetChargerStatus( chargingStatus ) == KErrNone)
       
   175                 {
       
   176                 switch (chargingStatus)
       
   177                     {
       
   178                     case EChargingStatusCharging:
       
   179                     case EChargingStatusAlmostComplete:
       
   180                     case EChargingStatusChargingComplete:
       
   181                     case EChargingStatusChargingContinued:
       
   182                         {
       
   183                         lowBattery = EFalse;
       
   184                         }
       
   185                         break;
       
   186 
       
   187                     default:
       
   188                         {
       
   189                         lowBattery = ETrue;
       
   190                         }
       
   191                         break;
       
   192                     }
       
   193                 }
       
   194             }
       
   195         }
       
   196 
       
   197     IPTVLOGSTRING2_HIGH_LEVEL("CIptvBatteryWatcher::IsLowPower %d", lowBattery);
       
   198     IPTVLOGSTRING_HIGH_LEVEL("<<< CIptvBatteryWatcher::IsLowPower");
       
   199 
       
   200     return lowBattery;
       
   201     }
       
   202 
       
   203 
       
   204 // ---------------------------------------------------------------------------
       
   205 // CIptvBatteryWatcher::AddObserverL
       
   206 // ---------------------------------------------------------------------------
       
   207 //
       
   208 void CIptvBatteryWatcher::AddObserverL( MBatteryWatcherObserver* aObserver )
       
   209     {
       
   210     iObservers.AppendL( aObserver );
       
   211     }
       
   212 
       
   213 void CIptvBatteryWatcher::RemoveObserver( MBatteryWatcherObserver* aObserver )
       
   214     {
       
   215     TInt index = iObservers.Find( aObserver );
       
   216     if (index != KErrNotFound)
       
   217         {
       
   218         iObservers.Remove( index );
       
   219         }
       
   220     }
       
   221 
       
   222 
       
   223 // ---------------------------------------------------------------------------
       
   224 // CIptvBatteryWatcher::RunL
       
   225 //
       
   226 // From class CActive.
       
   227 // ---------------------------------------------------------------------------
       
   228 //
       
   229 void CIptvBatteryWatcher::RunL()
       
   230     {
       
   231     IPTVLOGSTRING_HIGH_LEVEL(">>> CIptvBatteryWatcher::RunL");
       
   232 
       
   233     // Resubscribe before processing new value to prevent missing updates
       
   234     iProperty.Subscribe( iStatus );
       
   235     SetActive();
       
   236 
       
   237     TBool lowBattery = IsLowPower();
       
   238 
       
   239     if (lowBattery)
       
   240         {
       
   241         IPTVLOGSTRING_HIGH_LEVEL("CIptvBatteryWatcher::RunL low battery and no charger");
       
   242 
       
   243         for (TInt i = 0; i < iObservers.Count(); i++)
       
   244             {
       
   245             iObservers[i]->BatteryLow();
       
   246             }
       
   247         }
       
   248 
       
   249     IPTVLOGSTRING_HIGH_LEVEL("<<< CIptvBatteryWatcher::RunL");
       
   250     }
       
   251 
       
   252 // ---------------------------------------------------------------------------
       
   253 // CIptvBatteryWatcher::DoCancel
       
   254 // 
       
   255 // From class CActive.
       
   256 // ---------------------------------------------------------------------------
       
   257 //
       
   258 void CIptvBatteryWatcher::DoCancel()
       
   259     {
       
   260     iProperty.Cancel();
       
   261     }
       
   262