Skip to content

Commit

Permalink
FIX: [droid] proper BroadcastReceiver
Browse files Browse the repository at this point in the history
  • Loading branch information
koying committed Jan 26, 2018
1 parent 49dc1b4 commit 98c2b84
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 13 deletions.
2 changes: 2 additions & 0 deletions xbmc/platform/android/activity/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(SOURCES android_main.cpp
JNIXBMCJsonHandler.cpp
JNIXBMCFile.cpp
JNIXBMCInputDeviceListener.cpp
JNIXBMCBroadcastReceiver.cpp
XBMCApp.cpp
${NDKROOT}/sources/android/native_app_glue/android_native_app_glue.c
${NDKROOT}/sources/android/cpufeatures/cpu-features.c)
Expand All @@ -44,6 +45,7 @@ set(HEADERS AndroidExtra.h
JNIXBMCJsonHandler.h
JNIXBMCFile.h
JNIXBMCInputDeviceListener.h
JNIXBMCBroadcastReceiver.h
XBMCApp.h
)

Expand Down
84 changes: 84 additions & 0 deletions xbmc/platform/android/activity/JNIXBMCBroadcastReceiver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (C) 2018 Christian Browet
* http://kodi.tv
*
* 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, 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 XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/

#include <algorithm>

#include "JNIXBMCBroadcastReceiver.h"

#include <androidjni/jutils-details.hpp>
#include <androidjni/Intent.h>

#include "XBMCApp.h"
#include "CompileInfo.h"

using namespace jni;

static std::string s_className = std::string(CCompileInfo::GetClass()) + "/XBMCBroadcastReceiver";

CJNIXBMCBroadcastReceiver::CJNIXBMCBroadcastReceiver(CJNIBroadcastReceiver* receiver)
: CJNIBase(s_className)
, m_receiver(receiver)
{
m_object = new_object(CXBMCApp::get()->getClassLoader().loadClass(GetDotClassName(GetClassName())));
m_object.setGlobal();

add_instance(m_object, this);
}

CJNIXBMCBroadcastReceiver::CJNIXBMCBroadcastReceiver(const CJNIXBMCBroadcastReceiver& other)
: CJNIBase(other)
{
add_instance(m_object, this);
}

CJNIXBMCBroadcastReceiver::~CJNIXBMCBroadcastReceiver()
{
remove_instance(this);
}

void CJNIXBMCBroadcastReceiver::RegisterNatives(JNIEnv* env)
{
jclass cClass = env->FindClass(s_className.c_str());
if(cClass)
{
JNINativeMethod methods[] =
{
{"_onReceive", "(Landroid/content/Intent;)V", (void*)&CJNIXBMCBroadcastReceiver::_onReceive},
};

env->RegisterNatives(cClass, methods, sizeof(methods)/sizeof(methods[0]));
}
}

void CJNIXBMCBroadcastReceiver::_onReceive(JNIEnv *env, jobject thiz, jobject intent)
{
(void)env;

CJNIXBMCBroadcastReceiver *inst = find_instance(thiz);
if (inst)
inst->onReceive(CJNIIntent(jhobject::fromJNI(intent)));
}

void CJNIXBMCBroadcastReceiver::onReceive(CJNIIntent intent)
{
if (m_receiver)
m_receiver->onReceive(intent);
}

47 changes: 47 additions & 0 deletions xbmc/platform/android/activity/JNIXBMCBroadcastReceiver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once
/*
* Copyright (C) 2018 Christian Browet
* http://kodi.tv
*
* 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, 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 XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/

#include <androidjni/JNIBase.h>

#include <androidjni/BroadcastReceiver.h>

namespace jni
{

class CJNIXBMCBroadcastReceiver : public CJNIBroadcastReceiver, public CJNIInterfaceImplem<CJNIXBMCBroadcastReceiver>
{
public:
CJNIXBMCBroadcastReceiver(CJNIBroadcastReceiver* receiver);
CJNIXBMCBroadcastReceiver(const CJNIXBMCBroadcastReceiver& other);
CJNIXBMCBroadcastReceiver(const jni::jhobject &object) : CJNIBase(object) {}
virtual ~CJNIXBMCBroadcastReceiver();

static void RegisterNatives(JNIEnv* env);

public:
void onReceive(CJNIIntent intent);

protected:
CJNIBroadcastReceiver* m_receiver;
static void _onReceive(JNIEnv* env, jobject thiz, jobject intent);
};

}
7 changes: 4 additions & 3 deletions xbmc/platform/android/activity/XBMCApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ CRect CXBMCApp::m_surface_rect;
CXBMCApp::CXBMCApp(ANativeActivity* nativeActivity)
: CJNIBase()
, CJNIMainActivity(nativeActivity->clazz)
, CJNIBroadcastReceiver(std::string(CCompileInfo::GetPackage()) + "/XBMCBroadcastReceiver")
, CJNIXBMCInputDeviceListener()
, CJNIBroadcastReceiver()
, m_videosurfaceInUse(false)
, m_inputDeviceCallbacks(nullptr)
, m_inputDeviceEventHandler(nullptr)
Expand All @@ -174,6 +174,7 @@ CXBMCApp::CXBMCApp(ANativeActivity* nativeActivity)
exit(1);
return;
}
m_broadcastReceiver.reset(new CJNIXBMCBroadcastReceiver(this));
m_mainView.reset(new CJNIXBMCMainView(this));
m_firstrun = true;
m_exiting = false;
Expand Down Expand Up @@ -255,7 +256,7 @@ void CXBMCApp::onStart()
intentFilter.addAction("android.intent.action.HEADSET_PLUG");
intentFilter.addAction("android.media.action.HDMI_AUDIO_PLUG");
intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(*this, intentFilter);
registerReceiver(*m_broadcastReceiver, intentFilter);

m_mediaSession.reset(new CJNIXBMCMediaSession());
}
Expand Down Expand Up @@ -320,7 +321,7 @@ void CXBMCApp::onDestroy()
{
android_printf("%s", __PRETTY_FUNCTION__);

unregisterReceiver(*this);
unregisterReceiver(*m_broadcastReceiver);

m_mediaSession.release();

Expand Down
2 changes: 2 additions & 0 deletions xbmc/platform/android/activity/XBMCApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "JNIXBMCMainView.h"
#include "JNIXBMCMediaSession.h"
#include "JNIXBMCInputDeviceListener.h"
#include "JNIXBMCBroadcastReceiver.h"
#include "platform/xbmc.h"

// forward delares
Expand Down Expand Up @@ -262,6 +263,7 @@ class CXBMCApp
static CCriticalSection m_AppMutex;

CJNIXBMCAudioManagerOnAudioFocusChangeListener m_audioFocusListener;
std::unique_ptr<jni::CJNIXBMCBroadcastReceiver> m_broadcastReceiver;
static std::unique_ptr<CJNIXBMCMainView> m_mainView;
std::unique_ptr<jni::CJNIXBMCMediaSession> m_mediaSession;
bool HasLaunchIntent(const std::string &package);
Expand Down
12 changes: 2 additions & 10 deletions xbmc/platform/android/android_onload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "platform/android/activity/JNIXBMCJsonHandler.h"
#include "platform/android/activity/JNIXBMCFile.h"
#include "platform/android/activity/JNIXBMCInputDeviceListener.h"
#include "platform/android/activity/JNIXBMCBroadcastReceiver.h"
#include "utils/StringUtils.h"
#include "activity/XBMCApp.h"
#include "service/XBMCService.h"
Expand Down Expand Up @@ -64,6 +65,7 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved)
jni::CJNIXBMCJsonHandler::RegisterNatives(env);
jni::CJNIXBMCFile::RegisterNatives(env);
jni::CJNIXBMCInputDeviceListener::RegisterNatives(env);
jni::CJNIXBMCBroadcastReceiver::RegisterNatives(env);

jclass cKASvc = env->FindClass(keepAliveService.c_str());
if(cKASvc)
Expand Down Expand Up @@ -95,16 +97,6 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved)
env->RegisterNatives(cMain, methods, sizeof(methods)/sizeof(methods[0]));
}

jclass cBroadcastReceiver = env->FindClass(bcReceiver.c_str());
if(cBroadcastReceiver)
{
JNINativeMethod methods[] =
{
{"_onReceive", "(Landroid/content/Intent;)V", (void*)&CJNIBroadcastReceiver::_onReceive},
};
env->RegisterNatives(cBroadcastReceiver, methods, sizeof(methods)/sizeof(methods[0]));
}

jclass cSettingsObserver = env->FindClass(settingsObserver.c_str());
if(cSettingsObserver)
{
Expand Down

0 comments on commit 98c2b84

Please sign in to comment.