├── .gitignore ├── .idea ├── .gitignore ├── gradle.xml ├── migrations.xml ├── misc.xml └── runConfigurations.xml ├── app ├── .gitignore ├── build.gradle.kts ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── plcoding │ │ └── internetconnectionobserver │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── plcoding │ │ │ └── internetconnectionobserver │ │ │ ├── AndroidConnectivityObserver.kt │ │ │ ├── ConnectivityObserver.kt │ │ │ ├── ConnectivityViewModel.kt │ │ │ ├── MainActivity.kt │ │ │ └── ui │ │ │ └── theme │ │ │ ├── Color.kt │ │ │ ├── Theme.kt │ │ │ └── Type.kt │ └── res │ │ ├── drawable │ │ ├── ic_launcher_background.xml │ │ └── ic_launcher_foreground.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 │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── themes.xml │ │ └── xml │ │ ├── backup_rules.xml │ │ └── data_extraction_rules.xml │ └── test │ └── java │ └── com │ └── plcoding │ └── internetconnectionobserver │ └── ExampleUnitTest.kt ├── build.gradle.kts ├── gradle.properties ├── gradle ├── libs.versions.toml └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle.kts /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | local.properties 16 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/migrations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 336 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 17 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | alias(libs.plugins.android.application) 3 | alias(libs.plugins.kotlin.android) 4 | alias(libs.plugins.kotlin.compose) 5 | } 6 | 7 | android { 8 | namespace = "com.plcoding.internetconnectionobserver" 9 | compileSdk = 35 10 | 11 | defaultConfig { 12 | applicationId = "com.plcoding.internetconnectionobserver" 13 | minSdk = 24 14 | targetSdk = 35 15 | versionCode = 1 16 | versionName = "1.0" 17 | 18 | testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" 19 | } 20 | 21 | buildTypes { 22 | release { 23 | isMinifyEnabled = false 24 | proguardFiles( 25 | getDefaultProguardFile("proguard-android-optimize.txt"), 26 | "proguard-rules.pro" 27 | ) 28 | } 29 | } 30 | compileOptions { 31 | sourceCompatibility = JavaVersion.VERSION_11 32 | targetCompatibility = JavaVersion.VERSION_11 33 | } 34 | kotlinOptions { 35 | jvmTarget = "11" 36 | } 37 | buildFeatures { 38 | compose = true 39 | } 40 | } 41 | 42 | dependencies { 43 | 44 | implementation(libs.androidx.lifecycle.runtime.compose) 45 | implementation(libs.androidx.lifecycle.viewmodel.compose) 46 | 47 | implementation(libs.androidx.core.ktx) 48 | implementation(libs.androidx.lifecycle.runtime.ktx) 49 | implementation(libs.androidx.activity.compose) 50 | implementation(platform(libs.androidx.compose.bom)) 51 | implementation(libs.androidx.ui) 52 | implementation(libs.androidx.ui.graphics) 53 | implementation(libs.androidx.ui.tooling.preview) 54 | implementation(libs.androidx.material3) 55 | testImplementation(libs.junit) 56 | androidTestImplementation(libs.androidx.junit) 57 | androidTestImplementation(libs.androidx.espresso.core) 58 | androidTestImplementation(platform(libs.androidx.compose.bom)) 59 | androidTestImplementation(libs.androidx.ui.test.junit4) 60 | debugImplementation(libs.androidx.ui.tooling) 61 | debugImplementation(libs.androidx.ui.test.manifest) 62 | } -------------------------------------------------------------------------------- /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/com/plcoding/internetconnectionobserver/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.plcoding.internetconnectionobserver 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("com.plcoding.internetconnectionobserver", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 17 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/plcoding/internetconnectionobserver/AndroidConnectivityObserver.kt: -------------------------------------------------------------------------------- 1 | package com.plcoding.internetconnectionobserver 2 | 3 | import android.content.Context 4 | import android.net.ConnectivityManager 5 | import android.net.ConnectivityManager.NetworkCallback 6 | import android.net.Network 7 | import android.net.NetworkCapabilities 8 | import androidx.core.content.getSystemService 9 | import kotlinx.coroutines.channels.awaitClose 10 | import kotlinx.coroutines.flow.Flow 11 | import kotlinx.coroutines.flow.callbackFlow 12 | 13 | class AndroidConnectivityObserver( 14 | private val context: Context 15 | ): ConnectivityObserver { 16 | 17 | private val connectivityManager = context.getSystemService()!! 18 | 19 | override val isConnected: Flow 20 | get() = callbackFlow { 21 | val callback = object : NetworkCallback() { 22 | override fun onCapabilitiesChanged( 23 | network: Network, 24 | networkCapabilities: NetworkCapabilities 25 | ) { 26 | super.onCapabilitiesChanged(network, networkCapabilities) 27 | val connected = networkCapabilities.hasCapability( 28 | NetworkCapabilities.NET_CAPABILITY_VALIDATED 29 | ) 30 | trySend(connected) 31 | } 32 | 33 | override fun onUnavailable() { 34 | super.onUnavailable() 35 | trySend(false) 36 | } 37 | 38 | override fun onLost(network: Network) { 39 | super.onLost(network) 40 | trySend(false) 41 | } 42 | 43 | override fun onAvailable(network: Network) { 44 | super.onAvailable(network) 45 | trySend(true) 46 | } 47 | } 48 | 49 | connectivityManager.registerDefaultNetworkCallback(callback) 50 | 51 | awaitClose { 52 | connectivityManager.unregisterNetworkCallback(callback) 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /app/src/main/java/com/plcoding/internetconnectionobserver/ConnectivityObserver.kt: -------------------------------------------------------------------------------- 1 | package com.plcoding.internetconnectionobserver 2 | 3 | import kotlinx.coroutines.flow.Flow 4 | 5 | interface ConnectivityObserver { 6 | val isConnected: Flow 7 | } -------------------------------------------------------------------------------- /app/src/main/java/com/plcoding/internetconnectionobserver/ConnectivityViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.plcoding.internetconnectionobserver 2 | 3 | import androidx.lifecycle.ViewModel 4 | import androidx.lifecycle.viewModelScope 5 | import kotlinx.coroutines.flow.SharingStarted 6 | import kotlinx.coroutines.flow.stateIn 7 | 8 | class ConnectivityViewModel( 9 | private val connectivityObserver: ConnectivityObserver 10 | ): ViewModel() { 11 | 12 | val isConnected = connectivityObserver 13 | .isConnected 14 | .stateIn( 15 | viewModelScope, 16 | SharingStarted.WhileSubscribed(5000L), 17 | false 18 | ) 19 | } -------------------------------------------------------------------------------- /app/src/main/java/com/plcoding/internetconnectionobserver/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.plcoding.internetconnectionobserver 2 | 3 | import android.os.Bundle 4 | import androidx.activity.ComponentActivity 5 | import androidx.activity.compose.setContent 6 | import androidx.activity.enableEdgeToEdge 7 | import androidx.activity.viewModels 8 | import androidx.compose.foundation.layout.Box 9 | import androidx.compose.foundation.layout.fillMaxSize 10 | import androidx.compose.foundation.layout.padding 11 | import androidx.compose.material3.Scaffold 12 | import androidx.compose.material3.Text 13 | import androidx.compose.runtime.Composable 14 | import androidx.compose.runtime.collectAsState 15 | import androidx.compose.runtime.getValue 16 | import androidx.compose.ui.Alignment 17 | import androidx.compose.ui.Modifier 18 | import androidx.compose.ui.tooling.preview.Preview 19 | import androidx.lifecycle.compose.collectAsStateWithLifecycle 20 | import androidx.lifecycle.viewmodel.compose.viewModel 21 | import androidx.lifecycle.viewmodel.viewModelFactory 22 | import com.plcoding.internetconnectionobserver.ui.theme.InternetConnectionObserverTheme 23 | 24 | class MainActivity : ComponentActivity() { 25 | 26 | override fun onCreate(savedInstanceState: Bundle?) { 27 | super.onCreate(savedInstanceState) 28 | enableEdgeToEdge() 29 | setContent { 30 | InternetConnectionObserverTheme { 31 | val viewModel = viewModel { 32 | ConnectivityViewModel( 33 | connectivityObserver = AndroidConnectivityObserver( 34 | context = applicationContext 35 | ) 36 | ) 37 | } 38 | val isConnected by viewModel.isConnected.collectAsStateWithLifecycle() 39 | Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> 40 | Box( 41 | modifier = Modifier 42 | .fillMaxSize() 43 | .padding(innerPadding), 44 | contentAlignment = Alignment.Center 45 | ) { 46 | Text( 47 | text = "Connected? $isConnected" 48 | ) 49 | } 50 | } 51 | } 52 | } 53 | } 54 | } 55 | 56 | @Composable 57 | fun Greeting(name: String, modifier: Modifier = Modifier) { 58 | Text( 59 | text = "Hello $name!", 60 | modifier = modifier 61 | ) 62 | } 63 | 64 | @Preview(showBackground = true) 65 | @Composable 66 | fun GreetingPreview() { 67 | InternetConnectionObserverTheme { 68 | Greeting("Android") 69 | } 70 | } -------------------------------------------------------------------------------- /app/src/main/java/com/plcoding/internetconnectionobserver/ui/theme/Color.kt: -------------------------------------------------------------------------------- 1 | package com.plcoding.internetconnectionobserver.ui.theme 2 | 3 | import androidx.compose.ui.graphics.Color 4 | 5 | val Purple80 = Color(0xFFD0BCFF) 6 | val PurpleGrey80 = Color(0xFFCCC2DC) 7 | val Pink80 = Color(0xFFEFB8C8) 8 | 9 | val Purple40 = Color(0xFF6650a4) 10 | val PurpleGrey40 = Color(0xFF625b71) 11 | val Pink40 = Color(0xFF7D5260) -------------------------------------------------------------------------------- /app/src/main/java/com/plcoding/internetconnectionobserver/ui/theme/Theme.kt: -------------------------------------------------------------------------------- 1 | package com.plcoding.internetconnectionobserver.ui.theme 2 | 3 | import android.app.Activity 4 | import android.os.Build 5 | import androidx.compose.foundation.isSystemInDarkTheme 6 | import androidx.compose.material3.MaterialTheme 7 | import androidx.compose.material3.darkColorScheme 8 | import androidx.compose.material3.dynamicDarkColorScheme 9 | import androidx.compose.material3.dynamicLightColorScheme 10 | import androidx.compose.material3.lightColorScheme 11 | import androidx.compose.runtime.Composable 12 | import androidx.compose.ui.platform.LocalContext 13 | 14 | private val DarkColorScheme = darkColorScheme( 15 | primary = Purple80, 16 | secondary = PurpleGrey80, 17 | tertiary = Pink80 18 | ) 19 | 20 | private val LightColorScheme = lightColorScheme( 21 | primary = Purple40, 22 | secondary = PurpleGrey40, 23 | tertiary = Pink40 24 | 25 | /* Other default colors to override 26 | background = Color(0xFFFFFBFE), 27 | surface = Color(0xFFFFFBFE), 28 | onPrimary = Color.White, 29 | onSecondary = Color.White, 30 | onTertiary = Color.White, 31 | onBackground = Color(0xFF1C1B1F), 32 | onSurface = Color(0xFF1C1B1F), 33 | */ 34 | ) 35 | 36 | @Composable 37 | fun InternetConnectionObserverTheme( 38 | darkTheme: Boolean = isSystemInDarkTheme(), 39 | // Dynamic color is available on Android 12+ 40 | dynamicColor: Boolean = true, 41 | content: @Composable () -> Unit 42 | ) { 43 | val colorScheme = when { 44 | dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { 45 | val context = LocalContext.current 46 | if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) 47 | } 48 | 49 | darkTheme -> DarkColorScheme 50 | else -> LightColorScheme 51 | } 52 | 53 | MaterialTheme( 54 | colorScheme = colorScheme, 55 | typography = Typography, 56 | content = content 57 | ) 58 | } -------------------------------------------------------------------------------- /app/src/main/java/com/plcoding/internetconnectionobserver/ui/theme/Type.kt: -------------------------------------------------------------------------------- 1 | package com.plcoding.internetconnectionobserver.ui.theme 2 | 3 | import androidx.compose.material3.Typography 4 | import androidx.compose.ui.text.TextStyle 5 | import androidx.compose.ui.text.font.FontFamily 6 | import androidx.compose.ui.text.font.FontWeight 7 | import androidx.compose.ui.unit.sp 8 | 9 | // Set of Material typography styles to start with 10 | val Typography = Typography( 11 | bodyLarge = TextStyle( 12 | fontFamily = FontFamily.Default, 13 | fontWeight = FontWeight.Normal, 14 | fontSize = 16.sp, 15 | lineHeight = 24.sp, 16 | letterSpacing = 0.5.sp 17 | ) 18 | /* Other default text styles to override 19 | titleLarge = TextStyle( 20 | fontFamily = FontFamily.Default, 21 | fontWeight = FontWeight.Normal, 22 | fontSize = 22.sp, 23 | lineHeight = 28.sp, 24 | letterSpacing = 0.sp 25 | ), 26 | labelSmall = TextStyle( 27 | fontFamily = FontFamily.Default, 28 | fontWeight = FontWeight.Medium, 29 | fontSize = 11.sp, 30 | lineHeight = 16.sp, 31 | letterSpacing = 0.5.sp 32 | ) 33 | */ 34 | ) -------------------------------------------------------------------------------- /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/drawable/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /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/philipplackner/InternetConnectionObserver/22395f21ce95e8b8ca2bd78c19aac74ff5b9af27/app/src/main/res/mipmap-hdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipplackner/InternetConnectionObserver/22395f21ce95e8b8ca2bd78c19aac74ff5b9af27/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipplackner/InternetConnectionObserver/22395f21ce95e8b8ca2bd78c19aac74ff5b9af27/app/src/main/res/mipmap-mdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipplackner/InternetConnectionObserver/22395f21ce95e8b8ca2bd78c19aac74ff5b9af27/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipplackner/InternetConnectionObserver/22395f21ce95e8b8ca2bd78c19aac74ff5b9af27/app/src/main/res/mipmap-xhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipplackner/InternetConnectionObserver/22395f21ce95e8b8ca2bd78c19aac74ff5b9af27/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipplackner/InternetConnectionObserver/22395f21ce95e8b8ca2bd78c19aac74ff5b9af27/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipplackner/InternetConnectionObserver/22395f21ce95e8b8ca2bd78c19aac74ff5b9af27/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipplackner/InternetConnectionObserver/22395f21ce95e8b8ca2bd78c19aac74ff5b9af27/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipplackner/InternetConnectionObserver/22395f21ce95e8b8ca2bd78c19aac74ff5b9af27/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /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 | InternetConnectionObserver 3 | -------------------------------------------------------------------------------- /app/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |