Fixed N_SE-38563, N_SE-38552
[apps/osp/Settings.git] / src / StMemoryStatusForm.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                StMemoryStatusForm.cpp
19  * @brief               This is the implementation file for MemoryStatusForm class.
20  */
21
22 #include "StMemoryStatusForm.h"
23 #include "StResourceManager.h"
24 #include "StTypes.h"
25
26 using namespace Tizen::App;
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Collection;
29 using namespace Tizen::Graphics;
30 using namespace Tizen::Io;
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 int ID_GROUP_MEMORY_STATUS_MAIN = 0;
37
38 static const int ID_GROUP_COUNT = 1;
39
40 static const int GROUP_TOTAL_ITEM_COUNT_DEFAULT = 1;
41 static const int GROUP_TOTAL_ITEM_COUNT_MAX = 2;
42
43 static const int AVILABLE_SPACE_BYTE_CHANGED = 1024;
44 static const int AVILABLE_SPACE_STRING_LIMIT = 10;
45
46 enum SdCardTranslateTabelGroupIndex
47 {
48         SD_CARD_TRANSLATE_TABLE_GROUP_INDEX_BASE,
49         SD_CARD_TRANSLATE_TABLE_GROUP_INDEX_0 = SD_CARD_TRANSLATE_TABLE_GROUP_INDEX_BASE,
50         SD_CARD_TRANSLATE_TABLE_GROUP_INDEX_1,
51         SD_CARD_TRANSLATE_TABLE_GROUP_INDEX_2,
52         SD_CARD_TRANSLATE_TABLE_GROUP_INDEX_MAX
53 };
54
55 enum SdCardGroupItem
56 {
57         SD_CARD_GROUP_ITEM_BASE,
58         SD_CARD_GROUP_ITEM_MOUNT = SD_CARD_GROUP_ITEM_BASE,
59         SD_CARD_GROUP_ITEM_UNMOUNT,
60         SD_CARD_GORUP_ITEM_AVAILABLE_SPACE,
61         SD_CARD_GORUP_ITEM_MAX
62 };
63
64 MemoryStatusForm::MemoryStatusForm(void)
65         : __pMmcStorageManagerInstance(null)
66         , __sdGroupTotalItemCount(0)
67 {
68 }
69
70 MemoryStatusForm::~MemoryStatusForm(void)
71 {
72 }
73
74 void
75 MemoryStatusForm::CreateFooter(void)
76 {
77         Footer* pFooter = GetFooter();
78         AppAssert(pFooter);
79
80         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
81         pFooter->SetBackButton();
82         pFooter->AddActionEventListener(*this);
83
84         SetFormBackEventListener(this);
85 }
86
87 result
88 MemoryStatusForm::OnInitializing(void)
89 {
90         CreateHeader(ResourceManager::GetString(L"IDS_ST_BODY_MEMORY_STATUS"));
91         CreateFooter();
92         CreateTableView();
93
94         SetDeviceManagerEventListner();
95         RefreshSdGroupItemIndex();
96
97         return InitMmcStorageManagerInstance();
98 }
99
100 result
101 MemoryStatusForm::OnTerminating(void)
102 {
103         RemoveDeviceManagerEventListner();
104         __pMmcStorageManagerInstance->RemoveMmcStorageMountListener(*this);
105         __pMmcStorageManagerInstance = null;
106
107         __pTableView = null;
108
109         SetFormBackEventListener(null);
110         return E_SUCCESS;
111 }
112
113 void
114 MemoryStatusForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
115 {
116         RefreshSdGroupItemIndex();
117         __pTableView->UpdateTableView();
118 }
119
120 void
121 MemoryStatusForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
122 {
123 }
124
125 void
126 MemoryStatusForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
127 {
128         SceneManager* pSceneManager = SceneManager::GetInstance();
129         AppAssert(pSceneManager);
130
131         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
132 }
133
134 int
135 MemoryStatusForm::GetGroupCount(void)
136 {
137         return ID_GROUP_COUNT;
138 }
139
140 int
141 MemoryStatusForm::GetItemCount(int groupIndex)
142 {
143         int itemCount = 0;
144
145         switch (groupIndex)
146         {
147         case ID_GROUP_MEMORY_STATUS_MAIN:
148                 {
149                         itemCount = __sdGroupTotalItemCount;
150                 }
151                 break;
152
153         default:
154                 break;
155         }
156
157         return itemCount;
158 }
159
160 TableViewGroupItem*
161 MemoryStatusForm::CreateGroupItem(int groupIndex, int itemWidth)
162 {
163         AppLogDebug("ENTER");
164
165         int itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
166         int yItemOffset = H_GROUP_INDEX_HELP_TEXT_TOP_GAP;
167         LabelTextStyle style = LABEL_TEXT_STYLE_NORMAL;
168         Rectangle itemMainRectangle;
169         String groupText;
170         Label* pLabel = null;
171
172         if (groupIndex == ID_GROUP_MEMORY_STATUS_MAIN)
173         {
174                 groupText = ResourceManager::GetString(L"IDS_ST_BODY_SD_CARD");
175         }
176
177         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
178
179         yItemOffset = H_GROUP_INDEX_NO_HELP_TEXT_GAP;
180         itemHeight = H_GROUP_INDEX_DEFAULT;
181
182         itemMainRectangle.x = X_GROUP_ITEM_DEFAULT_LABEL;
183         itemMainRectangle.y = yItemOffset;
184         itemMainRectangle.width = itemWidth;
185         itemMainRectangle.height = itemHeight;
186
187         RelativeLayout relativeLayout;
188         relativeLayout.Construct();
189
190         pItem->Construct(relativeLayout, Dimension(itemWidth, itemHeight));
191         pItem->SetBackgroundColor(COLOR_BG_GROUP_INDEX_DEFAULT);
192
193         pLabel = new (std::nothrow) Label();
194         pLabel->Construct(itemMainRectangle, groupText);
195         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
196         pLabel->SetTextVerticalAlignment(ALIGNMENT_TOP);
197         pLabel->SetTextConfig(FONT_SIZE_GROUP_TITLE_TEXT, style);
198         pLabel->SetTextColor(COLOR_HELP_TEXT_TYPE_01);
199
200         pItem->AddControl(pLabel);
201         relativeLayout.SetMargin(*pLabel, itemMainRectangle.x, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
202         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
203         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
204         pItem->SetEnabled(false);
205
206         return pItem;
207 }
208
209 TableViewItem*
210 MemoryStatusForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
211 {
212         AppLogDebug("group[%d] index[%d]", groupIndex, itemIndex);
213
214         bool isItemSubText = false;
215         Rectangle itemMainRectangle;
216         Rectangle itemSubRectangle;
217         TableViewAnnexStyle style = TABLE_VIEW_ANNEX_STYLE_NORMAL;
218         String itemMainText;
219         String itemSubText;
220         String fontReturnValue;
221         int fontSize = GetFontSize();
222         int itemHeight = 0;
223
224         switch (groupIndex)
225         {
226         case ID_GROUP_MEMORY_STATUS_MAIN:
227                 {
228                         switch (__sdGroupItemTranslateTable[itemIndex])
229                         {
230                         case SD_CARD_GROUP_ITEM_MOUNT:
231                                 {
232                                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_MOUNT_SD_CARD");
233                                         itemSubText = L"Insert SD card";
234                                         ItemTypeTwoLine(itemMainRectangle, itemSubRectangle, fontSize);
235                                         itemHeight = itemMainRectangle.height + itemSubRectangle.height;
236                                         isItemSubText = true;
237                                 }
238                                 break;
239
240                         case SD_CARD_GROUP_ITEM_UNMOUNT:
241                                 {
242                                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_UNMOUNT_SD_CARD");;
243                                         ItemTypeOneLine(itemMainRectangle);
244                                         itemHeight = itemMainRectangle.height;
245                                 }
246                                 break;
247
248                         case SD_CARD_GORUP_ITEM_AVAILABLE_SPACE:
249                                 {
250                                         long long availableSpace = 0;
251                                         RuntimeInfo::GetValue(L"http://tizen.org/runtime/storage.available.external", availableSpace);
252                                         if (IsFailed(GetLastResult()))
253                                         {
254                                                 break;
255                                         }
256                                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_AVAILABLE_SPACE");
257                                         float megabyteAvailableSpace = static_cast<float>(availableSpace) / (AVILABLE_SPACE_BYTE_CHANGED * AVILABLE_SPACE_BYTE_CHANGED);
258                                         if (megabyteAvailableSpace > AVILABLE_SPACE_BYTE_CHANGED)
259                                         {
260                                                 float gigabyteAvailableSpace = megabyteAvailableSpace / AVILABLE_SPACE_BYTE_CHANGED;
261                                                 itemSubText.Format(AVILABLE_SPACE_STRING_LIMIT, L"%.1f", gigabyteAvailableSpace);
262                                                 itemSubText.Append(L" GB");
263                                         }
264                                         else
265                                         {
266                                                 itemSubText.Format(AVILABLE_SPACE_STRING_LIMIT, L"%.0f", megabyteAvailableSpace);
267                                                 itemSubText.Append(L" MB");
268                                         }
269                                         ItemTypeTwoLine(itemMainRectangle, itemSubRectangle, fontSize);
270                                         itemHeight = itemMainRectangle.height + itemSubRectangle.height;
271                                         isItemSubText = true;
272                                 }
273                                 break;
274
275                         default:
276                                 break;
277                         }
278                 }
279                 break;
280
281         default:
282                 break;
283         }
284
285         TableViewItem* pItem = new (std::nothrow) TableViewItem();
286         RelativeLayout relativeLayout;
287         relativeLayout.Construct();
288
289         result r = pItem->Construct(relativeLayout, Dimension(itemWidth, itemHeight), style);
290         if (IsFailed(r))
291         {
292                 AppLogDebug("Construct fail [%s]", GetErrorMessage(r));
293                 delete pItem;
294                 return null;
295         }
296         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
297
298         Label* pLabel = new (std::nothrow) Label();
299         if (pLabel == null)
300         {
301                 AppLogDebug("pLabel is null");
302                 return pItem;
303         }
304         r = pLabel->Construct(itemMainRectangle, itemMainText);
305         if (IsFailed(r))
306         {
307                 AppLogDebug("Construct fail [%s]", GetErrorMessage(r));
308                 delete pLabel;
309                 return pItem;
310         }
311         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
312         pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
313         pLabel->SetTextColor(COLOR_MAIN_TEXT);
314
315         pItem->AddControl(pLabel);
316         relativeLayout.SetMargin(*pLabel, itemMainRectangle.x, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
317         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
318         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
319
320         if (isItemSubText == true)
321         {
322                 Label* pSecondLabel = new (std::nothrow) Label();
323                 if (pSecondLabel == null)
324                 {
325                         AppLogDebug("pLabel is null");
326                         return pItem;
327                 }
328                 r = pSecondLabel->Construct(itemSubRectangle, itemSubText);
329                 if (IsFailed(r))
330                 {
331                         AppLogDebug("Construct fail [%s]", GetErrorMessage(r));
332                         delete pSecondLabel;
333                         return pItem;
334                 }
335
336                 pSecondLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
337                 pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
338         
339                 pSecondLabel->SetTextColor(COLOR_SUB_TEXT);
340
341                 pItem->AddControl(pSecondLabel);
342                 relativeLayout.SetMargin(*pSecondLabel, itemSubRectangle.x, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
343                 relativeLayout.SetRelation(*pSecondLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
344                 relativeLayout.SetRelation(*pSecondLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
345         }
346
347         return pItem;
348 }
349
350 bool
351 MemoryStatusForm::DeleteGroupItem(int groupIndex, TableViewGroupItem* pItem)
352 {
353         AppLogDebug("ENTER");
354
355         delete pItem;
356         pItem = null;
357
358         return true;
359 }
360
361 bool
362 MemoryStatusForm::DeleteItem(int groupIndex, int itemIndex, TableViewItem* pItem)
363 {
364         AppLogDebug("ENTER");
365
366         delete pItem;
367         pItem = null;
368
369         return true;
370 }
371
372 void
373 MemoryStatusForm::OnGroupedTableViewItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
374 {
375         if ( __pMmcStorageManagerInstance == null)
376         {
377                 AppLogDebug("__pMmcStorageManagerInstance is null");
378                 return;
379         }
380
381         switch (groupIndex)
382         {
383         case ID_GROUP_MEMORY_STATUS_MAIN:
384                 {
385                         switch (__sdGroupItemTranslateTable[itemIndex])
386                         {
387                         case SD_CARD_GROUP_ITEM_MOUNT:
388                                 {
389                                         result r = __pMmcStorageManagerInstance->Mount();
390                                         if (IsFailed(r))
391                                         {
392                                                 AppLogDebug("Mount failed(%s)", GetErrorMessage(r));
393                                         }
394                                 }
395                                 break;
396
397                         case SD_CARD_GROUP_ITEM_UNMOUNT:
398                                 {
399                                         result r = __pMmcStorageManagerInstance->Unmount();
400                                         if (IsFailed(r))
401                                         {
402                                                 AppLogDebug("Unmount failed(%s)", GetErrorMessage(r));
403                                         }
404                                 }
405                                 break;
406
407                         default:
408                                 break;
409                         }
410                 }
411                 break;
412
413         default:
414                 break;
415         }
416 }
417
418 int
419 MemoryStatusForm::GetDefaultGroupItemHeight(void)
420 {
421         return H_GROUP_INDEX_NO_TITLE_DEFAULT;
422 }
423
424 int
425 MemoryStatusForm::GetDefaultItemHeight(void)
426 {
427         return H_GROUP_ITEM_DEFAULT;
428 }
429
430 void
431 MemoryStatusForm::UpdateGroupItem(int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem)
432 {
433 }
434
435 void
436 MemoryStatusForm::UpdateItem(int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
437 {
438 }
439
440 void
441 MemoryStatusForm::OnGroupedTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem* pContextItem, bool activated)
442 {
443 }
444
445 void
446 MemoryStatusForm::OnGroupedTableViewGroupItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
447 {
448 }
449
450 void
451 MemoryStatusForm::OnMmcStorageMounted(result r)
452 {
453         if (!IsFailed(r))
454         {
455                 RefreshSdGroupItemIndex();
456                 __pTableView->UpdateTableView();
457         }
458         AppLogDebug("Exit(%s)", GetErrorMessage(r));
459 }
460
461 void
462 MemoryStatusForm::OnMmcStorageUnmounted(result r)
463 {
464         if (!IsFailed(r))
465         {
466                 RefreshSdGroupItemIndex();
467                 __pTableView->UpdateTableView();
468         }
469         AppLogDebug("Exit(%s)", GetErrorMessage(r));
470 }
471
472 void
473 MemoryStatusForm::OnMmcStorageFormatted(result r)
474 {
475 }
476
477 result
478 MemoryStatusForm::InitMmcStorageManagerInstance(void)
479 {
480         __pMmcStorageManagerInstance = MmcStorageManager::GetInstance();
481         if (__pMmcStorageManagerInstance == null)
482         {
483                 return E_FAILURE;
484         }
485         return __pMmcStorageManagerInstance->AddMmcStorageMountListener(*this);
486 }
487
488 void
489 MemoryStatusForm::SetDeviceManagerEventListner(void)
490 {
491         DeviceManager::AddDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
492 }
493
494 void
495 MemoryStatusForm::RemoveDeviceManagerEventListner(void)
496 {
497         DeviceManager::RemoveAllDeviceEventListeners();
498 }
499
500 void
501 MemoryStatusForm::RefreshSdGroupItemIndex(void)
502 {
503         __sdGroupTotalItemCount = GROUP_TOTAL_ITEM_COUNT_DEFAULT;
504         __sdGroupItemTranslateTable[SD_CARD_TRANSLATE_TABLE_GROUP_INDEX_0] = SD_CARD_GROUP_ITEM_MOUNT;
505         if (IsSdMounted() == true)
506         {
507                 __sdGroupTotalItemCount = GROUP_TOTAL_ITEM_COUNT_MAX;
508                 __sdGroupItemTranslateTable[SD_CARD_TRANSLATE_TABLE_GROUP_INDEX_0] = SD_CARD_GORUP_ITEM_AVAILABLE_SPACE;
509                 __sdGroupItemTranslateTable[SD_CARD_TRANSLATE_TABLE_GROUP_INDEX_1] = SD_CARD_GROUP_ITEM_UNMOUNT;
510         }
511 }
512
513 bool
514 MemoryStatusForm::IsSdMounted(void)
515 {
516         String sdCardState;
517         DeviceManager::GetState(DEVICE_TYPE_STORAGE_CARD, sdCardState);
518         if (sdCardState.Equals(L"Mounted", false))
519         {
520                 return true;
521         }
522         return false;
523 }