Style bits on wi-fi code
[zxing.git] / android / src / com / google / zxing / client / android / wifi / NetworkUtil.java
1 /*
2  * Copyright (C) 2010 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.android.wifi;
18
19 import java.util.regex.Pattern;
20
21 import android.text.TextUtils;
22
23 /**
24  * Try with:
25  * http://chart.apis.google.com/chart?cht=qr&chs=240x240&chl=WIFI:S:linksys;P:mypass;T:WPA;;
26  *
27  * TODO(vikrama): Test with binary ssid or password.
28  * 
29  * @author Vikram Aggarwal
30  */
31 final class NetworkUtil {
32
33   private static final Pattern HEX_DIGITS = Pattern.compile("[0-9A-Fa-f]+");
34
35   private NetworkUtil() {
36   }
37
38   /**
39    * Encloses the incoming string inside double quotes, if it isn't already quoted.
40    * @param string: the input string
41    * @return a quoted string, of the form "input".  If the input string is null, it returns null as well.
42    */
43   static String convertToQuotedString(String string) {
44     if (string == null){
45       return null;
46     }
47     if (TextUtils.isEmpty(string)) {
48       return "";
49     }
50     int lastPos = string.length() - 1;
51     if (lastPos < 0 || (string.charAt(0) == '"' && string.charAt(lastPos) == '"')) {
52       return string;
53     }
54     return '\"' + string + '\"';
55   }
56
57   /**
58    * Check if wepKey is a valid hexadecimal string.
59    * @param wepKey the input to be checked
60    * @return true if the input string is indeed hex or empty.  False if the input string is non-hex or null.
61    */
62   static boolean isHexWepKey(CharSequence wepKey) {
63     if (wepKey == null) {
64       return false;
65     }
66     int length = wepKey.length();
67     // WEP-40, WEP-104, and some vendors using 256-bit WEP (WEP-232?)
68     return (length == 10 || length == 26 || length == 58) && HEX_DIGITS.matcher(wepKey).matches();
69   }
70
71 }