Nokia N97 SDK
Example Applications Guide

COomExampleAppUi Class Reference

#include <OomExampleAppUi.h>

Inheritance diagram for COomExampleAppUi:

MMemoryRequesterObserver List of all members.

Detailed Description

COomExampleAppUi application UI class. Interacts with the user through the UI and request message processing from the handler class

Definition at line 35 of file OomExampleAppUi.h.


Public Member Functions

void  ConstructL ()
  COomExampleAppUi ()
virtual  ~COomExampleAppUi ()

Private Member Functions

void  HandleCommandL (TInt aCommand)
virtual void  MemoryRequestCompleteL (TInt aResult)
void  HandleSynchronouslyRequestFreeMemoryMenuItemSelectedL ()
void  HandleAsynchronouslyRequestFreeMemoryMenuItemSelectedL ()
void  HandleNormalMenuItemSelectedL ()
void  HandleHighMenuItemSelectedL ()
void  HandleBusyMenuItemSelectedL ()
TInt  MemoryToRequestL (TInt &aKilobytesRequested)
void  DoAllocateMemoryL (TBool aAllocate, TInt aResult)

Private Attributes

TInt  iBytesRequested
ROomMonitorSession  iOomMonitorSession
COomExampleAppView *  iAppView
CMemoryRequester *  iMemoryRequester


Constructor & Destructor Documentation

COomExampleAppUi::COomExampleAppUi (   ) 
 

COomExampleAppUi. C++ default constructor. This needs to be public due to the way the framework constructs the AppUi

Definition at line 48 of file OomExampleAppUi.cpp.

00049     {
00050     // No implementation required
00051     }

COomExampleAppUi::~COomExampleAppUi (   )  [virtual]
 

~COomExampleAppUi. Virtual Destructor.

Definition at line 53 of file OomExampleAppUi.cpp.

References iAppView, iMemoryRequester, and iOomMonitorSession.

00054     {
00055     if (iAppView)
00056         {
00057         delete iAppView;
00058         iAppView = NULL;
00059         }
00060     
00061     //Close the handle to the OomMonitor session using standard Close() API
00062     iOomMonitorSession.Close();
00063     
00064     delete iMemoryRequester;
00065     }


Member Function Documentation

void COomExampleAppUi::ConstructL (   ) 
 

ConstructL. 2nd phase constructor.

Definition at line 33 of file OomExampleAppUi.cpp.

References iAppView, iMemoryRequester, iOomMonitorSession, CMemoryRequester::NewL(), and COomExampleAppView::NewL().

00034     {
00035     // Initialise app UI with standard value.
00036     BaseConstructL(CAknAppUi::EAknEnableSkin);
00037 
00038     iAppView = COomExampleAppView::NewL(ClientRect());
00039     
00040     // Connect to the OomMonitorSession using standard Connect() API.
00041     // As this app is exclusively written to demostrate OomMonitor functionality there is no value 
00042     // in trying to trying to handle any error here so we just leave. 
00043     User::LeaveIfError(iOomMonitorSession.Connect());
00044     
00045     iMemoryRequester = CMemoryRequester::NewL(iOomMonitorSession, *this);
00046     }

void COomExampleAppUi::HandleCommandL (  TInt  aCommand  )  [private]
 

From CEikAppUi, HandleCommandL. Takes care of command handling.

Parameters:
aCommand  Command to be handled.

Definition at line 71 of file OomExampleAppUi.cpp.

References HandleAsynchronouslyRequestFreeMemoryMenuItemSelectedL(), HandleBusyMenuItemSelectedL(), HandleHighMenuItemSelectedL(), HandleNormalMenuItemSelectedL(), and HandleSynchronouslyRequestFreeMemoryMenuItemSelectedL().

00072     {
00073     switch (aCommand)
00074         {
00075         case EEikCmdExit: //fall through
00076         case EAknSoftkeyExit:
00077             Exit();
00078             break;
00079         case EMMDContainerViewSynchronouslyRequestFreeMemoryMenuItemCommand:
00080             HandleSynchronouslyRequestFreeMemoryMenuItemSelectedL();
00081             break;
00082         case EMMDContainerViewAsynchronouslyRequestFreeMemoryMenuItemCommand:
00083             HandleAsynchronouslyRequestFreeMemoryMenuItemSelectedL();
00084             break;
00085         case EMMDContainerViewNormalMenuItemCommand:
00086             HandleNormalMenuItemSelectedL();
00087             break;
00088         case EMMDContainerViewHigh_MenuItemCommand:
00089             HandleHighMenuItemSelectedL();
00090             break;
00091         case EMMDContainerViewBusyMenuItemCommand:
00092             HandleBusyMenuItemSelectedL();
00093             break;
00094         default:
00095             Panic(EOomExampleUiInvalidCommand);
00096             break;
00097         }
00098     }

void COomExampleAppUi::MemoryRequestCompleteL (  TInt  aResult  )  [private, virtual]
 

Callback from CMemoryRequester::Start()

Implements MMemoryRequesterObserver.

Definition at line 103 of file OomExampleAppUi.cpp.

References DoAllocateMemoryL(), and iBytesRequested.

00104     {
00105     if (aResult < 0)
00106         {
00107         // A system wide error code, there is insufficient memory available to make a large 
00108         // allocation
00109         
00110         DoAllocateMemoryL(EFalse, aResult);
00111         }
00112     else //KErrNone
00113         {
00114         //Sucess, we can allocate memory
00115         
00116         DoAllocateMemoryL(ETrue, iBytesRequested);
00117         }
00118     }

void COomExampleAppUi::HandleSynchronouslyRequestFreeMemoryMenuItemSelectedL (   )  [private]
 

Handle an attempt to ask the OomMonitor to synchronously free memory before making an allocation

Definition at line 124 of file OomExampleAppUi.cpp.

References DoAllocateMemoryL(), iBytesRequested, iOomMonitorSession, and MemoryToRequestL().

Referenced by HandleCommandL().

00125     {
00126     TInt kilobytesRequested = 0;
00127     //in this example app we ask the user how much memory we should allocate
00128     if (MemoryToRequestL(kilobytesRequested))
00129         {
00130                 iBytesRequested = kilobytesRequested * KBytesInKilobyte;
00131                 
00132                 //It is generally not necessary to dynamically compare against KOomMaxAllocationWithoutPermission 
00133                 //as this value will not change from 1MB.
00134         if (iBytesRequested > KOomMaxAllocationWithoutPermission)
00135             {
00136             // An application making an allocation greater than KOomMaxAllocationWithoutPermission
00137             // bytes should first request free memory from the Oom Monitor 
00138             //
00139             // The request can take a number of seconds to complete, so the synchronous 
00140             // version of the API is likely to be inappropriate in most real application 
00141             // situations.        
00142             TInt ret = iOomMonitorSession.RequestFreeMemory(iBytesRequested);
00143             
00144             if (ret == KErrNone)
00145                 {
00146                 // Return of KErrNone suggests that either there was already plenty of memory in the 
00147                 // system, or the OomMonitor has managed to free enough memory for use to safely 
00148                 // make the allocation.
00149                 
00150                 DoAllocateMemoryL(ETrue, iBytesRequested);
00151                 }
00152             else if (ret == KErrNoMemory)
00153                 {
00154                 // Return of KErrMemory suggests that the OomMonitor has not managed to free sufficient 
00155                 // memory. 
00156                 // It is not safe to make a large allocation.
00157                 //
00158                 // At this point the application could try freeing it's own resources and then make 
00159                 // another cal to ROomMonitorSession::RequestFreeMemory()
00160                 
00161                 DoAllocateMemoryL(EFalse, KErrNoMemory);
00162                 }
00163             else User::LeaveIfError(ret);
00164             }
00165         else
00166             {
00167             // For small allocations, less than KOomMaxAllocationWithoutPermission
00168             // there is no need to inform the OOM Monitor before allocation.
00169             // If the memory level falls before a certain threshold, the OOM Monitor
00170             // will automatically start trying to free memory.
00171             
00172             DoAllocateMemoryL(ETrue, iBytesRequested);
00173             }
00174         }
00175     }

void COomExampleAppUi::HandleAsynchronouslyRequestFreeMemoryMenuItemSelectedL (   )  [private]
 

Handle an attempt ask the OomMonitor to asynchronously free memory before making an allocation

Definition at line 181 of file OomExampleAppUi.cpp.

References DoAllocateMemoryL(), iBytesRequested, iMemoryRequester, MemoryToRequestL(), and CMemoryRequester::Start().

Referenced by HandleCommandL().

00182     {
00183     TInt kilobytesRequested = 0;
00184     //in this example app we ask the user how much memory we should allocate
00185     if (MemoryToRequestL(kilobytesRequested))
00186         {
00187                 iBytesRequested = kilobytesRequested * KBytesInKilobyte;
00188                 //It is generally not necessary to dynamically compare against KOomMaxAllocationWithoutPermission 
00189             //as this value will not change from 1MB.
00190         if (iBytesRequested > KOomMaxAllocationWithoutPermission)
00191             {
00192             // An application making an allocation greater than KOomMaxAllocationWithoutPermission
00193             // bytes should first request free memory from the Oom Monitor 
00194             //
00195             // The request can take a number of seconds to complete, so the asynchronous 
00196             // version of the API used here is likely to be the more appropriate in one 
00197             // in most real application situations.
00198             // 
00199             // The asynchronous call to the oommonitor is wrapped up in an active object 
00200 
00201             iMemoryRequester->Start(iBytesRequested);        
00202             }
00203         else 
00204             {
00205             // For small allocations, less than KOomMaxAllocationWithoutPermission
00206             // there is no need to inform the OOM Monitor before allocation.
00207             // If the memory level falls before a certain threshold, the OOM Monitor
00208             // will automatically start trying to free memory.
00209             
00210             DoAllocateMemoryL(ETrue, iBytesRequested);            
00211             }
00212         }
00213     }

void COomExampleAppUi::HandleNormalMenuItemSelectedL (   )  [private]
 

A request to set the application's priority as normal with the OOMMonitor

Definition at line 263 of file OomExampleAppUi.cpp.

References iOomMonitorSession.

Referenced by HandleCommandL().

00264     {
00265     // This  API should be called after 
00266     // ROomMonitorSession::SetOomPriority(ROomMonitorSession::EOomPriorityHigh) or 
00267     // ROomMonitorSession::SetOomPriority(ROomMonitorSession::EOomPriorityBusy)
00268     // to return the applications's priority back to it's default value
00269     iOomMonitorSession.SetOomPriority(ROomMonitorSession::EOomPriorityNormal);
00270      
00271     CAknGlobalNote* globalNote = CAknGlobalNote::NewL(); //Initialize the note
00272     CleanupStack::PushL(globalNote);
00273     _LIT(Kmsg, "Priority changed to Normal");
00274     globalNote->SetSoftkeys(R_AVKON_SOFTKEYS_CLOSE);
00275     globalNote->ShowNoteL( EAknGlobalInformationNote, Kmsg);
00276     CleanupStack::PopAndDestroy(); // globalNote    
00277     }

void COomExampleAppUi::HandleHighMenuItemSelectedL (   )  [private]
 

A request to set the application's priority as high with the OOMMonitor

Definition at line 218 of file OomExampleAppUi.cpp.

References iOomMonitorSession.

Referenced by HandleCommandL().

00219     {
00220     // Inform the OomMonitor that the application is doing something of high priority while being in background
00221     // 
00222     // Closing high priority applications will typically be one of the last resorts the 
00223     // OomMonitor will take in the event of RAM levels going below the device specific 
00224     // low memory threshold
00225     //
00226     // Note that the OomMonitor will never close the foreground application so it is only 
00227     // necessary to call this API if doing important background processing  
00228     iOomMonitorSession.SetOomPriority(ROomMonitorSession::EOomPriorityHigh);
00229   
00230     CAknGlobalNote* globalNote = CAknGlobalNote::NewL(); //Initialize the note
00231     CleanupStack::PushL(globalNote);
00232     _LIT(Kmsg, "Priority changed to High");
00233     globalNote->SetSoftkeys(R_AVKON_SOFTKEYS_CLOSE);
00234     globalNote->ShowNoteL( EAknGlobalInformationNote, Kmsg);
00235     CleanupStack::PopAndDestroy(); // globalNote    
00236     }

void COomExampleAppUi::HandleBusyMenuItemSelectedL (   )  [private]
 

A request to set the application's priority as busy with the OOMMonitor

Definition at line 241 of file OomExampleAppUi.cpp.

References iOomMonitorSession.

Referenced by HandleCommandL().

00242     {
00243     // Inform the OomMonitor that the application is busy doing some critical task
00244     // 
00245     // The OomMonitor will never close applications that have set themselves 
00246     // as busy even if system RAM goes below the device specific low memory threshold.
00247     //
00248     // Note that the OomMonitor will never close the foreground application so it is only 
00249     // necessary to call this API if doing critical background processing 
00250     iOomMonitorSession.SetOomPriority(ROomMonitorSession::EOomPriorityBusy);
00251     
00252     CAknGlobalNote* globalNote = CAknGlobalNote::NewL(); //Initialize the note
00253     CleanupStack::PushL(globalNote);
00254     _LIT(Kmsg, "Priority changed to Busy");
00255     globalNote->SetSoftkeys(R_AVKON_SOFTKEYS_CLOSE);
00256     globalNote->ShowNoteL( EAknGlobalInformationNote, Kmsg);
00257     CleanupStack::PopAndDestroy(); // globalNote    
00258     }

TInt COomExampleAppUi::MemoryToRequestL (  TInt &  aKilobytesRequested  )  [private]
 

Asks the user to simulate an application requiring some memory

Parameters:
aBytesRequested  a number of bytes to allocate
Returns:
a positive integer if the user successfully enters a valid number of bytes

Definition at line 312 of file OomExampleAppUi.cpp.

Referenced by HandleAsynchronouslyRequestFreeMemoryMenuItemSelectedL(), and HandleSynchronouslyRequestFreeMemoryMenuItemSelectedL().

00313     {
00314     _LIT(Kmsg, "Number of KiloBytes to request");
00315     
00316     CAknNumberQueryDialog* dialog = CAknNumberQueryDialog::NewL(aKilobytesRequested,CAknQueryDialog::ENoTone);
00317     dialog->PrepareLC(R_SC_DIALOG);
00318     dialog->SetPromptL(Kmsg);
00319     return dialog->RunLD();     
00320     }


Member Data Documentation

COomExampleAppView* COomExampleAppUi::iAppView [private]
 

The application view Own

Definition at line 89 of file OomExampleAppUi.h.

Referenced by ConstructL(), and ~COomExampleAppUi().

CMemoryRequester* COomExampleAppUi::iMemoryRequester [private]
 

Own

Definition at line 95 of file OomExampleAppUi.h.

Referenced by ConstructL(), HandleAsynchronouslyRequestFreeMemoryMenuItemSelectedL(), and ~COomExampleAppUi().


The documentation for this class was generated from the following files:

© Nokia 2009

Back to top