Fix for N_SE-48378 N_SE-48015
[apps/osp/Call.git] / src / CallAppControlRequestMgr.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
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        CallAppControlRequestMgr.cpp
19  * @brief       Manages AppControl Request to other apps.
20  */
21 #include <FApp.h>
22 #include "CallAppControlRequestMgr.h"
23 #include "CallTypes.h"
24
25 using namespace Tizen::App;
26 using namespace Tizen::Base;
27 using namespace Tizen::Base::Collection;
28
29 CallAppControlRequestMgr* CallAppControlRequestMgr::__pInstance = null;
30
31 CallAppControlRequestMgr::CallAppControlRequestMgr(void)
32 {
33         __pOpenedAppControl = null;
34 }
35
36 CallAppControlRequestMgr::~CallAppControlRequestMgr(void)
37 {
38         if (__pOpenedAppControl != null)
39         {
40                 delete __pOpenedAppControl;
41                 __pOpenedAppControl = null;
42         }
43 }
44
45 void
46 CallAppControlRequestMgr::CreateInstance(void)
47 {
48         __pInstance = new (std::nothrow) CallAppControlRequestMgr();
49         result r = __pInstance->Construct();
50         if(IsFailed(r))
51         {
52                 delete __pInstance;
53                 __pInstance = null;
54                 return;
55         }
56
57         std::atexit(DestroyInstance);
58 }
59
60 CallAppControlRequestMgr*
61 CallAppControlRequestMgr::GetInstance(void)
62 {
63         if (__pInstance == null)
64         {
65                 CreateInstance();
66         }
67         return __pInstance;
68 }
69
70 void
71 CallAppControlRequestMgr::DestroyInstance(void)
72 {
73         if (__pInstance != null)
74         {
75                 delete __pInstance;
76                 __pInstance = null;
77         }
78 }
79
80 result
81 CallAppControlRequestMgr::Construct(void)
82 {
83         return E_SUCCESS;
84 }
85
86 bool
87 CallAppControlRequestMgr::IsAppControlRunning(void)
88 {
89         AppLog("ENTER");
90         return (__pOpenedAppControl != null);
91 }
92
93 void
94 CallAppControlRequestMgr::AbortAppControlRequest(void)
95 {
96         AppLog("ENTER");
97         if (__pOpenedAppControl != null)
98         {
99                 result r = __pOpenedAppControl->Stop();
100                 AppLog("Abort failed with r=%d", r);
101                 delete __pOpenedAppControl;
102                 __pOpenedAppControl = null;
103         }
104         AppLog("ENTER");
105 }
106
107 void
108 CallAppControlRequestMgr::AppControlRequestCompleted(void)
109 {
110         AppLog("ENTER");
111         if (__pOpenedAppControl != null)
112         {
113                 delete __pOpenedAppControl;
114                 __pOpenedAppControl = null;
115         }
116 }
117
118 bool
119 CallAppControlRequestMgr::LaunchComposeMessageAppControl(String& contactNumber, IAppControlResponseListener* pListener)
120 {
121         String tempContactNumber;
122         int commaIndex = 0;
123         int semiColonIndex = 0;
124         int index = 0;
125         contactNumber.IndexOf(",", 0, commaIndex);
126         contactNumber.IndexOf(";", 0, semiColonIndex);
127
128         if(commaIndex != -1 && semiColonIndex != -1)
129         {
130                 index = commaIndex < semiColonIndex ? commaIndex : semiColonIndex;
131         }
132         else if(commaIndex != -1 || semiColonIndex != -1)
133         {
134                 index = commaIndex > semiColonIndex ? commaIndex : semiColonIndex;
135         }
136         else
137         {
138                 index = contactNumber.GetLength();
139         }
140         contactNumber.SubString(0, index, tempContactNumber);
141
142         if (__pOpenedAppControl != null)
143         {
144                 //An AppControl is already running.
145                 String provider = __pOpenedAppControl->GetAppControlProviderId();
146                 String operation = __pOpenedAppControl->GetOperationId();
147                 AppLog( "Error:- AppControl is already running for provider id = %ls, operation id = %ls",
148                                 provider.GetPointer(), operation.GetPointer());
149                 return false;
150         }
151
152         result r = E_FAILURE;
153         //request parameters
154         HashMap extraData;
155         extraData.Construct();
156         extraData.Add(new (std::nothrow) String(MESSAGE_TYPE), new (std::nothrow) String(MESSAGE_SMS_TYPE));
157         extraData.Add(new (std::nothrow) String(MESSAGE_TO), new (std::nothrow) String(tempContactNumber));
158
159         __pOpenedAppControl = AppManager::FindAppControlN(PROVIDER_ID_MESSAGE, OPERATION_ID_COMPOSE);
160         if (__pOpenedAppControl != null)
161         {
162                 r = __pOpenedAppControl->Start(null, null, &extraData, pListener);
163         }
164         extraData.RemoveAll(true);
165
166         if(r != E_SUCCESS)
167         {
168                 //AppControl request failed, delete AppControl.
169                 delete __pOpenedAppControl;
170                 __pOpenedAppControl = null;
171                 return false;
172         }
173         else
174         {
175                 //AppControl request was successful.
176                 return true;
177         }
178 }
179
180 bool
181 CallAppControlRequestMgr::LaunchViewContactAppControl(String& contactId, IAppControlResponseListener* pListener)
182 {
183         AppLogDebug("ENTER");
184         if (__pOpenedAppControl != null)
185         {
186                 //An AppControl is already running.
187                 String provider = __pOpenedAppControl->GetAppControlProviderId();
188                 String operation = __pOpenedAppControl->GetOperationId();
189                 AppLog( "Error:- AppControl is already running for provider id = %ls, operation id = %ls",
190                                 provider.GetPointer(), operation.GetPointer());
191                 return false;
192         }
193
194         result r = E_FAILURE;
195         //request parameters
196         HashMap extraData;
197         extraData.Construct();
198         extraData.Add(new (std::nothrow) String(CONTACTS_VIEW_TYPE), new (std::nothrow) String(CONTACTS_VIEW_TYPE_CONTACT));
199         extraData.Add(new (std::nothrow) String(CONTACTS_VIEW_TYPE_CONTACT), new (std::nothrow) String(CONTACTS_VIEW_TYPE_CONTACT));
200         extraData.Add(new (std::nothrow) String(CONTACT_KEY_ITEM_ID), new (std::nothrow) String(contactId));
201         extraData.Add(new (std::nothrow) String(CONTACTS_ID_KEY), new (std::nothrow) String(contactId));
202         extraData.Add(new (std::nothrow) String(CONTACT_KEY_ITEM_TYPE), new (std::nothrow) String(ITEM_TYPE_PERSON));
203
204         __pOpenedAppControl = AppManager::FindAppControlN(PROVIDER_ID_CONTACTS, OPERATION_ID_VIEW);
205         if (__pOpenedAppControl != null)
206         {
207                 r = __pOpenedAppControl->Start(null, null, &extraData, pListener);
208         }
209         extraData.RemoveAll(true);
210
211         if(r != E_SUCCESS)
212         {
213                 //AppControl request failed, delete AppControl.
214                 delete __pOpenedAppControl;
215                 __pOpenedAppControl = null;
216                 return false;
217         }
218         else
219         {
220                 //AppControl request was successful.
221                 return true;
222         }
223 }
224
225 bool
226 CallAppControlRequestMgr::LaunchAddContactAppControl(String& contactNumber, IAppControlResponseListener* pListener)
227 {
228         AppLogDebug("ENTER");
229         if (__pOpenedAppControl != null)
230         {
231                 //An AppControl is already running.
232                 String provider = __pOpenedAppControl->GetAppControlProviderId();
233                 String operation = __pOpenedAppControl->GetOperationId();
234                 AppLog( "Error:- AppControl is already running for provider id = %ls, operation id = %ls",
235                                 provider.GetPointer(), operation.GetPointer());
236                 return false;
237         }
238
239         result r = E_FAILURE;
240         //request parameters
241         HashMap extraData;
242         extraData.Construct();
243         extraData.Add(new (std::nothrow) String(INPUT_TYPE_PHONE), new (std::nothrow) String(contactNumber));
244         extraData.Add(new (std::nothrow) String(CONTACT_KEY_ITEM_TYPE), new (std::nothrow) String(ITEM_TYPE_CONTACT));
245
246         __pOpenedAppControl = AppManager::FindAppControlN(PROVIDER_ID_CONTACTS, OPERATION_ID_ADD);
247         if (__pOpenedAppControl != null)
248         {
249                 r = __pOpenedAppControl->Start(null, null, &extraData, pListener);
250         }
251         extraData.RemoveAll(true);
252
253         if(r != E_SUCCESS)
254         {
255                 //AppControl request failed, delete AppControl.
256                 delete __pOpenedAppControl;
257                 __pOpenedAppControl = null;
258                 return false;
259         }
260         else
261         {
262                 //AppControl request was successful.
263                 return true;
264         }
265 }
266
267 bool
268 CallAppControlRequestMgr::LaunchDialerAppControl(IAppControlResponseListener* pListener)
269 {
270         AppLogDebug("ENTER");
271         if (__pOpenedAppControl != null)
272         {
273                 //An AppControl is already running.
274                 String provider = __pOpenedAppControl->GetAppControlProviderId();
275                 String operation = __pOpenedAppControl->GetOperationId();
276                 AppLog( "Error:- AppControl is already running for provider id = %ls, operation id = %ls",
277                                 provider.GetPointer(), operation.GetPointer());
278                 return false;
279         }
280
281         result r = E_FAILURE;
282         HashMap extraData;
283         extraData.Construct();
284         extraData.Add(new (std::nothrow) String(PARAM_PHONE_NUMBER), new (std::nothrow) String(L""));
285
286         __pOpenedAppControl = AppManager::FindAppControlN(PROVIDER_ID_PHONE, OPERATION_ID_DIAL);
287         if (__pOpenedAppControl != null)
288         {
289                 r = __pOpenedAppControl->Start(null, null, &extraData, pListener);
290         }
291         extraData.RemoveAll(true);
292
293         if(r != E_SUCCESS)
294         {
295                 //AppControl request failed, delete AppControl.
296                 delete __pOpenedAppControl;
297                 __pOpenedAppControl = null;
298                 return false;
299         }
300         else
301         {
302                 //AppControl request was successful.
303                 return true;
304         }
305 }