2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://floralicense.org/license/
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file CallCallPresentationModel.cpp
19 * @brief Call Presentation model class
25 #include "CallAppControlRequestMgr.h"
26 #include "CallActiveCallForm.h"
28 #include "CallPresentationModel.h"
29 #include "CallSettingsPresentationModel.h"
30 #include "CallConfCallerListForm.h"
31 #include "CallTelephonyManager.h"
32 #include "CallSceneRegister.h"
33 #include "CallTypes.h"
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Collection;
37 using namespace Tizen::App;
38 using namespace Tizen::Graphics;
39 using namespace Tizen::Media;
40 using namespace Tizen::Social;
41 using namespace Tizen::Ui::Scenes;
42 using namespace Tizen::Telephony;
43 using namespace Tizen::Base::Utility;
45 CallPresentationModel* CallPresentationModel::__pInstance = null;
47 CallPresentationModel::CallPresentationModel(void)
49 __pTelephonyMgr = null;
50 __pTelEventListener = null;
51 __pSettingsPresentor = null;
52 __isMessageAppControlRunning = false;
53 __isDialAppControlRunning = false;
54 __pAppControlMgr = null;
55 __pNetworkManager = null;
56 __psimStateManager = null;
60 CallPresentationModel::~CallPresentationModel(void)
62 __pTelephonyMgr = null;
63 __pSettingsPresentor = null;
64 __pAppControlMgr =null;
65 if(__pNetworkManager != null)
67 delete __pNetworkManager;
68 __pNetworkManager = null;
70 if(__psimStateManager != null)
72 delete __psimStateManager;
73 __psimStateManager = null;
75 if(__psimInfo != null)
83 CallPresentationModel::CreateInstance(void)
85 __pInstance = new (std::nothrow) CallPresentationModel();
86 result r = __pInstance->Construct();
94 std::atexit(DestroyInstance);
97 CallPresentationModel*
98 CallPresentationModel::GetInstance(void)
100 if (__pInstance == null)
108 CallPresentationModel::DestroyInstance(void)
110 if (__pInstance != null)
118 CallPresentationModel::Construct(void)
120 //Fetch Telephony Manager
121 __pTelephonyMgr = TelephonyManager::GetInstance(this);
122 __pSettingsPresentor = SettingsPresentationModel::GetInstance();
123 __pAppControlMgr = CallAppControlRequestMgr::GetInstance();
124 __pNetworkManager = new (std::nothrow)NetworkManager();
125 __pNetworkManager->Construct(null);
131 CallPresentationModel::SetTelEventListener(ITelephonyEventListener* pTelEventListener)
133 //set form as telephony event listener
134 __pTelEventListener = pTelEventListener;
138 CallPresentationModel::IfNumberEndsWithHash(Tizen::Base::String& phoneNumber)
140 String phoneNumberEndingWithHash(L"#$");
141 RegularExpression checkHash;
142 checkHash.Construct(phoneNumberEndingWithHash);
143 bool endsWithHash = checkHash.Match(phoneNumber,false);
144 if(endsWithHash == true)
152 CallPresentationModel::DialCall(String& contactNumber, bool isEmergency)
154 int errorCode = ERROR_NONE;
155 bool isCallServiceAvailable = false;
156 bool numberEndsWithHash = false;
157 NetworkStatus networkStatus;
159 //Check if Telephony Manager is initialized
160 TryCatch(__pTelephonyMgr != null, (errorCode = ERROR_TAPI_INIT_FAILED), "TAPI initialization failed");
162 //check if phone is in flight mode
163 if(__pSettingsPresentor != null && __pSettingsPresentor->GetFlightModeStatus() == true)
165 __pTelEventListener->HandleTelephonyError(ERROR_FLIGHT_MODE_SET);
168 //Check if dialing a call is possible - Check if sim is available
169 if (IsSimAvailable() == false)
171 __pTelEventListener->HandleTelephonyError(ERROR_CODE_SIM_INITIALIZATION_FAILED);
175 //fetch call service status
176 if(__pNetworkManager != null)
178 r = __pNetworkManager->GetNetworkStatus(networkStatus);
181 isCallServiceAvailable = networkStatus.IsCallServiceAvailable();
186 if (isCallServiceAvailable == false)
188 __pTelEventListener->HandleTelephonyError(ERROR_DIAL_FAILED);
191 // check if GPRS number
192 numberEndsWithHash = IfNumberEndsWithHash(contactNumber);
193 if(numberEndsWithHash == true)
195 __pTelEventListener->HandleTelephonyError(ERROR_GPRS_NUMBER);
199 //setup outgoing call
200 errorCode = __pTelephonyMgr->SetupMoCall(contactNumber, isEmergency);
201 TryCatch(errorCode == ERROR_NONE,,"Error occurred while setup MO call");
202 if(__pSettingsPresentor != null)
204 __pSettingsPresentor->SetCallState(CALL_STATE_CALL_VOICE_CONNECTING);
209 __pTelEventListener->HandleTelephonyError(errorCode);
213 CallPresentationModel::EndActiveCall(Long callHandle)
215 if(__pTelephonyMgr != null)
217 __pTelephonyMgr->EndActiveCall(callHandle);
222 CallPresentationModel::EndDialingCall(String& contactNumber)
224 if(__pTelephonyMgr != null)
226 __pTelephonyMgr->EndDialingCall(contactNumber);
231 CallPresentationModel::EndConferenceCall(void)
233 result r = __pTelephonyMgr->EndConferenceCall();
236 //TODO: send proper error code
237 __pTelEventListener->HandleTelephonyError(ERROR_GENERAL);
244 CallPresentationModel::EndAllCall(void)
246 if(__pTelephonyMgr != null)
248 __pTelephonyMgr->EndAllCalls();
252 CallPresentationModel::HoldCall(Tizen::Base::Long callHandle)
254 result r = __pTelephonyMgr->HoldCall(callHandle, true);
255 return (!IsFailed(r));
259 CallPresentationModel::UnHoldCall(Tizen::Base::Long callHandle)
261 result r = __pTelephonyMgr->HoldCall(callHandle, false);
262 return (!IsFailed(r));
266 CallPresentationModel::HoldConferenceCall(void)
268 result r = __pTelephonyMgr->HoldConferenceCall(true);
269 return (!IsFailed(r));
273 CallPresentationModel::ActivateConferenceCall(void)
275 result r = __pTelephonyMgr->HoldConferenceCall(false);
276 return (!IsFailed(r));
280 CallPresentationModel::JoinCall(void)
282 result r = __pTelephonyMgr->JoinCall();
285 __pTelEventListener->HandleTelephonyError(ERROR_JOIN_FAILED);
290 CallPresentationModel::SwapCalls(void)
292 result r = __pTelephonyMgr->SwapCalls();
295 __pTelEventListener->HandleTelephonyError(ERROR_SWAP_FAILED);
300 CallPresentationModel::SetMuteStatus(bool setMute)
302 result r = __pTelephonyMgr->SetMuteStatus(setMute);
303 return (!IsFailed(r));
307 CallPresentationModel::IsCallMuted(void)
309 return __pTelephonyMgr->IsCallMuted();
313 CallPresentationModel::SetSpeakerStatus(bool setSpeaker)
315 result r = __pTelephonyMgr->SetSpeakerStatus(setSpeaker);
316 return (!IsFailed(r));
320 CallPresentationModel::IsSpeakerOn(void)
322 return __pTelephonyMgr->IsSpeakerOn();
325 CallPresentationModel::SendDTMFSignal(String& textToBeSent)
327 __pTelephonyMgr->SendCallDTMF(textToBeSent);
331 CallPresentationModel::GetConferenceCallInfoN(void)
333 return __pTelephonyMgr->GetConferenceCallInfoN();
337 CallPresentationModel::SplitFromConference(SplitConfCallerCmdIds splitCallerCmdId, IListT<AppCallInfo>* pConfCallList)
340 AppCallInfo callToBeSpli;
341 switch (splitCallerCmdId)
343 case IDA_SPLIT_CALLER1:
347 case IDA_SPLIT_CALLER2:
351 case IDA_SPLIT_CALLER3:
355 case IDA_SPLIT_CALLER4:
359 case IDA_SPLIT_CALLER5:
367 result r = pConfCallList->GetAt(callIndex, callToBeSpli);
368 TryCatch(r == E_SUCCESS,,"conf. call list corrupted");
369 //split single call from conference
370 r = __pTelephonyMgr->SplitFromConference(callToBeSpli.GetCallHandle()->ToLong());
371 TryCatch(r == E_SUCCESS,,"Split from conf. call failed");
375 __pTelEventListener->HandleTelephonyError(ERROR_SPLIT_FROM_CONFERENCE_FAILED);
380 CallPresentationModel::EndCallFromConference(EndConfCallerCmdIds endCallerCmdId, IListT<AppCallInfo>* pConfCallList)
383 AppCallInfo callToBeEnded;
384 switch (endCallerCmdId)
386 case IDA_END_CALLER1:
390 case IDA_END_CALLER2:
394 case IDA_END_CALLER3:
398 case IDA_END_CALLER4:
402 case IDA_END_CALLER5:
410 result r = pConfCallList->GetAt(callIndex, callToBeEnded);
411 TryCatch(r == E_SUCCESS,,"conference call list corrupted");
413 r = __pTelephonyMgr->EndFromConference(callToBeEnded.GetCallHandle()->ToLong());
414 TryCatch(r == E_SUCCESS,,"End single call from conference call failed");
419 __pTelEventListener->HandleTelephonyError(ERROR_END_CALL_FAILED);
424 CallPresentationModel::IsSplitAllowed(void)
426 return __pTelephonyMgr->IsSplitAllowed();
430 CallPresentationModel::AcceptIncomingCall(CallAnsweringOptions answerOptions,int callHandle)
432 result r = E_FAILURE;
433 if (answerOptions == ANSERWING_OPTION_ACCEPT_CALL)
435 r = __pTelephonyMgr->AnswerCall(callHandle, true);
439 r = __pTelephonyMgr->AcceptCall(answerOptions,callHandle);
443 __pTelEventListener->HandleTelephonyError(ERROR_GENERAL);
448 CallPresentationModel::GetCallListN(void)
450 return __pTelephonyMgr->GetCallListN();
455 CallPresentationModel::RejectCall(int callHandle, bool sendMsg, const String& contactNumber)
457 AppLogDebug("Enter");
458 if (sendMsg == true && __pAppControlMgr != null)
460 //launch compose message AppControl
461 __isMessageAppControlRunning = __pAppControlMgr->LaunchComposeMessageAppControl(*(const_cast<String*>(&contactNumber)), this);
463 result r = __pTelephonyMgr->AnswerCall(callHandle, false);
466 __pTelEventListener->HandleTelephonyError(ERROR_GENERAL);
475 void CallPresentationModel::OnAppForeground(void)
477 if (__isDialAppControlRunning == true)
479 //This comes here, when Dialer AppControl is finished working.
480 __isDialAppControlRunning = false;
481 __pAppControlMgr->AppControlRequestCompleted();
483 if (__isMessageAppControlRunning == true)
485 //This comes here, when Message AppControl is finished working.
486 __isMessageAppControlRunning = false;
487 __pAppControlMgr->AppControlRequestCompleted();
488 //Check if this was the last call, then terminate application.
489 //And if any calls are active, then those cases are already handled from Other places.
490 if( GetCurrentCallCount() == 0)
492 CallApp* pPhoneApp = static_cast<CallApp*>(UiApp::GetInstance());
493 pPhoneApp->Terminate();
499 CallPresentationModel::OnAppControlCompleteResponseReceived(const AppId& appId, const String& operationId, AppCtrlResult appControlResult, const IMap* pExtraData)
501 AppLogDebug("Enter");
502 if (__isMessageAppControlRunning == true)
504 //This comes here, when Message AppControl is finished working.
505 __isMessageAppControlRunning = false;
506 __pAppControlMgr->AppControlRequestCompleted();
507 //Check if this was the last call, then terminate application.
508 //And if any calls are active, then those cases are already handled from Other places.
509 if( GetCurrentCallCount() == 0)
511 CallApp* pPhoneApp = static_cast<CallApp*>(UiApp::GetInstance());
512 pPhoneApp->Terminate();
515 if(appId.Equals(PROVIDER_ID_PHONE,false) == true && operationId.Equals(OPERATION_ID_DIAL,false) == true)
517 __isDialAppControlRunning = false;
519 __pAppControlMgr->AppControlRequestCompleted();
520 if(appControlResult == APP_CTRL_RESULT_SUCCEEDED)
522 String* pKey = new (std::nothrow) String(PARAM_PHONE_NUMBER);
523 if (pExtraData->ContainsKey(*pKey) == true)
525 const String* pPhoneNumber = static_cast<const String*>(pExtraData->GetValue(*pKey));
526 if(pPhoneNumber != null && pPhoneNumber->IsEmpty() == false)
528 AddCall(*pPhoneNumber);
539 CallPresentationModel::AddCall(const String& phoneNumber)
541 ArrayList* pLaunchArgs = null;
542 SceneManager* pSceneManager = SceneManager::GetInstance();
543 int currentActiveCallCount = GetCurrentCallCount();
544 if(currentActiveCallCount <= 1 && IsIncomingorDialingCallPresent() == false)
546 //make an outgoing call with given number
547 String* contactTxt = new (std::nothrow) String(phoneNumber);
548 pLaunchArgs = new (std::nothrow) ArrayList(SingleObjectDeleter);
549 pLaunchArgs->Construct();
550 pLaunchArgs->Add(contactTxt);
551 bool isEmergencyCall = IsEmergencyNumber(*contactTxt, true);
553 SceneId nextScene = IDSCN_SCENE_OUTCALL;
556 nextScene = IDSCN_SCENE_OUT_EMERGENCYCALL;
558 pSceneManager->GoForward( ForwardSceneTransition( nextScene), pLaunchArgs);
564 CallPresentationModel::GetCurrentCallCount(void)
566 return __pTelephonyMgr->GetCurrentCallCount();
570 CallPresentationModel::CheckSimInitializationIsCompleted()
572 result r = E_FAILURE;
573 if(__pTelephonyMgr != null)
575 r = __pTelephonyMgr->CheckIfMOCallIsPossible();
577 return (!IsFailed(r));
581 CallPresentationModel::IsEmergencyNumber(const Tizen::Base::String& phoneNumber, bool isSimInitialized)
583 return __pTelephonyMgr->CheckIfMOCallIsEmergency(phoneNumber, isSimInitialized);
587 CallPresentationModel::StartAlert(AppCallInfo& incomingCallInfo)
589 //Adding incoming call sate setting here
590 if(__pSettingsPresentor != null)
592 __pSettingsPresentor->SetCallState(CALL_STATE_CALL_VOICE_CONNECTING);
594 __pTelephonyMgr->StartAlert(incomingCallInfo);
598 CallPresentationModel::StopAlert(void)
600 __pTelephonyMgr->StopAlert();
604 CallPresentationModel::GetContactN(const String& phoneNumber)
606 return __pTelephonyMgr->GetContactN(phoneNumber);
610 CallPresentationModel::FetchIncomingCallDetailsN(const String& callHandle, const String& contactNumber)
613 return __pTelephonyMgr->FetchIncomingCallHandleN(callHandle, contactNumber);
617 CallPresentationModel::CheckIncomingCallToBeRejected(AppCallInfo* pIncomingCallInfo)
619 return __pTelephonyMgr->CheckIncomingCallToBeRejected(pIncomingCallInfo);
622 /////////////////////////////////////////////////////////////////
623 ///// Event Listener methods from ITelephonyEventListener /////
624 /////////////////////////////////////////////////////////////////
626 CallPresentationModel::HandleCallConnected(Tizen::Base::Collection::IListT<AppCallInfo>& pCallList)
628 if (__pTelEventListener != null)
630 __pTelEventListener->HandleCallConnected(pCallList);
632 if(__pSettingsPresentor != null)
634 __pSettingsPresentor->SetCallState(CALL_STATE_CALL_VOICE_ACTIVE);
639 CallPresentationModel::HandleCallDisconnected(bool isLastCall, Tizen::Base::Collection::IListT<AppCallInfo>& pCallList)
641 AppLogDebug("Enter");
642 if (isLastCall == true)
644 SetSpeakerStatus(false);
645 SetMuteStatus(false);
646 if(__pSettingsPresentor != null)
648 __pSettingsPresentor->SetCallState(CALL_STATE_CALL_OFF);
653 if(__pSettingsPresentor != null)
655 __pSettingsPresentor->SetCallState(CALL_STATE_CALL_VOICE_ACTIVE);
658 //1) Defer from sending call disconnected event to form, in case Msg AppControl is running,
659 //to avoid PhoneApp from going to EndCall form, where it shows for 3 sec. and automatically closes.
660 //2) Do not send call disconnected event to any form, in case an incoming call or dialing call is present.
661 if (__pTelEventListener != null /*&& __isMessageAppControlRunning == false*/ )
663 __pTelEventListener->HandleCallDisconnected(isLastCall, pCallList);
668 CallPresentationModel::HandleConferenceCall(AppCallInfo& pCallInfo)
670 if (__pTelEventListener != null)
672 __pTelEventListener->HandleConferenceCall(pCallInfo);
677 CallPresentationModel::HandleIncomingCall(AppCallInfo& pCallInfo)
679 AppLogDebug("Error - This will never come here. Since, now we are getting incoming call event through AppControl!!");
683 CallPresentationModel::HandleCallSwapOccured(Tizen::Base::Collection::IListT<AppCallInfo>& pCallList)
685 if (__pTelEventListener != null)
687 __pTelEventListener->HandleCallSwapOccured(pCallList);
692 CallPresentationModel::HandleConferenceChange(void)
694 //1) Do not send conf. call changed event to any form, in case an incoming call or dialing call is present.
695 if (__pTelEventListener != null)
697 __pTelEventListener->HandleConferenceChange();
702 CallPresentationModel::HandleTelephonyError(int errorCode)
704 if (__pTelEventListener != null)
706 __pTelEventListener->HandleTelephonyError(errorCode);
711 CallPresentationModel::LaunchDialAppControl()
714 if(__isDialAppControlRunning == true)
716 //Do not allow another app control if already running
720 //Launch dialer AppControl
721 if (__pAppControlMgr != null)
723 __isDialAppControlRunning = __pAppControlMgr->LaunchDialerAppControl(this);
729 CallPresentationModel::IsEnableJoinCallButton(void)
731 //Check if conf. call has maximum participants
732 AppCallInfo* pConfCallInfo = GetConferenceCallInfoN();
733 if(pConfCallInfo != null && pConfCallInfo->GetCallerListCount() >= IDI_MAX_CONF_CALL_PARTICIPANTS)
738 //check if either of the caller is same or present in conf call.
739 IListT<AppCallInfo>* pActiveCallList = GetCallListN();
740 if ( pActiveCallList != null && pActiveCallList->GetCount() == IDI_MAX_ACTIVE_CALLS)
742 AppCallInfo firstCall;
743 AppCallInfo secondCall;
744 pActiveCallList->GetAt(0, firstCall);
745 pActiveCallList->GetAt(1, secondCall);
747 if (firstCall.IsConferenceCall() == true)
749 IListT<AppCallInfo>* pConfMemberList = firstCall.GetCallerList();
750 for (int index = 0; index < pConfMemberList->GetCount(); index++)
752 AppCallInfo singleConfMember;
753 pConfMemberList->GetAt(index, singleConfMember);
754 if (secondCall.GetContactNumber().IsEmpty() == false && secondCall.GetContactNumber().Equals(singleConfMember.GetContactNumber()) == true)
760 else if (secondCall.IsConferenceCall() == true)
762 IListT<AppCallInfo>* pConfMemberList = secondCall.GetCallerList();
763 for (int index = 0; index < pConfMemberList->GetCount(); index++)
765 AppCallInfo singleConfMember;
766 pConfMemberList->GetAt(index, singleConfMember);
767 if (firstCall.GetContactNumber().IsEmpty() == false && firstCall.GetContactNumber().Equals(singleConfMember.GetContactNumber()) == true)
775 //Now, we definitely know that both are single active calls.
776 if (firstCall.GetContactNumber().IsEmpty() == false && firstCall.GetContactNumber().Equals(secondCall.GetContactNumber()) == true)
782 delete pConfCallInfo;
783 pConfCallInfo = null;
788 CallPresentationModel::IsIncomingorDialingCallPresent(void)
790 //returns false, if incoming call or dialed call is present.
791 return __pTelephonyMgr->IsIncomingorDialingCallPresent();
795 CallPresentationModel::LaunchComposeMessageAppControl(String& contactNumber, IAppControlResponseListener* pListener)
797 if (__pAppControlMgr != null)
799 return __pAppControlMgr->LaunchComposeMessageAppControl(contactNumber, pListener);
805 CallPresentationModel::LaunchViewContactAppControl(String& contactId, IAppControlResponseListener* pListener)
807 if (__pAppControlMgr != null)
809 return __pAppControlMgr->LaunchViewContactAppControl(contactId, pListener);
815 CallPresentationModel::LaunchAddContactAppControl(Tizen::Base::String& contactNumber, Tizen::App::IAppControlResponseListener* pListener)
817 if (__pAppControlMgr != null)
819 return __pAppControlMgr->LaunchAddContactAppControl(contactNumber, pListener);
825 CallPresentationModel::IsAppControlRunning(void)
827 if (__pAppControlMgr != null)
829 return __pAppControlMgr->IsAppControlRunning();
835 CallPresentationModel::AbortAppControlRequest(void)
837 if (__pAppControlMgr != null)
839 __pAppControlMgr->AbortAppControlRequest();
844 CallPresentationModel::AppControlRequestCompleted(void)
846 if (__pAppControlMgr != null)
848 __pAppControlMgr->AppControlRequestCompleted();
853 CallPresentationModel::GetSimInfo(void)
855 __psimStateManager = new (std::nothrow)SimStateManager();
856 result r = __psimStateManager->Construct();
859 delete __psimStateManager;
860 __psimStateManager = null;
863 __psimStateManager->SetSimEventListener(this);
865 __psimInfo = new (std::nothrow)SimInfo();
866 r = __psimStateManager->GetSimInfo(*__psimInfo);
869 delete __psimStateManager;
870 __psimStateManager = null;
879 CallPresentationModel::OnTelephonyNetworkStatusChanged(const NetworkStatus& networkStatus)
885 CallPresentationModel::OnTelephonySimStateChanged(Tizen::Telephony::SimState state)
887 if(__psimStateManager != null)
889 delete __psimStateManager;
890 __psimStateManager =null;
892 if(__psimInfo != null)
898 __psimStateManager = new SimStateManager();
899 result r = __psimStateManager->Construct();
902 delete __psimStateManager;
903 __psimStateManager = null;
907 __psimInfo = new SimInfo();
908 r = __psimStateManager->GetSimInfo(*__psimInfo);
911 delete __psimStateManager;
912 __psimStateManager = null;
921 CallPresentationModel::IsSimAvailable(void)
923 if(__psimInfo != null)
925 return __psimInfo->IsAvailable();