|
| 1 | +# Network Connection Class |
| 2 | + |
| 3 | +Network Connection Class is an Android library that allows you to figure out |
| 4 | +the quality of the current user's internet connection. The connection gets |
| 5 | +classified into several "Connection Classes" that make it easy to develop |
| 6 | +against. The library does this by listening to the existing internet traffic |
| 7 | +done by your app and notifying you when the user's connection quality changes. |
| 8 | +Developers can then use this Connection Class information and adjust the application's |
| 9 | +behaviour (request lower quality images or video, throttle type-ahead, etc). |
| 10 | + |
| 11 | +## Integration |
| 12 | + |
| 13 | +### Download |
| 14 | +Download [the latest JARs](https://github.com/facebook/network-connection-class/releases/latest) or grab via Gradle: |
| 15 | +```groovy |
| 16 | +compile 'com.facebook.network.connectionclass:connectionclass:1.0.0' |
| 17 | +``` |
| 18 | +or Maven: |
| 19 | +```xml |
| 20 | +<dependency> |
| 21 | + <groupId>com.facebook.network.connectionclass</groupId> |
| 22 | + <artifactId>connectionclass</artifactId> |
| 23 | + <version>1.0.0</version> |
| 24 | +</dependency> |
| 25 | +``` |
| 26 | + |
| 27 | +### Calculate Connection Class |
| 28 | +Connection Class provides an interface for classes to add themselves as |
| 29 | +listeners for when the network's connection quality changes. In the subscriber |
| 30 | +class, implement `ConnectionClassStateChangeListener`: |
| 31 | + |
| 32 | +```java |
| 33 | +public interface ConnectionClassStateChangeListener { |
| 34 | + public void onBandwidthStateChange(ConnectionQuality bandwidthState); |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +and subscribe with the listener: |
| 39 | + |
| 40 | +```java |
| 41 | +ConnectionClassManager.getInstance().register(mListener); |
| 42 | +``` |
| 43 | + |
| 44 | +Alternatively, you can manually query for the connection quality bucket with |
| 45 | +`getCurrentBandwidthQuality()`. |
| 46 | + |
| 47 | +```java |
| 48 | +ConnectionQuality cq = ConnectionClassManager.getInstance().getCurrentBandwidthQuality(); |
| 49 | +``` |
| 50 | + |
| 51 | +The main way to provide the ConnectionClassManager data is to use the DownloadBandwidthSampler. |
| 52 | +The DownloadBandwidthSampler samples the device's underlying network stats when you tell it |
| 53 | +you're performing some sort of network activity (downloading photos, playing a video, etc). |
| 54 | + |
| 55 | +```java |
| 56 | +// Override ConnectionClassStateChangeListener |
| 57 | +ConnectionClassManager.getInstance().register(mListener); |
| 58 | +DownloadBandwidthSampler.getInstance().startSampling(); |
| 59 | +// Do some downloading tasks |
| 60 | +DownloadBandwidthSampler.getInstance().stopSampling(); |
| 61 | +``` |
| 62 | + |
| 63 | +If the application is aware of the bandwidth downloaded in a certain time frame, |
| 64 | +data can be added to the moving average using: |
| 65 | + |
| 66 | +```java |
| 67 | +ConnectionClassManager.addBandwidth(bandwidth, time); |
| 68 | +``` |
| 69 | + |
| 70 | +See the `connectionclass-sample` project for more details. |
| 71 | + |
| 72 | +## Improve Connection Class! |
| 73 | +See the CONTRIBUTING.md file for how to help out. |
| 74 | + |
| 75 | +## License |
| 76 | +Connection Class is BSD-licensed. We also provide an additional patent grant. |
0 commit comments