1 /* 2 * Copyright (c) 2024 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 MOTION_NAPI_H 17 #define MOTION_NAPI_H 18 19 #include <map> 20 #include <uv.h> 21 22 #include "napi/native_api.h" 23 #include "napi/native_node_api.h" 24 25 #ifdef MOTION_ENABLE 26 #include "motion_callback_stub.h" 27 #endif 28 29 #include "motion_event_napi.h" 30 31 namespace OHOS { 32 namespace Msdp { 33 #ifdef MOTION_ENABLE 34 class MotionCallback : public MotionCallbackStub { 35 public: MotionCallback(napi_env env)36 explicit MotionCallback(napi_env env) : env_(env) {} ~MotionCallback()37 ~MotionCallback() override {}; 38 void OnMotionChanged(const MotionEvent& event) override; 39 static void EmitOnEvent(MotionEvent* data); 40 41 private: 42 napi_env env_; 43 }; 44 #endif 45 46 class MotionNapi : public MotionEventNapi { 47 public: 48 explicit MotionNapi(napi_env env, napi_value thisVar); 49 ~MotionNapi() override; 50 51 static napi_value Init(napi_env env, napi_value exports); 52 static void DefineHoldingHandStatus(napi_env env, napi_value exports); 53 static napi_value SubscribeMotion(napi_env env, napi_callback_info info); 54 static napi_value UnSubscribeMotion(napi_env env, napi_callback_info info); 55 static napi_value GetRecentOptHandStatus(napi_env env, napi_callback_info info); 56 public: 57 #ifdef MOTION_ENABLE 58 std::map<int32_t, sptr<IMotionCallback>> callbacks_; 59 #endif 60 61 private: 62 #ifdef MOTION_ENABLE 63 static bool SubscribeCallback(napi_env env, int32_t type); 64 static bool UnSubscribeCallback(napi_env env, int32_t type); 65 #endif 66 static int32_t GetMotionType(const std::string &type); 67 static bool ConstructMotion(napi_env env, napi_value jsThis); 68 static bool ValidateArgsType(napi_env env, napi_value *args, size_t argc, 69 const std::vector<std::string> &expectedTypes); 70 static bool TransJsToStr(napi_env env, napi_value value, std::string &str); 71 static void SetInt32Property(napi_env env, napi_value targetObj, int32_t value, const char *propName); 72 static void SetPropertyName(napi_env env, napi_value targetObj, const char *propName, napi_value propValue); 73 74 private: 75 napi_env env_; 76 }; 77 } // namespace Msdp 78 } // namespace OHOS 79 #endif //MOTION_NAPI_H 80