Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Content Launcher to android tv-casting-app #18356

Merged
merged 1 commit into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void run() {

/** Interface for notifying the host. */
public interface Callback {
/** Notifies listener of Skip to manual Commissioning Button click. */
/** Notifies listener of Commissioning Button click. */
void handleCommissioningButtonClicked(DiscoveredNodeData selectedCommissioner);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import androidx.fragment.app.Fragment;
import com.chip.casting.TvCastingApp;
import com.chip.casting.dnssd.DiscoveredNodeData;
import com.chip.casting.platform.MatterCallbackHandler;
import com.chip.casting.util.GlobalCastingConstants;

/** A {@link Fragment} to get the TV Casting App commissioned. */
Expand All @@ -19,6 +20,10 @@ public class CommissioningFragment extends Fragment {
private final TvCastingApp tvCastingApp;
private final DiscoveredNodeData selectedCommissioner;

private boolean initServerSuccess;
private boolean openCommissioningWindowSuccess;
private boolean sendUdcSuccess;

public CommissioningFragment(TvCastingApp tvCastingApp, DiscoveredNodeData selectedCommissioner) {
this.tvCastingApp = tvCastingApp;
this.selectedCommissioner = selectedCommissioner;
Expand All @@ -43,40 +48,68 @@ public void onCreate(Bundle savedInstanceState) {
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
Callback callback = (CommissioningFragment.Callback) this.getActivity();
this.initServerSuccess =
tvCastingApp.initServer(
new MatterCallbackHandler() {
@Override
public boolean handle(boolean success) {
Log.d(
TAG, "handle() called on CommissioningComplete event with success " + success);
if (success) {
callback.handleCommissioningComplete();
}
return true;
}
});

if (this.initServerSuccess) {
this.openCommissioningWindowSuccess =
tvCastingApp.openBasicCommissioningWindow(
GlobalCastingConstants.CommissioningWindowDurationSecs);
if (this.openCommissioningWindowSuccess) {
if (selectedCommissioner != null && selectedCommissioner.getNumIPs() > 0) {
String ipAddress = selectedCommissioner.getIpAddresses().get(0).getHostAddress();
Log.d(
TAG,
"CommissioningFragment calling tvCastingApp.sendUserDirectedCommissioningRequest with IP: "
+ ipAddress
+ " port: "
+ selectedCommissioner.getPort());

this.sendUdcSuccess =
tvCastingApp.sendUserDirectedCommissioningRequest(
ipAddress, selectedCommissioner.getPort());
}
}
}
return inflater.inflate(R.layout.fragment_commissioning, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

tvCastingApp.initServer();

String commissioningWindowStatus = "Failed to open commissioning window";
if (tvCastingApp.openBasicCommissioningWindow(
GlobalCastingConstants.CommissioningWindowDurationSecs)) {
commissioningWindowStatus = "Commissioning window has been opened. Commission manually.";
if (selectedCommissioner != null && selectedCommissioner.getNumIPs() > 0) {
String ipAddress = selectedCommissioner.getIpAddresses().get(0).getHostAddress();
Log.d(
TAG,
"CommissioningFragment calling tvCastingApp.sendUserDirectedCommissioningRequest with IP: "
+ ipAddress
+ " port: "
+ selectedCommissioner.getPort());

if (tvCastingApp.sendUserDirectedCommissioningRequest(
ipAddress, selectedCommissioner.getPort())) {
if (this.initServerSuccess) {
if (this.openCommissioningWindowSuccess) {
commissioningWindowStatus = "Commissioning window has been opened. Commission manually.";
if (this.sendUdcSuccess) {
commissioningWindowStatus =
"Commissioning window has been opened. Commissioning requested from "
+ selectedCommissioner.getDeviceName();
}
TextView onboardingPayloadView = getView().findViewById(R.id.onboardingPayload);
onboardingPayloadView.setText("Onboarding PIN: " + GlobalCastingConstants.SetupPasscode);
}
TextView onboardingPayloadView = getView().findViewById(R.id.onboardingPayload);
onboardingPayloadView.setText("Onboarding PIN: " + GlobalCastingConstants.SetupPasscode);
}

TextView commissioningWindowStatusView = getView().findViewById(R.id.commissioningWindowStatus);
commissioningWindowStatusView.setText(commissioningWindowStatus);
}

/** Interface for notifying the host. */
public interface Callback {
/** Notifies listener to trigger transition on completion of commissioning */
void handleCommissioningComplete();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.chip.casting.app;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.chip.casting.TvCastingApp;

/** A {@link Fragment} to send Content Launcher commands from the TV Casting App. */
public class ContentLauncherFragment extends Fragment {
private static final String TAG = ContentLauncherFragment.class.getSimpleName();

private final TvCastingApp tvCastingApp;

private View.OnClickListener launchUrlButtonClickListener;

public ContentLauncherFragment(TvCastingApp tvCastingApp) {
this.tvCastingApp = tvCastingApp;
}

/**
* Use this factory method to create a new instance of this fragment using the provided
* parameters.
*
* @param tvCastingApp TV Casting App (JNI)
* @return A new instance of fragment ContentLauncherFragment.
*/
public static ContentLauncherFragment newInstance(TvCastingApp tvCastingApp) {
return new ContentLauncherFragment(tvCastingApp);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.launchUrlButtonClickListener =
new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText contentUrl = getView().findViewById(R.id.contentUrlEditText);
EditText contentDisplayString =
getView().findViewById(R.id.contentDisplayStringEditText);
tvCastingApp.contentLauncherLaunchURL(
contentUrl.getText().toString(), contentDisplayString.getText().toString());
}
};

return inflater.inflate(R.layout.fragment_content_launcher, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, "ContentLauncherFragment.onViewCreated called");
getView().findViewById(R.id.launchUrlButton).setOnClickListener(launchUrlButtonClickListener);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.chip.casting.util.GlobalCastingConstants;

public class MainActivity extends AppCompatActivity
implements CommissionerDiscoveryFragment.Callback {
implements CommissionerDiscoveryFragment.Callback, CommissioningFragment.Callback {

private ChipAppServer chipAppServer;
private TvCastingApp tvCastingApp;
Expand All @@ -43,6 +43,11 @@ public void handleCommissioningButtonClicked(DiscoveredNodeData commissioner) {
showFragment(CommissioningFragment.newInstance(tvCastingApp, commissioner));
}

@Override
public void handleCommissioningComplete() {
showFragment(ContentLauncherFragment.newInstance(tvCastingApp));
}

private void initJni() {
tvCastingApp =
new TvCastingApp((app, clusterId, duration) -> app.openBasicCommissioningWindow(duration));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.chip.casting.platform;

public interface MatterCallbackHandler {
boolean handle(boolean success);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ private void postClusterInit(int clusterId, int endpoint) {

public native boolean discoverCommissioners();

public native void initServer();
public native boolean initServer(Object commissioningCompleteHandler);

public native void contentLauncherLaunchURL(String contentUrl, String contentDisplayStr);

static {
System.loadLibrary("TvCastingApp");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
*
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "CallbackHelper.h"

#include <lib/support/JniReferences.h>

using namespace chip;

struct MatterCallbackHandler gCommissioningCompleteHandler;

CHIP_ERROR SetUpMatterCallbackHandler(JNIEnv * env, jobject inHandler, MatterCallbackHandler & callback)
{
ChipLogProgress(AppServer, "SetUpMatterCallbackHandler called");
CHIP_ERROR err = CHIP_NO_ERROR;

callback.object = env->NewGlobalRef(inHandler);
VerifyOrExit(callback.object != nullptr, ChipLogError(AppServer, "Failed to NewGlobalRef for handler object"));

callback.clazz = env->GetObjectClass(callback.object);
VerifyOrExit(callback.clazz != nullptr, ChipLogError(AppServer, "Failed to get handler Java class"));

callback.method = env->GetMethodID(callback.clazz, "handle", "(Z)Z");
if (callback.method == nullptr)
{
ChipLogError(AppServer, "Failed to access 'handle' method");
env->ExceptionClear();
}

exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "SetUpMatterCallbackHandler error: %s", err.AsString());
return err;
}

return err;
}

CHIP_ERROR CommissioningCompleteHandler()
{
ChipLogProgress(AppServer, "CommissioningCompleteHandler called");

JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
CHIP_ERROR err = CHIP_NO_ERROR;
VerifyOrExit(gCommissioningCompleteHandler.object != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(gCommissioningCompleteHandler.method != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(env->CallBooleanMethod(gCommissioningCompleteHandler.object, gCommissioningCompleteHandler.method,
static_cast<jboolean>(true)) != JNI_FALSE,
err = CHIP_ERROR_INCORRECT_STATE);
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "CommissioningCompleteHandler status error: %s", err.AsString());
}

return err;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
*
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <jni.h>
#include <lib/core/CHIPError.h>

struct MatterCallbackHandler
{
jobject object = nullptr;
jclass clazz = nullptr;
jmethodID method = nullptr;
};

extern struct MatterCallbackHandler gCommissioningCompleteHandler;

CHIP_ERROR SetUpMatterCallbackHandler(JNIEnv * env, jobject inHandler, MatterCallbackHandler & callback);

CHIP_ERROR CommissioningCompleteHandler();
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include "TvCastingApp-JNI.h"
#include "CallbackHelper.h"
#include "CastingServer.h"
#include "JNIDACProvider.h"

Expand Down Expand Up @@ -146,8 +147,30 @@ JNI_METHOD(jboolean, discoverCommissioners)(JNIEnv *, jobject)
return true;
}

JNI_METHOD(void, initServer)(JNIEnv *, jobject)
JNI_METHOD(jboolean, initServer)(JNIEnv * env, jobject, jobject jCommissioningCompleteHandler)
{
ChipLogProgress(AppServer, "JNI_METHOD initServer called");
CastingServer::GetInstance()->InitServer();
CHIP_ERROR err = SetUpMatterCallbackHandler(env, jCommissioningCompleteHandler, gCommissioningCompleteHandler);
if (err == CHIP_NO_ERROR)
{
CastingServer::GetInstance()->InitServer(CommissioningCompleteHandler);
return true;
}
else
{
ChipLogError(AppServer, "initServer error: %s", err.AsString());
return false;
}
}

JNI_METHOD(void, contentLauncherLaunchURL)(JNIEnv * env, jobject, jstring contentUrl, jstring contentDisplayStr)
{
ChipLogProgress(AppServer, "JNI_METHOD contentLauncherLaunchURL called");
const char * nativeContentUrl = env->GetStringUTFChars(contentUrl, 0);
const char * nativeContentDisplayStr = env->GetStringUTFChars(contentDisplayStr, 0);

CastingServer::GetInstance()->ContentLauncherLaunchURL(nativeContentUrl, nativeContentDisplayStr);

env->ReleaseStringUTFChars(contentUrl, nativeContentUrl);
env->ReleaseStringUTFChars(contentDisplayStr, nativeContentDisplayStr);
}
Loading