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 GetAnrHandleType() const69int AmsConfigurationParameter::GetAnrHandleType() const 70 { 71 return anrHandleType_; 72 } 73 GetBootAnimationTimeoutTime() const74int AmsConfigurationParameter::GetBootAnimationTimeoutTime() const 75 { 76 return bootAnimationTime_; 77 } 78 LoadAmsConfiguration(const std::string & filePath)79int AmsConfigurationParameter::LoadAmsConfiguration(const std::string &filePath) 80 { 81 HILOG_DEBUG("%{public}s", __func__); 82 int ret[2] = {0}; 83 std::ifstream inFile; 84 inFile.open(filePath, std::ios::in); 85 if (!inFile.is_open()) { 86 HILOG_INFO("read ams config error ..."); 87 nonConfigFile_ = true; 88 return READ_FAIL; 89 } 90 91 json amsJson; 92 inFile >> amsJson; 93 if (amsJson.is_discarded()) { 94 HILOG_INFO("json discarded error ..."); 95 nonConfigFile_ = true; 96 inFile.close(); 97 return READ_JSON_FAIL; 98 } 99 100 ret[0] = LoadAppConfigurationForStartUpService(amsJson); 101 if (ret[0] != 0) { 102 HILOG_ERROR("LoadAppConfigurationForStartUpService return error"); 103 } 104 105 ret[1] = LoadAppConfigurationForMemoryThreshold(amsJson); 106 if (ret[1] != 0) { 107 HILOG_ERROR("LoadAppConfigurationForMemoryThreshold return error"); 108 } 109 110 LoadSystemConfiguration(amsJson); 111 amsJson.clear(); 112 inFile.close(); 113 114 for (const auto& i : ret) { 115 if (i != 0) { 116 HILOG_ERROR("json no have service item ..."); 117 return READ_JSON_FAIL; 118 } 119 } 120 121 HILOG_INFO("read ams config success!"); 122 return READ_OK; 123 } 124 LoadAppConfigurationForStartUpService(nlohmann::json & Object)125int AmsConfigurationParameter::LoadAppConfigurationForStartUpService(nlohmann::json& Object) 126 { 127 int ret = -1; 128 if (Object.contains(AmsConfig::SERVICE_ITEM_AMS)) { 129 missionSaveTime_ = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::MISSION_SAVE_TIME).get<int>(); 130 anrTime_ = 131 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::APP_NOT_RESPONSE_PROCESS_TIMEOUT_TIME).get<int>(); 132 amsTime_ = 133 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::AMS_TIMEOUT_TIME).get<int>(); 134 maxRootLauncherRestartNum_ = 135 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::ROOT_LAUNCHER_RESTART_MAX).get<int>(); 136 if (Object.at(AmsConfig::SERVICE_ITEM_AMS).contains(AmsConfig::RESIDENT_RESTART_MAX)) { 137 maxResidentRestartNum_ = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::RESIDENT_RESTART_MAX).get<int>(); 138 } 139 if (Object.at(AmsConfig::SERVICE_ITEM_AMS).contains(AmsConfig::RESTART_INTERVAL_TIME)) { 140 restartIntervalTime_ = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::RESTART_INTERVAL_TIME).get<int>(); 141 } 142 deviceType_ = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::DEVICE_TYPE).get<std::string>(); 143 bootAnimationTime_ = 144 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::BOOT_ANIMATION_TIMEOUT_TIME).get<int>(); 145 anrHandleType_ = 146 Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::APP_NOT_RESPONSE_HANDLE_TYPE).get<int>(); 147 HILOG_INFO("get ams service config success!"); 148 ret = 0; 149 } 150 151 return ret; 152 } 153 LoadAppConfigurationForMemoryThreshold(nlohmann::json & Object)154int AmsConfigurationParameter::LoadAppConfigurationForMemoryThreshold(nlohmann::json &Object) 155 { 156 int ret = 0; 157 if (!Object.contains("memorythreshold")) { 158 HILOG_ERROR("LoadAppConfigurationForMemoryThreshold return error"); 159 ret = -1; 160 } 161 162 return ret; 163 } 164 LoadSystemConfiguration(nlohmann::json & Object)165int AmsConfigurationParameter::LoadSystemConfiguration(nlohmann::json& Object) 166 { 167 if (Object.contains(AmsConfig::SYSTEM_CONFIGURATION)) { 168 orientation_ = Object.at(AmsConfig::SYSTEM_CONFIGURATION).at(AmsConfig::SYSTEM_ORIENTATION).get<std::string>(); 169 return READ_OK; 170 } 171 172 return READ_FAIL; 173 } 174 } // namespace AAFwk 175 } // namespace OHOS 176