2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.1 (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 CalllogManager.cpp
19 * @brief This class provides call log APIs
23 #include "CalllogManager.h"
25 using namespace Tizen::Base;
26 using namespace Tizen::Base::Collection;
27 using namespace Tizen::Base::Utility;
28 using namespace Tizen::System;
29 using namespace Tizen::Locales;
31 CallLogManager* CallLogManager::__pCallogManager = null;
33 CallLogManager::CallLogManager(void)
36 pAddressBook = AddressbookManager::GetInstance()->GetAddressbookN();
39 CallLogManager::~CallLogManager(void)
41 __pCallogManager = null;
45 CallLogManager::GetInstance(void)
47 if(__pCallogManager == null)
51 return __pCallogManager;
55 CallLogManager::CreateInstance(void)
57 __pCallogManager = new CallLogManager();
58 result r = __pCallogManager->Construct();
61 delete __pCallogManager;
62 __pCallogManager = null;
65 std::atexit(DestroyInstance);
69 CallLogManager::Construct(void)
71 int ret = contacts_connect2();
81 CallLogManager::DestroyInstance(void)
83 contacts_disconnect2();
84 delete __pCallogManager;
88 CallLogManager::AddCallogInfoToDatabase(AppCallInfo* calllogInfo)
90 AppLog(" AddVoiceCallInfo Entry");
94 if(calllogInfo != null)
96 CallLogType callLogType = calllogInfo->GetCalllogType();
97 contacts_phone_log_type_e logType = CONTACTS_PLOG_TYPE_NONE;
100 case CALL_LOG_TYPE_VOICE_INCOMING:
101 logType = CONTACTS_PLOG_TYPE_VOICE_INCOMMING;
103 case CALL_LOG_TYPE_VOICE_OUTGOING:
104 logType = CONTACTS_PLOG_TYPE_VOICE_OUTGOING;
106 case CALL_LOG_TYPE_VOICE_MISSED_UNSEEN:
107 logType = CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN;
109 case CALL_LOG_TYPE_VOICE_REJECTED:
110 logType = CONTACTS_PLOG_TYPE_VOICE_REJECT;
112 case CALL_LOG_TYPE_VOICE_BLOCKED:
113 logType = CONTACTS_PLOG_TYPE_VOICE_BLOCKED;
118 startTime = calllogInfo->GetCallNotificationTime();
119 if ((logType == CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN)
120 || (logType == CONTACTS_PLOG_TYPE_VOICE_REJECT)
121 || (logType == CONTACTS_PLOG_TYPE_VOICE_BLOCKED))
127 //start time is in miliseconds . so convert to seconds and set it to time_t format.
128 long long connectTime = calllogInfo->GetDurationTime();
129 if(connectTime == 0 || connectTime < 0)
131 //connectTime = time(null);
132 CallApp* pPhoneApp = static_cast<CallApp*>(CallApp::GetInstance());
133 connectTime = pPhoneApp->GetCallAppTicks();
138 connectTime = calllogInfo->GetDurationTime()/ 1000;
140 duration = (int)GetDuration(connectTime);
143 String PhNumber(L"");
144 ByteBuffer* pByteBuffer = null;
146 if(calllogInfo->GetContactNumber().IsEmpty() == false)
148 PhNumber.Append(calllogInfo->GetContactNumber());
149 pByteBuffer = StringUtil::StringToUtf8N(PhNumber);
153 contacts_record_h hContactLog=0;
154 int ret = contacts_record_create(_contacts_phone_log._uri,&hContactLog);
161 int personid = GetPersonId(PhNumber);
164 if(contacts_record_set_int(hContactLog,_contacts_phone_log.person_id,personid) != CONTACTS_ERROR_NONE)
166 AppLog(" calllog_set_type personid failed");
170 if (contacts_record_set_int(hContactLog,_contacts_phone_log.log_type,logType) != CONTACTS_ERROR_NONE)
172 AppLog(" calllog_set_type is failed");
174 else if (contacts_record_set_int(hContactLog,_contacts_phone_log.log_time,startTime/1000) != CONTACTS_ERROR_NONE)
176 AppLog(" calllog_set_time is failed");
178 else if (contacts_record_set_int(hContactLog,_contacts_phone_log.extra_data1,duration) != CONTACTS_ERROR_NONE)
180 AppLog(" calllog_set_duration is failed");
182 else if ((pByteBuffer != null) && (contacts_record_set_str(hContactLog,_contacts_phone_log.address,(const char*) pByteBuffer->GetPointer()) != CONTACTS_ERROR_NONE))
184 AppLog(" calllog_set_number is failed");
186 else if (contacts_db_insert_record(hContactLog,&id) != CONTACTS_ERROR_NONE)
188 AppLog(" calllog_insert_to_db is failed");
192 AppLog(" Call log is added successfully");
196 AppLog(" AddVoiceCallInfo Exit");
201 CallLogManager::GetDuration(long long start_time)
204 unsigned long call_duration_in_sec = 50;
205 // curr_time = time(0);
206 CallApp* pPhoneApp = static_cast<CallApp*>(CallApp::GetInstance());
207 curr_time = pPhoneApp->GetCallAppTicks();
209 /*if(start_time != 0)
213 call_duration_in_sec = curr_time - start_time;
214 return call_duration_in_sec;
217 CallLogManager::GetPersonId(Tizen::Base::String& phoneNumber)
219 AppLogDebug("Enter");
221 bool bContactFound = false;
222 if (pAddressBook != null)
224 IList* pNumberContactsList = pAddressBook->SearchContactsByPhoneNumberN(phoneNumber);
225 if (pNumberContactsList == null || IsFailed(GetLastResult()))
230 //Fetch the contact's info to be displayed
231 IEnumerator* pContactEnum = pNumberContactsList->GetEnumeratorN();
232 if(pNumberContactsList != null)
234 while (E_SUCCESS == pContactEnum->MoveNext())
236 Contact* pContact = static_cast<Contact*>(pContactEnum->GetCurrent());
238 IList* pPhoneNumberList = pContact->GetValuesN(CONTACT_MPROPERTY_ID_PHONE_NUMBERS);
239 if (pPhoneNumberList != null)
241 IEnumerator* pPhoneEnum = pPhoneNumberList->GetEnumeratorN();
242 while (E_SUCCESS == pPhoneEnum->MoveNext())
244 PhoneNumber* pPhoneNumber = (PhoneNumber*) pPhoneEnum->GetCurrent();
245 //Check if this is the correct contact
246 if (pPhoneNumber->GetPhoneNumber().Equals(phoneNumber))
248 //save newly fetched contact info.
249 bContactFound = true;
250 personId = pContact->GetPersonId();
255 pPhoneNumberList->RemoveAll(true);
256 delete pPhoneNumberList;
258 if(bContactFound == true)
265 pNumberContactsList->RemoveAll(true);
266 delete pNumberContactsList;