Symbian Security Studio

About symbian software programming ,security analysis and other things about symbian.

Monday, October 1, 2007

Capturing foreground events - Forum Nokia Wiki


Capturing foreground events
From Forum Nokia Wiki
Jump to: navigation, search
CFgrObserver illustrates how to capture foreground events in a background application. This implementation should work with all platform versions, but with S60 3rd Edition you need to have the SwEvent capability.
The usage is quite simple. Just implement the defined callback interface in your class and construct an instance of the CFgrObserver. Whenever a GUI application is taking focus you will get a notification through the callback functions. The aAppUid argument will have the UID of the application getting the focus.
Note that when constructing this class the aWsSession session variable should be connected. Otherwise the construction process will fail.
If you are only interested in your own application's focus changes, you could overwrite the Application user interface class' HandleForegroundEventL() method.

[edit] ForeGroundObserver.cpp CFgrObserver* CFgrObserver::NewL(RWsSession& aWsSession,MFgrCallBack& aCallBack)
{
CFgrObserver* self = CFgrObserver::NewLC(aWsSession,aCallBack);
CleanupStack::Pop(self);
return self;
}


CFgrObserver* CFgrObserver::NewLC(RWsSession& aWsSession,MFgrCallBack& aCallBack)
{
CFgrObserver* self = new (ELeave) CFgrObserver(aWsSession,aCallBack);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}


CFgrObserver::CFgrObserver(RWsSession& aWsSession,MFgrCallBack& aCallBack)
:CActive(EPriorityHigh),iCallBack(aCallBack), iWsSession(aWsSession), iWg(aWsSession)
{
}

CFgrObserver::~CFgrObserver()
{
Cancel();
iWg.Close();
}

void CFgrObserver::ConstructL()
{
CActiveScheduler::Add(this);

User::LeaveIfError(iWg.Construct((TUint32)&iWg, EFalse));
iWg.SetOrdinalPosition(-1);
iWg.EnableReceiptOfFocus(EFalse);

CApaWindowGroupName* wn=CApaWindowGroupName::NewLC(iWsSession);
wn->SetHidden(ETrue);
wn->SetWindowGroupName(iWg);
CleanupStack::PopAndDestroy();

User::LeaveIfError(iWg.EnableFocusChangeEvents());

Listen();
}

void CFgrObserver::RunL()
{
if (iStatus == KErrNone)
{
TWsEvent e;
iWsSession.GetEvent(e);
}

TInt wgid = iWsSession.GetFocusWindowGroup();

CApaWindowGroupName* gn;
gn = CApaWindowGroupName::NewLC(iWsSession, wgid);

iCallBack.ForegroundEventL(gn->AppUid());

CleanupStack::PopAndDestroy(); // gn

if (iStatus != KErrCancel)
{
Listen();
}
}

void CFgrObserver::DoCancel()
{
iWsSession.EventReadyCancel();
}

void CFgrObserver::Listen()
{
iWsSession.EventReady(&iStatus);
SetActive();
}
[edit] ForeGroundObserver.h class MFgrCallBack
{
public:
virtual void ForegroundEventL(TUid aAppUid) = 0;
};

class CFgrObserver : public CActive
{
public:
static CFgrObserver* NewL(RWsSession& aWsSession,MFgrCallBack& aCallBack);
static CFgrObserver* NewLC(RWsSession& aWsSession,MFgrCallBack& aCallBack);
virtual ~CFgrObserver();
private:
CFgrObserver(RWsSession& aWsSession,MFgrCallBack* aCallBack);
void ConstructL();
void RunL();
void DoCancel();
void Listen();
private:
MFgrCallBack& iCallBack;
RWsSession& iWsSession;
RWindowGroup iWg;
};

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home