ansphoneengine.cpp

来自「series60 应用程序开发的源代码 series60 应用程序开发的源代码」· C++ 代码 · 共 551 行 · 第 1/2 页

CPP
551
字号
/**
*
* @brief Definition of CAnsPhoneEngine
*
* Copyright (c) EMCC Software Ltd 2003
* @version 1.0
*/

#include "AnsPhoneEngine.h"

#include <bautils.h>    // BaflUtils
#include <aknutils.h>    // BaflUtils

_LIT(KYourMessageFileName, "\\system\\apps\\AnsPhone\\yourmes.wav");
_LIT(KTheirMessageFileName, "\\system\\apps\\AnsPhone\\theirmes.wav");
_LIT(KMessagesDir, "c:\\system\\apps\\AnsPhone\\messages\\");

_LIT(KTSY, "phonetsy");
_LIT(KMessageType, ".wav");


const TInt KSpace = 32;

const TInt KMaxMessages = 5;
const TInt KDelay = 10000;
const TInt KHalfSecond = 500000;

const TInt KGranMessages = 5;


CAnsPhoneEngine::CAnsPhoneEngine(MAnsPhoneEngineObserver& aObserver)
:iObserver(aObserver),
 iState(ENoState),
 iIsLocal(EFalse)
{
}

CAnsPhoneEngine::~CAnsPhoneEngine()
{
    if (iTimer)
    {
        iTimer->Cancel();
        delete iTimer;
    }

    SoundCleanup();

    delete iMessageList;

    TelephonyCleanup();

    iFs.Close();
}

CAnsPhoneEngine* CAnsPhoneEngine::NewL(MAnsPhoneEngineObserver& aObserver)
{
    CAnsPhoneEngine* self = new (ELeave) CAnsPhoneEngine(aObserver);
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop(self);
    return self;
}

void CAnsPhoneEngine::ConstructL()
{
    User::LeaveIfError(iFs.Connect());

    // create the messages directory if it does not exist
    if (!BaflUtils::PathExists(iFs, KMessagesDir))
        User::LeaveIfError(iFs.MkDirAll(KMessagesDir));

    iMessageList = new (ELeave) CArrayFixSeg<TMessage>(KGranMessages);

    // load the messages from the messages directory
    CDir* dir = NULL;
    User::LeaveIfError(iFs.GetDir(KMessagesDir, KEntryAttNormal, ESortByDate|EDescending, dir));
    CleanupStack::PushL(dir);

    TInt numberFiles = dir->Count();
    for(TInt a = 0; a < numberFiles; a++)
    {
        const TEntry& file = (*dir)[a];
        TMessage message;
        message.iNumber = file.iName;
        message.iTime    = file.iModified;
        iMessageList->AppendL(message);
    }

    CleanupStack::PopAndDestroy(dir);
}


void CAnsPhoneEngine::MoscoStateChangeEventL(CBase* /*aObject*/, TInt /*aPreviousState*/, TInt aCurrentState, TInt aErrorCode)
{
    // if the recording has died, this means that the recording should be finished;
    // should only happen when recording on the telephony line
    if(iState == ERecord && !iIsLocal && aErrorCode == KErrDied)
    {
        Stop();
        return;
    }

    User::LeaveIfError(aErrorCode);

    if(aCurrentState != CMdaAudioClipUtility::EOpen)
        return;

    switch(iState)
    {
        case ERecordInit:
            // Record from the telephony line and set to max gain
            if(iIsLocal)
            {
                iSound->SetAudioDeviceMode(CMdaAudioRecorderUtility::ELocal);
                iSound->SetGain(iSound->MaxGain());
            }
            else
            {
                iSound->SetAudioDeviceMode(CMdaAudioRecorderUtility::ETelephonyNonMixed);
                TInt maxGain = iSound->MaxGain();
                iSound->SetGain(maxGain / 2);
            }

            // Delete current audio sample from beginning of file
            iSound->SetPosition(TTimeIntervalMicroSeconds(0));
            iSound->CropL();

            // start recording
            iSound->RecordL();
            iState = ERecord;
            break;

        case ERecord:
            break;

        case EPlayInit:
        {
            // Play through the device speaker and set to max volume
            if(iIsLocal)
            {
                iSound->SetAudioDeviceMode(CMdaAudioRecorderUtility::ELocal);
            }
            else
            {
                iSound->SetAudioDeviceMode(CMdaAudioRecorderUtility::ETelephonyOrLocal);
//                iPhone.DeviceSpeakerOff();
            }
            iSound->SetVolume(iSound->MaxVolume());

            // Set the playback position to the start of the file
            iSound->SetPosition(TTimeIntervalMicroSeconds(0));
            iSound->PlayL();
            iState = EPlay;

            // we start the timer
            // add a half a second onto the play time to make sure we've finished playing
            TInt64 playTime = iSound->Duration().Int64() + KHalfSecond;
            if (iTimer)
            {
                delete iTimer;
                iTimer = NULL;
            }
            iTimer = CAnsPhoneTimer::NewL(*this, playTime.GetTInt());
            break;
        }
        default:
            break;
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////
// MMdaObjectStateChangeObserver
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

void CAnsPhoneEngine::MoscoStateChangeEvent(CBase* aObject, TInt aPreviousState, TInt aCurrentState, TInt aErrorCode)
// this function is called by the CMdaAudioRecorderUtility object
// it handles starting playing/recording and stopping the recording when the call has ended
//
//    Note:
//    When the CMdaAudioRecorderUtility plays/records a message, the function SetAudioDeviceMode()
//    sets how the device mixes the microphone and telephony downlink
//    CMdaAudioRecorderUtility::ETelephonyNonMixed should there should be no mixing between
//    device speaker/microphone and the telephony downlink. However, this does not work and so
//    when playing/recording during a call, the sound microphone is mixed with the telephony downlink
//    One way to prevent this for recording is to set the volume to zero; this means the user cannot
//    hear what the other person is recording. This cannot be done when playing because the downlink
//    will have a volume of zero as well.
{
    TRAPD (err, MoscoStateChangeEventL(aObject, aPreviousState, aCurrentState, aErrorCode))
    if (err)
    {
        Stop();
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////
// MAnsPhoneTimerObserver
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

void CAnsPhoneEngine::TimerCompleteL()
// play has finished, so either alert the observer or start recording
{
    // check for having cancelled the playing/recording before they have finished
    // this will happen if the person hangs up before the playing has finished
    if(iState == ENoState)
        return;

    delete iTimer;
    iTimer = NULL;

    iState = ENoState;
    SoundCleanup();

    if(iIsLocal)
    {
        iObserver.HandlePlayMessageOverL();
    }
    else
    {
        TMessage message;
        TTime time;
        time.HomeTime();
        message.iTime = time;
        iMessageList->InsertL(0, message);

        RecordMessageL(EFalse);
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////
// MAnsPhoneCallWatcherObserver
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

void CAnsPhoneEngine::HandleCallInChangeL(const RCall::TStatus& aStatus)
{
    switch(aStatus)
    {
        case RCall::EStatusConnected:
        {
            iObserver.HandleCallChangeL(aStatus);
            PlayMessageL(EFalse, 0, ETrue);
            break;
        }
        case RCall::EStatusHangingUp:
            // if we have started recording, then tell the observer that we have recorded a new message
            if(iState == ERecord)
            {
                iObserver.HandleNewMessageL();
                iCallLog->GetNumberL();
            }
            // if we have initialized recording without yet recording and the call is hung up,
            // then we want to delete the file we have just created
            else if(iState == ERecordInit)
            {
                iMessageList->Delete(0);
            }

            SoundCleanup();

            iObserver.HandleCallChangeL(aStatus);

            break;
        default:
            break;
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////
// MAnsPhoneCallMakerObserver
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

void CAnsPhoneEngine::HandleCallHungUpL()

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?