From 9dc0ad010cf79cf030b5a080cec512c8fdba9ac9 Mon Sep 17 00:00:00 2001 From: Sharad Binjola Date: Wed, 21 Aug 2024 12:53:58 -0700 Subject: [PATCH] Fixed JNI loading issues --- .../android/App/app/build.gradle | 1 + .../jni/com/chip/casting/TvCastingApp.java | 109 +++++++++++++----- .../java/com/matter/casting/MainActivity.java | 3 - .../src/main/jni/cpp/core/CastingApp-JNI.cpp | 11 ++ 4 files changed, 92 insertions(+), 32 deletions(-) diff --git a/examples/tv-casting-app/android/App/app/build.gradle b/examples/tv-casting-app/android/App/app/build.gradle index 4302c740de769e..757e6a6522f5da 100644 --- a/examples/tv-casting-app/android/App/app/build.gradle +++ b/examples/tv-casting-app/android/App/app/build.gradle @@ -48,6 +48,7 @@ android { 'src/main/java', 'src/main/jni', 'src/compat/java', + 'src/compat/jni', ] // uncomment this code to debug diff --git a/examples/tv-casting-app/android/App/app/src/compat/jni/com/chip/casting/TvCastingApp.java b/examples/tv-casting-app/android/App/app/src/compat/jni/com/chip/casting/TvCastingApp.java index df330d29e1c57a..cd965905065bee 100644 --- a/examples/tv-casting-app/android/App/app/src/compat/jni/com/chip/casting/TvCastingApp.java +++ b/examples/tv-casting-app/android/App/app/src/compat/jni/com/chip/casting/TvCastingApp.java @@ -348,11 +348,26 @@ public boolean purgeCache() { return true; } + private Endpoint getTargetEndpoint(ContentApp contentApp) { + if (targetCastingPlayer == null || targetCastingPlayer.getEndpoints() == null) { + return null; + } + + for (Endpoint endpoint : targetCastingPlayer.getEndpoints()) { + if (endpoint.getId() == contentApp.getEndpointId()) { + return endpoint; + } + } + + return null; + } + public boolean contentLauncherLaunchURL( ContentApp contentApp, String contentUrl, String contentDisplayStr, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.ContentLauncherCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.ContentLauncherCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.ContentLauncherCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -387,8 +402,9 @@ public boolean contentLauncher_launchContent( String data, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.ContentLauncherCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.ContentLauncherCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.ContentLauncherCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -444,8 +460,9 @@ public void onError(Exception error) { public boolean mediaPlayback_play(ContentApp contentApp, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.MediaPlaybackCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.MediaPlaybackCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.MediaPlaybackCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -471,8 +488,9 @@ public void onError(Exception error) { public boolean mediaPlayback_pause(ContentApp contentApp, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.MediaPlaybackCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.MediaPlaybackCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.MediaPlaybackCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -498,8 +516,9 @@ public void onError(Exception error) { public boolean mediaPlayback_stopPlayback(ContentApp contentApp, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.MediaPlaybackCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.MediaPlaybackCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.MediaPlaybackCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -525,8 +544,9 @@ public void onError(Exception error) { public boolean mediaPlayback_next(ContentApp contentApp, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.MediaPlaybackCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.MediaPlaybackCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.MediaPlaybackCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -552,8 +572,10 @@ public void onError(Exception error) { public boolean mediaPlayback_previous(ContentApp contentApp, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.MediaPlaybackCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.MediaPlaybackCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.MediaPlaybackCluster.class) : null; + if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -579,8 +601,9 @@ public void onError(Exception error) { public boolean mediaPlayback_rewind(ContentApp contentApp, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.MediaPlaybackCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.MediaPlaybackCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.MediaPlaybackCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -607,8 +630,9 @@ public void onError(Exception error) { public boolean mediaPlayback_fastForward(ContentApp contentApp, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.MediaPlaybackCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.MediaPlaybackCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.MediaPlaybackCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -635,8 +659,9 @@ public void onError(Exception error) { public boolean mediaPlayback_startOver(ContentApp contentApp, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.MediaPlaybackCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.MediaPlaybackCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.MediaPlaybackCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -662,8 +687,9 @@ public void onError(Exception error) { public boolean mediaPlayback_seek(ContentApp contentApp, long position, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.MediaPlaybackCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.MediaPlaybackCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.MediaPlaybackCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -691,8 +717,9 @@ public void onError(Exception error) { public boolean mediaPlayback_skipForward( ContentApp contentApp, long deltaPositionMilliseconds, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.MediaPlaybackCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.MediaPlaybackCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.MediaPlaybackCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -720,8 +747,9 @@ public void onError(Exception error) { public boolean mediaPlayback_skipBackward( ContentApp contentApp, long deltaPositionMilliseconds, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.MediaPlaybackCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.MediaPlaybackCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.MediaPlaybackCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -754,8 +782,9 @@ public boolean mediaPlayback_subscribeToCurrentState( int maxInterval, SubscriptionEstablishedCallback subscriptionEstablishedHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.MediaPlaybackCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.MediaPlaybackCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.MediaPlaybackCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -791,8 +820,11 @@ public boolean applicationLauncher_launchApp( byte[] data, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.ApplicationLauncherCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.ApplicationLauncherCluster.class); + endpoint != null + ? endpoint.getCluster(ChipClusters.ApplicationLauncherCluster.class) + : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -831,8 +863,11 @@ public void onError(Exception error) { public boolean applicationLauncher_stopApp( ContentApp contentApp, short catalogVendorId, String applicationId, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.ApplicationLauncherCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.ApplicationLauncherCluster.class); + endpoint != null + ? endpoint.getCluster(ChipClusters.ApplicationLauncherCluster.class) + : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -870,8 +905,11 @@ public void onError(Exception error) { public boolean applicationLauncher_hideApp( ContentApp contentApp, short catalogVendorId, String applicationId, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.ApplicationLauncherCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.ApplicationLauncherCluster.class); + endpoint != null + ? endpoint.getCluster(ChipClusters.ApplicationLauncherCluster.class) + : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -909,8 +947,9 @@ public void onError(Exception error) { public boolean targetNavigator_navigateTarget( ContentApp contentApp, byte target, String data, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.TargetNavigatorCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.TargetNavigatorCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.TargetNavigatorCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -944,8 +983,9 @@ public boolean targetNavigator_subscribeToTargetList( int maxInterval, SubscriptionEstablishedCallback subscriptionEstablishedHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.TargetNavigatorCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.TargetNavigatorCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.TargetNavigatorCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -985,8 +1025,9 @@ public void onSubscriptionEstablished(long subscriptionId) { public boolean keypadInput_sendKey(ContentApp contentApp, byte keyCode, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.KeypadInputCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.KeypadInputCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.KeypadInputCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -1016,8 +1057,9 @@ public boolean applicationBasic_readVendorName( SuccessCallback readSuccessHandler, FailureCallback readFailureHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.ApplicationBasicCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.ApplicationBasicCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.ApplicationBasicCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -1044,8 +1086,9 @@ public boolean applicationBasic_readVendorID( SuccessCallback readSuccessHandler, FailureCallback readFailureHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.ApplicationBasicCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.ApplicationBasicCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.ApplicationBasicCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -1072,8 +1115,9 @@ public boolean applicationBasic_readApplicationName( SuccessCallback readSuccessHandler, FailureCallback readFailureHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.ApplicationBasicCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.ApplicationBasicCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.ApplicationBasicCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -1100,8 +1144,9 @@ public boolean applicationBasic_readProductID( SuccessCallback readSuccessHandler, FailureCallback readFailureHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.ApplicationBasicCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.ApplicationBasicCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.ApplicationBasicCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -1128,8 +1173,9 @@ public boolean applicationBasic_readApplicationVersion( SuccessCallback readSuccessHandler, FailureCallback readFailureHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.ApplicationBasicCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.ApplicationBasicCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.ApplicationBasicCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -1153,8 +1199,9 @@ public void onError(Exception error) { public boolean onOff_on(ContentApp contentApp, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.OnOffCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.OnOffCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.OnOffCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -1180,8 +1227,9 @@ public void onError(Exception error) { public boolean onOff_off(ContentApp contentApp, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.OnOffCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.OnOffCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.OnOffCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -1207,8 +1255,9 @@ public void onError(Exception error) { public boolean onOff_toggle(ContentApp contentApp, Object responseHandler) { + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.OnOffCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.OnOffCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.OnOffCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; @@ -1242,8 +1291,10 @@ public boolean messages_presentMessages( String messageText, Optional> responses, Object responseHandler) { + + Endpoint endpoint = getTargetEndpoint(contentApp); ChipClusters.MessagesCluster cluster = - contentApp.getEndpoint().getCluster(ChipClusters.MessagesCluster.class); + endpoint != null ? endpoint.getCluster(ChipClusters.MessagesCluster.class) : null; if (cluster == null) { Log.e(TAG, "Cluster not found"); return false; diff --git a/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/MainActivity.java b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/MainActivity.java index ad971a144f132c..53908322d10155 100644 --- a/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/MainActivity.java +++ b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/MainActivity.java @@ -2,11 +2,9 @@ import android.os.Bundle; import android.util.Log; - import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; - import com.R; import com.chip.casting.AppParameters; import com.chip.casting.DiscoveredNodeData; @@ -19,7 +17,6 @@ import com.chip.casting.app.SelectClusterFragment; import com.chip.casting.util.GlobalCastingConstants; import com.matter.casting.core.CastingPlayer; - import java.util.Random; public class MainActivity extends AppCompatActivity diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/CastingApp-JNI.cpp b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/CastingApp-JNI.cpp index 993ca933f1da83..2242619df27431 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/CastingApp-JNI.cpp +++ b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/CastingApp-JNI.cpp @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -37,6 +38,16 @@ using namespace chip; #define JNI_METHOD(RETURN, METHOD_NAME) extern "C" JNIEXPORT RETURN JNICALL Java_com_matter_casting_core_CastingApp_##METHOD_NAME +jint JNI_OnLoad(JavaVM * jvm, void * reserved) +{ + return AndroidAppServerJNI_OnLoad(jvm, reserved); +} + +void JNI_OnUnload(JavaVM * jvm, void * reserved) +{ + return AndroidAppServerJNI_OnUnload(jvm, reserved); +} + namespace matter { namespace casting { namespace core {