Fix for Call sate not set properly when there is 1 waiting and 1 active call and...
[apps/osp/Call.git] / src / CallOptionPopup.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        CallCallOptionPopup.cpp
19  * @brief       Call Options Popup class
20  */
21 #include <FApp.h>
22 #include <FSocial.h>
23 #include <FSystem.h>
24 #include <FUi.h>
25 #include <FGraphics.h>
26 #include <FMedia.h>
27
28 #include "CallOptionPopup.h"
29 #include "CallAppUtility.h"
30
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::Base::Utility;
34 using namespace Tizen::App;
35 using namespace Tizen::Graphics;
36 using namespace Tizen::Social;
37 using namespace Tizen::Ui;
38 using namespace Tizen::Ui::Controls;
39 using namespace Tizen::Ui::Scenes;
40
41 const int W_POPUP = 620;
42 const int H_POPUP = 712;
43 const int W_CANCEL_BUTTON = 400;
44 const int H_CANCEL_BUTTON = 72;
45 const int CANCEL_BUTTON_PADDING = 20;
46 const int H_LIST_ITEM = 112;
47 const int W_LIST_ITEM = 112;
48 const int HORIZONTAL_PADDING = 16;
49 const int VERTICAL_PADDING = 110;
50 const int IDA_BUTTON_CANCEL_OPTIONS_POPUP = 502;
51 static const wchar_t* IDL_CALL_OPTION_POPUP = L"IDL_CALL_OPTION_POPUP";
52
53 CallOptionPopup::CallOptionPopup(IPopupItemSelectListener& listener, CallPresentationModel& callPresenter)
54         : __pOptionsList(null)
55         , __popupListener(listener)
56         , __callPresenter(callPresenter)
57 {
58 }
59
60 CallOptionPopup::~CallOptionPopup(void)
61 {
62         if (__pOptionsList != null)
63         {
64                 delete __pOptionsList;
65                 __pOptionsList = null;
66         }
67 }
68
69 result
70 CallOptionPopup::Initialize(void)
71 {
72         result r = E_SUCCESS;
73         r = Construct(IDL_CALL_OPTION_POPUP);
74         TryReturn(r == E_SUCCESS, r, "CallOptionPopup construction failed");
75
76         r = ConstructListData();
77         TryReturn(r == E_SUCCESS, r, "List data not available");
78
79         int listHeight = H_LIST_ITEM * __pOptionsList->GetCount();
80         int popupHeight = H_CANCEL_BUTTON + listHeight + CANCEL_BUTTON_PADDING + VERTICAL_PADDING;
81         r = SetSize(Dimension(W_POPUP + HORIZONTAL_PADDING, popupHeight));
82         TryReturn(r == E_SUCCESS, r, "CallOptionPopup::SetSize() failed");
83         SetMinimumSize(Dimension(W_POPUP + HORIZONTAL_PADDING, popupHeight));
84         TryReturn(r == E_SUCCESS, r, "CallOptionPopup::SetMinSize() failed");
85
86         String strText;
87         Application::GetInstance()->GetAppResource()->GetString(L"IDS_CANCEL", strText);
88         Button* pCancelButton = new (std::nothrow) Button();
89         r = pCancelButton->Construct(Rectangle(VERTICAL_PADDING, listHeight + CANCEL_BUTTON_PADDING , W_CANCEL_BUTTON, H_CANCEL_BUTTON), strText);
90         pCancelButton->SetActionId(IDA_BUTTON_CANCEL_OPTIONS_POPUP);
91         pCancelButton->AddActionEventListener(*this);
92         SetPropagatedKeyEventListener(this);
93         r = AddControl(pCancelButton);
94         TryReturn(r == E_SUCCESS, r, "CallOptionPopup::AddControl() failed");
95
96         ListView* pListView = new (std::nothrow) ListView();
97         pListView->Construct(Rectangle(0, 0, W_POPUP + HORIZONTAL_PADDING, listHeight), true, SCROLL_STYLE_FADE_OUT);
98         pListView->AddListViewItemEventListener(*this);
99         pListView->SetItemProvider(*this);
100         r = AddControl(pListView);
101
102
103
104         return r;
105 }
106
107 result
108 CallOptionPopup::ConstructListData(void)
109 {
110         result r = E_FAILURE;
111
112         //fetch active call list
113         ArrayListT<AppCallInfo>* pCallList  = static_cast<ArrayListT<AppCallInfo>*>(__callPresenter.GetCallListN());
114         if (pCallList != null && pCallList->GetCount() > 0)
115         {
116                 String optionStr(L"");
117                 String* pFirstOption = new (std::nothrow) String();
118                 String* pSecondOption = new (std::nothrow) String();
119                 String* pThirdOption = null;
120                 if (pCallList->GetCount() == IDI_MAX_ACTIVE_CALLS)
121                 {
122                         //2 calls(Active+Held) present - 3 options are shown
123                         for (int index = 0;index < pCallList->GetCount(); index++)
124                         {
125                                 AppCallInfo callInfo;
126                                 pCallList->GetAt(index,callInfo);
127
128                                 optionStr.Clear();
129                                 if (callInfo.IsConferenceCall() == true)
130                                 {
131                                         String option = AppUtility::GetResourceString(L"IDS_CALL_OPTION4");;
132                                         if (callInfo.IsOnHold() == true)
133                                         {
134                                                 option = AppUtility::GetResourceString(L"IDS_CALL_OPTION3");
135                                         }
136                                         optionStr.Format(option.GetLength(), option.GetPointer(), callInfo.GetCallerListCount());
137                                 }
138                                 else
139                                 {
140                                         //show display name, if available, Else show contact number
141                                         String* pDisplayName = null;
142                                         if(callInfo.GetContactNumber().IsEmpty() == false)
143                                         {
144                                                 pDisplayName = callInfo.FetchLatestCallerNameN(callInfo.GetContactNumber());
145                                         }
146                                         else
147                                         {
148                                                 pDisplayName = new (std::nothrow) String(L"");
149                                         }
150                                         if (pDisplayName->IsEmpty() == true)
151                                         {
152                                                 //If contact number is also not present show unknown
153                                                 if(callInfo.GetContactNumber().IsEmpty() == false)
154                                                 {
155                                                         pDisplayName->Append(callInfo.GetContactNumber());
156                                                 }
157                                                 else
158                                                 {
159                                                         pDisplayName->Append(AppUtility::GetResourceString(IDS_NUMBER_UNKNOWN));
160                                                 }
161                                         }
162                                         optionStr = AppUtility::GetResourceString(L"IDS_CALL_OPTION5");
163                                         optionStr.Append(*pDisplayName);
164                                         delete pDisplayName;
165                                 }
166
167                                 //Check whether to show as 1st or 2nd option
168                                 if (callInfo.IsOnHold() == false)
169                                 {
170                                         pFirstOption->Append(optionStr);
171                                 }
172                                 else
173                                 {
174                                         pSecondOption->Append(optionStr);
175                                 }
176                         }
177                         //3rd Option
178                         pThirdOption = new (std::nothrow) String();
179                         pThirdOption->Append(AppUtility::GetResourceString(L"IDS_CALL_OPTION6"));
180                 }
181                 else
182                 {
183                         //only one call - 2 options are shown.
184                         AppCallInfo firstCallInfo;
185                         pCallList->GetAt(0,firstCallInfo);
186                         if (firstCallInfo.IsConferenceCall() == false)
187                         {
188                                 //show display name if available, else show contact number
189                                 String* pDisplayName = null;
190                                 if(firstCallInfo.GetContactNumber().IsEmpty() == false)
191                                 {
192                                         pDisplayName = firstCallInfo.FetchLatestCallerNameN(firstCallInfo.GetContactNumber());
193                                 }
194                                 else
195                                 {
196                                         pDisplayName = new (std::nothrow) String(L"");
197                                 }
198                                 if (pDisplayName->IsEmpty() == true)
199                                 {
200                                         if(firstCallInfo.GetContactNumber().IsEmpty() == false)
201                                         {
202                                                 pDisplayName->Append(firstCallInfo.GetContactNumber());
203                                         }
204                                         else
205                                         {
206                                                 pDisplayName->Append(AppUtility::GetResourceString(IDS_NUMBER_UNKNOWN));
207                                         }
208                                 }
209                                 optionStr = AppUtility::GetResourceString(L"IDS_CALL_OPTION1");
210                                 pFirstOption->Format((optionStr.GetLength() + pDisplayName->GetLength()),optionStr.GetPointer(),pDisplayName->GetPointer());
211                                 //2nd option
212                                 optionStr = AppUtility::GetResourceString(L"IDS_CALL_OPTION2");
213                                 pSecondOption->Append(optionStr + *pDisplayName);
214                                 delete pDisplayName;
215                         }
216                         else
217                         {
218                                 String optionStr2(L"");
219                                 if (firstCallInfo.IsOnHold() == true)
220                                 {
221                                         optionStr = AppUtility::GetResourceString(L"IDS_CALL_OPTION8");
222                                         optionStr2 = AppUtility::GetResourceString(L"IDS_CALL_OPTION3");
223                                 }
224                                 else
225                                 {
226                                         optionStr = AppUtility::GetResourceString(L"IDS_CALL_OPTION7");
227                                         optionStr2 = AppUtility::GetResourceString(L"IDS_CALL_OPTION4");
228                                 }
229                                 pFirstOption->Format(optionStr.GetLength(), optionStr.GetPointer(), firstCallInfo.GetCallerListCount());
230                                 pSecondOption->Format(optionStr2.GetLength(), optionStr2.GetPointer(), firstCallInfo.GetCallerListCount());
231                         }
232                 }
233                 //show options
234                 __pOptionsList =  new (std::nothrow) ArrayList(SingleObjectDeleter);
235                 __pOptionsList->Construct();
236                 __pOptionsList->Add(pFirstOption);
237                 __pOptionsList->Add(pSecondOption);
238                 if (pThirdOption != null)
239                 {
240                         __pOptionsList->Add(pThirdOption);
241                 }
242                 r = E_SUCCESS;
243                 pCallList->RemoveAll();
244         }
245         delete pCallList;
246         return r;
247 }
248
249 void
250 CallOptionPopup::OnListViewItemStateChanged(ListView& listView, int index,
251                 int elementId, ListItemStatus status)
252 {
253         Popup::SetShowState(false);
254         if(elementId == -1)
255         {
256                 elementId = ANSERWING_OPTION_HOLD_SINGLE_CALL;
257                 if (__pOptionsList->GetCount() == 2)
258                 {
259                         if(index == 1)
260                         {
261                                 elementId = ANSERWING_OPTION_END_SINGLE_CALL;
262                         }
263                 }
264                 else
265                 {
266                         elementId = ANSERWING_OPTION_REPLACE_ACTIVE_CALL;
267                         if(index == 1)
268                         {
269                                 elementId = ANSERWING_OPTION_REPLACE_HELD_CALL;
270                         }
271                         else if(index == 2)
272                         {
273                                 elementId = ANSERWING_OPTION_END_ALL_CALLS;
274                         }
275                 }
276         }
277         __popupListener.OnItemSelected(elementId);
278         return;
279 }
280
281 int
282 CallOptionPopup::GetItemCount(void)
283 {
284         if (__pOptionsList != null)
285         {
286                 return __pOptionsList->GetCount();
287         }
288         return 0;
289 }
290
291 ListItemBase*
292 CallOptionPopup::CreateItem(int index, int itemWidth)
293 {
294         int elementId = ANSERWING_OPTION_HOLD_SINGLE_CALL;
295         String* strText = static_cast<String*>(__pOptionsList->GetAt(index));
296         ListAnnexStyle style = LIST_ANNEX_STYLE_NORMAL;
297         CustomItem* pItem = new CustomItem();
298         pItem->Construct(Dimension(itemWidth, W_LIST_ITEM), style);
299         if (__pOptionsList->GetCount() == 2)
300         {
301                 if(index == 1)
302                 {
303                         elementId = ANSERWING_OPTION_END_SINGLE_CALL;
304                 }
305         }
306         else
307         {
308                 elementId = ANSERWING_OPTION_REPLACE_ACTIVE_CALL;
309                 if(index == 1)
310                 {
311                         elementId = ANSERWING_OPTION_REPLACE_HELD_CALL;
312                 }
313                 else if(index == 2)
314                 {
315                         elementId = ANSERWING_OPTION_END_ALL_CALLS;
316                 }
317         }
318         pItem->AddElement(Rectangle(HORIZONTAL_PADDING, 0, W_POPUP - HORIZONTAL_PADDING, H_LIST_ITEM), elementId, *strText);
319         return pItem;
320 }
321
322 bool
323 CallOptionPopup::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
324 {
325         delete pItem;
326         pItem = null;
327         return true;
328 }
329
330 void
331 CallOptionPopup::OnActionPerformed(const Control& source, int actionId)
332 {
333         switch (actionId)
334         {
335         case IDA_BUTTON_CANCEL_OPTIONS_POPUP:
336         {
337                 Popup::SetShowState(false);
338                 __popupListener.OnItemSelected(IDA_BUTTON_CANCEL_OPTIONS_POPUP);
339         }
340         break;
341
342         default:
343                 break;
344         }
345 }
346
347 bool
348 CallOptionPopup::OnKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
349 {
350         AppLogDebug("Enter");
351         if(keyEventInfo.GetKeyCode() == KEY_BACK || keyEventInfo.GetKeyCode() == KEY_ESC)
352         {
353                 Popup::SetShowState(false);
354                 __popupListener.OnItemSelected(IDA_BUTTON_CANCEL_OPTIONS_POPUP);
355                 return true;
356
357         }
358         return false;
359 }