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 #include "clone_update_strategy.h"
16 #include <fstream>
17 #include "securec.h"
18 #include "intell_voice_log.h"
19 #include "scope_guard.h"
20 #include "update_engine_utils.h"
21 #include "history_info_mgr.h"
22 #include "json/json.h"
23
24 #define LOG_TAG "CloneUpdateStrategy"
25
26 using namespace OHOS::IntellVoiceUtils;
27
28 namespace OHOS {
29 namespace IntellVoiceEngine {
CloneUpdateStrategy(const std::string & param,sptr<IIntelligentVoiceUpdateCallback> updateCallback)30 CloneUpdateStrategy::CloneUpdateStrategy(const std::string ¶m,
31 sptr<IIntelligentVoiceUpdateCallback> updateCallback): IUpdateStrategy(param), updateCallback_(updateCallback)
32 {
33 }
34
~CloneUpdateStrategy()35 CloneUpdateStrategy::~CloneUpdateStrategy()
36 {
37 }
38
UpdateRestrain()39 bool CloneUpdateStrategy::UpdateRestrain()
40 {
41 std::string versionNumberSave;
42 HistoryInfoMgr &historyInfoMgr = HistoryInfoMgr::GetInstance();
43
44 versionNumberSave = historyInfoMgr.GetWakeupVesion();
45 if (!versionNumberSave.empty()) {
46 INTELL_VOICE_LOG_ERROR("saved version number is not null");
47 return true;
48 }
49 return false;
50 }
51
GetUpdatePriority()52 UpdatePriority CloneUpdateStrategy::GetUpdatePriority()
53 {
54 return CLONE_UPDATE_PRIORITY;
55 }
56
GetRetryTimes()57 int CloneUpdateStrategy::GetRetryTimes()
58 {
59 return 0;
60 }
61
GetBundleOrAbilityName(const std::string & key)62 std::string CloneUpdateStrategy::GetBundleOrAbilityName(const std::string &key)
63 {
64 std::istringstream jsonStrm(param_);
65 Json::CharReaderBuilder reader;
66 Json::Value root;
67 std::string errs;
68
69 reader["collectComments"] = false;
70 if (!parseFromStream(reader, jsonStrm, &root, &errs)) {
71 INTELL_VOICE_LOG_ERROR("input str is not json");
72 return "";
73 }
74
75 if ((!root.isMember(key)) || (!root[key].isString())) {
76 INTELL_VOICE_LOG_ERROR("invalid key");
77 return "";
78 }
79
80 return root[key].asString();
81 }
82
SetBundleAndAbilityName()83 void CloneUpdateStrategy::SetBundleAndAbilityName()
84 {
85 HistoryInfoMgr &historyInfoMgr = HistoryInfoMgr::GetInstance();
86
87 std::string bundleName = GetBundleOrAbilityName("bundle_name");
88 if (!bundleName.empty()) {
89 INTELL_VOICE_LOG_INFO("set bundle");
90 historyInfoMgr.SetWakeupEngineBundleName(bundleName);
91 }
92
93 std::string abilityName = GetBundleOrAbilityName("ability_name");
94 if (!abilityName.empty()) {
95 INTELL_VOICE_LOG_INFO("set ability");
96 historyInfoMgr.SetWakeupEngineAbilityName(abilityName);
97 }
98 }
99
OnUpdateCompleteCallback(const int result,bool isLast)100 int CloneUpdateStrategy::OnUpdateCompleteCallback(const int result, bool isLast)
101 {
102 if (updateCallback_ != nullptr) {
103 INTELL_VOICE_LOG_INFO("enter");
104 if (result == UPDATE_STATE_COMPLETE_SUCCESS) {
105 SetBundleAndAbilityName();
106 }
107 updateCallback_->OnUpdateComplete(result);
108 }
109
110 return 0;
111 }
112 }
113 }
114