[WRTjs] Refactor popup
[platform/framework/web/chromium-efl.git] / wrt / src / browser / vibration_manager.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #include "wrt/src/browser/vibration_manager.h"
18
19 #include "base/logging.h"
20 #include "tizen_src/chromium_impl/services/device/vibration/vibration_manager_impl_efl.h"
21
22 namespace wrt {
23
24 // static
25 void VibrationManager::CreateInstance() {
26   static VibrationManager* vibration_provider_client = nullptr;
27   // vibration_provider_client is deleted by device::VibrationProviderClient
28   if (!vibration_provider_client)
29     vibration_provider_client = new VibrationManager;
30 }
31
32 VibrationManager::VibrationManager() : handle_(0) {
33   device::VibrationManagerImplEfl::RegisterProviderClient(this);
34 }
35
36 VibrationManager::~VibrationManager() {
37   LOG(INFO) << "~VibrationManager";
38   if (handle_ != 0)
39     device_haptic_close(handle_);
40 }
41
42 bool VibrationManager::Initialize() {
43   if (handle_ != 0)
44     return true;
45
46   int ret = device_haptic_open(0, &handle_);
47   if (ret != DEVICE_ERROR_NONE) {
48     LOG(ERROR) << "Fail to open haptic device";
49     handle_ = 0;
50     return false;
51   }
52   LOG(INFO) << "Device Haptic open successful";
53   return true;
54 }
55
56 void VibrationManager::Vibrate(uint64_t ms) {
57   if (Initialize()) {
58     LOG(INFO) << "Device Vibration Start";
59     device_haptic_vibrate(handle_, ms, 100, nullptr);
60   }
61 }
62
63 void VibrationManager::CancelVibration() {
64   if (Initialize()) {
65     LOG(INFO) << "Device Vibration Stop";
66     device_haptic_stop(handle_, nullptr);
67   }
68 }
69
70 } // namespace wrt