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_SYSTEM_ABILITY_CLIENT_H 17 #define ACCESSIBILITY_SYSTEM_ABILITY_CLIENT_H 18 19 #include <cstdint> 20 #include <list> 21 #include <map> 22 #include <mutex> 23 #include <string> 24 #include <vector> 25 26 #include "accessibility_ability_info.h" 27 #include "accessibility_caption.h" 28 #include "accessibility_element_operator.h" 29 #include "accessibility_event_info.h" 30 #include "accessibility_state_event.h" 31 #include "context.h" 32 #include "refbase.h" 33 34 namespace OHOS { 35 namespace Accessibility { 36 enum AccessibilityControlType : int { 37 CONTENT_CONTROLS = 0x00000001, 38 CONTENT_ICONS = 0x00000002, 39 CONTENT_TEXT = 0x00000004, 40 }; 41 42 enum AbilityStateType : int32_t { 43 ABILITY_STATE_INVALID = 0, 44 ABILITY_STATE_ENABLE, 45 ABILITY_STATE_DISABLE, 46 ABILITY_STATE_INSTALLED, 47 }; 48 49 /* 50 * The class register the accessibility service observer to AAMS,and 51 * dispatch the accessibility service status changed. such as Service Enable, 52 * Accessibility Enable. It calls AAMS API to send the event to AA. 53 * It supply sington instance for each process. 54 */ 55 class AccessibilitySystemAbilityClient { 56 public: 57 static const int32_t NUM_INT32 = 32; 58 static const uint32_t STATE_ACCESSIBILITY_ENABLED = 0x00000001; 59 static const uint32_t STATE_EXPLORATION_ENABLED = 0x00000002; 60 static const uint32_t STATE_CAPTION_ENABLED = 0x00000004; 61 static const uint32_t STATE_KEYEVENT_ENABLED = 0x00000008; 62 static const uint32_t STATE_GESTURE_ENABLED = 0x00000010; 63 64 /** 65 * @brief Construct. 66 * @param context Indicates the context of the associated ability. 67 * @param accountId User Id 68 */ 69 AccessibilitySystemAbilityClient(const AppExecFwk::Context& context, int accountId); 70 71 /** 72 * @brief Register the interaction operation, so the AA can get node info from ACE. 73 * @param windowId Window ID 74 * @param operation The callback object. 75 * @param accountId User ID 76 * @return 0: Succeed ; otherwise is failed. 77 */ 78 int RegisterElementOperator( 79 const int windowId, const std::shared_ptr<AccessibilityElementOperator>& operation, int accountId); 80 81 /** 82 * @brief Deregister the interaction operation. 83 * @param windowId Window ID 84 * @return 85 */ 86 void DeregisterElementOperator(const int windowId); 87 88 /** 89 * @brief Checks whether accessibility ability is enabled. 90 * @param - 91 * @return true: enabled; false: disabled 92 */ 93 bool IsEnabled(); 94 95 /** 96 * @brief Checks whether touch exploration ability is enabled. 97 * @param - 98 * @return true: enabled; false: disabled 99 */ 100 bool IsTouchExplorationEnabled(); 101 102 bool IsCaptionEnabled(); 103 104 /** 105 * @brief Queries the list of accessibility abilities. 106 * @param accessibilityAbilityTypes Indicates the accessibility type specified by 107 * AccessibilityAbilityInfo#ACCESSIBILITY_ABILITY_TYPE_SPOKEN. 108 * @param stateType Indicates the accessibility ability status. 109 * 1 indicates that the ability is enabled; 110 * 2 indicates that the ability is disabled; 111 * 4 indicates that the ability has been installed. 112 * @return 113 */ 114 std::vector<AccessibilityAbilityInfo> GetAbilityList( 115 const uint32_t accessibilityAbilityTypes, const AbilityStateType stateType); 116 117 /** 118 * @brief Obtains the AccessibilitySystemAbilityClient instance. 119 * @param abilityContext Indicates the context of the associated ability. 120 * @return AccessibilitySystemAbilityClient instance 121 */ 122 static std::shared_ptr<AccessibilitySystemAbilityClient> GetInstance(const AppExecFwk::Context& abilityContext); 123 124 /** 125 * @brief Obtains the AccessibilitySystemAbilityClient instance. 126 * @param - 127 * @return AccessibilitySystemAbilityClient instance 128 */ 129 static std::shared_ptr<AccessibilitySystemAbilityClient> GetInstance(); 130 131 /** 132 * @brief Obtains the properties of the accessibility caption function. 133 * Remained for caption. 134 * @param - 135 * @return Returns the properties of the accessibility caption function. 136 */ 137 CaptionProperty GetCaptionProperty() const; 138 139 bool SetCaptionProperty(const CaptionProperty& caption); 140 141 bool SetCaptionState(const bool state); 142 143 /** 144 * @brief Checks whether the accessibility caption function is enabled. 145 * Remained for caption. 146 * @param - 147 * @return True if the caption function is enabled; returns False otherwise. 148 */ 149 bool IsAccessibilityCaptionEnabled() const; 150 151 /** 152 * @brief Sends an accessibility event. 153 * @param eventType Identifies the accessibility event specified by AccessibilityEventInfo. 154 * @param componentId Indicates the ID of the component to be associated with the event. 155 * @return true: send ok; otherwise is refused. 156 */ 157 bool SendEvent(const EventType eventType, const int componentId); 158 159 /** 160 * @brief Sends information about an accessibility event. 161 * @param event Indicates the accessibility event information specified by AccessibilityEventInfo. 162 * @return true: send ok; otherwise is refused. 163 */ 164 bool SendEvent(const AccessibilityEventInfo& event); 165 166 /** 167 * @brief Subscribes to the specified type of accessibility status change events. 168 * @param observer Indicates the observer for listening to status events, which is specified 169 * by AccessibilityStateObserver. 170 * @param eventType Indicates the status type, which is specified by AccessibilityStateEvent 171 * #EVENT_ACCESSIBILITY_STATE_CHANGED and AccessibilityStateEvent#EVENT_TOUCH_BROWSE_STATE_CHANGED 172 * @return true: send ok; otherwise is refused. 173 */ 174 bool SubscribeStateObserver(const std::shared_ptr<AccessibilityStateObserver>& observer, const int eventType); 175 176 /** 177 * @brief Unsubscribe the specified type of accessibility status change events. 178 * @param observer Indicates the registered accessibility status event observer. 179 * @param eventType Indicates the status type, which is specified by AccessibilityStateEvent 180 * #EVENT_ACCESSIBILITY_STATE_CHANGED and AccessibilityStateEvent#EVENT_TOUCH_BROWSE_STATE_CHANGED 181 * @return true: send ok; otherwise is refused. 182 */ 183 bool UnsubscribeStateObserver(const std::shared_ptr<AccessibilityStateObserver>& observer, const int eventType); 184 185 /** 186 * @brief Unsubscribe the accessibility status change events from the observer. 187 * @param observer Indicates the registered accessibility status event observer. 188 * @return true is succeed otherwise is failed. 189 */ 190 bool UnsubscribeStateObserver(const std::shared_ptr<AccessibilityStateObserver>& observer); 191 192 /** 193 * @brief Inner function for aams status update; 194 * Set whether accessibility ability is enabled. 195 * @param enabled true is enabled otherwise is disabled. 196 * @return - 197 */ 198 void UpdateEnabled(const bool enabled); 199 200 /** 201 * @brief Inner function for aams status update; 202 * Set whether touch exploration is enabled. 203 * @param enabled true is enabled otherwise is disabled. 204 * @return - 205 */ 206 void UpdateTouchExplorationEnabled(const bool enabled); 207 208 /** 209 * @brief Update the properties of caption. 210 * @param 211 * @return 212 */ 213 void UpdatecaptionProperty(const CaptionProperty& property); 214 215 void SetCaptionEnabled(const bool enabled); 216 217 bool SetEnabled(const bool state); 218 219 /** 220 * @brief Get eventlist that accessibility abilities are needed. 221 * @return enabled eventlist mask. 222 */ 223 int GetEnabledEventMask(); 224 225 /** 226 * @brief Check otherwise Accessibility Ability(AA) is conflicted with 227 * other AA. Remained for setting subsystem. 228 * @param abilityName The AA specified (include 229 * deviceId/bundleName/abilityName) 230 * @return LAUNCH_CONFLICT_NONE: no conflict; 231 * LAUNCH_CONFLICT_TOUCH_BROWSER: conflict with touch browser; 232 * LAUNCH_CONFLICT_DISPLAY_RESIZE: conflict with display resize; 233 * LAUNCH_CONFLICT_KEY_EVENT: conflict with key event; 234 */ CheckConflictWithEnabledAbility(const AppExecFwk::ElementName & abilityName)235 int CheckConflictWithEnabledAbility(const AppExecFwk::ElementName& abilityName) 236 { 237 return 0; 238 } 239 240 /** 241 * @brief Get the AA specified capability. 242 * Remained for setting subsystem. 243 * @param abilityName The AA specified (include deviceId/bundleName/abilityName) 244 * @return refer to AccessibilityAbilityInfo.Capability 245 */ GetAccessibleAbilityCapability(const AppExecFwk::ElementName & abilityName)246 int GetAccessibleAbilityCapability(const AppExecFwk::ElementName& abilityName) 247 { 248 return 0; 249 } 250 251 /** 252 * @brief Inner function. 253 * Get the callback object registered by ACE. 254 * @param windowId The window id related the operation object registered. 255 * @return The callback object of ACE. 256 */ 257 std::shared_ptr<AccessibilityElementOperator> GetOperatorObject(int windowId); 258 259 bool AddCaptionListener(const std::shared_ptr<CaptionObserver>& ob, const int type); 260 bool DeleteCaptionListener(const std::shared_ptr<CaptionObserver>& ob, const int type); 261 262 bool GetEnabledState(); 263 bool GetCaptionState(); 264 bool GetTouchGuideState(); 265 bool GetGestureState(); 266 bool GetKeyEventObserverState(); 267 268 bool SetTouchGuideState(const bool state); 269 bool SetGestureState(const bool state); 270 bool SetKeyEventObserverState(const bool state); 271 272 bool SetEnabledObj(std::map<std::string, AppExecFwk::ElementName> it); 273 std::vector<AccessibilityAbilityInfo> GetInstalledAbilities(); 274 std::map<std::string, AppExecFwk::ElementName> GetEnabledAbilities(); 275 bool SetCaptionPropertyTojson(const CaptionProperty& caption); 276 bool SetCaptionStateTojson(const bool state); 277 bool DisableAbilities(std::map<std::string, AppExecFwk::ElementName> it); 278 int GetActiveWindow(); 279 280 private: 281 /** 282 * @brief Clean the AAMS object data. 283 * @param remote The object access to AAMS. 284 * @return 285 */ 286 void ResetService(const wptr<IRemoteObject>& remote); 287 288 /** 289 * @brief Notify the state of accessibility is changed. 290 * @param 291 * @return 292 */ 293 void NotifyAccessibilityStateChanged(); 294 295 /** 296 * @brief Notify the state of touch exploration is changed. 297 * @param 298 * @return 299 */ 300 void NotifyTouchExplorationStateChanged(); 301 302 /** 303 * @brief Notify the state of caption is changed. 304 * @param 305 * @return 306 */ 307 void NotifyCaptionStateChanged(); 308 309 /** 310 * @brief Notify the properties of caption is changed. 311 * @param 312 * @return 313 */ 314 void NotifyCaptionChanged(); 315 316 /** 317 * @brief Check the event type is valid or not. 318 * @param eventType The data of event type. 319 * @return True: The data of event type is valid; otherwise is not. 320 */ 321 bool CheckEventType(EventType eventType); 322 323 /** 324 * @brief Check the action type is valid or not. 325 * @param eventType The data of event type. 326 * @return True: The data of event type is valid; otherwise is not. 327 */ 328 bool CheckActionType(ActionType actionType); 329 330 void NotifyKeyEventStateChanged(); 331 332 void NotifyGestureStateChanged(); 333 334 std::vector<std::shared_ptr<AccessibilityStateObserver>> observersAccessibilityState_; 335 std::vector<std::shared_ptr<AccessibilityStateObserver>> observersTouchState_; 336 std::vector<std::shared_ptr<CaptionObserver>> observersCaptionProperty_; 337 std::vector<std::shared_ptr<CaptionObserver>> observersCaptionEnable_; 338 339 CaptionProperty captionProperty_; 340 int accountId_ = 0; 341 bool isEnabled_ = 0; 342 bool isTouchExplorationEnabled_ = 0; 343 bool isCaptionEnabled_ = 0; 344 std::recursive_mutex asacProxyLock_; 345 static std::shared_ptr<AccessibilitySystemAbilityClient> instance_; 346 std::shared_ptr<AccessibilityElementOperator> interactionOperator_ = nullptr; 347 std::map<int, std::shared_ptr<AccessibilityElementOperator>> interactionOperators_; 348 int connectionWindowId_ = 0; 349 350 std::vector<AccessibilityAbilityInfo> installedAbilities_; 351 std::map<std::string, AppExecFwk::ElementName> enabledAbilities_; 352 353 bool isFilteringKeyEventsEnabled_ = 0; 354 bool isGesturesSimulationEnabled_ = 0; 355 std::vector<std::shared_ptr<AccessibilityStateObserver>> observersKeyEventState_; 356 std::vector<std::shared_ptr<AccessibilityStateObserver>> observersGestureState_; 357 358 struct Impl; 359 std::unique_ptr<Impl> pimpl; 360 }; 361 } // namespace Accessibility 362 } // namespace OHOS 363 #endif