Network Connection Class is an Android library that allows you to figure out the quality of the current user's internet connection. The connection gets classified into several "Connection Classes" that make it easy to develop against. The library does this by listening to the existing internet traffic done by your app and notifying you when the user's connection quality changes. Developers can then use this Connection Class information and adjust the application's behaviour (request lower quality images or video, throttle type-ahead, etc).
Network Connection Class currently only measures the user's downstream bandwidth. Latency is also an important factor, but in our tests, we've found that bandwidth is a good proxy for both.
The Network Connection Class library takes care of spikes using a moving average
of the incoming samples:
Download the latest JARs or grab via Gradle:
compile 'com.facebook.network.connectionclass:connectionclass:1.0.1'
or Maven:
<dependency>
<groupId>com.facebook.network.connectionclass</groupId>
<artifactId>connectionclass</artifactId>
<version>1.0.1</version>
</dependency>
Connection Class provides an interface for classes to add themselves as
listeners for when the network's connection quality changes. In the subscriber
class, implement ConnectionClassStateChangeListener
:
public interface ConnectionClassStateChangeListener {
public void onBandwidthStateChange(ConnectionQuality bandwidthState);
}
and subscribe with the listener:
ConnectionClassManager.getInstance().register(mListener);
Alternatively, you can manually query for the current connection quality bucket with
getCurrentBandwidthQuality()
.
ConnectionQuality cq = ConnectionClassManager.getInstance().getCurrentBandwidthQuality();
The main way to provide the ConnectionClassManager data is to use the DeviceBandwidthSampler. The DeviceBandwidthSampler samples the device's underlying network stats when you tell it you're performing some sort of network activity (downloading photos, playing a video, etc).
// Override ConnectionClassStateChangeListener
ConnectionClassManager.getInstance().register(mListener);
DeviceBandwidthSampler.getInstance().startSampling();
// Do some downloading tasks
DeviceBandwidthSampler.getInstance().stopSampling();
If the application is aware of the bandwidth downloaded in a certain time frame, data can be added to the moving average using:
ConnectionClassManager.addBandwidth(bandwidth, time);
See the connectionclass-sample
project for more details.
See the CONTRIBUTING.md file for how to help out.
Connection Class is BSD-licensed. We also provide an additional patent grant.
network-connection-class's People
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.