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.
26 #include "CallAppFrame.h"
27 #include "CallPresentationModel.h"
28 #include "CallTypes.h"
29 #include "CallAppUtility.h"
30 #include "CallIAppStateChangeListner.h"
32 using namespace Tizen::App;
33 using namespace Tizen::Base;
34 using namespace Tizen::System;
35 using namespace Tizen::Ui;
36 using namespace Tizen::Ui::Controls;
37 using namespace Tizen::Ui::Scenes;
38 using namespace Tizen::Base::Utility;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Base::Utility;
41 using namespace Tizen::Shell;
43 static const int FONT_SIZE_AUTOREJECT_POPUP_TEXT = 36;
46 CallApp::CallApp(void):__initialSceneId(L""), __pLaunchArgs(null)
48 __listenerList.Construct();
49 __pCallRejectedIonPopup = null;
50 __pCallAppTimer = null;
54 CallApp::~CallApp(void)
59 CallApp::CreateInstance(void)
61 // Create the instance through the constructor.
66 CallApp::OnAppInitializing(AppRegistry& appRegistry)
68 AppControlProviderManager* pProviderMgr = AppControlProviderManager::GetInstance();
69 pProviderMgr->SetAppControlProviderEventListener(this);
70 PowerManager::AddScreenEventListener(*this);
71 __callRejectedIontimer.Construct(*this);
76 CallApp::OnAppInitialized(void)
82 CallAppFrame* pCallAppFrame = new CallAppFrame();
83 pCallAppFrame->Construct();
84 pCallAppFrame->SetName(L"CallApp");
85 AddFrame(*pCallAppFrame);
87 //Check if there is no initial scene, then exit application.
88 //This case will normally come when invalid AppControl request has come,
89 //or incoming call is coming from unknown number and "reject unknown number" settings is enabled.
90 if (GetInitialScene().IsEmpty() == true)
95 if (__pCallAppTimer == null)
97 __pCallAppTimer = new (std::nothrow) Tizen::Base::Runtime::Timer();
98 __pCallAppTimer->Construct(*this);
99 long long currTime = 0;
100 SystemTime::GetTicks(currTime);
101 __callTicks = currTime;
102 __pCallAppTimer->StartAsRepeatable(TimeSpan::NUM_OF_TICKS_IN_SECOND);
110 CallApp::OnAppWillTerminate(void)
118 CallApp::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
121 // Deallocate resources allocated by this App for termination.
122 // The App's permanent data and context can be saved via appRegistry.
123 PowerManager::RemoveScreenEventListener(*this);
124 if (__pCallAppTimer != null)
126 __pCallAppTimer->Cancel();
127 delete __pCallAppTimer;
134 CallApp::OnForeground(void)
136 IEnumerator* pEnum = __listenerList.GetEnumeratorN();
137 while (pEnum->MoveNext() == E_SUCCESS)
139 IAppStateChangeListener* pInterface = static_cast<IAppStateChangeListener*>(pEnum->GetCurrent());
140 if (pInterface == null)
146 pInterface->OnForeground();
152 CallApp::OnBackground(void)
155 // Stop drawing when the application is moved to the background.
159 CallApp::OnLowMemory(void)
162 // Free unused resources or close the application.
166 CallApp::OnBatteryLevelChanged(BatteryLevel batteryLevel)
169 // Handle any changes in battery level here.
170 // Stop using multimedia features(camera, mp3 etc.) if the battery level is CRITICAL.
174 CallApp::OnScreenOn(void)
177 // Get the released resources or resume the operations that were paused or stopped in OnScreenOff().
181 CallApp::OnScreenOff(void)
183 AppLogDebug("Enter");
184 IEnumerator* pEnum = __listenerList.GetEnumeratorN();
185 while (pEnum->MoveNext() == E_SUCCESS)
187 IAppStateChangeListener* pInterface = static_cast<IAppStateChangeListener*>(pEnum->GetCurrent());
188 if (pInterface == null)
194 pInterface->OnScreenOff();
201 CallApp::GetInitialScene(void)
203 return __initialSceneId;
207 CallApp::GetAppLaunchArguments(void)
209 return __pLaunchArgs;
213 CallApp::AddAppStateChangeListener(const IAppStateChangeListener& listener)
215 __listenerList.Add(listener);
219 CallApp::RemoveAppStateChangeListener(const IAppStateChangeListener& listener)
221 __listenerList.Remove(listener);
225 CallApp::OnAppControlRequestReceived(RequestId reqId, const String& operationId, const String* pUriData,
226 const String* pMimeType, const IMap* pExtraData)
228 AppLogDebug("Enter ");
231 AppLogDebug("%ls ",pUriData->GetPointer());
234 if(pExtraData == null && pUriData != null)
236 //The request is from web app
237 AppLogDebug("%ls",pUriData->GetPointer());
238 ProcessWebAppControlRequest(reqId, operationId, pUriData);
242 //process AppControl parameters
243 ProcessAppControlRequest(reqId, operationId, pExtraData,pUriData);
249 CallApp::ProcessWebAppControlRequest(RequestId reqId, const String& operationId,const String* pUriData)
251 //Construct map from string
252 String delim(DELIMITER);
253 StringTokenizer st(*pUriData,delim);
256 extraData.Construct();
257 while(st.HasMoreTokens())
261 st.GetNextToken(token);
264 if(st.HasMoreTokens())
267 st.GetNextToken(token);
271 extraData.Add(new (std::nothrow) String(key), new (std::nothrow) String(value));
274 //Adding this explicitly as there no other way to invoke call from webapp
275 extraData.Add(new (std::nothrow) String(PARAM_CALL_TYPE), new (std::nothrow) String(PARAM_CALL_VALUE_VOICE));
277 ProcessAppControlRequest(reqId,operationId,&extraData);
279 extraData.RemoveAll(true);
283 CallApp::ProcessAppControlRequest(RequestId reqId, const String& operationId,const IMap* pArgsMap,const String* pUriData)
285 AppLogDebug("Enter %ls",operationId.GetPointer());
286 __pLaunchArgs = null;
287 if(operationId.Equals(OPERATION_ID_CALL,true) == true)
289 AppLogDebug("OPERATION_ID_CALL");
292 bool isIncomingCallRequest = false;
293 String* pKey = new (std::nothrow) String(LAUNCHTYPE);
294 if (pArgsMap->ContainsKey(*pKey) == true)
296 const String* pValue = static_cast<const String*>(pArgsMap->GetValue(*pKey));
297 if ((pValue != null) && (pValue->Equals(PARAM_ORIGIN_MT, true) == true))
299 isIncomingCallRequest = true;
302 //Check if incoming call request or outgoing call request
303 if(isIncomingCallRequest == true)
305 HandleIncomingCallAppControlRequest(reqId, pArgsMap);
309 HandleDialCallAppControlRequest(reqId, pArgsMap,pUriData);
314 AppLogDebug("pArgsMap == null");
320 CallApp::HandleIncomingCallAppControlRequest(RequestId reqId,const IMap* pArgsMap)
322 AppLogDebug("Enter");
323 SceneManager* pSceneManager = SceneManager::GetInstance();
325 AppCtrlResult appControlResult = APP_CTRL_RESULT_FAILED;
328 String callHandle(L"");
329 String* pKey = new (std::nothrow) String(CALL_HANDLE);
330 if (pArgsMap->ContainsKey(*pKey) == true)
332 const String* pValue = static_cast<const String*>(pArgsMap->GetValue(*pKey));
335 callHandle.Append(*pValue);
340 String contactNumber(L"");
341 pKey = new (std::nothrow) String(CONTACT_NUMBER);
342 if (pArgsMap->ContainsKey(*pKey) == true)
344 const String* pContactValue = static_cast<const String*>(pArgsMap->GetValue(*pKey));
345 if (pContactValue != null)
347 contactNumber.Append(*pContactValue);
348 AppLogDebug("%ls",contactNumber.GetPointer());
354 //Fetch incoming call details
355 CallPresentationModel* pCallPresentor = CallPresentationModel::GetInstance();
356 //Check if there is already a dial call present .This can happen in some
357 //race conditions N_SE-39531
358 // removing this as per N_SE-49099
359 /*if(pCallPresentor->IsIncomingorDialingCallPresent())
362 Integer::Parse(callHandle,incomingHandle);
363 pCallPresentor->RejectCall(incomingHandle,false,contactNumber);
364 AppLog("Cancelled already an incoming call present");
365 appControlResult = APP_CTRL_RESULT_CANCELED;
366 AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, appControlResult, null);
370 AppCallInfo* pIncomingCall = pCallPresentor->FetchIncomingCallDetailsN(callHandle, contactNumber);
371 if(pIncomingCall != null)
373 bool isCallRejected = pCallPresentor->CheckIncomingCallToBeRejected(pIncomingCall);
374 if(isCallRejected == false)
376 //save app launch argument list
377 __pLaunchArgs = new (std::nothrow) ArrayList(SingleObjectDeleter);
378 __pLaunchArgs->Construct(1);
379 __pLaunchArgs->Add(pIncomingCall);
380 if(__initialSceneId.IsEmpty() == true)
382 __initialSceneId = IDSCN_SCENE_INCOMINGCALL;
386 //App already initialized, goto incoming call form
387 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_SCENE_INCOMINGCALL, SCENE_TRANSITION_ANIMATION_TYPE_NONE,
388 SCENE_HISTORY_OPTION_NO_HISTORY, SCENE_DESTROY_OPTION_DESTROY), __pLaunchArgs);
389 __pLaunchArgs = null;
396 //Show messageBox showing automatic call rejection
397 /*MessageBox callRejectedInoMsgBox;
398 String msg(L"Call From ");
399 msg.Append(contactNumber);
400 msg.Append(L" Rejected.");
401 callRejectedInoMsgBox.Construct(L"Call Rejected", msg, MSGBOX_STYLE_NONE,1000);
403 // Calls ShowAndWait() : Draws and Shows itself and processes events
404 callRejectedInoMsgBox.ShowAndWait(modalResult);*/
405 //go back to previous scene if App was already running, else exit application.
406 if(__initialSceneId.IsEmpty() == true)
408 //KEEP "__initialSceneId" as empty and return false from "OnAppInitialized()"
409 AppLog("Terminate Phone Application");
414 ShowAutoRejectPopup(contactNumber);
417 //set success message
418 appControlResult = APP_CTRL_RESULT_SUCCEEDED;
422 appControlResult = APP_CTRL_RESULT_FAILED;
424 AppLogDebug("Exiting %d",appControlResult);
425 AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, appControlResult, null);
429 CallApp::ShowAutoRejectPopup(Tizen::Base::String contactNumber)
431 String msg(AppUtility::GetResourceString(IDS_CALL_REJECT_FROM_STRING));
433 msg.Append(contactNumber);
434 if(__pCallRejectedIonPopup != null)
436 delete __pCallRejectedIonPopup;
437 __pCallRejectedIonPopup = null;
439 __pCallRejectedIonPopup = new (std::nothrow) Popup();
440 Tizen::Graphics::Dimension dim(600,200);
441 __pCallRejectedIonPopup->Construct(true, dim);
442 __pCallRejectedIonPopup->SetTitleText(AppUtility::GetResourceString(IDS_CALL_REJECTED_TITLE_STRING));
443 Label* pLabelText = new (std::nothrow) Label();
444 pLabelText->Construct(Tizen::Graphics::Rectangle(0, 0, dim.width, dim.height-100), msg);
445 pLabelText->SetTextConfig(FONT_SIZE_AUTOREJECT_POPUP_TEXT, LABEL_TEXT_STYLE_NORMAL);
446 pLabelText->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
447 pLabelText->SetTextVerticalAlignment(ALIGNMENT_MIDDLE);
448 __pCallRejectedIonPopup->AddControl(pLabelText);
449 __pCallRejectedIonPopup->Show();
450 __pCallRejectedIonPopup->SetShowState(true);
451 __callRejectedIontimer.Start(3000);
455 CallApp::OnTimerExpired(Tizen::Base::Runtime::Timer &timer)
457 if (__callRejectedIontimer.Equals(timer))
459 if(__pCallRejectedIonPopup != null)
461 delete __pCallRejectedIonPopup;
462 __pCallRejectedIonPopup = null;
466 if(__pCallAppTimer->Equals(timer))
473 CallApp::HandleDialCallAppControlRequest(RequestId reqId,const IMap* pArgsMap,const String* pUriData)
476 AppCtrlResult appControlResult = APP_CTRL_RESULT_FAILED;
478 if (pArgsMap != null)
480 String callType(L"");
481 String phoneNumber(L"");
483 String* pKey = new (std::nothrow) String(PARAM_PHONE_NUMBER);
484 if(pArgsMap->ContainsKey(*pKey) == true)
486 const String* pPhoneValue = static_cast<const String*>(pArgsMap->GetValue(*pKey));
487 if(pPhoneValue != null)
489 AppLogDebug("%ls",pPhoneValue->GetPointer());
490 phoneNumber.Append(*pPhoneValue);
495 AppLogDebug("PARAM_PHONE_NUMBER not present");
496 //Now check if tel uri is present
499 AppLogDebug("pUriData is present %ls",pUriData->GetPointer());
500 phoneNumber.Append(*pUriData);
501 if(phoneNumber.Contains(PARAM_PHONE_NUMBER))
503 phoneNumber.Replace(PARAM_PHONE_NUMBER,L"");
504 if(phoneNumber.Contains(DELIMITER))
506 phoneNumber.Replace(DELIMITER,L"");
507 AppLogDebug("%ls",phoneNumber.GetPointer());
510 AppLogDebug("%ls",phoneNumber.GetPointer());
514 //Check if its a valid number
515 /* if(CheckNumberIsValid(phoneNumber) == false)
517 //go back to previous scene if App was already running, else exit application.
518 if(__initialSceneId.IsEmpty() == true)
520 //KEEP "__initialSceneId" as empty and return false from "OnAppInitialized()"
521 AppLog("Terminate Phone Application");
522 AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, appControlResult, null);
528 //invalid phone number.always return, but App will come to foreground
529 //and show current screen, if any calls is present.
534 pKey = new (std::nothrow) String(PARAM_CALL_TYPE);
535 if(pArgsMap->ContainsKey(*pKey) == true)
537 const String* pCallTypeValue = static_cast<const String*>(pArgsMap->GetValue(*pKey));
538 if(pCallTypeValue != null)
540 callType.Append(*pCallTypeValue);
546 //Fetch currently active call count
547 if (callType.IsEmpty() == false
548 && callType.Equals(PARAM_CALL_VALUE_VOICE, false) == true
549 && phoneNumber.IsEmpty() == false)
551 SceneManager* pSceneManager = SceneManager::GetInstance();
552 //check if there is already a call in dialing mode, then dont accept any other dialing request.
553 if (pSceneManager->GetCurrentSceneId() == IDSCN_SCENE_OUTCALL
554 || pSceneManager->GetCurrentSceneId()
555 == IDSCN_SCENE_OUT_EMERGENCYCALL)
558 appControlResult = APP_CTRL_RESULT_CANCELED;
559 AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, appControlResult, null);
563 CallPresentationModel* pCallPresentor = CallPresentationModel::GetInstance();
564 //Check if there is already an incoming call
565 //this can happen in some race conditions N_SE-39531
566 if(pCallPresentor->IsIncomingorDialingCallPresent() == true)
568 AppLog("Cancelled already an incoming call present");
569 appControlResult = APP_CTRL_RESULT_CANCELED;
570 AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, appControlResult, null);
573 int currentActiveCallCount = pCallPresentor->GetCurrentCallCount();
574 if(currentActiveCallCount <= 1)
576 //Abort any AppControl Request running already to show incoming call screen
577 if (pCallPresentor->IsAppControlRunning() == true)
579 pCallPresentor->AbortAppControlRequest();
581 //make an outgoing call with given number
582 String* contactTxt = new (std::nothrow) String(phoneNumber);
583 __pLaunchArgs = new (std::nothrow) ArrayList(SingleObjectDeleter);
584 __pLaunchArgs->Construct();
585 __pLaunchArgs->Add(contactTxt);
586 bool isEmergencyCall = pCallPresentor->IsEmergencyNumber(*contactTxt, true);
588 SceneId nextScene = IDSCN_SCENE_OUTCALL;
591 nextScene = IDSCN_SCENE_OUT_EMERGENCYCALL;
593 //Check if app was already running
594 if(__initialSceneId.IsEmpty() == true)
596 //phone App is not already launched
597 __initialSceneId = nextScene;
601 AppLog("Outgoing call");
602 pSceneManager->GoForward( ForwardSceneTransition( nextScene, SCENE_TRANSITION_ANIMATION_TYPE_NONE,
603 SCENE_HISTORY_OPTION_NO_HISTORY, SCENE_DESTROY_OPTION_KEEP), __pLaunchArgs);
605 appControlResult = APP_CTRL_RESULT_SUCCEEDED;
609 //already 2 active calls, 3rd call not allowed
610 appControlResult = APP_CTRL_RESULT_CANCELED;
615 appControlResult = APP_CTRL_RESULT_FAILED;
618 //send response message
619 AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, appControlResult, null);
623 CallApp::CheckNumberIsValid(String phoneNumber)
625 //Pattern to compare all characters except 0-9 * # P ; , +
626 String phoneNumberPattern(L"[^0-9*#P,p+;]");
627 RegularExpression checkPhoneNumber;
628 checkPhoneNumber.Construct(phoneNumberPattern);
629 //If there is any character other than these listed above then display invalid number
630 bool resultMatch = checkPhoneNumber.Match(phoneNumber,false);
631 //return false for patterns other than 0-9 * # P ; , +
632 if(resultMatch == true)
634 //return phone number is invalid
643 CallApp::SetTopMostWindow(bool bTopMost)
645 AppLogDebug("bTopMost = %d",bTopMost);
646 result res = E_FAILURE;
647 //ToDO: Need to see if there is better way to handle
652 GetAppFrame()->GetFrame()->SetZOrderGroup(WINDOW_Z_ORDER_GROUP_HIGHEST);
653 AppManager::GetInstance()->AddActiveAppEventListener(*this);
654 /*if(PowerManager::IsScreenOn() == false)
656 AppLogDebug("TurnScreenOn");
657 res = PowerManager::TurnScreenOn();
658 AppLogDebug("TurnScreenOn %d",res);
664 GetAppFrame()->GetFrame()->SetZOrderGroup(WINDOW_Z_ORDER_GROUP_NORMAL);
665 PowerManager::KeepScreenOnState(false);
666 AppManager::GetInstance()->RemoveActiveAppEventListener(*this);
669 if(LockManager::GetInstance()->IsLocked())
671 AppLogDebug("Phone Locked");
672 LockManager::GetInstance()->Unlock();
677 CallApp::OnActiveAppChanged(const String& appId)
679 AppLogDebug("Enter %ls",appId.GetPointer());
680 if(GetAppId().Equals(appId) == true)
682 result res = PowerManager::KeepScreenOnState(true,false);
683 AppLogDebug("KeepScreenOnState %d",res);
690 CallApp::GetCallAppTicks(void)