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