2 * Copyright (C) 2013 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.os.Parcel;
20 import android.os.Parcelable;
23 * Class that represents useful attributes of generic network links
24 * such as the upload/download throughput or packet error rate.
25 * Generally speaking, you should be dealing with instances of
26 * LinkQualityInfo subclasses, such as {@link android.net.#WifiLinkQualityInfo}
27 * or {@link android.net.#MobileLinkQualityInfo} which provide additional
31 public class LinkQualityInfo implements Parcelable {
34 * Represents a value that you can use to test if an integer field is set to a good value
36 public static final int UNKNOWN_INT = Integer.MAX_VALUE;
39 * Represents a value that you can use to test if a long field is set to a good value
41 public static final long UNKNOWN_LONG = Long.MAX_VALUE;
43 public static final int NORMALIZED_MIN_SIGNAL_STRENGTH = 0;
45 public static final int NORMALIZED_MAX_SIGNAL_STRENGTH = 99;
47 public static final int NORMALIZED_SIGNAL_STRENGTH_RANGE =
48 NORMALIZED_MAX_SIGNAL_STRENGTH - NORMALIZED_MIN_SIGNAL_STRENGTH + 1;
50 /* Network type as defined by ConnectivityManager */
51 private int mNetworkType = ConnectivityManager.TYPE_NONE;
53 private int mNormalizedSignalStrength = UNKNOWN_INT;
55 private long mPacketCount = UNKNOWN_LONG;
56 private long mPacketErrorCount = UNKNOWN_LONG;
57 private int mTheoreticalTxBandwidth = UNKNOWN_INT;
58 private int mTheoreticalRxBandwidth = UNKNOWN_INT;
59 private int mTheoreticalLatency = UNKNOWN_INT;
61 /* Timestamp when last sample was made available */
62 private long mLastDataSampleTime = UNKNOWN_LONG;
64 /* Sample duration in millisecond */
65 private int mDataSampleDuration = UNKNOWN_INT;
67 public LinkQualityInfo() {
72 * Implement the Parcelable interface
75 public int describeContents() {
80 * Implement the Parcelable interface.
83 protected static final int OBJECT_TYPE_LINK_QUALITY_INFO = 1;
84 protected static final int OBJECT_TYPE_WIFI_LINK_QUALITY_INFO = 2;
85 protected static final int OBJECT_TYPE_MOBILE_LINK_QUALITY_INFO = 3;
90 public void writeToParcel(Parcel dest, int flags) {
91 writeToParcel(dest, flags, OBJECT_TYPE_LINK_QUALITY_INFO);
97 public void writeToParcel(Parcel dest, int flags, int objectType) {
98 dest.writeInt(objectType);
99 dest.writeInt(mNetworkType);
100 dest.writeInt(mNormalizedSignalStrength);
101 dest.writeLong(mPacketCount);
102 dest.writeLong(mPacketErrorCount);
103 dest.writeInt(mTheoreticalTxBandwidth);
104 dest.writeInt(mTheoreticalRxBandwidth);
105 dest.writeInt(mTheoreticalLatency);
106 dest.writeLong(mLastDataSampleTime);
107 dest.writeInt(mDataSampleDuration);
113 public static final Creator<LinkQualityInfo> CREATOR =
114 new Creator<LinkQualityInfo>() {
115 public LinkQualityInfo createFromParcel(Parcel in) {
116 int objectType = in.readInt();
117 if (objectType == OBJECT_TYPE_LINK_QUALITY_INFO) {
118 LinkQualityInfo li = new LinkQualityInfo();
119 li.initializeFromParcel(in);
121 } else if (objectType == OBJECT_TYPE_WIFI_LINK_QUALITY_INFO) {
122 return WifiLinkQualityInfo.createFromParcelBody(in);
123 } else if (objectType == OBJECT_TYPE_MOBILE_LINK_QUALITY_INFO) {
124 return MobileLinkQualityInfo.createFromParcelBody(in);
130 public LinkQualityInfo[] newArray(int size) {
131 return new LinkQualityInfo[size];
138 protected void initializeFromParcel(Parcel in) {
139 mNetworkType = in.readInt();
140 mNormalizedSignalStrength = in.readInt();
141 mPacketCount = in.readLong();
142 mPacketErrorCount = in.readLong();
143 mTheoreticalTxBandwidth = in.readInt();
144 mTheoreticalRxBandwidth = in.readInt();
145 mTheoreticalLatency = in.readInt();
146 mLastDataSampleTime = in.readLong();
147 mDataSampleDuration = in.readInt();
151 * returns the type of network this link is connected to
152 * @return network type as defined by {@link android.net.ConnectivityManager} or
153 * {@link android.net.LinkQualityInfo#UNKNOWN_INT}
155 public int getNetworkType() {
162 public void setNetworkType(int networkType) {
163 mNetworkType = networkType;
167 * returns the signal strength normalized across multiple types of networks
168 * @return an integer value from 0 - 99 or {@link android.net.LinkQualityInfo#UNKNOWN_INT}
170 public int getNormalizedSignalStrength() {
171 return mNormalizedSignalStrength;
177 public void setNormalizedSignalStrength(int normalizedSignalStrength) {
178 mNormalizedSignalStrength = normalizedSignalStrength;
182 * returns the total number of packets sent or received in sample duration
183 * @return number of packets or {@link android.net.LinkQualityInfo#UNKNOWN_LONG}
185 public long getPacketCount() {
192 public void setPacketCount(long packetCount) {
193 mPacketCount = packetCount;
197 * returns the total number of packets errors encountered in sample duration
198 * @return number of errors or {@link android.net.LinkQualityInfo#UNKNOWN_LONG}
200 public long getPacketErrorCount() {
201 return mPacketErrorCount;
207 public void setPacketErrorCount(long packetErrorCount) {
208 mPacketErrorCount = packetErrorCount;
212 * returns the theoretical upload bandwidth of this network
213 * @return bandwidth in Kbps or {@link android.net.LinkQualityInfo#UNKNOWN_INT}
215 public int getTheoreticalTxBandwidth() {
216 return mTheoreticalTxBandwidth;
222 public void setTheoreticalTxBandwidth(int theoreticalTxBandwidth) {
223 mTheoreticalTxBandwidth = theoreticalTxBandwidth;
227 * returns the theoretical download bandwidth of this network
228 * @return bandwidth in Kbps or {@link android.net.LinkQualityInfo#UNKNOWN_INT}
230 public int getTheoreticalRxBandwidth() {
231 return mTheoreticalRxBandwidth;
237 public void setTheoreticalRxBandwidth(int theoreticalRxBandwidth) {
238 mTheoreticalRxBandwidth = theoreticalRxBandwidth;
242 * returns the theoretical latency of this network
243 * @return latency in milliseconds or {@link android.net.LinkQualityInfo#UNKNOWN_INT}
245 public int getTheoreticalLatency() {
246 return mTheoreticalLatency;
252 public void setTheoreticalLatency(int theoreticalLatency) {
253 mTheoreticalLatency = theoreticalLatency;
257 * returns the time stamp of the last sample
258 * @return milliseconds elapsed since start and sample time or
259 * {@link android.net.LinkQualityInfo#UNKNOWN_LONG}
261 public long getLastDataSampleTime() {
262 return mLastDataSampleTime;
268 public void setLastDataSampleTime(long lastDataSampleTime) {
269 mLastDataSampleTime = lastDataSampleTime;
273 * returns the sample duration used
274 * @return duration in milliseconds or {@link android.net.LinkQualityInfo#UNKNOWN_INT}
276 public int getDataSampleDuration() {
277 return mDataSampleDuration;
283 public void setDataSampleDuration(int dataSampleDuration) {
284 mDataSampleDuration = dataSampleDuration;