1 /* 2 * Copyright (C) 2022-2023 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_SYSTEM_ABILITY_CLIENT_IMPL_H 17 #define ACCESSIBILITY_SYSTEM_ABILITY_CLIENT_IMPL_H 18 19 #include <array> 20 #include <mutex> 21 #include "accessibility_element_operator_impl.h" 22 #include "accessibility_system_ability_client.h" 23 #include "accessible_ability_manager_state_observer_stub.h" 24 #include "i_accessible_ability_manager_service.h" 25 26 namespace OHOS { 27 namespace Accessibility { 28 using StateArray = std::array<bool, AccessibilityStateEventType::EVENT_TYPE_MAX>; 29 using StateObserverVector = std::vector<std::shared_ptr<AccessibilityStateObserver>>; 30 using StateObserversArray = std::array<StateObserverVector, AccessibilityStateEventType::EVENT_TYPE_MAX>; 31 class AccessibilitySystemAbilityClientImpl 32 : public AccessibilitySystemAbilityClient, public AccessibilityElementOperatorCallback { 33 public: 34 /** 35 * @brief Construct. 36 */ 37 AccessibilitySystemAbilityClientImpl(); 38 39 /** 40 * @brief Deconstruct. 41 */ 42 ~AccessibilitySystemAbilityClientImpl(); 43 44 /** 45 * @brief Register the element operator, so the AA can get node info from ACE. 46 * @param windowId Window ID 47 * @param operation The callback object. 48 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 49 */ 50 virtual RetError RegisterElementOperator(const int32_t windowId, 51 const std::shared_ptr<AccessibilityElementOperator> &operation) override; 52 53 /** 54 * @brief Deregister the element operator. 55 * @param windowId Window ID 56 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 57 */ 58 virtual RetError DeregisterElementOperator(const int32_t windowId) override; 59 60 /** 61 * @brief Checks whether accessibility ability is enabled. 62 * @param isEnabled true: enabled; false: disabled 63 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 64 */ 65 virtual RetError IsEnabled(bool &isEnabled) override; 66 67 /** 68 * @brief Checks whether touch exploration ability is enabled. 69 * @param isEnabled true: enabled; false: disabled 70 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 71 */ 72 virtual RetError IsTouchExplorationEnabled(bool &isEnabled) override; 73 74 /** 75 * @brief Queries the list of accessibility abilities. 76 * @param accessibilityAbilityTypes Indicates the accessibility type specified by AccessibilityAbilityTypes. 77 * @param stateType Indicates the accessibility ability status. 78 * 1 indicates that the ability is enabled; 79 * 2 indicates that the ability is disabled; 80 * 3 indicates that the ability has been installed. 81 * @param infos accessibility ability infos by specified types. 82 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 83 */ 84 virtual RetError GetAbilityList(const uint32_t accessibilityAbilityTypes, const AbilityStateType stateType, 85 std::vector<AccessibilityAbilityInfo> &infos) override; 86 87 /** 88 * @brief Sends an accessibility event. 89 * @param eventType Identifies the accessibility event specified by AccessibilityEventInfo. 90 * @param componentId Indicates the ID of the component to be associated with the event. 91 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 92 */ 93 virtual RetError SendEvent(const EventType eventType, const int32_t componentId) override; 94 95 /** 96 * @brief Sends information about an accessibility event. 97 * @param event Indicates the accessibility event information specified by AccessibilityEventInfo. 98 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 99 */ 100 virtual RetError SendEvent(const AccessibilityEventInfo &event) override; 101 102 /** 103 * @brief Subscribes to the specified type of accessibility status change events. 104 * @param observer Indicates the observer for listening to status events, which is specified 105 * by AccessibilityStateObserver. 106 * @param eventType Indicates the status type, which is specified by AccessibilityStateEventType. 107 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 108 */ 109 virtual RetError SubscribeStateObserver(const std::shared_ptr<AccessibilityStateObserver> &observer, 110 const uint32_t eventType) override; 111 112 /** 113 * @brief Unsubscribe the specified type of accessibility status change events. 114 * @param observer Indicates the registered accessibility status event observer. 115 * @param eventType Indicates the status type, which is specified by AccessibilityStateEventType. 116 * @return Returns RET_OK if successful, otherwise refer to the RetError for the failure. 117 */ 118 virtual RetError UnsubscribeStateObserver(const std::shared_ptr<AccessibilityStateObserver> &observer, 119 const uint32_t eventType) override; 120 121 /** 122 * @brief Get enabled abilities. 123 * @param enabledAbilities The infos of enabled abilities. 124 * @return Return RET_OK if successful, otherwise refer to the RetError for the failure. 125 */ 126 virtual RetError GetEnabledAbilities(std::vector<std::string> &enabledAbilities) override; 127 128 /** 129 * @brief Clean the AAMS object data. 130 * @param remote The object access to AAMS. 131 */ 132 void ResetService(const wptr<IRemoteObject> &remote); 133 134 /** 135 * @brief Receive the state notify from AAMS and send it to the observer registered. 136 * @param stateType The state type and value. 137 * state type: Refer to AccessibilityStateEventType. 138 * value: STATE_ACCESSIBILITY_ENABLED/STATE_EXPLORATION_ENABLED/ 139 * STATE_ACCESSIBILITY_DISABLED/STATE_EXPLORATION_DISABLED 140 */ 141 void OnAccessibleAbilityManagerStateChanged(const uint32_t stateType); 142 143 /** 144 * @brief Set the element information by accessibility id to AA. 145 * @param infos The element info searched by accessibility id. 146 * @param requestId The request id from AA, it is used to match with request and response. 147 */ 148 virtual void SetSearchElementInfoByAccessibilityIdResult(const std::list<AccessibilityElementInfo> &infos, 149 const int32_t requestId) override; 150 151 /** 152 * @brief Set the element information matched with text to AA. 153 * @param infos The element information searched matched with text. 154 * @param requestId The request id from AA, it is used to match with request and response. 155 */ 156 virtual void SetSearchElementInfoByTextResult(const std::list<AccessibilityElementInfo> &infos, 157 const int32_t requestId) override; 158 159 /** 160 * @brief Set the element information matched with focus type to AA. 161 * @param info The element information searched matched with focus type. 162 * @param requestId The request id from AA, it is used to match with request and response. 163 */ 164 virtual void SetFindFocusedElementInfoResult(const AccessibilityElementInfo &info, 165 const int32_t requestId) override; 166 167 /** 168 * @brief Set the element information by focus direction to AA. 169 * @param info The element information searched by focus direction. 170 * @param requestId The request id from AA, it is used to match with request and response. 171 */ 172 virtual void SetFocusMoveSearchResult(const AccessibilityElementInfo &info, const int32_t requestId) override; 173 174 /** 175 * @brief Set the result of action executed to AA. 176 * @param succeeded True: The action is executed successfully; otherwise is false. 177 * @param requestId The request id from AA, it is used to match with request and response. 178 */ 179 virtual void SetExecuteActionResult(const bool succeeded, const int32_t requestId) override; 180 181 private: 182 class AccessibleAbilityManagerStateObserverImpl : public AccessibleAbilityManagerStateObserverStub { 183 public: AccessibleAbilityManagerStateObserverImpl(AccessibilitySystemAbilityClientImpl & client)184 explicit AccessibleAbilityManagerStateObserverImpl(AccessibilitySystemAbilityClientImpl &client) 185 : client_(client) {} 186 ~AccessibleAbilityManagerStateObserverImpl() = default; 187 OnStateChanged(const uint32_t stateType)188 virtual void OnStateChanged(const uint32_t stateType) override 189 { 190 client_.OnAccessibleAbilityManagerStateChanged(stateType); 191 } 192 private: 193 AccessibilitySystemAbilityClientImpl &client_; 194 }; 195 196 class DeathRecipient : public IRemoteObject::DeathRecipient { 197 public: DeathRecipient(AccessibilitySystemAbilityClientImpl & client)198 explicit DeathRecipient(AccessibilitySystemAbilityClientImpl &client) : client_(client) {} 199 ~DeathRecipient() = default; 200 DISALLOW_COPY_AND_MOVE(DeathRecipient); 201 OnRemoteDied(const wptr<IRemoteObject> & remote)202 void OnRemoteDied(const wptr<IRemoteObject> &remote) 203 { 204 client_.ResetService(remote); 205 } 206 private: 207 AccessibilitySystemAbilityClientImpl &client_; 208 }; 209 210 /** 211 * @brief Connect to AAMS Service. 212 * @return success : true, failed : false. 213 */ 214 bool ConnectToService(); 215 216 void Init(); 217 218 /** 219 * @brief Notify the state is changed. 220 * @param stateType The state type and value. 221 * state type: Refer to AccessibilityStateEventType. 222 * @param value The value be changed. 223 */ 224 void NotifyStateChanged(uint32_t eventType, bool value); 225 226 /** 227 * @brief Check the event type is valid or not. 228 * @param eventType The data of event type. 229 * @return True: The data of event type is valid; otherwise is not. 230 */ 231 bool CheckEventType(EventType eventType); 232 static void OnParameterChanged(const char *key, const char *value, void *context); 233 void ReregisterElementOperator(); 234 235 std::mutex mutex_; 236 StateArray stateArray_; 237 StateObserversArray stateObserversArray_; 238 239 std::map<int32_t, sptr<AccessibilityElementOperatorImpl>> elementOperators_; 240 sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr; 241 sptr<IAccessibleAbilityManagerService> serviceProxy_ = nullptr; 242 sptr<AccessibleAbilityManagerStateObserverImpl> stateObserver_ = nullptr; 243 }; 244 } // namespace Accessibility 245 } // namespace OHOS 246 #endif // ACCESSIBILITY_SYSTEM_ABILITY_CLIENT_IMPL_H