From 87aecfa304004d75e5e91af8c528f2357dc4fd5d Mon Sep 17 00:00:00 2001 From: Erwan Croze Date: Fri, 12 Jan 2018 11:54:00 +0100 Subject: [PATCH] Moving some setting init from android application to liblinphone --- coreapi/linphonecore.c | 3 + .../core/tools/AndroidPlatformHelper.java | 74 ++++++++++++++-- .../android-platform-helpers.cpp | 3 + .../classes/tools/AndroidPlatformHelper.java | 85 +++++++++++++++++-- wrappers/java/java_class.mustache | 14 ++- wrappers/java/jni.mustache | 4 + 6 files changed, 169 insertions(+), 14 deletions(-) diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index 2708a3c62..f87fe9cbf 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -38,6 +38,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include #include #include +#include #include "mediastreamer2/dtmfgen.h" #include "mediastreamer2/mediastream.h" @@ -2249,6 +2250,8 @@ static void linphone_core_init(LinphoneCore * lc, LinphoneCoreCbs *cbs, LpConfig if (lc->platform_helper == NULL) lc->platform_helper = new LinphonePrivate::StubbedPlatformHelpers(lc); + belr::GrammarLoader::get().addPath(getPlatformHelpers(lc)->getDataPath()); + linphone_task_list_init(&lc->hooks); _linphone_core_init_account_creator_service(lc); diff --git a/java/impl/org/linphone/core/tools/AndroidPlatformHelper.java b/java/impl/org/linphone/core/tools/AndroidPlatformHelper.java index a202dbc77..b601a2972 100644 --- a/java/impl/org/linphone/core/tools/AndroidPlatformHelper.java +++ b/java/impl/org/linphone/core/tools/AndroidPlatformHelper.java @@ -20,6 +20,7 @@ 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.mediastream.Log; import org.linphone.mediastream.MediastreamerAndroidContext; import org.linphone.mediastream.Version; @@ -33,10 +34,14 @@ import android.net.Network; import android.net.NetworkInfo; import android.os.PowerManager; import android.os.PowerManager.WakeLock; +import android.os.Build; import java.net.InetAddress; import java.util.List; -import android.os.Build; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; /** * This class is instanciated directly by the linphone library in order to access specific features only accessible in java. @@ -49,27 +54,56 @@ public class AndroidPlatformHelper { private ConnectivityManager mConnectivityManager; private PowerManager mPowerManager; private WakeLock mWakeLock; + private String mLinphoneRootCaFile; + private String mRingSoundFile; + private String mRingbackSoundFile; + private String mPauseSoundFile; + private String mErrorToneFile; + private String mGrammarCpimFile; + private String mGrammarVcardFile ; + private String mUserCertificatePath; public AndroidPlatformHelper(Object ctx_obj) { mContext = (Context) ctx_obj; MediastreamerAndroidContext.setContext(mContext); - + WifiManager wifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); mConnectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); - + mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AndroidPlatformHelper"); mWakeLock.setReferenceCounted(true); mMcastLock = wifiMgr.createMulticastLock("AndroidPlatformHelper"); mMcastLock.setReferenceCounted(true); mWifiLock = wifiMgr.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "AndroidPlatformHelper"); mWifiLock.setReferenceCounted(true); + + String basePath = mContext.getFilesDir().getAbsolutePath(); + mLinphoneRootCaFile = basePath + "/rootca.pem"; + mRingSoundFile = basePath + "/ringtone.mkv"; + mRingbackSoundFile = basePath + "/ringback.wav"; + mPauseSoundFile = basePath + "/hold.mkv"; + mErrorToneFile = basePath + "/error.wav"; + mGrammarCpimFile = basePath + "/cpim_grammar"; + mGrammarVcardFile = basePath + "/vcard_gramamr"; + mUserCertificatePath = basePath; + + copyAssetsFromPackage(); } - + + public void initCore(long ptrLc) { + Core lc = Factory.instance().getCore(ptrLc); + if (lc == null) return; + lc.setRingback(mRingbackSoundFile); + lc.setRootCa(mLinphoneRootCaFile); + lc.setPlayFile(mPauseSoundFile); + lc.setUserCertificatesPath(mUserCertificatePath); + } + public Object getPowerManager() { return mPowerManager; } - + public String[] getDnsServers() { if (mConnectivityManager == null || Build.VERSION.SDK_INT < Version.API23_MARSHMALLOW_60) return null; @@ -132,6 +166,36 @@ public class AndroidPlatformHelper { Log.i("releaseCpuLock()"); mWakeLock.release(); } + + private void copyAssetsFromPackage() throws IOException { + copyIfNotExist(mContext.getResources().getIdentifier("org.linphone:raw/notes_of_the_optimistic", null, null), mRingSoundFile); + copyIfNotExist(mContext.getResources().getIdentifier("org.linphone:raw/ringback", null, null), mRingbackSoundFile); + copyIfNotExist(mContext.getResources().getIdentifier("org.linphone:raw/hold", null, null), mPauseSoundFile); + copyIfNotExist(mContext.getResources().getIdentifier("org.linphone:raw/incoming_chat", null, null), mErrorToneFile); + copyIfNotExist(mContext.getResources().getIdentifier("org.linphone:raw/cpim_grammar", null, null), mGrammarCpimFile); + copyIfNotExist(mContext.getResources().getIdentifier("org.linphone:raw/vcard_grammar", null, null), mGrammarVcardFile); + copyFromPackage(R.raw.rootca, new File(mLinphoneRootCaFile).getName()); + } + + public void copyIfNotExist(int ressourceId, String target) throws IOException { + File lFileToCopy = new File(target); + if (!lFileToCopy.exists()) { + copyFromPackage(ressourceId,lFileToCopy.getName()); + } + } + + public void copyFromPackage(int ressourceId, String target) throws IOException{ + FileOutputStream lOutputStream = mServiceContext.openFileOutput (target, 0); + InputStream lInputStream = mR.openRawResource(ressourceId); + int readByte; + byte[] buff = new byte[8048]; + while (( readByte = lInputStream.read(buff)) != -1) { + lOutputStream.write(buff,0, readByte); + } + lOutputStream.flush(); + lOutputStream.close(); + lInputStream.close(); + } }; diff --git a/src/core/platform-helpers/android-platform-helpers.cpp b/src/core/platform-helpers/android-platform-helpers.cpp index 9123fb1ef..4d3f056fe 100644 --- a/src/core/platform-helpers/android-platform-helpers.cpp +++ b/src/core/platform-helpers/android-platform-helpers.cpp @@ -103,6 +103,9 @@ AndroidPlatformHelpers::AndroidPlatformHelpers (LinphoneCore *lc, void *systemCo mGetDataPathId = getMethodId(env, klass, "getDataPath", "()Ljava/lang/String;"); mGetConfigPathId = getMethodId(env, klass, "getConfigPath", "()Ljava/lang/String;"); + jmethodID initCoreId = getMethodId(env, klass, "initCore", "(J)V"); + env->CallVoidMethod(mJavaHelper, initCoreId, (jlong)lc); + jobject pm = env->CallObjectMethod(mJavaHelper, mGetPowerManagerId); belle_sip_wake_lock_init(env, pm); diff --git a/wrappers/java/classes/tools/AndroidPlatformHelper.java b/wrappers/java/classes/tools/AndroidPlatformHelper.java index a202dbc77..1a3388f66 100644 --- a/wrappers/java/classes/tools/AndroidPlatformHelper.java +++ b/wrappers/java/classes/tools/AndroidPlatformHelper.java @@ -20,10 +20,13 @@ 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.Factory; import org.linphone.mediastream.Log; import org.linphone.mediastream.MediastreamerAndroidContext; import org.linphone.mediastream.Version; +import android.content.res.Resources; import android.net.wifi.WifiManager; import android.net.wifi.WifiManager.MulticastLock; import android.net.wifi.WifiManager.WifiLock; @@ -33,14 +36,19 @@ import android.net.Network; import android.net.NetworkInfo; import android.os.PowerManager; import android.os.PowerManager.WakeLock; +import android.os.Build; import java.net.InetAddress; import java.util.List; -import android.os.Build; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; /** * This class is instanciated directly by the linphone library in order to access specific features only accessible in java. -**/ + **/ public class AndroidPlatformHelper { private Context mContext; @@ -49,27 +57,62 @@ public class AndroidPlatformHelper { private ConnectivityManager mConnectivityManager; private PowerManager mPowerManager; private WakeLock mWakeLock; + private Resources mResources; + private String mLinphoneRootCaFile; + private String mRingSoundFile; + private String mRingbackSoundFile; + private String mPauseSoundFile; + private String mErrorToneFile; + private String mGrammarCpimFile; + private String mGrammarVcardFile ; + private String mUserCertificatePath; public AndroidPlatformHelper(Object ctx_obj) { mContext = (Context) ctx_obj; + mResources = mContext.getResources(); MediastreamerAndroidContext.setContext(mContext); - + WifiManager wifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); mConnectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); - + mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AndroidPlatformHelper"); mWakeLock.setReferenceCounted(true); mMcastLock = wifiMgr.createMulticastLock("AndroidPlatformHelper"); mMcastLock.setReferenceCounted(true); mWifiLock = wifiMgr.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "AndroidPlatformHelper"); mWifiLock.setReferenceCounted(true); + + String basePath = mContext.getFilesDir().getAbsolutePath(); + mLinphoneRootCaFile = basePath + "/rootca.pem"; + mRingSoundFile = basePath + "/ringtone.mkv"; + mRingbackSoundFile = basePath + "/ringback.wav"; + mPauseSoundFile = basePath + "/hold.mkv"; + mErrorToneFile = basePath + "/error.wav"; + mGrammarCpimFile = basePath + "/cpim_grammar"; + mGrammarVcardFile = basePath + "/vcard_grammar"; + mUserCertificatePath = basePath; + + try { + copyAssetsFromPackage(); + } catch (Exception e) { + Log.e(e, "AndroidPlatformHelper: Cannot copy assets from package."); + } } - + + public void initCore(long ptrLc) { + Core lc = Factory.instance().getCore(ptrLc); + if (lc == null) return; + lc.setRingback(mRingbackSoundFile); + lc.setRootCa(mLinphoneRootCaFile); + lc.setPlayFile(mPauseSoundFile); + lc.setUserCertificatesPath(mUserCertificatePath); + } + public Object getPowerManager() { return mPowerManager; } - + public String[] getDnsServers() { if (mConnectivityManager == null || Build.VERSION.SDK_INT < Version.API23_MARSHMALLOW_60) return null; @@ -132,6 +175,36 @@ public class AndroidPlatformHelper { Log.i("releaseCpuLock()"); mWakeLock.release(); } + + private void copyAssetsFromPackage() throws IOException { + copyIfNotExist(mResources.getIdentifier("org.linphone:raw/notes_of_the_optimistic", null, null), mRingSoundFile); + copyIfNotExist(mResources.getIdentifier("org.linphone:raw/ringback", null, null), mRingbackSoundFile); + copyIfNotExist(mResources.getIdentifier("org.linphone:raw/hold", null, null), mPauseSoundFile); + copyIfNotExist(mResources.getIdentifier("org.linphone:raw/incoming_chat", null, null), mErrorToneFile); + copyIfNotExist(mResources.getIdentifier("org.linphone:raw/cpim_grammar", null, null), mGrammarCpimFile); + copyIfNotExist(mResources.getIdentifier("org.linphone:raw/vcard_grammar", null, null), mGrammarVcardFile); + copyFromPackage(mResources.getIdentifier("org.linphone:raw/rootca", null, null), new File(mLinphoneRootCaFile).getName()); + } + + public void copyIfNotExist(int ressourceId, String target) throws IOException { + File lFileToCopy = new File(target); + if (!lFileToCopy.exists()) { + copyFromPackage(ressourceId,lFileToCopy.getName()); + } + } + + public void copyFromPackage(int ressourceId, String target) throws IOException{ + FileOutputStream lOutputStream = mContext.openFileOutput (target, 0); + InputStream lInputStream = mResources.openRawResource(ressourceId); + int readByte; + byte[] buff = new byte[8048]; + while (( readByte = lInputStream.read(buff)) != -1) { + lOutputStream.write(buff,0, readByte); + } + lOutputStream.flush(); + lOutputStream.close(); + lInputStream.close(); + } }; diff --git a/wrappers/java/java_class.mustache b/wrappers/java/java_class.mustache index 3037a9e34..f409b8d84 100644 --- a/wrappers/java/java_class.mustache +++ b/wrappers/java/java_class.mustache @@ -96,7 +96,9 @@ public {{#isLinphoneFactory}}abstract class{{/isLinphoneFactory}}{{#isNotLinphon abstract public OpenH264DownloadHelper createOpenH264DownloadHelper(Context context); abstract public void setDebugMode(boolean enable, String tag); - + + abstract public Core getCore(long ptr); + {{/isLinphoneFactory}} {{#isLinphoneCore}} /** @@ -168,6 +170,12 @@ class {{classImplName}} {{#isLinphoneFactory}}extends{{/isLinphoneFactory}}{{#is return new OpenH264DownloadHelper(context); } + private native Core getCore(long nativePtr, long ptr); + @Override + public Core getCore(long ptr) { + return getCore(nativePtr, ptr); + } + @Override public native void setDebugMode(boolean enable, String tag); {{/isLinphoneFactory}} @@ -178,7 +186,7 @@ class {{classImplName}} {{#isLinphoneFactory}}extends{{/isLinphoneFactory}}{{#is synchronized public {{return}} {{name}}({{params}}) {{#exception}}throws CoreException{{/exception}} { {{#hasCoreAccessor}}{{#isNotGetCore}}synchronized(core) { {{/isNotGetCore}}{{/hasCoreAccessor}} {{#exception}}int exceptionResult = {{/exception}}{{return_keyword}}{{#enumCast}}{{return}}.fromInt({{/enumCast}}{{#classCast}}({{return}}){{/classCast}}{{name}}(nativePtr{{native_params_impl}}){{#enumCast}}){{/enumCast}};{{#exception}} - if (exceptionResult != 0) throw new CoreException("{{name}} returned value " + exceptionResult);{{/exception}}{{#hasCoreAccessor}}{{#isNotGetCore}} + if (exceptionResult != 0) throw new CoreException("{{name}} returned value " + exceptionResult);{{/exception}}{{#hasCoreAccessor}}{{#isNotGetCore}} }{{/isNotGetCore}}{{/hasCoreAccessor}} } @@ -200,7 +208,7 @@ class {{classImplName}} {{#isLinphoneFactory}}extends{{/isLinphoneFactory}}{{#is super.finalize(); } {{/isNotLinphoneFactory}} - + @Override public void setUserData(Object data) { userData = data; diff --git a/wrappers/java/jni.mustache b/wrappers/java/jni.mustache index a8b430329..1228d1737 100644 --- a/wrappers/java/jni.mustache +++ b/wrappers/java/jni.mustache @@ -535,6 +535,10 @@ void Java_org_linphone_core_CoreImpl_setNativeVideoWindowId(JNIEnv *env, jobject } } +jobject Java_org_linphone_core_FactoryImpl_getCore(JNIEnv *env, jobject thiz, jlong ptr, jlong lcPtr) { + return getCore(env, (LinphoneCore*)lcPtr); +} + #ifdef __cplusplus } #endif