examples/ForumNokia/ThreadAndActiveObjectsEx/src/bluetoothrefreshtimer.cpp

00001 /*
00002  * Copyright © 2008 Nokia Corporation.
00003  */
00004 
00005 
00006 // INCLUDES
00007 #include <e32base.h> 
00008 #include "aknnotewrappers.h"
00009 #include "..\inc\BluetoothRefreshTimer.h"
00010 #include "btdiscoverer.h"
00011 #include "DeviceListContainer.h"
00012 
00013 // a second
00014 const TInt KThreadSecond = 1000 * 1000;
00015 
00016 _LIT( refreshDevices, "refreshing devices..." );
00017 
00018 // ----------------------------------------------------------------------------
00019 // CBluetoothRefreshTimer::CBluetoothRefreshTimer()
00020 //
00021 // Constructor.
00022 // ----------------------------------------------------------------------------
00023 CBluetoothRefreshTimer::CBluetoothRefreshTimer(
00024          CSharedIntermediator* aSMediator,
00025          CActiveSchedulerWait* aWait)
00026              : CTimer(CActive::EPriorityStandard),
00027                iWait(aWait), iSMediator(aSMediator), 
00028            iBTDiscoverer(NULL), iRefreshTime(0), iTime(0)  
00029         {
00030         iSMediator->SetBTRefreshTimerPtr(this);
00031         }
00032 
00033 // ----------------------------------------------------------------------------
00034 // CBluetoothRefreshTimer::~CBluetoothRefreshTimer()
00035 //
00036 // Destructor.
00037 // ----------------------------------------------------------------------------
00038 CBluetoothRefreshTimer::~CBluetoothRefreshTimer()
00039         {
00040         Cancel();
00041         delete iBTDiscoverer;
00042         }
00043 
00044 
00045 CBluetoothRefreshTimer* CBluetoothRefreshTimer::NewL(
00046      CSharedIntermediator* aSMediator, CActiveSchedulerWait* aWait)
00047         {
00048         CBluetoothRefreshTimer* self = CBluetoothRefreshTimer::NewLC(
00049                                                             aSMediator, aWait );
00050         CleanupStack::Pop(self);
00051         return self;
00052         }
00053 
00054 CBluetoothRefreshTimer* CBluetoothRefreshTimer::NewLC(
00055     CSharedIntermediator* aSMediator, CActiveSchedulerWait* aWait)
00056         {
00057         CBluetoothRefreshTimer* self = new (ELeave) CBluetoothRefreshTimer(
00058                                                              aSMediator, aWait );
00059         CleanupStack::PushL(self);
00060         self->ConstructL();
00061         return self;
00062         }
00063 
00064 // Standard Symbian OS 2nd phase constructor
00065 void CBluetoothRefreshTimer::ConstructL()
00066         {
00067         CTimer::ConstructL();
00068    
00069         iBTDiscoverer = CBTDiscoverer::NewL(iSMediator);
00070         }
00071 
00072 // ----------------------------------------------------------------------------
00073 // CBluetoothRefreshTimer::StartL()
00074 //
00075 // Start discovering.
00076 // ----------------------------------------------------------------------------
00077 void CBluetoothRefreshTimer::StartL()
00078         {
00079         iRefreshTime = KThreadSecond*iSMediator->RefreshTimerInitlVal();
00080 
00081         iBTDiscoverer->StartDiscoveringDevicesL();
00082         After( KThreadSecond );
00083         }
00084 
00085 // ----------------------------------------------------------------------------
00086 // CBluetoothRefreshTimer::StopWaitLoop()
00087 //
00088 // Cancel active scheduler wait loop and cancel bluetooth discoverer.
00089 // ----------------------------------------------------------------------------
00090 void CBluetoothRefreshTimer::StopWaitLoop()
00091         {
00092         iBTDiscoverer->DoCancel();
00093         Cancel();
00094         iWait->AsyncStop();     
00095         }       
00096 
00097 // ----------------------------------------------------------------------------
00098 // CBluetoothRefreshTimer::RunL()
00099 //
00100 // ----------------------------------------------------------------------------
00101 void CBluetoothRefreshTimer::RunL()
00102         {
00103         // Is it time to refresh yet
00104         if ( iTime >= iRefreshTime ) 
00105                 {
00106                 CDeviceListContainer* container = iSMediator->DeviceListContainer();
00107         
00108                 container->ClearListBox();
00109                 container->AddItemL( refreshDevices );
00110 
00111                 // Clear also shared intermediator
00112                 iSMediator->ResetArray();
00113 
00114                 // Find bluetooth devices again
00115                 iBTDiscoverer->RefreshDevices();
00116                 
00117                 iTime = 0;
00118                 }
00119 
00120         // Stop seaching for bt devices
00121         if (iSMediator->StopSearching())
00122                 {
00123                 // Stop Active Scheduler wait loop
00124                 StopWaitLoop();
00125                 }
00126 
00127         // Wait for a while and run the same function again 
00128         iTime+=KThreadSecond;
00129         After( KThreadSecond );
00130         }
00131 
00132 // ----------------------------------------------------------------------------
00133 // CBluetoothRefreshTimer::DoCancel()
00134 //
00135 // Cancel and stop the timer. 
00136 // ----------------------------------------------------------------------------
00137 void CBluetoothRefreshTimer::DoCancel() 
00138         {
00139         CTimer::DoCancel();
00140         }
00141 
00142 // ----------------------------------------------------------------------------
00143 // CBluetoothRefreshTimer::SetRefreshTime(TInt aRefreshTime)
00144 //
00145 // Set a new refresh time. Convert time to microseconds.
00146 // ----------------------------------------------------------------------------
00147 void CBluetoothRefreshTimer::SetRefreshTime(TInt aRefreshTime)
00148         {
00149         
00150         if ( aRefreshTime < KRefreshTimeMin )
00151                 {
00152                 aRefreshTime = KRefreshTimeDefault;
00153                 }
00154 
00155         // Convert to microseconds
00156         iRefreshTime = aRefreshTime * KThreadSecond;
00157         }
00158 // ----------------------------------------------------------------------------
00159 // CBluetoothRefreshTimer::RunError(TInt)
00160 //
00161 // Handles a leave occurring in the request completion event handler RunL().
00162 // ----------------------------------------------------------------------------
00163 TInt CBluetoothRefreshTimer::RunError(TInt)
00164         {
00165         return KErrNone;
00166         }
00167 

Generated by  doxygen 1.6.2