Skip to content

Commit

Permalink
asd
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Apr 16, 2024
1 parent 5ec82a3 commit 7f3f256
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
import com.R;
import com.matter.casting.core.CastingPlayer;
import com.matter.casting.core.Endpoint;
import com.matter.casting.core.MatterEndpoint;
import com.matter.casting.support.MatterCallback;
import com.matter.casting.support.MatterError;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -79,41 +76,29 @@ public View onCreateView(
}

// TODO: complete command invocation API implementation
((MatterEndpoint) endpoint)
.getDeviceProxy(
new MatterCallback<Long>() {
@Override
public void handle(Long device) {
Log.d(TAG, "getDeviceProxy success. Device: " + device);
ChipClusters.ContentLauncherCluster cluster =
endpoint.getCluster(ChipClusters.ContentLauncherCluster.class);
if (cluster == null) {
Log.e(
TAG,
"Could not get ContentLauncherCluster for endpoint with ID: " + endpoint.getId());
}

if (device != null) {
ChipClusters.ContentLauncherCluster cluster =
new ChipClusters.ContentLauncherCluster(device, endpoint.getId());
Log.d(TAG, "Content launcher cluster created " + cluster);
cluster.launchURL(
new ChipClusters.ContentLauncherCluster.LauncherResponseCallback() {
@Override
public void onSuccess(Integer status, Optional<String> data) {
Log.d(TAG, "Content launcher success " + status + data);
}
cluster.launchURL(
new ChipClusters.ContentLauncherCluster.LauncherResponseCallback() {
@Override
public void onSuccess(Integer status, Optional<String> data) {
Log.d(TAG, "Content launcher success " + status + data);
}

@Override
public void onError(Exception error) {
Log.e(TAG, "Content launcher failure " + error);
}
},
"my test url",
Optional.of("my display str"),
Optional.empty());
}
}
},
new MatterCallback<MatterError>() {
@Override
public void handle(MatterError err) {
Log.e(TAG, "getDeviceProxy err" + err);
}
});
@Override
public void onError(Exception error) {
Log.e(TAG, "Content launcher failure " + error);
}
},
"my test url",
Optional.of("my display str"),
Optional.empty());
};
return inflater.inflate(R.layout.fragment_content_launcher, container, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

package com.matter.casting.core;

import chip.devicecontroller.ChipClusters;
import com.matter.casting.support.DeviceTypeStruct;
import java.util.List;

/** This represents an Endpoint on a CastingPlayer e.g. a Speaker or a Matter Content App */
public interface Endpoint {
int getId();

Expand All @@ -29,5 +31,9 @@ public interface Endpoint {

List<DeviceTypeStruct> getDeviceTypeList();

/** Get an instance of a cluster based on its Class */
<T extends ChipClusters.BaseChipCluster> T getCluster(Class<T> clusterClass);

/** Get the CastingPlayer that this Endpoint is a part of. */
CastingPlayer getCastingPlayer();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@
*/
package com.matter.casting.core;

import android.util.Log;
import chip.devicecontroller.ChipClusters;
import com.matter.casting.support.DeviceTypeStruct;
import com.matter.casting.support.MatterCallback;
import com.matter.casting.support.MatterError;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class MatterEndpoint implements Endpoint {
private static final String TAG = MatterEndpoint.class.getSimpleName();
private static final long MAX_WAIT_FOR_DEVICE_PROXY_SEC = 5000;
protected long _cppEndpoint;

@Override
Expand All @@ -39,10 +48,28 @@ public class MatterEndpoint implements Endpoint {
public native List<DeviceTypeStruct> getDeviceTypeList();

@Override
public native CastingPlayer getCastingPlayer();
public <T extends ChipClusters.BaseChipCluster> T getCluster(Class<T> clusterClass) {
try {
Constructor<T> constructor = clusterClass.getDeclaredConstructor(long.class, int.class);
Long deviceProxy = getDeviceProxy();
if (deviceProxy == null) {
Log.e(TAG, "Could not get DeviceProxy while constructing cluster object");
return null;
}
return constructor.newInstance(deviceProxy, getId());
} catch (InstantiationException
| IllegalAccessException
| InvocationTargetException
| NoSuchMethodException e) {
Log.e(
TAG,
"Could not create cluster object for " + clusterClass.getSimpleName() + " exc: " + e);
return null;
}
}

public native void getDeviceProxy(
MatterCallback<Long> successCallback, MatterCallback<MatterError> failureCallback);
@Override
public native CastingPlayer getCastingPlayer();

@Override
public String toString() {
Expand All @@ -61,4 +88,32 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(getId());
}

private Long getDeviceProxy() {
CompletableFuture<Long> deviceProxyFuture = new CompletableFuture<>();
getDeviceProxy(
new MatterCallback<Long>() {
@Override
public void handle(Long deviceProxy) {
deviceProxyFuture.complete(deviceProxy);
}
},
new MatterCallback<MatterError>() {
@Override
public void handle(MatterError response) {
deviceProxyFuture.completeExceptionally(
new RuntimeException("Failed on getDeviceProxy: " + response));
}
});

try {
return deviceProxyFuture.get(MAX_WAIT_FOR_DEVICE_PROXY_MS, TimeUnit.MILLISECONDS);
} catch (ExecutionException | InterruptedException | TimeoutException e) {
Log.e(TAG, "Exception while waiting on getDeviceProxy future: " + e);
return null;
}
}

protected native void getDeviceProxy(
MatterCallback<Long> successCallback, MatterCallback<MatterError> failureCallback);
}

0 comments on commit 7f3f256

Please sign in to comment.