package com.vocsy.network;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import androidx.annotation.NonNull;
public class NetworkManager {
private static Activity activity;
private static OnMonitorListener listener;
private static NetworkRequest networkRequest = new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.build();
private static ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(@NonNull Network network) {
super.onAvailable(network);
if (listener != null) {
listener.onConnectivityChanged(true);
}
}
@Override
public void onLost(@NonNull Network network) {
super.onLost(network);
if (listener != null) {
listener.onConnectivityChanged(false);
}
}
@Override
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
super.onCapabilitiesChanged(network, networkCapabilities);
final boolean isWifiNetwork = networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
}
};
public interface OnMonitorListener {
void onConnectivityChanged(boolean isConnected);
}
public static void Monitoring(Activity activity, OnMonitorListener listener) {
NetworkManager.activity = activity;
NetworkManager.listener = listener;
if (listener != null && !isConnected(activity)) {
listener.onConnectivityChanged(isConnected(activity));
}
ConnectivityManager connectivityManager = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
connectivityManager = (ConnectivityManager) activity.getSystemService(ConnectivityManager.class);
}
if (connectivityManager != null) {
connectivityManager.requestNetwork(networkRequest, networkCallback);
}
}
private static boolean isConnected(Context context) {
NetworkInfo info = getNetworkInfo(context);
return (info != null && info.isConnectedOrConnecting());
}
private static NetworkInfo getNetworkInfo(Context context) {
try {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo();
} catch (Exception e) {
System.out.println("CheckConnectivity Exception: " + e.getMessage());
return null;
}
}
public static boolean isConnectedToWifi(Context context) {
NetworkInfo info = getNetworkInfo(context);
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}
public static boolean isConnectedToMobile(Context context) {
NetworkInfo info = getNetworkInfo(context);
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
}
}