2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.1 (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 AppLogDebug("Enter %d %d",__isDialAppControlRunning ,__isMessageAppControlRunning);
478 if (__isDialAppControlRunning == true)
480 //This comes here, when Dialer AppControl is finished working.
481 __isDialAppControlRunning = false;
482 __pAppControlMgr->AppControlRequestCompleted();
484 if (__isMessageAppControlRunning == true)
486 //This comes here, when Message AppControl is finished working.
487 __isMessageAppControlRunning = false;
488 __pAppControlMgr->AppControlRequestCompleted();
489 //Check if this was the last call, then terminate application.
490 //And if any calls are active, then those cases are already handled from Other places.
491 if( GetCurrentCallCount() == 0 && IsIncomingorDialingCallPresent() == false)
493 CallApp* pCallApp = static_cast<CallApp*>(UiApp::GetInstance());
494 pCallApp->Terminate();
500 CallPresentationModel::OnAppControlCompleteResponseReceived(const AppId& appId, const String& operationId, AppCtrlResult appControlResult, const IMap* pExtraData)
502 AppLogDebug("Enter");
503 if (__isMessageAppControlRunning == true)
505 //This comes here, when Message AppControl is finished working.
506 __isMessageAppControlRunning = false;
507 __pAppControlMgr->AppControlRequestCompleted();
508 //Check if this was the last call, then terminate application.
509 //And if any calls are active, then those cases are already handled from Other places.
510 if( GetCurrentCallCount() == 0)
512 CallApp* pPhoneApp = static_cast<CallApp*>(UiApp::GetInstance());
513 pPhoneApp->Terminate();
516 if(appId.Equals(PROVIDER_ID_PHONE,false) == true && operationId.Equals(OPERATION_ID_DIAL,false) == true)
518 __isDialAppControlRunning = false;
520 __pAppControlMgr->AppControlRequestCompleted();
521 if(appControlResult == APP_CTRL_RESULT_SUCCEEDED)
523 String* pKey = new (std::nothrow) String(PARAM_PHONE_NUMBER);
524 if (pExtraData->ContainsKey(*pKey) == true)
526 const String* pPhoneNumber = static_cast<const String*>(pExtraData->GetValue(*pKey));
527 if(pPhoneNumber != null && pPhoneNumber->IsEmpty() == false)
529 AddCall(*pPhoneNumber);
540 CallPresentationModel::AddCall(const String& phoneNumber)
542 ArrayList* pLaunchArgs = null;
543 SceneManager* pSceneManager = SceneManager::GetInstance();
544 int currentActiveCallCount = GetCurrentCallCount();
545 if(currentActiveCallCount <= 1 && IsIncomingorDialingCallPresent() == false)
547 //make an outgoing call with given number
548 String* contactTxt = new (std::nothrow) String(phoneNumber);
549 pLaunchArgs = new (std::nothrow) ArrayList(SingleObjectDeleter);
550 pLaunchArgs->Construct();
551 pLaunchArgs->Add(contactTxt);
552 bool isEmergencyCall = IsEmergencyNumber(*contactTxt, true);
554 SceneId nextScene = IDSCN_SCENE_OUTCALL;
557 nextScene = IDSCN_SCENE_OUT_EMERGENCYCALL;
559 pSceneManager->GoForward( ForwardSceneTransition( nextScene), pLaunchArgs);
565 CallPresentationModel::GetCurrentCallCount(void)
567 return __pTelephonyMgr->GetCurrentCallCount();
571 CallPresentationModel::CheckSimInitializationIsCompleted()
573 result r = E_FAILURE;
574 if(__pTelephonyMgr != null)
576 r = __pTelephonyMgr->CheckIfMOCallIsPossible();
578 return (!IsFailed(r));
582 CallPresentationModel::IsEmergencyNumber(const Tizen::Base::String& phoneNumber, bool isSimInitialized)
584 return __pTelephonyMgr->CheckIfMOCallIsEmergency(phoneNumber, isSimInitialized);
588 CallPresentationModel::StartAlert(AppCallInfo& incomingCallInfo)
590 //Adding incoming call sate setting here
591 if(__pSettingsPresentor != null)
593 __pSettingsPresentor->SetCallState(CALL_STATE_CALL_VOICE_CONNECTING);
595 __pTelephonyMgr->StartAlert(incomingCallInfo);
599 CallPresentationModel::StopAlert(void)
601 __pTelephonyMgr->StopAlert();
605 CallPresentationModel::GetContactN(const String& phoneNumber)
607 return __pTelephonyMgr->GetContactN(phoneNumber);
611 CallPresentationModel::FetchIncomingCallDetailsN(const String& callHandle, const String& contactNumber)
614 return __pTelephonyMgr->FetchIncomingCallHandleN(callHandle, contactNumber);
618 CallPresentationModel::CheckIncomingCallToBeRejected(AppCallInfo* pIncomingCallInfo)
620 return __pTelephonyMgr->CheckIncomingCallToBeRejected(pIncomingCallInfo);
623 /////////////////////////////////////////////////////////////////
624 ///// Event Listener methods from ITelephonyEventListener /////
625 /////////////////////////////////////////////////////////////////
627 CallPresentationModel::HandleCallConnected(Tizen::Base::Collection::IListT<AppCallInfo>& pCallList)
629 if (__pTelEventListener != null)
631 __pTelEventListener->HandleCallConnected(pCallList);
633 if(__pSettingsPresentor != null)
635 __pSettingsPresentor->SetCallState(CALL_STATE_CALL_VOICE_ACTIVE);
640 CallPresentationModel::HandleCallDisconnected(bool isLastCall, Tizen::Base::Collection::IListT<AppCallInfo>& pCallList)
642 AppLogDebug("Enter");
643 if (isLastCall == true)
645 SetSpeakerStatus(false);
646 SetMuteStatus(false);
647 if(__pSettingsPresentor != null)
649 __pSettingsPresentor->SetCallState(CALL_STATE_CALL_OFF);
654 if(__pSettingsPresentor != null)
656 __pSettingsPresentor->SetCallState(CALL_STATE_CALL_VOICE_ACTIVE);
659 //1) Defer from sending call disconnected event to form, in case Msg AppControl is running,
660 //to avoid PhoneApp from going to EndCall form, where it shows for 3 sec. and automatically closes.
661 //2) Do not send call disconnected event to any form, in case an incoming call or dialing call is present.
662 if (__pTelEventListener != null /*&& __isMessageAppControlRunning == false*/ )
664 __pTelEventListener->HandleCallDisconnected(isLastCall, pCallList);
669 CallPresentationModel::HandleConferenceCall(AppCallInfo& pCallInfo)
671 if (__pTelEventListener != null)
673 __pTelEventListener->HandleConferenceCall(pCallInfo);
678 CallPresentationModel::HandleIncomingCall(AppCallInfo& pCallInfo)
680 AppLogDebug("Error - This will never come here. Since, now we are getting incoming call event through AppControl!!");
684 CallPresentationModel::HandleCallSwapOccured(Tizen::Base::Collection::IListT<AppCallInfo>& pCallList)
686 if (__pTelEventListener != null)
688 __pTelEventListener->HandleCallSwapOccured(pCallList);
693 CallPresentationModel::HandleConferenceChange(void)
695 //1) Do not send conf. call changed event to any form, in case an incoming call or dialing call is present.
696 if (__pTelEventListener != null)
698 __pTelEventListener->HandleConferenceChange();
703 CallPresentationModel::HandleTelephonyError(int errorCode)
705 if (__pTelEventListener != null)
707 __pTelEventListener->HandleTelephonyError(errorCode);
712 CallPresentationModel::LaunchDialAppControl()
715 if(__isDialAppControlRunning == true)
717 AppLogDebug("__isDialAppControlRunning == true");
718 //Do not allow another app control if already running
722 //Launch dialer AppControl
723 if (__pAppControlMgr != null)
725 __isDialAppControlRunning = __pAppControlMgr->LaunchDialerAppControl(this);
731 CallPresentationModel::IsEnableJoinCallButton(void)
733 //Check if conf. call has maximum participants
734 AppCallInfo* pConfCallInfo = GetConferenceCallInfoN();
735 if(pConfCallInfo != null && pConfCallInfo->GetCallerListCount() >= IDI_MAX_CONF_CALL_PARTICIPANTS)
740 //check if either of the caller is same or present in conf call.
741 IListT<AppCallInfo>* pActiveCallList = GetCallListN();
742 if ( pActiveCallList != null && pActiveCallList->GetCount() == IDI_MAX_ACTIVE_CALLS)
744 AppCallInfo firstCall;
745 AppCallInfo secondCall;
746 pActiveCallList->GetAt(0, firstCall);
747 pActiveCallList->GetAt(1, secondCall);
749 if (firstCall.IsConferenceCall() == true)
751 IListT<AppCallInfo>* pConfMemberList = firstCall.GetCallerList();
752 for (int index = 0; index < pConfMemberList->GetCount(); index++)
754 AppCallInfo singleConfMember;
755 pConfMemberList->GetAt(index, singleConfMember);
756 if (secondCall.GetContactNumber().IsEmpty() == false && secondCall.GetContactNumber().Equals(singleConfMember.GetContactNumber()) == true)
762 else if (secondCall.IsConferenceCall() == true)
764 IListT<AppCallInfo>* pConfMemberList = secondCall.GetCallerList();
765 for (int index = 0; index < pConfMemberList->GetCount(); index++)
767 AppCallInfo singleConfMember;
768 pConfMemberList->GetAt(index, singleConfMember);
769 if (firstCall.GetContactNumber().IsEmpty() == false && firstCall.GetContactNumber().Equals(singleConfMember.GetContactNumber()) == true)
777 //Now, we definitely know that both are single active calls.
778 if (firstCall.GetContactNumber().IsEmpty() == false && firstCall.GetContactNumber().Equals(secondCall.GetContactNumber()) == true)
784 delete pConfCallInfo;
785 pConfCallInfo = null;
790 CallPresentationModel::IsIncomingorDialingCallPresent(void)
792 //returns false, if incoming call or dialed call is present.
793 return __pTelephonyMgr->IsIncomingorDialingCallPresent();
797 CallPresentationModel::LaunchComposeMessageAppControl(String& contactNumber, IAppControlResponseListener* pListener)
799 if (__pAppControlMgr != null)
801 return __pAppControlMgr->LaunchComposeMessageAppControl(contactNumber, pListener);
807 CallPresentationModel::LaunchViewContactAppControl(String& contactId, IAppControlResponseListener* pListener)
809 if (__pAppControlMgr != null)
811 return __pAppControlMgr->LaunchViewContactAppControl(contactId, pListener);
817 CallPresentationModel::LaunchAddContactAppControl(Tizen::Base::String& contactNumber, Tizen::App::IAppControlResponseListener* pListener)
819 if (__pAppControlMgr != null)
821 return __pAppControlMgr->LaunchAddContactAppControl(contactNumber, pListener);
827 CallPresentationModel::IsAppControlRunning(void)
829 if (__pAppControlMgr != null)
831 return __pAppControlMgr->IsAppControlRunning();
837 CallPresentationModel::AbortAppControlRequest(void)
839 if (__pAppControlMgr != null)
841 __pAppControlMgr->AbortAppControlRequest();
846 CallPresentationModel::AppControlRequestCompleted(void)
848 if (__pAppControlMgr != null)
850 __pAppControlMgr->AppControlRequestCompleted();
855 CallPresentationModel::GetSimInfo(void)
857 __psimStateManager = new (std::nothrow)SimStateManager();
858 result r = __psimStateManager->Construct();
861 delete __psimStateManager;
862 __psimStateManager = null;
865 __psimStateManager->SetSimEventListener(this);
867 __psimInfo = new (std::nothrow)SimInfo();
868 r = __psimStateManager->GetSimInfo(*__psimInfo);
871 delete __psimStateManager;
872 __psimStateManager = null;
881 CallPresentationModel::OnTelephonyNetworkStatusChanged(const NetworkStatus& networkStatus)
887 CallPresentationModel::OnTelephonySimStateChanged(Tizen::Telephony::SimState state)
889 if(__psimStateManager != null)
891 delete __psimStateManager;
892 __psimStateManager =null;
894 if(__psimInfo != null)
900 __psimStateManager = new SimStateManager();
901 result r = __psimStateManager->Construct();
904 delete __psimStateManager;
905 __psimStateManager = null;
909 __psimInfo = new SimInfo();
910 r = __psimStateManager->GetSimInfo(*__psimInfo);
913 delete __psimStateManager;
914 __psimStateManager = null;
923 CallPresentationModel::IsSimAvailable(void)
925 if(__psimInfo != null)
927 return __psimInfo->IsAvailable();