Skip to content

Commit

Permalink
tv-casting-app: Skeleton for simplified Android discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
pgregorr-amazon committed Oct 18, 2023
1 parent f47aed4 commit 97cabdf
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2023 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.matter.casting;

import android.util.Log;
import com.matter.casting.core.CastingPlayer;
import com.matter.casting.core.CastingPlayerDiscovery;
import com.matter.casting.core.CastingPlayerDiscovery.CastingPlayerChangeListener;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class DiscoveryExample {
private static final ScheduledExecutorService executorService =
Executors.newSingleThreadScheduledExecutor();

/**
* Implementation of a CastingPlayerChangeListener used to listen to changes in the discovered
* CastingPlayers
*/
private static final CastingPlayerChangeListener castingPlayerChangeListener =
new CastingPlayerChangeListener() {
private final String TAG = CastingPlayerChangeListener.class.getSimpleName();

@Override
public void onChanged(CastingPlayer castingPlayer) {
Log.i(TAG, "Discovered changes to " + castingPlayer);
// consume changes to the provided castingPlayers
}

@Override
public void onAdded(CastingPlayer castingPlayer) {
Log.i(TAG, "Discovered " + castingPlayer);
// consume discovered castingPlayers
}

@Override
public void onRemoved(CastingPlayer castingPlayer) {
Log.i(TAG, "Removed " + castingPlayer);
// consume castingPlayers removed or lost from the network
}
};

public static void demoDiscovery() {
// Get the singleton instance of the PlayerDiscovery
CastingPlayerDiscovery discoverer = CastingPlayerDiscovery.getInstance();

// Add our castingPlayerChangeListener to listen to changes in the discovered CastingPlayers
discoverer.addCastingPlayerChangeListener(castingPlayerChangeListener);

// Start discovery
discoverer.startDiscovery();

// After some time, stop discovering and remove our castingPlayerChangeListener from the set of
// listeners the CastingPlayerDiscoverer informs
executorService.schedule(
() -> {
discoverer.stopDiscovery();
discoverer.removeCastingPlayerChangeListener(castingPlayerChangeListener);
},
30,
TimeUnit.SECONDS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2023 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.matter.casting.core;

public interface CastingPlayer {
String getId();

String getName();

String getVendorId();

String getProductId();

String getType();

// TODO
// List<Endpoint> getEndpoints();
//
// ConnectionState getConnectionState();
//
// CompletableFuture<Void> connect(long timeout);

// static class ConnectionState extends Observable {
// private boolean connected;
//
// void setConnected(boolean connected) {
// this.connected = connected;
// setChanged();
// notifyObservers(this.connected);
// }
//
// boolean isConnected() {
// return connected;
// }
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2023 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.matter.casting.core;

import java.util.List;

public interface CastingPlayerDiscovery {
static CastingPlayerDiscovery getInstance() {
return null;
}

List<CastingPlayer> getDiscoveredCastingPlayers();

/**
*
*/
void startDiscovery();

void stopDiscovery();

/**
*
* @param listener
*/
void addCastingPlayerChangeListener(CastingPlayerChangeListener listener);

void removeCastingPlayerChangeListener(CastingPlayerChangeListener listener);

interface CastingPlayerChangeListener {
void onChanged(CastingPlayer castingPlayer);

void onAdded(CastingPlayer castingPlayer);

void onRemoved(CastingPlayer castingPlayer);
}
}

0 comments on commit 97cabdf

Please sign in to comment.