Skip to content

Commit

Permalink
Merge pull request #1 from raheemadamboev/1.1
Browse files Browse the repository at this point in the history
1.1
  • Loading branch information
raheemadamboev authored May 8, 2022
2 parents 0e4c0f7 + 825184e commit b1baf95
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 25 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 7 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ android {
}

dependencies {
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

implementation 'com.github.raheemadamboev:check-internet-android:1.0.1'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.1"

implementation project(path: ':check_internet')
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package xyz.teamgravity.checkinternetdemo

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import xyz.teamgravity.checkinternet.CheckInternet
import xyz.teamgravity.checkinternetdemo.databinding.ActivityMainBinding

Expand All @@ -18,12 +20,13 @@ class MainActivity : AppCompatActivity() {
}

private fun button() {
onCheck()
onCheckCallback()
onCheckSuspend()
}

private fun onCheck() {
private fun onCheckCallback() {
binding.apply {
checkB.setOnClickListener {
checkCallbackB.setOnClickListener {

stateT.text = getString(R.string.checking)

Expand All @@ -37,4 +40,16 @@ class MainActivity : AppCompatActivity() {
}
}
}

private fun onCheckSuspend() {
binding.apply {
checkSuspendB.setOnClickListener {
lifecycleScope.launch {
stateT.text = getString(R.string.checking)
val connected = CheckInternet().check()
stateT.text = getString(if (connected) R.string.internet else R.string.no_internet)
}
}
}
}
}
18 changes: 14 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,28 @@
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="22sp"
app:layout_constraintBottom_toTopOf="@id/check_b"
app:layout_constraintBottom_toTopOf="@id/check_callback_b"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />

<com.google.android.material.button.MaterialButton
android:id="@+id/check_b"
android:id="@+id/check_callback_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:text="@string/check_internet"
app:layout_constraintBottom_toBottomOf="parent"
android:text="@string/check_internet_callback"
app:layout_constraintBottom_toTopOf="@id/check_suspend_b"
app:layout_constraintTop_toBottomOf="@id/state_t" />

<com.google.android.material.button.MaterialButton
android:id="@+id/check_suspend_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:text="@string/check_internet_suspend"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/check_callback_b" />
</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<string name="app_name">CheckInternetDemo</string>

<string name="check_internet">Check internet</string>
<string name="check_internet_callback">Check internet (callback)</string>
<string name="check_internet_suspend">Check internet (suspend)</string>
<string name="checking">Checking…</string>
<string name="no_internet">Internet NOT working</string>
<string name="internet">Internet working</string>
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ buildscript {
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"
classpath 'com.android.tools.build:gradle:7.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
8 changes: 3 additions & 5 deletions check_internet/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ android {
defaultConfig {
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand All @@ -35,8 +33,8 @@ android {
}

dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0'
}

afterEvaluate {
Expand All @@ -47,7 +45,7 @@ afterEvaluate {

groupId = 'com.github.raheemadamboev'
artifactId = 'check-internet-android'
version = '1.0'
version = '1.1'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ import kotlinx.coroutines.withContext
import java.io.IOException
import java.net.InetSocketAddress
import java.net.Socket
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

class CheckInternet {

companion object {
private const val HOST_NAME = "8.8.8.8"
private const val PORT = 53
private const val TIMEOUT = 1_500
}

fun check(listener: (connected: Boolean) -> Unit) {
CoroutineScope(Dispatchers.IO).launch(Dispatchers.IO) {
try {
val socket = Socket()
socket.connect(InetSocketAddress("8.8.8.8", 53), 1500)
socket.connect(InetSocketAddress(HOST_NAME, PORT), TIMEOUT)
socket.close()
withContext(Dispatchers.Main) {
listener(true)
Expand All @@ -26,4 +34,19 @@ class CheckInternet {
}
}
}

suspend fun check(): Boolean {
return suspendCoroutine { continuation ->
CoroutineScope(Dispatchers.IO).launch(Dispatchers.IO) {
try {
val socket = Socket()
socket.connect(InetSocketAddress(HOST_NAME, PORT), TIMEOUT)
socket.close()
continuation.resume(true)
} catch (e: IOException) {
continuation.resume(false)
}
}
}
}
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Oct 06 22:18:59 UZT 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

0 comments on commit b1baf95

Please # to comment.