From 97cabdfb6c1feaec16b8bc94dc3141714aa4c222 Mon Sep 17 00:00:00 2001 From: Philip Gregor Date: Wed, 18 Oct 2023 15:32:46 -0700 Subject: [PATCH] tv-casting-app: Skeleton for simplified Android discovery --- .../com/matter/casting/DiscoveryExample.java | 79 +++++++++++++++++++ .../matter/casting/core/CastingPlayer.java | 50 ++++++++++++ .../casting/core/CastingPlayerDiscovery.java | 50 ++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/DiscoveryExample.java create mode 100644 examples/tv-casting-app/android/App/app/src/main/jni/com/matter/casting/core/CastingPlayer.java create mode 100644 examples/tv-casting-app/android/App/app/src/main/jni/com/matter/casting/core/CastingPlayerDiscovery.java diff --git a/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/DiscoveryExample.java b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/DiscoveryExample.java new file mode 100644 index 00000000000000..f07ffea637418a --- /dev/null +++ b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/DiscoveryExample.java @@ -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); + } +} \ No newline at end of file diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/com/matter/casting/core/CastingPlayer.java b/examples/tv-casting-app/android/App/app/src/main/jni/com/matter/casting/core/CastingPlayer.java new file mode 100644 index 00000000000000..7a49bf3482c49a --- /dev/null +++ b/examples/tv-casting-app/android/App/app/src/main/jni/com/matter/casting/core/CastingPlayer.java @@ -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 getEndpoints(); +// +// ConnectionState getConnectionState(); +// +// CompletableFuture 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; +// } +// } +} \ No newline at end of file diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/com/matter/casting/core/CastingPlayerDiscovery.java b/examples/tv-casting-app/android/App/app/src/main/jni/com/matter/casting/core/CastingPlayerDiscovery.java new file mode 100644 index 00000000000000..0684bc37199c93 --- /dev/null +++ b/examples/tv-casting-app/android/App/app/src/main/jni/com/matter/casting/core/CastingPlayerDiscovery.java @@ -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 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); + } +} \ No newline at end of file