fix(PlatformHelpers): clean code, fix coding style, use cpp logger...

This commit is contained in:
Ronan Abhamon 2017-10-19 16:58:51 +02:00
parent 2941e64e3e
commit 4e3c5d9548
6 changed files with 152 additions and 148 deletions

View file

@ -222,7 +222,7 @@ if (APPLE)
list(APPEND LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES core/paths/paths-apple.h) list(APPEND LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES core/paths/paths-apple.h)
elseif (ANDROID) elseif (ANDROID)
list(APPEND LINPHONE_CXX_OBJECTS_SOURCE_FILES core/paths/paths-android.cpp core/platform-helpers/android-helpers.cpp) list(APPEND LINPHONE_CXX_OBJECTS_SOURCE_FILES core/paths/paths-android.cpp core/platform-helpers/android-platform-helpers.cpp)
list(APPEND LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES core/paths/paths-android.h) list(APPEND LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES core/paths/paths-android.h)
elseif (WIN32) elseif (WIN32)

View file

@ -19,14 +19,13 @@
#include <algorithm> #include <algorithm>
#include "address/address.h"
#include "chat/chat-room/basic-chat-room.h" #include "chat/chat-room/basic-chat-room.h"
#include "core-p.h" #include "core-p.h"
#include "db/main-db.h" #include "db/main-db.h"
#include "linphone/core.h"
#include "object/object-p.h" #include "object/object-p.h"
#include "paths/paths.h" #include "paths/paths.h"
// TODO: Remove me later.
#include "private.h" #include "private.h"
// ============================================================================= // =============================================================================

View file

@ -22,10 +22,12 @@
#include <list> #include <list>
#include "chat/chat-room/chat-room.h" #include "object/object.h"
// ============================================================================= // =============================================================================
L_DECL_C_STRUCT(LinphoneCore);
LINPHONE_BEGIN_NAMESPACE LINPHONE_BEGIN_NAMESPACE
class ChatRoom; class ChatRoom;

View file

@ -1,35 +1,41 @@
/* /*
linphone * android-platform-helpers.h
Copyright (C) 2017 Belledonne Communications SARL * Copyright (C) 2010-2017 Belledonne Communications SARL
*
This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2 * as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. * of the License, or (at your option) any later version.
*
This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. * GNU General Public License for more details.
*
You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
#include "linphone/utils/utils.h" #include <jni.h>
#include "platform-helpers.h"
#include "logger/logger.h"
// TODO: Remove me later.
#include "private.h" #include "private.h"
#ifdef __ANDROID__ // =============================================================================
#include <jni.h> using namespace std;
LINPHONE_BEGIN_NAMESPACE LINPHONE_BEGIN_NAMESPACE
class AndroidPlatformHelpers : public PlatformHelpers { class AndroidPlatformHelpers : public PlatformHelpers {
public: public:
AndroidPlatformHelpers (LinphoneCore *lc, void *system_context); AndroidPlatformHelpers (LinphoneCore *lc, void *systemContext);
~AndroidPlatformHelpers ();
void setDnsServers () override; void setDnsServers () override;
void acquireWifiLock () override; void acquireWifiLock () override;
void releaseWifiLock () override; void releaseWifiLock () override;
@ -37,9 +43,9 @@ public:
void releaseMcastLock () override; void releaseMcastLock () override;
void acquireCpuLock () override; void acquireCpuLock () override;
void releaseCpuLock () override; void releaseCpuLock () override;
std::string getDataPath () override; string getDataPath () override;
std::string getConfigPath () override; string getConfigPath () override;
~AndroidPlatformHelpers ();
private: private:
int callVoidMethod (jmethodID id); int callVoidMethod (jmethodID id);
static jmethodID getMethodId (JNIEnv *env, jclass klass, const char *method, const char *signature); static jmethodID getMethodId (JNIEnv *env, jclass klass, const char *method, const char *signature);
@ -54,11 +60,10 @@ private:
jmethodID mGetPowerManagerId; jmethodID mGetPowerManagerId;
jmethodID mGetDataPathId; jmethodID mGetDataPathId;
jmethodID mGetConfigPathId; jmethodID mGetConfigPathId;
}; };
static const char *GetStringUTFChars (JNIEnv *env, jstring string) { static const char *GetStringUTFChars (JNIEnv *env, jstring string) {
const char *cstring = string ? env->GetStringUTFChars(string, NULL) : NULL; const char *cstring = string ? env->GetStringUTFChars(string, nullptr) : nullptr;
return cstring; return cstring;
} }
@ -68,23 +73,21 @@ static void ReleaseStringUTFChars (JNIEnv* env, jstring string, const char *cstr
jmethodID AndroidPlatformHelpers::getMethodId (JNIEnv *env, jclass klass, const char *method, const char *signature) { jmethodID AndroidPlatformHelpers::getMethodId (JNIEnv *env, jclass klass, const char *method, const char *signature) {
jmethodID id = env->GetMethodID(klass, method, signature); jmethodID id = env->GetMethodID(klass, method, signature);
if (id == 0) { if (id == 0)
ms_fatal("Could not find java method '%s %s'", method, signature); lFatal() << "Could not find java method: `" << method << ", " << signature << "`.";
}
return id; return id;
} }
AndroidPlatformHelpers::AndroidPlatformHelpers (LinphoneCore *lc, void *system_context) : PlatformHelpers(lc) { AndroidPlatformHelpers::AndroidPlatformHelpers (LinphoneCore *lc, void *systemContext) : PlatformHelpers(lc) {
JNIEnv *env = ms_get_jni_env(); JNIEnv *env = ms_get_jni_env();
jclass klass = env->FindClass("org/linphone/core/tools/AndroidPlatformHelper"); jclass klass = env->FindClass("org/linphone/core/tools/AndroidPlatformHelper");
if (!klass) { if (!klass)
ms_fatal("Could not find java AndroidPlatformHelper class"); lFatal() << "Could not find java AndroidPlatformHelper class.";
return;
}
jmethodID ctor = env->GetMethodID(klass, "<init>", "(Ljava/lang/Object;)V"); jmethodID ctor = env->GetMethodID(klass, "<init>", "(Ljava/lang/Object;)V");
mJavaHelper = env->NewObject(klass, ctor, (jobject)system_context); mJavaHelper = env->NewObject(klass, ctor, (jobject)systemContext);
if (!mJavaHelper) { if (!mJavaHelper) {
ms_error("Could not instanciate AndroidPlatformHelper object."); lError() << "Could not instanciate AndroidPlatformHelper object.";
return; return;
} }
mJavaHelper = (jobject)env->NewGlobalRef(mJavaHelper); mJavaHelper = (jobject)env->NewGlobalRef(mJavaHelper);
@ -103,7 +106,7 @@ AndroidPlatformHelpers::AndroidPlatformHelpers (LinphoneCore *lc, void *system_c
jobject pm = env->CallObjectMethod(mJavaHelper, mGetPowerManagerId); jobject pm = env->CallObjectMethod(mJavaHelper, mGetPowerManagerId);
belle_sip_wake_lock_init(env, pm); belle_sip_wake_lock_init(env, pm);
ms_message("AndroidPlatformHelpers is fully initialised"); lInfo() << "AndroidPlatformHelpers is fully initialised.";
} }
AndroidPlatformHelpers::~AndroidPlatformHelpers () { AndroidPlatformHelpers::~AndroidPlatformHelpers () {
@ -111,28 +114,27 @@ AndroidPlatformHelpers::~AndroidPlatformHelpers () {
JNIEnv *env = ms_get_jni_env(); JNIEnv *env = ms_get_jni_env();
belle_sip_wake_lock_uninit(env); belle_sip_wake_lock_uninit(env);
env->DeleteGlobalRef(mJavaHelper); env->DeleteGlobalRef(mJavaHelper);
mJavaHelper = NULL; mJavaHelper = nullptr;
} }
} }
void AndroidPlatformHelpers::setDnsServers () { void AndroidPlatformHelpers::setDnsServers () {
if (!mJavaHelper) return; if (!mJavaHelper) return;
JNIEnv *env = ms_get_jni_env(); JNIEnv *env = ms_get_jni_env();
if (env && mJavaHelper) { if (env && mJavaHelper) {
jobjectArray jservers = (jobjectArray)env->CallObjectMethod(mJavaHelper, mGetDnsServersId); jobjectArray jservers = (jobjectArray)env->CallObjectMethod(mJavaHelper, mGetDnsServersId);
bctbx_list_t *l = NULL; bctbx_list_t *l = nullptr;
if (env->ExceptionCheck()) { if (env->ExceptionCheck()) {
env->ExceptionClear(); env->ExceptionClear();
ms_error("AndroidPlatformHelpers::setDnsServers() exception"); lError() << "AndroidPlatformHelpers::setDnsServers() exception.";
return; return;
} }
if (jservers != NULL) { if (jservers != nullptr) {
int count = env->GetArrayLength(jservers); int count = env->GetArrayLength(jservers);
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
jstring jserver = (jstring)env->GetObjectArrayElement(jservers, i); jstring jserver = (jstring)env->GetObjectArrayElement(jservers, i);
const char *str = env->GetStringUTFChars(jserver, NULL); const char *str = env->GetStringUTFChars(jserver, nullptr);
if (str) { if (str) {
l = bctbx_list_append(l, ms_strdup(str)); l = bctbx_list_append(l, ms_strdup(str));
env->ReleaseStringUTFChars(jserver, str); env->ReleaseStringUTFChars(jserver, str);
@ -168,20 +170,20 @@ void AndroidPlatformHelpers::releaseCpuLock () {
callVoidMethod(mCpuLockReleaseId); callVoidMethod(mCpuLockReleaseId);
} }
std::string AndroidPlatformHelpers::getDataPath () { string AndroidPlatformHelpers::getDataPath () {
JNIEnv *env = ms_get_jni_env(); JNIEnv *env = ms_get_jni_env();
jstring jdata_path = (jstring)env->CallObjectMethod(mJavaHelper, mGetDataPathId); jstring jdata_path = (jstring)env->CallObjectMethod(mJavaHelper, mGetDataPathId);
const char *data_path = GetStringUTFChars(env, jdata_path); const char *data_path = GetStringUTFChars(env, jdata_path);
std::string dataPath = data_path; string dataPath = data_path;
ReleaseStringUTFChars(env, jdata_path, data_path); ReleaseStringUTFChars(env, jdata_path, data_path);
return dataPath; return dataPath;
} }
std::string AndroidPlatformHelpers::getConfigPath () { string AndroidPlatformHelpers::getConfigPath () {
JNIEnv *env = ms_get_jni_env(); JNIEnv *env = ms_get_jni_env();
jstring jconfig_path = (jstring)env->CallObjectMethod(mJavaHelper, mGetConfigPathId); jstring jconfig_path = (jstring)env->CallObjectMethod(mJavaHelper, mGetConfigPathId);
const char *config_path = GetStringUTFChars(env, jconfig_path); const char *config_path = GetStringUTFChars(env, jconfig_path);
std::string configPath = config_path; string configPath = config_path;
ReleaseStringUTFChars(env, jconfig_path, config_path); ReleaseStringUTFChars(env, jconfig_path, config_path);
return configPath; return configPath;
} }
@ -199,10 +201,8 @@ int AndroidPlatformHelpers::callVoidMethod (jmethodID id) {
return -1; return -1;
} }
PlatformHelpers *createAndroidPlatformHelpers (LinphoneCore *lc, void *system_context) { PlatformHelpers *createAndroidPlatformHelpers (LinphoneCore *lc, void *systemContext) {
return new AndroidPlatformHelpers(lc, system_context); return new AndroidPlatformHelpers(lc, systemContext);
} }
LINPHONE_END_NAMESPACE LINPHONE_END_NAMESPACE
#endif

View file

@ -1,53 +1,50 @@
/* /*
linphone * platform-helpers.cpp
Copyright (C) 2017 Belledonne Communications SARL * Copyright (C) 2010-2017 Belledonne Communications SARL
*
This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2 * as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. * of the License, or (at your option) any later version.
*
This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. * GNU General Public License for more details.
*
You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
#include "linphone/utils/utils.h" #include "platform-helpers.h"
#include "private.h" // =============================================================================
LINPHONE_BEGIN_NAMESPACE LINPHONE_BEGIN_NAMESPACE
PlatformHelpers::~PlatformHelpers () {}
StubbedPlatformHelpers::StubbedPlatformHelpers (LinphoneCore *lc) : PlatformHelpers(lc) {} StubbedPlatformHelpers::StubbedPlatformHelpers (LinphoneCore *lc) : PlatformHelpers(lc) {}
void StubbedPlatformHelpers::setDnsServers () { void StubbedPlatformHelpers::setDnsServers () {}
}
void StubbedPlatformHelpers::acquireWifiLock () { void StubbedPlatformHelpers::acquireWifiLock () {}
}
void StubbedPlatformHelpers::releaseWifiLock () { void StubbedPlatformHelpers::releaseWifiLock () {}
}
void StubbedPlatformHelpers::acquireMcastLock () { void StubbedPlatformHelpers::acquireMcastLock () {}
}
void StubbedPlatformHelpers::releaseMcastLock () { void StubbedPlatformHelpers::releaseMcastLock () {}
}
void StubbedPlatformHelpers::acquireCpuLock () { void StubbedPlatformHelpers::acquireCpuLock () {}
}
void StubbedPlatformHelpers::releaseCpuLock () { void StubbedPlatformHelpers::releaseCpuLock () {}
}
std::string StubbedPlatformHelpers::getDataPath () { std::string StubbedPlatformHelpers::getDataPath () {
return Utils::getEmptyConstRefObject<std::string>(); return "";
} }
std::string StubbedPlatformHelpers::getConfigPath () { std::string StubbedPlatformHelpers::getConfigPath () {
return Utils::getEmptyConstRefObject<std::string>(); return "";
}
StubbedPlatformHelpers::~StubbedPlatformHelpers () {
} }
LINPHONE_END_NAMESPACE LINPHONE_END_NAMESPACE

View file

@ -1,20 +1,20 @@
/* /*
linphone * platform-helpers.h
Copyright (C) 2017 Belledonne Communications SARL * Copyright (C) 2010-2017 Belledonne Communications SARL
*
This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2 * as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. * of the License, or (at your option) any later version.
*
This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. * GNU General Public License for more details.
*
You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
#ifndef _PLATFORM_HELPERS_H_ #ifndef _PLATFORM_HELPERS_H_
@ -22,17 +22,22 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <string> #include <string>
#include "linphone/core.h"
#include "linphone/utils/general.h" #include "linphone/utils/general.h"
// =============================================================================
L_DECL_C_STRUCT(LinphoneCore);
LINPHONE_BEGIN_NAMESPACE LINPHONE_BEGIN_NAMESPACE
/** /**
* This interface aims at abstracting some features offered by the platform, most often mobile platforms. * This interface aims at abstracting some features offered by the platform, most often mobile platforms.
* A per platform implementation is to be made to implement these features, if available on the platform * A per platform implementation is to be made to implement these features, if available on the platform.
*/ */
class PlatformHelpers { class PlatformHelpers {
public: public:
virtual ~PlatformHelpers () = default;
// This method shall retrieve DNS server list from the platform and assign it to the core. // This method shall retrieve DNS server list from the platform and assign it to the core.
virtual void setDnsServers () = 0; virtual void setDnsServers () = 0;
virtual void acquireWifiLock () = 0; virtual void acquireWifiLock () = 0;
@ -43,16 +48,18 @@ public:
virtual void releaseCpuLock () = 0; virtual void releaseCpuLock () = 0;
virtual std::string getDataPath () = 0; virtual std::string getDataPath () = 0;
virtual std::string getConfigPath () = 0; virtual std::string getConfigPath () = 0;
virtual ~PlatformHelpers ();
protected: protected:
inline PlatformHelpers (LinphoneCore *lc) : mCore(lc) {} inline PlatformHelpers (LinphoneCore *lc) : mCore(lc) {}
LinphoneCore *mCore; LinphoneCore *mCore;
}; };
class StubbedPlatformHelpers : public PlatformHelpers { class StubbedPlatformHelpers : public PlatformHelpers {
public: public:
StubbedPlatformHelpers (LinphoneCore *lc); StubbedPlatformHelpers (LinphoneCore *lc);
virtual ~StubbedPlatformHelpers () = default;
void setDnsServers () override; void setDnsServers () override;
void acquireWifiLock () override; void acquireWifiLock () override;
void releaseWifiLock () override; void releaseWifiLock () override;
@ -62,10 +69,9 @@ public:
void releaseCpuLock () override; void releaseCpuLock () override;
std::string getDataPath () override; std::string getDataPath () override;
std::string getConfigPath () override; std::string getConfigPath () override;
virtual ~StubbedPlatformHelpers ();
}; };
PlatformHelpers *createAndroidPlatformHelpers (LinphoneCore *lc, void *system_context); PlatformHelpers *createAndroidPlatformHelpers (LinphoneCore *lc, void *systemContext);
LINPHONE_END_NAMESPACE LINPHONE_END_NAMESPACE