-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
WifiGenerator.java
174 lines (153 loc) · 5.09 KB
/
WifiGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright (C) 2010 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.web.generator.client;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
/**
* A Generator for Wifi networks.
*
* @author Vikram Aggarwal
* @author Sean Owen
*/
public final class WifiGenerator implements GeneratorSource {
private Grid table;
private final TextBox ssid = new TextBox();
private final TextBox password = new TextBox();
private final ListBox networkType = new ListBox();
private final CheckBox hidden = new CheckBox();
public WifiGenerator(ChangeHandler handler, KeyPressHandler keyListener) {
networkType.addItem("WEP", "WEP");
networkType.addItem("WPA/WPA2", "WPA");
networkType.addItem("No encryption", "nopass");
TextBox[] widgets = {ssid, password};
for (TextBox w: widgets) {
w.addChangeHandler(handler);
w.addKeyPressHandler(keyListener);
}
}
@Override
public String getName() {
return "Wifi network";
}
@Override
public String getText() throws GeneratorException {
String ssid = getSsidField();
String password = getPasswordField();
String networkType = getNetworkTypeField();
boolean hidden = getHiddenField();
// Build the output with obtained data.
return getWifiString(ssid, password, networkType, hidden);
}
private static String getWifiString(String ssid, String password, String type, boolean hidden) {
StringBuilder output = new StringBuilder(100);
output.append("WIFI:");
output.append("S:").append(ssid).append(';');
if (type != null && !type.isEmpty() && !"nopass".equals(type)) {
maybeAppend(output, "T:", type);
}
maybeAppend(output, "P:", password);
if (hidden) {
maybeAppend(output, "H:", "true");
}
output.append(';');
return output.toString();
}
private static void maybeAppend(StringBuilder output, String prefix, String value) {
if (value != null && !value.isEmpty()) {
output.append(prefix).append(value).append(';');
}
}
private static String parseTextField(String name, HasText textBox) throws GeneratorException {
String input = textBox.getText();
if (input.isEmpty()) {
return "";
}
if (input.contains("\n")) {
throw new GeneratorException(name + " field must not contain \\n characters.");
}
return input.replaceAll("([\\\\:;])", "\\\\$1");
}
private String getSsidField() throws GeneratorException {
String input = ssid.getText();
if (input.isEmpty()) {
throw new GeneratorException("SSID must be at least 1 character.");
}
String parsed = parseTextField("SSID", ssid);
return quoteHex(parsed); // Android needs hex-like SSIDs quoted or will be read as hex
}
private String getPasswordField() throws GeneratorException {
return parseTextField("Password", password);
}
private String getNetworkTypeField() {
return networkType.getValue(networkType.getSelectedIndex());
}
private boolean getHiddenField() {
Boolean value = hidden.getValue();
return value != null && value;
}
@Override
public Grid getWidget() {
if (table != null) {
// early termination if the table has already been constructed
return table;
}
table = new Grid(4, 2);
table.setText(0, 0, "SSID");
table.setWidget(0, 1, ssid);
table.setText(1, 0, "Password");
table.setWidget(1, 1, password);
table.setText(2, 0, "Network Type");
table.setWidget(2, 1, networkType);
table.setText(3, 0, "Hidden?");
table.setWidget(3, 1, hidden);
ssid.addStyleName(StylesDefs.INPUT_FIELD_REQUIRED);
return table;
}
@Override
public void validate(Widget widget) throws GeneratorException {
if (widget == ssid) {
getSsidField();
}
if (widget == password) {
getPasswordField();
}
if (widget == networkType) {
getNetworkTypeField();
}
if (widget == hidden) {
getHiddenField();
}
}
@Override
public void setFocus() {
ssid.setFocus(true);
}
private static String quoteHex(String value) {
if (value != null && value.matches("[0-9A-Fa-f]+")) {
if (value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"') {
return value;
}
return '\"' + value + '\"';
}
return value;
}
}