Commit 99de75eb authored by Mickaël Turnel's avatar Mickaël Turnel

Merge branch 'feature/android_connectivity_manager'

parents 44981477 ff2ea6c3
Pipeline #543 canceled with stage
in 0 seconds
......@@ -2096,6 +2096,8 @@ void linphone_configuring_terminated(LinphoneCore *lc, LinphoneConfiguringState
lc->provisioning_http_listener = NULL;
}
getPlatformHelpers(lc)->onLinphoneCoreReady(lc->auto_net_state_mon);
linphone_core_set_state(lc,LinphoneGlobalOn,"Ready");
}
......@@ -2333,7 +2335,7 @@ static void linphone_core_init(LinphoneCore * lc, LinphoneCoreCbs *cbs, LpConfig
lc->platform_helper = LinphonePrivate::createIosPlatformHelpers(lc, system_context);
#endif
if (lc->platform_helper == NULL)
lc->platform_helper = new LinphonePrivate::StubbedPlatformHelpers(lc);
lc->platform_helper = new LinphonePrivate::GenericPlatformHelpers(lc);
msplugins_dir = linphone_factory_get_msplugins_dir(lfactory);
image_resources_dir = linphone_factory_get_image_resources_dir(lfactory);
......@@ -2373,9 +2375,12 @@ static void linphone_core_init(LinphoneCore * lc, LinphoneCoreCbs *cbs, LpConfig
lc->tunnel=linphone_core_tunnel_new(lc);
#endif
lc->network_last_check = 0;
lc->network_last_status = FALSE;
lc->network_reachable = TRUE;
lc->sip_network_reachable = TRUE;
lc->media_network_reachable = TRUE;
/* Create the http provider in dual stack mode (ipv4 and ipv6.
* If this creates problem, we may need to implement parallel ipv6/ ipv4 http requests in belle-sip.
*/
......@@ -3229,6 +3234,17 @@ void linphone_core_enable_ipv6(LinphoneCore *lc, bool_t val){
}
}
bool_t linphone_core_wifi_only_enabled(LinphoneCore *lc) {
return (bool_t)lp_config_get_int(lc->config, "net", "wifi_only", 0);
}
void linphone_core_enable_wifi_only(LinphoneCore *lc, bool_t val) {
if (linphone_core_ready(lc)) {
lp_config_set_int(lc->config, "net", "wifi_only", (int)val);
getPlatformHelpers(lc)->onWifiOnlyEnabled(val);
}
}
bool_t linphone_core_content_encoding_supported(const LinphoneCore *lc, const char *content_encoding) {
const char *handle_content_encoding = lp_config_get_string(lc->config, "sip", "handle_content_encoding", "deflate");
return (strcmp(handle_content_encoding, content_encoding) == 0) && lc->sal->isContentEncodingAvailable(content_encoding);
......@@ -3244,38 +3260,6 @@ static void notify_network_reachable_change (LinphoneCore *lc) {
linphone_core_resolve_stun_server(lc);
}
static void monitor_network_state(LinphoneCore *lc, time_t curtime){
bool_t new_status=lc->network_last_status;
char newip[LINPHONE_IPADDR_SIZE];
// only do the network up checking every five seconds
if (lc->network_last_check==0 || (curtime-lc->network_last_check)>=5){
linphone_core_get_local_ip(lc,AF_UNSPEC,NULL,newip);
if (strcmp(newip,"::1")!=0 && strcmp(newip,"127.0.0.1")!=0){
new_status=TRUE;
}else new_status=FALSE; //no network
if (new_status==lc->network_last_status && new_status==TRUE && strcmp(newip,lc->localip)!=0){
//IP address change detected
ms_message("IP address change detected.");
set_network_reachable(lc,FALSE,curtime);
lc->network_last_status=FALSE;
}
strncpy(lc->localip,newip,sizeof(lc->localip));
if (new_status!=lc->network_last_status) {
if (new_status){
ms_message("New local ip address is %s",lc->localip);
}
set_network_reachable(lc,new_status, curtime);
lc->network_last_status=new_status;
}
lc->network_last_check=curtime;
}
notify_network_reachable_change(lc);
}
static void proxy_update(LinphoneCore *lc){
bctbx_list_t *elem,*next;
bctbx_list_for_each(lc->sip_conf.proxies,(void (*)(void*))&linphone_proxy_config_update);
......@@ -3420,7 +3404,6 @@ void linphone_core_iterate(LinphoneCore *lc){
lc->sal->iterate();
if (lc->msevq) ms_event_queue_pump(lc->msevq);
if (lc->auto_net_state_mon) monitor_network_state(lc, current_real_time);
proxy_update(lc);
......@@ -6393,20 +6376,29 @@ static void disable_internal_network_reachability_detection(LinphoneCore *lc){
}
}
void linphone_core_set_network_reachable(LinphoneCore *lc, bool_t isReachable) {
disable_internal_network_reachability_detection(lc);
set_network_reachable(lc, isReachable, ms_time(NULL));
void linphone_core_set_network_reachable_internal(LinphoneCore *lc, bool_t is_reachable) {
if (lc->auto_net_state_mon) {
set_network_reachable(lc, lc->network_reachable && is_reachable, ms_time(NULL));
notify_network_reachable_change(lc);
}
}
void linphone_core_set_network_reachable(LinphoneCore *lc, bool_t is_reachable) {
bool_t reachable = is_reachable;
if (lc->auto_net_state_mon) reachable = reachable && getPlatformHelpers(lc)->isNetworkReachable();
lc->network_reachable = is_reachable;
set_network_reachable(lc, reachable, ms_time(NULL));
notify_network_reachable_change(lc);
}
void linphone_core_set_media_network_reachable(LinphoneCore *lc, bool_t is_reachable){
disable_internal_network_reachability_detection(lc);
set_media_network_reachable(lc, is_reachable);
notify_network_reachable_change(lc);
}
void linphone_core_set_sip_network_reachable(LinphoneCore *lc, bool_t is_reachable){
disable_internal_network_reachability_detection(lc);
set_sip_network_reachable(lc, is_reachable, ms_time(NULL));
notify_network_reachable_change(lc);
}
......
......@@ -189,6 +189,8 @@ int parse_hostname_to_addr(const char *server, struct sockaddr_storage *ss, sock
bool_t host_has_ipv6_network(void);
bool_t lp_spawn_command_line_sync(const char *command, char **result,int *command_ret);
void linphone_core_set_network_reachable_internal(LinphoneCore *lc, bool_t is_reachable);
static MS2_INLINE void set_string(char **dest, const char *src, bool_t lowercase){
if (*dest){
ms_free(*dest);
......
......@@ -769,7 +769,6 @@ namespace LinphonePrivate {
char* zrtp_secrets_cache; \
char* user_certificates_path; \
LinphoneVideoPolicy video_policy; \
time_t network_last_check; \
LinphoneNatPolicy *nat_policy; \
LinphoneImNotifPolicy *im_notif_policy; \
bool_t use_files; \
......@@ -778,6 +777,7 @@ namespace LinphonePrivate {
bool_t bl_refresh; \
bool_t preview_finished; \
bool_t auto_net_state_mon; \
bool_t network_reachable; \
bool_t sip_network_reachable; \
bool_t media_network_reachable; \
bool_t network_reachable_to_be_notified; \
......
......@@ -1555,6 +1555,22 @@ LINPHONE_PUBLIC bool_t linphone_core_ipv6_enabled(LinphoneCore *lc);
**/
LINPHONE_PUBLIC void linphone_core_enable_ipv6(LinphoneCore *lc, bool_t val);
/**
* Tells whether Wifi only mode is enabled or not
* @param[in] lc #LinphoneCore object
* @return A boolean value telling whether Wifi only mode is enabled or not
* @ingroup network_parameters
**/
LINPHONE_PUBLIC bool_t linphone_core_wifi_only_enabled(LinphoneCore *lc);
/**
* Turns Wifi only mode on or off
* @param[in] lc #LinphoneCore object
* @param[in] val A boolean value telling whether to enable IPv6 support
* @ingroup network_parameters
**/
LINPHONE_PUBLIC void linphone_core_enable_wifi_only(LinphoneCore *lc, bool_t val);
/**
* Same as linphone_core_get_primary_contact() but the result is a #LinphoneAddress object
* instead of const char *.
......
......@@ -48,6 +48,11 @@ public:
string getConfigPath () override;
void setVideoWindow (void *windowId) override;
void setVideoPreviewWindow (void *windowId) override;
void setNetworkReachable (bool reachable) override;
bool isNetworkReachable () override;
void onLinphoneCoreReady (bool monitoringEnabled) override;
void onWifiOnlyEnabled (bool enabled) override;
void setHttpProxy (string host, int port) override;
void _setPreviewVideoWindow(jobject window);
void _setVideoWindow(jobject window);
......@@ -56,6 +61,7 @@ private:
int callVoidMethod (jmethodID id);
static jmethodID getMethodId (JNIEnv *env, jclass klass, const char *method, const char *signature);
string getNativeLibraryDir();
jobject mJavaHelper;
jmethodID mWifiLockAcquireId;
jmethodID mWifiLockReleaseId;
......@@ -70,8 +76,13 @@ private:
jmethodID mGetNativeLibraryDirId;
jmethodID mSetNativeVideoWindowId;
jmethodID mSetNativePreviewVideoWindowId;
jmethodID mUpdateNetworkReachabilityId;
jmethodID mOnLinphoneCoreReadyId;
jmethodID mOnWifiOnlyEnabledId;
jobject mPreviewVideoWindow;
jobject mVideoWindow;
bool mNetworkReachable;
};
static const char *GetStringUTFChars (JNIEnv *env, jstring string) {
......@@ -96,8 +107,8 @@ AndroidPlatformHelpers::AndroidPlatformHelpers (LinphoneCore *lc, void *systemCo
if (!klass)
lFatal() << "Could not find java AndroidPlatformHelper class.";
jmethodID ctor = env->GetMethodID(klass, "<init>", "(JLjava/lang/Object;)V");
mJavaHelper = env->NewObject(klass, ctor, this, (jobject)systemContext);
jmethodID ctor = env->GetMethodID(klass, "<init>", "(JLjava/lang/Object;Z)V");
mJavaHelper = env->NewObject(klass, ctor, (jlong)this, (jobject)systemContext, (jboolean)linphone_core_wifi_only_enabled(lc));
if (!mJavaHelper) {
lError() << "Could not instanciate AndroidPlatformHelper object.";
return;
......@@ -117,6 +128,9 @@ AndroidPlatformHelpers::AndroidPlatformHelpers (LinphoneCore *lc, void *systemCo
mGetNativeLibraryDirId = getMethodId(env, klass, "getNativeLibraryDir", "()Ljava/lang/String;");
mSetNativeVideoWindowId = getMethodId(env, klass, "setVideoRenderingView", "(Ljava/lang/Object;)V");
mSetNativePreviewVideoWindowId = getMethodId(env, klass, "setVideoPreviewView", "(Ljava/lang/Object;)V");
mUpdateNetworkReachabilityId = getMethodId(env, klass, "updateNetworkReachability", "()V");
mOnLinphoneCoreReadyId = getMethodId(env, klass, "onLinphoneCoreReady", "(Z)V");
mOnWifiOnlyEnabledId = getMethodId(env, klass, "onWifiOnlyEnabled", "(Z)V");
jobject pm = env->CallObjectMethod(mJavaHelper, mGetPowerManagerId);
belle_sip_wake_lock_init(env, pm);
......@@ -127,6 +141,7 @@ AndroidPlatformHelpers::AndroidPlatformHelpers (LinphoneCore *lc, void *systemCo
mPreviewVideoWindow = nullptr;
mVideoWindow = nullptr;
mNetworkReachable = false;
}
AndroidPlatformHelpers::~AndroidPlatformHelpers () {
......@@ -294,6 +309,34 @@ void AndroidPlatformHelpers::_setVideoWindow(jobject window) {
_linphone_core_set_native_video_window_id(lc, (void *)mVideoWindow);
}
void AndroidPlatformHelpers::setNetworkReachable(bool reachable) {
mNetworkReachable = reachable;
linphone_core_set_network_reachable_internal(mCore, reachable ? 1 : 0);
}
bool AndroidPlatformHelpers::isNetworkReachable() {
return mNetworkReachable;
}
void AndroidPlatformHelpers::onLinphoneCoreReady(bool monitoringEnabled) {
JNIEnv *env = ms_get_jni_env();
if (env && mJavaHelper) {
env->CallVoidMethod(mJavaHelper, mOnLinphoneCoreReadyId, (jboolean)monitoringEnabled);
}
}
void AndroidPlatformHelpers::onWifiOnlyEnabled(bool enabled) {
JNIEnv *env = ms_get_jni_env();
if (env && mJavaHelper) {
env->CallVoidMethod(mJavaHelper, mOnWifiOnlyEnabledId, (jboolean)enabled);
}
}
void AndroidPlatformHelpers::setHttpProxy(string host, int port) {
linphone_core_set_http_proxy_host(mCore, host.c_str());
linphone_core_set_http_proxy_port(mCore, port);
}
PlatformHelpers *createAndroidPlatformHelpers (LinphoneCore *lc, void *systemContext) {
return new AndroidPlatformHelpers(lc, systemContext);
}
......@@ -308,4 +351,15 @@ extern "C" JNIEXPORT void JNICALL Java_org_linphone_core_tools_AndroidPlatformHe
androidPlatformHelper->_setVideoWindow(id);
}
extern "C" JNIEXPORT void JNICALL Java_org_linphone_core_tools_AndroidPlatformHelper_setNetworkReachable(JNIEnv* env, jobject thiz, jlong ptr, jboolean reachable) {
AndroidPlatformHelpers *androidPlatformHelper = (AndroidPlatformHelpers *)ptr;
androidPlatformHelper->setNetworkReachable(reachable);
}
extern "C" JNIEXPORT void JNICALL Java_org_linphone_core_tools_AndroidPlatformHelper_setHttpProxy(JNIEnv* env, jobject thiz, jlong ptr, jstring host, jint port) {
AndroidPlatformHelpers *androidPlatformHelper = (AndroidPlatformHelpers *)ptr;
const char *hostC = GetStringUTFChars(env, host);
androidPlatformHelper->setHttpProxy(hostC, port);
}
LINPHONE_END_NAMESPACE
......@@ -57,6 +57,11 @@ public:
string getConfigPath () override {return Utils::getEmptyConstRefObject<string>();}
void setVideoWindow (void *windowId) override {}
void setVideoPreviewWindow (void *windowId) override {}
void setNetworkReachable (bool reachable) override {}
bool isNetworkReachable () override {}
void onLinphoneCoreReady (bool monitoringEnabled) override {}
void onWifiOnlyEnabled (bool enabled) override {}
void setHttpProxy (string host, int port) override {}
private:
void bgTaskTimeout ();
......
......@@ -18,6 +18,8 @@
*/
#include "platform-helpers.h"
#include "logger/logger.h"
#include "c-wrapper/c-wrapper.h"
// =============================================================================
......@@ -25,36 +27,91 @@ using namespace std;
LINPHONE_BEGIN_NAMESPACE
StubbedPlatformHelpers::StubbedPlatformHelpers (LinphoneCore *lc) : PlatformHelpers(lc) {}
GenericPlatformHelpers::GenericPlatformHelpers (LinphoneCore *lc) : PlatformHelpers(lc), mMonitorTimer(nullptr) {}
void StubbedPlatformHelpers::setDnsServers () {}
GenericPlatformHelpers::~GenericPlatformHelpers () {
if (mMonitorTimer) {
if (mCore && mCore->sal) mCore->sal->cancelTimer(mMonitorTimer);
belle_sip_object_unref(mMonitorTimer);
mMonitorTimer = nullptr;
}
}
void GenericPlatformHelpers::setDnsServers () {}
void StubbedPlatformHelpers::acquireWifiLock () {}
void GenericPlatformHelpers::acquireWifiLock () {}
void StubbedPlatformHelpers::releaseWifiLock () {}
void GenericPlatformHelpers::releaseWifiLock () {}
void StubbedPlatformHelpers::acquireMcastLock () {}
void GenericPlatformHelpers::acquireMcastLock () {}
void StubbedPlatformHelpers::releaseMcastLock () {}
void GenericPlatformHelpers::releaseMcastLock () {}
void StubbedPlatformHelpers::acquireCpuLock () {}
void GenericPlatformHelpers::acquireCpuLock () {}
void StubbedPlatformHelpers::releaseCpuLock () {}
void GenericPlatformHelpers::releaseCpuLock () {}
string StubbedPlatformHelpers::getDataPath () {
string GenericPlatformHelpers::getDataPath () {
return "";
}
string StubbedPlatformHelpers::getConfigPath () {
string GenericPlatformHelpers::getConfigPath () {
return "";
}
void StubbedPlatformHelpers::setVideoPreviewWindow (void *windowId) {
void GenericPlatformHelpers::setVideoPreviewWindow (void *windowId) {}
void GenericPlatformHelpers::setVideoWindow (void *windowId) {}
void GenericPlatformHelpers::setNetworkReachable (bool reachable) {
mNetworkReachable = reachable;
linphone_core_set_network_reachable_internal(mCore, reachable);
}
bool GenericPlatformHelpers::isNetworkReachable () {
return mNetworkReachable;
}
void GenericPlatformHelpers::onLinphoneCoreReady (bool monitoringEnabled) {
if (!monitoringEnabled) return;
if (!mMonitorTimer) {
mMonitorTimer = mCore->sal->createTimer(monitorTimerExpired, this,
mDefaultMonitorTimeout * 1000, "monitor network timeout");
} else {
belle_sip_source_set_timeout(mMonitorTimer, mDefaultMonitorTimeout * 1000);
}
}
void StubbedPlatformHelpers::setVideoWindow (void *windowId) {
void GenericPlatformHelpers::onWifiOnlyEnabled (bool enabled) {}
void GenericPlatformHelpers::setHttpProxy (string host, int port) {}
int GenericPlatformHelpers::monitorTimerExpired (void *data, unsigned int revents) {
GenericPlatformHelpers *helper = static_cast<GenericPlatformHelpers *>(data);
LinphoneCore *core = helper->getCore();
char newIp[LINPHONE_IPADDR_SIZE];
linphone_core_get_local_ip(core, AF_UNSPEC, nullptr, newIp);
bool status = strcmp(newIp,"::1") != 0 && strcmp(newIp,"127.0.0.1") != 0;
if (status && core->network_last_status && strcmp(newIp, core->localip) != 0) {
lInfo() << "IP address change detected";
helper->setNetworkReachable(false);
core->network_last_status = FALSE;
}
strncpy(core->localip, newIp, sizeof core->localip);
if (bool_t(status) != core->network_last_status) {
if (status) {
lInfo() << "New local ip address is " << core->localip;
}
helper->setNetworkReachable(status);
core->network_last_status = status;
}
return BELLE_SIP_CONTINUE;
}
LINPHONE_END_NAMESPACE
......@@ -24,6 +24,9 @@
#include "linphone/utils/general.h"
// TODO: Remove me later.
#include "private.h"
// =============================================================================
L_DECL_C_STRUCT(LinphoneCore);
......@@ -52,6 +55,11 @@ public:
virtual std::string getConfigPath () = 0;
virtual void setVideoWindow (void *windowId) = 0;
virtual void setVideoPreviewWindow (void *windowId) = 0;
virtual void setNetworkReachable (bool reachable) = 0;
virtual bool isNetworkReachable () = 0;
virtual void onLinphoneCoreReady (bool monitoringEnabled) = 0;
virtual void onWifiOnlyEnabled (bool enabled) = 0;
virtual void setHttpProxy (std::string host, int port) = 0;
protected:
inline explicit PlatformHelpers (LinphoneCore *lc) : mCore(lc) {}
......@@ -59,10 +67,10 @@ protected:
LinphoneCore *mCore;
};
class StubbedPlatformHelpers : public PlatformHelpers {
class GenericPlatformHelpers : public PlatformHelpers {
public:
explicit StubbedPlatformHelpers (LinphoneCore *lc);
virtual ~StubbedPlatformHelpers () = default;
explicit GenericPlatformHelpers (LinphoneCore *lc);
~GenericPlatformHelpers ();
void setDnsServers () override;
void acquireWifiLock () override;
......@@ -75,6 +83,20 @@ public:
std::string getConfigPath () override;
void setVideoWindow (void *windowId) override;
void setVideoPreviewWindow (void *windowId) override;
void setNetworkReachable (bool reachable) override;
bool isNetworkReachable () override;
void onLinphoneCoreReady (bool monitoringEnabled) override;
void onWifiOnlyEnabled (bool enabled) override;
void setHttpProxy (std::string host, int port) override;
private:
static int monitorTimerExpired (void *data, unsigned int revents);
private:
static const int mDefaultMonitorTimeout = 5;
belle_sip_source_t *mMonitorTimer;
bool mNetworkReachable;
};
PlatformHelpers *createAndroidPlatformHelpers (LinphoneCore *lc, void *systemContext);
......
......@@ -21,6 +21,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
package org.linphone.core.tools;
import org.linphone.core.Core;
import org.linphone.core.tools.DozeReceiver;
import org.linphone.core.tools.NetworkManager;
import org.linphone.core.tools.NetworkManagerAbove21;
import org.linphone.mediastream.Log;
import org.linphone.mediastream.MediastreamerAndroidContext;
import org.linphone.mediastream.Version;
......@@ -30,11 +33,14 @@ import android.graphics.SurfaceTexture;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.net.wifi.WifiManager.WifiLock;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.net.ProxyInfo;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.Build;
......@@ -59,6 +65,7 @@ public class AndroidPlatformHelper {
private WifiManager.WifiLock mWifiLock;
private WifiManager.MulticastLock mMcastLock;
private ConnectivityManager mConnectivityManager;
private int mLastNetworkType = -1;
private PowerManager mPowerManager;
private WakeLock mWakeLock;
private Resources mResources;
......@@ -71,13 +78,24 @@ public class AndroidPlatformHelper {
private String mGrammarVcardFile ;
private String mUserCertificatePath;
private Surface mSurface;
private boolean dozeModeEnabled;
private BroadcastReceiver mDozeReceiver;
private BroadcastReceiver mNetworkReceiver;
private IntentFilter mDozeIntentFilter;
private IntentFilter mNetworkIntentFilter;
private boolean mWifiOnly;
private boolean mUsingHttpProxy;
private NetworkManagerAbove21 mNetworkManagerAbove21;
private native void setNativePreviewWindowId(long nativePtr, Object view);
private native void setNativeVideoWindowId(long nativePtr, Object view);
private native void setNetworkReachable(long nativePtr, boolean reachable);
private native void setHttpProxy(long nativePtr, String host, int port);
public AndroidPlatformHelper(long nativePtr, Object ctx_obj) {
public AndroidPlatformHelper(long nativePtr, Object ctx_obj, boolean wifiOnly) {
mNativePtr = nativePtr;
mContext = (Context) ctx_obj;
mWifiOnly = wifiOnly;
mResources = mContext.getResources();
MediastreamerAndroidContext.setContext(mContext);
......@@ -110,6 +128,34 @@ public class AndroidPlatformHelper {
}
}
public void onLinphoneCoreReady(boolean monitoringEnabled) {
if (!monitoringEnabled) return;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
mNetworkReceiver = new NetworkManager(this);
mNetworkIntentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
mContext.registerReceiver(mNetworkReceiver, mNetworkIntentFilter);
} else {
mNetworkManagerAbove21 = new NetworkManagerAbove21(this);
mNetworkManagerAbove21.registerNetworkCallbacks(mConnectivityManager);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mDozeIntentFilter = new IntentFilter();
mDozeIntentFilter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
mDozeReceiver = new DozeReceiver(this);
dozeModeEnabled = ((PowerManager) mContext.getSystemService(Context.POWER_SERVICE)).isDeviceIdleMode();
mContext.registerReceiver(mDozeReceiver, mDozeIntentFilter);
}
updateNetworkReachability();
}
public void onWifiOnlyEnabled(boolean enabled) {
mWifiOnly = enabled;
updateNetworkReachability();
}
public Object getPowerManager() {
return mPowerManager;
}
......@@ -347,6 +393,59 @@ public class AndroidPlatformHelper {
setNativeVideoWindowId(mNativePtr, mSurface);
}
}
public void updateNetworkReachability() {
if (mConnectivityManager == null) return;
boolean usingHttpProxyBefore = mUsingHttpProxy;
boolean connected = false;
NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
connected = networkInfo != null && networkInfo.isConnected();
if (connected && Build.VERSION.SDK_INT >= Version.API23_MARSHMALLOW_60){
ProxyInfo proxy = mConnectivityManager.getDefaultProxy();
if (proxy != null && proxy.getHost() != null){
Log.i("The active network is using an http proxy: " + proxy.toString());
setHttpProxy(mNativePtr, proxy.getHost(), proxy.getPort());
mUsingHttpProxy = true;
}else{
setHttpProxy(mNativePtr, "", 0);
mUsingHttpProxy = false;
}
}
if (networkInfo == null || !connected) {
Log.i("No connectivity: setting network unreachable");
setNetworkReachable(mNativePtr, false);
} else if (dozeModeEnabled) {
Log.i("Doze Mode enabled: shutting down network");
setNetworkReachable(mNativePtr, false);
} else if (connected) {
if (mWifiOnly) {
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
setNetworkReachable(mNativePtr, true);
} else {
Log.i("Wifi-only mode, setting network not reachable");
setNetworkReachable(mNativePtr, false);
}
} else {
int curtype = networkInfo.getType();
if (curtype != mLastNetworkType || mUsingHttpProxy != usingHttpProxyBefore) {
//if kind of network has changed, we need to notify network_reachable(false) to make sure all current connections are destroyed.
//they will be re-created during setNetworkReachable(true).
Log.i("Connectivity has changed.");
setNetworkReachable(mNativePtr, false);
}
setNetworkReachable(mNativePtr, true);
mLastNetworkType = curtype;
}
}
}
public void setDozeModeEnabled(boolean b) {
dozeModeEnabled = b;
}
};
/*
DozeReceiver.java
Copyright (C) 2017 Belledonne Communications, Grenoble, France
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.linphone.core.tools;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.PowerManager;
import org.linphone.core.tools.AndroidPlatformHelper;
/*
* Purpose of this receiver is to disable keep alives when device is on idle
* */
public class DozeReceiver extends android.content.BroadcastReceiver {
private AndroidPlatformHelper mHelper;
public DozeReceiver(AndroidPlatformHelper helper) {
mHelper = helper;
}
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
boolean dozeM = pm.isDeviceIdleMode();
if (mHelper != null) {
mHelper.setDozeModeEnabled(dozeM);
mHelper.updateNetworkReachability();
}
}
}
}
/*
NetworkManager.java
Copyright (C) 2017 Belledonne Communications, Grenoble, France
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.linphone.core.tools;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import org.linphone.core.tools.AndroidPlatformHelper;
/**
* Intercept network state changes and update linphone core.
*/
public class NetworkManager extends BroadcastReceiver {
private AndroidPlatformHelper mHelper;