99397f94e1f0d99647f820ad85f7f30ca3b4efc2
[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                         //go back to previous scene if App was already running, else exit application.
405                         if(__initialSceneId.IsEmpty() == true)
406                         {
407                                 //KEEP "__initialSceneId" as empty and return false from "OnAppInitialized()"
408                                 AppLog("Terminate Phone Application");
409                                 AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, appControlResult, null);
410                                 Terminate();
411                                 return;
412                         }
413
414                 }
415                 //call type
416                 pKey = new (std::nothrow) String(PARAM_CALL_TYPE);
417                 if(pArgsMap->ContainsKey(*pKey) == true)
418                 {
419                         const String* pCallTypeValue = static_cast<const String*>(pArgsMap->GetValue(*pKey));
420                         if(pCallTypeValue != null)
421                         {
422                                 callType.Append(*pCallTypeValue);
423                         }
424                 }
425                 delete pKey;
426                 pKey = null;
427
428                 //Fetch currently active call count
429                 if (callType.IsEmpty() == false
430                                 && callType.Equals(PARAM_CALL_VALUE_VOICE, false) == true
431                                 && phoneNumber.IsEmpty() == false)
432                 {
433                         SceneManager* pSceneManager = SceneManager::GetInstance();
434                         //check if there is already a call in dialing mode, then dont accept any other dialing request.
435                         if (pSceneManager->GetCurrentSceneId() == IDSCN_SCENE_OUTCALL
436                                         || pSceneManager->GetCurrentSceneId()
437                                                         == IDSCN_SCENE_OUT_EMERGENCYCALL)
438                         {
439                                 AppLog("Cancelled");
440                                 appControlResult = APP_CTRL_RESULT_CANCELED;
441                                 AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, appControlResult, null);
442                                 return;
443                         }
444                         CallPresentationModel* pCallPresentor = CallPresentationModel::GetInstance();
445                         int currentActiveCallCount = pCallPresentor->GetCurrentCallCount();
446                         if(currentActiveCallCount <= 1)
447                         {
448                                 //make an outgoing call with given number
449                                 String* contactTxt = new (std::nothrow) String(phoneNumber);
450                                 __pLaunchArgs =  new (std::nothrow) ArrayList(SingleObjectDeleter);
451                                 __pLaunchArgs->Construct();
452                                 __pLaunchArgs->Add(contactTxt);
453                                 bool isEmergencyCall = pCallPresentor->IsEmergencyNumber(*contactTxt, true);
454
455                                 SceneId nextScene = IDSCN_SCENE_OUTCALL;
456                                 if (isEmergencyCall)
457                                 {
458                                         nextScene = IDSCN_SCENE_OUT_EMERGENCYCALL;
459                                 }
460                                 //Check if app was already running
461                                 if(__initialSceneId.IsEmpty() == true)
462                                 {
463                                         //phone App is not already launched
464                                         __initialSceneId = nextScene;
465                                 }
466                                 else
467                                 {
468                                         AppLog("Outgoing call");
469                                         pSceneManager->GoForward( ForwardSceneTransition( nextScene), __pLaunchArgs);
470                                 }
471                                 appControlResult = APP_CTRL_RESULT_SUCCEEDED;
472                         }
473                         else
474                         {
475                                 //already 2 active calls, 3rd call not allowed
476                                 appControlResult = APP_CTRL_RESULT_CANCELED;
477                         }
478                 }
479                 else
480                 {
481                         appControlResult = APP_CTRL_RESULT_FAILED;
482                 }
483         }
484         //send response message
485         AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, appControlResult, null);
486 }
487
488 bool
489 CallApp::CheckNumberIsValid(String phoneNumber)
490 {
491         //Pattern to compare all characters except 0-9 * # P ; , +
492         String phoneNumberPattern(L"[^0-9*#P,+;]");
493         RegularExpression checkPhoneNumber;
494         checkPhoneNumber.Construct(phoneNumberPattern);
495         //If there is any character other than these listed above then display invalid number
496         bool resultMatch = checkPhoneNumber.Match(phoneNumber,false);
497         //return false for patterns other than 0-9 * # P ; , +
498         return !resultMatch;
499
500 }
501
502 void
503 CallApp::SetTopMostWindow(bool bTopMost)
504 {
505         AppLogDebug("bTopMost = %d",bTopMost);
506         result res = E_FAILURE;
507         //ToDO: Need to see if there is better way to handle
508         //this case
509
510         if(bTopMost == true)
511         {
512                 GetAppFrame()->GetFrame()->SetZOrderGroup(WINDOW_Z_ORDER_GROUP_HIGHEST);
513                 if(PowerManager::IsScreenOn() == false)
514                 {
515                         AppLogDebug("TurnScreenOn");
516                         res = PowerManager::TurnScreenOn();
517                         AppLogDebug("TurnScreenOn %d",res);
518                 }
519                 AppManager::GetInstance()->AddActiveAppEventListener(*this);
520
521         }
522         else
523         {
524                 GetAppFrame()->GetFrame()->SetZOrderGroup(WINDOW_Z_ORDER_GROUP_NORMAL);
525                 PowerManager::KeepScreenOnState(false);
526                 AppManager::GetInstance()->RemoveActiveAppEventListener(*this);
527         }
528         //Unlock the phone if its locked
529 /*      if(LockManager::GetInstance()->IsLocked())
530         {
531                 AppLogDebug("Phone Locked");
532                 LockManager::GetInstance()->Unlock();
533         }*/
534
535 }
536 void
537 CallApp::OnActiveAppChanged(const String& appId)
538 {
539         AppLogDebug("Enter %ls",appId.GetPointer());
540         if(GetAppId().Equals(appId) == true)
541         {
542                 result res = PowerManager::KeepScreenOnState(true,false);
543                 AppLogDebug("KeepScreenOnState %d",res);
544
545
546         }
547
548 }