contextframework/cfwplugins/ApplicationStateSourcePlugIn/src/ApplicationStateSourcePlugin.cpp
changeset 0 2e3d3ce01487
child 1 0fdb7f6b0309
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 /*
       
     2 * Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <cfcontextinterface.h>
       
    21 #include <cfcontextsourcesetting.h>
       
    22 #include <cfcontextsourcesettingarray.h>
       
    23 #include <cfcontextsourcesettingparameter.h>
       
    24 #include <cfkeyvaluepair.h>
       
    25 #include <centralrepository.h>
       
    26 #include <apgwgnam.h>
       
    27 #include <applicationorientation.h>
       
    28 #include <aknappui.h>
       
    29 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    30 #include <vwsdefpartner.h>
       
    31 #endif
       
    32 
       
    33 #include "ApplicationStateSourcePlugin.h"
       
    34 #include "ApplicationStateSourceContextDef.h"
       
    35 #include "ContextFrameworkPrivateCRKeys.h"
       
    36 #include "trace.h"
       
    37 
       
    38 // CONSTANTS
       
    39 
       
    40 #ifdef _DEBUG
       
    41 // Panic codes
       
    42 enum TPanicCode
       
    43     {
       
    44     EInvalidUid,
       
    45     EInvalidAttribute
       
    46     };
       
    47 #endif
       
    48 
       
    49 _LIT( KDefaultApplicationName,  "DefaultAppName" );
       
    50 _LIT( KDefaultViewName,         "DefaultViewName" );
       
    51 _LIT( KAttributeAppUid,         "AppUid" );
       
    52 _LIT( KAttributeAppName,        "AppName" );
       
    53 _LIT( KTagView,                 "View" );
       
    54 _LIT( KAttributeUid,            "Uid" );
       
    55 _LIT( KAttributeName,           "Name" );
       
    56 
       
    57 const TUint KLimit = 0xFFFFFFFF;
       
    58 
       
    59 // ============================ MEMBER FUNCTIONS ===============================
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CApplicationStateSourcePlugIn::CApplicationStateSourcePlugIn
       
    63 // C++ default constructor can NOT contain any code, that might leave.
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 CApplicationStateSourcePlugIn::CApplicationStateSourcePlugIn( TContextSourceInitParams* aParams ):
       
    67     CCFContextSourcePlugIn( aParams ),
       
    68     iDefStateSetManyTimes( 0 )
       
    69     {
       
    70     FUNC_LOG;
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CApplicationStateSourcePlugIn::ConstructL
       
    75 // Symbian second-phase constructor that may leave.
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 void CApplicationStateSourcePlugIn::ConstructL()
       
    79     {
       
    80     FUNC_LOG;
       
    81 
       
    82     iDefaultAppName = KApplicationStateDefaultValue().AllocL();
       
    83     iDefaultViewName = KApplicationStateDefaultValue().AllocL();
       
    84     iContext = CCFContextObject::NewL();
       
    85     }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CApplicationStateSourcePlugIn::NewL
       
    89 // Two-phased constructor.
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 CApplicationStateSourcePlugIn* CApplicationStateSourcePlugIn::NewL(
       
    93     TContextSourceInitParams* aParams )
       
    94     {
       
    95     FUNC_LOG;
       
    96 
       
    97     CApplicationStateSourcePlugIn* self = CApplicationStateSourcePlugIn::NewLC(
       
    98         aParams );
       
    99     CleanupStack::Pop( self );
       
   100     return self;
       
   101     }
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // CApplicationStateSourcePlugIn::NewLC
       
   105 // Two-phased constructor.
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 CApplicationStateSourcePlugIn* CApplicationStateSourcePlugIn::NewLC(
       
   109     TContextSourceInitParams* aParams )
       
   110     {
       
   111     FUNC_LOG;
       
   112 
       
   113     CApplicationStateSourcePlugIn* self = new( ELeave ) CApplicationStateSourcePlugIn(
       
   114         aParams );
       
   115     CleanupStack::PushL( self );
       
   116     self->ConstructL();
       
   117     return self;
       
   118     }
       
   119 
       
   120 // Destructor
       
   121 CApplicationStateSourcePlugIn::~CApplicationStateSourcePlugIn()
       
   122     {
       
   123     FUNC_LOG;
       
   124 
       
   125     if( iCRAppOrientationListener )
       
   126         {
       
   127         iCRAppOrientationListener->StopListening();
       
   128         delete iCRAppOrientationListener;
       
   129         }
       
   130     iUidOrientationList.Close();    
       
   131     delete iCRAppOrientation;
       
   132     iApplicationSettings.ResetAndDestroy();
       
   133     delete iSettings;
       
   134     delete iVws;
       
   135     delete iDefaultViewName;
       
   136     delete iDefaultAppName;
       
   137     delete iApplDefaultState;
       
   138     delete iContext;
       
   139     }
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // CApplicationStateSourcePlugIn::HandleSettingL
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 void CApplicationStateSourcePlugIn::HandleSettingL(
       
   146     CCFContextSourceSettingArray* aSettingList )
       
   147     {
       
   148     FUNC_LOG;
       
   149 
       
   150     // Take ownership from settings
       
   151     iSettings = aSettingList;
       
   152     }
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // CApplicationStateSourcePlugIn::DefineContextsL
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 void CApplicationStateSourcePlugIn::DefineContextsL()
       
   159     {
       
   160     FUNC_LOG;
       
   161 
       
   162     // Define contexts here
       
   163     _LIT_SECURITY_POLICY_C2(
       
   164         appStatePolicy, ECapabilitySwEvent, ECapabilityUserEnvironment );
       
   165 
       
   166     TInt err = iCF.DefineContext( KApplicationStateSource,
       
   167        KApplicationStateForegroundApplicationType,
       
   168        appStatePolicy );
       
   169     User::LeaveIfError( err );
       
   170     
       
   171     err = iCF.DefineContext( KApplicationStateSource,
       
   172        KApplicationStateForegroundApplicationViewType,
       
   173        appStatePolicy );
       
   174     User::LeaveIfError( err );
       
   175 
       
   176     err = iCF.DefineContext( KApplicationStateSource,
       
   177        KApplicationStateBackgroundApplicationType,
       
   178        appStatePolicy );
       
   179     User::LeaveIfError( err );
       
   180     
       
   181     err = iCF.DefineContext( KApplicationStateSource,
       
   182         KFgApplicationOrientation,
       
   183         KFgApplicationOrientationSec );
       
   184     User::LeaveIfError( err );
       
   185     }
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // CApplicationStateSourcePlugIn::InitializeL
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 void CApplicationStateSourcePlugIn::InitializeL()
       
   192     {
       
   193     FUNC_LOG;
       
   194 
       
   195     iContext->SetSourceL( KApplicationStateSource );
       
   196 
       
   197     // Get default app orientation
       
   198     CRepository* cenRep = CRepository::NewL( KCRUidCFApplDefaultState );
       
   199     TInt err = cenRep->Get( KApplDefaultState, iDefaultFgAppOrientation );
       
   200     if( err != KErrNone )
       
   201         {
       
   202         ERROR( err, "Failed to get default fg app orientation" );
       
   203         iDefaultFgAppOrientation = 0;
       
   204         }
       
   205     delete cenRep;
       
   206     cenRep = NULL;
       
   207 
       
   208     // Create KCRUidDefaultAppOrientation repository for default app orientation
       
   209     iCRAppOrientation = CRepository::NewL( KCRUidDefaultAppOrientation ); 
       
   210     
       
   211     // Create KCRUidDefaultAppOrientation repository listener
       
   212     iCRAppOrientationListener = CCenRepNotifyHandler::NewL(
       
   213         *this, *iCRAppOrientation );
       
   214     iCRAppOrientationListener->StartListeningL();
       
   215         
       
   216     if( iSettings )
       
   217         {
       
   218         InitializeApplicationsFromXMLL();
       
   219         InitializeFgApplicationL();
       
   220 
       
   221         // Free memory allocated for settings since we don't need them anymore
       
   222         delete iSettings;
       
   223         iSettings = NULL;
       
   224         }
       
   225 
       
   226     iVws = CVwsSessionWrapper::NewL( *this );
       
   227     iVws->NotifyNextActivation();
       
   228     }
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // CApplicationStateSourcePlugIn::InitializeApplicationsFromXMLL
       
   232 // -----------------------------------------------------------------------------
       
   233 //
       
   234 void CApplicationStateSourcePlugIn::InitializeApplicationsFromXMLL()
       
   235     {
       
   236     FUNC_LOG;
       
   237 
       
   238      for( TInt i = 0; i < iSettings->Count(); ++i )
       
   239         {
       
   240         // Check attributes
       
   241         TBool defaultValueDefinitions = EFalse;
       
   242         TPtrC appUid( KNullDesC );
       
   243         TPtrC appName( KNullDesC );
       
   244         const RKeyValueArray& attributes = iSettings->Item( i ).Attributes();
       
   245 
       
   246         // Check application and view names (also default values)
       
   247         for( TInt j = 0; j < attributes.Count(); ++j )
       
   248             {
       
   249             TPtrC key( attributes[ j ]->Key() );
       
   250             TPtrC value( attributes[ j ]->Value() );
       
   251 
       
   252             if( key.CompareF( KAttributeAppUid ) == KErrNone )
       
   253                 {
       
   254                 appUid.Set( value );
       
   255                 }
       
   256             else if( key.CompareF( KAttributeAppName ) == KErrNone )
       
   257                 {
       
   258                 appName.Set( value );
       
   259                 }
       
   260             else if( key.CompareF( KDefaultApplicationName ) == KErrNone )
       
   261                 {
       
   262                 delete iDefaultAppName;
       
   263                 iDefaultAppName = NULL;
       
   264                 iDefaultAppName = value.AllocL();
       
   265                 defaultValueDefinitions = ETrue;
       
   266                 }
       
   267             else if( key.CompareF( KDefaultViewName ) == KErrNone )
       
   268                 {
       
   269                 delete iDefaultViewName;
       
   270                 iDefaultViewName = NULL;
       
   271                 iDefaultViewName = value.AllocL();
       
   272                 defaultValueDefinitions = ETrue;
       
   273                 }
       
   274             }
       
   275 
       
   276         if ( defaultValueDefinitions )
       
   277             {
       
   278             continue; // Default value definitions contain only attributes.
       
   279             }
       
   280 
       
   281         if ( appUid.Length() == 0 || appName.Length() == 0 )
       
   282             {
       
   283             ERROR_GEN( "Either AppUid or AppName tag (or both) not found, skipping" );
       
   284             continue; // Skip this definition.
       
   285             }
       
   286 
       
   287         TUint32 uid = 0;
       
   288         TInt err = ConvertToHex( appUid, uid );
       
   289         if( err != KErrNone )
       
   290             {
       
   291             ERROR_1( err, "Invalid uid [%S] for an application, skipping", &appUid );
       
   292             continue; // Skip this definition.
       
   293             }
       
   294 
       
   295         // below:                                  CLEANUP<< appStateSettings
       
   296         CCFApplicationStateSettings* appStateSettings
       
   297             = CCFApplicationStateSettings::NewLC( TUid::Uid( uid ), appName );
       
   298 
       
   299         // Check parameters
       
   300         const RSettingParameterArray& parameters = iSettings->Item( i ).Parameters();
       
   301         for( TInt j = 0; j < parameters.Count(); ++j )
       
   302             {
       
   303             if ( parameters[ j ]->Name() != KTagView )
       
   304                 {
       
   305                 continue; // At the moment, handle only view tags
       
   306                 }
       
   307 
       
   308             TPtrC viewUid( KNullDesC );
       
   309             TPtrC viewName( KNullDesC );
       
   310             const RKeyValueArray& paramAttr = parameters[ j ]->Attributes();
       
   311             for( TInt k = 0; k < paramAttr.Count(); ++k )
       
   312                 {
       
   313                 TPtrC key( paramAttr[ k ]->Key() );
       
   314                 TPtrC value( paramAttr[ k ]->Value() );
       
   315 
       
   316                 // Check view uid
       
   317                 if( key.CompareF( KAttributeUid ) == KErrNone )
       
   318                     {
       
   319                     viewUid.Set( value );
       
   320                     }
       
   321                 // Check view name
       
   322                 else if( key.CompareF( KAttributeName ) == KErrNone )
       
   323                     {
       
   324                     viewName.Set( value );
       
   325                     }
       
   326                 }
       
   327 
       
   328             if ( viewUid.Length() == 0 || viewName.Length() == 0 )
       
   329                 {
       
   330                 ERROR_GEN( "Either View's Uid or Name tag (or both) not found, skipping" );
       
   331                 continue; // Skip this definition.
       
   332                 }
       
   333 
       
   334             uid = 0;
       
   335             TInt err = ConvertToHex( viewUid, uid );
       
   336             if( err != KErrNone )
       
   337                 {
       
   338                 ERROR_1( err, "Invalid view uid [%S], skipping", &viewUid );
       
   339                 continue; // Skip this definition.
       
   340                 }
       
   341 
       
   342             // below:                              CLEANUP<< viewUidName
       
   343             CCFUidNamePair* viewUidName
       
   344                 = CCFUidNamePair::NewLC( TUid::Uid( uid ), viewName );
       
   345             appStateSettings->AddViewSettingsL( viewUidName );
       
   346             CleanupStack::Pop( viewUidName );   // CLEANUP>> viewUidName
       
   347             }
       
   348 
       
   349         iApplicationSettings.AppendL( appStateSettings );
       
   350         CleanupStack::Pop( appStateSettings );  // CLEANUP>> appStateSettings
       
   351         }
       
   352     }
       
   353 
       
   354 // -----------------------------------------------------------------------------
       
   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 
       
   382 //------------------------------------------------------------------------------
       
   383 // CApplicationStateSourcePlugIn::HandleNotifyGeneric
       
   384 //------------------------------------------------------------------------------
       
   385 //
       
   386 void CApplicationStateSourcePlugIn::HandleNotifyGeneric( TUint32 aId )
       
   387     {
       
   388     FUNC_LOG;
       
   389 
       
   390     // Get the new value from cenrep
       
   391     TInt val = 0;
       
   392     TInt err = iCRAppOrientation->Get( aId, val );
       
   393     if( err == KErrNone )
       
   394         {
       
   395         // New value found, check if the element is found from cache
       
   396         TInt index = FindByUid( TUid::Uid( aId ) );
       
   397         if( index != KErrNotFound )
       
   398             {
       
   399             // Update cache element
       
   400             TUidOrientationPair& pair = iUidOrientationList[index];
       
   401             pair.iOrientation = val;
       
   402             INFO_2( "Updated cache uid: [0x%x] to orientation: [%d]", pair.iUid.iUid, val );
       
   403             }
       
   404         else
       
   405             {
       
   406             // Add new cache element
       
   407             TUidOrientationPair pair( TUid::Uid( aId ), val );
       
   408             err = iUidOrientationList.Append( pair );
       
   409             if( err == KErrNone )
       
   410                 {
       
   411                 INFO_2( "Added cache uid: [0x%x], orientation: [%d]", pair.iUid.iUid, val );
       
   412                 }
       
   413             else
       
   414                 {
       
   415                 ERROR_1( err, "Failed to add cache uid: [0x%x]", pair.iUid.iUid );
       
   416                 }
       
   417             }
       
   418         }
       
   419     else
       
   420         {
       
   421         ERROR_1( err, "Failed to get new value from CR key: [0x%x]", aId );
       
   422         }
       
   423     }
       
   424 
       
   425 // -----------------------------------------------------------------------------
       
   426 // CApplicationStateSourcePlugIn::DoHandleViewEventL
       
   427 // Maps view activations to contexts.
       
   428 // -----------------------------------------------------------------------------
       
   429 //
       
   430 void CApplicationStateSourcePlugIn::DoHandleViewEventL(
       
   431     const TVwsViewEvent& aEvent )
       
   432     {
       
   433     FUNC_LOG;
       
   434 
       
   435     TPtrC bgApp( *iDefaultAppName );
       
   436     TPtrC fgApp( *iDefaultAppName );
       
   437     TPtrC fgView( *iDefaultViewName );
       
   438     TBool bgFound( EFalse );
       
   439     TBool fgFound( EFalse );
       
   440 
       
   441     if ( aEvent.iViewTwoId.iAppUid == TUid::Null() )
       
   442         {
       
   443         // Previous foreground application was closed, get the stored name for
       
   444         // background application's name.
       
   445         bgFound = ETrue;
       
   446         bgApp.Set( iPreviousForegroundApplication );
       
   447         }
       
   448 
       
   449     for ( TInt i = 0; i < iApplicationSettings.Count(); ++i )
       
   450         {
       
   451         CCFApplicationStateSettings* appSettings = iApplicationSettings[ i ];
       
   452         if ( !fgFound && aEvent.iViewOneId.iAppUid == appSettings->Uid() )
       
   453             {
       
   454             fgApp.Set( appSettings->Name() );
       
   455             appSettings->GetViewName( aEvent.iViewOneId.iViewUid, fgView );
       
   456             fgFound = ETrue;
       
   457             }
       
   458         if ( !bgFound && aEvent.iViewTwoId.iAppUid == appSettings->Uid() )
       
   459             {
       
   460             bgApp.Set( appSettings->Name() );
       
   461             bgFound = ETrue;
       
   462             }
       
   463         if ( bgFound && fgFound )
       
   464             {
       
   465             break;
       
   466             }
       
   467         }
       
   468 
       
   469     TBool publishFgApp( EFalse );
       
   470     TBool publishFgView( EFalse );
       
   471     TBool publishBgApp( EFalse );
       
   472 
       
   473     if ( iPreviousForegroundApplication.Compare( fgApp ) != 0 )
       
   474         {
       
   475         publishFgApp = ETrue;
       
   476         }
       
   477     else if ( iPreviousForegroundView.Compare( fgView ) != 0 )
       
   478         {
       
   479         publishFgView = ETrue;
       
   480         }
       
   481 
       
   482     if ( bgApp != fgApp )
       
   483         {
       
   484         publishBgApp = ETrue;
       
   485         }
       
   486 
       
   487     iPreviousForegroundApplication.Set( fgApp ); // Store for next round
       
   488     iPreviousForegroundView.Set( fgView );
       
   489 
       
   490     // Publish foreground application and view contexts.
       
   491     RThread thread;
       
   492 
       
   493     if ( publishFgApp )
       
   494         {
       
   495         iContext->SetTypeL( KApplicationStateForegroundApplicationType );
       
   496         iContext->SetValueL( fgApp );
       
   497         iCF.PublishContext( *iContext, thread );
       
   498         }
       
   499 
       
   500     if ( publishFgApp || publishFgView )
       
   501         {
       
   502         iContext->SetTypeL( KApplicationStateForegroundApplicationViewType );
       
   503         iContext->SetValueL( fgView );
       
   504         iCF.PublishContext( *iContext, thread );
       
   505         }
       
   506 
       
   507     if ( publishBgApp )
       
   508         {
       
   509         iContext->SetTypeL( KApplicationStateBackgroundApplicationType );
       
   510         iContext->SetValueL( bgApp );
       
   511         iCF.PublishContext( *iContext, thread );
       
   512         }
       
   513     
       
   514     // Publish foreground application orientation context
       
   515     TPtrC fgAppOrientation( ResolveFgAppOrientation( aEvent.iViewOneId.iAppUid ) );
       
   516     if( fgAppOrientation.Length() )
       
   517         {
       
   518         // Fg app found
       
   519         if( fgAppOrientation.Compare( iPreviousOrientation ) != KErrNone )
       
   520             {
       
   521             // Update for next round
       
   522             iPreviousOrientation.Set( fgAppOrientation );
       
   523             
       
   524             // Fg app orientation changed, publish new context
       
   525             iContext->SetTypeL( KFgApplicationOrientation );
       
   526             iContext->SetValueL( fgAppOrientation );
       
   527             iCF.PublishContext( *iContext, thread );
       
   528             }
       
   529         }
       
   530 
       
   531     thread.Close();
       
   532     }
       
   533 
       
   534 //------------------------------------------------------------------------------
       
   535 // CApplicationStateSourcePlugIn::ResolveFgAppOrientation
       
   536 //------------------------------------------------------------------------------
       
   537 //
       
   538 TPtrC CApplicationStateSourcePlugIn::ResolveFgAppOrientation(
       
   539     const TUid& aUid )
       
   540     {
       
   541     FUNC_LOG;
       
   542     
       
   543     TPtrC fgAppOrientation( KNullDesC );
       
   544     TInt val = 0;
       
   545     
       
   546     // Check cache first
       
   547     TInt index = FindByUid( aUid );
       
   548     if( index != KErrNotFound )
       
   549         {
       
   550         // App found from cache
       
   551         val = iUidOrientationList[index].iOrientation;
       
   552         }
       
   553     else
       
   554         {
       
   555         // Get the SID from cenrep and update cache
       
   556         TInt err = iCRAppOrientation->Get( aUid.iUid, val );
       
   557         if( err != KErrNone )
       
   558             {
       
   559             INFO_1( "Application uid: [0x%x] not found, using default orientation",
       
   560                 aUid.iUid );
       
   561             val = iDefaultFgAppOrientation;
       
   562             }
       
   563         
       
   564         // Update cache
       
   565         TUidOrientationPair pair( aUid, val );
       
   566         err = iUidOrientationList.Append( pair );
       
   567         if( err != KErrNone )
       
   568             {
       
   569             ERROR_2( err, "Failed to add uid: [0x%x], orientation: [%d] pair in cache",
       
   570                 aUid.iUid, val );
       
   571             }
       
   572         }
       
   573     
       
   574     // Resolve context value
       
   575     if( val == CAknAppUiBase::EAppOrientationPortrait )
       
   576         {
       
   577         // Portrait
       
   578         fgAppOrientation.Set( TPtrC(
       
   579             KFgApplicationOrientationVal[EFgApplicationOrientationPortrait] ) );
       
   580         }
       
   581     else if( val == CAknAppUiBase::EAppOrientationLandscape )
       
   582         {
       
   583         // Landscape
       
   584         fgAppOrientation.Set( TPtrC(
       
   585             KFgApplicationOrientationVal[EFgApplicationOrientationLandscape] ) );
       
   586         }
       
   587     else
       
   588         {
       
   589         // Automatic
       
   590         fgAppOrientation.Set( TPtrC(
       
   591             KFgApplicationOrientationVal[EFgApplicationOrientationAutomatic] ) );
       
   592         }
       
   593     
       
   594     return fgAppOrientation;
       
   595     }
       
   596 
       
   597 // -----------------------------------------------------------------------------
       
   598 // CApplicationStateSourcePlugIn::InitializeFgApplicationL
       
   599 // -----------------------------------------------------------------------------
       
   600 //
       
   601 void CApplicationStateSourcePlugIn::InitializeFgApplicationL()
       
   602     {
       
   603     FUNC_LOG;
       
   604 
       
   605     RWsSession wsSession;
       
   606     User::LeaveIfError( wsSession.Connect() );
       
   607     CleanupClosePushL( wsSession );
       
   608     TUid appUid( TUid::Null() );
       
   609 
       
   610     TInt focScr( wsSession.GetFocusScreen() );
       
   611     TInt focWndGrp( wsSession.GetFocusWindowGroup() );
       
   612     TInt wndGrpPrio( wsSession.GetWindowGroupOrdinalPriority( focWndGrp ) );
       
   613     
       
   614     INFO_1( "CApplicationStateSourcePlugIn::focScr     = [%d]", focScr );
       
   615     INFO_1( "CApplicationStateSourcePlugIn::focWndGrp  = [%d]", focWndGrp );
       
   616     INFO_1( "CApplicationStateSourcePlugIn::wndGrpPrio = [%d]", wndGrpPrio );
       
   617 
       
   618     CApaWindowGroupName* wndGrpName = CApaWindowGroupName::NewL( wsSession );
       
   619     CleanupStack::PushL( wndGrpName );
       
   620 
       
   621     wndGrpName->ConstructFromWgIdL( focWndGrp );
       
   622 
       
   623     TPtrC appCaption = wndGrpName->Caption();
       
   624     TPtrC appName = wndGrpName->AppUid().Name();
       
   625 
       
   626     // if focScr == 0 it's not CoverUi
       
   627     // if focScr != 0 it's CoverUi
       
   628 
       
   629     // if wndGrpPrio == 0 it's S60 application
       
   630 
       
   631     if ( ( focScr != 0 ) || ( ( focScr == 0 ) && ( wndGrpPrio == 0 ) ) )
       
   632         {
       
   633         INFO( "CApplicationStateSourcePlugIn::It's CoverUi or S60 application" );
       
   634         // It is CoverUi
       
   635         // or
       
   636         // It is Not CoverUi and it is S60 application
       
   637 
       
   638         appUid = wndGrpName->AppUid();
       
   639         INFO_1( "appUid = [0x%x]", appUid.iUid );
       
   640         }
       
   641     else
       
   642         {
       
   643         // Default values will still be published
       
   644         TUid tmpAppUid( TUid::Null() );
       
   645         tmpAppUid = wndGrpName->AppUid();
       
   646 
       
   647         INFO( "CApplicationStateSourcePlugIn::It's neiher CoverUi nor S60 application" );
       
   648         INFO_1( "appUid = [0x%x]", tmpAppUid.iUid );
       
   649         }
       
   650 
       
   651     INFO_1( "appCaption = [%S]", &appCaption );
       
   652     INFO_1( "appName    = [%S]", &appName );
       
   653 
       
   654     CleanupStack::PopAndDestroy( wndGrpName );
       
   655     CleanupStack::PopAndDestroy( &wsSession );
       
   656 
       
   657     TPtrC bgApp( *iDefaultAppName );
       
   658     TPtrC fgApp( *iDefaultAppName );
       
   659     TPtrC fgView( *iDefaultViewName );
       
   660 
       
   661     if ( appUid != TUid::Null() )
       
   662         {
       
   663         for ( TInt i = 0; i < iApplicationSettings.Count(); ++i )
       
   664             {
       
   665             CCFApplicationStateSettings* appSettings = iApplicationSettings[ i ];
       
   666             if ( appUid == appSettings->Uid() )
       
   667                 {
       
   668                 fgApp.Set( appSettings->Name() );
       
   669                 break;
       
   670                 }
       
   671             }
       
   672         }
       
   673 
       
   674     iPreviousForegroundApplication.Set( fgApp ); // Store for next round
       
   675     iPreviousForegroundView.Set( fgView );
       
   676 
       
   677     RThread thread;
       
   678 
       
   679     // Publish foreground application
       
   680     iContext->SetTypeL( KApplicationStateForegroundApplicationType );
       
   681     iContext->SetValueL( fgApp );
       
   682     iCF.PublishContext( *iContext, thread );
       
   683 
       
   684     // Publish  foreground view
       
   685     iContext->SetTypeL( KApplicationStateForegroundApplicationViewType );
       
   686     iContext->SetValueL( fgView );
       
   687     iCF.PublishContext( *iContext, thread );
       
   688 
       
   689     // Publish background application
       
   690     iContext->SetTypeL( KApplicationStateBackgroundApplicationType );
       
   691     iContext->SetValueL( bgApp );
       
   692     iCF.PublishContext( *iContext, thread );
       
   693     
       
   694     // Publish foreground application orientation
       
   695     TPtrC fgAppOrientation( ResolveFgAppOrientation( appUid ) );
       
   696     iContext->SetTypeL( KFgApplicationOrientation );
       
   697     iContext->SetValueL( fgAppOrientation );
       
   698     iCF.PublishContext( *iContext, thread );
       
   699     
       
   700     // Store for next round
       
   701     iPreviousOrientation.Set( fgAppOrientation );
       
   702 
       
   703     thread.Close();
       
   704     }
       
   705 
       
   706 
       
   707 //-----------------------------------------------------------------------------
       
   708 // CApplicationStateSourcePlugIn::ConvertToHex
       
   709 //-----------------------------------------------------------------------------
       
   710 //
       
   711 TInt CApplicationStateSourcePlugIn::ConvertToHex( const TDesC& aString,
       
   712     TUint32& aValue )
       
   713     {
       
   714     FUNC_LOG;
       
   715 
       
   716     TInt err = KErrNone;
       
   717     TLex lex( aString );
       
   718 
       
   719     // Get rid of '0x'
       
   720     const TInt skip = 2;
       
   721     lex.Inc( skip );
       
   722     if( lex.BoundedVal( aValue, EHex, KLimit ) != KErrNone )
       
   723         {
       
   724         err = KErrNotFound;
       
   725         }
       
   726     return err;
       
   727     }
       
   728 
       
   729 //------------------------------------------------------------------------------
       
   730 // CApplicationStateSourcePlugIn::FindByUid
       
   731 //------------------------------------------------------------------------------
       
   732 //
       
   733 TInt CApplicationStateSourcePlugIn::FindByUid( const TUid& aUid ) const
       
   734     {
       
   735     FUNC_LOG;
       
   736     
       
   737     TInt index = KErrNotFound;
       
   738     for( TInt i = 0; i < iUidOrientationList.Count(); i++ )
       
   739         {
       
   740         if( iUidOrientationList[i].iUid == aUid )
       
   741             {
       
   742             index = i;
       
   743             break;
       
   744             }
       
   745         }
       
   746     return index;
       
   747     }