qtinternetradio/irqsystemeventhandler/src/iraccessoryobserver.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Thu, 27 May 2010 12:46:34 +0300
changeset 5 0930554dc389
permissions -rw-r--r--
Revision: 201019 Kit: 2010121

/*
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*
*/

#include <AccPolGenericID.h>

#include "irsystemeventobserverinterface.h"
#include "iraccessoryobserver.h" 
#include "irqlogger.h"

CIRAccessoryObserver::CIRAccessoryObserver(MIRHeadsetObserverInterface *aObserver) :
    CActive(CActive::EPriorityStandard),iObserver(aObserver)
{

}

void CIRAccessoryObserver::ConstructL()
{
    User::LeaveIfError(iAccessoryServer.Connect());

    User::LeaveIfError(iAccessoryModeSession.CreateSubSession(iAccessoryServer));
    User::LeaveIfError(iAccessoryModeSession.GetAccessoryMode(
            iAccPolAccessoryMode));

    CActiveScheduler::Add(this);

    iAccessoryModeSession.NotifyAccessoryModeChanged(iStatus, iAccPolAccessoryMode);
    SetActive();
}

CIRAccessoryObserver* CIRAccessoryObserver::NewL(MIRHeadsetObserverInterface *aObserver)
{
    CIRAccessoryObserver* self = new (ELeave) CIRAccessoryObserver(aObserver);
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop(self);

    return self;
}

CIRAccessoryObserver::~CIRAccessoryObserver()
{
    Cancel();
    iAccessoryModeSession.CloseSubSession();
    iAccessoryServer.Close();
}

TBool CIRAccessoryObserver::IsHeadsetConnected() const
{     
    TBool accessoryConnected;

    switch (iAccPolAccessoryMode.iAccessoryMode)
    {
    case EAccModeWiredHeadset:  
    case EAccModeLoopset:  
    case EAccModeHeadphones:
    {
        accessoryConnected = ETrue;
        break;
    }
    default:
    {
        accessoryConnected = EFalse;
        break;
    }
    }

    return accessoryConnected;
} 

void CIRAccessoryObserver::RunL()
{

    TRequestStatus status = iStatus;
    iAccessoryModeSession.NotifyAccessoryModeChanged(iStatus,
            iAccPolAccessoryMode);
    SetActive();

    if (status == KErrNone)
    {
        // Accessory mode may change when combined connection status changes 
        // or when audio routing status changes. 
        if (IsHeadsetConnected())
        {
            iObserver->headsetIsConnected();
        }
        else
        {
            iObserver->headsetIsDisconnected();
        }
    }
}

void CIRAccessoryObserver::DoCancel()
{
    iAccessoryModeSession.CancelNotifyAccessoryModeChanged();
}