├── .gitignore
├── .idea
├── .name
├── appInsightsSettings.xml
├── codeStyles
│ ├── Project.xml
│ └── codeStyleConfig.xml
├── compiler.xml
├── kotlinc.xml
└── misc.xml
├── Demo.gif
├── LICENSE
├── NetworkStateObserver
├── .gitignore
├── build.gradle
├── consumer-rules.pro
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ └── java
│ └── io
│ └── github
│ └── rhymezxcode
│ └── networkstateobserver
│ └── network
│ ├── CheckConnectivity.kt
│ ├── CheckNetworkConnection.kt
│ ├── CheckNetworkConnectionFlow.kt
│ ├── NetworkObserver.kt
│ ├── NetworkStateObserver.kt
│ └── Reachability.kt
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── io
│ │ └── github
│ │ └── rhymezxcode
│ │ └── app
│ │ └── ExampleInstrumentedTest.kt
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── io
│ │ │ └── github
│ │ │ └── rhymezxcode
│ │ │ └── app
│ │ │ ├── ui
│ │ │ └── NetworkStateObserverExample.kt
│ │ │ └── util
│ │ │ └── Extension.kt
│ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ └── ic_launcher_background.xml
│ │ ├── layout
│ │ └── activity_main.xml
│ │ ├── mipmap-anydpi-v26
│ │ ├── ic_launcher.xml
│ │ └── ic_launcher_round.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── values-night
│ │ └── themes.xml
│ │ ├── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── themes.xml
│ │ └── xml
│ │ ├── backup_rules.xml
│ │ └── data_extraction_rules.xml
│ └── test
│ └── java
│ └── io
│ └── github
│ └── rhymezxcode
│ └── app
│ └── ExampleUnitTest.kt
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── jitpack.yml
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.aar
4 | *.ap_
5 | *.aab
6 |
7 | # Files for the ART/Dalvik VM
8 | *.dex
9 |
10 | # Java class files
11 | *.class
12 |
13 | # Generated files
14 | bin/
15 | gen/
16 | out/
17 | # Uncomment the following line in case you need and you don't have the release build type files in your app
18 | # release/
19 |
20 | # Gradle files
21 | .gradle/
22 | build/
23 |
24 | # Local configuration file (sdk path, etc)
25 | local.properties
26 |
27 | # Proguard folder generated by Eclipse
28 | proguard/
29 |
30 | # Log Files
31 | *.log
32 |
33 | # Android Studio Navigation editor temp files
34 | .navigation/
35 |
36 | # Android Studio captures folder
37 | captures/
38 |
39 | # IntelliJ
40 | *.iml
41 | .idea/workspace.xml
42 | .idea/tasks.xml
43 | .idea/gradle.xml
44 | .idea/assetWizardSettings.xml
45 | .idea/dictionaries
46 | .idea/libraries
47 | # Android Studio 3 in .gitignore file.
48 | .idea/caches
49 | .idea/modules.xml
50 | # Comment next line if keeping position of elements in Navigation Editor is relevant for you
51 | .idea/navEditor.xml
52 |
53 | # Keystore files
54 | # Uncomment the following lines if you do not want to check your keystore files in.
55 | #*.jks
56 | #*.keystore
57 |
58 | # External native build folder generated in Android Studio 2.2 and later
59 | .externalNativeBuild
60 | .cxx/
61 |
62 | # Google Services (e.g. APIs or Firebase)
63 | # google-services.json
64 |
65 | # Freeline
66 | freeline.py
67 | freeline/
68 | freeline_project_description.json
69 |
70 | # fastlane
71 | fastlane/report.xml
72 | fastlane/Preview.html
73 | fastlane/screenshots
74 | fastlane/test_output
75 | fastlane/readme.md
76 |
77 | # Version control
78 | vcs.xml
79 |
80 | # lint
81 | lint/intermediates/
82 | lint/generated/
83 | lint/outputs/
84 | lint/tmp/
85 | # lint/reports/
86 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | NetworkStateObserverSample
--------------------------------------------------------------------------------
/.idea/appInsightsSettings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
25 |
26 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | xmlns:android
18 |
19 | ^$
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 | xmlns:.*
29 |
30 | ^$
31 |
32 |
33 | BY_NAME
34 |
35 |
36 |
37 |
38 |
39 |
40 | .*:id
41 |
42 | http://schemas.android.com/apk/res/android
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 | .*:name
52 |
53 | http://schemas.android.com/apk/res/android
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 | name
63 |
64 | ^$
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 | style
74 |
75 | ^$
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 | .*
85 |
86 | ^$
87 |
88 |
89 | BY_NAME
90 |
91 |
92 |
93 |
94 |
95 |
96 | .*
97 |
98 | http://schemas.android.com/apk/res/android
99 |
100 |
101 | ANDROID_ATTRIBUTE_ORDER
102 |
103 |
104 |
105 |
106 |
107 |
108 | .*
109 |
110 | .*
111 |
112 |
113 | BY_NAME
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
--------------------------------------------------------------------------------
/.idea/codeStyles/codeStyleConfig.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/kotlinc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Demo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RhymezxCode/NetworkStateObserver/0885c77f0af51fc359abfc2a36fcd6ee6d684d16/Demo.gif
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Awodire babajide samuel
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/NetworkStateObserver/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/NetworkStateObserver/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.library'
3 | id 'org.jetbrains.kotlin.android'
4 | id 'maven-publish'
5 | }
6 |
7 | android {
8 | namespace 'io.github.rhymezxcode.networkstateobserver'
9 | compileSdk 34
10 |
11 | defaultConfig {
12 | minSdk 21
13 | targetSdk 34
14 |
15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
16 | consumerProguardFiles "consumer-rules.pro"
17 | }
18 |
19 | buildTypes {
20 | release {
21 | minifyEnabled false
22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),
23 | 'proguard-rules.pro'
24 | }
25 | }
26 | compileOptions {
27 | sourceCompatibility JavaVersion.VERSION_17
28 | targetCompatibility JavaVersion.VERSION_17
29 | }
30 | kotlinOptions {
31 | jvmTarget = '17'
32 | }
33 |
34 | publishing {
35 | publishing {
36 | singleVariant("release") {
37 | withSourcesJar()
38 | withJavadocJar()
39 | }
40 | }
41 | }
42 |
43 | }
44 |
45 | dependencies {
46 | implementation 'androidx.lifecycle:lifecycle-livedata-core-ktx:2.7.0'
47 | implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.7.0'
48 | }
49 |
50 | publishing {
51 | publications {
52 | release(MavenPublication) {
53 | groupId = 'com.github.RhymezxCode'
54 | artifactId = 'NetworkStateObserver'
55 | version = '1.1.3'
56 |
57 | afterEvaluate {
58 | from components.release
59 | }
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/NetworkStateObserver/consumer-rules.pro:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RhymezxCode/NetworkStateObserver/0885c77f0af51fc359abfc2a36fcd6ee6d684d16/NetworkStateObserver/consumer-rules.pro
--------------------------------------------------------------------------------
/NetworkStateObserver/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/NetworkStateObserver/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/NetworkStateObserver/src/main/java/io/github/rhymezxcode/networkstateobserver/network/CheckConnectivity.kt:
--------------------------------------------------------------------------------
1 | @file:Suppress("DEPRECATION")
2 |
3 | package io.github.rhymezxcode.networkstateobserver.network
4 |
5 | import android.content.Context
6 | import android.net.ConnectivityManager
7 | import android.net.NetworkCapabilities
8 | import android.os.Build
9 |
10 | object CheckConnectivity {
11 |
12 | /**
13 | * Function to check if network is available
14 | */
15 |
16 | fun isNetworkAvailable(context: Context?): Boolean {
17 | if (context == null) return false
18 | val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as
19 | ConnectivityManager
20 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
21 | val capabilities = connectivityManager.getNetworkCapabilities(connectivityManager
22 | .activeNetwork)
23 | if (capabilities != null) {
24 | when {
25 | capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> {
26 | return true
27 | }
28 | capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> {
29 | return true
30 | }
31 | capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> {
32 | return true
33 | }
34 | }
35 | }
36 | } else {
37 | val activeNetworkInfo = connectivityManager.activeNetworkInfo
38 | if (activeNetworkInfo != null && activeNetworkInfo.isConnected) {
39 | return true
40 | }
41 | }
42 | return false
43 | }
44 |
45 | }
--------------------------------------------------------------------------------
/NetworkStateObserver/src/main/java/io/github/rhymezxcode/networkstateobserver/network/CheckNetworkConnection.kt:
--------------------------------------------------------------------------------
1 | package io.github.rhymezxcode.networkstateobserver.network
2 |
3 | import android.annotation.SuppressLint
4 | import android.app.Application
5 | import android.content.Context
6 | import android.net.ConnectivityManager
7 | import android.net.Network
8 | import android.net.NetworkRequest
9 | import android.os.Build
10 | import androidx.annotation.RequiresApi
11 | import androidx.lifecycle.LiveData
12 |
13 | class CheckNetworkConnection (private val connectivityManager: ConnectivityManager)
14 | : LiveData() {
15 | constructor(application: Application) : this(application
16 | .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager)
17 | private val networkCallback = @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
18 |
19 | object : ConnectivityManager.NetworkCallback(){
20 | override fun onUnavailable() {
21 | super.onUnavailable()
22 | postValue(false)
23 | }
24 |
25 | override fun onAvailable(network: Network) {
26 | super.onAvailable(network)
27 | postValue(true)
28 | }
29 |
30 | override fun onLost(network: Network) {
31 | super.onLost(network)
32 | postValue(false)
33 | }
34 | }
35 |
36 | @SuppressLint("MissingPermission")
37 | @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
38 | override fun onActive() {
39 | super.onActive()
40 | val builder = NetworkRequest.Builder()
41 | connectivityManager.registerNetworkCallback(builder.build(),networkCallback)
42 | }
43 |
44 | @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
45 | override fun onInactive() {
46 | super.onInactive()
47 | connectivityManager.unregisterNetworkCallback(networkCallback)
48 | }
49 |
50 | }
--------------------------------------------------------------------------------
/NetworkStateObserver/src/main/java/io/github/rhymezxcode/networkstateobserver/network/CheckNetworkConnectionFlow.kt:
--------------------------------------------------------------------------------
1 | package io.github.rhymezxcode.networkstateobserver.network
2 |
3 | import android.content.Context
4 | import android.net.ConnectivityManager
5 | import android.net.Network
6 | import android.os.Build
7 | import androidx.annotation.RequiresApi
8 | import kotlinx.coroutines.channels.awaitClose
9 | import kotlinx.coroutines.flow.Flow
10 | import kotlinx.coroutines.flow.callbackFlow
11 | import kotlinx.coroutines.flow.distinctUntilChanged
12 | import kotlinx.coroutines.launch
13 |
14 | class CheckNetworkConnectionFlow(
15 | context: Context
16 | ): NetworkObserver {
17 |
18 | private val connectivityManager =
19 | context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
20 |
21 | @RequiresApi(Build.VERSION_CODES.N)
22 | override fun observe(): Flow {
23 | return callbackFlow {
24 | val callback = object : ConnectivityManager.NetworkCallback() {
25 | override fun onAvailable(network: Network) {
26 | super.onAvailable(network)
27 | launch { send(NetworkObserver.Status.Available) }
28 | }
29 |
30 | override fun onLosing(network: Network, maxMsToLive: Int) {
31 | super.onLosing(network, maxMsToLive)
32 | launch { send(NetworkObserver.Status.Losing) }
33 | }
34 |
35 | override fun onLost(network: Network) {
36 | super.onLost(network)
37 | launch { send(NetworkObserver.Status.Lost) }
38 | }
39 |
40 | override fun onUnavailable() {
41 | super.onUnavailable()
42 | launch { send(NetworkObserver.Status.Unavailable) }
43 | }
44 | }
45 |
46 | connectivityManager.registerDefaultNetworkCallback(callback)
47 | awaitClose {
48 | connectivityManager.unregisterNetworkCallback(callback)
49 | }
50 | }.distinctUntilChanged()
51 | }
52 | }
--------------------------------------------------------------------------------
/NetworkStateObserver/src/main/java/io/github/rhymezxcode/networkstateobserver/network/NetworkObserver.kt:
--------------------------------------------------------------------------------
1 | package io.github.rhymezxcode.networkstateobserver.network
2 |
3 | import kotlinx.coroutines.flow.Flow
4 |
5 | interface NetworkObserver {
6 | fun observe(): Flow
7 | enum class Status {
8 | Available, Unavailable, Losing, Lost
9 | }
10 | }
--------------------------------------------------------------------------------
/NetworkStateObserver/src/main/java/io/github/rhymezxcode/networkstateobserver/network/NetworkStateObserver.kt:
--------------------------------------------------------------------------------
1 | package io.github.rhymezxcode.networkstateobserver.network
2 |
3 | import android.app.Activity
4 |
5 | class NetworkStateObserver(
6 | private val activity: Activity?,
7 | ) {
8 |
9 | fun callNetworkConnection(): CheckNetworkConnection{
10 | return CheckNetworkConnection(activity?.application!!)
11 | }
12 |
13 | fun callNetworkConnectionFlow(): CheckNetworkConnectionFlow{
14 | return CheckNetworkConnectionFlow(activity?.application!!)
15 | }
16 |
17 | private constructor(builder: Builder) : this(
18 | builder.activity
19 | )
20 |
21 | class Builder {
22 |
23 | var activity: Activity? = null
24 | private set
25 |
26 | fun activity(activity: Activity) = apply { this.activity = activity }
27 |
28 | fun build() = NetworkStateObserver(this)
29 | }
30 |
31 | }
--------------------------------------------------------------------------------
/NetworkStateObserver/src/main/java/io/github/rhymezxcode/networkstateobserver/network/Reachability.kt:
--------------------------------------------------------------------------------
1 | package io.github.rhymezxcode.networkstateobserver.network
2 |
3 | import android.content.Context
4 | import android.util.Log
5 | import kotlinx.coroutines.Dispatchers
6 | import kotlinx.coroutines.flow.Flow
7 | import kotlinx.coroutines.flow.catch
8 | import kotlinx.coroutines.flow.flow
9 | import kotlinx.coroutines.flow.flowOn
10 | import java.net.HttpURLConnection
11 | import java.net.URL
12 |
13 | object Reachability {
14 | fun hasInternetConnected(
15 | context: Context,
16 | reachUrl: String? = null,
17 | ): Boolean {
18 | var state: Boolean
19 | if (CheckConnectivity.isNetworkAvailable(context)) {
20 | try {
21 | val connection = URL(reachUrl ?: "https://www.google.com")
22 | .openConnection() as
23 | HttpURLConnection
24 | connection.setRequestProperty("User-Agent", "Test")
25 | connection.setRequestProperty("Connection", "close")
26 | connection.connectTimeout = 1500 // configurable
27 | connection.connect()
28 | Log.d(
29 | "NetworkStateObserver", "hasInternetConnected: " +
30 | "${(connection.responseCode == 200)}"
31 | )
32 | state = (connection.responseCode == 200)
33 | } catch (e: Exception) {
34 | state = false
35 | Log.e(
36 | "NetworkStateObserver",
37 | "Error checking internet connection", e
38 | )
39 | }
40 | } else {
41 | state = false
42 | Log.w("NetworkStateObserver", "No network available!")
43 | }
44 |
45 | return state
46 | }
47 |
48 | fun hasServerConnected(
49 | context: Context,
50 | serverUrl: String
51 | ): Boolean {
52 | var state: Boolean
53 | if (CheckConnectivity.isNetworkAvailable(context)) {
54 | try {
55 | val connection = URL(serverUrl)
56 | .openConnection() as
57 | HttpURLConnection
58 | connection.connectTimeout = 1500 // configurable
59 | connection.connect()
60 | Log.d(
61 | "NetworkStateObserver", "hasServerConnected: " +
62 | "${(connection.responseCode == 200)}"
63 | )
64 | state = (connection.responseCode == 200)
65 | } catch (e: Exception) {
66 | state = false
67 | Log.e(
68 | "NetworkStateObserver",
69 | "Error checking your server connection", e
70 | )
71 | }
72 | } else {
73 | state = false
74 | Log.e(
75 | "NetworkStateObserver",
76 | "No, Internet connection to check server connection!",
77 | )
78 | }
79 | return state
80 | }
81 |
82 | fun hasInternetConnectedFlow(
83 | context: Context,
84 | reachUrl: String? = null,
85 | ): Flow = flow {
86 | if (CheckConnectivity.isNetworkAvailable(context)) {
87 | val connection = URL(reachUrl ?: "https://www.google.com")
88 | .openConnection() as
89 | HttpURLConnection
90 | connection.connectTimeout = 1500 // configurable
91 | connection.connect()
92 | Log.d(
93 | "NetworkStateObserver", "hasConnected: " +
94 | "${(connection.responseCode == 200)}"
95 | )
96 | emit((connection.responseCode == 200))
97 | } else {
98 | emit(false)
99 | Log.e(
100 | "NetworkStateObserver",
101 | "No, Internet connection to check connection!",
102 | )
103 | }
104 | }.catch { e ->
105 | emit(false)
106 | Log.e(
107 | "NetworkStateObserver",
108 | "Error checking your connection: \n" + e.localizedMessage
109 | )
110 | }.flowOn(Dispatchers.IO)
111 |
112 |
113 | fun hasServerConnectedFlow(
114 | context: Context,
115 | serverUrl: String
116 | ): Flow = flow {
117 | if (CheckConnectivity.isNetworkAvailable(context)) {
118 | val connection = URL(serverUrl)
119 | .openConnection() as
120 | HttpURLConnection
121 | connection.connectTimeout = 1500 // configurable
122 | connection.connect()
123 | Log.d(
124 | "NetworkStateObserver", "hasServerConnected: " +
125 | "${(connection.responseCode == 200)}"
126 | )
127 | emit((connection.responseCode == 200))
128 | } else {
129 | emit(false)
130 | Log.e(
131 | "NetworkStateObserver",
132 | "No, Internet connection to check your server connection!",
133 | )
134 | }
135 | }.catch { e ->
136 | emit(false)
137 | Log.e(
138 | "NetworkStateObserver",
139 | "Error checking your connection: \n" + e.localizedMessage
140 | )
141 | }.flowOn(Dispatchers.IO)
142 |
143 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
29 |
30 |
31 | ## NetworkStateObserver Android Library
32 | A library that helps you check the state of your network, if it is either available, lost, unavailable and also check the reach-ability of your network when your server is either down or your ISP is connected but no data subscription.
33 | ## Guide:
34 |
35 | **Dev.to:**
36 | [Using NetworkStateObserver in Large Projects: A Comprehensive Guide](https://dev.to/rhymezxcode/using-networkstateobserver-in-large-projects-a-comprehensive-guide-3bo9)
37 |
38 | **Medium:**
39 | [Using NetworkStateObserver in Large Projects: A Comprehensive Guide](https://rhymezxcode.medium.com/using-networkstateobserver-in-large-projects-a-comprehensive-guide-b41e61ab64dc)
40 |
41 | Demo:
42 |
43 |
44 | 
45 |
46 | ### 1. Adding NetworkStateObserver to your project
47 |
48 | * Include jitpack in your root `settings.gradle` file.
49 |
50 | ```gradle
51 | pluginManagement {
52 | repositories {
53 | ...
54 | maven { url 'https://jitpack.io' }
55 | }
56 | }
57 |
58 | dependencyResolutionManagement {
59 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
60 | repositories {
61 | ...
62 | maven { url 'https://jitpack.io' }
63 | }
64 | }
65 | ```
66 |
67 | * And add it's dependency to your app level `build.gradle` file:
68 |
69 | ```gradle
70 | dependencies {
71 | implementation 'com.github.RhymezxCode:NetworkStateObserver:1.1.3'
72 |
73 | //Livedata
74 | implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0'
75 | implementation 'androidx.lifecycle:lifecycle-livedata-core-ktx:2.7.0'
76 | implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.7.0'
77 | implementation 'android.arch.lifecycle:extensions:1.1.1'
78 | }
79 | ```
80 |
81 | #### Sync your project, and :scream: boom :fire: you have added NetworkStateObserver successfully. :exclamation:
82 |
83 | ### 2. Usage
84 |
85 | * First initialize the builder class:
86 |
87 | ```kt
88 | val network = NetworkStateObserver.Builder()
89 | .activity(activity = this@NetworkStateObserverExample)
90 | .build()
91 | ```
92 |
93 | * If you just want to check for connectivity, before performing a task or job():
94 |
95 | ```kt
96 | if(CheckConnectivity.isNetworkAvailable(requireContext())){
97 | showToast(
98 | this@NetworkStateObserverExample,
99 | "Network restored"
100 | )
101 | }
102 | ```
103 |
104 | * Use the live-data method to determine your network state, and replace the code in the lifecycleScope.launchWhenStarted { ....your code here } to do what you want:
105 |
106 | ```kt
107 | network.callNetworkConnection().observe(this) { isConnected ->
108 | lifecycleScope.launch(Dispatchers.IO) {
109 | if (isConnected) {
110 | when {
111 | Reachability.hasServerConnected(
112 | context = this@NetworkStateObserverExample,
113 | serverUrl = "https://www.your-server-url.com"
114 | ) -> lifecycleScope.launch{
115 | showToast(
116 | "Server url works"
117 | )
118 | }
119 |
120 | Reachability.hasInternetConnected(
121 | context = this@NetworkStateObserverExample
122 | ) -> lifecycleScope.launch{
123 | showToast(
124 | "Network restored"
125 | )
126 | }
127 |
128 | else -> lifecycleScope.launch{
129 | showToast(
130 | "Network is lost or issues with server"
131 | )
132 | }
133 | }
134 | } else {
135 | //check for lost connection
136 | lifecycleScope.launch{
137 | showToast(
138 | "No Network connection"
139 | )
140 | }
141 | }
142 |
143 | }
144 |
145 | }
146 | }
147 | ```
148 |
149 | * Use the flow method to determine your network state, and also retry when an exception is thrown:
150 |
151 | ```kt
152 | lifecycleScope.launch {
153 | network.callNetworkConnectionFlow()
154 | .observe()
155 | .collect {
156 | when (it) {
157 | NetworkObserver.Status.Available -> {
158 | lifecycleScope.launch {
159 | when {
160 | Reachability.hasServerConnectedFlow(
161 | context = this@NetworkStateObserverExample,
162 | serverUrl = "https://www.github.com"
163 | ).retryWhen { cause, attempt ->
164 | if (cause is IOException && attempt < 3) {
165 | delay(2000)
166 | return@retryWhen true
167 | } else {
168 | return@retryWhen false
169 | }
170 | }.buffer().first() -> lifecycleScope.launch {
171 | showToast(
172 | this@NetworkStateObserverExample,
173 | "Server url works"
174 | )
175 | }
176 |
177 | Reachability.hasInternetConnectedFlow(
178 | context = this@NetworkStateObserverExample
179 | ).retryWhen { cause, attempt ->
180 | if (cause is IOException && attempt < 3) {
181 | delay(2000)
182 | return@retryWhen true
183 | } else {
184 | return@retryWhen false
185 | }
186 | }.buffer().first() -> lifecycleScope.launch {
187 | showToast(
188 | this@NetworkStateObserverExample,
189 | "Network restored"
190 | )
191 | }
192 |
193 | else -> lifecycleScope.launch {
194 | showToast(
195 | this@NetworkStateObserverExample,
196 | "Network is lost or issues with server"
197 | )
198 | }
199 | }
200 | }
201 | }
202 |
203 | NetworkObserver.Status.Unavailable -> {
204 | showToast(
205 | "Network is unavailable!"
206 | )
207 | }
208 |
209 | NetworkObserver.Status.Losing -> {
210 | showToast(
211 | "You are losing your network!"
212 | )
213 | }
214 |
215 | NetworkObserver.Status.Lost -> {
216 | showToast(
217 | "Network is lost!"
218 | )
219 | }
220 | }
221 | }
222 | }
223 | ```
224 |
225 | * You can check if your internet connection is stable only, if you don't have a server url:
226 |
227 | ```kt
228 | network.callNetworkConnection().observe(this) { isConnected ->
229 | lifecycleScope.launch(Dispatchers.IO) {
230 | if (isConnected) {
231 | when {
232 |
233 | Reachability.hasInternetConnected(
234 | context = this@NetworkStateObserverExample
235 | ) -> lifecycleScope.launchW{
236 | showToast(
237 | "Network restored"
238 | )
239 | }
240 |
241 | else -> lifecycleScope.launch{
242 | showToast(
243 | "Network is lost or issues with server"
244 | )
245 | }
246 | }
247 | } else {
248 | //check for lost connection
249 | lifecycleScope.launch{
250 | showToast(
251 | "No Network connection"
252 | )
253 | }
254 | }
255 |
256 | }
257 |
258 | }
259 | }
260 | ```
261 |
262 | ### 3. You can also inject NetworkStateObserver, and use it everywhere in your app with Hilt :syringe: :
263 |
264 | * Create an object for the NetworkStateModule in your di package:
265 |
266 | ```kt
267 | @Module
268 | @InstallIn(ActivityComponent::class)
269 | object NetworkStateModule {
270 | @Provides
271 | fun provideNetworkStateObserver(
272 | activity: Activity
273 | ): NetworkStateObserver {
274 | return NetworkStateObserver.Builder()
275 | .activity(activity = activity)
276 | .build()
277 | }
278 | }
279 | ```
280 |
281 | * Declare the variable in your class either a fragment or activity, it works in both:
282 |
283 | ```kt
284 | @AndroidEntryPoint
285 | class myFragment : Fragment(){
286 | @Inject
287 | lateinit var network: NetworkStateObserver
288 |
289 | private fun callNetworkConnection() {
290 | network.callNetworkConnection().observe(this) { isConnected ->
291 | lifecycleScope.launch(Dispatchers.IO) {
292 | if (isConnected) {
293 | when {
294 | Reachability.hasInternetConnected(
295 | context = this@NetworkStateObserverExample
296 | ) -> lifecycleScope.launch{
297 | showToast(
298 | "Network restored"
299 | )
300 | }
301 |
302 | else -> lifecycleScope.launch{
303 | showToast(
304 | "Network is lost or issues with server"
305 | )
306 | }
307 | }
308 | } else {
309 | //check for lost connection
310 | lifecycleScope.launch{
311 | showToast(
312 | "No Network connection"
313 | )
314 | }
315 | }
316 |
317 | }
318 |
319 | }
320 | }
321 | }
322 |
323 | override fun onResume() {
324 | super.onResume()
325 | callNetworkConnection()
326 | }
327 |
328 | }
329 | ```
330 |
331 | * Add the method in onResume() of your fragment or activity to have a great experience:
332 |
333 | ```kt
334 | override fun onResume() {
335 | super.onResume()
336 | callNetworkConnection()
337 | }
338 | ```
339 |
340 | :pushpin: Please, feel free to give me a star :star2:, I also love sparkles :sparkles: :relaxed:
341 |
346 |
347 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.application'
3 | id 'org.jetbrains.kotlin.android'
4 | }
5 |
6 | android {
7 | namespace 'io.github.rhymezxcode.app'
8 | compileSdk 34
9 |
10 | defaultConfig {
11 | applicationId "io.github.rhymezxcode.app"
12 | minSdk 21
13 | targetSdk 34
14 | versionCode 1
15 | versionName "1.0"
16 |
17 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
18 | }
19 | buildTypes {
20 | release {
21 | minifyEnabled false
22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23 | }
24 | }
25 | compileOptions {
26 | sourceCompatibility JavaVersion.VERSION_17
27 | targetCompatibility JavaVersion.VERSION_17
28 | }
29 | kotlinOptions {
30 | jvmTarget = '17'
31 | }
32 | }
33 |
34 | dependencies {
35 | implementation project(":NetworkStateObserver")
36 | // implementation 'com.github.RhymezxCode:NetworkStateObserver:1.0.1'
37 |
38 | implementation 'androidx.core:core-ktx:1.12.0'
39 | implementation 'androidx.appcompat:appcompat:1.6.1'
40 | implementation 'com.google.android.material:material:1.11.0'
41 | implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
42 | testImplementation 'junit:junit:4.13.2'
43 | androidTestImplementation 'androidx.test.ext:junit:1.1.5'
44 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
45 |
46 | //Livedata
47 | implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0'
48 | implementation 'androidx.lifecycle:lifecycle-livedata-core-ktx:2.7.0'
49 | implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.7.0'
50 | implementation 'android.arch.lifecycle:extensions:1.1.1'
51 | }
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/app/src/androidTest/java/io/github/rhymezxcode/app/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.rhymezxcode.app
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("io.github.rhymezxcode.app", appContext.packageName)
23 | }
24 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
15 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/app/src/main/java/io/github/rhymezxcode/app/ui/NetworkStateObserverExample.kt:
--------------------------------------------------------------------------------
1 | package io.github.rhymezxcode.app.ui
2 |
3 | import android.annotation.SuppressLint
4 | import android.os.Build
5 | import android.os.Bundle
6 | import androidx.annotation.RequiresApi
7 | import androidx.appcompat.app.AppCompatActivity
8 | import androidx.lifecycle.lifecycleScope
9 | import io.github.rhymezxcode.app.R
10 | import io.github.rhymezxcode.app.util.showToast
11 | import io.github.rhymezxcode.networkstateobserver.network.NetworkObserver
12 | import io.github.rhymezxcode.networkstateobserver.network.NetworkStateObserver
13 | import io.github.rhymezxcode.networkstateobserver.network.Reachability
14 | import kotlinx.coroutines.Dispatchers
15 | import kotlinx.coroutines.delay
16 | import kotlinx.coroutines.flow.buffer
17 | import kotlinx.coroutines.flow.catch
18 | import kotlinx.coroutines.flow.collect
19 | import kotlinx.coroutines.flow.first
20 | import kotlinx.coroutines.flow.retryWhen
21 | import kotlinx.coroutines.launch
22 | import java.io.IOException
23 |
24 |
25 | class NetworkStateObserverExample : AppCompatActivity() {
26 | @SuppressLint("NewApi")
27 | override fun onCreate(savedInstanceState: Bundle?) {
28 | super.onCreate(savedInstanceState)
29 | setContentView(R.layout.activity_main)
30 |
31 | val network = NetworkStateObserver.Builder()
32 | .activity(activity = this@NetworkStateObserverExample)
33 | .build()
34 |
35 | lifecycleScope.launch {
36 | network.callNetworkConnectionFlow()
37 | .observe()
38 | .collect {
39 | when (it) {
40 | NetworkObserver.Status.Available -> {
41 | lifecycleScope.launch {
42 | when {
43 | Reachability.hasServerConnectedFlow(
44 | context = this@NetworkStateObserverExample,
45 | serverUrl = "https://www.github.com"
46 | ).retryWhen { cause, attempt ->
47 | if (cause is IOException && attempt < 3) {
48 | delay(2000)
49 | return@retryWhen true
50 | } else {
51 | return@retryWhen false
52 | }
53 | }.buffer().first() -> lifecycleScope.launch {
54 | showToast(
55 | "Server url works"
56 | )
57 | }
58 |
59 | Reachability.hasInternetConnectedFlow(
60 | context = this@NetworkStateObserverExample
61 | ).retryWhen { cause, attempt ->
62 | if (cause is IOException && attempt < 3) {
63 | delay(2000)
64 | return@retryWhen true
65 | } else {
66 | return@retryWhen false
67 | }
68 | }.buffer().first() -> lifecycleScope.launch {
69 | showToast(
70 | "Network restored"
71 | )
72 | }
73 |
74 | else -> lifecycleScope.launch {
75 | showToast(
76 | "Network is lost or issues with server"
77 | )
78 | }
79 | }
80 | }
81 | }
82 |
83 | NetworkObserver.Status.Unavailable -> {
84 | showToast(
85 | "Network is unavailable!"
86 | )
87 | }
88 |
89 | NetworkObserver.Status.Losing -> {
90 | showToast(
91 | "You are losing your network!"
92 | )
93 | }
94 |
95 | NetworkObserver.Status.Lost -> {
96 | showToast(
97 | "Network is lost!"
98 | )
99 | }
100 | }
101 | }
102 | }
103 |
104 | // network.callNetworkConnection().observe(this) { isConnected ->
105 | // lifecycleScope.launch(Dispatchers.IO) {
106 | // if (isConnected) {
107 | // when {
108 | // Reachability.hasServerConnected(
109 | // context = this@NetworkStateObserverExample,
110 | // serverUrl = "https://www.github.com"
111 | // ) -> lifecycleScope.launchWhenStarted {
112 | // showToast(
113 | // this@NetworkStateObserverExample,
114 | // "Server url works"
115 | // )
116 | // }
117 | //
118 | // Reachability.hasInternetConnected(
119 | // context = this@NetworkStateObserverExample
120 | // ) -> lifecycleScope.launchWhenStarted {
121 | // showToast(
122 | // this@NetworkStateObserverExample,
123 | // "Network restored"
124 | // )
125 | // }
126 | //
127 | // else -> lifecycleScope.launchWhenStarted {
128 | // showToast(
129 | // this@NetworkStateObserverExample,
130 | // "Network is lost or issues with server"
131 | // )
132 | // }
133 | // }
134 | // } else {
135 | // //check for lost connection
136 | // lifecycleScope.launchWhenStarted {
137 | // showToast(
138 | // this@NetworkStateObserverExample,
139 | // "No Network connection"
140 | // )
141 | // }
142 | // }
143 | //
144 | // }
145 | // }
146 | }
147 |
148 |
149 | }
--------------------------------------------------------------------------------
/app/src/main/java/io/github/rhymezxcode/app/util/Extension.kt:
--------------------------------------------------------------------------------
1 | package io.github.rhymezxcode.app.util
2 |
3 | import android.content.Context
4 | import android.widget.Toast
5 |
6 | fun Context.showToast(message: String) {
7 | Toast.makeText(this, message, Toast.LENGTH_LONG)
8 | .show()
9 | }
10 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
17 |
18 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RhymezxCode/NetworkStateObserver/0885c77f0af51fc359abfc2a36fcd6ee6d684d16/app/src/main/res/mipmap-hdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RhymezxCode/NetworkStateObserver/0885c77f0af51fc359abfc2a36fcd6ee6d684d16/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RhymezxCode/NetworkStateObserver/0885c77f0af51fc359abfc2a36fcd6ee6d684d16/app/src/main/res/mipmap-mdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RhymezxCode/NetworkStateObserver/0885c77f0af51fc359abfc2a36fcd6ee6d684d16/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RhymezxCode/NetworkStateObserver/0885c77f0af51fc359abfc2a36fcd6ee6d684d16/app/src/main/res/mipmap-xhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RhymezxCode/NetworkStateObserver/0885c77f0af51fc359abfc2a36fcd6ee6d684d16/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RhymezxCode/NetworkStateObserver/0885c77f0af51fc359abfc2a36fcd6ee6d684d16/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RhymezxCode/NetworkStateObserver/0885c77f0af51fc359abfc2a36fcd6ee6d684d16/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RhymezxCode/NetworkStateObserver/0885c77f0af51fc359abfc2a36fcd6ee6d684d16/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RhymezxCode/NetworkStateObserver/0885c77f0af51fc359abfc2a36fcd6ee6d684d16/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/values-night/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FFBB86FC
4 | #FF6200EE
5 | #FF3700B3
6 | #FF03DAC5
7 | #FF018786
8 | #FF000000
9 | #FFFFFFFF
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | App
3 |
--------------------------------------------------------------------------------
/app/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/xml/backup_rules.xml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/xml/data_extraction_rules.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
12 |
13 |
19 |
--------------------------------------------------------------------------------
/app/src/test/java/io/github/rhymezxcode/app/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package io.github.rhymezxcode.app
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 | plugins {
3 | id 'com.android.application' version '8.2.0' apply false
4 | id 'com.android.library' version '8.2.0' apply false
5 | id 'org.jetbrains.kotlin.android' version '1.9.20' apply false
6 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | ## For more details on how to configure your build environment visit
2 | # http://www.gradle.org/docs/current/userguide/build_environment.html
3 | #
4 | # Specifies the JVM arguments used for the daemon process.
5 | # The setting is particularly useful for tweaking memory settings.
6 | # Default value: -Xmx1024m -XX:MaxPermSize=256m
7 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
8 | #
9 | # When configured, Gradle will run in incubating parallel mode.
10 | # This option should only be used with decoupled projects. More details, visit
11 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
12 | # org.gradle.parallel=true
13 | #Mon Nov 28 04:49:27 WAT 2022
14 | #android.defaults.buildfeatures.buildconfig=true
15 | android.enableJetifier=true
16 | android.nonFinalResIds=false
17 | android.nonTransitiveRClass=true
18 | android.useAndroidX=true
19 | kotlin.code.style=official
20 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding\=UTF-8
21 |
22 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RhymezxCode/NetworkStateObserver/0885c77f0af51fc359abfc2a36fcd6ee6d684d16/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Feb 29 15:13:55 WAT 2024
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
5 | zipStoreBase=GRADLE_USER_HOME
6 | zipStorePath=wrapper/dists
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/jitpack.yml:
--------------------------------------------------------------------------------
1 | jdk:
2 | - openjdk17
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | pluginManagement {
2 | repositories {
3 | google()
4 | mavenCentral()
5 | gradlePluginPortal()
6 | maven { url 'https://jitpack.io' }
7 | }
8 | }
9 | dependencyResolutionManagement {
10 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
11 | repositories {
12 | google()
13 | mavenCentral()
14 | maven { url 'https://jitpack.io' }
15 | }
16 | }
17 | rootProject.name = "NetworkStateObserverSample"
18 | include ':app'
19 | include ':NetworkStateObserver'
20 |
21 |
--------------------------------------------------------------------------------