Skip to content

Commit

Permalink
Examples for ui and non ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishwajith-Shettigar committed Aug 2, 2024
1 parent 671b948 commit e652187
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 27 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
android:theme="@style/Theme.Network_Monitoring"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:name=".NetworkMonitorWithUi"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
21 changes: 0 additions & 21 deletions app/src/main/java/com/example/network_monitoring/MainActivity.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.example.network_monitoring

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.example.networkmonitor.NetworkMonitor
import com.example.networkmonitor.NetworkStatusView

class NetworkMonitorWithUi : AppCompatActivity() {
private lateinit var networkStatusView: NetworkStatusView
private lateinit var networkMonitor:NetworkMonitor

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
networkStatusView = findViewById(R.id.network_status_view)

networkStatusView.startMonitoring(this)


// Network monitoring api.
networkMonitor = NetworkMonitor(this)
networkMonitor.networkStatus.observe(this) {
if (it) {
// Connection on
Log.e("#", it.toString() + " " + networkMonitor.isNetworkAvailable())
} else {
// Connection off
Log.e("#", it.toString() + " " + networkMonitor.isNetworkAvailable())
}
}

// If you dont want to observe.
val isNetworkAvailable=networkMonitor.isNetworkAvailable()

}

override fun onDestroy() {
super.onDestroy()
// Stop monitoring.
networkStatusView.stopMonitoring()

// Stop obeserving.
networkMonitor.unregisterCallback()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.network_monitoring

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.example.networkmonitor.NetworkMonitor

class NetworkMonitorWithoutUi : AppCompatActivity() {
private lateinit var networkMonitor: NetworkMonitor

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Network monitoring api.
networkMonitor = NetworkMonitor(this)

networkMonitor.networkStatus.observe(this) {
if (it) {
// Connection on
Log.e("#", it.toString() + " " + networkMonitor.isNetworkAvailable())
} else {
// Connection off
Log.e("#", it.toString() + " " + networkMonitor.isNetworkAvailable())
}
}

// If you dont want to observe.
val isNetworkAvailable=networkMonitor.isNetworkAvailable()

}

override fun onDestroy() {
super.onDestroy()
// Stop obeserving.
networkMonitor.unregisterCallback()
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
tools:context=".NetworkMonitorWithUi">

<com.example.networkmonitor.NetworkStatusView
android:id="@+id/network_status_view"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class NetworkMonitor(context: Context) {
}

init {

if (!isNetworkAvailable)
isFirstTime = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class NetworkStatusView @JvmOverloads constructor(
* Animation code taken from SO thread
* (https://stackoverflow.com/questions/23925907/slidedown-and-slideup-layout-with-animation).
*/
fun setVisible(v: View) {
private fun setVisible(v: View) {
v.measure(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT)
val targetHeight = v.measuredHeight
v.layoutParams.height = 1
Expand All @@ -117,7 +117,7 @@ class NetworkStatusView @JvmOverloads constructor(
v.startAnimation(a)
}

fun setHide(v: View) {
private fun setHide(v: View) {
val initialHeight = v.measuredHeight
val a: Animation = object : Animation() {
override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
Expand Down Expand Up @@ -145,7 +145,7 @@ class NetworkStatusView @JvmOverloads constructor(
}
}

fun unregisterNetworkMonitor() {
fun stopMonitoring() {
networkMonitor.unregisterCallback()
}
}

0 comments on commit e652187

Please sign in to comment.