forked from tuxmobil/CampFahrplan
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encapsulate network connectivity observing for different API versions.
+ New: Using ConnectivityManager#registerDefaultNetworkCallback for Android Nougat and newer. + NetworkInfo#isConnectedOrConnecting and ConnectivityManager#CONNECTIVITY_ACTION have been deprecated in API level 28. + Related commit: 49a2bbf. + Relates to issue tuxmobil#138.
- Loading branch information
1 parent
218e491
commit f3c8651
Showing
6 changed files
with
129 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
app/src/main/java/nerd/tuxmobil/fahrplan/congress/net/Connectivity.kt
This file was deleted.
Oops, something went wrong.
95 changes: 95 additions & 0 deletions
95
app/src/main/java/nerd/tuxmobil/fahrplan/congress/net/ConnectivityObserver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
@file:JvmName("ConnectivityObserver") | ||
|
||
package nerd.tuxmobil.fahrplan.congress.net | ||
|
||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.IntentFilter | ||
import android.net.ConnectivityManager | ||
import android.net.Network | ||
import android.os.Build | ||
|
||
/** | ||
* Observes network connectivity by consulting the [ConnectivityManager]. | ||
* Observing can run infinitely or automatically be stopped after the first response is received. | ||
*/ | ||
class ConnectivityObserver @JvmOverloads constructor( | ||
|
||
val context: Context, | ||
val onConnectionAvailable: () -> Unit, | ||
val onConnectionLost: () -> Unit = {}, | ||
val shouldStopAfterFirstResponse: Boolean = false | ||
|
||
) { | ||
|
||
private val connectivityManager | ||
get() = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
|
||
@Suppress("DEPRECATION") | ||
private val intentFilter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION) | ||
|
||
private val broadCastReceiver = object : BroadcastReceiver() { | ||
|
||
@Suppress("DEPRECATION") | ||
override fun onReceive(context: Context?, intent: Intent?) { | ||
if (ConnectivityManager.CONNECTIVITY_ACTION != intent?.action) { | ||
return | ||
} | ||
val networkInfo = connectivityManager.activeNetworkInfo | ||
if (networkInfo != null && networkInfo.isConnectedOrConnecting) { | ||
onConnectionAvailable.invoke() | ||
} else { | ||
onConnectionLost.invoke() | ||
} | ||
if (shouldStopAfterFirstResponse) { | ||
stop() | ||
} | ||
} | ||
|
||
} | ||
|
||
private lateinit var networkCallback: ConnectivityManager.NetworkCallback | ||
|
||
init { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
networkCallback = object : ConnectivityManager.NetworkCallback() { | ||
|
||
override fun onAvailable(network: Network) { | ||
super.onAvailable(network) | ||
onConnectionAvailable.invoke() | ||
if (shouldStopAfterFirstResponse) { | ||
stop() | ||
} | ||
} | ||
|
||
override fun onLost(network: Network?) { | ||
super.onLost(network) | ||
onConnectionLost.invoke() | ||
if (shouldStopAfterFirstResponse) { | ||
stop() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun start() { | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { | ||
// Decouple from component lifecycle, use application context. | ||
// See: https://developer.android.com/reference/android/content/Context.html#getApplicationContext() | ||
context.applicationContext.registerReceiver(broadCastReceiver, intentFilter) | ||
} else { | ||
connectivityManager.registerDefaultNetworkCallback(networkCallback) | ||
} | ||
} | ||
|
||
fun stop() { | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { | ||
context.applicationContext.unregisterReceiver(broadCastReceiver) | ||
} else { | ||
connectivityManager.unregisterNetworkCallback(networkCallback) | ||
} | ||
} | ||
|
||
} |
43 changes: 0 additions & 43 deletions
43
app/src/main/java/nerd/tuxmobil/fahrplan/congress/net/ConnectivityStateReceiver.java
This file was deleted.
Oops, something went wrong.