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