examples/ForumNokia/BluetoothPMPExample/src/BluetoothPMPExampleAppUi.cpp

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

Generated by  doxygen 1.6.2