1/*
2 * Copyright (c) 2023 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 "firmware_changelog_utils.h"
17
18#include "firmware_component_operator.h"
19#include "firmware_constant.h"
20#include "firmware_log.h"
21#include "firmware_preferences_utils.h"
22
23namespace OHOS {
24namespace UpdateEngine {
25FirmwareChangelogUtils::FirmwareChangelogUtils() {}
26
27FirmwareChangelogUtils::~FirmwareChangelogUtils() {}
28std::string FirmwareChangelogUtils::GetChangelogDir()
29{
30    return Firmware::CHANGELOG_PATH;
31}
32
33    std::vector<std::string> FirmwareChangelogUtils::GetNewVersionComponentIds()
34{
35    std::vector<std::string> componentIds;
36    std::vector<FirmwareComponent> dbComponentList;
37    FirmwareComponentOperator().QueryAll(dbComponentList);
38    for (FirmwareComponent &component : dbComponentList) {
39        if (std::find(componentIds.begin(), componentIds.end(), component.componentId) == componentIds.end()) {
40            FIRMWARE_LOGI("new version componentId %{public}s", component.componentId.c_str());
41            componentIds.push_back(component.componentId);
42        }
43    }
44
45    return componentIds;
46}
47
48std::vector<std::string> FirmwareChangelogUtils::GetCurrentVersionComponentIds()
49{
50    std::vector<std::string> componentIds;
51    std::shared_ptr<FirmwarePreferencesUtil> preferencesUtil = DelayedSingleton<FirmwarePreferencesUtil>::GetInstance();
52    if (preferencesUtil->IsExist(Firmware::HOTA_CURRENT_COMPONENT_ID)) {
53        componentIds.push_back(preferencesUtil->ObtainString(Firmware::HOTA_CURRENT_COMPONENT_ID, ""));
54    }
55
56    return componentIds;
57}
58
59void FirmwareChangelogUtils::SaveHotaCurrentVersionComponentId()
60{
61    std::shared_ptr<FirmwarePreferencesUtil> preferencesUtil = DelayedSingleton<FirmwarePreferencesUtil>::GetInstance();
62    std::vector<FirmwareComponent> components;
63    FirmwareComponentOperator().QueryAll(components);
64    for (auto const &component : components) {
65        if (component.versionPackageType == PackageType::NORMAL) {
66            preferencesUtil->SaveString(Firmware::HOTA_CURRENT_COMPONENT_ID, component.componentId);
67            return;
68        }
69    }
70}
71
72void FirmwareChangelogUtils::GetAllComponentIds(std::vector<std::string> &componentIds)
73{
74    std::vector<std::string> curComponentIds = GetCurrentVersionComponentIds();
75    std::vector<std::string> newComponentIds = GetNewVersionComponentIds();
76
77    componentIds.insert(componentIds.end(), curComponentIds.begin(), curComponentIds.end());
78    componentIds.insert(componentIds.end(), newComponentIds.begin(), newComponentIds.end());
79}
80} // namespace UpdateEngine
81} // namespace OHOS
82