camerauis/cameraxui/cxui/src/cxuiapplicationstate.cpp
changeset 37 64817133cd1d
child 42 feebad15db8c
equal deleted inserted replaced
36:b12f3922a74f 37:64817133cd1d
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QMetaEnum>
       
    19 
       
    20 #include "cxutils.h"
       
    21 #include "cxuieventlog.h"
       
    22 #include "cxuierrormanager.h"
       
    23 #include "cxuiapplicationstate.h"
       
    24 
       
    25 namespace
       
    26 {
       
    27     static const char *EVENT_APPLICATION_STATE = "application state";
       
    28 }
       
    29 
       
    30 /*!
       
    31 * Constructor.
       
    32 */
       
    33 CxuiApplicationState::CxuiApplicationState(CxuiApplication &application,
       
    34                                            CxeSettings &settings,
       
    35                                            CxuiCaptureKeyHandler &keyHandler,
       
    36                                            CxuiDocumentLoader *documentLoader)
       
    37     : mState(Background), mApplicationMonitor(NULL), mErrorManager(NULL), mEventLog(NULL)
       
    38 {
       
    39     mApplicationMonitor = new CxuiApplicationFrameworkMonitor(application, settings);
       
    40     mErrorManager = new CxuiErrorManager(documentLoader);
       
    41 
       
    42     // Foreground state change signals
       
    43     connect(mApplicationMonitor, SIGNAL(foregroundStateChanged(CxuiApplicationFrameworkMonitor::ForegroundState)),
       
    44             this, SLOT(handleForegroundStateChanged(CxuiApplicationFrameworkMonitor::ForegroundState)));
       
    45 
       
    46     // Battery empty signal
       
    47     connect(mApplicationMonitor, SIGNAL(batteryEmpty()), this, SLOT(handleBatteryEmpty()));
       
    48 
       
    49     // USB mass memory mode signal
       
    50     connect(mApplicationMonitor, SIGNAL(usbMassMemoryModeToggled(bool)),
       
    51             this, SLOT(handleUsbMassMemoryModeChanged(bool)));
       
    52 
       
    53     // Severe error signals
       
    54     connect(mErrorManager, SIGNAL(errorPopupShown()), this, SLOT(handleSevereError()));
       
    55     connect(mErrorManager, SIGNAL(errorPopupClosed()), this, SLOT(handleErrorCleared()));
       
    56 
       
    57 #ifdef CX_DEBUG
       
    58     mEventLog = new CxuiEventLog("CxuiApplicationState");
       
    59 #endif
       
    60 }
       
    61 
       
    62 /*!
       
    63 * Destructor.
       
    64 */
       
    65 CxuiApplicationState::~CxuiApplicationState()
       
    66 {
       
    67     delete mErrorManager;
       
    68     delete mApplicationMonitor;
       
    69     delete mEventLog;
       
    70 }
       
    71 
       
    72 /*!
       
    73 * Get current application state.
       
    74 */
       
    75 CxuiApplicationState::State CxuiApplicationState::currentState() const
       
    76 {
       
    77     return mState;
       
    78 }
       
    79 
       
    80 /*!
       
    81 * Start monitoring the application state.
       
    82 * Initial state is checked and signal emitted about state change *unless* state is Background.
       
    83 */
       
    84 void CxuiApplicationState::startMonitoring()
       
    85 {
       
    86     CX_DEBUG_ENTER_FUNCTION();
       
    87     // Foreground handling checks for errors if needed.
       
    88     handleForegroundStateChanged(mApplicationMonitor->foregroundState());
       
    89     CX_DEBUG_EXIT_FUNCTION();
       
    90 }
       
    91 
       
    92 /*!
       
    93 * Handle error from UI or engine.
       
    94 * Error will be checked and if it is severe, application state will become Error and error note will be shown.
       
    95 * If error is not severe, a warning note will be shown.
       
    96 * @param error The error id.
       
    97 */
       
    98 void CxuiApplicationState::handleApplicationError(CxeError::Id error)
       
    99 {
       
   100     CX_DEBUG_ENTER_FUNCTION();
       
   101     if (error != CxeError::None) {
       
   102         mErrorManager->check(error);
       
   103         // If error manager sees this error as severe one, it will signal that back.
       
   104         // We will handle updating state in handleSevereError().
       
   105         // If only warning note (or nothing) is needed, application state is not changed.
       
   106     }
       
   107     CX_DEBUG_EXIT_FUNCTION();
       
   108 }
       
   109 
       
   110 /*!
       
   111 * Handle change in application foreground status.
       
   112 * @param state New foreground status.
       
   113 */
       
   114 void CxuiApplicationState::handleForegroundStateChanged(CxuiApplicationFrameworkMonitor::ForegroundState state)
       
   115 {
       
   116     CX_DEBUG_ENTER_FUNCTION();
       
   117     if (state == CxuiApplicationFrameworkMonitor::ForegroundFullyLost) {
       
   118         CX_DEBUG(("CxuiApplicationState - application is in background"));
       
   119         // Background overwrites even any error.  We clear any active errors.
       
   120         // When returning to background, error situation will be re-checked.
       
   121         setState(Background);
       
   122         mErrorManager->clear();
       
   123     } else {
       
   124         CX_DEBUG(("CxuiApplicationState - application is in partial / full foreground"));
       
   125         // Check that we were in background. Switching between partial and full background
       
   126         // needs no actions.
       
   127         if (currentState() == Background) {
       
   128            CX_DEBUG(("CxuiApplicationState - application was in background before, moving to foreground"));
       
   129             // Check that there's no active errors that have been ignored in background.
       
   130             checkErrors();
       
   131             if (currentState() != Error) {
       
   132                 setState(Normal);
       
   133             }
       
   134         }
       
   135     }
       
   136 
       
   137     CX_DEBUG_EXIT_FUNCTION();
       
   138 }
       
   139 
       
   140 /*!
       
   141 * Handle USB mass memory mode (USB MMM) activating or deactivating.
       
   142 * If USB MMM activates, we enter error state and display error note.
       
   143 * When USB MMM deactivates, we hide the note and move to standby mode.
       
   144 * @param active Is the USB mass memory mode active.
       
   145 */
       
   146 void CxuiApplicationState::handleUsbMassMemoryModeChanged(bool active)
       
   147 {
       
   148     CX_DEBUG_ENTER_FUNCTION();
       
   149     if (active) {
       
   150         // USB error is not handled if:
       
   151         // (a) other severe error already active
       
   152         // (b) application is in background
       
   153         if (currentState() == Normal || currentState() == Standby) {
       
   154             // Emulate memory not accessible error.
       
   155             handleApplicationError(CxeError::MemoryNotAccessible);
       
   156         }
       
   157     } else {
       
   158         // If we had USB error, clear it now.
       
   159         if (currentState() == Error) {
       
   160             setState(Standby);
       
   161             // Clear memory not accessible error.
       
   162             mErrorManager->clear();
       
   163         }
       
   164     }
       
   165     CX_DEBUG_EXIT_FUNCTION();
       
   166 }
       
   167 
       
   168 /*!
       
   169 * Handle battery emptying. We need to stop all activity and exit the application.
       
   170 */
       
   171 void CxuiApplicationState::handleBatteryEmpty()
       
   172 {
       
   173     CX_DEBUG_ENTER_FUNCTION();
       
   174     setState(Background);
       
   175     CX_DEBUG_EXIT_FUNCTION();
       
   176 }
       
   177 
       
   178 /*!
       
   179 * Handle a severe error after Error Manager has analyzed it.
       
   180 */
       
   181 void CxuiApplicationState::handleSevereError()
       
   182 {
       
   183     CX_DEBUG_ENTER_FUNCTION();
       
   184     // In background we do not change the state from Background to anything else.
       
   185     if (currentState() != Background) {
       
   186         setState(Error);
       
   187     }
       
   188     CX_DEBUG_EXIT_FUNCTION();
       
   189 }
       
   190 
       
   191 /*!
       
   192 * Severe error has been cleared.
       
   193 * Check if we should return to normal or background state.
       
   194 */
       
   195 void CxuiApplicationState::handleErrorCleared()
       
   196 {
       
   197     CX_DEBUG_ENTER_FUNCTION();
       
   198     // No state change if we are not currently in Error state.
       
   199     if (currentState() == Error) {
       
   200         setState(Normal);
       
   201     }
       
   202     CX_DEBUG_EXIT_FUNCTION();
       
   203 }
       
   204 
       
   205 /*!
       
   206 * Slot for requesting Standby state to be entered.
       
   207 * Request is accepted only if current state is Normal. Otherwise call has no effect.
       
   208 */
       
   209 void CxuiApplicationState::enterStandby()
       
   210 {
       
   211     CX_DEBUG_ENTER_FUNCTION();
       
   212     if (currentState() == Normal) {
       
   213         setState(Standby);
       
   214     }
       
   215     CX_DEBUG_EXIT_FUNCTION();
       
   216 }
       
   217 
       
   218 /*!
       
   219 * Slot for requesting state change from Standby to Normal state.
       
   220 * Request is accepted only if current state is Standby. Otherwise call has no effect.
       
   221 */
       
   222 void CxuiApplicationState::exitStandby()
       
   223 {
       
   224     CX_DEBUG_ENTER_FUNCTION();
       
   225     if (currentState() == Standby) {
       
   226         setState(Normal);
       
   227     }
       
   228     CX_DEBUG_EXIT_FUNCTION();
       
   229 }
       
   230 
       
   231 /*!
       
   232 * Set new state.
       
   233 * If state is actually changing, stateChanged() signal is emitted.
       
   234 */
       
   235 void CxuiApplicationState::setState(State newState)
       
   236 {
       
   237     if (mState != newState) {
       
   238 #ifdef CX_DEBUG
       
   239         if (mEventLog) {
       
   240             mEventLog->append(EVENT_APPLICATION_STATE,
       
   241                               CxuiApplicationState::staticMetaObject.enumerator(
       
   242                                   CxuiApplicationState::staticMetaObject.
       
   243                                         indexOfEnumerator("State")).valueToKey(newState));
       
   244             mEventLog->print();
       
   245         }
       
   246 #endif // CX_DEBUG
       
   247 
       
   248         State oldState = mState;
       
   249         mState = newState;
       
   250         emit stateChanged(newState, oldState);
       
   251     }
       
   252 }
       
   253 
       
   254 /*!
       
   255 * Check if we have known errors active at the moment.
       
   256 * State is set to CxuiApplicationState::Error, if error is found.
       
   257 */
       
   258 void CxuiApplicationState::checkErrors()
       
   259 {
       
   260     CX_DEBUG_ENTER_FUNCTION();
       
   261     if (mApplicationMonitor->isUsbMassMemoryModeActive()) {
       
   262         // Force Error state even if Background is still the current state.
       
   263         // We use this method to check errors also when returning from background.
       
   264         // Normally in Background state we do not enter Error state.
       
   265         setState(Error);
       
   266         handleApplicationError(CxeError::MemoryNotAccessible);
       
   267     }
       
   268     CX_DEBUG_EXIT_FUNCTION();
       
   269 }
       
   270 
       
   271 
       
   272 // end of file