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 * A network identifier along with a score for the quality of that network.
27 *
28 * @hide
29 */
30@SystemApi
31public class ScoredNetwork implements Parcelable {
32
33    /** A {@link NetworkKey} uniquely identifying this network. */
34    public final NetworkKey networkKey;
35
36    /**
37     * The {@link RssiCurve} representing the scores for this network based on the RSSI.
38     *
39     * <p>This field is optional and may be set to null to indicate that no score is available for
40     * this network at this time. Such networks, along with networks for which the scorer has not
41     * responded, are always prioritized below scored networks, regardless of the score.
42     */
43    public final RssiCurve rssiCurve;
44
45    /**
46     * Construct a new {@link ScoredNetwork}.
47     *
48     * @param networkKey the {@link NetworkKey} uniquely identifying this network.
49     * @param rssiCurve the {@link RssiCurve} representing the scores for this network based on the
50     *     RSSI. This field is optional, and may be skipped to represent a network which the scorer
51     *     has opted not to score at this time. Passing a null value here is strongly preferred to
52     *     not returning any {@link ScoredNetwork} for a given {@link NetworkKey} because it
53     *     indicates to the system not to request scores for this network in the future, although
54     *     the scorer may choose to issue an out-of-band update at any time.
55     */
56    public ScoredNetwork(NetworkKey networkKey, RssiCurve rssiCurve) {
57        this.networkKey = networkKey;
58        this.rssiCurve = rssiCurve;
59    }
60
61    private ScoredNetwork(Parcel in) {
62        networkKey = NetworkKey.CREATOR.createFromParcel(in);
63        if (in.readByte() == 1) {
64            rssiCurve = RssiCurve.CREATOR.createFromParcel(in);
65        } else {
66            rssiCurve = null;
67        }
68    }
69
70    @Override
71    public int describeContents() {
72        return 0;
73    }
74
75    @Override
76    public void writeToParcel(Parcel out, int flags) {
77        networkKey.writeToParcel(out, flags);
78        if (rssiCurve != null) {
79            out.writeByte((byte) 1);
80            rssiCurve.writeToParcel(out, flags);
81        } else {
82            out.writeByte((byte) 0);
83        }
84    }
85
86    @Override
87    public boolean equals(Object o) {
88        if (this == o) return true;
89        if (o == null || getClass() != o.getClass()) return false;
90
91        ScoredNetwork that = (ScoredNetwork) o;
92
93        return Objects.equals(networkKey, that.networkKey) &&
94                Objects.equals(rssiCurve, that.rssiCurve);
95    }
96
97    @Override
98    public int hashCode() {
99        return Objects.hash(networkKey, rssiCurve);
100    }
101
102    @Override
103    public String toString() {
104        return "ScoredNetwork[key=" + networkKey + ",score=" + rssiCurve + "]";
105    }
106
107    public static final Parcelable.Creator<ScoredNetwork> CREATOR =
108            new Parcelable.Creator<ScoredNetwork>() {
109                @Override
110                public ScoredNetwork createFromParcel(Parcel in) {
111                    return new ScoredNetwork(in);
112                }
113
114                @Override
115                public ScoredNetwork[] newArray(int size) {
116                    return new ScoredNetwork[size];
117                }
118            };
119}
120