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("isIdleStateSync", IsIdleStateSync),
42 DECLARE_NAPI_FUNCTION("queryAppGroupSync", QueryAppGroupSync),
43 DECLARE_NAPI_FUNCTION("queryCurrentBundleEvents", QueryCurrentBundleEvents),
44 DECLARE_NAPI_FUNCTION("queryBundleEvents", QueryBundleEvents),
45 DECLARE_NAPI_FUNCTION("queryBundleStatsInfoByInterval", QueryBundleStatsInfoByInterval),
46 DECLARE_NAPI_FUNCTION("queryBundleStatsInfos", QueryBundleStatsInfos),
47 DECLARE_NAPI_FUNCTION("queryModuleUsageRecords", QueryModuleUsageRecords),
48 DECLARE_NAPI_FUNCTION("setAppGroup", SetAppGroup),
49 DECLARE_NAPI_FUNCTION("registerAppGroupCallBack", RegisterAppGroupCallBack),
50 DECLARE_NAPI_FUNCTION("unregisterAppGroupCallBack", UnRegisterAppGroupCallBack),
51 DECLARE_NAPI_FUNCTION("queryDeviceEventStats", QueryDeviceEventStats),
52 DECLARE_NAPI_FUNCTION("queryNotificationEventStats", QueryNotificationEventStats)
53 };
54
55 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
56
57 InitIntervalType(env, exports);
58 InitAppGroupType(env, exports);
59 return exports;
60 }
61
InitIntervalType(napi_env env,napi_value exports)62 napi_value InitIntervalType(napi_env env, napi_value exports)
63 {
64 napi_value by_optimized;
65 napi_value by_daily;
66 napi_value by_weekly;
67 napi_value by_monthly;
68 napi_value by_annually;
69 int32_t refCount = 1;
70
71 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_OPTIMIZED),
72 &by_optimized);
73 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_DAILY),
74 &by_daily);
75 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_WEEKLY),
76 &by_weekly);
77 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_MONTHLY),
78 &by_monthly);
79 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::IntervalType::BY_ANNUALLY),
80 &by_annually);
81
82 napi_property_descriptor desc[] = {
83 DECLARE_NAPI_STATIC_PROPERTY("BY_OPTIMIZED", by_optimized),
84 DECLARE_NAPI_STATIC_PROPERTY("BY_DAILY", by_daily),
85 DECLARE_NAPI_STATIC_PROPERTY("BY_WEEKLY", by_weekly),
86 DECLARE_NAPI_STATIC_PROPERTY("BY_MONTHLY", by_monthly),
87 DECLARE_NAPI_STATIC_PROPERTY("BY_ANNUALLY", by_annually),
88 };
89
90 napi_value result = nullptr;
91 napi_define_class(env, "IntervalType", NAPI_AUTO_LENGTH, EnumTypeConstructor,
92 nullptr, sizeof(desc) / sizeof(*desc), desc, &result);
93 napi_create_reference(env, result, refCount, &typeConstructor_);
94 napi_set_named_property(env, exports, "IntervalType", result);
95 return exports;
96 }
97
InitAppGroupType(napi_env env,napi_value exports)98 napi_value InitAppGroupType(napi_env env, napi_value exports)
99 {
100 napi_value active_group_alive;
101 napi_value active_group_daily;
102 napi_value active_group_fixed;
103 napi_value active_group_rare;
104 napi_value active_group_limit;
105 napi_value active_group_never;
106 int32_t refCount = 1;
107
108 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::GroupType::ALIVE_GROUP),
109 &active_group_alive);
110 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::GroupType::DAILY_GROUP),
111 &active_group_daily);
112 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::GroupType::FIXED_GROUP),
113 &active_group_fixed);
114 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::GroupType::RARE_GROUP),
115 &active_group_rare);
116 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::GroupType::LIMITED_GROUP),
117 &active_group_limit);
118 napi_create_uint32(env, static_cast<uint32_t>(BundleStateCondition::GroupType::NEVER_GROUP),
119 &active_group_never);
120
121 napi_property_descriptor desc[] = {
122 DECLARE_NAPI_STATIC_PROPERTY("ALIVE_GROUP", active_group_alive),
123 DECLARE_NAPI_STATIC_PROPERTY("DAILY_GROUP", active_group_daily),
124 DECLARE_NAPI_STATIC_PROPERTY("FIXED_GROUP", active_group_fixed),
125 DECLARE_NAPI_STATIC_PROPERTY("RARE_GROUP", active_group_rare),
126 DECLARE_NAPI_STATIC_PROPERTY("LIMITED_GROUP", active_group_limit),
127 DECLARE_NAPI_STATIC_PROPERTY("NEVER_GROUP", active_group_never),
128 };
129
130 napi_value result = nullptr;
131 napi_define_class(env, "GroupType", NAPI_AUTO_LENGTH, EnumTypeConstructor,
132 nullptr, sizeof(desc) / sizeof(*desc), desc, &result);
133 napi_create_reference(env, result, refCount, &typeConstructor_);
134 napi_set_named_property(env, exports, "GroupType", result);
135 return exports;
136 }
137
EnumTypeConstructor(napi_env env,napi_callback_info info)138 napi_value EnumTypeConstructor(napi_env env, napi_callback_info info)
139 {
140 size_t argc = 0;
141 napi_value args[ARG_FIRST] = {0};
142 napi_value res = nullptr;
143 void *data = nullptr;
144
145 napi_status status = napi_get_cb_info(env, info, &argc, args, &res, &data);
146 if (status != napi_ok) {
147 return nullptr;
148 }
149
150 return res;
151 }
152
153 /*
154 * Module register function
155 */
RegisterModule(void)156 __attribute__((constructor)) void RegisterModule(void)
157 {
158 napi_module_register(&_usageStatisticsModule);
159 }
160 EXTERN_C_END
161 } // namespace DeviceUsageStats
162 } // namespace OHOS
163
164