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";
|
||||
|
||||
// We are storing application context so it is okay.
|
||||
@SuppressLint("StaticFieldLeak") private static NetworkMonitor instance;
|
||||
|
||||
private final Context applicationContext;
|
||||
private static NetworkMonitor instance;
|
||||
|
||||
// Native observers of the connection type changes.
|
||||
private final ArrayList<Long> nativeNetworkObservers;
|
||||
@ -53,33 +51,22 @@ public class NetworkMonitor {
|
||||
|
||||
private ConnectionType currentConnectionType = ConnectionType.CONNECTION_UNKNOWN;
|
||||
|
||||
private NetworkMonitor(Context context) {
|
||||
assertIsTrue(context != null);
|
||||
applicationContext = context.getApplicationContext();
|
||||
|
||||
private NetworkMonitor() {
|
||||
nativeNetworkObservers = new ArrayList<Long>();
|
||||
networkObservers = new ArrayList<NetworkObserver>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the singleton once.
|
||||
* Called from the native code.
|
||||
*/
|
||||
public static NetworkMonitor init(Context context) {
|
||||
if (!isInitialized()) {
|
||||
instance = new NetworkMonitor(context);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static boolean isInitialized() {
|
||||
return instance != null;
|
||||
}
|
||||
// TODO(sakal): Remove once downstream dependencies have been updated.
|
||||
@Deprecated
|
||||
public static void init(Context context) {}
|
||||
|
||||
/**
|
||||
* Returns the singleton instance.
|
||||
*/
|
||||
public static NetworkMonitor getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new NetworkMonitor();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
@ -163,7 +150,7 @@ public class NetworkMonitor {
|
||||
public void onNetworkDisconnect(long networkHandle) {
|
||||
notifyObserversOfNetworkDisconnect(networkHandle);
|
||||
}
|
||||
}, applicationContext);
|
||||
}, ContextUtils.getApplicationContext());
|
||||
final NetworkMonitorAutoDetect.NetworkState networkState =
|
||||
autoDetector.getCurrentNetworkState();
|
||||
updateCurrentConnectionType(NetworkMonitorAutoDetect.getConnectionType(networkState));
|
||||
@ -250,8 +237,8 @@ public class NetworkMonitor {
|
||||
long nativePtr, NetworkInformation[] networkInfos);
|
||||
|
||||
// For testing only.
|
||||
static void resetInstanceForTests(Context context) {
|
||||
instance = new NetworkMonitor(context);
|
||||
static void resetInstanceForTests() {
|
||||
instance = new NetworkMonitor();
|
||||
}
|
||||
|
||||
// For testing only.
|
||||
|
||||
@ -161,7 +161,7 @@ public class NetworkMonitorTest {
|
||||
*/
|
||||
private void createTestMonitor() {
|
||||
Context context = InstrumentationRegistry.getTargetContext();
|
||||
NetworkMonitor.resetInstanceForTests(context);
|
||||
NetworkMonitor.resetInstanceForTests();
|
||||
NetworkMonitor.setAutoDetectConnectivityState(true);
|
||||
receiver = NetworkMonitor.getAutoDetectorForTest();
|
||||
assertNotNull(receiver);
|
||||
@ -182,6 +182,7 @@ public class NetworkMonitorTest {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
ContextUtils.initialize(InstrumentationRegistry.getTargetContext());
|
||||
createTestMonitor();
|
||||
}
|
||||
|
||||
|
||||
@ -27,7 +27,6 @@ enum AndroidSdkVersion {
|
||||
SDK_VERSION_MARSHMALLOW = 23
|
||||
};
|
||||
|
||||
jobject AndroidNetworkMonitor::application_context_ = nullptr;
|
||||
int AndroidNetworkMonitor::android_sdk_int_ = 0;
|
||||
|
||||
static NetworkType GetNetworkTypeFromJava(JNIEnv* jni, jobject j_network_type) {
|
||||
@ -163,14 +162,6 @@ std::string NetworkInformation::ToString() const {
|
||||
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()
|
||||
: j_network_monitor_class_(jni(),
|
||||
FindClass(jni(), "org/webrtc/NetworkMonitor")),
|
||||
@ -178,13 +169,10 @@ AndroidNetworkMonitor::AndroidNetworkMonitor()
|
||||
jni(),
|
||||
jni()->CallStaticObjectMethod(
|
||||
*j_network_monitor_class_,
|
||||
GetStaticMethodID(
|
||||
jni(),
|
||||
GetStaticMethodID(jni(),
|
||||
*j_network_monitor_class_,
|
||||
"init",
|
||||
"(Landroid/content/Context;)Lorg/webrtc/NetworkMonitor;"),
|
||||
application_context_)) {
|
||||
RTC_DCHECK(application_context_ != nullptr);
|
||||
"getInstance",
|
||||
"()Lorg/webrtc/NetworkMonitor;"))) {
|
||||
CHECK_EXCEPTION(jni()) << "Error during NetworkMonitor.init";
|
||||
if (android_sdk_int_ <= 0) {
|
||||
jmethodID m = GetStaticMethodID(jni(), *j_network_monitor_class_,
|
||||
|
||||
@ -53,7 +53,8 @@ class AndroidNetworkMonitor : public rtc::NetworkMonitorBase,
|
||||
public:
|
||||
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 Stop() override;
|
||||
|
||||
@ -1145,7 +1145,6 @@ JOW(void, PeerConnectionFactory_nativeInitializeAndroidGlobals)
|
||||
jobject context,
|
||||
jboolean video_hw_acceleration) {
|
||||
video_hw_acceleration_enabled = video_hw_acceleration;
|
||||
AndroidNetworkMonitor::SetAndroidContext(jni, context);
|
||||
if (!factory_static_initialized) {
|
||||
RTC_DCHECK(j_application_context == nullptr);
|
||||
j_application_context = NewGlobalRef(jni, context);
|
||||
|
||||
Reference in New Issue
Block a user