taskswitcher/contextengine/tsfswserver/engine/src/tsfswengine.cpp
changeset 4 4d54b72983ae
child 9 f966699dea19
equal deleted inserted replaced
3:fb3763350a08 4:4d54b72983ae
       
     1 /*
       
     2 * Copyright (c) 2008 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:  Task monitor engine
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "tsfswengine.h"
       
    20 #include "tsfsalwaysshownapplist.h"
       
    21 #include "tsfshiddenapplist.h"
       
    22 #include "tsfswidgetlist.h"
       
    23 #include "tsfswiconcache.h"
       
    24 #include "tspreviewprovider.h"
       
    25 #include "tsfswclient.h"
       
    26 #include <apgtask.h>
       
    27 #include <AknDef.h>
       
    28 #include <apgwgnam.h>
       
    29 #include <mmf/common/mmfcontrollerpluginresolver.h> // for CleanupResetAndDestroyPushL
       
    30 #include <featmgr.h>
       
    31 #include <swi/swispubsubdefs.h>
       
    32 
       
    33 #include "enginelogging.h"
       
    34 
       
    35 // time to wait before refreshing content
       
    36 const TInt KContentRefreshDelay = 500000; // 0.5 sec
       
    37 
       
    38 // for screenshots, they are scaled down to (screensize/this_factor).
       
    39 const TInt KScreenSizeFactor = 2;
       
    40 
       
    41 // format to get a lowercase hex string prefixed with 0x
       
    42 _LIT( KHexFmt, "0x%x" );
       
    43 
       
    44 // --------------------------------------------------------------------------
       
    45 // CTsFswEngine::NewL
       
    46 // --------------------------------------------------------------------------
       
    47 //
       
    48 EXPORT_C CTsFswEngine* CTsFswEngine::NewL( MTsFswEngineObserver& aObserver )
       
    49     {
       
    50     CTsFswEngine* self = NewLC( aObserver );
       
    51     CleanupStack::Pop( self );
       
    52     return self;
       
    53     }
       
    54 
       
    55 // --------------------------------------------------------------------------
       
    56 // CTsFswEngine::NewLC
       
    57 // --------------------------------------------------------------------------
       
    58 //
       
    59 EXPORT_C CTsFswEngine* CTsFswEngine::NewLC( MTsFswEngineObserver& aObserver )
       
    60     {
       
    61     CTsFswEngine* self = new ( ELeave ) CTsFswEngine( aObserver );
       
    62     CleanupStack::PushL( self );
       
    63     self->ConstructL();
       
    64     return self;
       
    65     }
       
    66 
       
    67 // --------------------------------------------------------------------------
       
    68 // CTsFswEngine::CTsFswEngine
       
    69 // --------------------------------------------------------------------------
       
    70 //
       
    71 CTsFswEngine::CTsFswEngine( MTsFswEngineObserver& aObserver )
       
    72         : CActive( EPriorityStandard ), iObserver( aObserver )
       
    73     {
       
    74     CActiveScheduler::Add( this );
       
    75     }
       
    76 
       
    77 // --------------------------------------------------------------------------
       
    78 // CTsFswEngine::ConstructL
       
    79 // --------------------------------------------------------------------------
       
    80 //
       
    81 void CTsFswEngine::ConstructL()
       
    82     {
       
    83     iEnv = CEikonEnv::Static();
       
    84 
       
    85     User::LeaveIfError( iWsSession.Connect() );
       
    86     User::LeaveIfError( iAppArcSession.Connect() );
       
    87 
       
    88     iHiddenAppList = CTsFsHiddenAppList::NewL( *this );
       
    89     iAlwaysShownAppList = CTsFsAlwaysShownAppList::NewL();
       
    90 
       
    91     iWidgetsSupported = FeatureManager::FeatureSupported( KFeatureIdWebWidgets );
       
    92     if ( iWidgetsSupported )
       
    93         {
       
    94         iWidgetList = CTsFsWidgetList::NewL();
       
    95         }
       
    96 
       
    97     // create app icon retriever instance
       
    98     iAppIcons = CTsFswIconCache::NewL();
       
    99 
       
   100     // get an initial list of tasks
       
   101     iAppDataRefreshNeeded = ETrue;
       
   102     CollectTasksL();
       
   103 
       
   104     // listen for app screenshots
       
   105     iPreviewProvider = CTsFastSwapPreviewProvider::NewL( *this );
       
   106     SetPreviewParams();
       
   107     UpdatePreviewContent();
       
   108 
       
   109     iUpdateStarter = CPeriodic::NewL( CActive::EPriorityStandard );
       
   110     
       
   111     // start listening for swi ps property
       
   112     if ( iSwiProp.Attach( KUidSystemCategory,
       
   113             Swi::KUidSoftwareInstallKey ) == KErrNone )
       
   114         {
       
   115         iSwiProp.Subscribe( iStatus );
       
   116         SetActive();
       
   117         }
       
   118     }
       
   119 
       
   120 // --------------------------------------------------------------------------
       
   121 // CTsFswEngine::~CTsFswEngine
       
   122 // --------------------------------------------------------------------------
       
   123 //
       
   124 CTsFswEngine::~CTsFswEngine()
       
   125     {
       
   126     Cancel();
       
   127     iSwiProp.Close();
       
   128 
       
   129     delete iUpdateStarter;
       
   130     delete iPreviewProvider;
       
   131 
       
   132     // delete the bitmaps as the hash map cannot do that
       
   133     THashMapIter<TInt, CFbsBitmap*> iter( iScreenshots );
       
   134     while ( const TInt* key = iter.NextKey() )
       
   135         {
       
   136         CFbsBitmap** value = iter.CurrentValue();
       
   137         delete *value;
       
   138         }
       
   139     iScreenshots.Close();
       
   140     delete iAppIcons;
       
   141 
       
   142     iData.ResetAndDestroy();
       
   143     iWgIds.Close();
       
   144 
       
   145     delete iHiddenAppList;
       
   146     delete iAlwaysShownAppList;
       
   147     delete iWidgetList;
       
   148     iAppArcSession.Close();
       
   149     iWsSession.Close();
       
   150 
       
   151 //    delete iContextUtility;
       
   152     }
       
   153 
       
   154 // --------------------------------------------------------------------------
       
   155 // CTsFswEngine::FswDataL
       
   156 // --------------------------------------------------------------------------
       
   157 //
       
   158 EXPORT_C const RTsFswArray& CTsFswEngine::FswDataL()
       
   159     {
       
   160     TSLOG_CONTEXT( FswDataL, TSLOG_LOCAL );
       
   161     TSLOG1_IN( "dirty flag = %d", iTaskListDirty );
       
   162 
       
   163     // check the dirty flag and refresh if needed
       
   164     if ( iTaskListDirty )
       
   165         {
       
   166         CollectTasksL();
       
   167         // dirty flag is cleared in the above call
       
   168         }
       
   169 
       
   170     // Get app icon for entries without screenshot,
       
   171     // do this only here as the app icon is not needed in case a screenshot
       
   172     // is already available.
       
   173     for ( TInt i = 0, ie = iData.Count(); i != ie; ++i )
       
   174         {
       
   175         if ( !iData[i]->ScreenshotHandle() )
       
   176             {
       
   177             CFbsBitmap* bmp;
       
   178             CFbsBitmap* mask;
       
   179             TUid appUid = iData[i]->AppUid();
       
   180             // this will leave with -46 in case of widgets if we do not have AllFiles cap
       
   181             TRAPD( err, iAppIcons->GetIconL( appUid, bmp, mask ) );
       
   182             if ( err == KErrNone && bmp )
       
   183                 {
       
   184                 iData[i]->SetAppIconHandles( bmp->Handle(),
       
   185                     mask ? mask->Handle() : 0 );
       
   186                 }
       
   187             else
       
   188                 {
       
   189                 iAppIcons->DefaultIcon( bmp, mask );
       
   190                 iData[i]->SetAppIconHandles( bmp->Handle(),
       
   191                     mask ? mask->Handle() : 0 );
       
   192                 }
       
   193             }
       
   194         }
       
   195 
       
   196     TSLOG_OUT();
       
   197     return iData;
       
   198     }
       
   199 
       
   200 // --------------------------------------------------------------------------
       
   201 // CTsFswEngine::CloseAppL
       
   202 // --------------------------------------------------------------------------
       
   203 //
       
   204 EXPORT_C void CTsFswEngine::CloseAppL( TInt aWgId )
       
   205     {
       
   206     TSLOG_CONTEXT( CloseAppL, TSLOG_LOCAL );
       
   207     TSLOG1_IN( "aWgId = %d", aWgId );
       
   208 
       
   209     if ( iWidgetsSupported && aWgId < 0 && iWidgetAppUiWgId )
       
   210         {
       
   211         // for widgets clients see a wgid that is -1*(index+1)
       
   212         const RWidgetInfoArray& arr( iWidgetList->RunningWidgets() );
       
   213         // convert aWgId to an index in the list of running widgets
       
   214         TInt idx = -aWgId - 1;
       
   215         // if index is valid then handle the widget specially
       
   216         if ( idx >= 0 && idx < arr.Count() )
       
   217             {
       
   218             TWsEvent event;
       
   219             event.SetType( EEventUser );
       
   220             TUint8* eventData = event.EventData();
       
   221             // Fill bits 0-31 with widget application uid.
       
   222             reinterpret_cast<TUint32&>( *eventData ) = KWidgetAppUidValue;
       
   223             eventData += sizeof( TUint32 );
       
   224             // Fill bits 32-63 with uid of the widget that should be closed.
       
   225             reinterpret_cast<TUint32&>( *eventData ) = arr[idx]->iUid.iUid;
       
   226             // Send the event to Widget AppUi.
       
   227             iEnv->WsSession().SendEventToWindowGroup(
       
   228                 iWidgetAppUiWgId, event );
       
   229             // closing a widget may not cause a window group change so trigger
       
   230             // the update manually
       
   231             UpdateTaskList();
       
   232             TSLOG0_OUT( "widget processing finished" );
       
   233             return;
       
   234             }
       
   235         }
       
   236 
       
   237     TUid appUid = AppUidForWgIdL( aWgId );
       
   238     if ( !iAlwaysShownAppList->IsAlwaysShownApp( appUid ) )
       
   239         {
       
   240         // send window group event to kill the app
       
   241         TWsEvent event;
       
   242         event.SetTimeNow();
       
   243         event.SetType( KAknShutOrHideApp );
       
   244         iEnv->WsSession().SendEventToWindowGroup( aWgId, event );
       
   245         TSLOG0( TSLOG_INFO, "event sent to wg" );
       
   246         }
       
   247 
       
   248     TSLOG_OUT();
       
   249     }
       
   250 
       
   251 // --------------------------------------------------------------------------
       
   252 // CTsFswEngine::SwitchToAppL
       
   253 // --------------------------------------------------------------------------
       
   254 //
       
   255 EXPORT_C void CTsFswEngine::SwitchToAppL( TInt aWgId )
       
   256     {
       
   257     TSLOG_CONTEXT( SwitchToAppL, TSLOG_LOCAL );
       
   258     TSLOG1_IN( "aWgId = %d", aWgId );
       
   259 
       
   260     if ( iWidgetsSupported && aWgId < 0 )
       
   261         {
       
   262         // for widgets clients see a wgid that is -1*(index+1)
       
   263         const RWidgetInfoArray& arr( iWidgetList->RunningWidgets() );
       
   264         // convert aWgId to an index in the list of running widgets
       
   265         TInt idx = -aWgId - 1;
       
   266         // if index is valid then handle the widget specially
       
   267         if ( idx >= 0 && idx < arr.Count() )
       
   268             {
       
   269             SwitchToWidgetL( idx );
       
   270             TSLOG1_OUT( "switched to widget %d", idx );
       
   271             return;
       
   272             }
       
   273         }
       
   274 
       
   275     TApaTask task( iEnv->WsSession() );
       
   276     task.SetWgId( aWgId );
       
   277     task.BringToForeground();
       
   278 
       
   279     TSLOG_OUT();
       
   280     }
       
   281 
       
   282 // --------------------------------------------------------------------------
       
   283 // CTsFswEngine::SwitchToWidgetL
       
   284 // --------------------------------------------------------------------------
       
   285 //
       
   286 void CTsFswEngine::SwitchToWidgetL( TInt aWidgetIndex )
       
   287     {
       
   288     const RWidgetInfoArray& arr( iWidgetList->RunningWidgets() );
       
   289     RApaLsSession ls;
       
   290     User::LeaveIfError( ls.Connect() );
       
   291     CleanupClosePushL( ls );
       
   292     CApaCommandLine* cmdLine = CApaCommandLine::NewLC();
       
   293     cmdLine->SetCommandL( EApaCommandRun );
       
   294     TApaAppInfo info;
       
   295     iAppArcSession.GetAppInfo( info, arr[aWidgetIndex]->iUid ); // codescanner::accessArrayElementWithoutCheck2 (aWidgetIndex checked in SwitchToAppL())
       
   296     cmdLine->SetExecutableNameL( info.iFullName );
       
   297     ls.StartApp( *cmdLine );
       
   298     CleanupStack::PopAndDestroy( 2, &ls );
       
   299     }
       
   300 
       
   301 // --------------------------------------------------------------------------
       
   302 // CTsFswEngine::UpdateTaskList
       
   303 // Callback from appui
       
   304 // --------------------------------------------------------------------------
       
   305 //
       
   306 void CTsFswEngine::UpdateTaskList()
       
   307     {
       
   308     TSLOG_CONTEXT( UpdateTaskList, TSLOG_LOCAL );
       
   309     TSLOG_IN();
       
   310 
       
   311     // If no clients are subscribed for fsw content change notifications
       
   312     // then there is no need to waste time with rebuilding the task list,
       
   313     // just set the dirty flag.
       
   314     TInt listenerCount = iObserver.FswDataListenerCount();
       
   315     TSLOG1( TSLOG_INFO, "listener count = %d", listenerCount );
       
   316     if ( listenerCount > 0 )
       
   317         {
       
   318         // There can be many calls in a row, use a timer to prevent degrading
       
   319         // device performance.
       
   320         if ( !iUpdateStarter->IsActive() )
       
   321             {
       
   322             iUpdateStarter->Start( KContentRefreshDelay, 0,
       
   323                 TCallBack( UpdateStarterCallback, this ) );
       
   324             }
       
   325         }
       
   326     else
       
   327         {
       
   328         iTaskListDirty = ETrue;
       
   329         }
       
   330 
       
   331     // screenshot taking support - call Register and Unregister when needed
       
   332     UpdatePreviewContent();
       
   333 
       
   334     // get the foreground app uid and publish it to CFW if different than before
       
   335     TRAP_IGNORE( PublishFgAppUidL() );
       
   336     
       
   337     TSLOG_OUT();
       
   338     }
       
   339 
       
   340 // --------------------------------------------------------------------------
       
   341 // CTsFswEngine::ForegroundAppUidL
       
   342 // --------------------------------------------------------------------------
       
   343 //
       
   344 EXPORT_C TUid CTsFswEngine::ForegroundAppUidL( TInt aType )
       
   345     {
       
   346     // Do not use GetFocusWindowGroup or others.
       
   347     // We want to see only "real" application uids in FgApp,
       
   348     // i.e. stuff that would also be shown in the task swapper
       
   349     // area of Taskswitcher.
       
   350 
       
   351     TUid result = KNullUid;
       
   352     RArray<RWsSession::TWindowGroupChainInfo> allWgIds;
       
   353     CleanupClosePushL( allWgIds );
       
   354     User::LeaveIfError( iWsSession.WindowGroupList( 0, &allWgIds ) );
       
   355     TInt count = allWgIds.Count();
       
   356     for ( TInt i = 0; i < count; ++i )
       
   357         {
       
   358         // Depending on aType we may not need to bother with iParentId here.
       
   359         // If aType == EUseEmbeddedUid => embedded apps are treated as others.
       
   360         // If aType == EUseStandaloneUid => embedded apps must be ignored.
       
   361         if ( allWgIds[i].iParentId > 0
       
   362                 && aType == CTsFswClient::EUseStandaloneUid )
       
   363             {
       
   364             continue;
       
   365             }
       
   366 
       
   367         CApaWindowGroupName* wgn = CApaWindowGroupName::NewLC(
       
   368             iWsSession, allWgIds[i].iId );
       
   369         TUid newUid = wgn->AppUid();
       
   370         TBool hidden = wgn->Hidden() || iHiddenAppList->IsHiddenL(
       
   371             newUid, iWsSession, allWgIds[i].iId );
       
   372         CleanupStack::PopAndDestroy( wgn );
       
   373 
       
   374         if ( !hidden && newUid.iUid )
       
   375             {
       
   376             result = newUid;
       
   377             break;
       
   378             }
       
   379         }
       
   380     CleanupStack::PopAndDestroy( &allWgIds );
       
   381     
       
   382     return result;
       
   383     }
       
   384 
       
   385 // --------------------------------------------------------------------------
       
   386 // CTsFswEngine::PublishFgAppUidL
       
   387 // --------------------------------------------------------------------------
       
   388 //
       
   389 void CTsFswEngine::PublishFgAppUidL()
       
   390     {
       
   391     TSLOG_CONTEXT( PublishFgAppUidL, TSLOG_LOCAL );
       
   392     TSLOG_IN();
       
   393 
       
   394     TUid newUid = ForegroundAppUidL( CTsFswClient::EUseEmbeddedUid );
       
   395     TSLOG2( TSLOG_INFO, "ws says: %x we have: %x", newUid.iUid, iFgAppUid.iUid );
       
   396     if ( iFgAppUid != newUid && newUid.iUid )
       
   397         {
       
   398         iFgAppUid = newUid;
       
   399         iFgAppUidStr.Format( KHexFmt, iFgAppUid.iUid );
       
   400         }
       
   401 
       
   402     TSLOG_OUT();
       
   403     }
       
   404 
       
   405 // --------------------------------------------------------------------------
       
   406 // CTsFswEngine::UpdateStarterCallback
       
   407 // Callback for the timer
       
   408 // --------------------------------------------------------------------------
       
   409 //
       
   410 TInt CTsFswEngine::UpdateStarterCallback( TAny* aParam )
       
   411     {
       
   412     CTsFswEngine* self = static_cast<CTsFswEngine*>( aParam );
       
   413     self->iUpdateStarter->Cancel();
       
   414     // get list of tasks and notify observer if there is a change in the list
       
   415     TBool changed = EFalse;
       
   416     TRAPD( err, changed = self->CollectTasksL() );
       
   417     if ( err == KErrNone && changed )
       
   418         {
       
   419         self->iObserver.FswDataChanged();
       
   420         }
       
   421     return 0;
       
   422     }
       
   423 
       
   424 // --------------------------------------------------------------------------
       
   425 // CTsFswEngine::CollectTasksL
       
   426 // --------------------------------------------------------------------------
       
   427 //
       
   428 TBool CTsFswEngine::CollectTasksL()
       
   429     {
       
   430     TSLOG_CONTEXT( CollectTasksL, TSLOG_LOCAL );
       
   431     TSLOG_IN();
       
   432 
       
   433     // clear dirty flag
       
   434     iTaskListDirty = EFalse;
       
   435 
       
   436     TBool changed = EFalse;
       
   437     RTsFswArray newList;
       
   438     CleanupResetAndDestroyPushL( newList );
       
   439 
       
   440     // update app data if needed
       
   441     // (usually on startup and when new apps might have been installed)
       
   442     if ( iAppDataRefreshNeeded )
       
   443         {
       
   444         TSLOG0( TSLOG_INFO, "refreshing app data" );
       
   445         iAppArcSession.GetAllApps();
       
   446         iAlwaysShownAppList->InitializeAlwaysShownListL();
       
   447         iAppDataRefreshNeeded = EFalse;
       
   448         }
       
   449 
       
   450     // get all window groups
       
   451     RArray<RWsSession::TWindowGroupChainInfo> allWgIds;
       
   452     CleanupClosePushL( allWgIds );
       
   453     User::LeaveIfError( iWsSession.WindowGroupList( 0, &allWgIds ) );
       
   454     TInt count = allWgIds.Count();
       
   455 
       
   456     for ( TInt i = 0; i < count; ++i )
       
   457         {
       
   458         // ignore uninteresting entries (e.g. embedded apps)
       
   459         if ( allWgIds[i].iParentId > 0 )
       
   460             {
       
   461             continue;
       
   462             }
       
   463 
       
   464         // get window group name
       
   465         TInt wgId = allWgIds[i].iId;
       
   466         CApaWindowGroupName* windowName =
       
   467             CApaWindowGroupName::NewLC( iWsSession, wgId );
       
   468         TUid appUid = windowName->AppUid();
       
   469 
       
   470         // ignore entries with null uid
       
   471         if ( !appUid.iUid )
       
   472             {
       
   473             CleanupStack::PopAndDestroy( windowName );
       
   474             continue;
       
   475             }
       
   476 
       
   477         // will append the task to our own list only if it is not hidden
       
   478         TBool onHiddenList = iHiddenAppList->IsHiddenL(
       
   479             appUid, iWsSession, wgId );
       
   480 
       
   481         // if this is the widget app then save wgid for later use
       
   482         // and ignore it, but include running widgets instead
       
   483         if ( iWidgetsSupported && appUid.iUid == KWidgetAppUidValue )
       
   484             {
       
   485             iWidgetAppUiWgId = wgId;
       
   486             onHiddenList = ETrue;
       
   487             CheckWidgetsL( newList );
       
   488             }
       
   489 
       
   490         // get screen number (-1=console, 0=main screen, 1=cover ui)
       
   491         TInt appScreen = 0;
       
   492         TInt scrNumErr = iAppArcSession.GetDefaultScreenNumber( appScreen, appUid );
       
   493 
       
   494         // check if it is on always-shown list
       
   495         TBool mustShow = iAlwaysShownAppList->IsAlwaysShownApp( appUid );
       
   496 
       
   497 #ifdef _DEBUG
       
   498         const TDesC& captionDbg( windowName->Caption() );
       
   499         TSLOG4( TSLOG_INFO, "[%d] wgid=%d appuid=%x (%S)", i, wgId,
       
   500             appUid.iUid, &captionDbg );
       
   501         TSLOG4( TSLOG_INFO, "hidden=%d onhiddenlist=%d mustshow=%d scrno=%d",
       
   502             windowName->Hidden(), onHiddenList, mustShow, appScreen );
       
   503 #endif
       
   504 
       
   505         // add item to task list if it is always-shown OR it is not hidden
       
   506         // and it is not on cover ui
       
   507         if ( mustShow
       
   508                 || ( !onHiddenList
       
   509                     && !windowName->Hidden()
       
   510                     && ( appScreen == 0 || appScreen == -1 )
       
   511                     && scrNumErr == KErrNone ) )
       
   512             {
       
   513             if ( AddEntryL( wgId, appUid, windowName, newList, EFalse ) )
       
   514                 {
       
   515                 changed = ETrue;
       
   516                 }
       
   517             }
       
   518         CleanupStack::PopAndDestroy( windowName );
       
   519         }
       
   520     CleanupStack::PopAndDestroy( &allWgIds );    
       
   521 
       
   522     // if counts for old and new lists do not match then there is a change for sure,
       
   523     // probably an app has been closed
       
   524     if ( iData.Count() != newList.Count() )
       
   525         {
       
   526         changed = ETrue;
       
   527         }
       
   528 
       
   529     // move pointers from the temporary list into the real one
       
   530     iData.ResetAndDestroy();
       
   531     TInt newListCount = newList.Count();
       
   532     TSLOG1( TSLOG_INFO, "new list count = %d", newListCount );
       
   533     for ( TInt i = 0; i < newListCount; ++i )
       
   534         {
       
   535         TSLOG3( TSLOG_INFO, "[%d] %S wgid=%d",
       
   536             i, &newList[i]->AppName(), newList[i]->WgId() );
       
   537         iData.AppendL( newList[i] );
       
   538         newList[i] = 0;
       
   539         }
       
   540     CleanupStack::PopAndDestroy( &newList );
       
   541     
       
   542     TSLOG1_OUT( "change flag = %d", changed );
       
   543     return changed;
       
   544     }
       
   545 
       
   546 // --------------------------------------------------------------------------
       
   547 // CTsFswEngine::AddEntryL
       
   548 // --------------------------------------------------------------------------
       
   549 //
       
   550 TBool CTsFswEngine::AddEntryL( TInt aWgId, const TUid& aAppUid,
       
   551         CApaWindowGroupName* aWgName, RTsFswArray& aNewList,
       
   552         TBool aIsWidget )
       
   553     {
       
   554     TBool changed = EFalse;
       
   555     CTsFswEntry* entry = CTsFswEntry::NewLC();
       
   556     entry->SetWgId( aWgId );
       
   557     entry->SetAppUid( aAppUid );
       
   558     entry->SetWidget( aIsWidget );
       
   559 
       
   560     // check if present in old list and if yes then take some of the old data
       
   561     TBool found = CheckIfExistsL( *entry, aAppUid, changed, aNewList );
       
   562 
       
   563     // if not present previously then find out app name
       
   564     // and check if screenshot is already available
       
   565     if ( !found )
       
   566         {
       
   567         // when adding a new entry to the list it is changed for sure
       
   568         changed = ETrue;
       
   569         HBufC* name = FindAppNameLC( aWgName, aAppUid, aWgId );
       
   570         if ( name )
       
   571             {
       
   572             entry->SetAppNameL( *name );
       
   573             }
       
   574         CleanupStack::PopAndDestroy( name );
       
   575         TInt h = LookupScreenshotHandle( aWgId );
       
   576         if ( h )
       
   577             {
       
   578             entry->SetScreenshotHandle( h );
       
   579             }
       
   580         entry->SetAlwaysShown( iAlwaysShownAppList->IsAlwaysShownApp( aAppUid ) );
       
   581         if ( aWgName )
       
   582             {
       
   583             entry->SetSystemApp( aWgName->IsSystem() );
       
   584             }
       
   585         }
       
   586 
       
   587     // add to new list, ownership is transferred
       
   588     aNewList.AppendL( entry );
       
   589     CleanupStack::Pop( entry );
       
   590     return changed;
       
   591     }
       
   592 
       
   593 // --------------------------------------------------------------------------
       
   594 // CTsFswEngine::CheckIfExistsL
       
   595 // --------------------------------------------------------------------------
       
   596 //
       
   597 TBool CTsFswEngine::CheckIfExistsL( CTsFswEntry& aEntry,
       
   598         const TUid& aAppUid,
       
   599         TBool& aChanged,
       
   600         RTsFswArray& aNewList )
       
   601     {
       
   602     for ( TInt entryIdx = 0, oldCount = iData.Count();
       
   603             entryIdx < oldCount; ++entryIdx )
       
   604         {
       
   605         // Enough to check appuid, no need to bother with wgid as the
       
   606         // screenshot handle is up-to-date or will be updated later anyway.
       
   607         if ( iData[entryIdx]->AppUid() == aAppUid )
       
   608             {
       
   609             // if positions do not match then the list is different than before
       
   610             if ( entryIdx != aNewList.Count() )
       
   611                 {
       
   612                 aChanged = ETrue;
       
   613                 }
       
   614             CTsFswEntry* oldEntry = iData[entryIdx];
       
   615             aEntry.SetAppNameL( oldEntry->AppName() );
       
   616             aEntry.SetScreenshotHandle( oldEntry->ScreenshotHandle() );
       
   617             aEntry.SetAlwaysShown( oldEntry->AlwaysShown() );
       
   618             aEntry.SetSystemApp( oldEntry->SystemApp() );
       
   619             return ETrue;
       
   620             }
       
   621         }
       
   622     return EFalse;
       
   623     }
       
   624     
       
   625 // --------------------------------------------------------------------------
       
   626 // CTsFswEngine::CheckWidgetsL
       
   627 // --------------------------------------------------------------------------
       
   628 //
       
   629 void CTsFswEngine::CheckWidgetsL( RTsFswArray& aNewList )
       
   630     {
       
   631     if ( iWidgetsSupported )
       
   632         {
       
   633         iWidgetList->InitializeWidgetListL();
       
   634         const RWidgetInfoArray& arr( iWidgetList->RunningWidgets() );
       
   635         for ( TInt i = 0, ie = arr.Count(); i != ie; ++i )
       
   636             {
       
   637             // wgid will be a special negative value
       
   638             // windowgroupname is not needed here so pass NULL
       
   639             AddEntryL( -(i+1), arr[i]->iUid, 0, aNewList, ETrue );
       
   640             }
       
   641         }
       
   642     }
       
   643 
       
   644 // --------------------------------------------------------------------------
       
   645 // CTsFswEngine::HiddenAppListUpdated
       
   646 // Callback from the hidden app list watcher
       
   647 // --------------------------------------------------------------------------
       
   648 //
       
   649 void CTsFswEngine::HiddenAppListUpdated()
       
   650     {
       
   651     UpdateTaskList();
       
   652     }
       
   653 
       
   654 // --------------------------------------------------------------------------
       
   655 // CTsFswEngine::AppUidForWgIdL
       
   656 // --------------------------------------------------------------------------
       
   657 //
       
   658 TUid CTsFswEngine::AppUidForWgIdL( TInt aWgId )
       
   659     {
       
   660     CApaWindowGroupName* windowName =
       
   661         CApaWindowGroupName::NewLC( iWsSession, aWgId );
       
   662     TUid appUid = windowName->AppUid();
       
   663     CleanupStack::PopAndDestroy( windowName );
       
   664     return appUid;
       
   665     }
       
   666 
       
   667 // --------------------------------------------------------------------------
       
   668 // CTsFswEngine::FindParentWgId
       
   669 // --------------------------------------------------------------------------
       
   670 //
       
   671 TInt CTsFswEngine::FindParentWgId( TInt aWgId )
       
   672     {
       
   673     TInt parent( KErrNotFound );
       
   674     RArray<RWsSession::TWindowGroupChainInfo> allWgIds;
       
   675     // Ask for window group list from RWsSession
       
   676     TInt error = iWsSession.WindowGroupList( 0, &allWgIds );
       
   677     if ( !error )
       
   678         {
       
   679         TInt count( allWgIds.Count() );
       
   680         for ( TInt i( 0 ); i < count; i++ )
       
   681             {
       
   682             RWsSession::TWindowGroupChainInfo info = allWgIds[i];
       
   683             if ( info.iId == aWgId )
       
   684                 {
       
   685                 parent = info.iParentId;
       
   686                 break;
       
   687                 }
       
   688             }
       
   689         }
       
   690     allWgIds.Close();
       
   691     return parent;
       
   692     }
       
   693 
       
   694 // --------------------------------------------------------------------------
       
   695 // CTsFswEngine::FindAppNameLC
       
   696 // --------------------------------------------------------------------------
       
   697 //
       
   698 HBufC* CTsFswEngine::FindAppNameLC( CApaWindowGroupName* aWindowName,
       
   699                                   const TUid& aAppUid,
       
   700                                   TInt aWgId )
       
   701     {
       
   702     //Retrieve the app name
       
   703     TApaAppInfo info;
       
   704     iAppArcSession.GetAppInfo( info, aAppUid );
       
   705     TPtrC caption = info.iShortCaption;
       
   706 
       
   707     HBufC* tempName = 0;
       
   708     if ( !caption.Length() && aWindowName ) // if not set - use thread name instead
       
   709         {
       
   710         if ( aWindowName->Caption().Length() )
       
   711             {
       
   712             tempName = aWindowName->Caption().AllocL();
       
   713             //put on cleanupstack after the if
       
   714             }
       
   715         else
       
   716             {
       
   717             TThreadId threadId;
       
   718             TInt err = iWsSession.GetWindowGroupClientThreadId(
       
   719                     aWgId, threadId );
       
   720             if ( err == KErrNone )
       
   721                 {
       
   722                 RThread thread;
       
   723                 CleanupClosePushL( thread );
       
   724                 err = thread.Open ( threadId );
       
   725                 if ( err==KErrNone )
       
   726                     {
       
   727                     tempName = thread.Name().AllocL(); // codescanner::forgottoputptroncleanupstack
       
   728                     // tempName put on cleanupstack after the if
       
   729                     }
       
   730                 CleanupStack::PopAndDestroy( &thread );
       
   731                 }
       
   732             }
       
   733         }
       
   734     else
       
   735         {
       
   736         tempName = caption.AllocL();
       
   737         //put on cleanupstack after the if
       
   738         }
       
   739     CleanupStack::PushL( tempName );
       
   740     return tempName;
       
   741     }
       
   742 
       
   743 // --------------------------------------------------------------------------
       
   744 // CTsFswEngine::CopyBitmapL
       
   745 // --------------------------------------------------------------------------
       
   746 //
       
   747 CFbsBitmap* CTsFswEngine::CopyBitmapL( TInt aFbsHandle, TBool aKeepAspectRatio )
       
   748     {
       
   749     CFbsBitmap* ret = new (ELeave) CFbsBitmap;
       
   750     CleanupStack::PushL( ret );
       
   751     CFbsBitmap* bmp = new (ELeave) CFbsBitmap;
       
   752     CleanupStack::PushL( bmp );
       
   753     User::LeaveIfError( bmp->Duplicate( aFbsHandle ) );
       
   754 
       
   755     //Make bitmap width and height equal so that it will look better in FSW control.
       
   756     TSize size  = bmp->SizeInPixels();
       
   757 
       
   758     if ( !aKeepAspectRatio )
       
   759         {
       
   760         if ( size.iHeight > size.iWidth )
       
   761             {
       
   762             size.iHeight = size.iWidth;
       
   763             }
       
   764         else
       
   765             {
       
   766             size.iWidth = size.iHeight;
       
   767             }
       
   768         }
       
   769 
       
   770     // preserve size and mode
       
   771     User::LeaveIfError( ret->Create( size, EColor64K ) );
       
   772 
       
   773     CFbsBitmapDevice* doubleBufferDev = CFbsBitmapDevice::NewL( ret );
       
   774     CleanupStack::PushL( doubleBufferDev );
       
   775 
       
   776     CFbsBitGc* doubleBufferGc = 0;
       
   777     User::LeaveIfError( doubleBufferDev->CreateContext( doubleBufferGc ) );
       
   778     CleanupStack::PushL( doubleBufferGc );
       
   779 
       
   780     // preserve size
       
   781     doubleBufferGc->BitBlt( TPoint( 0, 0 ), bmp, TRect(size) );
       
   782 
       
   783     CleanupStack::PopAndDestroy( doubleBufferGc );
       
   784     CleanupStack::PopAndDestroy( doubleBufferDev );
       
   785     CleanupStack::PopAndDestroy( bmp );
       
   786     CleanupStack::Pop( ret );
       
   787 
       
   788     return ret;
       
   789     }
       
   790 
       
   791 // --------------------------------------------------------------------------
       
   792 // CTsFswEngine::UpdatePreviewContent
       
   793 // --------------------------------------------------------------------------
       
   794 //
       
   795 void CTsFswEngine::UpdatePreviewContent()
       
   796     {
       
   797     TSLOG_CONTEXT( UpdatePreviewContent, TSLOG_LOCAL );
       
   798     TSLOG_IN();
       
   799 
       
   800     RArray<RWsSession::TWindowGroupChainInfo> allWgIds;
       
   801     if ( iWsSession.WindowGroupList( 0, &allWgIds ) == KErrNone )
       
   802         {
       
   803         // check window groups that do not exist anymore
       
   804         for ( TInt i = 0, ie = iWgIds.Count(); i != ie; ++i )
       
   805             {
       
   806             TInt wgid = iWgIds[i];
       
   807             TBool present = EFalse;
       
   808             for ( TInt j = 0, je = allWgIds.Count(); j != je; ++j )
       
   809                 {
       
   810                 if ( allWgIds[j].iId == wgid )
       
   811                     {
       
   812                     present = ETrue;
       
   813                     break;
       
   814                     }
       
   815                 }
       
   816             if ( !present )
       
   817                 {
       
   818                 TSLOG1( TSLOG_INFO, "unregister: %d", wgid );
       
   819                 iPreviewProvider->Unregister( wgid );
       
   820                 }
       
   821             }
       
   822         // check topmost window group
       
   823         if ( allWgIds.Count() )
       
   824             {
       
   825             if ( !iWgIds.Count() || allWgIds[0].iId != iWgIds[0] )
       
   826                 {
       
   827                 TInt wgid = allWgIds[0].iId;
       
   828                 TSLOG1( TSLOG_INFO, "registering %d", wgid );
       
   829                 iPreviewProvider->Register( wgid );
       
   830                 }
       
   831             }
       
   832         // store the new list
       
   833         iWgIds.Reset();
       
   834         for ( TInt i = 0, ie = allWgIds.Count(); i != ie; ++i )
       
   835             {
       
   836             iWgIds.Append( allWgIds[i].iId );
       
   837             }
       
   838         }
       
   839     allWgIds.Close();
       
   840 
       
   841     TSLOG_OUT();
       
   842     }
       
   843 
       
   844 // --------------------------------------------------------------------------
       
   845 // CTsFswEngine::HandleFswPpApplicationChange
       
   846 // Callback from CTsFastSwapPreviewProvider
       
   847 // Note: aWgId is for the window group for which the screenshot is taken,
       
   848 // it may not be the same as the application's wgid (in case of embedded apps)
       
   849 // --------------------------------------------------------------------------
       
   850 //
       
   851 void CTsFswEngine::HandleFswPpApplicationChange( TInt aWgId, TInt aFbsHandle )
       
   852     {
       
   853     TSLOG_CONTEXT( HandleFswPpApplicationChange, TSLOG_LOCAL );
       
   854     TSLOG2_IN( "aWgId = %d aFbsHandle = %d", aWgId, aFbsHandle );
       
   855 
       
   856     CFbsBitmap* bmp = 0;
       
   857     TRAPD( err, bmp = CopyBitmapL( aFbsHandle, EFalse ) );
       
   858     iPreviewProvider->AckPreview(aFbsHandle);
       
   859     if ( err == KErrNone )
       
   860         {
       
   861         CFbsBitmap** oldbmp = iScreenshots.Find( aWgId );
       
   862         if ( oldbmp )
       
   863             {
       
   864             delete *oldbmp;
       
   865             }
       
   866         if ( iScreenshots.Insert( aWgId, bmp ) != KErrNone )
       
   867             {
       
   868             delete bmp;
       
   869             iScreenshots.Remove( aWgId );
       
   870             }
       
   871         else
       
   872             {
       
   873             AssignScreenshotHandle( aWgId, bmp->Handle() );
       
   874             }
       
   875         }
       
   876 
       
   877     TSLOG_OUT();
       
   878     }
       
   879 
       
   880 // --------------------------------------------------------------------------
       
   881 // CTsFswEngine::HandleFswPpApplicationUnregistered
       
   882 // Callback from CTsFastSwapPreviewProvider
       
   883 // --------------------------------------------------------------------------
       
   884 //
       
   885 void CTsFswEngine::HandleFswPpApplicationUnregistered( TInt aWgId )
       
   886     {
       
   887     TSLOG_CONTEXT( HandleFswPpApplicationUnregistered, TSLOG_LOCAL );
       
   888     TSLOG1_IN( "aWgId = %d", aWgId );
       
   889 
       
   890     CFbsBitmap** bmp = iScreenshots.Find( aWgId );
       
   891     if ( bmp )
       
   892         {
       
   893         delete *bmp;
       
   894         iScreenshots.Remove( aWgId );
       
   895         AssignScreenshotHandle( aWgId, 0 );
       
   896         }
       
   897 
       
   898     TSLOG_OUT();
       
   899     }
       
   900 
       
   901 // --------------------------------------------------------------------------
       
   902 // CTsFswEngine::AssignScreenshotHandle
       
   903 // Called when a screenshot arrives to check if there is a corresponding
       
   904 // application in the task list.
       
   905 // --------------------------------------------------------------------------
       
   906 //
       
   907 void CTsFswEngine::AssignScreenshotHandle( TInt aWgIdForScreenshot,
       
   908         TInt aBitmapHandle )
       
   909     {
       
   910     TBool changed = EFalse;
       
   911     TInt parentWgId = FindParentWgId( aWgIdForScreenshot );
       
   912     // now parentWgId is a valid wgid or KErrNotFound (-1)
       
   913     for ( TInt i = 0, ie = iData.Count(); i != ie; ++i )
       
   914         {
       
   915         if ( iData[i]->Widget() )
       
   916             {
       
   917             // Do not do anything for now => no screenshot for widgets.
       
   918             continue;
       
   919             }
       
   920         TInt appWgId = iData[i]->WgId();
       
   921         if ( appWgId == aWgIdForScreenshot || appWgId == parentWgId )
       
   922             {
       
   923             iData[i]->SetScreenshotHandle( aBitmapHandle );
       
   924             changed = ETrue;
       
   925             break;
       
   926             }
       
   927         }
       
   928     if ( changed )
       
   929         {
       
   930         iObserver.FswDataChanged();
       
   931         }
       
   932     }
       
   933 
       
   934 // --------------------------------------------------------------------------
       
   935 // CTsFswEngine::LookupScreenshotHandle
       
   936 // Called to check if there is a screenshot already stored for the given
       
   937 // app. (or a screenshot with a wgid for which aWgIdForApp is the parent)
       
   938 // --------------------------------------------------------------------------
       
   939 //
       
   940 TInt CTsFswEngine::LookupScreenshotHandle( TInt aWgIdForApp )
       
   941     {
       
   942     CFbsBitmap** bmp = iScreenshots.Find( aWgIdForApp );
       
   943     if ( bmp )
       
   944         {
       
   945         // there is a screenshot stored for this wgid
       
   946         return (*bmp)->Handle();
       
   947         }
       
   948     else if ( aWgIdForApp > 0 )
       
   949         {
       
   950         // must check if there is a screenshot for which aWgIdForApp is the parent
       
   951         THashMapIter<TInt, CFbsBitmap*> iter( iScreenshots );
       
   952         while ( const TInt* wgid = iter.NextKey() )
       
   953             {
       
   954             if ( FindParentWgId( *wgid ) == aWgIdForApp )
       
   955                 {
       
   956                 CFbsBitmap** bmp = iter.CurrentValue();
       
   957                 return (*bmp)->Handle();
       
   958                 }
       
   959             }
       
   960         }
       
   961     else if ( aWgIdForApp < 0 )
       
   962         {
       
   963         // Must be a widget, wgid is useless in this case.
       
   964         // Do not do anything for now => no screenshot for widgets.
       
   965         }
       
   966     return 0;
       
   967     }
       
   968 
       
   969 // --------------------------------------------------------------------------
       
   970 // CTsFswEngine::RunL
       
   971 // --------------------------------------------------------------------------
       
   972 //
       
   973 void CTsFswEngine::RunL()
       
   974     {
       
   975     if ( iStatus != KErrCancel ) // when cancelling the subscribe it completes with KErrCancel
       
   976         {
       
   977         TInt value = 0;
       
   978         if ( iSwiProp.Get( value ) == KErrNone )
       
   979             {
       
   980             TInt operationStatus( value & Swi::KSwisOperationStatusMask );
       
   981             // When an (un)installation has finished change the flag to
       
   982             // refresh the app list during the next task list update.
       
   983             if ( operationStatus == Swi::ESwisStatusSuccess )
       
   984                 {
       
   985                 iAppDataRefreshNeeded = ETrue;
       
   986                 }
       
   987             }
       
   988         iSwiProp.Subscribe( iStatus );
       
   989         SetActive();
       
   990         }
       
   991     }
       
   992 
       
   993 // --------------------------------------------------------------------------
       
   994 // CTsFswEngine::DoCancel
       
   995 // --------------------------------------------------------------------------
       
   996 //
       
   997 void CTsFswEngine::DoCancel()
       
   998     {
       
   999     iSwiProp.Cancel();
       
  1000     }
       
  1001 
       
  1002 // --------------------------------------------------------------------------
       
  1003 // CTsFswEngine::RunError
       
  1004 // --------------------------------------------------------------------------
       
  1005 //
       
  1006 TInt CTsFswEngine::RunError( TInt /*aError*/ )
       
  1007     {
       
  1008     return KErrNone;
       
  1009     }
       
  1010 
       
  1011 // --------------------------------------------------------------------------
       
  1012 // CTsFswEngine::HandleResourceChange
       
  1013 // callback from appui's HandleResourceChangeL
       
  1014 // --------------------------------------------------------------------------
       
  1015 //
       
  1016 void CTsFswEngine::HandleResourceChange( TInt aType )
       
  1017     {
       
  1018     TSLOG_CONTEXT( HandleResourceChange, TSLOG_LOCAL );
       
  1019     TSLOG_IN();
       
  1020 
       
  1021     if ( iPreviewProvider && aType == KEikDynamicLayoutVariantSwitch )
       
  1022         {
       
  1023         SetPreviewParams();
       
  1024         }
       
  1025 
       
  1026     TSLOG_OUT();
       
  1027     }
       
  1028 
       
  1029 // --------------------------------------------------------------------------
       
  1030 // CTsFswEngine::SetPreviewParams
       
  1031 // --------------------------------------------------------------------------
       
  1032 //
       
  1033 void CTsFswEngine::SetPreviewParams()
       
  1034     {
       
  1035     TSize screenSize = iEnv->ScreenDevice()->SizeInPixels();
       
  1036     iPreviewProvider->SetPreviewParam(
       
  1037         TSize( screenSize.iWidth / KScreenSizeFactor,
       
  1038             screenSize.iHeight / KScreenSizeFactor ),
       
  1039         EColor64K ); // displaymode is ignored
       
  1040     }
       
  1041 
       
  1042     
       
  1043 // end of file