satui/satapp/SATShellControllerSrc/CSatShellController.cpp
changeset 0 ff3b6d0fd310
child 7 7237db0d9fca
equal deleted inserted replaced
-1:000000000000 0:ff3b6d0fd310
       
     1 /*
       
     2 * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *     This file contains the SAT Shell Controller class that is responsible
       
    16 *     for the actual implementation of the following functionality:
       
    17 *       - add SAT UI application to Desk (or similar)
       
    18 *       - remove SAT UI application from Desk (or similar)
       
    19 *       - bring SAT UI application to foreground if it already is up
       
    20 *           and running
       
    21 *       - launching the SAT UI application
       
    22 *       - launching the Browser
       
    23 *       - resolving default access point
       
    24 *       - bring Browser to the foreground
       
    25 *
       
    26 *
       
    27 */
       
    28 
       
    29 
       
    30 //  INCLUDE FILES
       
    31 #include <w32std.h>             // RWsSession
       
    32 #include <apaid.h>              // TApaAppInfo
       
    33 #include <apacmdln.h>           // CApaCommandLine
       
    34 #include <apgcli.h>             // RApaLsSession
       
    35 #include <apgtask.h>            // TApaTaskList
       
    36 #include <apgwgnam.h>           // CApaWindowGroupName
       
    37 #include <MenuSatInterface.h>   // CMenuSATInterface
       
    38 #include <DocumentHandler.h>    // KWmlcHandler
       
    39 #include "CSatShellController.h"
       
    40 
       
    41 // Browser Cen Rep Keys.
       
    42 #ifdef __SERIES60_NATIVE_BROWSER
       
    43     #include <browseruisdkcrkeys.h>
       
    44     #include <BrowserUiInternalCRKeys.h>
       
    45 #endif // __SERIES60_NATIVE_BROWSER
       
    46 #include "tflogger.h"
       
    47 
       
    48 // CONSTANTS
       
    49 const TUid KUidSatUi = { 0x101f4ce0 };
       
    50 _LIT( KFour, "4" );                     // Browser parameter.
       
    51 _LIT( KFive, "5" );                     // Browser parameter.
       
    52 _LIT( KSpace, " " );                    // Used as Browser parameter.
       
    53 static const TUid KEmptyUid = { KErrNotFound };
       
    54 static const TUid KUidBrowser = { KWmlcHandler };
       
    55 const TInt KTimerTime( 2000000 );       // 2 seconds.
       
    56 const TInt KBrowserParamAndTwoSpaces( 3 );
       
    57 #ifndef __SERIES60_NATIVE_BROWSER
       
    58     const TUid KCRUidBrowser = { 0x10008D39 };
       
    59     const TUint32 KBrowserDefaultAccessPoint( 0x0000000E );
       
    60 #endif // __SERIES60_NATIVE_BROWSER
       
    61 
       
    62 // ================= MEMBER FUNCTIONS =======================================
       
    63 
       
    64 // C++ default constructor can NOT contain any code that
       
    65 // might leave.
       
    66 //
       
    67 CSatShellController::CSatShellController():
       
    68     iUidWmlBrowser( TUid::Uid( KWmlcHandler ))
       
    69     {
       
    70     TFLOGSTRING( "CSatShellController::CSatShellController called-exit" )
       
    71     }
       
    72 
       
    73 // Symbian OS constructor
       
    74 void CSatShellController::ConstructL()
       
    75     {
       
    76     TFLOGSTRING( "CSatShellController::ConstructL called" )
       
    77 
       
    78     // Timer is used to get the Browser application to the foreground.
       
    79     iTimer = CPeriodic::NewL( EPriorityNormal );
       
    80 
       
    81     TFLOGSTRING( "CSatShellController::ConstructL exit" )
       
    82     }
       
    83 
       
    84 // Two-phased constructor.
       
    85 CSatShellController* CSatShellController::NewL()
       
    86     {
       
    87     TFLOGSTRING( "CSatShellController::NewL called" )
       
    88 
       
    89     CSatShellController* self = new ( ELeave ) CSatShellController;
       
    90     CleanupStack::PushL( self );
       
    91     self->ConstructL();
       
    92     CleanupStack::Pop( self );
       
    93 
       
    94     TFLOGSTRING( "CSatShellController::NewL exit" )
       
    95     return self;
       
    96     }
       
    97 
       
    98 
       
    99 // Destructor
       
   100 CSatShellController::~CSatShellController()
       
   101     {
       
   102     TFLOGSTRING( "CSatShellController::~CSatShellController called" )
       
   103 
       
   104     // Frees resources.
       
   105     if ( iTimer )
       
   106         {
       
   107         iTimer->Cancel();
       
   108         }
       
   109     delete iTimer;
       
   110 
       
   111     TFLOGSTRING( "CSatShellController::~CSatShellController exit" )
       
   112     }
       
   113 
       
   114 // ---------------------------------------------------------
       
   115 // CSatShellController::AddSatUiL
       
   116 // Adds SAT UI Client application to phone's
       
   117 // Application Shell (or similar).
       
   118 // ---------------------------------------------------------
       
   119 //
       
   120 void CSatShellController::AddSatUiL(
       
   121     TDesC& aName )
       
   122     {
       
   123     TFLOGSTRING( "CSatShellController::AddSatUiL(name) called" )
       
   124 
       
   125     iSimAppName = aName;
       
   126 
       
   127     CMenuSATInterface* menu = new ( ELeave ) CMenuSATInterface;
       
   128     menu->MakeSatUiVisible( ETrue, aName );
       
   129     delete menu;
       
   130 
       
   131     TFLOGSTRING( "CSatShellController::AddSatUiL exit" )
       
   132     }
       
   133 
       
   134 //------------------------------------------------------------
       
   135 // CSatShellController::AddSatUiL
       
   136 // Adds SAT UI Client application to phone's
       
   137 // Application Shell (or similar).
       
   138 // ---------------------------------------------------------
       
   139 //
       
   140 void CSatShellController::AddSatUiL(
       
   141     TDesC& aName, TUint8 aIconId )
       
   142     {
       
   143     TFLOGSTRING( "CSatShellController::AddSatUiL(name,icon) called" )
       
   144 
       
   145     iSimAppName = aName;
       
   146 
       
   147     CMenuSATInterface* menu = new ( ELeave ) CMenuSATInterface;
       
   148     menu->MakeSatUiVisible( ETrue, aName, aIconId );
       
   149     delete menu;
       
   150 
       
   151     TFLOGSTRING( "CSatShellController::AddSatUiL exit" )
       
   152     }
       
   153 
       
   154 // ---------------------------------------------------------
       
   155 // CSatShellController::RemoveSatUiL
       
   156 // Removes SAT UI Client application from phone's
       
   157 // Application Shell (or similar).
       
   158 // ---------------------------------------------------------
       
   159 //
       
   160 
       
   161 void CSatShellController::RemoveSatUiL()
       
   162     {
       
   163     TFLOGSTRING( "CSatShellController::RemoveSatUiL called" )
       
   164 
       
   165     CMenuSATInterface* menu = new ( ELeave ) CMenuSATInterface;
       
   166     menu->MakeSatUiVisible( EFalse, iSimAppName );
       
   167     delete menu;
       
   168 
       
   169     TFLOGSTRING( "CSatShellController::RemoveSatUiL exit" )
       
   170     }
       
   171 
       
   172 // ---------------------------------------------------------
       
   173 // CSatShellController::LaunchSatUiL
       
   174 // Launches SAT UI Client application.
       
   175 // ---------------------------------------------------------
       
   176 //
       
   177 void CSatShellController::LaunchSatUiL()
       
   178     {
       
   179     TFLOGSTRING( "CSatShellController::LaunchSatUiL called" )
       
   180 
       
   181     RApaLsSession rapaLsSession;
       
   182     User::LeaveIfError( rapaLsSession.Connect() );
       
   183     CleanupClosePushL( rapaLsSession );
       
   184     TThreadId id( static_cast<TInt64>( 0 ) );
       
   185     TApaAppInfo appInfo;
       
   186 
       
   187     User::LeaveIfError( rapaLsSession.GetAppInfo( appInfo, KUidSatUi ) );
       
   188     CApaCommandLine* cmdLine = CApaCommandLine::NewLC();
       
   189     cmdLine->SetExecutableNameL( appInfo.iFullName );
       
   190     cmdLine->SetCommandL( EApaCommandOpen );
       
   191     rapaLsSession.StartApp( *cmdLine, id );
       
   192 
       
   193     CleanupStack::PopAndDestroy( cmdLine );
       
   194     CleanupStack::PopAndDestroy( &rapaLsSession );
       
   195 
       
   196     TFLOGSTRING( "CSatShellController::LaunchSatUiL exit" )
       
   197     }
       
   198 
       
   199 
       
   200 // ---------------------------------------------------------
       
   201 // CSatShellController::BringBrowserToForeground
       
   202 // Brings the Browser to foreground.
       
   203 // ---------------------------------------------------------
       
   204 //
       
   205 void CSatShellController::BringBrowserToForeground() const
       
   206     {
       
   207     TFLOGSTRING( "CSatShellController::BringBrowserToForeground called" )
       
   208 
       
   209     BringApplicationToForeground( iUidWmlBrowser );
       
   210 
       
   211     TFLOGSTRING( "CSatShellController::BringBrowserToForeground exit" )
       
   212     }
       
   213 
       
   214 // ---------------------------------------------------------
       
   215 // CSatShellController::BringBrowserToForegroundAfterPeriod
       
   216 // Brings the Browser to foreground after time has passed.
       
   217 // ---------------------------------------------------------
       
   218 //
       
   219 void CSatShellController::BringBrowserToForegroundAfterPeriod()
       
   220     {
       
   221     TFLOGSTRING(
       
   222         "CSatShellController::BringBrowserToForegroundAfterPeriod called" )
       
   223 
       
   224     if ( !iTimer->IsActive() )
       
   225         {
       
   226         TFLOGSTRING(
       
   227             "CSatShellController::BringBrowserToForegroundAfterPeriod \
       
   228              start iTimer" )
       
   229         iTimer->Start( KTimerTime,
       
   230                        KTimerTime,
       
   231                        TCallBack( TimerCompleted, this ) );
       
   232         }
       
   233 
       
   234     TFLOGSTRING(
       
   235         "CSatShellController::BringBrowserToForegroundAfterPeriod exit" )
       
   236     }
       
   237 
       
   238 // ---------------------------------------------------------
       
   239 // CSatShellController::BringSatUiToForeground
       
   240 // Brings the SAT UI application to foreground.
       
   241 // ---------------------------------------------------------
       
   242 //
       
   243 void CSatShellController::BringSatUiToForeground()
       
   244     {
       
   245     TFLOGSTRING( "CSatShellController::BringSatUiToForeground called" )
       
   246 
       
   247     CheckSatUiStatus();
       
   248     BringApplicationToForeground( KUidSatUi );
       
   249 
       
   250     TFLOGSTRING( "CSatShellController::BringSatUiToForeground exit" )
       
   251     }
       
   252 
       
   253 // ---------------------------------------------------------
       
   254 // CSatShellController::BringApplicationToForeground
       
   255 // Brings the SAT UI application to foreground.
       
   256 // ---------------------------------------------------------
       
   257 //
       
   258 void CSatShellController::BringApplicationToForeground(
       
   259     const TUid& aAppUid ) const
       
   260     {
       
   261     TFLOGSTRING( "CSatShellController::BringApplicationToForeground called" )
       
   262 
       
   263     RWsSession wsSession;
       
   264     // Open the WS server session.
       
   265     if ( KErrNone == wsSession.Connect() )
       
   266         {
       
   267         TFLOGSTRING( "CSatShellController::BringApplicationToForeground \
       
   268             open WS server session" )
       
   269         TApaTaskList tasklist( wsSession );
       
   270         // Find the task with uid
       
   271         TApaTask task = tasklist.FindApp( aAppUid );
       
   272         task.BringToForeground();
       
   273         wsSession.Close();
       
   274         }
       
   275 
       
   276     TFLOGSTRING( "CSatShellController::BringApplicationToForeground exit" )
       
   277     }
       
   278 
       
   279 // ---------------------------------------------------------
       
   280 // CSatShellController::SetSatUiToBackground
       
   281 // Sets SAT UI Application to background if needed.
       
   282 // ---------------------------------------------------------
       
   283 //
       
   284 void CSatShellController::SetSatUiToBackground() const
       
   285     {
       
   286     TFLOGSTRING( "CSatShellController::SetSatUiToBackground called" )
       
   287     
       
   288     if ( iSetSatUiToBackground )
       
   289         {
       
   290         RWsSession wsSession;
       
   291         // Open the WS server session.
       
   292         if ( KErrNone == wsSession.Connect() )
       
   293             {
       
   294             TFLOGSTRING( "CSatShellController::SetSatUiToBackground \
       
   295                 open WS server session" )
       
   296             TApaTaskList tasklist( wsSession );
       
   297             // Find the task with uid
       
   298             TApaTask task = tasklist.FindApp( KUidSatUi );
       
   299             task.SendToBackground();
       
   300             wsSession.Close();
       
   301             }            
       
   302         }
       
   303                     
       
   304     TFLOGSTRING( "CSatShellController::SetSatUiToBackground exit" )                    
       
   305     }
       
   306 
       
   307 // ---------------------------------------------------------
       
   308 // CSatShellController::CycleSatUiBackwards
       
   309 // Brings the SAT UI application to foreground.
       
   310 // ---------------------------------------------------------
       
   311 //
       
   312 void CSatShellController::CycleSatUiBackwards()
       
   313     {
       
   314     TFLOGSTRING( "CSatShellController::BringSatUiToForeground called" )
       
   315 
       
   316     RWsSession wsSession;
       
   317     // Open the WS server session.
       
   318     if ( KErrNone == wsSession.Connect() )
       
   319         {
       
   320         TFLOGSTRING( "CSatShellController::CycleSatUiBackwards \
       
   321             open WS server session" )
       
   322         TApaTaskList taskList( wsSession );
       
   323         // Finds topmost application.
       
   324         TApaTask task = taskList.FindByPos(0);
       
   325         iTimer->Cancel();
       
   326 
       
   327         CApaWindowGroupName* name = 0;
       
   328         TRAPD( err,
       
   329             name = CApaWindowGroupName::NewL( wsSession, task.WgId() ) );
       
   330 
       
   331         if ( name )
       
   332             {
       
   333             // Check is that Browser is foremost application.
       
   334             if ( !err && ( name->AppUid() == KUidBrowser ) )
       
   335                 {
       
   336                 TFLOGSTRING(
       
   337                     "CSatShellController::BSUTF Browser is in foreground" )
       
   338                 }
       
   339             else
       
   340                 {
       
   341                 TFLOGSTRING(
       
   342                     "CSatShellController::BSUTF Browser isn't in foreground" )
       
   343                 TApaTask browserTask = taskList.FindApp( KUidBrowser );
       
   344                 browserTask.BringToForeground();
       
   345                 iTimer->Start(
       
   346                     KTimerTime, KTimerTime, TCallBack( TimerCompleted, this ) );
       
   347                 }
       
   348 
       
   349             delete name;
       
   350             }
       
   351 
       
   352         wsSession.Close();
       
   353         }
       
   354 
       
   355     TFLOGSTRING( "CSatShellController::BringSatUiToForeground exit" )
       
   356     }
       
   357 
       
   358 // ---------------------------------------------------------
       
   359 // CSatShellController::TimerCompleted
       
   360 // Callback function. Completes after predefined
       
   361 // time has passed
       
   362 // ---------------------------------------------------------
       
   363 //
       
   364 TInt CSatShellController::TimerCompleted( TAny* aObject )
       
   365     {
       
   366     TFLOGSTRING( "CSatShellController::TimerCompleted called" )
       
   367 
       
   368     if ( aObject )
       
   369         {
       
   370         STATIC_CAST( CSatShellController*, aObject )->CycleSatUiBackwards();
       
   371         }
       
   372 
       
   373     TFLOGSTRING( "CSatShellController::TimerCompleted exit" )
       
   374     return KErrNone;
       
   375     }
       
   376 
       
   377 // ---------------------------------------------------------
       
   378 // CSatShellController::LaunchBrowserL
       
   379 // Launch XHTML-browser
       
   380 // Browser launched with parameter
       
   381 // "4" + "<Space>" + "<Url>" + "<Space>" + "<UID of App>" or "5"
       
   382 // More information see Browser API Specification Document
       
   383 // ---------------------------------------------------------
       
   384 //
       
   385 TInt CSatShellController::LaunchBrowserL( const TDesC& aUrl,
       
   386      TUid aAccessPointUid )
       
   387     {
       
   388     TFLOGSTRING( "CSatShellController::LaunchBrowserL called" )
       
   389 
       
   390     TInt errorCode( KErrNone );
       
   391     HBufC* param = HBufC::NewLC( aUrl.Length() + KMaxUidName +
       
   392         KBrowserParamAndTwoSpaces );
       
   393     
       
   394     TFLOGSTRING2( "CSatShellController::LaunchBrowserL length of aUrl: %d", \
       
   395         aUrl.Length() )
       
   396     if ( aUrl.Length() > 0 )
       
   397         {
       
   398         param->Des().Copy( KFour );
       
   399         param->Des().Append( KSpace );
       
   400         param->Des().Append( aUrl );
       
   401         TFLOGSTRING2( "CSatShellController::LaunchBrowserL \
       
   402                       aAccessPointUid: %d", aAccessPointUid )
       
   403         // Create script for Browser if not empty accesspoint UID.
       
   404         if ( aAccessPointUid != KEmptyUid )
       
   405             {
       
   406             TFLOGSTRING( "CSatShellController::LaunchBrowserL not empty \
       
   407                 accesspoint" )
       
   408             // Uid is in decimal format
       
   409             param->Des().Append( KSpace );
       
   410             param->Des().AppendNum( aAccessPointUid.iUid );
       
   411             }
       
   412 
       
   413         }
       
   414     else
       
   415         {
       
   416         param->Des().Copy( KFive );
       
   417         }
       
   418 
       
   419     User::LeaveIfError( iWsSession.Connect() );
       
   420 
       
   421     TFLOGSTRING2( "CSatShellController::LaunchBrowserL \
       
   422                    param string: %S", param )
       
   423     TApaTaskList taskList( iWsSession );
       
   424     TApaTask task = taskList.FindApp( iUidWmlBrowser );
       
   425 
       
   426     // If browser already open.
       
   427     if ( task.Exists() )
       
   428         {
       
   429         TFLOGSTRING( "CSatShellController::LaunchBrowserL browser open" )
       
   430         HBufC8* param8 = HBufC8::NewLC( param->Length() );
       
   431         param8->Des().Append( *param );
       
   432         errorCode = task.SendMessage( TUid::Uid( 0 ), param8->Des() );
       
   433         CleanupStack::PopAndDestroy( param8 );
       
   434         }
       
   435     else
       
   436         {
       
   437         TFLOGSTRING( "CSatShellController::LaunchBrowserL launch browser" )
       
   438         // Launch the Browser.
       
   439         TThreadId id( static_cast<TInt64>( 0 ) );
       
   440         RApaLsSession rapaLsSession;
       
   441         User::LeaveIfError( rapaLsSession.Connect() );
       
   442         errorCode = rapaLsSession.StartDocument( *param, iUidWmlBrowser, id );
       
   443         rapaLsSession.Close();
       
   444         }
       
   445 
       
   446     BringBrowserToForeground();
       
   447 
       
   448     iWsSession.Close();
       
   449     CleanupStack::PopAndDestroy( param );
       
   450 
       
   451     TFLOGSTRING2( "CSatShellController::LaunchBrowserL exit %d", errorCode )
       
   452     return errorCode;
       
   453     }
       
   454 
       
   455 // -----------------------------------------------------------------------------
       
   456 // CSatShellController::CheckSatUiStatus
       
   457 // Check if SatUi is needed to set to background.
       
   458 // -----------------------------------------------------------------------------
       
   459 //
       
   460 void CSatShellController::CheckSatUiStatus()
       
   461     {
       
   462     TFLOGSTRING( "CSatShellController::CheckSatUiStatus called" )
       
   463     
       
   464     // if SatUi task is found and SatUi is not in foreground flag is set ETrue
       
   465     iSetSatUiToBackground = EFalse;
       
   466 
       
   467     RWsSession wsSession;
       
   468     // Open the WS server session.
       
   469     if ( KErrNone == wsSession.Connect() )
       
   470         {
       
   471         TFLOGSTRING( "CSatShellController::CheckSatUiStatus \
       
   472             open WS server session" )
       
   473         TApaTaskList tasklist( wsSession );
       
   474         TApaTask satUiTask = tasklist.FindApp( KUidSatUi );
       
   475         
       
   476         if ( satUiTask.Exists() )
       
   477             {
       
   478             TFLOGSTRING(
       
   479                  "CSatShellController::CheckSatUiStatus task exists" )
       
   480             TApaTask topMostTask = tasklist.FindByPos( 0 );
       
   481             
       
   482             if ( topMostTask.ThreadId() != satUiTask.ThreadId() )
       
   483                 {
       
   484                 TFLOGSTRING(
       
   485                     "CSatShellController::CheckSatUiStatus set SatUI to BG" )
       
   486                 iSetSatUiToBackground = ETrue;                    
       
   487                 }
       
   488             }
       
   489             
       
   490         wsSession.Close();
       
   491         }
       
   492         
       
   493     TFLOGSTRING( "CSatShellController::CheckSatUiStatus exit" )
       
   494     }
       
   495     
       
   496 // ================= OTHER EXPORTED FUNCTIONS ===============================
       
   497 
       
   498 EXPORT_C MSatShellController* NewSatController()
       
   499     {
       
   500     TFLOGSTRING( "CSatShellController::NewSatController called" )
       
   501 
       
   502     MSatShellController* satController = NULL;
       
   503     TRAPD( err, satController = CSatShellController::NewL() )
       
   504     if ( KErrNone != err )
       
   505         {
       
   506         TFLOGSTRING2( "   CSatShellController::NewSatController \
       
   507             failed: %d", err )
       
   508         satController = NULL;
       
   509         }
       
   510 
       
   511     TFLOGSTRING( "CSatShellController::NewSatController exit" )
       
   512     return satController;
       
   513     }
       
   514 
       
   515 //  End of File