| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.net; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.annotation.SystemApi; |
| 23 |
import android.net.wifi.ScanResult; |
| 24 |
import android.net.wifi.WifiInfo; |
| 25 |
import android.net.wifi.WifiManager; |
| 26 |
import android.os.Parcel; |
| 27 |
import android.os.Parcelable; |
| 28 |
import android.text.TextUtils; |
| 29 |
import android.util.Log; |
| 30 |
|
| 31 |
import java.lang.annotation.Retention; |
| 32 |
import java.lang.annotation.RetentionPolicy; |
| 33 |
import java.util.Objects; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
@Deprecated |
| 42 |
@SystemApi |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public class NetworkKey implements Parcelable { |
| 50 |
|
| 51 |
private static final String TAG = "NetworkKey"; |
| 52 |
|
| 53 |
|
| 54 |
public static final int TYPE_WIFI = 1; |
| 55 |
|
| 56 |
|
| 57 |
@Retention(RetentionPolicy.SOURCE) |
| 58 |
@IntDef(prefix = {"TYPE_"}, value = { |
| 59 |
TYPE_WIFI |
| 60 |
}) |
| 61 |
public @interface NetworkType {} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public final int type; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public final WifiKey wifiKey; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
@Nullable |
| 83 |
public static NetworkKey createFromScanResult(@NonNull ScanResult result) { |
| 84 |
Objects.requireNonNull(result); |
| 85 |
final String ssid = result.SSID; |
| 86 |
if (TextUtils.isEmpty(ssid) || ssid.equals(WifiManager.UNKNOWN_SSID)) { |
| 87 |
return null; |
| 88 |
} |
| 89 |
final String bssid = result.BSSID; |
| 90 |
if (TextUtils.isEmpty(bssid)) { |
| 91 |
return null; |
| 92 |
} |
| 93 |
|
| 94 |
try { |
| 95 |
WifiKey wifiKey = new WifiKey(String.format("\"%s\"", ssid), bssid); |
| 96 |
return new NetworkKey(wifiKey); |
| 97 |
} catch (IllegalArgumentException e) { |
| 98 |
Log.e(TAG, "Unable to create WifiKey.", e); |
| 99 |
return null; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
@Nullable |
| 112 |
public static NetworkKey createFromWifiInfo(@Nullable WifiInfo wifiInfo) { |
| 113 |
if (wifiInfo != null) { |
| 114 |
final String ssid = wifiInfo.getSSID(); |
| 115 |
final String bssid = wifiInfo.getBSSID(); |
| 116 |
if (!TextUtils.isEmpty(ssid) && !ssid.equals(WifiManager.UNKNOWN_SSID) |
| 117 |
&& !TextUtils.isEmpty(bssid)) { |
| 118 |
WifiKey wifiKey; |
| 119 |
try { |
| 120 |
wifiKey = new WifiKey(ssid, bssid); |
| 121 |
} catch (IllegalArgumentException e) { |
| 122 |
Log.e(TAG, "Unable to create WifiKey.", e); |
| 123 |
return null; |
| 124 |
} |
| 125 |
return new NetworkKey(wifiKey); |
| 126 |
} |
| 127 |
} |
| 128 |
return null; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public NetworkKey(WifiKey wifiKey) { |
| 136 |
this.type = TYPE_WIFI; |
| 137 |
this.wifiKey = wifiKey; |
| 138 |
} |
| 139 |
|
| 140 |
private NetworkKey(Parcel in) { |
| 141 |
type = in.readInt(); |
| 142 |
switch (type) { |
| 143 |
case TYPE_WIFI: |
| 144 |
wifiKey = WifiKey.CREATOR.createFromParcel(in); |
| 145 |
break; |
| 146 |
default: |
| 147 |
throw new IllegalArgumentException("Parcel has unknown type: " + type); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
@Override |
| 152 |
public int describeContents() { |
| 153 |
return 0; |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public void writeToParcel(Parcel out, int flags) { |
| 158 |
out.writeInt(type); |
| 159 |
switch (type) { |
| 160 |
case TYPE_WIFI: |
| 161 |
wifiKey.writeToParcel(out, flags); |
| 162 |
break; |
| 163 |
default: |
| 164 |
throw new IllegalStateException("NetworkKey has unknown type " + type); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
@Override |
| 169 |
public boolean equals(@Nullable Object o) { |
| 170 |
if (this == o) return true; |
| 171 |
if (o == null || getClass() != o.getClass()) return false; |
| 172 |
|
| 173 |
NetworkKey that = (NetworkKey) o; |
| 174 |
|
| 175 |
return type == that.type && Objects.equals(wifiKey, that.wifiKey); |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public int hashCode() { |
| 180 |
return Objects.hash(type, wifiKey); |
| 181 |
} |
| 182 |
|
| 183 |
@NonNull |
| 184 |
@Override |
| 185 |
public String toString() { |
| 186 |
switch (type) { |
| 187 |
case TYPE_WIFI: |
| 188 |
return wifiKey.toString(); |
| 189 |
default: |
| 190 |
|
| 191 |
|
| 192 |
return "InvalidKey"; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
public static final @android.annotation.NonNull Parcelable.Creator<NetworkKey> CREATOR = |
| 197 |
new Parcelable.Creator<NetworkKey>() { |
| 198 |
@Override |
| 199 |
public NetworkKey createFromParcel(Parcel in) { |
| 200 |
return new NetworkKey(in); |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public NetworkKey[] newArray(int size) { |
| 205 |
return new NetworkKey[size]; |
| 206 |
} |
| 207 |
}; |
| 208 |
} |