Skip to content

Commit

Permalink
Command working
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Feb 14, 2024
1 parent 3c64ebd commit 903ee77
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public View onCreateView(
this.selectContentLauncherButtonClickListener =
v -> {
Log.d(TAG, "handle() called on selectContentLauncherButtonClickListener");
callback.handleContentLauncherLaunchURLSelected();
callback.handleContentLauncherLaunchURLSelected(selectedCastingPlayer);
};

this.disconnectButtonClickListener =
Expand All @@ -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);
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<unsigned long long int>(commissioningWindowTimeoutSec));
}
else
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<init>", "()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<jint>(response->status));
jfieldID statusField = env->GetFieldID(responseTypeClass, "status", "Ljava/lang/Integer;");
env->SetIntField(jResponseObj, statusField, static_cast<jint>(responseData->status));
env->SetObjectField(jResponseObj, statusField, statusObj);

return jResponseObj;
}
Expand All @@ -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, "<init>", "()Ljava/lang/Object;");
jmethodID completableFutureConstructor = env->GetMethodID(completableFutureClass, "<init>", "()V");
jobject completableFutureObj = env->NewObject(completableFutureClass, completableFutureConstructor);
jobject completableFutureObjGlobalRef = env->NewGlobalRef(completableFutureObj);
VerifyOrReturnValue(completableFutureObjGlobalRef != nullptr, nullptr,
Expand Down Expand Up @@ -141,10 +147,8 @@ JNI_METHOD(jobject, invoke)
static_cast<int>(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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CastingPlayer>(endpoint->GetCastingPlayer()));
}

Expand All @@ -100,8 +101,8 @@ JNI_METHOD(jobject, getCluster)
matter::casting::memory::Strong<matter::casting::clusters::content_launcher::ContentLauncherCluster>
contentLauncherCluster = endpoint->GetCluster<matter::casting::clusters::content_launcher::ContentLauncherCluster>();

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ jobject convertEndpointFromCppToJava(matter::casting::memory::Strong<core::Endpo
jmethodID constructor = env->GetMethodID(matterEndpointJavaClass, "<init>", "()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;
}

Expand All @@ -64,7 +65,8 @@ jobject convertEndpointFromCppToJava(matter::casting::memory::Strong<core::Endpo
jMatterEndpoint = env->NewObject(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.
Expand Down Expand Up @@ -96,8 +98,7 @@ jobject convertCastingPlayerFromCppToJava(matter::casting::memory::Strong<core::
jclass matterCastingPlayerJavaClass = env->FindClass("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;
}

Expand All @@ -107,9 +108,7 @@ jobject convertCastingPlayerFromCppToJava(matter::casting::memory::Strong<core::
"(ZLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/util/List;IIIJ)V");
if (constructor == nullptr)
{
ChipLogError(
AppServer,
"convertCastingPlayerFromCppToJava() could not locate MatterCastingPlayer Java class constructor");
ChipLogError(AppServer, "convertCastingPlayerFromCppToJava() could not locate MatterCastingPlayer Java class constructor");
return nullptr;
}

Expand Down Expand Up @@ -143,8 +142,7 @@ jobject convertCastingPlayerFromCppToJava(matter::casting::memory::Strong<core::
(jint) (player->GetVendorId()), (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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ core::CastingPlayer * convertCastingPlayerFromJavaToCpp(jobject jCastingPlayerOb
*/
jobject convertClusterFromCppToJava(matter::casting::memory::Strong<core::BaseCluster> cluster, const char * className);


core::BaseCluster * convertClusterFromJavaToCpp(jobject jClusterObject);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@

<TextView
android:id="@+id/launchUrlStatus"
android:layout_width="150dp"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
</LinearLayout>
Expand Down

0 comments on commit 903ee77

Please sign in to comment.