1.Dtmf change 2.Removed Unlock
[apps/osp/Call.git] / src / CallApp.cpp
1 /**
2  * Name        : CallApp
3  * Version     :
4  * Vendor      :
5  * Description :
6  */
7
8 #include <FUi.h>
9 #include <FShell.h>
10 #include "CallApp.h"
11 #include "CallAppFrame.h"
12 #include "CallPresentationModel.h"
13 #include "CallTypes.h"
14 #include "CallAppUtility.h"
15 #include "CallIAppStateChangeListner.h"
16
17 using namespace Tizen::App;
18 using namespace Tizen::Base;
19 using namespace Tizen::System;
20 using namespace Tizen::Ui;
21 using namespace Tizen::Ui::Controls;
22 using namespace Tizen::Ui::Scenes;
23 using namespace Tizen::Base::Utility;
24 using namespace Tizen::Base::Collection;
25 using namespace Tizen::Base::Utility;
26
27
28 CallApp::CallApp(void):__initialSceneId(L""), __pLaunchArgs(null)
29 {
30         __listenerList.Construct();
31 }
32
33 CallApp::~CallApp(void)
34 {
35 }
36
37 UiApp*
38 CallApp::CreateInstance(void)
39 {
40         // Create the instance through the constructor.
41         return new CallApp();
42 }
43
44 bool
45 CallApp::OnAppInitializing(AppRegistry& appRegistry)
46 {
47         AppControlProviderManager* pProviderMgr = AppControlProviderManager::GetInstance();
48         pProviderMgr->SetAppControlProviderEventListener(this);
49         return true;
50 }
51
52 bool
53 CallApp::OnAppInitialized(void)
54 {
55         // TODO:
56         // Comment.
57
58         // Create a Frame
59         CallAppFrame* pCallAppFrame = new CallAppFrame();
60         pCallAppFrame->Construct();
61         pCallAppFrame->SetName(L"CallApp");
62         AddFrame(*pCallAppFrame);
63
64         //Check if there is no initial scene, then exit application.
65         //This case will normally come when invalid AppControl request has come,
66         //or incoming call is coming from unknown number and "reject unknown number" settings is enabled.
67         if (GetInitialScene().IsEmpty() == true)
68         {
69                 return false;
70         }
71
72         return true;
73 }
74
75 bool
76 CallApp::OnAppWillTerminate(void)
77 {
78         // TODO:
79         // Comment.
80         return true;
81 }
82
83 bool
84 CallApp::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
85 {
86         // TODO:
87         // Deallocate resources allocated by this App for termination.
88         // The App's permanent data and context can be saved via appRegistry.
89         return true;
90 }
91
92 void
93 CallApp::OnForeground(void)
94 {
95         IEnumerator* pEnum = __listenerList.GetEnumeratorN();
96         while (pEnum->MoveNext() == E_SUCCESS)
97         {
98                 IAppStateChangeListener* pInterface = static_cast<IAppStateChangeListener*>(pEnum->GetCurrent());
99                 if (pInterface == null)
100                 {
101                         delete pEnum;
102
103                         return;
104                 }
105                 pInterface->OnForeground();
106         }
107         delete pEnum;
108 }
109
110 void
111 CallApp::OnBackground(void)
112 {
113         // TODO:
114         // Stop drawing when the application is moved to the background.
115 }
116
117 void
118 CallApp::OnLowMemory(void)
119 {
120         // TODO:
121         // Free unused resources or close the application.
122 }
123
124 void
125 CallApp::OnBatteryLevelChanged(BatteryLevel batteryLevel)
126 {
127         // TODO:
128         // Handle any changes in battery level here.
129         // Stop using multimedia features(camera, mp3 etc.) if the battery level is CRITICAL.
130 }
131
132 void
133 CallApp::OnScreenOn(void)
134 {
135         // TODO:
136         // Get the released resources or resume the operations that were paused or stopped in OnScreenOff().
137 }
138
139 void
140 CallApp::OnScreenOff(void)
141 {
142         // TODO:
143         // Unless there is a strong reason to do otherwise, release resources (such as 3D, media, and sensors) to allow the device
144         // to enter the sleep mode to save the battery.
145         // Invoking a lengthy asynchronous method within this listener method can be risky, because it is not guaranteed to invoke a
146         // callback before the device enters the sleep mode.
147         // Similarly, do not perform lengthy operations in this listener method. Any operation must be a quick one.
148 }
149
150 SceneId
151 CallApp::GetInitialScene(void)
152 {
153         return __initialSceneId;
154 }
155
156 IList*
157 CallApp::GetAppLaunchArguments(void)
158 {
159         return __pLaunchArgs;
160 }
161
162 void
163 CallApp::AddAppStateChangeListener(const IAppStateChangeListener& listener)
164 {
165         __listenerList.Add(listener);
166
167 }
168 void
169 CallApp::RemoveAppStateChangeListener(const IAppStateChangeListener& listener)
170 {
171         __listenerList.Remove(listener);
172 }
173
174 void
175 CallApp::OnAppControlRequestReceived(RequestId reqId, const String& operationId, const String* pUriData,
176                 const String* pMimeType, const IMap* pExtraData)
177 {
178         AppLogDebug("Enter ");
179         if(pUriData != null)
180         {
181                 AppLogDebug("%ls ",pUriData->GetPointer());
182         }
183
184         if(pExtraData == null && pUriData != null)
185         {
186                 //The request is from web app
187                 AppLogDebug("%ls",pUriData->GetPointer());
188                 ProcessWebAppControlRequest(reqId, operationId, pUriData);
189         }
190         else
191         {
192                 //process AppControl parameters
193                 ProcessAppControlRequest(reqId, operationId, pExtraData,pUriData);
194         }
195         AppLogDebug("EXIT");
196 }
197
198 void
199 CallApp::ProcessWebAppControlRequest(RequestId reqId, const String& operationId,const String* pUriData)
200 {
201         //Construct map from string
202         String delim(DELIMITER);
203         StringTokenizer st(*pUriData,delim);
204         String token;
205         HashMap extraData;
206         extraData.Construct();
207         while(st.HasMoreTokens())
208         {
209                 String key=L"";
210                 String value=L"";
211                 st.GetNextToken(token);
212                 token.Trim();
213                 key.Append(token);
214                 if(st.HasMoreTokens())
215                 {
216                         token.Clear();
217                         st.GetNextToken(token);
218                         token.Trim();
219                         value.Append(token);
220                 }
221                 extraData.Add(new (std::nothrow) String(key), new (std::nothrow) String(value));
222         }
223
224         //Adding this explicitly as there no other way to invoke call from webapp
225         extraData.Add(new (std::nothrow) String(PARAM_CALL_TYPE), new (std::nothrow) String(PARAM_CALL_VALUE_VOICE));
226
227         ProcessAppControlRequest(reqId,operationId,&extraData);
228
229         extraData.RemoveAll(true);
230 }
231
232 void
233 CallApp::ProcessAppControlRequest(RequestId reqId, const String& operationId,const IMap* pArgsMap,const String* pUriData)
234 {
235         AppLogDebug("Enter %ls",operationId.GetPointer());
236         __pLaunchArgs = null;
237         if(operationId.Equals(OPERATION_ID_CALL,true) == true)
238         {
239                 AppLogDebug("OPERATION_ID_CALL");
240                 if(pArgsMap != null)
241                 {
242                         bool isIncomingCallRequest = false;
243                         String* pKey = new (std::nothrow) String(LAUNCHTYPE);
244                         if (pArgsMap->ContainsKey(*pKey) == true)
245                         {
246                                 const String* pValue = static_cast<const String*>(pArgsMap->GetValue(*pKey));
247                                 if ((pValue != null) && (pValue->Equals(PARAM_ORIGIN_MT, true) == true))
248                                 {
249                                         isIncomingCallRequest = true;
250                                 }
251                         }
252                         //Check if incoming call request or outgoing call request
253                         if(isIncomingCallRequest == true)
254                         {
255                                 HandleIncomingCallAppControlRequest(reqId, pArgsMap);
256                         }
257                         else
258                         {
259                                 HandleDialCallAppControlRequest(reqId, pArgsMap,pUriData);
260                         }
261                 }
262                 else
263                 {
264                         AppLogDebug("pArgsMap == null");
265                 }
266         }
267 }
268
269 void
270 CallApp::HandleIncomingCallAppControlRequest(RequestId reqId,const IMap* pArgsMap)
271 {
272         AppLogDebug("Enter");
273         SceneManager* pSceneManager = SceneManager::GetInstance();
274         //response message
275         AppCtrlResult appControlResult = APP_CTRL_RESULT_FAILED;
276
277         //call handle
278         String callHandle(L"");
279         String* pKey = new (std::nothrow) String(CALL_HANDLE);
280         if (pArgsMap->ContainsKey(*pKey) == true)
281         {
282                 const String* pValue = static_cast<const String*>(pArgsMap->GetValue(*pKey));
283                 if (pValue != null)
284                 {
285                         callHandle.Append(*pValue);
286                 }
287         }
288         delete pKey;
289         //contact number
290         String contactNumber(L"");
291         pKey = new (std::nothrow) String(CONTACT_NUMBER);
292         if (pArgsMap->ContainsKey(*pKey) == true)
293         {
294                 const String* pContactValue = static_cast<const String*>(pArgsMap->GetValue(*pKey));
295                 if (pContactValue != null)
296                 {
297                         contactNumber.Append(*pContactValue);
298                         AppLogDebug("%ls",contactNumber.GetPointer());
299                 }
300         }
301         delete pKey;
302         pKey = null;
303
304         //Fetch incoming call details
305         CallPresentationModel* pCallPresentor = CallPresentationModel::GetInstance();
306         AppCallInfo* pIncomingCall = pCallPresentor->FetchIncomingCallDetailsN(callHandle, contactNumber);
307         if(pIncomingCall != null)
308         {
309                 bool isCallRejected = pCallPresentor->CheckIncomingCallToBeRejected(pIncomingCall);
310                 if(isCallRejected == false)
311                 {
312                         //save app launch argument list
313                         __pLaunchArgs = new (std::nothrow) ArrayList(SingleObjectDeleter);
314                         __pLaunchArgs->Construct(1);
315                         __pLaunchArgs->Add(pIncomingCall);
316                         if(__initialSceneId.IsEmpty() == true)
317                         {
318                                 __initialSceneId = IDSCN_SCENE_INCOMINGCALL;
319                         }
320                         else
321                         {
322                                 //App already initialized, goto incoming call form
323                                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_SCENE_INCOMINGCALL), __pLaunchArgs);
324                                 __pLaunchArgs = null;
325                         }
326                 }
327                 else
328                 {
329                         //Show messageBox showing automatic call rejection
330                 /*      MessageBox callRejectedInoMsgBox;
331                         String msg(L"Call From ");
332                         msg.Append(contactNumber);
333                         msg.Append(L" Rejected.");
334                         callRejectedInoMsgBox.Construct(L"Call Rejected", msg, MSGBOX_STYLE_NONE,1000);
335                         int modalResult = 0;
336                         // Calls ShowAndWait() : Draws and Shows itself and processes events
337                         callRejectedInoMsgBox.ShowAndWait(modalResult);*/
338
339                         //go back to previous scene if App was already running, else exit application.
340                         if(__initialSceneId.IsEmpty() == true)
341                         {
342                                 //KEEP "__initialSceneId" as empty and return false from "OnAppInitialized()"
343                                 AppLog("Terminate Phone Application");
344                                 Terminate();
345                         }
346
347                 }
348                 //set success message
349                 appControlResult = APP_CTRL_RESULT_SUCCEEDED;
350         }
351         else
352         {
353                 appControlResult = APP_CTRL_RESULT_FAILED;
354         }
355         AppLogDebug("Exiting %d",appControlResult);
356         AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, appControlResult, null);
357 }
358
359 void
360 CallApp::HandleDialCallAppControlRequest(RequestId reqId,const IMap* pArgsMap,const String* pUriData)
361 {
362         //response message
363         AppCtrlResult appControlResult = APP_CTRL_RESULT_FAILED;
364
365         if (pArgsMap != null)
366         {
367                 String callType(L"");
368                 String phoneNumber(L"");
369                 //phone number
370                 String* pKey = new (std::nothrow) String(PARAM_PHONE_NUMBER);
371                 if(pArgsMap->ContainsKey(*pKey) == true)
372                 {
373                         const String* pPhoneValue = static_cast<const String*>(pArgsMap->GetValue(*pKey));
374                         if(pPhoneValue != null)
375                         {
376                                 AppLogDebug("%ls",pPhoneValue->GetPointer());
377                                 phoneNumber.Append(*pPhoneValue);
378                         }
379                 }
380                 else
381                 {
382                         AppLogDebug("PARAM_PHONE_NUMBER not present");
383                         //Now check if tel uri is present
384                         if(pUriData != null)
385                         {
386                                 AppLogDebug("pUriData is present %ls",pUriData->GetPointer());
387                                 phoneNumber.Append(*pUriData);
388                                 if(phoneNumber.Contains(PARAM_PHONE_NUMBER))
389                                 {
390                                         phoneNumber.Replace(PARAM_PHONE_NUMBER,L"");
391                                         if(phoneNumber.Contains(DELIMITER))
392                                         {
393                                                 phoneNumber.Replace(DELIMITER,L"");
394                                                 AppLogDebug("%ls",phoneNumber.GetPointer());
395                                         }
396                                 }
397                                 AppLogDebug("%ls",phoneNumber.GetPointer());
398                         }
399                 }
400                 delete pKey;
401                 //Check if its a valid number
402                 if(CheckNumberIsValid(phoneNumber) == false)
403                 {
404                         //Show messageBox showing automatic call rejection
405                         MessageBox InvalidNumberMsgBox;
406                         InvalidNumberMsgBox.Construct(AppUtility::GetResourceString(IDS_INVALID_NUMBER), L"",MSGBOX_STYLE_NONE,1000);
407                         int modalResult = 0;
408                         // Calls ShowAndWait() : Draws and Shows itself and processes events
409                         InvalidNumberMsgBox.ShowAndWait(modalResult);
410
411                         //go back to previous scene if App was already running, else exit application.
412                         if(__initialSceneId.IsEmpty() == true)
413                         {
414                                 //KEEP "__initialSceneId" as empty and return false from "OnAppInitialized()"
415                                 AppLog("Terminate Phone Application");
416                                 AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, appControlResult, null);
417                                 Terminate();
418                                 return;
419                         }
420
421                 }
422                 //call type
423                 pKey = new (std::nothrow) String(PARAM_CALL_TYPE);
424                 if(pArgsMap->ContainsKey(*pKey) == true)
425                 {
426                         const String* pCallTypeValue = static_cast<const String*>(pArgsMap->GetValue(*pKey));
427                         if(pCallTypeValue != null)
428                         {
429                                 callType.Append(*pCallTypeValue);
430                         }
431                 }
432                 delete pKey;
433                 pKey = null;
434
435                 //Fetch currently active call count
436                 if (callType.IsEmpty() == false
437                                 && callType.Equals(PARAM_CALL_VALUE_VOICE, false) == true
438                                 && phoneNumber.IsEmpty() == false)
439                 {
440                         SceneManager* pSceneManager = SceneManager::GetInstance();
441                         //check if there is already a call in dialing mode, then dont accept any other dialing request.
442                         if (pSceneManager->GetCurrentSceneId() == IDSCN_SCENE_OUTCALL
443                                         || pSceneManager->GetCurrentSceneId()
444                                                         == IDSCN_SCENE_OUT_EMERGENCYCALL)
445                         {
446                                 AppLog("Cancelled");
447                                 appControlResult = APP_CTRL_RESULT_CANCELED;
448                                 AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, appControlResult, null);
449                                 return;
450                         }
451                         CallPresentationModel* pCallPresentor = CallPresentationModel::GetInstance();
452                         int currentActiveCallCount = pCallPresentor->GetCurrentCallCount();
453                         if(currentActiveCallCount <= 1)
454                         {
455                                 //make an outgoing call with given number
456                                 String* contactTxt = new (std::nothrow) String(phoneNumber);
457                                 __pLaunchArgs =  new (std::nothrow) ArrayList(SingleObjectDeleter);
458                                 __pLaunchArgs->Construct();
459                                 __pLaunchArgs->Add(contactTxt);
460                                 bool isEmergencyCall = pCallPresentor->IsEmergencyNumber(*contactTxt, true);
461
462                                 SceneId nextScene = IDSCN_SCENE_OUTCALL;
463                                 if (isEmergencyCall)
464                                 {
465                                         nextScene = IDSCN_SCENE_OUT_EMERGENCYCALL;
466                                 }
467                                 //Check if app was already running
468                                 if(__initialSceneId.IsEmpty() == true)
469                                 {
470                                         //phone App is not already launched
471                                         __initialSceneId = nextScene;
472                                 }
473                                 else
474                                 {
475                                         AppLog("Outgoing call");
476                                         pSceneManager->GoForward( ForwardSceneTransition( nextScene), __pLaunchArgs);
477                                 }
478                                 appControlResult = APP_CTRL_RESULT_SUCCEEDED;
479                         }
480                         else
481                         {
482                                 //already 2 active calls, 3rd call not allowed
483                                 appControlResult = APP_CTRL_RESULT_CANCELED;
484                         }
485                 }
486                 else
487                 {
488                         appControlResult = APP_CTRL_RESULT_FAILED;
489                 }
490         }
491         //send response message
492         AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, appControlResult, null);
493 }
494
495 bool
496 CallApp::CheckNumberIsValid(String phoneNumber)
497 {
498         //Pattern to compare all characters except 0-9 * # P ; , +
499         String phoneNumberPattern(L"[^0-9*#P,+;]");
500         RegularExpression checkPhoneNumber;
501         checkPhoneNumber.Construct(phoneNumberPattern);
502         //If there is any character other than these listed above then display invalid number
503         bool resultMatch = checkPhoneNumber.Match(phoneNumber,false);
504         //return false for patterns other than 0-9 * # P ; , +
505         return !resultMatch;
506
507 }
508
509 void
510 CallApp::SetTopMostWindow(bool bTopMost)
511 {
512         AppLogDebug("bTopMost = %d",bTopMost);
513         result res = E_FAILURE;
514         //ToDO: Need to see if there is better way to handle
515         //this case
516
517         if(bTopMost == true)
518         {
519                 GetAppFrame()->GetFrame()->SetZOrderGroup(WINDOW_Z_ORDER_GROUP_HIGHEST);
520                 if(PowerManager::IsScreenOn() == false)
521                 {
522                         res = PowerManager::TurnScreenOn();
523                 }
524                 res = PowerManager::KeepScreenOnState(true,false);
525
526         }
527         else
528         {
529                 GetAppFrame()->GetFrame()->SetZOrderGroup(WINDOW_Z_ORDER_GROUP_NORMAL);
530                 PowerManager::KeepScreenOnState(false);
531         }
532         //Unlock the phone if its locked
533 /*      if(LockManager::GetInstance()->IsLocked())
534         {
535                 AppLogDebug("Phone Locked");
536                 LockManager::GetInstance()->Unlock();
537         }*/
538
539 }