Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 100 additions & 4 deletions app/src/org/commcare/utils/ConnectivityStatus.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
package org.commcare.utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import org.javarosa.core.services.Logger;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import androidx.annotation.RequiresApi;
import androidx.annotation.StringDef;
import androidx.core.app.ActivityCompat;

import static org.commcare.tasks.ConnectionDiagnosticTask.CONNECTION_DIAGNOSTIC_REPORT;

/**
Expand All @@ -15,18 +28,101 @@
public class ConnectivityStatus {
public static final String logNotConnectedMessage = "Network test: Not connected.";
public static final String logConnectionSuccessMessage = "Network test: Success.";

private static final String WIFI = "WIFI";
private static final String ETHERNET = "ETHERNET";
private static final String UNKNOWN_NETWORK = "UNKNOWN_NETWORK";
private static final String READ_PHONE_STATE_UNKNOWN_NETWORK = "READ_PHONE_STATE_UNKNOWN_NETWORK";
private static final String NO_NETWORK = "NO_NETWORK";
private static final String TWO_G = "2G";
private static final String THREE_G = "3G";
private static final String FOUR_G = "4G";
private static final String FIVE_G = "5G";

@StringDef({WIFI,ETHERNET,TWO_G,THREE_G,FOUR_G,FIVE_G,UNKNOWN_NETWORK,READ_PHONE_STATE_UNKNOWN_NETWORK,NO_NETWORK})
@Retention(RetentionPolicy.SOURCE)
public @interface NetworkType {}

public static boolean isAirplaneModeOn(Context context) {
return Settings.Global.getInt(context.getApplicationContext().getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}

public static boolean isNetworkAvailable(Context context) {
ConnectivityManager conManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conManager == null) {
Logger.log(CONNECTION_DIAGNOSTIC_REPORT, logNotConnectedMessage);
return false;
}
if (conManager == null) {
Logger.log(CONNECTION_DIAGNOSTIC_REPORT, logNotConnectedMessage);
return false;
}
NetworkInfo netInfo = conManager.getActiveNetworkInfo();

return (netInfo != null && netInfo.isConnected());
}

public static @NetworkType String getNetworkType(Context context){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return getNetworkTypeFromAndroid24(context);
}else{
return getNetworkTypeFromBelowAndroid24(context);
}
}

@RequiresApi(Build.VERSION_CODES.N)
private static @NetworkType String getNetworkTypeFromAndroid24(Context context){
ConnectivityManager connectivityManager = context.getSystemService(ConnectivityManager.class);
Network currentNetwork = connectivityManager.getActiveNetwork();
if(currentNetwork!=null) {
NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(currentNetwork);
if (networkCapabilities != null) {
if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
return WIFI;
}else if(networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)){
return ETHERNET;
}else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
return getNetworkType(telephonyManager.getDataNetworkType());
}else{
return READ_PHONE_STATE_UNKNOWN_NETWORK;
}
}
return UNKNOWN_NETWORK;
}
}
return NO_NETWORK;
}

private static @NetworkType String getNetworkTypeFromBelowAndroid24(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
if (activeNetwork == null || !activeNetwork.isConnected()) {
return NO_NETWORK;
}else if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
return WIFI;
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
return getNetworkType(telephonyManager.getNetworkType());
}else{
return READ_PHONE_STATE_UNKNOWN_NETWORK;
}
}
return UNKNOWN_NETWORK;
}

private static @NetworkType String getNetworkType(int telephonyManagerNetworkType){
return switch (telephonyManagerNetworkType) {
case TelephonyManager.NETWORK_TYPE_GPRS, TelephonyManager.NETWORK_TYPE_EDGE,
TelephonyManager.NETWORK_TYPE_CDMA, TelephonyManager.NETWORK_TYPE_1xRTT,
TelephonyManager.NETWORK_TYPE_IDEN, TelephonyManager.NETWORK_TYPE_GSM -> TWO_G;
case TelephonyManager.NETWORK_TYPE_UMTS, TelephonyManager.NETWORK_TYPE_EVDO_0,
TelephonyManager.NETWORK_TYPE_EVDO_A, TelephonyManager.NETWORK_TYPE_HSDPA,
TelephonyManager.NETWORK_TYPE_HSUPA, TelephonyManager.NETWORK_TYPE_HSPA,
TelephonyManager.NETWORK_TYPE_EVDO_B, TelephonyManager.NETWORK_TYPE_EHRPD,
TelephonyManager.NETWORK_TYPE_HSPAP, TelephonyManager.NETWORK_TYPE_TD_SCDMA ->
THREE_G;
case TelephonyManager.NETWORK_TYPE_LTE -> FOUR_G;
case TelephonyManager.NETWORK_TYPE_NR -> FIVE_G;
default -> UNKNOWN_NETWORK;
};
}
}
4 changes: 2 additions & 2 deletions app/src/org/commcare/utils/FirebaseMessagingUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ private static OnCompleteListener handleFCMTokenRetrieval() {
if (!StringUtils.isEmpty(token)) {
updateFCMToken(token);
} else {
Logger.exception("Fetching FCM registration token failed",
Logger.exception("Fetching FCM registration token failed with network status: " + ConnectivityStatus.getNetworkType(CommCareApplication.instance()) ,
new Throwable("FCM registration token is empty"));
}
} else {
Throwable throwable = task.getException() != null ? task.getException() : new Throwable(
"Task to fetch FCM registration token failed");
Logger.exception("Fetching FCM registration token failed", throwable);
Logger.exception("Fetching FCM registration token failed with network status: " + ConnectivityStatus.getNetworkType(CommCareApplication.instance()), throwable);
}
};
}
Expand Down