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 Parse()23void AmsConfigurationParameter::Parse() 24 { 25 auto ref = LoadAmsConfiguration(AmsConfig::AMS_CONFIG_FILE_PATH); 26 HILOG_INFO("load config ref : %{public}d", ref); 27 } 28 NonConfigFile() const29bool AmsConfigurationParameter::NonConfigFile() const 30 { 31 return nonConfigFile_; 32 } 33 GetMissionSaveTime() const34int AmsConfigurationParameter::GetMissionSaveTime() const 35 { 36 return missionSaveTime_; 37 } 38 GetOrientation() const39std::string AmsConfigurationParameter::GetOrientation() const 40 { 41 return orientation_; 42 } 43 GetANRTimeOutTime() const44int AmsConfigurationParameter::GetANRTimeOutTime() const 45 { 46 return anrTime_; 47 } 48 GetAMSTimeOutTime() const49int AmsConfigurationParameter::GetAMSTimeOutTime() const 50 { 51 return amsTime_; 52 } 53 GetMaxRestartNum(bool isRootLauncher) const54int AmsConfigurationParameter::GetMaxRestartNum(bool isRootLauncher) const 55 { 56 return (isRootLauncher ? maxRootLauncherRestartNum_ : maxResidentRestartNum_); 57 } 58 GetRestartIntervalTime() const59int AmsConfigurationParameter::GetRestartIntervalTime() const 60 { 61 return restartIntervalTime_; 62 } 63 GetDeviceType() const64std::string AmsConfigurationParameter::GetDeviceType() const 65 { 66 return deviceType_; 67 } 68 GetBootAnimationTimeoutTime() const69int AmsConfigurationParameter::GetBootAnimationTimeoutTime() const 70 { 71 return bootAnimationTime_; 72 } 73 LoadAmsConfiguration(const std::string & filePath)74int AmsConfigurationParameter::LoadAmsConfiguration(const std::string &filePath) 75 { 76 HILOG_DEBUG("%{public}s", __func__); 77 int ret[2] = {0}; 78 std::ifstream inFile; 79 inFile.open(filePath, std::ios::in); 80 if (!inFile.is_open()) { 81 HILOG_INFO("read ams config error ..."); 82 nonConfigFile_ = true; 83 return READ_FAIL; 84 } 85 86 json amsJson; 87 inFile >> amsJson; 88 if (amsJson.is_discarded()) { 89 HILOG_INFO("json discarded error ..."); 90 nonConfigFile_ = true; 91 inFile.close(); 92 return READ_JSON_FAIL; 93 } 94 95 ret[0] = LoadAppConfigurationForStartUpService(amsJson); 96 if (ret[0] != 0) { 97 HILOG_ERROR("LoadAppConfigurationForStartUpService return error"); 98 } 99 100 ret[1] = LoadAppConfigurationForMemoryThreshold(amsJson); 101 if (ret[1] != 0) { 102 HILOG_ERROR("LoadAppConfigurationForMemoryThreshold return error"); 103 } 104 105 LoadSystemConfiguration(amsJson); 106 amsJson.clear(); 107 inFile.close(); 108 109 for (const auto& i : ret) { 110 if (i != 0) { 111 HILOG_ERROR("json no have service item ..."); 112 return READ_JSON_FAIL; 113 } 114 } 115 116 HILOG_INFO("read ams config success!"); 117 return READ_OK; 118 } 119 LoadAppConfigurationForStartUpService(nlohmann::json & Object)120int AmsConfigurationParameter::LoadAppConfigurationForStartUpService(nlohmann::json& Object) 121 { 122 int ret = -1; 123 if (Object.contains(AmsConfig::SERVICE_ITEM_AMS)) { 124 missionSaveTime_ = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::MISSION_SAVE_TIME).get<int>(); 125 anrTime_ = 126 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::APP_NOT_RESPONSE_PROCESS_TIMEOUT_TIME).get<int>(); 127 amsTime_ = 128 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::AMS_TIMEOUT_TIME).get<int>(); 129 maxRootLauncherRestartNum_ = 130 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::ROOT_LAUNCHER_RESTART_MAX).get<int>(); 131 if (Object.at(AmsConfig::SERVICE_ITEM_AMS).contains(AmsConfig::RESIDENT_RESTART_MAX)) { 132 maxResidentRestartNum_ = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::RESIDENT_RESTART_MAX).get<int>(); 133 } 134 if (Object.at(AmsConfig::SERVICE_ITEM_AMS).contains(AmsConfig::RESTART_INTERVAL_TIME)) { 135 restartIntervalTime_ = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::RESTART_INTERVAL_TIME).get<int>(); 136 } 137 deviceType_ = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::DEVICE_TYPE).get<std::string>(); 138 bootAnimationTime_ = 139 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::BOOT_ANIMATION_TIMEOUT_TIME).get<int>(); 140 HILOG_INFO("get ams service config success!"); 141 ret = 0; 142 } 143 144 return ret; 145 } 146 LoadAppConfigurationForMemoryThreshold(nlohmann::json & Object)147int AmsConfigurationParameter::LoadAppConfigurationForMemoryThreshold(nlohmann::json &Object) 148 { 149 int ret = 0; 150 if (!Object.contains("memorythreshold")) { 151 HILOG_ERROR("LoadAppConfigurationForMemoryThreshold return error"); 152 ret = -1; 153 } 154 155 return ret; 156 } 157 LoadSystemConfiguration(nlohmann::json & Object)158int AmsConfigurationParameter::LoadSystemConfiguration(nlohmann::json& Object) 159 { 160 if (Object.contains(AmsConfig::SYSTEM_CONFIGURATION)) { 161 orientation_ = Object.at(AmsConfig::SYSTEM_CONFIGURATION).at(AmsConfig::SYSTEM_ORIENTATION).get<std::string>(); 162 return READ_OK; 163 } 164 165 return READ_FAIL; 166 } 167 } // namespace AAFwk 168 } // namespace OHOS 169