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 "base_page.h"
17 #include "component/component_factory.h"
18 #include "dock/focus_manager.h"
19 #include "log/log.h"
20
21 namespace Updater {
22 using namespace OHOS;
BasePage()23 BasePage::BasePage()
24 : width_ {}, height_ {}, root_ {std::make_unique<OHOS::UIViewGroup>()},
25 coms_ {}, comsMap_ {}, pageId_ {}, color_ {Color::Black()}
26 {
27 }
28
BasePage(int16_t width,int16_t height)29 BasePage::BasePage(int16_t width, int16_t height)
30 : width_ {width}, height_ {height}, root_ {std::make_unique<OHOS::UIViewGroup>()},
31 coms_ {}, comsMap_ {}, pageId_ {}, color_ {Color::Black()}
32 {
33 }
34
IsPageInfoValid(const UxPageInfo & pageInfo)35 bool BasePage::IsPageInfoValid(const UxPageInfo &pageInfo)
36 {
37 if (pageInfo.id.empty()) {
38 LOG(ERROR) << "page id is empty";
39 return false;
40 }
41 if (!CheckColor(pageInfo.bgColor)) {
42 LOG(ERROR) << "page color not valid, bgcolor: " << pageInfo.bgColor;
43 return false;
44 }
45 return true;
46 }
47
Reset()48 void BasePage::Reset()
49 {
50 root_->RemoveAll();
51 coms_.clear();
52 comsMap_.clear();
53 focusedView_ = nullptr;
54 }
55
BuildPage(const UxPageInfo & pageInfo)56 bool BasePage::BuildPage(const UxPageInfo &pageInfo)
57 {
58 Reset();
59 pageId_ = pageInfo.id;
60 color_ = StrToColor(pageInfo.bgColor);
61 root_->SetViewId(pageId_.c_str());
62 root_->SetPosition(0, 0, width_, height_);
63 root_->SetVisible(true);
64 root_->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, color_.full);
65 root_->SetStyle(OHOS::STYLE_BACKGROUND_OPA, color_.alpha);
66 return BuildComs(pageInfo);
67 }
68
BuildComs(const UxPageInfo & pageInfo)69 bool BasePage::BuildComs(const UxPageInfo &pageInfo)
70 {
71 const auto &infos = pageInfo.viewInfos;
72 coms_.reserve(infos.size());
73 int minY = INT_MAX;
74 for (auto &info : infos) {
75 if (!BuildCom(info, minY)) {
76 return false;
77 }
78 }
79 return true;
80 }
81
BuildCom(const UxViewInfo & viewInfo,int & minY)82 bool BasePage::BuildCom(const UxViewInfo &viewInfo, int &minY)
83 {
84 if (!ComponentFactory::CheckUxComponent(viewInfo)) {
85 LOG(ERROR) << "component (" << viewInfo.commonInfo.id
86 << ") is invalid, please check your page config json";
87 return false;
88 }
89 auto upView = std::make_unique<ViewProxy>(ComponentFactory::CreateUxComponent(viewInfo),
90 std::string("component id:") + viewInfo.commonInfo.id + ", ");
91 auto &view = *upView;
92 if (view->GetViewId() == nullptr) {
93 LOG(ERROR) << "id is nullptr";
94 return false;
95 }
96 if (view->IsFocusable() && viewInfo.commonInfo.y < minY) {
97 minY = viewInfo.commonInfo.y;
98 focusedView_ = view.operator->();
99 }
100 coms_.push_back(std::move(upView));
101 root_->Add(view.operator->());
102 // empty id is allowed. id is needed only when get specific view by id.
103 if (std::string(view->GetViewId()).empty()) {
104 return true; // skip this view. build com success, but not save in map
105 }
106 // only map non-empty id
107 if (!comsMap_.emplace(view->GetViewId(), coms_.back().get()).second) {
108 LOG(ERROR) << "view id duplicated:" << view->GetViewId();
109 }
110 return true;
111 }
112
operator [](const std::string & id)113 ViewProxy &BasePage::operator[](const std::string &id)
114 {
115 static ViewProxy dummy;
116 auto it = comsMap_.find(id);
117 if (it != comsMap_.end() && it->second != nullptr) {
118 return *it->second;
119 }
120 return dummy;
121 }
122
IsValidCom(const std::string & id) const123 bool BasePage::IsValidCom(const std::string &id) const
124 {
125 return comsMap_.find(id) != comsMap_.end();
126 }
127
IsValid() const128 bool BasePage::IsValid() const
129 {
130 return root_ != nullptr;
131 }
132
GetPageId()133 std::string BasePage::GetPageId()
134 {
135 return pageId_;
136 }
137
SetVisible(bool isVisible)138 void BasePage::SetVisible(bool isVisible)
139 {
140 if (root_ == nullptr) {
141 LOG(ERROR) << "root is null";
142 return;
143 }
144 root_->SetVisible(isVisible);
145 if (isVisible) {
146 // change background
147 root_->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, color_.full);
148 root_->SetStyle(OHOS::STYLE_BACKGROUND_OPA, color_.alpha);
149 }
150 UpdateFocus(isVisible);
151 }
152
IsVisible() const153 bool BasePage::IsVisible() const
154 {
155 if (root_ == nullptr) {
156 LOG(ERROR) << "root is null";
157 return false;
158 }
159 return root_->IsVisible();
160 }
161
GetView() const162 OHOS::UIViewGroup *BasePage::GetView() const
163 {
164 return root_.get();
165 }
166 } // namespace Updater
167