Skip to content

Commit

Permalink
Merge pull request #41 from defold/fix/1.2.188
Browse files Browse the repository at this point in the history
Smaller mutex scope + use new SDK android methods (Defold 1.2.188)
  • Loading branch information
AGulev committed Jan 4, 2022
2 parents a8a539d + faed359 commit 1f74a32
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 52 deletions.
71 changes: 28 additions & 43 deletions extension-push/src/push_android.cpp
Expand Up @@ -3,22 +3,11 @@
#include <unistd.h>
#include <stdlib.h>
#include <dmsdk/sdk.h>
#include <dmsdk/dlib/android.h>
#include "push_utils.h"

#define LIB_NAME "push"

static JNIEnv* Attach()
{
JNIEnv* env;
dmGraphics::GetNativeAndroidJavaVM()->AttachCurrentThread(&env, NULL);
return env;
}

static void Detach()
{
dmGraphics::GetNativeAndroidJavaVM()->DetachCurrentThread();
}

struct ScheduledNotification
{
int32_t id;
Expand Down Expand Up @@ -66,9 +55,10 @@ static int Push_Register(lua_State* L)
// NOTE: We ignore argument one. Only for iOS
g_Push.m_Callback = dmScript::CreateCallback(L, 2);

JNIEnv* env = Attach();
dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv();

env->CallVoidMethod(g_Push.m_Push, g_Push.m_Register, dmGraphics::GetNativeAndroidActivity());
Detach();

return 0;
}
Expand All @@ -83,9 +73,10 @@ static int Push_SetListener(lua_State* L)
g_Push.m_Listener = dmScript::CreateCallback(L, 1);

// Flush stored notifications stored on Java side
JNIEnv* env = Attach();
dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv();

env->CallVoidMethod(g_Push.m_Push, g_Push.m_FlushStored);
Detach();

return 0;
}
Expand Down Expand Up @@ -185,15 +176,16 @@ static int Push_Schedule(lua_State* L)
}
g_Push.m_ScheduledNotifications.Push( sn );

JNIEnv* env = Attach();
dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv();

jstring jtitle = env->NewStringUTF(sn.title);
jstring jmessage = env->NewStringUTF(sn.message);
jstring jpayload = env->NewStringUTF(sn.payload);
env->CallVoidMethod(g_Push.m_Push, g_Push.m_Schedule, dmGraphics::GetNativeAndroidActivity(), sn.id, sn.timestamp / 1000, jtitle, jmessage, jpayload, sn.priority);
env->DeleteLocalRef(jpayload);
env->DeleteLocalRef(jmessage);
env->DeleteLocalRef(jtitle);
Detach();

assert(top == lua_gettop(L));

Expand Down Expand Up @@ -239,15 +231,15 @@ static int Push_Cancel(lua_State* L)

if (sn.id == cancel_id)
{
JNIEnv* env = Attach();
dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv();
jstring jtitle = env->NewStringUTF(sn.title);
jstring jmessage = env->NewStringUTF(sn.message);
jstring jpayload = env->NewStringUTF(sn.payload);
env->CallVoidMethod(g_Push.m_Push, g_Push.m_Cancel, dmGraphics::GetNativeAndroidActivity(), sn.id, jtitle, jmessage, jpayload, sn.priority);
env->DeleteLocalRef(jpayload);
env->DeleteLocalRef(jmessage);
env->DeleteLocalRef(jtitle);
Detach();

RemoveNotification(cancel_id);
break;
Expand Down Expand Up @@ -343,9 +335,11 @@ static int Push_CancelAllIssued(lua_State* L)
{
DM_LUA_STACK_CHECK(L, 0);

JNIEnv* env = Attach();
dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv();

env->CallVoidMethod(g_Push.m_Push, g_Push.m_CancelAllIssued, dmGraphics::GetNativeAndroidActivity());
Detach();

return 0;
}

Expand Down Expand Up @@ -498,21 +492,11 @@ static dmExtension::Result AppInitializePush(dmExtension::AppParams* params)
{
dmPush::QueueCreate(&g_Push.m_CommandQueue);

JNIEnv* env = Attach();

jclass activity_class = env->FindClass("android/app/NativeActivity");
jmethodID get_class_loader = env->GetMethodID(activity_class,"getClassLoader", "()Ljava/lang/ClassLoader;");
jobject cls = env->CallObjectMethod(dmGraphics::GetNativeAndroidActivity(), get_class_loader);
jclass class_loader = env->FindClass("java/lang/ClassLoader");
jmethodID find_class = env->GetMethodID(class_loader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");

jstring str_class_name = env->NewStringUTF("com.defold.push.Push");
jclass push_class = (jclass)env->CallObjectMethod(cls, find_class, str_class_name);
env->DeleteLocalRef(str_class_name);
dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv();

str_class_name = env->NewStringUTF("com.defold.push.PushJNI");
jclass push_jni_class = (jclass)env->CallObjectMethod(cls, find_class, str_class_name);
env->DeleteLocalRef(str_class_name);
jclass push_class = dmAndroid::LoadClass(env, "com.defold.push.Push");
jclass push_jni_class = dmAndroid::LoadClass(env, "com.defold.push.PushJNI");

g_Push.m_Start = env->GetMethodID(push_class, "start", "(Landroid/app/Activity;Lcom/defold/push/IPushListener;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
g_Push.m_Stop = env->GetMethodID(push_class, "stop", "()V");
Expand Down Expand Up @@ -546,8 +530,6 @@ static dmExtension::Result AppInitializePush(dmExtension::AppParams* params)
jmethodID loadPendingNotifications = env->GetMethodID(push_class, "loadPendingNotifications", "(Landroid/app/Activity;)V");
env->CallVoidMethod(g_Push.m_Push, loadPendingNotifications, dmGraphics::GetNativeAndroidActivity());

Detach();

return dmExtension::RESULT_OK;
}

Expand All @@ -559,11 +541,14 @@ static dmExtension::Result UpdatePush(dmExtension::Params* params)

static dmExtension::Result AppFinalizePush(dmExtension::AppParams* params)
{
JNIEnv* env = Attach();
env->CallVoidMethod(g_Push.m_Push, g_Push.m_Stop);
env->DeleteGlobalRef(g_Push.m_Push);
env->DeleteGlobalRef(g_Push.m_PushJNI);
Detach();
{
dmAndroid::ThreadAttacher threadAttacher;
JNIEnv* env = threadAttacher.GetEnv();

env->CallVoidMethod(g_Push.m_Push, g_Push.m_Stop);
env->DeleteGlobalRef(g_Push.m_Push);
env->DeleteGlobalRef(g_Push.m_PushJNI);
}
g_Push.m_Push = NULL;
g_Push.m_PushJNI = NULL;

Expand Down
10 changes: 6 additions & 4 deletions extension-push/src/push_ios.mm
Expand Up @@ -504,11 +504,13 @@ static int Push_CancelAllIssued(lua_State* L)
{
// Set the new callback to the saved notifications, and put them on the queue
if (!g_Push.m_SavedNotifications.m_Commands.Empty() & g_Push.m_Listener != 0) {
DM_MUTEX_SCOPED_LOCK(g_Push.m_SavedNotifications.m_Mutex);
for (int i = 0; i < g_Push.m_SavedNotifications.m_Commands.Size(); ++i)
{
dmPush::Command& cmd = g_Push.m_SavedNotifications.m_Commands[i];
cmd.m_Callback = g_Push.m_Listener;
DM_MUTEX_SCOPED_LOCK(g_Push.m_SavedNotifications.m_Mutex);
for (int i = 0; i < g_Push.m_SavedNotifications.m_Commands.Size(); ++i)
{
dmPush::Command& cmd = g_Push.m_SavedNotifications.m_Commands[i];
cmd.m_Callback = g_Push.m_Listener;
}
}

dmPush::QueueFlush(&g_Push.m_SavedNotifications, dmPush::HandleCommand, 0);
Expand Down
12 changes: 7 additions & 5 deletions extension-push/src/push_utils.cpp
Expand Up @@ -159,14 +159,16 @@ void dmPush::QueueFlush(CommandQueue* queue, CommandFn fn, void* ctx)
return;
}

DM_MUTEX_SCOPED_LOCK(queue->m_Mutex);
dmArray<Command> tmp;
{
DM_MUTEX_SCOPED_LOCK(queue->m_Mutex);
tmp.Swap(queue->m_Commands);
}

for(uint32_t i = 0; i != queue->m_Commands.Size(); ++i)
for(uint32_t i = 0; i != tmp.Size(); ++i)
{
fn(&queue->m_Commands[i], ctx);
fn(&tmp[i], ctx);
}
queue->m_Commands.SetSize(0);
}


#endif // DM_PLATFORM_ANDROID || DM_PLATFORM_IOS

0 comments on commit 1f74a32

Please sign in to comment.