Skip to content

Commit

Permalink
Merged converters, cleaned up headers
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Feb 14, 2024
1 parent 1c3ff17 commit d342284
Show file tree
Hide file tree
Showing 26 changed files with 638 additions and 870 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,70 +28,70 @@

/** An interstitial {@link Fragment} to select one of the supported media actions to perform */
public class ActionSelectorFragment extends Fragment {
private static final String TAG = ActionSelectorFragment.class.getSimpleName();
private static final String TAG = ActionSelectorFragment.class.getSimpleName();

private final CastingPlayer selectedCastingPlayer;
private final CastingPlayer selectedCastingPlayer;

private View.OnClickListener selectContentLauncherButtonClickListener;
private View.OnClickListener disconnectButtonClickListener;
private View.OnClickListener selectContentLauncherButtonClickListener;
private View.OnClickListener disconnectButtonClickListener;

public ActionSelectorFragment(CastingPlayer selectedCastingPlayer) {
this.selectedCastingPlayer = selectedCastingPlayer;
}
public ActionSelectorFragment(CastingPlayer selectedCastingPlayer) {
this.selectedCastingPlayer = selectedCastingPlayer;
}

/**
* Use this factory method to create a new instance of this fragment using the provided
* parameters.
*
* @param selectedCastingPlayer CastingPlayer that the casting app connected to
* @return A new instance of fragment SelectActionFragment.
*/
public static ActionSelectorFragment newInstance(CastingPlayer selectedCastingPlayer) {
return new ActionSelectorFragment(selectedCastingPlayer);
}
/**
* Use this factory method to create a new instance of this fragment using the provided
* parameters.
*
* @param selectedCastingPlayer CastingPlayer that the casting app connected to
* @return A new instance of fragment SelectActionFragment.
*/
public static ActionSelectorFragment newInstance(CastingPlayer selectedCastingPlayer) {
return new ActionSelectorFragment(selectedCastingPlayer);
}

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

@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ActionSelectorFragment.Callback callback = (ActionSelectorFragment.Callback) this.getActivity();
this.selectContentLauncherButtonClickListener =
v -> {
Log.d(TAG, "handle() called on selectContentLauncherButtonClickListener");
callback.handleContentLauncherLaunchURLSelected();
};
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ActionSelectorFragment.Callback callback = (ActionSelectorFragment.Callback) this.getActivity();
this.selectContentLauncherButtonClickListener =
v -> {
Log.d(TAG, "handle() called on selectContentLauncherButtonClickListener");
callback.handleContentLauncherLaunchURLSelected();
};

this.disconnectButtonClickListener =
v -> {
Log.d(TAG, "Disconnecting from current casting player");
selectedCastingPlayer.disconnect();
callback.handleDisconnect();
};
this.disconnectButtonClickListener =
v -> {
Log.d(TAG, "Disconnecting from current casting player");
selectedCastingPlayer.disconnect();
callback.handleDisconnect();
};

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

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, "ActionSelectorFragment.onViewCreated called");
getView()
.findViewById(R.id.selectContentLauncherButton)
.setOnClickListener(selectContentLauncherButtonClickListener);
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, "ActionSelectorFragment.onViewCreated called");
getView()
.findViewById(R.id.selectContentLauncherButton)
.setOnClickListener(selectContentLauncherButtonClickListener);

getView().findViewById(R.id.disconnectButton).setOnClickListener(disconnectButtonClickListener);
}
getView().findViewById(R.id.disconnectButton).setOnClickListener(disconnectButtonClickListener);
}

/** Interface for notifying the host. */
public interface Callback {
/** Notifies listener to trigger transition on selection of Content Launcher cluster */
void handleContentLauncherLaunchURLSelected();
/** Interface for notifying the host. */
public interface Callback {
/** Notifies listener to trigger transition on selection of Content Launcher cluster */
void handleContentLauncherLaunchURLSelected();

/** Notifies listener to trigger transition on click of the Disconnect button */
void handleDisconnect();
}
/** Notifies listener to trigger transition on click of the Disconnect button */
void handleDisconnect();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,106 +11,126 @@
import androidx.fragment.app.Fragment;
import com.R;
import com.chip.casting.ContentApp;
import com.chip.casting.MatterCallbackHandler;
import com.chip.casting.MatterError;
import com.chip.casting.TvCastingApp;
import com.matter.casting.clusters.MatterClusters;
import com.matter.casting.clusters.MatterCommands;
import com.matter.casting.core.CastingPlayer;
import com.matter.casting.core.Endpoint;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

/** A {@link Fragment} to send Content Launcher LaunchURL command from the TV Casting App. */
public class ContentLauncherLaunchURLExampleFragment extends Fragment {
private static final String TAG = ContentLauncherLaunchURLExampleFragment.class.getSimpleName();
private static final Integer SAMPLE_ENDPOINT_VID = 65521;

private final CastingPlayer selectedCastingPlayer;

private View.OnClickListener launchUrlButtonClickListener;

private static final ContentApp kContentApp = new ContentApp((short) 4, null);

public ContentLauncherLaunchURLExampleFragment(CastingPlayer selectedCastingPlayer) {
this.selectedCastingPlayer = selectedCastingPlayer;
}

/**
* Use this factory method to create a new instance of this fragment using the provided
* parameters.
*
* @param selectedCastingPlayer CastingPlayer that the casting app connected to
* @return A new instance of fragment ContentLauncherLaunchURLExampleFragment.
*/
public static ContentLauncherLaunchURLExampleFragment newInstance(CastingPlayer selectedCastingPlayer) {
return new ContentLauncherLaunchURLExampleFragment(selectedCastingPlayer);
}

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

@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.launchUrlButtonClickListener =
v -> {
List<Endpoint> endpoints = selectedCastingPlayer.getEndpoints();
if(endpoints == null)
{
Log.e(TAG, "No Endpoints found on CastingPlayer");
return;
}

Endpoint endpoint = endpoints.stream().filter(e -> SAMPLE_ENDPOINT_VID.equals(e.getVendorId())).findFirst().get();
if(endpoint == null)
{
Log.e(TAG, "No Endpoint with chosen vendorID: " + SAMPLE_ENDPOINT_VID + " found on CastingPlayer");
return;
}

if(!endpoint.hasCluster(MatterClusters.ContentLauncherCluster.class))
{
Log.e(TAG, "Endpoint with chosen vendorID does not support ContentLauncher cluster");
return;
}

MatterClusters.ContentLauncherCluster cluster = endpoint.getCluster(MatterClusters.ContentLauncherCluster.class);
MatterCommands.ContentLauncherClusterLaunchURLCommand command = cluster.getCommand(MatterCommands.ContentLauncherClusterLaunchURLCommand.class);
if(command == null)
{
Log.e(TAG, "ContentLauncher cluster on Endpoint with chosen vendorID does not support LaunchURL command");
return;
}

MatterCommands.ContentLauncherClusterLaunchURLRequest request = new MatterCommands.ContentLauncherClusterLaunchURLRequest();
request.contentURL = ((EditText) getView().findViewById(R.id.contentUrlEditText)).getText().toString();
request.displayString = ((EditText) getView().findViewById(R.id.contentDisplayStringEditText)).getText().toString();
CompletableFuture<MatterCommands.ContentLauncherClusterResponse> responseFuture = command.invoke(request, 5000);
responseFuture.thenAccept(response -> {
Log.d(TAG, "Command response " + response);
TextView launchUrlStatus = getView().findViewById(R.id.launchUrlStatus);
getActivity().runOnUiThread(() -> launchUrlStatus.setText("Success! Response data: " + response.data));
})
.exceptionally(exc -> {
Log.e(TAG, "Command failure: " + exc.getMessage());
TextView launchUrlStatus = getView().findViewById(R.id.launchUrlStatus);
getActivity().runOnUiThread(() -> launchUrlStatus.setText("Command failure: " + exc.getMessage()));
return null;
});
};

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, "ContentLauncherLaunchURLExampleFragment.onViewCreated called");
getView().findViewById(R.id.launchUrlButton).setOnClickListener(launchUrlButtonClickListener);
}
private static final String TAG = ContentLauncherLaunchURLExampleFragment.class.getSimpleName();
private static final Integer SAMPLE_ENDPOINT_VID = 65521;

private final CastingPlayer selectedCastingPlayer;

private View.OnClickListener launchUrlButtonClickListener;

private static final ContentApp kContentApp = new ContentApp((short) 4, null);

public ContentLauncherLaunchURLExampleFragment(CastingPlayer selectedCastingPlayer) {
this.selectedCastingPlayer = selectedCastingPlayer;
}

/**
* Use this factory method to create a new instance of this fragment using the provided
* parameters.
*
* @param selectedCastingPlayer CastingPlayer that the casting app connected to
* @return A new instance of fragment ContentLauncherLaunchURLExampleFragment.
*/
public static ContentLauncherLaunchURLExampleFragment newInstance(
CastingPlayer selectedCastingPlayer) {
return new ContentLauncherLaunchURLExampleFragment(selectedCastingPlayer);
}

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

@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.launchUrlButtonClickListener =
v -> {
List<Endpoint> endpoints = selectedCastingPlayer.getEndpoints();
if (endpoints == null) {
Log.e(TAG, "No Endpoints found on CastingPlayer");
return;
}

Endpoint endpoint =
endpoints
.stream()
.filter(e -> SAMPLE_ENDPOINT_VID.equals(e.getVendorId()))
.findFirst()
.get();
if (endpoint == null) {
Log.e(
TAG,
"No Endpoint with chosen vendorID: "
+ SAMPLE_ENDPOINT_VID
+ " found on CastingPlayer");
return;
}

if (!endpoint.hasCluster(MatterClusters.ContentLauncherCluster.class)) {
Log.e(TAG, "Endpoint with chosen vendorID does not support ContentLauncher cluster");
return;
}

MatterClusters.ContentLauncherCluster cluster =
endpoint.getCluster(MatterClusters.ContentLauncherCluster.class);
MatterCommands.ContentLauncherClusterLaunchURLCommand command =
cluster.getCommand(MatterCommands.ContentLauncherClusterLaunchURLCommand.class);
if (command == null) {
Log.e(
TAG,
"ContentLauncher cluster on Endpoint with chosen vendorID does not support LaunchURL command");
return;
}

MatterCommands.ContentLauncherClusterLaunchURLRequest request =
new MatterCommands.ContentLauncherClusterLaunchURLRequest();
request.contentURL =
((EditText) getView().findViewById(R.id.contentUrlEditText)).getText().toString();
request.displayString =
((EditText) getView().findViewById(R.id.contentDisplayStringEditText))
.getText()
.toString();
CompletableFuture<MatterCommands.ContentLauncherClusterResponse> responseFuture =
command.invoke(request, 5000);
responseFuture
.thenAccept(
response -> {
Log.d(TAG, "Command response " + response);
TextView launchUrlStatus = getView().findViewById(R.id.launchUrlStatus);
getActivity()
.runOnUiThread(
() ->
launchUrlStatus.setText(
"Success! Response data: " + response.data));
})
.exceptionally(
exc -> {
Log.e(TAG, "Command failure: " + exc.getMessage());
TextView launchUrlStatus = getView().findViewById(R.id.launchUrlStatus);
getActivity()
.runOnUiThread(
() -> launchUrlStatus.setText("Command failure: " + exc.getMessage()));
return null;
});
};

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, "ContentLauncherLaunchURLExampleFragment.onViewCreated called");
getView().findViewById(R.id.launchUrlButton).setOnClickListener(launchUrlButtonClickListener);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ public abstract class MatterCommand<Request, Response> implements Command<Reques
protected long _cppCommand;

@Override
public native CompletableFuture<Response> invoke(
Request request, Integer timedInvokeTimeoutMs);
public native CompletableFuture<Response> invoke(Request request, Integer timedInvokeTimeoutMs);
}
Loading

0 comments on commit d342284

Please sign in to comment.