1 /* 2 * Copyright 2020 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 package org.webrtc; 12 13 import androidx.annotation.Nullable; 14 import java.util.List; 15 16 /** Interface for detecting network changes */ 17 public interface NetworkChangeDetector { 18 // java equivalent of c++ android_network_monitor.h / NetworkType. 19 public static enum ConnectionType { 20 CONNECTION_UNKNOWN, 21 CONNECTION_ETHERNET, 22 CONNECTION_WIFI, 23 CONNECTION_5G, 24 CONNECTION_4G, 25 CONNECTION_3G, 26 CONNECTION_2G, 27 CONNECTION_UNKNOWN_CELLULAR, 28 CONNECTION_BLUETOOTH, 29 CONNECTION_VPN, 30 CONNECTION_NONE 31 } 32 33 public static class IPAddress { 34 public final byte[] address; 35 IPAddress(byte[] address)36 public IPAddress(byte[] address) { 37 this.address = address; 38 } 39 40 @CalledByNative("IPAddress") getAddress()41 private byte[] getAddress() { 42 return address; 43 } 44 } 45 46 /** Java version of NetworkMonitor.NetworkInformation */ 47 public static class NetworkInformation { 48 public final String name; 49 public final ConnectionType type; 50 // Used to specify the underlying network type if the type is CONNECTION_VPN. 51 public final ConnectionType underlyingTypeForVpn; 52 public final long handle; 53 public final IPAddress[] ipAddresses; 54 NetworkInformation(String name, ConnectionType type, ConnectionType underlyingTypeForVpn, long handle, IPAddress[] addresses)55 public NetworkInformation(String name, ConnectionType type, ConnectionType underlyingTypeForVpn, 56 long handle, IPAddress[] addresses) { 57 this.name = name; 58 this.type = type; 59 this.underlyingTypeForVpn = underlyingTypeForVpn; 60 this.handle = handle; 61 this.ipAddresses = addresses; 62 } 63 64 @CalledByNative("NetworkInformation") getIpAddresses()65 private IPAddress[] getIpAddresses() { 66 return ipAddresses; 67 } 68 69 @CalledByNative("NetworkInformation") getConnectionType()70 private ConnectionType getConnectionType() { 71 return type; 72 } 73 74 @CalledByNative("NetworkInformation") getUnderlyingConnectionTypeForVpn()75 private ConnectionType getUnderlyingConnectionTypeForVpn() { 76 return underlyingTypeForVpn; 77 } 78 79 @CalledByNative("NetworkInformation") getHandle()80 private long getHandle() { 81 return handle; 82 } 83 84 @CalledByNative("NetworkInformation") getName()85 private String getName() { 86 return name; 87 } 88 }; 89 90 /** Observer interface by which observer is notified of network changes. */ 91 public static abstract class Observer { 92 /** Called when default network changes. */ onConnectionTypeChanged(ConnectionType newConnectionType)93 public abstract void onConnectionTypeChanged(ConnectionType newConnectionType); 94 onNetworkConnect(NetworkInformation networkInfo)95 public abstract void onNetworkConnect(NetworkInformation networkInfo); 96 onNetworkDisconnect(long networkHandle)97 public abstract void onNetworkDisconnect(long networkHandle); 98 99 /** 100 * Called when network preference change for a (list of) connection type(s). (e.g WIFI) is 101 * `NOT_PREFERRED` or `NEUTRAL`. 102 * 103 * <p>note: `types` is a list of ConnectionTypes, so that all cellular types can be modified in 104 * one call. 105 */ onNetworkPreference( List<ConnectionType> types, @NetworkPreference int preference)106 public abstract void onNetworkPreference( 107 List<ConnectionType> types, @NetworkPreference int preference); 108 109 // Add default impl. for down-stream tests. getFieldTrialsString()110 public String getFieldTrialsString() { 111 return ""; 112 } 113 } 114 getCurrentConnectionType()115 public ConnectionType getCurrentConnectionType(); 116 supportNetworkCallback()117 public boolean supportNetworkCallback(); 118 getActiveNetworkList()119 @Nullable public List<NetworkInformation> getActiveNetworkList(); 120 destroy()121 public void destroy(); 122 } 123