1/*
2 * libjingle
3 * Copyright 2015 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/java/jni/androidnetworkmonitor_jni.h"
29
30#include "webrtc/base/common.h"
31#include "talk/app/webrtc/java/jni/classreferenceholder.h"
32#include "talk/app/webrtc/java/jni/jni_helpers.h"
33
34namespace webrtc_jni {
35jobject AndroidNetworkMonitor::application_context_ = nullptr;
36
37// static
38void AndroidNetworkMonitor::SetAndroidContext(JNIEnv* jni, jobject context) {
39  if (application_context_) {
40    jni->DeleteGlobalRef(application_context_);
41  }
42  application_context_ = NewGlobalRef(jni, context);
43}
44
45AndroidNetworkMonitor::AndroidNetworkMonitor()
46    : j_network_monitor_class_(jni(),
47                               FindClass(jni(), "org/webrtc/NetworkMonitor")),
48      j_network_monitor_(
49          jni(),
50          jni()->CallStaticObjectMethod(
51              *j_network_monitor_class_,
52              GetStaticMethodID(
53                  jni(),
54                  *j_network_monitor_class_,
55                  "init",
56                  "(Landroid/content/Context;)Lorg/webrtc/NetworkMonitor;"),
57              application_context_)) {
58  ASSERT(application_context_ != nullptr);
59  CHECK_EXCEPTION(jni()) << "Error during NetworkMonitor.init";
60}
61
62void AndroidNetworkMonitor::Start() {
63  RTC_CHECK(thread_checker_.CalledOnValidThread());
64  jmethodID m =
65      GetMethodID(jni(), *j_network_monitor_class_, "startMonitoring", "(J)V");
66  jni()->CallVoidMethod(*j_network_monitor_, m, jlongFromPointer(this));
67  CHECK_EXCEPTION(jni()) << "Error during NetworkMonitor.startMonitoring";
68}
69
70void AndroidNetworkMonitor::Stop() {
71  RTC_CHECK(thread_checker_.CalledOnValidThread());
72  jmethodID m =
73      GetMethodID(jni(), *j_network_monitor_class_, "stopMonitoring", "(J)V");
74  jni()->CallVoidMethod(*j_network_monitor_, m, jlongFromPointer(this));
75  CHECK_EXCEPTION(jni()) << "Error during NetworkMonitor.stopMonitoring";
76}
77
78JOW(void, NetworkMonitor_nativeNotifyConnectionTypeChanged)(
79    JNIEnv* jni, jobject j_monitor, jlong j_native_monitor) {
80  rtc::NetworkMonitorInterface* network_monitor =
81      reinterpret_cast<rtc::NetworkMonitorInterface*>(j_native_monitor);
82  network_monitor->OnNetworksChanged();
83}
84
85}  // namespace webrtc_jni
86