1 /*
2 * Copyright (c) 2022 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
16 #include "vibration_priority_manager.h"
17
18 #include "sensors_errors.h"
19
20 namespace OHOS {
21 namespace Sensors {
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "VibrationPriorityManager" };
24 } // namespace
25
VibrationPriorityManager()26 VibrationPriorityManager::VibrationPriorityManager() {}
27
~VibrationPriorityManager()28 VibrationPriorityManager::~VibrationPriorityManager() {}
29
ShouldIgnoreVibrate(const VibrateInfo & vibrateInfo,std::shared_ptr<VibratorThread> vibratorThread)30 VibrateStatus VibrationPriorityManager::ShouldIgnoreVibrate(const VibrateInfo &vibrateInfo,
31 std::shared_ptr<VibratorThread> vibratorThread)
32 {
33 if (vibratorThread == nullptr) {
34 MISC_HILOGD("There is no vibration, it can vibrate");
35 return VIBRATION;
36 }
37 if (!IsCurrentVibrate(vibratorThread)) {
38 MISC_HILOGD("There is no vibration at the moment, it can vibrate");
39 return VIBRATION;
40 }
41 if (IsLoopVibrate(vibrateInfo)) {
42 MISC_HILOGD("Can vibrate, loop priority is high");
43 return VIBRATION;
44 }
45 return ShouldIgnoreVibrate(vibrateInfo, vibratorThread->GetCurrentVibrateInfo());
46 }
47
IsCurrentVibrate(std::shared_ptr<VibratorThread> vibratorThread) const48 bool VibrationPriorityManager::IsCurrentVibrate(std::shared_ptr<VibratorThread> vibratorThread) const
49 {
50 #if defined(OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM)
51 return ((vibratorThread != nullptr) && (vibratorThread->IsRunning() || VibratorDevice.IsVibratorRunning()));
52 #else
53 return ((vibratorThread != nullptr) && (vibratorThread->IsRunning()));
54 #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
55 }
56
IsLoopVibrate(const VibrateInfo & vibrateInfo) const57 bool VibrationPriorityManager::IsLoopVibrate(const VibrateInfo &vibrateInfo) const
58 {
59 return ((vibrateInfo.mode == "preset") && (vibrateInfo.count > 1));
60 }
61
ShouldIgnoreVibrate(const VibrateInfo & vibrateInfo,VibrateInfo currentVibrateInfo) const62 VibrateStatus VibrationPriorityManager::ShouldIgnoreVibrate(const VibrateInfo &vibrateInfo,
63 VibrateInfo currentVibrateInfo) const
64 {
65 if (currentVibrateInfo.usage == USAGE_ALARM) {
66 MISC_HILOGD("Vibration is ignored for alarm");
67 return IGNORE_ALARM;
68 }
69 if (IsLoopVibrate(currentVibrateInfo)) {
70 MISC_HILOGD("Vibration is ignored for repeat");
71 return IGNORE_REPEAT;
72 }
73 if ((currentVibrateInfo.usage != vibrateInfo.usage) && (vibrateInfo.usage == USAGE_UNKNOWN)) {
74 MISC_HILOGD("Vibration is ignored, unknown has a low priority");
75 return IGNORE_UNKNOWN;
76 }
77 return VIBRATION;
78 }
79 } // namespace Sensors
80 } // namespace OHOS