Fixed N_SE-38563, N_SE-38552
[apps/osp/Settings.git] / src / StBluetoothDetailForm.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                StBluetoothDetailForm.cpp
19  * @brief               This is the implementation file for BluetoothDetailForm class.
20  */
21
22 #include "StBluetoothDetailForm.h"
23 #include "StResourceManager.h"
24 #include "StTypes.h"
25
26 using namespace Tizen::Base;
27 using namespace Tizen::Base::Collection;
28 using namespace Tizen::Graphics;
29 using namespace Tizen::Ui;
30 using namespace Tizen::Ui::Controls;
31 using namespace Tizen::Ui::Scenes;
32
33 static const int ID_BLUETOOTH_DETAIL_GROUP_COUNT_DEFAULT = 1;
34 static const int ID_BLUETOOTH_DETAIL_ITEM_COUNT_DEFAULT = 2;
35 static const int ID_BLUETOOTH_DETAIL_INITIALIZE_VALUE = -1;
36
37 static const RequestId ID_BLUETOOTH_DETAIL_USER_PENDING_EVNET_GOBACKWARD = 100;
38
39 enum BluetoothDetailStatusGroupItem
40 {
41         BLUETOOTH_DETAIL_STATUS_GROUP_ITEM_BASE,
42         BLUETOOTH_DETAIL_STATUS_GROUP_ITEM_DEVICE_NAME = BLUETOOTH_DETAIL_STATUS_GROUP_ITEM_BASE,
43         BLUETOOTH_DETAIL_STATUS_GROUP_ITEM_UNPAIR,
44         BLUETOOTH_DETAIL_STATUS_GROUP_ITEM_TOTALCOUNT,
45         BLUETOOTH_DETAIL_STATUS_GROUP_ITEM_MAX
46 };
47
48 BluetoothDetailForm::BluetoothDetailForm(void)
49         : __pPendingEventReceivedForm(null)
50         , __pairedDeviceIndex(ID_BLUETOOTH_DETAIL_INITIALIZE_VALUE)
51         , __isTerminate(false)
52 {
53 }
54
55 BluetoothDetailForm::~BluetoothDetailForm(void)
56 {
57 }
58
59 void
60 BluetoothDetailForm::CreateFooter(void)
61 {
62         Footer* pFooter = GetFooter();
63         AppAssert(pFooter);
64
65         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
66         pFooter->SetBackButton();
67         pFooter->AddActionEventListener(*this);
68
69         SetFormBackEventListener(this);
70 }
71
72 result
73 BluetoothDetailForm::OnInitializing(void)
74 {
75         CreateHeader(ResourceManager::GetString(L"IDS_BT_BODY_DETAILS"));
76         CreateFooter();
77
78         SetUserPendingEventReceivedForm(this);
79         return E_SUCCESS;
80 }
81
82 result
83 BluetoothDetailForm::OnTerminating(void)
84 {
85         __pPendingEventReceivedForm = null;
86         __pBluetoothPresentationModelInstance = null;
87         __pTableView = null;
88
89         SetFormBackEventListener(null);
90         return E_SUCCESS;
91 }
92
93 void
94 BluetoothDetailForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
95 {
96         if (pArgs == null)
97         {
98                 AppLogDebug("pArgs is null");
99                 __isTerminate = true;
100                 return;
101         }
102
103         String* pPairedDeviceIndex = static_cast<String*>(pArgs->GetAt(0));
104         if (pPairedDeviceIndex == null)
105         {
106                 AppLogDebug("pPairedDeviceIndex is null");
107                 __isTerminate = true;
108                 return;
109         }
110
111         result r = Integer::Parse(*pPairedDeviceIndex, __pairedDeviceIndex);
112         if ((IsFailed(r))
113                 || (__pairedDeviceIndex < 0))
114         {
115                 AppLogDebug("Integer::Parse fail[%s] __pairedDeviceIndex[%d]", GetErrorMessage(r), __pairedDeviceIndex);
116                 __isTerminate = true;
117                 return;
118         }
119
120         r = BluetoothPresentationModelInit();
121         if (IsFailed(r))
122         {
123                 AppLogDebug("BluetoothPresentationModelInit fail [%s]", GetErrorMessage(r));
124                 __isTerminate = true;
125                 return;
126         }
127
128         CreateTableView();
129
130         pArgs->RemoveAll();
131         delete pArgs;
132 }
133
134 void
135 BluetoothDetailForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
136 {
137 }
138
139 void
140 BluetoothDetailForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
141 {
142         SceneManager* pSceneManager = SceneManager::GetInstance();
143         AppAssert(pSceneManager);
144
145         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
146 }
147
148 void
149 BluetoothDetailForm::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs)
150 {
151         SceneManager* pSceneManager = SceneManager::GetInstance();
152         AppAssert(pSceneManager);
153
154         if (requestId == ID_BLUETOOTH_DETAIL_USER_PENDING_EVNET_GOBACKWARD)
155         {
156                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT));
157         }
158 }
159
160 int
161 BluetoothDetailForm::GetGroupCount(void)
162 {
163         return ID_BLUETOOTH_DETAIL_GROUP_COUNT_DEFAULT;
164 }
165
166 int
167 BluetoothDetailForm::GetItemCount(int groupIndex)
168 {
169         return ID_BLUETOOTH_DETAIL_ITEM_COUNT_DEFAULT;
170 }
171
172 TableViewGroupItem*
173 BluetoothDetailForm::CreateGroupItem(int groupIndex, int itemWidth)
174 {
175         int itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
176         LabelTextStyle style = LABEL_TEXT_STYLE_BOLD;
177         Rectangle itemRectangle;
178         String groupText;
179
180         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
181
182         itemRectangle.x = X_GROUP_ITEM_DEFAULT_LABEL;
183         itemRectangle.y = Y_GROUP_ITEM_DEFAULT_LABEL;
184         itemRectangle.width = itemWidth;
185         itemRectangle.height = itemHeight;
186
187         RelativeLayout relativeLayout;
188         relativeLayout.Construct();
189
190         pItem->Construct(relativeLayout, Dimension(itemWidth, itemHeight));
191
192         Label* pLabel = new (std::nothrow) Label();
193         if (pLabel == null)
194         {
195                 AppLogDebug("pLabel is null");
196                 return pItem;
197         }
198         result r = pLabel->Construct(itemRectangle, groupText);
199         if (IsFailed(r))
200         {
201                 AppLogDebug("Construct fail [%s]", GetErrorMessage(r));
202                 delete pLabel;
203                 return pItem;
204         }
205
206         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
207         pLabel->SetTextVerticalAlignment(ALIGNMENT_TOP);
208         pLabel->SetTextConfig(FONT_SIZE_GROUP_TITLE_TEXT, style);
209         pItem->SetBackgroundColor(COLOR_BG_GROUP_INDEX_DEFAULT);
210         pLabel->SetTextColor(COLOR_GROUP_TITLE_TEXT);
211
212         pItem->AddControl(pLabel);
213         pItem->SetEnabled(false);
214         relativeLayout.SetMargin(*pLabel, itemRectangle.x, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
215         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
216         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
217
218         return pItem;
219 }
220
221 TableViewItem*
222 BluetoothDetailForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
223 {
224         bool isItemSubText = false;
225         int itemHeight = H_GROUP_ITEM_DEFAULT;
226         Rectangle itemMainRectangle;
227         Rectangle itemSubRectangle;
228         String itemMainText;
229         String itemSubText;
230         int fontSize = GetFontSize();
231
232         TableViewItem* pItem = new (std::nothrow) TableViewItem();
233         if (pItem == null)
234         {
235                 AppLogDebug("pItem is null");
236                 return null;
237         }
238
239         if ((groupIndex != BLUETOOTH_DETAIL_STATUS_GROUP_ITEM_BASE)
240                 || (__pBluetoothPresentationModelInstance == null))
241         {
242                 if (__pBluetoothPresentationModelInstance == null)
243                 {
244                         AppLogDebug("__pBluetoothPresentationModelInstance is null");
245                 }
246                 else
247                 {
248                         AppLogDebug("Unknown groupIndex");
249                 }
250
251                 delete pItem;
252                 return null;
253         }
254
255         switch (itemIndex)
256         {
257         case BLUETOOTH_DETAIL_STATUS_GROUP_ITEM_DEVICE_NAME:
258                 {
259                         itemMainText = ResourceManager::GetString(L"IDS_ST_HEADER_DEVICE_NAME_ABB");
260                         itemSubText =__pBluetoothPresentationModelInstance->GetPairedDeviceNameAt(__pairedDeviceIndex);
261                         isItemSubText = true;
262                         ItemTypeTwoLine(itemMainRectangle, itemSubRectangle, fontSize);
263                         itemHeight = (itemMainRectangle.height + itemSubRectangle.height);
264                 }
265                 break;
266
267         case BLUETOOTH_DETAIL_STATUS_GROUP_ITEM_UNPAIR:
268                 {
269                         itemMainText = ResourceManager::GetString(L"IDS_BT_BUTTON_UNPAIR");
270                         ItemTypeOneLine(itemMainRectangle);
271                 }
272                 break;
273
274         default:
275                 {
276                         delete pItem;
277                         return null;
278                 }
279                 break;
280         }
281
282         RelativeLayout relativeLayout;
283         relativeLayout.Construct();
284
285         result r = pItem->Construct(relativeLayout, Dimension(itemWidth, itemHeight));
286         if (IsFailed(r))
287         {
288                 AppLogDebug("Construct fail [%s]", GetErrorMessage(r));
289                 delete pItem;
290                 return null;
291         }
292         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
293
294         Label* pLabel = new (std::nothrow) Label();
295         if (pLabel == null)
296         {
297                 AppLogDebug("pLabel is null");
298                 return pItem;
299         }
300
301         r = pLabel->Construct(itemMainRectangle, itemMainText);
302         if (IsFailed(r))
303         {
304                 AppLogDebug("Construct fail [%s]", GetErrorMessage(r));
305                 delete pLabel;
306                 return pItem;
307         }
308
309         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
310         pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
311         pLabel->SetTextColor(COLOR_MAIN_TEXT);
312
313         pItem->AddControl(pLabel);
314
315         relativeLayout.SetMargin(*pLabel, 0, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
316         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
317         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
318         if (isItemSubText)
319         {
320                 Label* pSecondLabel = new (std::nothrow) Label();
321                 if (pSecondLabel == null)
322                 {
323                         AppLogDebug("pLabel is null");
324                         return pItem;
325                 }
326
327                 r = pSecondLabel->Construct(itemSubRectangle, itemSubText);
328                 if (IsFailed(r))
329                 {
330                         AppLogDebug("Construct fail [%s]", GetErrorMessage(r));
331                         delete pSecondLabel;
332                         return pItem;
333                 }
334
335                 pSecondLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
336                 pSecondLabel->SetTextConfig(FONT_SIZE_SUB_TEXT, LABEL_TEXT_STYLE_NORMAL);
337                 pSecondLabel->SetTextColor(COLOR_SUB_TEXT);
338
339                 pItem->AddControl(pSecondLabel);
340                 relativeLayout.SetMargin(*pSecondLabel, 0, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
341                 relativeLayout.SetRelation(*pSecondLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
342                 relativeLayout.SetRelation(*pSecondLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
343         }
344
345         return pItem;
346 }
347
348 bool
349 BluetoothDetailForm::DeleteGroupItem(int groupIndex, TableViewGroupItem* pItem)
350 {
351         delete pItem;
352         pItem = null;   
353
354         return true;
355 }
356
357 bool
358 BluetoothDetailForm::DeleteItem(int groupIndex, int itemIndex, TableViewItem* pItem)
359 {
360         delete pItem;
361         pItem = null;
362
363         return true;
364 }
365
366 void
367 BluetoothDetailForm::OnGroupedTableViewItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
368 {
369         SceneManager* pSceneManager = SceneManager::GetInstance();
370         AppAssert(pSceneManager);
371
372         if ((groupIndex != BLUETOOTH_DETAIL_STATUS_GROUP_ITEM_BASE)
373                 || (__pBluetoothPresentationModelInstance == null))
374         {
375                 if (__pBluetoothPresentationModelInstance == null)
376                 {
377                         AppLogDebug("__pBluetoothPresentationModelInstance is null");
378                 }
379                 else
380                 {
381                         AppLogDebug("Unknown groupIndex");
382                 }
383
384                 return;
385         }
386
387         if ((itemIndex != BLUETOOTH_DETAIL_STATUS_GROUP_ITEM_UNPAIR)
388                 || (__pairedDeviceIndex == ID_BLUETOOTH_DETAIL_INITIALIZE_VALUE))
389         {
390                 if (__pairedDeviceIndex == ID_BLUETOOTH_DETAIL_INITIALIZE_VALUE)
391                 {
392                         AppLogDebug("__pairedDeviceIndex is unKnown");
393                         SendToUserPendingEvent(ID_BLUETOOTH_DETAIL_USER_PENDING_EVNET_GOBACKWARD);
394                 }
395                 else
396                 {
397                         AppLogDebug("itemIndex is unKnown");
398                 }
399                 return;
400         }
401
402         __pBluetoothPresentationModelInstance->UnpairToPairedBluetoothDeviceAt(__pairedDeviceIndex);
403 }
404
405 result
406 BluetoothDetailForm::BluetoothPresentationModelInit(void)
407 {
408         __pBluetoothPresentationModelInstance = BluetoothPresentationModel::GetInstance();
409         if (__pBluetoothPresentationModelInstance == null)
410         {
411                 AppLogDebug("__pBluetoothPresentationModelInstance is null");
412                 return E_FAILURE;
413         }
414         __pBluetoothPresentationModelInstance->SetEventListener(this);
415
416         return E_SUCCESS;
417 }
418
419 void
420 BluetoothDetailForm::SetUserPendingEventReceivedForm(Tizen::Ui::Controls::Form* pEventReveivedForm)
421 {
422         __pPendingEventReceivedForm = pEventReveivedForm;
423 }
424
425 void
426 BluetoothDetailForm::SendToUserPendingEvent(RequestId requestId)
427 {
428         __pPendingEventReceivedForm->SendUserEvent(requestId, null);
429 }
430
431 int
432 BluetoothDetailForm::GetDefaultGroupItemHeight(void)
433 {
434         return H_GROUP_INDEX_NO_TITLE_DEFAULT;
435 }
436
437 int
438 BluetoothDetailForm::GetDefaultItemHeight(void)
439 {
440         return H_GROUP_ITEM_DEFAULT;
441 }
442
443 void
444 BluetoothDetailForm::UpdateGroupItem(int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem)
445 {
446 }
447
448 void
449 BluetoothDetailForm::UpdateItem(int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
450 {
451 }
452
453 void
454 BluetoothDetailForm::OnGroupedTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem* pContextItem, bool activated)
455 {
456 }
457
458 void
459 BluetoothDetailForm::OnGroupedTableViewGroupItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
460 {
461 }
462
463 void
464 BluetoothDetailForm::OnBluetoothPresentationModelEventCompletedN(BluetoothPresentationModelEvent bluetoothEvent, result r, Tizen::Net::Bluetooth::BluetoothDevice* pDevice)
465 {
466         if (bluetoothEvent == BLUETOOTH_PRESENTATION_MODEL_EVENT_UNPAIRED)
467         {
468                 SendToUserPendingEvent(ID_BLUETOOTH_DETAIL_USER_PENDING_EVNET_GOBACKWARD);
469         }
470         else
471         {
472                 AppLogDebug("BluetoothPresentationModelEvent is unknown");
473         }
474 }