examples/ForumNokia/CameraExample/src/CameraWrapperExampleAppUi.cpp

00001 /*
00002  * Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003  *    
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *    
00007  *  * Redistributions of source code must retain the above copyright notice, this
00008  *    list of conditions and the following disclaimer.
00009  *  * Redistributions in binary form must reproduce the above copyright notice,
00010  *    this list of conditions and the following disclaimer in the documentation
00011  *    and/or other materials provided with the distribution.
00012  *  * Neither the name of Nokia Corporation nor the names of its contributors
00013  *    may be used to endorse or promote products derived from this software
00014  *    without specific prior written permission.
00015  *    
00016  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  *    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020  *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021  *    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022  *    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023  *    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024  *    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *    
00027  *    Description:  
00028  */
00029 
00030 // INCLUDE FILES
00031 #include <avkon.hrh>
00032 #include <eikon.hrh>
00033 #include <aknmessagequerydialog.h>
00034 #include <aknnotewrappers.h>
00035 #include <eikenv.h>
00036 
00037 #include <CameraExample_0xEF24C10A.rsg>
00038 #include "CameraWrapperExample.hrh"
00039 #include "CameraWrapperExample.pan"
00040 #include "CameraWrapperExampleApplication.h"
00041 #include "CameraWrapperExampleAppUi.h"
00042 #include "CameraWrapperExampleAppView.h"
00043 
00044 
00045 // ============================ MEMBER FUNCTIONS ===============================
00046 
00047 void CCameraWrapperExampleAppUi::ConstructL ()
00048     {
00049     // Initialise app UI with standard value.
00050     BaseConstructL (CAknAppUi::EAknEnableSkin );
00051 
00052     // Start receiving camera shutter key events
00053     #ifdef ENABLE_CAMERA_SHUTTER
00054     CaptureCameraShutter(ETrue);
00055     #endif
00056     
00057     // Make this class observe changes in foreground events
00058     iEikonEnv->AddForegroundObserverL(*this);
00059     
00060     // Create view
00061     iAppView = CCameraWrapperExampleAppView::NewL (ClientRect () );
00062     }
00063 
00064 CCameraWrapperExampleAppUi::CCameraWrapperExampleAppUi ()
00065     {
00066     }
00067 
00068 CCameraWrapperExampleAppUi::~CCameraWrapperExampleAppUi ()
00069     {
00070     delete iAppView;
00071     
00072     #ifdef ENABLE_CAMERA_SHUTTER
00073     CaptureCameraShutter(EFalse);
00074     #endif
00075 
00076     iShutterKeyHandles.Close();
00077     }
00078 
00079 #ifdef ENABLE_CAMERA_SHUTTER
00080 
00081 void CCameraWrapperExampleAppUi::CaptureCameraShutter(TBool aEnable)
00082     {
00083     // Try to capture events from the camera shutter key(s)
00084     // http://wiki.forum.nokia.com/index.php/KIS000563_-_Camera_shutter_key_(EKeyCamera_events)_cannot_be_used_in_3rd_party_applications
00085     if (aEnable && !iCameraKeyCaptured)
00086         {
00087         iCameraKeyCaptured = ETrue;
00088         // Enable capturing
00089         RProcess proc;
00090         iShutterKeyHandles.Reset();
00091         if(proc.HasCapability(ECapabilitySwEvent))
00092             {
00093             for(TInt i=0; KCameraShutterKeyEventCodes[i] != 0; i++)
00094                 {
00095                 TInt32 handle = iEikonEnv->RootWin().CaptureKey( KCameraShutterKeyEventCodes[i], 0, 0 );
00096                 if(handle >= 0)
00097                     {
00098                     iShutterKeyHandles.Append(handle);
00099                     }
00100                 }
00101             }
00102         }
00103     else if(!aEnable && iCameraKeyCaptured)
00104         {
00105         iCameraKeyCaptured = EFalse;
00106         // Disable capturing
00107         // Release the captured camera shutter key(s)
00108         for(TInt i=0; i < iShutterKeyHandles.Count(); i++)
00109             {
00110             iEikonEnv->RootWin().CancelCaptureKey( iShutterKeyHandles[i] );
00111             }
00112         }
00113     }
00114 #endif
00115 
00116 
00117 void CCameraWrapperExampleAppUi::HandleGainingForeground()
00118     {
00119     // Application gets focused so reserve the camera
00120     // http://wiki.forum.nokia.com/index.php/CS000821_-_Handling_Camera_resource
00121     if ( iAppView && 
00122          iAppView->CameraEngine() &&
00123          iAppView->CameraEngine()->State() != CCameraEngine::EEngineNotReady )
00124         {
00125         iAppView->CameraEngine()->ReserveAndPowerOn();
00126         
00127 #ifdef ENABLE_CAMERA_SHUTTER
00128         CaptureCameraShutter(ETrue);
00129 #endif
00130         }
00131     }
00132 
00133 void CCameraWrapperExampleAppUi::HandleLosingForeground()
00134     {
00135     // Application loses focus so release the camera
00136     // http://wiki.forum.nokia.com/index.php/CS000821_-_Handling_Camera_resource
00137     if ( iAppView && 
00138          iAppView->CameraEngine() &&
00139          iAppView->CameraEngine()->State() != CCameraEngine::EEngineNotReady )
00140         {
00141         iAppView->CameraEngine()->ReleaseAndPowerOff();
00142         
00143 #ifdef ENABLE_CAMERA_SHUTTER
00144         CaptureCameraShutter(EFalse);
00145 #endif
00146         }
00147     }
00148 
00149 void CCameraWrapperExampleAppUi::UseOptionsExitCbaL()
00150     {
00151     CEikButtonGroupContainer* cba = Cba();
00152     if (cba)
00153         {
00154         cba->SetCommandSetL(R_AVKON_SOFTKEYS_OPTIONS_EXIT);
00155         cba->DrawNow();
00156         }
00157     }
00158 
00159 void CCameraWrapperExampleAppUi::UseOptionsBackCbaL()
00160     {
00161     CEikButtonGroupContainer* cba = Cba();
00162     if (cba)
00163         {
00164         cba->SetCommandSetL(R_AVKON_SOFTKEYS_OPTIONS_BACK);
00165         cba->DrawNow();
00166         }
00167     }
00168 
00169 TBool CCameraWrapperExampleAppUi::IsBackCBA()
00170     {
00171     CEikButtonGroupContainer* cba = Cba();
00172     // NOTE: There should be EAknSoftkeyBack in the application because
00173     // we use R_AVKON_SOFTKEYS_SELECT_BACK, but it seems that there is EAknSoftkeyCancel
00174     CCoeControl* back = cba->ControlOrNull(EAknSoftkeyBack);
00175     CCoeControl* cancel = cba->ControlOrNull(EAknSoftkeyCancel);
00176     if (back || cancel)
00177         return ETrue;
00178     else
00179         return EFalse;
00180     }
00181 
00182 TKeyResponse CCameraWrapperExampleAppUi::HandleKeyEventL(
00183     const TKeyEvent& aKeyEvent,TEventCode aType)
00184     {
00185     // Capture picture with selection key
00186     switch ( aKeyEvent.iCode )
00187         {
00188         case EKeyOK:
00189         case EStdKeyDevice3:
00190         case EKeyUpArrow:
00191         case EKeyDownArrow:  
00192             {
00193             // Capture picture
00194             return iAppView->OfferKeyEventL(aKeyEvent,aType);
00195             }
00196         default:
00197             {
00198             break;
00199             }
00200         };
00201 
00202     // Camera shutter events handling
00203     #ifdef ENABLE_CAMERA_SHUTTER
00204     // Camera shutter autofocus
00205     switch ( aKeyEvent.iScanCode )
00206         {
00207         case KStdKeyCameraFocus:
00208         case KStdKeyCameraFocus2:
00209             {
00210             // Camera shutter autofocus
00211             return iAppView->OfferKeyEventL(aKeyEvent,aType);
00212             }
00213         default:
00214             {
00215             break;
00216             }
00217         };
00218     // Camera shutter key
00219     for(TInt i=0; KCameraShutterKeyEventCodes[i] != 0; i++)
00220           {
00221           if( KCameraShutterKeyEventCodes[i] == aKeyEvent.iCode )
00222             {
00223             // Capture image
00224             iAppView->Capture();
00225             return EKeyWasConsumed;
00226             }
00227           }
00228     #endif
00229 
00230     return EKeyWasNotConsumed;
00231     }
00232 
00233 void CCameraWrapperExampleAppUi::HandleCommandL (TInt aCommand )
00234     {
00235     switch (aCommand )
00236         {
00237         case EEikCmdExit:
00238         case EAknSoftkeyExit:
00239             {
00240             Exit();
00241             break;
00242             }
00243         case EAknSoftkeyBack:
00244             {
00245             iAppView->CancelCapturedPicture();
00246             UseOptionsExitCbaL();
00247             break;
00248             }
00249         case EAbout:
00250             {
00251             CAknMessageQueryDialog* dlg = new (ELeave) CAknMessageQueryDialog ();
00252             dlg->PrepareLC (R_ABOUT_QUERY_DIALOG );
00253             HBufC* title = iEikonEnv->AllocReadResourceLC (R_ABOUT_DIALOG_TITLE );
00254             dlg->QueryHeading ()->SetTextL (*title );
00255             CleanupStack::PopAndDestroy (); //title
00256             HBufC* msg = iEikonEnv->AllocReadResourceLC (R_ABOUT_DIALOG_TEXT );
00257             dlg->SetMessageTextL (*msg );
00258             CleanupStack::PopAndDestroy (); //msg
00259             dlg->RunLD();
00260             break;
00261             }
00262         default:
00263             {
00264             break;
00265             }
00266         };
00267     }
00268 
00269 void CCameraWrapperExampleAppUi::HandleResourceChangeL(TInt aType)
00270     {
00271     CAknAppUi::HandleResourceChangeL( aType );
00272        
00273     if ( aType==KEikDynamicLayoutVariantSwitch )
00274         {
00275         if (iAppView)
00276             {
00277             TRect rect;
00278             AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane,rect);
00279             iAppView->SetRect(rect);
00280             }
00281         }   
00282     
00283     }
00284 
00285 // End of File

Generated by  doxygen 1.6.2