2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file NfcPushUiForm.cpp
20 * @brief This file contains the implementation of NfcPushUiForm class.
23 #include <unique_ptr.h>
25 #include <FAppAppControl.h>
26 #include <FAppAppResource.h>
27 #include <FAppAppControlProviderManager.h>
28 #include <FUiAnimVisualElement.h>
29 #include <FUiSystemUtil.h>
30 #include <FUiAnimAnimationTransaction.h>
31 #include <FUiAnimEaseInOutTimingFunction.h>
32 #include <FUiAnimVisualElementPropertyAnimation.h>
33 #include <FBaseSysLog.h>
34 #include <FBaseColAllElementsDeleter.h>
36 #include <FMediaImage.h>
37 #include "NfcPushUiForm.h"
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Collection;
41 using namespace Tizen::App;
42 using namespace Tizen::Ui;
43 using namespace Tizen::Ui::Controls;
44 using namespace Tizen::Ui::Animations;
45 using namespace Tizen::Graphics;
46 using namespace Tizen::Net::Nfc;
47 using namespace Tizen::Media;
49 const int NfcPushUiForm::_DURATION_GROW_ = 667;
50 const int NfcPushUiForm::_DURATION_OPACITY_ = 497;
51 const int NfcPushUiForm::_DURATION_REPEAT_ = 516;
52 const int NfcPushUiForm::_DURATION_OPACITY2_ = 431;
54 NfcPushUiForm::NfcPushUiForm(void)
56 , __pCapturedScreen(null)
62 , __pVisualElementBounds(null)
63 , __pVisualElementOpacity(null)
64 , __pVisualElement(null)
67 , __startOpacity(0.0f)
72 NfcPushUiForm::~NfcPushUiForm(void)
74 delete __pCapturedScreen;
79 __pVisualElement->Destroy();
84 NfcPushUiForm::Initialize(RequestId reqId, const Tizen::Graphics::Rectangle& rect)
87 std::unique_ptr<NfcManager> pNfcManager;
89 r = Construct(FORM_STYLE_NORMAL);
90 SysTryReturn(NID_NET_NFC, r == E_SUCCESS, false, r, "[%s] exception occurred.", GetErrorMessage(r));
99 pNfcManager.reset(new (std::nothrow) NfcManager());
100 SysTryCatch(NID_NET_NFC, pNfcManager != null, r = E_OUT_OF_MEMORY, r, "[%s] exception occurred.", GetErrorMessage(r));
103 r = pNfcManager->Construct(*this);
104 SysTryCatch(NID_NET_NFC, r == E_SUCCESS, , r, "[%s] exception occurred.", GetErrorMessage(r));
106 // set INfcDeviceDiscoveryEventListener
107 r = pNfcManager->AddDeviceDiscoveryEventListener(*this);
108 SysTryCatch(NID_NET_NFC, r == E_SUCCESS, , r, "[%s] exception occurred.", GetErrorMessage(r));
110 __pNfcManager = pNfcManager.release();
115 FinishAppControl(__reqId, APP_CTRL_RESULT_FAILED);
120 NfcPushUiForm::SetAppInfo(const String& iconPath, const String& description)
123 if (Tizen::Io::File::IsFileExist(iconPath))
125 __iconPath = iconPath;
127 else // if icon path is empty, use default icon.
129 SysLog(NID_NET_NFC, "There is not such icon. [%ls]", iconPath.GetPointer());
130 __iconPath = App::GetInstance()->GetAppRootPath() + L"res/icon.png";
131 SysLog(NID_NET_NFC, "So use default icon: [%ls]", __iconPath.GetPointer());
135 __description = description;
139 NfcPushUiForm::OnInitializing(void)
141 result r = E_SUCCESS;
144 AddTouchEventListener(*this);
147 __pVisualElement = new (std::nothrow) VisualElement();
148 __pVisualElement->Construct();
149 __pVisualElement->SetName(L"VisualElement");
150 __pVisualElement->SetBounds(FloatRectangle(0.0f, 0.0f, 0.0f, 0.0f));
151 __pVisualElement->SetClipChildrenEnabled(false);
152 __pVisualElement->SetShowState(true);
153 GetVisualElement()->AttachChild(*__pVisualElement);
155 ////////////////////////////////////////////////////////////////////////////////
157 ////////////////////////////////////////////////////////////////////////////////
158 __pVisualElementBounds = new (std::nothrow) VisualElement();
159 __pVisualElementBounds->Construct();
160 __pVisualElementBounds->SetName(L"Grow");
161 __pVisualElementBounds->SetBounds(FloatRectangle((__rect.width / 2.0f) - 75.0f, (__rect.height / 2.0f) - 75.0f, 150.0f, 150.0f));
162 __pVisualElementBounds->SetShowState(true);
163 __pVisualElementBounds->SetImplicitAnimationEnabled(false);
164 __pVisualElement->AttachChild(*__pVisualElementBounds);
166 __endRect = __pVisualElementBounds->GetBounds();
167 __startRect = FloatRectangle(__endRect.x + 75.0f, __endRect.y + 75.0f, 0, 0);
169 std::unique_ptr<Canvas> pCanvas(__pVisualElementBounds->GetCanvasN());
170 SysTryReturnResult(NID_NET_NFC, pCanvas != null, E_OUT_OF_MEMORY, "Failed to get canvas.");
171 pCanvas->SetForegroundColor(Color::GetColor(COLOR_ID_GREY));
172 pCanvas->SetLineStyle(LINE_STYLE_SOLID);
173 pCanvas->SetLineWidth(6);
174 pCanvas->DrawEllipse(Rectangle(25, 25, 100, 100));
175 pCanvas->SetLineWidth(4);
176 pCanvas->DrawEllipse(Rectangle(15, 15, 120, 120));
177 pCanvas->SetLineWidth(2);
178 pCanvas->DrawEllipse(Rectangle(5, 5, 140, 140));
180 __pVisualElementBounds->SetBounds(__startRect);
182 ////////////////////////////////////////////////////////////////////////////////
184 ////////////////////////////////////////////////////////////////////////////////
185 __pVisualElementOpacity = new (std::nothrow) VisualElement();
186 __pVisualElementOpacity->Construct();
187 __pVisualElementOpacity->SetName(L"Opacity");
188 __pVisualElementOpacity->SetBounds(FloatRectangle((__rect.width / 2) - 50, (__rect.height / 2) - 50, 100, 100));
189 __pVisualElementOpacity->SetShowState(true);
190 __pVisualElementOpacity->SetImplicitAnimationEnabled(false);
191 __pVisualElement->AttachChild(*__pVisualElementOpacity);
193 pCanvas.reset(__pVisualElementOpacity->GetCanvasN());
194 SysTryReturnResult(NID_NET_NFC, pCanvas != null, E_OUT_OF_MEMORY, "Failed to get canvas.");
196 std::unique_ptr<Bitmap> pBitmap;
199 r = image.Construct();
200 SysTryReturnResult(NID_NET_NFC, r == E_SUCCESS, r, "Failed to construct an Image instance.");
203 pBitmap.reset(image.DecodeN(__iconPath, BITMAP_PIXEL_FORMAT_ARGB8888));
204 SysTryReturnResult(NID_NET_NFC, pBitmap != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
207 r = pBitmap->Scale(Dimension(100, 100));
208 SysTryReturnResult(NID_NET_NFC, r == E_SUCCESS, r, "Failed to scale the bitmap.");
210 __startOpacity = 0.0f;
214 pCanvas->SetBackgroundColor(Color(0x00000000));
216 r = pCanvas->DrawBitmap(Point(__rect.x, __rect.y), *pBitmap);
217 SysTryReturnResult(NID_NET_NFC, r == E_SUCCESS, r, "Failed to draw the bitmap.");
219 // set an opacity level
220 __pVisualElementOpacity->SetOpacity(__startOpacity);
222 SysLog(NID_NET_NFC, "start to play animation");
223 AnimationTransaction::Begin();
224 PlayBoundsAnimation(L"grow", 1, _DURATION_GROW_);
225 PlayOpacityAnimation(L"opacity", _DURATION_OPACITY_);
226 AnimationTransaction::Commit();
232 NfcPushUiForm::OnTerminating(void)
238 NfcPushUiForm::OnDraw(void)
240 result r = E_SUCCESS;
241 Rectangle rect = GetClientAreaBounds();
243 if (__isCaptured == false)
245 // get canvas from form
247 SysTryCatch(NID_NET_NFC, r == E_SUCCESS, , r, "[%s] exception occurred.", GetErrorMessage(r));
252 r = DrawBitmapToCanvas(rect);
253 SysTryCatch(NID_NET_NFC, r == E_SUCCESS, , r, "[%s] Failed to draw captured screen.", GetErrorMessage(r));
256 r = DrawGuideTextToCanvas(rect);
257 SysTryCatch(NID_NET_NFC, r == E_SUCCESS, , r, "[%s] Failed to draw guide text.", GetErrorMessage(r));
260 r = DrawAppInfoToCanvas(rect);
261 SysTryCatch(NID_NET_NFC, r == E_SUCCESS, , r, "[%s] Failed to draw app info.", GetErrorMessage(r));
266 FinishAppControl(__reqId, APP_CTRL_RESULT_FAILED);
271 NfcPushUiForm::CaptureScreen(void)
273 if (__pCapturedScreen)
275 delete __pCapturedScreen;
276 __pCapturedScreen = null;
279 // capture the current screen
280 __pCapturedScreen = SystemUtil::CaptureScreenN();
281 SysTryReturnResult(NID_NET_NFC, __pCapturedScreen != null, GetLastResult(), "Failed to capture the screen.");
287 NfcPushUiForm::DrawBitmapToCanvas(const Rectangle& rect)
289 result r = E_SUCCESS;
290 std::unique_ptr<Canvas> pCanvas;
292 // there should be a captured screen.
293 SysTryReturnResult(NID_NET_NFC, __pCapturedScreen != null, E_INVALID_STATE,
294 "[E_INVALID_STATE] exception occurred. There is no captures screen.");
297 pCanvas.reset(GetCanvasN());
299 SysTryReturnResult(NID_NET_NFC, pCanvas != null, r,
300 "[%s] exception occurred. Failed to get canvas instance.", GetErrorMessage(r));
303 r = pCanvas->DrawBitmap(rect, *__pCapturedScreen);
304 SysTryReturnResult(NID_NET_NFC, r == E_SUCCESS, r,
305 "[%s] exception occurred. Failed to draw captured screen to canvas.", GetErrorMessage(r));
308 r = pCanvas->FillRectangle(Color(0xC0000000), rect);
309 SysTryReturnResult(NID_NET_NFC, r == E_SUCCESS, r,
310 "[%s] exception occurred. Failed to darken the captured screen.", GetErrorMessage(r));
316 NfcPushUiForm::DrawAppInfoToCanvas(const Tizen::Graphics::Rectangle& rect)
318 result r = E_SUCCESS;
319 const int textAreaWidth = rect.width;
321 std::unique_ptr<EnrichedText> pEnrichedText(new (std::nothrow) EnrichedText());
322 SysTryReturnResult(NID_NET_NFC, pEnrichedText != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
324 std::unique_ptr<TextElement> pTextElement(new (std::nothrow) TextElement());
325 SysTryReturnResult(NID_NET_NFC, pTextElement != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
327 r = pEnrichedText->Construct(Dimension(textAreaWidth, 200));
328 SysTryReturnResult(NID_NET_NFC, r == E_SUCCESS, r, "Failed to construct an enriched text.");
330 pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_CENTER);
331 pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_MIDDLE);
332 pEnrichedText->SetTextWrapStyle(TEXT_WRAP_CHARACTER_WRAP);
333 pEnrichedText->SetTextAbbreviationEnabled(true);
335 // Creates a TextElement and sets attributes
336 r = pTextElement->Construct(__description);
337 SysTryReturnResult(NID_NET_NFC, r == E_SUCCESS, r, "Failed to construct a text element.");
339 pTextElement->SetTextColor(Color::GetColor(COLOR_ID_WHITE));
342 font.Construct(FONT_STYLE_BOLD, 40);
343 pTextElement->SetFont(font);
346 // Adds the TextElement and the bitmap to the EnrichedText
347 pEnrichedText->Add(*pTextElement);
350 std::unique_ptr<Canvas> pCanvas(GetCanvasN());
351 SysTryReturnResult(NID_NET_NFC, pCanvas != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
353 // Draws the EnrichedText at the specified Point
354 pCanvas->DrawText(Point(0, (rect.height / 2) + 100), *pEnrichedText);
360 NfcPushUiForm::DrawGuideTextToCanvas(const Tizen::Graphics::Rectangle& rect)
362 result r = E_SUCCESS;
363 const int textAreaWidth = rect.width;
364 String sendNotiStr(L"Tap to send");
365 AppResource* pAppResource = App::GetInstance()->GetAppResource();
369 pAppResource->GetString(L"IDS_NFC_BODY_TAP_TO_SEND", sendNotiStr);
372 std::unique_ptr<EnrichedText> pEnrichedText(new (std::nothrow) EnrichedText());
373 SysTryReturnResult(NID_NET_NFC, pEnrichedText != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
375 std::unique_ptr<TextElement> pTextElement(new (std::nothrow) TextElement());
376 SysTryReturnResult(NID_NET_NFC, pTextElement != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
378 r = pEnrichedText->Construct(Dimension(textAreaWidth, 100));
379 SysTryReturnResult(NID_NET_NFC, r == E_SUCCESS, r, "Failed to construct an enriched text.");
381 r = pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_CENTER);
382 SysTryReturnResult(NID_NET_NFC, r == E_SUCCESS, r, "Failed to set horizontal alignment.");
384 pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_MIDDLE);
385 SysTryReturnResult(NID_NET_NFC, r == E_SUCCESS, r, "Failed to set vertical alignment.");
387 pEnrichedText->SetTextWrapStyle(TEXT_WRAP_CHARACTER_WRAP);
388 SysTryReturnResult(NID_NET_NFC, r == E_SUCCESS, r, "Failed to set text wrap style.");
390 pEnrichedText->SetTextAbbreviationEnabled(true);
391 SysTryReturnResult(NID_NET_NFC, r == E_SUCCESS, r, "Failed to set abbreviation enabled.");
393 // Creates a TextElement and sets attributes
394 r = pTextElement->Construct(sendNotiStr);
395 SysTryReturnResult(NID_NET_NFC, r == E_SUCCESS, r, "Failed to construct a text element.");
397 pTextElement->SetTextColor(Color::GetColor(COLOR_ID_WHITE));
400 font.Construct(FONT_STYLE_BOLD, 40);
401 pTextElement->SetFont(font);
404 // Adds the TextElement and the bitmap to the EnrichedText
405 r = pEnrichedText->Add(*pTextElement);
406 SysTryReturnResult(NID_NET_NFC, r == E_SUCCESS, r, "Failed to add text element to enriched text.");
409 std::unique_ptr<Canvas> pCanvas(GetCanvasN());
410 SysTryReturnResult(NID_NET_NFC, pCanvas != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
412 // Draws the EnrichedText at the specified Point
413 r = pCanvas->DrawText(Point((rect.width - textAreaWidth) / 2, (int)(rect.height / 4)), *pEnrichedText);
414 SysTryReturnResult(NID_NET_NFC, r == E_SUCCESS, r, "Failed to draw text to canvas.");
420 NfcPushUiForm::PlayBoundsAnimation(const Tizen::Base::String& animationName, int repeatCount, int duration, bool autoReverse)
422 std::unique_ptr<EaseInOutTimingFunction> pTimingFunction;
423 std::unique_ptr<VisualElementPropertyAnimation> pAnimation(new (std::nothrow) VisualElementPropertyAnimation());
424 SysTryReturnVoidResult(NID_NET_NFC, pAnimation != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
427 pAnimation->SetPropertyName(L"bounds");
428 pAnimation->SetStartValue(Variant(__startRect));
429 pAnimation->SetEndValue(Variant(__endRect));
430 pAnimation->SetDuration(duration);
431 pAnimation->SetRepeatCount(repeatCount);
432 pAnimation->SetAutoReverseEnabled(autoReverse);
433 pAnimation->SetVisualElementAnimationStatusEventListener(this);
434 if (animationName == L"grow" || animationName == L"shrink")
436 pTimingFunction.reset(new (std::nothrow) EaseInOutTimingFunction());
437 pAnimation->SetTimingFunction(pTimingFunction.release());
440 __pVisualElementBounds->AddAnimation(animationName, *pAnimation);
444 NfcPushUiForm::PlayOpacityAnimation(const Tizen::Base::String& animationName, int duration)
446 std::unique_ptr<VisualElementPropertyAnimation> pAnimation(new (std::nothrow) VisualElementPropertyAnimation());
447 SysTryReturnVoidResult(NID_NET_NFC, pAnimation != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
450 pAnimation->SetPropertyName(L"opacity");
451 pAnimation->SetStartValue(Variant(__startOpacity));
452 pAnimation->SetEndValue(Variant(__endOpacity));
453 pAnimation->SetDuration(duration);
454 pAnimation->SetVisualElementAnimationStatusEventListener(this);
455 if (animationName == L"opacity")
457 pAnimation->SetDelay(247);
460 __pVisualElementOpacity->AddAnimation(animationName, *pAnimation);
464 NfcPushUiForm::FinishAppControl(RequestId reqId, AppCtrlResult appCtrlResult)
466 result r = E_SUCCESS;
469 r = AppControlProviderManager::GetInstance()->SendAppControlResult(reqId, appCtrlResult, null);
470 SysTryReturnVoidResult(NID_NET_NFC, r == E_SUCCESS, r, "[%s] exception occurred.", GetErrorMessage(r));
473 SysLog(NID_NET_NFC, "Terminating this app control. [%d]", __reqId);
474 App::GetInstance()->Terminate();
478 NfcPushUiForm::OnVisualElementAnimationFinished(const VisualElementAnimation& animation, const String& keyName, VisualElement& target, bool completedNormally)
480 if(completedNormally)
482 if (keyName == L"opacity")
484 float margin = (__endRect.width * 0.2f) / 2;
485 __startRect = __endRect;
486 __endRect = FloatRectangle(__startRect.x - margin, __startRect.y - margin,
487 __startRect.width + (margin * 2), __startRect.height + (margin * 2));
489 // start second animation - repeat
490 PlayBoundsAnimation(L"repeat", 0, _DURATION_REPEAT_, true);
492 else if (keyName == L"shrink")
494 // finish this app control with success
496 // remove the guide text and app info
497 Rectangle rect = GetClientAreaBounds();
499 result r = E_SUCCESS;
501 // remove the guide text and app description
502 r = DrawBitmapToCanvas(rect);
503 SysTryReturnVoidResult(NID_NET_NFC, r == E_SUCCESS, r, "[%s] Failed to remove guide text and app description.", GetErrorMessage(r));
505 FinishAppControl(__reqId, APP_CTRL_RESULT_SUCCEEDED);
510 // ITouchEventListener
512 NfcPushUiForm::OnTouchReleased(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
514 __startOpacity = __endOpacity;
517 // reset the circles size
518 __startRect = __endRect;
519 __endRect = FloatRectangle(__startRect.x + 90.0f, __startRect.y + 90.0f, 0, 0);
522 AnimationTransaction::Begin();
523 PlayOpacityAnimation(L"opacity2", _DURATION_OPACITY2_);
524 PlayBoundsAnimation(L"shrink", 1, 667, false);
525 AnimationTransaction::Commit();
528 // INfcDeviceDiscoveryEventListener
530 NfcPushUiForm::OnNfcDeviceLost(void)
532 SysLog(NID_NET_NFC, "Devices are lost each other. (User-Cancelled) Terminating this app-control...");
533 FinishAppControl(__reqId, APP_CTRL_RESULT_CANCELED);