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 GetInstance()22AmsConfigurationParameter &AmsConfigurationParameter::GetInstance() 23 { 24 static AmsConfigurationParameter amsConfiguration; 25 return amsConfiguration; 26 } 27 28 using json = nlohmann::json; 29 Parse()30void AmsConfigurationParameter::Parse() 31 { 32 auto ref = LoadAmsConfiguration(AmsConfig::AMS_CONFIG_FILE_PATH); 33 HILOG_INFO("load config ref : %{public}d", ref); 34 } 35 NonConfigFile() const36bool AmsConfigurationParameter::NonConfigFile() const 37 { 38 return nonConfigFile_; 39 } 40 GetMissionSaveTime() const41int AmsConfigurationParameter::GetMissionSaveTime() const 42 { 43 return missionSaveTime_; 44 } 45 GetOrientation() const46std::string AmsConfigurationParameter::GetOrientation() const 47 { 48 return orientation_; 49 } 50 GetANRTimeOutTime() const51int AmsConfigurationParameter::GetANRTimeOutTime() const 52 { 53 return anrTime_; 54 } 55 GetAMSTimeOutTime() const56int AmsConfigurationParameter::GetAMSTimeOutTime() const 57 { 58 return amsTime_; 59 } 60 GetMaxRestartNum(bool isRootLauncher) const61int AmsConfigurationParameter::GetMaxRestartNum(bool isRootLauncher) const 62 { 63 return (isRootLauncher ? maxRootLauncherRestartNum_ : maxResidentRestartNum_); 64 } 65 GetRestartIntervalTime() const66int AmsConfigurationParameter::GetRestartIntervalTime() const 67 { 68 return restartIntervalTime_; 69 } 70 GetDeviceType() const71std::string AmsConfigurationParameter::GetDeviceType() const 72 { 73 return deviceType_; 74 } 75 GetBootAnimationTimeoutTime() const76int AmsConfigurationParameter::GetBootAnimationTimeoutTime() const 77 { 78 return bootAnimationTime_; 79 } 80 GetAppStartTimeoutTime() const81int AmsConfigurationParameter::GetAppStartTimeoutTime() const 82 { 83 return timeoutUnitTime_; 84 } 85 LoadAmsConfiguration(const std::string & filePath)86int AmsConfigurationParameter::LoadAmsConfiguration(const std::string &filePath) 87 { 88 HILOG_DEBUG("%{public}s", __func__); 89 int ret[2] = {0}; 90 std::ifstream inFile; 91 inFile.open(filePath, std::ios::in); 92 if (!inFile.is_open()) { 93 HILOG_INFO("read ams config error ..."); 94 nonConfigFile_ = true; 95 return READ_FAIL; 96 } 97 98 json amsJson; 99 inFile >> amsJson; 100 if (amsJson.is_discarded()) { 101 HILOG_INFO("json discarded error ..."); 102 nonConfigFile_ = true; 103 inFile.close(); 104 return READ_JSON_FAIL; 105 } 106 107 ret[0] = LoadAppConfigurationForStartUpService(amsJson); 108 if (ret[0] != 0) { 109 HILOG_ERROR("LoadAppConfigurationForStartUpService return error"); 110 } 111 112 ret[1] = LoadAppConfigurationForMemoryThreshold(amsJson); 113 if (ret[1] != 0) { 114 HILOG_ERROR("LoadAppConfigurationForMemoryThreshold return error"); 115 } 116 117 LoadSystemConfiguration(amsJson); 118 amsJson.clear(); 119 inFile.close(); 120 121 for (const auto& i : ret) { 122 if (i != 0) { 123 HILOG_ERROR("json no have service item ..."); 124 return READ_JSON_FAIL; 125 } 126 } 127 128 HILOG_INFO("read ams config success!"); 129 return READ_OK; 130 } 131 LoadAppConfigurationForStartUpService(nlohmann::json & Object)132int AmsConfigurationParameter::LoadAppConfigurationForStartUpService(nlohmann::json& Object) 133 { 134 int ret = -1; 135 if (Object.contains(AmsConfig::SERVICE_ITEM_AMS)) { 136 missionSaveTime_ = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::MISSION_SAVE_TIME).get<int>(); 137 anrTime_ = 138 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::APP_NOT_RESPONSE_PROCESS_TIMEOUT_TIME).get<int>(); 139 amsTime_ = 140 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::AMS_TIMEOUT_TIME).get<int>(); 141 maxRootLauncherRestartNum_ = 142 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::ROOT_LAUNCHER_RESTART_MAX).get<int>(); 143 if (Object.at(AmsConfig::SERVICE_ITEM_AMS).contains(AmsConfig::RESIDENT_RESTART_MAX)) { 144 maxResidentRestartNum_ = 145 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::RESIDENT_RESTART_MAX).get<int>(); 146 } 147 if (Object.at(AmsConfig::SERVICE_ITEM_AMS).contains(AmsConfig::RESTART_INTERVAL_TIME)) { 148 restartIntervalTime_ = 149 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::RESTART_INTERVAL_TIME).get<int>(); 150 } 151 deviceType_ = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::DEVICE_TYPE).get<std::string>(); 152 bootAnimationTime_ = 153 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::BOOT_ANIMATION_TIMEOUT_TIME).get<int>(); 154 if (Object.at(AmsConfig::SERVICE_ITEM_AMS).contains(AmsConfig::TIMEOUT_UNIT_TIME)) { 155 timeoutUnitTime_ = 156 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::TIMEOUT_UNIT_TIME).get<int>(); 157 } 158 HILOG_INFO("get ams service config success!"); 159 ret = 0; 160 } 161 162 return ret; 163 } 164 LoadAppConfigurationForMemoryThreshold(nlohmann::json & Object)165int AmsConfigurationParameter::LoadAppConfigurationForMemoryThreshold(nlohmann::json &Object) 166 { 167 int ret = 0; 168 if (!Object.contains("memorythreshold")) { 169 HILOG_ERROR("LoadAppConfigurationForMemoryThreshold return error"); 170 ret = -1; 171 } 172 173 return ret; 174 } 175 LoadSystemConfiguration(nlohmann::json & Object)176int AmsConfigurationParameter::LoadSystemConfiguration(nlohmann::json& Object) 177 { 178 if (Object.contains(AmsConfig::SYSTEM_CONFIGURATION)) { 179 orientation_ = Object.at(AmsConfig::SYSTEM_CONFIGURATION).at(AmsConfig::SYSTEM_ORIENTATION).get<std::string>(); 180 return READ_OK; 181 } 182 183 return READ_FAIL; 184 } 185 } // namespace AAFwk 186 } // namespace OHOS 187