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.compat.annotation.UnsupportedAppUsage; 20 import android.os.Build; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 24 /** 25 * Class that represents useful attributes of generic network links 26 * such as the upload/download throughput or packet error rate. 27 * Generally speaking, you should be dealing with instances of 28 * LinkQualityInfo subclasses, such as {@link android.net.WifiLinkQualityInfo} 29 * or {@link android.net.MobileLinkQualityInfo} which provide additional 30 * information. 31 * @hide 32 */ 33 public class LinkQualityInfo implements Parcelable { 34 35 /** 36 * Represents a value that you can use to test if an integer field is set to a good value 37 */ 38 public static final int UNKNOWN_INT = Integer.MAX_VALUE; 39 40 /** 41 * Represents a value that you can use to test if a long field is set to a good value 42 */ 43 public static final long UNKNOWN_LONG = Long.MAX_VALUE; 44 45 public static final int NORMALIZED_MIN_SIGNAL_STRENGTH = 0; 46 47 public static final int NORMALIZED_MAX_SIGNAL_STRENGTH = 99; 48 49 public static final int NORMALIZED_SIGNAL_STRENGTH_RANGE = 50 NORMALIZED_MAX_SIGNAL_STRENGTH - NORMALIZED_MIN_SIGNAL_STRENGTH + 1; 51 52 /* Network type as defined by ConnectivityManager */ 53 private int mNetworkType = ConnectivityManager.TYPE_NONE; 54 55 private int mNormalizedSignalStrength = UNKNOWN_INT; 56 57 private long mPacketCount = UNKNOWN_LONG; 58 private long mPacketErrorCount = UNKNOWN_LONG; 59 private int mTheoreticalTxBandwidth = UNKNOWN_INT; 60 private int mTheoreticalRxBandwidth = UNKNOWN_INT; 61 private int mTheoreticalLatency = UNKNOWN_INT; 62 63 /* Timestamp when last sample was made available */ 64 private long mLastDataSampleTime = UNKNOWN_LONG; 65 66 /* Sample duration in millisecond */ 67 private int mDataSampleDuration = UNKNOWN_INT; 68 LinkQualityInfo()69 public LinkQualityInfo() { 70 71 } 72 73 /** 74 * Implement the Parcelable interface 75 * @hide 76 */ describeContents()77 public int describeContents() { 78 return 0; 79 } 80 81 /** 82 * Implement the Parcelable interface. 83 */ 84 85 protected static final int OBJECT_TYPE_LINK_QUALITY_INFO = 1; 86 protected static final int OBJECT_TYPE_WIFI_LINK_QUALITY_INFO = 2; 87 protected static final int OBJECT_TYPE_MOBILE_LINK_QUALITY_INFO = 3; 88 89 /** 90 * @hide 91 */ writeToParcel(Parcel dest, int flags)92 public void writeToParcel(Parcel dest, int flags) { 93 writeToParcel(dest, flags, OBJECT_TYPE_LINK_QUALITY_INFO); 94 } 95 96 /** 97 * @hide 98 */ writeToParcel(Parcel dest, int flags, int objectType)99 public void writeToParcel(Parcel dest, int flags, int objectType) { 100 dest.writeInt(objectType); 101 dest.writeInt(mNetworkType); 102 dest.writeInt(mNormalizedSignalStrength); 103 dest.writeLong(mPacketCount); 104 dest.writeLong(mPacketErrorCount); 105 dest.writeInt(mTheoreticalTxBandwidth); 106 dest.writeInt(mTheoreticalRxBandwidth); 107 dest.writeInt(mTheoreticalLatency); 108 dest.writeLong(mLastDataSampleTime); 109 dest.writeInt(mDataSampleDuration); 110 } 111 112 /** 113 * @hide 114 */ 115 public static final @android.annotation.NonNull Creator<LinkQualityInfo> CREATOR = 116 new Creator<LinkQualityInfo>() { 117 public LinkQualityInfo createFromParcel(Parcel in) { 118 int objectType = in.readInt(); 119 if (objectType == OBJECT_TYPE_LINK_QUALITY_INFO) { 120 LinkQualityInfo li = new LinkQualityInfo(); 121 li.initializeFromParcel(in); 122 return li; 123 } else if (objectType == OBJECT_TYPE_WIFI_LINK_QUALITY_INFO) { 124 return WifiLinkQualityInfo.createFromParcelBody(in); 125 } else if (objectType == OBJECT_TYPE_MOBILE_LINK_QUALITY_INFO) { 126 return MobileLinkQualityInfo.createFromParcelBody(in); 127 } else { 128 return null; 129 } 130 } 131 132 public LinkQualityInfo[] newArray(int size) { 133 return new LinkQualityInfo[size]; 134 } 135 }; 136 137 /** 138 * @hide 139 */ initializeFromParcel(Parcel in)140 protected void initializeFromParcel(Parcel in) { 141 mNetworkType = in.readInt(); 142 mNormalizedSignalStrength = in.readInt(); 143 mPacketCount = in.readLong(); 144 mPacketErrorCount = in.readLong(); 145 mTheoreticalTxBandwidth = in.readInt(); 146 mTheoreticalRxBandwidth = in.readInt(); 147 mTheoreticalLatency = in.readInt(); 148 mLastDataSampleTime = in.readLong(); 149 mDataSampleDuration = in.readInt(); 150 } 151 152 /** 153 * returns the type of network this link is connected to 154 * @return network type as defined by {@link android.net.ConnectivityManager} or 155 * {@link android.net.LinkQualityInfo#UNKNOWN_INT} 156 */ getNetworkType()157 public int getNetworkType() { 158 return mNetworkType; 159 } 160 161 /** 162 * @hide 163 */ setNetworkType(int networkType)164 public void setNetworkType(int networkType) { 165 mNetworkType = networkType; 166 } 167 168 /** 169 * returns the signal strength normalized across multiple types of networks 170 * @return an integer value from 0 - 99 or {@link android.net.LinkQualityInfo#UNKNOWN_INT} 171 */ getNormalizedSignalStrength()172 public int getNormalizedSignalStrength() { 173 return mNormalizedSignalStrength; 174 } 175 176 /** 177 * @hide 178 */ setNormalizedSignalStrength(int normalizedSignalStrength)179 public void setNormalizedSignalStrength(int normalizedSignalStrength) { 180 mNormalizedSignalStrength = normalizedSignalStrength; 181 } 182 183 /** 184 * returns the total number of packets sent or received in sample duration 185 * @return number of packets or {@link android.net.LinkQualityInfo#UNKNOWN_LONG} 186 */ getPacketCount()187 public long getPacketCount() { 188 return mPacketCount; 189 } 190 191 /** 192 * @hide 193 */ 194 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) setPacketCount(long packetCount)195 public void setPacketCount(long packetCount) { 196 mPacketCount = packetCount; 197 } 198 199 /** 200 * returns the total number of packets errors encountered in sample duration 201 * @return number of errors or {@link android.net.LinkQualityInfo#UNKNOWN_LONG} 202 */ getPacketErrorCount()203 public long getPacketErrorCount() { 204 return mPacketErrorCount; 205 } 206 207 /** 208 * @hide 209 */ 210 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) setPacketErrorCount(long packetErrorCount)211 public void setPacketErrorCount(long packetErrorCount) { 212 mPacketErrorCount = packetErrorCount; 213 } 214 215 /** 216 * returns the theoretical upload bandwidth of this network 217 * @return bandwidth in Kbps or {@link android.net.LinkQualityInfo#UNKNOWN_INT} 218 */ getTheoreticalTxBandwidth()219 public int getTheoreticalTxBandwidth() { 220 return mTheoreticalTxBandwidth; 221 } 222 223 /** 224 * @hide 225 */ setTheoreticalTxBandwidth(int theoreticalTxBandwidth)226 public void setTheoreticalTxBandwidth(int theoreticalTxBandwidth) { 227 mTheoreticalTxBandwidth = theoreticalTxBandwidth; 228 } 229 230 /** 231 * returns the theoretical download bandwidth of this network 232 * @return bandwidth in Kbps or {@link android.net.LinkQualityInfo#UNKNOWN_INT} 233 */ getTheoreticalRxBandwidth()234 public int getTheoreticalRxBandwidth() { 235 return mTheoreticalRxBandwidth; 236 } 237 238 /** 239 * @hide 240 */ setTheoreticalRxBandwidth(int theoreticalRxBandwidth)241 public void setTheoreticalRxBandwidth(int theoreticalRxBandwidth) { 242 mTheoreticalRxBandwidth = theoreticalRxBandwidth; 243 } 244 245 /** 246 * returns the theoretical latency of this network 247 * @return latency in milliseconds or {@link android.net.LinkQualityInfo#UNKNOWN_INT} 248 */ getTheoreticalLatency()249 public int getTheoreticalLatency() { 250 return mTheoreticalLatency; 251 } 252 253 /** 254 * @hide 255 */ setTheoreticalLatency(int theoreticalLatency)256 public void setTheoreticalLatency(int theoreticalLatency) { 257 mTheoreticalLatency = theoreticalLatency; 258 } 259 260 /** 261 * returns the time stamp of the last sample 262 * @return milliseconds elapsed since start and sample time or 263 * {@link android.net.LinkQualityInfo#UNKNOWN_LONG} 264 */ getLastDataSampleTime()265 public long getLastDataSampleTime() { 266 return mLastDataSampleTime; 267 } 268 269 /** 270 * @hide 271 */ 272 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) setLastDataSampleTime(long lastDataSampleTime)273 public void setLastDataSampleTime(long lastDataSampleTime) { 274 mLastDataSampleTime = lastDataSampleTime; 275 } 276 277 /** 278 * returns the sample duration used 279 * @return duration in milliseconds or {@link android.net.LinkQualityInfo#UNKNOWN_INT} 280 */ getDataSampleDuration()281 public int getDataSampleDuration() { 282 return mDataSampleDuration; 283 } 284 285 /** 286 * @hide 287 */ 288 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) setDataSampleDuration(int dataSampleDuration)289 public void setDataSampleDuration(int dataSampleDuration) { 290 mDataSampleDuration = dataSampleDuration; 291 } 292 } 293