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