Commit 5305754f authored by Mickaël Turnel's avatar Mickaël Turnel

Network avaibility is now done in platform helpers

parent 7c13573c
......@@ -2096,6 +2096,8 @@ void linphone_configuring_terminated(LinphoneCore *lc, LinphoneConfiguringState
lc->provisioning_http_listener = NULL;
}
getPlatformHelpers(lc)->onLinphoneCoreReady();
linphone_core_set_state(lc,LinphoneGlobalOn,"Ready");
}
......
......@@ -48,6 +48,8 @@ public:
string getConfigPath () override;
void setVideoWindow (void *windowId) override;
void setVideoPreviewWindow (void *windowId) override;
void setNetworkReachable (bool reachable) override;
void onLinphoneCoreReady () override;
void _setPreviewVideoWindow(jobject window);
void _setVideoWindow(jobject window);
......@@ -70,6 +72,8 @@ private:
jmethodID mGetNativeLibraryDirId;
jmethodID mSetNativeVideoWindowId;
jmethodID mSetNativePreviewVideoWindowId;
jmethodID mUpdateNetworkReachabilityId;
jmethodID mOnLinphoneCoreReadyId;
jobject mPreviewVideoWindow;
jobject mVideoWindow;
};
......@@ -117,6 +121,8 @@ 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", "()V");
jobject pm = env->CallObjectMethod(mJavaHelper, mGetPowerManagerId);
belle_sip_wake_lock_init(env, pm);
......@@ -294,6 +300,17 @@ void AndroidPlatformHelpers::_setVideoWindow(jobject window) {
_linphone_core_set_native_video_window_id(lc, (void *)mVideoWindow);
}
void AndroidPlatformHelpers::setNetworkReachable(bool reachable) {
linphone_core_set_network_reachable(mCore, reachable);
}
void AndroidPlatformHelpers::onLinphoneCoreReady() {
JNIEnv *env = ms_get_jni_env();
if (env && mJavaHelper) {
env->CallVoidMethod(mJavaHelper, mOnLinphoneCoreReadyId);
}
}
PlatformHelpers *createAndroidPlatformHelpers (LinphoneCore *lc, void *systemContext) {
return new AndroidPlatformHelpers(lc, systemContext);
}
......@@ -308,4 +325,9 @@ 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);
}
LINPHONE_END_NAMESPACE
......@@ -49,12 +49,12 @@ string StubbedPlatformHelpers::getConfigPath () {
return "";
}
void StubbedPlatformHelpers::setVideoPreviewWindow (void *windowId) {
void StubbedPlatformHelpers::setVideoPreviewWindow (void *windowId) {}
}
void StubbedPlatformHelpers::setVideoWindow (void *windowId) {}
void StubbedPlatformHelpers::setVideoWindow (void *windowId) {
void StubbedPlatformHelpers::setNetworkReachable (bool reachable) {}
}
void StubbedPlatformHelpers::onLinphoneCoreReady () {}
LINPHONE_END_NAMESPACE
......@@ -52,6 +52,8 @@ 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 void onLinphoneCoreReady () = 0;
protected:
inline explicit PlatformHelpers (LinphoneCore *lc) : mCore(lc) {}
......@@ -75,6 +77,8 @@ public:
std::string getConfigPath () override;
void setVideoWindow (void *windowId) override;
void setVideoPreviewWindow (void *windowId) override;
void setNetworkReachable (bool reachable) override;
void onLinphoneCoreReady () override;
};
PlatformHelpers *createAndroidPlatformHelpers (LinphoneCore *lc, void *systemContext);
......
package org.linphone.core.receivers;
/*
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.
*/
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 {
@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 (AndroidPlatformHelper.isInstanciated()) {
AndroidPlatformHelper.getInstance().setDozeModeEnabled(dozeM);
AndroidPlatformHelper.getInstance().updateNetworkReachability();
}
}
}
}
package org.linphone.core.receivers;
/*
ContactPickerActivity.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.
*/
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 through LinphoneManager.
*/
public class NetworkManager extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Boolean lNoConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if (AndroidPlatformHelper.isInstanciated()) {
AndroidPlatformHelper.getInstance().updateNetworkReachability();
}
}
}
......@@ -21,6 +21,8 @@ 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.receivers.DozeReceiver;
import org.linphone.core.receivers.NetworkManager;
import org.linphone.mediastream.Log;
import org.linphone.mediastream.MediastreamerAndroidContext;
import org.linphone.mediastream.Version;
......@@ -30,11 +32,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.NetworkRequest;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.Build;
......@@ -54,11 +59,14 @@ import java.io.InputStream;
**/
public class AndroidPlatformHelper {
private static boolean mIsInstanciated = false;
private static AndroidPlatformHelper instance = null;
private long mNativePtr;
private Context mContext;
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,9 +79,15 @@ 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 native void setNativePreviewWindowId(long nativePtr, Object view);
private native void setNativeVideoWindowId(long nativePtr, Object view);
private native void setNetworkReachable(long nativePtr, boolean reachable);
public AndroidPlatformHelper(long nativePtr, Object ctx_obj) {
mNativePtr = nativePtr;
......@@ -108,6 +122,52 @@ public class AndroidPlatformHelper {
} catch (IOException e) {
Log.e("AndroidPlatformHelper(): failed to install some resources.");
}
mIsInstanciated = true;
instance = this;
}
public static boolean isInstanciated() {
return mIsInstanciated;
}
public static AndroidPlatformHelper getInstance() {
return instance;
}
public void onLinphoneCoreReady() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
mNetworkReceiver = new NetworkManager();
mNetworkIntentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
mContext.registerReceiver(mNetworkReceiver, mNetworkIntentFilter);
} else {
mConnectivityManager.registerNetworkCallback(
new NetworkRequest.Builder().build(),
new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
updateNetworkReachability();
}
@Override
public void onLost(Network network) {
updateNetworkReachability();
}
}
);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mDozeIntentFilter = new IntentFilter();
mDozeIntentFilter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
mDozeReceiver = new DozeReceiver();
dozeModeEnabled = ((PowerManager) mContext.getSystemService(Context.POWER_SERVICE)).isDeviceIdleMode();
mContext.registerReceiver(mDozeReceiver, mDozeIntentFilter);
}
updateNetworkReachability();
}
}
public Object getPowerManager() {
......@@ -347,6 +407,37 @@ public class AndroidPlatformHelper {
setNativeVideoWindowId(mNativePtr, mSurface);
}
}
public void updateNetworkReachability() {
if (mConnectivityManager == null) return;
boolean connected = false;
NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
connected = networkInfo != null && networkInfo.isConnected();
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) {
int curtype = networkInfo.getType();
if (curtype != mLastNetworkType) {
//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;
}
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment