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