Fixed N_SE-38563, N_SE-38552
[apps/osp/Settings.git] / src / StPrivacyForm.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                StPrivacyForm.cpp
19  * @brief               This is the implementation file for PrivacyForm class.
20  */
21
22 #include <FSecurity.h>
23 #include "StPrivacyForm.h"
24 #include "StResourceManager.h"
25 #include "StSettingScenesList.h"
26 #include "StTypes.h"
27
28 using namespace Tizen::App::Package;
29 using namespace Tizen::Base;
30 using namespace Tizen::Base::Collection;
31 using namespace Tizen::Base::Utility;
32 using namespace Tizen::Graphics;
33 using namespace Tizen::Security;
34 using namespace Tizen::Ui;
35 using namespace Tizen::Ui::Controls;
36 using namespace Tizen::Ui::Scenes;
37
38 static const int ID_GROUP_COUNT = 1;
39
40 PrivacyForm::PrivacyForm(void)
41         : __pPrivacyManager(null)
42         , __privacyPackageList(null)
43 {
44 }
45
46 PrivacyForm::~PrivacyForm(void)
47 {
48 }
49
50 void
51 PrivacyForm::CreateFooter(void)
52 {
53         Footer* pFooter = GetFooter();
54         AppAssert(pFooter);
55
56         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
57         pFooter->SetBackButton();
58         pFooter->AddActionEventListener(*this);
59
60         SetFormBackEventListener(this);
61 }
62
63 result
64 PrivacyForm::OnInitializing(void)
65 {
66         AppLogDebug("Enter");
67
68         result r = InitPrivacyAppPackageList();
69         if (IsFailed(r))
70         {
71                 AppLogDebug("InitPrivacyAppPackageList");
72                 return E_FAILURE;
73         }
74
75         CreateHeader(ResourceManager::GetString(L"IDS_COM_BODY_PRIVACY"));
76         CreateFooter();
77         UpdateTableViewAndNoContent();
78
79         return r;
80 }
81
82 result
83 PrivacyForm::OnTerminating(void)
84 {
85         __pPrivacyManager = null;
86
87         if (__privacyPackageList != null)
88         {
89                 __privacyPackageList->RemoveAll(true);
90                 __privacyPackageList = null;
91         }
92
93         int controlCount = GetControlCount();
94         for (int i = 0; i < controlCount; i++)
95         {
96                 RemoveControl(GetControl(0));
97         }
98         __pTableView = null;
99
100         SetFormBackEventListener(null);
101         return E_SUCCESS;
102 }
103
104 void
105 PrivacyForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
106 {
107         AppLogDebug("Enter");
108
109         if (pArgs != null)
110         {
111                 delete pArgs;
112         }
113
114         UpdateTableViewAndNoContent();
115 }
116
117 result
118 PrivacyForm::InitPrivacyAppPackageList(void)
119 {
120         __pPrivacyManager = PrivacyManager::GetInstance();
121         if (__pPrivacyManager == null)
122         {
123                 AppLogDebug("pPrivacyManager is null");
124                 return E_FAILURE;
125         }
126
127         __privacyPackageList = static_cast<ArrayList*>(__pPrivacyManager->GetPrivacyAppPackageListN());
128
129         if (__privacyPackageList == null)
130         {
131                 AppLogDebug("__privacyPackageList is null");
132                 return E_FAILURE;
133         }
134         return E_SUCCESS;
135 }
136
137 Tizen::Base::String
138 PrivacyForm::GetDisplayPackageName(int itemIndex)
139 {
140         String result;
141
142         if (__privacyPackageList == null)
143         {
144                 return result;
145         }
146
147         String packageId = *static_cast<String*>(__privacyPackageList->GetAt(itemIndex));
148         if (packageId.IsEmpty() == true)
149         {
150                 AppLogDebug("packageId is empty");
151                 return result;
152         }
153
154         PackageManager* pPackageManager = PackageManager::GetInstance();
155         if (pPackageManager == null)
156         {
157                 AppLogDebug("pPackageManager is null");
158         }
159         else
160         {
161                 PackageInfo* pPackageInfo = pPackageManager->GetPackageInfoN(packageId);
162                 if (pPackageInfo == null)
163                 {
164                         AppLogDebug("pPackageInfo is null. GetDisplayName fail");
165                         return result;
166                 }
167                 return pPackageInfo->GetDisplayName();
168         }
169         return result;
170 }
171
172 Tizen::Base::String
173 PrivacyForm::GetUsedPrivacyService(int itemIndex)
174 {
175         String result;
176
177         if (__privacyPackageList == null)
178         {
179                 return result;
180         }
181
182         ArrayList* pPrivacyServiceList = static_cast<ArrayList*>(__privacyPackageList->GetAt(itemIndex));
183         if (pPrivacyServiceList == null)
184         {
185                 AppLogDebug("pPrivacyServiceList is null");
186                 return result;
187         }
188
189         String packageId = *static_cast<String*>(__privacyPackageList->GetAt(itemIndex));
190
191         ArrayList* privacyListInfo = static_cast<ArrayList*>(__pPrivacyManager->GetPrivacyInfoListN(packageId));
192         if (privacyListInfo == null)
193         {
194                 AppLogDebug("privacyListInfo null");
195                 return result;
196         }
197
198         int serviceCount = privacyListInfo->GetCount();
199         for (int index = 0; index < serviceCount; index++)
200         {
201                 PrivacyInfo* pPrivacyService = static_cast<PrivacyInfo*>(privacyListInfo->GetAt(index));
202
203                 String tokenData;
204                 String key = pPrivacyService->GetDisplayName();
205
206                 StringTokenizer stringTokenizer(key, L"/");
207                 while (stringTokenizer.HasMoreTokens() == true)
208                 {
209                         stringTokenizer.GetNextToken(tokenData);
210                 }
211
212                 result.Append(tokenData);
213
214                 if (index != (serviceCount - 1))
215                 {
216                         result.Append(L", ");
217                 }
218         }
219         return result;
220 }
221
222 void
223 PrivacyForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
224 {
225 }
226
227 void
228 PrivacyForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
229 {
230         SceneManager* pSceneManager = SceneManager::GetInstance();
231         AppAssert(pSceneManager);
232
233         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
234 }
235
236 int
237 PrivacyForm::GetGroupCount(void)
238 {
239         AppLogDebug("ENTER");
240         return ID_GROUP_COUNT;
241 }
242
243 int
244 PrivacyForm::GetItemCount(int groupIndex)
245 {
246         int itemCount = 0;
247
248         itemCount = GetPackageListCount();
249         AppLogDebug("GetItemCount %d", itemCount);
250
251         return itemCount;
252 }
253
254 TableViewGroupItem*
255 PrivacyForm::CreateGroupItem(int groupIndex, int itemWidth)
256 {
257         AppLogDebug("ENTER");
258
259         int itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
260         int yItemOffset = H_GROUP_INDEX_HELP_TEXT_TOP_GAP;
261         LabelTextStyle style = LABEL_TEXT_STYLE_NORMAL;
262         Rectangle itemMainRectangle;
263         String groupText;
264         Label* pLabel = null;
265         int fontSize = GetFontSize();
266
267         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
268
269         yItemOffset = H_GROUP_INDEX_NO_HELP_TEXT_GAP;
270         itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
271
272         itemMainRectangle.x = X_GROUP_ITEM_DEFAULT_LABEL;
273         itemMainRectangle.y = yItemOffset;
274         itemMainRectangle.width = itemWidth;
275         itemMainRectangle.height = itemHeight;
276
277         RelativeLayout relativeLayout;
278         relativeLayout.Construct();
279
280         pItem->Construct(relativeLayout, Dimension(itemWidth, itemHeight));
281         pItem->SetBackgroundColor(COLOR_BG_GROUP_INDEX_DEFAULT);
282
283         pLabel = new (std::nothrow) Label();
284         pLabel->Construct(itemMainRectangle, groupText);
285         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
286         pLabel->SetTextVerticalAlignment(ALIGNMENT_TOP);
287         pLabel->SetTextConfig(fontSize, style);
288         pLabel->SetTextColor(COLOR_HELP_TEXT_TYPE_01);
289
290         pItem->AddControl(pLabel);
291         relativeLayout.SetMargin(*pLabel, itemMainRectangle.x, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
292         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
293         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
294         pItem->SetEnabled(false);
295
296         return pItem;
297 }
298
299 TableViewItem*
300 PrivacyForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
301 {
302         AppLogDebug("group[%d] index[%d]", groupIndex, itemIndex);
303
304         Rectangle itemMainRectangle;
305         Rectangle itemSubRectangle;
306         TableViewAnnexStyle style = TABLE_VIEW_ANNEX_STYLE_NORMAL;
307         String itemMainText;
308         String itemSubText;
309         String fontReturnValue;
310         Label* pLabel = null;
311
312         int fontSize = GetFontSize();
313         int itemHeight = H_GROUP_ITEM_DEFAULT;
314         Rectangle detailArrow;
315
316         itemMainText = GetDisplayPackageName(itemIndex);
317         itemSubText = GetUsedPrivacyService(itemIndex);
318
319         ItemTypeTwoLine(itemMainRectangle, itemSubRectangle, fontSize);
320         itemHeight = (itemMainRectangle.height + itemSubRectangle.height);
321
322         RelativeLayout relativeLayout;
323         relativeLayout.Construct();
324
325         TableViewItem* pItem = new (std::nothrow) TableViewItem();
326
327         pItem->Construct(relativeLayout, Dimension(itemWidth, itemHeight), style);
328         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
329
330         pLabel = new (std::nothrow) Label();
331         pLabel->Construct(itemMainRectangle, itemMainText);
332         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
333         pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
334         pLabel->SetTextColor(COLOR_MAIN_TEXT);
335
336         pItem->AddControl(pLabel);
337         relativeLayout.SetMargin(*pLabel, itemMainRectangle.x, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
338         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
339
340         pLabel = new (std::nothrow) Label();
341         pLabel->Construct(itemSubRectangle, itemSubText);
342         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
343         pLabel->SetTextVerticalAlignment(ALIGNMENT_TOP);
344         pLabel->SetTextConfig(FONT_SIZE_SUB_TEXT, LABEL_TEXT_STYLE_NORMAL);
345         pLabel->SetTextColor(COLOR_SUB_TEXT);
346
347         pItem->AddControl(pLabel);
348         relativeLayout.SetMargin(*pLabel, itemSubRectangle.x, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
349         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
350         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
351
352         return pItem;
353 }
354
355 bool
356 PrivacyForm::DeleteGroupItem(int groupIndex, TableViewGroupItem* pItem)
357 {
358         AppLogDebug("ENTER");
359
360         delete pItem;
361         pItem = null;
362
363         return true;
364 }
365
366 bool
367 PrivacyForm::DeleteItem(int groupIndex, int itemIndex, TableViewItem* pItem)
368 {
369         AppLogDebug("ENTER");
370
371         delete pItem;
372         pItem = null;
373
374         return true;
375 }
376
377 void
378 PrivacyForm::OnGroupedTableViewItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
379 {
380         AppLogDebug("group[%d] index[%d]", groupIndex, itemIndex);
381
382         SceneManager* pSceneManager = SceneManager::GetInstance();
383         AppAssert(pSceneManager);
384
385         ArrayList* pPrivacyServiceData = new (std::nothrow) ArrayList(SingleObjectDeleter);
386         pPrivacyServiceData->Construct();
387
388         Label* firstLineLabel = static_cast<Label*>(pItem->GetControl(0));
389         Label* secondLineLabel = static_cast<Label*>(pItem->GetControl(1));
390
391         String packageId = *static_cast<String*>(__privacyPackageList->GetAt(itemIndex));
392         pPrivacyServiceData->Add(new (std::nothrow) String(packageId));
393
394         if (firstLineLabel)
395         {
396                 pPrivacyServiceData->Add(new (std::nothrow) String(firstLineLabel->GetText()));
397         }
398
399         if (secondLineLabel)
400         {
401                 pPrivacyServiceData->Add(new (std::nothrow) String(secondLineLabel->GetText()));
402         }
403
404         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_PRIVACY_DETAIL, SCENE_TRANSITION_ANIMATION_TYPE_LEFT), pPrivacyServiceData);
405 }
406
407 int
408 PrivacyForm::GetDefaultGroupItemHeight(void)
409 {
410         return H_GROUP_INDEX_NO_TITLE_DEFAULT;
411 }
412
413 int
414 PrivacyForm::GetDefaultItemHeight(void)
415 {
416         return H_GROUP_ITEM_DEFAULT;
417 }
418
419 void
420 PrivacyForm::UpdateGroupItem(int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem)
421 {
422 }
423
424 void
425 PrivacyForm::UpdateItem(int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
426 {
427         Invalidate(true);
428 }
429
430 void
431 PrivacyForm::OnGroupedTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem* pContextItem, bool activated)
432 {
433 }
434
435 void
436 PrivacyForm::OnGroupedTableViewGroupItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
437 {
438 }
439
440 int
441 PrivacyForm::GetPackageListCount(void)
442 {
443         if (__privacyPackageList == null)
444         {
445                 AppLogDebug("__privacyPackageList is null");
446                 return 0;
447         }
448
449         return __privacyPackageList->GetCount();
450 }
451
452 void
453 PrivacyForm::UpdateTableViewAndNoContent(void)
454 {
455         bool createTableView = false;
456         if (__pTableView != null)
457         {
458                 if (GetPackageListCount() != 0)
459                 {
460                         __pTableView->UpdateTableView();
461                         return;
462                 }
463         }
464         else
465         {
466                 if (GetPackageListCount() != 0)
467                 {
468                         createTableView = true;
469                 }
470         }
471
472         if (__pTableView != null)
473         {
474                 int tableViewControlCount = __pTableView->GetControlCount();
475                 for (int i = 0; i < tableViewControlCount; i++)
476                 {
477                         __pTableView->RemoveControl(__pTableView->GetControl(0));
478                 }
479 //                      __pTableView->RemoveAllControls();
480                 __pTableView = null;
481         }
482         int controlCount = GetControlCount();
483         for (int i = 0; i < controlCount; i++)
484         {
485                 RemoveControl(GetControl(0));
486         }
487 //      RemoveAllControls();
488
489         if (createTableView)
490         {
491                 CreateTableView();
492                 __pTableView->UpdateTableView();
493         }
494         else
495         {
496                 Label* pLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS, false));
497                 if (pLabel == null)
498                 {
499                         Rectangle clientRect = GetClientAreaBounds();
500                         Bitmap* pBitmp = ResourceManager::GetBitmapN(IDB_NO_CONTENTS);
501
502                         int bitmapWidth = pBitmp->GetWidth();
503                         int bitmapHeight = pBitmp->GetHeight();
504                         String labelText = ResourceManager::GetString(L"IDS_ST_BODY_NO_CONTENT");
505                         Color textColor = COLOR_HELP_TEXT_TYPE_03_NORMAL;
506                         int noContentTextHeight = GetHeightForStringArea(labelText, bitmapWidth, 32);
507
508                         int xPos = (clientRect.width / LINE_COUNT_2) - (bitmapWidth / DIVIDE_BY_TWO);
509                         int yPos = (clientRect.height / LINE_COUNT_2) - ((bitmapHeight + noContentTextHeight) / DIVIDE_BY_TWO);
510
511                         Label* pLabel = new (std::nothrow) Label();
512                         pLabel->Construct(Rectangle(xPos, yPos, bitmapWidth, bitmapHeight), L"");
513                         pLabel->SetName(NO_CONTENTS);
514                         pLabel->SetBackgroundBitmap(*pBitmp);
515
516                         yPos = yPos + bitmapHeight;
517
518                         Label* pLabelNoContents = new (std::nothrow) Label();
519                         pLabelNoContents->Construct(Rectangle(0, yPos, clientRect.width, noContentTextHeight), L"");
520                         pLabelNoContents->SetName(NO_CONTENTS_TEXT);
521                         pLabelNoContents->SetTextColor(textColor);
522                         pLabelNoContents->SetText(labelText);
523
524                         AddControl(pLabel);
525                         AddControl(pLabelNoContents);
526                 }
527         }
528 }
529
530 void
531 PrivacyForm::OnOrientationChanged(const Tizen::Ui::Control& source, Tizen::Ui::OrientationStatus orientationStatus)
532 {
533         Label* pLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS, false));
534         if (pLabel != null)
535         {
536                 Rectangle clientRect = GetClientAreaBounds();
537                 Rectangle labelBound = pLabel->GetBounds();
538
539                 int xPos = (clientRect.width / LINE_COUNT_2) - (labelBound.width / LINE_COUNT_2);
540                 int yPos = (clientRect.height / LINE_COUNT_2) -(labelBound.height / LINE_COUNT_2);
541
542                 pLabel->SetBounds(Rectangle(xPos, yPos, labelBound.width, labelBound.height));
543                 yPos = yPos + pLabel->GetBounds().height;
544                 Label* pTextLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS_TEXT, false));
545                 if (pTextLabel != null)
546                 {
547                         pTextLabel->SetBounds(Rectangle(0, yPos, clientRect.width, pTextLabel->GetBounds().height));
548                 }
549         }
550         Invalidate(true);
551 }