Remove passing Android context to NetworkMonitor.
Instead NetworkMonitor calls ContextUtils.getApplicationContext when needed. Bug: webrtc:7730 Change-Id: I312781da4222f7107ea1bf57099f17709fec2385 Reviewed-on: https://chromium-review.googlesource.com/517792 Commit-Queue: Sami Kalliomäki <sakal@webrtc.org> Reviewed-by: Magnus Jedvert <magjed@webrtc.org> Cr-Commit-Position: refs/heads/master@{#18382}
This commit is contained in:
committed by
Commit Bot
parent
bc9ffad966
commit
3afb899655
@ -39,9 +39,7 @@ public class NetworkMonitor {
|
|||||||
private static final String TAG = "NetworkMonitor";
|
private static final String TAG = "NetworkMonitor";
|
||||||
|
|
||||||
// We are storing application context so it is okay.
|
// We are storing application context so it is okay.
|
||||||
@SuppressLint("StaticFieldLeak") private static NetworkMonitor instance;
|
private static NetworkMonitor instance;
|
||||||
|
|
||||||
private final Context applicationContext;
|
|
||||||
|
|
||||||
// Native observers of the connection type changes.
|
// Native observers of the connection type changes.
|
||||||
private final ArrayList<Long> nativeNetworkObservers;
|
private final ArrayList<Long> nativeNetworkObservers;
|
||||||
@ -53,33 +51,22 @@ public class NetworkMonitor {
|
|||||||
|
|
||||||
private ConnectionType currentConnectionType = ConnectionType.CONNECTION_UNKNOWN;
|
private ConnectionType currentConnectionType = ConnectionType.CONNECTION_UNKNOWN;
|
||||||
|
|
||||||
private NetworkMonitor(Context context) {
|
private NetworkMonitor() {
|
||||||
assertIsTrue(context != null);
|
|
||||||
applicationContext = context.getApplicationContext();
|
|
||||||
|
|
||||||
nativeNetworkObservers = new ArrayList<Long>();
|
nativeNetworkObservers = new ArrayList<Long>();
|
||||||
networkObservers = new ArrayList<NetworkObserver>();
|
networkObservers = new ArrayList<NetworkObserver>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// TODO(sakal): Remove once downstream dependencies have been updated.
|
||||||
* Initializes the singleton once.
|
@Deprecated
|
||||||
* Called from the native code.
|
public static void init(Context context) {}
|
||||||
*/
|
|
||||||
public static NetworkMonitor init(Context context) {
|
|
||||||
if (!isInitialized()) {
|
|
||||||
instance = new NetworkMonitor(context);
|
|
||||||
}
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isInitialized() {
|
|
||||||
return instance != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the singleton instance.
|
* Returns the singleton instance.
|
||||||
*/
|
*/
|
||||||
public static NetworkMonitor getInstance() {
|
public static NetworkMonitor getInstance() {
|
||||||
|
if (instance == null) {
|
||||||
|
instance = new NetworkMonitor();
|
||||||
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,7 +150,7 @@ public class NetworkMonitor {
|
|||||||
public void onNetworkDisconnect(long networkHandle) {
|
public void onNetworkDisconnect(long networkHandle) {
|
||||||
notifyObserversOfNetworkDisconnect(networkHandle);
|
notifyObserversOfNetworkDisconnect(networkHandle);
|
||||||
}
|
}
|
||||||
}, applicationContext);
|
}, ContextUtils.getApplicationContext());
|
||||||
final NetworkMonitorAutoDetect.NetworkState networkState =
|
final NetworkMonitorAutoDetect.NetworkState networkState =
|
||||||
autoDetector.getCurrentNetworkState();
|
autoDetector.getCurrentNetworkState();
|
||||||
updateCurrentConnectionType(NetworkMonitorAutoDetect.getConnectionType(networkState));
|
updateCurrentConnectionType(NetworkMonitorAutoDetect.getConnectionType(networkState));
|
||||||
@ -250,8 +237,8 @@ public class NetworkMonitor {
|
|||||||
long nativePtr, NetworkInformation[] networkInfos);
|
long nativePtr, NetworkInformation[] networkInfos);
|
||||||
|
|
||||||
// For testing only.
|
// For testing only.
|
||||||
static void resetInstanceForTests(Context context) {
|
static void resetInstanceForTests() {
|
||||||
instance = new NetworkMonitor(context);
|
instance = new NetworkMonitor();
|
||||||
}
|
}
|
||||||
|
|
||||||
// For testing only.
|
// For testing only.
|
||||||
|
|||||||
@ -161,7 +161,7 @@ public class NetworkMonitorTest {
|
|||||||
*/
|
*/
|
||||||
private void createTestMonitor() {
|
private void createTestMonitor() {
|
||||||
Context context = InstrumentationRegistry.getTargetContext();
|
Context context = InstrumentationRegistry.getTargetContext();
|
||||||
NetworkMonitor.resetInstanceForTests(context);
|
NetworkMonitor.resetInstanceForTests();
|
||||||
NetworkMonitor.setAutoDetectConnectivityState(true);
|
NetworkMonitor.setAutoDetectConnectivityState(true);
|
||||||
receiver = NetworkMonitor.getAutoDetectorForTest();
|
receiver = NetworkMonitor.getAutoDetectorForTest();
|
||||||
assertNotNull(receiver);
|
assertNotNull(receiver);
|
||||||
@ -182,6 +182,7 @@ public class NetworkMonitorTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
|
ContextUtils.initialize(InstrumentationRegistry.getTargetContext());
|
||||||
createTestMonitor();
|
createTestMonitor();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -27,7 +27,6 @@ enum AndroidSdkVersion {
|
|||||||
SDK_VERSION_MARSHMALLOW = 23
|
SDK_VERSION_MARSHMALLOW = 23
|
||||||
};
|
};
|
||||||
|
|
||||||
jobject AndroidNetworkMonitor::application_context_ = nullptr;
|
|
||||||
int AndroidNetworkMonitor::android_sdk_int_ = 0;
|
int AndroidNetworkMonitor::android_sdk_int_ = 0;
|
||||||
|
|
||||||
static NetworkType GetNetworkTypeFromJava(JNIEnv* jni, jobject j_network_type) {
|
static NetworkType GetNetworkTypeFromJava(JNIEnv* jni, jobject j_network_type) {
|
||||||
@ -163,14 +162,6 @@ std::string NetworkInformation::ToString() const {
|
|||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
|
||||||
void AndroidNetworkMonitor::SetAndroidContext(JNIEnv* jni, jobject context) {
|
|
||||||
if (application_context_) {
|
|
||||||
jni->DeleteGlobalRef(application_context_);
|
|
||||||
}
|
|
||||||
application_context_ = NewGlobalRef(jni, context);
|
|
||||||
}
|
|
||||||
|
|
||||||
AndroidNetworkMonitor::AndroidNetworkMonitor()
|
AndroidNetworkMonitor::AndroidNetworkMonitor()
|
||||||
: j_network_monitor_class_(jni(),
|
: j_network_monitor_class_(jni(),
|
||||||
FindClass(jni(), "org/webrtc/NetworkMonitor")),
|
FindClass(jni(), "org/webrtc/NetworkMonitor")),
|
||||||
@ -178,13 +169,10 @@ AndroidNetworkMonitor::AndroidNetworkMonitor()
|
|||||||
jni(),
|
jni(),
|
||||||
jni()->CallStaticObjectMethod(
|
jni()->CallStaticObjectMethod(
|
||||||
*j_network_monitor_class_,
|
*j_network_monitor_class_,
|
||||||
GetStaticMethodID(
|
GetStaticMethodID(jni(),
|
||||||
jni(),
|
|
||||||
*j_network_monitor_class_,
|
*j_network_monitor_class_,
|
||||||
"init",
|
"getInstance",
|
||||||
"(Landroid/content/Context;)Lorg/webrtc/NetworkMonitor;"),
|
"()Lorg/webrtc/NetworkMonitor;"))) {
|
||||||
application_context_)) {
|
|
||||||
RTC_DCHECK(application_context_ != nullptr);
|
|
||||||
CHECK_EXCEPTION(jni()) << "Error during NetworkMonitor.init";
|
CHECK_EXCEPTION(jni()) << "Error during NetworkMonitor.init";
|
||||||
if (android_sdk_int_ <= 0) {
|
if (android_sdk_int_ <= 0) {
|
||||||
jmethodID m = GetStaticMethodID(jni(), *j_network_monitor_class_,
|
jmethodID m = GetStaticMethodID(jni(), *j_network_monitor_class_,
|
||||||
|
|||||||
@ -53,7 +53,8 @@ class AndroidNetworkMonitor : public rtc::NetworkMonitorBase,
|
|||||||
public:
|
public:
|
||||||
AndroidNetworkMonitor();
|
AndroidNetworkMonitor();
|
||||||
|
|
||||||
static void SetAndroidContext(JNIEnv* jni, jobject context);
|
// TODO(sakal): Remove once down stream dependencies have been updated.
|
||||||
|
static void SetAndroidContext(JNIEnv* jni, jobject context) {}
|
||||||
|
|
||||||
void Start() override;
|
void Start() override;
|
||||||
void Stop() override;
|
void Stop() override;
|
||||||
|
|||||||
@ -1145,7 +1145,6 @@ JOW(void, PeerConnectionFactory_nativeInitializeAndroidGlobals)
|
|||||||
jobject context,
|
jobject context,
|
||||||
jboolean video_hw_acceleration) {
|
jboolean video_hw_acceleration) {
|
||||||
video_hw_acceleration_enabled = video_hw_acceleration;
|
video_hw_acceleration_enabled = video_hw_acceleration;
|
||||||
AndroidNetworkMonitor::SetAndroidContext(jni, context);
|
|
||||||
if (!factory_static_initialized) {
|
if (!factory_static_initialized) {
|
||||||
RTC_DCHECK(j_application_context == nullptr);
|
RTC_DCHECK(j_application_context == nullptr);
|
||||||
j_application_context = NewGlobalRef(jni, context);
|
j_application_context = NewGlobalRef(jni, context);
|
||||||
|
|||||||
Reference in New Issue
Block a user