1 /*
2 * Copyright (c) 2021-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 "ams_configuration_parameter.h"
17 #include "hilog_wrapper.h"
18
19 namespace OHOS {
20 namespace AAFwk {
21 using json = nlohmann::json;
22 static const int EXPERIENCE_MEM_THRESHOLD = 20;
23
Parse()24 void AmsConfigurationParameter::Parse()
25 {
26 auto ref = LoadAmsConfiguration(AmsConfig::AMS_CONFIG_FILE_PATH);
27 HILOG_INFO("load config ref : %{public}d", ref);
28 }
29
GetStartSettingsDataState() const30 bool AmsConfigurationParameter::GetStartSettingsDataState() const
31 {
32 return canStartSettingsData_;
33 }
34
GetStartScreenLockState() const35 bool AmsConfigurationParameter::GetStartScreenLockState() const
36 {
37 return canStartScreenLock_;
38 }
39
GetStatusBarState() const40 bool AmsConfigurationParameter::GetStatusBarState() const
41 {
42 return canStartUiStatusBar_;
43 }
44
GetNavigationBarState() const45 bool AmsConfigurationParameter::GetNavigationBarState() const
46 {
47 return canStartUiNavigationBar_;
48 }
49
GetPhoneServiceState() const50 bool AmsConfigurationParameter::GetPhoneServiceState() const
51 {
52 return canStartPhoneService_;
53 }
54
GetStartMmsState() const55 bool AmsConfigurationParameter::GetStartMmsState() const
56 {
57 return canStartMms;
58 }
59
NonConfigFile() const60 bool AmsConfigurationParameter::NonConfigFile() const
61 {
62 return nonConfigFile_;
63 }
64
GetMissionSaveTime() const65 int AmsConfigurationParameter::GetMissionSaveTime() const
66 {
67 return missionSaveTime_;
68 }
69
GetOrientation() const70 std::string AmsConfigurationParameter::GetOrientation() const
71 {
72 return orientation_;
73 }
74
IsUseNewMission() const75 bool AmsConfigurationParameter::IsUseNewMission() const
76 {
77 return useNewMission_;
78 }
79
GetANRTimeOutTime() const80 int AmsConfigurationParameter::GetANRTimeOutTime() const
81 {
82 return anrTime_;
83 }
84
GetAMSTimeOutTime() const85 int AmsConfigurationParameter::GetAMSTimeOutTime() const
86 {
87 return amsTime_;
88 }
89
GetMaxRestartNum() const90 int AmsConfigurationParameter::GetMaxRestartNum() const
91 {
92 return maxRestartNum_;
93 }
94
LoadAmsConfiguration(const std::string & filePath)95 int AmsConfigurationParameter::LoadAmsConfiguration(const std::string &filePath)
96 {
97 HILOG_DEBUG("%{public}s", __func__);
98 int ret[2] = {0};
99 std::ifstream inFile;
100 inFile.open(filePath, std::ios::in);
101 if (!inFile.is_open()) {
102 HILOG_INFO("read ams config error ...");
103 nonConfigFile_ = true;
104 return READ_FAIL;
105 }
106
107 json amsJson;
108 inFile >> amsJson;
109 if (amsJson.is_discarded()) {
110 HILOG_INFO("json discarded error ...");
111 nonConfigFile_ = true;
112 inFile.close();
113 return READ_JSON_FAIL;
114 }
115
116 ret[0] = LoadAppConfigurationForStartUpService(amsJson);
117 if (ret[0] != 0) {
118 HILOG_ERROR("LoadAppConfigurationForStartUpService return error");
119 }
120
121 ret[1] = LoadAppConfigurationForMemoryThreshold(amsJson);
122 if (ret[1] != 0) {
123 HILOG_ERROR("LoadAppConfigurationForMemoryThreshold return error");
124 }
125
126 LoadSystemConfiguration(amsJson);
127 amsJson.clear();
128 inFile.close();
129
130 for (auto& i : ret) {
131 if (i != 0) {
132 HILOG_ERROR("json no have service item ...");
133 return READ_JSON_FAIL;
134 }
135 }
136
137 HILOG_INFO("read ams config success!");
138 return READ_OK;
139 }
140
LoadAppConfigurationForStartUpService(nlohmann::json & Object)141 int AmsConfigurationParameter::LoadAppConfigurationForStartUpService(nlohmann::json& Object)
142 {
143 int ret = -1;
144 if (Object.contains(AmsConfig::SERVICE_ITEM_AMS)) {
145 canStartSettingsData_ = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::STARTUP_SETTINGS_DATA).get<bool>();
146 canStartScreenLock_ = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::STARTUP_SCREEN_LOCK).get<bool>();
147 canStartUiStatusBar_ = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::STARTUP_STATUS_BAR).get<bool>();
148 canStartUiNavigationBar_ =
149 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::STARTUP_NAVIGATION_BAR).get<bool>();
150 canStartPhoneService_ =
151 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::STARTUP_PHONE_SERVICE).get<bool>();
152 canStartMms = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::STARTUP_MMS).get<bool>();
153 missionSaveTime_ = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::MISSION_SAVE_TIME).get<int>();
154 anrTime_ =
155 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::APP_NOT_RESPONSE_PROCESS_TIMEOUT_TIME).get<int>();
156 amsTime_ =
157 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::AMS_TIMEOUT_TIME).get<int>();
158 if (Object.at(AmsConfig::SERVICE_ITEM_AMS).contains(AmsConfig::USE_NEW_MISSION)) {
159 useNewMission_ = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::USE_NEW_MISSION).get<bool>();
160 }
161 maxRestartNum_ = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::ROOT_LAUNCHER_RESTART_MAX).get<int>();
162 HILOG_INFO("get ams service config succes!");
163 ret = 0;
164 }
165
166 return ret;
167 }
168
LoadAppConfigurationForMemoryThreshold(nlohmann::json & Object)169 int AmsConfigurationParameter::LoadAppConfigurationForMemoryThreshold(nlohmann::json &Object)
170 {
171 int ret = 0;
172 if (!Object.contains("memorythreshold")) {
173 HILOG_ERROR("LoadAppConfigurationForMemoryThreshold return error");
174 ret = -1;
175 return ret;
176 }
177
178 if (Object.at("memorythreshold").contains("home_application")) {
179 memThreshold_["home_application"] = Object.at("memorythreshold").at("home_application").get<std::string>();
180 } else {
181 HILOG_ERROR("LoadAppConfigurationForMemoryThreshold memorythreshold::home_application is nullptr");
182 }
183
184 return ret;
185 }
186
LoadSystemConfiguration(nlohmann::json & Object)187 int AmsConfigurationParameter::LoadSystemConfiguration(nlohmann::json& Object)
188 {
189 if (Object.contains(AmsConfig::SYSTEM_CONFIGURATION)) {
190 orientation_ = Object.at(AmsConfig::SYSTEM_CONFIGURATION).at(AmsConfig::SYSTEM_ORIENTATION).get<std::string>();
191 return READ_OK;
192 }
193
194 return READ_FAIL;
195 }
196
197 /**
198 * The low memory threshold under which the system will kill background processes
199 */
GetMemThreshold(const std::string & key)200 int AmsConfigurationParameter::GetMemThreshold(const std::string &key)
201 {
202 auto threshold = memThreshold_.find(key);
203 if (threshold == memThreshold_.end()) {
204 HILOG_ERROR("%{public}s, threshold[%{public}s] find failed", __func__, key.c_str());
205 return EXPERIENCE_MEM_THRESHOLD;
206 }
207
208 return std::stoi(threshold->second);
209 }
210
211 } // namespace AAFwk
212 } // namespace OHOS
213