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