examples/ForumNokia/ThreadAndActiveObjectsEx/src/bluetoothrefreshtimer.cpp

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

Generated by  doxygen 1.6.2