|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 ============================================================================ 00003 Name : HsSMWidgetExample.cpp 00004 Author : 00005 Version : 1.1 00006 Copyright : This material, including documentation and any related computer 00007 programs, is protected by copyright controlled by Nokia Corporation. 00008 Description : CHsSMWidgetExample implementation 00009 ============================================================================ 00010 */ 00011 00012 #include "HsSMWidgetExample.h" 00013 #include "gmssignalmonitor.h" 00014 00015 #include <HsWidget.rsg> 00016 #include <hswidgetpublisher.h> 00017 #include <hswidget.h> 00018 #include <hsexception.h> 00019 #include <hsdataobserver.h> 00020 00021 #include <coemain.h> 00022 #include <eikenv.h> 00023 #include <utf.h> 00024 #include <apgtask.h> 00025 #include <e32math.h> 00026 #include <StringLoader.h> 00027 00028 // skin support 00029 #include <aknsskininstance.h> 00030 #include <aknsutils.h> 00031 #include <aknsbasicbackgroundcontrolcontext.h> 00032 #include <aknsdrawutils.h> 00033 00034 // logging 00035 #include "logger.h" 00036 00037 // the name of the widget template's image item 00038 const char* wideimageitem = "image1"; 00039 00040 // ----------------------------------------------------------------------------- 00041 // CHsSMWidgetExample::CHsSMWidgetExample() 00042 // C++ constructor, for performing the 1st stage construction 00043 // ----------------------------------------------------------------------------- 00044 // 00045 CHsSMWidgetExample::CHsSMWidgetExample() 00046 { 00047 //initialize measurements array with random values 00048 for (TInt i=0; i<KNumberOfMeasurements; i++) 00049 iMeasurements[i]= Math::Random()%8; 00050 } 00051 00052 // ----------------------------------------------------------------------------- 00053 // CHsSMWidgetExample::~CHsSMWidgetExample() 00054 // C++ destructor 00055 // ----------------------------------------------------------------------------- 00056 // 00057 CHsSMWidgetExample::~CHsSMWidgetExample() 00058 { 00059 delete iMonitor; 00060 00061 delete iWideFrameContext; 00062 delete iWideFrameDevice; 00063 delete iWideFrame; 00064 00065 delete iHsWidgetPublisher; 00066 } 00067 00068 // ----------------------------------------------------------------------------- 00069 // CHsSMWidgetExample::NewLC() 00070 // Two-phased constructor. Leaves the constructed object on the cleanup stack 00071 // ----------------------------------------------------------------------------- 00072 // 00073 CHsSMWidgetExample* CHsSMWidgetExample::NewLC() 00074 { 00075 CHsSMWidgetExample* self = new (ELeave)CHsSMWidgetExample(); 00076 CleanupStack::PushL(self); 00077 self->ConstructL(); 00078 return self; 00079 } 00080 00081 // ----------------------------------------------------------------------------- 00082 // CHsSMWidgetExample::NewL() 00083 // Two-phased constructor 00084 // ----------------------------------------------------------------------------- 00085 // 00086 CHsSMWidgetExample* CHsSMWidgetExample::NewL() 00087 { 00088 CHsSMWidgetExample* self=CHsSMWidgetExample::NewLC(); 00089 CleanupStack::Pop(); // self; 00090 return self; 00091 } 00092 00093 // ----------------------------------------------------------------------------- 00094 // CHsSMWidgetExample::ConstructL() 00095 // Two-phased constructor. 00096 // Creates an instance of Homescreen Publishing API's HsWidgetPublisher and loads 00097 // the widget's definition from the resource file 00098 // ----------------------------------------------------------------------------- 00099 // 00100 void CHsSMWidgetExample::ConstructL() 00101 { 00102 __LOGSTR_TOFILE("Enter CHsSMWidgetExample::ConstructL"); 00103 00104 try 00105 { 00106 iHsWidgetPublisher = new Hs::HsWidgetPublisher( this ); 00107 } 00108 catch( Hs::HsException& e) 00109 { 00110 //catch error from Homescreen Publishing API 00111 __LOGSTR_TOFILE1("[-] HSPAPI exception : %d", e.getReason()); 00112 User::Leave( e.getReason() ); 00113 } 00114 catch( ... ) 00115 { 00116 //catch error from other libraries 00117 __LOGSTR_TOFILE("[-] Generic exception"); 00118 User::Leave( KErrGeneral ); 00119 } 00120 00121 // instantiate the network signal monitor and assign this object as the 00122 // observer of network signal value changes 00123 iMonitor = CGMSSignalMonitor::NewL(*this); 00124 00125 // create a buffer bitmap on which a graph will be drawn 00126 iWideFrame = new (ELeave) CFbsBitmap; 00127 User::LeaveIfError( iWideFrame->Create(TSize(312,82), 00128 CEikonEnv::Static()->DefaultDisplayMode())); 00129 00130 // Create a frame buffer graphics context 00131 iWideFrameDevice = CFbsBitmapDevice::NewL(iWideFrame); 00132 User::LeaveIfError(iWideFrameDevice->CreateContext(iWideFrameContext)); 00133 00134 //converitng string from UNICODE to UTF-8 std::string 00135 HBufC* buf1 = StringLoader::LoadLC(R_SM_TEMPLATE_TYPE); 00136 templateType = ToStringL( *buf1 ); 00137 CleanupStack::PopAndDestroy(buf1); // delete buf1 00138 buf1 = StringLoader::LoadLC( R_SM_WIDGET_NAME ); 00139 widgetName = ToStringL( *buf1 ); 00140 CleanupStack::PopAndDestroy(buf1); // delete buf1 00141 buf1 = StringLoader::LoadLC( R_SM_WIDGET_ID ); 00142 widgetId = ToStringL( *buf1 ); 00143 CleanupStack::PopAndDestroy(buf1); // delete buf1 00144 00145 __LOGSTR_TOFILE("Exit CHsSMWidgetExample::ConstructL"); 00146 } 00147 00148 // ----------------------------------------------------------------------------- 00149 // CHsSMWidgetExample::RegisterWidgetL() 00150 // Once registered the widget will become available to the user for adding it to 00151 // the home screen. If the widgetId is already registered the iHsWidgetPublisher 00152 // will now point to it. Also, note that the Home Screen Publishing API will 00153 // record this application's UID3/SID as the publiser of this widget and based 00154 // on this information the application will be started in the background if at 00155 // any given time an event is due to be sent to it but the application is found 00156 // not to be running. 00157 // ----------------------------------------------------------------------------- 00158 // 00159 void CHsSMWidgetExample::RegisterWidgetL() 00160 { 00161 __LOGSTR_TOFILE("Enter CHsSMWidgetExample::RegisterWidgetL"); 00162 try 00163 { 00164 iHsWidgetPublisher->createHsWidget( templateType, 00165 widgetName, widgetId ); 00166 } 00167 catch( Hs::HsException& e) 00168 { 00169 //catch error from Homescreen Publishing API 00170 __LOGSTR_TOFILE1("[-] HSPAPI exception : %d", e.getReason()); 00171 User::Leave( e.getReason() ); 00172 } 00173 catch( ... ) 00174 { 00175 //catch error from other libraries 00176 __LOGSTR_TOFILE("[-] Generic exception"); 00177 User::Leave( KErrGeneral ); 00178 } 00179 __LOGSTR_TOFILE("Exit CHsSMWidgetExample::RegisterWidgetL"); 00180 } 00181 00182 // ----------------------------------------------------------------------------- 00183 // CHsSMWidgetExample::PublishWidgetL 00184 // A template is published by the Home Screen Publishing API. If the home screen gets 00185 // template's data it obtains the actual information. 00186 // ----------------------------------------------------------------------------- 00187 // 00188 void CHsSMWidgetExample::PublishWidgetL() 00189 { 00190 __LOGSTR_TOFILE("Enter CHsSMWidgetExample::PublishWidgetL"); 00191 try 00192 { 00193 iHsWidgetPublisher->publishHsWidget( 00194 iHsWidgetPublisher->getHsWidget( templateType, 00195 widgetName, widgetId ) ); 00196 } 00197 catch( Hs::HsException& e) 00198 { 00199 //catch error from Homescreen Publishing API 00200 __LOGSTR_TOFILE1("[-] HSPAPI exception : %d", e.getReason()); 00201 User::Leave( e.getReason() ); 00202 } 00203 catch( ... ) 00204 { 00205 //catch error from other libraries 00206 __LOGSTR_TOFILE("[-] Generic exception"); 00207 User::Leave( KErrGeneral ); 00208 } 00209 __LOGSTR_TOFILE("Exit CHsSMWidgetExample::PublishWidgetL"); 00210 } 00211 00212 // ----------------------------------------------------------------------------- 00213 // CHsSMWidgetExample::ChangeWidgetValuesL() 00214 // Change items' values. They will only be commited (and updated on the 00215 // home screen) once you call CHsSMWidgetExample::PublishWidgetL() 00216 // ----------------------------------------------------------------------------- 00217 // 00218 void CHsSMWidgetExample::ChangeWidgetValuesL() 00219 { 00220 __LOGSTR_TOFILE("Enter CHsSMWidgetExample::ChangeWidgetValuesL"); 00221 try 00222 { 00223 // passing the bitmap handle to the Homescreen Publishing API 00224 iHsWidgetPublisher->getHsWidget( templateType, widgetName, 00225 widgetId ).setItem(wideimageitem, iWideFrame->Handle()); 00226 } 00227 catch( Hs::HsException& e) 00228 { 00229 //catch error from Homescreen Publishing API 00230 __LOGSTR_TOFILE1("HSPAPI exception : %d", e.getReason()); 00231 User::Leave( e.getReason() ); 00232 } 00233 catch( ... ) 00234 { 00235 //catch error from other libraries 00236 __LOGSTR_TOFILE("Generic exception"); 00237 User::Leave( KErrGeneral ); 00238 } 00239 __LOGSTR_TOFILE("Exit CHsSMWidgetExample::ChangeWidgetValuesL"); 00240 } 00241 00242 // ----------------------------------------------------------------------------- 00243 // CHsSMWidgetExample::RemoveWidgetL() 00244 // Remove a template from the Home Screen Publishing API. If the widget is currently 00245 // visible on the homescreen it will be removed. Once removed the widget is now 00246 // not available for selection on the home screen and it doesn't report events 00247 // ----------------------------------------------------------------------------- 00248 // 00249 void CHsSMWidgetExample::RemoveWidgetL() 00250 { 00251 __LOGSTR_TOFILE("Enter CHsSMWidgetExample::RemoveWidgetL"); 00252 try 00253 { 00254 iHsWidgetPublisher->removeHsWidget( templateType, 00255 widgetName, widgetId); 00256 } 00257 catch( Hs::HsException& e) 00258 { 00259 //catch error from Homescreen Publishing API 00260 __LOGSTR_TOFILE1("HSPAPI exception : %d", e.getReason()); 00261 User::Leave( e.getReason() ); 00262 } 00263 catch( ... ) 00264 { 00265 //catch error from other libraries 00266 __LOGSTR_TOFILE("Generic exception"); 00267 User::Leave( KErrGeneral ); 00268 } 00269 __LOGSTR_TOFILE("Exit CHsSMWidgetExample::RemoveWidgetL"); 00270 } 00271 00272 // ----------------------------------------------------------------------------- 00273 // CHsSMWidgetExample::IsWidgetRegistered() 00274 // Check whether the widget is already registered or not. 00275 // ----------------------------------------------------------------------------- 00276 /** 00277 * 00278 * One note of warning about the implementation of IsWidgetRegisteredL: it relies 00279 * on the fact that HsWidgetPublisher::getHsWidget will throw exception 00280 * (KErrNotFound) if the referenced widget is not already registered. 00281 * The problem is that in fact HsWidgetPublisher::getHsWidget (and the other 00282 * methods as well) will only search for the widget locally, in the current 00283 * process. 00284 * 00285 * Consider the case where we would like to check the registration status of a 00286 * widget when our application starts. Even if the widget is already registered 00287 * the call to IsWidgetRegisteredL will always return false. 00288 * 00289 * For this reason in CHsWidgetAppUi::ConstructL the code sequence: 00290 * 00291 * \code 00292 * iSMWidgetExample = CHsSMWidgetExample::NewL(); 00293 * if(iSMWidgetExample->IsWidgetRegisteredL()) 00294 * iSMWidgetExample->RemoveWidgetL(); 00295 * delete iSMWidgetExample; 00296 * iSMWidgetExample = NULL; 00297 * \endcode 00298 * 00299 * had to be replaced by 00300 * 00301 * \code 00302 * iSMWidgetExample = CHsSMWidgetExample::NewL(); 00303 * iSMWidgetExample->RegisterWidgetL(); 00304 * iSMWidgetExample->RemoveWidgetL(); 00305 * delete iSMWidgetExample; 00306 * iSMWidgetExample = NULL; 00307 * \endcode 00308 * 00309 * You may expect that since we know the widget to be already registered 00310 * the call to iSMWidgetExample->RegisterWidgetL(); would leave with 00311 * KErrAlreadyExists. In fact this does not happen as this is the first 00312 * "local" registration of the widget. 00313 * 00314 */ 00315 TBool CHsSMWidgetExample::IsWidgetRegisteredL() 00316 { 00317 __LOGSTR_TOFILE("Enter CHsSMWidgetExample::IsWidgetRegisteredL"); 00318 00319 TBool result = ETrue; 00320 try 00321 { 00322 iHsWidgetPublisher->getHsWidget( templateType, 00323 widgetName, widgetId); 00324 } 00325 catch( Hs::HsException& e) 00326 { 00327 __LOGSTR_TOFILE1("[-] HSPAPI exception : %d", e.getReason()); 00328 if(e.getReason()==KErrNotFound) 00329 result = EFalse; 00330 } 00331 catch( ... ) 00332 { 00333 //catch error from other libraries 00334 __LOGSTR_TOFILE("[-] Generic exception"); 00335 User::Leave( KErrGeneral ); 00336 } 00337 __LOGSTR_TOFILE("Exit CHsSMWidgetExample::IsWidgetRegisteredL"); 00338 00339 return result; 00340 } 00341 00342 // ----------------------------------------------------------------------------- 00343 // CHsSMWidgetExample::handleEvent 00344 // A template is published when the received event is EActivate or EResume 00345 // ----------------------------------------------------------------------------- 00346 // 00347 void CHsSMWidgetExample::handleEvent( std::string /*aWidgetName*/, 00348 Hs::IHsDataObserver::EEvent aEvent ) 00349 { 00350 __LOGSTR_TOFILE("Enter CHsSMWidgetExample::handleEvent"); 00351 00352 // this method should not leave 00353 // TRAP all leaving methods 00354 00355 switch(aEvent) 00356 { 00357 case EResume: 00358 { 00359 // homescreen is back in the foreground,so you can show new stuff 00360 iCanPublish = ETrue; 00361 // force a refresh if needed 00362 // e.g. if the active theme has been changed or signal update 00363 // happend during suspend mode 00364 if(iNeedUpdate) 00365 { 00366 TRAP_IGNORE(ChangeWidgetValuesL()); 00367 TRAP_IGNORE(PublishWidgetL()); 00368 iNeedUpdate = EFalse; 00369 } 00370 }; 00371 break; 00372 case EActivate: 00373 { 00374 // widget is added to the homescreen 00375 // make sure it has content 00376 TRAP_IGNORE(SignalLevelChangedL(-1,-1)); 00377 }; 00378 case EDeactivate: 00379 case ESuspend: 00380 default: 00381 iCanPublish = EFalse; 00382 } 00383 00384 00385 __LOGSTR_TOFILE("Exit CHsSMWidgetExample::handleEvent"); 00386 } 00387 00388 // ----------------------------------------------------------------------------- 00389 // CHsSMWidgetExample::handleItemEvent 00390 // Run an animation test when the user taps the signal monitor widget 00391 // ----------------------------------------------------------------------------- 00392 // 00393 void CHsSMWidgetExample::handleItemEvent( std::string /*aWidgetName*/, 00394 std::string /*aTemplateItemName*/, 00395 Hs::IHsDataObserver::EItemEvent /*aEvent*/) 00396 { 00397 __LOGSTR_TOFILE("Enter CHsSMWidgetExample::handleItemEvent"); 00398 00399 // this method should not leave 00400 // TRAP all leaving methods 00401 if(iCanPublish) 00402 TRAP_IGNORE(RunAnimationTestL()); 00403 00404 __LOGSTR_TOFILE("Exit CHsSMWidgetExample::handleItemEvent"); 00405 } 00406 00407 // ----------------------------------------------------------------------------- 00408 // CHsSMWidgetExample::ToStringL 00409 // Converts string from UNICODE to UTF-8 std::string. 00410 // Add one extra character for the ending zero. 00411 // ----------------------------------------------------------------------------- 00412 // 00413 std::string CHsSMWidgetExample::ToStringL(const TDesC& aText) 00414 { 00415 HBufC8* text = HBufC8::NewLC( aText.Length() + 4 /*for ending zero*/ ); 00416 TPtr8 dest( text->Des() ); 00417 CnvUtfConverter::ConvertFromUnicodeToUtf8( dest, aText ); 00418 std::string ret((const char*)dest.PtrZ()); 00419 CleanupStack::PopAndDestroy(text); // delete text 00420 return ret; 00421 } 00422 00423 // ----------------------------------------------------------------------------- 00424 // CHsSMWidgetExample::SignalLevelChangedL 00425 // Takes in the values measured by the network signal monitor 00426 // (CGMSSignalMonitor) and uses them to draw a graph on a bitmap. This bitmap is 00427 // then passed to the home screen widget to be displayed. 00428 // @param aSignal The current network signal value, see 00429 // TSignalStrengthV1::iSignalStrength 00430 // @param aBars The current network signal value, represented as the number of 00431 // bars in the signal indicator, see TSignalStrengthV1::iBar. A negative value 00432 // is intended to force the rendering of the data rather than updating the 00433 // measured values. 00434 // ----------------------------------------------------------------------------- 00435 // 00436 void CHsSMWidgetExample::SignalLevelChangedL(TInt /* aSignal */, TInt aBars) 00437 { 00438 __LOGSTR_TOFILE1("Enter CHsSMWidgetExample::SignalLevelChangedL with aBars = %d", aBars); 00439 00440 TInt i=0; 00441 00442 // handle only real values received from the signal monitor 00443 // negative aBars are passed to this method only when a repaint of the 00444 // old data is desired. 00445 if(aBars >= 0) 00446 { 00447 // advance list of measurements 00448 for ( i=0; i<KNumberOfMeasurements-1; i++) 00449 iMeasurements[i]=iMeasurements[i+1]; 00450 // add current value at the end 00451 iMeasurements[KNumberOfMeasurements-1] = aBars; 00452 } 00453 00454 // there's no point publishing this data if the home screen is not in the 00455 // foreground or if the widget is not selected on the home screen 00456 // unless aBars < 0 which means a forced update is intended 00457 if(iCanPublish || aBars<0) 00458 { 00459 __LOGSTR_TOFILE(" *** drawing"); 00460 00461 // get the current template 00462 MAknsSkinInstance* skin = AknsUtils::SkinInstance(); 00463 00464 // widget position unknown, using arbitrary rectangle 00465 // works OK only with plain theme backgrounds 00466 TRect rect(0,0,312,82); 00467 00468 // fetch background from the current theme 00469 CAknsBasicBackgroundControlContext* bgContext; 00470 bgContext = CAknsBasicBackgroundControlContext::NewL( 00471 KAknsIIDQsnBgAreaMainIdle,rect,ETrue); 00472 AknsDrawUtils::Background( skin, bgContext, 00473 (CWindowGc&)*iWideFrameContext, rect); 00474 delete bgContext; 00475 00476 TRgb textColor; 00477 // fetch the text color from the current scheme 00478 AknsUtils::GetCachedColor( skin, textColor, 00479 KAknsIIDQsnTextColors, EAknsCIQsnTextColorsCG20 ); 00480 00481 iWideFrameContext->SetPenStyle(CGraphicsContext::ESolidPen); 00482 iWideFrameContext->SetPenColor(textColor); 00483 iWideFrameContext->SetBrushStyle(CGraphicsContext::ESolidBrush); 00484 iWideFrameContext->SetBrushColor(textColor); 00485 00486 // draw the signal bars on the bitmap 00487 for (i=0; i<KNumberOfMeasurements; i++) 00488 { 00489 for(TInt j=0; j<iMeasurements[i]; j++) 00490 { 00491 // magic numbers here :) 00492 TRect rect(8+13*i,63-9*j,18+13*i,70-9*j); 00493 iWideFrameContext->DrawRect(rect); 00494 } 00495 } 00496 00497 // set this bitmap to the template item 00498 ChangeWidgetValuesL(); 00499 // commit the bitmap to the home screen 00500 PublishWidgetL(); 00501 00502 // publishing was done 00503 iNeedUpdate = EFalse; 00504 } 00505 else 00506 // can't publish the changed data now, remember to update 00507 // as soon as possible 00508 iNeedUpdate = ETrue; 00509 00510 __LOGSTR_TOFILE("Exit CHsSMWidgetExample::SignalLevelChangedL"); 00511 } 00512 00513 // ----------------------------------------------------------------------------- 00514 // CHsSMWidgetExample::RunAnimationTestL 00515 // This method is intended to give a hint with regards to the speed with which 00516 // new images can be published on the widget in order to create for example a 00517 // marquee animation. 00518 // Please note that this method will keep application's thread busy and while 00519 // in this loop the application cannot receive other events from the home screen 00520 // If indeed you intend to implement such blocking feature you should break it 00521 // into smaller tasks that can be execured under an active object's controll 00522 // leaving the thread a chance to receive callbacks 00523 // ----------------------------------------------------------------------------- 00524 // 00525 void CHsSMWidgetExample::RunAnimationTestL() 00526 { 00527 __LOGSTR_TOFILE("Enter CHsSMWidgetExample::RunAnimationTestL"); 00528 for(TInt i=0; i<100; i++) 00529 { 00530 SignalLevelChangedL(0,i%8); 00531 } 00532 __LOGSTR_TOFILE("Exit CHsSMWidgetExample::RunAnimationTestL"); 00533 }
|
© Nokia 2009 |