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.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 * Information which identifies a specific network. 37 * 38 * @deprecated as part of the {@link NetworkScoreManager} deprecation. 39 * @hide 40 */ 41 @Deprecated 42 @SystemApi 43 // NOTE: Ideally, we would abstract away the details of what identifies a network of a specific 44 // type, so that all networks appear the same and can be scored without concern to the network type 45 // itself. However, because no such cross-type identifier currently exists in the Android framework, 46 // and because systems might obtain information about networks from sources other than Android 47 // devices, we need to provide identifying details about each specific network type (wifi, cell, 48 // etc.) so that clients can pull out these details depending on the type of network. 49 public class NetworkKey implements Parcelable { 50 51 private static final String TAG = "NetworkKey"; 52 53 /** A wifi network, for which {@link #wifiKey} will be populated. */ 54 public static final int TYPE_WIFI = 1; 55 56 /** @hide */ 57 @Retention(RetentionPolicy.SOURCE) 58 @IntDef(prefix = {"TYPE_"}, value = { 59 TYPE_WIFI 60 }) 61 public @interface NetworkType {} 62 63 /** 64 * The type of this network. 65 * @see #TYPE_WIFI 66 */ 67 public final int type; 68 69 /** 70 * Information identifying a Wi-Fi network. Only set when {@link #type} equals 71 * {@link #TYPE_WIFI}. 72 */ 73 public final WifiKey wifiKey; 74 75 /** 76 * Constructs a new NetworkKey for the given wifi {@link ScanResult}. 77 * 78 * @return A new {@link NetworkKey} instance or <code>null</code> if the given 79 * {@link ScanResult} instance is malformed. 80 * @throws NullPointerException 81 */ 82 @Nullable createFromScanResult(@onNull ScanResult result)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 * Constructs a new NetworkKey for the given {@link WifiInfo}. 105 * 106 * @param wifiInfo the {@link WifiInfo} to create a {@link NetworkKey} for. 107 * @return A new {@link NetworkKey} instance or <code>null</code> if the given {@link WifiInfo} 108 * instance doesn't represent a connected WiFi network. 109 * @hide 110 */ 111 @Nullable createFromWifiInfo(@ullable WifiInfo wifiInfo)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 * Construct a new {@link NetworkKey} for a Wi-Fi network. 133 * @param wifiKey the {@link WifiKey} identifying this Wi-Fi network. 134 */ NetworkKey(WifiKey wifiKey)135 public NetworkKey(WifiKey wifiKey) { 136 this.type = TYPE_WIFI; 137 this.wifiKey = wifiKey; 138 } 139 NetworkKey(Parcel in)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 describeContents()152 public int describeContents() { 153 return 0; 154 } 155 156 @Override writeToParcel(Parcel out, int flags)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 equals(@ullable Object o)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 hashCode()179 public int hashCode() { 180 return Objects.hash(type, wifiKey); 181 } 182 183 @NonNull 184 @Override toString()185 public String toString() { 186 switch (type) { 187 case TYPE_WIFI: 188 return wifiKey.toString(); 189 default: 190 // Don't throw an exception here in case someone is logging this object in a catch 191 // block for debugging purposes. 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 } 209