Applied latest source code
[apps/native/preloaded/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         , __pLabel(null)
68 {
69 }
70
71 MemoryStatusForm::~MemoryStatusForm(void)
72 {
73 }
74
75 void
76 MemoryStatusForm::CreateFooter(void)
77 {
78         Footer* pFooter = GetFooter();
79         AppAssert(pFooter);
80
81         pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
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_STORAGE"));
91         CreateTableView();
92
93         SetDeviceManagerEventListner();
94         RefreshSdGroupItemIndex();
95
96         return E_SUCCESS;
97 }
98
99 result
100 MemoryStatusForm::OnTerminating(void)
101 {
102         RemoveDeviceManagerEventListner();
103         if (__pMmcStorageManagerInstance)
104         {
105                 __pMmcStorageManagerInstance->RemoveMmcStorageMountListener(*this);
106                 __pMmcStorageManagerInstance = null;
107         }
108
109         __pTableView = null;
110
111         SetFormBackEventListener(null);
112         return E_SUCCESS;
113 }
114
115 void
116 MemoryStatusForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId, const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
117 {
118         RefreshSdGroupItemIndex();
119         __pTableView->UpdateTableView();
120 }
121
122 void
123 MemoryStatusForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId, const Tizen::Ui::Scenes::SceneId& nextSceneId)
124 {
125 }
126
127 void
128 MemoryStatusForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
129 {
130         SceneManager* pSceneManager = SceneManager::GetInstance();
131         AppAssert(pSceneManager);
132
133         pSceneManager->GoBackward(BackwardSceneTransition(SCENE_TRANSITION_ANIMATION_TYPE_RIGHT), null);
134 }
135
136 int
137 MemoryStatusForm::GetGroupCount(void)
138 {
139         return ID_GROUP_COUNT;
140 }
141
142 int
143 MemoryStatusForm::GetItemCount(int groupIndex)
144 {
145         int itemCount = 0;
146
147         switch (groupIndex)
148         {
149         case ID_GROUP_MEMORY_STATUS_MAIN:
150                 {
151                         itemCount = __sdGroupTotalItemCount;
152                 }
153                 break;
154
155         default:
156                 break;
157         }
158
159         return itemCount;
160 }
161
162 TableViewGroupItem*
163 MemoryStatusForm::CreateGroupItem(int groupIndex, int itemWidth)
164 {
165         AppLogDebug("ENTER");
166
167         int itemHeight = H_GROUP_INDEX_NO_TITLE_DEFAULT;
168         int yItemOffset = H_GROUP_INDEX_HELP_TEXT_TOP_GAP;
169         LabelTextStyle style = LABEL_TEXT_STYLE_NORMAL;
170         Rectangle itemMainRectangle;
171         String groupText;
172         Label* pLabel = null;
173
174         if (groupIndex == ID_GROUP_MEMORY_STATUS_MAIN)
175         {
176                 groupText = ResourceManager::GetString(L"IDS_ST_BODY_SD_CARD");
177         }
178
179         TableViewGroupItem* pItem = new (std::nothrow) TableViewGroupItem();
180
181         yItemOffset = H_GROUP_INDEX_NO_HELP_TEXT_GAP;
182         itemHeight = H_GROUP_INDEX_DEFAULT;
183
184         itemMainRectangle.x = X_GROUP_DEFAULT;
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(FONT_SIZE_GROUP_TITLE_TEXT, 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 MemoryStatusForm::CreateItem(int groupIndex, int itemIndex, int itemWidth)
213 {
214         AppLogDebug("group[%d] index[%d]", groupIndex, itemIndex);
215
216         bool isItemSubText = false;
217         Rectangle itemMainRectangle;
218         Rectangle itemSubRectangle;
219         TableViewAnnexStyle style = TABLE_VIEW_ANNEX_STYLE_NORMAL;
220         String itemMainText;
221         String itemSubText;
222         String fontReturnValue;
223         int fontSize = GetFontSize();
224         int itemHeight = 0;
225
226         switch (groupIndex)
227         {
228         case ID_GROUP_MEMORY_STATUS_MAIN:
229                 {
230                         switch (__sdGroupItemTranslateTable[itemIndex])
231                         {
232                         case SD_CARD_GROUP_ITEM_MOUNT:
233                                 {
234                                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_MOUNT_SD_CARD");
235                                         itemSubText = L"Insert SD card";
236                                         ItemTypeTwoLine(itemMainRectangle, itemSubRectangle, fontSize);
237                                         itemHeight = itemMainRectangle.height + itemSubRectangle.height;
238                                         isItemSubText = true;
239                                 }
240                                 break;
241
242                         case SD_CARD_GROUP_ITEM_UNMOUNT:
243                                 {
244                                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_UNMOUNT_SD_CARD");;
245                                         ItemTypeOneLine(itemMainRectangle);
246                                         itemHeight = itemMainRectangle.height;
247                                 }
248                                 break;
249
250                         case SD_CARD_GORUP_ITEM_AVAILABLE_SPACE:
251                                 {
252                                         long long availableSpace = 0;
253                                         RuntimeInfo::GetValue(L"http://tizen.org/runtime/storage.available.external", availableSpace);
254                                         if (IsFailed(GetLastResult()))
255                                         {
256                                                 break;
257                                         }
258                                         itemMainText = ResourceManager::GetString(L"IDS_ST_BODY_AVAILABLE_SPACE");
259                                         float megabyteAvailableSpace = static_cast<float>(availableSpace) / (AVILABLE_SPACE_BYTE_CHANGED * AVILABLE_SPACE_BYTE_CHANGED);
260                                         if (megabyteAvailableSpace > AVILABLE_SPACE_BYTE_CHANGED)
261                                         {
262                                                 float gigabyteAvailableSpace = megabyteAvailableSpace / AVILABLE_SPACE_BYTE_CHANGED;
263                                                 itemSubText.Format(AVILABLE_SPACE_STRING_LIMIT, L"%.1f", gigabyteAvailableSpace);
264                                                 itemSubText.Append(L" GB");
265                                         }
266                                         else
267                                         {
268                                                 itemSubText.Format(AVILABLE_SPACE_STRING_LIMIT, L"%.0f", megabyteAvailableSpace);
269                                                 itemSubText.Append(L" MB");
270                                         }
271                                         ItemTypeTwoLine(itemMainRectangle, itemSubRectangle, fontSize);
272                                         itemHeight = itemMainRectangle.height + itemSubRectangle.height;
273                                         isItemSubText = true;
274                                 }
275                                 break;
276
277                         default:
278                                 break;
279                         }
280                 }
281                 break;
282
283         default:
284                 break;
285         }
286
287         TableViewItem* pItem = new (std::nothrow) TableViewItem();
288         RelativeLayout relativeLayout;
289         relativeLayout.Construct();
290
291         result r = pItem->Construct(relativeLayout, Dimension(itemWidth, itemHeight), style);
292         if (IsFailed(r))
293         {
294                 AppLogDebug("Construct fail [%s]", GetErrorMessage(r));
295                 delete pItem;
296                 return null;
297         }
298         pItem->SetBackgroundColor(COLOR_BG_GROUP_ITEM_DEFAULT);
299
300         Label* pLabel = new (std::nothrow) Label();
301         if (pLabel == null)
302         {
303                 AppLogDebug("pLabel is null");
304                 return pItem;
305         }
306         r = pLabel->Construct(itemMainRectangle, itemMainText);
307         if (IsFailed(r))
308         {
309                 AppLogDebug("Construct fail [%s]", GetErrorMessage(r));
310                 delete pLabel;
311                 return pItem;
312         }
313         pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
314         pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
315         pLabel->SetTextColor(COLOR_MAIN_TEXT);
316         if (groupIndex == ID_GROUP_MEMORY_STATUS_MAIN && itemIndex == SD_CARD_GROUP_ITEM_BASE)
317         {
318                 pLabel->AddTouchEventListener(*this);
319         }
320         pItem->AddControl(pLabel);
321         relativeLayout.SetMargin(*pLabel, itemMainRectangle.x, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
322         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
323         relativeLayout.SetRelation(*pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
324
325         if (isItemSubText == true)
326         {
327                 __pLabel = new (std::nothrow) Label();
328                 if (__pLabel == null)
329                 {
330                         AppLogDebug("pLabel is null");
331                         return pItem;
332                 }
333                 r = __pLabel->Construct(itemSubRectangle, itemSubText);
334                 if (IsFailed(r))
335                 {
336                         AppLogDebug("Construct fail [%s]", GetErrorMessage(r));
337                         delete __pLabel;
338                         return pItem;
339                 }
340
341                 __pLabel->SetTextHorizontalAlignment(ALIGNMENT_LEFT);
342                 pLabel->SetTextConfig(fontSize, LABEL_TEXT_STYLE_NORMAL);
343                 __pLabel->AddTouchEventListener(*this);
344                 __pLabel->SetTextColor(COLOR_SUB_TEXT);
345
346                 pItem->AddControl(__pLabel);
347                 pItem->AddKeyEventListener(*this);
348                 pItem->AddFocusEventListener(*this);
349                 relativeLayout.SetMargin(*__pLabel, itemSubRectangle.x, RELATIVE_LAYOUT_RIGHT_MARGIN, 0, 0);
350                 relativeLayout.SetRelation(*__pLabel, pItem, RECT_EDGE_RELATION_LEFT_TO_LEFT);
351                 relativeLayout.SetRelation(*__pLabel, pItem, RECT_EDGE_RELATION_RIGHT_TO_RIGHT);
352         }
353
354         return pItem;
355 }
356
357 bool
358 MemoryStatusForm::DeleteGroupItem(int groupIndex, TableViewGroupItem* pItem)
359 {
360         AppLogDebug("ENTER");
361
362         delete pItem;
363         pItem = null;
364
365         return true;
366 }
367
368 bool
369 MemoryStatusForm::DeleteItem(int groupIndex, int itemIndex, TableViewItem* pItem)
370 {
371         AppLogDebug("ENTER");
372
373         delete pItem;
374         pItem = null;
375
376         return true;
377 }
378
379 void
380 MemoryStatusForm::OnGroupedTableViewItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
381 {
382         switch (groupIndex)
383         {
384         case ID_GROUP_MEMORY_STATUS_MAIN:
385                 {
386                         switch (__sdGroupItemTranslateTable[itemIndex])
387                         {
388                         case SD_CARD_GROUP_ITEM_MOUNT:
389                                 {
390                                         if (__pMmcStorageManagerInstance == null)
391                                         {
392                                                 InitMmcStorageManagerInstance();
393                                         }
394
395                                         result r = __pMmcStorageManagerInstance->Mount();
396                                         if (IsFailed(r))
397                                         {
398                                                 AppLogDebug("Mount failed(%s)", GetErrorMessage(r));
399                                         }
400                                 }
401                                 break;
402
403                         case SD_CARD_GROUP_ITEM_UNMOUNT:
404                                 {
405                                         if (__pMmcStorageManagerInstance == null)
406                                         {
407                                                 InitMmcStorageManagerInstance();
408                                         }
409
410                                         result r = __pMmcStorageManagerInstance->Unmount();
411                                         if (IsFailed(r))
412                                         {
413                                                 AppLogDebug("Unmount failed(%s)", GetErrorMessage(r));
414                                         }
415                                 }
416                                 break;
417
418                         default:
419                                 break;
420                         }
421                 }
422                 break;
423
424         default:
425                 break;
426         }
427 }
428
429 int
430 MemoryStatusForm::GetDefaultGroupItemHeight(void)
431 {
432         return H_GROUP_INDEX_NO_TITLE_DEFAULT;
433 }
434
435 int
436 MemoryStatusForm::GetDefaultItemHeight(void)
437 {
438         return H_GROUP_ITEM_DEFAULT;
439 }
440
441 void
442 MemoryStatusForm::UpdateGroupItem(int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem)
443 {
444 }
445
446 void
447 MemoryStatusForm::UpdateItem(int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewItem* pItem)
448 {
449 }
450
451 void
452 MemoryStatusForm::OnGroupedTableViewContextItemActivationStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, int itemIndex, Tizen::Ui::Controls::TableViewContextItem* pContextItem, bool activated)
453 {
454 }
455
456 void
457 MemoryStatusForm::OnGroupedTableViewGroupItemStateChanged(Tizen::Ui::Controls::GroupedTableView& tableView, int groupIndex, Tizen::Ui::Controls::TableViewGroupItem* pItem, Tizen::Ui::Controls::TableViewItemStatus status)
458 {
459 }
460
461 void
462 MemoryStatusForm::OnMmcStorageMounted(result r)
463 {
464         AppLog("Enter");
465         if (!IsFailed(r))
466         {
467                 RefreshSdGroupItemIndex();
468                 __pTableView->UpdateTableView();
469         }
470         AppLogDebug("Exit(%s)", GetErrorMessage(r));
471 }
472
473 void
474 MemoryStatusForm::OnMmcStorageUnmounted(result r)
475 {
476         if (!IsFailed(r))
477         {
478                 RefreshSdGroupItemIndex();
479                 __pTableView->UpdateTableView();
480         }
481         AppLogDebug("Exit(%s)", GetErrorMessage(r));
482 }
483
484 void
485 MemoryStatusForm::OnMmcStorageFormatted(result r)
486 {
487 }
488
489 result
490 MemoryStatusForm::InitMmcStorageManagerInstance(void)
491 {
492         __pMmcStorageManagerInstance = MmcStorageManager::GetInstance();
493         if (__pMmcStorageManagerInstance == null)
494         {
495                 return E_FAILURE;
496         }
497         return __pMmcStorageManagerInstance->AddMmcStorageMountListener(*this);
498 }
499
500 void
501 MemoryStatusForm::SetDeviceManagerEventListner(void)
502 {
503         DeviceManager::AddDeviceEventListener(DEVICE_TYPE_STORAGE_CARD, *this);
504 }
505
506 void
507 MemoryStatusForm::RemoveDeviceManagerEventListner(void)
508 {
509         DeviceManager::RemoveAllDeviceEventListeners();
510 }
511
512 void
513 MemoryStatusForm::RefreshSdGroupItemIndex(void)
514 {
515         __sdGroupTotalItemCount = GROUP_TOTAL_ITEM_COUNT_DEFAULT;
516         __sdGroupItemTranslateTable[SD_CARD_TRANSLATE_TABLE_GROUP_INDEX_0] = SD_CARD_GROUP_ITEM_MOUNT;
517         if (IsSdMounted() == true)
518         {
519                 __sdGroupTotalItemCount = GROUP_TOTAL_ITEM_COUNT_MAX;
520                 __sdGroupItemTranslateTable[SD_CARD_TRANSLATE_TABLE_GROUP_INDEX_0] = SD_CARD_GORUP_ITEM_AVAILABLE_SPACE;
521                 __sdGroupItemTranslateTable[SD_CARD_TRANSLATE_TABLE_GROUP_INDEX_1] = SD_CARD_GROUP_ITEM_UNMOUNT;
522         }
523 }
524
525 bool
526 MemoryStatusForm::IsSdMounted(void)
527 {
528         String sdCardState;
529         DeviceManager::GetState(DEVICE_TYPE_STORAGE_CARD, sdCardState);
530         if (sdCardState.Equals(L"Mounted", false))
531         {
532                 return true;
533         }
534         return false;
535 }
536
537 void
538 MemoryStatusForm::OnDeviceStateChanged(Tizen::System::DeviceType deviceType, const Tizen::Base::String& state)
539 {
540         AppLog("Enter");
541         if (deviceType == DEVICE_TYPE_STORAGE_CARD)
542         {
543                 RefreshSdGroupItemIndex();
544                 __pTableView->UpdateTableView();
545         }
546         AppLog("Exit");
547 }
548
549 void
550 MemoryStatusForm::OnTouchMoved(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
551 {
552         __pLabel->SetTextColor(COLOR_SUB_TEXT);
553         __pLabel->Invalidate(false);
554 }
555
556 void
557 MemoryStatusForm::OnTouchPressed(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
558 {
559         __pLabel->SetTextColor(Color::GetColor(COLOR_ID_WHITE));
560         __pLabel->Invalidate(false);
561 }
562
563 void
564 MemoryStatusForm::OnTouchReleased(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
565 {
566         __pLabel->SetTextColor(COLOR_SUB_TEXT);
567         __pLabel->Invalidate(false);
568 }
569
570 void
571 MemoryStatusForm::OnTouchCanceled(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
572 {
573         __pLabel->SetTextColor(COLOR_SUB_TEXT);
574         __pLabel->Invalidate(false);
575 }
576
577 void
578 MemoryStatusForm::OnKeyPressed(const Tizen::Ui::Control& source, Tizen::Ui::KeyCode keyCode)
579 {
580         AppLog("Enter");
581         if(keyCode == KEY_ENTER)
582         {
583                 AppLog("Enter");
584                 __pLabel->SetTextColor(Color::GetColor(COLOR_ID_WHITE));
585                 __pLabel->Invalidate(false);
586         }
587 }
588
589 void
590 MemoryStatusForm::OnKeyReleased(const Tizen::Ui::Control& source, Tizen::Ui::KeyCode keyCode)
591 {
592         AppLog("Enter");
593         if(keyCode == KEY_ENTER)
594         {
595                 AppLog("Enter");
596                 __pLabel->SetTextColor(COLOR_SUB_TEXT);
597                 __pLabel->Invalidate(false);
598         }
599 }
600
601 void
602 MemoryStatusForm::OnKeyLongPressed(const Tizen::Ui::Control& source, Tizen::Ui::KeyCode keyCode)
603 {
604 }
605
606 void
607 MemoryStatusForm::OnFocusLost(const Tizen::Ui::Control& source)
608 {
609         AppLog("Enter");
610         __pLabel->SetTextColor(COLOR_SUB_TEXT);
611         __pLabel->Invalidate(false);
612 }
613
614 void
615 MemoryStatusForm::OnFocusGained(const Tizen::Ui::Control& source)
616 {
617         AppLog("Enter");
618 }