1 /*
2 * Copyright (c) 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 "adapter/preview/osal/stage_card_parser.h"
17
18 namespace OHOS::Ace {
19
StageCardParser()20 StageCardParser::StageCardParser() : manifestWindow_(Referenced::MakeRefPtr<Framework::ManifestWindow>())
21 {}
22
Parse(const std::string & contents,const std::string & selectUrl)23 void StageCardParser::Parse(const std::string& contents, const std::string& selectUrl)
24 {
25 auto rootJson = JsonUtil::ParseJsonString(contents);
26 if (!rootJson || !rootJson->IsValid()) {
27 LOGE("the form config is illegal");
28 return;
29 }
30 std::unique_ptr<JsonValue> formConfig;
31 auto formConfigs = rootJson->GetValue("forms");
32 int32_t index = 0;
33 if (formConfigs && formConfigs->IsArray()) {
34 for (; index < formConfigs->GetArraySize(); ++index) {
35 formConfig = formConfigs->GetArrayItem(index);
36 if (formConfig && formConfig->Contains("src") && formConfig->GetString("src") == selectUrl) {
37 break;
38 }
39 }
40 }
41 if (formConfigs && index == formConfigs->GetArraySize()) {
42 LOGE("The configuration information for the url %{public}s does not exist", selectUrl.c_str());
43 return;
44 }
45
46 auto supportDimensions = formConfig->GetValue("supportDimensions");
47 if (supportDimensions && supportDimensions->IsArray()) {
48 for (index = 0; index < supportDimensions->GetArraySize(); ++index) {
49 auto supportDimension = supportDimensions->GetArrayItem(index);
50 if (supportDimension && supportDimension->IsString()) {
51 supportDimensions_.push_back(supportDimension->GetString());
52 }
53 }
54 }
55
56 colorMode_ = formConfig->GetString("colorMode", "auto");
57 defaultDimension_ = formConfig->GetString("defaultDimension");
58 description_ = formConfig->GetString("description");
59 formConfigAbility_ = formConfig->GetString("formConfigAbility");
60 isDefault_ = formConfig->GetBool("updateEnabled", true);
61 name_ = formConfig->GetString("name");
62 scheduledUpdateTime_ = formConfig->GetString("scheduledUpdateTime");
63 src_ = formConfig->GetString("src");
64 updateDuration_ = formConfig->GetUInt("updateDuration", 1);
65 updateEnabled_ = formConfig->GetBool("updateEnabled", true);
66 manifestWindow_->WindowParse(formConfig);
67 }
68
GetColorMode() const69 const std::string& StageCardParser::GetColorMode() const
70 {
71 return colorMode_;
72 }
73
GetDefaultDimension() const74 const std::string& StageCardParser::GetDefaultDimension() const
75 {
76 return defaultDimension_;
77 }
78
GetDescription() const79 const std::string& StageCardParser::GetDescription() const
80 {
81 return description_;
82 }
83
GetFormConfigAbility() const84 const std::string& StageCardParser::GetFormConfigAbility() const
85 {
86 return formConfigAbility_;
87 }
88
GetIsDefault() const89 bool StageCardParser::GetIsDefault() const
90 {
91 return isDefault_;
92 }
93
GetName() const94 const std::string& StageCardParser::GetName() const
95 {
96 return name_;
97 }
98
GetScheduledUpdateTime() const99 const std::string& StageCardParser::GetScheduledUpdateTime() const
100 {
101 return scheduledUpdateTime_;
102 }
103
GetSrc() const104 const std::string& StageCardParser::GetSrc() const
105 {
106 return src_;
107 }
108
GetSupportDimensions() const109 const std::vector<std::string>& StageCardParser::GetSupportDimensions() const
110 {
111 return supportDimensions_;
112 }
113
GetUpdateDuration() const114 uint32_t StageCardParser::GetUpdateDuration() const
115 {
116 return updateDuration_;
117 }
118
GetUpdateEnabled() const119 bool StageCardParser::GetUpdateEnabled() const
120 {
121 return updateEnabled_;
122 }
123
GetWindowConfig() const124 WindowConfig& StageCardParser::GetWindowConfig() const
125 {
126 return manifestWindow_->GetWindowConfig();
127 }
128
129 } // namespace OHOS::Ace
130