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