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 #include "ui_appearance_log.h"
17 #include "background_app_color_switch_settings.h"
18 #include "json_utils.h"
19 #include <algorithm>
20
21 namespace {
22 constexpr const char* CONFIG_PATH = "/etc/dark_mode_whilelist.json";
23 constexpr const char* ALLOW_LIST = "whiteList";
24 constexpr const char* BUNDLE_NAME = "bundleName";
25 constexpr const char* STRATEGY = "strategy";
26 constexpr const char* DURATION = "duration";
27 constexpr const char* PERTASK_NUMBER = "perTaskNumber";
28 } // namespace
29
30 namespace OHOS::ArkUi::UiAppearance {
GetInstance()31 BackGroundAppColorSwitchSettings &BackGroundAppColorSwitchSettings::GetInstance()
32 {
33 static BackGroundAppColorSwitchSettings instance;
34 return instance;
35 }
36
IsSupportHotUpdate()37 bool BackGroundAppColorSwitchSettings::IsSupportHotUpdate()
38 {
39 std::lock_guard lock(policyMutex_);
40 return isAllowListEnable_;
41 }
42
Initialize()43 ErrCode BackGroundAppColorSwitchSettings::Initialize()
44 {
45 std::lock_guard lock(policyMutex_);
46 nlohmann::json object;
47 if (!JsonUtils::LoadConfiguration(CONFIG_PATH, object)) {
48 LOGW("BackGroundAppColorSwitchSettings read file failed");
49 return ERR_NAME_NOT_FOUND;
50 }
51
52 if (!object.contains(ALLOW_LIST) || !object.at(ALLOW_LIST).is_array()) {
53 LOGW("BackGroundAppColorSwitchSettings unable to query white list");
54 return ERR_INVALID_VALUE;
55 }
56
57 for (auto &item : object.at(ALLOW_LIST).items()) {
58 const nlohmann::json& jsonObject = item.value();
59 if (!jsonObject.contains(BUNDLE_NAME) || !jsonObject.at(BUNDLE_NAME).is_string()) {
60 continue;
61 }
62 auto bundleName = jsonObject.at(BUNDLE_NAME).get<std::string>();
63 LOGI("insert allowList_ bundleName = %{public}s", bundleName.c_str());
64 allowList_.push_back(bundleName);
65 }
66
67 if (!object.contains(STRATEGY)) {
68 LOGW("BackGroundAppColorSwitchSettings unable to query strategy");
69 return ERR_NAME_NOT_FOUND;
70 }
71
72 auto strategy = object.at(STRATEGY);
73 if (!strategy.contains(DURATION) || !strategy.at(DURATION).is_number()) {
74 LOGW("BackGroundAppColorSwitchSettings unable to query duration");
75 return ERR_INVALID_VALUE;
76 }
77 durationMillisecond_ = strategy.at(DURATION).get<int32_t>();
78
79 if (!strategy.contains(PERTASK_NUMBER) || !strategy.at(PERTASK_NUMBER).is_number()) {
80 LOGW("BackGroundAppColorSwitchSettings unable to query perTaskNumber");
81 return ERR_INVALID_VALUE;
82 }
83 taskQuantity_ = strategy.at(PERTASK_NUMBER).get<int32_t>();
84 if (taskQuantity_ <= 0 || durationMillisecond_ <= 0) {
85 LOGW("settings error, taskQuantity_:%{public}d durationMillisecond_:%{public}d",
86 taskQuantity_, durationMillisecond_);
87 return ERR_INVALID_VALUE;
88 }
89 isAllowListEnable_ = true;
90
91 LOGI("taskQuantity_= %{public}d durationMillisecond_= %{public}d", taskQuantity_, durationMillisecond_);
92 return ERR_OK;
93 }
94
GetTaskQuantity()95 int32_t BackGroundAppColorSwitchSettings::GetTaskQuantity()
96 {
97 std::lock_guard lock(policyMutex_);
98 return taskQuantity_;
99 }
100
GetDurationMillisecond()101 int32_t BackGroundAppColorSwitchSettings::GetDurationMillisecond()
102 {
103 std::lock_guard lock(policyMutex_);
104 return durationMillisecond_;
105 }
106
CheckInWhileList(const std::string & bundleName)107 bool BackGroundAppColorSwitchSettings::CheckInWhileList(const std::string& bundleName)
108 {
109 std::lock_guard lock(policyMutex_);
110 if (std::find(allowList_.begin(), allowList_.end(), bundleName) != allowList_.end()) {
111 return true;
112 }
113 return false;
114 }
115
Reset()116 void BackGroundAppColorSwitchSettings::Reset()
117 {
118 std::lock_guard lock(policyMutex_);
119 allowList_.clear();
120 taskQuantity_ = -1;
121 durationMillisecond_ = -1;
122 isAllowListEnable_ = false;
123 }
124
GetWhileList()125 std::list<std::string> BackGroundAppColorSwitchSettings::GetWhileList()
126 {
127 std::lock_guard lock(policyMutex_);
128 return allowList_;
129 }
130 } // namespace OHOS::ArkUi::UiAppearance
131