1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "core/common/vibrator/vibrator_utils.h"
16
17 #include "vibrator_agent.h"
18
19 using namespace OHOS::Sensors;
20
21 namespace OHOS::Ace::NG {
22 namespace {
23 static std::vector<const char*> effectIdNames = {
24 VIBRATOR_TYPE_SLIDE,
25 VIBRATOR_TYPE_SLIDE_LIGHT,
26 VIBRATOR_TYPE_CHARGING,
27 VIBRATOR_TYPE_CLOCK_TIMER
28 };
29
30 const char* VIBRATOR_TYPE_INVALID = "vibrator.type.invalid";
GetVibratorType(const std::string & vibratorType)31 const char* GetVibratorType(const std::string& vibratorType)
32 {
33 #ifdef SUPPORT_DIGITAL_CROWN
34 std::string watchhaptic = "watchhaptic.feedback.crown";
35 if (vibratorType.find(watchhaptic) != std::string::npos) {
36 return vibratorType.c_str();
37 }
38 #endif
39 if (vibratorType == "longPress.light") {
40 return VIBRATOR_TYPE_LONG_PRESS_LIGHT;
41 } else if (vibratorType == "slide") {
42 return VIBRATOR_TYPE_SLIDE;
43 } else if (vibratorType == "slide.light") {
44 return VIBRATOR_TYPE_SLIDE_LIGHT;
45 }
46 return VIBRATOR_TYPE_INVALID;
47 }
48 };
49
50 const char* VibratorUtils::supportedEffectId = nullptr;
51
GetFirstSupportedId()52 const char* VibratorUtils::GetFirstSupportedId()
53 {
54 bool state = false;
55 for (auto item : effectIdNames) {
56 Sensors::IsSupportEffect(item, &state);
57 if (state) {
58 return item;
59 }
60 }
61 return nullptr;
62 }
63
StartVibraFeedback()64 void VibratorUtils::StartVibraFeedback()
65 {
66 #ifdef INDEXER_SUPPORT_VIBRATOR
67 if (supportedEffectId == nullptr) {
68 supportedEffectId = VibratorUtils::GetFirstSupportedId();
69 }
70 if (supportedEffectId != nullptr) {
71 Sensors::StartVibrator(supportedEffectId);
72 }
73 #endif
74 }
75
StartVibraFeedback(const std::string & vibratorType)76 void VibratorUtils::StartVibraFeedback(const std::string& vibratorType)
77 {
78 const char* realVibratorType = GetVibratorType(vibratorType);
79 if (strcmp(realVibratorType, VIBRATOR_TYPE_INVALID) == 0) {
80 return;
81 }
82 Sensors::StartVibrator(realVibratorType);
83 }
84
StartExclusiveVibraFeedback(const char * effectId)85 bool VibratorUtils::StartExclusiveVibraFeedback(const char* effectId)
86 {
87 bool state { false };
88 Sensors::IsSupportEffect(effectId, &state);
89 if (state) {
90 Sensors::StartVibrator(effectId);
91 }
92 return state;
93 }
94
StartViratorDirectly(const std::string & vibratorType)95 void VibratorUtils::StartViratorDirectly(const std::string& vibratorType)
96 {
97 if (!vibratorType.empty()) {
98 Sensors::StartVibrator(vibratorType.c_str());
99 }
100 }
101 } // namespace OHOS::Ace::NG
102