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 #ifndef ACCESSIBILITY_CONFIG_H
17 #define ACCESSIBILITY_CONFIG_H
18 
19 #include <memory>
20 #include "accessibility_caption.h"
21 #include "accessibility_singleton.h"
22 #include "accessibility_def.h"
23 
24 namespace OHOS {
25 namespace AccessibilityConfig {
26 enum CONFIG_ID : int32_t {
27     CONFIG_HIGH_CONTRAST_TEXT = 0,
28     CONFIG_INVERT_COLOR,
29     CONFIG_DALTONIZATION_COLOR_FILTER,
30     CONFIG_CONTENT_TIMEOUT,
31     CONFIG_ANIMATION_OFF,
32     CONFIG_BRIGHTNESS_DISCOUNT,
33     CONFIG_AUDIO_MONO,
34     CONFIG_AUDIO_BALANCE,
35     CONFIG_MOUSE_KEY,
36     CONFIG_SHORT_KEY,
37     CONFIG_CAPTION_STATE,
38     CONFIG_CAPTION_STYLE,
39     CONFIG_SCREEN_MAGNIFICATION,
40     CONFIG_SHORT_KEY_TARGET,
41     CONFIG_MOUSE_AUTOCLICK,
42     CONFIG_DALTONIZATION_STATE,
43     CONIFG_CLICK_RESPONSE_TIME,
44     CONFIG_IGNORE_REPEAT_CLICK_STATE,
45     CONFIG_IGNORE_REPEAT_CLICK_TIME,
46     CONFIG_SHORT_KEY_MULTI_TARGET,
47     CONFIG_ID_MAX,
48 };
49 
50 struct ConfigValue {
51     bool highContrastText;
52     bool invertColor;
53     bool animationOff;
54     bool screenMagnifier;
55     bool audioMono;
56     bool mouseKey;
57     bool shortkey;
58     bool captionState;
59     bool daltonizationState;
60     DALTONIZATION_TYPE daltonizationColorFilter;
61     uint32_t contentTimeout;
62     int32_t mouseAutoClick;
63     float brightnessDiscount;
64     float audioBalance;
65     std::string shortkey_target;
66     CaptionProperty captionStyle;
67     CLICK_RESPONSE_TIME clickResponseTime;
68     bool ignoreRepeatClickState;
69     IGNORE_REPEAT_CLICK_TIME ignoreRepeatClickTime;
70     std::vector<std::string> shortkeyMultiTarget;
71 };
72 
73 class AccessibilityConfigObserver {
74 public:
75     /**
76      * @brief Destruct
77      */
78     virtual ~AccessibilityConfigObserver() = default;
79 
80     /**
81      * @brief Called when the config value changed.
82      * @param id the id of config.
83      * @param value the value of config.
84      */
85     virtual void OnConfigChanged(const CONFIG_ID id, const ConfigValue &value) = 0;
86 };
87 
88 class AccessibilityEnableAbilityListsObserver {
89 public:
90     /**
91      * @brief Destruct
92      */
93     virtual ~AccessibilityEnableAbilityListsObserver() = default;
94 
95     /**
96      * @brief Called when the enable ability lists changed.
97      */
98     virtual void OnEnableAbilityListsStateChanged() = 0;
99 
100     /**
101      * @brief Called when the enable ability lists changed.
102      */
103     virtual void OnInstallAbilityListsStateChanged() = 0;
104 };
105 
106 class AccessibilityConfig {
107     ACCESSIBILITY_DECLARE_SINGLETON(AccessibilityConfig)
108 public:
109     /**
110      * @brief Initialize the run context.
111      * @return Return true if successfully, else return false.
112      */
113     bool InitializeContext();
114 
115     /**
116      * @brief Subscribes to accessibility config value.
117      * @param id  the config id which is observed.
118      * @param observer Indicates the observer for listening to accessibility
119      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
120      */
121     Accessibility::RetError SubscribeConfigObserver(const CONFIG_ID id,
122         const std::shared_ptr<AccessibilityConfigObserver> &observer, const bool retFlag = true);
123 
124     /**
125      * @brief Unsubscribe the accessibility config value observer.
126      * @param id  the id which is observed.
127      * @param observer Indicates the registered accessibility config observer.
128      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
129      */
130     Accessibility::RetError UnsubscribeConfigObserver(const CONFIG_ID id,
131         const std::shared_ptr<AccessibilityConfigObserver> &observer);
132 
133     /**
134      * @brief Subscribes the observer of enable Ability lists
135      * @param observer Indicates the observer for listening to enable Ability lists
136      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
137      */
138     Accessibility::RetError SubscribeEnableAbilityListsObserver(
139         const std::shared_ptr<AccessibilityEnableAbilityListsObserver> &observer);
140 
141     /**
142      * @brief Unsubscribe the observer of enable Ability lists
143      * @param observer Indicates the observer for listening to enable Ability lists
144      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
145      */
146     Accessibility::RetError UnsubscribeEnableAbilityListsObserver(
147         const std::shared_ptr<AccessibilityEnableAbilityListsObserver> &observer);
148 
149     /**
150      * @brief Enabled specified abilities
151      * @param name The string formatted by 'bundleName/abilityName'.
152      * @param capabilities The capabilities you permit.
153      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
154      */
155     Accessibility::RetError EnableAbility(const std::string &name, const uint32_t capabilities);
156 
157     /**
158      * @brief Disabled specified ability
159      * @param name The string formatted by 'bundleName/abilityName'.
160      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
161      */
162     Accessibility::RetError DisableAbility(const std::string &name);
163 
164     /**
165      * @brief Set whether to enable the magnification function
166      * @param state true:enable magnification function; false:disable magnification function
167      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
168      */
169     Accessibility::RetError SetScreenMagnificationState(const bool state);
170 
171     /**
172      * @brief Set whether to enable the function of using short key to open accessibility ability
173      * @param state true:enable short key function; false:disable short key function
174      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
175      */
176     Accessibility::RetError SetShortKeyState(const bool state);
177 
178     /**
179      * @brief Set whether to enable the mouse key function
180      * @param state true:enable mouse key function; false:disable mouse key function
181      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
182      */
183     Accessibility::RetError SetMouseKeyState(const bool state);
184 
185     /**
186      * @brief Set whether to enable the caption function
187      * @param state true:enable caption function; false:disable caption function
188      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
189      */
190     Accessibility::RetError SetCaptionsState(const bool state);
191 
192     /**
193      * @brief Set caption properties
194      * @param caption caption properties
195      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
196      */
197     Accessibility::RetError SetCaptionsProperty(const CaptionProperty &caption);
198 
199     /**
200      * @brief Set the time for the cursor to stop for the mouse to automatically perform the click action
201      * @param time The valid time is 1000ms~5000ms, otherwise the automatic mouse click action will not be enabled
202      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
203      */
204     Accessibility::RetError SetMouseAutoClick(const int32_t time);
205 
206     /**
207      * @brief Set the name of the accessibility ability to be opened by the short key
208      * @param name name The string formatted by 'bundleName/abilityName'
209      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
210      */
211     Accessibility::RetError SetShortkeyTarget(const std::string &name);
212 
213     /**
214      * @brief Set the name of the multi accessibility ability to be opened by the short key
215      * @param name name The vector string formatted by 'bundleName/abilityName'
216      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
217      */
218     Accessibility::RetError SetShortkeyMultiTarget(const std::vector<std::string> &name);
219 
220     /**
221      * @brief Set whether to enable the high-contrast text function
222      * @param state true:enable high-contrast text function; false:disable high-contrast text function
223      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
224      */
225     Accessibility::RetError SetHighContrastTextState(bool state);
226 
227     /**
228      * @brief Set whether to enable the invert color function
229      * @param state true:enable invert color function; false:disable invert color function
230      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
231      */
232     Accessibility::RetError SetInvertColorState(const bool state);
233 
234     /**
235      * @brief Set whether to enable the daltonization
236      * @param state true:enable daltonization function; false:disable daltonization function
237      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
238      */
239     Accessibility::RetError SetDaltonizationState(bool state);
240 
241     /**
242      * @brief Set daltonization color filter
243      * @param type Daltonization color filter type
244      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
245      */
246     Accessibility::RetError SetDaltonizationColorFilter(const DALTONIZATION_TYPE type);
247 
248     /**
249      * @brief Set content duration
250      * @param timer duration
251      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
252      */
253     Accessibility::RetError SetContentTimeout(const uint32_t timer);
254 
255     /**
256      * @brief Set whether to turn off animation
257      * @param state true:turn off animation; false:turn on animation
258      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
259      */
260     Accessibility::RetError SetAnimationOffState(const bool state);
261 
262     /**
263      * @brief Set brightness discount
264      * @param brightness The discount of brightness
265      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
266      */
267     Accessibility::RetError SetBrightnessDiscount(const float brightness);
268 
269     /**
270      * @brief Set whether to enable audio mono
271      * @param state true:enable audio mono; false:disable audio mono
272      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
273      */
274     Accessibility::RetError SetAudioMonoState(const bool state);
275 
276     /**
277      * @brief Set audio balance
278      * @param balance The balance of audio
279      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
280      */
281     Accessibility::RetError SetAudioBalance(const float balance);
282 
283     /**
284      * @brief Set click response time
285      * @param time The value of the click response time
286      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
287      */
288     Accessibility::RetError SetClickResponseTime(const CLICK_RESPONSE_TIME time);
289 
290     /**
291      * @brief Set ignore repeat click state
292      * @param state The value of the ignore repeat click state
293      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
294      */
295     Accessibility::RetError SetIgnoreRepeatClickState(const bool state);
296 
297     /**
298      * @brief Set ignore repeat click time
299      * @param time The value of the ignore repeat click time
300      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
301      */
302     Accessibility::RetError SetIgnoreRepeatClickTime(const IGNORE_REPEAT_CLICK_TIME time);
303 
304     /**
305      * @brief Get the status of whether the magnification function is enabled
306      * @param state(out) true:the magnification function is enabled; false:the magnification function is disabled
307      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
308      */
309     Accessibility::RetError GetScreenMagnificationState(bool &state) const;
310 
311     /**
312      * @brief Get the status of whether the use of short key to open accessibility ability is enabled
313      * @param state(out) true:the short key is enabled; false:the short key is disabled
314      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
315      */
316     Accessibility::RetError GetShortKeyState(bool &state) const;
317 
318     /**
319      * @brief Get the status of whether the mouse key function is enabled
320      * @param state(out) true:the mouse key function is enabled; false:the mouse key function is disabled
321      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
322      */
323     Accessibility::RetError GetMouseKeyState(bool &state) const;
324 
325     /**
326      * @brief Get the status of whether the caption function is enabled
327      * @param state(out) true:the caption function is enabled; false:the caption function is disabled
328      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
329      */
330     Accessibility::RetError GetCaptionsState(bool &state) const;
331 
332     /**
333      * @brief Get caption properties
334      * @param caption(out) caption properties
335      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
336      */
337     Accessibility::RetError GetCaptionsProperty(CaptionProperty &caption) const;
338 
339     /**
340      * @brief Get the time for the cursor to stop for the mouse to automatically perform the click action
341      * @param time(out) the time for the cursor to stop for the mouse to automatically perform the click action
342      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
343      */
344     Accessibility::RetError GetMouseAutoClick(int32_t &time) const;
345 
346     /**
347      * @brief Get the name of the accessibility ability to be opened by the short key
348      * @param name(out) The string formatted by 'bundleName/abilityName'
349      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
350      */
351     Accessibility::RetError GetShortkeyTarget(std::string &name) const;
352 
353     /**
354      * @brief Get the name of the multi accessibility ability to be opened by the short key
355      * @param name(out) The vector string formatted by 'bundleName/abilityName'
356      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
357      */
358     Accessibility::RetError GetShortkeyMultiTarget(std::vector<std::string> &name) const;
359 
360     /**
361      * @brief Get the status of whether the invert color function is enabled
362      * @param state(out) true:the invert color function is enabled; false:the invert color function is disabled
363      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
364      */
365     Accessibility::RetError GetInvertColorState(bool &state) const;
366 
367     /**
368      * @brief Get the status of whether the high-contrast text function is enabled
369      * @param state(out) true:the high-contrast text function is enabled;
370      *                   false:the high-contrast text function is disabled
371      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
372      */
373     Accessibility::RetError GetHighContrastTextState(bool &state) const;
374 
375     /**
376      * @brief Get the status of whether the daltonization function is enabled
377      * @param state(out) true:the daltonization function is enabled;
378      *                   false:the daltonization is disabled
379      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
380      */
381     Accessibility::RetError GetDaltonizationState(bool &state) const;
382 
383     /**
384      * @brief Get daltonization color filter
385      * @param type(out) Daltonization color filter type
386      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
387      */
388     Accessibility::RetError GetDaltonizationColorFilter(DALTONIZATION_TYPE &type) const;
389 
390     /**
391      * @brief Get content duration
392      * @param timer(out) duration
393      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
394      */
395     Accessibility::RetError GetContentTimeout(uint32_t &timer) const;
396 
397     /**
398      * @brief Get the status of whether animation is disabled
399      * @param state(out) true:animation is disabled; false:animation is enabled
400      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
401      */
402     Accessibility::RetError GetAnimationOffState(bool &state) const;
403 
404     /**
405      * @brief Get brightness discount parameter
406      * @param brightness(out) The discount parameter of brightness
407      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
408      */
409     Accessibility::RetError GetBrightnessDiscount(float &brightness) const;
410 
411     /**
412      * @brief Get the status of whether the audio mono is enabled
413      * @param state(out) true:audio mono is enabled; false:audio mono is disabled
414      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
415      */
416     Accessibility::RetError GetAudioMonoState(bool &state) const;
417 
418     /**
419      * @brief Get the value of the audio balance
420      * @param balance(out) The value of the audio balance
421      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
422      */
423     Accessibility::RetError GetAudioBalance(float &balance) const;
424 
425     /**
426      * @brief Get the value of the click response time
427      * @param time(out) The value of the click response time
428      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
429      */
430     Accessibility::RetError GetClickResponseTime(CLICK_RESPONSE_TIME &time) const;
431 
432     /**
433      * @brief Get the value of the ignore repeat click state
434      * @param time(out) The value of the ignore repeat click state
435      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
436      */
437     Accessibility::RetError GetIgnoreRepeatClickState(bool &state) const;
438 
439     /**
440      * @brief Get the value of the ignore repeat click time
441      * @param time(out) The value of the ignore repeat click time
442      * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure.
443      */
444     Accessibility::RetError GetIgnoreRepeatClickTime(IGNORE_REPEAT_CLICK_TIME &time) const;
445 
446 private:
447     class Impl;
448     std::unique_ptr<Impl> pImpl_;
449 };
450 } // namespace AccessibilityConfig
451 } // namespace OHOS
452 #endif // ACCESSIBILITY_CONFIG_H
453