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

Extending Media attribute subscriptions to Android tv-casting-app/lib #22859

Merged
merged 1 commit into from
Sep 28, 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 @@ -9,6 +9,7 @@
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.chip.casting.MatterCallbackHandler;
import com.chip.casting.MatterError;
import com.chip.casting.TvCastingApp;
import com.chip.casting.dnssd.DiscoveredNodeData;
import com.chip.casting.util.GlobalCastingConstants;
Expand Down Expand Up @@ -53,9 +54,9 @@ public View onCreateView(
GlobalCastingConstants.CommissioningWindowDurationSecs,
new MatterCallbackHandler() {
@Override
public void handle(Status status) {
Log.d(TAG, "handle() called on CommissioningComplete event with " + status);
if (status.isSuccess()) {
public void handle(MatterError error) {
Log.d(TAG, "handle() called on CommissioningComplete event with " + error);
if (error.isNoError()) {
callback.handleCommissioningComplete();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.chip.casting.MatterCallbackHandler;
import com.chip.casting.MatterError;
import com.chip.casting.TvCastingApp;

/** A {@link Fragment} to send Content Launcher commands from the TV Casting App. */
Expand Down Expand Up @@ -55,10 +56,10 @@ public void onClick(View v) {
contentDisplayString.getText().toString(),
new MatterCallbackHandler() {
@Override
public void handle(Status status) {
Log.d(TAG, "handle() called on LaunchURLResponse with success " + status);
public void handle(MatterError error) {
Log.d(TAG, "handle() called on LaunchURLResponse with " + error);
TextView launchUrlStatus = getView().findViewById(R.id.launchUrlStatus);
launchUrlStatus.setText(status.isSuccess() ? "Success!" : "Failure!");
launchUrlStatus.setText(error.isNoError() ? "Success!" : "Failure!");
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import com.chip.casting.util.GlobalCastingConstants;

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

private static final String TAG = MainActivity.class.getSimpleName();

Expand Down Expand Up @@ -49,9 +51,19 @@ public void handleCommissioningButtonClicked(DiscoveredNodeData commissioner) {

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

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

@Override
public void handleMediaPlaybackSelected() {
showFragment(MediaPlaybackFragment.newInstance(tvCastingApp));
}

/**
* The order is important, must first new TvCastingApp to load dynamic library, then
* AndroidChipPlatform to prepare platform, then start ChipAppServer, then call init on
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
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.TextView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.chip.casting.FailureCallback;
import com.chip.casting.MatterError;
import com.chip.casting.MediaPlaybackTypes;
import com.chip.casting.SubscriptionEstablishedCallback;
import com.chip.casting.SuccessCallback;
import com.chip.casting.TvCastingApp;

/** A {@link Fragment} for the Media Playback cluster */
public class MediaPlaybackFragment extends Fragment {
private static final String TAG = MediaPlaybackFragment.class.getSimpleName();

private final TvCastingApp tvCastingApp;

private View.OnClickListener subscribeToCurrentStateButtonClickListener;

public MediaPlaybackFragment(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 MediaPlaybackFragment.
*/
public static MediaPlaybackFragment newInstance(TvCastingApp tvCastingApp) {
return new MediaPlaybackFragment(tvCastingApp);
}

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

@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.subscribeToCurrentStateButtonClickListener =
new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "SubscribeToCurrentStateButtonClickListener called");
TextView minInterval = getView().findViewById(R.id.minIntervalEditText);
TextView maxInterval = getView().findViewById(R.id.maxIntervalEditText);
TextView subscriptionStatus =
getView().findViewById(R.id.currentStateSubscriptionEstablishedStatus);
TextView currentStateValue = getView().findViewById(R.id.currentStateValue);

SuccessCallback<MediaPlaybackTypes.PlaybackStateEnum> successCallback =
playbackStateEnum -> {
Log.d(
TAG,
"handle() called on SuccessCallback<MediaPlaybackResponseTypes.PlaybackStateEnum> with "
+ playbackStateEnum);
getActivity()
.runOnUiThread(
new Runnable() {
@Override
public void run() {
if (playbackStateEnum != null) {
currentStateValue.setText(playbackStateEnum.toString());
}
}
});
};

FailureCallback failureCallback =
new FailureCallback() {
@Override
public void handle(MatterError matterError) {
Log.d(TAG, "handle() called on FailureCallback with " + matterError);
getActivity()
.runOnUiThread(
new Runnable() {
@Override
public void run() {
currentStateValue.setText("Error!");
}
});
}
};

SubscriptionEstablishedCallback subscriptionEstablishedCallback =
(SubscriptionEstablishedCallback)
() -> {
Log.d(TAG, "handle() called on SubscriptionEstablishedCallback");
getActivity()
.runOnUiThread(
new Runnable() {
@Override
public void run() {
subscriptionStatus.setText("Subscription established!");
}
});
};

boolean retVal =
tvCastingApp.mediaPlayback_subscribeToCurrentState(
successCallback,
failureCallback,
Integer.parseInt(minInterval.getText().toString()),
Integer.parseInt(maxInterval.getText().toString()),
subscriptionEstablishedCallback);
Log.d(TAG, "tvCastingApp.mediaPlayback_subscribeToCurrentState returned " + retVal);
if (retVal == false) {
subscriptionStatus.setText("Subscribe call failed!");
}
}
};

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

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, "MediaPlaybackFragment.onViewCreated called");
getView()
.findViewById(R.id.subscribeToCurrentStateButton)
.setOnClickListener(subscribeToCurrentStateButtonClickListener);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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 androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

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

private View.OnClickListener selectContentLauncherButtonClickListener;
private View.OnClickListener selectMediaPlaybackButtonClickListener;

/**
* Use this factory method to create a new instance of this fragment using the provided
* parameters.
*
* @return A new instance of fragment SelectActionFragment.
*/
public static SelectClusterFragment newInstance() {
return new SelectClusterFragment();
}

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

@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
SelectClusterFragment.Callback callback = (SelectClusterFragment.Callback) this.getActivity();
this.selectContentLauncherButtonClickListener =
new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "handle() called on selectContentLauncherButtonClickListener");
callback.handleContentLauncherSelected();
}
};

this.selectMediaPlaybackButtonClickListener =
new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "handle() called on selectMediaPlaybackButtonClickListener");
callback.handleMediaPlaybackSelected();
}
};

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

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

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

/** Notifies listener to trigger transition on selection of Media Playback cluster */
void handleMediaPlaybackSelected();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.
*
*/
package com.chip.casting;

import java.util.ArrayList;
import java.util.Optional;

public class ContentLauncherTypes {
public static class AdditionalInfo {
public String name;
public String value;

public AdditionalInfo(String name, String value) {
this.name = name;
this.value = value;
}

@Override
public String toString() {
StringBuilder output = new StringBuilder();
output.append("AdditionalInfo {\n");
output.append("\tname: ");
output.append(name);
output.append("\n");
output.append("\tvalue: ");
output.append(value);
output.append("\n");
output.append("}\n");
return output.toString();
}
}

public static class Parameter {
public Integer type;
public String value;
public Optional<ArrayList<AdditionalInfo>> externalIDList;

public Parameter(
Integer type, String value, Optional<ArrayList<AdditionalInfo>> externalIDList) {
this.type = type;
this.value = value;
this.externalIDList = externalIDList;
}

@Override
public String toString() {
StringBuilder output = new StringBuilder();
output.append("Parameter {\n");
output.append("\ttype: ");
output.append(type);
output.append("\n");
output.append("\tvalue: ");
output.append(value);
output.append("\n");
output.append("\texternalIDList: ");
output.append(externalIDList);
output.append("\n");
output.append("}\n");
return output.toString();
}
}

public static class ContentSearch {
public ArrayList<Parameter> parameterList;

public ContentSearch(ArrayList<Parameter> parameterList) {
this.parameterList = parameterList;
}

@Override
public String toString() {
StringBuilder output = new StringBuilder();
output.append("ContentSearch {\n");
output.append("\tparameterList: ");
output.append(parameterList);
output.append("\n");
output.append("}\n");
return output.toString();
}
}
}
Loading