Applied latest source code
[apps/native/preloaded/Settings.git] / src / StManageApplicationForm.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                StManageApplicationForm.cpp
19  * @brief               This is the implementation file for ManageApplicationForm class.
20  */
21
22 #include "StManageApplicationForm.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 IDA_UNINSTALL_OK = 400;
38
39 static const int Y_ALERT_POPUP_CLIENT_RECT_DEFAULT = 160;
40 static const int X_ALERT_POPUP_ITEM_RECT_LABEL_DEFAULT = 26;
41 static const int Y_ALERT_POPUP_ITEM_RECT_LABEL_DEFAULT = 20;
42 static const int H_ALERT_POPUP_ITEM_RECT_LABEL_DEFAULT = 340;
43 static const int W_ALERT_POPUP_ITEM_RECT_LABEL_DEFAULT = 690;
44 static const int X_ALERT_POPUP_ITEM_RECT_LHS_BUTTON_DEFAULT = 12;
45 static const int Y_ALERT_POPUP_ITEM_RECT_LHS_BUTTON_DEFAULT = 360;
46 static const int W_ALERT_POPUP_ITEM_RECT_LHS_BUTTON_DEFAULT = 620;
47 static const int H_ALERT_POPUP_ITEM_RECT_LHS_BUTTON_DEFAULT = 80;
48 static const int ALERT_POPUP_LABEL_FONT_SIZE = 40;
49
50 static const int W_ALERT_POPUP_CLIENT_RECT_DEFAULT = 700;
51 static const int H_ALERT_POPUP_CLIENT_RECT_DEFAULT = 550;
52
53 static const int BYTE_TO_KILOBYTE = 1024;
54
55 enum _dataType
56 {
57         DATA_TYPE_APPLICATION_NAME = 0,
58         DATA_TYPE_APPLICATION_ICON_PATH,
59         DATA_TYPE_APPLICATION_UNINSTALLABLE,
60         DATA_TYPE_APPLICATION_SIZE,
61         DATA_TYPE_APPLICATION_DATA_SIZE,
62         DATA_TYPE_APPLICATION_ID,
63         DATA_TYPE_APPLICATION_PACKAGE_ID,
64 };
65
66 ManageApplicationForm::ManageApplicationForm(void)
67         : __pPackageInfoList(null)
68         , __uninstallResultPopup(null)
69 {
70 }
71
72 ManageApplicationForm::~ManageApplicationForm(void)
73 {
74 }
75
76 void
77 ManageApplicationForm::CreateHeaderStyleTab(void)
78 {
79 #ifdef HEADER_HAS_THREE_TAB
80         Header* pHeader = GetHeader();
81         pHeader->SetStyle(HEADER_STYLE_TAB_WITH_TITLE);
82         pHeader->SetTitleText(ResourceManager::GetString(L"IDS_ST_BODY_MANAGE_APPLICATIONS"));
83
84         HeaderItem headerItemApplication;
85         headerItemApplication.Construct(IDA_HEADER_APPLICATION);
86         headerItemApplication.SetText(ResourceManager::GetString(L"IDS_ST_BODY_APPLICATION"));
87
88         HeaderItem headerItemRunning;
89         headerItemRunning.Construct(IDA_HEADER_RUNNING);
90         headerItemRunning.SetText(L"Running");
91
92         HeaderItem headerItemAll;
93         headerItemAll.Construct(IDA_HEADER_ALL);
94         headerItemAll.SetText(ResourceManager::GetString(L"IDS_ST_OPT_ALL"));
95
96         pHeader->AddItem(headerItemApplication);
97         pHeader->AddItem(headerItemRunning);
98         pHeader->AddItem(headerItemAll);
99         pHeader->AddActionEventListener(*this);
100 #else
101         CreateHeader(ResourceManager::GetString(L"IDS_ST_BODY_MANAGE_APPLICATIONS"));
102 #endif
103 }
104
105 void
106 ManageApplicationForm::CreateFooter(void)
107 {
108         Footer* pFooter = GetFooter();
109         AppAssert(pFooter);
110
111         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
112         pFooter->AddActionEventListener(*this);
113
114         SetFormBackEventListener(this);
115 }
116
117 Tizen::Base::String
118 ManageApplicationForm::GetPackageInfoItem(int index, int type)
119 {
120         String data;
121         PackageInfo* pPackageInfo = static_cast<PackageInfo*>(__pPackageInfoList->GetAt(index));
122
123         switch (type)
124         {
125         case DATA_TYPE_APPLICATION_NAME:
126                 {
127                         data = pPackageInfo->GetDisplayName();
128                 }
129                 break;
130
131         case DATA_TYPE_APPLICATION_ICON_PATH:
132                 {
133                         IList* pPackageAppInfoList = pPackageInfo->GetPackageAppInfoListN();
134
135                         for (int i = 0; i< pPackageAppInfoList->GetCount(); i++)
136                         {
137                                 PackageAppInfo* pPackageAppInfo = static_cast<PackageAppInfo*>(pPackageAppInfoList->GetAt(i));
138
139                                 data = pPackageAppInfo->GetAppMenuIconPath();
140                         }
141
142                         delete pPackageAppInfoList;
143                 }
144                 break;
145
146         case DATA_TYPE_APPLICATION_UNINSTALLABLE:
147                 {
148                         data = Integer::ToString(pPackageInfo->IsUninstallable());
149                 }
150                 break;
151
152         case DATA_TYPE_APPLICATION_SIZE:
153                 {
154                         data = Integer::ToString((pPackageInfo->GetSize() / BYTE_TO_KILOBYTE));
155                 }
156                 break;
157
158         case DATA_TYPE_APPLICATION_DATA_SIZE:
159                 {
160                         data = Integer::ToString((pPackageInfo->GetDataSize() / BYTE_TO_KILOBYTE));
161                 }
162                 break;
163
164         case DATA_TYPE_APPLICATION_ID:
165                 {
166                         data = pPackageInfo->GetMainAppId();
167                 }
168                 break;
169
170         case DATA_TYPE_APPLICATION_PACKAGE_ID:
171                 {
172                         data = pPackageInfo->GetId();
173                 }
174                 break;
175
176         default:
177                 break;
178         }
179         return data;
180 }
181
182 void
183 ManageApplicationForm::LoadPackageInfoList(void)
184 {
185         if (__pPackageInfoList == null)
186         {
187                 PackageManager* pPackageManager = PackageManager::GetInstance();
188                 __pPackageInfoList = pPackageManager->GetPackageInfoListN();
189         }
190 }
191
192 void
193 ManageApplicationForm::DeletePackageInfoList(void)
194 {
195         if (__pPackageInfoList)
196         {
197                 __pPackageInfoList->RemoveAll(true);
198                 delete __pPackageInfoList;
199         }
200 }
201
202 void
203 ManageApplicationForm::ReLoadPackageInfoList(void)
204 {
205         __pPackageInfoList->RemoveAll(true);
206         delete __pPackageInfoList;
207         __pPackageInfoList = null;
208         LoadPackageInfoList();
209 }
210
211 result
212 ManageApplicationForm::OnInitializing(void)
213 {
214         LoadPackageInfoList();
215
216         CreateHeaderStyleTab();
217         CreateTableView();
218
219         AppLogDebug("ENTER");
220
221         return E_SUCCESS;
222 }
223
224 result
225 ManageApplicationForm::OnTerminating(void)
226 {
227         __pTableView = null;
228
229         DeletePackageInfoList();
230         DeleteAlertPopup();
231
232         SetFormBackEventListener(null);
233         return E_SUCCESS;
234 }
235
236 void
237 ManageApplicationForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
238 {
239         if (previousSceneId.Equals(IDSCN_MANAGE_APPLICATION_INFO, false))
240         {
241                 if (pArgs != null)
242                 {
243                         String* uninstalledApplicationName = (static_cast<String*>(pArgs->GetAt(0)));
244                         String uninstallationResult = *(static_cast<String*>(pArgs->GetAt(1)));
245
246                         ReLoadPackageInfoList();
247                         CreateAlertPopup(uninstalledApplicationName, uninstallationResult.Equals(L"true", false));
248                         ShowAlertPopup();
249                         delete pArgs;
250                 }
251         }
252         __pTableView->UpdateTableView();
253 }
254
255 void
256 ManageApplicationForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
257 {
258 }
259
260 void
261 ManageApplicationForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
262 {
263         SceneManager* pSceneManager = SceneManager::GetInstance();
264         AppAssert(pSceneManager);
265
266         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
267 }
268
269 int
270 ManageApplicationForm::GetGroupCount(void)
271 {
272         // Temporary code due to the UI Framework defect
273         if (!__pPackageInfoList)
274         {
275                 return 0;
276         }
277
278         AppLogDebug("ENTER");
279         return 1;
280 }
281
282 int
283 ManageApplicationForm::GetItemCount(int groupIndex)
284 {
285         // Temporary code due to the UI Framework defect
286         if (!__pPackageInfoList)
287         {
288                 return 0;
289         }
290
291         int itemCount = __pPackageInfoList->GetCount();
292
293         AppLogDebug("GetItemCount %d", itemCount);
294
295         return itemCount;
296 }
297
298 TableViewGroupItem*
299 ManageApplicationForm::CreateGroupItem(int groupIndex, int itemWidth)
300 {
301         AppLogDebug("ENTER");
302
303         int itemHeight = Y_GROUP_DEFAULT;
304         int yItemOffset = H_GROUP_INDEX_HELP_TEXT_TOP_GAP;
305         LabelTextStyle style = LABEL_TEXT_STYLE_NORMAL;
306         Rectangle itemMainRectangle;
307         int fontSize = GetFontSize();
308         String groupText;
309         Label* pLabel = null;
310
311         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
312
313         itemMainRectangle.x = X_GROUP_ITEM_DEFAULT_LABEL;
314         itemMainRectangle.y = yItemOffset;
315         itemMainRectangle.width = itemWidth;
316         itemMainRectangle.height = itemHeight;
317
318         RelativeLayout relativeLayout;
319         relativeLayout.Construct();
320
321         pItem->Construct(relativeLayout, Dimension(itemWidth, itemHeight));
322         pItem->SetBackgroundColor(COLOR_BG_GROUP_INDEX_DEFAULT);
323
324         pLabel = new (std::nothrow) Label();
325         pLabel->Construct(itemMainRectangle, groupText);
326         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
327         pLabel->SetTextVerticalAlignment(ALIGNMENT_TOP);
328         pLabel->SetTextConfig(fontSize, style);
329         pLabel->SetTextColor(COLOR_HELP_TEXT_TYPE_01);
330
331         pItem->AddControl(pLabel);
332         pItem->SetEnabled(false);
333         relativeLayout.SetMargin(*pLabel, itemMainRectangle.x, 0, 0, 0);
334         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
335         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
336
337         return pItem;
338 }
339
340 TableViewItem*
341 ManageApplicationForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
342 {
343         AppLogDebug("group[%d] index[%d]", groupIndex, itemIndex);
344
345         Rectangle itemIconRectangle;
346         Rectangle itemMainRectangle;
347         Rectangle itemSubRectangle;
348         TableViewAnnexStyle style = TABLE_VIEW_ANNEX_STYLE_NORMAL;
349         String itemMainText;
350         String itemSubText;
351         String fontReturnValue;
352         Label* pLabel = null;
353         Bitmap* pBitmap = null;
354
355         int fontSize = GetFontSize();
356         TableViewItem* pItem = new (std::nothrow) TableViewItem();
357
358         itemMainText = GetPackageInfoItem(itemIndex, DATA_TYPE_APPLICATION_NAME);
359         ItemTypeIconAndOneLine(itemIconRectangle, itemMainRectangle);
360
361         RelativeLayout relativeLayout;
362         relativeLayout.Construct();
363
364         pItem->Construct(relativeLayout, Dimension(itemWidth, H_GROUP_ITEM_DEFAULT), style);
365         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
366
367         pBitmap = ResourceManager::GetApplicationBitmapN(GetPackageInfoItem(itemIndex, DATA_TYPE_APPLICATION_ICON_PATH));
368
369         if (pBitmap == null)
370         {
371                 pBitmap = ResourceManager::GetBitmapN(IDB_DEFAULT_ICON);
372         }
373         pBitmap->SetScalingQuality(BITMAP_SCALING_QUALITY_HIGH);
374         pBitmap->Scale(Dimension(W_ITEM_TYPE_3_ICON, H_ITEM_TYPE_3_ICON));
375         pLabel = new (std::nothrow) Label();
376         pLabel->Construct(itemIconRectangle, L"");
377         pLabel->SetBackgroundBitmap(*pBitmap);
378         pItem->AddControl(pLabel);
379
380         pLabel = new (std::nothrow) Label();
381         pLabel->Construct(itemMainRectangle, itemMainText);
382         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
383
384         pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
385         pLabel->SetTextColor(COLOR_MAIN_TEXT);
386
387         pItem->AddControl(pLabel);
388
389         relativeLayout.SetMargin(*pLabel, itemMainRectangle.x, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
390         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
391         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
392         delete pBitmap;
393         return pItem;
394 }
395
396 bool
397 ManageApplicationForm::DeleteGroupItem(int groupIndex, TableViewGroupItem* pItem)
398 {
399         AppLogDebug("ENTER");
400
401         delete pItem;
402         pItem = null;
403
404         return true;
405 }
406
407 bool
408 ManageApplicationForm::DeleteItem(int groupIndex, int itemIndex, TableViewItem* pItem)
409 {
410         AppLogDebug("ENTER");
411
412         delete pItem;
413         pItem = null;
414
415         return true;
416 }
417
418 void
419 ManageApplicationForm::OnGroupedTableViewItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
420 {
421         AppLogDebug("group[%d] index[%d]", groupIndex, itemIndex);
422
423         SceneManager* pSceneManager = SceneManager::GetInstance();
424         AppAssert(pSceneManager);
425
426         ArrayList* pData = new (std::nothrow) ArrayList(SingleObjectDeleter);
427         pData->Construct();
428
429         String applicationName(GetPackageInfoItem(itemIndex, DATA_TYPE_APPLICATION_NAME));
430         String applicationIconPath(GetPackageInfoItem(itemIndex, DATA_TYPE_APPLICATION_ICON_PATH));
431         String applicationUninstallAble(GetPackageInfoItem(itemIndex, DATA_TYPE_APPLICATION_UNINSTALLABLE));
432         String applicationSize(GetPackageInfoItem(itemIndex, DATA_TYPE_APPLICATION_SIZE));
433         String applicationDataSize(GetPackageInfoItem(itemIndex, DATA_TYPE_APPLICATION_DATA_SIZE));
434         String applicationId(GetPackageInfoItem(itemIndex, DATA_TYPE_APPLICATION_ID));
435         String applicationPackageId(GetPackageInfoItem(itemIndex, DATA_TYPE_APPLICATION_PACKAGE_ID));
436
437         pData->Add(new (std::nothrow) String(applicationName));
438         pData->Add(new (std::nothrow) String(applicationIconPath));
439         pData->Add(new (std::nothrow) String(applicationUninstallAble));
440         pData->Add(new (std::nothrow) String(applicationSize));
441         pData->Add(new (std::nothrow) String(applicationDataSize));
442         pData->Add(new (std::nothrow) String(applicationId));
443         pData->Add(new (std::nothrow) String(applicationPackageId));
444
445         pSceneManager->GoForward(ForwardSceneTransition(IDSCN_MANAGE_APPLICATION_INFO, SCENE_TRANSITION_ANIMATION_TYPE_LEFT), pData);
446 }
447
448 int
449 ManageApplicationForm::GetDefaultGroupItemHeight(void)
450 {
451         return H_GROUP_INDEX_NO_TITLE_DEFAULT;
452 }
453
454 int
455 ManageApplicationForm::GetDefaultItemHeight(void)
456 {
457         return H_GROUP_ITEM_DEFAULT;
458 }
459
460 void
461 ManageApplicationForm::UpdateGroupItem(int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem)
462 {
463 }
464
465 void
466 ManageApplicationForm::UpdateItem(int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
467 {
468 }
469
470 void
471 ManageApplicationForm::OnGroupedTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem* pContextItem, bool activated)
472 {
473 }
474
475 void
476 ManageApplicationForm::OnGroupedTableViewGroupItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
477 {
478 }
479
480 void
481 ManageApplicationForm::OnOrientationChanged(const Tizen::Ui::Control& source, Tizen::Ui::OrientationStatus orientationStatus)
482 {
483         Invalidate(true);
484 }
485
486 void
487 ManageApplicationForm::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs)
488 {
489         if (requestId == REFRESH_REQUEST_EVENT)
490         {
491                 if (__pTableView)
492                 {
493                         ReLoadPackageInfoList();
494                         __pTableView->UpdateTableView();
495                 }
496                 if (pArgs)
497                 {
498                         pArgs->RemoveAll(true);
499                         delete pArgs;
500                 }
501         }
502 }
503
504 void
505 ManageApplicationForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
506 {
507         switch (actionId)
508         {
509         case IDA_UNINSTALL_OK:
510                 {
511                         HideAlertPopup();
512                         this->SetFocus();
513                 }
514                 break;
515
516         default:
517                 break;
518         }
519 }
520
521 result
522 ManageApplicationForm::CreateAlertPopup(Tizen::Base::String* appName, bool uninstallResult)
523 {
524         String buttonText;
525         Label* pLabel = null;
526         Button* pOkButton = null;
527
528         Rectangle clientRect = GetClientAreaBounds();
529         Rectangle itemRectLabel;
530         Rectangle itemRectLhsButton;
531         Rectangle itemRectRhsButton;
532
533         clientRect.x = 0;
534         clientRect.y = Y_ALERT_POPUP_CLIENT_RECT_DEFAULT;
535         clientRect.width = W_ALERT_POPUP_CLIENT_RECT_DEFAULT;
536         clientRect.height = H_ALERT_POPUP_CLIENT_RECT_DEFAULT;
537
538         itemRectLabel.x = X_ALERT_POPUP_ITEM_RECT_LABEL_DEFAULT;
539         itemRectLabel.y = Y_ALERT_POPUP_ITEM_RECT_LABEL_DEFAULT;
540         itemRectLabel.width = W_ALERT_POPUP_ITEM_RECT_LABEL_DEFAULT;
541         itemRectLabel.height = H_ALERT_POPUP_ITEM_RECT_LABEL_DEFAULT;
542
543         itemRectLhsButton.x = X_ALERT_POPUP_ITEM_RECT_LHS_BUTTON_DEFAULT + itemRectLabel.x;
544         itemRectLhsButton.y = Y_ALERT_POPUP_ITEM_RECT_LHS_BUTTON_DEFAULT;
545         itemRectLhsButton.width = W_ALERT_POPUP_ITEM_RECT_LHS_BUTTON_DEFAULT;
546         itemRectLhsButton.height = H_ALERT_POPUP_ITEM_RECT_LHS_BUTTON_DEFAULT;
547
548         Dimension bounds = Dimension(clientRect.width, clientRect.height);
549
550         __uninstallResultPopup = new (std::nothrow) Popup();
551         __uninstallResultPopup->Construct(true, bounds);
552         __uninstallResultPopup->SetTitleText(*appName);
553
554         String descriptionText = L"";
555         if (uninstallResult)
556         {
557                 descriptionText = ResourceManager::GetString(L"IDS_ST_POP_UNINSTALLED");
558         }
559         else
560         {
561                 descriptionText = ResourceManager::GetString(L"IDS_COM_POP_FAILED");
562         }
563
564         pLabel = new (std::nothrow) Label();
565         pLabel->Construct(itemRectLabel, descriptionText);
566         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
567         pLabel->SetTextVerticalAlignment(ALIGNMENT_MIDDLE);
568         pLabel->SetTextConfig(ALERT_POPUP_LABEL_FONT_SIZE, LABEL_TEXT_STYLE_BOLD);
569
570         __uninstallResultPopup->AddControl(pLabel);
571
572         buttonText = ResourceManager::GetString(L"IDS_ST_BUTTON_OK");
573         pOkButton = new (std::nothrow) Button();
574         pOkButton->Construct(itemRectLhsButton, buttonText);
575         pOkButton->SetActionId(IDA_UNINSTALL_OK);
576
577         __uninstallResultPopup->AddControl(pOkButton);
578
579         pOkButton->AddActionEventListener(*this);
580
581         return E_SUCCESS;
582 }
583
584 void
585 ManageApplicationForm::ShowAlertPopup(void)
586 {
587         __uninstallResultPopup->SetShowState(true);
588         __uninstallResultPopup->Show();
589 }
590
591 void
592 ManageApplicationForm::HideAlertPopup(void)
593 {
594         __uninstallResultPopup->SetShowState(false);
595         __uninstallResultPopup->Invalidate(true);
596 }
597
598 void
599 ManageApplicationForm::DeleteAlertPopup(void)
600 {
601         if (__uninstallResultPopup)
602         {
603                 delete __uninstallResultPopup;
604                 __uninstallResultPopup = null;
605         }
606 }