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