1 /*
2 * Copyright (c) 2024-2025 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 <file_ex.h>
17
18 #include "system_upgrade_scene_recognizer.h"
19 #include "ffrt_inner.h"
20 #include "parameters.h"
21 #include "plugin_mgr.h"
22 #include "res_sched_log.h"
23 #include "res_sched_mgr.h"
24 #include "res_type.h"
25
26
27 namespace OHOS {
28 namespace ResourceSchedule {
29 namespace {
30 static const char* OLD_SYSTEM_FINGERPRINT_PATH = "/data/service/el1/public/ressched/device_version";
31 static const char* SEPARATOR = "|";
32 static const std::vector<std::string> FINGERPRINTS = {
33 "const.product.software.version",
34 "const.product.build.type",
35 "const.product.brand",
36 "const.product.name",
37 "const.product.incremental.version",
38 "const.comp.hl.product_base_version.real"
39 };
40 }
41
~SystemUpgradeSceneRecognizer()42 SystemUpgradeSceneRecognizer::~SystemUpgradeSceneRecognizer()
43 {
44 RESSCHED_LOGI("~UpdatingSceneRecognizer");
45 }
46
SystemUpgradeSceneRecognizer()47 SystemUpgradeSceneRecognizer::SystemUpgradeSceneRecognizer()
48 {
49 Init();
50 }
51
Init()52 void SystemUpgradeSceneRecognizer::Init()
53 {
54 std::string oldSystemFingerprint;
55 OHOS::LoadStringFromFile(OLD_SYSTEM_FINGERPRINT_PATH, oldSystemFingerprint);
56 std::string curSystemFingerprint = GetCurSystemFingerprint();
57 isSystemUpgraded_ = oldSystemFingerprint.empty() || (oldSystemFingerprint != curSystemFingerprint);
58 if (isSystemUpgraded_) {
59 OHOS::SaveStringToFile(OLD_SYSTEM_FINGERPRINT_PATH, curSystemFingerprint, true);
60 }
61 }
62
GetCurSystemFingerprint()63 std::string SystemUpgradeSceneRecognizer::GetCurSystemFingerprint()
64 {
65 std::string curSystemFingerprint;
66 for (const auto &item : FINGERPRINTS) {
67 std::string itemFingerprint = OHOS::system::GetParameter(item, "");
68 if (itemFingerprint.empty()) {
69 continue;
70 }
71 if (!curSystemFingerprint.empty()) {
72 curSystemFingerprint.append(SEPARATOR);
73 }
74 curSystemFingerprint.append(itemFingerprint);
75 }
76 return curSystemFingerprint;
77 }
78
OnDispatchResource(uint32_t resType,int64_t value,const nlohmann::json & payload)79 void SystemUpgradeSceneRecognizer::OnDispatchResource(uint32_t resType, int64_t value, const nlohmann::json& payload)
80 {
81 if (resType != ResType::RES_TYPE_BOOT_COMPLETED) {
82 return;
83 }
84 if (isSystemUpgraded_) {
85 RESSCHED_LOGI("enter updating scene");
86 nlohmann::json payload;
87 ResSchedMgr::GetInstance().ReportData(ResType::RES_TYPE_FIRST_BOOT_AFTER_SYSTEM_UPGRADE, 0, payload);
88 }
89 }
90 } // namespace ResourceSchedule
91 } // namespace OHOS