widgets/widgetapp/src/WidgetUiAppUi.cpp
changeset 0 dd21522fd290
child 8 7c90e6132015
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2006, 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 the License "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 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "WidgetUiAppUi.h"
       
    20 #include "WidgetUiWindowView.h"
       
    21 #include "WidgetUiWindowManager.h"
       
    22 #include "WidgetUiWindow.h"
       
    23 #include "WidgetUiAsyncExit.h"
       
    24 #include "widgetappdefs.rh"
       
    25 #include <WidgetUi.rsg>
       
    26 #include "WidgetUi.hrh"
       
    27 #include <S32MEM.H>
       
    28 #include <avkon.hrh>
       
    29 #include <e32std.h>
       
    30 #include <bautils.h>
       
    31 #include <oommonitorplugin.h>
       
    32 #include <BrowserDialogsProvider.h>
       
    33 #include <e32property.h>
       
    34 
       
    35 // EXTERNAL DATA STRUCTURES
       
    36 
       
    37 // EXTERNAL FUNCTION PROTOTYPES
       
    38 
       
    39 // CONSTANTS
       
    40 
       
    41 // const TUid KWidgetAppUid  = TUid::Uid( 0x10282822 );
       
    42 
       
    43 // MACROS
       
    44 
       
    45 // LOCAL CONSTANTS AND MACROS
       
    46 
       
    47 // MODULE DATA STRUCTURES
       
    48 
       
    49 // LOCAL FUNCTION PROTOTYPES
       
    50 
       
    51 // FORWARD DECLARATIONS
       
    52 
       
    53 // ============================= LOCAL FUNCTIONS ================================
       
    54 
       
    55 static void NotifyWidgetRunning()
       
    56     {
       
    57     const TUid KMyPropertyCat = { 0x10282E5A };
       
    58     enum TMyPropertyKeys { EMyPropertyState = 109 };
       
    59     TInt state( 1 );
       
    60     RProperty::Set( KMyPropertyCat, EMyPropertyState , state );
       
    61     }
       
    62 
       
    63 // ============================ MEMBER FUNCTIONS ================================
       
    64 //
       
    65 CWidgetUiAppUi::CWidgetUiAppUi()
       
    66     {
       
    67     //Nothing
       
    68     }
       
    69 
       
    70 // ------------------------------------------------------------------------------
       
    71 // CWidgetUiAppUi::ConstructL
       
    72 // Symbian constructor
       
    73 //
       
    74 // ------------------------------------------------------------------------------
       
    75 //
       
    76 void CWidgetUiAppUi::ConstructL()
       
    77     {
       
    78     CWidgetUiWindowView* contentView( NULL );
       
    79     BaseConstructL( EAknEnableSkin | EAknEnableMSK );
       
    80 
       
    81     //
       
    82     iWindowManager = CWidgetUiWindowManager::NewL( *this );
       
    83     iWidgetUiAsyncExit = CWidgetUiAsyncExit::NewL( *this );
       
    84     iDelayedForegroundEvent = CIdle::NewL(CActive::EPriorityIdle);
       
    85     contentView = CWidgetUiWindowView::NewLC( *iWindowManager );
       
    86 
       
    87     iWindowManager->SetView( *contentView );
       
    88     // Transfer ownership to CAknViewAppUi
       
    89     AddViewL( contentView );
       
    90     CleanupStack::Pop( contentView );
       
    91 
       
    92     SetRotationSupport();
       
    93     LoadAdditionalResourcefile();
       
    94     }
       
    95 
       
    96 // ------------------------------------------------------------------------------
       
    97 // CWidgetUiAppUi::LoadAdditionalResourcefile
       
    98 // Load resource file for creating dynamic options menu
       
    99 //
       
   100 // ------------------------------------------------------------------------------
       
   101 //
       
   102 void CWidgetUiAppUi::LoadAdditionalResourcefile()
       
   103     {
       
   104     // add widgetmenu resource file to the coe environment
       
   105     // load resource file
       
   106     TFileName resourceFileName;
       
   107     TParse parse;
       
   108     CEikAppUi* pAppUI = (CEikAppUi*)(CCoeEnv::Static()->AppUi());
       
   109     TFileName dllName = pAppUI->Application()->DllName();
       
   110 
       
   111     parse.Set( dllName, NULL, NULL );
       
   112     resourceFileName += parse.Drive();
       
   113      _LIT(WIDGET_RESOURCE_DIR,"\\resource\\");
       
   114     resourceFileName += WIDGET_RESOURCE_DIR;
       
   115     resourceFileName += _L("widgetmenu.rsc");
       
   116 
       
   117     CCoeEnv* coeEnv = CCoeEnv::Static();
       
   118     BaflUtils::NearestLanguageFile( coeEnv->FsSession(), resourceFileName  );
       
   119     iResourceFile = coeEnv->AddResourceFileL( resourceFileName );
       
   120     }
       
   121 
       
   122 // ------------------------------------------------------------------------------
       
   123 // CWidgetUiAppUi::~CWidgetUiAppUi
       
   124 // Destructor
       
   125 //
       
   126 // ------------------------------------------------------------------------------
       
   127 //
       
   128 CWidgetUiAppUi::~CWidgetUiAppUi()
       
   129     {
       
   130     // remove resource file
       
   131     CCoeEnv::Static()->DeleteResourceFile( iResourceFile );
       
   132     delete iWindowManager;
       
   133     if (iDelayedForegroundEvent)
       
   134         {
       
   135         iDelayedForegroundEvent->Cancel(); 
       
   136         }
       
   137     delete iDelayedForegroundEvent;
       
   138     delete iWidgetUiAsyncExit;
       
   139     }
       
   140 
       
   141 // ------------------------------------------------------------------------------
       
   142 // CWidgetUiAppUi::::DynInitMenuPaneL
       
   143 // This function is called by the EIKON framework just before it displays
       
   144 // a menu pane. Its default implementation is empty, and by overriding it,
       
   145 // the application can set the state of menu items dynamically according
       
   146 // to the state of application data.
       
   147 // ------------------------------------------------------------------------------
       
   148 //
       
   149 void CWidgetUiAppUi::DynInitMenuPaneL( TInt aResourceId, CEikMenuPane* aMenuPane )
       
   150     {
       
   151     ActiveView()->DynInitMenuPaneL( aResourceId, aMenuPane );
       
   152     }
       
   153 
       
   154 // ------------------------------------------------------------------------------
       
   155 // CWidgetUiAppUi::HandleCommandL
       
   156 // Handles user commands
       
   157 //
       
   158 // ------------------------------------------------------------------------------
       
   159 //
       
   160 void CWidgetUiAppUi::HandleCommandL(TInt aCommand)
       
   161     {
       
   162     switch ( aCommand )
       
   163         {
       
   164         case EEikCmdExit:
       
   165             {
       
   166             Exit();
       
   167             break;
       
   168             }
       
   169         default:
       
   170             {
       
   171             break;
       
   172             }
       
   173         }
       
   174     }
       
   175 
       
   176 // ------------------------------------------------------------------------------
       
   177 // CWidgetUiAppUi::ActiveView
       
   178 // returns the app UI's currently active view
       
   179 //
       
   180 // ------------------------------------------------------------------------------
       
   181 //
       
   182 CAknView* CWidgetUiAppUi::ActiveView()
       
   183     {
       
   184     TVwsViewId activeViewId;
       
   185 
       
   186     if ( GetActiveViewId( activeViewId ) == KErrNone )
       
   187         {
       
   188         return View( activeViewId.iViewUid );
       
   189         }
       
   190     return NULL;
       
   191     }
       
   192 
       
   193 
       
   194 // ------------------------------------------------------------------------------
       
   195 // CWidgetUiAppUi::ProcessCommandParametersL
       
   196 // Process shell commands
       
   197 //
       
   198 // ------------------------------------------------------------------------------
       
   199 //
       
   200 TBool CWidgetUiAppUi::ProcessCommandParametersL(CApaCommandLine &aCommandLine)
       
   201     {   
       
   202     TBool res( ETrue );
       
   203     
       
   204     const TPtrC doc( aCommandLine.DocumentName() );
       
   205     TInt len( doc.Length() * 2 );
       
   206     
       
   207     if ( len )
       
   208         {
       
   209         HBufC8* params( HBufC8::NewMaxLC( len ) );
       
   210         
       
   211         Mem::Copy( 
       
   212             const_cast< TUint8* >( params->Ptr() ),
       
   213             reinterpret_cast< const TUint16* >( doc.Ptr() ),
       
   214             len );
       
   215         
       
   216         LaunchWindowL( *params );
       
   217 
       
   218         CleanupStack::PopAndDestroy( params );
       
   219         }
       
   220     else
       
   221         {
       
   222         res = CEikAppUi::ProcessCommandParametersL( aCommandLine );
       
   223         }
       
   224         
       
   225     return res;
       
   226 }
       
   227 
       
   228 // ------------------------------------------------------------------------------
       
   229 // CWidgetUiAppUi::LaunchWindowL
       
   230 // Process shell commands
       
   231 //
       
   232 // ------------------------------------------------------------------------------
       
   233 //
       
   234 void CWidgetUiAppUi::LaunchWindowL( const TDesC8& aMessage )
       
   235     {
       
   236     TUid uid( KNullUid );
       
   237     TUint32 operation( 0 );
       
   238     
       
   239     ProcessMessageArgumentsL( aMessage, uid, operation );
       
   240         
       
   241     iWindowManager->HandleWidgetCommandL( uid, operation );
       
   242     
       
   243     // Widget is up and running, notify that next one can be launched
       
   244     // TODO magic numbers from Launcher.
       
   245     if( operation == LaunchMiniview )
       
   246         {
       
   247         NotifyWidgetRunning();
       
   248         }
       
   249     }
       
   250 
       
   251 // ------------------------------------------------------------------------------
       
   252 // CWidgetUiAppUi::HandleMessageL
       
   253 // Called by WidgetLaucher
       
   254 //
       
   255 // ------------------------------------------------------------------------------
       
   256 //
       
   257 MCoeMessageObserver::TMessageResponse CWidgetUiAppUi::HandleMessageL(
       
   258                     TUint32 aClientHandleOfTargetWindowGroup,
       
   259                     TUid aMessageUid,
       
   260                     const TDesC8& aMessageParameters )
       
   261     {
       
   262     MCoeMessageObserver::TMessageResponse res( EMessageNotHandled );
       
   263     // TODO Magic number 1 comes from Widget Launcher.
       
   264     if ( ( aMessageUid == TUid::Uid( 1 ) ) )
       
   265         {
       
   266         // Hide red exclamation marks, as it is the habit of UI applications.
       
   267         LaunchWindowL( aMessageParameters );
       
   268         
       
   269         res = EMessageHandled;
       
   270         }
       
   271     else
       
   272         {
       
   273         res = CAknViewAppUi::HandleMessageL( 
       
   274             aClientHandleOfTargetWindowGroup,
       
   275             aMessageUid,
       
   276             aMessageParameters );
       
   277         }
       
   278     
       
   279     return res;
       
   280     }
       
   281 
       
   282 // ------------------------------------------------------------------------------
       
   283 // CWidgetUiAppUi::HandleResourceChangeL
       
   284 // Handles change to the application's resources shared across the environment
       
   285 //
       
   286 // ------------------------------------------------------------------------------
       
   287 //
       
   288 void CWidgetUiAppUi::HandleResourceChangeL( TInt aType )
       
   289     {
       
   290     CAknViewAppUi::HandleResourceChangeL( aType );
       
   291     if ( aType == KEikDynamicLayoutVariantSwitch && iWindowManager && iWindowManager->ActiveWindow())
       
   292         {
       
   293         iWindowManager->ActiveWindow()->Relayout();
       
   294         }
       
   295     }
       
   296 
       
   297 // ------------------------------------------------------------------------------
       
   298 // CWidgetUiAppUi::HandleWsEventL
       
   299 // Handles events sent to the application by the window server
       
   300 //
       
   301 // ------------------------------------------------------------------------------
       
   302 //
       
   303 void CWidgetUiAppUi::HandleWsEventL(
       
   304     const TWsEvent& aEvent, CCoeControl* aDestination )
       
   305     {
       
   306 
       
   307     if ( aEvent.Type() == KAknUidValueEndKeyCloseEvent )
       
   308         {
       
   309         // Let the Container::OfferKeyEventL handle the red end key
       
   310         return;
       
   311         }
       
   312 
       
   313     if ( aEvent.Type() == EEventUser )
       
   314         {
       
   315         // Fetch the event data of the user event. Content of the event data:
       
   316         // bits 0-31: Uid of the WidgetAppUi to check if the event should be
       
   317         // handled here.
       
   318         // bits 32-63: Uid of the widget that should be closed.
       
   319         TUint8* eventData = aEvent.EventData();
       
   320         // Check if first 32 bits of event data contains uid of WidgetAppUi
       
   321         if ( ( TUint32& )*eventData == KWidgetAppUid )
       
   322             {
       
   323             eventData += sizeof( TUint32 );
       
   324             // Fetch uid of the widget that should be to be closed.
       
   325             TInt32 uidInteger = ( TInt32& )*eventData;
       
   326             TUid widgetUid = TUid::Uid( uidInteger );
       
   327 
       
   328             // Close the running widget with uid widgetUid
       
   329             //iWindowManager->CloseWindow( widgetUid );
       
   330             iWindowManager->Exit( EEikCmdExit,widgetUid );
       
   331 
       
   332             TWsEvent event;
       
   333             event.SetType(EEventWindowGroupListChanged);
       
   334             event.SetTimeNow();
       
   335             iEikonEnv->WsSession().SendEventToWindowGroup(
       
   336                 iEikonEnv->WsSession().GetFocusWindowGroup(), event);
       
   337 
       
   338             return;
       
   339             }
       
   340         }
       
   341 
       
   342     CAknViewAppUi::HandleWsEventL( aEvent, aDestination );
       
   343     }
       
   344     
       
   345 // -----------------------------------------------------------------------------
       
   346 // CWidgetUiAppUi::HandleForegroundEventL
       
   347 // called when application goes to background or comes back to foreground.
       
   348 //
       
   349 // Although WindowManager::OpenOrCreateWindowL does handle opening a new window,
       
   350 // we have to worry about "blank screen" / "resume" in power save operations.
       
   351 // It is true that there is now overhead with HandleForegroundEventL( ETrue )
       
   352 // and OpenOrCreateWindowL. The latter might need some checking, or e.g.
       
   353 // blinking might occur (flip the old widget visible, then open a new one).
       
   354 // -----------------------------------------------------------------------------
       
   355 //
       
   356 void CWidgetUiAppUi::HandleForegroundEventL( TBool aForeground )
       
   357     {
       
   358     iIsForeground = aForeground;
       
   359 
       
   360     if ( !iDelayedForegroundEvent->IsActive() )
       
   361         {
       
   362         iDelayedForegroundEvent->Start(TCallBack(HandleDelayedForegroundEventCallback,this));
       
   363         }
       
   364     }
       
   365     
       
   366 // ----------------------------------------------------
       
   367 // CWidgetUiAppUi::HandleApplicationSpecificEventL
       
   368 // called when application specific events like OOM from window server are triggered
       
   369 //
       
   370 // ----------------------------------------------------
       
   371 //
       
   372 void CWidgetUiAppUi::HandleApplicationSpecificEventL(TInt aEventType, const TWsEvent& aWsEvent)
       
   373     {
       
   374     CAknAppUi::HandleApplicationSpecificEventL(aEventType, aWsEvent);
       
   375 
       
   376     if ( iWindowManager )
       
   377         {
       
   378         if(aEventType == KAppOomMonitor_FreeRam )
       
   379             {
       
   380             iWindowManager->HandleOOMEventL(iIsForeground);
       
   381             }
       
   382         else if(aEventType == KAppOomMonitor_MemoryGood && iWindowManager->ActiveWindow())
       
   383             {
       
   384             iWindowManager->ActiveWindow()->Engine()->HandleCommandL( 
       
   385                                 (TInt)TBrCtlDefs::ECommandMemoryGood + (TInt)TBrCtlDefs::ECommandIdBase);
       
   386             }
       
   387         }
       
   388     }
       
   389 
       
   390 
       
   391 // -----------------------------------------------------------------------------
       
   392 // CWidgetUiAppUi::SendAppToBackground
       
   393 // force the application to the background
       
   394 //
       
   395 // -----------------------------------------------------------------------------
       
   396 //
       
   397 void CWidgetUiAppUi::SendAppToBackground()
       
   398     {
       
   399     TApaTaskList taskList( CEikonEnv::Static()->WsSession() );
       
   400     TUid wapUid( TUid::Uid( KWidgetAppUid ) );
       
   401     TApaTask task = taskList.FindApp( wapUid );
       
   402     task.SendToBackground();
       
   403     }
       
   404     
       
   405 // -----------------------------------------------------------------------------
       
   406 // CWidgetUiAppUi::SetRotationSupport()
       
   407 // check to see if screen oriantation can be changed
       
   408 // this code must match Widget::Widget in WidgetEngine
       
   409 //
       
   410 // -----------------------------------------------------------------------------
       
   411 //
       
   412 void CWidgetUiAppUi::SetRotationSupport()
       
   413     {
       
   414     iRotationSupported = iAvkonAppUi->OrientationCanBeChanged();
       
   415     }
       
   416 
       
   417 // -----------------------------------------------------------------------------
       
   418 // CWidgetUiAppUi::SetDisplayLandscapeL()
       
   419 // switch the display orientation to landscape
       
   420 //
       
   421 // -----------------------------------------------------------------------------
       
   422 //
       
   423 void CWidgetUiAppUi::SetDisplayLandscapeL( )
       
   424     {
       
   425     if (!iRotationSupported) return;
       
   426 
       
   427     if ( Orientation() == EAppUiOrientationPortrait )
       
   428         {
       
   429         SetOrientationL(EAppUiOrientationLandscape);
       
   430         }
       
   431     else if (Orientation() == EAppUiOrientationUnspecified)
       
   432         {
       
   433         TRect rect = ApplicationRect();
       
   434         TInt width = rect.Width();
       
   435         TInt height = rect.Height();
       
   436 
       
   437         if (width < height)
       
   438             {
       
   439             SetOrientationL(EAppUiOrientationLandscape);
       
   440             }
       
   441         }
       
   442     }
       
   443 
       
   444 // -----------------------------------------------------------------------------
       
   445 // CWidgetUiAppUi::SetDisplayPortraitL()
       
   446 // switch the display orientation to portrait
       
   447 //
       
   448 // -----------------------------------------------------------------------------
       
   449 //
       
   450 void CWidgetUiAppUi::SetDisplayPortraitL( )
       
   451     {
       
   452     if (!iRotationSupported) return;
       
   453 
       
   454     if ( Orientation() == EAppUiOrientationLandscape )
       
   455         {
       
   456         SetOrientationL(EAppUiOrientationPortrait);
       
   457         }
       
   458     else if (Orientation() == EAppUiOrientationUnspecified)
       
   459         {
       
   460         TRect rect = ApplicationRect();
       
   461         TInt width = rect.Width();
       
   462         TInt height = rect.Height();
       
   463 
       
   464         if (width > height)
       
   465             {
       
   466             SetOrientationL(EAppUiOrientationPortrait);
       
   467             }
       
   468         }
       
   469     }
       
   470 
       
   471 // -----------------------------------------------------------------------------
       
   472 // CWidgetUiAppUi::SetDisplayAuto()
       
   473 // switch the display orientation to default device orientation
       
   474 //
       
   475 // -----------------------------------------------------------------------------
       
   476 //
       
   477 void CWidgetUiAppUi::SetDisplayAuto( )
       
   478     {
       
   479     TRAP_IGNORE(SetOrientationL(EAppUiOrientationAutomatic));
       
   480     }
       
   481 
       
   482 // -----------------------------------------------------------------------------
       
   483 // CWidgetUiAppUi::AsyncExit()
       
   484 // start an async exit
       
   485 //
       
   486 // -----------------------------------------------------------------------------
       
   487 //
       
   488 void CWidgetUiAppUi::AsyncExit( TBool aAllWidgets )
       
   489     {
       
   490     if (aAllWidgets && iWindowManager)
       
   491         {
       
   492         iWindowManager->CloseAllWindowsExceptCurrent();
       
   493         }
       
   494     iWidgetUiAsyncExit->Start(); // close current and exits app if no widgets left running
       
   495     }
       
   496 
       
   497 // -----------------------------------------------------------------------------
       
   498 // CWidgetUiAppUi::HandleDelayedForegroundEventCallback()
       
   499 // CIdle callback function to handle delayed foreground events for WidgetUI
       
   500 //
       
   501 // -----------------------------------------------------------------------------
       
   502 //
       
   503 TInt CWidgetUiAppUi::HandleDelayedForegroundEventCallback( TAny* aAppUi )
       
   504     {
       
   505     return ((CWidgetUiAppUi*)aAppUi)->HandleDelayedForegroundEvent();
       
   506     }
       
   507     
       
   508 // -----------------------------------------------------------------------------
       
   509 // CWidgetUiAppUi::HandleDelayedForegroundEvent()
       
   510 // function to handle delayed foreground events for WidgetUI
       
   511 //
       
   512 // -----------------------------------------------------------------------------
       
   513 //
       
   514 TInt CWidgetUiAppUi::HandleDelayedForegroundEvent( )
       
   515     {
       
   516     if (iWindowManager)
       
   517         {
       
   518         iWindowManager->HandleForegroundEvent(iIsForeground);
       
   519         }
       
   520     return (KErrNone);
       
   521     }
       
   522 
       
   523 // ------------------------------------------------------------------------
       
   524 // CWidgetUiWindowManager::RegisterWidgetL
       
   525 //
       
   526 // Register widget to CPS listener.
       
   527 // ------------------------------------------------------------------------
       
   528 void CWidgetUiAppUi::ProcessMessageArgumentsL( 
       
   529     const TDesC8& aLine,
       
   530     TUid& aUid,
       
   531     TUint32& aOperation )
       
   532     {
       
   533     __UHEAP_MARK;
       
   534     CWidgetPropertyValue* value( NULL );
       
   535     TUint32 version( -1 );
       
   536     TPtrC ptr( NULL, 0 );
       
   537     HBufC* tmp( NULL );
       
   538     
       
   539     RDesReadStream stream( aLine );
       
   540     CleanupClosePushL( stream );
       
   541     
       
   542     version = stream.ReadUint32L();
       
   543     
       
   544     if ( version != 1 )
       
   545         {
       
   546         User::Leave( KErrNotSupported );
       
   547         }
       
   548     
       
   549     aUid.iUid = stream.ReadUint32L();
       
   550     aOperation = stream.ReadInt32L();
       
   551     
       
   552     CleanupStack::PopAndDestroy( &stream );
       
   553     __UHEAP_MARKEND;
       
   554     }
       
   555     
       
   556 // -----------------------------------------------------------------------------
       
   557 // CWidgetUiAppUi::ExitWidget()
       
   558 // ApiProvider callback function to exit WidgetUI
       
   559 //
       
   560 // -----------------------------------------------------------------------------
       
   561 //
       
   562 void CWidgetUiAppUi::ExitWidget( )
       
   563     {
       
   564     if (iWindowManager)
       
   565         {
       
   566         if ( iWindowManager->ActiveWindow() )
       
   567             iWindowManager->Exit(EEikCmdExit, iWindowManager->ActiveWindow()->Uid());
       
   568         }
       
   569     }
       
   570 
       
   571 void CWidgetUiAppUi::ProcessCommandL(TInt aCommand )
       
   572     {
       
   573         TBool doesLSKHandlerExist = EFalse;
       
   574     
       
   575         if ( aCommand == EAknSoftkeyOptions )
       
   576             {
       
   577                 if ( iWindowManager)
       
   578                     {
       
   579                         doesLSKHandlerExist = iWindowManager->HandleLSKCommandL(aCommand);
       
   580                     }
       
   581             }
       
   582         
       
   583         if ( doesLSKHandlerExist == EFalse ) 
       
   584             {
       
   585                 CAknViewAppUi::ProcessCommandL(aCommand);
       
   586             }
       
   587     }
       
   588 
       
   589 // End of File