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_EVENT_TARGET_H 17 #define ANI_EVENT_TARGET_H 18 19 #include <ani.h> 20 21 #include "define_multimodal.h" 22 #include "error_multimodal.h" 23 #include "input_manager.h" 24 #include "ani_util.h" 25 26 namespace OHOS { 27 namespace MMI { 28 class AniEventTarget : public IInputDeviceListener, public std::enable_shared_from_this<AniEventTarget> { 29 public: 30 AniEventTarget(); 31 virtual ~AniEventTarget(); 32 DISALLOW_COPY_AND_MOVE(AniEventTarget); 33 void AddListener(ani_env *env, const std::string &type, ani_object handle); 34 void ResetEnv(); 35 void OnDeviceAdded(int32_t deviceId, const std::string &type) override; 36 void OnDeviceRemoved(int32_t deviceId, const std::string &type) override; 37 38 private: 39 static void EmitAddedDeviceEvent(const std::shared_ptr<AniUtil::ReportData> &reportData); 40 static void EmitRemoveDeviceEvent(const std::shared_ptr<AniUtil::ReportData> &reportData); 41 static bool EmitCallbackWork(ani_env *env, const std::shared_ptr<AniUtil::ReportData> &reportData, 42 const std::string &type); 43 void GetMainEventHandler(); 44 void PostMainThreadTask(const std::function<void()> task); 45 46 private: 47 inline static std::map<std::string, std::vector<std::unique_ptr<AniUtil::CallbackInfo>>> devListener_ {}; 48 bool isListeningProcess_ { false }; 49 std::shared_ptr<OHOS::AppExecFwk::EventHandler> handler_ = nullptr; 50 }; 51 } // namespace MMI 52 } // namespace OHOS 53 #endif // ANI_EVENT_TARGET_H