1.Disconnecting call when no network 2.Support for Automatic:4dir
[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         AppLogDebug("Enter");
180         if(pExtraData == null && pUriData != null)
181         {
182                 //The request is from web app
183                 AppLogDebug("%ls",pUriData->GetPointer());
184                 ProcessWebAppControlRequest(reqId, operationId, pUriData);
185         }
186         else
187         {
188                 //process AppControl parameters
189                 ProcessAppControlRequest(reqId, operationId, pExtraData);
190         }
191         AppLogDebug("EXIT");
192 }
193
194 void
195 CallApp::ProcessWebAppControlRequest(RequestId reqId, const String& operationId,const String* pUriData)
196 {
197         //Construct map from string
198         String delim(DELIMITER);
199         StringTokenizer st(*pUriData,delim);
200         String token;
201         HashMap extraData;
202         extraData.Construct();
203         while(st.HasMoreTokens())
204         {
205                 String key=L"";
206                 String value=L"";
207                 st.GetNextToken(token);
208                 token.Trim();
209                 key.Append(token);
210                 if(st.HasMoreTokens())
211                 {
212                         token.Clear();
213                         st.GetNextToken(token);
214                         token.Trim();
215                         value.Append(token);
216                 }
217                 extraData.Add(new (std::nothrow) String(key), new (std::nothrow) String(value));
218         }
219
220         //Adding this explicitly as there no other way to invoke call from webapp
221         extraData.Add(new (std::nothrow) String(PARAM_CALL_TYPE), new (std::nothrow) String(PARAM_CALL_VALUE_VOICE));
222
223         ProcessAppControlRequest(reqId,operationId,&extraData);
224
225         extraData.RemoveAll(true);
226 }
227
228 void
229 CallApp::ProcessAppControlRequest(RequestId reqId, const String& operationId,const IMap* pArgsMap)
230 {
231         AppLogDebug("Enter %ls",operationId.GetPointer());
232         __pLaunchArgs = null;
233         if(operationId.Equals(OPERATION_ID_CALL,true) == true)
234         {
235                 AppLogDebug("OPERATION_ID_CALL");
236                 if(pArgsMap != null)
237                 {
238                         bool isIncomingCallRequest = false;
239                         String* pKey = new (std::nothrow) String(LAUNCHTYPE);
240                         if (pArgsMap->ContainsKey(*pKey) == true)
241                         {
242                                 const String* pValue = static_cast<const String*>(pArgsMap->GetValue(*pKey));
243                                 if ((pValue != null) && (pValue->Equals(PARAM_ORIGIN_MT, true) == true))
244                                 {
245                                         isIncomingCallRequest = true;
246                                 }
247                         }
248                         //Check if incoming call request or outgoing call request
249                         if(isIncomingCallRequest == true)
250                         {
251                                 HandleIncomingCallAppControlRequest(reqId, pArgsMap);
252                         }
253                         else
254                         {
255                                 HandleDialCallAppControlRequest(reqId, pArgsMap);
256                         }
257                 }
258                 else
259                 {
260                         AppLogDebug("pArgsMap == null");
261                 }
262         }
263 }
264
265 void
266 CallApp::HandleIncomingCallAppControlRequest(RequestId reqId,const IMap* pArgsMap)
267 {
268         AppLogDebug("Enter");
269         SceneManager* pSceneManager = SceneManager::GetInstance();
270         //response message
271         AppCtrlResult appControlResult = APP_CTRL_RESULT_FAILED;
272
273         //call handle
274         String callHandle(L"");
275         String* pKey = new (std::nothrow) String(CALL_HANDLE);
276         if (pArgsMap->ContainsKey(*pKey) == true)
277         {
278                 const String* pValue = static_cast<const String*>(pArgsMap->GetValue(*pKey));
279                 if (pValue != null)
280                 {
281                         callHandle.Append(*pValue);
282                 }
283         }
284         delete pKey;
285         //contact number
286         String contactNumber(L"");
287         pKey = new (std::nothrow) String(CONTACT_NUMBER);
288         if (pArgsMap->ContainsKey(*pKey) == true)
289         {
290                 const String* pContactValue = static_cast<const String*>(pArgsMap->GetValue(*pKey));
291                 if (pContactValue != null)
292                 {
293                         contactNumber.Append(*pContactValue);
294                         AppLogDebug("%ls",contactNumber.GetPointer());
295                 }
296         }
297         delete pKey;
298         pKey = null;
299
300         //Fetch incoming call details
301         CallPresentationModel* pCallPresentor = CallPresentationModel::GetInstance();
302         AppCallInfo* pIncomingCall = pCallPresentor->FetchIncomingCallDetailsN(callHandle, contactNumber);
303         if(pIncomingCall != null)
304         {
305                 bool isCallRejected = pCallPresentor->CheckIncomingCallToBeRejected(pIncomingCall);
306                 if(isCallRejected == false)
307                 {
308                         //save app launch argument list
309                         __pLaunchArgs = new (std::nothrow) ArrayList(SingleObjectDeleter);
310                         __pLaunchArgs->Construct(1);
311                         __pLaunchArgs->Add(pIncomingCall);
312                         if(__initialSceneId.IsEmpty() == true)
313                         {
314                                 __initialSceneId = IDSCN_SCENE_INCOMINGCALL;
315                         }
316                         else
317                         {
318                                 //App already initialized, goto incoming call form
319                                 pSceneManager->GoForward(ForwardSceneTransition(IDSCN_SCENE_INCOMINGCALL), __pLaunchArgs);
320                                 __pLaunchArgs = null;
321                         }
322                 }
323                 else
324                 {
325                         //Show messageBox showing automatic call rejection
326                         MessageBox callRejectedInoMsgBox;
327                         String msg(L"Call From ");
328                         msg.Append(contactNumber);
329                         msg.Append(L" Rejected.");
330                         callRejectedInoMsgBox.Construct(L"Call Rejected", msg, MSGBOX_STYLE_NONE,1000);
331                         int modalResult = 0;
332                         // Calls ShowAndWait() : Draws and Shows itself and processes events
333                         callRejectedInoMsgBox.ShowAndWait(modalResult);
334
335                         //go back to previous scene if App was already running, else exit application.
336                         if(__initialSceneId.IsEmpty() == true)
337                         {
338                                 //KEEP "__initialSceneId" as empty and return false from "OnAppInitialized()"
339                                 AppLog("Terminate Phone Application");
340                                 Terminate();
341                         }
342
343                 }
344                 //set success message
345                 appControlResult = APP_CTRL_RESULT_SUCCEEDED;
346         }
347         else
348         {
349                 appControlResult = APP_CTRL_RESULT_FAILED;
350         }
351         AppLogDebug("Exiting %d",appControlResult);
352         AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, appControlResult, null);
353 }
354
355 void
356 CallApp::HandleDialCallAppControlRequest(RequestId reqId,const IMap* pArgsMap)
357 {
358         //response message
359         AppCtrlResult appControlResult = APP_CTRL_RESULT_FAILED;
360
361         if (pArgsMap != null)
362         {
363                 String callType(L"");
364                 String phoneNumber(L"");
365                 //phone number
366                 String* pKey = new (std::nothrow) String(PARAM_PHONE_NUMBER);
367                 if(pArgsMap->ContainsKey(*pKey) == true)
368                 {
369                         const String* pPhoneValue = static_cast<const String*>(pArgsMap->GetValue(*pKey));
370                         if(pPhoneValue != null)
371                         {
372                                 AppLogDebug("%ls",pPhoneValue->GetPointer());
373                                 phoneNumber.Append(*pPhoneValue);
374                         }
375                 }
376                 else
377                 {
378                         AppLogDebug("PARAM_PHONE_NUMBER not present");
379                 }
380                 delete pKey;
381                 //Check if its a valid number
382                 if(CheckNumberIsValid(phoneNumber) == false)
383                 {
384                         //Show messageBox showing automatic call rejection
385                         MessageBox InvalidNumberMsgBox;
386                         InvalidNumberMsgBox.Construct(AppUtility::GetResourceString(IDS_INVALID_NUMBER), L"",MSGBOX_STYLE_NONE,1000);
387                         int modalResult = 0;
388                         // Calls ShowAndWait() : Draws and Shows itself and processes events
389                         InvalidNumberMsgBox.ShowAndWait(modalResult);
390
391                         //go back to previous scene if App was already running, else exit application.
392                         if(__initialSceneId.IsEmpty() == true)
393                         {
394                                 //KEEP "__initialSceneId" as empty and return false from "OnAppInitialized()"
395                                 AppLog("Terminate Phone Application");
396                                 AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, appControlResult, null);
397                                 Terminate();
398                                 return;
399                         }
400
401                 }
402                 //call type
403                 pKey = new (std::nothrow) String(PARAM_CALL_TYPE);
404                 if(pArgsMap->ContainsKey(*pKey) == true)
405                 {
406                         const String* pCallTypeValue = static_cast<const String*>(pArgsMap->GetValue(*pKey));
407                         if(pCallTypeValue != null)
408                         {
409                                 callType.Append(*pCallTypeValue);
410                         }
411                 }
412                 delete pKey;
413                 pKey = null;
414
415                 //Fetch currently active call count
416                 if (callType.IsEmpty() == false
417                                 && callType.Equals(PARAM_CALL_VALUE_VOICE, false) == true
418                                 && phoneNumber.IsEmpty() == false)
419                 {
420                         SceneManager* pSceneManager = SceneManager::GetInstance();
421                         //check if there is already a call in dialing mode, then dont accept any other dialing request.
422                         if (pSceneManager->GetCurrentSceneId() == IDSCN_SCENE_OUTCALL
423                                         || pSceneManager->GetCurrentSceneId()
424                                                         == IDSCN_SCENE_OUT_EMERGENCYCALL)
425                         {
426                                 AppLog("Cancelled");
427                                 appControlResult = APP_CTRL_RESULT_CANCELED;
428                                 AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, appControlResult, null);
429                                 return;
430                         }
431                         CallPresentationModel* pCallPresentor = CallPresentationModel::GetInstance();
432                         int currentActiveCallCount = pCallPresentor->GetCurrentCallCount();
433                         if(currentActiveCallCount <= 1)
434                         {
435                                 //make an outgoing call with given number
436                                 String* contactTxt = new (std::nothrow) String(phoneNumber);
437                                 __pLaunchArgs =  new (std::nothrow) ArrayList(SingleObjectDeleter);
438                                 __pLaunchArgs->Construct();
439                                 __pLaunchArgs->Add(contactTxt);
440                                 bool isEmergencyCall = pCallPresentor->IsEmergencyNumber(*contactTxt, true);
441
442                                 SceneId nextScene = IDSCN_SCENE_OUTCALL;
443                                 if (isEmergencyCall)
444                                 {
445                                         nextScene = IDSCN_SCENE_OUT_EMERGENCYCALL;
446                                 }
447                                 //Check if app was already running
448                                 if(__initialSceneId.IsEmpty() == true)
449                                 {
450                                         //phone App is not already launched
451                                         __initialSceneId = nextScene;
452                                 }
453                                 else
454                                 {
455                                         AppLog("Outgoing call");
456                                         pSceneManager->GoForward( ForwardSceneTransition( nextScene), __pLaunchArgs);
457                                 }
458                                 appControlResult = APP_CTRL_RESULT_SUCCEEDED;
459                         }
460                         else
461                         {
462                                 //already 2 active calls, 3rd call not allowed
463                                 appControlResult = APP_CTRL_RESULT_CANCELED;
464                         }
465                 }
466                 else
467                 {
468                         appControlResult = APP_CTRL_RESULT_FAILED;
469                 }
470         }
471         //send response message
472         AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, appControlResult, null);
473 }
474
475 bool
476 CallApp::CheckNumberIsValid(String phoneNumber)
477 {
478         //Pattern to compare all characters except 0-9 * # P ; , +
479         String phoneNumberPattern(L"[^0-9*#P,+;]");
480         RegularExpression checkPhoneNumber;
481         checkPhoneNumber.Construct(phoneNumberPattern);
482         //If there is any character other than these listed above then display invalid number
483         bool resultMatch = checkPhoneNumber.Match(phoneNumber,false);
484         //return false for patterns other than 0-9 * # P ; , +
485         return !resultMatch;
486
487 }
488
489 void
490 CallApp::SetTopMostWindow(bool bTopMost)
491 {
492         AppLogDebug("bTopMost = %d",bTopMost);
493         result res = E_FAILURE;
494         //ToDO: Need to see if there is better way to handle
495         //this case
496
497         if(bTopMost == true)
498         {
499                 GetAppFrame()->GetFrame()->SetZOrderGroup(WINDOW_Z_ORDER_GROUP_HIGHEST);
500                 if(PowerManager::IsScreenOn() == false)
501                 {
502                         res = PowerManager::TurnScreenOn();
503                 }
504                 res = PowerManager::KeepScreenOnState(true,false);
505
506         }
507         else
508         {
509                 GetAppFrame()->GetFrame()->SetZOrderGroup(WINDOW_Z_ORDER_GROUP_NORMAL);
510                 PowerManager::KeepScreenOnState(false);
511         }
512         //Unlock the phone if its locked
513         if(LockManager::GetInstance()->IsLocked())
514         {
515                 LockManager::GetInstance()->Unlock();
516         }
517
518 }