Init Tizen 2.2.1
[framework/osp/web.git] / src / controls / FWebCtrl_WebNotification.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                FWebCtrl_WebNotification.cpp
20  * @brief               The file contains the definition of _WebNotification class.
21  */
22 #include <FBaseRtTimer.h>
23 #include <FBaseSysLog.h>
24 #include <FSysVibrator.h>
25 #include <FSys_VibratorImpl.h>
26 #include <FUiAnimControlAnimator.h>
27 #include <FUiAnimPointAnimation.h>
28 #include <FUiAnimFloatAnimation.h>
29 #include <FUiAnimAnimationTransaction.h>
30 #include <FUiAnimVisualElementPropertyAnimation.h>
31 #include <FUiCtrlButton.h>
32 #include "FWebCtrl_WebImpl.h"
33 #include "FWebCtrl_WebNotificationHandler.h"
34 #include "FWebCtrl_WebNotification.h"
35
36
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Runtime;
39 using namespace Tizen::Graphics;
40 using namespace Tizen::Ui;
41 using namespace Tizen::Ui::Animations;
42 using namespace Tizen::Ui::Controls;
43
44
45 namespace Tizen { namespace Web { namespace Controls
46 {
47
48
49 _WebNotification::_WebNotification(void)
50                                 : __pNotificationHandler(null)
51                                 , __pContext(null)
52                                 , __notificationId(0)
53                                 , __pTimer(null)
54                                 , __pWebImpl(null)
55 {
56 }
57
58
59 _WebNotification::~_WebNotification(void)
60 {
61 }
62
63
64 result
65 _WebNotification::Construct(Ewk_Context *pContext, uint64_t notificationId, Tizen::Web::Controls::_WebImpl* pImpl)
66 {
67         Rectangle rect(NOTIFCATION_RECT_AREA);
68         result r = E_SUCCESS;
69
70         __pContext = pContext;
71         __notificationId = notificationId;
72
73         r = Window::Construct(rect, true, true);
74         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Error Propogating.", GetErrorMessage(r));
75         SetBounds(rect);
76
77         Window::SetOwner(&pImpl->GetPublic());
78         __pNotificationHandler = std::unique_ptr<_WebNotificationHandler>(new (std::nothrow) _WebNotificationHandler());
79         SysTryReturnResult(NID_WEB_CTRL, __pNotificationHandler.get(), E_OUT_OF_MEMORY, "Memory allocation failed.");
80
81         r = __pNotificationHandler->Construct(this);
82         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Error Propogating.", GetErrorMessage(r));
83
84         SetPropagatedTouchEventListener(__pNotificationHandler.get());
85         __pTimer = std::unique_ptr<Timer>( new (std::nothrow) Timer());
86         SysTryReturnResult(NID_WEB_CTRL, __pTimer.get(), E_OUT_OF_MEMORY, "Memory allocation failed.");
87
88         r = __pTimer->Construct(*this);
89         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Error Propogating.", GetErrorMessage(r));
90
91         __pWebImpl = pImpl;
92
93         return r;
94 }
95
96
97 void
98 _WebNotification::SetText(const String& text)
99 {
100         __pNotificationHandler->SetText(text);
101 }
102
103
104 String
105 _WebNotification::GetText(void) const
106 {
107         return __pNotificationHandler->GetText();
108 }
109
110
111 result
112 _WebNotification::OnDraw(void)
113 {
114         result r = E_SUCCESS;
115         SysTryReturnResult(NID_WEB_CTRL, __pNotificationHandler.get(), E_SYSTEM, "Failed to find Impl Instance.");
116
117         std::unique_ptr<Canvas> pCanvas(GetCanvasN());
118         SysTryReturnResult(NID_WEB_CTRL, pCanvas.get(), GetLastResult(), "[%s] Error Propagated.", GetErrorMessage(GetLastResult()));
119
120         r = __pNotificationHandler->DrawNotification(*pCanvas);
121
122         return r;
123 }
124
125
126 void
127 _WebNotification::OnClicked()
128 {
129         SysTryReturnVoidResult(NID_WEB_CTRL, __pContext != null, E_SYSTEM, "[E_SYSTEM] Context instance not found.");
130         ewk_notification_clicked(__pContext,__notificationId);
131 }
132
133
134 void
135 _WebNotification::OnChangeLayout(void)
136 {
137         SetShowState(false);
138         SetShowState(true);
139         Window::Show();
140 }
141
142
143 result
144 _WebNotification::LaunchNotification()
145 {
146         result r = E_SUCCESS;
147         std::unique_ptr<Button> pButton( new (std::nothrow) Button());
148         SysTryReturnResult(NID_WEB_CTRL, pButton.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
149
150         r = pButton->Construct(Rectangle(NOTIFCATION_BUTTON_AREA));
151         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Error Propogating.", GetErrorMessage(r));
152
153         pButton->SetText(L"x"); //Need to add bitmap
154         pButton->AddActionEventListener(*this);
155
156         r = AddControl(pButton.get());
157         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Error Propogating.", GetErrorMessage(r));
158         pButton.release();
159
160         SetShowState(true);
161         r = Window::Show();
162         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Error Propogating.", GetErrorMessage(r));
163
164         FloatRectangle rect(static_cast< float >(NOTIFCATION_RECT_AREA.x),static_cast< float >(NOTIFCATION_RECT_AREA.y),static_cast< float >(NOTIFCATION_RECT_AREA.width),static_cast< float >(NOTIFCATION_RECT_AREA.height));
165
166         FloatRectangle __endRect = rect;
167         FloatRectangle __startRect = FloatRectangle(rect.x, rect.height - 200, rect.width, rect.height);
168
169         std::unique_ptr<VisualElementPropertyAnimation> pAnimation(new (std::nothrow) VisualElementPropertyAnimation());
170         SysTryReturnResult(NID_WEB_CTRL, pAnimation.get(), E_OUT_OF_MEMORY, "Memory allocation failed.");
171
172         pAnimation->SetPropertyName(L"bounds");
173         pAnimation->SetStartValue(Variant(__startRect));
174         pAnimation->SetEndValue(Variant(__endRect));
175         pAnimation->SetDuration(2000);
176         this->GetVisualElement()->AddAnimation(L"bounds", *pAnimation);
177
178         __pTimer->Start(15000);
179         return E_SUCCESS;
180 }
181
182
183 void
184 _WebNotification::OnTimerExpired(Timer& timer)
185 {
186         float start = 1.0f;
187         float end = 0.0f;
188
189         std::unique_ptr<VisualElementPropertyAnimation> pAnimation(new (std::nothrow) VisualElementPropertyAnimation());
190         SysTryReturnVoidResult(NID_WEB_CTRL, pAnimation.get(), E_OUT_OF_MEMORY, "Memory allocation failed.");
191
192         pAnimation->SetPropertyName(L"opacity");
193         pAnimation->SetStartValue(Variant(start));
194         pAnimation->SetEndValue(Variant(end));
195         pAnimation->SetDuration(2000);
196         this->GetVisualElement()->AddAnimation(L"opacity", *pAnimation);
197 }
198
199
200 void
201 _WebNotification::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
202 {
203         SetShowState(false);
204
205         __pWebImpl->ClearWebNotification(this);
206
207         delete this;
208 }
209
210
211 }}}