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 ACCESSIBLE_ABILITY_MANAGER_SERVICE_H
17  #define ACCESSIBLE_ABILITY_MANAGER_SERVICE_H
18  
19  #include <map>
20  #include <memory>
21  #include <string>
22  
23  #include "accessibility_dumper.h"
24  #include "accessibility_def.h"
25  #include "accessible_ability_manager_service_stub.h"
26  #include "accessible_ability_manager_service_event_handler.h"
27  #include "accessibility_account_data.h"
28  #include "accessibility_common_event.h"
29  #ifdef OHOS_BUILD_ENABLE_DISPLAY_MANAGER
30  #include "accessibility_display_manager.h"
31  #endif
32  #include "accessibility_element_operator_callback_stub.h"
33  #include "accessibility_input_interceptor.h"
34  #include "accessibility_keyevent_filter.h"
35  #include "accessibility_settings.h"
36  #include "accessibility_touchEvent_injector.h"
37  #include "accessibility_window_info.h"
38  #include "bundlemgr/bundle_mgr_interface.h"
39  #include "input_manager.h"
40  #include "singleton.h"
41  #include "system_ability.h"
42  #include "window_manager_lite.h"
43  #include "accessibility_short_key.h"
44  
45  namespace OHOS {
46  namespace Accessibility {
47  class AccessibilityAccountData;
48  class TouchEventInjector;
49  class AccessibilitySettings;
50  
51  enum CallBackID {
52      STATE_CALLBACK,
53      CAPTION_PROPERTY_CALLBACK,
54      ENABLE_ABILITY_LISTS_CALLBACK,
55      CONFIG_CALLBACK
56  };
57  
58  constexpr int REQUEST_ID_INIT = 65535;
59  
60  const std::map<std::string, int32_t> AccessibilityConfigTable = {
61      {"HIGH_CONTRAST_TEXT", HIGH_CONTRAST_TEXT},
62      {"INVERT_COLOR", INVERT_COLOR},
63      {"DALTONIZATION_COLOR_FILTER", DALTONIZATION_COLOR_FILTER},
64      {"CONTENT_TIMEOUT", CONTENT_TIMEOUT},
65      {"ANIMATION_OFF", ANIMATION_OFF},
66      {"BRIGHTNESS_DISCOUNT", BRIGHTNESS_DISCOUNT},
67      {"AUDIO_MONO", AUDIO_MONO},
68      {"AUDIO_BALANCE", AUDIO_BALANCE},
69      {"MOUSE_KEY", MOUSE_KEY},
70      {"CAPTION_STATE", CAPTION_STATE},
71      {"CAPTION_STYLE", CAPTION_STYLE},
72      {"SCREEN_MAGNIFICATION", SCREEN_MAGNIFICATION},
73      {"MOUSE_AUTOCLICK", MOUSE_AUTOCLICK}
74  };
75  
76  class AccessibleAbilityManagerService : public SystemAbility, public AccessibleAbilityManagerServiceStub {
77      DECLARE_SINGLETON(AccessibleAbilityManagerService)
78      DECLEAR_SYSTEM_ABILITY(AccessibleAbilityManagerService)
79  public:
80      /* For system ability */
81      void OnStart() override;
82      void OnStop() override;
83      void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
84      void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
85      int Dump(int fd, const std::vector<std::u16string>& args) override;
86      void PostDelayUnloadTask() override;
87  
88  public:
89      /* For AccessibleAbilityManagerServiceStub */
90      RetError SendEvent(const AccessibilityEventInfo &uiEvent, const int32_t flag = 0) override;
91  
92      RetError VerifyingToKenId(const int32_t windowId, const int64_t elementId);
93  
94      uint32_t RegisterStateObserver(const sptr<IAccessibleAbilityManagerStateObserver> &callback) override;
95  
96      uint32_t RegisterCaptionObserver(const sptr<IAccessibleAbilityManagerCaptionObserver> &callback) override;
97  
98      void RegisterEnableAbilityListsObserver(
99          const sptr<IAccessibilityEnableAbilityListsObserver> &observer) override;
100  
101      RetError GetAbilityList(const uint32_t abilityTypes, const int32_t stateType,
102          std::vector<AccessibilityAbilityInfo> &infos) override;
103  
104      RetError RegisterElementOperator(const int32_t windowId,
105          const sptr<IAccessibilityElementOperator> &operation, bool isApp) override;
106  
107      RetError RegisterElementOperator(Registration parameter,
108          const sptr<IAccessibilityElementOperator> &operation, bool isApp) override;
109  
110      RetError DeregisterElementOperator(const int32_t windowId) override;
111  
112      RetError DeregisterElementOperator(const int32_t windowId, const int32_t treeId) override;
113  
114      RetError GetCaptionProperty(AccessibilityConfig::CaptionProperty &caption) override;
115      RetError SetCaptionProperty(const AccessibilityConfig::CaptionProperty &caption) override;
116      RetError SetCaptionState(const bool state) override;
117  
118      bool GetEnabledState() override;
119      RetError GetCaptionState(bool &state) override;
120      bool GetTouchGuideState() override;
121      bool GetGestureState() override;
122      bool GetKeyEventObserverState() override;
123  
124      RetError EnableAbility(const std::string &name, const uint32_t capabilities) override;
125      RetError GetEnabledAbilities(std::vector<std::string> &enabledAbilities) override;
126      RetError SetCurtainScreenUsingStatus(bool isEnable);
127      RetError DisableAbility(const std::string &name) override;
128      RetError EnableUITestAbility(const sptr<IRemoteObject>& obj) override;
129      RetError DisableUITestAbility() override;
130      int32_t GetActiveWindow() override;
131      void GetRealWindowAndElementId(int32_t& windowId, int64_t& elementId) override;
132      void GetSceneBoardInnerWinId(int32_t windowId, int64_t elementId, int32_t& innerWid) override;
133      bool FindFocusedElement(AccessibilityElementInfo &elementInfo);
134      bool ExecuteActionOnAccessibilityFocused(const ActionType &action);
135      RetError GetFocusedWindowId(int32_t &focusedWindowId) override;
136      void SetFocusWindowId(const int32_t focusWindowId);
137      void SetFocusElementId(const int64_t focusElementId);
138      int32_t GetFocusWindowId();
139      int64_t GetFocusElementId();
140      static int32_t GetTreeIdBySplitElementId(const int64_t elementId);
141      int64_t GetRootParentId(int32_t windowId, int32_t treeId) override;
142      RetError GetAllTreeId(int32_t windowId, std::vector<int32_t> &treeIds) override;
143      void SetTokenIdMapAndRootParentId(const sptr<AccessibilityWindowConnection> connection,
144          const int32_t treeId, const int64_t nodeId, const uint32_t tokenId);
145      void RemoveTreeDeathRecipient(const int32_t windowId, const int32_t treeId,
146          const sptr<AccessibilityWindowConnection> connection);
147      int32_t GenerateRequestId();
148      void GetElementOperatorConnection(sptr<AccessibilityWindowConnection> &connection,
149          const int64_t elementId, sptr<IAccessibilityElementOperator> &elementOperator);
150  private:
151      int32_t focusWindowId_ = -1;
152      int64_t focusElementId_ = -1;
153      std::atomic<int> requestId_ = REQUEST_ID_INIT;
154  public:
155      /* For inner modules */
156      bool EnableShortKeyTargetAbility(const std::string &name = "");
157      bool DisableShortKeyTargetAbility();
158      void OnShortKeyProcess();
159      void UpdateShortKeyRegister();
160  
161      void SetTouchEventInjector(const sptr<TouchEventInjector> &touchEventInjector);
162  
GetTouchEventInjector()163      inline sptr<TouchEventInjector> GetTouchEventInjector()
164      {
165          return touchEventInjector_;
166      }
167  
GetKeyEventFilter()168      inline sptr<KeyEventFilter> GetKeyEventFilter()
169      {
170          return keyEventFilter_;
171      }
172  
173      void SetKeyEventFilter(const sptr<KeyEventFilter> &keyEventFilter);
174  
175      /* For DisplayResize */
176      void NotifyDisplayResizeStateChanged(int32_t displayId, Rect& rect, float scale, float centerX, float centerY);
177  
IsServiceReady()178      inline bool IsServiceReady()
179      {
180          return isReady_;
181      }
182  
GetCurrentAccountId()183      inline int32_t GetCurrentAccountId()
184      {
185          return currentAccountId_;
186      }
187  
GetMainHandler()188      inline std::shared_ptr<AAMSEventHandler> &GetMainHandler()
189      {
190          return handler_;
191      }
192  
GetMainRunner()193      inline std::shared_ptr<AppExecFwk::EventRunner> &GetMainRunner()
194      {
195          return runner_;
196      }
197  
198      sptr<AccessibilityAccountData> GetAccountData(int32_t accountId);
199      sptr<AccessibilityAccountData> GetCurrentAccountData();
200      std::vector<int32_t> GetAllAccountIds();
201      sptr<AppExecFwk::IBundleMgr> GetBundleMgrProxy();
202  
203      /* For common event */
204      void AddedUser(int32_t accountId);
205      void RemovedUser(int32_t accountId);
206      void SwitchedUser(int32_t accountId);
207      void PackageChanged(const std::string &bundleName);
208      void PackageRemoved(const std::string &bundleName);
209      void PackageAdd(const std::string &bundleName);
210  
211      void UpdateAccessibilityManagerService();
212      void InsertWindowIdEventPair(int32_t windowId, const AccessibilityEventInfo &event);
213      bool CheckWindowIdEventExist(int32_t windowId);
214      bool CheckWindowRegister(int32_t windowId);
215  
216      // used for arkui windowId 1 map to WMS windowId
217      void FindInnerWindowId(const AccessibilityEventInfo &event, int32_t& windowId);
218      bool GetParentElementRecursively(int32_t windowId, int64_t elementId,
219          std::vector<AccessibilityElementInfo>& infos);
220  
221      // used for arkui windowId 1 map to WMS windowId
222      class ElementOperatorCallbackImpl : public AccessibilityElementOperatorCallbackStub {
223      public:
224          ElementOperatorCallbackImpl() = default;
225          ~ElementOperatorCallbackImpl() = default;
226  
227          virtual void SetSearchElementInfoByAccessibilityIdResult(const std::vector<AccessibilityElementInfo> &infos,
228              const int32_t requestId) override;
229          virtual void SetSearchElementInfoByTextResult(const std::vector<AccessibilityElementInfo> &infos,
230              const int32_t requestId) override;
231          virtual void SetFindFocusedElementInfoResult(const AccessibilityElementInfo &info,
232              const int32_t requestId) override;
233          virtual void SetFocusMoveSearchResult(const AccessibilityElementInfo &info, const int32_t requestId) override;
234          virtual void SetExecuteActionResult(const bool succeeded, const int32_t requestId) override;
235          virtual void SetCursorPositionResult(const int32_t cursorPosition, const int32_t requestId) override;
236  
237      private:
238          ffrt::promise<void> promise_;
239          bool executeActionResult_ = false;
240          AccessibilityElementInfo accessibilityInfoResult_ = {};
241          std::vector<AccessibilityElementInfo> elementInfosResult_;
242          int32_t callCursorPosition_ = 0;
243  
244          friend class AccessibleAbilityManagerService;
245      };
246  
247      RetError SetScreenMagnificationState(const bool state) override;
248      RetError SetShortKeyState(const bool state) override;
249      RetError SetMouseKeyState(const bool state) override;
250      RetError SetMouseAutoClick(const int32_t time) override;
251      RetError SetShortkeyTarget(const std::string &name) override;
252      RetError SetShortkeyMultiTarget(const std::vector<std::string> &name) override;
253      RetError SetHighContrastTextState(const bool state) override;
254      RetError SetInvertColorState(const bool state) override;
255      RetError SetAnimationOffState(const bool state) override;
256      RetError SetAudioMonoState(const bool state) override;
257      RetError SetDaltonizationState(const bool state) override;
258      RetError SetDaltonizationColorFilter(const uint32_t filter) override;
259      RetError SetContentTimeout(const uint32_t time) override;
260      RetError SetBrightnessDiscount(const float discount) override;
261      RetError SetAudioBalance(const float balance) override;
262      RetError SetClickResponseTime(const uint32_t time) override;
263      RetError SetIgnoreRepeatClickState(const bool state) override;
264      RetError SetIgnoreRepeatClickTime(const uint32_t time) override;
265  
266      RetError GetScreenMagnificationState(bool &state) override;
267      RetError GetShortKeyState(bool &state) override;
268      RetError GetMouseKeyState(bool &state) override;
269      RetError GetMouseAutoClick(int32_t &time) override;
270      RetError GetShortkeyTarget(std::string &name) override;
271      RetError GetShortkeyMultiTarget(std::vector<std::string> &name) override;
272      RetError GetHighContrastTextState(bool &state) override;
273      RetError GetInvertColorState(bool &state) override;
274      RetError GetAnimationOffState(bool &state) override;
275      RetError GetAudioMonoState(bool &state) override;
276      RetError GetDaltonizationState(bool &state) override;
277      RetError GetDaltonizationColorFilter(uint32_t &type) override;
278      RetError GetContentTimeout(uint32_t &timer) override;
279      RetError GetBrightnessDiscount(float &brightness) override;
280      RetError GetAudioBalance(float &balance) override;
281      RetError GetClickResponseTime(uint32_t &time) override;
282      RetError GetIgnoreRepeatClickState(bool &state) override;
283      RetError GetIgnoreRepeatClickTime(uint32_t &time) override;
284      void GetAllConfigs(AccessibilityConfigData &configData) override;
285  
286      uint32_t RegisterConfigObserver(const sptr<IAccessibleAbilityManagerConfigObserver> &callback) override;
287      void UpdateConfigState();
288      void UpdateAudioBalance();
289      void UpdateBrightnessDiscount();
290      void UpdateContentTimeout();
291      void UpdateDaltonizationColorFilter();
292      void UpdateMouseAutoClick();
293      void UpdateShortkeyTarget();
294      void UpdateShortkeyMultiTarget();
295      void UpdateClickResponseTime();
296      void UpdateIgnoreRepeatClickTime();
297  
298      void UpdateInputFilter();
299      void AddRequestId(int32_t windowId, int32_t treeId, int32_t requestId,
300          sptr<IAccessibilityElementOperatorCallback> callback);
301      void RemoveRequestId(int32_t requestId) override;
302      void OnDataClone();
303  
304  private:
305      void StopCallbackWait(int32_t windowId);
306      void StopCallbackWait(int32_t windowId, int32_t treeId);
307      RetError CheckCallingUid();
308      sptr<AccessibilityWindowConnection> GetRealIdConnection();
309      bool FindFocusedElementByConnection(sptr<AccessibilityWindowConnection> connection,
310          AccessibilityElementInfo &elementInfo);
311      bool SetTargetAbility(const int32_t targetAbilityValue);
312      RetError RegisterElementOperatorChildWork(const Registration &parameter, const int32_t treeId,
313          const int64_t nodeId, const sptr<IAccessibilityElementOperator> &operation,
314          const uint32_t tokenId, bool isApp);
315      void IsCheckWindowIdEventExist(const int32_t windowId);
316      class StateCallbackDeathRecipient final : public IRemoteObject::DeathRecipient {
317      public:
318          StateCallbackDeathRecipient() = default;
319          ~StateCallbackDeathRecipient() final = default;
320          DISALLOW_COPY_AND_MOVE(StateCallbackDeathRecipient);
321  
322          void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
323      };
324  
325      class InteractionOperationDeathRecipient final : public IRemoteObject::DeathRecipient {
326      public:
InteractionOperationDeathRecipient(int32_t windowId,int32_t accountId)327          InteractionOperationDeathRecipient(int32_t windowId, int32_t accountId) : windowId_(windowId),
328              accountId_(accountId) {};
InteractionOperationDeathRecipient(int32_t windowId,int32_t treeId,int32_t accountId)329          InteractionOperationDeathRecipient(int32_t windowId, int32_t treeId, int32_t accountId) : windowId_(windowId),
330              treeId_(treeId), accountId_(accountId) {};
331          ~InteractionOperationDeathRecipient() final = default;
332          DISALLOW_COPY_AND_MOVE(InteractionOperationDeathRecipient);
333  
334          void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
335          int32_t windowId_ = INVALID_WINDOW_ID;
336          int32_t treeId_ = INVALID_TREE_ID;
337          int32_t accountId_ = 0;
338      };
339  
340      class CaptionPropertyCallbackDeathRecipient final : public IRemoteObject::DeathRecipient {
341      public:
342          CaptionPropertyCallbackDeathRecipient() = default;
343          ~CaptionPropertyCallbackDeathRecipient() final = default;
344          DISALLOW_COPY_AND_MOVE(CaptionPropertyCallbackDeathRecipient);
345  
346          void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
347      };
348  
349      class EnableAbilityListsObserverDeathRecipient final : public IRemoteObject::DeathRecipient {
350      public:
351          EnableAbilityListsObserverDeathRecipient() = default;
352          ~EnableAbilityListsObserverDeathRecipient() final = default;
353          DISALLOW_COPY_AND_MOVE(EnableAbilityListsObserverDeathRecipient);
354  
355          void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
356      };
357  
358      bool Init();
359      void InitInnerResource();
360  
361      class ConfigCallbackDeathRecipient final : public IRemoteObject::DeathRecipient {
362      public:
363          ConfigCallbackDeathRecipient() = default;
364          ~ConfigCallbackDeathRecipient() final = default;
365          DISALLOW_COPY_AND_MOVE(ConfigCallbackDeathRecipient);
366  
367          void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
368      };
369  
370      class BundleManagerDeathRecipient final : public IRemoteObject::DeathRecipient {
371      public:
372          BundleManagerDeathRecipient() = default;
373          ~BundleManagerDeathRecipient() final = default;
374          DISALLOW_COPY_AND_MOVE(BundleManagerDeathRecipient);
375  
376          void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
377      };
378  
379      class StateObservers {
380      public:
381          StateObservers() = default;
382          ~StateObservers() = default;
383          void AddStateObserver(const sptr<IAccessibleAbilityManagerStateObserver>& stateObserver);
384          void OnStateObservers(uint32_t state);
385          void RemoveStateObserver(const wptr<IRemoteObject>& remote);
386          void Clear();
387      private:
388          std::vector<sptr<IAccessibleAbilityManagerStateObserver>> observersList_;
389          ffrt::mutex stateObserversMutex_;
390      };
391  
392      RetError InnerEnableAbility(const std::string &name, const uint32_t capabilities);
393      RetError InnerDisableAbility(const std::string &name);
394  
395      sptr<AccessibilityWindowConnection> GetAccessibilityWindowConnection(int32_t windowId);
396      void ClearFocus(int32_t windowId);
397      void OutsideTouch(int32_t windowId);
398      void UpdateAccessibilityWindowStateByEvent(const AccessibilityEventInfo &event);
399  
400      void UpdateAccessibilityState();
401      void UpdateCaptionProperty();
402      void UpdateSettingsInAtoHosTask();
403      void UpdateSettingsInAtoHos();
404      void UpdateAutoStartAbilities();
405      void UpdateAllSetting();
406  
407      void RemoveCallback(CallBackID callback, const sptr<DeathRecipient> &recipient, const wptr<IRemoteObject> &remote);
408      void RemoveSavedConfigCallback(const wptr<IRemoteObject>& callback);
409      void OnBundleManagerDied(const wptr<IRemoteObject> &remote);
410      void DeleteConnectionAndDeathRecipient(
411          const int32_t windowId, const sptr<AccessibilityWindowConnection> &connection);
412  
413      void OnDeviceProvisioned();
414      void InitializeShortKeyState();
415      void RegisterProvisionCallback();
416      void RegisterShortKeyEvent();
417      bool IsNeedUnload();
418      void OffZoomGesture();
419      void OnScreenMagnificationStateChanged();
420      void RegisterScreenMagnificationState();
421      void OnScreenMagnificationTypeChanged();
422      void RegisterScreenMagnificationType();
423  
424      bool isReady_ = false;
425      bool isPublished_ = false;
426      std::map<int32_t, bool> dependentServicesStatus_;
427      int32_t currentAccountId_ = -1;
428      AccessibilityAccountDataMap  a11yAccountsData_;
429      sptr<AppExecFwk::IBundleMgr> bundleManager_ = nullptr;
430  
431      sptr<AccessibilityInputInterceptor> inputInterceptor_ = nullptr;
432      sptr<TouchEventInjector> touchEventInjector_ = nullptr;
433      sptr<KeyEventFilter> keyEventFilter_ = nullptr;
434      sptr<AccessibilityDumper> accessibilityDumper_ = nullptr;
435  
436      std::shared_ptr<AppExecFwk::EventRunner> runner_;
437      std::shared_ptr<AAMSEventHandler> handler_;
438  
439      std::shared_ptr<AppExecFwk::EventRunner> actionRunner_;
440      std::shared_ptr<AAMSEventHandler> actionHandler_;
441  
442      std::shared_ptr<AppExecFwk::EventRunner> sendEventRunner_;
443      std::shared_ptr<AAMSEventHandler> sendEventHandler_;
444  
445      int64_t ipcTimeoutNum_ = 0; // count ipc timeout number
446  
447      sptr<IRemoteObject::DeathRecipient> stateObserversDeathRecipient_ = nullptr;
448      std::map<int32_t, sptr<IRemoteObject::DeathRecipient>> interactionOperationDeathRecipients_ {};
449      std::map<int32_t, std::map<int32_t, sptr<IRemoteObject::DeathRecipient>>> interactionOperationDeathMap_ {};
450      sptr<IRemoteObject::DeathRecipient> captionPropertyCallbackDeathRecipient_ = nullptr;
451      sptr<IRemoteObject::DeathRecipient> enableAbilityListsObserverDeathRecipient_ = nullptr;
452      sptr<IRemoteObject::DeathRecipient> configCallbackDeathRecipient_ = nullptr;
453      sptr<IRemoteObject::DeathRecipient> bundleManagerDeathRecipient_ = nullptr;
454      StateObservers stateObservers_;
455      ffrt::mutex mutex_; // current used for register state observer
456      std::vector<sptr<IAccessibleAbilityManagerConfigObserver>> defaultConfigCallbacks_;
457      std::shared_ptr<AccessibilitySettings> accessibilitySettings_ = nullptr;
458      std::shared_ptr<AccessibilityShortKey> accessibilityShortKey_ = nullptr;
459      std::vector<std::string> removedAutoStartAbilities_ {};
460      std::map<int32_t, AccessibilityEventInfo> windowFocusEventMap_ {};
461  
462      std::map<int32_t, std::map<int32_t, std::set<int32_t>>> windowRequestIdMap_ {}; // windowId->treeId->requestId
463      std::map<int32_t, sptr<IAccessibilityElementOperatorCallback>> requestIdMap_ {}; // requestId->callback
464  };
465  } // namespace Accessibility
466  } // namespace OHOS
467  #endif // ACCESSIBLE_ABILITY_MANAGER_SERVICE_H