refactoring
[framework/osp/appwidget-service.git] / src / FShell_AppWidgetContext.cpp
index 2132179..750eb47 100644 (file)
@@ -1,12 +1,11 @@
 //
-// Open Service Platform
-// Copyright (c) 2012 Samsung Electronics Co., Ltd.
+// Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
 //
-// Licensed under the Apache License, Version 2.0 (the License);
+// Licensed under the Flora License, Version 1.1 (the License);
 // you may not use this file except in compliance with the License.
 // You may obtain a copy of the License at
 //
-//     http://www.apache.org/licenses/LICENSE-2.0
+//     http://floralicense.org/license/
 //
 // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
  * @brief      This is the implementation for the _AppWidgetContext class.
  */
 
-#include <stdlib.h>
+#include <provider.h>
 #include <unique_ptr.h>
 
-#include "provider_buffer.h"
-
 #include <FBase.h>
 #include <FBaseSysLog.h>
+#include <FSysSystemTime.h>
+#include <FAppApp.h>
 #include <FBase_StringConverter.h>
-
-#include <FApp_AppControlManager.h>
-
+#include <FApp_AppManagerImpl.h>
+#include "FShell_AppWidgetManagerImpl.h"
 #include "FShell_AppWidgetManagerService.h"
 #include "FShell_AppWidgetPopupContext.h"
-#include "FShell_AppWidgetContextBase.h"
 #include "FShell_AppWidgetContext.h"
-
+#include "FShell_AppWidgetContextHelper.h"
+#include "FShell_AppWidgetRemoteBuffer.h"
 
 namespace Tizen { namespace Shell  { namespace App
 {
 
+using namespace std;
 using namespace Tizen::App;
 using namespace Tizen::Base;
 using namespace Tizen::Base::Collection;
+using namespace Tizen::System;
+
+const wchar_t ARG_KEY_INSTANCE_ID[] = L"_InstanceId";
+const wchar_t ARG_KEY_PROVIDER_NAME[] = L"_ProviderName";
+const wchar_t ARG_KEY_USER_INFO[] = L"_UserInfo";
+const wchar_t ARG_KEY_X[] = L"_X";
+const wchar_t ARG_KEY_Y[] = L"_Y";
+const wchar_t ARG_KEY_WIDTH[] = L"_Width";
+const wchar_t ARG_KEY_HEIGHT[] = L"_Height";
+const wchar_t ARG_KEY_POPUP_WIDTH[] = L"_PopupWidth";
+const wchar_t ARG_KEY_POPUP_HEIGHT[] = L"_PopupHeight";
+const wchar_t ARG_KEY_ARGUMENT[] = L"_Argument";
+const wchar_t ARG_KEY_EVENT_TYPE[] = L"_EventType";
+const wchar_t ARG_KEY_TIME_STAMP[] = L"_TimeStamp";
+
+const wchar_t APPWIDGET_ON_ADD[] = L"http://tizen.org/appcontrol/appwidget/add";
+const wchar_t APPWIDGET_ON_REMOVE[] = L"http://tizen.org/appcontrol/appwidget/remove";
+const wchar_t APPWIDGET_ON_UPDATE[] = L"http://tizen.org/appcontrol/appwidget/update";
+const wchar_t APPWIDGET_ON_RESIZE[] = L"http://tizen.org/appcontrol/appwidget/resize";
+const wchar_t APPWIDGET_ON_TOUCH[] = L"http://tizen.org/appcontrol/appwidget/touch";
+
+const int UPDATE_PERIOD_MSEC_MIN = 1800000;    // 30min
+
+
+_AppWidgetContext::_AppWidgetContext(_AppContext* pAppContext, const String& instanceId, const String& providerId, int width, int height, const Tizen::Base::String& userInfo, int period, int priority)
+       :__instanceId(instanceId)
+       ,__providerId(providerId)
+       ,__appId(_AppWidgetHelper::ExtractAppId(providerId))
+       ,__providerName(_AppWidgetHelper::ExtractProviderName(providerId))
+       ,__userInfo(userInfo)
+       ,__width(width)
+       ,__height(height)
+       ,__priority(priority)
+       ,__updateMillis( (period > UPDATE_PERIOD_MSEC_MIN) ? period : UPDATE_PERIOD_MSEC_MIN )
+       ,__ipcClientId(-1)
+       ,__isForeground(true)
+       ,__isRemoteBufferProxyCreated(false)
+       ,__lastUpdatedTime(0)
+       ,__hasPendingRequest(false)
+       ,__pAppContext(pAppContext)
+       ,__pAppWidgetPopup(null)
+       ,__pAppWidgetRemoteBuffer(null)
+       ,__pPendingTouchEventList(null)
+{
+       SysSecureLog(NID_SHELL, "appId(%ls), providerId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d), period(%d)", __appId.GetPointer(), __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority, __updateMillis);
 
+       // for updating period
+       if (__updateMillis > 0)
+       {
+               SystemTime::GetTicks(this->__lastUpdatedTime);
 
-const char APPWIDGET_ON_ADD[] = "http://tizen.org/appcontrol/appwidget/add";
-const char APPWIDGET_ON_REMOVE[] = "http://tizen.org/appcontrol/appwidget/remove";
-const char APPWIDGET_ON_UPDATE[] = "http://tizen.org/appcontrol/appwidget/update";
-const char APPWIDGET_ON_RESIZE[] = "http://tizen.org/appcontrol/appwidget/resize";
-const char APPWIDGET_ON_TOUCH[] = "http://tizen.org/appcontrol/appwidget/touch";
+               result r = __updateTimer.Construct(*this);
+               SysTryReturnVoidResult(NID_SHELL, !IsFailed(r), E_SYSTEM, "");
 
-const String ARG_KEY_WIDTH = L"_Width";
-const String ARG_KEY_HEIGHT = L"_Height";
-const String ARG_KEY_ARGUMENT = L"_Argument";
-const String ARG_KEY_EVENT_TYPE = L"_EventType";
-const String ARG_KEY_TIME_STAMP = L"_TimeStamp";
-const String ARG_KEY_X = L"_X";
-const String ARG_KEY_Y = L"_Y";
+               r = __updateTimer.StartAsRepeatable(__updateMillis);
+               SysTryReturnVoidResult(NID_SHELL, !IsFailed(r), E_SYSTEM, "");
+       }
 
-const int LIVE_DURATION_MSEC= 30000;//30sec
+       __pPendingTouchEventList = new (nothrow) ArrayListT<PendingTouchEvent*>();
+       SysTryReturnVoidResult(NID_SHELL, __pPendingTouchEventList, E_OUT_OF_MEMORY, "");
+       __pPendingTouchEventList->Construct();
 
-_AppWidgetContext::_AppWidgetContext(const String& info, const String& providerId, const String& instanceId, int width, int height, int period, int priority)
-:_AppWidgetContextBase(TYPE_LB, info, providerId, instanceId, width, height, priority)
-,__pAppWidgetPopup(null), __UpdateMillis(period)
+       __pAppWidgetRemoteBuffer = new (nothrow) _AppWidgetRemoteBuffer(providerId, instanceId, TYPE_LB, this);
+       SysTryReturnVoidResult(NID_SHELL, __pAppWidgetRemoteBuffer, E_OUT_OF_MEMORY, "");
+}
+
+_AppWidgetContext::~_AppWidgetContext(void)
 {
-       SysLog(NID_APP, "period(%d)", period);
+       SysSecureLog(NID_SHELL, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", GetProviderId().GetPointer(), GetInstanceId().GetPointer(), GetWidth(), GetHeight(), GetPriority());
 
-       __lifeDurationTimer.Construct(*this);//, true);
-       __lifeDurationTimer.Start(LIVE_DURATION_MSEC);
+       __updateTimer.Cancel();
 
-       SysLog(NID_APP, "period(%d)", __UpdateMillis);
-       if( __UpdateMillis > 0)
+       if (__pAppWidgetPopup)
        {
-               __UpdateTimer.Construct(*this);//, false);
-               __UpdateTimer.StartAsRepeatable(__UpdateMillis);
+               SysLog(NID_SHELL, "Destroying dangling AppWidgetPopup instance..");
+               delete __pAppWidgetPopup;
        }
-}
 
-_AppWidgetContext::~_AppWidgetContext()
-{
-       SysLog(NID_APP, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
-       __lifeDurationTimer.Cancel();
-       __UpdateTimer.Cancel();
+       if (__pPendingTouchEventList)
+       {
+               for (int i = 0; i < __pPendingTouchEventList->GetCount(); i++)
+               {
+                       PendingTouchEvent* pTouchEvent = null;
+                       __pPendingTouchEventList->GetAt(i, pTouchEvent);
+                       delete pTouchEvent;
+               }
+               __pPendingTouchEventList->RemoveAll();
+               delete __pPendingTouchEventList;
+       }
 
-       if (__pAppWidgetPopup)
+       if (__pAppWidgetRemoteBuffer)
        {
-               SysLog(NID_APP, "Destroying dangling AppWidgetPopup instance..");
-               __pAppWidgetPopup->OnPopupDestoyed();
-               delete __pAppWidgetPopup;
+               delete __pAppWidgetRemoteBuffer;
        }
 }
 
+_AppContext*
+_AppWidgetContext::GetAppContext(void) const
+{
+       return __pAppContext;
+}
+
 _AppWidgetPopupContext*
-_AppWidgetContext::GetAppWidgetPopup() const
+_AppWidgetContext::GetAppWidgetPopup(void) const
 {
        return __pAppWidgetPopup;
 }
@@ -101,202 +153,436 @@ _AppWidgetContext::GetAppWidgetPopup() const
 void
 _AppWidgetContext::OnAdded(void)
 {
-       SendAddRequest(__width, __height);
+       SendAddRequest(GetWidth(), GetHeight());
 }
 
-void
-_AppWidgetContext::OnRemoved()
+result
+_AppWidgetContext::OnRemoved(bool isTriggeredByViewer)
 {
-       SendRemoveRequest();
+       if (isTriggeredByViewer)
+       {
+               return SendRemoveRequest();
+       }
+       else
+       {
+               std::unique_ptr<char[]> providerId(_StringConverter::CopyToCharArrayN(GetProviderId()));
+               std::unique_ptr<char[]> id(_StringConverter::CopyToCharArrayN(GetInstanceId()));
+
+               int ret = provider_send_deleted(providerId.get(), id.get());
+               SysTryReturnResult(NID_SHELL, ret >= 0 , E_SYSTEM, "Failed to execute provider_send_deleted.");
+       }
+       return E_SUCCESS;
 }
 
 void
 _AppWidgetContext::OnUpdate(const String& argument)
 {
-       SysLog(NID_APP, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
+       SysLog(NID_SHELL, "argument(%ls)", argument.GetPointer());
+       ClearLastResult();
+
+       if (this->IsForeground())
+       {
+               result r = this->SendUpdateRequest(GetWidth(), GetHeight(), argument);
+               SysTryLog(NID_SHELL, !IsFailed(r), "Failed to execute SendUpdateRequest.");
+
+               SystemTime::GetTicks(this->__lastUpdatedTime);
+               SysLog(NID_SHELL, "The last updated time is %lld.", this->__lastUpdatedTime);
+
+               __hasPendingRequest = false;
+               __pendingArgument.Clear();
+       }
+       else
+       {
+               // Queueing the pointer of background context.
+               if (!argument.IsEmpty())
+               {
+                       __pendingArgument = argument;
+               }
+
+               __hasPendingRequest = true;
+               SysLog(NID_SHELL, "Update is requested but the %ls is background.", (this->GetProviderId()).GetPointer());
+       }
+}
+
+result
+_AppWidgetContext::OnUpdateAsync(_AppContext* pAppContext, const String& argument)
+{
+       ArrayList* pArray = new (std::nothrow) ArrayList();
+       SysTryReturnResult(NID_SHELL, pArray, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY]");
+
+       pArray->Construct();
+       pArray->Add(pAppContext);
+       pArray->Add(this);
+       pArray->Add(new String(argument));
 
-       SendUpdateRequest(__width, __height, argument);
+       result r = Tizen::App::App::GetInstance()->SendUserEvent(LOCAL_EVENT_REQUEST_UPDATE, pArray);
+       SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Propagated.", GetErrorMessage(r));
+       SysLog(NID_SHELL, "UserEvent(%d) is sent for '%ls.%ls'.", LOCAL_EVENT_REQUEST_UPDATE, GetAppId().GetPointer(), GetProviderName().GetPointer() );
+
+       return E_SUCCESS;
 }
 
 void
 _AppWidgetContext::OnResize(int width, int height)
 {
-       SysLog(NID_APP, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
+       SysSecureLog(NID_SHELL, "appId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", GetProviderId().GetPointer(), GetInstanceId().GetPointer(), GetWidth(), GetHeight(), GetPriority());
 
-       __width = width;
-       __height = height;
-       SendResizeRequest(__width, __height);
+       SetWidth(width);
+       SetHeight(height);
+
+       SendResizeRequest(width, height);
 }
 
 void
-_AppWidgetContext::OnForeground()
+_AppWidgetContext::OnForeground(void)
 {
-       SysLog(NID_APP, "");
+       if (IsForeground())
+       {
+               SysLog(NID_SHELL, "%ls is already foreground.", (this->GetAppId()).GetPointer());
+               return;
+       }
+
        __isForeground = true;
 
-       __UpdateTimer.Cancel();
-       __UpdateTimer.StartAsRepeatable(__UpdateMillis);
+       if (__hasPendingRequest == true)
+       {
+               SysLog(NID_SHELL, "There is pending request.");
+               OnUpdateAsync(__pAppContext, __pendingArgument);
+       }
+       else
+       {
+               if (IsUpdatePeriodExpired() == true)
+               {
+                       SysLog(NID_SHELL, "The period is expired.");
+                       OnUpdateAsync(__pAppContext, L"");
+               }
+       }
+}
+
+bool
+_AppWidgetContext::IsUpdatePeriodExpired(void) const
+{
+       if (this->GetPeriod() <= 0)
+       {
+               return false;
+       }
+
+       long long currentTicks = 0;
+       SystemTime::GetTicks(currentTicks);
+
+       SysLog(NID_SHELL, "current[%lld] - last updated time[%lld] = [%lld], period[%d]",
+                       currentTicks, this->GetLastUpdatedTime(), currentTicks - this->GetLastUpdatedTime(), this->GetPeriod());
+
+       bool isPeriodExpired = (currentTicks - this->GetLastUpdatedTime()) >= (this->GetPeriod());
+       return isPeriodExpired;
 }
 
 void
-_AppWidgetContext::OnBackground()
+_AppWidgetContext::OnBackground(void)
 {
-       SysLog(NID_APP, "");
+       if (!IsForeground())
+       {
+               SysLog(NID_SHELL, "%ls is already background.", (this->GetAppId()).GetPointer());
+               return;
+       }
+
+       SysLog(NID_SHELL, "OnBackground");
        __isForeground = false;
-       __UpdateTimer.Cancel();
 }
 
 void
 _AppWidgetContext::OnPopupCreated(double x, double y, int width, int height)
 {
-       __pAppWidgetPopup = new (std::nothrow) _AppWidgetPopupContext(__userInfo, __providerId, __instanceId, width, height, __priority);
-       __pAppWidgetPopup->SetClientId(__ipcClientId);
-       __pAppWidgetPopup->OnPopupCreated(x, y, width, height);
+       __pAppWidgetPopup = new (nothrow) _AppWidgetPopupContext(this);
+       SysTryReturnVoidResult(NID_SHELL, __pAppWidgetPopup, E_OUT_OF_MEMORY, "");
 
-       __lifeDurationTimer.Cancel();
+       __pAppWidgetPopup->OnPopupCreated(x, y, width, height);
 }
 
 void
 _AppWidgetContext::OnPopupDestoyed(void)
 {
+       SysLog(NID_SHELL, "");
+
        if (__pAppWidgetPopup)
        {
-               __pAppWidgetPopup->OnPopupDestoyed();
                delete __pAppWidgetPopup;
                __pAppWidgetPopup = null;
        }
-       RestartLifeDurationTimer();
 }
 
 result
-_AppWidgetContext::SendAddRequest(int width, int height)
+_AppWidgetContext::SendAddRequest(int width, int height) const
 {
-/*     std::unique_ptr<ArrayList, AllElementsDeleter> pArgs (new (std::nothrow) ArrayList);
-       pArgs->Construct();
-       pArgs->Add(*new String(__instanceId));
-       pArgs->Add(*new String(__providerId));
-       pArgs->Add(*new String(__info));
-       pArgs->Add(*new String(Integer::ToString(width)));
-       pArgs->Add(*new String(Integer::ToString(height)));*/
-
-       std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
-
-       pArgs->Add(new String(ARG_KEY_WIDTH), new String(Integer::ToString(width)));
-       pArgs->Add(new String(ARG_KEY_HEIGHT), new String(Integer::ToString(height)));
+       std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgsN());
 
-       return SendRequestToApp( __appId, APPWIDGET_ON_ADD, pArgs.get());
+       return __pAppContext->SendRequestToApp(GetAppId(), APPWIDGET_ON_ADD, pArgs.release());
 }
 
 result
-_AppWidgetContext::SendUpdateRequest(int width, int height, const String& argument)
+_AppWidgetContext::SendUpdateRequest(int width, int height, const String& argument) const
 {
-       std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
+       std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgsN());
 
-       pArgs->Add(new String(ARG_KEY_WIDTH), new String(Integer::ToString(width)));
-       pArgs->Add(new String(ARG_KEY_HEIGHT), new String(Integer::ToString(height)));
-       pArgs->Add(new String(ARG_KEY_ARGUMENT), new String(argument));
+       pArgs->Add(new (nothrow) String(ARG_KEY_ARGUMENT), new (nothrow) String(argument));
 
-       return SendRequestToApp( __appId, APPWIDGET_ON_UPDATE, pArgs.get());
+       return __pAppContext->SendRequestToApp(GetAppId(), APPWIDGET_ON_UPDATE, pArgs.release());
 }
 
 result
-_AppWidgetContext::SendResizeRequest(int width, int height)
+_AppWidgetContext::SendResizeRequest(int width, int height) const
 {
-       std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
+       std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgsN() );
 
-       pArgs->Add(new String(ARG_KEY_WIDTH), new String(Integer::ToString(width)));
-       pArgs->Add(new String(ARG_KEY_HEIGHT), new String(Integer::ToString(height)));
-
-       return SendRequestToApp( __appId, APPWIDGET_ON_RESIZE, pArgs.get());
+       return __pAppContext->SendRequestToApp(GetAppId(), APPWIDGET_ON_RESIZE, pArgs.release());
 }
 
 result
-_AppWidgetContext::SendRemoveRequest()
+_AppWidgetContext::SendRemoveRequest(void) const
 {
-       std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
+       std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgsN());
 
-       return SendRequestToApp( __appId, APPWIDGET_ON_REMOVE, pArgs.get());
+       return __pAppContext->SendRequestToApp(GetAppId(), APPWIDGET_ON_REMOVE, pArgs.release());
 }
 
-result
-_AppWidgetContext::SendTouchEvent(buffer_event event, double timestamp, double x, double y)
+void
+_AppWidgetContext::SendPendingTouchEvent(void)
 {
-       SysLog(NID_APP, "");
-       if( HasValidClientId() == false )
+       for (int i =0; i< __pPendingTouchEventList->GetCount(); i++)
        {
-               std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgs() );
-
-               pArgs->Add(new String(ARG_KEY_EVENT_TYPE), new String(Integer::ToString(event)));
-               pArgs->Add(new String(ARG_KEY_TIME_STAMP), new String(Double::ToString(timestamp)));
-               pArgs->Add(new String(ARG_KEY_X), new String(Double::ToString(x)));
-               pArgs->Add(new String(ARG_KEY_Y), new String(Double::ToString(y)));
+               PendingTouchEvent* pTouchEvent = null;
+               __pPendingTouchEventList->GetAt(i, pTouchEvent);
+               AppWidgetManagerService::GetInstance()->SendTouchEvent(GetClientId(), GetInstanceId(), pTouchEvent->eventType, pTouchEvent->timeStamp, pTouchEvent->x, pTouchEvent->y);
+               delete pTouchEvent;
+       }
+       __pPendingTouchEventList->RemoveAll();
+}
 
-               return SendRequestToApp( __appId, APPWIDGET_ON_TOUCH, pArgs.get());
+void
+_AppWidgetContext::OnTouchEventReceived(buffer_event event, double timestamp, double x, double y)
+{
+       if (__pAppContext->GetConnectionState() == CONNECTION_STATE_CONNECTED && __isRemoteBufferProxyCreated)
+       {
+               SysLog(NID_SHELL, "%d, %f, %f", event, x, y);
+               AppWidgetManagerService::GetInstance()->SendTouchEvent(GetClientId(), GetInstanceId(), event, timestamp, x, y);
        }
        else
        {
-//             SysAssertf( Tizen::App::AppManager::GetInstance()->IsRunning(__appId) == false, "application isn't running");
-               AppWidgetManagerService::GetInstance()->SendTouchEvent(__ipcClientId, __instanceId, event, timestamp, x, y);
+               __pPendingTouchEventList->Add(new (nothrow) PendingTouchEvent(event, timestamp, x, y));
+
+               if( _AppManagerImpl::GetInstance()->IsRunning(this->GetAppId() ) == false ||
+                       ( __pAppContext->GetConnectionState() == CONNECTION_STATE_CONNECTED && __isRemoteBufferProxyCreated == false))
+               {
+                       SysLog(NID_SHELL, "request to start AppControl");
+                       std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgsN() );
+
+                       // TODO: consider to remove these unused args.
+                       pArgs->Add(new (nothrow) String(ARG_KEY_EVENT_TYPE), new (nothrow) String(Integer::ToString(event)));
+                       pArgs->Add(new (nothrow) String(ARG_KEY_TIME_STAMP), new (nothrow) String(Double::ToString(timestamp)));
+                       pArgs->Add(new (nothrow) String(ARG_KEY_X), new (nothrow) String(Double::ToString(x)));
+                       pArgs->Add(new (nothrow) String(ARG_KEY_Y), new (nothrow) String(Double::ToString(y)));
+
+                       _AppWidgetRequestHelper::SendAppControlRequest(GetAppId(), APPWIDGET_ON_TOUCH, pArgs.get());
+               }
        }
+}
+
+result
+_AppWidgetContext::SyncRemoteBuffer(int width, int height)
+{
+       std::unique_ptr<char[]> providerId(_StringConverter::CopyToCharArrayN(GetProviderId()));
+       std::unique_ptr<char[]> id(_StringConverter::CopyToCharArrayN(GetInstanceId()));
+       std::unique_ptr<char[]> content_info(_StringConverter::CopyToCharArrayN(GetUserInfo()));
+
+       int ret = provider_send_updated(providerId.get(), id.get(), width, height, GetPriority(), content_info.get(), null);
+       SysTryReturnResult(NID_SHELL, ret >= 0 , E_SYSTEM, "[E_SYSTEM] failed to provider_send_updated");
+
+       SendPendingTouchEvent();
+       SysLog(NID_SHELL, "Done");
        return E_SUCCESS;
 }
 
 result
-_AppWidgetContext::SendRequestToApp(const AppId& appId, const String& operation, HashMap* pArgs)
+_AppWidgetContext::SyncRemoteBufferForPD(void) const
 {
-       result r = _AppWidgetContextBase::SendRequestToApp(appId, operation, pArgs);
-       RestartLifeDurationTimer();
+       _AppWidgetPopupContext* pPopupContext = GetAppWidgetPopup();
+       SysSecureTryReturnResult(NID_SHELL, pPopupContext , E_SYSTEM, "pPopupContext is null for (%ls)", GetProviderId().GetPointer());
 
-       return r;
+       return pPopupContext->SyncRemoteBuffer();
 }
 
 result
-_AppWidgetContext::RequestUpdateRemote(int width, int height)
+_AppWidgetContext::SendAccessStatus(int accessStatus) const
 {
-       /*if( GetAppWidgetPopup() != null)
-       {
-               SysLog(NID_APP, "AppWidgetPopup is appeared, so appWidget doesn't need to update");
-               return E_SUCCESS;
-       }*/
-       std::unique_ptr<char[]> packageName(_StringConverter::CopyToCharArrayN(__providerId));
-       std::unique_ptr<char[]> id(_StringConverter::CopyToCharArrayN(__instanceId));
-       std::unique_ptr<char[]> content_info(_StringConverter::CopyToCharArrayN(__userInfo));
+       std::unique_ptr<char[]> providerId(_StringConverter::CopyToCharArrayN(GetProviderId()));
+       std::unique_ptr<char[]> id(_StringConverter::CopyToCharArrayN(GetInstanceId()));
 
-       int ret = provider_send_updated(packageName.get(), id.get(), width, height, __priority, content_info.get(), null);
-       SysTryReturnResult(NID_APP, ret >= 0 , E_SYSTEM, "[E_SYSTEM] failed to provider_send_updated");
+       int ret = provider_send_access_status(providerId.get(), id.get(), accessStatus);
+       SysTryReturnResult(NID_SHELL, ret >= 0 , E_SYSTEM, "[E_SYSTEM] failed to provider_send_access_status");
 
-       SysLog(NID_APP, "Done");
        return E_SUCCESS;
 }
 
+Tizen::Base::Collection::HashMap*
+_AppWidgetContext::CreateRequestArgsN(void) const
+{
+       HashMap* pArgs = new (nothrow) HashMap(SingleObjectDeleter);
+       pArgs->Construct();
+
+       pArgs->Add(new (nothrow) String(ARG_KEY_INSTANCE_ID), new (nothrow) String(GetInstanceId()));
+       pArgs->Add(new (nothrow) String(ARG_KEY_PROVIDER_NAME), new (nothrow) String(GetProviderName()));
+       pArgs->Add(new (nothrow) String(ARG_KEY_USER_INFO), new (nothrow) String(GetUserInfo()));
+       pArgs->Add(new (nothrow) String(ARG_KEY_WIDTH), new (nothrow) String(Integer::ToString(GetWidth())));
+       pArgs->Add(new (nothrow) String(ARG_KEY_HEIGHT), new (nothrow) String(Integer::ToString(GetHeight())));
+
+       return pArgs;
+}
+
+int
+_AppWidgetContext::GetPeriod(void) const
+{
+       return __updateMillis;
+}
+
+long long
+_AppWidgetContext::GetLastUpdatedTime(void) const
+{
+       return __lastUpdatedTime;
+}
+
+int
+_AppWidgetContext::GetClientId(void) const
+{
+       return GetAppContext()->GetClientId();
+}
+
+int
+_AppWidgetContext::GetWidth(void) const
+{
+       return __width;
+}
+
 void
-_AppWidgetContext::RestartLifeDurationTimer()
+_AppWidgetContext::SetWidth(int width)
 {
-       __lifeDurationTimer.Cancel();
-       __lifeDurationTimer.Start(LIVE_DURATION_MSEC);
-       SysLog(NID_APP, "lifeDuration timer restarted (%d)msec", LIVE_DURATION_MSEC);
+       __width = width;
+}
+
+int
+_AppWidgetContext::GetHeight(void) const
+{
+       return __height;
 }
 
 void
-_AppWidgetContext::OnTimerExpired(Tizen::Base::Runtime::Timer& timer)
+_AppWidgetContext::SetHeight(int height)
+{
+       __height = height;
+}
+
+String
+_AppWidgetContext::GetProviderId(void) const
+{
+       return __providerId;
+}
+
+/*void
+_AppWidgetContext::SetProviderId(const String& providerId)
+{
+       __providerId = providerId;
+}*/
+
+String
+_AppWidgetContext::GetAppId(void) const
+{
+       return __appId;
+}
+
+int
+_AppWidgetContext::GetPriority(void) const
+{
+       return __priority;
+}
+
+/*void
+_AppWidgetContext::SetPriority(int priority)
+{
+       __priority = priority;
+}*/
+
+String
+_AppWidgetContext::GetProviderName(void) const
+{
+       return __providerName;
+}
+
+bool
+_AppWidgetContext::IsForeground(void) const
+{
+       return __isForeground;
+}
+
+String
+_AppWidgetContext::GetUserInfo(void) const
+{
+       return __userInfo;
+}
+
+/*void
+_AppWidgetContext::SetUserInfo(const String& userInfo)
 {
-       SysLog(NID_APP, "");
+       __userInfo = userInfo;
+}*/
 
-       if( &timer == &__lifeDurationTimer)
+String
+_AppWidgetContext::GetInstanceId(void) const
+{
+       return __instanceId;
+}
+
+void
+_AppWidgetContext::OnTimerExpired(Tizen::Base::Runtime::Timer& timer)
+{
+       if (&timer == &__updateTimer)
        {
-               SysLog(NID_APP, "lifeDuration timer is expired, so terminating appWidget app(%ls)..", __providerId.GetPointer() );
+               SysLog(NID_SHELL, "update timer is expired for appWidget app(%ls).", GetProviderId().GetPointer());
+               OnUpdate(L"");
+       }
+}
 
-               ReleaseSharedMem();
-               AppManager::GetInstance()->TerminateApplication(__appId);
+void
+_AppWidgetContext::OnDisconnected(void)
+{
+       SysSecureLog(NID_SHELL, "%ls, %ls", GetInstanceId().GetPointer(), GetProviderId().GetPointer());
 
+       __isRemoteBufferProxyCreated = false;
+       if (GetAppWidgetPopup())
+       {
+               OnPopupDestoyed();
        }
-       else if( &timer == &__UpdateTimer)
+}
+
+int
+_AppWidgetContext::AcquireRemoteBuffer(int w, int h) const
+{
+       int id = __pAppWidgetRemoteBuffer->Acquire(w, h);
+       if (id != -1)
        {
-               SysLog(NID_APP, "update timer is expired for appWidget app(%ls)..", __providerId.GetPointer() );
-               OnUpdate(L"");
+               __isRemoteBufferProxyCreated = true;
        }
+       return id;
 }
 
+int
+_AppWidgetContext::AcquireRemoteBufferForPD(int w, int h) const
+{
+       _AppWidgetPopupContext* pPopupContext = GetAppWidgetPopup();
+       SysSecureTryReturnResult(NID_SHELL, pPopupContext , E_SYSTEM, "pPopupContext is null for (%ls)", GetProviderId().GetPointer());
+
+       int bufferId = pPopupContext->AcquireRemoteBuffer(w, h);
+       SysSecureTryReturnResult(NID_SHELL, bufferId != -1, E_SYSTEM, "Failed to AcquireRemoteBuffer for (%ls)", GetProviderId().GetPointer());
 
-} } } // Tizen::Shell::App {
+       return bufferId;
+}
 
+}}}  // Tizen::Shell::App