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 #include "accessibility_display_manager.h" 32 #include "accessibility_input_interceptor.h" 33 #include "accessibility_keyevent_filter.h" 34 #include "accessibility_touchEvent_injector.h" 35 #include "accessibility_window_info.h" 36 #include "bundlemgr/bundle_mgr_interface.h" 37 #include "input_manager.h" 38 #include "singleton.h" 39 #include "system_ability.h" 40 #include "window_manager.h" 41 42 namespace OHOS { 43 namespace Accessibility { 44 class AccessibilityAccountData; 45 class TouchEventInjector; 46 47 enum CallBackID { 48 STATE_CALLBACK, 49 CAPTION_PROPERTY_CALLBACK, 50 ENABLE_ABILITY_LISTS_CALLBACK, 51 CONFIG_CALLBACK 52 }; 53 54 const std::map<std::string, int32_t> AccessibilityConfigTable = { 55 {"HIGH_CONTRAST_TEXT", HIGH_CONTRAST_TEXT}, 56 {"INVERT_COLOR", INVERT_COLOR}, 57 {"DALTONIZATION_COLOR_FILTER", DALTONIZATION_COLOR_FILTER}, 58 {"CONTENT_TIMEOUT", CONTENT_TIMEOUT}, 59 {"ANIMATION_OFF", ANIMATION_OFF}, 60 {"BRIGHTNESS_DISCOUNT", BRIGHTNESS_DISCOUNT}, 61 {"AUDIO_MONO", AUDIO_MONO}, 62 {"AUDIO_BALANCE", AUDIO_BALANCE}, 63 {"MOUSE_KEY", MOUSE_KEY}, 64 {"CAPTION_STATE", CAPTION_STATE}, 65 {"CAPTION_STYLE", CAPTION_STYLE}, 66 {"SCREEN_MAGNIFICATION", SCREEN_MAGNIFICATION}, 67 {"MOUSE_AUTOCLICK", MOUSE_AUTOCLICK} 68 }; 69 70 class AccessibleAbilityManagerService : public SystemAbility, public AccessibleAbilityManagerServiceStub { 71 DECLARE_SINGLETON(AccessibleAbilityManagerService) 72 DECLEAR_SYSTEM_ABILITY(AccessibleAbilityManagerService) 73 public: 74 /* For system ability */ 75 void OnStart() override; 76 void OnStop() override; 77 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 78 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 79 int Dump(int fd, const std::vector<std::u16string>& args) override; 80 81 public: 82 /* For AccessibleAbilityManagerServiceStub */ 83 RetError SendEvent(const AccessibilityEventInfo &uiEvent) override; 84 85 uint32_t RegisterStateObserver(const sptr<IAccessibleAbilityManagerStateObserver> &callback) override; 86 87 uint32_t RegisterCaptionObserver(const sptr<IAccessibleAbilityManagerCaptionObserver> &callback) override; 88 89 void RegisterEnableAbilityListsObserver( 90 const sptr<IAccessibilityEnableAbilityListsObserver> &observer) override; 91 92 RetError GetAbilityList(const uint32_t abilityTypes, const int32_t stateType, 93 std::vector<AccessibilityAbilityInfo> &infos) override; 94 95 RetError RegisterElementOperator(const int32_t windowId, 96 const sptr<IAccessibilityElementOperator> &operation) override; 97 98 RetError DeregisterElementOperator(const int32_t windowId) override; 99 100 RetError GetCaptionProperty(AccessibilityConfig::CaptionProperty &caption) override; 101 RetError SetCaptionProperty(const AccessibilityConfig::CaptionProperty &caption) override; 102 RetError SetCaptionState(const bool state) override; 103 104 bool GetEnabledState() override; 105 RetError GetCaptionState(bool &state) override; 106 bool GetTouchGuideState() override; 107 bool GetGestureState() override; 108 bool GetKeyEventObserverState() override; 109 110 RetError EnableAbility(const std::string &name, const uint32_t capabilities) override; 111 RetError GetEnabledAbilities(std::vector<std::string> &enabledAbilities) override; 112 RetError DisableAbility(const std::string &name) override; 113 RetError EnableUITestAbility(const sptr<IRemoteObject>& obj) override; 114 RetError DisableUITestAbility() override; 115 int32_t GetActiveWindow() override; 116 117 public: 118 /* For inner modules */ 119 bool EnableShortKeyTargetAbility(); 120 bool DisableShortKeyTargetAbility(); 121 122 void SetTouchEventInjector(const sptr<TouchEventInjector> &touchEventInjector); 123 GetTouchEventInjector()124 inline sptr<TouchEventInjector> GetTouchEventInjector() 125 { 126 return touchEventInjector_; 127 } 128 GetKeyEventFilter()129 inline sptr<KeyEventFilter> GetKeyEventFilter() 130 { 131 return keyEventFilter_; 132 } 133 134 void SetKeyEventFilter(const sptr<KeyEventFilter> &keyEventFilter); 135 136 /* For DisplayResize */ 137 void NotifyDisplayResizeStateChanged(int32_t displayId, Rect& rect, float scale, float centerX, float centerY); 138 IsServiceReady()139 inline bool IsServiceReady() 140 { 141 return isReady_; 142 } 143 GetCurrentAccountId()144 inline int32_t GetCurrentAccountId() 145 { 146 return currentAccountId_; 147 } 148 GetMainHandler()149 inline std::shared_ptr<AAMSEventHandler> &GetMainHandler() 150 { 151 return handler_; 152 } 153 GetMainRunner()154 inline std::shared_ptr<AppExecFwk::EventRunner> &GetMainRunner() 155 { 156 return runner_; 157 } 158 159 sptr<AccessibilityAccountData> GetAccountData(int32_t accountId) const; 160 sptr<AccessibilityAccountData> GetCurrentAccountData(); 161 sptr<AppExecFwk::IBundleMgr> GetBundleMgrProxy(); 162 163 /* For common event */ 164 void AddedUser(int32_t accountId); 165 void RemovedUser(int32_t accountId); 166 void SwitchedUser(int32_t accountId); 167 void PackageChanged(const std::string &bundleName); 168 void PackageRemoved(const std::string &bundleName); 169 void PackageAdd(const std::string &bundleName); 170 171 void UpdateAccessibilityManagerService(); 172 173 RetError SetScreenMagnificationState(const bool state) override; 174 RetError SetShortKeyState(const bool state) override; 175 RetError SetMouseKeyState(const bool state) override; 176 RetError SetMouseAutoClick(const int32_t time) override; 177 RetError SetShortkeyTarget(const std::string &name) override; 178 RetError SetHighContrastTextState(const bool state) override; 179 RetError SetInvertColorState(const bool state) override; 180 RetError SetAnimationOffState(const bool state) override; 181 RetError SetAudioMonoState(const bool state) override; 182 RetError SetDaltonizationColorFilter(const uint32_t filter) override; 183 RetError SetContentTimeout(const uint32_t time) override; 184 RetError SetBrightnessDiscount(const float discount) override; 185 RetError SetAudioBalance(const float balance) override; 186 187 RetError GetScreenMagnificationState(bool &state) override; 188 RetError GetShortKeyState(bool &state) override; 189 RetError GetMouseKeyState(bool &state) override; 190 RetError GetMouseAutoClick(int32_t &time) override; 191 RetError GetShortkeyTarget(std::string &name) override; 192 RetError GetHighContrastTextState(bool &state) override; 193 RetError GetInvertColorState(bool &state) override; 194 RetError GetAnimationOffState(bool &state) override; 195 RetError GetAudioMonoState(bool &state) override; 196 RetError GetDaltonizationColorFilter(uint32_t &type) override; 197 RetError GetContentTimeout(uint32_t &timer) override; 198 RetError GetBrightnessDiscount(float &brightness) override; 199 RetError GetAudioBalance(float &balance) override; 200 void GetAllConfigs(AccessibilityConfigData &configData) override; 201 202 uint32_t RegisterConfigObserver(const sptr<IAccessibleAbilityManagerConfigObserver> &callback) override; 203 void UpdateConfigState(); 204 void UpdateAudioBalance(); 205 void UpdateBrightnessDiscount(); 206 void UpdateContentTimeout(); 207 void UpdateDaltonizationColorFilter(); 208 void UpdateMouseAutoClick(); 209 void UpdateShortkeyTarget(); 210 211 private: 212 bool SetTargetAbility(const int32_t targetAbilityValue); 213 class StateCallbackDeathRecipient final : public IRemoteObject::DeathRecipient { 214 public: 215 StateCallbackDeathRecipient() = default; 216 ~StateCallbackDeathRecipient() final = default; 217 DISALLOW_COPY_AND_MOVE(StateCallbackDeathRecipient); 218 219 void OnRemoteDied(const wptr<IRemoteObject> &remote) final; 220 }; 221 222 class InteractionOperationDeathRecipient final : public IRemoteObject::DeathRecipient { 223 public: InteractionOperationDeathRecipient(int32_t windowId)224 InteractionOperationDeathRecipient(int32_t windowId) : windowId_(windowId) {}; 225 ~InteractionOperationDeathRecipient() final = default; 226 DISALLOW_COPY_AND_MOVE(InteractionOperationDeathRecipient); 227 228 void OnRemoteDied(const wptr<IRemoteObject> &remote) final; 229 int32_t windowId_ = INVALID_WINDOW_ID; 230 }; 231 232 class CaptionPropertyCallbackDeathRecipient final : public IRemoteObject::DeathRecipient { 233 public: 234 CaptionPropertyCallbackDeathRecipient() = default; 235 ~CaptionPropertyCallbackDeathRecipient() final = default; 236 DISALLOW_COPY_AND_MOVE(CaptionPropertyCallbackDeathRecipient); 237 238 void OnRemoteDied(const wptr<IRemoteObject> &remote) final; 239 }; 240 241 class EnableAbilityListsObserverDeathRecipient final : public IRemoteObject::DeathRecipient { 242 public: 243 EnableAbilityListsObserverDeathRecipient() = default; 244 ~EnableAbilityListsObserverDeathRecipient() final = default; 245 DISALLOW_COPY_AND_MOVE(EnableAbilityListsObserverDeathRecipient); 246 247 void OnRemoteDied(const wptr<IRemoteObject> &remote) final; 248 }; 249 250 bool Init(); 251 252 class ConfigCallbackDeathRecipient final : public IRemoteObject::DeathRecipient { 253 public: 254 ConfigCallbackDeathRecipient() = default; 255 ~ConfigCallbackDeathRecipient() final = default; 256 DISALLOW_COPY_AND_MOVE(ConfigCallbackDeathRecipient); 257 258 void OnRemoteDied(const wptr<IRemoteObject> &remote) final; 259 }; 260 261 class BundleManagerDeathRecipient final : public IRemoteObject::DeathRecipient { 262 public: 263 BundleManagerDeathRecipient() = default; 264 ~BundleManagerDeathRecipient() final = default; 265 DISALLOW_COPY_AND_MOVE(BundleManagerDeathRecipient); 266 267 void OnRemoteDied(const wptr<IRemoteObject> &remote) final; 268 }; 269 270 RetError InnerEnableAbility(const std::string &name, const uint32_t capabilities); 271 RetError InnerDisableAbility(const std::string &name); 272 273 sptr<AccessibilityWindowConnection> GetAccessibilityWindowConnection(int32_t windowId); 274 void ClearFocus(int32_t windowId); 275 void OutsideTouch(int32_t windowId); 276 void UpdateAccessibilityWindowStateByEvent(const AccessibilityEventInfo &event); 277 278 void UpdateAccessibilityState(); 279 void UpdateInputFilter(); 280 void UpdateCaptionProperty(); 281 282 void RemoveCallback(CallBackID callback, const sptr<DeathRecipient> &recipient, const wptr<IRemoteObject> &remote); 283 void RemoveSavedConfigCallback(const wptr<IRemoteObject>& callback); 284 void OnBundleManagerDied(const wptr<IRemoteObject> &remote); 285 286 bool isReady_ = false; 287 bool isPublished_ = false; 288 std::map<int32_t, bool> dependentServicesStatus_; 289 int32_t currentAccountId_ = -1; 290 std::map<int32_t, sptr<AccessibilityAccountData>> a11yAccountsData_; 291 sptr<AppExecFwk::IBundleMgr> bundleManager_ = nullptr; 292 293 sptr<AccessibilityInputInterceptor> inputInterceptor_ = nullptr; 294 sptr<TouchEventInjector> touchEventInjector_ = nullptr; 295 sptr<KeyEventFilter> keyEventFilter_ = nullptr; 296 sptr<AccessibilityDumper> accessibilityDumper_ = nullptr; 297 298 std::shared_ptr<AppExecFwk::EventRunner> runner_; 299 std::shared_ptr<AAMSEventHandler> handler_; 300 301 sptr<IRemoteObject::DeathRecipient> stateCallbackDeathRecipient_ = nullptr; 302 std::map<int32_t, sptr<IRemoteObject::DeathRecipient>> interactionOperationDeathRecipients_ {}; 303 sptr<IRemoteObject::DeathRecipient> captionPropertyCallbackDeathRecipient_ = nullptr; 304 sptr<IRemoteObject::DeathRecipient> enableAbilityListsObserverDeathRecipient_ = nullptr; 305 sptr<IRemoteObject::DeathRecipient> configCallbackDeathRecipient_ = nullptr; 306 sptr<IRemoteObject::DeathRecipient> bundleManagerDeathRecipient_ = nullptr; 307 std::vector<sptr<IAccessibleAbilityManagerStateObserver>> stateCallbacks_; 308 std::vector<sptr<IAccessibleAbilityManagerConfigObserver>> defaultConfigCallbacks_; 309 std::mutex mutex_; 310 }; 311 } // namespace Accessibility 312 } // namespace OHOS 313 #endif // ACCESSIBLE_ABILITY_MANAGER_SERVICE_H