Skip to content

Commit

Permalink
Addressed comments by sharadb-amazon
Browse files Browse the repository at this point in the history
  • Loading branch information
pgregorr-amazon committed Aug 7, 2024
1 parent 125d6c6 commit 6c34233
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
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;
Expand All @@ -58,11 +56,6 @@ 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(
Expand Down Expand Up @@ -130,13 +123,6 @@ 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(
() -> {
Expand Down Expand Up @@ -369,24 +355,6 @@ 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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@
android:layout_height="wrap_content"
android:text="@string/matter_connection_next_button_text" />

<Button
android:enabled="true"
android:id="@+id/stopDiscoveryButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/matter_stop_discovery_button_text" />

</LinearLayout>

</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,9 @@ CHIP_ERROR CastingPlayerDiscovery::StopDiscovery()
VerifyOrReturnError(mState == DISCOVERY_RUNNING, CHIP_ERROR_INCORRECT_STATE);
ReturnErrorOnFailure(mCommissionableNodeController.StopDiscovery());

// Clear mCastingPlayersInternal of disconnected CastingPlayers
ClearDisconnectedCastingPlayersInternal();
for (const auto & player : mCastingPlayers)
{
mCastingPlayersInternal.push_back(player);
}
// Copy mCastingPlayers to mCastingPlayersInternal
mCastingPlayersInternal = std::vector<memory::Strong<CastingPlayer>>(mCastingPlayers);

// Clear mCastingPlayers of all CastingPlayers
mCastingPlayers.clear();
mState = DISCOVERY_READY;
Expand All @@ -86,22 +83,17 @@ void CastingPlayerDiscovery::ClearDisconnectedCastingPlayersInternal()
ChipLogProgress(Discovery, "CastingPlayerDiscovery::ClearDisconnectedCastingPlayersInternal() mCastingPlayersInternal: %lu",
mCastingPlayersInternal.size());
// Only clear the CastingPlayers in mCastingPlayersInternal with ConnectionState == CASTING_PLAYER_NOT_CONNECTED
ClearDisconnectedCastingPlayers(mCastingPlayersInternal);
}

void CastingPlayerDiscovery::ClearDisconnectedCastingPlayers(std::vector<std::shared_ptr<CastingPlayer>> & castingPlayers)
{
ChipLogProgress(Discovery, "CastingPlayerDiscovery::ClearDisconnectedCastingPlayers() called");
for (auto it = castingPlayers.begin(); it != castingPlayers.end();)
for (auto it = mCastingPlayersInternal.begin(); it != mCastingPlayersInternal.end();)
{
auto & player = *it;
if (player->GetConnectionState() == CASTING_PLAYER_NOT_CONNECTED)
{
ChipLogProgress(Discovery,
"CastingPlayerDiscovery::ClearDisconnectedCastingPlayers() Removing disconnected CastingPlayer: %s "
"with reference count: %lu",
player->GetDeviceName(), player.use_count());
it = castingPlayers.erase(it);
ChipLogProgress(
Discovery,
"CastingPlayerDiscovery::ClearDisconnectedCastingPlayersInternal() Removing disconnected CastingPlayer: %s "
"with reference count: %lu",
player->GetDeviceName(), player.use_count());
it = mCastingPlayersInternal.erase(it);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,6 @@ class CastingPlayerDiscovery
*/
void ClearDisconnectedCastingPlayersInternal();

/**
* @brief Clear CastingPlayers in the specified vector with ConnectionState == CASTING_PLAYER_NOT_CONNECTED
*
* @param castingPlayers vector of CastingPlayers to clear
*/
void ClearDisconnectedCastingPlayers(std::vector<std::shared_ptr<CastingPlayer>> & castingPlayers);

/**
* @brief Clear all CastingPlayers in mCastingPlayersInternal
*/
Expand Down

0 comments on commit 6c34233

Please sign in to comment.