examples/ForumNokia/BluetoothPMPExample/src/BluetoothPMPExampleAppUi.cpp

00001 /*
00002  * Copyright © 2009 Nokia Corporation.
00003  */
00004 
00005 // INCLUDE FILES
00006 #include <avkon.hrh>
00007 #include <eikmenup.h>
00008 #include <AknGlobalNote.h>  //avkon.lib, aknnotify.lib
00009 #include <BtPmpEx.rsg>
00010 #include <aknutils.h>
00011 
00012 #include "BluetoothPMPExampleAppUi.h"
00013 #include "bluetoothpmpexample.hrh"
00014 
00015 #include "BluetoothPMPExampleEngine.h"
00016 #include "BluetoothPMPExampleRTEContainer.h"
00017 
00018 
00019 // ----------------------------------------------------------------------------
00020 // CBluetoothExampleAppUi::ConstructL()
00021 //
00022 // standard EPOC 2nd phase constructor
00023 // ----------------------------------------------------------------------------
00024 void CBluetoothPMPExampleAppUi::ConstructL()
00025     {
00026     BaseConstructL(EAknEnableSkin);
00027 
00028     #ifdef __WINS__
00029         //in emulator do nothing
00030         _LIT(KEmulatorWarning,"This application should be tested on device or use S60bt driver on Emulator");
00031         CAknGlobalNote* note = CAknGlobalNote::NewLC();
00032         note->ShowNoteL(EAknGlobalConfirmationNote,  KEmulatorWarning);
00033         CleanupStack::PopAndDestroy(note);
00034     #endif
00035 
00036     //Create container first so engine can display messages right away
00037     TRect rect;
00038     AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane,rect);
00039     iContainer = CBluetoothPMPExampleRTEContainer::NewL(rect);
00040     iContainer->SetMopParent( this );
00041 
00042     // Reset UI to init position
00043     ResetUiL();
00044 
00045     // Create engine
00046     iEngine = CBluetoothPMPExampleEngine::NewL( *this );
00047     iEngine->TurnBtOnL();
00048 
00049     // add view to control stack
00050     AddToStackL( iContainer );
00051     }
00052 
00053 void CBluetoothPMPExampleAppUi::ResetUiL()
00054     {
00055     TBuf<KMaxTimeFormatSpec+KMaxTimeFormatSpec> time;
00056     TTime now;
00057     now.HomeTime(); // Current time
00058     now.FormatL(time,TTimeFormatSpec());
00059     iContainer->ClearScreenL();
00060     iContainer->DrawTextL(KInfo);
00061     iContainer->DrawTextL(time);
00062     iContainer->AddCarriageReturnL();
00063     }
00064 
00065 
00066 // ----------------------------------------------------------------------------
00067 // CBluetoothPMPExampleAppUi::~CBluetoothPMPExampleAppUi()
00068 //
00069 // destructor
00070 // ----------------------------------------------------------------------------
00071 CBluetoothPMPExampleAppUi::~CBluetoothPMPExampleAppUi()
00072     {
00073     RemoveFromStack( iContainer );
00074 
00075     delete iContainer;
00076     iContainer = NULL;
00077 
00078     delete iEngine;
00079     iEngine=NULL;
00080     }
00081 
00082 // ----------------------------------------------------------------------------
00083 // CBluetoothPMPExampleAppUi::DynInitMenuPaneL(TInt aResourceId,
00084 //     CEikMenuPane* aMenuPane)
00085 //
00086 // this function is called by the EIKON framework just before it displays
00087 // a menu pane. Its default implementation is empty, and by overriding it,
00088 // the application can set the state of menu items dynamically according
00089 // to the state of application data.
00090 // ----------------------------------------------------------------------------
00091 void CBluetoothPMPExampleAppUi::DynInitMenuPaneL(TInt aResourceId,
00092                                                  CEikMenuPane* aMenuPane)
00093     {
00094     if (aResourceId == R_BLUETOOTHPMPEXAMPLE_MENU)
00095         {
00096         TBool myActiveDiscovery( iEngine->IsDiscoveryActive()  );
00097 
00098         // Dimm all menu items
00099         aMenuPane->SetItemDimmed(EBTCmdSendMessage, ETrue);
00100         aMenuPane->SetItemDimmed(EBTCmdStartSlave, ETrue);
00101         aMenuPane->SetItemDimmed(EBTCmdDiscoverDevices, ETrue);
00102         aMenuPane->SetItemDimmed(EBTCmdStopDiscovery, ETrue);
00103         aMenuPane->SetItemDimmed(EBTCmdDiscoverServices, ETrue);
00104         aMenuPane->SetItemDimmed(EBTCmdConnectDevices, ETrue);
00105         aMenuPane->SetItemDimmed(EBTCmdDisconnect, ETrue);
00106         aMenuPane->SetItemDimmed(EBTCmdDisconnectDevices, ETrue);
00107         aMenuPane->SetItemDimmed(EBtCmdBluetoothSetLimitedDiscoverableStatusOn, ETrue);
00108         aMenuPane->SetItemDimmed(EBtCmdBluetoothSetLimitedDiscoverableStatusOff, ETrue);
00109         aMenuPane->SetItemDimmed(EBTCmdShowConnectedDevices, ETrue);
00110         
00111         // Slave Mode =========================================================
00112         if (iEngine->iIsSlave)
00113             {
00114             if ( iEngine->iListener->IsConnected() )
00115                 {
00116                 aMenuPane->SetItemDimmed(EBTCmdSendMessage, EFalse);
00117                 }
00118             aMenuPane->SetItemDimmed(EBTCmdDisconnect, EFalse);
00119             }
00120         // Master Mode ========================================================
00121         else if (iEngine->iIsMaster)
00122             {
00123             aMenuPane->SetItemDimmed(EBTCmdStopDiscovery, !myActiveDiscovery );
00124             aMenuPane->SetItemDimmed(EBTCmdDiscoverDevices, myActiveDiscovery );
00125             if ( iEngine->iDeviceDiscoverer->HasDevices() && !iEngine->iDeviceDiscoverer->IsActive())
00126                 {
00127                 // we have found devices, show discover services item
00128                 aMenuPane->SetItemDimmed(EBTCmdDiscoverServices, EFalse);
00129                 }
00130             if ( iEngine->iServiceDiscoverer->HasServices() && !iEngine->iServiceDiscoverer->iRunning)
00131                 {
00132                 // we have found services, show connect item
00133                 aMenuPane->SetItemDimmed(EBTCmdConnectDevices, EFalse);
00134                 }
00135             if ( iEngine->HasConnections() )
00136                 {
00137                 // we are connected
00138                 aMenuPane->SetItemDimmed(EBTCmdSendMessage, EFalse);
00139                 aMenuPane->SetItemDimmed(EBTCmdShowConnectedDevices, EFalse);
00140                 aMenuPane->SetItemDimmed(EBTCmdDisconnectDevices, EFalse);
00141 
00142                 aMenuPane->SetItemDimmed(EBTCmdConnectDevices, ETrue);
00143                 }
00144             }
00145         // Init state =========================================================
00146         else
00147             {
00148             aMenuPane->SetItemDimmed(EBTCmdStartSlave, EFalse);
00149             aMenuPane->SetItemDimmed(EBTCmdDiscoverDevices, EFalse);
00150             }
00151         
00152         // LIAC ===============================================================
00153         #ifdef ENABLE_LIAC
00154         // If device discovering is ongoing do not allow to change LIAC state
00155         if (myActiveDiscovery)
00156             {
00157             aMenuPane->SetItemDimmed(
00158                     EBtCmdBluetoothSetLimitedDiscoverableStatusOn, ETrue );
00159             aMenuPane->SetItemDimmed(
00160                     EBtCmdBluetoothSetLimitedDiscoverableStatusOff, ETrue );
00161             }
00162         else
00163             {
00164             TBool myLIAC( iEngine->LIAC() );
00165             aMenuPane->SetItemDimmed(
00166                     EBtCmdBluetoothSetLimitedDiscoverableStatusOn, myLIAC);
00167             aMenuPane->SetItemDimmed(
00168                     EBtCmdBluetoothSetLimitedDiscoverableStatusOff, !myLIAC);
00169             }
00170         #else
00171         // LIAC is not supported on S60 1st or 2nd editions
00172         aMenuPane->SetItemDimmed(
00173             EBtCmdBluetoothSetLimitedDiscoverableStatusOn, ETrue );
00174         aMenuPane->SetItemDimmed(
00175             EBtCmdBluetoothSetLimitedDiscoverableStatusOff, ETrue );
00176         #endif
00177 
00178         
00179         
00180         }
00181     }
00182 
00183 
00184 
00185 // ----------------------------------------------------------------------------
00186 // CBluetoothPMPExampleAppUi::HandleKeyEventL(
00187 //     const TKeyEvent& aKeyEvent,TEventCode /*aType*/)
00188 //
00189 // takes care of key event handling
00190 // ----------------------------------------------------------------------------
00191 TKeyResponse CBluetoothPMPExampleAppUi::HandleKeyEventL(
00192     const TKeyEvent& /*aKeyEvent*/,TEventCode /*aType*/)
00193     {
00194     return EKeyWasNotConsumed;
00195     }
00196 
00197 
00198 // ----------------------------------------------------------------------------
00199 // CBluetoothPMPExampleAppUi::HandleCommandL(TInt aCommand)
00200 //
00201 // takes care of command handling
00202 // ----------------------------------------------------------------------------
00203 void CBluetoothPMPExampleAppUi::HandleCommandL(TInt aCommand)
00204     {
00205     switch ( aCommand )
00206         {
00207         case EEikCmdExit:
00208         case EAknSoftkeyExit:
00209         case EAknCmdExit:
00210         case EAknSoftkeyBack:
00211             {
00212             iEngine->StopDiscovery();
00213             Exit();
00214             break;
00215             }
00216         case EBTCmdStartSlave:
00217             {
00218             if ( !iEngine->iIsSlave )
00219                 iEngine->StartSlaveL();
00220             break;
00221             }
00222         case EBTCmdDiscoverDevices:
00223             {
00224             if ( !iEngine->iIsSlave )
00225                 iEngine->DiscoverDevicesL();
00226             break;
00227             }
00228         case EBTCmdDiscoverServices:
00229             {
00230             if ( !iEngine->iIsSlave )
00231                 iEngine->DiscoverServicesL();
00232             break;
00233             }
00234         case EBTCmdConnectDevices:
00235             {
00236             if ( !iEngine->iIsSlave )
00237                 iEngine->ConnectDevicesL();
00238             break;
00239             }
00240         case EBTCmdDisconnect:
00241             {
00242             if ( iEngine->iIsSlave )
00243                 {
00244                 iEngine->StopSlaveL();
00245                 ResetUiL();
00246                 }
00247             break;
00248             }
00249         case EBTCmdDisconnectDevices:
00250             {
00251             if ( !iEngine->iIsSlave )
00252                 iEngine->DisconnectDevices();
00253             break;
00254             }
00255         case EBTCmdShowConnectedDevices:
00256             {
00257             if ( !iEngine->iIsSlave )
00258                 iEngine->ShowConnectedDevicesL();
00259             break;
00260             }
00261         case EBTCmdSendMessage:
00262             {
00263             TRAPD(err,iEngine->SendMessageL() );
00264             if( err )
00265                 iContainer->ShowMessageL( KMsgSendFailed );
00266             break;
00267             }
00268         case EBtCmdClearScreen:
00269             {
00270             iContainer->ClearScreenL();
00271             break;
00272             }
00273 #ifdef ENABLE_LIAC
00274         case EBtCmdBluetoothSetLimitedDiscoverableStatusOn:
00275             {
00276             iEngine->SetLIAC( ETrue );
00277             break;
00278             }
00279         case EBtCmdBluetoothSetLimitedDiscoverableStatusOff:
00280             {
00281             iEngine->SetLIAC( EFalse );
00282             break;
00283             }
00284 #endif
00285         case EBTCmdStopDiscovery:
00286             {
00287             iEngine->StopDiscovery();
00288             break;
00289             }
00290 
00291         default:
00292             break;
00293         }
00294     }
00295 
00296 void CBluetoothPMPExampleAppUi::HandleResourceChangeL(TInt aType)
00297     {
00298     CAknAppUi::HandleResourceChangeL(aType);
00299     if ( aType == KEikDynamicLayoutVariantSwitch )
00300         {
00301         TRect rect;
00302         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane,rect);
00303         iContainer->SetRect(rect);
00304         }
00305     }
00306 
00307 
00308 CBluetoothPMPExampleRTEContainer* CBluetoothPMPExampleAppUi::Container()
00309     {
00310     return iContainer;
00311     }
00312 
00313 // End of File

Generated by  doxygen 1.6.2