sync with master
[platform/framework/native/appfw.git] / src / app / FApp_AppImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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 /**
19  * @file        FApp_AppImpl.cpp
20  * @brief       This is the implementation for the _AppImpl class.
21  */
22
23 #include <unique_ptr.h>
24
25 #include <notification/notification.h>
26 #include <appsvc/appsvc.h>
27 #include <vconf.h>
28
29 #include <FBaseInteger.h>
30 #include <FBaseColArrayList.h>
31 #include <FBaseColHashMap.h>
32 #include <FBaseRtThread.h>
33 #include <FAppAppRegistry.h>
34 #include <FAppAppControl.h>
35 #include <FAppDataControlProviderManager.h>
36 #include <FAppIAppCheckPointEventListener.h>
37 #include <FAppISqlDataControlProviderEventListener.h>
38 #include <FAppIMapDataControlProviderEventListener.h>
39 #include <FAppIAppControlProviderEventListener.h>
40 #include <FAppIAppLaunchConditionEventListener.h>
41 #include <FIoFile.h>
42
43 #include <FBaseSysLog.h>
44 #include <FIo_RegistryImpl.h>
45 #include <FSys_EnvironmentImpl.h>
46 #include <FSys_PowerManagerImpl.h>
47 #include <FApp_Aul.h>
48
49 #include "FApp_IAppManager.h"
50 #include "FApp_AppManagerProxy.h"
51 #include "FApp_AppImpl.h"
52 #include "FApp_AppInfo.h"
53 #include "FApp_AppControlManager.h"
54 #include "FApp_AppArg.h"
55 #include "FApp_IAppImpl.h"
56 #include "FApp_AppResourceImpl.h"
57
58 using namespace Tizen::Base;
59 using namespace Tizen::Base::Collection;
60 using namespace Tizen::Base::Runtime;
61 using namespace Tizen::Io;
62 using namespace Tizen::System;
63
64 namespace
65 {
66
67 const int HEARTBEAT_WAIT_COUNT = 3;
68 const wchar_t SERVICE_APPID[] = L"aospd00043.osp-app-service";
69 const char SELECTOR_RESPONSE_KEY[] = "__APP_SVC_START_INFO__";
70 #ifndef VCONFKEY_APPSERVICE_STATUS
71 #define VCONFKEY_APPSERVICE_STATUS  "memory/appservice/status"
72 #endif
73
74 }
75
76 namespace Tizen { namespace App
77 {
78
79 _AppImpl* _AppImpl::__pAppImpl = null;
80 bool _AppImpl::__isTerminationRequested = false;
81 static const int _DATACONTROL_PACKET_INDEX_APPID = 0;
82 static const int _DATACONTROL_PACKET_INDEX_REQUESTTYPE = 1;
83 static const int _DATACONTROL_PACKET_INDEX_REQID = 2;
84 static const int _DATACONTROL_PACKET_INDEX_PROVIDERID = 3;
85 static const int _DATACONTROL_PACKET_INDEX_DATAID = 4;
86 static const int _DATACONTROL_PACKET_INDEX_COLUMNCOUNT = 5;
87 static const int _DATACONTROL_PACKET_INDEX_DELETEWHERE = 5;
88 static const int _DATACONTROL_PACKET_INDEX_COLUMNLIST = 6;
89 static const int _DATACONTROL_PACKET_INDEX_INSERTMAP = 6;
90 static const int _DATACONTROL_PACKET_INDEX_UPDATEMAP = 6;
91
92 _AppImpl::_AppImpl(App* pApp)
93         : __pCheckpointEventListener(null)
94         , __pSqlDataControlProviderEventListener(null)
95         , __pMapDataControlProviderEventListener(null)
96         , __pAppControlProviderEventListener(null)
97         , __pAppControlProviderInternalEventListener(null)
98         , __pAppLaunchConditionEventListener(null)
99         , __pApp(pApp)
100         , __pIAppImpl(null)
101         , __pRequestHandler(&_AppImpl::HandleAppRequest)
102         , __pLegacyRequestHandler(&_AppImpl::HandleDummyAppRequest)
103         , __forcedTermination(false)
104 {
105         __pAppImpl = this;
106 }
107
108
109 result
110 _AppImpl::Construct(const IList* pArgs)
111 {
112         SysTryReturnResult(NID_APP, pArgs != null, E_INVALID_ARG, "pArguments must not be null.");
113         SysAssertf(__pApp != null, "Getting App instance failed.");
114
115         result r = __appUserEvent.Construct();
116         SysTryLog(NID_APP, !IsFailed(r), "[%s] User event handler failure.", GetErrorMessage(r));
117
118         __appUserEvent.AddListener(*this);
119
120         _AppInfo::SetAppState(INITIALIZING);
121
122         return E_SUCCESS;
123 }
124
125
126 result
127 _AppImpl::Execute(_IAppImpl* pIAppImpl)
128 {
129         SysLogTag(NID_APP, "LAUNCH","[%ls:<Initialize Application>:start]", _AppInfo::GetAppExecutableName().GetPointer());
130         result r = E_SUCCESS;
131         int eflResult = APP_ERROR_NONE;
132         int argc = _AppInfo::GetArgc();
133         char** pArgv = _AppInfo::GetArgv();
134         _IAppManager* pAppManager = null;
135
136         _AppImpl* pAppImpl = _AppImpl::GetInstance();
137         SysTryReturnResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
138         pAppImpl->__pIAppImpl = pIAppImpl;
139
140         // handle legacy version handling
141         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
142         {
143                 pAppImpl->SetLegacyRequestHandler();
144         }
145
146         // clear previous notification
147         // [INFO] behavior change : platform does not clear previous notification
148         notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_NOTI);
149
150         app_event_callback_s state_handler;
151         memset(&state_handler, 0, sizeof(app_event_callback_s));
152
153         state_handler.create = &OnCreate;
154         state_handler.service = &OnService;
155         state_handler.terminate = &OnTerminate;
156         state_handler.pause = &OnPause;
157         state_handler.resume = &OnResume;
158         state_handler.device_orientation = &OnDeviceOrientationChanged;
159
160         state_handler.low_memory = &OnLowMemory;
161         state_handler.low_battery = &OnLowBattery;
162         state_handler.language_changed = &OnLanguageChanged;
163         state_handler.region_format_changed = &OnRegionFormatChanged;
164
165         _Aul::SetPowerOffNotiListener(OnPowerOffNotiReceived, this);
166 //      if (pAppManager != null)
167 //      {
168 //              r = pAppManager->RegisterApplication(_AppInfo::GetPackageId(), _AppInfo::GetAppExecutableName(), _AppInfo::GetAppType() , _AppInfo::GetProcessId());
169 //              SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Application registration failed.", GetErrorMessage(r));
170 //      }
171
172         eflResult = app_efl_main(&argc, &pArgv, &state_handler, this);
173
174         SysTryLog(NID_APP, eflResult == APP_ERROR_NONE, "app_efl_main failed with error (%d): Unknown", eflResult);
175
176         // clear outstanding ongoing notification
177         notification_delete_all_by_type(NULL, NOTIFICATION_TYPE_ONGOING);
178
179 //CATCH:
180         pAppManager = _AppManagerProxy::GetService();
181
182         if (pAppManager != null)
183         {
184                 pAppManager->UnregisterApplication(_AppInfo::GetProcessId());
185         }
186
187         return r;
188 }
189
190
191 bool
192 _AppImpl::OnCreate(void* user_data)
193 {
194         SysLog(NID_APP, "Platform creation event.");
195
196         _AppImpl* pAppImpl = static_cast<_AppImpl*>(user_data);
197         if (pAppImpl == null)
198         {
199                 _AppInfo::SetAppState(TERMINATED);
200                 SysLogException(NID_APP, E_INIT_FAILED, "[E_INIT_FAILED] Invalid platform state.");
201                 return false;
202         }
203
204         _AppInfo::SetAppState(INITIALIZING);
205
206         if (!ConfirmAppService())
207         {
208                 _AppInfo::SetAppState(TERMINATED);
209                 SysLogException(NID_APP, E_INIT_FAILED, "[E_INIT_FAILED] Osp AppService is not running.");
210                 return false;
211         }
212
213         SysLogTag(NID_APP, "LAUNCH","[%ls:<OnAppInitializing>:start]", _AppInfo::GetAppExecutableName().GetPointer());
214         if (!pAppImpl->__pIAppImpl->OnAppInitializing())
215         {
216                 _AppInfo::SetAppState(TERMINATED);
217                 SysLogException(NID_APP, E_INIT_FAILED, "[E_INIT_FAILED] The initialization of your application failed.");
218                 return false;
219         }
220         SysLogTag(NID_APP, "LAUNCH","[%ls:<OnAppInitializing>:done]", _AppInfo::GetAppExecutableName().GetPointer());
221
222         SysLogTag(NID_APP, "LAUNCH","[%ls:<RegisterApplication>:start]", _AppInfo::GetAppExecutableName().GetPointer());
223         _IAppManager* pAppManager = _AppManagerProxy::GetService();
224
225         if (pAppManager != null)
226         {
227                 result r = pAppManager->RegisterApplication(_AppInfo::GetPackageId(), _AppInfo::GetAppExecutableName(), static_cast<_AppType>(_AppInfo::GetAppType()), _AppInfo::GetProcessId());
228                 SysTryLog(NID_APP, !IsFailed(r), "[%s] Application registration failed.", GetErrorMessage(r));
229         }
230         SysLogTag(NID_APP, "LAUNCH","[%ls:<RegisterApplication>:done]", _AppInfo::GetAppExecutableName().GetPointer());
231         // hide splash window
232
233         _PowerManagerImpl::InitBatteryEvent();
234
235         return true;
236 }
237
238
239 void
240 _AppImpl::OnService(service_s* service, void* user_data)
241 {
242         _AppImpl* pAppImpl = _AppImpl::GetInstance();
243         SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
244         SysTryReturnVoidResult(NID_APP, pAppImpl->__pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
245
246         // ugly code for app selector event handling
247         bundle* pBundle = _AppArg::GetBundleFromSvc(service);
248         if (pBundle)
249         {
250                 const char* pValue = appsvc_get_data(pBundle, SELECTOR_RESPONSE_KEY);
251                 if (pValue)
252                 {
253                         const int req = _AppArg::GetRequestIdFromBundle(pBundle);
254
255                         const char* pOperation = appsvc_get_operation(pBundle);
256
257                         SysLog(NID_APP, "App selector response [%s, %s], req(%d).", pValue, pOperation, req);
258
259                         //_AppArg::Print(pBundle);
260                         _AppControlManager::GetInstance()->SendAppControlStartResponse(req, pValue, pOperation);
261                         return;
262                 }
263         }
264
265         //_AppArg::Print(pBundle);
266         int req = -1;
267         _AppHandler handler = _APP_HANDLER_NONE;
268         pAppImpl->RegisterAppRequest(service, req, handler);
269
270         bool initialLaunch = false;
271
272         // initial launch
273         // (1) AppControl / DataControl provider listener
274         // (2) OnAppInitialized()
275         // (3) OnResume() (OnForeground()) (UiApp only)
276         // (4) OnUserEventReceivedN()
277
278         // already launched
279         // (1) AppControl / DataControl provider listener
280         // (2) OnResume() (OnForeground()) (UiApp only)
281         // (3) OnUserEventReceivedN()
282
283         if (_AppInfo::GetAppState() == INITIALIZING)
284         {
285                 initialLaunch = true;
286
287                 unsigned int winId = 0;
288                 service_get_window(service, &winId);
289                 if (winId > 0)
290                 {
291                         _AppInfo::GetAppInfo()->SetParentWindowHandle(winId);
292                         SysLog(NID_APP, "Saving window handle 0x%x.", winId);
293                 }
294                 // call for callbacks
295                 // ptr to member function
296                 SysLogTag(NID_APP, "LAUNCH","[%ls:<RequestHandler and OnAppInitialized>:start]", _AppInfo::GetAppExecutableName().GetPointer());
297                 (pAppImpl->*pAppImpl->__pRequestHandler)(service, req, handler);
298
299                 if (!pAppImpl->__pIAppImpl->OnAppInitialized())
300                 {
301                         _AppInfo::SetAppState(TERMINATING);
302                         SysLog(NID_APP, "[E_INIT_FAILED] The Initialization of your application failed.");
303                         app_efl_exit();
304                         return;
305                 }
306                 SysLogTag(NID_APP, "LAUNCH","[%ls:<RequestHandler and OnAppInitialized>:done]", _AppInfo::GetAppExecutableName().GetPointer());
307
308                 _AppInfo::SetAppState(RUNNING);
309
310                 SysLogTag(NID_APP, "LAUNCH","[%ls:<Initialize Application>:done]", _AppInfo::GetAppExecutableName().GetPointer());
311         }
312         else
313         {
314                 // already running
315
316                 // call for callbacks
317                 // ptr to member function
318                 (pAppImpl->*pAppImpl->__pRequestHandler)(service, req, handler);
319         }
320
321         pAppImpl->__pIAppImpl->OnService(service, initialLaunch);
322
323         // ptr to member function
324         (pAppImpl->*pAppImpl->__pLegacyRequestHandler)(service, req, handler);
325 }
326
327
328 void
329 _AppImpl::OnTerminate(void* user_data)
330 {
331         SysLog(NID_APP, "Termination event 0x%x state", _AppInfo::GetAppState());
332
333         _AppImpl* pAppImpl = _AppImpl::GetInstance();
334         SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
335         SysTryReturnVoidResult(NID_APP, pAppImpl->__pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
336
337         __isTerminationRequested = true;
338         pAppImpl->__pIAppImpl->OnTerminate();
339 }
340
341
342 void
343 _AppImpl::OnResume(void* user_data)
344 {
345         SysLog(NID_APP, "System resume event on 0x%x state", _AppInfo::GetAppState());
346         SysLogTag(NID_APP, "LAUNCH","[%ls:<OnResume>:start]", _AppInfo::GetAppExecutableName().GetPointer());
347
348         _AppImpl* pAppImpl = _AppImpl::GetInstance();
349         SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
350         SysTryReturnVoidResult(NID_APP, pAppImpl->__pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
351         pAppImpl->__pIAppImpl->OnResume();
352         SysLogTag(NID_APP, "LAUNCH","[%ls:<OnResume>:done]", _AppInfo::GetAppExecutableName().GetPointer());
353 }
354
355
356 void
357 _AppImpl::OnPause(void* user_data)
358 {
359         SysLog(NID_APP, "System pause event on 0x%x state", _AppInfo::GetAppState());
360         SysLogTag(NID_APP, "LAUNCH","[%ls:<OnPause>:start]", _AppInfo::GetAppExecutableName().GetPointer());
361
362         _AppImpl* pAppImpl = _AppImpl::GetInstance();
363         SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
364         SysTryReturnVoidResult(NID_APP, pAppImpl->__pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
365         pAppImpl->__pIAppImpl->OnPause();
366         pAppImpl->OnAppCheckpoint();
367         SysLogTag(NID_APP, "LAUNCH","[%ls:<OnPause>:done]", _AppInfo::GetAppExecutableName().GetPointer());
368 }
369
370
371 void
372 _AppImpl::OnDeviceOrientationChanged(app_device_orientation_e orientation, void* user_data)
373 {
374         _AppImpl* pAppImpl = _AppImpl::GetInstance();
375         SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
376         SysTryReturnVoidResult(NID_APP, pAppImpl->__pIAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
377
378         const AppState state = _AppInfo::GetAppState();
379         SysTryReturnVoidResult(NID_APP, state == INITIALIZING || state == RUNNING, E_INVALID_STATE, "[E_INVALID_STATE] Invalid application state : %d.", state);
380
381         pAppImpl->__pIAppImpl->OnDeviceOrientationChanged(orientation);
382 }
383
384
385 _AppImpl::~_AppImpl(void)
386 {
387         __pAppImpl = null;
388 }
389
390
391 _AppImpl*
392 _AppImpl::GetInstance(void)
393 {
394         return __pAppImpl;
395 }
396
397
398 App*
399 _AppImpl::GetAppInstance(void)
400 {
401         return __pApp;
402 }
403
404
405 IList*
406 _AppImpl::GetAppArgumentListN(void) const
407 {
408         ArrayList* pList = null;
409         const _AppArg* pArg = _AppControlManager::GetInstance()->FindResultRequest(0);
410
411         if (pArg)
412         {
413                 pList = pArg->GetArgListN(0);
414         }
415         else
416         {
417                 // fallback
418                 pList = new (std::nothrow) ArrayList();
419                 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
420                 String* pData = new (std::nothrow) String(LEGACY_LAUNCH_REASON_NORMAL);
421                 SysTryReturn(NID_APP, pData != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
422                 String* pData2 = new (std::nothrow) String(L"osp.operation.MAIN");
423                 SysTryReturn(NID_APP, pData2 != null, null, E_OUT_OF_MEMORY, "ArrayList creation failure.");
424
425                 pList->Construct();
426                 pList->Add(*pData);
427                 pList->Add(*pData2);
428         }
429
430         return pList;
431 }
432
433
434 result
435 _AppImpl::Terminate(void)
436 {
437         result r = E_SUCCESS;
438
439         __isTerminationRequested = true;
440
441         app_efl_exit();
442         return r;
443 }
444
445
446 bool
447 _AppImpl::OnAppInitializing(void)
448 {
449         return false;
450 }
451
452
453 bool
454 _AppImpl::OnAppWillTerminate(void)
455 {
456         SysTryReturn(NID_APP, __pApp != null, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
457
458         return __pApp->OnAppWillTerminate();
459 }
460
461
462 result
463 _AppImpl::RegisterAppRequest(service_s* service, int& req, _AppHandler& handler)
464 {
465         _AppControlManager* pManager = _AppControlManager::GetInstance();
466         SysTryReturnResult(NID_APP, pManager != null, E_SYSTEM, "Wrong system state.");
467
468         result r = pManager->RegisterRequest(service, req, handler);
469         SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
470
471         int providerType = _AppInfo::GetAppHandlerType();
472         providerType |= (_APP_HANDLER_LAUNCH_NORMAL | _APP_HANDLER_LAUNCH_COND);
473
474         SysTryLog(NID_APP, (handler & providerType) != 0, "Request 0x%x received for 0x%x.", handler,
475                         _AppInfo::GetAppHandlerType());
476         return E_SUCCESS;
477 }
478
479
480 result
481 _AppImpl::HandleAppRequest(service_s* service, int req, _AppHandler handler)
482 {
483         const _AppArg* pArg = _AppControlManager::GetInstance()->FindResultRequest(req);
484         SysTryReturnResult(NID_APP, pArg != null, E_SYSTEM, "No argument found for %d.", req);
485
486         pArg->Print();
487
488         switch (handler)
489         {
490         case _APP_HANDLER_DATACONTROL:
491                 SysLog(NID_APP, "Handling DataControl request");
492
493                 OnDataControlRequestReceived(*pArg, static_cast< RequestId >(req));
494
495                 _AppControlManager::GetInstance()->RemoveResultRequest(req);
496                 break;
497
498         case _APP_HANDLER_LAUNCH_COND:
499                 SysLog(NID_APP, "Handling conditional launch request");
500
501                 OnConditionRequestReceived(*pArg, static_cast< RequestId >(req));
502                 break;
503
504         default:
505                 SysLog(NID_APP, "Handling AppControl request");
506
507                 OnAppControlRequestReceived(*pArg, static_cast< RequestId >(req));
508                 break;
509         }
510
511         return E_SUCCESS;
512 }
513
514
515 result
516 _AppImpl::HandleLegacyAppRequest(service_s* service, int req, _AppHandler handler)
517 {
518         const _AppArg* pArg = _AppControlManager::GetInstance()->FindResultRequest(0);
519         SysTryReturnResult(NID_APP, pArg != null, E_SYSTEM, "No argument found for %d.", req);
520
521         //pArg->Print();
522
523         switch (handler)
524         {
525         case _APP_HANDLER_LAUNCH_NORMAL:
526                 SysLog(NID_APP, "Handling normal launch request");
527                 OnUserEventReceivedN(AppLaunchRequestId, pArg->GetArgListN(req));
528
529                 // [TODO] request handle memory cleanup confirm
530                 _AppControlManager::GetInstance()->RemoveResultRequest(req);
531                 break;
532
533         case _APP_HANDLER_LAUNCH_COND:
534                 SysLog(NID_APP, "Handling conditional launch request");
535
536                 OnUserEventReceivedN(AppLaunchRequestId, pArg->GetArgListN(req));
537
538                 // [TODO] request handle memory cleanup confirm
539                 _AppControlManager::GetInstance()->RemoveResultRequest(req);
540                 break;
541
542         default:
543                 break;
544         }
545
546         return E_SUCCESS;
547 }
548
549
550 result
551 _AppImpl::HandleDummyAppRequest(service_s* service, int req, _AppHandler handler)
552 {
553         SysLog(NID_APP, "Dummy handler");
554
555         return E_SUCCESS;
556 }
557
558
559 result
560 _AppImpl::SetLegacyRequestHandler(void)
561 {
562         SysLog(NID_APP, "Setting request handler for legacy application.");
563
564         __pRequestHandler = &_AppImpl::HandleDummyAppRequest;
565         __pLegacyRequestHandler = &_AppImpl::HandleLegacyAppRequest;
566
567         return E_SUCCESS;
568 }
569
570
571 void
572 _AppImpl::OnLowMemory(void* user_data)
573 {
574         _AppImpl* pAppImpl = _AppImpl::GetInstance();
575
576         SysTryReturnVoidResult(NID_APP, pAppImpl != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
577         SysTryReturnVoidResult(NID_APP, pAppImpl->__pApp != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
578
579         if (_AppInfo::GetAppState() == RUNNING)
580         {
581                 pAppImpl->__pApp->OnLowMemory();
582         }
583 }
584
585
586 void
587 _AppImpl::OnLowBattery(void* user_data)
588 {
589         // Osp doesn't implement this callback.
590 }
591
592 void
593 _AppImpl::OnBatteryLevelChanged(BatteryLevel batteryLevel)
594 {
595         SysTryReturnVoidResult(NID_APP, __pApp != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
596
597         __pApp->OnBatteryLevelChanged(batteryLevel);
598 }
599
600 void
601 _AppImpl::OnLanguageChanged(void* user_data)
602 {
603         SysLog(NID_APP, "");
604         _AppInfo::UpdatePackageInfo(true);
605
606         _AppResourceImpl::Reinitialize();
607         SysLog(NID_APP, "Language change done.");
608 }
609
610 void
611 _AppImpl::OnRegionFormatChanged(void* user_data)
612 {
613         // Osp doesn't implement this callback.
614 }
615
616
617 result
618 _AppImpl::SendUserEvent(RequestId requestId, const IList* pArgs)
619 {
620         _AppUserEventArg* pArg = new (std::nothrow) _AppUserEventArg(requestId, pArgs);
621         SysTryReturnResult(NID_APP, pArg != null, E_OUT_OF_MEMORY, "User event creation failure for %d.", requestId);
622
623         result r = __appUserEvent.FireAsync(*pArg);
624
625         return r;
626 }
627
628
629 void
630 _AppImpl::OnUserEventReceivedN(RequestId requestId, IList* pArgs)
631 {
632         SysTryReturnVoidResult(NID_APP, __pApp != null, E_INVALID_STATE, "[E_INVALID_STATE] Getting internal instance failed.");
633
634         __pApp->OnUserEventReceivedN(requestId, pArgs);
635 }
636
637
638 void
639 _AppImpl::OnAppControlRequestReceived(const _AppArg& arg, RequestId reqId)
640 {
641         IAppControlProviderEventListener* pAppControlProviderEventListener = null;
642
643         const String internalOperation = arg.GetValue(OSP_K_APPCONTROL_INTERNAL_OPERATION);
644         if( !internalOperation.IsEmpty() )
645         {
646                 SysLog(NID_APP, "internalOperation:%ls", internalOperation.GetPointer() );
647                 pAppControlProviderEventListener = __pAppControlProviderInternalEventListener;
648         }
649         else
650         {
651                 pAppControlProviderEventListener = __pAppControlProviderEventListener;
652         }
653
654         if( pAppControlProviderEventListener == null)
655         {
656                 SysLog(NID_APP, "No AppControl provider event listener set for request %d.", reqId);
657                 return;
658         }
659
660         const String& appId = _AppInfo::GetApplicationId();
661
662         const char* p = appsvc_get_operation(arg.GetBundle());
663         String operationId = (p) ? String(p) : TIZEN_OPERATION_MAIN;
664         if (operationId == L"http://tizen.org/appcontrol/operation/default")
665         {
666                 operationId = TIZEN_OPERATION_MAIN;
667         }
668
669         SysLog(NID_APP, "AppControl (%ls, %ls).", appId.GetPointer(), operationId.GetPointer());
670
671         String uri;
672         String mime;
673         const String* pUri = null;
674         const String* pMime = null;
675         p = appsvc_get_uri(arg.GetBundle());
676         if (p)
677         {
678                 uri = p;
679                 pUri = &uri;
680
681                 SysLog(NID_APP, "Delivered uri is [%ls].", pUri->GetPointer());
682         }
683
684         p = appsvc_get_mime(arg.GetBundle());
685         if (p)
686         {
687                 mime = p;
688                 pMime = &mime;
689         }
690
691         std::unique_ptr<HashMap> pMap(arg.GetArgMapN());
692
693         pAppControlProviderEventListener->OnAppControlRequestReceived(reqId, operationId, pUri, pMime, pMap.get());
694
695         SysLog(NID_APP, "AppControl (%ls, %ls) invocation finished.", appId.GetPointer(), operationId.GetPointer());
696 }
697
698
699 void
700 _AppImpl::OnDataControlRequestReceived(const _AppArg& arg, RequestId reqId)
701 {
702         String tempFilePath;
703         ISqlDataControlProviderEventListener* pSqlListener = null;
704         IMapDataControlProviderEventListener* pMapListener = null;
705         ArrayList* pColumnList = null;
706         HashMap* pInsertMap = null;
707         HashMap* pUpdateMap = null;
708         String* pAppId = null;
709         String* pRequestType = null;
710         String* pProviderId = null;
711         String* pDataId = null;
712         String* pReqId = null;
713         String* pColumnCount = null;
714         String* pColumn = null;
715         String* pValue = null;
716         String* pWhere = null;
717         String* pOrder = null;
718         String* pErrorMsg = null;
719         String* pNo = null;
720         String* pCount = null;
721         String empty(L"NULL");
722 #if 0
723         _DataControlResultSetImpl* pResultSetImpl = null;
724         String* pResult = null;
725         String* pObject = null;
726         String* pTempObject = null;
727         String* pTempFilePath = null;
728 #endif
729         _AppArg resultArg;
730         int type = 0;
731         _DataControlRequestType requestType = _DATACONTROL_REQUEST_TYPE_UNDEFINED;
732         int columnCount = 0;
733         int pageNo = 0;
734         int countPerPage = 0;
735         int i = 0;
736         int index = 0;
737         //ArrayList* pResultArgList = null;
738         result r = E_SUCCESS;
739         result res = E_SUCCESS;
740         ArrayList* pList = null;
741         String* pKey = null;
742         String* pNewValue = null;
743         String* pOldValue = null;
744 #if 0
745         String* pResultCount = null;
746         String* pResultValue = null;
747 #endif
748
749         SysLog(NID_APP, "[DC_PROVIDER] Data control request is received.");
750         //arg.Print();
751
752         pList = arg.GetArgListN(0);
753         SysTryCatch(NID_APP, pList, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
754
755         // key-based
756         pAppId = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_APPID)); // key(0)
757         SysTryCatch(NID_APP, pAppId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
758
759         pRequestType = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_REQUESTTYPE)); // key(1)
760         SysTryCatch(NID_APP, pRequestType, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
761         Integer::Parse(*pRequestType, type);
762         requestType = static_cast< _DataControlRequestType >(type);
763
764         pReqId = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_REQID)); // key(2)
765         SysTryCatch(NID_APP, pReqId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
766
767         pProviderId = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_PROVIDERID)); // key(3)
768         SysTryCatch(NID_APP, pProviderId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
769
770         // list-based
771         pDataId = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_DATAID));// list(0): data ID
772         SysTryCatch(NID_APP, pDataId, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
773
774         if (requestType >= _DATACONTROL_REQUEST_TYPE_SQL_QUERY && requestType <= _DATACONTROL_REQUEST_TYPE_SQL_DELETE)
775         {
776                 pSqlListener = __pSqlDataControlProviderEventListener;
777                 SysTryCatch(NID_APP, pSqlListener != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
778         }
779         else if (requestType >= _DATACONTROL_REQUEST_TYPE_MAP_QUERY && requestType <= _DATACONTROL_REQUEST_TYPE_MAP_DELETE)
780         {
781                 pKey = dynamic_cast< String* >(pList->GetAt(5)); // list[1]
782                 SysTryCatch(NID_APP, pKey, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
783
784                 pMapListener = __pMapDataControlProviderEventListener;
785                 SysTryCatch(NID_APP, pMapListener != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
786         }
787         else
788         {
789                 SysLogException(NID_APP, E_SYSTEM, "[E_SYSTEM] invalid request");
790                 r = E_SYSTEM;
791         }
792
793         switch (requestType)
794         {
795         case _DATACONTROL_REQUEST_TYPE_SQL_QUERY:
796                 SysLog(NID_APP, "[DC_PROVIDER] SqlDataControl SELECT");
797
798                 // list(1): selected column count
799                 pColumnCount = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNCOUNT));
800                 SysTryCatch(NID_APP, pColumnCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
801
802                 if (pColumnCount->Equals(empty) == true)
803                 {
804                         pColumnList = null;
805                 }
806                 else
807                 {
808                         Integer::Parse(*pColumnCount, columnCount);
809                         pColumnList = new (std::nothrow) ArrayList();
810                         SysTryReturnVoidResult(NID_APP, pColumnList, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
811                         pColumnList->Construct();
812                         SysLog(NID_APP, "[DC_PROVIDER] selected column count: %d", columnCount);
813
814                         i = 0;
815                         while (i < columnCount) // list(2): column list
816                         {
817                                 pColumn = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNLIST + i));
818                                 SysTryCatch(NID_APP, pColumn != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
819
820                                 pColumnList->Add(*(new String(*pColumn)));
821                                 SysLog(NID_APP, "[DC_PROVIDER] column[%d]: %ls", i, pColumn->GetPointer());
822                                 i++;
823                         }
824                 }
825
826                 i += _DATACONTROL_PACKET_INDEX_COLUMNLIST;
827                 pWhere = dynamic_cast< String* >(pList->GetAt(i)); // list(3): where clause
828                 SysTryCatch(NID_APP, pWhere, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
829
830                 if (pWhere->Equals(empty) == true)
831                 {
832                         pWhere = null;
833                 }
834                 else
835                 {
836                         SysLog(NID_APP, "[DC_PROVIDER] pWhere: %ls", pWhere->GetPointer());
837                 }
838
839                 i++;
840                 pOrder = dynamic_cast< String* >(pList->GetAt(i)); // list(4): order clause
841                 SysTryCatch(NID_APP, pOrder, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
842
843                 if (pOrder->Equals(empty) == true)
844                 {
845                         pOrder = null;
846                 }
847                 else
848                 {
849                         SysLog(NID_APP, "[DC_PROVIDER] pOrder: %ls", pOrder->GetPointer());
850                 }
851
852                 i++;
853                 pNo = dynamic_cast <String*>(pList->GetAt(i)); // list(5): page number
854                 SysTryCatch(NID_APP, pNo, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
855                 Integer::Parse(*pNo, pageNo);
856
857                 i++;
858                 pCount = dynamic_cast <String*>(pList->GetAt(i)); // list(6): count per page
859                 SysTryCatch(NID_APP, pCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
860                 Integer::Parse(*pCount, countPerPage);
861
862                 pSqlListener->OnSqlDataControlSelectRequestReceived(reqId, *pProviderId, *pDataId, pColumnList,
863                                 pWhere, pOrder);
864
865                 SysLog(NID_APP, "[DC_PROVIDER] caller appId: %ls, requestType: %d, reqId: %ls, providerId: %ls, dataId: %ls, pColumnList: 0x%0x, pWhere: 0x%x, pOrder: 0x%x, pageNo: %d, countPerPage: %d",
866                                 pAppId->GetPointer(), requestType, pReqId->GetPointer(), pProviderId->GetPointer(),
867                                 pDataId->GetPointer(), pColumnList, pWhere, pOrder, pageNo, countPerPage);
868                 break;
869         case _DATACONTROL_REQUEST_TYPE_SQL_INSERT:
870                 SysLog(NID_APP, "[DC_PROVIDER] SqlDataControl INSERT");
871
872                 // list(1): insert column count
873                 pColumnCount = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNCOUNT));
874                 SysTryCatch(NID_APP, pColumnCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
875
876                 Integer::Parse(*pColumnCount, columnCount);
877                 SysLog(NID_APP, "[DC_PROVIDER] inserted column count: %d", columnCount);
878
879                 pInsertMap = new (std::nothrow) HashMap();
880                 SysTryReturnVoidResult(NID_APP, pInsertMap, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
881                 pInsertMap->Construct();
882
883                 i = 0;
884                 index = 0;
885                 while (i < columnCount * 2) // list(2): column-value pairs
886                 {
887                         pColumn = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_INSERTMAP + i++));
888                         SysTryCatch(NID_APP, pColumn != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
889                         SysLog(NID_APP, "[DC_PROVIDER] column[%d]: %ls", index, pColumn->GetPointer());
890
891                         pValue = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_INSERTMAP + i++));
892                         SysTryCatch(NID_APP, pValue != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
893                         SysLog(NID_APP, "[DC_PROVIDER] value[%d]: %ls", index, pValue->GetPointer());
894
895                         pInsertMap->Add(*(new String(*pColumn)), *(new String(*pValue)));
896                         index++;
897                 }
898
899                 pSqlListener->OnSqlDataControlInsertRequestReceived(reqId, *pProviderId, *pDataId, *pInsertMap);
900
901                 SysLog(NID_APP, "[DC_PROVIDER] caller appId: %ls, requestType: %d, reqId: %ls, providerId: %ls, dataId: %ls, pInsertMap: 0x%x",
902                                 pAppId->GetPointer(), requestType, pReqId->GetPointer(), pProviderId->GetPointer(),
903                                 pDataId->GetPointer(), pInsertMap);
904                 break;
905         case _DATACONTROL_REQUEST_TYPE_SQL_UPDATE:
906                 SysLog(NID_APP, "[DC_PROVIDER] SqlDataControl UPDATE");
907
908                 // list(1): updated column count
909                 pColumnCount = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNCOUNT));
910                 SysTryCatch(NID_APP, pColumnCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
911
912                 Integer::Parse(*pColumnCount, columnCount);
913                 SysLog(NID_APP, "[DC_PROVIDER] updated column count: %d", columnCount);
914
915                 pUpdateMap = new (std::nothrow) HashMap();
916                 SysTryReturnVoidResult(NID_APP, pUpdateMap, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
917                 pUpdateMap->Construct();
918
919                 i = 0;
920                 index = 0;
921                 while (i < columnCount * 2) // list(2): column-value pairs
922                 {
923                         pColumn = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_UPDATEMAP + i++));
924                         SysTryCatch(NID_APP, pColumn != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
925                         SysLog(NID_APP, "[DC_PROVIDER] column[%d]: %ls", index, pColumn->GetPointer());
926
927                         pValue = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_UPDATEMAP + i++));
928                         SysTryCatch(NID_APP, pValue != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
929                         SysLog(NID_APP, "[DC_PROVIDER] value[%d]: %ls", index, pValue->GetPointer());
930
931                         pUpdateMap->Add(*(new String(*pColumn)), *(new String(*pValue)));
932                         index++;
933                 }
934
935                 // list(3): where clause
936                 pWhere = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_UPDATEMAP + i));
937                 SysTryCatch(NID_APP, pWhere, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
938
939                 if (pWhere->Equals(empty) == true)
940                 {
941                         pWhere = null;
942                 }
943                 else
944                 {
945                         SysLog(NID_APP, "[DC_PROVIDER] pWhere: %ls", pWhere->GetPointer());
946                 }
947
948                 pSqlListener->OnSqlDataControlUpdateRequestReceived(reqId, *pProviderId, *pDataId, *pUpdateMap, pWhere);
949
950                 SysLog(NID_APP, "[DC_PROVIDER] caller appId: %ls, requestType: %d, reqId: %ls, providerId: %ls, dataId: %ls, pUpdateMap: 0x%x, pWhere: 0x%x",
951                                 pAppId->GetPointer(), requestType, pReqId->GetPointer(), pProviderId->GetPointer(),
952                                 pDataId->GetPointer(), pUpdateMap, pWhere);
953                 break;
954         case _DATACONTROL_REQUEST_TYPE_SQL_DELETE:
955                 SysLog(NID_APP, "[DC_PROVIDER] SqlDataControl DELETE");
956
957                 // list(1): where clause
958                 pWhere = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_DELETEWHERE));
959                 SysTryCatch(NID_APP, pWhere, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
960
961                 if (pWhere->Equals(empty) == true)
962                 {
963                         pWhere = null;
964                 }
965                 else
966                 {
967                         SysLog(NID_APP, "[DC_PROVIDER] pWhere: %ls", pWhere->GetPointer());
968                 }
969
970                 pSqlListener->OnSqlDataControlDeleteRequestReceived(reqId, *pProviderId, *pDataId, pWhere);
971
972                 SysLog(NID_APP, "[DC_PROVIDER] caller appId: %ls, requestType: %d, reqId: %ls, providerId: %ls, dataId: %ls, pWhere: 0x%x",
973                                 pAppId->GetPointer(), requestType, pReqId->GetPointer(), pProviderId->GetPointer(),
974                                 pDataId->GetPointer(), pWhere);
975                 break;
976         case _DATACONTROL_REQUEST_TYPE_MAP_QUERY:
977                 pNo = dynamic_cast <String*>(pList->GetAt(6)); // list[2]
978                 SysTryCatch(NID_APP, pNo, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
979                 Integer::Parse(*pNo, pageNo);
980
981                 pCount = dynamic_cast <String*>(pList->GetAt(7)); //list[3]
982                 SysTryCatch(NID_APP, pCount, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
983                 Integer::Parse(*pCount, countPerPage);
984
985                 pMapListener->OnMapDataControlGetValueRequestReceived(reqId, *pProviderId, *pDataId, *pKey);
986                 break;
987         case _DATACONTROL_REQUEST_TYPE_MAP_INSERT:
988                 pValue = dynamic_cast <String*>(pList->GetAt(6)); // list[2]
989                 SysTryCatch(NID_APP, pValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
990
991                 pMapListener->OnMapDataControlAddValueRequestReceived(reqId, *pProviderId, *pDataId, *pKey, *pValue);
992                 break;
993         case _DATACONTROL_REQUEST_TYPE_MAP_UPDATE:
994                 pOldValue = dynamic_cast <String*>(pList->GetAt(6)); // list[2]
995                 SysTryCatch(NID_APP, pOldValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
996
997                 pNewValue = dynamic_cast <String*>(pList->GetAt(7)); // list[3]
998                 SysTryCatch(NID_APP, pNewValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
999
1000                 pMapListener->OnMapDataControlSetValueRequestReceived(reqId, *pProviderId, *pDataId, *pKey, *pOldValue, *pNewValue);
1001                 break;
1002         case _DATACONTROL_REQUEST_TYPE_MAP_DELETE:
1003                 pValue = dynamic_cast <String*>(pList->GetAt(6)); // list[2]
1004                 SysTryCatch(NID_APP, pValue, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1005
1006                 pMapListener->OnMapDataControlRemoveValueRequestReceived(reqId, *pProviderId, *pDataId, *pKey, *pValue);
1007                 break;
1008         default:
1009                 SysTryCatch(NID_APP, false, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request");
1010                 break;
1011         }
1012
1013         if (requestType >= _DATACONTROL_REQUEST_TYPE_MAP_QUERY && requestType <= _DATACONTROL_REQUEST_TYPE_MAP_DELETE)
1014         {
1015                 SysLog(NID_APP, "caller appId: %ls, requestType: %d, reqId: %ls, providerId: %ls, dataId: %ls, key: %ls",
1016                                 pAppId->GetPointer(), requestType, pReqId->GetPointer(), pProviderId->GetPointer(),
1017                                 pDataId->GetPointer(), pKey->GetPointer());
1018         }
1019
1020         if (pColumnList)
1021         {
1022                 pColumnList->RemoveAll(true);
1023                 delete pColumnList;
1024         }
1025
1026         if (pInsertMap)
1027         {
1028                 pInsertMap->RemoveAll(true);
1029                 delete pInsertMap;
1030         }
1031
1032         if (pUpdateMap)
1033         {
1034                 pUpdateMap->RemoveAll(true);
1035                 delete pUpdateMap;
1036         }
1037
1038         return;
1039
1040 CATCH:
1041         if (r == E_SYSTEM)
1042         {
1043                 pErrorMsg = new (std::nothrow) String("[E_SYSTEM] A system error has occurred.");
1044         }
1045         else if (r == E_OUT_OF_MEMORY)
1046         {
1047                 pErrorMsg = new (std::nothrow) String("[E_OUT_OF_MEMORY] The memory was insufficient.");
1048         }
1049         SysTryReturnVoidResult(NID_APP, pErrorMsg, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory was insufficient.");
1050
1051         res = DataControlProviderManager::GetInstance()->SendDataControlError(reqId, *pErrorMsg);
1052
1053         if (pColumnList)
1054         {
1055                 pColumnList->RemoveAll(true);
1056                 delete pColumnList;
1057         }
1058
1059         if (pInsertMap)
1060         {
1061                 pInsertMap->RemoveAll(true);
1062                 delete pInsertMap;
1063         }
1064
1065         if (pUpdateMap)
1066         {
1067                 pUpdateMap->RemoveAll(true);
1068                 delete pUpdateMap;
1069         }
1070
1071         delete pErrorMsg;
1072
1073         return;
1074 }
1075
1076
1077 void
1078 _AppImpl::OnConditionRequestReceived(const _AppArg& arg, RequestId reqId)
1079 {
1080         if (__pAppLaunchConditionEventListener == null)
1081         {
1082                 SysLog(NID_APP, "No App launch condition event listener");
1083                 return;
1084         }
1085
1086         String condition = arg.GetValue(OSP_K_COND);
1087         //Object* pExtraData = null;//TODO: retrieve NFC extra data.
1088     HashMap* pExtraData = arg.GetArgMapN();
1089         ArrayList* pList = arg.GetArgListN();
1090
1091         __pAppLaunchConditionEventListener->OnAppLaunchConditionMetN(condition, pExtraData, pList);
1092 }
1093
1094
1095 long
1096 _AppImpl::GetWindowHandle(void) const
1097 {
1098         SysTryReturn(NID_APP, __pIAppImpl != null, -1, E_INVALID_STATE, "[E_INVALID_STATE] Getting App instance failed.");
1099
1100         return __pIAppImpl->OnWindowHandleRequest();
1101 }
1102
1103
1104 void
1105 _AppImpl::OnAppCheckpoint(void)
1106 {
1107         if (__pCheckpointEventListener)
1108         {
1109                 __pCheckpointEventListener->OnAppCheckpointing(*(AppRegistry::GetInstance()));
1110         }
1111 }
1112
1113
1114 bool
1115 _AppImpl::ConfirmAppService(void)
1116 {
1117         if (_AppInfo::GetApplicationId() == SERVICE_APPID)
1118         {
1119                 return true;
1120         }
1121
1122         int value = -1;
1123         int count = 0;
1124         long interval = 100;
1125
1126         while (true)
1127         {
1128                 int res = vconf_get_int(VCONFKEY_APPSERVICE_STATUS, &value);
1129                 if (value > 0)
1130                 {
1131                         return true;
1132                 }
1133
1134                 if (count >= HEARTBEAT_WAIT_COUNT)
1135                 {
1136                         return false;
1137                 }
1138
1139                 SysLog(NID_APP, "Waiting for app service %dth time(%d msec).", count, interval);
1140                 count++;
1141                 Thread::Sleep(interval);
1142                 interval += 100;
1143         }
1144
1145         return false;
1146 }
1147
1148 result
1149 _AppImpl::SetListener(_AppEvent appEvent, IEventListener* pListener)
1150 {
1151         switch (appEvent)
1152         {
1153         case AE_CHECKPOINT:
1154                 SysTryReturnResult(NID_APP, __pCheckpointEventListener == null, E_OBJ_ALREADY_EXIST, "Checkpoint listener is already set.");
1155
1156                 // fall through
1157         case AE_CLEAR_LISTENER:
1158                 __pCheckpointEventListener = dynamic_cast <IAppCheckpointEventListener*>(pListener);
1159                 break;
1160
1161         default:
1162                 break;
1163         }
1164
1165         return E_SUCCESS;
1166 }
1167
1168
1169 result
1170 _AppImpl::SetSqlDataControlProviderEventListener(ISqlDataControlProviderEventListener* pListener)
1171 {
1172         __pSqlDataControlProviderEventListener = pListener;
1173         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1174         tmpAppHandlerType |= _APP_HANDLER_DATACONTROL;
1175         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1176         return E_SUCCESS;
1177 }
1178
1179
1180 result
1181 _AppImpl::SetMapDataControlProviderEventListener(IMapDataControlProviderEventListener* pListener)
1182 {
1183         __pMapDataControlProviderEventListener = pListener;
1184         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1185         tmpAppHandlerType |= _APP_HANDLER_DATACONTROL;
1186         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1187         return E_SUCCESS;
1188 }
1189
1190
1191 result
1192 _AppImpl::SetAppControlProviderEventListener(IAppControlProviderEventListener* pListener)
1193 {
1194         __pAppControlProviderEventListener = pListener;
1195         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1196         tmpAppHandlerType |= _APP_HANDLER_APPCONTROL;
1197         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1198         return E_SUCCESS;
1199 }
1200
1201 result
1202 _AppImpl::SetAppControlProviderInternalEventListener(IAppControlProviderEventListener* pListener)
1203 {
1204         __pAppControlProviderInternalEventListener = pListener;
1205         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1206         tmpAppHandlerType |= _APP_HANDLER_APPCONTROL;
1207         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1208         return E_SUCCESS;
1209 }
1210
1211
1212 result
1213 _AppImpl::SetAppLaunchConditionEventListener(IAppLaunchConditionEventListener* pListener)
1214 {
1215         __pAppLaunchConditionEventListener = pListener;
1216         int tmpAppHandlerType = _AppInfo::GetAppHandlerType();
1217         tmpAppHandlerType |= _APP_HANDLER_LAUNCH_COND;
1218         _AppInfo::SetAppHandlerType(tmpAppHandlerType);
1219
1220         return E_SUCCESS;
1221 }
1222
1223
1224 bool
1225 _AppImpl::IsTerminationRequested(void)
1226 {
1227         return __isTerminationRequested;
1228 }
1229
1230 void
1231 _AppImpl::OnPowerOffNotiReceived(void* user_data)
1232 {
1233         SysLog(NID_APP, "Application is being terminated by power off.");
1234
1235         _AppImpl* pAppImpl = _AppImpl::GetInstance();
1236         if (pAppImpl != null)
1237         {
1238                 pAppImpl->__forcedTermination = true;
1239         }
1240         
1241         app_efl_exit();
1242 }
1243
1244 bool 
1245 _AppImpl::IsForcedTermination(void)
1246 {
1247         return __forcedTermination;
1248 }
1249
1250 }} //Tizen::App
1251