|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * ============================================================================ 00003 * Name : OomExampleAppUi.cpp 00004 * Part of : OomMonitor Example Application 00005 * Description : Application UI 00006 * Version : %version: 1 % << Don't touch! Updated by Synergy at check-out. 00007 * 00008 * Copyright © 2009 Nokia. All rights reserved. 00009 * This material, including documentation and any related computer 00010 * programs, is protected by copyright controlled by Nokia. All 00011 * rights are reserved. Copying, including reproducing, storing, 00012 * adapting or translating, any or all of this material requires the 00013 * prior written consent of Nokia. This material also contains 00014 * confidential information which may not be disclosed to others 00015 * without the prior written consent of Nokia. 00016 * ============================================================================ 00017 * Template version: 4.2 00018 */ 00019 00020 #include <avkon.hrh> 00021 #include <aknnotewrappers.h> 00022 #include <aknglobalnote.h> 00023 #include <OomExample_0xA0007E05.rsg> 00024 00025 #include "OomExample.hrh" 00026 #include "OomExample.pan" 00027 #include "OomExampleApplication.h" 00028 #include "OomExampleAppUi.h" 00029 #include "OomExampleAppView.h" 00030 00031 const TInt KBytesInKilobyte = 1024; 00032 00033 void COomExampleAppUi::ConstructL() 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 } 00047 00048 COomExampleAppUi::COomExampleAppUi() 00049 { 00050 // No implementation required 00051 } 00052 00053 COomExampleAppUi::~COomExampleAppUi() 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 } 00066 00067 /** 00068 * COomExampleAppUi::HandleCommandL() 00069 * Takes care of command handling. 00070 */ 00071 void COomExampleAppUi::HandleCommandL(TInt aCommand) 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 } 00099 00100 /** 00101 * Callback from CMemoryRequester::Start() 00102 */ 00103 void COomExampleAppUi::MemoryRequestCompleteL(TInt aResult) 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 } 00119 00120 /** 00121 * Handle an attempt to ask the OomMonitor to synchronously free memory before making 00122 * an allocation 00123 */ 00124 void COomExampleAppUi::HandleSynchronouslyRequestFreeMemoryMenuItemSelectedL() 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 } 00176 00177 /** 00178 * Handle an attempt ask the OomMonitor to asynchronously free memory before making 00179 * an allocation 00180 */ 00181 void COomExampleAppUi::HandleAsynchronouslyRequestFreeMemoryMenuItemSelectedL() 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 } 00214 00215 /** 00216 * A request to set the application's priority as high with the OOMMonitor 00217 */ 00218 void COomExampleAppUi::HandleHighMenuItemSelectedL() 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 } 00237 00238 /** 00239 * A request to set the application's priority as busy with the OOMMonitor 00240 */ 00241 void COomExampleAppUi::HandleBusyMenuItemSelectedL() 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 } 00259 00260 /** 00261 * A request to set the application's priority as normal with the OOMMonitor 00262 */ 00263 void COomExampleAppUi::HandleNormalMenuItemSelectedL() 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 } 00278 00279 void COomExampleAppUi::DoAllocateMemoryL(TBool aAllocate, TInt aResult) 00280 { 00281 TBuf<64> msgBuf; 00282 00283 if(aAllocate) 00284 { 00285 // We would actually allocate the memory we want here... 00286 00287 _LIT(KMsgSuccess, "Memory request complete. Bytes freed: "); 00288 msgBuf.Append(KMsgSuccess); 00289 } 00290 else 00291 { 00292 // We would handle the fact that there is insufficient memory in the 00293 // system to make the large allocation we would like here... 00294 00295 _LIT(KMsgFail, "Memory request failed. Error Code: "); 00296 msgBuf.Append(KMsgFail); 00297 } 00298 00299 msgBuf.AppendNum(aResult); 00300 CAknGlobalNote* globalNote = CAknGlobalNote::NewL(); //Initialize the note 00301 CleanupStack::PushL(globalNote); 00302 globalNote->SetSoftkeys(R_AVKON_SOFTKEYS_CLOSE); 00303 globalNote->ShowNoteL( EAknGlobalInformationNote, msgBuf); 00304 CleanupStack::PopAndDestroy(); // globalNote 00305 } 00306 00307 /** 00308 * Asks the user to simulate an application requiring some memory 00309 * @param aBytesRequested a number of bytes to allocate 00310 * @return a positive integer if the user successfully enters a valid number of bytes 00311 */ 00312 TInt COomExampleAppUi::MemoryToRequestL(TInt& aKilobytesRequested) 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 }
|
© Nokia 2009 |