1 /*
2 * Copyright (c) 2025-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 #include <cinttypes>
16 #include "net_stats_settings_observer.h"
17 #include "net_datashare_utils.h"
18 #include "net_mgr_log_wrapper.h"
19 #include "net_manager_constants.h"
20 #include "net_stats_service.h"
21 #include "net_stats_client.h"
22 #include "net_stats_utils.h"
23 namespace OHOS {
24 namespace NetManagerStandard {
25
26 static constexpr const char *CELLULAR_DATA_SETTING_DATA_ENABLE_URI =
27 "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key=cellular_data_enable";
28
29 static constexpr const char *SETTING_URI =
30 "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key=";
31
32 const std::string UNLIMITED_TRAFFIC_ENABLE = "unlimited_traffic_enable";
33 const std::string MONTHLY_LIMITED_TRAFFIC = "monthly_limited_traffic";
34 const std::string MONTHLY_BEGIN_DATE = "monthly_start_date";
35 const std::string MONTHLY_NOTIFY_TYPE = "monthly_notify_type";
36 const std::string OVER_MONTHLY_MARK = "over_monthly_mark";
37 const std::string OVER_DAILY_MARK = "over_daily_mark";
38 const std::string TAG_NAME = "net_stats_";
39
TrafficDataObserver(int32_t simId)40 TrafficDataObserver::TrafficDataObserver(int32_t simId)
41 {
42 NETMGR_LOG_I("TrafficDataObserver start. simId: %{public}d", simId);
43 simId_ = simId;
44 mUnlimitTrafficEnableObserver_ = std::make_unique<UnlimitTrafficEnableObserver>(simId).release();
45 mTrafficMonthlyValueObserver_ = std::make_unique<TrafficMonthlyValueObserver>(simId).release();
46 mTrafficMonthlyBeginDateObserver_ = std::make_unique<TrafficMonthlyBeginDateObserver>(simId).release();
47 mTrafficMonthlyNotifyTypeObserver_ = std::make_unique<TrafficMonthlyNotifyTypeObserver>(simId).release();
48 mTrafficMonthlyMarkObserver_ = std::make_unique<TrafficMonthlyMarkObserver>(simId).release();
49 mTrafficDailyMarkObserver_ = std::make_unique<TrafficDailyMarkObserver>(simId).release();
50 mCellularDataObserver_ = std::make_unique<CellularDataObserver>().release();
51 }
52
RegisterTrafficDataSettingObserver()53 void TrafficDataObserver::RegisterTrafficDataSettingObserver()
54 {
55 NETMGR_LOG_E("RegisterTrafficDataSettingObserver start.");
56 auto dataShareHelperUtils = std::make_unique<NetDataShareHelperUtils>();
57 Uri uri1(CELLULAR_DATA_SETTING_DATA_ENABLE_URI);
58 if (dataShareHelperUtils->RegisterSettingsObserver(uri1, mCellularDataObserver_) != NETSYS_SUCCESS) {
59 NETMGR_LOG_E("register mCellularDataObserver_ failed.");
60 }
61
62 Uri uri2(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + UNLIMITED_TRAFFIC_ENABLE);
63 if (dataShareHelperUtils->RegisterSettingsObserver(uri2, mUnlimitTrafficEnableObserver_) != NETSYS_SUCCESS) {
64 NETMGR_LOG_E("register mUnlimitTrafficEnableObserver_ failed.");
65 }
66
67 Uri uri3(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + MONTHLY_LIMITED_TRAFFIC);
68 if (dataShareHelperUtils->RegisterSettingsObserver(uri3, mTrafficMonthlyValueObserver_) != NETSYS_SUCCESS) {
69 NETMGR_LOG_E("register mTrafficMonthlyValueObserver_ failed.");
70 }
71
72 Uri uri4(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + MONTHLY_BEGIN_DATE);
73 if (dataShareHelperUtils->RegisterSettingsObserver(uri4, mTrafficMonthlyBeginDateObserver_) != NETSYS_SUCCESS) {
74 NETMGR_LOG_E("register mTrafficMonthlyValueObserver_ failed.");
75 }
76
77 Uri uri5(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + MONTHLY_NOTIFY_TYPE);
78 if (dataShareHelperUtils->RegisterSettingsObserver(uri5, mTrafficMonthlyNotifyTypeObserver_) != NETSYS_SUCCESS) {
79 NETMGR_LOG_E("register mTrafficMonthlyNotifyTypeObserver_ failed.");
80 }
81
82 Uri uri6(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + OVER_MONTHLY_MARK);
83 if (dataShareHelperUtils->RegisterSettingsObserver(uri6, mTrafficMonthlyMarkObserver_) != NETSYS_SUCCESS) {
84 NETMGR_LOG_E("register mTrafficMonthlyMarkObserver_ failed.");
85 }
86
87 Uri uri7(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + OVER_DAILY_MARK);
88 if (dataShareHelperUtils->RegisterSettingsObserver(uri7, mTrafficDailyMarkObserver_) != NETSYS_SUCCESS) {
89 NETMGR_LOG_E("register mTrafficDailyMarkObserver_ failed.");
90 }
91 NETMGR_LOG_E("RegisterTrafficDataSettingObserver end.");
92 }
93
UnRegisterTrafficDataSettingObserver()94 void TrafficDataObserver::UnRegisterTrafficDataSettingObserver()
95 {
96 NETMGR_LOG_E("UnRegisterTrafficDataSettingObserver start.");
97 auto dataShareHelperUtils = std::make_unique<NetDataShareHelperUtils>();
98 Uri uri1(CELLULAR_DATA_SETTING_DATA_ENABLE_URI);
99 if (dataShareHelperUtils->UnRegisterSettingsObserver(uri1, mCellularDataObserver_) != NETSYS_SUCCESS) {
100 NETMGR_LOG_E("unregister mCellularDataObserver_ failed.");
101 }
102 Uri uri2(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + UNLIMITED_TRAFFIC_ENABLE);
103 if (dataShareHelperUtils->UnRegisterSettingsObserver(uri2, mUnlimitTrafficEnableObserver_) != NETSYS_SUCCESS) {
104 NETMGR_LOG_E("unregister mUnlimitTrafficEnableObserver_ failed.");
105 }
106 Uri uri3(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + MONTHLY_LIMITED_TRAFFIC);
107 if (dataShareHelperUtils->UnRegisterSettingsObserver(uri3, mTrafficMonthlyValueObserver_) != NETSYS_SUCCESS) {
108 NETMGR_LOG_E("unregister mTrafficMonthlyValueObserver_ failed.");
109 }
110 Uri uri4(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + MONTHLY_BEGIN_DATE);
111 if (dataShareHelperUtils->UnRegisterSettingsObserver(uri4, mTrafficMonthlyBeginDateObserver_) != NETSYS_SUCCESS) {
112 NETMGR_LOG_E("unregister mTrafficMonthlyValueObserver_ failed.");
113 }
114 Uri uri5(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + MONTHLY_NOTIFY_TYPE);
115 if (dataShareHelperUtils->UnRegisterSettingsObserver(uri5, mTrafficMonthlyNotifyTypeObserver_) != NETSYS_SUCCESS) {
116 NETMGR_LOG_E("unregister mTrafficMonthlyNotifyTypeObserver_ failed.");
117 }
118 Uri uri6(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + OVER_MONTHLY_MARK);
119 if (dataShareHelperUtils->UnRegisterSettingsObserver(uri6, mTrafficMonthlyMarkObserver_) != NETSYS_SUCCESS) {
120 NETMGR_LOG_E("unregister mTrafficMonthlyMarkObserver_ failed.");
121 }
122 Uri uri7(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + OVER_DAILY_MARK);
123 if (dataShareHelperUtils->UnRegisterSettingsObserver(uri7, mTrafficDailyMarkObserver_) != NETSYS_SUCCESS) {
124 NETMGR_LOG_E("unregister mTrafficDailyMarkObserver_ failed.");
125 }
126 NETMGR_LOG_E("UnRegisterTrafficDataSettingObserver end.");
127 }
128
ReadTrafficDataSettings(std::shared_ptr<TrafficSettingsInfo> info)129 void TrafficDataObserver::ReadTrafficDataSettings(std::shared_ptr<TrafficSettingsInfo> info)
130 {
131 NETMGR_LOG_E("ReadTrafficDataSettings start.");
132 auto dataShareHelperUtils = std::make_unique<NetDataShareHelperUtils>();
133 // 读取流量设置
134 Uri unLimitUri(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + UNLIMITED_TRAFFIC_ENABLE);
135 std::string value = "";
136 dataShareHelperUtils->Query(unLimitUri, TAG_NAME + std::to_string(simId_) + "_" + UNLIMITED_TRAFFIC_ENABLE, value);
137 info->unLimitedDataEnable = 0;
138 int32_t enable = 0;
139 if (!value.empty() && NetStatsUtils::ConvertToInt32(value, enable)) {
140 info->unLimitedDataEnable = enable;
141 }
142
143 Uri mLimitUri(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + MONTHLY_LIMITED_TRAFFIC);
144 value = "";
145 dataShareHelperUtils->Query(mLimitUri, TAG_NAME + std::to_string(simId_) + "_" + MONTHLY_LIMITED_TRAFFIC, value);
146 info->monthlyLimit = UINT64_MAX;
147 uint64_t trafficInt = 0;
148 if (!value.empty() && NetStatsUtils::ConvertToUint64(value, trafficInt)) {
149 info->monthlyLimit = trafficInt;
150 }
151
152 Uri beginTimeUri(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + MONTHLY_BEGIN_DATE);
153 value = "";
154 dataShareHelperUtils->Query(beginTimeUri, TAG_NAME + std::to_string(simId_) + "_" + MONTHLY_BEGIN_DATE, value);
155 info->beginDate = 1;
156 uint64_t dateInt = 0;
157 if (!value.empty() && NetStatsUtils::ConvertToUint64(value, dateInt)) {
158 info->beginDate = dateInt;
159 }
160
161 ReadTrafficDataSettingsPart2(info);
162 NETMGR_LOG_I("ReadTrafficDataSettings beginDate:%{public}d, unLimitedDataEnable:%{public}d,\
163 monthlyLimitdNotifyType:%{public}d, monthlyLimit:%{public}" PRIu64 ", monthlyMark:%{public}u, dailyMark:%{public}u",
164 info->beginDate, info->unLimitedDataEnable, info->monthlyLimitdNotifyType, info->monthlyLimit,
165 info->monthlyMark, info->dailyMark);
166 }
167
ReadTrafficDataSettingsPart2(std::shared_ptr<TrafficSettingsInfo> info)168 void TrafficDataObserver::ReadTrafficDataSettingsPart2(std::shared_ptr<TrafficSettingsInfo> info)
169 {
170 NETMGR_LOG_E("ReadTrafficDataSettings part2 start.");
171 auto dataShareHelperUtils = std::make_unique<NetDataShareHelperUtils>();
172 Uri typeUri(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + MONTHLY_NOTIFY_TYPE);
173 std::string value = "";
174 dataShareHelperUtils->Query(typeUri, TAG_NAME + std::to_string(simId_) + "_" + MONTHLY_NOTIFY_TYPE, value);
175 info->monthlyLimitdNotifyType = 1; // 1:默认断网
176 int32_t type = 0;
177 if (!value.empty() && NetStatsUtils::ConvertToInt32(value, type)) {
178 info->monthlyLimitdNotifyType = type;
179 }
180
181 Uri mMarkuri(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + OVER_MONTHLY_MARK);
182 value = "";
183 dataShareHelperUtils->Query(mMarkuri, TAG_NAME + std::to_string(simId_) + "_" + OVER_MONTHLY_MARK, value);
184 info->monthlyMark = 80; // 月限额比例默认80%
185 uint64_t mMark = 0;
186 if (!value.empty() && NetStatsUtils::ConvertToUint64(value, mMark)) {
187 info->monthlyMark = mMark;
188 }
189
190 Uri dMarkuri(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + OVER_DAILY_MARK);
191 value = "";
192 dataShareHelperUtils->Query(dMarkuri, TAG_NAME + std::to_string(simId_) + "_" + OVER_DAILY_MARK, value);
193 info->dailyMark = 10; // 日限额比例默认10%
194 uint64_t dMark = 0;
195 if (!value.empty() && NetStatsUtils::ConvertToUint64(value, dMark)) {
196 info->dailyMark = dMark;
197 }
198 }
199
200 // 无限流量开关
UnlimitTrafficEnableObserver(int32_t simId)201 UnlimitTrafficEnableObserver::UnlimitTrafficEnableObserver(int32_t simId) : simId_(simId) {}
202
OnChange()203 void UnlimitTrafficEnableObserver::OnChange()
204 {
205 Uri uri(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + UNLIMITED_TRAFFIC_ENABLE);
206
207 std::string value = "";
208 auto dataShareHelperUtils = std::make_unique<NetDataShareHelperUtils>();
209 dataShareHelperUtils->Query(uri, TAG_NAME + std::to_string(simId_) + "_" + UNLIMITED_TRAFFIC_ENABLE, value);
210 int32_t enable = 0;
211 if (!value.empty()) {
212 NetStatsUtils::ConvertToInt32(value, enable);
213 }
214 NETMGR_LOG_E("UnlimitTrafficEnableObserver OnChanged. dataString: %{public}s, TrafficInt: %{public}d",
215 value.c_str(), enable);
216 DelayedSingleton<NetStatsService>::GetInstance()->UpdataSettingsdata(simId_, NET_STATS_NO_LIMIT_ENABLE, enable);
217 }
218
219 // 套餐限额选项
TrafficMonthlyValueObserver(int32_t simId)220 TrafficMonthlyValueObserver::TrafficMonthlyValueObserver(int32_t simId) : simId_(simId) {}
221
OnChange()222 void TrafficMonthlyValueObserver::OnChange()
223 {
224 Uri uri(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + MONTHLY_LIMITED_TRAFFIC);
225 std::string value = "";
226 auto dataShareHelperUtils = std::make_unique<NetDataShareHelperUtils>();
227 dataShareHelperUtils->Query(uri, TAG_NAME + std::to_string(simId_) + "_" + MONTHLY_LIMITED_TRAFFIC, value);
228 uint64_t trafficInt = 0;
229 if (!value.empty()) {
230 NetStatsUtils::ConvertToUint64(value, trafficInt);
231 }
232 NETMGR_LOG_E("TrafficMonthlyValueObserver OnChanged. dataString: %{public}s, TrafficInt: %{public}lu",
233 value.c_str(), trafficInt);
234 DelayedSingleton<NetStatsService>::GetInstance()->UpdataSettingsdata(simId_, NET_STATS_MONTHLY_LIMIT, trafficInt);
235 }
236
237 // 每月起始日期
TrafficMonthlyBeginDateObserver(int32_t simId)238 TrafficMonthlyBeginDateObserver::TrafficMonthlyBeginDateObserver(int32_t simId) : simId_(simId) {}
239
OnChange()240 void TrafficMonthlyBeginDateObserver::OnChange()
241 {
242 Uri uri(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + MONTHLY_BEGIN_DATE);
243 std::string value = "";
244 auto dataShareHelperUtils = std::make_unique<NetDataShareHelperUtils>();
245 dataShareHelperUtils->Query(uri, TAG_NAME + std::to_string(simId_) + "_" + MONTHLY_BEGIN_DATE, value);
246 int32_t dateInt = 0;
247 if (!value.empty()) {
248 NetStatsUtils::ConvertToInt32(value, dateInt);
249 }
250 NETMGR_LOG_E("TrafficMonthlyBeginDateObserver OnChanged. dataString: %{public}s, dateInt: %{public}d",
251 value.c_str(), dateInt);
252 DelayedSingleton<NetStatsService>::GetInstance()->UpdataSettingsdata(simId_, NET_STATS_BEGIN_DATE, dateInt);
253 }
254
255 // 月超限提醒类型
TrafficMonthlyNotifyTypeObserver(int32_t simId)256 TrafficMonthlyNotifyTypeObserver::TrafficMonthlyNotifyTypeObserver(int32_t simId) : simId_(simId) {}
257
OnChange()258 void TrafficMonthlyNotifyTypeObserver::OnChange()
259 {
260 Uri uri(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + MONTHLY_NOTIFY_TYPE);
261 std::string value = "";
262 auto dataShareHelperUtils = std::make_unique<NetDataShareHelperUtils>();
263 dataShareHelperUtils->Query(uri, TAG_NAME + std::to_string(simId_) + "_" + MONTHLY_NOTIFY_TYPE, value);
264 int32_t typeInt = 0;
265 if (!value.empty()) {
266 NetStatsUtils::ConvertToInt32(value, typeInt);
267 }
268 NETMGR_LOG_E("TrafficMonthlyNotifyTypeObserver OnChanged. typeString: %{public}s, typeInt: %{public}d",
269 value.c_str(), typeInt);
270 DelayedSingleton<NetStatsService>::GetInstance()->UpdataSettingsdata(simId_, NET_STATS_NOTIFY_TYPE, typeInt);
271 }
272
273 // 月超额提醒比例
TrafficMonthlyMarkObserver(int32_t simId)274 TrafficMonthlyMarkObserver::TrafficMonthlyMarkObserver(int32_t simId) : simId_(simId) {}
275
OnChange()276 void TrafficMonthlyMarkObserver::OnChange()
277 {
278 Uri uri(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + OVER_MONTHLY_MARK);
279 std::string value = "";
280 auto dataShareHelperUtils = std::make_unique<NetDataShareHelperUtils>();
281 dataShareHelperUtils->Query(uri, TAG_NAME + std::to_string(simId_) + "_" + OVER_MONTHLY_MARK, value);
282 int32_t percentInt = 0;
283 if (!value.empty()) {
284 NetStatsUtils::ConvertToInt32(value, percentInt);
285 }
286 NETMGR_LOG_E("TrafficMonthlyMarkObserver OnChanged. percentString: %{public}s, percentInt: %{public}d",
287 value.c_str(), percentInt);
288 DelayedSingleton<NetStatsService>::GetInstance()->UpdataSettingsdata(simId_, NET_STATS_MONTHLY_MARK, percentInt);
289 }
290
291 // 日超额提醒比例
TrafficDailyMarkObserver(int32_t simId)292 TrafficDailyMarkObserver::TrafficDailyMarkObserver(int32_t simId) : simId_(simId) {}
293
OnChange()294 void TrafficDailyMarkObserver::OnChange()
295 {
296 Uri uri(SETTING_URI + TAG_NAME + std::to_string(simId_) + "_" + OVER_DAILY_MARK);
297 auto dataShareHelperUtils = std::make_unique<NetDataShareHelperUtils>();
298 std::string value = "";
299 dataShareHelperUtils->Query(uri, TAG_NAME + std::to_string(simId_) + "_" + OVER_DAILY_MARK, value);
300 int32_t percentInt = 0;
301 if (!value.empty()) {
302 NetStatsUtils::ConvertToInt32(value, percentInt);
303 }
304 NETMGR_LOG_E("TrafficDailyMarkObserver OnChanged. percentString: %{public}s, percentInt: %{public}d",
305 value.c_str(), percentInt);
306 DelayedSingleton<NetStatsService>::GetInstance()->UpdataSettingsdata(simId_, NET_STATS_DAILY_MARK, percentInt);
307 }
308
OnChange()309 void CellularDataObserver::OnChange()
310 {
311 auto dataShareHelperUtils = std::make_unique<NetDataShareHelperUtils>();
312 std::string value = "";
313 Uri uri(CELLULAR_DATA_SETTING_DATA_ENABLE_URI);
314 dataShareHelperUtils->Query(uri, "cellular_data_enable", value);
315 NETMGR_LOG_I("CellularDataObserver OnChanged. enable: %{public}s", value.c_str());
316 }
317
318 } // namespace NetManagerStandard
319 } // namespace OHOS
320