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 17package android.net; 18 19import android.annotation.SystemApi; 20import android.os.Parcel; 21import android.os.Parcelable; 22 23import java.util.Objects; 24 25/** 26 * Information which identifies a specific network. 27 * 28 * @hide 29 */ 30@SystemApi 31// NOTE: Ideally, we would abstract away the details of what identifies a network of a specific 32// type, so that all networks appear the same and can be scored without concern to the network type 33// itself. However, because no such cross-type identifier currently exists in the Android framework, 34// and because systems might obtain information about networks from sources other than Android 35// devices, we need to provide identifying details about each specific network type (wifi, cell, 36// etc.) so that clients can pull out these details depending on the type of network. 37public class NetworkKey implements Parcelable { 38 39 /** A wifi network, for which {@link #wifiKey} will be populated. */ 40 public static final int TYPE_WIFI = 1; 41 42 /** 43 * The type of this network. 44 * @see #TYPE_WIFI 45 */ 46 public final int type; 47 48 /** 49 * Information identifying a Wi-Fi network. Only set when {@link #type} equals 50 * {@link #TYPE_WIFI}. 51 */ 52 public final WifiKey wifiKey; 53 54 /** 55 * Construct a new {@link NetworkKey} for a Wi-Fi network. 56 * @param wifiKey the {@link WifiKey} identifying this Wi-Fi network. 57 */ 58 public NetworkKey(WifiKey wifiKey) { 59 this.type = TYPE_WIFI; 60 this.wifiKey = wifiKey; 61 } 62 63 private NetworkKey(Parcel in) { 64 type = in.readInt(); 65 switch (type) { 66 case TYPE_WIFI: 67 wifiKey = WifiKey.CREATOR.createFromParcel(in); 68 break; 69 default: 70 throw new IllegalArgumentException("Parcel has unknown type: " + type); 71 } 72 } 73 74 @Override 75 public int describeContents() { 76 return 0; 77 } 78 79 @Override 80 public void writeToParcel(Parcel out, int flags) { 81 out.writeInt(type); 82 switch (type) { 83 case TYPE_WIFI: 84 wifiKey.writeToParcel(out, flags); 85 break; 86 default: 87 throw new IllegalStateException("NetworkKey has unknown type " + type); 88 } 89 } 90 91 @Override 92 public boolean equals(Object o) { 93 if (this == o) return true; 94 if (o == null || getClass() != o.getClass()) return false; 95 96 NetworkKey that = (NetworkKey) o; 97 98 return type == that.type && Objects.equals(wifiKey, that.wifiKey); 99 } 100 101 @Override 102 public int hashCode() { 103 return Objects.hash(type, wifiKey); 104 } 105 106 @Override 107 public String toString() { 108 switch (type) { 109 case TYPE_WIFI: 110 return wifiKey.toString(); 111 default: 112 // Don't throw an exception here in case someone is logging this object in a catch 113 // block for debugging purposes. 114 return "InvalidKey"; 115 } 116 } 117 118 public static final Parcelable.Creator<NetworkKey> CREATOR = 119 new Parcelable.Creator<NetworkKey>() { 120 @Override 121 public NetworkKey createFromParcel(Parcel in) { 122 return new NetworkKey(in); 123 } 124 125 @Override 126 public NetworkKey[] newArray(int size) { 127 return new NetworkKey[size]; 128 } 129 }; 130} 131