• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_UTIL_H
17 #define ANI_UTIL_H
18 #include <ani.h>
19 
20 #include "input_device.h"
21 
22 namespace OHOS {
23 namespace MMI {
24 class AniUtil {
25 public:
26     struct ReportData {
27         ani_ref ref { nullptr };
28         int32_t deviceId { 0 };
29     };
30     struct CallbackInfo {
31         ani_env *env_ { nullptr };
32         ani_ref callback_ { nullptr };
33         bool SetCallback(ani_object handle);
34         ~CallbackInfo();
35     };
36     struct DeviceType {
37         std::string sourceTypeName;
38         uint32_t typeBit { 0 };
39     };
40 
41     static bool IsSameHandle(ani_env *env, ani_ref handle, ani_env *iterEnv, ani_ref iterhandle);
42     static ani_string StdStringToANIString(ani_env* env, const std::string& str);
43     static ani_boolean IsInstanceOf(ani_env *env, const std::string &cls_name, ani_object obj);
44     static ani_object CreateAniObject(ani_env *env, const char *nsName, const char *className);
45 };
46 
47 class AniLocalScopeGuard {
48 public:
AniLocalScopeGuard(ani_env * env,size_t nrRefs)49     AniLocalScopeGuard(ani_env *env, size_t nrRefs) : env_(env)
50     {
51         status_ = env_->CreateLocalScope(nrRefs);
52     }
53 
~AniLocalScopeGuard()54     ~AniLocalScopeGuard()
55     {
56         if (ANI_OK != status_) {
57             return;
58         }
59         env_->DestroyLocalScope();
60     }
61 
IsStatusOK()62     bool IsStatusOK()
63     {
64         return ANI_OK == status_;
65     }
66 
GetStatus()67     ani_status GetStatus()
68     {
69         return status_;
70     }
71 
72 private:
73     ani_env *env_ = nullptr;
74     ani_status status_ = ANI_ERROR;
75 };
76 } // namespace MMI
77 } // namespace OHOS
78 #endif // ANI_UTIL_H