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