• 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 "accessibility_settings_config.h"
17 #include "hilog_wrapper.h"
18 #include "utils.h"
19 
20 namespace OHOS {
21 namespace Accessibility {
22 namespace {
23     const std::string PREF_PATH = "/data/service/el1/public/barrierfree/accessibility_ability_manager_service/";
24     constexpr uint32_t DEFAULT_COLOR = 0xff000000;
25 } // namespace
AccessibilitySettingsConfig(int32_t id)26 AccessibilitySettingsConfig::AccessibilitySettingsConfig(int32_t id)
27 {
28     HILOG_DEBUG("id = [%{public}d]", id);
29     accountId_ = id;
30 }
31 
SetEnabled(const bool state)32 bool AccessibilitySettingsConfig::SetEnabled(const bool state)
33 {
34     HILOG_DEBUG("state = [%{public}s]", state ? "True" : "False");
35     enabled_ = state;
36     return SetStatePref(STATE::ACCESSIBILITY);
37 }
38 
SetTouchGuideState(const bool state)39 bool AccessibilitySettingsConfig::SetTouchGuideState(const bool state)
40 {
41     HILOG_DEBUG("state = [%{public}s]", state ? "True" : "False");
42     eventTouchGuideState_ = state;
43     return SetStatePref(STATE::TOUCHGUIDE);
44 }
45 
SetGestureState(const bool state)46 bool AccessibilitySettingsConfig::SetGestureState(const bool state)
47 {
48     HILOG_DEBUG("state = [%{public}s]", state ? "True" : "False");
49     gesturesSimulation_ = state;
50     return SetStatePref(STATE::GESTURE);
51 }
52 
SetKeyEventObserverState(const bool state)53 bool AccessibilitySettingsConfig::SetKeyEventObserverState(const bool state)
54 {
55     HILOG_DEBUG("state = [%{public}s]", state ? "True" : "False");
56     filteringKeyEvents_ = state;
57     return SetStatePref(STATE::KEYEVENT);
58 }
59 
60 
SetCaptionState(const bool state)61 RetError AccessibilitySettingsConfig::SetCaptionState(const bool state)
62 {
63     HILOG_DEBUG("state = [%{public}s]", state ? "True" : "False");
64     isCaptionState_ = state;
65     return SetStatePref(STATE::CAPTION) ? RET_OK : RET_ERR_FAILED;
66 }
67 
SetScreenMagnificationState(const bool state)68 RetError AccessibilitySettingsConfig::SetScreenMagnificationState(const bool state)
69 {
70     HILOG_DEBUG("state = [%{public}s]", state ? "True" : "False");
71     isScreenMagnificationState_ = state;
72     return SetStatePref(STATE::SCREENMAGNIFIER) ? RET_OK : RET_ERR_FAILED;
73 }
74 
SetShortKeyState(const bool state)75 RetError AccessibilitySettingsConfig::SetShortKeyState(const bool state)
76 {
77     HILOG_DEBUG("state = [%{public}s]", state ? "True" : "False");
78     isShortKeyState_ = state;
79     return SetStatePref(STATE::SHORTKEY) ? RET_OK : RET_ERR_FAILED;
80 }
81 
SetStartFromAtoHosState(const bool state)82 RetError AccessibilitySettingsConfig::SetStartFromAtoHosState(const bool state)
83 {
84     HILOG_DEBUG("state = [%{public}s]", state ? "True" : "False");
85     std::string strState = state ? "on" : "off";
86     if (!pref_) {
87         HILOG_ERROR("pref_ is null!");
88         return RET_ERR_NULLPTR;
89     }
90 
91     pref_->PutString("AccessibilityStartFromAtoHos", strState);
92     pref_->Flush();
93     return RET_OK;
94 }
95 
SetMouseKeyState(const bool state)96 RetError AccessibilitySettingsConfig::SetMouseKeyState(const bool state)
97 {
98     HILOG_DEBUG("state = [%{public}s]", state ? "True" : "False");
99     isMouseKeyState_ = state;
100     return SetStatePref(STATE::MOUSEKEY) ? RET_OK : RET_ERR_FAILED;
101 }
102 
SetMouseAutoClick(const int32_t time)103 RetError AccessibilitySettingsConfig::SetMouseAutoClick(const int32_t time)
104 {
105     HILOG_DEBUG("time = [%{public}d]", time);
106     mouseAutoClick_ = time;
107     if (!pref_) {
108         HILOG_ERROR("pref_ is null!");
109         return RET_ERR_NULLPTR;
110     }
111 
112     pref_->PutInt("MouseAutoClick", mouseAutoClick_);
113     pref_->Flush();
114     return RET_OK;
115 }
116 
SetShortkeyTarget(const std::string & name)117 RetError AccessibilitySettingsConfig::SetShortkeyTarget(const std::string &name)
118 {
119     HILOG_DEBUG("name = [%{public}s]", name.c_str());
120     shortkeyTarget_ = name;
121     if (!pref_) {
122         HILOG_ERROR("pref_ is null!");
123         return RET_ERR_NULLPTR;
124     }
125 
126     pref_->PutString("ShortkeyTarget", shortkeyTarget_);
127     pref_->Flush();
128     return RET_OK;
129 }
130 
SetShortkeyMultiTarget(const std::vector<std::string> & name)131 RetError AccessibilitySettingsConfig::SetShortkeyMultiTarget(const std::vector<std::string> &name)
132 {
133     HILOG_DEBUG();
134     shortkeyMultiTarget_ = name;
135     if (!pref_) {
136         HILOG_ERROR("pref_ is null!");
137         return RET_ERR_NULLPTR;
138     }
139 
140     std::string stringOut = "";
141     VectorToString(name, stringOut);
142     pref_->PutString("ShortkeyMultiTarget", stringOut);
143     pref_->Flush();
144     return RET_OK;
145 }
146 
SetShortkeyMultiTargetInPkgRemove(const std::string & name)147 RetError AccessibilitySettingsConfig::SetShortkeyMultiTargetInPkgRemove(const std::string &name)
148 {
149     HILOG_DEBUG();
150     if (!pref_) {
151         HILOG_ERROR("pref_ is null!");
152         return RET_ERR_NULLPTR;
153     }
154     for (auto iter = shortkeyMultiTarget_.begin(); iter != shortkeyMultiTarget_.end(); ++iter) {
155         if (*iter == name) {
156             shortkeyMultiTarget_.erase(iter);
157             std::string stringOut = "";
158             VectorToString(shortkeyMultiTarget_, stringOut);
159             pref_->PutString("ShortkeyMultiTarget", stringOut);
160             pref_->Flush();
161             break;
162         }
163     }
164     return RET_OK;
165 }
166 
SetHighContrastTextState(const bool state)167 RetError AccessibilitySettingsConfig::SetHighContrastTextState(const bool state)
168 {
169     HILOG_DEBUG("state = [%{public}s]", state ? "True" : "False");
170     highContrastTextState_ = state;
171     return SetStatePref(STATE::HIGHCONTRASTTEXT) ? RET_OK : RET_ERR_FAILED;
172 }
173 
SetInvertColorState(const bool state)174 RetError AccessibilitySettingsConfig::SetInvertColorState(const bool state)
175 {
176     HILOG_DEBUG("state = [%{public}s]", state ? "True" : "False");
177     invertColorState_ = state;
178     return SetStatePref(STATE::INVERTCOLORSTATE) ? RET_OK : RET_ERR_FAILED;
179 }
180 
SetAnimationOffState(const bool state)181 RetError AccessibilitySettingsConfig::SetAnimationOffState(const bool state)
182 {
183     HILOG_DEBUG("state = [%{public}s]", state ? "True" : "False");
184     animationOffState_ = state;
185     return SetStatePref(STATE::ANIMATIONOFF) ? RET_OK : RET_ERR_FAILED;
186 }
187 
SetAudioMonoState(const bool state)188 RetError AccessibilitySettingsConfig::SetAudioMonoState(const bool state)
189 {
190     HILOG_DEBUG("state = [%{public}s]", state ? "True" : "False");
191     audioMonoState_ = state;
192     return SetStatePref(STATE::AUDIOMONO) ? RET_OK : RET_ERR_FAILED;
193 }
194 
SetDaltonizationState(const bool state)195 RetError AccessibilitySettingsConfig::SetDaltonizationState(const bool state)
196 {
197     HILOG_DEBUG("state = [%{public}s]", state ? "True" : "False");
198     daltonizationState_ = state;
199     return SetStatePref(STATE::DALTONIZATIONSTATE) ? RET_OK : RET_ERR_FAILED;
200 }
201 
SetDaltonizationColorFilter(const uint32_t filter)202 RetError AccessibilitySettingsConfig::SetDaltonizationColorFilter(const uint32_t filter)
203 {
204     HILOG_DEBUG("filter = [%{public}u]", filter);
205     daltonizationColorFilter_ = filter;
206     if (!pref_) {
207         HILOG_ERROR("pref_ is null!");
208         return RET_ERR_NULLPTR;
209     }
210 
211     pref_->PutInt("daltonizationColorFilter", static_cast<int32_t>(daltonizationColorFilter_));
212     pref_->Flush();
213     return RET_OK;
214 }
215 
SetContentTimeout(const uint32_t time)216 RetError AccessibilitySettingsConfig::SetContentTimeout(const uint32_t time)
217 {
218     HILOG_DEBUG("time = [%{public}u]", time);
219     contentTimeout_ = time;
220     if (!pref_) {
221         HILOG_ERROR("pref_ is null!");
222         return RET_ERR_NULLPTR;
223     }
224 
225     pref_->PutInt("contentTimeout", static_cast<int32_t>(contentTimeout_));
226     pref_->Flush();
227     return RET_OK;
228 }
229 
SetBrightnessDiscount(const float discount)230 RetError AccessibilitySettingsConfig::SetBrightnessDiscount(const float discount)
231 {
232     HILOG_DEBUG("discount = [%{public}f]", discount);
233     brightnessDiscount_ = discount;
234     if (!pref_) {
235         HILOG_ERROR("pref_ is null!");
236         return RET_ERR_NULLPTR;
237     }
238 
239     pref_->PutFloat("brightnessDiscount", brightnessDiscount_);
240     pref_->Flush();
241     return RET_OK;
242 }
243 
SetAudioBalance(const float balance)244 RetError AccessibilitySettingsConfig::SetAudioBalance(const float balance)
245 {
246     HILOG_DEBUG("balance = [%{public}f]", balance);
247     audioBalance_ = balance;
248     if (!pref_) {
249         HILOG_ERROR("pref_ is null!");
250         return RET_ERR_NULLPTR;
251     }
252 
253     pref_->PutFloat("audioBalance", audioBalance_);
254     pref_->Flush();
255     return RET_OK;
256 }
257 
SetClickResponseTime(const uint32_t time)258 RetError AccessibilitySettingsConfig::SetClickResponseTime(const uint32_t time)
259 {
260     HILOG_DEBUG("clickResponseTime = [%{public}u]", time);
261     clickResponseTime_ = time;
262     if (!pref_) {
263         HILOG_ERROR("pref_ is null!");
264         return RET_ERR_NULLPTR;
265     }
266 
267     pref_->PutInt("clickResponseTime", clickResponseTime_);
268     pref_->Flush();
269     return RET_OK;
270 }
271 
SetIgnoreRepeatClickState(const bool state)272 RetError AccessibilitySettingsConfig::SetIgnoreRepeatClickState(const bool state)
273 {
274     HILOG_DEBUG("state = [%{public}s]", state ? "True" : "False");
275     ignoreRepeatClickState_ = state;
276     return SetStatePref(STATE::IGNOREREPEATCLICKSTATE) ? RET_OK : RET_ERR_FAILED;
277 }
278 
SetIgnoreRepeatClickTime(const uint32_t time)279 RetError AccessibilitySettingsConfig::SetIgnoreRepeatClickTime(const uint32_t time)
280 {
281     HILOG_DEBUG("ignoreRepeatClickTime = [%{public}u]", time);
282     ignoreRepeatClickTime_ = time;
283     if (!pref_) {
284         HILOG_ERROR("pref_ is null!");
285         return RET_ERR_NULLPTR;
286     }
287 
288     pref_->PutInt("ignoreRepeatClickTime", time);
289     pref_->Flush();
290     return RET_OK;
291 }
292 
SetCaptionProperty(const AccessibilityConfig::CaptionProperty & caption)293 RetError AccessibilitySettingsConfig::SetCaptionProperty(const AccessibilityConfig::CaptionProperty& caption)
294 {
295     HILOG_DEBUG();
296     captionProperty_ = caption;
297 
298     if (!pref_) {
299         HILOG_ERROR("pref_ is null!");
300         return RET_ERR_NULLPTR;
301     }
302     const std::string& FONTFAMILY = captionProperty_.GetFontFamily();
303     int32_t FONTSCALE = captionProperty_.GetFontScale();
304     uint32_t FONTCOLOR = captionProperty_.GetFontColor();
305     const std::string& FONTEDGETYPE = captionProperty_.GetFontEdgeType();
306     uint32_t BACKGROUNDCOLOR = captionProperty_.GetBackgroundColor();
307     uint32_t WINDOWCOLOR = captionProperty_.GetWindowColor();
308 
309     pref_->PutString("fontFamily", FONTFAMILY);
310     pref_->PutInt("fontColor", static_cast<int32_t>(FONTCOLOR));
311     pref_->PutString("fontEdgeType", FONTEDGETYPE);
312     pref_->PutInt("backgroundColor", static_cast<int32_t>(BACKGROUNDCOLOR));
313     pref_->PutInt("windowColor", static_cast<int32_t>(WINDOWCOLOR));
314     pref_->PutInt("fontScale", FONTSCALE);
315     pref_->Flush();
316     return RET_OK;
317 }
318 
SetStatePrefExec(int32_t type)319 bool AccessibilitySettingsConfig::SetStatePrefExec(int32_t type)
320 {
321     bool ret = true;
322     std::string strValue;
323     if (type == STATE::ACCESSIBILITY) {
324         strValue = StateChange(enabled_);
325         pref_->PutString("accessible", strValue);
326     } else if (type == STATE::TOUCHGUIDE) {
327         strValue = StateChange(eventTouchGuideState_);
328         pref_->PutString("touchGuide", strValue);
329     } else if (type == STATE::GESTURE) {
330         strValue = StateChange(gesturesSimulation_);
331         pref_->PutString("gesture", strValue);
332     } else if (type == STATE::KEYEVENT) {
333         strValue = StateChange(filteringKeyEvents_);
334         pref_->PutString("keyEventObserver", strValue);
335     } else if (type == STATE::CAPTION) {
336         strValue = StateChange(isCaptionState_);
337         pref_->PutString("CaptionState", strValue);
338     } else if (type == STATE::SCREENMAGNIFIER) {
339         strValue = StateChange(isScreenMagnificationState_);
340         pref_->PutString("ScreenMagnification", strValue);
341     } else if (type == STATE::SHORTKEY) {
342         strValue = StateChange(isShortKeyState_);
343         pref_->PutString("ShortKey", strValue);
344     } else if (type == STATE::MOUSEKEY) {
345         strValue = StateChange(isMouseKeyState_);
346         pref_->PutString("MouseKey", strValue);
347     } else if (type == STATE::HIGHCONTRASTTEXT) {
348         strValue = StateChange(highContrastTextState_);
349         pref_->PutString("highContrastText", strValue);
350     } else if (type == STATE::DALTONIZATIONSTATE) {
351         strValue = StateChange(daltonizationState_);
352         pref_->PutString("daltonizationState", strValue);
353     } else if (type == STATE::INVERTCOLORSTATE) {
354         strValue = StateChange(invertColorState_);
355         pref_->PutString("invertColor", strValue);
356     } else if (type == STATE::ANIMATIONOFF) {
357         strValue = StateChange(animationOffState_);
358         pref_->PutString("animationOff", strValue);
359     } else if (type == STATE::AUDIOMONO) {
360         strValue = StateChange(audioMonoState_);
361         pref_->PutString("audioMono", strValue);
362     } else if (type == STATE::IGNOREREPEATCLICKSTATE) {
363         pref_->PutString("ignoreRepeatClickState", StateChange(ignoreRepeatClickState_));
364     } else {
365         ret = false;
366         HILOG_ERROR("invalid parameter type = [%{public}d]", type);
367     }
368     return ret;
369 }
370 
SetStatePref(int32_t type)371 bool AccessibilitySettingsConfig::SetStatePref(int32_t type)
372 {
373     HILOG_DEBUG("type = [%{public}d]", type);
374     if (!pref_) {
375         HILOG_ERROR("pref_ is null!");
376         return false;
377     }
378 
379     bool ret = SetStatePrefExec(type);
380     if (ret) {
381         pref_->Flush();
382     }
383     return ret;
384 }
385 
StateChange(bool state)386 std::string AccessibilitySettingsConfig::StateChange(bool state)
387 {
388     HILOG_DEBUG("state = [%{public}s]", state ? "True" : "False");
389     return state ? "on" : "off";
390 }
391 
GetCaptionState() const392 bool AccessibilitySettingsConfig::GetCaptionState() const
393 {
394     return isCaptionState_;
395 }
396 
GetScreenMagnificationState() const397 bool AccessibilitySettingsConfig::GetScreenMagnificationState() const
398 {
399     return isScreenMagnificationState_;
400 }
401 
GetShortKeyState() const402 bool AccessibilitySettingsConfig::GetShortKeyState() const
403 {
404     return isShortKeyState_;
405 }
406 
GetMouseKeyState() const407 bool AccessibilitySettingsConfig::GetMouseKeyState() const
408 {
409     return isMouseKeyState_;
410 }
411 
GetMouseAutoClick() const412 int32_t AccessibilitySettingsConfig::GetMouseAutoClick() const
413 {
414     return mouseAutoClick_;
415 }
416 
GetShortkeyTarget() const417 const std::string &AccessibilitySettingsConfig::GetShortkeyTarget() const
418 {
419     return shortkeyTarget_;
420 }
421 
GetShortkeyMultiTarget() const422 const std::vector<std::string> &AccessibilitySettingsConfig::GetShortkeyMultiTarget() const
423 {
424     return shortkeyMultiTarget_;
425 }
426 
GetHighContrastTextState() const427 bool AccessibilitySettingsConfig::GetHighContrastTextState() const
428 {
429     return highContrastTextState_;
430 }
431 
GetInvertColorState() const432 bool AccessibilitySettingsConfig::GetInvertColorState() const
433 {
434     return invertColorState_;
435 }
436 
GetAnimationOffState() const437 bool AccessibilitySettingsConfig::GetAnimationOffState() const
438 {
439     return animationOffState_;
440 }
441 
GetAudioMonoState() const442 bool AccessibilitySettingsConfig::GetAudioMonoState() const
443 {
444     return audioMonoState_;
445 }
446 
GetDaltonizationState() const447 bool AccessibilitySettingsConfig::GetDaltonizationState() const
448 {
449     return daltonizationState_;
450 }
451 
GetDaltonizationColorFilter() const452 uint32_t AccessibilitySettingsConfig::GetDaltonizationColorFilter() const
453 {
454     return daltonizationColorFilter_;
455 }
456 
GetContentTimeout() const457 uint32_t AccessibilitySettingsConfig::GetContentTimeout() const
458 {
459     return contentTimeout_;
460 }
461 
GetBrightnessDiscount() const462 float AccessibilitySettingsConfig::GetBrightnessDiscount() const
463 {
464     return brightnessDiscount_;
465 }
466 
GetAudioBalance() const467 float AccessibilitySettingsConfig::GetAudioBalance() const
468 {
469     return audioBalance_;
470 }
471 
GetEnabledState() const472 bool AccessibilitySettingsConfig::GetEnabledState() const
473 {
474     return enabled_;
475 }
476 
GetTouchGuideState() const477 bool AccessibilitySettingsConfig::GetTouchGuideState() const
478 {
479     return eventTouchGuideState_;
480 }
481 
GetGestureState() const482 bool AccessibilitySettingsConfig::GetGestureState() const
483 {
484     return gesturesSimulation_;
485 }
486 
GetKeyEventObserverState() const487 bool AccessibilitySettingsConfig::GetKeyEventObserverState() const
488 {
489     return filteringKeyEvents_;
490 }
491 
GetCaptionProperty() const492 const AccessibilityConfig::CaptionProperty &AccessibilitySettingsConfig::GetCaptionProperty() const
493 {
494     return captionProperty_;
495 };
496 
GetClickResponseTime() const497 uint32_t AccessibilitySettingsConfig::GetClickResponseTime() const
498 {
499     return clickResponseTime_;
500 }
501 
GetIgnoreRepeatClickState() const502 bool AccessibilitySettingsConfig::GetIgnoreRepeatClickState() const
503 {
504     return ignoreRepeatClickState_;
505 }
506 
GetIgnoreRepeatClickTime() const507 uint32_t AccessibilitySettingsConfig::GetIgnoreRepeatClickTime() const
508 {
509     return ignoreRepeatClickTime_;
510 }
511 
GetStartFromAtoHosState()512 bool AccessibilitySettingsConfig::GetStartFromAtoHosState()
513 {
514     HILOG_DEBUG();
515     if (!pref_) {
516         HILOG_ERROR("Input Parameter is nullptr");
517         return false;
518     }
519 
520     std::string strValue = pref_->GetString("AccessibilityStartFromAtoHos", "");
521     return (strValue == "off") ? false : true;
522 }
523 
GetConfigState()524 uint32_t AccessibilitySettingsConfig::GetConfigState()
525 {
526     HILOG_DEBUG();
527     uint32_t state = 0;
528     if (isCaptionState_) {
529         state |= STATE_CAPTION_ENABLED;
530     }
531 
532     if (isScreenMagnificationState_) {
533         state |= STATE_SCREENMAGNIFIER_ENABLED;
534     }
535 
536     if (isMouseKeyState_) {
537         state |= STATE_MOUSEKEY_ENABLED;
538     }
539 
540     if (isShortKeyState_) {
541         state |= STATE_SHORTKEY_ENABLED;
542     }
543 
544     if (highContrastTextState_) {
545         state |= STATE_HIGHCONTRAST_ENABLED;
546     }
547 
548     if (daltonizationState_) {
549         state |= STATE_DALTONIZATION_STATE_ENABLED;
550     }
551 
552     if (invertColorState_) {
553         state |= STATE_INVETRTCOLOR_ENABLED;
554     }
555 
556     if (animationOffState_) {
557         state |= STATE_ANIMATIONOFF_ENABLED;
558     }
559 
560     if (audioMonoState_) {
561         state |= STATE_AUDIOMONO_ENABLED;
562     }
563 
564     if (ignoreRepeatClickState_) {
565         state |= STATE_IGNORE_REPEAT_CLICK_ENABLED;
566     }
567     return state;
568 }
569 
InitCaption()570 void AccessibilitySettingsConfig::InitCaption()
571 {
572     HILOG_DEBUG();
573     if (!pref_) {
574         HILOG_ERROR("Input Parameter is nullptr");
575         return;
576     }
577 
578     std::string strValue = pref_->GetString("CaptionState", "");
579     HILOG_DEBUG(" pref_->GetString() = %{public}s.", strValue.c_str());
580     if (!std::strcmp(strValue.c_str(), "on")) {
581         isCaptionState_ = true;
582     } else {
583         isCaptionState_ = false;
584     }
585 
586     std::string FONTFAMILY = pref_->GetString("fontFamily", "default");
587     HILOG_DEBUG("fontFamily = %{public}s.", FONTFAMILY.c_str());
588 
589     int32_t FONTSCALE =  static_cast<int32_t>(pref_->GetInt("fontScale", 100));
590     HILOG_DEBUG("fontScale = %{public}d.", FONTSCALE);
591 
592     uint32_t FONTCOLOR = static_cast<uint32_t>(pref_->GetInt("fontColor", DEFAULT_COLOR));
593     HILOG_DEBUG("fontColor = 0x%{public}x.", FONTCOLOR);
594 
595     std::string FONTEDGETYPE = pref_->GetString("fontEdgeType", "none");
596     HILOG_DEBUG("FONTEDGETYPE = 0x%{public}s.", FONTEDGETYPE.c_str());
597 
598     uint32_t BACKGROUNDCOLOR = static_cast<uint32_t>(pref_->GetInt("backgroundColor", DEFAULT_COLOR));
599     HILOG_DEBUG("BACKGROUNDCOLOR = 0x%{public}x.", BACKGROUNDCOLOR);
600 
601     uint32_t WINDOWCOLOR = static_cast<uint32_t>(pref_->GetInt("windowColor", DEFAULT_COLOR));
602     HILOG_DEBUG("WINDOWCOLOR = 0x%{public}x.", WINDOWCOLOR);
603 
604     captionProperty_.SetFontFamily(FONTFAMILY);
605     captionProperty_.SetFontScale(FONTSCALE);
606     captionProperty_.SetFontColor(FONTCOLOR);
607     captionProperty_.SetFontEdgeType(FONTEDGETYPE);
608     captionProperty_.SetBackgroundColor(BACKGROUNDCOLOR);
609     captionProperty_.SetWindowColor(WINDOWCOLOR);
610 }
611 
InitSetting()612 void AccessibilitySettingsConfig::InitSetting()
613 {
614     HILOG_DEBUG();
615     if (!pref_) {
616         HILOG_ERROR("Input Parameter is nullptr");
617         return;
618     }
619 
620     std::string strValue = pref_->GetString("ScreenMagnification", "");
621     isScreenMagnificationState_ = std::strcmp(strValue.c_str(), "on") ? false : true;
622 
623     strValue = pref_->GetString("MouseKey", "");
624     isMouseKeyState_ = std::strcmp(strValue.c_str(), "on") ? false : true;
625 
626     strValue = pref_->GetString("ShortKey", "");
627     isShortKeyState_ = std::strcmp(strValue.c_str(), "on") ? false : true;
628 
629     strValue = pref_->GetString("animationOff", "");
630     animationOffState_ = std::strcmp(strValue.c_str(), "on") ? false : true;
631 
632     strValue = pref_->GetString("invertColor", "");
633     invertColorState_ = std::strcmp(strValue.c_str(), "on") ? false : true;
634 
635     strValue = pref_->GetString("highContrastText", "");
636     highContrastTextState_ = std::strcmp(strValue.c_str(), "on") ? false : true;
637 
638     strValue = pref_->GetString("daltonizationState", "");
639     daltonizationState_ = std::strcmp(strValue.c_str(), "on") ? false : true;
640 
641     strValue = pref_->GetString("audioMono", "");
642     audioMonoState_ = std::strcmp(strValue.c_str(), "on") ? false : true;
643 
644     strValue = pref_->GetString("ignoreRepeatClickState", "");
645     ignoreRepeatClickState_ = std::strcmp(strValue.c_str(), "on") ? false : true;
646 
647     shortkeyTarget_ = pref_->GetString("ShortkeyTarget", "none");
648 
649     std::string tmpString = pref_->GetString("ShortkeyMultiTarget", "");
650     StringToVector(tmpString, shortkeyMultiTarget_);
651 
652     mouseAutoClick_ = static_cast<int32_t>(pref_->GetInt("MouseAutoClick", -1));
653     daltonizationColorFilter_ = static_cast<uint32_t>(pref_->GetInt("daltonizationColorFilter", 0));
654     contentTimeout_ = static_cast<uint32_t>(pref_->GetInt("contentTimeout", 0));
655     brightnessDiscount_ = static_cast<float>(pref_->GetFloat("brightnessDiscount", 1.0));
656     audioBalance_ = static_cast<float>(pref_->GetFloat("audioBalance", 0));
657     clickResponseTime_ = static_cast<uint32_t>(pref_->GetInt("clickResponseTime", 0));
658     ignoreRepeatClickTime_ = static_cast<uint32_t>(pref_->GetInt("ignoreRepeatClickTime", 0));
659 }
660 
InitCapability()661 void AccessibilitySettingsConfig::InitCapability()
662 {
663     HILOG_DEBUG();
664     if (!pref_) {
665         HILOG_ERROR("Input Parameter is nullptr");
666         return;
667     }
668 
669     std::string strValue = pref_->GetString("accessible", "");
670     HILOG_DEBUG("accessible = %{public}s", strValue.c_str());
671     if (!std::strcmp(strValue.c_str(), "on")) {
672         enabled_ = true;
673     } else {
674         enabled_ = false;
675     }
676 
677     strValue = pref_->GetString("touchGuide", "");
678     HILOG_DEBUG("touchGuide = %{public}s", strValue.c_str());
679     if (!std::strcmp(strValue.c_str(), "on")) {
680         eventTouchGuideState_ = true;
681     } else {
682         eventTouchGuideState_ = false;
683     }
684 
685     strValue = pref_->GetString("gesture", "");
686     HILOG_DEBUG("gesture = %{public}s", strValue.c_str());
687     if (!std::strcmp(strValue.c_str(), "on")) {
688         gesturesSimulation_ = true;
689     } else {
690         gesturesSimulation_ = false;
691     }
692 
693     strValue = pref_->GetString("keyEventObserver", "");
694     HILOG_DEBUG("keyEventObserver = %{public}s", strValue.c_str());
695     if (!std::strcmp(strValue.c_str(), "on")) {
696         filteringKeyEvents_ = true;
697     } else {
698         filteringKeyEvents_ = false;
699     }
700 }
701 
InitEnabledList()702 void AccessibilitySettingsConfig::InitEnabledList()
703 {
704     HILOG_DEBUG();
705     if (!pref_) {
706         HILOG_ERROR("Input Parameter is nullptr");
707         return;
708     }
709 
710     std::string strValue = pref_->GetString("BundleName/AbilityName/Capabilities", "");
711     HILOG_DEBUG("Capabilities = %{public}s", strValue.c_str());
712 
713     StringToVector(strValue, enabledAbilityInfos_);
714 }
715 
Init()716 void AccessibilitySettingsConfig::Init()
717 {
718     HILOG_INFO();
719     int errCode = -1;
720 
721     pref_ = NativePreferences::PreferencesHelper::GetPreferences(PREF_PATH + std::to_string(accountId_) + ".xml",
722                                                                  errCode);
723     if (errCode) {
724         Utils::RecordUnavailableEvent(A11yUnavailableEvent::READ_EVENT, A11yError::ERROR_READ_FAILED);
725         HILOG_ERROR("GetPreferences failed! errCode(%{public}d).", errCode);
726         return;
727     }
728 
729     InitCaption();
730     InitSetting();
731 }
732 
VectorToString(const std::vector<std::string> & vectorVal,std::string & stringOut)733 void AccessibilitySettingsConfig::VectorToString(const std::vector<std::string> &vectorVal, std::string &stringOut)
734 {
735     HILOG_DEBUG();
736     int32_t i = 0;
737     for (auto& var : vectorVal) {
738         if (i > 0) {
739             stringOut = stringOut + ',';
740         }
741         stringOut = stringOut + var.c_str();
742         i++;
743     }
744     HILOG_DEBUG("stringOUT = %{public}s .", stringOut.c_str());
745 }
746 
StringToVector(const std::string & stringIn,std::vector<std::string> & vectorResult)747 void AccessibilitySettingsConfig::StringToVector(const std::string &stringIn, std::vector<std::string> &vectorResult)
748 {
749     HILOG_DEBUG();
750     int32_t strLength = static_cast<int32_t>(stringIn.size());
751     std::vector<int32_t> position;
752 
753     if (strLength == 0) {
754         return;
755     }
756 
757     for (int32_t j = 0; j < strLength; j++) {
758         if (stringIn[j] == ',') {
759             position.push_back(j);
760         }
761     }
762 
763     int32_t wrodCount = static_cast<int32_t>(position.size());
764     if ((wrodCount == 0) && (strLength > 0)) {
765         vectorResult.push_back(stringIn);
766     } else {
767         int32_t startWrod = 0;
768         int32_t length = 0;
769         for (int32_t i = 0; i <= wrodCount; i++) {
770             if (i == 0) {
771                 length = position[i];
772                 vectorResult.push_back(stringIn.substr(startWrod, length)); // First string
773             } else if (i < wrodCount) {
774                 startWrod = position[i - 1] + 1;
775                 length = position[i] - position[i - 1] - 1;
776                 vectorResult.push_back(stringIn.substr(startWrod, length)); // Second string to last-1 string
777             } else {
778                 startWrod = position[i - 1] + 1;
779                 length = strLength - position[i - 1] - 1;
780                 vectorResult.push_back(stringIn.substr(startWrod, length)); // Last string
781             }
782         }
783     }
784     HILOG_DEBUG("strLength = %{public}d, wrodCount = %{public}d, stringIn : %{public}s",
785         strLength, wrodCount, stringIn.c_str());
786     for (auto& var : vectorResult) {
787         HILOG_DEBUG("vectorResult = %{public}s ", var.c_str());
788     }
789 }
790 
GetEnabledAbilityInfos()791 const std::vector<std::string> &AccessibilitySettingsConfig::GetEnabledAbilityInfos()
792 {
793     HILOG_DEBUG();
794     return enabledAbilityInfos_;
795 }
796 
UpdateEnabledAbilities(const std::vector<std::string> & vecvalue)797 void AccessibilitySettingsConfig::UpdateEnabledAbilities(const std::vector<std::string> &vecvalue)
798 {
799     HILOG_DEBUG();
800     enabledAbilityInfos_ = vecvalue;
801     if (!pref_) {
802         HILOG_ERROR("pref_ is null!");
803         return;
804     }
805     std::string stringOut = "";
806     VectorToString(enabledAbilityInfos_, stringOut);
807     pref_->PutString("BundleName/AbilityName/Capabilities", stringOut);
808     pref_->Flush();
809 }
810 
ClearData()811 void AccessibilitySettingsConfig::ClearData()
812 {
813     HILOG_DEBUG();
814     int errCode = -1;
815     errCode = NativePreferences::PreferencesHelper::DeletePreferences(PREF_PATH + std::to_string(accountId_) + ".xml");
816     if (errCode) {
817         HILOG_ERROR("DeletePreferences failed!");
818     }
819 
820     pref_ = nullptr;
821 }
822 } // namespace Accessibility
823 } // namespace OHOS