Fix for 46412
[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 = callInfo.FetchCallerNameN();
142                                         if (pDisplayName->IsEmpty() == true)
143                                         {
144                                                 //If contact number is also not present show unknown
145                                                 if(callInfo.GetContactNumber().IsEmpty() == false)
146                                                 {
147                                                         pDisplayName->Append(callInfo.GetContactNumber());
148                                                 }
149                                                 else
150                                                 {
151                                                         pDisplayName->Append(AppUtility::GetResourceString(IDS_NUMBER_UNKNOWN));
152                                                 }
153                                         }
154                                         optionStr = AppUtility::GetResourceString(L"IDS_CALL_OPTION5");
155                                         optionStr.Append(*pDisplayName);
156                                         delete pDisplayName;
157                                 }
158
159                                 //Check whether to show as 1st or 2nd option
160                                 if (callInfo.IsOnHold() == false)
161                                 {
162                                         pFirstOption->Append(optionStr);
163                                 }
164                                 else
165                                 {
166                                         pSecondOption->Append(optionStr);
167                                 }
168                         }
169                         //3rd Option
170                         pThirdOption = new (std::nothrow) String();
171                         pThirdOption->Append(AppUtility::GetResourceString(L"IDS_CALL_OPTION6"));
172                 }
173                 else
174                 {
175                         //only one call - 2 options are shown.
176                         AppCallInfo firstCallInfo;
177                         pCallList->GetAt(0,firstCallInfo);
178                         if (firstCallInfo.IsConferenceCall() == false)
179                         {
180                                 //show display name if available, else show contact number
181                                 String* pDisplayName = firstCallInfo.FetchCallerNameN();
182                                 if (pDisplayName->IsEmpty() == true)
183                                 {
184                                         if(firstCallInfo.GetContactNumber().IsEmpty() == false)
185                                         {
186                                                 pDisplayName->Append(firstCallInfo.GetContactNumber());
187                                         }
188                                         else
189                                         {
190                                                 pDisplayName->Append(AppUtility::GetResourceString(IDS_NUMBER_UNKNOWN));
191                                         }
192                                 }
193                                 optionStr = AppUtility::GetResourceString(L"IDS_CALL_OPTION1");
194                                 pFirstOption->Format((optionStr.GetLength() + pDisplayName->GetLength()),optionStr.GetPointer(),pDisplayName->GetPointer());
195                                 //2nd option
196                                 optionStr = AppUtility::GetResourceString(L"IDS_CALL_OPTION2");
197                                 pSecondOption->Append(optionStr + *pDisplayName);
198                                 delete pDisplayName;
199                         }
200                         else
201                         {
202                                 String optionStr2(L"");
203                                 if (firstCallInfo.IsOnHold() == true)
204                                 {
205                                         optionStr = AppUtility::GetResourceString(L"IDS_CALL_OPTION8");
206                                         optionStr2 = AppUtility::GetResourceString(L"IDS_CALL_OPTION3");
207                                 }
208                                 else
209                                 {
210                                         optionStr = AppUtility::GetResourceString(L"IDS_CALL_OPTION7");
211                                         optionStr2 = AppUtility::GetResourceString(L"IDS_CALL_OPTION4");
212                                 }
213                                 pFirstOption->Format(optionStr.GetLength(), optionStr.GetPointer(), firstCallInfo.GetCallerListCount());
214                                 pSecondOption->Format(optionStr2.GetLength(), optionStr2.GetPointer(), firstCallInfo.GetCallerListCount());
215                         }
216                 }
217                 //show options
218                 __pOptionsList =  new (std::nothrow) ArrayList(SingleObjectDeleter);
219                 __pOptionsList->Construct();
220                 __pOptionsList->Add(pFirstOption);
221                 __pOptionsList->Add(pSecondOption);
222                 if (pThirdOption != null)
223                 {
224                         __pOptionsList->Add(pThirdOption);
225                 }
226                 r = E_SUCCESS;
227                 pCallList->RemoveAll();
228         }
229         delete pCallList;
230         return r;
231 }
232
233 void
234 CallOptionPopup::OnListViewItemStateChanged(ListView& listView, int index,
235                 int elementId, ListItemStatus status)
236 {
237         Popup::SetShowState(false);
238         if(elementId == -1)
239         {
240                 elementId = ANSERWING_OPTION_HOLD_SINGLE_CALL;
241                 if (__pOptionsList->GetCount() == 2)
242                 {
243                         if(index == 1)
244                         {
245                                 elementId = ANSERWING_OPTION_END_SINGLE_CALL;
246                         }
247                 }
248                 else
249                 {
250                         elementId = ANSERWING_OPTION_REPLACE_ACTIVE_CALL;
251                         if(index == 1)
252                         {
253                                 elementId = ANSERWING_OPTION_REPLACE_HELD_CALL;
254                         }
255                         else if(index == 2)
256                         {
257                                 elementId = ANSERWING_OPTION_END_ALL_CALLS;
258                         }
259                 }
260         }
261         __popupListener.OnItemSelected(elementId);
262         return;
263 }
264
265 int
266 CallOptionPopup::GetItemCount(void)
267 {
268         if (__pOptionsList != null)
269         {
270                 return __pOptionsList->GetCount();
271         }
272         return 0;
273 }
274
275 ListItemBase*
276 CallOptionPopup::CreateItem(int index, int itemWidth)
277 {
278         int elementId = ANSERWING_OPTION_HOLD_SINGLE_CALL;
279         String* strText = static_cast<String*>(__pOptionsList->GetAt(index));
280         ListAnnexStyle style = LIST_ANNEX_STYLE_NORMAL;
281         CustomItem* pItem = new CustomItem();
282         pItem->Construct(Dimension(itemWidth, W_LIST_ITEM), style);
283         if (__pOptionsList->GetCount() == 2)
284         {
285                 if(index == 1)
286                 {
287                         elementId = ANSERWING_OPTION_END_SINGLE_CALL;
288                 }
289         }
290         else
291         {
292                 elementId = ANSERWING_OPTION_REPLACE_ACTIVE_CALL;
293                 if(index == 1)
294                 {
295                         elementId = ANSERWING_OPTION_REPLACE_HELD_CALL;
296                 }
297                 else if(index == 2)
298                 {
299                         elementId = ANSERWING_OPTION_END_ALL_CALLS;
300                 }
301         }
302         pItem->AddElement(Rectangle(HORIZONTAL_PADDING, 0, W_POPUP - HORIZONTAL_PADDING, H_LIST_ITEM), elementId, *strText);
303         return pItem;
304 }
305
306 bool
307 CallOptionPopup::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
308 {
309         delete pItem;
310         pItem = null;
311         return true;
312 }
313
314 void
315 CallOptionPopup::OnActionPerformed(const Control& source, int actionId)
316 {
317         switch (actionId)
318         {
319         case IDA_BUTTON_CANCEL_OPTIONS_POPUP:
320         {
321                 Popup::SetShowState(false);
322                 __popupListener.OnItemSelected(IDA_BUTTON_CANCEL_OPTIONS_POPUP);
323         }
324         break;
325
326         default:
327                 break;
328         }
329 }
330
331 bool
332 CallOptionPopup::OnKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
333 {
334         AppLogDebug("Enter");
335         if(keyEventInfo.GetKeyCode() == KEY_BACK || keyEventInfo.GetKeyCode() == KEY_ESC)
336         {
337                 Popup::SetShowState(false);
338                 __popupListener.OnItemSelected(IDA_BUTTON_CANCEL_OPTIONS_POPUP);
339                 return true;
340
341         }
342         return false;
343 }