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