GithubHelp home page GithubHelp logo

fabricethilaw / sonarnet Goto Github PK

View Code? Open in Web Editor NEW
25.0 4.0 3.0 366 KB

Android library for seamless awareness of true Internet access and captive portals on top of Android connectivity API.

License: Apache License 2.0

Kotlin 100.00%
captive-portals detecting-captive-portals internet-connectivity connectivity connectivity-manager android kotlin-android kotlin-library android-library

sonarnet's Introduction

SonarNet

Codacy Badge Maintainability License

An open-source Android library providing a better implementation of Internet access and captive portals detection in your apps.

Technology stack

  • SonarNet is primarily built in Kotlin

  • Compatible with Android 5.0+ (API level >= 21)

Features

  • Detect when device has joined a network that has no Internet access.
  • Detect when connected to a router with captive portal

How it works

SonarNet wraps the ConnectivityManager and lets your app detect true Internet access, not just if the device has joinded a network. So when ConnectivityManager detects Wi-Fi or Cellular network, SonarNet uses a tiny HTTP probe to a known URL (such as connectivitycheck.gstatic.com), to detect whether there is true Internet access, or whether a captive portal is preventing the device to access Internet.

Add Sonarnet to your project

Step 1: Add in your root build.gradle at the end of repositories:

   allprojects {
    repositories {
       maven { url 'https://jitpack.io' }
    }
   }
 

or if there is (the new) dependencyResolutionManagement in settings.gradle :

   dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        maven { url 'https://jitpack.io' }
    }
  }

Step 2: Add the dependency

   dependencies {
     implementation 'com.github.fabricethilaw:sonarnet:1.0.0'
   }

Check Internet status

Usable in any class.

// Detect that INTERNET is available. Get the result from a callback
SonarNet.ping { result ->
    // check result
    when(result) {
        InternetStatus.INTERNET -> {}
        InternetStatus.NO_INTERNET -> {}
        InternetStatus.CAPTIVE_PORTAL -> {}
     }
}

You can also call Ping as a suspending function :

val internetStatus: InternetStatus = SonarNet.ping()
if(internetStatus == INTERNET) {
  // Do something
} else {
  // Proceed otherwise
}

Here is a idiom that enables to perform an action only if internet is available:

SonarNet.runWithInternet {
           // block of logic
        }

Note: In order to perform network operations, the following permissions must be added into your application AndroidManifest.xml :

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Notifications

If you prefer receiving notifications about changes in connectivity , you would provide a context ( from an Activity or a Fragment ) and register a ConnectivityCallback .

A notification from ConnectivityCallback provides knowledge about InternetStatus and the connected NetworkType

// Set a connectivity callback
 val connectivityCallback = object : ConnectivityCallback {
      override fun onConnectionChanged(result: ConnectivityResult) {
          // Check the result, see the Using Results section
         }
     }

  // register the callback
 SonarNet(context).registerConnectivityCallback(connectivityCallback)
 

When you no longer want to receive updates on connectivity events :

// unregister the callback
 SonarNet(context).unregisterConnectivityCallback()

Using results

ConnectivityResult is provided in network notification callbacks. It has a few useful fields :

  • internetStatus value can be one of : INTERNET, NO_INTERNET, CAPTIVE_PORTAL

  • networkType value can be one of : Cellular, Wifi, Ethernet, Unknown

Network types

You may also use the following methods if you are only interested in checking the type of connected network :

connectedViaWiFi(), connectedViaCellular(), connectedViaEthernet()

Getting help

If you have questions, concerns, bug reports, etc, please file an issue in this repository's Issue Tracker.

Getting involved

You have checked this library out.

  • Did you find a bug ?
  • Did you write a patch that fixes a bug?
  • Do you intend to add a new feature or change an existing one?

We encourage you to read the instructions on how to contribute, stated in CONTRIBUTING.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.