1 /* 2 * Copyright (C) 2025 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 ANI_ACCESSIBILITY_SYSTEM_ABILITY_CLIENT_H 17 #define ANI_ACCESSIBILITY_SYSTEM_ABILITY_CLIENT_H 18 19 #include <vector> 20 #include <map> 21 #include <ani.h> 22 #include "accessibility_system_ability_client.h" 23 #include "accessibility_state_event.h" 24 #include "ffrt.h" 25 26 struct ANIStateCallbackInfo { 27 ani_env *env_; 28 ani_ref fnRef_; 29 bool state_; 30 std::string mode_; 31 }; 32 33 struct StateListener { env_StateListener34 StateListener(ani_env *env, ani_ref fnRef, bool isBoolObserver = true) : env_(env), fnRef_(fnRef), 35 isBoolObserver_(isBoolObserver) {}; 36 static void NotifyETS(ani_env *env, bool state, ani_ref fnRef); 37 static void NotifyETS(ani_env *env, std::string mode, ani_ref fnRef); 38 void OnStateChanged(const bool state); 39 void OnStateChanged(const std::string mode); 40 41 ani_env* env_; 42 ani_ref fnRef_; 43 bool isBoolObserver_ = true; 44 }; 45 46 class StateListenerImpl : public OHOS::Accessibility::AccessibilityStateObserver, 47 public std::enable_shared_from_this<StateListenerImpl> { 48 public: StateListenerImpl(OHOS::Accessibility::AccessibilityStateEventType type)49 StateListenerImpl(OHOS::Accessibility::AccessibilityStateEventType type) : type_(type) {}; 50 void OnStateChanged(const bool state) override; 51 void SubscribeToFramework(); 52 void UnsubscribeFromFramework(); 53 void SubscribeObserver(ani_env *env, ani_object observer, bool isBoolObserver = true); 54 void UnsubscribeObserver(ani_env *env, ani_object observer); 55 void UnsubscribeObservers(); 56 57 private: 58 ffrt::mutex mutex_; 59 OHOS::Accessibility::AccessibilityStateEventType type_; 60 std::vector<std::shared_ptr<StateListener>> observers_ = {}; 61 }; 62 63 class ANIAccessibilityClient { 64 public: 65 static ani_boolean IsOpenTouchGuideSync([[maybe_unused]] ani_env *env); 66 static ani_boolean IsOpenAccessibilitySync([[maybe_unused]] ani_env *env); 67 static void SubscribeState(ani_env *env, ani_string type, ani_object callback); 68 static void UnsubscribeState(ani_env *env, ani_string type, ani_object callback); 69 static void UnsubscribeStateAll(ani_env *env, ani_string type); 70 static void SendAccessibilityEvent(ani_env *env, ani_object eventObject); 71 72 static std::shared_ptr<StateListenerImpl> accessibilityStateListeners_; 73 static std::shared_ptr<StateListenerImpl> touchGuideStateListeners_; 74 static std::shared_ptr<StateListenerImpl> screenReaderStateListeners_; 75 static std::shared_ptr<StateListenerImpl> touchModeListeners_; 76 77 private: 78 ANIAccessibilityClient() = default; 79 ~ANIAccessibilityClient() = default; 80 }; 81 82 #endif // ANI_ACCESSIBILITY_SYSTEM_ABILITY_CLIENT_H