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 "CallActiveCallForm.h"
27 #include "CallPresentationModel.h"
28 #include "CallSettingsPresentationModel.h"
29 #include "CallConfCallerListForm.h"
30 #include "CallTelephonyManager.h"
31 #include "CallSceneRegister.h"
32 #include "CallTypes.h"
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36 using namespace Tizen::App;
37 using namespace Tizen::Graphics;
38 using namespace Tizen::Media;
39 using namespace Tizen::Social;
40 using namespace Tizen::Ui::Scenes;
42 CallPresentationModel* CallPresentationModel::__pInstance = null;
44 CallPresentationModel::CallPresentationModel(void)
46 __pTelephonyMgr = null;
47 __pTelEventListener = null;
48 __pSettingsPresentor = null;
49 __isMessageAppControlRunning = false;
50 __isDialAppControlRunning = false;
53 CallPresentationModel::~CallPresentationModel(void)
55 __pTelephonyMgr = null;
56 __pSettingsPresentor = null;
60 CallPresentationModel::CreateInstance(void)
62 __pInstance = new (std::nothrow) CallPresentationModel();
63 result r = __pInstance->Construct();
71 std::atexit(DestroyInstance);
74 CallPresentationModel*
75 CallPresentationModel::GetInstance(void)
77 if (__pInstance == null)
85 CallPresentationModel::DestroyInstance(void)
87 if (__pInstance != null)
95 CallPresentationModel::Construct(void)
97 //Fetch Telephony Manager
98 __pTelephonyMgr = TelephonyManager::GetInstance(this);
99 __pSettingsPresentor = SettingsPresentationModel::GetInstance();
104 CallPresentationModel::SetTelEventListener(ITelephonyEventListener* pTelEventListener)
106 //set form as telephony event listener
107 __pTelEventListener = pTelEventListener;
111 CallPresentationModel::DialCall(String& contactNumber, bool isEmergency)
113 int errorCode = ERROR_NONE;
114 //Check if Telephony Manager is initialized
115 TryCatch(__pTelephonyMgr != null, (errorCode = ERROR_TAPI_INIT_FAILED), "TAPI initialization failed");
117 //check if phone is in flight mode
118 if(__pSettingsPresentor != null && __pSettingsPresentor->GetFlightModeStatus() == true)
120 __pTelEventListener->HandleTelephonyError(ERROR_FLIGHT_MODE_SET);
123 //setup outgoing call
124 errorCode = __pTelephonyMgr->SetupMoCall(contactNumber, isEmergency);
125 TryCatch(errorCode == ERROR_NONE,,"Error occurred while setup MO call");
126 if(__pSettingsPresentor != null)
128 __pSettingsPresentor->SetCallState(CALL_STATE_CALL_VOICE_CONNECTING);
133 __pTelEventListener->HandleTelephonyError(errorCode);
137 CallPresentationModel::EndActiveCall(Long callHandle)
139 if(__pTelephonyMgr != null)
141 __pTelephonyMgr->EndActiveCall(callHandle);
146 CallPresentationModel::EndDialingCall(String& contactNumber)
148 if(__pTelephonyMgr != null)
150 __pTelephonyMgr->EndDialingCall(contactNumber);
155 CallPresentationModel::EndConferenceCall(void)
157 result r = __pTelephonyMgr->EndConferenceCall();
160 //TODO: send proper error code
161 __pTelEventListener->HandleTelephonyError(ERROR_GENERAL);
168 CallPresentationModel::EndAllCall(void)
170 if(__pTelephonyMgr != null)
172 __pTelephonyMgr->EndAllCalls();
176 CallPresentationModel::HoldCall(Tizen::Base::Long callHandle)
178 result r = __pTelephonyMgr->HoldCall(callHandle, true);
179 return (!IsFailed(r));
183 CallPresentationModel::UnHoldCall(Tizen::Base::Long callHandle)
185 result r = __pTelephonyMgr->HoldCall(callHandle, false);
186 return (!IsFailed(r));
190 CallPresentationModel::HoldConferenceCall(void)
192 result r = __pTelephonyMgr->HoldConferenceCall(true);
193 return (!IsFailed(r));
197 CallPresentationModel::ActivateConferenceCall(void)
199 result r = __pTelephonyMgr->HoldConferenceCall(false);
200 return (!IsFailed(r));
204 CallPresentationModel::JoinCall(void)
206 result r = __pTelephonyMgr->JoinCall();
209 __pTelEventListener->HandleTelephonyError(ERROR_JOIN_FAILED);
214 CallPresentationModel::SwapCalls(void)
216 result r = __pTelephonyMgr->SwapCalls();
219 __pTelEventListener->HandleTelephonyError(ERROR_SWAP_FAILED);
224 CallPresentationModel::SetMuteStatus(bool setMute)
226 result r = __pTelephonyMgr->SetMuteStatus(setMute);
227 return (!IsFailed(r));
231 CallPresentationModel::IsCallMuted(void)
233 return __pTelephonyMgr->IsCallMuted();
237 CallPresentationModel::SetSpeakerStatus(bool setSpeaker)
239 result r = __pTelephonyMgr->SetSpeakerStatus(setSpeaker);
240 return (!IsFailed(r));
244 CallPresentationModel::IsSpeakerOn(void)
246 return __pTelephonyMgr->IsSpeakerOn();
249 CallPresentationModel::SendDTMFSignal(String& textToBeSent)
251 __pTelephonyMgr->SendCallDTMF(textToBeSent);
255 CallPresentationModel::GetConferenceCallInfoN(void)
257 return __pTelephonyMgr->GetConferenceCallInfoN();
261 CallPresentationModel::SplitFromConference(SplitConfCallerCmdIds splitCallerCmdId, IListT<AppCallInfo>* pConfCallList)
264 AppCallInfo callToBeSpli;
265 switch (splitCallerCmdId)
267 case IDA_SPLIT_CALLER1:
271 case IDA_SPLIT_CALLER2:
275 case IDA_SPLIT_CALLER3:
279 case IDA_SPLIT_CALLER4:
283 case IDA_SPLIT_CALLER5:
291 result r = pConfCallList->GetAt(callIndex, callToBeSpli);
292 TryCatch(r == E_SUCCESS,,"conf. call list corrupted");
293 //split single call from conference
294 r = __pTelephonyMgr->SplitFromConference(callToBeSpli.GetCallHandle()->ToLong());
295 TryCatch(r == E_SUCCESS,,"Split from conf. call failed");
299 __pTelEventListener->HandleTelephonyError(ERROR_SPLIT_FROM_CONFERENCE_FAILED);
304 CallPresentationModel::EndCallFromConference(EndConfCallerCmdIds endCallerCmdId, IListT<AppCallInfo>* pConfCallList)
307 AppCallInfo callToBeEnded;
308 switch (endCallerCmdId)
310 case IDA_END_CALLER1:
314 case IDA_END_CALLER2:
318 case IDA_END_CALLER3:
322 case IDA_END_CALLER4:
326 case IDA_END_CALLER5:
334 result r = pConfCallList->GetAt(callIndex, callToBeEnded);
335 TryCatch(r == E_SUCCESS,,"conference call list corrupted");
337 r = __pTelephonyMgr->EndFromConference(callToBeEnded.GetCallHandle()->ToLong());
338 TryCatch(r == E_SUCCESS,,"End single call from conference call failed");
343 __pTelEventListener->HandleTelephonyError(ERROR_END_CALL_FAILED);
348 CallPresentationModel::IsSplitAllowed(void)
350 return __pTelephonyMgr->IsSplitAllowed();
354 CallPresentationModel::AcceptIncomingCall(CallAnsweringOptions answerOptions,int callHandle)
356 result r = E_FAILURE;
357 if (answerOptions == ANSERWING_OPTION_ACCEPT_CALL)
359 r = __pTelephonyMgr->AnswerCall(callHandle, true);
363 r = __pTelephonyMgr->AcceptCall(answerOptions,callHandle);
367 __pTelEventListener->HandleTelephonyError(ERROR_GENERAL);
372 CallPresentationModel::GetCallListN(void)
374 return __pTelephonyMgr->GetCallListN();
379 CallPresentationModel::RejectCall(int callHandle, bool sendMsg, const String& contactNumber)
381 AppLogDebug("Enter");
382 result r = __pTelephonyMgr->AnswerCall(callHandle, false);
385 __pTelEventListener->HandleTelephonyError(ERROR_GENERAL);
392 result r = E_SUCCESS;
394 extraData.Construct();
396 extraData.Add(new (std::nothrow) String(MESSAGE_TYPE), new (std::nothrow) String(MESSAGE_SMS_TYPE));
397 extraData.Add(new (std::nothrow) String(MESSAGE_TO), new (std::nothrow) String(contactNumber));
399 AppControl* pAc = AppManager::FindAppControlN(PROVIDER_ID_MESSAGE, OPERATION_ID_COMPOSE);
402 AppLogDebug("Start");
403 r = pAc->Start(null, null, &extraData, this);
404 __isMessageAppControlRunning = true;
408 extraData.RemoveAll(true);
414 void CallPresentationModel::OnAppForeground(void)
416 if (__isMessageAppControlRunning == true)
418 //This comes here, when Message AppControl is finished working.
419 __isMessageAppControlRunning = false;
420 //Check if this was the last call, then terminate application.
421 //And if any calls are active, then those cases are already handled from Other places.
422 if( GetCurrentCallCount() == 0)
424 CallApp* pPhoneApp = static_cast<CallApp*>(UiApp::GetInstance());
425 pPhoneApp->Terminate();
431 CallPresentationModel::OnAppControlCompleteResponseReceived(const AppId& appId, const String& operationId, AppCtrlResult appControlResult, const IMap* pExtraData)
433 if (__isMessageAppControlRunning == true)
435 //This comes here, when Message AppControl is finished working.
436 __isMessageAppControlRunning = false;
437 //Check if this was the last call, then terminate application.
438 //And if any calls are active, then those cases are already handled from Other places.
439 if( GetCurrentCallCount() == 0)
441 CallApp* pPhoneApp = static_cast<CallApp*>(UiApp::GetInstance());
442 pPhoneApp->Terminate();
445 if(appId.Equals(PROVIDER_ID_PHONE,false) == true && operationId.Equals(OPERATION_ID_DIAL,false) == true)
447 __isDialAppControlRunning = false;
449 if(appControlResult == APP_CTRL_RESULT_SUCCEEDED)
451 String* pKey = new (std::nothrow) String(PARAM_PHONE_NUMBER);
452 if (pExtraData->ContainsKey(*pKey) == true)
454 const String* pPhoneNumber = static_cast<const String*>(pExtraData->GetValue(*pKey));
455 if(pPhoneNumber != null && pPhoneNumber->IsEmpty() == false)
457 AddCall(*pPhoneNumber);
468 CallPresentationModel::AddCall(const String& phoneNumber)
470 ArrayList* pLaunchArgs = null;
471 SceneManager* pSceneManager = SceneManager::GetInstance();
472 int currentActiveCallCount = GetCurrentCallCount();
473 if(currentActiveCallCount <= 1)
475 //make an outgoing call with given number
476 String* contactTxt = new (std::nothrow) String(phoneNumber);
477 pLaunchArgs = new (std::nothrow) ArrayList(SingleObjectDeleter);
478 pLaunchArgs->Construct();
479 pLaunchArgs->Add(contactTxt);
480 bool isEmergencyCall = IsEmergencyNumber(*contactTxt, true);
482 SceneId nextScene = IDSCN_SCENE_OUTCALL;
485 nextScene = IDSCN_SCENE_OUT_EMERGENCYCALL;
487 pSceneManager->GoForward( ForwardSceneTransition( nextScene), pLaunchArgs);
493 CallPresentationModel::GetCurrentCallCount(void)
495 return __pTelephonyMgr->GetCurrentCallCount();
499 CallPresentationModel::CheckSimInitializationIsCompleted()
501 result r = E_FAILURE;
502 if(__pTelephonyMgr != null)
504 r = __pTelephonyMgr->CheckIfMOCallIsPossible();
506 return (!IsFailed(r));
510 CallPresentationModel::IsEmergencyNumber(const Tizen::Base::String& phoneNumber, bool isSimInitialized)
512 return __pTelephonyMgr->CheckIfMOCallIsEmergency(phoneNumber, isSimInitialized);
516 CallPresentationModel::StartAlert(AppCallInfo& incomingCallInfo)
518 //Adding incoming call sate setting here
519 if(__pSettingsPresentor != null)
521 __pSettingsPresentor->SetCallState(CALL_STATE_CALL_VOICE_CONNECTING);
523 __pTelephonyMgr->StartAlert(incomingCallInfo);
527 CallPresentationModel::StopAlert(void)
529 __pTelephonyMgr->StopAlert();
533 CallPresentationModel::GetContactN(const String& phoneNumber)
535 return __pTelephonyMgr->GetContactN(phoneNumber);
539 CallPresentationModel::FetchIncomingCallDetailsN(const String& callHandle, const String& contactNumber)
542 return __pTelephonyMgr->FetchIncomingCallHandleN(callHandle, contactNumber);
546 CallPresentationModel::CheckIncomingCallToBeRejected(AppCallInfo* pIncomingCallInfo)
548 return __pTelephonyMgr->CheckIncomingCallToBeRejected(pIncomingCallInfo);
551 /////////////////////////////////////////////////////////////////
552 ///// Event Listener methods from ITelephonyEventListener /////
553 /////////////////////////////////////////////////////////////////
555 CallPresentationModel::HandleCallConnected(Tizen::Base::Collection::IListT<AppCallInfo>& pCallList)
557 if (__pTelEventListener != null)
559 __pTelEventListener->HandleCallConnected(pCallList);
561 if(__pSettingsPresentor != null)
563 __pSettingsPresentor->SetCallState(CALL_STATE_CALL_VOICE_ACTIVE);
568 CallPresentationModel::HandleCallDisconnected(bool isLastCall, Tizen::Base::Collection::IListT<AppCallInfo>& pCallList)
570 AppLogDebug("Enter");
571 if (isLastCall == true)
573 SetSpeakerStatus(false);
574 SetMuteStatus(false);
575 if(__pSettingsPresentor != null)
577 __pSettingsPresentor->SetCallState(CALL_STATE_CALL_OFF);
582 if(__pSettingsPresentor != null)
584 __pSettingsPresentor->SetCallState(CALL_STATE_CALL_VOICE_ACTIVE);
587 //Defer from sending call disconnected event to form, in case Msg AppControl is running,
588 //to avoid PhoneApp from going to EndCall form, where it shows for 3 sec. and automatically closes.
589 if (__pTelEventListener != null && __isMessageAppControlRunning == false)
591 __pTelEventListener->HandleCallDisconnected(isLastCall, pCallList);
597 CallPresentationModel::HandleConferenceCall(AppCallInfo& pCallInfo)
599 if (__pTelEventListener != null)
601 __pTelEventListener->HandleConferenceCall(pCallInfo);
606 CallPresentationModel::HandleIncomingCall(AppCallInfo& pCallInfo)
608 AppLogDebug("Error - This will never come here. Since, now we are getting incoming call event through AppControl!!");
612 CallPresentationModel::HandleCallSwapOccured(Tizen::Base::Collection::IListT<AppCallInfo>& pCallList)
614 if (__pTelEventListener != null)
616 __pTelEventListener->HandleCallSwapOccured(pCallList);
621 CallPresentationModel::HandleConferenceChange(void)
623 if (__pTelEventListener != null)
625 __pTelEventListener->HandleConferenceChange();
630 CallPresentationModel::HandleTelephonyError(int errorCode)
632 if (__pTelEventListener != null)
634 __pTelEventListener->HandleTelephonyError(errorCode);
640 CallPresentationModel::LaunchDialAppControl()
642 result r = E_SUCCESS;
643 AppControl* pAppControl = null;
645 if(__isDialAppControlRunning == true)
647 //Do not allow another app control if already running
652 extraData.Construct();
653 extraData.Add(new (std::nothrow) String(PARAM_PHONE_NUMBER), new (std::nothrow) String(L""));
655 pAppControl = AppManager::FindAppControlN(PROVIDER_ID_PHONE, OPERATION_ID_DIAL);
656 if (pAppControl != null)
658 r = pAppControl->Start(null, null, &extraData, this);
659 __isDialAppControlRunning = true;
662 extraData.RemoveAll(true);
668 CallPresentationModel::IsEnableJoinCallButton(void)
670 //Check if conf. call has maximum participants
671 AppCallInfo* pConfCallInfo = GetConferenceCallInfoN();
672 if(pConfCallInfo != null && pConfCallInfo->GetCallerListCount() >= IDI_MAX_CONF_CALL_PARTICIPANTS)
677 //check if either of the caller is same or present in conf call.
678 IListT<AppCallInfo>* pActiveCallList = GetCallListN();
679 if ( pActiveCallList != null && pActiveCallList->GetCount() == IDI_MAX_ACTIVE_CALLS)
681 AppCallInfo firstCall;
682 AppCallInfo secondCall;
683 pActiveCallList->GetAt(0, firstCall);
684 pActiveCallList->GetAt(1, secondCall);
686 if (firstCall.IsConferenceCall() == true)
688 IListT<AppCallInfo>* pConfMemberList = firstCall.GetCallerList();
689 for (int index = 0; index < pConfMemberList->GetCount(); index++)
691 AppCallInfo singleConfMember;
692 pConfMemberList->GetAt(index, singleConfMember);
693 if (secondCall.GetContactNumber().IsEmpty() == false && secondCall.GetContactNumber().Equals(singleConfMember.GetContactNumber()) == true)
699 else if (secondCall.IsConferenceCall() == true)
701 IListT<AppCallInfo>* pConfMemberList = secondCall.GetCallerList();
702 for (int index = 0; index < pConfMemberList->GetCount(); index++)
704 AppCallInfo singleConfMember;
705 pConfMemberList->GetAt(index, singleConfMember);
706 if (firstCall.GetContactNumber().IsEmpty() == false && firstCall.GetContactNumber().Equals(singleConfMember.GetContactNumber()) == true)
714 //Now, we definitely know that both are single active calls.
715 if (firstCall.GetContactNumber().IsEmpty() == false && firstCall.GetContactNumber().Equals(secondCall.GetContactNumber()) == true)
721 delete pConfCallInfo;
722 pConfCallInfo = null;