Applied latest source code
[apps/native/preloaded/Settings.git] / src / StLocationForm.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                StLocationForm.cpp
19  * @brief               This is the implementation file for LocationForm class.
20  */
21
22 #include "StLocationForm.h"
23 #include "StResourceManager.h"
24 #include "StSettingScenesList.h"
25 #include "StTypes.h"
26
27 using namespace Tizen::App;
28 using namespace Tizen::Base;
29 using namespace Tizen::Base::Collection;
30 using namespace Tizen::Graphics;
31 using namespace Tizen::System;
32 using namespace Tizen::Ui;
33 using namespace Tizen::Ui::Controls;
34 using namespace Tizen::Ui::Scenes;
35
36 static const char APPCONTROL_REQUEST_DEFAULT = 0x00;
37 static const char APPCONTROL_REQUEST_LOCATION = 0x20;
38
39 static const int ID_GROUP_GPS_1 = 0;
40 static const int ID_GROUP_GPS_1_ITEM_COUNT = 1;
41 static const int ID_ITEM_GPS_ON_OFF = 0;
42
43 static const int ID_GROUP_GPS_2 = 1;
44 static const int ID_GROUP_GPS_2_ITEM_COUNT = 1;
45 static const int ID_ITEM_GPS_NETWORK_POSITION = 0;
46 static const int ID_GROUP_GPS_3 = 2;
47 static const int ID_GROUP_GPS_3_ITEM_COUNT = 0;
48
49 static const int ID_GROUP_COUNT = 3;
50 static const int ID_FOOTER_ITEM_INDEX_1 = 0;
51
52 static const int IDA_FOOTER_ITEM_HELP = 100;
53
54 static const int W_RESIZE_LABEL_GAP = 170;
55
56 LocationForm::LocationForm(void)
57         : __isAppControlRequest(APPCONTROL_REQUEST_DEFAULT)
58         , __categoryCheck(L"")
59 {
60 }
61
62 LocationForm::~LocationForm(void)
63 {
64 }
65
66 void
67 LocationForm::CreateFooter(void)
68 {
69         Footer* pFooter = GetFooter();
70         AppAssert(pFooter);
71
72         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
73
74         FooterItem footerHelp;
75         footerHelp.Construct(IDA_FOOTER_ITEM_HELP);
76         footerHelp.SetText(ResourceManager::GetString(L"IDS_ST_BODY_HELP"));
77
78         pFooter->AddItem(footerHelp);
79         pFooter->AddActionEventListener(*this);
80         pFooter->SetItemEnabled(ID_FOOTER_ITEM_INDEX_1, true);
81
82         SetFormBackEventListener(this);
83 }
84
85 result
86 LocationForm::OnInitializing(void)
87 {
88         SetFormStyle(FORM_STYLE_NORMAL | FORM_STYLE_INDICATOR | FORM_STYLE_HEADER | FORM_STYLE_FOOTER | FORM_STYLE_LANDSCAPE_INDICATOR_AUTO_HIDE);
89         CreateHeader(ResourceManager::GetString(L"IDS_ST_BODY_LOCATIONS"));
90         CreateFooter();
91         CreateTableView();
92
93         AppLogDebug("ENTER");
94
95         return E_SUCCESS;
96 }
97
98 result
99 LocationForm::OnTerminating(void)
100 {
101         __pTableView = null;
102
103         SetFormBackEventListener(null);
104         return E_SUCCESS;
105 }
106
107 void
108 LocationForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
109 {
110         bool itemStatus = false;
111         __pTableView->UpdateTableView();
112         result r = E_SUCCESS;
113
114         r = SettingInfo::GetValue(SETTING_INFO_KEY_LOCATION_GPS, itemStatus);
115         if (!IsFailed(r)
116                 && (itemStatus == true))
117         {
118                 __pTableView->SetItemChecked(ID_GROUP_GPS_1, ID_ITEM_GPS_ON_OFF, itemStatus);
119         }
120         AppLogDebug("Gps value[%d] [%s]", itemStatus, GetErrorMessage(GetLastResult()));
121
122         r = SettingInfo::GetValue(SETTING_INFO_KEY_LOCATION_WPS, itemStatus);
123         if (!IsFailed(r)
124                 && (itemStatus == true))
125         {
126                 __pTableView->SetItemChecked(ID_GROUP_GPS_2, ID_ITEM_GPS_NETWORK_POSITION, itemStatus);
127         }
128         AppLogDebug("Wps value[%d] [%s]", itemStatus, GetErrorMessage(GetLastResult()));
129
130         if (pArgs != null)
131         {
132                 __categoryCheck = static_cast<String*>(pArgs->GetAt(0))->GetPointer();
133
134                 if (__categoryCheck.CompareTo(L"http://tizen.org/appcontrol/data/location/gps") == 0)
135                 {
136                         __isAppControlRequest = APPCONTROL_REQUEST_LOCATION;
137                 }
138                 else if (__categoryCheck.CompareTo(L"http://tizen.org/appcontrol/data/location/wps") == 0)
139                 {
140                         __isAppControlRequest = APPCONTROL_REQUEST_LOCATION;
141                 }
142
143                 delete pArgs;
144         }
145 }
146
147 void
148 LocationForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
149 {
150 }
151
152 void
153 LocationForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
154 {
155         SceneManager* pSceneManager = SceneManager::GetInstance();
156         AppAssert(pSceneManager);
157
158         if (__isAppControlRequest != APPCONTROL_REQUEST_DEFAULT)
159         {
160                 UiApp* pApp = UiApp::GetInstance();
161                 AppAssert(pApp);
162                 AppControlLocationResult();
163                 pApp->Terminate();
164         }
165         else
166         {
167                 pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
168         }
169 }
170
171 int
172 LocationForm::GetGroupCount(void)
173 {
174         AppLogDebug("ENTER");
175         return ID_GROUP_COUNT;
176 }
177
178 int
179 LocationForm::GetItemCount(int groupIndex)
180 {
181         int itemCount = 0;
182
183         switch (groupIndex)
184         {
185         case ID_GROUP_GPS_1:
186                 {
187                         itemCount = ID_GROUP_GPS_1_ITEM_COUNT;
188                 }
189                 break;
190
191         case ID_GROUP_GPS_2:
192                 {
193                         itemCount = ID_GROUP_GPS_2_ITEM_COUNT;
194                 }
195                 break;
196
197         case ID_GROUP_GPS_3:
198                 {
199                         itemCount = ID_GROUP_GPS_3_ITEM_COUNT;
200                 }
201                 break;
202
203         default:
204                 break;
205         }
206
207         AppLogDebug("GetItemCount %d", itemCount);
208         return itemCount;
209 }
210
211 TableViewGroupItem*
212 LocationForm::CreateGroupItem(int groupIndex, int itemWidth)
213 {
214         AppLogDebug("ENTER");
215
216         int itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
217         int yItemOffset = H_GROUP_INDEX_HELP_TEXT_TOP_GAP;
218         LabelTextStyle style = LABEL_TEXT_STYLE_NORMAL;
219         Rectangle itemMainRectangle;
220         String groupText;
221         Label* pLabel = null;
222
223         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
224
225         switch (groupIndex)
226         {
227         case ID_GROUP_GPS_1:
228                 {
229                         yItemOffset = Y_GROUP_INDEX_DEFAULT;
230                         itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
231                 }
232                 break;
233
234         default:
235                 {
236                         yItemOffset = Y_GROUP_INDEX_DEFAULT;
237                         itemHeight = Y_GROUP_INDEX_DEFAULT;
238                 }
239                 break;
240         }
241
242         itemMainRectangle.x = X_GROUP_ITEM_DEFAULT_LABEL;
243         itemMainRectangle.y = yItemOffset;
244         itemMainRectangle.width = itemWidth;
245         itemMainRectangle.height = itemHeight;
246
247         RelativeLayout relativeLayout;
248         relativeLayout.Construct();
249
250         pItem->Construct(relativeLayout, Dimension(itemWidth, itemHeight));
251         pItem->SetBackgroundColor(COLOR_BG_GROUP_INDEX_DEFAULT);
252
253         pLabel = new (std::nothrow) Label();
254         pLabel->Construct(itemMainRectangle, groupText);
255         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
256         pLabel->SetTextVerticalAlignment(ALIGNMENT_TOP);
257         pLabel->SetTextConfig(FONT_SIZE_GROUP_TITLE_TEXT, style);
258         pLabel->SetTextColor(COLOR_HELP_TEXT_TYPE_01);
259
260         pItem->AddControl(pLabel);
261         relativeLayout.SetMargin(*pLabel, itemMainRectangle.x, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
262         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
263         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
264         pItem->SetEnabled(false);
265
266         return pItem;
267 }
268
269 TableViewItem*
270 LocationForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
271 {
272         Rectangle itemMainRectangle;
273         TableViewAnnexStyle style = TABLE_VIEW_ANNEX_STYLE_NORMAL;
274         String itemMainText;
275         Label* pLabel = null;
276         int fontSize = GetFontSize();
277         result r = E_SUCCESS;
278         style = TABLE_VIEW_ANNEX_STYLE_ONOFF_SLIDING;
279         ItemTypeOneLine(itemMainRectangle);
280
281         switch (groupIndex)
282         {
283         case ID_GROUP_GPS_1:
284                 {
285                         bool gpsStatus;
286                         itemMainText = ResourceManager::GetString(L"IDS_CAM_OPT_GPS");
287                         r = SettingInfo::GetValue(SETTING_INFO_KEY_LOCATION_GPS, gpsStatus);
288                         if (!IsFailed(r)
289                                 && (gpsStatus == true))
290                         {
291                                 __pTableView->SetItemChecked(groupIndex, itemIndex, true);
292                         }
293                 }
294                 break;
295
296         case ID_GROUP_GPS_2:
297                 {
298                         switch (itemIndex)
299                         {
300                         case ID_ITEM_GPS_NETWORK_POSITION:
301                                 {
302                                         bool wpsStatus;
303                                         itemMainText = ResourceManager::GetString(L"IDS_LBS_BODY_NETWORK_POSITION");
304                                         itemMainRectangle.width -= W_RESIZE_LABEL_GAP;
305
306                                         r = SettingInfo::GetValue(SETTING_INFO_KEY_LOCATION_WPS, wpsStatus);
307                                         if (!IsFailed(r)
308                                                 && (wpsStatus == true))
309                                         {
310                                                 __pTableView->SetItemChecked(groupIndex, itemIndex, true);
311                                         }
312                                 }
313                                 break;
314
315                         default:
316                                 break;
317                         }
318                 }
319                 break;
320
321         default:
322                 break;
323         }
324
325         TableViewItem* pItem = new (std::nothrow) TableViewItem();
326
327         RelativeLayout relativeLayout;
328         relativeLayout.Construct();
329
330         pItem->Construct(relativeLayout, Dimension(itemWidth, H_GROUP_ITEM_DEFAULT), style);
331         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
332
333         pLabel = new (std::nothrow) Label();
334         pLabel->Construct(itemMainRectangle, itemMainText);
335         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
336         pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
337         pLabel->SetTextColor(COLOR_MAIN_TEXT);
338
339         pItem->AddControl(pLabel);
340         relativeLayout.SetMargin(*pLabel, itemMainRectangle.x, RELATIVE_LAYOUT_RIGHT_MARGIN_ONOFF_SLIDING, 0, 0);
341         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
342         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
343
344         return pItem;
345 }
346
347 bool
348 LocationForm::DeleteGroupItem(int groupIndex, TableViewGroupItem* pItem)
349 {
350         AppLogDebug("ENTER");
351
352         delete pItem;
353         pItem = null;
354
355         return true;
356 }
357
358 bool
359 LocationForm::DeleteItem(int groupIndex, int itemIndex, TableViewItem* pItem)
360 {
361         AppLogDebug("ENTER");
362
363         delete pItem;
364         pItem = null;
365
366         return true;
367 }
368
369 void
370 LocationForm::OnGroupedTableViewItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
371 {
372         bool itemSelectStatus = tableView.IsItemChecked(groupIndex, itemIndex);
373
374         switch (groupIndex)
375         {
376         case ID_GROUP_GPS_1:
377                 {
378                         if (itemIndex == ID_ITEM_GPS_ON_OFF)
379                         {
380                                 if (status == TABLE_VIEW_ITEM_STATUS_SELECTED)
381                                 {
382                                         itemSelectStatus = !itemSelectStatus;
383                                 }
384
385                                 if (SettingInfo::SetValueForPrivilegedKey(SETTING_INFO_KEY_LOCATION_GPS, itemSelectStatus) == E_SUCCESS)
386                                 {
387                                         tableView.SetItemChecked(groupIndex, itemIndex, itemSelectStatus);
388                                 }
389                         }
390                 }
391                 break;
392
393         case ID_GROUP_GPS_2:
394                 {
395                         if (itemIndex == ID_ITEM_GPS_NETWORK_POSITION)
396                         {
397                                 if (status == TABLE_VIEW_ITEM_STATUS_SELECTED)
398                                 {
399                                         itemSelectStatus = !itemSelectStatus;
400                                 }
401
402                                 if (SettingInfo::SetValueForPrivilegedKey(SETTING_INFO_KEY_LOCATION_WPS, itemSelectStatus) == E_SUCCESS)
403                                 {
404                                         tableView.SetItemChecked(groupIndex, itemIndex, itemSelectStatus);
405                                 }
406                         }
407                 }
408                 break;
409
410         default:
411                 break;
412         }
413 }
414
415 void
416 LocationForm::OnActionPerformed(const Control& source, int actionId)
417 {
418         SceneManager* pSceneManager = SceneManager::GetInstance();
419         AppAssert(pSceneManager);
420
421         switch (actionId)
422         {
423         case IDA_FOOTER_ITEM_HELP:
424                 {
425                         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_LOCATION_HELP, SCENE_TRANSITION_ANIMATION_TYPE_LEFT));
426                 }
427                 break;
428
429         default:
430                 {
431                         AppLogDebug("ID_HEADER_VOLUME Tab Selected %d", actionId);
432                 }
433                 break;
434         }
435 }
436
437 int
438 LocationForm::GetDefaultGroupItemHeight(void)
439 {
440         return H_GROUP_INDEX_NO_TITLE_DEFAULT;
441 }
442
443 int
444 LocationForm::GetDefaultItemHeight(void)
445 {
446         return H_GROUP_ITEM_DEFAULT;
447 }
448
449 void
450 LocationForm::UpdateGroupItem(int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem)
451 {
452 }
453
454 void
455 LocationForm::UpdateItem(int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
456 {
457 }
458
459 void
460 LocationForm::OnGroupedTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem* pContextItem, bool activated)
461 {
462 }
463
464 void
465 LocationForm::OnGroupedTableViewGroupItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
466 {
467 }
468
469 result
470 LocationForm::AppControlLocationResult(void)
471 {
472         RequestId reqId = 0;
473         String value;
474         bool gpsStatus = false;
475         bool wpsStatus = false;
476
477         AppControlProviderManager* pAppManager = AppControlProviderManager::GetInstance();
478
479 #if 0
480         IList* pDefaultResultIList = null;
481         ArrayList* pDataList = null;
482
483         pDataList = new (std::nothrow) ArrayList();
484         pDataList->Construct();
485
486         SettingInfo::GetValue(SETTING_INFO_KEY_LOCATION_GPS, gpsStatus);
487         SettingInfo::GetValue(SETTING_INFO_KEY_LOCATION_WPS, wpsStatus);
488
489         pDataList->Add(*(new (std::nothrow) String(L"APPCONTROL_RESULT_SUCCEEDED")));
490         pDataList->Add(*(new (std::nothrow) String(L"category:Location")));
491
492         if (gpsStatus == true)
493         {
494                 value = L"GPSEnabled";
495         }
496         else
497         {
498                 value = L"GPSDisabled";
499         }
500         pDataList->Add(*(new (std::nothrow) String(value)));
501
502         if (wpsStatus == true)
503         {
504                 value = L"WPSEnabled";
505         }
506         else
507         {
508                 value = L"WPSDisabled";
509         }
510         pDataList->Add(*(new (std::nothrow) String(value)));
511
512         pDefaultResultIList = (pDataList->GetItemsN(0, pDataList->GetCount()));
513
514         pAppManager->SendAppControlResult(reqId, pDefaultResultIList);
515
516         pDataList->RemoveAll(true);
517         delete pDataList;
518         pDefaultResultIList->RemoveAll(true);
519         delete pDefaultResultIList;
520 #else
521         String status;
522         AppCtrlResult ResultValue = APP_CTRL_RESULT_FAILED;
523         HashMap* hashMap = new (std::nothrow) HashMap();
524         hashMap->Construct();
525
526         if ((__categoryCheck.CompareTo(L"http://tizen.org/appcontrol/data/location/gps") == 0)
527                 || (__categoryCheck.CompareTo(L"http://tizen.org/appcontrol/data/location/wps") == 0)
528                 )
529         {
530                 result r = E_SUCCESS;
531                 ResultValue = APP_CTRL_RESULT_SUCCEEDED;
532                 hashMap->Add(new (std::nothrow) String(L"category"), new (std::nothrow) String(L"Location"));
533                 r = SettingInfo::GetValue(SETTING_INFO_KEY_LOCATION_GPS, gpsStatus);
534
535                 if (r == E_SUCCESS && gpsStatus == true)
536                 {
537                         status = L"GPSEnabled";
538                 }
539                 else
540                 {
541                         status = L"GPSDisabled";
542                 }
543
544                 hashMap->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/location/gps"), new (std::nothrow) String(status));
545                 r = SettingInfo::GetValue(SETTING_INFO_KEY_LOCATION_WPS, wpsStatus);
546
547                 if (r == E_SUCCESS && wpsStatus == true)
548                 {
549                         status = L"WPSEnabled";
550                 }
551                 else
552                 {
553                         status = L"WPSDisabled";
554                 }
555
556                 hashMap->Add(new (std::nothrow) String(L"http://tizen.org/appcontrol/data/location/wps"), new (std::nothrow) String(status));
557         }
558
559         pAppManager->SendAppControlResult(reqId, ResultValue, hashMap);
560
561         hashMap->RemoveAll();
562         delete hashMap;
563 #endif
564         return E_SUCCESS;
565 }
566
567 void
568 LocationForm::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs)
569 {
570         AppLog("Enter");
571         bool itemStatus = false;
572         result r = E_SUCCESS;
573
574         if (requestId == REFRESH_REQUEST_EVENT)
575         {
576                 r = SettingInfo::GetValue(SETTING_INFO_KEY_LOCATION_GPS, itemStatus);
577                 if (!IsFailed(r))
578                 {
579                         __pTableView->SetItemChecked(ID_GROUP_GPS_1, ID_ITEM_GPS_ON_OFF, itemStatus);
580                 }
581                 AppLogDebug("Gps value[%d] [%s]", itemStatus, GetErrorMessage(GetLastResult()));
582
583                 r = SettingInfo::GetValue(SETTING_INFO_KEY_LOCATION_WPS, itemStatus);
584                 if (!IsFailed(r))
585                 {
586                         __pTableView->SetItemChecked(ID_GROUP_GPS_2, ID_ITEM_GPS_NETWORK_POSITION, itemStatus);
587                 }
588                 AppLogDebug("Wps value[%d] [%s]", itemStatus, GetErrorMessage(GetLastResult()));
589                 __pTableView->UpdateTableView();
590         }
591 }