-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add status view about internet connection
- Loading branch information
Showing
12 changed files
with
254 additions
and
0 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
37 changes: 37 additions & 0 deletions
37
...ndroid/app/src/main/java/com/github/dedis/popstellar/repository/ConnectivityRepository.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,37 @@ | ||
package com.github.dedis.popstellar.repository | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import android.net.ConnectivityManager | ||
import io.reactivex.Observable | ||
import io.reactivex.schedulers.Schedulers | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class ConnectivityRepository @Inject constructor(application: Application) { | ||
|
||
private val connectivityManager = | ||
application.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) | ||
as ConnectivityManager | ||
|
||
fun observeConnectivity(): Observable<Boolean> { | ||
return Observable.create<Boolean> { emitter -> | ||
val callback = | ||
object : ConnectivityManager.NetworkCallback() { | ||
override fun onAvailable(network: android.net.Network) { | ||
emitter.onNext(true) | ||
} | ||
|
||
override fun onLost(network: android.net.Network) { | ||
emitter.onNext(false) | ||
} | ||
} | ||
|
||
connectivityManager.registerDefaultNetworkCallback(callback) | ||
|
||
emitter.setCancellable { connectivityManager.unregisterNetworkCallback(callback) } | ||
} | ||
.subscribeOn(Schedulers.io()) | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/NetworkStatusView.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,42 @@ | ||
package com.github.dedis.popstellar.ui | ||
|
||
import android.content.Context | ||
import android.transition.Slide | ||
import android.transition.TransitionManager | ||
import android.util.AttributeSet | ||
import android.view.Gravity | ||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import android.widget.FrameLayout | ||
import androidx.core.view.isVisible | ||
import com.github.dedis.popstellar.databinding.NetworkStatusBinding | ||
|
||
class NetworkStatusView | ||
@JvmOverloads | ||
constructor( | ||
context: Context, | ||
attrs: AttributeSet, | ||
defStyleAttr: Int = 0, | ||
defStyleRes: Int = 0, | ||
) : FrameLayout(context, attrs, defStyleAttr, defStyleRes) { | ||
|
||
private val binding = NetworkStatusBinding.inflate(LayoutInflater.from(context), this, true) | ||
|
||
fun setIsNetworkConnected(isVisible: Boolean) { | ||
setVisibility(isVisible) | ||
} | ||
|
||
private fun setVisibility(isVisible: Boolean) { | ||
TransitionManager.beginDelayedTransition( | ||
this.parent as ViewGroup, | ||
Slide(Gravity.TOP).apply { | ||
addTarget(binding.networkConnectionContainer) | ||
duration = VISIBILITY_CHANGE_DURATION | ||
}) | ||
binding.networkConnectionContainer.isVisible = !isVisible | ||
} | ||
|
||
companion object { | ||
private const val VISIBILITY_CHANGE_DURATION = 400L | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<layout xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<FrameLayout | ||
android:id="@+id/networkConnectionContainer" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:paddingVertical="2dp" | ||
android:background="@color/background_gray"> | ||
|
||
<TextView | ||
android:id="@+id/statusTextView" | ||
style="@style/text_high_emphasis" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:gravity="center" | ||
android:text="@string/network_status_connection_fail" /> | ||
|
||
</FrameLayout> | ||
|
||
</layout> |
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
88 changes: 88 additions & 0 deletions
88
...p/src/test/unit/java/com/github/dedis/popstellar/repository/ConnectivityRepositoryTest.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,88 @@ | ||
package com.github.dedis.popstellar.repository | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import android.net.ConnectivityManager | ||
import android.net.ConnectivityManager.NetworkCallback | ||
import android.net.Network | ||
import io.reactivex.Observable | ||
import io.reactivex.observers.TestObserver | ||
import org.junit.Assert.* | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.* | ||
import org.mockito.Mockito.* | ||
import org.mockito.junit.MockitoJUnitRunner | ||
|
||
|
||
@RunWith(MockitoJUnitRunner::class) | ||
class ConnectivityRepositoryTest { | ||
@Mock | ||
private lateinit var application: Application | ||
|
||
@Mock | ||
private lateinit var context: Context | ||
|
||
@Mock | ||
lateinit var connectivityManager: ConnectivityManager | ||
|
||
private lateinit var connectivityRepository: ConnectivityRepository | ||
|
||
@Before | ||
fun setUp() { | ||
MockitoAnnotations.openMocks(this) | ||
`when`(application.applicationContext).thenReturn(context) | ||
`when`(context.getSystemService(Context.CONNECTIVITY_SERVICE)) | ||
.thenReturn(connectivityManager) | ||
connectivityRepository = ConnectivityRepository(application) | ||
} | ||
|
||
@Test | ||
fun testObserveConnectivity_onAvailable() { | ||
val callbackCaptor = ArgumentCaptor.forClass( | ||
NetworkCallback::class.java | ||
) | ||
|
||
val observable: Observable<Boolean> = connectivityRepository.observeConnectivity() | ||
val testObserver: TestObserver<Boolean> = observable.test() | ||
|
||
verify(connectivityManager).registerDefaultNetworkCallback(callbackCaptor.capture()) | ||
val callback = callbackCaptor.value | ||
|
||
callback.onAvailable(mock(Network::class.java)) | ||
testObserver.assertValue(true) | ||
} | ||
|
||
@Test | ||
fun testObserveConnectivity_onLost() { | ||
val callbackCaptor = ArgumentCaptor.forClass( | ||
NetworkCallback::class.java | ||
) | ||
|
||
val observable = connectivityRepository.observeConnectivity() | ||
val testObserver = observable.test() | ||
|
||
verify(connectivityManager).registerDefaultNetworkCallback(callbackCaptor.capture()) | ||
val callback = callbackCaptor.value | ||
|
||
callback.onLost(mock(Network::class.java)) | ||
testObserver.assertValue(false) | ||
} | ||
|
||
@Test | ||
fun testObserveConnectivity_cancellable() { | ||
val callbackCaptor = ArgumentCaptor.forClass( | ||
NetworkCallback::class.java | ||
) | ||
|
||
val observable = connectivityRepository.observeConnectivity() | ||
val testObserver = observable.test() | ||
|
||
verify(connectivityManager).registerDefaultNetworkCallback(callbackCaptor.capture()) | ||
val callback = callbackCaptor.value | ||
|
||
testObserver.dispose() | ||
verify(connectivityManager).unregisterNetworkCallback(callback) | ||
} | ||
} |