1.Soundmanger checkes added for Media Palyer return 2.Diabling join call if the membe...
[apps/osp/Call.git] / src / CallTelephonyManager.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.0 (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
7 //
8 //     http://floralicense.org/license/
9 //
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.
15 //
16
17 /**
18  * @file        CallTelephonyManager.cpp
19  * @brief       Call log item provider
20  */
21 #include <stdio.h>
22 #include <FBaseSys.h>
23 #include <FSystem.h>
24 #include <FTelephony.h>
25 #include "ITapiModem.h"
26 #include "ITapiSim.h"
27 #include "CallInfo.h"
28 #include "CallTelephonyManager.h"
29 #include "CallSettingsManager.h"
30 #include "CallTypes.h"
31
32 using namespace Tizen::Base;
33 using namespace Tizen::Graphics;
34 using namespace Tizen::Social;
35 using namespace Tizen::System;
36 using namespace Tizen::Base::Collection;
37 using namespace Tizen::Telephony;
38
39 const char* callEventList[] = {
40                 TAPI_NOTI_VOICE_CALL_STATUS_IDLE,
41                 TAPI_NOTI_VOICE_CALL_STATUS_ACTIVE,
42                 TAPI_NOTI_VOICE_CALL_STATUS_HELD,
43                 TAPI_NOTI_VOICE_CALL_STATUS_DIALING,
44                 TAPI_NOTI_VOICE_CALL_STATUS_ALERT,
45                 TAPI_NOTI_VOICE_CALL_STATUS_WAITING,
46                 TAPI_NOTI_CALL_INFO_CALL_CONNECTED_LINE,
47                 TAPI_NOTI_CALL_INFO_WAITING,
48                 TAPI_NOTI_CALL_INFO_CUG,
49                 TAPI_NOTI_CALL_INFO_FORWARDED,
50                 TAPI_NOTI_CALL_INFO_BARRED_INCOMING,
51                 TAPI_NOTI_CALL_INFO_BARRED_OUTGOING,
52                 TAPI_NOTI_CALL_INFO_DEFLECTED,
53                 TAPI_NOTI_CALL_INFO_CLIR_SUPPRESSION_REJECT,
54                 TAPI_NOTI_CALL_INFO_FORWARD_UNCONDITIONAL,
55                 TAPI_NOTI_CALL_INFO_FORWARD_CONDITIONAL,
56                 TAPI_NOTI_CALL_INFO_CALL_LINE_IDENTITY,
57                 TAPI_NOTI_CALL_INFO_CALL_NAME_INFORMATION,
58                 TAPI_NOTI_CALL_INFO_FORWARDED_CALL,
59                 TAPI_NOTI_CALL_INFO_CUG_CALL,
60                 TAPI_NOTI_CALL_INFO_DEFLECTED_CALL,
61                 TAPI_NOTI_CALL_INFO_TRANSFERED_CALL,
62                 TAPI_NOTI_CALL_INFO_HELD,
63                 TAPI_NOTI_CALL_INFO_ACTIVE,
64                 TAPI_NOTI_CALL_INFO_JOINED,
65                 TAPI_NOTI_CALL_INFO_RELEASED_ON_HOLD,
66                 TAPI_NOTI_CALL_INFO_TRANSFER_ALERT,
67                 TAPI_NOTI_CALL_INFO_TRANSFERED,
68                 TAPI_NOTI_CALL_INFO_CF_CHECK_MESSAGE,
69 };
70
71 TelephonyManager* TelephonyManager::__pManager = null;
72
73 TelephonyManager::TelephonyManager(ITelephonyEventListener* pEventListener)
74 : __pEventListener(pEventListener)
75 {
76         __pDialedCall = null;
77         __pIncomingCall = null;
78         __pActiveCallList = null;
79         __pSettingsManager = null;
80         __pTapiHandle = null;
81         __pAddressBook = null;
82         __pCachedContact = null;
83         __isMuted = false;
84         __isSpeakerOn = false;
85         __pSoundManager = null;
86         __pCalllogMgr = null;
87         __pNetworkManager = null;
88 }
89
90 TelephonyManager::~TelephonyManager(void)
91 {
92         if (__pActiveCallList != null)
93         {
94                 delete __pActiveCallList;
95         }
96
97         if (__pDialedCall != null)
98         {
99                 delete __pDialedCall;
100                 __pDialedCall = null;
101         }
102
103         if (__pIncomingCall != null)
104         {
105                 delete __pIncomingCall;
106                 __pIncomingCall = null;
107         }
108
109         if (__pAddressBook != null)
110         {
111                 delete __pAddressBook;
112                 __pAddressBook = null;
113         }
114         if (__pCachedContact != null)
115         {
116                 delete __pCachedContact;
117                 __pCachedContact = null;
118         }
119
120         //unregister for events from TAPI Lib.
121         UnregisterEvents();
122
123         //De-initialize the TAPI Library
124         if(__pTapiHandle != null)
125         {
126                 tel_deinit(__pTapiHandle);
127         }
128
129         if (__pSoundManager != null)
130         {
131                 delete __pSoundManager;
132         }
133
134         if (__pCalllogMgr != null)
135         {
136                 __pCalllogMgr = null;
137         }
138 }
139
140 TelephonyManager*
141 TelephonyManager::GetInstance(ITelephonyEventListener* pEventListener)
142 {
143         if (__pManager == null)
144         {
145                 CreateInstance(pEventListener);
146         }
147         return __pManager;
148 }
149
150 void
151 TelephonyManager::CreateInstance(ITelephonyEventListener* pEventListener)
152 {
153         __pManager = new (std::nothrow) TelephonyManager(pEventListener);
154         result r = __pManager->Initialize();
155         if (IsFailed(r))
156         {
157                 delete __pManager;
158                 __pManager = null;
159         }
160         atexit(&(TelephonyManager::DestroyInstance));
161 }
162
163 void
164 TelephonyManager::DestroyInstance(void)
165 {
166         if (__pManager != null)
167         {
168                 __pManager->EndAllCalls();
169                 delete __pManager;
170                 __pManager = null;
171         }
172 }
173
174 result
175 TelephonyManager::Initialize(void)
176 {
177         //Initialize telephony library
178         result r = InitializeTelephonyLibrary();
179         if (IsFailed(r))
180         {
181                 return r;
182         }
183         __pActiveCallList = new (std::nothrow) HashMapT<long, AppCallInfo>();
184         __pActiveCallList->Construct(IDI_MAX_ACTIVE_CALLS);
185
186         //Initialize the Settings Manager to fetch call settings
187         __pSettingsManager = SettingsManager::GetInstance();
188
189         __pSoundManager = new (std::nothrow) SoundManager();
190         __pCalllogMgr = CallLogManager::GetInstance();
191
192         //initialize address book to fetch contacts information
193         __pAddressBook = AddressbookManager::GetInstance()->GetAddressbookN();
194         if(__pAddressBook == null)
195         {
196                 return E_FAILURE;
197         }
198         __pNetworkManager = new NetworkManager();
199         if(__pNetworkManager != null)
200         {
201                 __pNetworkManager->Construct(this);
202         }
203         return r;
204 }
205
206 result
207 TelephonyManager::InitializeTelephonyLibrary(void)
208 {
209         result r = E_FAILURE;
210
211         __pTapiHandle = tel_init(null);
212         if (__pTapiHandle != null)
213         {
214                 //register telephony events
215                 int errorCode = RegisterEvents();
216                 if (errorCode == TAPI_CAUSE_SUCCESS)
217                 {
218                         r = E_SUCCESS;
219                 }
220         }
221         //TAPI Library is initialized and events registered successfully
222         return r;
223 }
224
225 int
226 TelephonyManager::RegisterEvents(void)
227 {
228         int errCode = -1;
229         int eventCount = sizeof(callEventList) / sizeof(char *);
230         for (int index = 0; index < eventCount; index++)
231         {
232                 errCode = tel_register_noti_event(__pTapiHandle, callEventList[index], &HandleCallback, this);
233                 if (errCode != TAPI_API_SUCCESS)
234                 {
235                         return errCode;
236                 }
237         }
238         return errCode;
239 }
240
241 void
242 TelephonyManager::UnregisterEvents(void)
243 {
244         int eventCount = sizeof(callEventList) / sizeof(char *);
245         for (int index = 0; index < eventCount; index++)
246         {
247                 tel_deregister_noti_event(__pTapiHandle, callEventList[index]);
248         }
249 }
250
251 ErrorCodes
252 TelephonyManager::SetupMoCall(String& contactNumber, bool isEmergency)
253 {
254         result res = E_SUCCESS;
255         //check if valid phone number exist
256         res = CheckValidTelePhoneNumber(contactNumber);
257         if (IsFailed(res))
258         {
259                 return ERROR_INVALID_NUMBER;
260         }
261
262         //if dialing an emergency call and active calls exist
263         //then end all active calls.
264         if (isEmergency && __pActiveCallList->GetCount() > 0)
265         {
266                 //Get first call handle
267                 AppCallInfo endCallInfo;
268                 IListT<AppCallInfo>* pCallList = __pActiveCallList->GetValuesN();
269                 pCallList->GetAt(0, endCallInfo);
270                 int callHandle = endCallInfo.GetCallHandle()->ToLong();
271                 delete pCallList;
272                 pCallList = null;
273
274                 //release all active or held calls
275                 int tapiRes = tel_end_call(__pTapiHandle, callHandle, TAPI_CALL_END_ALL, &HandleCallbackResponse, this);
276                 if (tapiRes == TAPI_CAUSE_SUCCESS)
277                 {
278                         __pActiveCallList->RemoveAll();
279                         res = E_SUCCESS;
280                 }
281                 else
282                 {
283                         res = E_FAILURE;
284                 }
285         }
286         else if (__pActiveCallList->GetCount() == 1)
287         {
288                 //Check if there is already an active call,
289                 //Put the already active call on hold.
290                 AppCallInfo holdCallInfo;
291                 IListT<AppCallInfo>* pCallList = __pActiveCallList->GetValuesN();
292
293                 pCallList->GetAt(0, holdCallInfo);
294                 //Check if call is active, then put on hold
295                 if (holdCallInfo.IsOnHold() == false)
296                 {
297                         res = HoldActiveCall(&holdCallInfo, true);
298                 }
299                 delete pCallList;
300                 pCallList = null;
301         }
302
303         //make the next call, only if any existing active call
304         //is successfully put on hold or is already on hold.
305         if (res == E_SUCCESS)
306         {
307                 res = DialOutgoingCall(contactNumber, isEmergency);
308         }
309
310         if(res == E_SUCCESS)
311         {
312                 return ERROR_NONE;
313         }
314         else
315         {
316                 return ERROR_TAPI_ERROR;
317         }
318 }
319
320 void
321 TelephonyManager::EndAllCalls(void)
322 {
323         if(__pDialedCall != null)
324         {
325                 if(__pDialedCall->GetCallHandle() != null)
326                 {
327                         tel_end_call(__pTapiHandle, __pDialedCall->GetCallHandle()->ToLong(), TAPI_CALL_END, &HandleCallbackResponse, this);
328                 }
329         }
330         tel_end_call(__pTapiHandle, -1, TAPI_CALL_END_HOLD_ALL, &HandleCallbackResponse, this);
331         //end all active calls before terminating application
332         tel_end_call(__pTapiHandle, -1, TAPI_CALL_END_ALL, &HandleCallbackResponse, this);
333         __pSettingsManager->SetCallState(CALL_STATE_CALL_OFF);
334 }
335
336 result
337 TelephonyManager::EndActiveCall(Long callHandle)
338 {
339         result r = E_FAILURE;
340
341         //fetch ended callInfo from active call list
342         AppCallInfo endCall;
343         r = __pActiveCallList->GetValue(callHandle.ToLong(), endCall);
344         if (r == E_SUCCESS)
345         {
346                 r = EndCall(endCall);
347         }
348         return r;
349 }
350
351 result
352 TelephonyManager::EndDialingCall(String& contactNumber)
353 {
354         result r = E_FAILURE;
355         //This is because for a dialing call, call handle is updated with some delay in telephony manager.
356         //And it is not available with outgoing call screen.
357         if (contactNumber.IsEmpty())
358         {
359                 return r;
360         }
361
362         //Check If Ended call matches Dialed Call.
363         AppCallInfo endCall;
364         if (__pDialedCall != null && __pDialedCall->GetContactNumber().Equals(contactNumber))
365         {
366                 endCall = *__pDialedCall;
367                 r = EndCall(endCall);
368         }
369         return r;
370 }
371
372 result
373 TelephonyManager::EndCall(AppCallInfo& endCallInfo)
374 {
375         result r = E_FAILURE;
376
377         if (endCallInfo.GetCallHandle() != null)
378         {
379                 unsigned int callHandle = endCallInfo.GetCallHandle()->ToLong();
380                 //end "dialed but unconnected" call or active call - processing to be handled in HandleIdleCallback().
381                 int res = tel_end_call(__pTapiHandle, callHandle, TAPI_CALL_END, &HandleCallbackResponse, this);
382                 if (res == TAPI_CAUSE_SUCCESS)
383                 {
384                         r = E_SUCCESS;
385                 }
386         }
387         return r;
388 }
389
390 result
391 TelephonyManager::AnswerAutoRejectCall(int callHandle)
392 {
393         AppLogDebug("Enter ");
394         result r = E_FAILURE;
395         TelCallAnswerType_t answerType = TAPI_CALL_ANSWER_ACCEPT;
396         int res = -1;
397
398         AppLogDebug("tel_answer_call");
399         answerType = TAPI_CALL_ANSWER_REJECT;
400         AppCallInfo rejectedCallInfo;
401         rejectedCallInfo = *(__pIncomingCall);
402         rejectedCallInfo.SetCalllogType(CALL_LOG_TYPE_VOICE_BLOCKED);
403         SaveCallInfoToLogsDb(rejectedCallInfo);
404         delete __pIncomingCall;
405         __pIncomingCall = null;
406         // redirect to reject call back handler as the flow has to be handled
407         res = tel_answer_call(__pTapiHandle, callHandle, answerType, &HandleCallbackResponse, this);
408
409         if (res == TAPI_CAUSE_SUCCESS)
410         {
411                 r = E_SUCCESS;
412         }
413         else
414         {
415                 r = E_FAILURE;
416         }
417         return r;
418 }
419
420 result
421 TelephonyManager::AnswerCall(int callHandle, bool acceptCall)
422 {
423         AppLogDebug("Enter %d",acceptCall);
424         result r = E_FAILURE;
425         __pSoundManager->StopAlert();
426         TelCallAnswerType_t answerType = TAPI_CALL_ANSWER_ACCEPT;
427         int res = -1;
428         if (acceptCall == true)
429         {
430                 answerType = TAPI_CALL_ANSWER_ACCEPT;
431                 // redirect to dummy call back handler as the flow already handled in registered event callback
432                 res = tel_answer_call(__pTapiHandle, callHandle, answerType, &HandleCallbackResponse, this);
433         }
434         else
435         {
436                 AppLogDebug("tel_answer_call");
437                 answerType = TAPI_CALL_ANSWER_REJECT;
438                 // redirect to reject call back handler as the flow has to be handled
439                 res = tel_answer_call(__pTapiHandle, callHandle, answerType, &HandleRejectCallbackResponse, this);
440         }
441
442         if (res == TAPI_CAUSE_SUCCESS)
443         {
444                 r = E_SUCCESS;
445         }
446         else
447         {
448                 r = E_FAILURE;
449         }
450         return r;
451 }
452
453 result
454 TelephonyManager::AcceptCall(CallAnsweringOptions answerOptions,int callHandle)
455 {
456         result r = E_FAILURE;
457         __pSoundManager->StopAlert();
458         __pSoundManager->SetSoundMode(SOUND_MODE_VOICE);
459
460         //Check if this is a new incoming call
461         if(__pIncomingCall == null || (callHandle != (unsigned int)__pIncomingCall->GetCallHandle()->ToLong()))
462         {
463                 //construct and fetch new Incoming call Info
464                 String incomingHandle;
465                 incomingHandle.Append(callHandle);
466                 AppCallInfo* pDuplicateCallInfo = FetchIncomingCallHandleN(incomingHandle, String(L""));
467                 if(pDuplicateCallInfo == null)
468                 {
469                         r = E_FAILURE;
470                         return r;
471                 }
472                 delete pDuplicateCallInfo;
473                 pDuplicateCallInfo = null;
474         }
475
476         switch(answerOptions)
477         {
478         case ANSERWING_OPTION_HOLD_SINGLE_CALL:
479         case ANSERWING_OPTION_END_SINGLE_CALL:
480         {
481                 r = AcceptSecondCall(answerOptions, callHandle);
482         }
483         break;
484
485         case ANSERWING_OPTION_REPLACE_ACTIVE_CALL:
486         case ANSERWING_OPTION_REPLACE_HELD_CALL:
487         case ANSERWING_OPTION_END_ALL_CALLS:
488         {
489                 r = AcceptMultipleCall(answerOptions, callHandle);
490         }
491         break;
492
493         default:
494                 break;
495         }
496         return r;
497 }
498
499 result
500 TelephonyManager::AcceptSecondCall(CallAnsweringOptions answerOptions, const int incomingCallHandle)
501 {
502         result r = E_FAILURE;
503
504         switch (answerOptions)
505         {
506         case ANSERWING_OPTION_HOLD_SINGLE_CALL:
507         {
508                 //accept incoming call by putting active call on Hold with 'TAPI_CALL_ANSWER_HOLD_AND_ACCEPT'
509                 int res = tel_answer_call(__pTapiHandle, incomingCallHandle, TAPI_CALL_ANSWER_HOLD_AND_ACCEPT, &HandleCallbackResponse, this);
510                 if (res != 0)
511                 {
512                         r = E_FAILURE;
513                         break;
514                 }
515
516                 //Call connected successfully
517                 r = E_SUCCESS;
518                 //update status of first call to "OnHold"
519                 IListT<AppCallInfo>* pCallList = __pActiveCallList->GetValuesN();
520                 AppCallInfo firstCallInfo;
521                 pCallList->GetAt(0, firstCallInfo);
522                 //replace old object with update AppCallInfo
523                 AppCallInfo* pHeldCallInfo = new (std::nothrow) AppCallInfo();
524                 *pHeldCallInfo = firstCallInfo;
525                 pHeldCallInfo->SetOnHold(true);
526                 __pActiveCallList->Remove(firstCallInfo.GetCallHandle()->ToLong());
527                 __pActiveCallList->Add(pHeldCallInfo->GetCallHandle()->ToLong(), *pHeldCallInfo);
528                 delete pCallList;
529                 pCallList = null;
530         }
531         break;
532
533         case ANSERWING_OPTION_END_SINGLE_CALL:
534         {
535                 //Transfer Old active calls to a separate list to avoid any processing in HandleIdleCallback().
536                 HashMapT<long, AppCallInfo>*  pEndCallsList = __pActiveCallList;
537                 //create a new ActiveCallList
538                 __pActiveCallList = new (std::nothrow) HashMapT<long, AppCallInfo>();
539                 __pActiveCallList->Construct(IDI_MAX_ACTIVE_CALLS);
540
541                 //accept call and reject all active calls with 'TAPI_CALL_ANSWER_REPLACE'
542                 int res = tel_answer_call(__pTapiHandle, incomingCallHandle, TAPI_CALL_ANSWER_REPLACE, &HandleCallbackResponse, this);
543                 if (res != 0)
544                 {
545                         r = E_FAILURE;
546                         //delete newly constructed list and gain ownership of old list
547                         delete __pActiveCallList;
548                         __pActiveCallList = pEndCallsList;
549                         break;
550                 }
551
552                 //Call connected successfully
553                 r = E_SUCCESS;
554                 //Add calls information to call log before deleting from active call list.
555                 IListT<AppCallInfo>* pCallList = pEndCallsList->GetValuesN();
556                 if(pCallList != null)
557                 {
558                         AppCallInfo endCallInfo;
559                         if (pCallList->GetAt(0, endCallInfo) == E_SUCCESS)
560                         {
561                                 SaveCallInfoToLogsDb(endCallInfo);
562                         }
563                         delete pCallList;
564                 }
565                 pEndCallsList->RemoveAll();
566                 delete pEndCallsList;
567         }
568         break;
569
570         default:
571                 break;
572         }
573         return r;
574 }
575
576 result
577 TelephonyManager::AcceptMultipleCall(CallAnsweringOptions answerOptions, const int incomingCallHandle)
578 {
579         result r = E_FAILURE;
580
581         switch (answerOptions)
582         {
583         case ANSERWING_OPTION_REPLACE_ACTIVE_CALL:
584         {
585                 //Replace "Active" call by incoming call and save ended call to call logs db.
586                 IListT<AppCallInfo>* pCallList = __pActiveCallList->GetValuesN();
587                 AppCallInfo callToBeEnded;
588                 pCallList->GetAt(0, callToBeEnded);
589                 //Check if the call is on "Hold", then fetch 2nd callInfo
590                 if (callToBeEnded.IsOnHold() == true)
591                 {
592                         pCallList->GetAt(1, callToBeEnded);
593                 }
594                 delete pCallList;
595                 pCallList = null;
596
597                 //remove "CallToBeEnded" from Active call list to avoid processing in HandleIdleCallback().
598                 __pActiveCallList->Remove(callToBeEnded.GetCallHandle()->ToLong());
599
600                 //Accept incoming call by End Active call with 'TAPI_CALL_ANSWER_REPLACE'
601                 int res = tel_answer_call(__pTapiHandle, incomingCallHandle, TAPI_CALL_ANSWER_REPLACE, &HandleCallbackResponse, this);
602                 if (res != 0)
603                 {
604                         r = E_FAILURE;
605                         //save the previous call back to active call list
606                         __pActiveCallList->Add(callToBeEnded.GetCallHandle()->ToLong(), callToBeEnded);
607                         break;
608                 }
609
610                 //Incoming Call connected successfully
611                 r = E_SUCCESS;
612                 //save to ended call to call logs db.
613                 SaveCallInfoToLogsDb(callToBeEnded);
614         }
615         break;
616
617         case ANSERWING_OPTION_REPLACE_HELD_CALL:
618         {
619                 //Replace "Held" call by incoming call and save ended call to call logs db.
620                 IListT<AppCallInfo>* pCallList = __pActiveCallList->GetValuesN();
621                 //"Held" call is to be ended
622                 AppCallInfo callToBeEnded;
623                 //"Active" call will be put on Hold
624                 AppCallInfo callToPutOnHold;
625                 pCallList->GetAt(0, callToBeEnded);
626                 //Check if the call is NOT "Held", then fetch 2nd callInfo
627                 if (callToBeEnded.IsOnHold() == false)
628                 {
629                         callToPutOnHold = callToBeEnded;
630                         pCallList->GetAt(1, callToBeEnded);
631                 }
632                 else
633                 {
634                         pCallList->GetAt(1, callToPutOnHold);
635                 }
636                 delete pCallList;
637                 pCallList = null;
638
639                 //remove "CallToBeEnded" from Active call list to avoid processing in HandleIdleCallback().
640                 __pActiveCallList->Remove(callToBeEnded.GetCallHandle()->ToLong());
641
642                 //End "Held" Call using 'TAPI_CALL_END'.
643                 int res = -1;
644                 if (callToBeEnded.IsConferenceCall() == false)
645                 {
646                         res = tel_end_call(__pTapiHandle, callToBeEnded.GetCallHandle()->ToLong(), TAPI_CALL_END, &HandleCallbackResponse, this);
647                 }
648                 else
649                 {
650                         IListT<AppCallInfo>* pParticipantsInfo = callToBeEnded.GetCallerList();
651                         //need to end every participant individually for conference call
652                         for (int index = 0; index < pParticipantsInfo->GetCount(); index++)
653                         {
654                                 AppCallInfo memberCallInfo;
655                                 pParticipantsInfo->GetAt(index, memberCallInfo);
656                                 res = tel_end_call(__pTapiHandle, memberCallInfo.GetCallHandle()->ToLong(), TAPI_CALL_END, &HandleCallbackResponse, this);
657                         }
658                 }
659
660                 if (res != 0)
661                 {
662                         r = E_FAILURE;
663                         //save the previous "callToBeEnded" call back to active call list
664                         __pActiveCallList->Add(callToBeEnded.GetCallHandle()->ToLong(), callToBeEnded);
665                         break;
666                 }
667                 //"Held" call successfully ended - Add call ended to call log database
668                 SaveCallInfoToLogsDb(callToBeEnded);
669
670                 //accept incoming call by Holding "Active" call using "TAPI_CALL_ANSWER_HOLD_AND_ACCEPT".
671                 res = tel_answer_call(__pTapiHandle, incomingCallHandle, TAPI_CALL_ANSWER_HOLD_AND_ACCEPT, &HandleCallbackResponse, this);
672                 if (res != 0)
673                 {
674                         r = E_FAILURE;
675                         break;
676                 }
677
678                 //Call connected successfully and active call is "Onhold"
679                 r = E_SUCCESS;
680                 //replace old object with update CallInfo
681                 AppCallInfo* pHeldCallInfo = new (std::nothrow) AppCallInfo();
682                 *pHeldCallInfo = callToPutOnHold;
683                 pHeldCallInfo->SetOnHold(true);
684                 __pActiveCallList->Remove(callToPutOnHold.GetCallHandle()->ToLong());
685                 __pActiveCallList->Add(pHeldCallInfo->GetCallHandle()->ToLong(), *pHeldCallInfo);
686         }
687         break;
688
689         case ANSERWING_OPTION_END_ALL_CALLS:
690         {
691                 //End all active and Held calls after saving to call log. Incoming call is automatically accepted by TAPI engine,
692                 // and processing of Incoming call is handled in HandleActiveCallback().
693
694                 //Transfer Old active calls to a separate list to avoid any processing in HandleIdleCallback().
695                 HashMapT<long, AppCallInfo>*  pEndCallsList = __pActiveCallList;
696                 //create a new ActiveCallList
697                 __pActiveCallList = new (std::nothrow) HashMapT<long, AppCallInfo>();
698                 __pActiveCallList->Construct(IDI_MAX_ACTIVE_CALLS);
699
700                 //End all active calls and all hold calls
701                 int res = tel_end_call(__pTapiHandle, -1, TAPI_CALL_END_ACTIVE_ALL, &HandleCallbackResponse, this);
702                 if(res == 0)
703                 {
704                         res = tel_end_call(__pTapiHandle, -1, TAPI_CALL_END_HOLD_ALL, &HandleCallbackResponse, this);
705                 }
706
707                 if (res != 0)
708                 {
709                         r = E_FAILURE;
710                         //delete newly constructed list and gain ownership of old list
711                         delete __pActiveCallList;
712                         __pActiveCallList = pEndCallsList;
713                         break;
714                 }
715
716                 //all calls ended successfully, Incoming call is automatically accepted.
717                 r = E_SUCCESS;
718
719                 //Add calls information to call log before deleting from active call list.
720                 IListT<AppCallInfo>* pCallList = pEndCallsList->GetValuesN();
721                 if(pCallList != null)
722                 {
723                         int callCount = pCallList->GetCount();
724                         for (int index = 0; index < callCount; index++)
725                         {
726                                 AppCallInfo endCallInfo;
727                                 if (pCallList->GetAt(index, endCallInfo) == E_SUCCESS)
728                                 {
729                                         SaveCallInfoToLogsDb(endCallInfo);
730                                 }
731                         }
732                         delete pCallList;
733                         pCallList = null;
734                 }
735                 pEndCallsList->RemoveAll();
736                 delete pEndCallsList;
737                 pEndCallsList = null;
738         }
739         break;
740
741         default:
742                 break;
743         }
744         return r;
745 }
746
747 result
748 TelephonyManager::HoldCall(Tizen::Base::Long callHandle, bool holdCall)
749 {
750         result r = E_SUCCESS;
751         //Check if there are any existing active calls
752         if (__pActiveCallList->GetCount())
753         {
754                 IListT<AppCallInfo>* pCallList = __pActiveCallList->GetValuesN();
755                 int callCount = pCallList->GetCount();
756                 for (int index = 0; index < callCount; index++)
757                 {
758                         AppCallInfo holdCallInfo;
759
760                         r = pCallList->GetAt(index, holdCallInfo);
761                         //check if an active call is found with matching contact no.
762                         if ((r == E_SUCCESS) && (holdCallInfo.GetCallHandle()->Equals(callHandle)))
763                         {
764                                 r = HoldActiveCall(&holdCallInfo, holdCall);
765                                 break;
766                         }
767                 }
768                 delete pCallList;
769                 pCallList = null;
770         }
771
772         return r;
773 }
774
775 result
776 TelephonyManager::EndConferenceCall(void)
777 {
778         result r = E_FAILURE;
779         //fetch conference callInfo to end
780         AppCallInfo confCallToEnd;
781         bool isConferenceCallFound = false;
782
783         IListT<AppCallInfo>* pCallList = __pActiveCallList->GetValuesN();
784         int callCount = pCallList->GetCount();
785         for (int index = 0; index < callCount; index++)
786         {
787                 pCallList->GetAt(index, confCallToEnd);
788                 if (confCallToEnd.IsConferenceCall() == true)
789                 {
790                         isConferenceCallFound = true;
791                         break;
792                 }
793         }
794         delete pCallList;
795         pCallList = null;
796
797         if (isConferenceCallFound == true)
798         {
799                 //End conference call
800                 TelCallEndType_t callEndType = TAPI_CALL_END_ACTIVE_ALL;
801                 if (confCallToEnd.IsOnHold() == true)
802                 {
803                         callEndType = TAPI_CALL_END_HOLD_ALL;
804                 }
805                 int res = tel_end_call(__pTapiHandle, confCallToEnd.GetCallHandle()->ToLong(), callEndType, &HandleEndConferenceCallbackResponse, this);
806                 if (res == TAPI_CAUSE_SUCCESS)
807                 {
808                         r = E_SUCCESS;
809                 }
810         }
811         return r;
812 }
813
814 result
815 TelephonyManager::HoldConferenceCall(bool holdCall)
816 {
817         result r = E_FAILURE;
818         int confCallIndex = -1;
819         AppCallInfo confCallToHold;
820         bool isConferenceCallFound = false;
821
822         IListT<AppCallInfo>* pCallList = __pActiveCallList->GetValuesN();
823         int confCallCount = pCallList->GetCount();
824
825         for (int index = 0; index < confCallCount; index++)
826         {
827                 pCallList->GetAt(index, confCallToHold);
828                 if (confCallToHold.IsConferenceCall() == true)
829                 {
830                         isConferenceCallFound = true;
831                         confCallIndex = index;
832                         //Found the Conference call to be ended.
833                         break;
834                 }
835         }
836
837         if (isConferenceCallFound == false)
838         {
839                 delete pCallList;
840                 pCallList = null;
841                 return r;
842         }
843
844         unsigned int callHandle = confCallToHold.GetCallHandle()->ToLong();
845         int res = TAPI_API_INVALID_INPUT;
846         if (holdCall == true)
847         {
848                 res = tel_hold_call(__pTapiHandle, callHandle, &HandleCallbackResponse, this);
849         }
850         else
851         {
852                 res = tel_active_call(__pTapiHandle, callHandle, &HandleCallbackResponse, this);
853         }
854         if (res == TAPI_API_SUCCESS)
855         {
856                 r = E_SUCCESS;
857                 if (holdCall == true)
858                 {
859                         confCallToHold.SetOnHold(true);
860                 }
861                 else
862                 {
863                         confCallToHold.SetOnHold(false);
864                 }
865                 AppCallInfo* pConfCallInfo = new (std::nothrow) AppCallInfo();
866                 *pConfCallInfo = confCallToHold;
867                 __pActiveCallList->Remove(callHandle);
868                 __pActiveCallList->Add(callHandle, *pConfCallInfo);
869         }
870         else
871         {
872                 r = E_FAILURE;
873         }
874
875         delete pCallList;
876         pCallList = null;
877         return r;
878 }
879
880 result
881 TelephonyManager::JoinCall(void)
882 {
883         result r = E_FAILURE;
884         int res = -1;
885         AppCallInfo activeCall;
886         AppCallInfo heldCall;
887         // Use enumerator to access elements in the map
888         IListT<AppCallInfo>* pCallList = __pActiveCallList->GetValuesN();
889         r = pCallList->GetAt(0, activeCall);
890
891         if (r == E_SUCCESS)
892         {
893                 r = pCallList->GetAt(1, heldCall);
894                 if (r == E_SUCCESS)
895                 {
896                         unsigned int activeCallHandle = activeCall.GetCallHandle()->ToLong();
897                         unsigned int heldCallHandle = heldCall.GetCallHandle()->ToLong();
898
899                         //Check if participants in conference call are under limit.
900                         if ((heldCall.IsConferenceCall() == true) && (heldCall.GetCallerListCount() < IDI_MAX_CONF_CALL_PARTICIPANTS))
901                         {
902                                 res = tel_join_call(__pTapiHandle, heldCallHandle, activeCallHandle, &HandleJoinCallbackResponse, this);
903                         }
904                         else if (activeCall.GetCallerListCount() < IDI_MAX_CONF_CALL_PARTICIPANTS)
905                         {
906                                 res = tel_join_call(__pTapiHandle, activeCallHandle, heldCallHandle, &HandleJoinCallbackResponse, this);
907                         }
908                 }
909         }
910         delete pCallList;
911         pCallList = null;
912         if (res == TAPI_API_SUCCESS)
913         {
914                 r = E_SUCCESS;
915         }
916         else
917         {
918                 r = E_FAILURE;
919         }
920         return r;
921 }
922
923 result
924 TelephonyManager::HoldActiveCall(AppCallInfo* pActiveCallInfo, bool holdCall)
925 {
926         unsigned int callHandle = pActiveCallInfo->GetCallHandle()->ToLong();
927         int retStatus = -1;
928         if (holdCall == true)
929         {
930                 retStatus = tel_hold_call(__pTapiHandle, callHandle, &HandleCallbackResponse, this);
931         }
932         else
933         {
934                 retStatus = tel_active_call(__pTapiHandle, callHandle, &HandleCallbackResponse, this);
935         }
936
937         if (retStatus == TAPI_CAUSE_SUCCESS)
938         {
939                 AppCallInfo* pHeldCallInfo = new (std::nothrow) AppCallInfo();
940                 //copy state into new callinfo object
941                 *pHeldCallInfo = *pActiveCallInfo;
942
943                 //set call to hold state
944                 pHeldCallInfo->SetOnHold(holdCall);
945
946                 __pActiveCallList->Remove(callHandle);
947                 //replace old object with new
948                 __pActiveCallList->Add(callHandle, *pHeldCallInfo);
949                 return E_SUCCESS;
950         }
951         else
952         {
953                 return E_FAILURE;
954         }
955 }
956
957 result
958 TelephonyManager::DialOutgoingCall(String& contactNumber, bool isEmergency)
959 {
960         TelCallDial_t structDialCall;
961
962         AppLogDebug("Enter %ls",contactNumber.GetPointer());
963         //Temp String to replace , with P and ; with W
964         String TempContactNum;
965         TempContactNum.Append(contactNumber);
966         TempContactNum.Replace(L",",L"W");
967         TempContactNum.Replace(L";",L",");
968         //conversion "contactNumber" to char*
969         const wchar_t* pContact = TempContactNum.GetPointer();
970         int len = TempContactNum.GetLength()+1;
971         char* pNumber = new (std::nothrow) char[len];
972         wcstombs(pNumber, pContact, len);
973
974
975         //initialize request parameter
976         memset(&structDialCall, '\0', sizeof(TelCallDial_t));
977         memcpy(structDialCall.szNumber, pNumber, strlen(pNumber));
978         AppLogDebug("%s",structDialCall.szNumber);
979         if(isEmergency == true)
980         {
981                 structDialCall.CallType = TAPI_CALL_TYPE_E911;
982         }
983         else
984         {
985                 structDialCall.CallType = TAPI_CALL_TYPE_VOICE;
986         }
987
988         int res = tel_dial_call(__pTapiHandle, &structDialCall, &HandleDialCallbackResponse, this);
989         if (__pSoundManager == null)
990         {
991                 __pSoundManager = new (std::nothrow) SoundManager();
992         }
993         __pSoundManager->StartSession();
994         delete[] pNumber;
995         pNumber = null;
996
997         if (res == TAPI_CAUSE_SUCCESS)
998         {
999                 if (__pDialedCall != null)
1000                 {
1001                         delete __pDialedCall;
1002                         __pDialedCall = null;
1003                 }
1004                 __pDialedCall = new (std::nothrow) AppCallInfo();
1005                 __pDialedCall->SetContactNumber(contactNumber);
1006                 __pDialedCall->SetEmergency(isEmergency);
1007                 result r = FetchContactInfoForNumber(contactNumber);
1008                 if (!IsFailed(r))
1009                 {
1010                         __pDialedCall->SetContactInfo(*__pCachedContact);
1011                 }
1012                 return E_SUCCESS;
1013         }
1014         else
1015         {
1016                 return E_FAILURE;
1017         }
1018 }
1019
1020 result
1021 TelephonyManager::SwapCalls(void)
1022 {
1023         result r = E_FAILURE;
1024
1025         //check if there are atleast 2 active calls
1026         if (__pActiveCallList->GetCount() == IDI_MAX_ACTIVE_CALLS)
1027         {
1028                 int retStatus = 0;
1029
1030                 //fetch call handles
1031                 IListT<long>* pCallHandleList = __pActiveCallList->GetKeysN();
1032                 long callHandle1 = 0;
1033                 pCallHandleList->GetAt(0, callHandle1);
1034                 long callHandle2 = 0;
1035                 pCallHandleList->GetAt(1, callHandle2);
1036
1037                 retStatus = tel_swap_call(__pTapiHandle, callHandle1, callHandle2, &HandleSwapCallbackResponse, this);
1038
1039                 if (retStatus == TAPI_CAUSE_SUCCESS)
1040                 {
1041                         r = E_SUCCESS;
1042                 }
1043                 delete pCallHandleList;
1044                 pCallHandleList = null;
1045         }
1046
1047         return r;
1048 }
1049
1050 result
1051 TelephonyManager::SendCallDTMF(String& textToBeSent)
1052 {
1053         result r = E_FAILURE;
1054         //check if there is an active Call
1055         if (__pActiveCallList->GetCount() > 0)
1056         {
1057                 //conversion "textToBeSent" to char*
1058                 const wchar_t* pTextToBeSent = textToBeSent.GetPointer();
1059                 int len = textToBeSent.GetLength() + 1;
1060                 char* pNumber = new (std::nothrow) char[len];
1061                 wcstombs(pNumber, pTextToBeSent, len);
1062                 int retStatus = tel_call_dtmf(__pTapiHandle, pNumber, &HandleCallbackResponse, this);
1063                 delete []pNumber;
1064                 pNumber = null;
1065                 if (retStatus == TAPI_CAUSE_SUCCESS)
1066                 {
1067                         r = E_SUCCESS;
1068                 }
1069         }
1070         return r;
1071 }
1072
1073 result
1074 TelephonyManager::EndFromConference(int callHandle)
1075 {
1076         result r = E_FAILURE;
1077         int confCallIndex = -1;
1078         AppCallInfo endConfCall;
1079         bool isConferenceCallFound = false;
1080
1081         IListT<AppCallInfo>* pCallList = __pActiveCallList->GetValuesN();
1082         int callCount = pCallList->GetCount();
1083         for (int index = 0; index < callCount; index++)
1084         {
1085                 pCallList->GetAt(index, endConfCall);
1086                 if (endConfCall.IsConferenceCall() == true)
1087                 {
1088                         isConferenceCallFound = true;
1089                         confCallIndex = index;
1090                         //Found the Conference call to be ended.
1091                         break;
1092                 }
1093         }
1094
1095         delete pCallList;
1096         pCallList = null;
1097
1098         if (isConferenceCallFound == false)
1099         {
1100                 //no conference call found
1101                 return r;
1102         }
1103
1104         //Identify the call to be ended and remove from list on API success
1105         AppCallInfo callToBeEnded;
1106         IListT<AppCallInfo>* pParticipantList = endConfCall.GetCallerList();
1107         int participantCount = pParticipantList->GetCount();
1108         for (int index = 0; index < participantCount; index++)
1109         {
1110                 pParticipantList->GetAt(index, callToBeEnded);
1111                 if (callToBeEnded.GetCallHandle()->ToLong() == callHandle)
1112                 {
1113                         //Identify the call to be ended and remove from list on API success
1114                         TelCallEndType_t endType = TAPI_CALL_END;
1115
1116                         int res = tel_end_call(__pTapiHandle, callHandle, endType, &HandleEndFromConferenceCallbackResponse, this);
1117                         if (res == TAPI_CAUSE_SUCCESS)
1118                         {
1119                                 r = E_SUCCESS;
1120                         }
1121                         else
1122                         {
1123                                 r = E_FAILURE;
1124                         }
1125                         break;
1126                 }
1127         }
1128
1129         return r;
1130 }
1131
1132 result
1133 TelephonyManager::SplitFromConference(int callHandle)
1134 {
1135         result r = E_FAILURE;
1136         int confCallIndex = -1;
1137         AppCallInfo endConfCall;
1138         bool isConferenceCallFound = false;
1139
1140         IListT<AppCallInfo>* pCallList = __pActiveCallList->GetValuesN();
1141         int callCount = pCallList->GetCount();
1142         for (int index = 0; index < callCount; index++)
1143         {
1144                 pCallList->GetAt(index, endConfCall);
1145                 if (endConfCall.IsConferenceCall() == true)
1146                 {
1147                         isConferenceCallFound = true;
1148                         confCallIndex = index;
1149                         //Found the Conference call to be ended.
1150                         break;
1151                 }
1152         }
1153         delete pCallList;
1154         pCallList = null;
1155         if (isConferenceCallFound == false)
1156         {
1157                 //no conference call found
1158                 return r;
1159         }
1160
1161         //Identify the call to be ended and remove from list on API success
1162         AppCallInfo callToBeEnded;
1163         pCallList = endConfCall.GetCallerList();
1164         callCount = pCallList->GetCount();
1165         for (int index = 0; index < callCount; index++)
1166         {
1167                 pCallList->GetAt(index, callToBeEnded);
1168                 if (callToBeEnded.GetCallHandle()->ToLong() == callHandle)
1169                 {
1170                         int res = tel_split_call(__pTapiHandle, callHandle, &HandleSplitFromConferenceCallbackResponse, this);
1171                         if (res == TAPI_CAUSE_SUCCESS)
1172                         {
1173                                 r = E_SUCCESS;
1174                         }
1175                         else
1176                         {
1177                                 r = E_FAILURE;
1178                         }
1179                         break;
1180                 }
1181         }
1182         return r;
1183 }
1184
1185 result
1186 TelephonyManager::SetMuteStatus(bool setMute)
1187 {
1188         TelSoundMuteStatus_t muteStatus;
1189         result r = E_FAILURE;
1190         if (setMute == true)
1191         {
1192                 muteStatus = TAPI_SOUND_MUTE_STATUS_ON;
1193         }
1194         else
1195         {
1196                 muteStatus = TAPI_SOUND_MUTE_STATUS_OFF;
1197         }
1198         int res = tel_set_call_mute_status(__pTapiHandle, muteStatus, &HandleCallbackResponse, this);
1199         if (res == TAPI_CAUSE_SUCCESS)
1200         {
1201                 __isMuted = setMute;
1202                 r = E_SUCCESS;
1203         }
1204         else
1205         {
1206                 r = E_FAILURE;
1207         }
1208         return r;
1209 }
1210
1211 bool
1212 TelephonyManager::IsCallMuted(void)
1213 {
1214         return __isMuted;
1215 }
1216
1217 result
1218 TelephonyManager::SetSpeakerStatus(bool setSpeaker)
1219 {
1220         result r = E_FAILURE;
1221         TelCallSoundPathInfo_t callSoundPathInfo;
1222         __pSoundManager->SetSpeakerStatus(setSpeaker);
1223         if (setSpeaker == true)
1224         {
1225                 callSoundPathInfo.path = TAPI_SOUND_PATH_SPK_PHONE;
1226         }
1227         else
1228         {
1229                 callSoundPathInfo.path = TAPI_SOUND_PATH_HANDSET;
1230         }
1231         callSoundPathInfo.ex_volume = TelCallSoundPathInfo_t::TAPI_SOUND_EX_VOLUME_ON;
1232
1233         int res = tel_set_call_sound_path(__pTapiHandle, &callSoundPathInfo, &HandleCallbackResponse, this);
1234
1235         if (res == TAPI_CAUSE_SUCCESS)
1236         {
1237                 __isSpeakerOn = setSpeaker;
1238                 r = E_SUCCESS;
1239         }
1240         else
1241         {
1242                 r = E_FAILURE;
1243         }
1244         return r;
1245 }
1246
1247 bool
1248 TelephonyManager::IsSpeakerOn(void)
1249 {
1250         return __isSpeakerOn;
1251 }
1252
1253 bool
1254 TelephonyManager::IsSplitAllowed(void)
1255 {
1256         // Split functionality is allowed only if a one call is present.
1257         // The call can be a single call or a conference call
1258         if (__pActiveCallList->GetCount() == 1)
1259         {
1260                 return true;
1261         }
1262         return false;
1263 }
1264
1265 void
1266 TelephonyManager::HandleCallbackResponse(TapiHandle* pHandle, int callBackResult, void* pData, void* pUserData)
1267 {
1268         //should not do anything here.
1269 }
1270
1271 void
1272 TelephonyManager::HandleDialCallbackResponse(TapiHandle* pHandle, int callBackResult, void* pData, void* pUserData)
1273 {
1274         TelephonyManager* pTelManager = (TelephonyManager*) pUserData;
1275         if (callBackResult != TAPI_CAUSE_SUCCESS)
1276         {
1277                 if (pTelManager->__pDialedCall != null)
1278                 {
1279                         delete pTelManager->__pDialedCall;
1280                         pTelManager->__pDialedCall = null;
1281                 }
1282                 pTelManager->__pEventListener->HandleTelephonyError(ERROR_DIAL_FAILED);
1283         }
1284 }
1285
1286 void
1287 TelephonyManager::HandleRejectCallbackResponse(TapiHandle* pHandle, int callBackResult, void* pData, void* pUserData)
1288 {
1289         AppLogDebug("ENTER");
1290         // This callback comes only if user has either rejected an incoming call from IncomingCallForm.
1291         // or the incoming call was automatically blocked.
1292         TelephonyManager* pTelManager = (TelephonyManager*) pUserData;
1293         if (pData != null && callBackResult == TAPI_API_SUCCESS)
1294         {
1295                 unsigned int rejectedCallHandle = 0;
1296                 memcpy(&rejectedCallHandle, pData, sizeof(TS_UINT));
1297
1298                 //Check if incoming call is rejected
1299                 if (pTelManager->__pIncomingCall != null && (rejectedCallHandle == (unsigned int) pTelManager->__pIncomingCall->GetCallHandle()->ToLong()))
1300                 {
1301                         AppCallInfo rejectedCallInfo;
1302                         rejectedCallInfo = *(pTelManager->__pIncomingCall);
1303                         delete pTelManager->__pIncomingCall;
1304                         pTelManager->__pIncomingCall = null;
1305
1306                         //Check if number was automatically rejected using settings, then don't give any notification to user.
1307                         bool showIncomingCallRejectedNotification = true;
1308                         if (((pTelManager->__pSettingsManager->GetUnknownRejectStatus() == true) && (rejectedCallInfo.GetContactInfo() == null))
1309                                                 || (pTelManager->__pSettingsManager->IsCallToBeRejected(rejectedCallInfo.GetContactNumber()) == true))
1310                         {
1311                                 //blocked
1312                                 AppLogDebug("Call blocked");
1313                                 showIncomingCallRejectedNotification = false;
1314                                 rejectedCallInfo.SetCalllogType(CALL_LOG_TYPE_VOICE_BLOCKED);
1315                         }
1316                         else
1317                         {
1318                                 AppLogDebug("Call rejected");
1319                                 //rejected by user from incoming call form
1320                                 showIncomingCallRejectedNotification = true;
1321                                 rejectedCallInfo.SetCalllogType(CALL_LOG_TYPE_VOICE_REJECTED);
1322                         }
1323                         //Save rejected incoming call to call log db.
1324                         pTelManager->SaveCallInfoToLogsDb(rejectedCallInfo);
1325
1326                         if (showIncomingCallRejectedNotification == true)
1327                         {
1328                                 //check if the ended call was the last call
1329                                 bool isLastCall = (pTelManager->__pActiveCallList->GetCount() == 0);
1330                                 //Stop alert - started only for incoming calls which are not blocked.
1331                                 if(pTelManager->__pSoundManager != null)
1332                                 {
1333                                         pTelManager->__pSoundManager->StopAlert();
1334                                         //Do not call stop session if there is already a call going on
1335                                         if(isLastCall == true)
1336                                         {
1337                                                 pTelManager->__pSoundManager->StopSession();
1338                                         }
1339                                 }
1340                                 //Send notification to user
1341                                 ArrayListT<AppCallInfo>* pCallList = null;
1342                                 if (isLastCall)
1343                                 {
1344                                         //save 'RejectedCall' to list to show on EndCallForm
1345                                         pCallList = new (std::nothrow) ArrayListT<AppCallInfo>();
1346                                         pCallList->Construct(1);
1347                                         AppCallInfo* pRejectedCall = new (std::nothrow) AppCallInfo();
1348                                         *pRejectedCall = rejectedCallInfo;
1349                                         pCallList->Add(*pRejectedCall);
1350                                 }
1351                                 else
1352                                 {
1353                                         //fetch active calls to show appropriate scene
1354                                         pCallList = static_cast<ArrayListT<AppCallInfo>*>(pTelManager->__pActiveCallList->GetValuesN());
1355                                 }
1356                                 pTelManager->__pEventListener->HandleCallDisconnected(isLastCall, *pCallList);
1357                                 delete pCallList;
1358                                 pCallList = null;
1359                         }
1360                 }
1361         }
1362         else
1363         {
1364                 pTelManager->__pEventListener->HandleTelephonyError(ERROR_REJECT_FAILED);
1365         }
1366         AppLogDebug("EXIT");
1367 }
1368
1369 void
1370 TelephonyManager::HandleJoinCallbackResponse(TapiHandle* pHandle, int callBackResult, void* pData, void* pUserData)
1371 {
1372         TelephonyManager* pTelManager = (TelephonyManager*) pUserData;
1373         if (callBackResult == TAPI_API_SUCCESS && pData != null)
1374         {
1375                 unsigned int tempHandle = 0;
1376                 TelCallInfoJoinedNoti_t joinedInfoNotification;
1377                 AppCallInfo confCallInfo;
1378
1379                 memcpy(&tempHandle, pData, sizeof(TS_UINT));
1380                 joinedInfoNotification.id = tempHandle;
1381                 AppCallInfo activeCall;
1382                 AppCallInfo heldCall;
1383                 // Use enumerator to access elements in the map
1384                 IListT<AppCallInfo>* pCallList = pTelManager->__pActiveCallList->GetValuesN();
1385                 result r = pCallList->GetAt(0, activeCall);
1386
1387                 if (r == E_SUCCESS)
1388                 {
1389                         r = pCallList->GetAt(1, heldCall);
1390                         if (r == E_SUCCESS)
1391                         {
1392                                 AppCallInfo* pConfCallInfo = new (std::nothrow) AppCallInfo();
1393                                 unsigned int activeCallHandle = activeCall.GetCallHandle()->ToLong();
1394                                 unsigned int heldCallHandle = heldCall.GetCallHandle()->ToLong();
1395                                 if (activeCall.IsConferenceCall() == true)
1396                                 {
1397                                         r = E_SUCCESS;
1398                                         //When joined both become active
1399                                         activeCall.SetOnHold(false);
1400                                         heldCall.SetOnHold(false);
1401                                         *pConfCallInfo = activeCall;
1402                                         pConfCallInfo->AddCallToCallerList(heldCall);
1403                                         pConfCallInfo->SetCallHandle(activeCallHandle);
1404                                         //Set call start time
1405                                         if (pConfCallInfo->GetCallConnectTime() > heldCall.GetCallConnectTime())
1406                                         {
1407                                                 pConfCallInfo->SetCallConnectTime(heldCall.GetCallConnectTime());
1408                                                 pConfCallInfo->SetCallNotificationTime(heldCall.GetCallNotificationTime());
1409                                         }
1410                                 }
1411                                 else if (heldCall.IsConferenceCall() == true)
1412                                 {
1413                                         r = E_SUCCESS;
1414                                         heldCall.SetOnHold(false);
1415                                         activeCall.SetOnHold(false);
1416                                         *pConfCallInfo = heldCall;
1417                                         pConfCallInfo->AddCallToCallerList(activeCall);
1418                                         pConfCallInfo->SetCallHandle(heldCallHandle);
1419                                         //Set call start time
1420                                         if (pConfCallInfo->GetCallConnectTime() > activeCall.GetCallConnectTime())
1421                                         {
1422                                                 pConfCallInfo->SetCallConnectTime(activeCall.GetCallConnectTime());
1423                                                 pConfCallInfo->SetCallNotificationTime(activeCall.GetCallNotificationTime());
1424                                         }
1425                                 }
1426                                 else
1427                                 {
1428                                         r = E_SUCCESS;
1429                                         pConfCallInfo->SetConference(true);
1430                                         heldCall.SetOnHold(false);
1431                                         activeCall.SetOnHold(false);
1432                                         pConfCallInfo->AddCallToCallerList(activeCall);
1433                                         pConfCallInfo->AddCallToCallerList(heldCall);
1434                                         pConfCallInfo->SetCallHandle(activeCallHandle);
1435                                         //Set call start time
1436                                         if (activeCall.GetCallConnectTime() > heldCall.GetCallConnectTime())
1437                                         {
1438                                                 pConfCallInfo->SetCallConnectTime(heldCall.GetCallConnectTime());
1439                                                 pConfCallInfo->SetCallNotificationTime(heldCall.GetCallNotificationTime());
1440                                         }
1441                                         else
1442                                         {
1443                                                 pConfCallInfo->SetCallConnectTime(activeCall.GetCallConnectTime());
1444                                                 pConfCallInfo->SetCallNotificationTime(activeCall.GetCallNotificationTime());
1445                                         }
1446                                 }
1447                                 pConfCallInfo->SetCallHandle(joinedInfoNotification.id);
1448                                 pTelManager->__pActiveCallList->RemoveAll();
1449                                 //only one call in the list
1450                                 pTelManager->__pActiveCallList->Add(joinedInfoNotification.id, *pConfCallInfo);
1451                                 //notify listener that call is connected.
1452                                 pTelManager->__pEventListener->HandleConferenceCall(*pConfCallInfo);
1453                                 delete pCallList;
1454                                 pCallList = null;
1455                         }
1456                 }
1457         }
1458         else
1459         {
1460                 pTelManager->__pEventListener->HandleTelephonyError(ERROR_JOIN_FAILED);
1461         }
1462 }
1463
1464 void
1465 TelephonyManager::HandleSwapCallbackResponse(TapiHandle* pHandle, int callBackResult, void* pData, void* pUserData)
1466 {
1467         TelephonyManager* pTelManager = (TelephonyManager*) pUserData;
1468         if (callBackResult == TAPI_CAUSE_SUCCESS)
1469         {
1470                 IListT<AppCallInfo>* pCallList = pTelManager->__pActiveCallList->GetValuesN();
1471                 IListT<long>* pKeyList = pTelManager->__pActiveCallList->GetKeysN();
1472                 int callCount = pTelManager->__pActiveCallList->GetCount();
1473                 for (int index = 0; index < callCount; index++)
1474                 {
1475                         AppCallInfo* pTempCallInfo = new (std::nothrow) AppCallInfo();
1476                         pCallList->GetAt(index, *pTempCallInfo);
1477                         (pTempCallInfo->IsOnHold() == false) ? pTempCallInfo->SetOnHold(true) : pTempCallInfo->SetOnHold(false);
1478                         long callHandle;
1479                         pKeyList->GetAt(index, callHandle);
1480                         pTelManager->__pActiveCallList->SetValue(callHandle, *pTempCallInfo);
1481                 }
1482                 delete pCallList;
1483                 pCallList = null;
1484                 delete pKeyList;
1485                 pKeyList = null;
1486                 pCallList = pTelManager->__pActiveCallList->GetValuesN();
1487                 pTelManager->__pEventListener->HandleCallSwapOccured(*pCallList);
1488         }
1489         else
1490         {
1491                 pTelManager->__pEventListener->HandleTelephonyError(ERROR_SWAP_FAILED);
1492         }
1493 }
1494
1495 void
1496 TelephonyManager::HandleEndFromConferenceCallbackResponse(TapiHandle* pHandle, int callBackResult, void* pData, void* pUserData)
1497 {
1498         AppLogDebug("ENTER");
1499         //This callback comes if a single call is ended from Conference call.
1500         TelephonyManager* pTelManager = (TelephonyManager*) pUserData;
1501         bool isParticipantCallEnded = false;
1502
1503         if (callBackResult == TAPI_CAUSE_SUCCESS && pData != null)
1504         {
1505                 //fetch end call handle
1506                 TelCallEndCnf_t callEndNotification;
1507                 memcpy(&callEndNotification, pData, sizeof(TelCallEndCnf_t));
1508                 //Fetch conference call
1509                 AppCallInfo endConfCall;
1510                 bool isConferenceCallFound = false;
1511                 IListT<AppCallInfo>* pCallList = pTelManager->__pActiveCallList->GetValuesN();
1512                 int callCount = pCallList->GetCount();
1513                 for (int index = 0; index < callCount; index++)
1514                 {
1515                         pCallList->GetAt(index, endConfCall);
1516                         if (endConfCall.IsConferenceCall() == true)
1517                         {
1518                                 isConferenceCallFound = true;
1519                                 //Found the Conference call to be changed.
1520                                 break;
1521                         }
1522                 }
1523                 delete pCallList;
1524                 pCallList = null;
1525
1526                 //Identify the call to be ended and remove from list.
1527                 if (isConferenceCallFound == true)
1528                 {
1529                         isParticipantCallEnded = pTelManager->HandleParticipantEndedFromConference(callEndNotification.id, endConfCall);
1530                 }
1531         }
1532
1533         //Check if participant call or Conference call was not found, then show error
1534         if (isParticipantCallEnded == false)
1535         {
1536                 pTelManager->__pEventListener->HandleTelephonyError(ERROR_END_FROM_CONFERENCE_FAILED);
1537         }
1538         AppLogDebug("EXIT");
1539 }
1540
1541 bool
1542 TelephonyManager::HandleParticipantEndedFromConference(unsigned int participantCallHandle, AppCallInfo& conferenceCall)
1543 {
1544         AppLogDebug("ENTER");
1545         //to check if participant call was found and ended.
1546         bool isParticipantCallEnded = false;
1547         //Identify the call to be ended and remove from list.
1548         AppCallInfo callToBeEnded;
1549         IListT<AppCallInfo>* pCallerList = conferenceCall.GetCallerList();
1550         int callerCount = pCallerList->GetCount();
1551         for (int index = 0; index < callerCount; index++)
1552         {
1553                 pCallerList->GetAt(index, callToBeEnded);
1554                 if ((unsigned int)callToBeEnded.GetCallHandle()->ToLong() == participantCallHandle)
1555                 {
1556                         //Identify the call to be ended and remove from conference list
1557                         conferenceCall.RemoveCallFromCallerList(index);
1558                         //update its status to individual call before saving to database
1559                         callToBeEnded.SetConference(false);
1560                         SaveCallInfoToLogsDb(callToBeEnded);
1561                         isParticipantCallEnded = true;
1562                         break;
1563                 }
1564         }
1565
1566         if(isParticipantCallEnded == false)
1567         {
1568                 //participant call not found and not ended
1569                 return isParticipantCallEnded;
1570         }
1571
1572         unsigned int confCallHandle = (unsigned int)conferenceCall.GetCallHandle()->ToLong();
1573         //Check if last participant removed. If yes, switch to single active view
1574         if (conferenceCall.GetCallerListCount() == 1)
1575         {
1576                 AppCallInfo callFromList;
1577                 pCallerList = conferenceCall.GetCallerList();
1578                 pCallerList->GetAt(0, callFromList);
1579                 //construct a new single active call
1580                 AppCallInfo* pActiveCall = new (std::nothrow) AppCallInfo();
1581                 *pActiveCall = callFromList;
1582                 //update conference status and Hold status
1583                 pActiveCall->SetConference(false);
1584                 pActiveCall->SetOnHold(conferenceCall.IsOnHold());
1585
1586                 __pActiveCallList->Remove(confCallHandle);
1587                 __pActiveCallList->Add(pActiveCall->GetCallHandle()->ToLong(), *pActiveCall);
1588                 pActiveCall = null;
1589                 //using the callConnected to switch to single active screen
1590                 //or update multiple active call screen
1591                 IListT<AppCallInfo>* pActiveCallList = __pActiveCallList->GetValuesN();
1592                 __pEventListener->HandleCallConnected(*pActiveCallList);
1593                 delete pActiveCallList;
1594                 pActiveCallList = null;
1595         }
1596         else
1597         {
1598                 AppCallInfo callFromList;
1599                 pCallerList = conferenceCall.GetCallerList();
1600                 pCallerList->GetAt(0, callFromList);
1601                 //construct a new conference call
1602                 AppCallInfo* pConfCallInfo = new (std::nothrow) AppCallInfo();
1603                 *pConfCallInfo = conferenceCall;
1604                 if (confCallHandle == participantCallHandle)
1605                 {
1606                         //Call Handle is same as conf call handle, so need to change conf call handle
1607                         __pActiveCallList->Remove(confCallHandle);
1608                         int newConfCallHandle = callFromList.GetCallHandle()->ToLong();
1609                         pConfCallInfo->SetCallHandle(newConfCallHandle);
1610                         __pActiveCallList->Add(newConfCallHandle, *pConfCallInfo);
1611                 }
1612                 else
1613                 {
1614                         __pActiveCallList->SetValue(confCallHandle, *pConfCallInfo);
1615                 }
1616                 __pEventListener->HandleConferenceChange();
1617         }
1618         AppLogDebug("EXIT");
1619         return isParticipantCallEnded;
1620 }
1621
1622 void
1623 TelephonyManager::HandleSplitFromConferenceCallbackResponse(TapiHandle* pHandle, int callBackResult, void* pData, void* pUserData)
1624 {
1625         TelephonyManager* pTelManager = (TelephonyManager*) pUserData;
1626         if (callBackResult == TAPI_CAUSE_SUCCESS && pData != null)
1627         {
1628                 TelCallSplitCnf_t callSplitNotification;
1629                 memcpy(&callSplitNotification, pData, sizeof(TelCallSplitCnf_t));
1630                 int confCallIndex = -1;
1631                 AppCallInfo endConfCall;
1632                 bool isConferenceCallFound = false;
1633
1634                 IListT<AppCallInfo>* pCallList = pTelManager->__pActiveCallList->GetValuesN();
1635                 int callCount = pCallList->GetCount();
1636                 for (int index = 0; index < callCount; index++)
1637                 {
1638                         pCallList->GetAt(index, endConfCall);
1639                         if (endConfCall.IsConferenceCall() == true)
1640                         {
1641                                 isConferenceCallFound = true;
1642                                 confCallIndex = index;
1643                                 //Found the Conference call to be ended.
1644                                 break;
1645                         }
1646                 }
1647
1648                 if (isConferenceCallFound == false)
1649                 {
1650                         delete pCallList;
1651                         pCallList = null;
1652                         return;
1653                 }
1654                 delete pCallList;
1655                 pCallList = null;
1656                 //Identify the call to be ended and remove from list on API success
1657                 AppCallInfo callToBeEnded;
1658                 pCallList = endConfCall.GetCallerList();
1659                 callCount = pCallList->GetCount();
1660                 for (int index = 0; index < callCount; index++)
1661                 {
1662                         pCallList->GetAt(index, callToBeEnded);
1663                         if ((unsigned int)callToBeEnded.GetCallHandle()->ToLong() == callSplitNotification.id)
1664                         {
1665                                 //Identified the call to be ended and remove from conference list
1666                                 //Add this to the active call list
1667                                 endConfCall.RemoveCallFromCallerList(index);
1668                                 break;
1669                         }
1670                 }
1671                 unsigned int confCallHandle = (unsigned int)endConfCall.GetCallHandle()->ToLong();
1672                 //Set the hold flags correctly and make the changes to the active call list
1673                 if (endConfCall.GetCallerListCount() == 1)
1674                 {
1675                         //Set hold for the other single call
1676                         // and add to the list
1677                         AppCallInfo callFromList;
1678                         pCallList = endConfCall.GetCallerList();
1679                         pCallList->GetAt(0, callFromList);
1680                         AppCallInfo* pHeldCall = new (std::nothrow) AppCallInfo();
1681                         *pHeldCall = callFromList;
1682                         pHeldCall->SetConference(false);
1683                         pHeldCall->SetOnHold(true);
1684                         pTelManager->__pActiveCallList->Remove(confCallHandle);
1685                         pTelManager->__pActiveCallList->Add(pHeldCall->GetCallHandle()->ToLong(), *pHeldCall);
1686                         pHeldCall = null;
1687                 }
1688                 else
1689                 {
1690                         //Set hold flag for conference call
1691                         endConfCall.SetOnHold(true);
1692                         AppCallInfo callFromList;
1693                         pCallList = endConfCall.GetCallerList();
1694                         pCallList->GetAt(0, callFromList);
1695
1696                         AppCallInfo* pConfCallInfo = new (std::nothrow) AppCallInfo();
1697                         *pConfCallInfo = endConfCall;
1698                         if (confCallHandle == callSplitNotification.id)
1699                         {
1700                                 //Call Handle is same as conf call handle.
1701                                 //Change conf call handle
1702                                 pTelManager->__pActiveCallList->Remove(confCallHandle);
1703                                 int tmpCallHandle = callFromList.GetCallHandle()->ToLong();
1704                                 pConfCallInfo->SetCallHandle(tmpCallHandle);
1705                                 pTelManager->__pActiveCallList->Add(callFromList.GetCallHandle()->ToLong(), *pConfCallInfo);
1706                         }
1707                         else
1708                         {
1709                                 pTelManager->__pActiveCallList->Remove(confCallHandle);
1710                                 pTelManager->__pActiveCallList->Add(confCallHandle, *pConfCallInfo);
1711                         }
1712                 }
1713                 //Add the new active call to active call list
1714                 AppCallInfo* pActiveCall = new (std::nothrow) AppCallInfo();
1715                 *pActiveCall = callToBeEnded;
1716                 pActiveCall->SetConference(false);
1717                 pActiveCall->SetOnHold(false);
1718                 pTelManager->__pActiveCallList->Remove(pActiveCall->GetCallHandle()->ToLong());
1719                 pTelManager->__pActiveCallList->Add(pActiveCall->GetCallHandle()->ToLong(), *pActiveCall);
1720                 pActiveCall = null;
1721                 //using the callConnected to switch to Multiple active screen
1722                 pCallList = pTelManager->__pActiveCallList->GetValuesN();
1723                 pTelManager->__pEventListener->HandleCallConnected(*pCallList);
1724                 pCallList = null;
1725         }
1726         else
1727         {
1728                 pTelManager->__pEventListener->HandleTelephonyError(ERROR_SPLIT_FROM_CONFERENCE_FAILED);
1729         }
1730 }
1731
1732 void
1733 TelephonyManager::HandleEndConferenceCallbackResponse(TapiHandle* pHandle, int callBackResult, void* pData, void* pUserData)
1734 {
1735         AppLog("ENTER");
1736         //This callback comes only if a conference call is ended by user.
1737         TelephonyManager* pTelManager = (TelephonyManager*) pUserData;
1738         if (callBackResult == TAPI_CAUSE_SUCCESS && pData != null)
1739         {
1740                 //fetch ended confCall details
1741                 result r = E_FAILURE;
1742                 AppCallInfo endConfCallInfo;
1743                 IListT<AppCallInfo>* pActiveCallList = pTelManager->__pActiveCallList->GetValuesN();
1744                 if(pActiveCallList != null && pActiveCallList->GetCount() > 0)
1745                 {
1746                         for (int index = 0; index < pActiveCallList->GetCount(); index++)
1747                         {
1748                                 r = pActiveCallList->GetAt(index, endConfCallInfo);
1749                                 if(r == E_SUCCESS && endConfCallInfo.IsConferenceCall() == true)
1750                                 {
1751                                         //conference call found.
1752                                         r = E_SUCCESS;
1753                                         break;
1754                                 }
1755                                 else
1756                                 {
1757                                         r = E_FAILURE;
1758                                 }
1759                         }
1760                 }
1761                 delete pActiveCallList;
1762                 pActiveCallList = null;
1763
1764                 //check if no conference call found, then return.
1765                 if(r == E_FAILURE)
1766                 {
1767                         return;
1768                 }
1769                 //remove the conference call handle from active call list to avoid any processing in HandleIdleCallback().
1770                 pTelManager->__pActiveCallList->Remove(endConfCallInfo.GetCallHandle()->ToLong());
1771                 //Save "End" Conf. call info to call log database
1772                 pTelManager->SaveCallInfoToLogsDb(endConfCallInfo);
1773
1774                 //check if the ended call was the last call and show notification to user
1775                 bool isLastCall = (pTelManager->__pActiveCallList->GetCount() == 0);
1776                 ArrayListT<AppCallInfo>* pCallList = null;
1777                 if (isLastCall)
1778                 {
1779                         pTelManager->__pSoundManager->SetlastEndedConferenceCall();
1780                         //stop sound session
1781                         pTelManager->__pSoundManager->StopSession();
1782                         //send empty call list to show dialer or call log screen
1783                         pCallList = new (std::nothrow) ArrayListT<AppCallInfo>();
1784                 }
1785                 else
1786                 {
1787                         //fetch active calls to show appropriate scene
1788                         pCallList = static_cast<ArrayListT<AppCallInfo>*>(pTelManager->__pActiveCallList->GetValuesN());
1789                 }
1790                 //notify listener that call is disconnected.
1791                 pTelManager->__pEventListener->HandleCallDisconnected(isLastCall, *pCallList);
1792                 delete pCallList;
1793                 pCallList = null;
1794         }
1795         else
1796         {
1797                 pTelManager->__pEventListener->HandleTelephonyError(ERROR_END_CALL_FAILED);
1798         }
1799         AppLog("EXIT");
1800 }
1801
1802 void
1803 TelephonyManager::HandleIdleCallBack(void* pData)
1804 {
1805         AppLogDebug("ENTER");
1806         //This callback comes when any type of calls are ended
1807         //We do NOT handle below scenarios here -
1808         //1) In incoming call scenarios, if we end any active calls - handled in "AcceptCall()".
1809         //2) Incoming call automatically blocked and rejection by user scenarios are handled in "HandleRejectCallbackResponse()".
1810         //3) End conference call is handled in "HandleEndConferenceCallbackResponse()".
1811         //4) End Single Call from Conference call by user is handled in "HandleEndFromConferenceCallbackResponse()".
1812         //5) End Single Call from Conference call using eventInjector is diverted to "HandleParticipantEndedFromConference()".
1813         //BUT, we do handle below scenarios here -
1814         //1) "MISSED" incoming call scenario here i.e incoming call is rejected by other caller.
1815         //2) an "unconnected" dialed call is ended by caller or other party.
1816         //3) Any normal active calls(NOT conference calls) ended by user or by other party.
1817
1818         if(__pSoundManager->GetLastConferenceCall() == false)
1819         {
1820                 __pSoundManager->SetDisconnectTone();
1821         }
1822
1823         TelCallStatusIdleNoti_t idleNotification;
1824         memcpy(&idleNotification, pData, sizeof(TelCallStatusIdleNoti_t));
1825         //handle end call event, show next screen
1826         unsigned int endCallHandle = idleNotification.id;
1827
1828         //empty active call list or no dialed or incoming calls - ignore this event
1829         IListT<AppCallInfo>* pActiveCallList = __pActiveCallList->GetValuesN();
1830         if((pActiveCallList == null || pActiveCallList->GetCount() <= 0) && __pDialedCall == null && __pIncomingCall == null)
1831         {
1832                 delete pActiveCallList;
1833                 AppLogDebug("EXIT - no calls exist");
1834                 return;
1835         }
1836
1837         //Check if ended call was among conference caller list,
1838         //then divert event to "HandleParticipantEndedFromConference()"
1839         AppCallInfo confCallInfo;
1840         bool isConferenceCallChanged = false;
1841         for (int index = 0; (pActiveCallList != null && index < pActiveCallList->GetCount()); index++)
1842         {
1843                 //fetch conference call
1844                 result r = pActiveCallList->GetAt(index, confCallInfo);
1845                 if (r == E_SUCCESS && confCallInfo.IsConferenceCall() == true)
1846                 {
1847                         //Conference call found - check if ended call is a participant
1848                         isConferenceCallChanged = HandleParticipantEndedFromConference(endCallHandle, confCallInfo);
1849                         break;
1850                 }
1851         }
1852         delete pActiveCallList;
1853         pActiveCallList = null;
1854         if (isConferenceCallChanged == true)
1855         {
1856                 //end call event handled - conference call will now either remain as conf. call
1857                 //or become single active call, if it has only 1 participant left.
1858                 __pSoundManager->SetSoundMode(SOUND_MODE_VOICE);
1859                 AppLogDebug("isConferenceCallChanged == true");
1860                 return;
1861         }
1862
1863         //check if ended call was among the active call list and not a conference call
1864         AppCallInfo endCallInfo;
1865         result r = __pActiveCallList->GetValue(endCallHandle, endCallInfo);
1866         if (r == E_SUCCESS)
1867         {
1868                 bool isHandled = HandleEndNormalActiveCall(endCallInfo);
1869                 if (isHandled == true)
1870                 {
1871                         __pSoundManager->SetSoundMode(SOUND_MODE_VOICE);
1872                         return;
1873                 }
1874         }
1875
1876         //Check if dialed call is rejected by other party
1877         bool isDialedCallEnded = ((__pDialedCall != null)  && (__pDialedCall->GetCallHandle() != null) &&(((unsigned int)__pDialedCall->GetCallHandle()->ToLong()) == idleNotification.id));
1878         //Check if "missed" incoming call is ended
1879         bool isMissedIncomingCallEnded = (__pIncomingCall != null && ((unsigned int)__pIncomingCall->GetCallHandle()->ToLong() == idleNotification.id));
1880         if (isDialedCallEnded == true || isMissedIncomingCallEnded == true)
1881         {
1882                 //It comes here only if the ended call was either a "unconnected" dialed call or an "Missed" incoming call.
1883                 bool isLastCall = (__pActiveCallList->GetCount() == 0);
1884
1885                 ArrayListT<AppCallInfo>* pCallList = null;
1886                 //Check if dialed call was ended
1887                 if (isDialedCallEnded == true)
1888                 {
1889                         AppLogDebug("Dialed Call Ended");
1890                         //Call Ended is the dialed call
1891                         endCallInfo = *(__pDialedCall);
1892                         delete __pDialedCall;
1893                         __pDialedCall = null;
1894                 }
1895                 else
1896                 {
1897                         //Here, only "Missed" Incoming call ended by other caller is handled.
1898                         AppLogDebug("Missed Call Ended");
1899                         __pSoundManager->StopAlert();
1900                         endCallInfo = *(__pIncomingCall);
1901                         delete __pIncomingCall;
1902                         __pIncomingCall = null;
1903                         //update missed status
1904                         endCallInfo.SetCalllogType(CALL_LOG_TYPE_VOICE_MISSED_UNSEEN);
1905                 }
1906                 //save ended call to call log db.
1907                 SaveCallInfoToLogsDb(endCallInfo);
1908
1909                 //notify listener that call is disconnected.
1910                 if (isLastCall == true)
1911                 {
1912                         __pSoundManager->StopSession();
1913                         pCallList = new (std::nothrow) ArrayListT<AppCallInfo>();
1914                         pCallList->Construct(1);
1915                         if (isMissedIncomingCallEnded == false)
1916                         {
1917                                 //save to list to show EndCallForm
1918                                 pCallList->Add(endCallInfo);
1919                         }
1920                         __pSoundManager->GetTimer()->Cancel();
1921
1922                 }
1923                 else
1924                 {
1925                         pCallList = static_cast<ArrayListT<AppCallInfo>*>(__pActiveCallList->GetValuesN());
1926                 }
1927                 __pEventListener->HandleCallDisconnected(isLastCall, *pCallList);
1928                 delete pCallList;
1929                 pCallList = null;
1930         }
1931         __pSoundManager->SetSoundMode(SOUND_MODE_VOICE);
1932         AppLogDebug("EXIT");
1933 }
1934
1935 bool
1936 TelephonyManager::HandleEndNormalActiveCall(AppCallInfo& endCallInfo)
1937 {
1938         AppLogDebug("Enter");
1939         // This function gets called only from HandleIdleCallback(),
1940         // to handle disconnection of normal active calls.
1941         if (endCallInfo.IsConferenceCall() == false)
1942         {
1943                 //remove the call handle from active call list
1944                 __pActiveCallList->Remove(endCallInfo.GetCallHandle()->ToLong());
1945                 //check if the ended call was the last call and show notification to user
1946                 bool isLastCall = (__pActiveCallList->GetCount() == 0);
1947                 ArrayListT<AppCallInfo>* pCallList = null;
1948                 if (isLastCall)
1949                 {
1950                         //stop sound session
1951                         __pSoundManager->StopSession();
1952                         //save "End" CallInfo to list to show EndCallForm
1953                         pCallList = new (std::nothrow) ArrayListT<AppCallInfo>();
1954                         pCallList->Construct(1);
1955                         pCallList->Add(endCallInfo);
1956                 }
1957                 else
1958                 {
1959                         //fetch active calls to show appropriate scene
1960                         pCallList = static_cast<ArrayListT<AppCallInfo>*>(__pActiveCallList->GetValuesN());
1961                 }
1962
1963                 //Save "End" call info to call log database
1964                 SaveCallInfoToLogsDb(endCallInfo);
1965                 //notify listener that call is disconnected.
1966                 __pEventListener->HandleCallDisconnected(isLastCall, *pCallList);
1967                 delete pCallList;
1968                 pCallList = null;
1969                 return true;
1970         }
1971         return false;
1972 }
1973
1974 void
1975 TelephonyManager::HandleDialingCallBack(void* pData)
1976 {
1977         AppLogDebug("Enter");
1978         unsigned int tempHandle = 0;
1979         TelCallStatusDialingNoti_t dialingNotification;
1980         memcpy(&tempHandle, pData, sizeof(TS_UINT));
1981         dialingNotification.id = tempHandle;
1982         //check if callback is for different dialed call
1983         //Dont check for call handle, since this is the first time, we get call handle for a dialed call.
1984         if (__pDialedCall == null)
1985         {
1986                 AppLogDebug("__pDialedCall == null");
1987                 //construct new dialed call
1988                 __pDialedCall = new (std::nothrow) AppCallInfo();
1989
1990                 TelCallStatus_t callStatus;
1991                 int res = tel_get_call_status(__pTapiHandle, dialingNotification.id, &callStatus);
1992                 if (res == TAPI_CAUSE_SUCCESS)
1993                 {
1994                         //save phone number
1995                         String contactNumber(callStatus.pNumber);
1996                         __pDialedCall->SetContactNumber(contactNumber);
1997                         //set emergency state
1998                         if(callStatus.CallType == TAPI_CALL_TYPE_E911)
1999                         {
2000                                 __pDialedCall->SetEmergency(true);
2001                         }
2002                         else
2003                         {
2004                                 __pDialedCall->SetEmergency(false);
2005                         }
2006                         //save contact info
2007                         FetchContactInfoForNumber(__pDialedCall->GetContactNumber());
2008                         if (__pCachedContact != null)
2009                         {
2010                                 __pDialedCall->SetContactInfo(*(__pCachedContact));
2011                         }
2012                 }
2013         }
2014         //set call handle for dialed call
2015         __pDialedCall->SetCallHandle(dialingNotification.id);
2016         __pDialedCall->SetCalllogType(CALL_LOG_TYPE_VOICE_OUTGOING);
2017
2018         //set call notification time.
2019         long long startTime = 0;
2020         SystemTime::GetTicks(startTime);
2021         __pDialedCall->SetCallNotificationTime(startTime);
2022 }
2023
2024 void
2025 TelephonyManager::HandleActiveCallBack(void* pData)
2026 {
2027         // This callback comes whenever any new call is connected
2028         // Or, any "Held" call is activated (we ignore activation of "Held" calls).
2029         unsigned int newCallHandle = 0;
2030         TelCallStatusActiveNoti_t activeNotification;
2031         memcpy(&newCallHandle, pData, sizeof(TS_UINT));
2032         activeNotification.id = newCallHandle;
2033         IListT<AppCallInfo>* pCallList = __pActiveCallList->GetValuesN();
2034
2035         //Check if the "Held" call was activated, i.e it is already present in already activated calls list.
2036         bool toHandleEvent = true;
2037         for (int callIndex = 0; (callIndex < pCallList->GetCount() && toHandleEvent == true); callIndex++ )
2038         {
2039                 AppCallInfo tempCallInfo;
2040                 pCallList->GetAt(callIndex, tempCallInfo);
2041                 unsigned int tempCallHandle = tempCallInfo.GetCallHandle()->ToLong();
2042                 //Check if active callback came for "HandleJoinCallbackResponse"
2043                 //or for "UnHold Conference Call or normal call".
2044                 if(tempCallInfo.IsConferenceCall() == true)
2045                 {
2046                         if (tempCallHandle == activeNotification.id)
2047                         {
2048                                 toHandleEvent = false;
2049                         }
2050                         else
2051                         {
2052                                 //check individual participants of conf call
2053                                 IListT<AppCallInfo>* pConfCallList = tempCallInfo.GetCallerList();
2054                                 int confCallCount  = pConfCallList->GetCount();
2055                                 for (int callIndex = 0; (callIndex < confCallCount && toHandleEvent == true); callIndex++)
2056                                 {
2057                                         AppCallInfo confCallerInfo;
2058                                         pConfCallList->GetAt(callIndex, confCallerInfo);
2059                                         unsigned int confCallerHandle = confCallerInfo.GetCallHandle()->ToLong();
2060                                         if (confCallerHandle == activeNotification.id)
2061                                         {
2062                                                 toHandleEvent = false;
2063                                         }
2064                                 }
2065                         }
2066                 }
2067                 else if(tempCallHandle == activeNotification.id)
2068                 {
2069                         //If normal call is UnHold
2070                         toHandleEvent = false;
2071                 }
2072         }
2073
2074         //check if we need to handle this event.
2075         if(toHandleEvent == true)
2076         {
2077                 //Here it comes, only if either new dialed or incoming call was connected.
2078                 HandleCallConnected( activeNotification.id);
2079         }
2080         delete pCallList;
2081         pCallList = null;
2082 }
2083
2084 void
2085 TelephonyManager::HandleCallConnected(unsigned int connectedCallHandle)
2086 {
2087         //Here it comes, only if either new dialed or incoming call was connected.
2088         //This function should be called only from "HandleActiveCallback()".
2089         AppCallInfo* pConnectedCall = null;
2090         //to check if incoming call was connected
2091         bool isIncomingCallConnected = false;
2092
2093         __pSoundManager->SetConnectTone();
2094         //Check if dialed call is connected.
2095         if ((__pDialedCall != null) && (connectedCallHandle == (unsigned int)__pDialedCall->GetCallHandle()->ToLong()))
2096         {
2097                 pConnectedCall = __pDialedCall;
2098                 __pDialedCall = null;
2099         }
2100         //Check if connected call is incoming call.
2101         else if (__pIncomingCall != null && (connectedCallHandle == (unsigned int)__pIncomingCall->GetCallHandle()->ToLong()))
2102         {
2103                 pConnectedCall = __pIncomingCall;
2104                 __pIncomingCall = null;
2105                 isIncomingCallConnected = true;
2106         }
2107         else
2108         {
2109                 // this is just for safety. This scenario should never come.
2110                 // Otherwise Correct the code in some other function, if it comes here.
2111                 AppLogDebug("Error - Connected call was neither one of active calls nor it was dialed or incoming call");
2112                 //Construct a new CallInfo object for call
2113                 pConnectedCall = new (std::nothrow) AppCallInfo();
2114                 pConnectedCall->SetCallHandle(connectedCallHandle);
2115
2116                 TelCallStatus_t callStatus;
2117                 int res = tel_get_call_status(__pTapiHandle, connectedCallHandle, &callStatus);
2118                 if (res == TAPI_CAUSE_SUCCESS)
2119                 {
2120                         String contactNumber(callStatus.pNumber);
2121                         pConnectedCall->SetContactNumber(contactNumber);
2122                         //set emergency state
2123                         if(callStatus.CallType == TAPI_CALL_TYPE_E911)
2124                         {
2125                                 pConnectedCall->SetEmergency(true);
2126                         }
2127                         else
2128                         {
2129                                 pConnectedCall->SetEmergency(false);
2130                         }
2131                         //set call notification time
2132                         long long startTime = 0;
2133                         SystemTime::GetTicks(startTime);
2134                         pConnectedCall->SetCallNotificationTime(startTime);
2135                         if (callStatus.bMoCall == true)
2136                         {
2137                                 pConnectedCall->SetCalllogType(CALL_LOG_TYPE_VOICE_OUTGOING);
2138                         }
2139                         else
2140                         {
2141                                 pConnectedCall->SetCalllogType(CALL_LOG_TYPE_VOICE_INCOMING);
2142                                 isIncomingCallConnected = true;
2143                         }
2144                 }
2145                 //delete any dialed or incoming call objects
2146                 delete __pDialedCall;
2147                 __pDialedCall = null;
2148                 delete __pIncomingCall;
2149                 __pIncomingCall = null;
2150         }
2151
2152         //fetch contact info for connected call & it is not a hidden call
2153         if (pConnectedCall->GetContactInfo() == null && pConnectedCall->GetContactNumber().IsEmpty() == false)
2154         {
2155                 FetchContactInfoForNumber(pConnectedCall->GetContactNumber());
2156                 if (__pCachedContact != null)
2157                 {
2158                         pConnectedCall->SetContactInfo(*(__pCachedContact));
2159                 }
2160         }
2161         //set Call connect time for newly connected call
2162         long long startTime = 0;
2163         SystemTime::GetTicks(startTime);
2164         pConnectedCall->SetCallConnectTime(startTime);
2165         if(GetCurrentCallCount() == 0)
2166         {
2167                 __pSoundManager->SetMinuteReminderTone();
2168         }
2169
2170
2171         //transfer ownership to Active calls list
2172         __pActiveCallList->Add(connectedCallHandle, *(pConnectedCall));
2173         pConnectedCall = null;
2174
2175         //notify listener that call is connected.
2176         IListT<AppCallInfo>* pCallList = __pActiveCallList->GetValuesN();
2177         __pSoundManager->SetSoundMode(SOUND_MODE_VOICE);
2178         __pEventListener->HandleCallConnected(*pCallList);
2179         if (isIncomingCallConnected == true)
2180         {
2181                 __pSoundManager->StopAlert();
2182         }
2183         delete pCallList;
2184         pCallList = null;
2185 }
2186
2187 bool
2188 TelephonyManager::CheckIncomingCallToBeRejected(AppCallInfo* pIncomingCallInfo)
2189 {
2190         AppLogDebug("Enter");
2191         int callHandle = pIncomingCallInfo->GetCallHandle()->ToLong();
2192         String contactNumber(L"");
2193         contactNumber.Append(pIncomingCallInfo->GetContactNumber());
2194         //Check if "reject unknown calls" is set and contact number is not present in AddressBook
2195         //or if contact number is blacklisted
2196         if (((__pSettingsManager->GetUnknownRejectStatus() == true) && (pIncomingCallInfo->GetContactInfo() == null))
2197                         || (__pSettingsManager->IsCallToBeRejected(contactNumber) == true))
2198         {
2199                 AnswerAutoRejectCall(callHandle);
2200                 return true;
2201         }
2202         return false;
2203 }
2204
2205 void
2206 TelephonyManager::HandleCallback(TapiHandle* pHandle, const char* pNotiId, void* pData, void* pUserData)
2207 {
2208         unsigned int tempHandle = 0;
2209         TelephonyManager* pTelManager = (TelephonyManager*) pUserData;
2210         if (pTelManager->__pSoundManager == null)
2211         {
2212                 AppLog("Creating Sound Manager");
2213                 pTelManager->__pSoundManager = new (std::nothrow) SoundManager();
2214         }
2215         //Handle telephony events
2216         if (strcmp(pNotiId, TAPI_NOTI_VOICE_CALL_STATUS_IDLE) == 0)
2217         {
2218                 pTelManager->HandleIdleCallBack(pData);
2219         }
2220         else if (strcmp(pNotiId, TAPI_NOTI_VOICE_CALL_STATUS_ACTIVE) == 0)
2221         {
2222                 pTelManager->__pSoundManager->StartSession();
2223                 pTelManager->HandleActiveCallBack(pData);
2224         }
2225         else if (strcmp(pNotiId, TAPI_NOTI_VOICE_CALL_STATUS_DIALING) == 0)
2226         {
2227                 pTelManager->HandleDialingCallBack(pData);
2228         }
2229         else
2230         {
2231                 memcpy(&tempHandle, pData, sizeof(TS_UINT));
2232         }
2233 }
2234
2235 AppCallInfo*
2236 TelephonyManager::GetConferenceCallInfoN(void)
2237 {
2238         AppCallInfo* pConfCallInfo = null;
2239
2240         IListT<AppCallInfo>* pCallList = __pActiveCallList->GetValuesN();
2241         int callCount = pCallList->GetCount();
2242         for (int index = 0; index < callCount; index++)
2243         {
2244                 AppCallInfo callInfo;
2245                 pCallList->GetAt(index, callInfo);
2246                 if (callInfo.IsConferenceCall() == true)
2247                 {
2248                         pConfCallInfo = new (std::nothrow) AppCallInfo();
2249                         *pConfCallInfo = callInfo;
2250                         //Found the Conference call
2251                         break;
2252                 }
2253         }
2254         delete pCallList;
2255         pCallList = null;
2256
2257         return pConfCallInfo;
2258 }
2259
2260 IListT<AppCallInfo>*
2261 TelephonyManager::GetCallListN(void)
2262 {
2263         ArrayListT<AppCallInfo>* pCallList = null;
2264         if (__pActiveCallList != null)
2265         {
2266                 pCallList = static_cast<ArrayListT<AppCallInfo>*>(__pActiveCallList->GetValuesN());
2267         }
2268         return pCallList;
2269 }
2270
2271 int
2272 TelephonyManager::GetCurrentCallCount(void)
2273 {
2274         if (__pActiveCallList != null)
2275         {
2276                 return __pActiveCallList->GetCount();
2277         }
2278         return 0;
2279 }
2280
2281 void
2282 TelephonyManager::StartAlert(AppCallInfo& incomingCallInfo)
2283 {
2284         String contactRingTone(L"");
2285         String contactNumber = incomingCallInfo.GetContactNumber();
2286         //check if not hidden call
2287         if(contactNumber.IsEmpty() == false)
2288         {
2289                 //fetch contact info from Db
2290                 Contact* foundContact = GetContactN(contactNumber);
2291                 if(foundContact != null)
2292                 {
2293                         //fetch custom ringtone for contact
2294                         result r = foundContact->GetValue(CONTACT_PROPERTY_ID_RINGTONE, contactRingTone);
2295                         AppLog("ringtone fetched - r = %d", r);
2296                         delete foundContact;
2297                         foundContact = null;
2298                 }
2299         }
2300         __pSoundManager->StartAlert(contactRingTone);
2301 }
2302
2303 void
2304 TelephonyManager::StopAlert(void)
2305 {
2306         __pSoundManager->StopAlert();
2307 }
2308
2309 result
2310 TelephonyManager::CheckValidTelePhoneNumber(const String& contactNumber)
2311 {
2312         result r = E_SUCCESS;
2313         if (contactNumber.GetLength() > TAPI_CALL_DIALDIGIT_LEN_MAX)
2314         {
2315                 r = E_FAILURE;
2316         }
2317         //TODO: check if valid phone number else return error message
2318         return r;
2319 }
2320
2321 result
2322 TelephonyManager::CheckIfMOCallIsPossible()
2323 {
2324         result r = E_SUCCESS;
2325
2326         //Check modem power status
2327         int modemStatus = 0;
2328         int errorCode = tel_check_modem_power_status(__pTapiHandle, &modemStatus);
2329         if (errorCode != TAPI_API_SUCCESS || modemStatus == TAPI_PHONE_POWER_STATUS_OFF
2330                         || modemStatus == TAPI_PHONE_POWER_STATUS_ERROR)
2331         {
2332                 r = E_FAILURE;
2333         }
2334         else
2335         {
2336                 TelSimCardStatus_t simStatus;
2337                 int simChangedStatus;
2338                 //fetch sim initialization status
2339                 int errorCode = tel_get_sim_init_info(__pTapiHandle, &simStatus, &simChangedStatus);
2340                 if (errorCode != TAPI_API_SUCCESS)
2341                 {
2342                         r = E_FAILURE;
2343                 }
2344                 else
2345                 {
2346                         switch (simStatus)
2347                         {
2348                         case TAPI_SIM_STATUS_SIM_INIT_COMPLETED: // Sim Initialization ok
2349                                 r = E_SUCCESS;
2350                                 break;
2351
2352                         case TAPI_SIM_STATUS_UNKNOWN: //initial state
2353                         case TAPI_SIM_STATUS_CARD_NOT_PRESENT: //Card not present
2354                         case TAPI_SIM_STATUS_CARD_REMOVED: //Card removed
2355                         case TAPI_SIM_STATUS_CARD_ERROR: // Bad card / On the fly, SIM gone bad
2356                                 //TODO: might want to set different error code, to give proper message to user
2357                                 r = E_FAILURE;
2358                                 break;
2359                         default:
2360                                 r = E_FAILURE;
2361                                 break;
2362                         }
2363                 }
2364         }
2365         return r;
2366 }
2367
2368 bool
2369 TelephonyManager::CheckIfMOCallIsEmergency(const String& contactNumber, bool isSimInitialized)
2370 {
2371         //TODO: extract actual telephone number from contactNumber
2372         //by deleting prefix,'P','W', etx.
2373
2374         bool isEmergency = false;
2375         //conversion "contactNumber" to char*
2376         const wchar_t* pContact = contactNumber.GetPointer();
2377         int len = contactNumber.GetLength() + 1;
2378         char* pNumber = new (std::nothrow) char[len];
2379         wcstombs(pNumber, pContact, len);
2380
2381         if(isSimInitialized)
2382         {
2383                 //used to get Ecc information for 2G and 3G.
2384                 TelSimEccList_t simEccList;
2385                 memset(&simEccList, 0x00, sizeof(TelSimEccList_t));
2386                 //Check if given number matches the sim card's emergency numbers
2387                 int errorCode = tel_get_sim_ecc(__pTapiHandle, &simEccList);
2388                 if (errorCode == TAPI_API_SUCCESS && simEccList.ecc_count > 0)
2389                 {
2390                         for (int index = 0; index < simEccList.ecc_count; index++)
2391                         {
2392                                 if ((strcmp(pNumber, simEccList.list[index].number) == 0))
2393                                 {
2394                                         isEmergency = true;
2395                                 }
2396                         }
2397                 }
2398         }
2399         else
2400         {
2401                 //TODO: check if we need to also check SOS call numbers, if sim not present.
2402         }
2403
2404         delete[] pNumber;
2405         pNumber = null;
2406         return isEmergency;
2407 }
2408
2409 result
2410 TelephonyManager::FetchContactInfoForNumber(const String& phoneNumberStr)
2411 {
2412         result r = E_FAILURE;
2413
2414         //delete previously cached data
2415         if (__pCachedContact != null)
2416         {
2417                 delete __pCachedContact;
2418                 __pCachedContact = null;
2419         }
2420
2421         //Searches contacts by phone number.
2422         IList* pContactList = __pAddressBook->SearchContactsByPhoneNumberN(phoneNumberStr);
2423         if (pContactList == null || IsFailed(GetLastResult()))
2424         {
2425                 return r;
2426         }
2427
2428         //Fetch the contact's info to be displayed
2429         IEnumerator* pContactEnum = pContactList->GetEnumeratorN();
2430         while ((E_SUCCESS == pContactEnum->MoveNext()) && (__pCachedContact == null))
2431         {
2432                 Contact* pContact = static_cast<Contact*>(pContactEnum->GetCurrent());
2433
2434                 IList* pPhoneNumberList = pContact->GetValuesN(CONTACT_MPROPERTY_ID_PHONE_NUMBERS);
2435                 if (pPhoneNumberList != null)
2436                 {
2437                         IEnumerator* pPhoneEnum = pPhoneNumberList->GetEnumeratorN();
2438                         while (E_SUCCESS == pPhoneEnum->MoveNext())
2439                         {
2440                                 PhoneNumber* pPhoneNumber = (PhoneNumber*) pPhoneEnum->GetCurrent();
2441                                 //Check if this is the correct contact
2442                                 if (pPhoneNumber->GetPhoneNumber().Equals(phoneNumberStr))
2443                                 {
2444                                         //save newly fetched contact info.
2445                                         __pCachedContact = new (std::nothrow) Contact(*pContact);
2446                                         r = E_SUCCESS;
2447                                         break;
2448                                 }
2449                         }
2450                         delete pPhoneEnum;
2451                         pPhoneNumberList->RemoveAll(true);
2452                         delete pPhoneNumberList;
2453                 }
2454         }
2455         delete pContactEnum;
2456         pContactList->RemoveAll(true);
2457         delete pContactList;
2458
2459         return r;
2460 }
2461
2462 Contact*
2463 TelephonyManager::GetContactN(const String& phoneNumber)
2464 {
2465         result r = FetchContactInfoForNumber(phoneNumber);
2466         if (!IsFailed(r))
2467         {
2468                 return new (std::nothrow) Contact(*__pCachedContact);
2469         }
2470         return null;
2471 }
2472
2473 AppCallInfo*
2474 TelephonyManager::FetchIncomingCallHandleN(const String& callHandle, const String& contactNumber)
2475 {
2476         if(__pIncomingCall != null)
2477         {
2478                 delete __pIncomingCall;
2479                 __pIncomingCall = null;
2480         }
2481
2482         if(callHandle.IsEmpty() == false)
2483         {
2484                 int incomingHandle;
2485                 Integer::Parse(callHandle,incomingHandle);
2486                 //This API call is synchronous
2487                 TelCallStatus_t callStatus;
2488                 int errCode = tel_get_call_status(__pTapiHandle, incomingHandle, &callStatus);
2489                 if (errCode != TAPI_API_SUCCESS)
2490                 {
2491                         AppLogDebug("tel_get_call_status failed");
2492                         return null;
2493                 }
2494                 //construct incoming call info object
2495                 __pIncomingCall = new (std::nothrow) AppCallInfo();
2496                 __pIncomingCall->SetCallHandle(incomingHandle);
2497
2498                 //contact number
2499                 String phoneNumber(contactNumber);
2500                 if(phoneNumber.IsEmpty() == true)
2501                 {
2502                         phoneNumber.Append(callStatus.pNumber);
2503                 }
2504                 __pIncomingCall->SetContactNumber(phoneNumber);
2505                 //set emergency state
2506                 if(callStatus.CallType == TAPI_CALL_TYPE_E911)
2507                 {
2508                         __pIncomingCall->SetEmergency(true);
2509                 }
2510                 else
2511                 {
2512                         __pIncomingCall->SetEmergency(false);
2513                 }
2514                 //set start time, when call is connected
2515                 long long startTime = 0;
2516                 SystemTime::GetTicks(startTime);
2517                 __pIncomingCall->SetCallNotificationTime(startTime);
2518                 __pIncomingCall->SetCalllogType(CALL_LOG_TYPE_VOICE_INCOMING);
2519         }
2520         else
2521         {
2522                 //TODO: This 'else' block can be removed once AppControl request API is stabilized.
2523                 //This API call is synchronous and 'HandleIncomingCallStatusCallBack' is called for each active call.
2524                 int errCode = tel_get_call_status_all(__pTapiHandle, &HandleIncomingCallStatusCallBack, this);
2525                 if (errCode != TAPI_API_SUCCESS)
2526                 {
2527                         return null;
2528                 }
2529         }
2530
2531         if(__pIncomingCall != null)
2532         {
2533                 //set call notification time
2534                 long long startTime = 0;
2535                 SystemTime::GetTicks(startTime);
2536                 __pIncomingCall->SetCallNotificationTime(startTime);
2537
2538                 if(__pIncomingCall->GetContactNumber().IsEmpty() == false)
2539                 {
2540                         //fetch contact info
2541                         FetchContactInfoForNumber(__pIncomingCall->GetContactNumber());
2542                         if (__pCachedContact != null)
2543                         {
2544                                 __pIncomingCall->SetContactInfo(*__pCachedContact);
2545                         }
2546                 }
2547
2548                 //construct a new callinfo object to pass its ownership to caller.
2549                 AppCallInfo* pNewIncomingCall = new (std::nothrow) AppCallInfo();
2550                 *pNewIncomingCall = *__pIncomingCall;
2551                 return pNewIncomingCall;
2552         }
2553         //return null, if no incoming call found
2554         return null;
2555 }
2556
2557 void
2558 TelephonyManager::HandleIncomingCallStatusCallBack(TelCallStatus_t* pCallStatus, void* pUserData)
2559 {
2560         TelephonyManager* pTelManager = (TelephonyManager*) pUserData;
2561         if (pCallStatus != null && pCallStatus->bMoCall == false
2562                         && ((pCallStatus->CallState == TAPI_CALL_STATE_INCOMING)
2563                                         || (pCallStatus->CallState == TAPI_CALL_STATE_WAITING)))
2564         {
2565                 //construct incoming call details
2566                 pTelManager->__pIncomingCall = new (std::nothrow) AppCallInfo();
2567                 pTelManager->__pIncomingCall->SetCallHandle(pCallStatus->CallHandle);
2568                 //contact number
2569                 String contactNumber(pCallStatus->pNumber);
2570                 pTelManager->__pIncomingCall->SetContactNumber(contactNumber);
2571                 //set emergency state
2572                 if(pCallStatus->CallType == TAPI_CALL_TYPE_E911)
2573                 {
2574                         pTelManager->__pIncomingCall->SetEmergency(true);
2575                 }
2576                 else
2577                 {
2578                         pTelManager->__pIncomingCall->SetEmergency(false);
2579                 }
2580
2581                 pTelManager->__pIncomingCall->SetCalllogType(CALL_LOG_TYPE_VOICE_INCOMING);
2582         }
2583 }
2584
2585 void
2586 TelephonyManager::SaveCallInfoToLogsDb(AppCallInfo& endCallInfo)
2587 {
2588         if (endCallInfo.IsConferenceCall() == false)
2589         {
2590                 //single active call - Add call ended to call log database
2591                 __pCalllogMgr->AddCallogInfoToDatabase(&endCallInfo);
2592         }
2593         else
2594         {
2595                 //Conference call
2596                 int confCallCount = endCallInfo.GetCallerListCount();
2597                 IListT<AppCallInfo>* pParticipantList = endCallInfo.GetCallerList();
2598                 for (int index = 0; index < confCallCount; index++)
2599                 {
2600                         AppCallInfo participantInfo;
2601                         if (pParticipantList->GetAt(index, participantInfo) == E_SUCCESS)
2602                         {
2603                                 //Add call ended to call log database
2604                                 __pCalllogMgr->AddCallogInfoToDatabase(&participantInfo);
2605                         }
2606                 }
2607         }
2608 }
2609
2610 void
2611 TelephonyManager::OnTelephonyNetworkStatusChanged(const NetworkStatus& networkStatus)
2612 {
2613
2614         if(networkStatus.IsCallServiceAvailable() == false)
2615         {
2616                 EndAllCalls();
2617         }
2618 }