diff --git a/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ConnectionExampleFragment.java b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ConnectionExampleFragment.java index b61b63f3e0785e..38616434cc6a03 100644 --- a/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ConnectionExampleFragment.java +++ b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ConnectionExampleFragment.java @@ -31,6 +31,8 @@ import androidx.fragment.app.Fragment; import com.R; import com.matter.casting.core.CastingPlayer; +import com.matter.casting.core.CastingPlayerDiscovery; +import com.matter.casting.core.MatterCastingPlayerDiscovery; import com.matter.casting.support.CommissionerDeclaration; import com.matter.casting.support.ConnectionCallbacks; import com.matter.casting.support.IdentificationDeclarationOptions; @@ -56,6 +58,10 @@ public class ConnectionExampleFragment extends Fragment { private TextView connectionFragmentStatusTextView; private Button connectionFragmentNextButton; + // Get a singleton instance of the MatterCastingPlayerDiscovery. Which can be used to call stopDiscovery() during connection. + static final CastingPlayerDiscovery matterCastingPlayerDiscovery = + MatterCastingPlayerDiscovery.getInstance(); + public ConnectionExampleFragment( CastingPlayer targetCastingPlayer, boolean useCommissionerGeneratedPasscode) { Log.i( @@ -123,6 +129,13 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { callback.handleConnectionComplete(targetCastingPlayer, useCommissionerGeneratedPasscode); }); + Button stopDiscoveryButton = getView().findViewById(R.id.stopDiscoveryButton); + stopDiscoveryButton.setOnClickListener( + v -> { + Log.i(TAG, "onViewCreated() stopDiscoveryButton button clicked. Calling stopDiscovery()"); + stopDiscovery(); + }); + Executors.newSingleThreadExecutor() .submit( () -> { @@ -160,13 +173,17 @@ public void handle(Void v) { Log.i( TAG, "Successfully connected to CastingPlayer with deviceId: " - + targetCastingPlayer.getDeviceId()); + + targetCastingPlayer.getDeviceId() + + ", useCommissionerGeneratedPasscode: " + + useCommissionerGeneratedPasscode); getActivity() .runOnUiThread( () -> { connectionFragmentStatusTextView.setText( "Successfully connected to Casting Player with device name: " + targetCastingPlayer.getDeviceName() + + "\n\nUsed CastingPlayer Passcode: " + + useCommissionerGeneratedPasscode + "\n\n"); connectionFragmentNextButton.setEnabled(true); }); @@ -351,6 +368,19 @@ public void onClick(DialogInterface dialog, int which) { .setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } + private void stopDiscovery() { + Log.i(TAG, "ConnectionExampleFragment stopDiscovery() called, calling MatterCastingPlayerDiscovery.stopDiscovery()"); + + MatterError err = matterCastingPlayerDiscovery.stopDiscovery(); + if (err.hasError()) { + Log.e( + TAG, + "ConnectionExampleFragment stopDiscovery() MatterCastingPlayerDiscovery.stopDiscovery() called, err Stop: " + err); + } else { + Log.d(TAG, "ConnectionExampleFragment stopDiscovery() MatterCastingPlayerDiscovery.stopDiscovery() success"); + } + } + /** Interface for notifying the host. */ public interface Callback { /** Notifies listener to trigger transition on completion of connection */ 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 3dca2320b8924c..3bb791375f8bfe 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 @@ -67,7 +67,7 @@ public class DiscoveryExampleFragment extends Fragment { public void onAdded(CastingPlayer castingPlayer) { Log.i( TAG, - "onAdded() Discovered CastingPlayer deviceId: " + castingPlayer.getDeviceId()); + "DiscoveryExampleFragment onAdded() Discovered CastingPlayer deviceId: " + castingPlayer.getDeviceId()); // Display CastingPlayer info on the screen new Handler(Looper.getMainLooper()) .post( @@ -80,7 +80,7 @@ public void onAdded(CastingPlayer castingPlayer) { public void onChanged(CastingPlayer castingPlayer) { Log.i( TAG, - "onChanged() Discovered changes to CastingPlayer with deviceId: " + "DiscoveryExampleFragment onChanged() Discovered changes to CastingPlayer with deviceId: " + castingPlayer.getDeviceId()); // Update the CastingPlayer on the screen new Handler(Looper.getMainLooper()) @@ -107,7 +107,7 @@ public void onChanged(CastingPlayer castingPlayer) { public void onRemoved(CastingPlayer castingPlayer) { Log.i( TAG, - "onRemoved() Removed CastingPlayer with deviceId: " + "DiscoveryExampleFragment onRemoved() Removed CastingPlayer with deviceId: " + castingPlayer.getDeviceId()); // Remove CastingPlayer from the screen new Handler(Looper.getMainLooper()) @@ -215,7 +215,9 @@ public void onResume() { @Override public void onPause() { super.onPause(); - Log.i(TAG, "onPause() called"); + Log.i(TAG, "DiscoveryExampleFragment onPause() called, calling stopDiscovery()"); + // Stop discovery when leaving the fragment, for example, while displaying the ConnectionExampleFragment. + stopDiscovery(); } /** Interface for notifying the host. */ @@ -261,7 +263,7 @@ private boolean startDiscovery() { } private void stopDiscovery() { - Log.i(TAG, "stopDiscovery() called"); + Log.i(TAG, "DiscoveryExampleFragment stopDiscovery() called"); matterDiscoveryErrorMessageTextView.setText( getString(R.string.matter_discovery_error_message_initial)); diff --git a/examples/tv-casting-app/android/App/app/src/main/res/layout/custom_passcode_dialog.xml b/examples/tv-casting-app/android/App/app/src/main/res/layout/custom_passcode_dialog.xml index 1b868e06b71f29..c91fa4ad404089 100644 --- a/examples/tv-casting-app/android/App/app/src/main/res/layout/custom_passcode_dialog.xml +++ b/examples/tv-casting-app/android/App/app/src/main/res/layout/custom_passcode_dialog.xml @@ -1,5 +1,6 @@ @@ -11,7 +12,6 @@ android:textStyle="bold" android:textSize="18sp" android:padding="16dp" - android:textColor="@android:color/white" android:gravity="center" android:text="@string/matter_connection_input_dialog_title" /> @@ -24,7 +24,6 @@ android:paddingEnd="16dp" android:paddingTop="8dp" android:paddingBottom="8dp" - android:textColor="@android:color/white" android:text="@string/matter_connection_input_dialog_instructions" /> - - -