2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
19 import android.annotation.SystemApi;
20 import android.os.Parcel;
21 import android.os.Parcelable;
23 import java.util.Objects;
26 * Information which identifies a specific network.
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.
37 public class NetworkKey implements Parcelable {
39 /** A wifi network, for which {@link #wifiKey} will be populated. */
40 public static final int TYPE_WIFI = 1;
43 * The type of this network.
46 public final int type;
49 * Information identifying a Wi-Fi network. Only set when {@link #type} equals
52 public final WifiKey wifiKey;
55 * Construct a new {@link NetworkKey} for a Wi-Fi network.
56 * @param wifiKey the {@link WifiKey} identifying this Wi-Fi network.
58 public NetworkKey(WifiKey wifiKey) {
59 this.type = TYPE_WIFI;
60 this.wifiKey = wifiKey;
63 private NetworkKey(Parcel in) {
67 wifiKey = WifiKey.CREATOR.createFromParcel(in);
70 throw new IllegalArgumentException("Parcel has unknown type: " + type);
75 public int describeContents() {
80 public void writeToParcel(Parcel out, int flags) {
84 wifiKey.writeToParcel(out, flags);
87 throw new IllegalStateException("NetworkKey has unknown type " + type);
92 public boolean equals(Object o) {
93 if (this == o) return true;
94 if (o == null || getClass() != o.getClass()) return false;
96 NetworkKey that = (NetworkKey) o;
98 return type == that.type && Objects.equals(wifiKey, that.wifiKey);
102 public int hashCode() {
103 return Objects.hash(type, wifiKey);
107 public String toString() {
110 return wifiKey.toString();
112 // Don't throw an exception here in case someone is logging this object in a catch
113 // block for debugging purposes.
118 public static final Parcelable.Creator<NetworkKey> CREATOR =
119 new Parcelable.Creator<NetworkKey>() {
121 public NetworkKey createFromParcel(Parcel in) {
122 return new NetworkKey(in);
126 public NetworkKey[] newArray(int size) {
127 return new NetworkKey[size];