Applied latest source code
[apps/native/preloaded/Settings.git] / src / StScreenTypeForm.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                StScreenTypeForm.cpp
19  * @brief               This is the implementation file for ScreenTypeForm class.
20  */
21
22 #include "StScreenTypeForm.h"
23 #include "StResourceManager.h"
24 #include "StSettingScenesList.h"
25 #include "StTypes.h"
26
27 using namespace Tizen::App;
28 using namespace Tizen::App::Package;
29 using namespace Tizen::Base;
30 using namespace Tizen::Base::Collection;
31 using namespace Tizen::Graphics;
32 using namespace Tizen::System;
33 using namespace Tizen::Ui;
34 using namespace Tizen::Ui::Controls;
35 using namespace Tizen::Ui::Scenes;
36
37 static const int ID_GROUP_COUNT = 1;
38 static const int ID_GROUP_MAX_ITEM_COUNT = 1;
39 static const int ID_INDEX_HOME_SCREEN_WALLPAPER = 0;
40 static const int ID_INDEX_LOCK_SCREEN_WALLPAPER = 1;
41
42 ScreenTypeForm::ScreenTypeForm(void)
43         : __wallpaperIndex(0)
44         , __pPackageAppInfoList(null)
45 {
46 }
47
48 ScreenTypeForm::~ScreenTypeForm(void)
49 {
50 }
51
52 void
53 ScreenTypeForm::CreateFooter(void)
54 {
55         Footer* pFooter = GetFooter();
56         AppAssert(pFooter);
57
58         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
59         pFooter->AddActionEventListener(*this);
60
61         SetFormBackEventListener(this);
62 }
63
64 result
65 ScreenTypeForm::OnInitializing(void)
66 {
67         return E_SUCCESS;
68 }
69
70 result
71 ScreenTypeForm::OnTerminating(void)
72 {
73         RemovePackageInfoList();
74         __pTableView = null;
75
76         SetFormBackEventListener(null);
77         return E_SUCCESS;
78 }
79
80 void
81 ScreenTypeForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
82 {
83         if (pArgs != null)
84         {
85                 SetWallpaperIndex(*static_cast<String*>(pArgs->GetAt(0)));
86                 delete pArgs;
87                 AppLogDebug("Screen Mode[%d] ( home::0 / Lock::1 )", GetWallpaperIndex());
88
89                 InitPackageInfoList();
90
91                 CreateHeader(GetTitleText());
92                 UpdateTableViewAndNoContent();
93                 AppLogDebug("ENTER");
94
95                 if (__pPackageAppInfoList != null)
96                 {
97                         String applicationId;
98                         String screenType;
99                         if (GetWallpaperIndex() == 0)
100                         {
101                                 screenType = SETTING_INFO_KEY_SCREEN_TYPE_HOME;
102                         }
103                         else
104                         {
105                                 screenType = SETTING_INFO_KEY_SCREEN_TYPE_LOCK;
106                         }
107                         result r = SettingInfo::GetValue(screenType, applicationId);
108                         AppLogDebug("GetValue(%ls, ..) result=[%s]", applicationId.GetPointer(), GetErrorMessage(r));
109
110                         for (int index = 0; index < __pPackageAppInfoList->GetCount(); index++)
111                         {
112                                 PackageAppInfo* pPackageAppInfo = dynamic_cast<PackageAppInfo*>(__pPackageAppInfoList->GetAt(index));
113                                 if (pPackageAppInfo)
114                                 {
115                                         if (applicationId.CompareTo(pPackageAppInfo->GetAppId()) == 0)
116                                         {
117                                                 AppLogDebug("index [%d]", index);
118                                                 if (__pTableView != null)
119                                                 {
120                                                         __pTableView->SetItemChecked(0, index, true);
121                                                 }
122                                                 break;
123                                         }
124                                 }
125                         }
126                 }
127         }
128         else
129         {
130                 AppLogDebug("pArgs Null");
131         }
132 }
133
134 void
135 ScreenTypeForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
136 {
137 }
138
139 void
140 ScreenTypeForm::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 int
149 ScreenTypeForm::GetGroupCount(void)
150 {
151         AppLogDebug("ENTER");
152
153         return ID_GROUP_COUNT;
154 }
155
156 int
157 ScreenTypeForm::GetItemCount(int groupIndex)
158 {
159         int itemCount = GetPackageAppInfoListCount();
160
161         AppLogDebug("GetItemCount %d", itemCount);
162
163         return itemCount;
164 }
165
166 TableViewGroupItem*
167 ScreenTypeForm::CreateGroupItem(int groupIndex, int itemWidth)
168 {
169         AppLogDebug("ENTER");
170
171         int itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
172         int yItemOffset = H_GROUP_INDEX_HELP_TEXT_TOP_GAP;
173         LabelTextStyle style = LABEL_TEXT_STYLE_NORMAL;
174         Rectangle itemMainRectangle;
175         String groupText;
176         Label* pLabel = null;
177         int fontSize = GetFontSize();
178
179         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
180
181         yItemOffset = H_GROUP_INDEX_NO_HELP_TEXT_GAP;
182         itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
183
184         itemMainRectangle.x = X_GROUP_ITEM_DEFAULT_LABEL;
185         itemMainRectangle.y = yItemOffset;
186         itemMainRectangle.width = itemWidth;
187         itemMainRectangle.height = itemHeight;
188
189         RelativeLayout relativeLayout;
190         relativeLayout.Construct();
191
192         pItem->Construct(relativeLayout, Dimension(itemWidth, itemHeight));
193         pItem->SetBackgroundColor(COLOR_BG_GROUP_INDEX_DEFAULT);
194
195         pLabel = new (std::nothrow) Label();
196         pLabel->Construct(itemMainRectangle, groupText);
197         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
198         pLabel->SetTextVerticalAlignment(ALIGNMENT_TOP);
199         pLabel->SetTextConfig(fontSize, style);
200         pLabel->SetTextColor(COLOR_HELP_TEXT_TYPE_01);
201
202         pItem->AddControl(pLabel);
203         relativeLayout.SetMargin(*pLabel, itemMainRectangle.x, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
204         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
205         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
206         pItem->SetEnabled(false);
207
208         return pItem;
209 }
210
211 TableViewItem*
212 ScreenTypeForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
213 {
214         AppLogDebug("group[%d] index[%d]", groupIndex, itemIndex);
215
216         Rectangle itemMainRectangle;
217         TableViewAnnexStyle style = TABLE_VIEW_ANNEX_STYLE_RADIO;
218         String itemMainText = L"";
219         String valueKeyString = L"";
220         String fontReturnValue;
221         Label* pLabel = null;
222
223         int fontSize = GetFontSize();
224         int itemHeight = H_GROUP_ITEM_DEFAULT;
225
226         itemMainText = GetItemString(itemIndex);
227         TableViewItem* pItem = new (std::nothrow) TableViewItem();
228
229         RelativeLayout relativeLayout;
230         relativeLayout.Construct();
231
232         pItem->Construct(relativeLayout, Dimension(itemWidth, itemHeight), style);
233         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
234
235         ItemTypeOneLine(itemMainRectangle);
236         pLabel = new (std::nothrow) Label();
237         pLabel->Construct(itemMainRectangle, itemMainText);
238         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
239         pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
240         pLabel->SetTextColor(COLOR_MAIN_TEXT);
241
242         pItem->AddControl(pLabel);
243         relativeLayout.SetMargin(*pLabel, RELATIVE_LAYOUT_LEFT_MARGIN, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
244         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
245         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
246
247         return pItem;
248 }
249
250 bool
251 ScreenTypeForm::DeleteGroupItem(int groupIndex, TableViewGroupItem* pItem)
252 {
253         AppLogDebug("ENTER");
254
255         delete pItem;
256         pItem = null;
257
258         return true;
259 }
260
261 bool
262 ScreenTypeForm::DeleteItem(int groupIndex, int itemIndex, TableViewItem* pItem)
263 {
264         AppLogDebug("ENTER");
265
266         delete pItem;
267         pItem = null;
268
269         return true;
270 }
271
272 void
273 ScreenTypeForm::OnGroupedTableViewItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
274 {
275         AppLogDebug("group[%d] index[%d]", groupIndex, itemIndex);
276
277         int groupCount = __pTableView->GetItemCountAt(groupIndex);
278
279         for (int index = 0; index < groupCount; index++)
280         {
281                 if (index == itemIndex)
282                 {
283                         tableView.SetItemChecked(groupIndex, index, true);
284                 }
285                 else
286                 {
287                         tableView.SetItemChecked(groupIndex, index, false);
288                 }
289         }
290
291         SceneManager* pSceneManager = SceneManager::GetInstance();
292         AppAssert(pSceneManager);
293
294         PackageAppInfo* pPackageAppInfo = static_cast <PackageAppInfo*>(__pPackageAppInfoList->GetAt(itemIndex));
295         AppLogDebug("AppId [%ls]", pPackageAppInfo->GetAppId().GetPointer());
296
297         String screenType;
298         if (GetWallpaperIndex() == 0)
299         {
300                 screenType = SETTING_INFO_KEY_SCREEN_TYPE_HOME;
301         }
302         else
303         {
304                 screenType = SETTING_INFO_KEY_SCREEN_TYPE_LOCK;
305         }
306
307         result r = SettingInfo::SetValueForPrivilegedKey(screenType, pPackageAppInfo->GetAppId());
308         AppLogDebug("SetValue(%ls, ..) result=[%s]", screenType.GetPointer(), GetErrorMessage(r));
309
310         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
311 }
312
313 Tizen::Base::String
314 ScreenTypeForm::GetTitleText(void)
315 {
316         String headerText;
317
318         switch (GetWallpaperIndex())
319         {
320         case ID_INDEX_HOME_SCREEN_WALLPAPER:
321                 {
322                         headerText = ResourceManager::GetString(L"IDS_ST_BODY_HOME_SCREEN_TYPE");
323                 }
324                 break;
325
326         case ID_INDEX_LOCK_SCREEN_WALLPAPER:
327                 {
328                         headerText = ResourceManager::GetString(L"IDS_ST_BODY_SCREEN_LOCK_TYPE");
329                 }
330                 break;
331         }
332
333         return headerText;
334 }
335
336 int
337 ScreenTypeForm::GetWallpaperIndex(void)
338 {
339         return __wallpaperIndex;
340 }
341
342 void
343 ScreenTypeForm::SetWallpaperIndex(Tizen::Base::String& whitchOne)
344 {
345         Integer::Parse(whitchOne, __wallpaperIndex);
346 }
347
348 int
349 ScreenTypeForm::GetDefaultGroupItemHeight(void)
350 {
351         return H_GROUP_INDEX_NO_TITLE_DEFAULT;
352 }
353
354 int
355 ScreenTypeForm::GetDefaultItemHeight(void)
356 {
357         return H_GROUP_ITEM_DEFAULT;
358 }
359
360 void
361 ScreenTypeForm::UpdateGroupItem(int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem)
362 {
363 }
364
365 void
366 ScreenTypeForm::UpdateItem(int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
367 {
368         Invalidate(true);
369 }
370
371 void
372 ScreenTypeForm::OnGroupedTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem* pContextItem, bool activated)
373 {
374 }
375
376 void
377 ScreenTypeForm::OnGroupedTableViewGroupItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
378 {
379 }
380
381 result
382 ScreenTypeForm::InitPackageInfoList(void)
383 {
384         PackageManager* pPackageManager = PackageManager::GetInstance();
385         if (pPackageManager == null)
386         {
387                 return E_FAILURE;
388         }
389
390         result r;
391         HashMap packageAppFilterMap;
392         packageAppFilterMap.Construct();
393
394         String* pKey = null;
395         if (GetWallpaperIndex() == ID_INDEX_HOME_SCREEN_WALLPAPER)
396         {
397                 pKey = new (std::nothrow) String(L"http://tizen.org/category/homeapp");
398         }
399         else
400         {
401                 pKey = new (std::nothrow) String(L"http://tizen.org/category/lockapp");
402         }
403
404         Boolean* pValue = new (std::nothrow) Boolean(true);
405         r = packageAppFilterMap.Add(pKey, pValue);
406
407         __pPackageAppInfoList = pPackageManager->GetPackageAppInfoListN(packageAppFilterMap);
408
409 #if 1 // temp for debugging
410         AppLogDebug("Package app filter = [%ls]", pKey->GetPointer());
411
412         if (__pPackageAppInfoList)
413         {
414                 for (int i = 0; i < __pPackageAppInfoList->GetCount(); i++)
415                 {
416                         PackageAppInfo* pPackageAppInfo = dynamic_cast<PackageAppInfo*>(__pPackageAppInfoList->GetAt(i));
417                         if (pPackageAppInfo)
418                         {
419                                 AppLogDebug("[%d]-------------------------------------------------------", i+1);
420                                 AppLogDebug("AppId [%ls]", pPackageAppInfo->GetAppId().GetPointer());
421                                 AppLogDebug("Name [%ls]", pPackageAppInfo->GetAppName().GetPointer());
422                         }
423                         else
424                         {
425                                 AppLogDebug("pPackageAppInfo is null.");
426                         }
427                 }
428         }
429         else
430         {
431                 AppLogDebug("pList is null.");
432         }
433 #endif
434
435         if (__pPackageAppInfoList == null)
436         {
437                 return E_FAILURE;
438         }
439
440         return E_SUCCESS;
441 }
442
443 void
444 ScreenTypeForm::RemovePackageInfoList(void)
445 {
446         __pPackageAppInfoList = null;
447 }
448
449 int
450 ScreenTypeForm::GetPackageAppInfoListCount(void)
451 {
452         if(__pPackageAppInfoList)
453         {
454                 return __pPackageAppInfoList->GetCount();
455         }
456
457         return 0;
458 }
459
460 Tizen::Base::String
461 ScreenTypeForm::GetScreenType(void)
462 {
463         String screenType;
464
465         switch (GetWallpaperIndex())
466         {
467         case ID_INDEX_HOME_SCREEN_WALLPAPER:
468                 {
469                         screenType = SETTING_INFO_KEY_SCREEN_TYPE_HOME;
470                 }
471                 break;
472
473         case ID_INDEX_LOCK_SCREEN_WALLPAPER:
474                 {
475                         screenType = SETTING_INFO_KEY_SCREEN_TYPE_LOCK;
476                 }
477                 break;
478
479         default:
480                 {
481                         AppLogDebug("Page index [%d]", GetWallpaperIndex());
482                 }
483                 break;
484         }
485         return screenType;
486 }
487
488 Tizen::Base::String
489 ScreenTypeForm::GetItemString(int itemIndex)
490 {
491         String displayName;
492         String applicationId;
493         String returnValue;
494
495         PackageAppInfo* pPackageAppInfo = static_cast<PackageAppInfo*>(__pPackageAppInfoList->GetAt(itemIndex));
496         if (pPackageAppInfo)
497         {
498                 applicationId = pPackageAppInfo->GetAppId();
499
500                 if (applicationId.IsEmpty() == false)
501                 {
502                         if (applicationId.Equals(L"org.tizen.menu-screen", false))
503                         {
504                                 // Preloaded home
505                                 returnValue = ResourceManager::GetString(L"IDS_ST_BODY_DEFAULT_HOME_SCREEN");
506                         }
507                         else if (applicationId.Equals(L"org.tizen.lockscreen", false))
508                         {
509                                 // Preloaded lock
510                                 returnValue = ResourceManager::GetString(L"IDS_ST_MBODY_SWIPE");
511                         }
512                         else
513                         {
514                                 // Downloaded home/lock
515                                 displayName = pPackageAppInfo->GetAppDisplayName();
516
517                                 if (displayName.IsEmpty() == false)
518                                 {
519                                         returnValue = displayName;
520                                 }
521                                 else
522                                 {
523                                         returnValue = applicationId;
524                                 }
525                         }
526                 }
527         }
528
529         return returnValue;
530 }
531
532 void
533 ScreenTypeForm::UpdateTableViewAndNoContent(void)
534 {
535         bool createTableView = false;
536         if (__pTableView != null)
537         {
538                 if (GetPackageAppInfoListCount() != 0)
539                 {
540                         __pTableView->UpdateTableView();
541                         return;
542                 }
543         }
544         else
545         {
546                 if (GetPackageAppInfoListCount() != 0)
547                 {
548                         createTableView = true;
549                 }
550         }
551
552         if (__pTableView != null)
553         {
554                 int tableViewControlCount = __pTableView->GetControlCount();
555                 for (int i = 0; i < tableViewControlCount; i++)
556                 {
557                         __pTableView->RemoveControl(__pTableView->GetControl(0));
558                 }
559 //              __pTableView->RemoveAllControls();
560                 __pTableView = null;
561         }
562         int controlCount = GetControlCount();
563         for (int i = 0; i < controlCount; i++)
564         {
565                 RemoveControl(GetControl(0));
566         }
567 //      RemoveAllControls();
568
569         if (createTableView)
570         {
571                 CreateTableView();
572                 __pTableView->UpdateTableView();
573         }
574         else
575         {
576                 Label* pLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS, false));
577                 if (pLabel == null)
578                 {
579                         Rectangle clientRect = GetClientAreaBounds();
580                         Bitmap* pBitmp = ResourceManager::GetBitmapN(IDB_NO_CONTENTS);
581
582                         int bitmapWidth = pBitmp->GetWidth();
583                         int bitmapHeight = pBitmp->GetHeight();
584                         String labelText = ResourceManager::GetString(L"IDS_ST_BODY_NO_CONTENT");
585                         Color textColor = COLOR_HELP_TEXT_TYPE_03_NORMAL;
586                         int noContentTextHeight = GetHeightForStringArea(labelText, bitmapWidth, 32);
587
588                         int xPos = (clientRect.width / LINE_COUNT_2) - (bitmapWidth / DIVIDE_BY_TWO);
589                         int yPos = (clientRect.height / LINE_COUNT_2) - ((bitmapHeight + noContentTextHeight) / DIVIDE_BY_TWO);
590
591                         Label* pLabel = new (std::nothrow) Label();
592                         pLabel->Construct(Rectangle(xPos, yPos, bitmapWidth, bitmapHeight), L"");
593                         pLabel->SetName(NO_CONTENTS);
594                         pLabel->SetBackgroundBitmap(*pBitmp);
595
596                         yPos = yPos + bitmapHeight;
597
598                         Label* pLabelNoContents = new (std::nothrow) Label();
599                         pLabelNoContents->Construct(Rectangle(0, yPos, clientRect.width, noContentTextHeight), L"");
600                         pLabelNoContents->SetName(NO_CONTENTS_TEXT);
601                         pLabelNoContents->SetTextColor(textColor);
602                         pLabelNoContents->SetText(labelText);
603
604                         AddControl(pLabel);
605                         AddControl(pLabelNoContents);
606                 }
607         }
608 }
609
610 void
611 ScreenTypeForm::OnOrientationChanged(const Tizen::Ui::Control& source, Tizen::Ui::OrientationStatus orientationStatus)
612 {
613         Label* pLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS, false));
614         if (pLabel != null)
615         {
616                 Rectangle clientRect = GetClientAreaBounds();
617                 Rectangle labelBound = pLabel->GetBounds();
618
619                 int xPos = (clientRect.width / LINE_COUNT_2) - (labelBound.width / LINE_COUNT_2);
620                 int yPos = (clientRect.height / LINE_COUNT_2) -(labelBound.height / LINE_COUNT_2);
621
622                 pLabel->SetBounds(Rectangle(xPos, yPos, labelBound.width, labelBound.height));
623                 yPos = yPos + pLabel->GetBounds().height;
624                 Label* pTextLabel = static_cast<Label*>(this->GetControl(NO_CONTENTS_TEXT, false));
625                 if (pTextLabel != null)
626                 {
627                         pTextLabel->SetBounds(Rectangle(0, yPos, clientRect.width, pTextLabel->GetBounds().height));
628                 }
629         }
630         Invalidate(true);
631 }