diff --git a/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/DiscoveryExampleFragment.java b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/DiscoveryExampleFragment.java index b847e17f5b7026..50b47c348d6f6b 100644 --- a/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/DiscoveryExampleFragment.java +++ b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/DiscoveryExampleFragment.java @@ -38,9 +38,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; public class DiscoveryExampleFragment extends Fragment { private static final String TAG = DiscoveryExampleFragment.class.getSimpleName(); @@ -48,9 +45,7 @@ public class DiscoveryExampleFragment extends Fragment { private static final Long DISCOVERY_TARGET_DEVICE_TYPE = 35L; private static final int DISCOVERY_RUNTIME_SEC = 15; private TextView matterDiscoveryMessageTextView; - private static final ScheduledExecutorService executorService = - Executors.newSingleThreadScheduledExecutor(); - private ScheduledFuture scheduledFutureTask; + private TextView matterDiscoveryErrorMessageTextView; private static final List castingPlayerList = new ArrayList<>(); private static ArrayAdapter arrayAdapter; @@ -164,15 +159,17 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { matterDiscoveryMessageTextView.setText( getString(R.string.matter_discovery_message_initializing_text)); + matterDiscoveryErrorMessageTextView = + getActivity().findViewById(R.id.matterDiscoveryErrorTextView); + matterDiscoveryErrorMessageTextView.setText( + getString(R.string.matter_discovery_error_message_initial)); + arrayAdapter = new CastingPlayerArrayAdapter(getActivity(), castingPlayerList); final ListView list = getActivity().findViewById(R.id.castingPlayerList); list.setAdapter(arrayAdapter); Log.d(TAG, "onViewCreated() creating callbacks"); - // TODO: In following PRs. Enable startDiscoveryButton and stopDiscoveryButton when - // stopDiscovery is implemented in the core Matter SDK DNS-SD API. Enable in - // fragment_matter_discovery_example.xml Button startDiscoveryButton = getView().findViewById(R.id.startDiscoveryButton); startDiscoveryButton.setOnClickListener( v -> { @@ -188,8 +185,6 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { v -> { Log.i(TAG, "onViewCreated() stopDiscoveryButton button clicked. Calling stopDiscovery()"); stopDiscovery(); - Log.i(TAG, "onViewCreated() stopDiscoveryButton button clicked. Canceling future task"); - scheduledFutureTask.cancel(true); }); Button clearDiscoveryResultsButton = getView().findViewById(R.id.clearDiscoveryResultsButton); @@ -198,6 +193,7 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { Log.i( TAG, "onViewCreated() clearDiscoveryResultsButton button clicked. Clearing results"); arrayAdapter.clear(); + matterDiscoveryErrorMessageTextView.setText(getString(R.string.matter_discovery_error_message_initial)); }); } @@ -219,11 +215,7 @@ public void onResume() { public void onPause() { super.onPause(); Log.i(TAG, "onPause() called"); - // stopDiscovery(); - // Don't crash the app - if (scheduledFutureTask != null) { - scheduledFutureTask.cancel(true); - } + stopDiscovery(); } /** Interface for notifying the host. */ @@ -235,6 +227,7 @@ public interface Callback { private boolean startDiscovery() { Log.i(TAG, "startDiscovery() called"); + matterDiscoveryErrorMessageTextView.setText(getString(R.string.matter_discovery_error_message_initial)); arrayAdapter.clear(); @@ -244,6 +237,7 @@ private boolean startDiscovery() { matterCastingPlayerDiscovery.addCastingPlayerChangeListener(castingPlayerChangeListener); if (err.hasError()) { Log.e(TAG, "startDiscovery() addCastingPlayerChangeListener() called, err Add: " + err); + matterDiscoveryErrorMessageTextView.setText(getString(R.string.matter_discovery_error_message_stop_before_starting) + err); return false; } // Start discovery @@ -251,6 +245,7 @@ private boolean startDiscovery() { err = matterCastingPlayerDiscovery.startDiscovery(DISCOVERY_TARGET_DEVICE_TYPE); if (err.hasError()) { Log.e(TAG, "startDiscovery() startDiscovery() called, err Start: " + err); + matterDiscoveryErrorMessageTextView.setText(getString(R.string.matter_discovery_error_message_start_error) + err); return false; } @@ -259,28 +254,12 @@ private boolean startDiscovery() { matterDiscoveryMessageTextView.setText( getString(R.string.matter_discovery_message_discovering_text)); - // TODO: In following PRs. Enable this to auto-stop discovery after stopDiscovery is - // implemented in the core Matter SDK DNS-SD API. - // Schedule a service to stop discovery and remove the CastingPlayerChangeListener - // Safe to call if discovery is not running - // scheduledFutureTask = - // executorService.schedule( - // () -> { - // Log.i( - // TAG, - // "startDiscovery() executorService " - // + DISCOVERY_RUNTIME_SEC - // + " seconds timer expired. Auto-calling stopDiscovery()"); - // stopDiscovery(); - // }, - // DISCOVERY_RUNTIME_SEC, - // TimeUnit.SECONDS); - return true; } private void stopDiscovery() { Log.i(TAG, "stopDiscovery() called"); + matterDiscoveryErrorMessageTextView.setText(getString(R.string.matter_discovery_error_message_initial)); // Stop discovery MatterError err = matterCastingPlayerDiscovery.stopDiscovery(); @@ -288,8 +267,8 @@ private void stopDiscovery() { Log.e( TAG, "stopDiscovery() MatterCastingPlayerDiscovery.stopDiscovery() called, err Stop: " + err); + matterDiscoveryErrorMessageTextView.setText(getString(R.string.matter_discovery_error_message_stop_error) + err); } else { - // TODO: In following PRs. Implement stop discovery in the Android core API. Log.d(TAG, "stopDiscovery() MatterCastingPlayerDiscovery.stopDiscovery() success"); } @@ -309,6 +288,7 @@ private void stopDiscovery() { TAG, "stopDiscovery() matterCastingPlayerDiscovery.removeCastingPlayerChangeListener() called, err Remove: " + err); + matterDiscoveryErrorMessageTextView.setText(getString(R.string.matter_discovery_error_message_stop_error) + err); } } } diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/CastingPlayerDiscovery-JNI.cpp b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/CastingPlayerDiscovery-JNI.cpp index 5981ab80bbd52e..c42a6d05ff0cb0 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/CastingPlayerDiscovery-JNI.cpp +++ b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/core/CastingPlayerDiscovery-JNI.cpp @@ -246,6 +246,13 @@ JNI_METHOD(jobject, removeCastingPlayerChangeListener)(JNIEnv * env, jobject, jo return support::convertMatterErrorFromCppToJava(CHIP_NO_ERROR); } + else if (DiscoveryDelegateImpl::GetInstance()->castingPlayerChangeListenerJavaObject.ObjectRef() == nullptr) + { + ChipLogError(AppServer, + "CastingPlayerDiscovery-JNI::removeCastingPlayerChangeListener() Warning: Cannot remove listener. No " + "listener was added"); + return support::convertMatterErrorFromCppToJava(CHIP_NO_ERROR); + } else { ChipLogError(AppServer, diff --git a/examples/tv-casting-app/android/App/app/src/main/res/layout/fragment_matter_discovery_example.xml b/examples/tv-casting-app/android/App/app/src/main/res/layout/fragment_matter_discovery_example.xml index 1c3eb13ce6c486..3f3e3d987a5644 100644 --- a/examples/tv-casting-app/android/App/app/src/main/res/layout/fragment_matter_discovery_example.xml +++ b/examples/tv-casting-app/android/App/app/src/main/res/layout/fragment_matter_discovery_example.xml @@ -1,5 +1,5 @@ -