From 903ee77ee66fecaec472680ae760df32954ac1d5 Mon Sep 17 00:00:00 2001 From: Sharad Binjola Date: Wed, 14 Feb 2024 15:29:01 -0800 Subject: [PATCH] Command working --- .../com/chip/casting/app/MainActivity.java | 5 ++-- .../casting/ActionSelectorFragment.java | 6 ++-- .../casting/ConnectionExampleFragment.java | 6 +++- ...ntentLauncherLaunchURLExampleFragment.java | 14 ++++++---- .../jni/cpp/core/MatterCastingPlayer-JNI.cpp | 15 ++++++---- .../main/jni/cpp/core/MatterCluster-JNI.cpp | 4 +-- .../main/jni/cpp/core/MatterCommand-JNI.cpp | 28 +++++++++++-------- .../main/jni/cpp/core/MatterEndpoint-JNI.cpp | 7 +++-- .../main/jni/cpp/support/Converters-JNI.cpp | 16 +++++------ .../src/main/jni/cpp/support/Converters-JNI.h | 1 - ...ent_matter_content_launcher_launch_url.xml | 2 +- 11 files changed, 58 insertions(+), 46 deletions(-) diff --git a/examples/tv-casting-app/android/App/app/src/main/java/com/chip/casting/app/MainActivity.java b/examples/tv-casting-app/android/App/app/src/main/java/com/chip/casting/app/MainActivity.java index 851513d970e8e2..56ece9dc91cf9a 100644 --- a/examples/tv-casting-app/android/App/app/src/main/java/com/chip/casting/app/MainActivity.java +++ b/examples/tv-casting-app/android/App/app/src/main/java/com/chip/casting/app/MainActivity.java @@ -13,6 +13,7 @@ import com.chip.casting.util.PreferencesConfigurationManager; import com.matter.casting.ActionSelectorFragment; import com.matter.casting.ConnectionExampleFragment; +import com.matter.casting.ContentLauncherLaunchURLExampleFragment; import com.matter.casting.DiscoveryExampleFragment; import com.matter.casting.InitializationExample; import com.matter.casting.core.CastingPlayer; @@ -79,8 +80,8 @@ public void handleConnectionComplete(CastingPlayer castingPlayer) { } @Override - public void handleContentLauncherLaunchURLSelected() { - showFragment(ContentLauncherFragment.newInstance(tvCastingApp)); + public void handleContentLauncherLaunchURLSelected(CastingPlayer selectedCastingPlayer) { + showFragment(ContentLauncherLaunchURLExampleFragment.newInstance(selectedCastingPlayer)); } @Override diff --git a/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ActionSelectorFragment.java b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ActionSelectorFragment.java index e7951fd75d7f4b..2906ad186d6054 100644 --- a/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ActionSelectorFragment.java +++ b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ActionSelectorFragment.java @@ -62,7 +62,7 @@ public View onCreateView( this.selectContentLauncherButtonClickListener = v -> { Log.d(TAG, "handle() called on selectContentLauncherButtonClickListener"); - callback.handleContentLauncherLaunchURLSelected(); + callback.handleContentLauncherLaunchURLSelected(selectedCastingPlayer); }; this.disconnectButtonClickListener = @@ -80,7 +80,7 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); Log.d(TAG, "ActionSelectorFragment.onViewCreated called"); getView() - .findViewById(R.id.selectContentLauncherButton) + .findViewById(R.id.selectContentLauncherLaunchURLButton) .setOnClickListener(selectContentLauncherButtonClickListener); getView().findViewById(R.id.disconnectButton).setOnClickListener(disconnectButtonClickListener); @@ -89,7 +89,7 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { /** Interface for notifying the host. */ public interface Callback { /** Notifies listener to trigger transition on selection of Content Launcher cluster */ - void handleContentLauncherLaunchURLSelected(); + void handleContentLauncherLaunchURLSelected(CastingPlayer selectedCastingPlayer); /** Notifies listener to trigger transition on click of the Disconnect button */ void handleDisconnect(); diff --git a/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ConnectionExampleFragment.java b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ConnectionExampleFragment.java index fa89b028beef95..b7b590b344488b 100644 --- a/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ConnectionExampleFragment.java +++ b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ConnectionExampleFragment.java @@ -117,7 +117,11 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { TAG, "CompletableFuture.thenAccept(), connected to CastingPlayer with deviceId: " + targetCastingPlayer.getDeviceId()); - connectionFragmentNextButton.setEnabled(true); + getActivity() + .runOnUiThread( + () -> { + connectionFragmentNextButton.setEnabled(true); + }); }) .exceptionally( exc -> { diff --git a/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ContentLauncherLaunchURLExampleFragment.java b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ContentLauncherLaunchURLExampleFragment.java index 96e061771a817e..83187153403b4b 100644 --- a/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ContentLauncherLaunchURLExampleFragment.java +++ b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ContentLauncherLaunchURLExampleFragment.java @@ -61,12 +61,14 @@ public View onCreateView( return; } - Endpoint endpoint = - endpoints - .stream() - .filter(e -> SAMPLE_ENDPOINT_VID.equals(e.getVendorId())) - .findFirst() - .get(); + Endpoint endpoint = endpoints.stream().filter(e -> 1 == e.getId()).findFirst().get(); + + /*Endpoint endpoint = + endpoints + .stream() + .filter(e -> SAMPLE_ENDPOINT_VID.equals(e.getVendorId())) + .findFirst() + .get();*/ if (endpoint == null) { Log.e( TAG, diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/MatterCastingPlayer-JNI.cpp b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/MatterCastingPlayer-JNI.cpp index b6215daf65e84d..fbf382258f2b44 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/MatterCastingPlayer-JNI.cpp +++ b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/MatterCastingPlayer-JNI.cpp @@ -65,7 +65,8 @@ JNI_METHOD(jobject, verifyOrEstablishConnection) JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); VerifyOrReturn(env != nullptr, ChipLogError(AppServer, "ConnectCallback, env == nullptr")); - ChipLogProgress(AppServer, "MatterCastingPlayer-JNI::verifyOrEstablishConnection() before JniLocalReferenceManager manager(env)"); + ChipLogProgress(AppServer, + "MatterCastingPlayer-JNI::verifyOrEstablishConnection() before JniLocalReferenceManager manager(env)"); // Ensures proper cleanup of local references to Java objects. JniLocalReferenceManager manager(env); // ChipLogProgress(AppServer, "MatterCastingPlayer-JNI::verifyOrEstablishConnection() before JniGlobalRefWrapper @@ -128,10 +129,11 @@ JNI_METHOD(jobject, verifyOrEstablishConnection) if (desiredEndpointFilterJavaObject == nullptr) { - ChipLogProgress(AppServer, - "MatterCastingPlayer-JNI::verifyOrEstablishConnection() calling CastingPlayer::VerifyOrEstablishConnection() on " - "Casting Player with device ID: %s", - castingPlayer->GetId()); + ChipLogProgress( + AppServer, + "MatterCastingPlayer-JNI::verifyOrEstablishConnection() calling CastingPlayer::VerifyOrEstablishConnection() on " + "Casting Player with device ID: %s", + castingPlayer->GetId()); castingPlayer->VerifyOrEstablishConnection(callback, static_cast(commissioningWindowTimeoutSec)); } else @@ -200,7 +202,8 @@ JNI_METHOD(void, disconnect) ChipLogProgress(AppServer, "MatterCastingPlayer-JNI::disconnect()"); core::CastingPlayer * castingPlayer = support::convertCastingPlayerFromJavaToCpp(thiz); - VerifyOrReturn(castingPlayer != nullptr, ChipLogError(AppServer, "MatterCastingPlayer-JNI::disconnect() castingPlayer == nullptr")); + VerifyOrReturn(castingPlayer != nullptr, + ChipLogError(AppServer, "MatterCastingPlayer-JNI::disconnect() castingPlayer == nullptr")); castingPlayer->Disconnect(); } diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/MatterCluster-JNI.cpp b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/MatterCluster-JNI.cpp index d2ee86157b78af..9d57424daaa004 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/MatterCluster-JNI.cpp +++ b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/MatterCluster-JNI.cpp @@ -64,8 +64,8 @@ JNI_METHOD(jobject, getCommand) VerifyOrReturnValue(cluster != nullptr, 0, ChipLogError(AppServer, "MatterCluster-JNI::getCommand() cluster == nullptr")); void * command = cluster->GetCommand(chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Id); - jobject commandJavaObject = - support::convertCommandFromCppToJava(command, "com/matter/casting/clusters/MatterCommands$ContentLauncherClusterLaunchURLCommand"); + jobject commandJavaObject = support::convertCommandFromCppToJava( + command, "com/matter/casting/clusters/MatterCommands$ContentLauncherClusterLaunchURLCommand"); env->ReleaseStringUTFChars(jClassName, className); return commandJavaObject; } diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/MatterCommand-JNI.cpp b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/MatterCommand-JNI.cpp index b38283d4642e41..89daf72042c51a 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/MatterCommand-JNI.cpp +++ b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/MatterCommand-JNI.cpp @@ -66,29 +66,35 @@ CHIP_ERROR convertRequestFromJavaToCpp(jobject inRequest, return CHIP_NO_ERROR; } -jobject -convertResponseFromCppToJava(const chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type::ResponseType * responseData) +jobject convertResponseFromCppToJava(const chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type::ResponseType * response) { - VerifyOrReturnValue(responseData != nullptr, nullptr); + VerifyOrReturnValue(response != nullptr, nullptr); JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); jclass responseTypeClass = nullptr; VerifyOrReturnValue( JniReferences::GetInstance().GetClassRef(env, "com/matter/casting/clusters/MatterCommands$ContentLauncherClusterResponse", - responseTypeClass) != CHIP_NO_ERROR, - nullptr); + responseTypeClass) == CHIP_NO_ERROR, + nullptr, ChipLogError(AppServer, "convertResponseFromCppToJava could not get ContentLauncherClusterResponse class ref")); jmethodID constructor = env->GetMethodID(responseTypeClass, "", "()V"); jobject jResponseObj = env->NewObject(responseTypeClass, constructor); - if (responseData->data.HasValue()) + if (response->data.HasValue()) { jfieldID dataField = env->GetFieldID(responseTypeClass, "data", "Ljava/lang/String;"); - env->SetObjectField(jResponseObj, dataField, env->NewStringUTF(responseData->data.Value().data())); + char * buffer = new char[response->data.Value().size() + 1]; + strncpy(buffer, response->data.Value().data(), response->data.Value().size()); + buffer[response->data.Value().size()] = '\0'; + env->SetObjectField(jResponseObj, dataField, env->NewStringUTF(buffer)); + delete[] buffer; } + jclass integerClass = env->FindClass("java/lang/Integer"); + jmethodID valueOf = env->GetStaticMethodID(integerClass, "valueOf", "(I)Ljava/lang/Integer;"); + jobject statusObj = env->CallStaticObjectMethod(integerClass, valueOf, static_cast(response->status)); jfieldID statusField = env->GetFieldID(responseTypeClass, "status", "Ljava/lang/Integer;"); - env->SetIntField(jResponseObj, statusField, static_cast(responseData->status)); + env->SetObjectField(jResponseObj, statusField, statusObj); return jResponseObj; } @@ -103,7 +109,7 @@ JNI_METHOD(jobject, invoke) // Create a new Java CompletableFuture jclass completableFutureClass = env->FindClass("java/util/concurrent/CompletableFuture"); - jmethodID completableFutureConstructor = env->GetMethodID(completableFutureClass, "", "()Ljava/lang/Object;"); + jmethodID completableFutureConstructor = env->GetMethodID(completableFutureClass, "", "()V"); jobject completableFutureObj = env->NewObject(completableFutureClass, completableFutureConstructor); jobject completableFutureObjGlobalRef = env->NewGlobalRef(completableFutureObj); VerifyOrReturnValue(completableFutureObjGlobalRef != nullptr, nullptr, @@ -141,10 +147,8 @@ JNI_METHOD(jobject, invoke) static_cast(response.data.Value().size()), response.data.Value().data()); jobject jResponse = convertResponseFromCppToJava(&response); - + JNIEnv * env = chip::JniReferences::GetInstance().GetEnvForCurrentThread(); chip::DeviceLayer::StackUnlock unlock; - ChipLogProgress(AppServer, "LaunchURL Success, before env->CallBooleanMethod"); - JNIEnv * env = chip::JniReferences::GetInstance().GetEnvForCurrentThread(); env->CallBooleanMethod(completableFutureObjGlobalRef, completeMethod, jResponse); }, [completableFutureObjGlobalRef, completeExceptionallyMethod](void * context, CHIP_ERROR error) { diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/MatterEndpoint-JNI.cpp b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/MatterEndpoint-JNI.cpp index 9415b78dbbd03c..de132f133e5072 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/MatterEndpoint-JNI.cpp +++ b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/MatterEndpoint-JNI.cpp @@ -78,7 +78,8 @@ JNI_METHOD(jobject, getCastingPlayer) chip::DeviceLayer::StackLock lock; ChipLogProgress(AppServer, "MatterEndpoint-JNI::getCastingPlayer() called"); Endpoint * endpoint = support::convertEndpointFromJavaToCpp(thiz); - VerifyOrReturnValue(endpoint != nullptr, 0, ChipLogError(AppServer, "MatterEndpoint-JNI::getCastingPlayer() endpoint == nullptr")); + VerifyOrReturnValue(endpoint != nullptr, 0, + ChipLogError(AppServer, "MatterEndpoint-JNI::getCastingPlayer() endpoint == nullptr")); return support::convertCastingPlayerFromCppToJava(std::shared_ptr(endpoint->GetCastingPlayer())); } @@ -100,8 +101,8 @@ JNI_METHOD(jobject, getCluster) matter::casting::memory::Strong contentLauncherCluster = endpoint->GetCluster(); - jobject clusterJavaObject = - support::convertClusterFromCppToJava(contentLauncherCluster, "com/matter/casting/clusters/MatterClusters$ContentLauncherCluster"); + jobject clusterJavaObject = support::convertClusterFromCppToJava( + contentLauncherCluster, "com/matter/casting/clusters/MatterClusters$ContentLauncherCluster"); env->ReleaseStringUTFChars(jClassName, className); return clusterJavaObject; } diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/support/Converters-JNI.cpp b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/support/Converters-JNI.cpp index 6986d1ce37adde..26f5386129a3eb 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/support/Converters-JNI.cpp +++ b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/support/Converters-JNI.cpp @@ -55,7 +55,8 @@ jobject convertEndpointFromCppToJava(matter::casting::memory::StrongGetMethodID(matterEndpointJavaClass, "", "()V"); if (constructor == nullptr) { - ChipLogError(AppServer, "EndpointConverter-JNI.convertEndpointFromCppToJava() could not locate MatterEndpoint Java class constructor"); + ChipLogError(AppServer, + "EndpointConverter-JNI.convertEndpointFromCppToJava() could not locate MatterEndpoint Java class constructor"); return nullptr; } @@ -64,7 +65,8 @@ jobject convertEndpointFromCppToJava(matter::casting::memory::StrongNewObject(matterEndpointJavaClass, constructor); if (jMatterEndpoint == nullptr) { - ChipLogError(AppServer, "EndpointConverter-JNI.convertEndpointFromCppToJava(): Could not create MatterEndpoint Java object"); + ChipLogError(AppServer, + "EndpointConverter-JNI.convertEndpointFromCppToJava(): Could not create MatterEndpoint Java object"); return jMatterEndpoint; } // Set the value of the _cppEndpoint field in the Java object to the C++ Endpoint pointer. @@ -96,8 +98,7 @@ jobject convertCastingPlayerFromCppToJava(matter::casting::memory::StrongFindClass("com/matter/casting/core/MatterCastingPlayer"); if (matterCastingPlayerJavaClass == nullptr) { - ChipLogError(AppServer, - "convertCastingPlayerFromCppToJava() could not locate MatterCastingPlayer Java class"); + ChipLogError(AppServer, "convertCastingPlayerFromCppToJava() could not locate MatterCastingPlayer Java class"); return nullptr; } @@ -107,9 +108,7 @@ jobject convertCastingPlayerFromCppToJava(matter::casting::memory::StrongGetVendorId()), (jlong) (player->GetDeviceType())); if (jMatterCastingPlayer == nullptr) { - ChipLogError(AppServer, - "convertCastingPlayerFromCppToJava(): Could not create MatterCastingPlayer Java object"); + ChipLogError(AppServer, "convertCastingPlayerFromCppToJava(): Could not create MatterCastingPlayer Java object"); return jMatterCastingPlayer; } // Set the value of the _cppCastingPlayer field in the Java object to the C++ CastingPlayer pointer. diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/support/Converters-JNI.h b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/support/Converters-JNI.h index 64ab1a233ae58e..4b66781ce70cd9 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/support/Converters-JNI.h +++ b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/support/Converters-JNI.h @@ -59,7 +59,6 @@ core::CastingPlayer * convertCastingPlayerFromJavaToCpp(jobject jCastingPlayerOb */ jobject convertClusterFromCppToJava(matter::casting::memory::Strong cluster, const char * className); - core::BaseCluster * convertClusterFromJavaToCpp(jobject jClusterObject); /** diff --git a/examples/tv-casting-app/android/App/app/src/main/res/layout/fragment_matter_content_launcher_launch_url.xml b/examples/tv-casting-app/android/App/app/src/main/res/layout/fragment_matter_content_launcher_launch_url.xml index 77aa5e1b3e250e..ef1cbb9b23c3cf 100644 --- a/examples/tv-casting-app/android/App/app/src/main/res/layout/fragment_matter_content_launcher_launch_url.xml +++ b/examples/tv-casting-app/android/App/app/src/main/res/layout/fragment_matter_content_launcher_launch_url.xml @@ -79,7 +79,7 @@