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 PhnDialPresentationModel.cpp
19 * @brief Dial presentation model class
22 #include <FTelephony.h>
23 #include "PhnAppUtility.h"
24 #include "PhnCalllogManager.h"
25 #include "PhnCommonUtils.h"
26 #include "PhnDialContactInfo.h"
27 #include "PhnDialPresentationModel.h"
28 #include "PhnPhoneApp.h"
29 #include "PhnSettingsManager.h"
30 #include "PhnTabDialer.h"
33 using namespace Tizen::App;
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36 using namespace Tizen::Base::Runtime;
37 using namespace Tizen::Social;
38 using namespace Tizen::Telephony;
40 DialPresentationModel::DialPresentationModel(DialTabDialer& parentPanel) : __parentDialerPanel(parentPanel)
43 __pNumberSuggestionList = null;
44 __pSearchString = null;
45 __pSettingsManager = null;
46 __pAsyncSearchTask = null;
47 __pSearchThread = null;
52 DialPresentationModel::~DialPresentationModel()
60 DialPresentationModel::ClearSearchResults(void)
62 if(__pNumberSuggestionList != null)
65 __pNumberSuggestionList->RemoveAll();
66 delete __pNumberSuggestionList;
67 __pNumberSuggestionList = null;
70 if(__pSearchString != null)
72 delete __pSearchString;
73 __pSearchString = null;
78 DialPresentationModel::FetchSuggestionList(const String& searchString)
80 bool newSearch = false;
81 if (__pSearchThread != null)
83 //Wait until the previous thread finished execution.
84 //otherwise "start_thread()" crashes.
85 __pSearchThread->Join();
86 delete __pSearchThread;
87 __pSearchThread = null;
88 delete __pAsyncSearchTask;
89 __pAsyncSearchTask = null;
93 __pAsyncSearchTask = new (std::nothrow) RetrieveContactsListTask( *this);
94 //check if this search string is subset of previous search string
95 if(__pSearchString != null && searchString.StartsWith(*__pSearchString,0))
98 __pSearchString->Clear();
99 __pSearchString->Append(searchString);
101 r = __pAsyncSearchTask->Construct(*__pSearchString, __pNumberSuggestionList);
107 __pSearchString = new (std::nothrow) String(searchString);
108 r = __pAsyncSearchTask->Construct(*__pSearchString, null);
110 TryCatch(r == E_SUCCESS,,"Async Task construction failed");
112 //Start Async Task to search for contacts, call logs, Speed dial.
113 __pSearchThread = new (std::nothrow) Thread();
114 r = __pSearchThread->Construct(*__pAsyncSearchTask);
115 TryCatch(r == E_SUCCESS,,"Thread construction failed");
116 r = __pSearchThread->Start();
117 TryCatch(r == E_SUCCESS,,"Failed to start Thread");
122 //clear previous search results
123 ClearSearchResults();
124 delete __pAsyncSearchTask;
125 __pAsyncSearchTask = null;
126 delete __pSearchThread;
127 __pSearchThread = null;
132 DialPresentationModel::HandleContactsRetrievalCompleted(IList* pContactsList)
135 if(__pNumberSuggestionList != null)
137 __pNumberSuggestionList->RemoveAll();
138 delete __pNumberSuggestionList;
139 __pNumberSuggestionList = null;
142 if(pContactsList != null && pContactsList->GetCount() > 0)
144 //Taking Ownership of List.
145 __pNumberSuggestionList = static_cast<ArrayList*>(pContactsList);
147 //inform Tab Dialer to update view.
148 DialContactInfo* pDialInfo = null;
149 //Fetch the contact's info at a given index
150 if (__pNumberSuggestionList != null && __pNumberSuggestionList->GetCount() > 0)
152 pDialInfo = static_cast<DialContactInfo*>(__pNumberSuggestionList->GetAt(0));
155 if(pDialInfo != null)
157 //send first search result
158 ArrayList* pArgs = new (std::nothrow) ArrayList(SingleObjectDeleter);
161 //Send display name, mobile number, matched search string and result count.
162 String* argDisplayName = new (std::nothrow) String();
163 argDisplayName->Append(pDialInfo->GetDisplayName());
164 pArgs->Add(argDisplayName);
166 String* argMobilNum = new (std::nothrow) String();
167 argMobilNum->Append(pDialInfo->GetPhoneNumber());
168 pArgs->Add(argMobilNum);
170 String* argSearchString = new (std::nothrow) String();
171 argSearchString->Append(*(pDialInfo->GetSearchKey()));
172 pArgs->Add(argSearchString);
174 //fetch total no. of results
175 String* argResCount = new (std::nothrow) String();
176 argResCount->Append(__pNumberSuggestionList->GetCount());
177 pArgs->Add(argResCount);
178 __parentDialerPanel.SendUserEvent(REQUEST_SHOW_SUGGESTION,pArgs);
183 //remove old search key
184 if(__pSearchString != null)
186 delete __pSearchString;
187 __pSearchString = null;
189 //Show "Add to contact"
190 ArrayList* pArgs = new (std::nothrow) ArrayList(SingleObjectDeleter);
192 __parentDialerPanel.SendUserEvent(REQUEST_SHOW_ADD_TO_CONTACTS,pArgs);
194 delete pContactsList;
195 pContactsList = null;
201 DialPresentationModel::GetSuggestionAtIndex(int index)
203 DialContactInfo* pDialInfo = null;
205 //Fetch the contact's info at a given index
206 if (__pNumberSuggestionList != null
207 && __pNumberSuggestionList->GetCount() > 0
208 && (index >= 0 && index < __pNumberSuggestionList->GetCount()))
210 pDialInfo = static_cast<DialContactInfo*>(__pNumberSuggestionList->GetAt(index));
217 DialPresentationModel::GetNumberOfSuggestions(void)
220 if(__pNumberSuggestionList != null)
223 resultCount = __pNumberSuggestionList->GetCount();
230 DialPresentationModel::FetchSpeedDialNumberN(int rowId)
232 if(__pSettingsManager == null)
234 __pSettingsManager = SettingsManager::GetInstance();
236 return __pSettingsManager->GetMappedSpeedDialNumberN(rowId);
240 DialPresentationModel::FetchLatestNumberFromLogsN(void)
242 String* pRetString = CallLogManager::GetInstance()->GetLatestCallLogFromDbN();
247 DialPresentationModel::LaunchAddContactsAppControl(String& contactNumber)
249 result r = E_FAILURE;
251 extraData.Construct();
253 extraData.Add(new (std::nothrow) String(INPUT_TYPE_PHONE), new (std::nothrow) String(contactNumber));
254 extraData.Add(new (std::nothrow) String(CONTACT_KEY_ITEM_TYPE), new (std::nothrow) String(ITEM_TYPE_CONTACT));
255 AppControl* pAc = AppManager::FindAppControlN(PROVIDER_ID_CONTACTS, OPERATION_ID_ADD);
258 r = pAc->Start(null, null, &extraData, this);
262 extraData.RemoveAll(true);
267 DialPresentationModel::DialCall(const String& contactNumber)
269 AppLogDebug("ENTER");
270 result r = E_FAILURE;
271 r = LaunchCallAppControl(contactNumber);
278 return ERROR_DIAL_FAILED;
283 DialPresentationModel::LaunchCallAppControl(const String& contactNumber)
285 result r = E_FAILURE;
288 extraData.Construct();
289 extraData.Add(new (std::nothrow) String(PARAM_PHONE_NUMBER), new (std::nothrow) String(contactNumber));
290 extraData.Add(new (std::nothrow) String(PARAM_CALL_TYPE), new (std::nothrow) String(PARAM_CALL_VALUE_VOICE));
292 AppControl* pAppControl = AppManager::FindAppControlN(PROVIDER_ID_CALL, OPERATION_ID_CALL);
293 if (pAppControl != null)
295 r = pAppControl->Start(null, null, &extraData, this);
299 extraData.RemoveAll(true);
304 DialPresentationModel::OpenMessagingAppControl(String& contactNumber)
307 result r = E_SUCCESS;
309 extraData.Construct();
311 extraData.Add(new (std::nothrow) String(MESSAGE_TYPE), new (std::nothrow) String(MESSAGE_SMS_TYPE));
312 extraData.Add(new (std::nothrow) String(MESSAGE_TO), new (std::nothrow) String(contactNumber));
314 AppControl* pAc = AppManager::FindAppControlN(PROVIDER_ID_MESSAGE, OPERATION_ID_COMPOSE);
317 r = pAc->Start(null, null, &extraData, this);
320 extraData.RemoveAll(true);
326 DialPresentationModel::OnAppControlCompleteResponseReceived(const AppId& appId, const String& operationId, AppCtrlResult appControlResult, const IMap* pExtraData)
328 //reset 'isAppControlLaunched'
329 AppLogDebug("Enter");
330 // __parentDialerPanel.SetAppControlLaunched(false);
332 if (appId.Equals(String(PROVIDER_ID_CONTACTS)) && operationId.Equals(String(OPERATION_ID_ADD)))
334 if (appControlResult == APP_CTRL_RESULT_SUCCEEDED)
336 //delete search string to start new search
337 if(__pSearchString != null)
339 delete __pSearchString;
340 __pSearchString = null;
342 //Fetch latest added contact and show in suggestion list.
343 __parentDialerPanel.UpdateSearchResults();