Convert Figma logo to code with AI

facebookarchive logonetwork-connection-class

Listen to current network traffic in the app and categorize the quality of the network.

3,184
515
3,184
26

Top Related Projects

Augmented Traffic Control: A tool to simulate network conditions

1,671

Platform for building access networks and modular network services

1,478

An implementation of the QUIC transport protocol.

45,699

Square’s meticulous HTTP client for the JVM, Android, and GraalVM.

41,765

The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)

Quick Overview

Network Connection Class is an Android library developed by Facebook to detect the quality of a network connection. It allows developers to determine the current network conditions and adjust their app's behavior accordingly, such as modifying image quality or video streaming bitrate.

Pros

  • Provides real-time network quality detection
  • Helps optimize app performance based on network conditions
  • Easy integration into existing Android projects
  • Open-source and maintained by Facebook

Cons

  • Limited to Android platform
  • May require additional permissions for network access
  • Potential battery drain due to continuous network monitoring
  • Last updated in 2019, which might indicate less active maintenance

Code Examples

  1. Initializing the NetworkQualityEstimator:
NetworkQualityEstimator estimator = new NetworkQualityEstimator.Builder(context).build();
estimator.startSampling();
  1. Getting the current network quality:
ConnectionQuality quality = estimator.getCurrentBandwidthQuality();
switch (quality) {
    case POOR:
        // Handle poor connection
        break;
    case MODERATE:
        // Handle moderate connection
        break;
    case GOOD:
        // Handle good connection
        break;
    case EXCELLENT:
        // Handle excellent connection
        break;
}
  1. Registering a listener for network quality changes:
estimator.addNetworkQualityChangeListener(new NetworkQualityEstimator.NetworkQualityChangeListener() {
    @Override
    public void onNetworkQualityChanged(ConnectionQuality networkQuality) {
        // Handle network quality change
    }
});

Getting Started

To use Network Connection Class in your Android project:

  1. Add the following to your app's build.gradle:
dependencies {
    implementation 'com.facebook.network.connectionclass:connectionclass:1.0.1'
}
  1. Initialize the NetworkQualityEstimator in your application or activity:
NetworkQualityEstimator estimator = new NetworkQualityEstimator.Builder(context).build();
estimator.startSampling();
  1. Use the estimator to get network quality information or register listeners as shown in the code examples above.

Competitor Comparisons

Augmented Traffic Control: A tool to simulate network conditions

Pros of Augmented Traffic Control

  • More comprehensive network simulation capabilities, allowing for testing various network conditions
  • Supports multiple clients and devices simultaneously
  • Provides a web-based interface for easier management and configuration

Cons of Augmented Traffic Control

  • More complex setup and installation process
  • Requires additional hardware or network configuration to function properly
  • May have a steeper learning curve for new users

Code Comparison

Network Connection Class:

public enum ConnectionQuality {
    UNKNOWN,
    POOR,
    MODERATE,
    GOOD,
    EXCELLENT
}

Augmented Traffic Control:

class TrafficControlledDevice(models.Model):
    address = models.GenericIPAddressField(unique=True)
    name = models.CharField(max_length=255, blank=True)
    group = models.ForeignKey(TrafficControlGroup, related_name='devices')

The Network Connection Class repository focuses on classifying network quality, while Augmented Traffic Control provides more extensive network simulation and management features. Network Connection Class is simpler to implement but offers limited functionality compared to Augmented Traffic Control's broader capabilities for testing and simulating various network conditions across multiple devices.

1,671

Platform for building access networks and modular network services

Pros of Magma

  • Comprehensive mobile network platform with broader scope and functionality
  • Active development and community support
  • Supports multiple network technologies (4G, 5G, WiFi)

Cons of Magma

  • More complex setup and configuration
  • Steeper learning curve for new users
  • Requires more resources to run and maintain

Code Comparison

Network-connection-class (Java):

public enum ConnectionQuality {
    UNKNOWN,
    POOR,
    MODERATE,
    GOOD,
    EXCELLENT
}

Magma (Python):

class ConnectionQuality(Enum):
    UNKNOWN = 0
    POOR = 1
    MODERATE = 2
    GOOD = 3
    EXCELLENT = 4

Summary

Network-connection-class is a focused library for Android network quality detection, while Magma is a comprehensive mobile network platform. Network-connection-class is simpler to implement but has limited functionality. Magma offers more features and flexibility but requires more resources and expertise to set up and maintain. The code comparison shows similar approaches to defining connection quality, with Magma using Python and Network-connection-class using Java.

1,478

An implementation of the QUIC transport protocol.

Pros of mvfst

  • Active development: mvfst is actively maintained and updated, while network-connection-class is archived
  • Broader scope: Implements a full QUIC transport protocol, offering more comprehensive networking capabilities
  • Better performance: Designed for high-performance, low-latency networking applications

Cons of mvfst

  • Higher complexity: Requires more in-depth knowledge to implement and use effectively
  • Steeper learning curve: Less suitable for simple network classification tasks
  • Resource-intensive: May consume more system resources due to its comprehensive feature set

Code Comparison

network-connection-class:

public enum ConnectionQuality {
  UNKNOWN,
  POOR,
  MODERATE,
  GOOD,
  EXCELLENT
}

mvfst:

enum class StreamDirectionality : uint8_t {
  Unidirectional = 0,
  Bidirectional = 1,
};

The code snippets demonstrate the difference in scope and complexity. network-connection-class focuses on simple connection quality classification, while mvfst deals with more complex networking concepts like stream directionality in the QUIC protocol.

45,699

Square’s meticulous HTTP client for the JVM, Android, and GraalVM.

Pros of OkHttp

  • Actively maintained and widely used HTTP client for Android and Java applications
  • Supports modern protocols like HTTP/2 and QUIC
  • Extensive features including connection pooling, request/response interceptors, and caching

Cons of OkHttp

  • Larger library size compared to network-connection-class
  • More complex to use for simple network quality measurements
  • Focuses on HTTP communication rather than network quality classification

Code Comparison

network-connection-class:

ConnectionClassManager.getInstance().register(mListener);
ConnectionQuality quality = ConnectionClassManager.getInstance().getCurrentBandwidthQuality();

OkHttp:

OkHttpClient client = new OkHttpClient.Builder()
    .addInterceptor(new NetworkQualityInterceptor())
    .build();
Response response = client.newCall(request).execute();

Key Differences

  • network-connection-class is specifically designed for network quality classification, while OkHttp is a full-featured HTTP client
  • OkHttp provides more comprehensive networking capabilities but requires more setup for network quality monitoring
  • network-connection-class is simpler to use for basic network quality measurements but lacks advanced HTTP features

Use Cases

  • Choose network-connection-class for lightweight network quality monitoring in Android apps
  • Opt for OkHttp when building applications that require robust HTTP communication features alongside network quality insights
41,765

The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)

Pros of gRPC

  • Broader scope and functionality, offering a complete RPC framework
  • Active development and maintenance with regular updates
  • Extensive language support and cross-platform compatibility

Cons of gRPC

  • Steeper learning curve due to its comprehensive feature set
  • Potentially higher resource usage for simpler applications
  • More complex setup and configuration process

Code Comparison

network-connection-class (Java):

public class ConnectionClassManager {
    public static ConnectionQuality getCurrentBandwidthQuality() {
        // Implementation to determine network quality
    }
}

gRPC (Java):

public class GrpcServer {
    public static void main(String[] args) throws IOException, InterruptedException {
        Server server = ServerBuilder.forPort(8080)
            .addService(new MyServiceImpl())
            .build()
            .start();
        server.awaitTermination();
    }
}

Key Differences

  • network-connection-class focuses specifically on network quality detection
  • gRPC provides a full-featured RPC framework for building distributed systems
  • network-connection-class is archived and no longer maintained
  • gRPC offers more advanced features like streaming, authentication, and load balancing

Use Cases

  • network-connection-class: Ideal for apps needing simple network quality assessment
  • gRPC: Suitable for building complex, scalable microservices architectures

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Network Connection Class Network Connection Class

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, and also applies some hysteresis (both with a minimum number of samples and amount the average has to cross a boundary before triggering a bucket change): Bandwidth Averaging

Integration

Download

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>

Calculate Connection Class

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.

Improve Connection Class!

See the CONTRIBUTING.md file for how to help out.

License

Connection Class is BSD-licensed. We also provide an additional patent grant.