examples/ForumNokia/CameraExample/src/CameraWrapperExampleAppUi.cpp

00001 /*
00002  * Copyright (c) 2009 Nokia Corporation.
00003  */
00004 
00005 // INCLUDE FILES
00006 #include <avkon.hrh>
00007 #include <eikon.hrh>
00008 #include <aknmessagequerydialog.h>
00009 #include <aknnotewrappers.h>
00010 #include <eikenv.h>
00011 
00012 #include <CameraExample_0xEF24C10A.rsg>
00013 #include "CameraWrapperExample.hrh"
00014 #include "CameraWrapperExample.pan"
00015 #include "CameraWrapperExampleApplication.h"
00016 #include "CameraWrapperExampleAppUi.h"
00017 #include "CameraWrapperExampleAppView.h"
00018 
00019 
00020 // ============================ MEMBER FUNCTIONS ===============================
00021 
00022 void CCameraWrapperExampleAppUi::ConstructL ()
00023     {
00024     // Initialise app UI with standard value.
00025     BaseConstructL (CAknAppUi::EAknEnableSkin );
00026 
00027     // Start receiving camera shutter key events
00028     #ifdef ENABLE_CAMERA_SHUTTER
00029     CaptureCameraShutter(ETrue);
00030     #endif
00031     
00032     // Make this class observe changes in foreground events
00033     iEikonEnv->AddForegroundObserverL(*this);
00034     
00035     // Create view
00036     iAppView = CCameraWrapperExampleAppView::NewL (ClientRect () );
00037     }
00038 
00039 CCameraWrapperExampleAppUi::CCameraWrapperExampleAppUi ()
00040     {
00041     }
00042 
00043 CCameraWrapperExampleAppUi::~CCameraWrapperExampleAppUi ()
00044     {
00045     delete iAppView;
00046     
00047     #ifdef ENABLE_CAMERA_SHUTTER
00048     CaptureCameraShutter(EFalse);
00049     #endif
00050 
00051     iShutterKeyHandles.Close();
00052     }
00053 
00054 #ifdef ENABLE_CAMERA_SHUTTER
00055 
00056 void CCameraWrapperExampleAppUi::CaptureCameraShutter(TBool aEnable)
00057     {
00058     // Try to capture events from the camera shutter key(s)
00059     // http://wiki.forum.nokia.com/index.php/KIS000563_-_Camera_shutter_key_(EKeyCamera_events)_cannot_be_used_in_3rd_party_applications
00060     if (aEnable && !iCameraKeyCaptured)
00061         {
00062         iCameraKeyCaptured = ETrue;
00063         // Enable capturing
00064         RProcess proc;
00065         iShutterKeyHandles.Reset();
00066         if(proc.HasCapability(ECapabilitySwEvent))
00067             {
00068             for(TInt i=0; KCameraShutterKeyEventCodes[i] != 0; i++)
00069                 {
00070                 TInt32 handle = iEikonEnv->RootWin().CaptureKey( KCameraShutterKeyEventCodes[i], 0, 0 );
00071                 if(handle >= 0)
00072                     {
00073                     iShutterKeyHandles.Append(handle);
00074                     }
00075                 }
00076             }
00077         }
00078     else if(!aEnable && iCameraKeyCaptured)
00079         {
00080         iCameraKeyCaptured = EFalse;
00081         // Disable capturing
00082         // Release the captured camera shutter key(s)
00083         for(TInt i=0; i < iShutterKeyHandles.Count(); i++)
00084             {
00085             iEikonEnv->RootWin().CancelCaptureKey( iShutterKeyHandles[i] );
00086             }
00087         }
00088     }
00089 #endif
00090 
00091 
00092 void CCameraWrapperExampleAppUi::HandleGainingForeground()
00093     {
00094     // Application gets focused so reserve the camera
00095     // http://wiki.forum.nokia.com/index.php/CS000821_-_Handling_Camera_resource
00096     if ( iAppView && 
00097          iAppView->CameraEngine() &&
00098          iAppView->CameraEngine()->State() != CCameraEngine::EEngineNotReady )
00099         {
00100         iAppView->CameraEngine()->ReserveAndPowerOn();
00101         
00102 #ifdef ENABLE_CAMERA_SHUTTER
00103         CaptureCameraShutter(ETrue);
00104 #endif
00105         }
00106     }
00107 
00108 void CCameraWrapperExampleAppUi::HandleLosingForeground()
00109     {
00110     // Application loses focus so release the camera
00111     // http://wiki.forum.nokia.com/index.php/CS000821_-_Handling_Camera_resource
00112     if ( iAppView && 
00113          iAppView->CameraEngine() &&
00114          iAppView->CameraEngine()->State() != CCameraEngine::EEngineNotReady )
00115         {
00116         iAppView->CameraEngine()->ReleaseAndPowerOff();
00117         
00118 #ifdef ENABLE_CAMERA_SHUTTER
00119         CaptureCameraShutter(EFalse);
00120 #endif
00121         }
00122     }
00123 
00124 void CCameraWrapperExampleAppUi::UseOptionsExitCbaL()
00125     {
00126     CEikButtonGroupContainer* cba = Cba();
00127     if (cba)
00128         {
00129         cba->SetCommandSetL(R_AVKON_SOFTKEYS_OPTIONS_EXIT);
00130         cba->DrawNow();
00131         }
00132     }
00133 
00134 void CCameraWrapperExampleAppUi::UseOptionsBackCbaL()
00135     {
00136     CEikButtonGroupContainer* cba = Cba();
00137     if (cba)
00138         {
00139         cba->SetCommandSetL(R_AVKON_SOFTKEYS_OPTIONS_BACK);
00140         cba->DrawNow();
00141         }
00142     }
00143 
00144 TBool CCameraWrapperExampleAppUi::IsBackCBA()
00145     {
00146     CEikButtonGroupContainer* cba = Cba();
00147     // NOTE: There should be EAknSoftkeyBack in the application because
00148     // we use R_AVKON_SOFTKEYS_SELECT_BACK, but it seems that there is EAknSoftkeyCancel
00149     CCoeControl* back = cba->ControlOrNull(EAknSoftkeyBack);
00150     CCoeControl* cancel = cba->ControlOrNull(EAknSoftkeyCancel);
00151     if (back || cancel)
00152         return ETrue;
00153     else
00154         return EFalse;
00155     }
00156 
00157 TKeyResponse CCameraWrapperExampleAppUi::HandleKeyEventL(
00158     const TKeyEvent& aKeyEvent,TEventCode aType)
00159     {
00160     // Capture picture with selection key
00161     switch ( aKeyEvent.iCode )
00162         {
00163         case EKeyOK:
00164         case EStdKeyDevice3:
00165         case EKeyUpArrow:
00166         case EKeyDownArrow:  
00167             {
00168             // Capture picture
00169             return iAppView->OfferKeyEventL(aKeyEvent,aType);
00170             }
00171         default:
00172             {
00173             break;
00174             }
00175         };
00176 
00177     // Camera shutter events handling
00178     #ifdef ENABLE_CAMERA_SHUTTER
00179     // Camera shutter autofocus
00180     switch ( aKeyEvent.iScanCode )
00181         {
00182         case KStdKeyCameraFocus:
00183         case KStdKeyCameraFocus2:
00184             {
00185             // Camera shutter autofocus
00186             return iAppView->OfferKeyEventL(aKeyEvent,aType);
00187             }
00188         default:
00189             {
00190             break;
00191             }
00192         };
00193     // Camera shutter key
00194     for(TInt i=0; KCameraShutterKeyEventCodes[i] != 0; i++)
00195           {
00196           if( KCameraShutterKeyEventCodes[i] == aKeyEvent.iCode )
00197             {
00198             // Capture image
00199             iAppView->Capture();
00200             return EKeyWasConsumed;
00201             }
00202           }
00203     #endif
00204 
00205     return EKeyWasNotConsumed;
00206     }
00207 
00208 void CCameraWrapperExampleAppUi::HandleCommandL (TInt aCommand )
00209     {
00210     switch (aCommand )
00211         {
00212         case EEikCmdExit:
00213         case EAknSoftkeyExit:
00214             {
00215             Exit();
00216             break;
00217             }
00218         case EAknSoftkeyBack:
00219             {
00220             iAppView->CancelCapturedPicture();
00221             UseOptionsExitCbaL();
00222             break;
00223             }
00224         case EAbout:
00225             {
00226             CAknMessageQueryDialog* dlg = new (ELeave) CAknMessageQueryDialog ();
00227             dlg->PrepareLC (R_ABOUT_QUERY_DIALOG );
00228             HBufC* title = iEikonEnv->AllocReadResourceLC (R_ABOUT_DIALOG_TITLE );
00229             dlg->QueryHeading ()->SetTextL (*title );
00230             CleanupStack::PopAndDestroy (); //title
00231             HBufC* msg = iEikonEnv->AllocReadResourceLC (R_ABOUT_DIALOG_TEXT );
00232             dlg->SetMessageTextL (*msg );
00233             CleanupStack::PopAndDestroy (); //msg
00234             dlg->RunLD();
00235             break;
00236             }
00237         default:
00238             {
00239             break;
00240             }
00241         };
00242     }
00243 
00244 void CCameraWrapperExampleAppUi::HandleResourceChangeL(TInt aType)
00245     {
00246     CAknAppUi::HandleResourceChangeL( aType );
00247        
00248     if ( aType==KEikDynamicLayoutVariantSwitch )
00249         {
00250         if (iAppView)
00251             {
00252             TRect rect;
00253             AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane,rect);
00254             iAppView->SetRect(rect);
00255             }
00256         }   
00257     
00258     }
00259 
00260 // End of File

Generated by  doxygen 1.6.2