OSDN Git Service

9e92e89b96877d9755d57d6cfb3d61a23709fef8
[android-x86/frameworks-base.git] / core / java / android / net / WifiKey.java
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
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 android.net;
18
19 import android.os.Parcel;
20 import android.os.Parcelable;
21
22 import java.util.Objects;
23 import java.util.regex.Pattern;
24
25 /**
26  * Information identifying a Wi-Fi network.
27  * @see NetworkKey
28  *
29  * @hide
30  */
31 public class WifiKey implements Parcelable {
32
33     // Patterns used for validation.
34     private static final Pattern SSID_PATTERN = Pattern.compile("(\".*\")|(0x[\\p{XDigit}]+)");
35     private static final Pattern BSSID_PATTERN =
36             Pattern.compile("([\\p{XDigit}]{2}:){5}[\\p{XDigit}]{2}");
37
38     /**
39      * The service set identifier (SSID) of an 802.11 network. If the SSID can be decoded as
40      * UTF-8, it will be surrounded by double quotation marks. Otherwise, it will be a string of
41      * hex digits starting with 0x.
42      */
43     public final String ssid;
44
45     /**
46      * The basic service set identifier (BSSID) of an access point for this network. This will
47      * be in the form of a six-byte MAC address: {@code XX:XX:XX:XX:XX:XX}, where each X is a
48      * hexadecimal digit.
49      */
50     public final String bssid;
51
52     /**
53      * Construct a new {@link WifiKey} for the given Wi-Fi SSID/BSSID pair.
54      *
55      * @param ssid the service set identifier (SSID) of an 802.11 network. If the SSID can be
56      *         decoded as UTF-8, it should be surrounded by double quotation marks. Otherwise,
57      *         it should be a string of hex digits starting with 0x.
58      * @param bssid the basic service set identifier (BSSID) of this network's access point.
59      *         This should be in the form of a six-byte MAC address: {@code XX:XX:XX:XX:XX:XX},
60      *         where each X is a hexadecimal digit.
61      * @throws IllegalArgumentException if either the SSID or BSSID is invalid.
62      */
63     public WifiKey(String ssid, String bssid) {
64         if (!SSID_PATTERN.matcher(ssid).matches()) {
65             throw new IllegalArgumentException("Invalid ssid: " + ssid);
66         }
67         if (!BSSID_PATTERN.matcher(bssid).matches()) {
68             throw new IllegalArgumentException("Invalid bssid: " + bssid);
69         }
70         this.ssid = ssid;
71         this.bssid = bssid;
72     }
73
74     private WifiKey(Parcel in) {
75         ssid = in.readString();
76         bssid = in.readString();
77     }
78
79     @Override
80     public int describeContents() {
81         return 0;
82     }
83
84     @Override
85     public void writeToParcel(Parcel out, int flags) {
86         out.writeString(ssid);
87         out.writeString(bssid);
88     }
89
90     @Override
91     public boolean equals(Object o) {
92         if (this == o) return true;
93         if (o == null || getClass() != o.getClass()) return false;
94
95         WifiKey wifiKey = (WifiKey) o;
96
97         return Objects.equals(ssid, wifiKey.ssid) && Objects.equals(bssid, wifiKey.bssid);
98     }
99
100     @Override
101     public int hashCode() {
102         return Objects.hash(ssid, bssid);
103     }
104
105     @Override
106     public String toString() {
107         return "WifiKey[SSID=" + ssid + ",BSSID=" + bssid + "]";
108     }
109
110     public static final Creator<WifiKey> CREATOR =
111             new Creator<WifiKey>() {
112                 @Override
113                 public WifiKey createFromParcel(Parcel in) {
114                     return new WifiKey(in);
115                 }
116
117                 @Override
118                 public WifiKey[] newArray(int size) {
119                     return new WifiKey[size];
120                 }
121             };
122 }