-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn when changing to metered network if setting is enabled (#1640)
Co-authored-by: ashiagr <[email protected]>
- Loading branch information
Showing
17 changed files
with
370 additions
and
37 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> | ||
</manifest> |
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
55 changes: 55 additions & 0 deletions
55
...hared/src/main/java/au/com/shiftyjelly/pocketcasts/shared/NetworkConnectionWatcherImpl.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,55 @@ | ||
package au.com.shiftyjelly.pocketcasts.shared | ||
|
||
import android.content.Context | ||
import android.net.ConnectivityManager | ||
import android.net.Network | ||
import android.net.NetworkCapabilities | ||
import android.net.NetworkRequest | ||
import androidx.annotation.VisibleForTesting | ||
import au.com.shiftyjelly.pocketcasts.repositories.di.ApplicationScope | ||
import au.com.shiftyjelly.pocketcasts.repositories.playback.NetworkConnectionWatcher | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import au.com.shiftyjelly.pocketcasts.utils.Network as PCNetworkUtils | ||
|
||
@Singleton | ||
class NetworkConnectionWatcherImpl @Inject constructor( | ||
@ApplicationScope private val applicationScope: CoroutineScope, | ||
@ApplicationContext private val context: Context, | ||
) : NetworkConnectionWatcher { | ||
|
||
private val _networkCapabilities = MutableStateFlow<NetworkCapabilities?>(null) | ||
override val networkCapabilities: StateFlow<NetworkCapabilities?> = _networkCapabilities | ||
|
||
private val networkCallback = object : ConnectivityManager.NetworkCallback() { | ||
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) { | ||
this@NetworkConnectionWatcherImpl.onCapabilitiesChanged(networkCapabilities) | ||
super.onCapabilitiesChanged(network, networkCapabilities) | ||
} | ||
} | ||
|
||
fun startWatching() { | ||
val networkRequest = NetworkRequest.Builder() | ||
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) | ||
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI) | ||
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR) | ||
.build() | ||
|
||
PCNetworkUtils.getConnectivityManager(context) | ||
.registerNetworkCallback(networkRequest, networkCallback) | ||
} | ||
|
||
fun stopWatching() { | ||
PCNetworkUtils.getConnectivityManager(context) | ||
.unregisterNetworkCallback(networkCallback) | ||
} | ||
|
||
@VisibleForTesting | ||
internal fun onCapabilitiesChanged(networkCapabilities: NetworkCapabilities) { | ||
_networkCapabilities.value = networkCapabilities | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...es/features/shared/src/main/java/au/com/shiftyjelly/pocketcasts/shared/di/SharedModule.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,18 @@ | ||
package au.com.shiftyjelly.pocketcasts.shared.di | ||
|
||
import au.com.shiftyjelly.pocketcasts.repositories.playback.NetworkConnectionWatcher | ||
import au.com.shiftyjelly.pocketcasts.shared.NetworkConnectionWatcherImpl | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
abstract class SharedModule { | ||
|
||
@Binds | ||
@Singleton | ||
abstract fun provideNetworkConnectionWatcher(networkWatcherImpl: NetworkConnectionWatcherImpl): NetworkConnectionWatcher | ||
} |
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
8 changes: 8 additions & 0 deletions
8
...ain/java/au/com/shiftyjelly/pocketcasts/repositories/playback/NetworkConnectionWatcher.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,8 @@ | ||
package au.com.shiftyjelly.pocketcasts.repositories.playback | ||
|
||
import android.net.NetworkCapabilities | ||
import kotlinx.coroutines.flow.StateFlow | ||
|
||
interface NetworkConnectionWatcher { | ||
val networkCapabilities: StateFlow<NetworkCapabilities?> | ||
} |
Oops, something went wrong.