Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mob 4998 connectivity manager offline mode improvements #476

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

class IterableNetworkConnectivityManager {
private static final String TAG = "NetworkConnectivityManager";
private boolean isConnected;

private static IterableNetworkConnectivityManager sharedInstance;

private ConnectivityManager connectivityManager;
private ArrayList<IterableNetworkMonitorListener> networkMonitorListeners = new ArrayList<>();
private Set<Network> networkSet = new HashSet<>();

public interface IterableNetworkMonitorListener {
void onNetworkConnected();
Expand All @@ -37,21 +41,36 @@ private IterableNetworkConnectivityManager(Context context) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
checkInternetAvailabilityOnActiveNetwork();
startNetworkCallback(context);
}
}

private void checkInternetAvailabilityOnActiveNetwork() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Network activeNetwork = connectivityManager.getActiveNetwork();
isConnected = activeNetwork == null ? false : connectivityManager.getNetworkCapabilities(activeNetwork).hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
IterableLogger.v(TAG, "Internet active : " + isConnected);
} else {
IterableLogger.v(TAG, "Internet capability could not be detected on active network due to Android OS < Marshmallow.");
}
} catch (Exception e) {
IterableLogger.e(TAG, "Error in detecting network availability");
}
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void startNetworkCallback(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder networkBuilder = new NetworkRequest.Builder();

NetworkRequest networkRequest = new NetworkRequest.Builder().build();
if (connectivityManager != null) {
try {
connectivityManager.registerNetworkCallback(networkBuilder.build(), new ConnectivityManager.NetworkCallback() {
connectivityManager.registerNetworkCallback(networkRequest, new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(@NonNull Network network) {
super.onAvailable(network);
networkSet.add(network);
Ayyanchira marked this conversation as resolved.
Show resolved Hide resolved
IterableLogger.v(TAG, "Network Connected");
isConnected = true;
ArrayList<IterableNetworkMonitorListener> networkListenersCopy = new ArrayList<>(networkMonitorListeners);
Expand All @@ -64,10 +83,13 @@ public void onAvailable(@NonNull Network network) {
public void onLost(@NonNull Network network) {
super.onLost(network);
IterableLogger.v(TAG, "Network Disconnected");
isConnected = false;
ArrayList<IterableNetworkMonitorListener> networkListenersCopy = new ArrayList<>(networkMonitorListeners);
for (IterableNetworkMonitorListener listener : networkListenersCopy) {
listener.onNetworkDisconnected();
networkSet.remove(network);
if (networkSet.isEmpty()) {
isConnected = false;
ArrayList<IterableNetworkMonitorListener> networkListenersCopy = new ArrayList<>(networkMonitorListeners);
for (IterableNetworkMonitorListener listener : networkListenersCopy) {
listener.onNetworkDisconnected();
}
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void onSwitchToBackground() {

}

private void runNow() {
private synchronized void runNow() {
handler.removeMessages(OPERATION_PROCESS_TASKS);
handler.sendEmptyMessage(OPERATION_PROCESS_TASKS);
}
Expand Down
Loading