-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2011 from nextcloud/fix/receivers
Use ConnectivityManager Instead Deprecated IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION) and BroadcastReceiver
- Loading branch information
Showing
2 changed files
with
150 additions
and
77 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
67 changes: 67 additions & 0 deletions
67
app/src/main/java/it/niedermann/owncloud/notes/shared/util/ConnectionLiveData.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,67 @@ | ||
package it.niedermann.owncloud.notes.shared.util | ||
|
||
import android.content.Context | ||
import android.net.ConnectivityManager | ||
import android.net.Network | ||
import android.net.NetworkCapabilities | ||
import android.net.NetworkRequest | ||
import androidx.lifecycle.LiveData | ||
|
||
/** | ||
* LiveData subclass that provides network connection status updates. | ||
* It observes changes in network connectivity and posts updates to its observers. | ||
* | ||
* @property context The application context used to access system services. | ||
*/ | ||
class ConnectionLiveData(val context: Context) : LiveData<ConnectionLiveData.ConnectionType>() { | ||
|
||
private val connectivityManager = | ||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
private val networkRequest = NetworkRequest.Builder().build() | ||
|
||
/** | ||
* Enum representing different types of network connections. | ||
*/ | ||
enum class ConnectionType { | ||
Lost, WiFi, Ethernet, MobileData, Other | ||
} | ||
|
||
init { | ||
connectivityManager.registerNetworkCallback( | ||
networkRequest, | ||
object : ConnectivityManager.NetworkCallback() { | ||
override fun onAvailable(network: Network) { | ||
super.onAvailable(network) | ||
val connectivityManager = | ||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
val networkCapabilities = connectivityManager.getNetworkCapabilities(network) | ||
|
||
if (networkCapabilities != null) { | ||
when { | ||
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> { | ||
postValue(ConnectionType.WiFi) | ||
} | ||
|
||
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> { | ||
postValue(ConnectionType.Ethernet) | ||
} | ||
|
||
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> { | ||
postValue(ConnectionType.MobileData) | ||
} | ||
|
||
else -> { | ||
postValue(ConnectionType.Other) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onLost(network: Network) { | ||
super.onLost(network) | ||
postValue(ConnectionType.Lost) | ||
} | ||
} | ||
) | ||
} | ||
} |