0216579641fb41ea718b2be691a3d2fa2f70ca91
[apps/native/sample/DynamicBoxViewer.git] / project / src / LiveboxViewerMainForm.cpp
1 //
2 // Tizen C++ SDK
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <FApp.h>
19 #include <FMedia.h>
20 #include <FShellLiveboxProviderInfo.h>
21 #include <FShellLiveboxManager.h>
22 #include <FShellLiveboxSizeInfo.h>
23
24 #include "LiveboxViewerMainForm.h"
25 #include "LiveboxViewerFormFactory.h"
26
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Collection;
29 using namespace Tizen::App;
30 using namespace Tizen::Graphics;
31 using namespace Tizen::Ui;
32 using namespace Tizen::Ui::Controls;
33 using namespace Tizen::Ui::Scenes;
34 using namespace Tizen::Shell;
35
36 const int LIST_ITEM_ICON = 72;
37 const int LIST_ITEM_HEIGHT = 112;
38 const int LIST_ITEM_MARGIN_LEFT = 26;
39 const int LIST_ITEM_MARGIN_TOP = 32;
40 const int LIST_ITEM_TEXT_HEIGHT = 44;
41
42 LiveboxViewerMainForm::LiveboxViewerMainForm(void)
43         : __pList(null)
44         , __pLiveboxProviderList(null)
45 {
46         __pLiveboxProviderList = LiveboxManager::GetInstance()->GetLiveboxProviderInfoListN();
47         TryReturnVoid(__pLiveboxProviderList, "GetLiveboxListN returns null!!" );
48
49         __pLiveboxIconList = new ArrayList(SingleObjectDeleter);
50         __pLiveboxIconList->Construct();
51         for( int i = 0; i < __pLiveboxProviderList->GetCount(); i++)
52         {
53                 LiveboxProviderInfo* pLiveboxProviderInfo = dynamic_cast<LiveboxProviderInfo*>(__pLiveboxProviderList->GetAt(i) );
54
55                 Tizen::Media::Image image;
56                 result r = image.Construct();
57                 Bitmap* pBmp = image.DecodeN(pLiveboxProviderInfo->GetIconPath(), BITMAP_PIXEL_FORMAT_ARGB8888);
58
59                 __pLiveboxIconList->Add(pBmp);
60         }
61 }
62
63 LiveboxViewerMainForm::~LiveboxViewerMainForm(void)
64 {
65 }
66
67 bool
68 LiveboxViewerMainForm::Initialize(void)
69 {
70         Construct(L"IDF_FORM");
71
72         return true;
73 }
74
75 result
76 LiveboxViewerMainForm::OnInitializing(void)
77 {
78         result r = E_SUCCESS;
79
80         // Setup back event listener
81         SetFormBackEventListener(this);
82
83         if (!__pLiveboxProviderList)
84         {
85                 AppLog("GetLiveboxListN returns null!!");
86                 return E_SUCCESS;
87         }
88
89         for(int i = 0; i < __pLiveboxProviderList->GetCount(); i++)
90         {
91                 LiveboxProviderInfo* pLiveboxProviderInfo = dynamic_cast<LiveboxProviderInfo*>(__pLiveboxProviderList->GetAt(i) );
92
93                 IList* pSizeList = pLiveboxProviderInfo->GetSizeInfoListN();
94                 if(pSizeList == null) continue;
95                 AppLog("%ls, support (%d) sizes", pLiveboxProviderInfo->GetDisplayName().GetPointer(), pSizeList->GetCount() );
96
97                 for(int j = 0; j < pSizeList->GetCount(); j ++)
98                 {
99                         LiveboxSizeInfo* pSizeInfo = dynamic_cast<LiveboxSizeInfo*>(pSizeList->GetAt(j) );
100                         AppLog("(%d, %d)", pSizeInfo->GetSize().width, pSizeInfo->GetSize().height);
101                 }
102                 pSizeList->RemoveAll(true);
103                 delete pSizeList;
104         }
105         
106         // TODO:
107         // Add your initialization code here
108         __pList = new (std::nothrow) ListView();
109         __pList->Construct(Rectangle(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height), true, false);
110         __pList->SetItemProvider(*this);
111         __pList->AddListViewItemEventListener(*this);
112         AddControl(*__pList);
113
114         return r;
115 }
116
117 result
118 LiveboxViewerMainForm::OnTerminating(void)
119 {
120         result r = E_SUCCESS;
121
122         // TODO:
123         // Add your termination code here
124         if (__pLiveboxProviderList)
125         {
126                 __pLiveboxProviderList->RemoveAll(true);
127                 delete __pLiveboxProviderList;
128         }
129
130         if (__pLiveboxIconList)
131         {
132                 __pLiveboxIconList->RemoveAll();
133                 delete __pLiveboxIconList;
134         }
135
136         return r;
137 }
138
139 void
140 LiveboxViewerMainForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
141 {
142         SceneManager* pSceneManager = SceneManager::GetInstance();
143         AppAssert(pSceneManager);
144
145         switch(actionId)
146         {
147         default:
148                 break;
149         }
150 }
151
152 void
153 LiveboxViewerMainForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
154 {
155         UiApp* pApp = UiApp::GetInstance();
156         AppAssert(pApp);
157         pApp->Terminate();
158 }
159
160 void
161 LiveboxViewerMainForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId,
162                                                                                   const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
163 {
164         // TODO:
165         // Add your scene activate code here
166         AppLog("OnSceneActivatedN");
167 }
168
169 void
170 LiveboxViewerMainForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
171                                                                                    const Tizen::Ui::Scenes::SceneId& nextSceneId)
172 {
173         // TODO:
174         // Add your scene deactivate code here
175         AppLog("OnSceneDeactivated");
176 }
177
178 void
179 LiveboxViewerMainForm::OnListViewItemStateChanged(Tizen::Ui::Controls::ListView &listView, int index, int elementId, Tizen::Ui::Controls::ListItemStatus status)
180 {
181         if(status == LIST_ITEM_STATUS_SELECTED)
182         {
183                 __pSelectedProviderInfo = dynamic_cast<LiveboxProviderInfo*>(__pLiveboxProviderList->GetAt(index));
184                 AppAssert(__pSelectedProviderInfo);
185
186                 String configurationAppControlAppId = __pSelectedProviderInfo->GetConfigurationAppControlAppId();
187                 if(!configurationAppControlAppId.IsEmpty())
188                 {
189                         StartConfigurationAppControl(__pSelectedProviderInfo->GetConfigurationAppControlAppId(), __pSelectedProviderInfo->GetName());
190                 }
191                 else
192                 {
193                         AppLog("%ls", __pSelectedProviderInfo->GetDisplayName().GetPointer());
194                         GoLiveboxViewScene(__pSelectedProviderInfo->GetAppId(), __pSelectedProviderInfo->GetName(), L"");
195                 }
196         }
197 }
198
199 void
200 LiveboxViewerMainForm::StartConfigurationAppControl(const Tizen::Base::String& appId, const Tizen::Base::String& providerName)
201 {
202         AppControl* pAc = AppManager::FindAppControlN( appId, L"http://tizen.org/appcontrol/operation/livebox/configuration");
203         HashMap extra;
204         extra.Construct();
205         String key = L"ProviderName";
206         String value = providerName;
207         extra.Add(&key, &value);
208
209         pAc->Start(null, null, &extra, this);
210         delete pAc;
211 }
212
213 void
214 LiveboxViewerMainForm::GoLiveboxViewScene(const String& appId, const String& providerName, const String& userInfo)
215 {
216         ArrayList* pList = new (std::nothrow) ArrayList;
217         AppAssert(pList);
218         pList->Construct();
219
220         pList->Add(*(new (std::nothrow) String(appId) ));
221         pList->Add(*(new (std::nothrow) String(providerName) ));
222         pList->Add(*(new (std::nothrow) String(userInfo )) );
223
224         SceneManager* pSceneManager = SceneManager::GetInstance();
225         AppAssert(pSceneManager);
226
227         pSceneManager->GoForward(ForwardSceneTransition(VIEW_SCENE, SCENE_TRANSITION_ANIMATION_TYPE_LEFT), pList);
228         __pSelectedProviderInfo = null;
229 }
230
231 void
232 LiveboxViewerMainForm::OnListViewItemSwept(Tizen::Ui::Controls::ListView &listView, int index, Tizen::Ui::Controls::SweepDirection direction)
233 {
234 }
235
236 void
237 LiveboxViewerMainForm::OnListViewContextItemStateChanged(Tizen::Ui::Controls::ListView &listView, int index, int elementId, Tizen::Ui::Controls::ListContextItemStatus state)
238 {
239 }
240
241 void
242 LiveboxViewerMainForm::OnItemReordered(Tizen::Ui::Controls::ListView &view, int oldIndex, int newIndex)
243 {
244 }
245
246 int
247 LiveboxViewerMainForm::GetItemCount(void)
248 {
249         return __pLiveboxProviderList->GetCount();
250 }
251
252 Tizen::Ui::Controls::ListItemBase*
253 LiveboxViewerMainForm::CreateItem (int index, int itemWidth)
254 {
255         CustomItem* pItem = new (std::nothrow) CustomItem();
256         pItem->Construct(Tizen::Graphics::Dimension(itemWidth, LIST_ITEM_HEIGHT), LIST_ANNEX_STYLE_NORMAL);
257         
258         Rectangle iconRect(LIST_ITEM_MARGIN_LEFT, LIST_ITEM_MARGIN_TOP/2, LIST_ITEM_ICON, LIST_ITEM_ICON);
259         int text_x = LIST_ITEM_ICON + LIST_ITEM_MARGIN_LEFT + 20;
260         Rectangle textRect(text_x, LIST_ITEM_MARGIN_TOP, GetClientAreaBounds().width - LIST_ITEM_MARGIN_LEFT - text_x, LIST_ITEM_TEXT_HEIGHT);
261         
262         LiveboxProviderInfo* pProviderInfo = dynamic_cast<LiveboxProviderInfo*>(__pLiveboxProviderList->GetAt(index));
263
264         if (pProviderInfo)
265         {
266                 Bitmap* pBmp = dynamic_cast<Bitmap*>(__pLiveboxIconList->GetAt(index));
267
268                 pItem->AddElement(iconRect, ID_FORMAT_BITMAP, *pBmp, null, null);
269                 pItem->AddElement(textRect, ID_FORMAT_STRING, pProviderInfo->GetDisplayName()+" ("+pProviderInfo->GetAppId()+")", true);
270         }
271
272         return pItem;
273 }
274
275 bool
276 LiveboxViewerMainForm::DeleteItem(int index, Tizen::Ui::Controls::ListItemBase *pItem, int itemWidth)
277 {
278         delete pItem;
279         return true;
280 }
281
282
283 void
284 LiveboxViewerMainForm::OnAppControlCompleteResponseReceived(const AppId& appId, const Tizen::Base::String& operationId, AppCtrlResult appControlResult, const Tizen::Base::Collection::IMap* pExtraData)
285 {
286         String temp;
287         TryReturnVoid(__pSelectedProviderInfo, "__pSelectedProviderInfo should not be null!");
288
289         if(pExtraData)
290         {
291                 String userInfokey(L"UserInfo");
292                 const String* pUserInfoValue = dynamic_cast<const String*>(pExtraData->GetValue(userInfokey) );
293                 AppLog("%ls, %ls, %ls", appId.GetPointer(), operationId.GetPointer(), pUserInfoValue->GetPointer() );
294
295                 GoLiveboxViewScene(__pSelectedProviderInfo->GetAppId(), __pSelectedProviderInfo->GetName(), *pUserInfoValue);
296         }
297 }