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