?? clocationexampleengine.cpp
字號:
/*
* ============================================================================
* Name : clocationexampleengine.cpp
* Part of : Location Example
* Created : 21.05.2007 by Forum Nokia
* Description:
* Version : 1.0
* Copyright: Nokia Corporation
* ============================================================================
*/
#include <aknquerydialog.h>
#include <locationexample.rsg>
#include <eikedwin.h>
#include <aknquerydialog.h>
#include <aknnotewrappers.h>
#include "clocationexampleengine.h"
#include "clocationexamplelocationengine.h"
#include "clocationexamplepositionrequestor.h"
#include "clocationexampleapplication.h"
const TInt KSecond = 1000000;
const TInt KUpdateInterval = 10*KSecond;
// -----------------------------------------------------------------------------
// CLocationExampleEngine
// -----------------------------------------------------------------------------
//
CLocationExampleEngine* CLocationExampleEngine::NewL(MPositionObserver* aPositionListener)
{
CLocationExampleEngine* self = NewLC(aPositionListener);
CleanupStack::Pop(self);
return self;
}
// -----------------------------------------------------------------------------
// CLocationExampleEngine
// -----------------------------------------------------------------------------
//
CLocationExampleEngine* CLocationExampleEngine::NewLC(MPositionObserver* aPositionListener)
{
CLocationExampleEngine* self = new (ELeave) CLocationExampleEngine(aPositionListener);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
// -----------------------------------------------------------------------------
// CLocationExampleEngine
// -----------------------------------------------------------------------------
//
void CLocationExampleEngine::ConstructL()
{
iSmsEngine = CLocationExampleSmsEngine::NewL(this);
iLocationEngine = CLocationExampleLocationEngine::NewL();
}
// -----------------------------------------------------------------------------
// CLocationExampleEngine
// -----------------------------------------------------------------------------
//
CLocationExampleEngine::CLocationExampleEngine(MPositionObserver* aPositionListener)
: iPositionRequestor(NULL), iPositionListener(aPositionListener), iEngineState(EIdle),
iSmsEngine(NULL),iLocationEngine(NULL)
{
}
// -----------------------------------------------------------------------------
// CLocationExampleEngine
// -----------------------------------------------------------------------------
//
CLocationExampleEngine::~CLocationExampleEngine()
{
delete iPositionRequestor;
delete iSmsEngine;
delete iLocationEngine;
}
// -----------------------------------------------------------------------------
// CLocationExampleEngine
// -----------------------------------------------------------------------------
//
void CLocationExampleEngine::ActivateGpsL()
{
// Create and initialise position requestor
if (!iPositionRequestor)
{
iPositionRequestor = CLocationExamplePositionRequestor::NewL(KUpdateInterval,*this);
SetEngineState(EScanningGps);
}
}
// -----------------------------------------------------------------------------
// CLocationExampleEngine
// -----------------------------------------------------------------------------
//
void CLocationExampleEngine::DeactivateGpsL()
{
// Create and initialise position requestor
if (iPositionRequestor)
{
delete iPositionRequestor;
iPositionRequestor = NULL;
}
SetEngineState(EIdle);
}
// -----------------------------------------------------------------------------
// CLocationExampleEngine
// -----------------------------------------------------------------------------
//
void CLocationExampleEngine::PositionUpdatedL(TPositionInfoBase& aPosInfo)
{
iPositionListener->PositionUpdatedL(aPosInfo);
}
// -----------------------------------------------------------------------------
// CLocationExampleEngine
// -----------------------------------------------------------------------------
//
void CLocationExampleEngine::FriendPositionUpdatedL(const TDesC& aInfo, const TCoordinate& aCoordinate)
{
iPositionListener->FriendPositionUpdatedL(aInfo,aCoordinate);
}
// -----------------------------------------------------------------------------
// CLocationExampleEngine
// -----------------------------------------------------------------------------
//
void CLocationExampleEngine::ErrorL(const TDesC& aErrorString)
{
iPositionListener->ErrorL(aErrorString);
}
// -----------------------------------------------------------------------------
// CLocationExampleEngine
// -----------------------------------------------------------------------------
//
void CLocationExampleEngine::MessageL(const TDesC& aMessage)
{
iPositionListener->MessageL(aMessage);
}
// -----------------------------------------------------------------------------
// CLocationExampleEngine
// -----------------------------------------------------------------------------
//
void CLocationExampleEngine::SetEngineState(TEngineState aNewState)
{
iEngineState = aNewState;
};
// -----------------------------------------------------------------------------
// CLocationExampleEngine
// -----------------------------------------------------------------------------
//
TBool CLocationExampleEngine::SendSMSQueryL()
{
TBool ret = EFalse;
HBufC * prompt = CEikonEnv::Static()->AllocReadResourceLC(R_SMS_SEND_LOCATION);
HBufC * cap = CEikonEnv::Static()->AllocReadResourceLC(R_CAPTION);
ret = CEikonEnv::Static()->QueryWinL(*cap,*prompt);
CleanupStack::PopAndDestroy(cap);
CleanupStack::PopAndDestroy(prompt);
return ret;
}
// -----------------------------------------------------------------------------
// CLocationExampleEngine
// -----------------------------------------------------------------------------
//
TInt CLocationExampleEngine::NumberQueryL()
{
TInt ret = KErrNotReady;
HBufC * prompt = CEikonEnv::Static()->AllocReadResourceLC(R_GIVE_PHONE_NUMBER);
CAknTextQueryDialog* dlg = new(ELeave) CAknTextQueryDialog(iPhonenumber, *prompt);
dlg->SetMaxLength(KQueryStringInputMaxLength);
if (dlg->ExecuteLD(R_LOCATIONEXAMPLE_ADDRESS_QUERY))
{
ret = KErrNone;
}
CleanupStack::PopAndDestroy(prompt);
return ret;
}
// -----------------------------------------------------------------------------
// CLocationExampleEngine
//
// Message body: RES E01FF1Cd latitude_value longitude_value
// Message body: REQ E01FF1Cd latitude_value longitude_value
// -----------------------------------------------------------------------------
//
void CLocationExampleEngine::DoMessageAndSendL(const TPositionType& aType)
{
HBufC* message = HBufC::NewLC(KSMSMessageLength);
TPtr16 ptr = message->Des();
// Write postition request type REQUEST or RESPONSE
if (aType == EPositionResponse)
{
ptr.Append(KPosResponse);
}
else
{
ptr.Append(KPosRequest);
}
ptr.Append(KMark);
// Write App UID to SMS message
ptr.AppendFormat(_L("%X"),KUidLocationExampleApp.iUid);
ptr.Append(KMark);
if (iPositionRequestor)
{
TPositionInfoBase* pos = iPositionRequestor->CurrentPosition();
// Check if position information class type is TPositionInfo
if (pos && pos->PositionClassType() & EPositionInfoClass)
{
// Cast the TPositionInfoBase object to TPositionInfo
TPositionInfo* posInfo = static_cast<TPositionInfo*>(pos);
// Get basic position data
TPosition position;
posInfo->GetPosition(position);
// If the aDegree is a proper number
if ( !Math::IsNaN(position.Latitude()) && !Math::IsNaN(position.Longitude()))
{
TBuf<KSMSMessageDegreeLength> latStr;
latStr.Format(_L("%.8f"), position.Latitude());
ptr.Append(latStr);
ptr.Append(KMark);
TBuf<KSMSMessageDegreeLength> longStr;
longStr.Format(_L("%.8f"), position.Longitude());
ptr.Append(longStr);
// Send message
TRAP_IGNORE(iSmsEngine->SendSmsL(iPhonenumber,*message));
}
else
{
User::Leave(KErrNotReady);
}
}
else
{
User::Leave(KErrNotReady);
}
}
else
{
User::Leave(KErrNotReady);
}
CleanupStack::PopAndDestroy(); //message
}
// -----------------------------------------------------------------------------
// CLocationExampleEngine
// -----------------------------------------------------------------------------
//
void CLocationExampleEngine::MessageSent()
{
HBufC* text = CEikonEnv::Static()->AllocReadResourceAsDes16LC(R_SMS_SENT);
ShowMessageToUser(*text);
CleanupStack::PopAndDestroy(text);
}
// -----------------------------------------------------------------------------
// CLocationExampleEngine
// -----------------------------------------------------------------------------
//
void CLocationExampleEngine::MessageReceived(TDesC& aMsg, TDesC& aAddr)
{
// You are reseiving your friend location
TInt err = iSmsEngine->ParseMsgCoordinates(aMsg,iFriendCoordinate);
if (!err)
{
iPositionListener->FriendPositionUpdatedL(aAddr,iFriendCoordinate);
}
}
// -----------------------------------------------------------------------------
// MessageRequested
// -----------------------------------------------------------------------------
//
void CLocationExampleEngine::MessageRequested(TDesC& /*aMsg*/, TDesC& aAddr)
{
// Your friend ask your location
// Send your location to number from received SMS request
// Set SMS requster phone number
iPhonenumber.Copy(aAddr);
TBool ret = SendSMSQueryL();
if (ret)
{
TRAP_IGNORE(DoMessageAndSendL(EPositionResponse));
}
}
// -----------------------------------------------------------------------------
// CLocationExampleEngine
// -----------------------------------------------------------------------------
//
void CLocationExampleEngine::SmsEngineError(TInt aErrorCode)
{
TRAP_IGNORE(
HBufC* text = CEikonEnv::Static()->AllocReadResourceAsDes16LC(R_SMS_ERROR);
ShowErrorToUser(*text, aErrorCode);
CleanupStack::PopAndDestroy();
);
}
// -----------------------------------------------------------------------------
// CLocationExampleEngine
// -----------------------------------------------------------------------------
//
void CLocationExampleEngine::ShowErrorToUser(const TDesC& aMessage, const TInt aErrorCode)
{
const TInt LengthForErrorCode = 10;
TRAP_IGNORE(
HBufC* errorText = HBufC::NewLC(aMessage.Length() + LengthForErrorCode);
TPtr16 ptr = errorText->Des();
ptr.Copy(aMessage);
ptr.AppendFormat(_L("%d"),aErrorCode);
CAknErrorNote* note = new (ELeave) CAknErrorNote();
CleanupStack::PushL(note);
note->SetTextL(*errorText);
CleanupStack::Pop();
note->ExecuteLD();
CleanupStack::PopAndDestroy(errorText);
);
}
// -----------------------------------------------------------------------------
// CLocationExampleEngine
// -----------------------------------------------------------------------------
//
void CLocationExampleEngine::ShowMessageToUser(const TDesC& aMessage)
{
TRAP_IGNORE(
CAknInformationNote* note = new (ELeave) CAknInformationNote();
CleanupStack::PushL(note);
note->SetTextL(aMessage);
CleanupStack::Pop();
note->ExecuteLD();
);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -