3700267f089edcec65a178935705611e179d6634
[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 static const wchar_t* APPCONTROL_SELECT_KEY_IMAGE = L"http://tizen.org/appcontrol/data/image/crop_mode";
44 static const wchar_t* APPCONTROL_SELECT_VALUE_IMAGE = L"fit_to_screen";
45 static const wchar_t* APPCONTROL_PROVIDER_ID_IMAGE_VIEWER = L"tizen.imageviewer";
46 static const wchar_t* APPCONTROL_OPERATION_ID_IMAGE_VIEWER = L"http://tizen.org/appcontrol/operation/view";
47 const int MIN_MEMORY_NEEDED = 1048576;
48
49 InternetApp::InternetApp(void)
50 : __pMultiWindowManager(NULL),__pNewWindowInfo(null),pSceneManagementFrame(null),__value(null)
51 {
52 }
53
54 InternetApp::~InternetApp(void)
55 {
56         if (__pNewWindowInfo != NULL)
57         {
58                 delete __pNewWindowInfo;
59                 __pNewWindowInfo = NULL;
60         }
61 }
62
63 UiApp*
64 InternetApp::CreateInstance(void)
65 {
66         // Create the instance through the constructor.
67         return new(std::nothrow) InternetApp();
68 }
69
70 void
71 InternetApp::ShowPopup(String& pTitle, String& pText)
72 {
73         MessageBox messageBox;
74         messageBox.Construct(pTitle, pText,MSGBOX_STYLE_OK,3000);
75         int modalResult = 0;
76
77         messageBox.ShowAndWait(modalResult);
78         switch (modalResult)
79         {
80         case MSGBOX_RESULT_OK:
81         {
82
83         }
84         break;
85         default:
86                 break;
87         }
88 }
89
90 bool
91 InternetApp::OnAppInitializing(AppRegistry& appRegistry)
92 {
93         AppLog("ABC::Internet::OnAppInitializing entered");
94         AppControlProviderManager* pAppMgr = AppControlProviderManager::GetInstance();
95         if (pAppMgr != null)
96         {
97                 result r = pAppMgr->SetAppControlProviderEventListener(this);
98                 AppLogDebug("SetAppControlProviderEventListener result %s",GetErrorMessage(r));
99         }
100         return true;
101 }
102
103 bool
104 InternetApp::OnAppInitialized(void)
105 {
106         result r = E_SUCCESS;
107
108         long long mem = GetAvailableMemory();
109         AppLogDebug("%lld",mem);
110         if(mem < MIN_MEMORY_NEEDED)
111         {
112                 MessageBox LowMemory;
113                 String msg = L"";
114                 LowMemory.Construct(CommonUtil::GetString(L"IDS_LOW_MEMORY_MSG_TITLE"), CommonUtil::GetString(L"IDS_LOW_MEMORY_MSG"), MSGBOX_STYLE_NONE,1000);
115                 int modalResult = 0;
116                 // Calls ShowAndWait() : Draws and Shows itself and processes events
117                 LowMemory.ShowAndWait(modalResult);
118
119                 return false;
120
121         }
122         String title1 = L"URL1";
123         String title2 = L"URL2";
124         String folder1 = L"Folder 1";
125         String strSite1 = "Site 1";
126
127         BookmarkPresentationModel::GetInstance();
128         HistoryPresentationModel::GetInstance();
129         FaviconManager::GetInstance();
130         SettingPresentationModel::GetInstance();
131
132 //      MultipleWindowPresentationModel::GetInstance();
133
134         SettingInfo::AddSettingEventListener(*this);
135         __pMultiWindowManager = MultipleWindowPresentationModel::GetInstance();
136         AppLogDebug("window count %d",__pMultiWindowManager->GetAllWindowArrayList()->GetCount());
137
138
139
140         // Create a Frame
141         pSceneManagementFrame = new(std::nothrow) MainFrame();
142         pSceneManagementFrame->Construct();
143         pSceneManagementFrame->SetName(L"SceneManagement");
144         AddFrame(*pSceneManagementFrame);
145         pSceneManagementFrame->Show();
146         // Goto the first scene.
147
148         SceneManager* pSceneManager = SceneManager::GetInstance();
149         if (pSceneManager == null)
150         {
151                 AppLogDebug("pSceneManager is null returning");
152                 return false;
153         }
154
155         String homePage = SettingPresentationModel::GetInstance()->GetHomepage();
156         if (homePage == (L"IDS_BR_BODY_MOST_VISITED_SITES") || homePage == (L"IDS_BR_BODY_BLANK_PAGE"))
157         {
158                 homePage = L"";
159         }
160         else if (homePage == CommonUtil::GetString(L"IDS_BR_BODY_USER_HOMEPAGE"))
161         {
162                 AppLog("InternetApp::OnAppInitialized homaepage = userhomepage");
163                 homePage = SettingPresentationModel::GetInstance()->GetFavoriteUrl();
164                 AppLog("InternetApp::OnAppInitialized fav URL = %ls ",homePage.GetPointer());
165         }
166
167         if (__pNewWindowInfo == null)//if the application is launched normally
168         {
169                 __pNewWindowInfo = MultipleWindowPresentationModel::GetInstance()->CreateNewMainViewSceneN(homePage);
170         }
171
172         if (__pNewWindowInfo == null)
173         {
174                 return false;
175         }
176
177         if (__operationId.CompareTo(L"http://tizen.org/appcontrol/operation/main") == 0)
178         {
179                 AppControl* pAc = AppManager::FindAppControlN(APPCONTROL_PROVIDER_ID_IMAGE_VIEWER,
180                                 APPCONTROL_OPERATION_ID_IMAGE_VIEWER);
181                 if (pAc && __value && __value->GetLength() > 0)
182                 {
183                         AppLog("17jul __value is %S", __value->GetPointer());
184                         if (pAc->Start(__value, null, null, null) == E_SUCCESS)
185                         {
186                                 delete pAc;
187                         }
188                         if(__value != null)
189                         {
190                                 delete __value;
191                                 __value = null;
192                         }
193 //                      return true;
194                 }
195         }
196
197         ArrayList* pArgList = new(std::nothrow) ArrayList();
198         pArgList->Construct();
199         pArgList->Add(*__pNewWindowInfo);
200         /*
201          * The GoForward call cannot be moved into the MainFrame::OnInitializing
202          * because when the application is launched from the appcontol, the data(URL)
203          * is not available for Mainframe, and we cannot pass the url information to
204          * the Mainframe because logically MainFrame should not know the URL information.
205          */
206         if (E_SUCCESS != pSceneManager->GoForward(ForwardSceneTransition(__pNewWindowInfo->sceneID, SCENE_TRANSITION_ANIMATION_TYPE_NONE),pArgList))
207         {
208                 AppLogDebug("CustomAppControl::OnAppInitializing Result = %s", GetLastResult());
209                 delete pArgList;
210                 __pNewWindowInfo = null;
211                 return false;
212         }
213
214         delete pArgList;
215         pArgList = null;
216         __pNewWindowInfo = null;
217
218         return true;
219 }
220
221 bool
222 InternetApp::OnAppWillTerminate(void)
223 {
224         return true;
225 }
226
227
228 bool
229 InternetApp::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
230 {
231         AppLogDebug("Internet::OnAppTerminating");
232
233         SettingInfo::RemoveSettingEventListener(*this);
234
235         return true;
236 }
237
238 void
239 InternetApp::OnForeground(void)
240 {
241         UiApp* pApp = null;
242         pApp = UiApp::GetInstance();
243         if (pApp != null && pSceneManagementFrame != null)
244         {
245                 pApp->GetFrameAt(0)->SetShowMode(FRAME_SHOW_MODE_FULL_SCREEN);
246         }
247         AppLog("InternetApp::OnForeground");
248
249         AppRegistry* pAppRegistry = App::GetInstance()->GetAppRegistry();
250         String sliderKey("SliderValue");
251
252         int sliderValue = 0;
253
254
255         result r = pAppRegistry->Get(sliderKey, sliderValue);
256         r = PowerManager::SetScreenBrightness(sliderValue);
257 //      r = PowerManager::RestoreScreenBrightness();
258
259         AppLog("Settingscreenbrightness to %d result %s",sliderValue , GetErrorMessage(r));
260
261         Form* pCurrentForm = null;
262         if(SceneManager::GetInstance()->GetCurrentScene() != null)
263                 pCurrentForm = (SceneManager::GetInstance()->GetCurrentScene()->GetForm());
264         if (pCurrentForm)
265         {
266                 pCurrentForm->SendUserEvent(FORE_GROUND_APP_EVENT, NULL);
267         }
268
269
270 }
271
272 void
273 InternetApp::OnBackground(void)
274 {
275         Form* pCurrentForm = null;
276         if(SceneManager::GetInstance()->GetCurrentScene() != null)
277                 pCurrentForm = (SceneManager::GetInstance()->GetCurrentScene()->GetForm());
278         if(pCurrentForm)
279         {
280                 pCurrentForm->SendUserEvent(BACK_GROUND_APP_EVENT, NULL);
281         }
282 }
283
284 void
285 InternetApp::OnLowMemory(void)
286 {
287
288 }
289
290 void
291 InternetApp::OnBatteryLevelChanged(BatteryLevel batteryLevel)
292 {
293
294 }
295
296 void
297 InternetApp::OnScreenOn(void)
298 {
299
300 }
301
302 void
303 InternetApp::OnScreenOff(void)
304 {
305         // TODO:
306         // Unless there is a strong reason to do otherwise, release resources (such as 3D, media, and sensors) to allow the device
307         // to enter the sleep mode to save the battery.
308         // Invoking a lengthy asynchronous method within this listener method can be risky, because it is not guaranteed to invoke a
309         // callback before the device enters the sleep mode.
310         // Similarly, do not perform lengthy operations in this listener method. Any operation must be a quick one.
311 }
312
313 void
314 InternetApp::OnSettingChanged(Tizen::Base::String& key)
315 {
316         AppLog("InternetApp::OnSettingChanged: %ls", key.GetPointer());
317         if (key == L"http://tizen.org/setting/locale.language" || key == L"http://tizen.org/setting/font.size")
318         {
319                 Terminate();
320         }
321 }
322
323 void
324 InternetApp::OnAppControlRequestReceived(RequestId reqId, const String &operationId, const String* pUriScheme, const String *pDataType, const IMap *pArgs)
325 {
326         UiApp* pApp = null;
327         pApp = UiApp::GetInstance();
328         if (pApp != null &&  pSceneManagementFrame != null)
329         {
330                 pApp->GetFrameAt(0)->SetShowMode(FRAME_SHOW_MODE_FULL_SCREEN);
331
332         }
333
334         AppLogDebug("ABC::Internet::OnAppControlRequestReceivedN entered %ls",operationId.GetPointer());
335         __operationId.Clear();
336         __operationId.Append(operationId);
337         if (operationId.CompareTo(L"http://tizen.org/appcontrol/operation/view") == 0)
338         {
339                 String urlData;
340                 if (pUriScheme)
341                 {
342                         urlData = *pUriScheme;
343                 }
344                 else
345                 {
346                         int argCount = pArgs->GetCount();
347                         AppLog("argCount == %d", argCount);
348
349                         IList *pValues = pArgs->GetValuesN();
350                         if(pValues == null)
351                         {
352                                 AppLog("pValues == null");
353                                 return;
354                         }
355
356                         String url = L"url";
357                         String* pUrl = dynamic_cast<String*>(const_cast<Object*>(pArgs->GetValue(url)));
358                         if(pUrl == null)
359                         {
360                                 delete pValues;
361                                 AppLog("pUrl == null");
362                                 return;
363                         }
364                         urlData = *pUrl;
365                 }
366                 AppLog("URL received %ls", urlData.GetPointer());
367                 if(pSceneManagementFrame)
368                         pSceneManagementFrame->Show();
369
370                 if (__pNewWindowInfo == null)
371                 {
372                         __pNewWindowInfo = MultipleWindowPresentationModel::GetInstance()->CreateNewMainViewSceneN(urlData, true);
373                 }
374                 if (__pNewWindowInfo == null)
375                 {
376                         if (MultipleWindowPresentationModel::GetInstance()->GetAllWindowArrayList()->GetCount() >= 9)
377                         {
378                                 String strTitle = L"";//CommonUtil::GetString(L"IDS_COM_SK_MAX_LIMIT_REACHED");
379                                 String strText = CommonUtil::GetString(L"IDS_BR_BODY_COULD_NOT_OPEN_NEW_WINDOW_BECAUSE_YOU_HAVE_ALREADY_OPENED_MAXIMUM_NUMBER");
380                                 ShowPopup(strTitle, strText);
381                         }
382                         return;
383                 }
384                 if (__pMultiWindowManager != null && __pMultiWindowManager->GetAllWindowArrayList()->GetCount() != 0)
385                 {
386                         //if the application is running in background and it receives an appcontrol request it will launch a new(std::nothrow) window here,
387                         //otherwise appcontrol request will be handled by OnAppInitialized method
388                         SceneManager* pSceneManager = SceneManager::GetInstance();
389                         if (pSceneManager == null)
390                         {
391                                 return;
392                         }
393
394                         ArrayList* pArgList = new(std::nothrow) ArrayList();
395                         pArgList->Construct();
396                         pArgList->Add(*__pNewWindowInfo);
397
398                         if (E_SUCCESS != pSceneManager->GoForward(ForwardSceneTransition(__pNewWindowInfo->sceneID, SCENE_TRANSITION_ANIMATION_TYPE_NONE),pArgList))
399                         {
400                                 AppLogDebug("CustomAppControl::OnAppInitializing Result = %s", GetLastResult());
401                                                 delete pArgList;
402                                 __pNewWindowInfo = null;
403                                 return;
404                         }
405
406                         delete pArgList;
407
408                         __pNewWindowInfo = null;
409                 }
410         }
411         else if (operationId.CompareTo(L"http://tizen.org/appcontrol/operation/main") == 0)
412         {
413                 if (pArgs)
414                 {
415                         //const String* pVal = (String *) pArgs->GetValue(String(L"http://tizen.org/appcontrol/data/notification"));
416                         String *data = (String *) pArgs->GetValue(String(L"http://tizen.org/appcontrol/data/notification"));
417                         if(data != NULL)
418                                 __value = new String(*data);
419                         if (__value && __value->GetLength() > 0)
420                         {
421                                 AppLog("17jul rahul __value is %S", __value->GetPointer());
422                         }
423                         if (__value && __value->GetLength() > 0 && pSceneManagementFrame != null)
424                         {
425                                 AppLog("17jul OnAppControlRequestReceived notification");
426                                 HashMap extraData;
427                                 extraData.Construct();
428                                 String typeKey = APPCONTROL_SELECT_KEY_IMAGE;
429                                 String typeVal = APPCONTROL_SELECT_VALUE_IMAGE;
430                                 extraData.Add(&typeKey, &typeVal);
431
432                                 AppControl* pAc = AppManager::FindAppControlN(APPCONTROL_PROVIDER_ID_IMAGE_VIEWER,
433                                                 APPCONTROL_OPERATION_ID_IMAGE_VIEWER);
434                                 if (pAc)
435                                 {
436                                         AppLog("17jul __value is %S", __value->GetPointer());
437                                         if (pAc->Start(__value, null, null, null) == E_SUCCESS)
438                                         {
439                                                 delete pAc;
440                                         }
441                                 }
442                                 if(__value != null)
443                                 {
444                                         delete __value;
445                                         __value = null;
446                                 }
447                         }
448                 }
449         }
450
451         AppLogDebug("ABC::Internet::OnAppControlRequestReceivedN Exit");
452         return;
453 }
454
455
456 long long
457 InternetApp::GetAvailableMemory(void)
458 {
459     result r = E_SUCCESS;
460
461     String key(L"http://tizen.org/runtime/storage.available.internal");
462     long long allocatedMemory = 0;
463
464     r = RuntimeInfo::GetValue(key, allocatedMemory);
465     TryCatch(r == E_SUCCESS, , "MyRuntimeInfo: Failed to get value");
466
467     return allocatedMemory;
468
469    CATCH:
470     return 0;
471 }
472