examples/Telephony/ETel3rdPartyExample/PhoneMonitoring/CBatteryInfo.cpp

00001 /*
00002 Copyright (c) 2005-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */
00029 
00030 
00031 
00032 #include "CBatteryInfo.h"
00033 
00041 CBatteryInfo* CBatteryInfo::NewL(MExecAsync* aController)
00042         {
00043         CBatteryInfo* self = new(ELeave) CBatteryInfo(aController);
00044         CleanupStack::PushL(self);
00045         self->ConstructL();
00046         CleanupStack::Pop(self);
00047         return self;
00048         }
00049 
00054 CBatteryInfo::~CBatteryInfo()
00055         {
00056         Cancel();
00057         }
00058 
00062 void CBatteryInfo::DoStartRequestL()
00063         {
00064         _LIT( KNotifyPanic, "CBatteryInfo Get Method" );
00065         __ASSERT_ALWAYS( !IsActive(), User::Panic( KNotifyPanic, 1 ));
00066         iRequestNotify = EFalse;
00067         
00068         // Retrieves the status and charge level of the phone battery.
00069         iTelephony->GetBatteryInfo(iStatus, iBatteryInfoV1Pckg);
00070         SetActive();
00071         }
00072 
00079 CBatteryInfo::CBatteryInfo(MExecAsync* aController)
00080         : CISVAPIAsync(aController, KBatteryInfo),
00081           iBatteryInfoV1Pckg(iBatteryInfoV1)
00082         {
00083         // Empty method
00084         }
00085 
00089 void CBatteryInfo::ConstructL()
00090         {
00091         // Empty method
00092         }
00093 
00098 void CBatteryInfo::RunL()
00099         {
00100         CTelephony::TBatteryInfoV1 BatteryInfo = iBatteryInfoV1;
00101 
00102         if(iStatus != KErrNone)
00103                 {
00104                 iConsole->Printf(KError);
00105                 
00106                 // Print the error status code
00107                 iConsole->Printf(_L("%d\n"), iStatus.Int());
00108                 }
00109         else
00110                 {
00111                 // Print Battery Info
00112                 CTelephony::TBatteryStatus batteryStatus = BatteryInfo.iStatus;
00113                 TUint chargeLevel = BatteryInfo.iChargeLevel;
00114                 if (iRequestNotify)
00115                         {
00116                         iConsole->ClearScreen();
00117                         iConsole->Printf(_L("~*THIS IS A NOTIFICATION*~\n"));
00118                         }
00119                 iConsole->Printf(KBatteryInfoMsg);
00120                 iConsole->Printf(_L("Charge is %d\n"), chargeLevel);
00121                 switch (batteryStatus)
00122                         {
00123                 case CTelephony::EPowerStatusUnknown:
00124                         iConsole->Printf(_L("EPowerStatusUnknown %d\n"), batteryStatus);
00125                         break;
00126                 case CTelephony::EPoweredByBattery:
00127                         iConsole->Printf(_L("EPoweredByBattery %d\n"), batteryStatus);
00128                         break;
00129                 case CTelephony::EBatteryConnectedButExternallyPowered:
00130                         iConsole->Printf(_L("EBatteryConnectedButExternallyPowered %d\n"), batteryStatus);
00131                         break;
00132                 case CTelephony::ENoBatteryConnected:
00133                         iConsole->Printf(_L("ENoBatteryConnected %d\n"), batteryStatus);
00134                         break;
00135                 case CTelephony::EPowerFault:
00136                         iConsole->Printf(_L("EPowerFault %d\n"), batteryStatus);
00137                         break;
00138                 default:
00139                         iConsole->Printf(KError);
00140                         }
00141                 }
00142                 if (iRequestNotify)
00143                         {
00144                         DoRequestNotificationL();
00145                         }
00146                 else
00147                         {
00148                         ExampleComplete();
00149                         }
00150         }
00151 
00155 void CBatteryInfo::DoRequestNotificationL()
00156         {
00157         // Panics if this object is already performing an asynchronous operation.
00158         // Application will panic if you call SetActive() on an already active object.
00159         _LIT( KNotifyPanic, "CBatteryInfo Notify Method" );
00160         __ASSERT_ALWAYS( !IsActive(), User::Panic( KNotifyPanic, 1 ));
00161         iRequestNotify = ETrue;
00162         
00163         // Registers interest in receiving change notifications for events.
00164         iTelephony->NotifyChange(       iStatus,
00165                                                                 CTelephony::EBatteryInfoChange,
00166                                                                 iBatteryInfoV1Pckg );
00167         SetActive();
00168         }
00169 
00173 void CBatteryInfo::DoCancel()
00174         {
00175         // Cancels an outstanding asynchronous request.
00176         iTelephony->CancelAsync(CTelephony::EBatteryInfoChangeCancel);
00177         }
00178 

Generated by  doxygen 1.6.2