6932a3ddeded17128c625f4b701114d28f9eda0e
[apps/osp/Internet.git] / src / IntInternetApp.cpp
1 //
2
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.1 (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 //!Internet
19 /*@file:        Internet.cpp
20  *@brief:       provides basic features necessary to define an UiApp.
21  */
22
23 #include "IntBookmarkData.h"
24 #include "IntBookmarkPresentationModel.h"
25 #include "IntCommonLib.h"
26 #include "IntFaviconManager.h"
27 #include "IntHistoryPresentationModel.h"
28 #include "IntInternetApp.h"
29 #include "IntMainFrame.h"
30 #include "IntMainForm.h"
31 #include "IntMultipleWindowPresentationModel.h"
32 #include "IntSceneRegister.h"
33 #include "IntSettingPresentationModel.h"
34
35 using namespace Tizen::App;
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::System;
39 using namespace Tizen::Ui;
40 using namespace Tizen::Ui::Controls;
41 using namespace Tizen::Ui::Scenes;
42
43 const int MIN_MEMORY_NEEDED = 1048576;
44
45 InternetApp::InternetApp(void)
46 : __pMultiWindowManager(NULL),__pNewWindowInfo(null)
47 {
48 }
49
50 InternetApp::~InternetApp(void)
51 {
52         if (__pNewWindowInfo != NULL)
53         {
54                 delete __pNewWindowInfo;
55                 __pNewWindowInfo = NULL;
56         }
57 }
58
59 UiApp*
60 InternetApp::CreateInstance(void)
61 {
62         // Create the instance through the constructor.
63         return new(std::nothrow) InternetApp();
64 }
65
66 void
67 InternetApp::ShowPopup(String& pTitle, String& pText)
68 {
69         MessageBox messageBox;
70         messageBox.Construct(pTitle, pText,MSGBOX_STYLE_OK,3000);
71         int modalResult = 0;
72
73         messageBox.ShowAndWait(modalResult);
74         switch (modalResult)
75         {
76         case MSGBOX_RESULT_OK:
77         {
78
79         }
80         break;
81         default:
82                 break;
83         }
84 }
85
86 bool
87 InternetApp::OnAppInitializing(AppRegistry& appRegistry)
88 {
89         AppLog("ABC::Internet::OnAppInitializing entered");
90         AppControlProviderManager* pAppMgr = AppControlProviderManager::GetInstance();
91         if (pAppMgr != null)
92         {
93                 result r = pAppMgr->SetAppControlProviderEventListener(this);
94                 AppLogDebug("SetAppControlProviderEventListener result %s",GetErrorMessage(r));
95         }
96         return true;
97 }
98
99 bool
100 InternetApp::OnAppInitialized(void)
101 {
102         result r = E_SUCCESS;
103
104         long long mem = GetAvailableMemory();
105         AppLogDebug("%lld",mem);
106         if(mem < MIN_MEMORY_NEEDED)
107         {
108                 MessageBox LowMemory;
109                 String msg = L"";
110                 LowMemory.Construct(CommonUtil::GetString(L"IDS_LOW_MEMORY_MSG_TITLE"), CommonUtil::GetString(L"IDS_LOW_MEMORY_MSG"), MSGBOX_STYLE_NONE,1000);
111                 int modalResult = 0;
112                 // Calls ShowAndWait() : Draws and Shows itself and processes events
113                 LowMemory.ShowAndWait(modalResult);
114
115                 return false;
116
117         }
118         String title1 = L"URL1";
119         String title2 = L"URL2";
120         String folder1 = L"Folder 1";
121         String strSite1 = "Site 1";
122
123         BookmarkPresentationModel::GetInstance();
124         HistoryPresentationModel::GetInstance();
125         FaviconManager::GetInstance();
126         SettingPresentationModel::GetInstance();
127
128 //      MultipleWindowPresentationModel::GetInstance();
129
130         SettingInfo::AddSettingEventListener(*this);
131         __pMultiWindowManager = MultipleWindowPresentationModel::GetInstance();
132         AppLogDebug("window count %d",__pMultiWindowManager->GetAllWindowArrayList()->GetCount());
133
134
135
136         // Create a Frame
137         MainFrame* pSceneManagementFrame = new(std::nothrow) MainFrame();
138         pSceneManagementFrame->Construct();
139         pSceneManagementFrame->SetName(L"SceneManagement");
140         AddFrame(*pSceneManagementFrame);
141         // Goto the first scene.
142
143         SceneManager* pSceneManager = SceneManager::GetInstance();
144         if (pSceneManager == null)
145         {
146                 AppLogDebug("pSceneManager is null returning");
147                 return false;
148         }
149
150         String homePage = SettingPresentationModel::GetInstance()->GetHomepage();
151         if (homePage == (L"IDS_BR_BODY_MOST_VISITED_SITES") || homePage == (L"IDS_BR_BODY_BLANK_PAGE"))
152         {
153                 homePage = L"";
154         }
155         else if (homePage == CommonUtil::GetString(L"IDS_BR_BODY_USER_HOMEPAGE"))
156         {
157                 AppLog("InternetApp::OnAppInitialized homaepage = userhomepage");
158                 homePage = SettingPresentationModel::GetInstance()->GetFavoriteUrl();
159                 AppLog("InternetApp::OnAppInitialized fav URL = %ls ",homePage.GetPointer());
160         }
161
162         if (__pNewWindowInfo == null)//if the application is launched normally
163         {
164                 __pNewWindowInfo = MultipleWindowPresentationModel::GetInstance()->CreateNewMainViewSceneN(homePage);
165         }
166
167         if (__pNewWindowInfo == null)
168         {
169                 return false;
170         }
171
172         ArrayList* pArgList = new(std::nothrow) ArrayList();
173         pArgList->Construct();
174         pArgList->Add(*__pNewWindowInfo);
175         /*
176          * The GoForward call cannot be moved into the MainFrame::OnInitializing
177          * because when the application is launched from the appcontol, the data(URL)
178          * is not available for Mainframe, and we cannot pass the url information to
179          * the Mainframe because logically MainFrame should not know the URL information.
180          */
181         if (E_SUCCESS != pSceneManager->GoForward(ForwardSceneTransition(__pNewWindowInfo->sceneID, SCENE_TRANSITION_ANIMATION_TYPE_NONE),pArgList))
182         {
183                 AppLogDebug("CustomAppControl::OnAppInitializing Result = %s", GetLastResult());
184                 delete pArgList;
185                 __pNewWindowInfo = null;
186                 return false;
187         }
188
189         AppRegistry* pAppRegistry = App::GetInstance()->GetAppRegistry();
190         String sliderKey("SliderValue");
191
192         int sliderValue = 0;
193
194
195         r = pAppRegistry->Get(sliderKey, sliderValue);
196         r = PowerManager::SetScreenBrightness(sliderValue);
197
198         AppLog("Settingscreenbrightness to %d result %s",sliderValue , GetErrorMessage(r));
199
200         delete pArgList;
201         pArgList = null;
202         __pNewWindowInfo = null;
203
204         return true;
205 }
206
207 bool
208 InternetApp::OnAppWillTerminate(void)
209 {
210         return true;
211 }
212
213
214 bool
215 InternetApp::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
216 {
217         AppLogDebug("Internet::OnAppTerminating");
218
219         SettingInfo::RemoveSettingEventListener(*this);
220
221         return true;
222 }
223
224 void
225 InternetApp::OnForeground(void)
226 {
227         Form* pCurrentForm = (SceneManager::GetInstance()->GetCurrentScene()->GetForm());
228         if (pCurrentForm)
229         {
230                 pCurrentForm->SendUserEvent(FORE_GROUND_APP_EVENT, NULL);
231         }
232 }
233
234 void
235 InternetApp::OnBackground(void)
236 {
237         Form* pCurrentForm = (SceneManager::GetInstance()->GetCurrentScene()->GetForm());
238         if(pCurrentForm)
239         {
240                 pCurrentForm->SendUserEvent(BACK_GROUND_APP_EVENT, NULL);
241         }
242 }
243
244 void
245 InternetApp::OnLowMemory(void)
246 {
247
248 }
249
250 void
251 InternetApp::OnBatteryLevelChanged(BatteryLevel batteryLevel)
252 {
253
254 }
255
256 void
257 InternetApp::OnScreenOn(void)
258 {
259
260 }
261
262 void
263 InternetApp::OnScreenOff(void)
264 {
265         // TODO:
266         // Unless there is a strong reason to do otherwise, release resources (such as 3D, media, and sensors) to allow the device
267         // to enter the sleep mode to save the battery.
268         // Invoking a lengthy asynchronous method within this listener method can be risky, because it is not guaranteed to invoke a
269         // callback before the device enters the sleep mode.
270         // Similarly, do not perform lengthy operations in this listener method. Any operation must be a quick one.
271 }
272
273 void
274 InternetApp::OnSettingChanged(Tizen::Base::String& key)
275 {
276         AppLog("InternetApp::OnSettingChanged: %ls", key.GetPointer());
277         if (key == L"http://tizen.org/setting/locale.language")
278         {
279                 Terminate();
280         }
281 }
282
283 void
284 InternetApp::OnAppControlRequestReceived(RequestId reqId, const String &operationId, const String* pUriScheme, const String *pDataType, const IMap *pArgs)
285 {
286         AppLogDebug("ABC::Internet::OnAppControlRequestReceivedN entered %ls",operationId.GetPointer());
287
288         if (operationId.CompareTo(L"http://tizen.org/appcontrol/operation/view") == 0)
289         {
290                 String urlData;
291                 if (pUriScheme)
292                 {
293                         urlData = *pUriScheme;
294                 }
295                 else
296                 {
297                         int argCount = pArgs->GetCount();
298                         AppLog("argCount == %d", argCount);
299
300                         IList *pValues = pArgs->GetValuesN();
301                         if(pValues == null)
302                         {
303                                 AppLog("pValues == null");
304                                 return;
305                         }
306
307                         String url = L"url";
308                         String* pUrl = dynamic_cast<String*>(const_cast<Object*>(pArgs->GetValue(url)));
309                         if(pUrl == null)
310                         {
311                                 delete pValues;
312                                 AppLog("pUrl == null");
313                                 return;
314                         }
315                         urlData = *pUrl;
316                 }
317                 AppLog("URL received %ls", urlData.GetPointer());
318
319                 if (__pNewWindowInfo == null)
320                 {
321                         __pNewWindowInfo = MultipleWindowPresentationModel::GetInstance()->CreateNewMainViewSceneN(urlData, true);
322                 }
323                 if (__pNewWindowInfo == null)
324                 {
325                         if (MultipleWindowPresentationModel::GetInstance()->GetAllWindowArrayList()->GetCount() >= 9)
326                         {
327                                 String strTitle = CommonUtil::GetString(L"IDS_COM_SK_MAX_LIMIT_REACHED");
328                                 String strText = CommonUtil::GetString(L"IDS_COM_SK_CREATE_WINDOWS_BNO");
329                                 ShowPopup(strTitle, strText);
330                         }
331                         return;
332                 }
333                 if (__pMultiWindowManager != null && __pMultiWindowManager->GetAllWindowArrayList()->GetCount() != 0)
334                 {
335                         //if the application is running in background and it receives an appcontrol request it will launch a new(std::nothrow) window here,
336                         //otherwise appcontrol request will be handled by OnAppInitialized method
337                         SceneManager* pSceneManager = SceneManager::GetInstance();
338                         if (pSceneManager == null)
339                         {
340                                 return;
341                         }
342
343                         ArrayList* pArgList = new(std::nothrow) ArrayList();
344                         pArgList->Construct();
345                         pArgList->Add(*__pNewWindowInfo);
346
347                         if (E_SUCCESS != pSceneManager->GoForward(ForwardSceneTransition(__pNewWindowInfo->sceneID, SCENE_TRANSITION_ANIMATION_TYPE_NONE),pArgList))
348                         {
349                                 AppLogDebug("CustomAppControl::OnAppInitializing Result = %s", GetLastResult());
350                                                 delete pArgList;
351                                 __pNewWindowInfo = null;
352                                 return;
353                         }
354
355                         delete pArgList;
356
357                         __pNewWindowInfo = null;
358                 }
359         }
360         AppLogDebug("ABC::Internet::OnAppControlRequestReceivedN Exit");
361         return;
362 }
363
364
365 long long
366 InternetApp::GetAvailableMemory(void)
367 {
368     result r = E_SUCCESS;
369
370     String key(L"http://tizen.org/runtime/storage.available.internal");
371     long long allocatedMemory = 0;
372
373     r = RuntimeInfo::GetValue(key, allocatedMemory);
374     TryCatch(r == E_SUCCESS, , "MyRuntimeInfo: Failed to get value");
375
376     return allocatedMemory;
377
378    CATCH:
379     return 0;
380 }