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_, \
189 (UILabel*)lablelCursorText_, inputNum, CURSOR_POSITION_OFFSET);
190 inputButton->SetOnClickListener(clickLeftListener_);
191 scrollView_->Add(inputButton);
192 }
193
SetScrollView(void)194 void SettingWifiInputPasswordAbilitySlice::SetScrollView(void)
195 {
196 char buf[8] = {0};
197 int inputNum;
198 scrollView_ = new UIScrollView();
199 scrollView_->SetPosition(SCROLL_WIFI_INPUT_X, SCROLL_WIFI_INPUT_Y, SCROLL_WIFI_INPUT_WIDTH,
200 SCROLL_WIFI_INPUT_WIDTH);
201 scrollView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_SCROLL_COLOR);
202 scrollView_->SetXScrollBarVisible(false);
203 scrollView_->SetYScrollBarVisible(false);
204 rootView_->Add(scrollView_);
205 for (int countFirst = 0; countFirst < BUTTON_NUM; countFirst++) {
206 for (int countSecound = 0; countSecound < BUTTON_NUM; countSecound++) {
207 int myUseX = countSecound * BUTTON_INTERVAL_X;
208 int myUseY = countFirst * BUTTON_INTERVAL_Y;
209 inputNum = ((countFirst * BUTTON_NUM) + countSecound + 1);
210 UILabelButton* inputButton = new UILabelButton();
211 inputButton->SetPosition(myUseX, myUseY, BUTTON_WIDTH, BUTTON_HEIGHT);
212 int err = sprintf_s(buf, sizeof(buf), "%d", inputNum);
213 if (err < 0) {
214 printf("[ERROR]sprintf_s failed, err = %d\n", err);
215 return;
216 }
217 inputButton->SetText(buf); // SetText is system functions
218 inputButton->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR);
219 inputButton->SetStyle(STYLE_BORDER_RADIUS, RECT_RADIUS);
220 inputButton->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR);
221 inputButton->SetFont(DE_FONT_OTF, DE_CONTENT_FONT_SIZE);
222 UIView::OnClickListener* clickLeftListener = nullptr;
223 clickLeftListener = new TestBtnOnClickInputPasswordChangeListener((UILabel*)lablelInputText_,
224 (UILabel*)lablelCursorText_, inputNum, CURSOR_POSITION_OFFSET);
225 inputButton->SetOnClickListener(clickLeftListener);
226 scrollView_->Add(inputButton);
227 }
228 }
229 AddInputKeyBoardZero();
230 }
231
OnStart(const Want & want)232 void SettingWifiInputPasswordAbilitySlice::OnStart(const Want& want)
233 {
234 printf("[LOG]receive the data -> %s\n", reinterpret_cast<char*>(want.data));
235 AbilitySlice::OnStart(want);
236
237 g_inputSsid = reinterpret_cast<char*>(want.data);
238 rootView_ = RootView::GetWindowRootView();
239
240 int err = memset_s(g_inputPassword, sizeof(g_inputPassword), 0, g_maxPassword);
241 if (err != EOK) {
242 printf("[ERROR]memcpy_s failed, err = %d\n", err);
243 return;
244 }
245 g_inputCount = 0;
246 g_cursorPositionX = 20; // 20
247 rootView_->SetPosition(DE_ROOT_X, DE_ROOT_Y, DE_ROOT_WIDTH, DE_ROOT_HEIGHT);
248 rootView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_ROOT_BACKGROUND_COLOR);
249 SetButtonListener();
250 SetHead();
251 SetInput();
252 SetScrollView();
253 SetUIContent(rootView_);
254 }
255
OnInactive()256 void SettingWifiInputPasswordAbilitySlice::OnInactive()
257 {
258 AbilitySlice::OnInactive();
259 }
260
OnActive(const Want & want)261 void SettingWifiInputPasswordAbilitySlice::OnActive(const Want& want)
262 {
263 int err;
264 lablelInputText_->SetText("输入密码");
265 g_cursorPositionX = 20; // 20
266 lablelCursorText_->SetX(g_cursorPositionX);
267 g_inputCount = 0;
268 err = memset_s(g_inputPassword, sizeof(g_inputPassword), 0, sizeof(g_inputPassword));
269 if (err != EOK) {
270 return;
271 }
272 AbilitySlice::OnActive(want);
273 }
274
OnBackground()275 void SettingWifiInputPasswordAbilitySlice::OnBackground()
276 {
277 AbilitySlice::OnBackground();
278 }
279
OnStop()280 void SettingWifiInputPasswordAbilitySlice::OnStop()
281 {
282 AbilitySlice::OnStop();
283 }
284 } // namespace OHOS
285