From 7684f38d74c67db96449334b5e9b7d14e415fa7f Mon Sep 17 00:00:00 2001 From: Lorenzo Neumann <36760115+ln-12@users.noreply.github.com> Date: Thu, 6 Jan 2022 14:06:21 +0100 Subject: [PATCH] Support for API 21+, dependency updates --- build.gradle.kts | 17 ++++---- gradle/wrapper/gradle-wrapper.properties | 2 +- multiplatform_connectivity_status.podspec | 26 +++++------ sample/androidApp/build.gradle.kts | 14 +++--- .../androidApp/src/main/AndroidManifest.xml | 6 ++- sample/build.gradle.kts | 4 +- .../gradle/wrapper/gradle-wrapper.properties | 2 +- sample/shared/build.gradle.kts | 29 ++++++------- sample/shared/shared.podspec | 26 +++++------ settings.gradle.kts | 2 +- .../ln-12/library/ConnectivityStatus.kt | 43 ++++++++++++++++++- 11 files changed, 102 insertions(+), 69 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 46743f9..3c173ff 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,17 +1,16 @@ plugins { id("com.android.library") - kotlin("native.cocoapods") version "1.4.30" - kotlin("multiplatform") version "1.4.30" + kotlin("native.cocoapods") version "1.6.10" + kotlin("multiplatform") version "1.6.10" id("convention.publication") } group = "com.github.ln-12" -version = "1.0.0" +version = "1.1.0" repositories { google() mavenCentral() - jcenter() } kotlin { @@ -27,7 +26,7 @@ kotlin { sourceSets { val commonMain by getting { dependencies { - implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3-native-mt") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0") } } val commonTest by getting { @@ -41,7 +40,7 @@ kotlin { val androidTest by getting { dependencies { implementation(kotlin("test-junit")) - implementation("junit:junit:4.13.1") + implementation("junit:junit:4.13.2") } } @@ -61,11 +60,11 @@ kotlin { } android { - compileSdkVersion(30) + compileSdk = 31 sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml") defaultConfig { - minSdkVersion(24) // API >= 24 needed for ConnectivityManager.registerDefaultNetworkCallback() - targetSdkVersion(30) + minSdk = 21 + targetSdk = 31 } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 12d38de..2e6e589 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/multiplatform_connectivity_status.podspec b/multiplatform_connectivity_status.podspec index d212e8d..bcaa31e 100644 --- a/multiplatform_connectivity_status.podspec +++ b/multiplatform_connectivity_status.podspec @@ -1,13 +1,12 @@ Pod::Spec.new do |spec| spec.name = 'multiplatform_connectivity_status' - spec.version = '1.0.0' + spec.version = '1.1.0' spec.homepage = 'https://github.com/ln-12/multiplatform-connectivity-status' spec.source = { :git => "Not Published", :tag => "Cocoapods/#{spec.name}/#{spec.version}" } spec.authors = '' spec.license = '' spec.summary = 'A Kotlin multiplatform mobile library to monitor the connectivity status of the device' - spec.static_framework = true spec.vendored_frameworks = "build/cocoapods/framework/multiplatform_connectivity_status.framework" spec.libraries = "c++" spec.module_name = "#{spec.name}_umbrella" @@ -17,13 +16,8 @@ Pod::Spec.new do |spec| spec.dependency 'Reachability', '3.2' spec.pod_target_xcconfig = { - 'KOTLIN_TARGET[sdk=iphonesimulator*]' => 'ios_x64', - 'KOTLIN_TARGET[sdk=iphoneos*]' => 'ios_arm', - 'KOTLIN_TARGET[sdk=watchsimulator*]' => 'watchos_x64', - 'KOTLIN_TARGET[sdk=watchos*]' => 'watchos_arm', - 'KOTLIN_TARGET[sdk=appletvsimulator*]' => 'tvos_x64', - 'KOTLIN_TARGET[sdk=appletvos*]' => 'tvos_arm64', - 'KOTLIN_TARGET[sdk=macosx*]' => 'macos_x64' + 'KOTLIN_PROJECT_PATH' => ':', + 'PRODUCT_MODULE_NAME' => 'multiplatform_connectivity_status', } spec.script_phases = [ @@ -32,14 +26,16 @@ Pod::Spec.new do |spec| :execution_position => :before_compile, :shell_path => '/bin/sh', :script => <<-SCRIPT + if [ "YES" = "$COCOAPODS_SKIP_KOTLIN_BUILD" ]; then + echo "Skipping Gradle build task invocation due to COCOAPODS_SKIP_KOTLIN_BUILD environment variable set to \"YES\"" + exit 0 + fi set -ev REPO_ROOT="$PODS_TARGET_SRCROOT" - "$REPO_ROOT/gradlew" -p "$REPO_ROOT" ::syncFramework \ - -Pkotlin.native.cocoapods.target=$KOTLIN_TARGET \ - -Pkotlin.native.cocoapods.configuration=$CONFIGURATION \ - -Pkotlin.native.cocoapods.cflags="$OTHER_CFLAGS" \ - -Pkotlin.native.cocoapods.paths.headers="$HEADER_SEARCH_PATHS" \ - -Pkotlin.native.cocoapods.paths.frameworks="$FRAMEWORK_SEARCH_PATHS" + "$REPO_ROOT/gradlew" -p "$REPO_ROOT" $KOTLIN_PROJECT_PATH:syncFramework \ + -Pkotlin.native.cocoapods.platform=$PLATFORM_NAME \ + -Pkotlin.native.cocoapods.archs="$ARCHS" \ + -Pkotlin.native.cocoapods.configuration=$CONFIGURATION SCRIPT } ] diff --git a/sample/androidApp/build.gradle.kts b/sample/androidApp/build.gradle.kts index d253e40..411bf2c 100644 --- a/sample/androidApp/build.gradle.kts +++ b/sample/androidApp/build.gradle.kts @@ -5,18 +5,18 @@ plugins { dependencies { implementation(project(":shared")) - implementation("com.google.android.material:material:1.3.0") - implementation("androidx.appcompat:appcompat:1.2.0") - implementation("androidx.constraintlayout:constraintlayout:2.0.4") - implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.3") + implementation("com.google.android.material:material:1.4.0") + implementation("androidx.appcompat:appcompat:1.4.0") + implementation("androidx.constraintlayout:constraintlayout:2.1.2") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0") } android { - compileSdkVersion(30) + compileSdk = 31 defaultConfig { applicationId = "com.github.ln12.connectivitystatussample.androidApp" - minSdkVersion(24) - targetSdkVersion(30) + minSdk = 21 + targetSdk = 31 versionCode = 1 versionName = "1.0" } diff --git a/sample/androidApp/src/main/AndroidManifest.xml b/sample/androidApp/src/main/AndroidManifest.xml index b62ef2c..37fca64 100644 --- a/sample/androidApp/src/main/AndroidManifest.xml +++ b/sample/androidApp/src/main/AndroidManifest.xml @@ -3,8 +3,10 @@ - + android:theme="@style/AppTheme" > + diff --git a/sample/build.gradle.kts b/sample/build.gradle.kts index 5077cdc..35bfaf8 100644 --- a/sample/build.gradle.kts +++ b/sample/build.gradle.kts @@ -5,8 +5,8 @@ buildscript { mavenCentral() } dependencies { - classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31") - classpath("com.android.tools.build:gradle:4.0.2") + classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10") + classpath("com.android.tools.build:gradle:7.0.4) } } diff --git a/sample/gradle/wrapper/gradle-wrapper.properties b/sample/gradle/wrapper/gradle-wrapper.properties index d754ec7..d04344c 100644 --- a/sample/gradle/wrapper/gradle-wrapper.properties +++ b/sample/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ #Thu Jan 21 10:09:06 CET 2021 distributionBase=GRADLE_USER_HOME -distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip distributionPath=wrapper/dists zipStorePath=wrapper/dists zipStoreBase=GRADLE_USER_HOME diff --git a/sample/shared/build.gradle.kts b/sample/shared/build.gradle.kts index 0b9c30e..7707b78 100644 --- a/sample/shared/build.gradle.kts +++ b/sample/shared/build.gradle.kts @@ -8,7 +8,7 @@ kotlin { android() ios() - version = "1.0" + version = "1.1" cocoapods { summary = "Common library" @@ -18,8 +18,8 @@ kotlin { sourceSets { val commonMain by getting { dependencies { - implementation("com.github.ln-12:multiplatform-connectivity-status:1.0.0") - implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3-native-mt") + implementation("com.github.ln-12:multiplatform-connectivity-status:1.1.0") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0") } } val commonTest by getting { @@ -29,26 +29,25 @@ kotlin { } } val androidMain by getting { - dependencies { - implementation("com.google.android.material:material:1.3.0") - } + dependencies {} } val androidTest by getting { - dependencies { - implementation(kotlin("test-junit")) - implementation("junit:junit:4.13.1") - } + dependencies {} + } + val iosMain by getting { + dependencies {} + } + val iosTest by getting { + dependencies {} } - val iosMain by getting - val iosTest by getting } } android { - compileSdkVersion(30) + compileSdk = 31 sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml") defaultConfig { - minSdkVersion(24) - targetSdkVersion(30) + minSdk = 21 + targetSdk = 31 } } \ No newline at end of file diff --git a/sample/shared/shared.podspec b/sample/shared/shared.podspec index 31ed2ec..fc81e21 100644 --- a/sample/shared/shared.podspec +++ b/sample/shared/shared.podspec @@ -1,13 +1,12 @@ Pod::Spec.new do |spec| spec.name = 'shared' - spec.version = '1.0' + spec.version = '1.1' spec.homepage = 'https://github.com/ln-12/multiplatform-connectivity-status' spec.source = { :git => "Not Published", :tag => "Cocoapods/#{spec.name}/#{spec.version}" } spec.authors = '' spec.license = '' spec.summary = 'Common library' - spec.static_framework = true spec.vendored_frameworks = "build/cocoapods/framework/shared.framework" spec.libraries = "c++" spec.module_name = "#{spec.name}_umbrella" @@ -17,13 +16,8 @@ Pod::Spec.new do |spec| spec.pod_target_xcconfig = { - 'KOTLIN_TARGET[sdk=iphonesimulator*]' => 'ios_x64', - 'KOTLIN_TARGET[sdk=iphoneos*]' => 'ios_arm', - 'KOTLIN_TARGET[sdk=watchsimulator*]' => 'watchos_x64', - 'KOTLIN_TARGET[sdk=watchos*]' => 'watchos_arm', - 'KOTLIN_TARGET[sdk=appletvsimulator*]' => 'tvos_x64', - 'KOTLIN_TARGET[sdk=appletvos*]' => 'tvos_arm64', - 'KOTLIN_TARGET[sdk=macosx*]' => 'macos_x64' + 'KOTLIN_PROJECT_PATH' => ':shared', + 'PRODUCT_MODULE_NAME' => 'shared', } spec.script_phases = [ @@ -32,14 +26,16 @@ Pod::Spec.new do |spec| :execution_position => :before_compile, :shell_path => '/bin/sh', :script => <<-SCRIPT + if [ "YES" = "$COCOAPODS_SKIP_KOTLIN_BUILD" ]; then + echo "Skipping Gradle build task invocation due to COCOAPODS_SKIP_KOTLIN_BUILD environment variable set to \"YES\"" + exit 0 + fi set -ev REPO_ROOT="$PODS_TARGET_SRCROOT" - "$REPO_ROOT/../gradlew" -p "$REPO_ROOT" :shared:syncFramework \ - -Pkotlin.native.cocoapods.target=$KOTLIN_TARGET \ - -Pkotlin.native.cocoapods.configuration=$CONFIGURATION \ - -Pkotlin.native.cocoapods.cflags="$OTHER_CFLAGS" \ - -Pkotlin.native.cocoapods.paths.headers="$HEADER_SEARCH_PATHS" \ - -Pkotlin.native.cocoapods.paths.frameworks="$FRAMEWORK_SEARCH_PATHS" + "$REPO_ROOT/../gradlew" -p "$REPO_ROOT" $KOTLIN_PROJECT_PATH:syncFramework \ + -Pkotlin.native.cocoapods.platform=$PLATFORM_NAME \ + -Pkotlin.native.cocoapods.archs="$ARCHS" \ + -Pkotlin.native.cocoapods.configuration=$CONFIGURATION SCRIPT } ] diff --git a/settings.gradle.kts b/settings.gradle.kts index 5d5fd20..5cec5f5 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -7,7 +7,7 @@ pluginManagement { resolutionStrategy { eachPlugin { if (requested.id.namespace == "com.android" || requested.id.name == "kotlin-android-extensions") { - useModule("com.android.tools.build:gradle:4.0.1") + useModule("com.android.tools.build:gradle:7.0.4") } } } diff --git a/src/androidMain/kotlin/com/github/ln-12/library/ConnectivityStatus.kt b/src/androidMain/kotlin/com/github/ln-12/library/ConnectivityStatus.kt index 4d46463..92761c6 100644 --- a/src/androidMain/kotlin/com/github/ln-12/library/ConnectivityStatus.kt +++ b/src/androidMain/kotlin/com/github/ln-12/library/ConnectivityStatus.kt @@ -3,6 +3,9 @@ package com.github.`ln-12`.library import android.content.Context import android.net.ConnectivityManager import android.net.Network +import android.net.NetworkCapabilities +import android.net.NetworkRequest +import android.os.Build import android.util.Log import kotlinx.coroutines.flow.MutableStateFlow @@ -25,7 +28,44 @@ actual class ConnectivityStatus(private val context: Context) { actual fun start() { try { val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager - connectivityManager.registerDefaultNetworkCallback(networkCallback) + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + // API 24 and above + connectivityManager.registerDefaultNetworkCallback(networkCallback) + + val currentNetwork = connectivityManager.activeNetwork + + if(currentNetwork == null) { + isNetworkConnected.value = false + + Log.d("Connectivity status", "Disconnected") + } + } else { + // API 23 and below + val networkRequest = NetworkRequest.Builder().apply { + addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) + addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) + } + }.build() + + connectivityManager.registerNetworkCallback(networkRequest, networkCallback) + + val currentNetwork = connectivityManager.activeNetworkInfo + + if(currentNetwork == null || ( + currentNetwork.type != ConnectivityManager.TYPE_ETHERNET && + currentNetwork.type != ConnectivityManager.TYPE_WIFI && + currentNetwork.type != ConnectivityManager.TYPE_MOBILE + )) { + isNetworkConnected.value = false + + Log.d("Connectivity status", "Disconnected") + } + } + + Log.d("Connectivity status", "Started") } catch (e: Exception) { Log.d("Connectivity status", "Failed to start: ${e.message.toString()}") e.printStackTrace() @@ -35,5 +75,6 @@ actual class ConnectivityStatus(private val context: Context) { actual fun stop() { connectivityManager?.unregisterNetworkCallback(networkCallback) + Log.d("Connectivity status", "Stopped") } } \ No newline at end of file