2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
18 * @file PhnCalllogManager.cpp
19 * @brief This class provides call log APIs
21 #include "PhnCalllogManager.h"
24 using namespace Tizen::Base;
25 using namespace Tizen::Base::Collection;
26 using namespace Tizen::Base::Utility;
27 using namespace Tizen::System;
28 using namespace Tizen::Locales;
30 static const int MAX_CALL_LOG_COUNT = 100;
31 CallLogManager* CallLogManager::__pCallogManager = null;
34 LogListComparer::Compare(const CallLogDetails& obj1, const CallLogDetails& obj2, int& cmp) const
36 CallLogDetails* log1 = const_cast<CallLogDetails*>((&obj1));
37 CallLogDetails* log2 = const_cast<CallLogDetails*>((&obj2));
38 if(log1 == null || log2 == null)
42 log1->ConvertTimetoDateTime();
43 DateTime date1 = log1->GetDateTime();
44 //AppLog(" %d %d %s %d:%d:%d", log1->GetCalllogDbId(), log1->GetCalllogType(), log1->GetPhoneNumber(), date1.GetDay(), date1.GetMonth(), date1.GetYear());
45 log2->ConvertTimetoDateTime();
46 DateTime date2 = log2->GetDateTime();
47 //AppLog(" %d %d %s %d:%d:%d", log2->GetCalllogDbId(), log2->GetCalllogType(), log2->GetPhoneNumber(), date2.GetDay(), date2.GetMonth(), date2.GetYear());
48 cmp = date2.CompareTo(date1);
52 CallLogDetails::CallLogDetails(void)
54 dateTime.SetValue(0,0,0);
58 contact_image_path = null;
64 CallLogDetails::~CallLogDetails(void)
66 //NOTE: none of the fields are owned. Free() them will lead to crash.
70 CallLogDetails::operator ==(const CallLogDetails& rhs) const
72 if(this->calllog_db_id == rhs.calllog_db_id)
80 CallLogDetails::operator !=(const CallLogDetails& rhs) const
82 if(this->calllog_db_id != rhs.calllog_db_id)
90 CallLogDetails::operator =(const CallLogDetails& rhs)
92 this->calllog_db_id = rhs.calllog_db_id;
93 this->contact_db_id = rhs.contact_db_id;
94 this->calllog_type = rhs.calllog_type;
95 /*if(rhs.contact_image_path != null)
97 this->contact_image_path = strdup(rhs.contact_image_path);
99 if(rhs.display_name != null)
101 this->display_name = strdup(rhs.display_name);
104 this->duration_sec = rhs.duration_sec;
106 /*if(rhs.first_name != null)
108 this->first_name = strdup(rhs.first_name);
111 if(rhs.last_name != null)
113 this->last_name = strdup(rhs.last_name);
116 if(rhs.phone_number != null)
118 this->phone_number = strdup(rhs.phone_number);
121 /*if(rhs.short_message != null)
123 this->short_message = strdup(rhs.short_message);
126 this->startTime = rhs.startTime;
132 CallLogDetails::SetCalllogDbId(int id)
139 CallLogDetails::GetCalllogDbId(void)
141 return calllog_db_id;
145 CallLogDetails::SetCalllogType(contacts_phone_log_type_e type)
149 case CONTACTS_PLOG_TYPE_VOICE_INCOMMING:
150 calllog_type = CALL_LOG_TYPE_VOICE_INCOMING;
152 case CONTACTS_PLOG_TYPE_VOICE_OUTGOING:
153 calllog_type = CALL_LOG_TYPE_VOICE_OUTGOING;
155 case CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN:
156 calllog_type = CALL_LOG_TYPE_VOICE_MISSED;
158 case CONTACTS_PLOG_TYPE_VOICE_REJECT:
159 calllog_type = CALL_LOG_TYPE_VOICE_REJECTED;
161 case CONTACTS_PLOG_TYPE_VOICE_BLOCKED:
162 calllog_type = CALL_LOG_TYPE_VOICE_BLOCKED;
170 CallLogDetails::GetCalllogType(void)
176 CallLogDetails::SetContactDbId(int id)
182 CallLogDetails::GetContactDbId(void)
184 return contact_db_id;
188 CallLogDetails::SetFirstName(char* name)
190 first_name = strdup(name);
194 CallLogDetails::GetFirstName(void)
200 CallLogDetails::SetLastName(char* name)
202 last_name = strdup(name);
206 CallLogDetails::GetLastName(void)
212 CallLogDetails::SetDisplayName(char* name)
214 display_name = strdup(name);
218 CallLogDetails::GetDisplayName(void)
224 CallLogDetails::SetContactImagePath(char* name)
226 contact_image_path = strdup(name);
230 CallLogDetails::GetContactImagePath(void)
232 return contact_image_path;
236 CallLogDetails::SetPhoneNumber(char* name)
238 phone_number = strdup(name);
242 CallLogDetails::GetPhoneNumber(void)
248 CallLogDetails::SetShortMessage(char* name)
250 short_message = strdup(name);
254 CallLogDetails::GetShortMessage(void)
256 return short_message;
260 CallLogDetails::ConvertTimetoDateTime(void)
263 struct tm* time = null;
264 time = gmtime(&startTime);
265 LocaleManager localManager;
269 dtNow.SetValue(time->tm_year+1900,time->tm_mon+1, time->tm_mday,time->tm_hour, time->tm_min, time->tm_sec);
272 localManager.Construct();
273 tz = localManager.GetSystemTimeZone();
274 dateTime = tz.UtcTimeToWallTime(dtNow);
280 CallLogDetails::SetStartTime(time_t time)
283 ConvertTimetoDateTime();
287 CallLogDetails::GetStartTime(void)
293 CallLogDetails::GetDateTime(void)
299 CallLogDetails::SetDuration(int duration)
301 duration_sec = duration;
305 CallLogDetails::GetDuration(void)
311 CallLogManager::GetInstance(void)
313 if(__pCallogManager == null)
317 return __pCallogManager;
321 CallLogManager::CreateInstance(void)
323 __pCallogManager = new CallLogManager();
324 result r = __pCallogManager->Construct();
327 delete __pCallogManager;
328 __pCallogManager = null;
331 std::atexit(DestroyInstance);
334 CallLogManager::DestroyInstance(void)
336 contacts_disconnect2();
337 delete __pCallogManager;
340 CallLogManager::Construct(void)
342 int ret = contacts_connect2();
348 __pCalllogList = new (std::nothrow) LinkedListT<CallLogDetails>;
349 __pCalllogChangeListerners = new (std::nothrow) ArrayListT<ICalllogChangeListener*>;
350 __pCalllogChangeListerners->Construct(10);
351 __pCalllogDBIds = new (std::nothrow) HashMapT<int,int>();
352 __pCalllogDBIds->Construct(100);
354 GetAllCallogDataFromDatabseCB();
357 //calllog_add_calllog_db_changed_cb(changed_db_cb, this);
361 ///////////////////////////////////////Calllog Manager////////////////////////////////////////////////////////////////////////////////
362 CallLogManager::CallLogManager(void)
364 __pCalllogList = null;
365 __pCalllogListByNumber = null;
367 __pCalllogChangeListerners = null;
370 CallLogManager::~CallLogManager(void)
372 if (__pCalllogList != null)
374 __pCalllogList->RemoveAll();
375 delete __pCalllogList;
376 __pCalllogList = null;
378 if(__pCalllogListByNumber != null)
380 __pCalllogListByNumber = null;
382 if(__pNumber != null)
387 if(__pCalllogChangeListerners != null)
389 delete __pCalllogChangeListerners;
391 __pCallogManager = null;
395 CallLogManager::GetDuration(long long start_time)
398 unsigned long call_duration_in_sec = 50;
400 /*if(start_time != 0)
404 call_duration_in_sec = curr_time - start_time;
405 return call_duration_in_sec;
409 CallLogManager::AddCallogInfoToDatabase(CallInfo* calllogInfo)
411 AppLog(" AddVoiceCallInfo Entry");
415 if(calllogInfo != null)
417 CallLogType callLogType = calllogInfo->GetCalllogType();
418 contacts_phone_log_type_e logType = CONTACTS_PLOG_TYPE_NONE;
421 case CALL_LOG_TYPE_VOICE_INCOMING:
422 logType = CONTACTS_PLOG_TYPE_VOICE_INCOMMING;
424 case CALL_LOG_TYPE_VOICE_OUTGOING:
425 logType = CONTACTS_PLOG_TYPE_VOICE_OUTGOING;
427 case CALL_LOG_TYPE_VOICE_MISSED:
428 logType = CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN;
430 case CALL_LOG_TYPE_VOICE_REJECTED:
431 logType = CONTACTS_PLOG_TYPE_VOICE_REJECT;
433 case CALL_LOG_TYPE_VOICE_BLOCKED:
434 logType = CONTACTS_PLOG_TYPE_VOICE_BLOCKED;
439 startTime = calllogInfo->GetCallNotificationTime();
440 if ((logType == CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN)
441 || (logType == CONTACTS_PLOG_TYPE_VOICE_REJECT)
442 || (logType == CONTACTS_PLOG_TYPE_VOICE_BLOCKED))
448 //start time is in miliseconds . so convert to seconds and set it to time_t format.
449 long long connectTime = calllogInfo->GetCallConnectTime();
450 if(connectTime == 0 || connectTime < 0)
452 connectTime = time(null);
456 connectTime = calllogInfo->GetCallConnectTime()/ 1000;
458 duration = (int)GetDuration(connectTime);
461 String PhNumber(L"");
462 ByteBuffer* pByteBuffer = null;
464 if(calllogInfo->GetContactNumber().IsEmpty() == false)
466 PhNumber.Append(calllogInfo->GetContactNumber());
467 pByteBuffer = StringUtil::StringToUtf8N(PhNumber);
471 contacts_record_h hContactLog=0;
472 int ret = contacts_record_create(_contacts_phone_log._uri,&hContactLog);
480 if (contacts_record_set_int(hContactLog,_contacts_phone_log.log_type,logType) != CONTACTS_ERROR_NONE)
482 AppLog(" calllog_set_type is failed");
484 else if (contacts_record_set_int(hContactLog,_contacts_phone_log.log_time,startTime/1000) != CONTACTS_ERROR_NONE)
486 AppLog(" calllog_set_time is failed");
488 else if (contacts_record_set_int(hContactLog,_contacts_phone_log.extra_data1,duration) != CONTACTS_ERROR_NONE)
490 AppLog(" calllog_set_duration is failed");
492 else if ((pByteBuffer != null) && (contacts_record_set_str(hContactLog,_contacts_phone_log.address,(const char*) pByteBuffer->GetPointer()) != CONTACTS_ERROR_NONE))
494 AppLog(" calllog_set_number is failed");
496 else if (contacts_db_insert_record(hContactLog,&id) != CONTACTS_ERROR_NONE)
498 AppLog(" calllog_insert_to_db is failed");
502 AppLog(" Call log is added successfully");
507 AppLog(" AddVoiceCallInfo Exit");
512 CallLogManager::CopyDataToCallLogDetails(CallLogDetails* calllogInfo , contacts_record_h getrec)
515 contacts_record_get_int(getrec,_contacts_phone_log.id,&logid);
516 calllogInfo->SetCalllogDbId(logid);
518 contacts_phone_log_type_e logType=CONTACTS_PLOG_TYPE_NONE;
519 contacts_record_get_int(getrec,_contacts_phone_log.log_type,(int*)&logType);
520 calllogInfo->SetCalllogType(logType);
523 contacts_record_get_int(getrec,_contacts_phone_log.log_time,&log_time);
524 calllogInfo->SetStartTime((time_t)log_time);
526 char* address = null;
527 int ret = contacts_record_get_str(getrec,_contacts_phone_log.address,&address);
528 if(ret == 0 && address != null)
530 calllogInfo->SetPhoneNumber(address);
534 contacts_record_get_int(getrec,_contacts_phone_log.extra_data1,&duration);
535 calllogInfo->SetDuration(duration);
538 contacts_record_get_str(getrec,_contacts_phone_log.extra_data2,&shortMsg);
540 calllogInfo->SetShortMessage(shortMsg);
545 CallLogManager::GetAllCallogDataFromDatabseCB()
547 contacts_list_h list;
551 contacts_db_get_count(_contacts_phone_log._uri,&calLogCount);
553 if(calLogCount > MAX_CALL_LOG_COUNT)
555 ret = contacts_db_get_all_records(_contacts_phone_log._uri,calLogCount - MAX_CALL_LOG_COUNT,MAX_CALL_LOG_COUNT,&list);
559 ret = contacts_db_get_all_records(_contacts_phone_log._uri,0,MAX_CALL_LOG_COUNT,&list);
568 ret = contacts_list_get_count(list,&count);
569 contacts_list_first(list);
570 for (unsigned int i = 0; i < count; i++)
572 CallLogDetails* calllogInfo = new CallLogDetails();
574 contacts_record_h getrec=0;
575 ret = contacts_list_get_current_record_p(list,&getrec);
578 contacts_phone_log_type_e logType=CONTACTS_PLOG_TYPE_NONE;
579 contacts_record_get_int(getrec,_contacts_phone_log.log_type,(int*)&logType);
581 if(logType == CONTACTS_PLOG_TYPE_VOICE_INCOMMING ||
582 logType == CONTACTS_PLOG_TYPE_VOICE_OUTGOING ||
583 logType == CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN||
584 logType == CONTACTS_PLOG_TYPE_VOICE_REJECT ||
585 logType == CONTACTS_PLOG_TYPE_VOICE_BLOCKED)
588 CopyDataToCallLogDetails(calllogInfo, getrec);
589 if(__pCalllogDBIds->Add(calllogInfo->GetCalllogDbId(),calllogInfo->GetCalllogDbId()) == E_SUCCESS)
591 __pCalllogList->InsertAt(*calllogInfo, 0);
596 //move to next record
597 ret = contacts_list_next(list);
602 contacts_record_destroy(getrec,true);
610 CallLogManager::ResetAndNotifyCalllogData(void)
612 if(__pCalllogList != null)
614 __pCalllogList->RemoveAll();
615 delete __pCalllogList;
617 if(__pCalllogDBIds != null)
619 __pCalllogDBIds->RemoveAll();
620 delete __pCalllogDBIds;
623 __pCalllogList = new (std::nothrow) LinkedListT<CallLogDetails>;
624 __pCalllogDBIds = new (std::nothrow) HashMapT<int,int>;
625 __pCalllogDBIds->Construct(100);
627 GetAllCallogDataFromDatabseCB();
630 //__pCalllogChangeListerners->GetEnumeratorN();
632 IEnumeratorT<ICalllogChangeListener*>* pListenerEnum = null;
633 result r = E_FAILURE;
634 if (__pCalllogChangeListerners != null)
636 pListenerEnum = __pCalllogChangeListerners->GetEnumeratorN();
637 while ((r = pListenerEnum->MoveNext()) != E_OUT_OF_RANGE)
639 ICalllogChangeListener* pListener;
640 r = pListenerEnum->GetCurrent(pListener);
641 pListener->OnCalllogChanged();
650 IListT<CallLogDetails>*
651 CallLogManager::GetCalllogListByNumValues(void) //to be verified
653 if(__pCalllogListByNumber != null)
655 //return __pCalllogListByNumber->GetValuesN();
656 return __pCalllogListByNumber->GetItemsN(0, __pCalllogListByNumber->GetCount());
662 CallLogManager::GetCallogListByNumberFromDatabaseCB(char* number)
665 contacts_filter_h filter=NULL;
666 contacts_list_h list=NULL;
667 contacts_query_h query = NULL;
668 unsigned int count = 0;
671 contacts_filter_create(_contacts_phone_log._uri,&filter);
672 contacts_filter_add_str(filter,_contacts_phone_log.address,CONTACTS_MATCH_EXACTLY,number);
673 contacts_query_create(_contacts_phone_log._uri,&query);
674 contacts_query_set_filter(query,filter);
676 ret = contacts_db_get_records_with_query(query,0,0,&list);
679 contacts_filter_destroy(filter);
680 contacts_query_destroy(query);
683 contacts_filter_destroy(filter);
684 contacts_query_destroy(query);
687 contacts_list_get_count(list,&count);
688 contacts_list_first(list);
689 for(unsigned int i=0;i<count;i++)
691 CallLogDetails* calllogInfo = new CallLogDetails();
692 contacts_record_h getrec=0;
693 result ret = contacts_list_get_current_record_p(list,&getrec);
696 //__pCalllogListByNumber->Add( calllogInfo->GetCalllogDbId(), *calllogInfo);
697 contacts_phone_log_type_e logType=CONTACTS_PLOG_TYPE_NONE;
698 contacts_record_get_int(getrec,_contacts_phone_log.log_type,(int*)&logType);
700 if(logType == CONTACTS_PLOG_TYPE_VOICE_INCOMMING ||
701 logType == CONTACTS_PLOG_TYPE_VOICE_OUTGOING ||
702 logType == CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN||
703 logType == CONTACTS_PLOG_TYPE_VOICE_REJECT ||
704 logType == CONTACTS_PLOG_TYPE_VOICE_BLOCKED)
706 CopyDataToCallLogDetails(calllogInfo, getrec);
707 __pCalllogListByNumber->Add(*calllogInfo); //to be verified
712 ret = contacts_list_next(list);
717 contacts_record_destroy(getrec,true);
723 Tizen::Base::Collection::ArrayListT<CallLogDetails>*
724 CallLogManager::GetCallogListByNumberFromDatabaseN(char* number)
726 __pCalllogListByNumber = new (std::nothrow) Tizen::Base::Collection::ArrayListT<CallLogDetails>(); //to be verified
727 __pCalllogListByNumber->Construct(100);
728 if(__pNumber != null)
732 int len = strlen(number) + 1;
733 __pNumber = new (std::nothrow) char[len];
734 strcpy(__pNumber,number);
736 GetCallogListByNumberFromDatabaseCB(number);
738 if (__pCalllogListByNumber->GetCount() > 0)
740 SortCalllogListByNumber();
741 return __pCalllogListByNumber;
743 delete __pCalllogListByNumber;
744 __pCalllogListByNumber = null;
748 HashMapT<int, CallLogDetails>*
749 CallLogManager::GetCallogListByUnknownNumberFromDatabaseN(char* number)
751 //Used to fetch list of call from unknown numbers from logs.
752 __pCalllogUnkownListByNumber = new (std::nothrow) HashMapT<int, CallLogDetails>();
753 __pCalllogUnkownListByNumber->Construct(100);
754 if(__pNumber != null)
758 int len = strlen(number) + 1;
759 __pNumber = new (std::nothrow) char[len];
760 strcpy(__pNumber,number);
762 GetCallogListByNumberFromDatabaseCB(number);
763 if (__pCalllogUnkownListByNumber->GetCount() > 0)
765 return __pCalllogUnkownListByNumber;
767 delete __pCalllogUnkownListByNumber;
768 __pCalllogUnkownListByNumber = null;
773 CallLogManager::changed_db_cb()
775 ResetAndNotifyCalllogData();
780 CallLogManager::DeleteCalllogByDbId(int dbId)
783 contacts_filter_h filter=NULL;
784 contacts_list_h list=NULL;
785 contacts_query_h query = NULL;
788 contacts_filter_create(_contacts_phone_log._uri,&filter);
789 contacts_filter_add_int(filter,_contacts_phone_log.id,CONTACTS_MATCH_EQUAL,dbId);
790 contacts_query_create(_contacts_phone_log._uri,&query);
791 contacts_query_set_filter(query,filter);
793 ret = contacts_db_get_records_with_query(query,0,0,&list);
796 contacts_filter_destroy(filter);
797 contacts_query_destroy(query);
800 contacts_filter_destroy(filter);
801 contacts_query_destroy(query);
804 ret = contacts_list_get_count(list,&count);
805 contacts_list_first(list);
806 for(unsigned int i=0;i<count;i++)
808 contacts_record_h getrec=0;
809 ret = contacts_list_get_current_record_p(list,&getrec);
810 contacts_db_delete_record(_contacts_phone_log._uri,dbId);
811 contacts_record_destroy(getrec,true);
818 CallLogManager::DeleteAllCalllog(void)
822 AppLogDebug("Enter");
823 count = __pCalllogList->GetCount();
825 for(int i=0; i< count; i++)
827 CallLogDetails latestCallLog;
828 if(__pCalllogList->GetAt(i,latestCallLog) == E_SUCCESS)
830 contacts_db_delete_record(_contacts_phone_log._uri,latestCallLog.GetCalllogDbId());
834 contacts_phone_log_reset_statistics();
839 CallLogManager::GetAllCalllogCount(void)
841 if(__pCalllogList != null)
843 return __pCalllogList->GetCount();
849 CallLogManager::GetCalllogCountByNumber(void)
851 if(__pCalllogListByNumber != null)
853 return __pCalllogListByNumber->GetCount();
859 CallLogManager::SortCalllogList(void)
862 __pCalllogList->Sort(cmp);
866 CallLogManager::SortCalllogListByNumber(void) //to be verified
869 __pCalllogListByNumber->Sort(cmp);
873 CallLogManager::GetLatestCallLogFromDbN(void)
875 String* latestNumber = null;
876 if(__pCalllogList != null && __pCalllogList->GetCount() > 0)
878 CallLogDetails latestCallLog;
879 if(__pCalllogList->GetAt(0,latestCallLog) == E_SUCCESS)
881 latestNumber = new String(latestCallLog.GetPhoneNumber());
888 result CallLogManager::GetCallLogItemAtIndex(int index, CallLogDetails& calllogInfo)
890 return __pCalllogList->GetAt(index,calllogInfo);
896 void CallLogManager::AddCalllogChangeListener(ICalllogChangeListener& listner )
898 if(!__pCalllogChangeListerners->Contains(&listner))
899 __pCalllogChangeListerners->Add(&listner);
903 void CallLogManager::RemoveCalllogChangeListner(ICalllogChangeListener& listner)
905 __pCalllogChangeListerners->Remove(&listner);
908 void CallLogManager::GetCalllogContactName(String number,String& name)
911 ByteBuffer* pByteBuffer;
912 char * charname=NULL;
913 pByteBuffer = StringUtil::StringToUtf8N(number);
915 contacts_filter_h filter=NULL;
916 contacts_list_h list=NULL;
917 contacts_query_h query = NULL;
918 unsigned int count = 0;
920 contacts_filter_create(_contacts_contact_number._uri,&filter);
921 contacts_filter_add_str(filter,_contacts_contact_number.number,CONTACTS_MATCH_EXACTLY,(const char*)pByteBuffer->GetPointer());
922 contacts_query_create(_contacts_contact_number._uri,&query);
923 contacts_query_set_filter(query,filter);
925 int ret = contacts_db_get_records_with_query(query,0,0,&list);
928 contacts_filter_destroy(filter);
929 contacts_query_destroy(query);
933 contacts_filter_destroy(filter);
934 contacts_query_destroy(query);
937 contacts_list_get_count(list,&count);
938 AppLogTag("Amith","%d",count);
941 contacts_record_h getrec=0;
942 contacts_list_first(list);
943 contacts_list_get_current_record_p(list,&getrec);
945 contacts_record_get_str(getrec,_contacts_contact_number.display_name,&charname);
946 AppLogTag("Amith","%s",charname);
949 String buffer(charname);
954 contacts_record_destroy(getrec,true);