taskswitcher/taskswitcherui/taskswitcherapp/src/tsappui.cpp
branchRCL_3
changeset 34 5456b4e8b3a8
child 35 3321d3e205b6
equal deleted inserted replaced
33:5f0182e07bfb 34:5456b4e8b3a8
       
     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:  AppUi class
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "tsappui.h"
       
    20 #include "tsappview.h"
       
    21 #include "tsapplogging.h"
       
    22 #include "tsdevicestate.h"
       
    23 #include "tsuid.hrh"
       
    24 #include "tscommands.hrh"
       
    25 #include <akntitle.h>
       
    26 #include <hlplch.h>
       
    27 #include <avkon.rsg>
       
    28 #include <bautils.h>
       
    29 #include <AknUtils.h>
       
    30 #include <taskswitcher.rsg>
       
    31 #include <pslninternalcrkeys.h>
       
    32 #include <oommonitorsession.h>
       
    33 #include <hal.h>
       
    34 #include <hal_data.h>
       
    35 #include <akntranseffect.h>
       
    36 #include <UikonInternalPSKeys.h>
       
    37 #include <apgwgnam.h>
       
    38 
       
    39 
       
    40 // AknCapServer UID, used for P&S category
       
    41 const TUid KTaskswitcherStateCategory = { 0x10207218 };
       
    42 
       
    43 // Taskswitcher UI, used as P&S key
       
    44 const TInt KTaskswitcherStateKey = KTsAppUidValue;
       
    45 
       
    46 const TUid KTransitionsUid = {0x10281F90};
       
    47 
       
    48 // Values for Taskswitcher launching P&S
       
    49 const TInt KTaskswitcherBackgroundValue = 1;
       
    50 const TInt KTaskswitcherForegroundValue = KTaskswitcherBackgroundValue << 1;
       
    51 const TInt KTaskswitcherShortAppKeyPressed = KTaskswitcherForegroundValue << 1;
       
    52 const TInt KTaskswitcherLongAppKeyPressed = KTaskswitcherShortAppKeyPressed << 1;
       
    53 
       
    54 //short/long App key values 
       
    55 const TInt KAppKeyTypeShort = 1;
       
    56 const TInt KAppKeyTypeLong = 2;
       
    57 
       
    58 //values for checking the OOM
       
    59 const TInt KMemoryRequestAmountInBytes = 524288;
       
    60 const TInt KMinMemoryAmountInBytes = 524288;
       
    61 
       
    62 // time to wait before sending the task to background
       
    63 // (must give time to animation)
       
    64 const TInt KWaitBeforeGoingToBackground = 200000;
       
    65 
       
    66 const TUid KTsAppUid = { KTsAppUidValue };
       
    67 
       
    68 const TUid KTsCameraUid = { 0x101F857a };
       
    69 const TUid KTsTelephoneUid = { 0x100058b3 };
       
    70 
       
    71 const TInt KTsMultimediaAppActivationDelay = 1500000;
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CTsAppUi::ConstructL()
       
    75 // ConstructL is called by the application framework
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 void CTsAppUi::ConstructL()
       
    79     {
       
    80     TSLOG_CONTEXT( CTsAppUi::ConstructL, TSLOG_LOCAL );
       
    81     TSLOG_IN();
       
    82 
       
    83 #ifdef _DEBUG
       
    84     // create log folder
       
    85 	_LIT( KLogPath, "c:\\logs\\taskswitcher\\" );
       
    86 	RFs fs;
       
    87 	if ( fs.Connect() == KErrNone )
       
    88 		{
       
    89 		fs.MkDirAll( KLogPath );
       
    90 		fs.Close();
       
    91 		}
       
    92 #endif
       
    93 
       
    94     // Initialise app UI with standard value.
       
    95     BaseConstructL( CAknAppUi::EAknEnableSkin | 
       
    96 					CAknAppUi::EAknEnableMSK |
       
    97 					CAknAppUi::EAknSingleClickCompatible);
       
    98     SetFullScreenApp(EFalse);
       
    99 
       
   100 #ifndef _DEBUG
       
   101     // set as system application (in release build) so we never get closed
       
   102     iEikonEnv->SetSystem( ETrue );
       
   103 #endif
       
   104 
       
   105     //Initialize effects change observation
       
   106     iThemeEffectsEnabledWatcher =
       
   107         CTsCenrepListener::NewL( KCRUidThemes, KThemesTransitionEffects, *this );
       
   108     CenrepChanged( KThemesTransitionEffects, iThemeEffectsEnabledWatcher->Value() );
       
   109     
       
   110     // Hide status pane
       
   111     StatusPane()->MakeVisible( EFalse );
       
   112 
       
   113     // Change CBA if needed, default is non-touch in rss
       
   114     if ( AknLayoutUtils::PenEnabled() )
       
   115         {
       
   116         Cba()->SetCommandSetL( R_TS_CBA_TOUCH );
       
   117         }
       
   118     Cba()->MakeVisible(EFalse);
       
   119 
       
   120     // Create timer
       
   121     iGoToBackgroundTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
   122     iForegroundDelayTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
   123 
       
   124     // Create commonly used instances (device state only?)
       
   125     iDeviceState = CTsDeviceState::NewL();
       
   126 
       
   127     // Create custom window group
       
   128     iWg = RWindowGroup(CCoeEnv::Static()->WsSession());
       
   129     iWg.Construct((TUint32)&iWg, ETrue);
       
   130     iWg.EnableScreenChangeEvents(); 
       
   131     CApaWindowGroupName* rootWgName = CApaWindowGroupName::NewLC( iEikonEnv->WsSession(), iEikonEnv->RootWin().Identifier() );
       
   132     rootWgName->SetWindowGroupName( iWg );
       
   133     CleanupStack::PopAndDestroy( rootWgName );
       
   134     
       
   135     // Create UI
       
   136     iAppView = CTsAppView::NewL( ApplicationRect(), *iDeviceState, iWg );
       
   137     AddToStackL( iAppView );
       
   138     
       
   139     //Enable effects
       
   140     GfxTransEffect::Enable();
       
   141     GfxTransEffect::Register(iAppView,KTransitionsUid);
       
   142     GfxTransEffect::SetTransitionObserver(this);
       
   143 
       
   144     // Listen for change in the value of the ts state property.
       
   145     iPropListener = new ( ELeave ) CTsPropertyListener(
       
   146             KTaskswitcherStateCategory, KTaskswitcherStateKey, *this );
       
   147     
       
   148     // Listen for layout changes
       
   149     iLayoutListener = new ( ELeave ) CTsPropertyListener(
       
   150             KPSUidUikon, KUikLayoutState, *this );
       
   151 
       
   152     // Initialise the application task object with the window group id of
       
   153     // our application ( so that it represent our app )
       
   154     //iApplicationTask.SetWgId( iCoeEnv->RootWin().Identifier() );
       
   155     iApplicationTask.SetWgId( iWg.Identifier() );
       
   156 
       
   157     // And finally, go to background.
       
   158     MoveAppToBackground( ENoneTransition );
       
   159     
       
   160     iEikonEnv->RootWin().SetOrdinalPosition(-1, ECoeWinPriorityNeverAtFront);
       
   161     iEikonEnv->RootWin().EnableReceiptOfFocus(EFalse);
       
   162     
       
   163     RWindowGroup& windowGroup = CCoeEnv::Static()->RootWin();
       
   164     windowGroup.EnableGroupListChangeEvents();
       
   165     
       
   166     iIsPopUpShown = EFalse;
       
   167     iUiStarted = EFalse;
       
   168     iDisableAppKeyHandling = EFalse;
       
   169     
       
   170     // Set process priority to high to avoid being
       
   171     // locked by heavy ui apps processing
       
   172     iEikonEnv->WsSession().ComputeMode(RWsSession::EPriorityControlDisabled);
       
   173     RProcess().SetPriority(EPriorityHigh);
       
   174     
       
   175     iForegroundDelayed = EFalse;
       
   176     iDelayedForegroundInProgress = EFalse;
       
   177     
       
   178     TSLOG_OUT();
       
   179     }
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // CTsAppUi::CTsAppUi()
       
   183 // Perform the first phase of two phase construction
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 CTsAppUi::CTsAppUi(): iForeground( ETrue ),
       
   187                                       iApplicationTask( iCoeEnv->WsSession() )
       
   188     {
       
   189     // no implementation required
       
   190     }
       
   191 
       
   192 // -----------------------------------------------------------------------------
       
   193 // CTsAppUi::~CTsAppUi()
       
   194 // Destructor
       
   195 // -----------------------------------------------------------------------------
       
   196 //
       
   197 CTsAppUi::~CTsAppUi()
       
   198     {
       
   199     if( GfxTransEffect::IsRegistered( iAppView ) )
       
   200         {
       
   201         GfxTransEffect::Deregister(iAppView);
       
   202         
       
   203         }
       
   204     GfxTransEffect::SetTransitionObserver(0);
       
   205     
       
   206     delete iGoToBackgroundTimer;
       
   207     delete iForegroundDelayTimer;
       
   208     delete iPropListener;
       
   209     delete iLayoutListener;
       
   210 
       
   211     // destroy UI first
       
   212     if ( iAppView )
       
   213         {
       
   214         RemoveFromStack( iAppView );
       
   215         delete iAppView;
       
   216         }
       
   217 
       
   218     delete iDeviceState;
       
   219     delete iThemeEffectsEnabledWatcher;
       
   220     
       
   221     iWg.Close();
       
   222     }
       
   223 
       
   224 // -----------------------------------------------------------------------------
       
   225 // CTsAppView::EffectsEnabled
       
   226 // -----------------------------------------------------------------------------
       
   227 //
       
   228 TBool CTsAppUi::EffectsEnabled() const
       
   229     {
       
   230     return iEffectsEnabled;
       
   231     }
       
   232 
       
   233 // -----------------------------------------------------------------------------
       
   234 // CTsAppUi::StartTransion
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 void CTsAppUi::StartTransion( TUint aTransitionType, TUid aNextAppUid, TInt aWgId )
       
   238     {
       
   239     if( !EffectsEnabled() )
       
   240         {
       
   241         return;
       
   242         }
       
   243     switch(aTransitionType)
       
   244         {
       
   245     case EForegroundTransition:
       
   246         StartTransition( aTransitionType, 
       
   247                          ETrue, 
       
   248                          EFalse, 
       
   249                          CAknTransitionUtils::EForceVisible);
       
   250         break;
       
   251     case EBackgroundTransition:
       
   252         StartTransition( aTransitionType, 
       
   253                          EFalse, 
       
   254                          EFalse, 
       
   255                          CAknTransitionUtils::EForceInvisible );
       
   256         break;
       
   257     case EActivationTransition:
       
   258         StartAppActivateTransition( aNextAppUid, aWgId );
       
   259         break;
       
   260         }
       
   261     }
       
   262 
       
   263 // -----------------------------------------------------------------------------
       
   264 // CTsAppUi::StartTransition
       
   265 // -----------------------------------------------------------------------------
       
   266 //
       
   267 void CTsAppUi::StartTransition( TUint aTranstionId,
       
   268                                 TBool aVisibility,
       
   269                                 TBool /*aLayers*/, 
       
   270                                 TUint aSubCom )
       
   271     {
       
   272     const TDesC8* ptr = reinterpret_cast<const TDesC8*>(iAppView);
       
   273     GfxTransEffect::Abort(iAppView);
       
   274     GfxTransEffect::Begin( iAppView, aTranstionId );
       
   275     GfxTransEffect::SetDemarcation( iAppView, iAppView->Rect() );
       
   276     GfxTransEffect::NotifyExternalState( ECaptureComponentsBegin, ptr );
       
   277     iAppView->MakeVisible( aVisibility );
       
   278     CAknTransitionUtils::MakeVisibleSubComponents( 
       
   279         iAppView,
       
   280         static_cast<CAknTransitionUtils::TMakeVisibleSubComponentsInfo>(aSubCom) );
       
   281     GfxTransEffect::NotifyExternalState( ECaptureComponentsEnd, ptr );
       
   282     GfxTransEffect::End( iAppView );
       
   283     }
       
   284 
       
   285 // -----------------------------------------------------------------------------
       
   286 // CTsAppUi::StartAppActivateTransition
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 void CTsAppUi::StartAppActivateTransition( TUid aNextAppUid, TInt aWgId )
       
   290     {
       
   291     // Check what type of transition will be trigerred
       
   292     if ( aWgId == iUnderAppWgId )
       
   293         {
       
   294         // App under task switcher was launched
       
   295         StartTransition( EBackgroundTransition, 
       
   296                          EFalse, 
       
   297                          ETrue, 
       
   298                          CAknTransitionUtils::EForceInvisible );
       
   299         }
       
   300     else
       
   301         {
       
   302         // App start animation
       
   303         const TDesC8* ptr = reinterpret_cast<const TDesC8*>(iAppView);
       
   304         GfxTransEffect::Abort(iAppView);
       
   305         TInt groupId = GfxTransEffect::BeginGroup();
       
   306         GfxTransEffect::BeginFullScreen(
       
   307                  EActivationAppShowTransition, ApplicationRect(),
       
   308                  AknTransEffect::EParameterType,
       
   309                  AknTransEffect::GfxTransParam( aNextAppUid , KTsAppUid ) );
       
   310         GfxTransEffect::Begin( iAppView, EBackgroundTransition );
       
   311         GfxTransEffect::SetDemarcation( iAppView, iAppView->Rect() );
       
   312         GfxTransEffect::NotifyExternalState( ECaptureComponentsBegin, ptr );
       
   313         iAppView->MakeVisible( EFalse );
       
   314         CAknTransitionUtils::MakeVisibleSubComponents( 
       
   315             iAppView,
       
   316             static_cast<CAknTransitionUtils::TMakeVisibleSubComponentsInfo>(CAknTransitionUtils::EForceInvisible) );
       
   317         GfxTransEffect::NotifyExternalState( ECaptureComponentsEnd, ptr );
       
   318         GfxTransEffect::End( iAppView );
       
   319         GfxTransEffect::EndFullScreen();
       
   320         GfxTransEffect::EndGroup(groupId);
       
   321         }
       
   322     }
       
   323 
       
   324 // -----------------------------------------------------------------------------
       
   325 // CTsAppUi::TransitionFinished
       
   326 // -----------------------------------------------------------------------------
       
   327 //
       
   328 void CTsAppUi::TransitionFinished(const CCoeControl* /*aControl*/, 
       
   329                                   TUint /*aAction*/)
       
   330     {
       
   331     }
       
   332 
       
   333 // -----------------------------------------------------------------------------
       
   334 // CTsAppUi::HandleCommandL()
       
   335 // Takes care of command handling.
       
   336 // -----------------------------------------------------------------------------
       
   337 //
       
   338 void CTsAppUi::HandleCommandL( TInt aCommand )
       
   339     {
       
   340     switch ( aCommand )
       
   341         {
       
   342         case EAknCmdExit:
       
   343         case EEikCmdExit:
       
   344             Exit();
       
   345             break;
       
   346 
       
   347         case EAknSoftkeyExit:
       
   348         case EAknSoftkeyBack:
       
   349             // RSK => just hide
       
   350             MoveAppToBackground( EBackgroundTransition );
       
   351             break;
       
   352 
       
   353         case ETsCmdHelp:
       
   354             {
       
   355             MoveAppToBackground( EBackgroundTransition );
       
   356             CArrayFix<TCoeHelpContext>* buf = CCoeAppUi::AppHelpContextL();
       
   357             HlpLauncher::LaunchHelpApplicationL( iCoeEnv->WsSession(), buf );
       
   358             }
       
   359             break;
       
   360 
       
   361         case EAknSoftkeySelect:
       
   362             {
       
   363             // select is generated by lsk and msk (see the resource file)
       
   364             // Simulate a middle key press to the controls.
       
   365             // Note that SimulateKeyEventL must not be used here or else it
       
   366             // will end up in an infinite loop.
       
   367             TKeyEvent keyEvent;
       
   368             keyEvent.iCode = EKeyOK;
       
   369             keyEvent.iScanCode = EStdKeyDevice3;
       
   370             keyEvent.iModifiers = keyEvent.iRepeats = 0;
       
   371             iAppView->OfferKeyEventL( keyEvent, EEventKeyDown );
       
   372             iAppView->OfferKeyEventL( keyEvent, EEventKey );
       
   373             iAppView->OfferKeyEventL( keyEvent, EEventKeyUp );
       
   374             }
       
   375             break;
       
   376 
       
   377         default:
       
   378             break;
       
   379         }
       
   380     }
       
   381 
       
   382 // -----------------------------------------------------------------------------
       
   383 // CTsAppUi::HandleForegroundEventL
       
   384 // Called by the system when the app is moved to foreground or background.
       
   385 // -----------------------------------------------------------------------------
       
   386 //
       
   387 void CTsAppUi::HandleForegroundEventL( TBool aForeground )
       
   388     {
       
   389     TSLOG_CONTEXT( HandleForegroundEventL, TSLOG_LOCAL );
       
   390     TSLOG1_IN( "aForeground = %d", aForeground );
       
   391 
       
   392     // These calls may be redundant but the functions will do nothing if the
       
   393     // state has already been changed.
       
   394     // Both this function and the 'manual' MoveAppTo functions must fire the events
       
   395     // because in some cases only one of them will run (e.g. when bringing to foreground
       
   396     // not with the hw key but by other means etc.)
       
   397     iDisableAppKeyHandling = EFalse;
       
   398     if ( !iUiStarted )
       
   399         {
       
   400         // Ignore foreground events if UI is starting
       
   401         return;
       
   402         }
       
   403     if ( aForeground )
       
   404         {
       
   405         HandleSwitchToForegroundEvent();
       
   406         }
       
   407     // exclude cases with dialogs like power menu, memory card
       
   408     else
       
   409         {
       
   410         if( !IsFaded() )
       
   411             {
       
   412             HandleSwitchToBackgroundEvent();
       
   413             }
       
   414         else
       
   415             {
       
   416             iDisableAppKeyHandling = ETrue;
       
   417             }
       
   418         }
       
   419 
       
   420     // Call Base class method
       
   421     CAknAppUi::HandleForegroundEventL( aForeground );
       
   422 
       
   423     TSLOG_OUT();
       
   424     }
       
   425 
       
   426 // -----------------------------------------------------------------------------
       
   427 // CTsAppUi::PropertyChanged
       
   428 // -----------------------------------------------------------------------------
       
   429 //
       
   430 void CTsAppUi::PropertyChanged( TUid aCategory, TUint aKey )
       
   431     {
       
   432     TSLOG_CONTEXT( PropertyChanged, TSLOG_LOCAL );
       
   433     TSLOG_IN();
       
   434 
       
   435     TInt value( 0 );
       
   436 
       
   437     if ( aCategory == KTaskswitcherStateCategory )
       
   438         {
       
   439         if ( RProperty::Get( aCategory, aKey, value ) == KErrNone )
       
   440             {
       
   441             if ( iForeground && (value & KTaskswitcherBackgroundValue) )
       
   442                 {
       
   443                 MoveAppToBackground( EBackgroundTransition );
       
   444                 }
       
   445             else if ( !iForeground && (value & KTaskswitcherForegroundValue) )
       
   446                 {
       
   447                 MoveAppToForeground( EForegroundTransition );
       
   448                 }
       
   449             else if( value & KTaskswitcherLongAppKeyPressed )
       
   450                 {
       
   451                 if(!iForeground)
       
   452                     {
       
   453                     MoveAppToBackground( EBackgroundTransition );
       
   454                     }
       
   455                 else if( !iDisableAppKeyHandling )
       
   456                     {
       
   457                     iAppView->HandleAppKey(KAppKeyTypeLong);
       
   458                     }
       
   459                 }
       
   460             else if(  value & KTaskswitcherShortAppKeyPressed && !iDisableAppKeyHandling )
       
   461                 {
       
   462                 iAppView->HandleAppKey(KAppKeyTypeShort);
       
   463                 }
       
   464             }
       
   465         }
       
   466     else if ( aCategory == KPSUidUikon && iIsPopUpShown )
       
   467         {
       
   468         TRAP_IGNORE(
       
   469             DisablePopUpL();
       
   470             HandleResourceChangeL(KEikDynamicLayoutVariantSwitch) );
       
   471         }
       
   472 
       
   473     TSLOG_OUT();
       
   474     }
       
   475 
       
   476 // -----------------------------------------------------------------------------
       
   477 // CTsAppUi::HandleResourceChangeL
       
   478 // -----------------------------------------------------------------------------
       
   479 //
       
   480 void CTsAppUi::HandleResourceChangeL( TInt aType )
       
   481     {
       
   482     TSLOG_CONTEXT( CTsAppUi::HandleResourceChangeL, TSLOG_LOCAL );
       
   483     TSLOG_IN();
       
   484     // Must call base class implementation first,
       
   485     // sizes from LayoutMetricsRect etc. will only be correct after this.
       
   486     CAknAppUi::HandleResourceChangeL( aType );
       
   487     if( aType == KEikDynamicLayoutVariantSwitch && iAppView && !LayoutChangeAllowed() )
       
   488         {
       
   489         // Keep displayed orientation
       
   490         return;
       
   491         }
       
   492     // forward event
       
   493     iDeviceState->HandleResourceChange( aType );
       
   494     if ( iAppView )
       
   495         {
       
   496         iAppView->HandleResourceChange( aType );
       
   497         }
       
   498     TSLOG_OUT();
       
   499     }
       
   500 
       
   501 // -----------------------------------------------------------------------------
       
   502 // CTsAppUi::MoveAppToBackground()
       
   503 // -----------------------------------------------------------------------------
       
   504 //
       
   505 void CTsAppUi::MoveAppToBackground( TUint aTransitionType, TUid aAppUid, TInt aWgId )
       
   506     {
       
   507     TSLOG_CONTEXT( MoveAppToBackground, TSLOG_LOCAL );
       
   508     TSLOG_IN();
       
   509 
       
   510     if ( ENoneTransition == aTransitionType || !EffectsEnabled() )
       
   511         {
       
   512         GoToBackgroundTimerCallback( this );
       
   513         }
       
   514     else
       
   515         {
       
   516         StartTransion(aTransitionType, aAppUid, aWgId);
       
   517         iGoToBackgroundTimer->Cancel();
       
   518         iGoToBackgroundTimer->Start( 
       
   519                 KWaitBeforeGoingToBackground, 
       
   520                 0,
       
   521                 TCallBack( GoToBackgroundTimerCallback, this ) );
       
   522         }
       
   523     TSLOG_OUT();
       
   524     }
       
   525 
       
   526 // -----------------------------------------------------------------------------
       
   527 // CTsAppUi::CenrepChanged
       
   528 // -----------------------------------------------------------------------------
       
   529 //
       
   530 void CTsAppUi::CenrepChanged( TUint32 /*aKey*/, TInt aNewValue )
       
   531     {
       
   532     iEffectsEnabled = !(aNewValue & AknTransEffect::EFullScreenTransitionsOff);
       
   533     }
       
   534 
       
   535 // -----------------------------------------------------------------------------
       
   536 // CTsAppUi::GoToBackgroundTimerCallback
       
   537 // -----------------------------------------------------------------------------
       
   538 //
       
   539 TInt CTsAppUi::GoToBackgroundTimerCallback( TAny* aParam )
       
   540     {
       
   541     CTsAppUi* self = static_cast<CTsAppUi*>( aParam );
       
   542     if ( self->iGoToBackgroundTimer )
       
   543         {
       
   544         self->iGoToBackgroundTimer->Cancel();
       
   545         }
       
   546 
       
   547     // Request window server to bring our application
       
   548     // to background
       
   549     self->iApplicationTask.SendToBackground();
       
   550 
       
   551     // Notify
       
   552     self->HandleSwitchToBackgroundEvent();
       
   553 
       
   554     return 0;
       
   555     }
       
   556 
       
   557 // -----------------------------------------------------------------------------
       
   558 // CTsAppUi::DelayedForegroundCallback
       
   559 // -----------------------------------------------------------------------------
       
   560 //
       
   561 TInt CTsAppUi::DelayedForegroundCallback( TAny* aParam )
       
   562     {
       
   563     CTsAppUi* self = static_cast<CTsAppUi*>( aParam );
       
   564     self->iDelayedForegroundInProgress = ETrue;
       
   565     
       
   566     if ( self->iForegroundDelayTimer )
       
   567         {
       
   568         self->iForegroundDelayTimer->Cancel();
       
   569         }
       
   570     self->iForegroundDelayed = EFalse;
       
   571     self->HandleSwitchToForegroundEvent();
       
   572     
       
   573     self->iDelayedForegroundInProgress = EFalse;
       
   574     
       
   575     return 0;
       
   576     }
       
   577 
       
   578 // -----------------------------------------------------------------------------
       
   579 // CTsAppUi::MoveAppToForeground()
       
   580 // -----------------------------------------------------------------------------
       
   581 //
       
   582 void CTsAppUi::MoveAppToForeground( TUint  /*aTransitionType*/ )
       
   583     {
       
   584     TSLOG_CONTEXT( MoveAppToForeground, TSLOG_LOCAL );
       
   585     TSLOG_IN();
       
   586 
       
   587     iUiStarted = ETrue;
       
   588     
       
   589     // Request window server to bring our application
       
   590     // to foreground
       
   591     iApplicationTask.BringToForeground();
       
   592 
       
   593     // Notify
       
   594     TBool delayForeground(EFalse);
       
   595     TRAP_IGNORE( delayForeground = IsUnderlyingAppMultimediaL() );
       
   596     if ( delayForeground )
       
   597         {
       
   598         iForegroundDelayed = ETrue;
       
   599         iForegroundDelayTimer->Cancel();
       
   600         iForegroundDelayTimer->Start( 
       
   601                 KTsMultimediaAppActivationDelay, 
       
   602                 0,
       
   603                 TCallBack( DelayedForegroundCallback, this ) );
       
   604         }
       
   605     else
       
   606         {
       
   607         HandleSwitchToForegroundEvent();
       
   608         }
       
   609 
       
   610     TSLOG_OUT();
       
   611     }
       
   612 
       
   613 // -----------------------------------------------------------------------------
       
   614 // CTsAppUi::HandleSwitchToBackgroundEvent
       
   615 // -----------------------------------------------------------------------------
       
   616 //
       
   617 void CTsAppUi::HandleSwitchToBackgroundEvent()
       
   618     {
       
   619     TSLOG_CONTEXT( HandleSwitchToBackgroundEvent, TSLOG_LOCAL );
       
   620     TSLOG_IN();
       
   621 
       
   622     // must not do anything if iForeground is already up-to-date
       
   623 
       
   624     
       
   625     if( iForeground  )  
       
   626         {
       
   627         iForeground = EFalse;
       
   628         SetTaskswitcherStateProperty( KTaskswitcherBackgroundValue );
       
   629 
       
   630         // notify view
       
   631         iAppView->HandleSwitchToBackgroundEvent();
       
   632         
       
   633         iWg.SetOrdinalPosition(-1, ECoeWinPriorityNormal);
       
   634         
       
   635         iForegroundDelayed = EFalse;
       
   636         }
       
   637 
       
   638     TSLOG_OUT();
       
   639     }
       
   640 
       
   641 // -----------------------------------------------------------------------------
       
   642 // CTsAppUi::HandleSwitchToForegroundEvent
       
   643 // -----------------------------------------------------------------------------
       
   644 //
       
   645 void CTsAppUi::HandleSwitchToForegroundEvent()
       
   646     {
       
   647     TSLOG_CONTEXT( HandleSwitchToForegroundEvent, TSLOG_LOCAL );
       
   648     TSLOG_IN();
       
   649 
       
   650     // must not do anything if iForeground is already up-to-date
       
   651     if ( !iForeground && !iForegroundDelayed )
       
   652         {
       
   653         TInt freeRamMemory;
       
   654         HAL::Get( HALData::EMemoryRAMFree, freeRamMemory );
       
   655         if ( freeRamMemory <= KMinMemoryAmountInBytes )
       
   656             {
       
   657             FreeMemoryRequest();
       
   658             }
       
   659 
       
   660         iForeground = ETrue;
       
   661         SetTaskswitcherStateProperty( KTaskswitcherForegroundValue );
       
   662 
       
   663         // notify view
       
   664         iAppView->HandleSwitchToForegroundEvent();
       
   665         
       
   666         iWg.SetOrdinalPosition(iWg.OrdinalPosition(), ECoeWinPriorityAlwaysAtFront);
       
   667         }
       
   668 
       
   669     TSLOG_OUT();
       
   670     }
       
   671 
       
   672 // -----------------------------------------------------------------------------
       
   673 // CTsAppUi::SetTaskswitcherShowProperty
       
   674 // -----------------------------------------------------------------------------
       
   675 //
       
   676 void CTsAppUi::SetTaskswitcherStateProperty( TInt aValue )
       
   677     {
       
   678     TSLOG_CONTEXT( CTsAppUi::SetTaskswitcherShowProperty, TSLOG_LOCAL );
       
   679     TSLOG_IN();
       
   680 
       
   681     if ( RProperty::Set(
       
   682             KTaskswitcherStateCategory, KTaskswitcherStateKey, aValue ) != KErrNone )
       
   683         {
       
   684         TInt error = RProperty::Define(
       
   685                 KTaskswitcherStateCategory, KTaskswitcherStateKey, RProperty::EInt );
       
   686         if ( error != KErrNone )
       
   687             {
       
   688             TSLOG1( TSLOG_INFO, "RProperty::Define Error: %d", error );
       
   689             }
       
   690 
       
   691         error = RProperty::Set( KTaskswitcherStateCategory, KTaskswitcherStateKey, aValue );
       
   692         if ( error != KErrNone )
       
   693             {
       
   694             TSLOG1( TSLOG_INFO, "RProperty::Set Error: %d", error );
       
   695             }
       
   696         }
       
   697 
       
   698     TSLOG_OUT();
       
   699     }
       
   700 
       
   701 // -----------------------------------------------------------------------------
       
   702 // CTsAppUi::FreeMemoryRequest
       
   703 // -----------------------------------------------------------------------------
       
   704 //
       
   705 void CTsAppUi::FreeMemoryRequest()
       
   706     {
       
   707     TSLOG_CONTEXT( CTsAppUi::FreeMemoryRequest, TSLOG_LOCAL );
       
   708     TSLOG_IN();
       
   709     ROomMonitorSession oomMs;
       
   710     TInt error = oomMs.Connect();
       
   711     if ( error != KErrNone)
       
   712         {
       
   713         TSLOG1( TSLOG_INFO, "Could not connect to ROomMonitorSession, %d", error );
       
   714         }
       
   715     else
       
   716         {
       
   717         oomMs.RequestFreeMemory( KMemoryRequestAmountInBytes );
       
   718         oomMs.Close();
       
   719         }
       
   720     TSLOG_OUT();
       
   721     }
       
   722 
       
   723 
       
   724 // -----------------------------------------------------------------------------
       
   725 // CTsAppUi::RequestPopUpL
       
   726 // -----------------------------------------------------------------------------
       
   727 //
       
   728 void CTsAppUi::RequestPopUpL()
       
   729     {
       
   730     TSLOG_CONTEXT( CTsAppUi::RequestPopUpL, TSLOG_LOCAL );
       
   731     TSLOG_IN();
       
   732     if(!iIsPopUpShown)
       
   733         {
       
   734         iIsPopUpShown = ETrue;
       
   735         TSizeMode mode = iEikonEnv->ScreenDevice()->GetCurrentScreenModeAttributes();
       
   736         TInt isLandscape = mode.iScreenSize.iWidth > mode.iScreenSize.iHeight;
       
   737         SetFullScreenApp(ETrue);
       
   738         if(isLandscape)
       
   739             {
       
   740             SetOrientationL(EAppUiOrientationLandscape);
       
   741             }
       
   742         else
       
   743             {
       
   744             SetOrientationL(EAppUiOrientationPortrait);
       
   745             }
       
   746         RProcess().SetPriority(EPriorityForeground);
       
   747         SetFullScreenApp(EFalse);
       
   748         iEikonEnv->RootWin().SetOrdinalPosition(0, ECoeWinPriorityAlwaysAtFront);
       
   749         }
       
   750     TSLOG_OUT();
       
   751     }
       
   752 
       
   753 
       
   754 // -----------------------------------------------------------------------------
       
   755 // CTsAppUi::DisablePopUpL
       
   756 // -----------------------------------------------------------------------------
       
   757 //
       
   758 void CTsAppUi::DisablePopUpL()
       
   759     {
       
   760     TSLOG_CONTEXT( CTsAppUi::DisablePopUpL, TSLOG_LOCAL );
       
   761     TSLOG_IN();
       
   762     if(iIsPopUpShown)
       
   763         {
       
   764         iIsPopUpShown = EFalse;
       
   765         iEikonEnv->RootWin().SetOrdinalPosition(-1, ECoeWinPriorityNeverAtFront);
       
   766         SetOrientationL(EAppUiOrientationAutomatic);
       
   767         SetFullScreenApp(EFalse);
       
   768         RProcess().SetPriority(EPriorityHigh);
       
   769         }
       
   770     TSLOG_OUT();
       
   771     }
       
   772 
       
   773 
       
   774 // -----------------------------------------------------------------------------
       
   775 // CTsAppUi::LayoutCanBeChanged
       
   776 // -----------------------------------------------------------------------------
       
   777 //
       
   778 TBool CTsAppUi::LayoutChangeAllowed()
       
   779     {
       
   780     // Check if layout switch is necessary
       
   781     TSizeMode mode = iEikonEnv->ScreenDevice()->GetCurrentScreenModeAttributes();
       
   782     TBool isLandscape = mode.iScreenSize.iWidth > mode.iScreenSize.iHeight;
       
   783     TRect appRect = ApplicationRect();
       
   784     TBool isAppLandscape = appRect.Width() > appRect.Height();
       
   785     TBool retVal;
       
   786     if(isLandscape != isAppLandscape)
       
   787         {
       
   788         retVal = EFalse;
       
   789         }
       
   790     else
       
   791         {
       
   792         retVal = ETrue;
       
   793         }
       
   794     return retVal;  
       
   795     }
       
   796 
       
   797 
       
   798 // -----------------------------------------------------------------------------
       
   799 // CTsAppUi::HandleWsEventL
       
   800 // -----------------------------------------------------------------------------
       
   801 //
       
   802 void CTsAppUi::HandleWsEventL(const TWsEvent& aEvent,
       
   803         CCoeControl* aDestination)
       
   804     {
       
   805     CAknAppUi::HandleWsEventL(aEvent, aDestination);
       
   806     TInt eventType = aEvent.Type();
       
   807     if ( eventType == EEventWindowGroupListChanged )
       
   808         {
       
   809         TInt wgId = WgIdOfUnderlyingAppL(EFalse); 
       
   810         if ( iForeground &&
       
   811              wgId != iUnderAppWgId &&
       
   812              !iAppView->AppCloseInProgress(iUnderAppWgId) &&
       
   813              !iAppView->WgOnTaskList(wgId) )
       
   814             {
       
   815             MoveAppToBackground( ENoneTransition );
       
   816             }
       
   817         if ( WgIdOfUnderlyingAppL(ETrue) != iUnderAppWgId )
       
   818             {
       
   819             HandleResourceChangeL(KEikDynamicLayoutVariantSwitch);
       
   820             }
       
   821         iUnderAppWgId = wgId;
       
   822         }
       
   823     }
       
   824 
       
   825 
       
   826 // -----------------------------------------------------------------------------
       
   827 // CTsAppUi::WgIdOfUnderlyingApp
       
   828 // -----------------------------------------------------------------------------
       
   829 //
       
   830 TInt CTsAppUi::WgIdOfUnderlyingAppL( TBool aIgnoreParentChild )
       
   831     {
       
   832     TInt retVal(0);
       
   833     TInt underlyingWg = CheckForUnderlyingHiddenAppsL();
       
   834     if ( !underlyingWg )
       
   835         {
       
   836         TApaTaskList taskList( iEikonEnv->WsSession() );
       
   837         underlyingWg = taskList.FindByPos(0).WgId();
       
   838         }
       
   839     
       
   840     if ( aIgnoreParentChild )
       
   841         {
       
   842         retVal = underlyingWg;
       
   843         }
       
   844     else
       
   845         {
       
   846         TInt parentWg = GetTopParentWg( underlyingWg );
       
   847         retVal = parentWg ? parentWg : underlyingWg;
       
   848         }
       
   849     return retVal;
       
   850     }
       
   851 
       
   852 // -----------------------------------------------------------------------------
       
   853 // CTsAppUi::GetTopParentWg
       
   854 // -----------------------------------------------------------------------------
       
   855 //
       
   856 TInt CTsAppUi::GetTopParentWg( TInt aChildWg )
       
   857 	{
       
   858 	TInt parentWg = GetParentWg( aChildWg );
       
   859 	if( parentWg )
       
   860 		{
       
   861 		TInt topParentWg = GetTopParentWg( parentWg );
       
   862 		if( topParentWg )
       
   863 			{
       
   864 			parentWg = topParentWg; 
       
   865 			}
       
   866 		}
       
   867 	return parentWg;
       
   868 	}
       
   869 
       
   870 // -----------------------------------------------------------------------------
       
   871 // CTsAppUi::GetParentWg
       
   872 // -----------------------------------------------------------------------------
       
   873 //
       
   874 TInt CTsAppUi::GetParentWg( TInt aChildWg )
       
   875     {
       
   876 	TInt retVal(0);
       
   877     RArray<RWsSession::TWindowGroupChainInfo> allWgIds;
       
   878     // Ask for window group list from RWsSession
       
   879     TInt error = iEikonEnv->WsSession().WindowGroupList( 0, &allWgIds );
       
   880     if ( !error )
       
   881         {
       
   882         TInt count( allWgIds.Count() );
       
   883         for ( TInt i( 0 ); i < count; i++ )
       
   884             {
       
   885             RWsSession::TWindowGroupChainInfo info = allWgIds[i];
       
   886             if ( info.iId == aChildWg && info.iParentId > 0)
       
   887                 {
       
   888 				retVal = info.iParentId;
       
   889                 break;
       
   890                 }
       
   891             }
       
   892         }
       
   893     allWgIds.Close();
       
   894 	return retVal;
       
   895 	}
       
   896 
       
   897 // -----------------------------------------------------------------------------
       
   898 // CTsAppUi::IsForeground
       
   899 // -----------------------------------------------------------------------------
       
   900 //
       
   901 TBool CTsAppUi::IsForeground() const
       
   902     {
       
   903     return iForeground;
       
   904     }
       
   905 
       
   906 // -----------------------------------------------------------------------------
       
   907 // CTsAppUi::CheckForUnderlyingCameraL
       
   908 // -----------------------------------------------------------------------------
       
   909 //
       
   910 TInt CTsAppUi::CheckForUnderlyingHiddenAppsL()
       
   911     {
       
   912     TInt wgId(0);
       
   913     RArray<RWsSession::TWindowGroupChainInfo> allWgIds;
       
   914     CleanupClosePushL(allWgIds);
       
   915     User::LeaveIfError(iEikonEnv->WsSession().WindowGroupList(0, &allWgIds));
       
   916     TInt underlyingWg(allWgIds[0].iId);
       
   917     CleanupStack::PopAndDestroy(&allWgIds);
       
   918     
       
   919     CApaWindowGroupName* windowName =
       
   920         CApaWindowGroupName::NewLC( iEikonEnv->WsSession(), underlyingWg );
       
   921     TUid appUid = windowName->AppUid();
       
   922     CleanupStack::PopAndDestroy( windowName );
       
   923     if( appUid == KTsCameraUid ||
       
   924         appUid == KTsTelephoneUid )
       
   925         {
       
   926         wgId = underlyingWg;
       
   927         }
       
   928     return wgId;
       
   929     }
       
   930 
       
   931 // -----------------------------------------------------------------------------
       
   932 // CTsAppUi::IsUnderlyingAppMultimediaL
       
   933 // -----------------------------------------------------------------------------
       
   934 //
       
   935 TBool CTsAppUi::IsUnderlyingAppMultimediaL()
       
   936     {
       
   937     TBool retVal(EFalse);
       
   938     
       
   939     TApaTaskList taskList( iEikonEnv->WsSession() );
       
   940     TInt underlyingWg = taskList.FindByPos(0).WgId();
       
   941     
       
   942     CApaWindowGroupName* windowName =
       
   943         CApaWindowGroupName::NewLC( iEikonEnv->WsSession(), underlyingWg );
       
   944     TUid appUid = windowName->AppUid();
       
   945     CleanupStack::PopAndDestroy( windowName );
       
   946     if ( appUid == KTsCameraUid )
       
   947         {
       
   948         retVal = ETrue;
       
   949         }
       
   950     
       
   951     return retVal;
       
   952     }
       
   953 
       
   954 
       
   955 // -----------------------------------------------------------------------------
       
   956 // CTsAppUi::DelayedForegroundLaunched
       
   957 // -----------------------------------------------------------------------------
       
   958 //
       
   959 TBool CTsAppUi::DelayedForegroundLaunched()
       
   960     {
       
   961     return iDelayedForegroundInProgress;
       
   962     }
       
   963 
       
   964 // End of file