×
Namespaces

Variants
Actions

Closing a dialog after a timeout using Symbian C++

From Nokia Developer Wiki
Jump to: navigation, search

This code snippet shows how to create a dialog that will close after a specified amount of time. Obviously the actually closing of the dialog can be made dependent on other factors including for example when the inactivity timer times out.

Article Metadata
Tested with
Devices(s): N82, 5800 Music Express
Compatibility
Platform(s): S60
Platform Security
Signing Required: Self-Signed
Capabilities: None
Article
Keywords: TryExitL, CEikDialog
Created: Paul.Todd (05 Apr 2009)
Last edited: hamishwillee (09 Feb 2012)

Header file

There are three header files, one for the timer interface, one for the timer and one for the dialog that implements the timer interface.


First the interface for the timer to callback on when the timer expires. This will be implemented by observers who need to know when the timer condition has been met

class MTimerCallback
{
public:
virtual void HandleTimedOut() = 0;
};


Next the timer object it self which will call the MTimerCallback interface above when the timer expires

class CTimerCallback : public CTimer
{
public:
static CTimerCallback* NewL(MTimerCallback& aCallback);
 
private:
CTimerCallback(MTimerCallback& aCallback);
void ConstructL();
 
void RunL();
private:
MTimerCallback& iCallback;
};


Finally we have dialog that will be requesting information from the user and will be closed automatically when the timer fires

class CSymbianExampleQueryDialog 
: public CAknTextQueryDialog, public MTimerCallback
{
public:
static CSymbianExampleQueryDialog* NewL(TDes& aDataText);
~CSymbianExampleQueryDialog();
 
public: //MTimerCallback
void HandleTimedOut();
 
public: // CAknTextQueryDialog
void PostLayoutDynInitL();
 
private:
CSymbianExampleQueryDialog(TDes& aDataText);
 
private:
CTimerCallback* iCallBack;
};

Source file

The code for the timer looks like any other timer implementation.

This particular implementation starts the timer when the object is constructed since it results in less code all round for the example.

static CTimerCallback* CTimerCallback::NewL(MTimerCallback& aCallback)
{
CTimerCallback* self = new (ELeave) CTimerCallback(aCallback);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
 
CTimerCallback::CTimerCallback(MTimerCallback& aCallback)
: CTimer(CActive::EPriorityStandard),
iCallback(aCallback)
{
CActiveScheduler::Add(this);
}
 
void CTimerCallback::ConstructL()
{
CTimer::ConstructL();
 
// TODO: Your own conditions such as At or Inactivity for example
// This example hardcodes 5 seconds
const TTimeIntervalMicroSeconds32 KFiveSeconds(5 * 1000000);
After(KFiveSeconds);
}
 
void CTimerCallback::RunL()
{
// TODO: Handle error conditions such as clock skew adjustment
// and time changes
iCallback.HandleTimedOut();
}

The final "glue" to hold it all together is the implementation of the dialog To make this simple, the provided code just inherits from CAknTextQueryDialog

This is where the "magic" happens. When the dialog is constructed, the timer is also started (as above with a 5 second timeout). The implementation of the MTimerCallback inteface is also passed to the timer and so when the timer expires the HandleTimedOut method is called and this just calls TryExitL to exit the dialog asynchronously

CSymbianExampleQueryDialog* CSymbianExampleQueryDialog::NewL(TDes& aDataText)
{
CSymbianExampleQueryDialog* self = new (ELeave) CSymbianExampleQueryDialog(aDataText);
CleanupStack::PushL(self);
CleanupStack::Pop(self);
return self;
}
 
CSymbianExampleQueryDialog::~CSymbianExampleQueryDialog()
{
delete iCallBack;
iCallBack = NULL;
}
 
void CSymbianExampleQueryDialog::HandleTimedOut()
{
TRAPD(err, TryExitL(EAknSoftkeyCancel));
}
 
void CSymbianExampleQueryDialog::PostLayoutDynInitL()
{
iCallBack = CTimerCallback::NewL(*this);
}
 
CSymbianExampleQueryDialog::CSymbianExampleQueryDialog(TDes& aDataText)
: CAknTextQueryDialog(aDataText)
{
}

Postconditions

The dialog will be closed when the timer expires

This page was last modified on 9 February 2012, at 08:45.
60 page views in the last 30 days.
×