mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-23 10:20:25 +01:00
Revert all new network detection API usage and refactorings.
This commit is contained in:
@@ -2,8 +2,12 @@ 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;
|
||||
@@ -39,8 +43,11 @@ public class NetworkConstraint implements Constraint {
|
||||
return "NETWORK";
|
||||
}
|
||||
|
||||
public static boolean isMet(@NonNull Application application) {
|
||||
return NetworkConstraintObserver.getInstance(application).hasInternet();
|
||||
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 final class Factory implements Constraint.Factory<NetworkConstraint> {
|
||||
|
||||
@@ -6,112 +6,33 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.os.Build;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.jobmanager.ConstraintObserver;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class NetworkConstraintObserver implements ConstraintObserver {
|
||||
|
||||
private static final String REASON = Log.tag(NetworkConstraintObserver.class);
|
||||
private static final String TAG = Log.tag(NetworkConstraintObserver.class);
|
||||
|
||||
private final 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) {
|
||||
public NetworkConstraintObserver(Application application) {
|
||||
this.application = application;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(@NonNull Notifier notifier) {
|
||||
this.notifier = notifier;
|
||||
this.hasInternet = isActiveNetworkConnected(application);
|
||||
|
||||
requestNetwork();
|
||||
}
|
||||
|
||||
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() {
|
||||
application.registerReceiver(new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
hasInternet = isActiveNetworkConnected(context);
|
||||
NetworkConstraint constraint = new NetworkConstraint.Factory(application).create();
|
||||
|
||||
if (hasInternet) {
|
||||
Log.i(TAG, logPrefix() + "Network available.");
|
||||
if (constraint.isMet()) {
|
||||
notifier.onConstraintMet(REASON);
|
||||
} else {
|
||||
Log.w(TAG, logPrefix() + "Network unavailable.");
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
}, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private static String logPrefix() {
|
||||
return "[API " + Build.VERSION.SDK_INT + "] ";
|
||||
}
|
||||
|
||||
public interface NetworkListener {
|
||||
void onNetworkChanged();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user