warn user when lacking internet connnection

This commit is contained in:
Daniel Gultsch 2023-03-09 08:14:30 +01:00
parent b2c348a1df
commit bb2d077b7c
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
2 changed files with 21 additions and 1 deletions

View file

@ -1,6 +1,9 @@
package im.conversations.android.ui.model;
import android.app.Application;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
@ -340,7 +343,14 @@ public class SetupViewModel extends AndroidViewModel {
private void decideNextStep(
final Target current, final ConnectionException connectionException) {
final var state = connectionException.getConnectionState();
LOGGER.info("Deciding next step for {}", state);
final var isNetworkDown = isNetworkDown();
LOGGER.info("Deciding next step for {} isNetworkDown {}", state, isNetworkDown);
if (state == ConnectionState.SERVER_NOT_FOUND && isNetworkDown) {
this.genericErrorEvent.postValue(
new Event<>(
getApplication().getString(R.string.check_your_internet_connection)));
return;
}
if (Arrays.asList(ConnectionState.UNAUTHORIZED, ConnectionState.TEMPORARY_AUTH_FAILURE)
.contains(state)) {
if (this.redirectIfNecessary(current, Target.ENTER_PASSWORD)) {
@ -473,6 +483,15 @@ public class SetupViewModel extends AndroidViewModel {
super.onCleared();
}
private boolean isNetworkDown() {
final ConnectivityManager cm = getApplication().getSystemService(ConnectivityManager.class);
final Network activeNetwork = cm == null ? null : cm.getActiveNetwork();
final NetworkCapabilities capabilities =
activeNetwork == null ? null : cm.getNetworkCapabilities(activeNetwork);
return capabilities == null
|| !capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
}
public enum Target {
ENTER_ADDRESS,
ENTER_PASSWORD,

View file

@ -1047,5 +1047,6 @@
<string name="trust_certificate_warning">The server certificate is not trustworthy. If you dont know what this means its best to go back!</string>
<string name="trust_cerficate">Trust certificate</string>
<string name="you">You</string>
<string name="check_your_internet_connection">Check your Internet connection</string>
</resources>