contextframework/cfwplugins/ApplicationStateSourcePlugIn/src/ApplicationStateSourcePlugin.cpp
branchRCL_3
changeset 19 924385140d98
parent 17 5e7d68cc22e0
child 20 c2c61fdca848
equal deleted inserted replaced
18:0818dd463d41 19:924385140d98
     1 /*
     1 /*
     2 * Copyright (c) 2002-2010 Nokia Corporation and/or its subsidiary(-ies). 
     2 * Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). 
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    24 #include <cfkeyvaluepair.h>
    24 #include <cfkeyvaluepair.h>
    25 #include <centralrepository.h>
    25 #include <centralrepository.h>
    26 #include <apgwgnam.h>
    26 #include <apgwgnam.h>
    27 #include <applicationorientation.h>
    27 #include <applicationorientation.h>
    28 #include <aknappui.h>
    28 #include <aknappui.h>
       
    29 #include <coedef.h>
    29 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
    30 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
    30 #include <vwsdefpartner.h>
    31 #include <vwsdefpartner.h>
    31 #endif
    32 #endif
    32 #include "ApplicationStateSourcePlugin.h"
    33 #include "ApplicationStateSourcePlugin.h"
    33 #include "ApplicationStateSourceContextDef.h"
    34 #include "ApplicationStateSourceContextDef.h"
    79     FUNC_LOG;
    80     FUNC_LOG;
    80 
    81 
    81     iDefaultAppName = KApplicationStateDefaultValue().AllocL();
    82     iDefaultAppName = KApplicationStateDefaultValue().AllocL();
    82     iDefaultViewName = KApplicationStateDefaultValue().AllocL();
    83     iDefaultViewName = KApplicationStateDefaultValue().AllocL();
    83     iContext = CCFContextObject::NewL();
    84     iContext = CCFContextObject::NewL();
    84     
    85     iWsEventHandler = CWsEventHandler::NewL( *this );
       
    86     iWsEventHandler->IssueRequest();
    85     }
    87     }
    86 
    88 
    87 // -----------------------------------------------------------------------------
    89 // -----------------------------------------------------------------------------
    88 // CApplicationStateSourcePlugIn::NewL
    90 // CApplicationStateSourcePlugIn::NewL
    89 // Two-phased constructor.
    91 // Two-phased constructor.
   129         }
   131         }
   130     iUidOrientationList.Close();    
   132     iUidOrientationList.Close();    
   131     delete iCRAppOrientation;
   133     delete iCRAppOrientation;
   132     iApplicationSettings.ResetAndDestroy();
   134     iApplicationSettings.ResetAndDestroy();
   133     delete iSettings;
   135     delete iSettings;
   134     delete iVws;
   136     
   135     delete iDefaultViewName;
   137     delete iDefaultViewName;
   136     delete iDefaultAppName;
   138     delete iDefaultAppName;
   137     delete iApplDefaultState;
   139     delete iApplDefaultState;
   138     delete iContext;
   140     delete iContext;
   139     }
   141     }
   221         // Free memory allocated for settings since we don't need them anymore
   223         // Free memory allocated for settings since we don't need them anymore
   222         delete iSettings;
   224         delete iSettings;
   223         iSettings = NULL;
   225         iSettings = NULL;
   224         }
   226         }
   225 
   227 
   226     iVws = CVwsSessionWrapper::NewL( *this );
   228     
   227     iVws->NotifyNextActivation();
       
   228     }
   229     }
   229 
   230 
   230 // -----------------------------------------------------------------------------
   231 // -----------------------------------------------------------------------------
   231 // CApplicationStateSourcePlugIn::InitializeApplicationsFromXMLL
   232 // CApplicationStateSourcePlugIn::InitializeApplicationsFromXMLL
   232 // -----------------------------------------------------------------------------
   233 // -----------------------------------------------------------------------------
   349         iApplicationSettings.AppendL( appStateSettings );
   350         iApplicationSettings.AppendL( appStateSettings );
   350         CleanupStack::Pop( appStateSettings );  // CLEANUP>> appStateSettings
   351         CleanupStack::Pop( appStateSettings );  // CLEANUP>> appStateSettings
   351         }
   352         }
   352     }
   353     }
   353 
   354 
   354 // -----------------------------------------------------------------------------
   355 
   355 // CApplicationStateSourcePlugIn::HandleViewEventL
       
   356 // Maps view activations to contexts.
       
   357 // -----------------------------------------------------------------------------
       
   358 //
       
   359 void CApplicationStateSourcePlugIn::HandleViewEventL(
       
   360     const TVwsViewEvent& aEvent )
       
   361     {
       
   362     FUNC_LOG;
       
   363 
       
   364     // Renew listening
       
   365     iVws->NotifyNextActivation();
       
   366 
       
   367     // Handle the view server event
       
   368     DoHandleViewEventL( aEvent );
       
   369 
       
   370     // Log event members!
       
   371     INFO_1( "HandleViewEventL: eventType: [%d]", aEvent.iEventType );
       
   372     
       
   373     INFO_2( "HandleViewEventL: viewOne [appUid:%S] [viewUid%S]",
       
   374         &(const TDesC&)aEvent.iViewOneId.iAppUid.Name(),
       
   375         &(const TDesC&)aEvent.iViewOneId.iViewUid.Name() );
       
   376     
       
   377     INFO_2( "HandleViewEventL: viewTwo [appUid:%S] [viewUid%S]",
       
   378         &(const TDesC&)aEvent.iViewTwoId.iAppUid.Name(),
       
   379         &(const TDesC&)aEvent.iViewTwoId.iViewUid.Name() );
       
   380     }
       
   381 
   356 
   382 //------------------------------------------------------------------------------
   357 //------------------------------------------------------------------------------
   383 // CApplicationStateSourcePlugIn::HandleNotifyGeneric
   358 // CApplicationStateSourcePlugIn::HandleNotifyGeneric
   384 //------------------------------------------------------------------------------
   359 //------------------------------------------------------------------------------
   385 //
   360 //
   450         {
   425         {
   451         CCFApplicationStateSettings* appSettings = iApplicationSettings[ i ];
   426         CCFApplicationStateSettings* appSettings = iApplicationSettings[ i ];
   452         if ( !fgFound && aEvent.iViewOneId.iAppUid == appSettings->Uid() )
   427         if ( !fgFound && aEvent.iViewOneId.iAppUid == appSettings->Uid() )
   453             {
   428             {
   454             fgApp.Set( appSettings->Name() );
   429             fgApp.Set( appSettings->Name() );
   455             //Check for flag returned by GetViewName, don't directly set the Flag to ETrue
   430             appSettings->GetViewName( aEvent.iViewOneId.iViewUid, fgView );
   456             fgFound = appSettings->GetViewName( aEvent.iViewOneId.iViewUid, fgView );
   431             fgFound = ETrue;
   457             }
   432             }
   458         if ( !bgFound && aEvent.iViewTwoId.iAppUid == appSettings->Uid() )
   433         if ( !bgFound && aEvent.iViewTwoId.iAppUid == appSettings->Uid() )
   459             {
   434             {
   460             bgApp.Set( appSettings->Name() );
   435             bgApp.Set( appSettings->Name() );
   461             bgFound = ETrue;
   436             bgFound = ETrue;
   743             break;
   718             break;
   744             }
   719             }
   745         }
   720         }
   746     return index;
   721     return index;
   747     }
   722     }
   748 
   723 void CApplicationStateSourcePlugIn::HandleWsEventL( RWsSession& aWsSession )
   749 
   724 	{
       
   725 	TUid appUid( TUid::Null() );
       
   726     TPtrC bgApp( *iDefaultAppName );
       
   727     TPtrC fgApp( *iDefaultAppName );
       
   728     TPtrC fgView( *iDefaultViewName );
       
   729 
       
   730 	TInt focWndGrp( aWsSession.GetFocusWindowGroup() );
       
   731     CApaWindowGroupName* wndGrpName = CApaWindowGroupName::NewL( aWsSession );
       
   732     CleanupStack::PushL( wndGrpName );
       
   733     wndGrpName->ConstructFromWgIdL( focWndGrp );
       
   734     appUid = wndGrpName->AppUid();
       
   735 
       
   736     for ( TInt i = 0; i < iApplicationSettings.Count(); ++i )
       
   737         {
       
   738         CCFApplicationStateSettings* appSettings = iApplicationSettings[ i ];
       
   739         if ( appUid == appSettings->Uid() )
       
   740             {
       
   741             fgApp.Set( appSettings->Name() );
       
   742             appSettings->GetViewName( appUid, fgView );
       
   743             bgApp.Set( iPreviousForegroundApplication );
       
   744             break;
       
   745             }
       
   746         }
       
   747     TBool publishFgApp( EFalse );
       
   748     TBool publishFgView( EFalse );
       
   749     TBool publishBgApp( EFalse );
       
   750 
       
   751     if ( iPreviousForegroundApplication.Compare( fgApp ) != 0 )
       
   752         {
       
   753         publishFgApp = ETrue;
       
   754         }
       
   755     else if ( iPreviousForegroundView.Compare( fgView ) != 0 )
       
   756         {
       
   757         publishFgView = ETrue;
       
   758         }
       
   759 
       
   760     if ( bgApp != fgApp )
       
   761         {
       
   762         publishBgApp = ETrue;
       
   763         }
       
   764 
       
   765     iPreviousForegroundApplication.Set( fgApp ); // Store for next round
       
   766     iPreviousForegroundView.Set( fgView );
       
   767 
       
   768    RThread thread;
       
   769 
       
   770     if ( publishFgApp )
       
   771         {
       
   772         iContext->SetTypeL( KApplicationStateForegroundApplicationType );
       
   773         iContext->SetValueL( fgApp );
       
   774         iCF.PublishContext( *iContext, thread );
       
   775         }
       
   776 
       
   777     if ( publishFgApp || publishFgView )
       
   778         {
       
   779         iContext->SetTypeL( KApplicationStateForegroundApplicationViewType );
       
   780         iContext->SetValueL( fgView );
       
   781         iCF.PublishContext( *iContext, thread );
       
   782         }
       
   783 
       
   784     if ( publishBgApp )
       
   785         {
       
   786         iContext->SetTypeL( KApplicationStateBackgroundApplicationType );
       
   787         iContext->SetValueL( bgApp );
       
   788         iCF.PublishContext( *iContext, thread );
       
   789         }
   750 	
   790 	
       
   791 	thread.Close();
       
   792     CleanupStack::PopAndDestroy( wndGrpName );
       
   793 
       
   794 	}
       
   795 
       
   796 // ======================== CWsEventHandler ========================
       
   797 
       
   798 // ---------------------------------------------------------------------------
       
   799 // C++ constructor.
       
   800 // ---------------------------------------------------------------------------
       
   801 //
       
   802 CWsEventHandler::CWsEventHandler( MWsEventObserver& aObserver ):
       
   803     CActive( CActive::EPriorityStandard ),
       
   804     iObserver( aObserver )
       
   805     {
       
   806     CActiveScheduler::Add( this );
       
   807     }
       
   808 
       
   809 // ---------------------------------------------------------------------------
       
   810 // Symbian 2nd phase constructor.
       
   811 // ---------------------------------------------------------------------------
       
   812 //
       
   813 void CWsEventHandler::ConstructL()
       
   814     {
       
   815     // Connect to window server server
       
   816     User::LeaveIfError( iWsSession.Connect() );
       
   817     
       
   818     // Construct window group
       
   819     iWindowGroup = new( ELeave ) RWindowGroup( iWsSession );
       
   820     User::LeaveIfError( iWindowGroup->Construct( (TUint32)this, EFalse ) );
       
   821     User::LeaveIfError( iWindowGroup->EnableGroupChangeEvents() );
       
   822     iWindowGroup->SetOrdinalPosition( 0, ECoeWinPriorityNeverAtFront );
       
   823     iWindowGroup->EnableReceiptOfFocus( EFalse );
       
   824     
       
   825     // Hide the invisible window from the task manager
       
   826     iWindowGroupName = CApaWindowGroupName::NewL( iWsSession );
       
   827     iWindowGroupName->SetHidden( ETrue );
       
   828     iWindowGroupName->SetWindowGroupName( *iWindowGroup );
       
   829 
       
   830     }
       
   831 // ---------------------------------------------------------------------------
       
   832 // Symbian two phased constructor.
       
   833 // ---------------------------------------------------------------------------
       
   834 //
       
   835 CWsEventHandler* CWsEventHandler::NewL( MWsEventObserver& aObserver )
       
   836     {
       
   837     CWsEventHandler* self = CWsEventHandler::NewLC( aObserver );
       
   838     CleanupStack::Pop( self );
       
   839     
       
   840     return self;
       
   841     }
       
   842 
       
   843 // ---------------------------------------------------------------------------
       
   844 // Symbian two phased constructor.
       
   845 // Leaves pointer in the cleanup stack.
       
   846 // ---------------------------------------------------------------------------
       
   847 //
       
   848 CWsEventHandler* CWsEventHandler::NewLC( MWsEventObserver& aObserver )
       
   849     {
       
   850     CWsEventHandler* self = new ( ELeave ) CWsEventHandler( aObserver );
       
   851     CleanupStack::PushL( self );
       
   852     self->ConstructL();
       
   853     
       
   854     return self;
       
   855     }
       
   856 
       
   857 // ---------------------------------------------------------------------------
       
   858 // C++ destructor.
       
   859 // ---------------------------------------------------------------------------
       
   860 //
       
   861 CWsEventHandler::~CWsEventHandler()
       
   862     {
       
   863     Cancel();
       
   864         
       
   865     // Cleanup window group name
       
   866     delete iWindowGroupName;
       
   867        
       
   868     // Cleanup window group
       
   869     if( iWindowGroup )
       
   870         {
       
   871         iWindowGroup->DisableGroupChangeEvents();
       
   872         iWindowGroup->Close();
       
   873         delete iWindowGroup;
       
   874         }
       
   875     
       
   876     // Cleanup window server session
       
   877     iWsSession.Close();
       
   878     }
       
   879 
       
   880 //------------------------------------------------------------------------------
       
   881 // CWsEventHandler::IssueRequest
       
   882 //------------------------------------------------------------------------------
       
   883 //
       
   884 void CWsEventHandler::IssueRequest()
       
   885     {
       
   886     // Request events from window server
       
   887     iWsSession.EventReady( &iStatus );
       
   888     SetActive();
       
   889     }
       
   890 
       
   891 
       
   892 //------------------------------------------------------------------------------
       
   893 // CWsEventHandler::RunL
       
   894 //------------------------------------------------------------------------------
       
   895 //
       
   896 void CWsEventHandler::RunL()
       
   897     {
       
   898     TInt err = iStatus.Int();
       
   899     if( err == KErrNone )
       
   900         {
       
   901         // No errors occured, fetch event
       
   902         TWsEvent wsEvent;
       
   903         iWsSession.GetEvent( wsEvent );
       
   904         
       
   905         // Continue listening
       
   906         IssueRequest();
       
   907 		switch (wsEvent.Type()) 
       
   908 			{
       
   909 		case EEventWindowGroupsChanged :
       
   910 	       // Forward event to observer
       
   911 	       iObserver.HandleWsEventL( iWsSession );
       
   912 			break;
       
   913 		default:
       
   914 			break;
       
   915 			}
       
   916         }
       
   917     }
       
   918 
       
   919 //------------------------------------------------------------------------------
       
   920 // CWsEventHandler::DoCancel
       
   921 //------------------------------------------------------------------------------
       
   922 //
       
   923 void CWsEventHandler::DoCancel()
       
   924     {
       
   925     // Cancel event ready from window server
       
   926     iWsSession.EventReadyCancel();
       
   927     }
       
   928 
       
   929 //------------------------------------------------------------------------------
       
   930 // CWsEventHandler::RunError
       
   931 //------------------------------------------------------------------------------
       
   932 //
       
   933 TInt CWsEventHandler::RunError( TInt /*aError*/ )
       
   934     {
       
   935     // Issue a new request, other error handling is not performed since the
       
   936     // problem has occured in the observer code
       
   937     IssueRequest();
       
   938     
       
   939     return KErrNone;
       
   940     }
       
   941 
       
   942