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