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 <future> 20 #include <map> 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 25 #include "accessibility_dumper.h" 26 #include "accessibility_def.h" 27 #include "accessible_ability_manager_service_stub.h" 28 #include "accessible_ability_manager_service_event_handler.h" 29 #include "accessibility_account_data.h" 30 #include "accessibility_common_event.h" 31 #ifdef OHOS_BUILD_ENABLE_DISPLAY_MANAGER 32 #include "accessibility_display_manager.h" 33 #endif 34 #include "accessibility_element_operator_callback_stub.h" 35 #include "accessibility_input_interceptor.h" 36 #include "accessibility_keyevent_filter.h" 37 #include "accessibility_settings.h" 38 #include "accessibility_touchEvent_injector.h" 39 #include "accessibility_window_info.h" 40 #include "bundlemgr/bundle_mgr_interface.h" 41 #include "input_manager.h" 42 #include "singleton.h" 43 #include "system_ability.h" 44 #include "window_manager.h" 45 #include "accessibility_short_key.h" 46 47 namespace OHOS { 48 namespace Accessibility { 49 class AccessibilityAccountData; 50 class TouchEventInjector; 51 class AccessibilitySettings; 52 53 enum CallBackID { 54 STATE_CALLBACK, 55 CAPTION_PROPERTY_CALLBACK, 56 ENABLE_ABILITY_LISTS_CALLBACK, 57 CONFIG_CALLBACK 58 }; 59 60 constexpr int REQUEST_ID_INIT = 65535; 61 62 const std::map<std::string, int32_t> AccessibilityConfigTable = { 63 {"HIGH_CONTRAST_TEXT", HIGH_CONTRAST_TEXT}, 64 {"INVERT_COLOR", INVERT_COLOR}, 65 {"DALTONIZATION_COLOR_FILTER", DALTONIZATION_COLOR_FILTER}, 66 {"CONTENT_TIMEOUT", CONTENT_TIMEOUT}, 67 {"ANIMATION_OFF", ANIMATION_OFF}, 68 {"BRIGHTNESS_DISCOUNT", BRIGHTNESS_DISCOUNT}, 69 {"AUDIO_MONO", AUDIO_MONO}, 70 {"AUDIO_BALANCE", AUDIO_BALANCE}, 71 {"MOUSE_KEY", MOUSE_KEY}, 72 {"CAPTION_STATE", CAPTION_STATE}, 73 {"CAPTION_STYLE", CAPTION_STYLE}, 74 {"SCREEN_MAGNIFICATION", SCREEN_MAGNIFICATION}, 75 {"MOUSE_AUTOCLICK", MOUSE_AUTOCLICK} 76 }; 77 78 class AccessibleAbilityManagerService : public SystemAbility, public AccessibleAbilityManagerServiceStub { 79 DECLARE_SINGLETON(AccessibleAbilityManagerService) 80 DECLEAR_SYSTEM_ABILITY(AccessibleAbilityManagerService) 81 public: 82 /* For system ability */ 83 void OnStart() override; 84 void OnStop() override; 85 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 86 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 87 int Dump(int fd, const std::vector<std::u16string>& args) override; 88 void PostDelayUnloadTask() override; 89 90 public: 91 /* For AccessibleAbilityManagerServiceStub */ 92 RetError SendEvent(const AccessibilityEventInfo &uiEvent, const int32_t flag = 0) override; 93 94 RetError VerifyingToKenId(const int32_t windowId, const int64_t elementId); 95 96 uint32_t RegisterStateObserver(const sptr<IAccessibleAbilityManagerStateObserver> &callback) override; 97 98 uint32_t RegisterCaptionObserver(const sptr<IAccessibleAbilityManagerCaptionObserver> &callback) override; 99 100 void RegisterEnableAbilityListsObserver( 101 const sptr<IAccessibilityEnableAbilityListsObserver> &observer) override; 102 103 RetError GetAbilityList(const uint32_t abilityTypes, const int32_t stateType, 104 std::vector<AccessibilityAbilityInfo> &infos) override; 105 106 RetError RegisterElementOperator(const int32_t windowId, 107 const sptr<IAccessibilityElementOperator> &operation, bool isApp) override; 108 109 RetError RegisterElementOperator(Registration parameter, 110 const sptr<IAccessibilityElementOperator> &operation, bool isApp) override; 111 112 RetError DeregisterElementOperator(const int32_t windowId) override; 113 114 RetError DeregisterElementOperator(const int32_t windowId, const int32_t treeId) override; 115 116 RetError GetCaptionProperty(AccessibilityConfig::CaptionProperty &caption) override; 117 RetError SetCaptionProperty(const AccessibilityConfig::CaptionProperty &caption) override; 118 RetError SetCaptionState(const bool state) override; 119 120 bool GetEnabledState() override; 121 RetError GetCaptionState(bool &state) override; 122 bool GetTouchGuideState() override; 123 bool GetGestureState() override; 124 bool GetKeyEventObserverState() override; 125 126 RetError EnableAbility(const std::string &name, const uint32_t capabilities) override; 127 RetError GetEnabledAbilities(std::vector<std::string> &enabledAbilities) override; 128 RetError DisableAbility(const std::string &name) override; 129 RetError EnableUITestAbility(const sptr<IRemoteObject>& obj) override; 130 RetError DisableUITestAbility() override; 131 int32_t GetActiveWindow() override; 132 void GetRealWindowAndElementId(int32_t& windowId, int64_t& elementId) override; 133 void GetSceneBoardInnerWinId(int32_t windowId, int64_t elementId, int32_t& innerWid) override; 134 bool FindFocusedElement(AccessibilityElementInfo &elementInfo); 135 bool ExecuteActionOnAccessibilityFocused(const ActionType &action); 136 RetError GetFocusedWindowId(int32_t &focusedWindowId) override; 137 void SetFocusWindowId(const int32_t focusWindowId); 138 void SetFocusElementId(const int64_t focusElementId); 139 int32_t GetFocusWindowId(); 140 int64_t GetFocusElementId(); 141 static int32_t GetTreeIdBySplitElementId(const int64_t elementId); 142 int64_t GetRootParentId(int32_t windowId, int32_t treeId) override; 143 RetError GetAllTreeId(int32_t windowId, std::vector<int32_t> &treeIds) override; 144 void SetTokenIdMapAndRootParentId(const sptr<AccessibilityWindowConnection> connection, 145 const int32_t treeId, const int64_t nodeId, const uint32_t tokenId); 146 void RemoveTreeDeathRecipient(const int32_t windowId, const int32_t treeId, 147 const sptr<AccessibilityWindowConnection> connection); 148 int32_t GenerateRequestId(); 149 void GetElementOperatorConnection(sptr<AccessibilityWindowConnection> &connection, 150 const int64_t elementId, sptr<IAccessibilityElementOperator> &elementOperator); 151 private: 152 int32_t focusWindowId_ = -1; 153 int64_t focusElementId_ = -1; 154 std::atomic<int> requestId_ = REQUEST_ID_INIT; 155 public: 156 /* For inner modules */ 157 bool EnableShortKeyTargetAbility(const std::string &name = ""); 158 bool DisableShortKeyTargetAbility(); 159 void OnShortKeyProcess(); 160 void UpdateShortKeyRegister(); 161 162 void SetTouchEventInjector(const sptr<TouchEventInjector> &touchEventInjector); 163 GetTouchEventInjector()164 inline sptr<TouchEventInjector> GetTouchEventInjector() 165 { 166 return touchEventInjector_; 167 } 168 GetKeyEventFilter()169 inline sptr<KeyEventFilter> GetKeyEventFilter() 170 { 171 return keyEventFilter_; 172 } 173 174 void SetKeyEventFilter(const sptr<KeyEventFilter> &keyEventFilter); 175 176 /* For DisplayResize */ 177 void NotifyDisplayResizeStateChanged(int32_t displayId, Rect& rect, float scale, float centerX, float centerY); 178 IsServiceReady()179 inline bool IsServiceReady() 180 { 181 return isReady_; 182 } 183 GetCurrentAccountId()184 inline int32_t GetCurrentAccountId() 185 { 186 return currentAccountId_; 187 } 188 GetMainHandler()189 inline std::shared_ptr<AAMSEventHandler> &GetMainHandler() 190 { 191 return handler_; 192 } 193 GetMainRunner()194 inline std::shared_ptr<AppExecFwk::EventRunner> &GetMainRunner() 195 { 196 return runner_; 197 } 198 199 sptr<AccessibilityAccountData> GetAccountData(int32_t accountId); 200 sptr<AccessibilityAccountData> GetCurrentAccountData(); 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 std::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 ¶meter, 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)327 InteractionOperationDeathRecipient(int32_t windowId) : windowId_(windowId) {}; InteractionOperationDeathRecipient(int32_t windowId,int32_t treeId)328 InteractionOperationDeathRecipient(int32_t windowId, int32_t treeId) : windowId_(windowId), treeId_(treeId) {}; 329 ~InteractionOperationDeathRecipient() final = default; 330 DISALLOW_COPY_AND_MOVE(InteractionOperationDeathRecipient); 331 332 void OnRemoteDied(const wptr<IRemoteObject> &remote) final; 333 int32_t windowId_ = INVALID_WINDOW_ID; 334 int32_t treeId_ = INVALID_TREE_ID; 335 }; 336 337 class CaptionPropertyCallbackDeathRecipient final : public IRemoteObject::DeathRecipient { 338 public: 339 CaptionPropertyCallbackDeathRecipient() = default; 340 ~CaptionPropertyCallbackDeathRecipient() final = default; 341 DISALLOW_COPY_AND_MOVE(CaptionPropertyCallbackDeathRecipient); 342 343 void OnRemoteDied(const wptr<IRemoteObject> &remote) final; 344 }; 345 346 class EnableAbilityListsObserverDeathRecipient final : public IRemoteObject::DeathRecipient { 347 public: 348 EnableAbilityListsObserverDeathRecipient() = default; 349 ~EnableAbilityListsObserverDeathRecipient() final = default; 350 DISALLOW_COPY_AND_MOVE(EnableAbilityListsObserverDeathRecipient); 351 352 void OnRemoteDied(const wptr<IRemoteObject> &remote) final; 353 }; 354 355 bool Init(); 356 void InitInnerResource(); 357 358 class ConfigCallbackDeathRecipient final : public IRemoteObject::DeathRecipient { 359 public: 360 ConfigCallbackDeathRecipient() = default; 361 ~ConfigCallbackDeathRecipient() final = default; 362 DISALLOW_COPY_AND_MOVE(ConfigCallbackDeathRecipient); 363 364 void OnRemoteDied(const wptr<IRemoteObject> &remote) final; 365 }; 366 367 class BundleManagerDeathRecipient final : public IRemoteObject::DeathRecipient { 368 public: 369 BundleManagerDeathRecipient() = default; 370 ~BundleManagerDeathRecipient() final = default; 371 DISALLOW_COPY_AND_MOVE(BundleManagerDeathRecipient); 372 373 void OnRemoteDied(const wptr<IRemoteObject> &remote) final; 374 }; 375 376 class StateObservers { 377 public: 378 StateObservers() = default; 379 ~StateObservers() = default; 380 void AddStateObserver(const sptr<IAccessibleAbilityManagerStateObserver>& stateObserver); 381 void OnStateObservers(uint32_t state); 382 void RemoveStateObserver(const wptr<IRemoteObject>& remote); 383 void Clear(); 384 private: 385 std::vector<sptr<IAccessibleAbilityManagerStateObserver>> observersList_; 386 std::mutex stateObserversMutex_; 387 }; 388 389 RetError InnerEnableAbility(const std::string &name, const uint32_t capabilities); 390 RetError InnerDisableAbility(const std::string &name); 391 392 sptr<AccessibilityWindowConnection> GetAccessibilityWindowConnection(int32_t windowId); 393 void ClearFocus(int32_t windowId); 394 void OutsideTouch(int32_t windowId); 395 void UpdateAccessibilityWindowStateByEvent(const AccessibilityEventInfo &event); 396 397 void UpdateAccessibilityState(); 398 void UpdateCaptionProperty(); 399 void UpdateSettingsInAtoHosTask(); 400 void UpdateSettingsInAtoHos(); 401 void UpdateAutoStartAbilities(); 402 void UpdateAllSetting(); 403 404 void RemoveCallback(CallBackID callback, const sptr<DeathRecipient> &recipient, const wptr<IRemoteObject> &remote); 405 void RemoveSavedConfigCallback(const wptr<IRemoteObject>& callback); 406 void OnBundleManagerDied(const wptr<IRemoteObject> &remote); 407 void DeleteConnectionAndDeathRecipient( 408 const int32_t windowId, const sptr<AccessibilityWindowConnection> &connection); 409 410 void OnDeviceProvisioned(); 411 void InitializeShortKeyState(); 412 void RegisterProvisionCallback(); 413 void RegisterShortKeyEvent(); 414 bool IsNeedUnload(); 415 void OffZoomGesture(); 416 void OnScreenMagnificationStateChanged(); 417 void RegisterScreenMagnificationState(); 418 void OnScreenMagnificationTypeChanged(); 419 void RegisterScreenMagnificationType(); 420 421 bool isReady_ = false; 422 bool isPublished_ = false; 423 std::map<int32_t, bool> dependentServicesStatus_; 424 int32_t currentAccountId_ = -1; 425 AccessibilityAccountDataMap a11yAccountsData_; 426 sptr<AppExecFwk::IBundleMgr> bundleManager_ = nullptr; 427 428 sptr<AccessibilityInputInterceptor> inputInterceptor_ = nullptr; 429 sptr<TouchEventInjector> touchEventInjector_ = nullptr; 430 sptr<KeyEventFilter> keyEventFilter_ = nullptr; 431 sptr<AccessibilityDumper> accessibilityDumper_ = nullptr; 432 sptr<AccessibilityShortKey> accessibilityShortKey_ = nullptr; 433 434 std::shared_ptr<AppExecFwk::EventRunner> runner_; 435 std::shared_ptr<AAMSEventHandler> handler_; 436 437 std::shared_ptr<AppExecFwk::EventRunner> actionRunner_; 438 std::shared_ptr<AAMSEventHandler> actionHandler_; 439 440 int64_t ipcTimeoutNum_ = 0; // count ipc timeout number 441 442 sptr<IRemoteObject::DeathRecipient> stateObserversDeathRecipient_ = nullptr; 443 std::map<int32_t, sptr<IRemoteObject::DeathRecipient>> interactionOperationDeathRecipients_ {}; 444 std::map<int32_t, std::map<int32_t, sptr<IRemoteObject::DeathRecipient>>> interactionOperationDeathMap_ {}; 445 sptr<IRemoteObject::DeathRecipient> captionPropertyCallbackDeathRecipient_ = nullptr; 446 sptr<IRemoteObject::DeathRecipient> enableAbilityListsObserverDeathRecipient_ = nullptr; 447 sptr<IRemoteObject::DeathRecipient> configCallbackDeathRecipient_ = nullptr; 448 sptr<IRemoteObject::DeathRecipient> bundleManagerDeathRecipient_ = nullptr; 449 StateObservers stateObservers_; 450 std::mutex mutex_; // current used for register state observer 451 std::vector<sptr<IAccessibleAbilityManagerConfigObserver>> defaultConfigCallbacks_; 452 std::shared_ptr<AccessibilitySettings> accessibilitySettings_ = nullptr; 453 std::vector<std::string> removedAutoStartAbilities_ {}; 454 std::map<int32_t, AccessibilityEventInfo> windowFocusEventMap_ {}; 455 456 std::map<int32_t, std::map<int32_t, std::set<int32_t>>> windowRequestIdMap_ {}; // windowId->treeId->requestId 457 std::map<int32_t, sptr<IAccessibilityElementOperatorCallback>> requestIdMap_ {}; // requestId->callback 458 }; 459 } // namespace Accessibility 460 } // namespace OHOS 461 #endif // ACCESSIBLE_ABILITY_MANAGER_SERVICE_H