• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 const std::string CONFIG_PATH = "/vendor/etc/vibrator/vibration_policy_config.json";
25 }  // namespace
26 
VibrationPriorityManager()27 VibrationPriorityManager::VibrationPriorityManager()
28 {
29     CALL_LOG_ENTER;
30     PriorityConfig config;
31     int32_t ret = LoadPriorityConfig(CONFIG_PATH, config);
32     if (ret != SUCCESS) {
33         MISC_HILOGE("Load priority config fail");
34         return;
35     }
36     CopyPriorityConfig(config);
37 }
38 
~VibrationPriorityManager()39 VibrationPriorityManager::~VibrationPriorityManager() {}
40 
CopyPriorityConfig(PriorityConfig config)41 void VibrationPriorityManager::CopyPriorityConfig(PriorityConfig config)
42 {
43     priorityConfig_.privilegePkgs = config.privilegePkgs;
44     priorityConfig_.globalSettings.byPassUsages = config.globalSettings.byPassUsages;
45     priorityConfig_.globalSettings.byPassPkgs = config.globalSettings.byPassPkgs;
46     priorityConfig_.globalSettings.filterUsages = config.globalSettings.filterUsages;
47     priorityConfig_.globalSettings.filterPkgs = config.globalSettings.filterPkgs;
48 
49     priorityConfig_.lowPowerMode.byPassUsages = config.lowPowerMode.byPassUsages;
50     priorityConfig_.lowPowerMode.byPassPkgs = config.lowPowerMode.byPassPkgs;
51     priorityConfig_.lowPowerMode.filterUsages = config.lowPowerMode.filterUsages;
52     priorityConfig_.lowPowerMode.filterPkgs = config.lowPowerMode.filterPkgs;
53 
54     priorityConfig_.specialForeground.calling.byPassUsages = config.specialForeground.calling.byPassUsages;
55     priorityConfig_.specialForeground.calling.byPassPkgs = config.specialForeground.calling.byPassPkgs;
56     priorityConfig_.specialForeground.calling.filterUsages = config.specialForeground.calling.filterUsages;
57     priorityConfig_.specialForeground.calling.filterPkgs = config.specialForeground.calling.filterPkgs;
58 
59     priorityConfig_.specialForeground.camera.byPassUsages = config.specialForeground.camera.byPassUsages;
60     priorityConfig_.specialForeground.camera.byPassPkgs = config.specialForeground.camera.byPassPkgs;
61     priorityConfig_.specialForeground.camera.filterUsages = config.specialForeground.camera.filterUsages;
62     priorityConfig_.specialForeground.camera.filterPkgs = config.specialForeground.camera.filterPkgs;
63 }
64 
ShouldIgnoreVibrate(const VibrateInfo & vibrateInfo,std::shared_ptr<VibratorThread> vibratorThread)65 VibrateStatus VibrationPriorityManager::ShouldIgnoreVibrate(const VibrateInfo &vibrateInfo,
66     std::shared_ptr<VibratorThread> vibratorThread)
67 {
68     if (IsPrivilegeApp(vibrateInfo.packageName)) {
69         MISC_HILOGD("Privilege application, can vibrate");
70         return VIBRATION;
71     }
72     if (vibratorThread == nullptr) {
73         MISC_HILOGD("There is no vibration, it can vibrate");
74         return VIBRATION;
75     }
76     if (!IsCurrentVibrate(vibratorThread)) {
77         MISC_HILOGD("There is no vibration at the moment, it can vibrate");
78         return VIBRATION;
79     }
80     if (IsLoopVibrate(vibrateInfo)) {
81         MISC_HILOGD("Can vibrate, loop priority is high");
82         return VIBRATION;
83     }
84     return ShouldIgnoreVibrate(vibrateInfo, vibratorThread->GetCurrentVibrateInfo());
85 }
86 
IsCurrentVibrate(std::shared_ptr<VibratorThread> vibratorThread) const87 bool VibrationPriorityManager::IsCurrentVibrate(std::shared_ptr<VibratorThread> vibratorThread) const
88 {
89     return ((vibratorThread != nullptr) && (vibratorThread->IsRunning()));
90 }
91 
IsPrivilegeApp(const std::string & name) const92 bool VibrationPriorityManager::IsPrivilegeApp(const std::string &name) const
93 {
94     std::vector<std::string> privilegePkgs = priorityConfig_.privilegePkgs;
95     auto it = std::find(privilegePkgs.begin(), privilegePkgs.end(), name);
96     return (it != privilegePkgs.end());
97 }
98 
IsLoopVibrate(const VibrateInfo & vibrateInfo) const99 bool VibrationPriorityManager::IsLoopVibrate(const VibrateInfo &vibrateInfo) const
100 {
101     return ((vibrateInfo.mode == "preset") && (vibrateInfo.count > 1));
102 }
103 
ShouldIgnoreVibrate(const VibrateInfo & vibrateInfo,VibrateInfo currentVibrateInfo) const104 VibrateStatus VibrationPriorityManager::ShouldIgnoreVibrate(const VibrateInfo &vibrateInfo,
105     VibrateInfo currentVibrateInfo) const
106 {
107     if (currentVibrateInfo.usage == USAGE_ALARM) {
108         MISC_HILOGD("Vibration is ignored for alarm");
109         return IGNORE_ALARM;
110     }
111     if (IsLoopVibrate(currentVibrateInfo)) {
112         MISC_HILOGD("Vibration is ignored for repeat");
113         return IGNORE_REPEAT;
114     }
115     if ((currentVibrateInfo.usage != vibrateInfo.usage) && (vibrateInfo.usage == USAGE_UNKNOWN)) {
116         MISC_HILOGD("Vibration is ignored, unknown has a low priority");
117         return IGNORE_UNKNOWN;
118     }
119     return VIBRATION;
120 }
121 
LoadPriorityConfig(const std::string & configPath,PriorityConfig & config)122 int32_t VibrationPriorityManager::LoadPriorityConfig(const std::string &configPath, PriorityConfig &config)
123 {
124     JsonParser parser(configPath);
125     int32_t ret = parser.ParseJsonArray("privilegePkgs", config.privilegePkgs);
126     CHKCR((ret == SUCCESS), ERROR, "Parse privilegePkgs fail");
127 
128     cJSON* trustLists = parser.GetObjectItem("trustLists");
129     ret = ParserPriorityItem(parser, trustLists, "globalSettings", config.globalSettings);
130     CHKCR((ret == SUCCESS), ERROR, "parse globalSettings fail");
131 
132     ret = ParserPriorityItem(parser, trustLists, "lowPowerMode", config.lowPowerMode);
133     CHKCR((ret == SUCCESS), ERROR, "parse lowPowerMode fail");
134 
135     cJSON* specialForeground = parser.GetObjectItem(trustLists, "specialForeground");
136     ret = ParserPriorityItem(parser, specialForeground, "calling", config.specialForeground.calling);
137     CHKCR((ret == SUCCESS), ERROR, "parse calling fail");
138     return ParserPriorityItem(parser, specialForeground, "camera", config.specialForeground.camera);
139 }
140 
ParserPriorityItem(const JsonParser & parser,cJSON * json,const std::string & key,PriorityItem & item)141 int32_t VibrationPriorityManager::ParserPriorityItem(const JsonParser &parser, cJSON *json, const std::string &key,
142     PriorityItem &item)
143 {
144     cJSON* cJson = parser.GetObjectItem(json, key);
145     int32_t ret = parser.ParseJsonArray(cJson, "byPassUsages", item.byPassUsages);
146     CHKCR((ret == SUCCESS), ERROR, "parse byPassUsages fail");
147     ret = parser.ParseJsonArray(cJson, "byPassPkgs", item.byPassPkgs);
148     CHKCR((ret == SUCCESS), ERROR, "parse byPassPkgs fail");
149     ret = parser.ParseJsonArray(cJson, "filterUsages", item.filterUsages);
150     CHKCR((ret == SUCCESS), ERROR, "parse filterUsages fail");
151     return parser.ParseJsonArray(cJson, "filterPkgs", item.filterPkgs);
152 }
153 }  // namespace Sensors
154 }  // namespace OHOS