Fixed Nabi Issues 52332,54256,54601,54130,54264,54471
[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),__isShowMsg(false),__pAc(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                 if(__pAc)
180                 {
181                         r =__pAc->Stop();
182                         AppLog("AppControl Stop Result %s",GetErrorMessage(r));
183                         delete __pAc;
184                         __pAc = null;
185                 }
186                 __pAc = AppManager::FindAppControlN(APPCONTROL_PROVIDER_ID_IMAGE_VIEWER,
187                                 APPCONTROL_OPERATION_ID_IMAGE_VIEWER);
188                 if (__pAc && __value && __value->GetLength() > 0)
189                 {
190                         AppLog("17jul __value is %S", __value->GetPointer());
191                         if (__pAc->Start(__value, null, null, null) == E_SUCCESS)
192                         {
193 //                              delete pAc;
194                         }
195                         delete __value;
196                         __value = null;
197                 }
198         }
199
200         ArrayList* pArgList = new(std::nothrow) ArrayList();
201         pArgList->Construct();
202         pArgList->Add(*__pNewWindowInfo);
203         /*
204          * The GoForward call cannot be moved into the MainFrame::OnInitializing
205          * because when the application is launched from the appcontol, the data(URL)
206          * is not available for Mainframe, and we cannot pass the url information to
207          * the Mainframe because logically MainFrame should not know the URL information.
208          */
209         if (E_SUCCESS != pSceneManager->GoForward(ForwardSceneTransition(__pNewWindowInfo->sceneID, SCENE_TRANSITION_ANIMATION_TYPE_NONE),pArgList))
210         {
211                 AppLogDebug("CustomAppControl::OnAppInitializing Result = %s", GetLastResult());
212                 delete pArgList;
213                 __pNewWindowInfo = null;
214                 return false;
215         }
216
217         delete pArgList;
218         pArgList = null;
219         __pNewWindowInfo = null;
220
221         return true;
222 }
223
224 bool
225 InternetApp::OnAppWillTerminate(void)
226 {
227         return true;
228 }
229
230
231 bool
232 InternetApp::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
233 {
234         AppLogDebug("Internet::OnAppTerminating");
235
236         SettingInfo::RemoveSettingEventListener(*this);
237
238         return true;
239 }
240
241 void
242 InternetApp::OnForeground(void)
243 {
244         UiApp* pApp = null;
245         pApp = UiApp::GetInstance();
246         if (pApp != null && pSceneManagementFrame != null && pApp->GetFrameAt(0)->GetShowMode() != FRAME_SHOW_MODE_FULL_SCREEN)
247         {
248                 pApp->GetFrameAt(0)->SetShowMode(FRAME_SHOW_MODE_FULL_SCREEN);
249         }
250         AppLog("InternetApp::OnForeground called");
251
252         AppLog("InternetApp::OnForeground");
253
254         AppRegistry* pAppRegistry = App::GetInstance()->GetAppRegistry();
255         String sliderKey("SliderValue");
256
257         int sliderValue = 10;
258
259
260         result r = pAppRegistry->Get(sliderKey, sliderValue);
261         r = PowerManager::SetScreenBrightness(sliderValue);
262 //      r = PowerManager::RestoreScreenBrightness();
263
264         AppLog("Settingscreenbrightness to %d result %s",sliderValue , GetErrorMessage(r));
265
266         Form* pCurrentForm = null;
267         if(SceneManager::GetInstance()->GetCurrentScene() != null)
268                 pCurrentForm = (SceneManager::GetInstance()->GetCurrentScene()->GetForm());
269         if (pCurrentForm)
270         {
271                 pCurrentForm->SendUserEvent(FORE_GROUND_APP_EVENT, NULL);
272         }
273         Frame* pCurrentFrame = null;
274         pCurrentFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
275
276         if (pCurrentFrame != null)
277         {
278                 pCurrentFrame->SetEnabled(true);
279                 pCurrentFrame->Invalidate(true);
280         }
281
282         if ( __isShowMsg == true )
283         {
284                 __isShowMsg = false;
285                 SendUserEvent(FORE_GROUND_APP_EVENT, NULL);
286         }
287 }
288
289 void
290 InternetApp::OnBackground(void)
291 {
292         Form* pCurrentForm = null;
293         if(SceneManager::GetInstance()->GetCurrentScene() != null)
294                 pCurrentForm = (SceneManager::GetInstance()->GetCurrentScene()->GetForm());
295         if(pCurrentForm)
296         {
297                 pCurrentForm->SendUserEvent(BACK_GROUND_APP_EVENT, NULL);
298         }
299 }
300
301 void
302 InternetApp::OnLowMemory(void)
303 {
304
305 }
306
307 void
308 InternetApp::OnBatteryLevelChanged(BatteryLevel batteryLevel)
309 {
310
311 }
312
313 void
314 InternetApp::OnScreenOn(void)
315 {
316
317 }
318
319 void
320 InternetApp::OnScreenOff(void)
321 {
322         // TODO:
323         // Unless there is a strong reason to do otherwise, release resources (such as 3D, media, and sensors) to allow the device
324         // to enter the sleep mode to save the battery.
325         // Invoking a lengthy asynchronous method within this listener method can be risky, because it is not guaranteed to invoke a
326         // callback before the device enters the sleep mode.
327         // Similarly, do not perform lengthy operations in this listener method. Any operation must be a quick one.
328 }
329
330 void
331 InternetApp::OnSettingChanged(Tizen::Base::String& key)
332 {
333         AppLog("InternetApp::OnSettingChanged: %ls", key.GetPointer());
334         if (key == L"http://tizen.org/setting/locale.language" || key == L"http://tizen.org/setting/font.size")
335         {
336                 Terminate();
337         }
338         else if (key == L"http://tizen.org/setting/screen.brightness" )
339         {
340                 AppRegistry* pAppRegistry = App::GetInstance()->GetAppRegistry();
341                 String sliderKey("SliderValue");
342
343                 int sliderValue = 10;
344
345
346                 result r = pAppRegistry->Get(sliderKey, sliderValue);
347                 r = PowerManager::SetScreenBrightness(sliderValue);
348                 //      r = PowerManager::RestoreScreenBrightness();
349
350         }
351
352 }
353
354 void
355 InternetApp::OnAppControlRequestReceived(RequestId reqId, const String &operationId, const String* pUriScheme, const String *pDataType, const IMap *pArgs)
356 {
357         UiApp* pApp = null;
358         pApp = UiApp::GetInstance();
359         if (pApp != null &&  pSceneManagementFrame != null)
360         {
361                 pApp->GetFrameAt(0)->SetShowMode(FRAME_SHOW_MODE_FULL_SCREEN);
362
363         }
364
365         __isShowMsg = false;
366
367         AppLogDebug("ABC::Internet::OnAppControlRequestReceivedN entered %ls",operationId.GetPointer());
368         __operationId.Clear();
369         __operationId.Append(operationId);
370         if (operationId.CompareTo(L"http://tizen.org/appcontrol/operation/view") == 0)
371         {
372                 String urlData;
373                 if (pUriScheme)
374                 {
375                         urlData = *pUriScheme;
376                 }
377                 else
378                 {
379                         int argCount = pArgs->GetCount();
380                         AppLog("argCount == %d", argCount);
381
382                         IList *pValues = pArgs->GetValuesN();
383                         if(pValues == null)
384                         {
385                                 AppLog("pValues == null");
386                                 return;
387                         }
388
389                         String url = L"url";
390                         String* pUrl = dynamic_cast<String*>(const_cast<Object*>(pArgs->GetValue(url)));
391                         if(pUrl == null)
392                         {
393                                 delete pValues;
394                                 AppLog("pUrl == null");
395                                 return;
396                         }
397                         urlData = *pUrl;
398                 }
399                 AppLog("URL received %ls", urlData.GetPointer());
400                 if(pSceneManagementFrame)
401                         pSceneManagementFrame->Show();
402
403
404                 if (__pNewWindowInfo == null)
405                 {
406                         if (MultipleWindowPresentationModel::GetInstance()->GetAllWindowArrayList()->GetCount() >= 9)
407                         {
408                                 __isShowMsg = true;
409                                 return;
410                         }
411                 }
412
413                 if (__pNewWindowInfo == null)
414                 {
415                         __pNewWindowInfo = MultipleWindowPresentationModel::GetInstance()->CreateNewMainViewSceneN(urlData, true);
416                 }
417
418                 if (__pMultiWindowManager != null && __pMultiWindowManager->GetAllWindowArrayList()->GetCount() != 0)
419                 {
420                         //if the application is running in background and it receives an appcontrol request it will launch a new(std::nothrow) window here,
421                         //otherwise appcontrol request will be handled by OnAppInitialized method
422                         SceneManager* pSceneManager = SceneManager::GetInstance();
423                         if (pSceneManager == null)
424                         {
425                                 return;
426                         }
427
428                         ArrayList* pArgList = new(std::nothrow) ArrayList();
429                         pArgList->Construct();
430                         pArgList->Add(*__pNewWindowInfo);
431
432                         if (E_SUCCESS != pSceneManager->GoForward(ForwardSceneTransition(__pNewWindowInfo->sceneID, SCENE_TRANSITION_ANIMATION_TYPE_NONE),pArgList))
433                         {
434                                 AppLogDebug("CustomAppControl::OnAppInitializing Result = %s", GetLastResult());
435                                                 delete pArgList;
436                                 __pNewWindowInfo = null;
437                                 return;
438                         }
439
440                         delete pArgList;
441
442                         __pNewWindowInfo = null;
443                 }
444         }
445         else if (operationId.CompareTo(L"http://tizen.org/appcontrol/operation/main") == 0)
446         {
447                 if (pArgs)
448                 {
449                         //const String* pVal = (String *) pArgs->GetValue(String(L"http://tizen.org/appcontrol/data/notification"));
450                         String *data = (String *) pArgs->GetValue(String(L"http://tizen.org/appcontrol/data/notification"));
451                         if(data != NULL)
452                                 __value = new String(*data);
453                         if (__value && __value->GetLength() > 0)
454                         {
455                                 AppLog("17jul rahul __value is %S", __value->GetPointer());
456                         }
457                         if (__value && __value->GetLength() > 0 && pSceneManagementFrame != null)
458                         {
459                                 AppLog("17jul OnAppControlRequestReceived notification");
460                                 HashMap extraData;
461                                 extraData.Construct();
462                                 String typeKey = APPCONTROL_SELECT_KEY_IMAGE;
463                                 String typeVal = APPCONTROL_SELECT_VALUE_IMAGE;
464                                 extraData.Add(&typeKey, &typeVal);
465
466                                 if(__pAc)
467                                 {
468                                         result r = __pAc->Stop();
469                                         AppLog("AppControl Stop Result %s",GetErrorMessage(r));
470                                         delete __pAc;
471                                         __pAc = null;
472                                 }
473                                 __pAc = AppManager::FindAppControlN(APPCONTROL_PROVIDER_ID_IMAGE_VIEWER,
474                                                 APPCONTROL_OPERATION_ID_IMAGE_VIEWER);
475                                 if (__pAc)
476                                 {
477                                         AppLog("17jul __value is %S", __value->GetPointer());
478                                         if (__pAc->Start(__value, null, null, null) == E_SUCCESS)
479                                         {
480 //                                              delete pAc;
481                                         }
482                                 }
483                                 delete __value;
484                                 __value = null;
485
486                         }
487                 }
488         }
489
490         AppLogDebug("ABC::Internet::OnAppControlRequestReceivedN Exit");
491         return;
492 }
493
494
495 long long
496 InternetApp::GetAvailableMemory(void)
497 {
498     result r = E_SUCCESS;
499
500     String key(L"http://tizen.org/runtime/storage.available.internal");
501     long long allocatedMemory = 0;
502
503     r = RuntimeInfo::GetValue(key, allocatedMemory);
504     TryCatch(r == E_SUCCESS, , "MyRuntimeInfo: Failed to get value");
505
506     return allocatedMemory;
507
508    CATCH:
509     return 0;
510 }
511
512
513 void
514 InternetApp::OnUserEventReceivedN (RequestId requestId, Tizen::Base::Collection::IList *pArgs)
515 {
516         Form* pCurrentForm = null;
517         if(SceneManager::GetInstance()->GetCurrentScene() != null)
518                 pCurrentForm = (SceneManager::GetInstance()->GetCurrentScene()->GetForm());
519         if (pCurrentForm)
520         {
521                 AppLog("sending userevent for appcontrol");
522                 pCurrentForm->SendUserEvent(APPCONTROL_LAUNCH_MAX_LIMIT_REACHED, NULL);
523         }
524
525         if(requestId == FORE_GROUND_APP_EVENT)
526         {
527                 if( MultipleWindowPresentationModel::GetInstance()->GetAllWindowArrayList()->GetCount() >= 9)
528                  {
529                          String strText = CommonUtil::GetString(L"IDS_BR_BODY_COULD_NOT_OPEN_NEW_WINDOW_BECAUSE_YOU_HAVE_ALREADY_OPENED_MAXIMUM_NUMBER");
530                          MessageBox messageBox;
531                          messageBox.Construct(L"", strText, MSGBOX_STYLE_OK, 3000);
532                          int modalResult = 0;
533
534                          messageBox.ShowAndWait(modalResult);
535                  }
536         }
537
538 }
539
540