WebRTC-DeprecateGlobalFieldTrialString/Enabled/ - part 14/inf

This cl/ passes field trials all the way from c++
to the android NetworkMonitorAutoDetect.java

Bug: webrtc:10335
Change-Id: Ic6842612eed36b684340f0f78f4087bee249cc50
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/257081
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Florent Castelli <orphis@webrtc.org>
Commit-Queue: Jonas Oreland <jonaso@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36498}
This commit is contained in:
Jonas Oreland
2022-04-08 11:11:16 +02:00
committed by WebRTC LUCI CQ
parent 35716230b5
commit f177081eee
8 changed files with 84 additions and 56 deletions

View File

@ -13,5 +13,6 @@ package org.webrtc;
import android.content.Context;
public interface NetworkChangeDetectorFactory {
public NetworkChangeDetector create(NetworkChangeDetector.Observer observer, Context context);
public NetworkChangeDetector create(
NetworkChangeDetector.Observer observer, Context context, String fieldTrialsString);
}

View File

@ -46,8 +46,8 @@ public class NetworkMonitor {
new NetworkChangeDetectorFactory() {
@Override
public NetworkChangeDetector create(
NetworkChangeDetector.Observer observer, Context context) {
return new NetworkMonitorAutoDetect(observer, context);
NetworkChangeDetector.Observer observer, Context context, String fieldTrialsString) {
return new NetworkMonitorAutoDetect(observer, context, fieldTrialsString);
}
};
@ -101,20 +101,26 @@ public class NetworkMonitor {
* multi-networking. This requires the embedding app have the platform ACCESS_NETWORK_STATE and
* CHANGE_NETWORK_STATE permission.
*/
public void startMonitoring(Context applicationContext) {
public void startMonitoring(Context applicationContext, String fieldTrialsString) {
synchronized (networkChangeDetectorLock) {
++numObservers;
if (networkChangeDetector == null) {
networkChangeDetector = createNetworkChangeDetector(applicationContext);
networkChangeDetector = createNetworkChangeDetector(applicationContext, fieldTrialsString);
}
currentConnectionType = networkChangeDetector.getCurrentConnectionType();
}
}
/** Deprecated, use startMonitoring with fieldTrialsStringString argument. */
@Deprecated
public void startMonitoring(Context applicationContext) {
startMonitoring(applicationContext, "");
}
/** Deprecated, pass in application context in startMonitoring instead. */
@Deprecated
public void startMonitoring() {
startMonitoring(ContextUtils.getApplicationContext());
startMonitoring(ContextUtils.getApplicationContext(), "");
}
/**
@ -123,11 +129,15 @@ public class NetworkMonitor {
* CHANGE_NETWORK_STATE permission.
*/
@CalledByNative
private void startMonitoring(@Nullable Context applicationContext, long nativeObserver) {
Logging.d(TAG, "Start monitoring with native observer " + nativeObserver);
private void startMonitoring(
@Nullable Context applicationContext, long nativeObserver, String fieldTrialsString) {
Logging.d(TAG,
"Start monitoring with native observer " + nativeObserver
+ " fieldTrialsString: " + fieldTrialsString);
startMonitoring(
applicationContext != null ? applicationContext : ContextUtils.getApplicationContext());
applicationContext != null ? applicationContext : ContextUtils.getApplicationContext(),
fieldTrialsString);
// The native observers expect a network list update after they call startMonitoring.
synchronized (nativeNetworkObservers) {
nativeNetworkObservers.add(nativeObserver);
@ -177,7 +187,8 @@ public class NetworkMonitor {
return currentConnectionType;
}
private NetworkChangeDetector createNetworkChangeDetector(Context appContext) {
private NetworkChangeDetector createNetworkChangeDetector(
Context appContext, String fieldTrialsString) {
return networkChangeDetectorFactory.create(new NetworkChangeDetector.Observer() {
@Override
public void onConnectionTypeChanged(NetworkChangeDetector.ConnectionType newConnectionType) {
@ -199,7 +210,7 @@ public class NetworkMonitor {
List<NetworkChangeDetector.ConnectionType> types, int preference) {
notifyObserversOfNetworkPreference(types, preference);
}
}, appContext);
}, appContext, fieldTrialsString);
}
private void updateCurrentConnectionType(NetworkChangeDetector.ConnectionType newConnectionType) {
@ -339,10 +350,11 @@ public class NetworkMonitor {
}
// For testing only.
static NetworkMonitorAutoDetect createAndSetAutoDetectForTest(Context context) {
static NetworkMonitorAutoDetect createAndSetAutoDetectForTest(
Context context, String fieldTrialsString) {
NetworkMonitor networkMonitor = getInstance();
NetworkChangeDetector networkChangeDetector =
networkMonitor.createNetworkChangeDetector(context);
networkMonitor.createNetworkChangeDetector(context, fieldTrialsString);
networkMonitor.networkChangeDetector = networkChangeDetector;
return (NetworkMonitorAutoDetect) networkChangeDetector;
}

View File

@ -176,26 +176,29 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver implements Netwo
private final boolean requestVPN;
private final boolean includeOtherUidNetworks;
ConnectivityManagerDelegate(Context context, Set<Network> availableNetworks) {
ConnectivityManagerDelegate(
Context context, Set<Network> availableNetworks, String fieldTrialsString) {
this((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE),
availableNetworks,
PeerConnectionFactory.fieldTrialsFindFullName("WebRTC-NetworkMonitorAutoDetect"));
availableNetworks, fieldTrialsString);
}
@VisibleForTesting
ConnectivityManagerDelegate(ConnectivityManager connectivityManager,
Set<Network> availableNetworks, String fieldTrials) {
Set<Network> availableNetworks, String fieldTrialsString) {
this.connectivityManager = connectivityManager;
this.availableNetworks = availableNetworks;
this.getAllNetworksFromCache = checkFieldTrial(fieldTrials, "getAllNetworksFromCache", false);
this.requestVPN = checkFieldTrial(fieldTrials, "requestVPN", false);
this.includeOtherUidNetworks = checkFieldTrial(fieldTrials, "includeOtherUidNetworks", false);
this.getAllNetworksFromCache =
checkFieldTrial(fieldTrialsString, "getAllNetworksFromCache", false);
this.requestVPN = checkFieldTrial(fieldTrialsString, "requestVPN", false);
this.includeOtherUidNetworks =
checkFieldTrial(fieldTrialsString, "includeOtherUidNetworks", false);
}
private static boolean checkFieldTrial(String fieldTrials, String key, boolean defaultValue) {
if (fieldTrials.contains(key + ":true")) {
private static boolean checkFieldTrial(
String fieldTrialsString, String key, boolean defaultValue) {
if (fieldTrialsString.contains(key + ":true")) {
return true;
} else if (fieldTrials.contains(key + ":false")) {
} else if (fieldTrialsString.contains(key + ":false")) {
return false;
}
return defaultValue;
@ -635,10 +638,12 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver implements Netwo
/** Constructs a NetworkMonitorAutoDetect. Should only be called on UI thread. */
@SuppressLint("NewApi")
public NetworkMonitorAutoDetect(NetworkChangeDetector.Observer observer, Context context) {
public NetworkMonitorAutoDetect(
NetworkChangeDetector.Observer observer, Context context, String fieldTrialsString) {
this.observer = observer;
this.context = context;
connectivityManagerDelegate = new ConnectivityManagerDelegate(context, availableNetworks);
connectivityManagerDelegate =
new ConnectivityManagerDelegate(context, availableNetworks, fieldTrialsString);
wifiManagerDelegate = new WifiManagerDelegate(context);
final NetworkState networkState = connectivityManagerDelegate.getNetworkState();