1 /*
2  * Copyright (C) 2013 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.os.Parcel;
20 import android.os.Parcelable;
21 
22 /**
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
28  *  information.
29  *  @hide
30  */
31 public class LinkQualityInfo implements Parcelable {
32 
33     /**
34      * Represents a value that you can use to test if an integer field is set to a good value
35      */
36     public static final int UNKNOWN_INT = Integer.MAX_VALUE;
37 
38     /**
39      * Represents a value that you can use to test if a long field is set to a good value
40      */
41     public static final long UNKNOWN_LONG = Long.MAX_VALUE;
42 
43     public static final int NORMALIZED_MIN_SIGNAL_STRENGTH = 0;
44 
45     public static final int NORMALIZED_MAX_SIGNAL_STRENGTH = 99;
46 
47     public static final int NORMALIZED_SIGNAL_STRENGTH_RANGE =
48             NORMALIZED_MAX_SIGNAL_STRENGTH - NORMALIZED_MIN_SIGNAL_STRENGTH + 1;
49 
50     /* Network type as defined by ConnectivityManager */
51     private int mNetworkType = ConnectivityManager.TYPE_NONE;
52 
53     private int mNormalizedSignalStrength = UNKNOWN_INT;
54 
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;
60 
61     /* Timestamp when last sample was made available */
62     private long mLastDataSampleTime = UNKNOWN_LONG;
63 
64     /* Sample duration in millisecond */
65     private int mDataSampleDuration = UNKNOWN_INT;
66 
LinkQualityInfo()67     public LinkQualityInfo() {
68 
69     }
70 
71     /**
72      * Implement the Parcelable interface
73      * @hide
74      */
describeContents()75     public int describeContents() {
76         return 0;
77     }
78 
79     /**
80      * Implement the Parcelable interface.
81      */
82 
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;
86 
87     /**
88      * @hide
89      */
writeToParcel(Parcel dest, int flags)90     public void writeToParcel(Parcel dest, int flags) {
91         writeToParcel(dest, flags, OBJECT_TYPE_LINK_QUALITY_INFO);
92     }
93 
94     /**
95      * @hide
96      */
writeToParcel(Parcel dest, int flags, int objectType)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);
108     }
109 
110     /**
111      * @hide
112      */
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);
120                         return li;
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);
125                     } else {
126                         return null;
127                     }
128                 }
129 
130                 public LinkQualityInfo[] newArray(int size) {
131                     return new LinkQualityInfo[size];
132                 }
133             };
134 
135     /**
136      * @hide
137      */
initializeFromParcel(Parcel in)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();
148     }
149 
150     /**
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}
154      */
getNetworkType()155     public int getNetworkType() {
156         return mNetworkType;
157     }
158 
159     /**
160      * @hide
161      */
setNetworkType(int networkType)162     public void setNetworkType(int networkType) {
163         mNetworkType = networkType;
164     }
165 
166     /**
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}
169      */
getNormalizedSignalStrength()170     public int getNormalizedSignalStrength() {
171         return mNormalizedSignalStrength;
172     }
173 
174     /**
175      * @hide
176      */
setNormalizedSignalStrength(int normalizedSignalStrength)177     public void setNormalizedSignalStrength(int normalizedSignalStrength) {
178         mNormalizedSignalStrength = normalizedSignalStrength;
179     }
180 
181     /**
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}
184      */
getPacketCount()185     public long getPacketCount() {
186         return mPacketCount;
187     }
188 
189     /**
190      * @hide
191      */
setPacketCount(long packetCount)192     public void setPacketCount(long packetCount) {
193         mPacketCount = packetCount;
194     }
195 
196     /**
197      * returns the total number of packets errors encountered in sample duration
198      * @return number of errors or {@link android.net.LinkQualityInfo#UNKNOWN_LONG}
199      */
getPacketErrorCount()200     public long getPacketErrorCount() {
201         return mPacketErrorCount;
202     }
203 
204     /**
205      * @hide
206      */
setPacketErrorCount(long packetErrorCount)207     public void setPacketErrorCount(long packetErrorCount) {
208         mPacketErrorCount = packetErrorCount;
209     }
210 
211     /**
212      * returns the theoretical upload bandwidth of this network
213      * @return bandwidth in Kbps or {@link android.net.LinkQualityInfo#UNKNOWN_INT}
214      */
getTheoreticalTxBandwidth()215     public int getTheoreticalTxBandwidth() {
216         return mTheoreticalTxBandwidth;
217     }
218 
219     /**
220      * @hide
221      */
setTheoreticalTxBandwidth(int theoreticalTxBandwidth)222     public void setTheoreticalTxBandwidth(int theoreticalTxBandwidth) {
223         mTheoreticalTxBandwidth = theoreticalTxBandwidth;
224     }
225 
226     /**
227      * returns the theoretical download bandwidth of this network
228      * @return bandwidth in Kbps or {@link android.net.LinkQualityInfo#UNKNOWN_INT}
229      */
getTheoreticalRxBandwidth()230     public int getTheoreticalRxBandwidth() {
231         return mTheoreticalRxBandwidth;
232     }
233 
234     /**
235      * @hide
236      */
setTheoreticalRxBandwidth(int theoreticalRxBandwidth)237     public void setTheoreticalRxBandwidth(int theoreticalRxBandwidth) {
238         mTheoreticalRxBandwidth = theoreticalRxBandwidth;
239     }
240 
241     /**
242      * returns the theoretical latency of this network
243      * @return latency in milliseconds or {@link android.net.LinkQualityInfo#UNKNOWN_INT}
244      */
getTheoreticalLatency()245     public int getTheoreticalLatency() {
246         return mTheoreticalLatency;
247     }
248 
249     /**
250      * @hide
251      */
setTheoreticalLatency(int theoreticalLatency)252     public void setTheoreticalLatency(int theoreticalLatency) {
253         mTheoreticalLatency = theoreticalLatency;
254     }
255 
256     /**
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}
260      */
getLastDataSampleTime()261     public long getLastDataSampleTime() {
262         return mLastDataSampleTime;
263     }
264 
265     /**
266      * @hide
267      */
setLastDataSampleTime(long lastDataSampleTime)268     public void setLastDataSampleTime(long lastDataSampleTime) {
269         mLastDataSampleTime = lastDataSampleTime;
270     }
271 
272     /**
273      * returns the sample duration used
274      * @return duration in milliseconds or {@link android.net.LinkQualityInfo#UNKNOWN_INT}
275      */
getDataSampleDuration()276     public int getDataSampleDuration() {
277         return mDataSampleDuration;
278     }
279 
280     /**
281      * @hide
282      */
setDataSampleDuration(int dataSampleDuration)283     public void setDataSampleDuration(int dataSampleDuration) {
284         mDataSampleDuration = dataSampleDuration;
285     }
286 }
287