telephonyutils/etel3rdpartyapi/ExampleApps/PhoneMonitoring/CBatteryInfo.cpp
changeset 0 3553901f7fa8
equal deleted inserted replaced
-1:000000000000 0:3553901f7fa8
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 #include "CBatteryInfo.h"
       
    18 
       
    19 /**
       
    20 Factory constructor.
       
    21 
       
    22 @param  aController Pointer to MExecAsync object passed to constructor of 
       
    23                     CISVAPIBase
       
    24 @return             Instance of CBatteryInfo class
       
    25 */
       
    26 CBatteryInfo* CBatteryInfo::NewL(MExecAsync* aController)
       
    27 	{
       
    28 	CBatteryInfo* self = new(ELeave) CBatteryInfo(aController);
       
    29 	CleanupStack::PushL(self);
       
    30 	self->ConstructL();
       
    31 	CleanupStack::Pop(self);
       
    32 	return self;
       
    33 	}
       
    34 
       
    35 /**
       
    36 Destructor.
       
    37 Cancels outstanding requests.
       
    38 */
       
    39 CBatteryInfo::~CBatteryInfo()
       
    40 	{
       
    41 	Cancel();
       
    42 	}
       
    43 
       
    44 /**
       
    45 Gets battery information and stores it in the iBatteryInfoV1Pckg.
       
    46 */
       
    47 void CBatteryInfo::DoStartRequestL()
       
    48 	{
       
    49 	_LIT( KNotifyPanic, "CBatteryInfo Get Method" );
       
    50 	__ASSERT_ALWAYS( !IsActive(), User::Panic( KNotifyPanic, 1 ));
       
    51 	iRequestNotify = EFalse;
       
    52 	
       
    53 	// Retrieves the status and charge level of the phone battery.
       
    54 	iTelephony->GetBatteryInfo(iStatus, iBatteryInfoV1Pckg);
       
    55 	SetActive();
       
    56 	}
       
    57 
       
    58 /**
       
    59 Constructor.
       
    60 
       
    61 @param aController Pointer to an MExecAsync object passed to constructor of 
       
    62                    CISVAPIBase
       
    63 */
       
    64 CBatteryInfo::CBatteryInfo(MExecAsync* aController)
       
    65 	: CISVAPIAsync(aController, KBatteryInfo),
       
    66 	  iBatteryInfoV1Pckg(iBatteryInfoV1)
       
    67 	{
       
    68 	// Empty method
       
    69 	}
       
    70 
       
    71 /**
       
    72 Second phase constructor.
       
    73 */
       
    74 void CBatteryInfo::ConstructL()
       
    75 	{
       
    76 	// Empty method
       
    77 	}
       
    78 
       
    79 /**
       
    80 Checks the status of the active object and prints the battery information to
       
    81 the console if there is no error.
       
    82 */
       
    83 void CBatteryInfo::RunL()
       
    84 	{
       
    85 	CTelephony::TBatteryInfoV1 BatteryInfo = iBatteryInfoV1;
       
    86 
       
    87 	if(iStatus != KErrNone)
       
    88 		{
       
    89 		iConsole->Printf(KError);
       
    90 		
       
    91 		// Print the error status code
       
    92 		iConsole->Printf(_L("%d\n"), iStatus.Int());
       
    93 		}
       
    94 	else
       
    95 		{
       
    96 		// Print Battery Info
       
    97 		CTelephony::TBatteryStatus batteryStatus = BatteryInfo.iStatus;
       
    98 		TUint chargeLevel = BatteryInfo.iChargeLevel;
       
    99 		if (iRequestNotify)
       
   100 			{
       
   101 			iConsole->ClearScreen();
       
   102 			iConsole->Printf(_L("~*THIS IS A NOTIFICATION*~\n"));
       
   103 			}
       
   104 		iConsole->Printf(KBatteryInfoMsg);
       
   105 		iConsole->Printf(_L("Charge is %d\n"), chargeLevel);
       
   106 		switch (batteryStatus)
       
   107 			{
       
   108 		case CTelephony::EPowerStatusUnknown:
       
   109 			iConsole->Printf(_L("EPowerStatusUnknown %d\n"), batteryStatus);
       
   110 			break;
       
   111 		case CTelephony::EPoweredByBattery:
       
   112 			iConsole->Printf(_L("EPoweredByBattery %d\n"), batteryStatus);
       
   113 			break;
       
   114 		case CTelephony::EBatteryConnectedButExternallyPowered:
       
   115 			iConsole->Printf(_L("EBatteryConnectedButExternallyPowered %d\n"), batteryStatus);
       
   116 			break;
       
   117 		case CTelephony::ENoBatteryConnected:
       
   118 			iConsole->Printf(_L("ENoBatteryConnected %d\n"), batteryStatus);
       
   119 			break;
       
   120 		case CTelephony::EPowerFault:
       
   121 			iConsole->Printf(_L("EPowerFault %d\n"), batteryStatus);
       
   122 			break;
       
   123 		default:
       
   124 			iConsole->Printf(KError);
       
   125 			}
       
   126 		}
       
   127 		if (iRequestNotify)
       
   128 			{
       
   129 			DoRequestNotificationL();
       
   130 			}
       
   131 		else
       
   132 			{
       
   133 			ExampleComplete();
       
   134 			}
       
   135 	}
       
   136 
       
   137 /**
       
   138 Requests to receive notifications of change in the battery information.
       
   139 */
       
   140 void CBatteryInfo::DoRequestNotificationL()
       
   141 	{
       
   142 	// Panics if this object is already performing an asynchronous operation.
       
   143 	// Application will panic if you call SetActive() on an already active object.
       
   144 	_LIT( KNotifyPanic, "CBatteryInfo Notify Method" );
       
   145 	__ASSERT_ALWAYS( !IsActive(), User::Panic( KNotifyPanic, 1 ));
       
   146 	iRequestNotify = ETrue;
       
   147 	
       
   148 	// Registers interest in receiving change notifications for events.
       
   149 	iTelephony->NotifyChange(	iStatus,
       
   150 								CTelephony::EBatteryInfoChange,
       
   151 								iBatteryInfoV1Pckg );
       
   152 	SetActive();
       
   153 	}
       
   154 
       
   155 /**
       
   156 Cancels asynchronous request to CTelephony::GetBatteryInfo().
       
   157 */
       
   158 void CBatteryInfo::DoCancel()
       
   159 	{
       
   160 	// Cancels an outstanding asynchronous request.
       
   161 	TInt error = iTelephony->CancelAsync(CTelephony::EBatteryInfoChangeCancel);
       
   162 	}
       
   163