fe151086578717cc595a4183534bb7de166cf12c
[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 android.text.TextUtils;
20
21 /**
22  * Try with:
23  * http://chart.apis.google.com/chart?cht=qr&chs=240x240&chl=WIFI:S:linksys;P:mypass;T:WPA;;
24  *
25  * TODO(vikrama): Test with binary ssid or password.
26  * 
27  * @author Vikram Aggarwal
28  */
29 final class NetworkUtil {
30
31   private NetworkUtil() {
32   }
33
34   /**
35    * Encloses the incoming string inside double quotes, if it isn't already quoted.
36    * @param string: the input string
37    * @return a quoted string, of the form "input".  If the input string is null, it returns null as well.
38    */
39   static String convertToQuotedString(String string) {
40     if (string == null){
41       return null;
42     }
43     if (TextUtils.isEmpty(string)) {
44       return "";
45     }
46     final int lastPos = string.length() - 1;
47     if (lastPos < 0 || (string.charAt(0) == '"' && string.charAt(lastPos) == '"')) {
48       return string;
49     }
50     return '\"' + string + '\"';
51   }
52
53   private static boolean isHex(CharSequence key) {
54     if (key == null){
55       return false;
56     }
57     for (int i = key.length() - 1; i >= 0; i--) {
58       final char c = key.charAt(i);
59       if (!(c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f')) {
60         return false;
61       }
62     }
63     return true;
64   }
65
66   /**
67    * Check if wepKey is a valid hexadecimal string.
68    * @param wepKey the input to be checked
69    * @return true if the input string is indeed hex or empty.  False if the input string is non-hex or null.
70    */
71   static boolean isHexWepKey(CharSequence wepKey) {
72     if (wepKey == null) {
73       return false;
74     }
75     final int length = wepKey.length();
76     // WEP-40, WEP-104, and some vendors using 256-bit WEP (WEP-232?)
77     return (length == 10 || length == 26 || length == 58) && isHex(wepKey);
78   }
79
80 }