• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "usage_statistics_init.h"
17 
18 #include "bundle_state_condition.h"
19 #include "bundle_state_query_napi.h"
20 #include "bundle_active_app_group_napi.h"
21 
22 namespace OHOS {
23 namespace DeviceUsageStats {
24 EXTERN_C_START
25 
26 static const uint8_t ARG_FIRST = 1;
27 
28 napi_ref typeConstructor_ = nullptr;
29 
30 /*
31  * Module export function
32  */
UsageStatisticsInit(napi_env env,napi_value exports)33 static napi_value UsageStatisticsInit(napi_env env, napi_value exports)
34 {
35     /*
36      * Propertise define
37      */
38     napi_property_descriptor desc[] = {
39         DECLARE_NAPI_FUNCTION("isIdleState", IsIdleState),
40         DECLARE_NAPI_FUNCTION("queryAppGroup", QueryAppGroup),
41         DECLARE_NAPI_FUNCTION("queryCurrentBundleEvents", QueryCurrentBundleEvents),
42         DECLARE_NAPI_FUNCTION("queryBundleEvents", QueryBundleEvents),
43         DECLARE_NAPI_FUNCTION("queryBundleStatsInfoByInterval", QueryBundleStatsInfoByInterval),
44         DECLARE_NAPI_FUNCTION("queryBundleStatsInfos", QueryBundleStatsInfos),
45         DECLARE_NAPI_FUNCTION("queryModuleUsageRecords", QueryModuleUsageRecords),
46         DECLARE_NAPI_FUNCTION("setAppGroup", SetAppGroup),
47         DECLARE_NAPI_FUNCTION("registerAppGroupCallBack", RegisterAppGroupCallBack),
48         DECLARE_NAPI_FUNCTION("unregisterAppGroupCallBack", UnRegisterAppGroupCallBack),
49         DECLARE_NAPI_FUNCTION("queryDeviceEventStats", QueryDeviceEventStats),
50         DECLARE_NAPI_FUNCTION("queryNotificationEventStats", QueryNotificationEventStats)
51     };
52 
53     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
54 
55     InitIntervalType(env, exports);
56     InitAppGroupType(env, exports);
57     return exports;
58 }
59 
InitIntervalType(napi_env env,napi_value exports)60 napi_value InitIntervalType(napi_env env, napi_value exports)
61 {
62     napi_value by_optimized;
63     napi_value by_daily;
64     napi_value by_weekly;
65     napi_value by_monthly;
66     napi_value by_annually;
67     int32_t refCount = 1;
68 
69     napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_OPTIMIZED),
70         &by_optimized);
71     napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_DAILY),
72         &by_daily);
73     napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_WEEKLY),
74         &by_weekly);
75     napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_MONTHLY),
76         &by_monthly);
77     napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_ANNUALLY),
78         &by_annually);
79 
80     napi_property_descriptor desc[] = {
81         DECLARE_NAPI_STATIC_PROPERTY("BY_OPTIMIZED", by_optimized),
82         DECLARE_NAPI_STATIC_PROPERTY("BY_DAILY", by_daily),
83         DECLARE_NAPI_STATIC_PROPERTY("BY_WEEKLY", by_weekly),
84         DECLARE_NAPI_STATIC_PROPERTY("BY_MONTHLY", by_monthly),
85         DECLARE_NAPI_STATIC_PROPERTY("BY_ANNUALLY", by_annually),
86     };
87 
88     napi_value result = nullptr;
89     napi_define_class(env, "IntervalType", NAPI_AUTO_LENGTH, EnumTypeConstructor,
90         nullptr, sizeof(desc) / sizeof(*desc), desc, &result);
91     napi_create_reference(env, result, refCount, &typeConstructor_);
92     napi_set_named_property(env, exports, "IntervalType", result);
93     return exports;
94 }
95 
InitAppGroupType(napi_env env,napi_value exports)96 napi_value InitAppGroupType(napi_env env, napi_value exports)
97 {
98     napi_value active_group_alive;
99     napi_value active_group_daily;
100     napi_value active_group_fixed;
101     napi_value active_group_rare;
102     napi_value active_group_limit;
103     napi_value active_group_never;
104     int32_t refCount = 1;
105 
106     napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::AppGroupType::ALIVE_GROUP),
107         &active_group_alive);
108     napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::AppGroupType::DAILY_GROUP),
109         &active_group_daily);
110     napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::AppGroupType::FIXED_GROUP),
111         &active_group_fixed);
112     napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::AppGroupType::RARE_GROUP),
113         &active_group_rare);
114     napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::AppGroupType::LIMITED_GROUP),
115         &active_group_limit);
116     napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::AppGroupType::NEVER_GROUP),
117         &active_group_never);
118 
119     napi_property_descriptor desc[] = {
120         DECLARE_NAPI_STATIC_PROPERTY("ACTIVE_GROUP_ALIVE", active_group_alive),
121         DECLARE_NAPI_STATIC_PROPERTY("ACTIVE_GROUP_DAILY", active_group_daily),
122         DECLARE_NAPI_STATIC_PROPERTY("ACTIVE_GROUP_FIXED", active_group_fixed),
123         DECLARE_NAPI_STATIC_PROPERTY("ACTIVE_GROUP_RARE", active_group_rare),
124         DECLARE_NAPI_STATIC_PROPERTY("ACTIVE_GROUP_LIMIT", active_group_limit),
125         DECLARE_NAPI_STATIC_PROPERTY("ACTIVE_GROUP_NEVER", active_group_never),
126     };
127 
128     napi_value result = nullptr;
129     napi_define_class(env, "AppGroupType", NAPI_AUTO_LENGTH, EnumTypeConstructor,
130         nullptr, sizeof(desc) / sizeof(*desc), desc, &result);
131     napi_create_reference(env, result, refCount, &typeConstructor_);
132     napi_set_named_property(env, exports, "AppGroupType", result);
133     return exports;
134 }
135 
EnumTypeConstructor(napi_env env,napi_callback_info info)136 napi_value EnumTypeConstructor(napi_env env, napi_callback_info info)
137 {
138     size_t argc = 0;
139     napi_value args[ARG_FIRST] = {0};
140     napi_value res = nullptr;
141     void *data = nullptr;
142 
143     napi_status status = napi_get_cb_info(env, info, &argc, args, &res, &data);
144     if (status != napi_ok) {
145         return nullptr;
146     }
147 
148     return res;
149 }
150 
151 /*
152  * Module register function
153  */
RegisterModule(void)154 __attribute__((constructor)) void RegisterModule(void)
155 {
156     napi_module_register(&_usageStatisticsModule);
157 }
158 EXTERN_C_END
159 }  // namespace DeviceUsageStats
160 }  // namespace OHOS
161 
162