mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-27 20:24:32 +01:00
Use newer APIs for detecting network changes.
This commit is contained in:
@@ -2,12 +2,8 @@ package org.thoughtcrime.securesms.jobmanager.impl;
|
||||
|
||||
import android.app.Application;
|
||||
import android.app.job.JobInfo;
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
import org.thoughtcrime.securesms.jobmanager.Constraint;
|
||||
@@ -43,14 +39,10 @@ public class NetworkConstraint implements Constraint {
|
||||
return "NETWORK";
|
||||
}
|
||||
|
||||
public static boolean isMet(@NonNull Context context) {
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
|
||||
|
||||
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
|
||||
public static boolean isMet(@NonNull Application application) {
|
||||
return NetworkConstraintObserver.getInstance(application).hasInternet();
|
||||
}
|
||||
|
||||
|
||||
public static final class Factory implements Constraint.Factory<NetworkConstraint> {
|
||||
|
||||
private final Application application;
|
||||
|
||||
@@ -1,38 +1,156 @@
|
||||
package org.thoughtcrime.securesms.jobmanager.impl;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.Application;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.Network;
|
||||
import android.net.NetworkCapabilities;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.NetworkRequest;
|
||||
import android.os.Build;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.jobmanager.ConstraintObserver;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
public class NetworkConstraintObserver implements ConstraintObserver {
|
||||
|
||||
private static final String REASON = Log.tag(NetworkConstraintObserver.class);
|
||||
|
||||
private final Application application;
|
||||
|
||||
public NetworkConstraintObserver(Application application) {
|
||||
private volatile Notifier notifier;
|
||||
private volatile boolean hasInternet;
|
||||
|
||||
private final Set<NetworkListener> networkListeners = new HashSet<>();
|
||||
|
||||
private static volatile NetworkConstraintObserver instance;
|
||||
|
||||
public static NetworkConstraintObserver getInstance(@NonNull Application application) {
|
||||
if (instance == null) {
|
||||
synchronized (NetworkConstraintObserver.class) {
|
||||
if (instance == null) {
|
||||
instance = new NetworkConstraintObserver(application);
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private NetworkConstraintObserver(Application application) {
|
||||
this.application = application;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(@NonNull Notifier notifier) {
|
||||
application.registerReceiver(new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
NetworkConstraint constraint = new NetworkConstraint.Factory(application).create();
|
||||
this.notifier = notifier;
|
||||
requestNetwork(0);
|
||||
}
|
||||
|
||||
if (constraint.isMet()) {
|
||||
notifier.onConstraintMet(REASON);
|
||||
@TargetApi(19)
|
||||
private static boolean isActiveNetworkConnected(@NonNull Context context) {
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
|
||||
|
||||
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
|
||||
}
|
||||
|
||||
private void requestNetwork(int retryCount) {
|
||||
if (Build.VERSION.SDK_INT < 21 || retryCount > 5) {
|
||||
hasInternet = isActiveNetworkConnected(application);
|
||||
|
||||
application.registerReceiver(new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
hasInternet = isActiveNetworkConnected(context);
|
||||
|
||||
if (hasInternet) {
|
||||
notifier.onConstraintMet(REASON);
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
}, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
|
||||
}, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
|
||||
} else {
|
||||
NetworkRequest request = new NetworkRequest.Builder().addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||
.build();
|
||||
|
||||
ConnectivityManager connectivityManager = Objects.requireNonNull(ContextCompat.getSystemService(application, ConnectivityManager.class));
|
||||
connectivityManager.requestNetwork(request, Build.VERSION.SDK_INT >= 26 ? new NetworkStateListener26(retryCount) : new NetworkStateListener21());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasInternet() {
|
||||
return hasInternet;
|
||||
}
|
||||
|
||||
public void addListener(@Nullable NetworkListener networkListener) {
|
||||
synchronized (networkListeners) {
|
||||
networkListeners.add(networkListener);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeListener(@Nullable NetworkListener networkListener) {
|
||||
if (networkListener == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
synchronized (networkListeners) {
|
||||
networkListeners.remove(networkListener);
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyListeners() {
|
||||
synchronized (networkListeners) {
|
||||
//noinspection SimplifyStreamApiCallChains
|
||||
networkListeners.stream().forEach(NetworkListener::onNetworkChanged);
|
||||
}
|
||||
}
|
||||
|
||||
@TargetApi(21)
|
||||
private class NetworkStateListener21 extends ConnectivityManager.NetworkCallback {
|
||||
@Override
|
||||
public void onAvailable(@NonNull Network network) {
|
||||
Log.i(REASON, "Network available: " + network.hashCode());
|
||||
hasInternet = true;
|
||||
notifier.onConstraintMet(REASON);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLost(@NonNull Network network) {
|
||||
Log.i(REASON, "Network loss: " + network.hashCode());
|
||||
hasInternet = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
@TargetApi(26)
|
||||
private class NetworkStateListener26 extends NetworkStateListener21 {
|
||||
private final int retryCount;
|
||||
|
||||
public NetworkStateListener26(int retryCount) {
|
||||
this.retryCount = retryCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUnavailable() {
|
||||
Log.w(REASON, "No networks available");
|
||||
requestNetwork(retryCount + 1);
|
||||
}
|
||||
}
|
||||
|
||||
public interface NetworkListener {
|
||||
void onNetworkChanged();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user