1 /*
2 * Copyright (c) 2020 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 <setting_wifi_input_password_ability_slice.h>
17 #include "gfx_utils/style.h"
18
19 namespace OHOS {
20 REGISTER_AS(SettingWifiInputPasswordAbilitySlice)
21
22 static UIView::OnClickListener* clickLeftListener_ = nullptr;
23
24 static const int g_maxPassword = 10; // Maximum length of a password.
25 static char* g_inputSsid = nullptr;
26 static char g_inputPassword[g_maxPassword + 1] = { 0 };
27 static int g_inputCount = 0;
28 static int g_cursorPositionX = 20; // Initial position of cursor X
29
~SettingWifiInputPasswordAbilitySlice()30 SettingWifiInputPasswordAbilitySlice::~SettingWifiInputPasswordAbilitySlice()
31 {
32 if (scrollView_) {
33 DeleteChildren(scrollView_);
34 scrollView_ = nullptr;
35 }
36 if (inputView_) {
37 DeleteChildren(inputView_);
38 inputView_ = nullptr;
39 }
40 if (headView_) {
41 DeleteChildren(headView_);
42 headView_ = nullptr;
43 };
44 if (buttonBackListener_) {
45 delete buttonBackListener_;
46 buttonBackListener_ = nullptr;
47 }
48 if (clickLeftListener_) {
49 delete clickLeftListener_;
50 clickLeftListener_ = nullptr;
51 }
52 }
53
54 class TestBtnOnClickInputPasswordChangeListener : public OHOS::UIView::OnClickListener {
55 public:
~TestBtnOnClickInputPasswordChangeListener()56 ~TestBtnOnClickInputPasswordChangeListener() {}
TestBtnOnClickInputPasswordChangeListener(UILabel * uiLabel,UILabel * uiCursor,const int ii,const int cursorOffset)57 TestBtnOnClickInputPasswordChangeListener(UILabel* uiLabel, UILabel* uiCursor, const int ii,
58 const int cursorOffset) : myUiLabel(uiLabel), myUiCursor(uiCursor), mIi(ii), myCursorOffset(cursorOffset) {}
OnClick(UIView & view,const ClickEvent & event)59 bool OnClick(UIView& view, const ClickEvent& event) override
60 {
61 if (g_inputCount >= g_maxPassword) {
62 return true;
63 }
64 g_inputPassword[g_inputCount] = '0' + mIi;
65 myUiLabel->SetText(g_inputPassword);
66 g_inputCount++;
67 g_inputPassword[g_inputCount] = '\0';
68 g_cursorPositionX += myCursorOffset;
69 myUiCursor->SetX(g_cursorPositionX);
70 return true;
71 }
72
73 private:
74 UILabel* myUiLabel;
75 UILabel* myUiCursor;
76 int mIi;
77 int myCursorOffset;
78 };
79
80 class TestBtnOnClickEnterChangeListener : public OHOS::UIView::OnClickListener {
81 public:
TestBtnOnClickEnterChangeListener(SettingWifiInputPasswordAbilitySlice * slice)82 explicit TestBtnOnClickEnterChangeListener(SettingWifiInputPasswordAbilitySlice* slice) : mySlice(slice) {}
~TestBtnOnClickEnterChangeListener()83 ~TestBtnOnClickEnterChangeListener() {}
OnClick(UIView & view,const ClickEvent & event)84 bool OnClick(UIView& view, const ClickEvent& event) override
85 {
86 WpaScanReconnect(g_inputSsid, g_inputPassword, HIDDEN_OPEN);
87 g_inputSsid = nullptr;
88 int err = memset_s(g_inputPassword, sizeof(g_inputPassword), 0, sizeof(g_inputPassword));
89 if (err != EOK) {
90 printf("[ERROR]memset_s failed, err = %d\n", err);
91 return false;
92 }
93 mySlice->Terminate();
94 return true;
95 }
96 SettingWifiInputPasswordAbilitySlice* mySlice;
97 };
98
SetButtonListener(void)99 void SettingWifiInputPasswordAbilitySlice::SetButtonListener(void)
100 {
101 auto onClick = [this](UIView& view, const Event& event) -> bool {
102 Terminate();
103 return true;
104 };
105 buttonBackListener_ = new EventListener(onClick, nullptr);
106 }
107
SetHead(void)108 void SettingWifiInputPasswordAbilitySlice::SetHead(void)
109 {
110 headView_ = new UIViewGroup();
111 rootView_->Add(headView_);
112 headView_->SetPosition(DE_HEAD_X, DE_HEAD_Y, DE_HEAD_WIDTH, DE_HEAD_HEIGHT);
113 headView_->SetStyle(STYLE_BACKGROUND_OPA, 0);
114 headView_->SetTouchable(true);
115 headView_->SetOnClickListener(buttonBackListener_);
116
117 UIImageView* imageView = new UIImageView();
118 headView_->Add(imageView);
119 imageView->SetPosition(DE_HEAD_IMAGE_X, DE_HEAD_IMAGE_Y, DE_HEAD_IMAGE_WIDTH, DE_HEAD_IMAGE_HEIGHT);
120 imageView->SetSrc(DE_IMAGE_BACK);
121
122 UILabel* lablelFont = new UILabel();
123 lablelFont->SetPosition(DE_HEAD_TEXT_X, DE_HEAD_TEXT_Y, DE_HEAD_TEXT_WIDTH, DE_HEAD_TEXT_HEIGHT);
124 lablelFont->SetText(g_inputSsid);
125 lablelFont->SetFont(DE_FONT_OTF, DE_HEAD_TEXT_SIZE);
126 lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_HEAD_TEXT_COLOR);
127 headView_->Add(lablelFont);
128 }
129
SetInput(void)130 void SettingWifiInputPasswordAbilitySlice::SetInput(void)
131 {
132 inputView_ = new UIViewGroup();
133 inputView_->SetPosition(INPUT_X, INPUT_Y, INPUT_WIDTH, INPUT_HEIGHT);
134
135 inputView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR);
136 inputView_->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL);
137 inputView_->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS);
138 rootView_->Add(inputView_);
139
140 lablelInputText_ = new UILabel();
141 lablelInputText_->SetPosition(INPUT_TEXT_X, INPUT_TEXT_Y, INPUT_TEXT_WIDTH, INPUT_TEXT_HEIGHT);
142 lablelInputText_->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR);
143 lablelInputText_->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL);
144 lablelInputText_->SetText("输入密码");
145 lablelInputText_->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE);
146 inputView_->Add(lablelInputText_);
147
148 lablelCursorText_ = new UILabel();
149 lablelCursorText_->SetPosition(g_cursorPositionX, INPUT_CURSOR_Y, INPUT_CURSOR_WIDTH, INPUT_CURSOR_HEIGHT);
150 lablelCursorText_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::GetColorFromRGB(0x0D, 0x9F, 0xF8)));
151 lablelCursorText_->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL);
152 inputView_->Add(lablelCursorText_);
153
154 UIViewGroup* enterView = new UIViewGroup();
155 enterView->SetPosition(INPUT_ENTER_X, INPUT_ENTER_Y, INPUT_ENTER_WIDTH, INPUT_ENTER_HEIGHT);
156 enterView->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::GetColorFromRGB(0x0D, 0x9F, 0xF8)));
157 enterView->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL);
158 enterView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS);
159 inputView_->Add(enterView);
160
161 UIImageView* imageView = new UIImageView();
162 imageView->SetPosition(INPUT_IMAGE_X, INPUT_IMAGE_Y, INPUT_IMAGE_WIDTH, INPUT_IMAGE_HEIGHT);
163 imageView->SetSrc(DE_IMAGE_ENTER);
164 enterView->Add(imageView);
165 imageView->SetTouchable(true);
166 clickLeftListener_ = new TestBtnOnClickEnterChangeListener(this);
167 imageView->SetOnClickListener(clickLeftListener_);
168 }
169
AddInputKeyBoardZero(void)170 void SettingWifiInputPasswordAbilitySlice::AddInputKeyBoardZero(void)
171 {
172 char buf[8] = {0};
173 int myUseX = BUTTON_INTERVAL_X;
174 int myUseY = 198;
175 int inputNum = 0;
176 UILabelButton* inputButton = new UILabelButton();
177
178 inputButton->SetPosition(myUseX, myUseY);
179 sprintf_s(buf, sizeof(buf), "%d", inputNum);
180 inputButton->SetWidth(BUTTON_WIDTH);
181 inputButton->SetHeight(BUTTON_HEIGHT);
182 inputButton->SetText(buf);
183 inputButton->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR);
184 inputButton->SetStyle(STYLE_BORDER_RADIUS, RECT_RADIUS);
185 inputButton->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR);
186 inputButton->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE);
187
188 clickLeftListener_ = new TestBtnOnClickInputPasswordChangeListener((UILabel*)lablelInputText_, (UILabel*)lablelCursorText_, inputNum, CURSOR_POSITION_OFFSET);
189 inputButton->SetOnClickListener(clickLeftListener_);
190 scrollView_->Add(inputButton);
191 }
192
SetScrollView(void)193 void SettingWifiInputPasswordAbilitySlice::SetScrollView(void)
194 {
195 char buf[8] = {0};
196 int inputNum;
197 scrollView_ = new UIScrollView();
198 scrollView_->SetPosition(SCROLL_WIFI_INPUT_X, SCROLL_WIFI_INPUT_Y, SCROLL_WIFI_INPUT_WIDTH,
199 SCROLL_WIFI_INPUT_WIDTH);
200 scrollView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_SCROLL_COLOR);
201 scrollView_->SetXScrollBarVisible(false);
202 scrollView_->SetYScrollBarVisible(false);
203 rootView_->Add(scrollView_);
204 for (int countFirst = 0; countFirst < BUTTON_NUM; countFirst++) {
205 for (int countSecound = 0; countSecound < BUTTON_NUM; countSecound++) {
206 int myUseX = countSecound * BUTTON_INTERVAL_X;
207 int myUseY = countFirst * BUTTON_INTERVAL_Y;
208 inputNum = ((countFirst * BUTTON_NUM) + countSecound + 1);
209 UILabelButton* inputButton = new UILabelButton();
210 inputButton->SetPosition(myUseX, myUseY, BUTTON_WIDTH, BUTTON_HEIGHT);
211 int err = sprintf_s(buf, sizeof(buf), "%d", inputNum);
212 if (err < 0) {
213 printf("[ERROR]sprintf_s failed, err = %d\n", err);
214 return;
215 }
216 inputButton->SetText(buf); // SetText is system functions
217 inputButton->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR);
218 inputButton->SetStyle(STYLE_BORDER_RADIUS, RECT_RADIUS);
219 inputButton->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR);
220 inputButton->SetFont(DE_FONT_OTF, DE_CONTENT_FONT_SIZE);
221 UIView::OnClickListener* clickLeftListener = nullptr;
222 clickLeftListener = new TestBtnOnClickInputPasswordChangeListener((UILabel*)lablelInputText_,
223 (UILabel*)lablelCursorText_, inputNum, CURSOR_POSITION_OFFSET);
224 inputButton->SetOnClickListener(clickLeftListener);
225 scrollView_->Add(inputButton);
226 }
227 }
228 AddInputKeyBoardZero();
229 }
230
OnStart(const Want & want)231 void SettingWifiInputPasswordAbilitySlice::OnStart(const Want& want)
232 {
233 printf("[LOG]receive the data -> %s\n", reinterpret_cast<char*>(want.data));
234 AbilitySlice::OnStart(want);
235
236 g_inputSsid = reinterpret_cast<char*>(want.data);
237 rootView_ = RootView::GetWindowRootView();
238
239 int err = memset_s(g_inputPassword, sizeof(g_inputPassword), 0, g_maxPassword);
240 if (err != EOK) {
241 printf("[ERROR]memcpy_s failed, err = %d\n", err);
242 return;
243 }
244 g_inputCount = 0;
245 g_cursorPositionX = 20; // 20
246 rootView_->SetPosition(DE_ROOT_X, DE_ROOT_Y, DE_ROOT_WIDTH, DE_ROOT_HEIGHT);
247 rootView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_ROOT_BACKGROUND_COLOR);
248 SetButtonListener();
249 SetHead();
250 SetInput();
251 SetScrollView();
252 SetUIContent(rootView_);
253 }
254
OnInactive()255 void SettingWifiInputPasswordAbilitySlice::OnInactive()
256 {
257 AbilitySlice::OnInactive();
258 }
259
OnActive(const Want & want)260 void SettingWifiInputPasswordAbilitySlice::OnActive(const Want& want)
261 {
262 int err;
263 lablelInputText_->SetText("输入密码");
264 g_cursorPositionX = 20; // 20
265 lablelCursorText_->SetX(g_cursorPositionX);
266 g_inputCount = 0;
267 err = memset_s(g_inputPassword, sizeof(g_inputPassword), 0, sizeof(g_inputPassword));
268 if (err != EOK) {
269 return;
270 }
271 AbilitySlice::OnActive(want);
272 }
273
OnBackground()274 void SettingWifiInputPasswordAbilitySlice::OnBackground()
275 {
276 AbilitySlice::OnBackground();
277 }
278
OnStop()279 void SettingWifiInputPasswordAbilitySlice::OnStop()
280 {
281 AbilitySlice::OnStop();
282 }
283 } // namespace OHOS
284